Skip to content

Implements structured clone in Node.js, from a live object or serialized/deserialized from a buffer.

License

Notifications You must be signed in to change notification settings

tcr/structured-clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

structured-clone

Implements the structured clone algorithm. Clone JSON types, RegExp, Buffers, and Dates, returning a cloned object or a buffer that can be deserialized into a structured clone

npm install structured-clone
var clone = require('structured-clone');
  • clone(obj) → Object
  • clone.serialize(obj) → Buffer
  • clone.deserialize(buf) → Object

Encoded format

The encoded format takes this form:

  1. A UTF-8 encoded JSON string.
  2. A null byte.
  3. A binary blob that is the concatenation of all binary buffers in the original object. There are no delimiters in this buffer, indexes are represented in the JSON value (see below).

Dates, regexps, buffers, and cycles are encoded in a particular way to be decoded properly:

  • Dates are encoded as the string '\x10d' followed by the JSON-stringified encoding of the date.
  • Regexps are encoded as the string '\x10r{flags},{regexp source}'.
  • Buffers are encoded as the string '\x10b{start},{length}'. All buffers in the encoded value are concatenated and placed in the binary blob. The start and length parameters indicate the indexes the slice of the buffer was encoded in.
  • Lastly, string that begin with '\x10' are encoded as '\x10s{string}' to properly escape them.

Optimizations: If only a JSON value is being encoded (i.e. no Buffer values included), the null byte can be omitted, thus making the encoded format equivalent to a JSON-encoded string. If only a buffer is being encoded, it is equivalent to a null byte followed by the buffer (i.e. the JSON string is 0-length).

License

MIT

About

Implements structured clone in Node.js, from a live object or serialized/deserialized from a buffer.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published