Package exports
| Entry | Purpose |
|---|---|
import Message from 'js-message' | Default and named exports from the canonical native module. |
require('js-message') | The same synchronous Message.js constructor on Node 22.13+. |
js-message/index.js | Compatibility module that re-exports the canonical constructor. |
js-message/Message.js | Supported direct module path; resolves to the same class. |
js-message/js-message-vanilla.js | Side-effect entry that assigns globalThis.Message. |
js-message/package.json | Package metadata for tools that explicitly need it. |
import Message, { Message as NamedMessage } from 'js-message';
Message === NamedMessage; // trueconst CommonMessage = require('js-message');
CommonMessage === CommonMessage.Message; // trueConstructor and fields
new Message([input])
Creates an envelope with type === '' and an independent data === {}. When supplied, input is passed through load().
- input
- Optional JSON string or message object.
- returns
- A new
Messageinstance.
message.type
Mutable, enumerable message or event discriminator. Direct assignment is intentionally unrestricted.
- default
- Empty string.
- wire key
type
message.data
Mutable, enumerable payload. Direct assignment follows native JSON behavior, including errors for cycles and BigInt.
- default
- Fresh empty object.
- wire key
data
Serialization and loading
message.JSON
Returns JSON.stringify({ type: message.type, data: message.data }). Instance extras are excluded.
- returns
- JSON string.
- throws
- Native
TypeErrorfor unsupported values such as cycles orBigInt.
message.toJSON()
Returns a new plain { type, data } object. Native JSON.stringify(message) calls this method automatically.
- returns
- Plain object with exactly two keys.
message.load(input)
Parses JSON or JSON-coercible text, or accepts an existing object, then assigns its available type and data fields. Either field may be absent; extra input keys are ignored by serialization.
- returns
undefined, preserving the legacy contract.- failure
- Malformed or uncoercible input mutates the instance into an error envelope; complete JSON serialization still requires a JSON-safe preserved response.
Error envelope
{
type: 'error',
data: {
message: 'Invalid JSON response format',
err: SyntaxError | TypeError,
response: originalInput
}
}The live err remains an Error instance. Its JSON form includes stable name and message fields instead of collapsing to {}. The original response stays by reference, so cycles or BigInt still make native serialization throw.