realistic-structured-clone
Advanced tools
Comparing version 0.0.3 to 1.0.0
32
index.js
@@ -15,3 +15,3 @@ 'use strict'; | ||
// http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm | ||
// https://html.spec.whatwg.org/multipage/infrastructure.html#structuredclone | ||
function structuredClone(input, memory) { | ||
@@ -31,2 +31,6 @@ memory = memory !== undefined ? memory : new Map(); | ||
if (type === 'symbol') { | ||
throw new DataCloneError('Value could not be cloned: ' + input.toString() + ' is a Symbol'); | ||
} | ||
var deepClone = 'none'; | ||
@@ -47,9 +51,2 @@ | ||
} | ||
// Supposed to also handle Blob, FileList, ImageData, ImageBitmap, but fuck it | ||
} else if (Array.isArray(input)) { | ||
output = new Array(input.length); | ||
deepClone = 'own'; | ||
} else if (isPlainObject(input)) { | ||
output = {}; | ||
deepClone = 'own'; | ||
} else if (input instanceof Map) { | ||
@@ -61,4 +58,21 @@ output = new Map(); | ||
deepClone = 'set'; | ||
} else if (typeof Blob !== 'undefined' && input instanceof Blob) { | ||
output = input.slice(0, input.size, input.type); | ||
} else if (Array.isArray(input)) { | ||
output = new Array(input.length); | ||
deepClone = 'own'; | ||
} else if (typeof input === 'function') { | ||
throw new DataCloneError('Object could not be cloned: ' + input.name + ' is a function'); | ||
} else if (!isPlainObject(input)) { | ||
// This is way too restrictive. Should be able to clone any object that isn't | ||
// a platform object with an internal slot other than [[Prototype]] or [[Extensible]]. | ||
// But need to reject all platform objects, except those whitelisted for cloning | ||
// (ie, those with a [[Clone]] internal method), and this errs on the side of caution | ||
// for now. | ||
// Supposed to also handle FileList, ImageData, ImageBitmap, but fuck it | ||
throw new DataCloneError(); | ||
} else { | ||
throw new DataCloneError(); | ||
output = {}; | ||
deepClone = 'own'; | ||
} | ||
@@ -65,0 +79,0 @@ |
{ | ||
"name": "realistic-structured-clone", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "A pure JS implementation of the structured clone algorithm (or at least something pretty close to that)", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dumbmatter/realistic-structured-clone", |
@@ -50,3 +50,3 @@ # Realistic Structured Clone [![Build Status](https://travis-ci.org/dumbmatter/realistic-structured-clone.svg?branch=master)](https://travis-ci.org/dumbmatter/realistic-structured-clone) | ||
[The spec](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#internal-structured-cloning-algorithm) 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`. | ||
[The spec](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#internal-structured-cloning-algorithm) says it's supposed to handle 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`. | ||
@@ -53,0 +53,0 @@ 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. |
@@ -134,2 +134,44 @@ 'use strict'; | ||
}); | ||
it('getter', function () { | ||
var value; | ||
var obj = { | ||
get ref1() {return value;}, | ||
get ref2() {return value;} | ||
}; | ||
value = obj; | ||
assert.throws(function () { | ||
obj.ref1 = 1; | ||
}); | ||
assert.equal(obj, obj.ref1); | ||
assert.equal(obj, obj.ref2); | ||
var obj2 = structuredClone(obj); | ||
assert.equal(obj2, obj2.ref1); | ||
assert.equal(obj2, obj2.ref2); | ||
assert.doesNotThrow(function () { | ||
obj2.ref1 = 1; | ||
}); | ||
assert.equal(obj2.ref1, 1); | ||
assert.equal(obj2, obj2.ref2); | ||
}); | ||
/* disabled tests that don't pass yet | ||
it('POD class', function () { | ||
var MyClass = function () { | ||
this.x = 'x'; | ||
} | ||
confirmWorks(new MyClass()); | ||
}); | ||
it('class with method', function () { | ||
var MyClass = function () { | ||
this.x = 'y'; | ||
} | ||
MyClass.prototype = {method1() {}}; | ||
var obj = new MyClass(); | ||
assert.equal(typeof obj.method1, 'function'); | ||
confirmWorks(obj); | ||
}); | ||
*/ | ||
}); | ||
@@ -141,3 +183,3 @@ | ||
structuredClone(x); | ||
}); | ||
}, /DataCloneError/); | ||
}; | ||
@@ -152,2 +194,19 @@ | ||
}); | ||
it('WeakMap', function () { | ||
confirmFails(new WeakMap()); | ||
}); | ||
it('WeakSet', function () { | ||
confirmFails(new WeakSet()); | ||
}); | ||
it('throwing getter', function () { | ||
var x = { | ||
get bad() {throw new RangeError();} | ||
}; | ||
assert.throws(function () { | ||
structuredClone(x); | ||
}, RangeError); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
19149
267
0