Socket
Book a DemoInstallSign in
Socket

structured-clone

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

latest
Source
npmnpm
Version
0.2.2
Version published
Weekly downloads
3.3K
14.97%
Maintainers
1
Weekly downloads
 
Created
Source

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:

  • A UTF-8 encoded JSON string.
  • A null byte.
  • 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

FAQs

Package last updated on 21 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts