replicator is a JavaScript library designed for advanced object serialization, addressing limitations of standard JSON.stringify. It currently stands at version 1.0.5, with an active release cadence indicated by recent patch versions. A key differentiator is its ability to correctly handle circular references within objects, preventing common serialization errors. Beyond standard JSON-serializable types, replicator offers built-in support for a wide array of JavaScript objects including `undefined`, `NaN`, `Date`, `RegExp`, `Error`, `Map`, `Set`, `ArrayBuffer`, and various Typed Arrays. This makes it suitable for complex data structures that would otherwise require manual transformation before serialization. Furthermore, the library is highly extensible, allowing developers to define custom type transforms to serialize and deserialize any bespoke object types. It is also agnostic to the underlying serialization format, enabling its use with JSON, BSON, Protobuf, or other encoders. This flexibility allows it to be adapted for diverse application needs, from IPC in Node.js to storing complex state in the browser.
npm install replicatorVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic encoding and decoding of an object with various built-in types, including Set, RegExp, Date, undefined, NaN, Error, and a circular reference.
Check the decoded object structure carefully, especially when interoperating across different environments or older JavaScript runtimes. Implement custom transforms if specific fallback behavior is undesirable or to ensure consistent behavior.
For CommonJS (e.g., Node.js without `"type": "module"` in `package.json`), use `const Replicator = require('replicator');`. For ES Modules (e.g., modern Node.js with `"type": "module"` or browser bundlers), use `import Replicator from 'replicator';`.Call `replicator.addTransforms(myTransforms)` on both the encoder and decoder instances: `const encoder = new Replicator(); encoder.addTransforms(myCustomTransforms);` and `const decoder = new Replicator(); decoder.addTransforms(myCustomTransforms);`.
Ensure `Replicator` is imported correctly based on your module system. For CommonJS: `const Replicator = require('replicator');`. For ESM: `import Replicator from 'replicator';`.When running in Node.js, avoid custom transforms that directly interact with browser DOM APIs. If necessary, provide global mocks for these types or use conditional logic based on the environment (`typeof window !== 'undefined'`).
Ensure you are creating an instance: `const replicator = new Replicator();` before calling its methods like `encode` or `decode`.
No dependency data recorded yet.