Registry / messaging / sc-formatter

sc-formatter

JSON →
library4.0.0jsnpmunverified

Formats used to serialize and deserialize SocketCluster messages. This package provides simple encode/decode functions for SocketCluster's internal message protocol. Current stable version is 4.0.0, released as part of the SocketCluster ecosystem. It is tightly coupled with SocketCluster and should not be used standalone. Release cadence follows SocketCluster's major updates. Key differentiator: designed specifically for SocketCluster's message format, not a general-purpose serializer.

npm install sc-formatter
INSTALL
IMPORT
SIG · SC-FORMATTER
S
sc-formatter
messagingjavascriptv4.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

scFormatter
import scFormatter from 'sc-formatter'
const scFormatter = require('sc-formatter')
ESM only since v4. Use named import { scFormatter }? Actually default export.
format
import { format } from 'sc-formatter'
import format from 'sc-formatter'
Named export format, not default. Check exports.
parse
import { parse } from 'sc-formatter'
const { parse } = require('sc-formatter')
ESM only since v4, no CommonJS.

Shows how to serialize and deserialize a SocketCluster message using format and parse functions.

import { format, parse } from 'sc-formatter'; const message = { event: 'chat', data: 'hello' }; const serialized = format(message); // '1#{"event":"chat","data":"hello"}' const deserialized = parse(serialized); // { event: 'chat', data: 'hello' } console.log(deserialized);
Debug
Known issues
breakingsc-formatter v4 is ESM-only. CommonJS require() will not work.
fix
Use ESM imports (import { format } from 'sc-formatter'). If you must use CommonJS, stay on v3.
affects: >=4.0.0
deprecatedv3 and earlier used Buffer for binary data. v4 uses Uint8Array.
fix
Update code expecting Buffer to use Uint8Array or use Buffer.from() for compatibility.
affects: >=4.0.0
gotchaparse() can throw if input is not a valid SocketCluster message format. Not all JSON strings are valid.
fix
Wrap parse() in try/catch or validate input format beforehand.
affects: *
breakingThe default export in v3 was a function, but v4 has named exports only.
fix
Use named imports: import { format, parse } from 'sc-formatter'.
affects: >=4.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Using require() with an ESM-only module (v4).
fix
Change to ESM import or downgrade to v3.
TypeError: scFormatter is not a function
Assuming default export in v4, but v4 does not have a default export.
fix
Use named imports: import { format } from 'sc-formatter'.
SyntaxError: Unexpected token '#'
Trying to parse a message that contains '#' prefix with JSON.parse instead of sc-formatter's parse.
fix
Use sc-formatter's parse() method, not JSON.parse.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
sc-formatter — npm install sc-formatter · libregistry