resurrect-esm
v2.0.6
Published
ResurrectJS preserves object behavior (prototypes) and reference circularity with a special JSON encoding. Unlike flat JSON, it can also properly resurrect self-referential objects, objects with shared structure, Dates, RegExps, HTMLElements, NaN, Infinit
Readme
ResurrectESM
An ES6 module port of ResurrectTS.
[!CAUTION] This is not just a naive
exports.foo=>export {foo}rewrite. Some API changes have been made:
- All of the "irrelevant" methods of Resurrect have been renamed using private
#names.ResurrectErroris now a top-level export, instead of living inside aResurrectobject.NamespaceResolveris now a top-level export, instead of living inside theResurrectstatic constructor namespace.NamespaceResolvernow has a new method,getConstructor(name: string), which should return the constructor function to generate the object (the constructor itself will not be called directly, so the parameters are irrelevant).- The bug found in skeeto/resurrect-js#11 has been fixed.
ResurrectJS preserves object behavior (prototypes) and reference circularity with a special JSON encoding. Unlike flat JSON, it can also properly resurrect these types of values:
- Date
- URL
- RegExp
undefined- NaN, Infinity, -Infinity
Supported Browsers:
- Chrome
- Firefox
- Safari
- Opera
- IE9+
Read about how it works.
Examples
import { Resurrect } from "resurrect-esm";
window.Foo = class Foo {
greet() { return "hello"; }
}
// Behavior is preserved:
const necromancer = new Resurrect();
const json = necromancer.stringify(new Foo());
const foo = necromancer.resurrect(json);
foo.greet(); // => "hello"
// References to the same object are preserved:
json = necromancer.stringify([foo, foo]);
const array = necromancer.resurrect(json);
array[0] === array[1]; // => true
array[1].greet(); // => "hello"
// Dates are restored properly
json = necromancer.stringify(new Date());
const date = necromancer.resurrect(json);
{}.toString.call(date); // => "[object Date]"Options
Options are provided to the constructor as an object with these properties:
prefix (default:
"#"): A prefix string used for temporary properties added to objects during serialization and deserialization. It is important that you don't use any properties beginning with this string. This option also must be the same between both serialization and deserialization.cleanup (default: false): Perform full property cleanup after both serialization and deserialization using the
deleteoperator. This may cause performance penalties (i.e. breaking hidden classes and forcing de-optimization) on objects that Resurrect touches, so enable with care.revive (default: true): Restore behavior (
__proto__) to objects that have been resurrected. If this is set to false during serialization, resurrection information will not be encoded. You still get circularity and Date/HTML/URL support.resolver (
NamespaceResolver, default undefined): Converts between a name and a prototype. Create a custom resolver if your constructors are not stored in global variables. The resolver has three methods:getName(object),getConstructor(string), andgetPrototype(string).
For example,
import { Resurrect } from "resurrect-esm";
const necromancer = new Resurrect({
prefix: "__#",
cleanup: true
});Methods
There are only two public methods:
.stringify(object[, replacer[, space]]): Serializes an arbitrary object or value into a string. Thereplacerandspacearguments are the same as JSON.stringify, being passed through to this method. Note that the replacer will not be called for Resurrect's intrusive keys (i.e. the ones that are created using the.prefixoption)..resurrect(string): Deserializes an object stored in a string by a previous call to.stringify(). Circularity and, optionally, behavior (prototype chain) will be restored.
Restrictions
With the default resolver, all constructors must be named and stored in the global scope under that name. This is required so that the prototypes can be looked up and reconnected at resurrection time.
[!CAUTION] If you're using ES6 modules for your custom classes, you MUST use a custom resolver since module scope is not global scope!
The wrapper objects Boolean, String, and Number will be unwrapped. This means extra properties added to these objects will not be preserved.
Functions cannot ever be serialized. Resurrect will throw an error if a function is found when traversing a data structure, rather than just silently dropping the property or replacing it with null like JSON.stringify does.
Custom Resolvers
New in 2.0.5: the NamespaceResolver will now use the name on the namespace object of whatever you pass in instead of the .name of the object's constructor. This is to better support minifiers that rename the class and produce code output similar to this:
import { Resurrect as r, NamespaceResolver as e } from "resurrect-esm";
var c = class d {
constructor() {
this.bar = true;
d.bax(this);
}
static bax(obj) {
}
};
var n = {
Foo: c, // <== Always uses name given here
};
var a = new r({
resolver: new e(n)
});In the serializer above, the "name" given to the class will always be "Foo", even though the .name of the constructor is "c" and might be different on the next build.
