Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

realistic-structured-clone

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

realistic-structured-clone

A pure JS implementation of the structured clone algorithm (or at least something pretty close to that)

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
253K
decreased by-7.93%
Maintainers
1
Weekly downloads
 
Created
Source

Realistic Structured Clone Build Status

This is a pure JS implementation of the structured clone algorithm (or at least something pretty close to that).

Why do you want this? Well, you probably don't. If your goal is to just clone a JS object, you're better off with lodash's _.cloneDeep or the popular clone module on npm.

Let's try again... why do you want this? If you are making an implementation of an API that explicitly uses the structured clone algorithm (such as IndexedDB), then you want something that handles quirks and edge cases exactly like the structured clone algorithm. That's what realistic-structured-clone is for. It's not totally there (see below) but it's a decent start.

Use

Install through npm:

$ npm install realistic-structured-clone

Then use it:

// First load the module
// (Use Browserify or something if you're targeting the web)
var structuredClone = require('realistic-structured-clone');

// Clone a variable (will throw a DataCloneError for invalid input)
var clonedX = structuredClone(x);

Alternatives

If you look around, you'll notice various modules calling themselves implementations of the structured clone algorithm, such as the structured-clone package on npm. But that package, like all the others I've seen, doesn't actually seem to be an attempt at implementing the structured clone algorithm. It's just some arbitrary type of clone. As I wrote above, this distinction only matters if you really care about the nuances of the structured clone algorithm, which you probably don't.

If you're working in the browser, you can do something like this to do a real structured clone:

function clone(x) {
    return new Promise(function (resolve, reject) {
        window.addEventListener('message', function(e) {
            resolve(e.data);
        });
        window.postMessage(x, "*");
    });
}
var x = {a:[1,2,3], b:{c:1}};
clone(x).then(function(cloned) {
    console.log("x: %s", JSON.stringify(x));
    console.log("cloned: %s", JSON.stringify(cloned));
    console.log("x == cloned %s", x == cloned);
    console.log("x === cloned %s", x === cloned);
});

However, that won't help you in Node.js/io.js. It's also asynchronous, which could be a problem. realistic-structured-clone is synchronous and works everywhere.

Current State

The spec says it's supposed to handle Blob, FileList, ImageData, and ImageBitmap objects. But none of that is implemented yet, so passing an object containing any of those types of objects will result in an erroneous DataCloneError.

All other data types should work like described in the spec. Check the tests if you don't believe me, and please create an issue if you find a problem.

License

Apache 2.0

Keywords

FAQs

Package last updated on 04 Nov 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc