resurrect-esm 2.0.6 → 2.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -7
- package/resurrect.test.ts +10 -9
- package/resurrect.ts +12 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resurrect-esm",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "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, Infinity, undefined (which won't get turned into null), and any other custom object type given the proper resolver.",
|
|
6
6
|
"repository": {
|
|
@@ -26,15 +26,14 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/dragoncoder047/resurrect-esm#readme",
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@happy-dom/global-registrator": "^20.
|
|
30
|
-
"@types/bun": "^1.3.
|
|
31
|
-
"bun": "^1.3.
|
|
32
|
-
"
|
|
33
|
-
"typescript": "^5.6.2"
|
|
29
|
+
"@happy-dom/global-registrator": "^20.10.5",
|
|
30
|
+
"@types/bun": "^1.3.14",
|
|
31
|
+
"bun": "^1.3.14",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
34
33
|
},
|
|
35
34
|
"main": "resurrect.ts",
|
|
36
35
|
"scripts": {
|
|
37
|
-
"build": "pnpm
|
|
36
|
+
"build": "pnpm bun build --sourcemap --target=browser --format=esm resurrect.ts --outfile=resurrect.js",
|
|
38
37
|
"test": "pnpm bun test",
|
|
39
38
|
"test:watch": "pnpm test --watch",
|
|
40
39
|
"prepare": "pnpm build --minify"
|
package/resurrect.test.ts
CHANGED
|
@@ -4,7 +4,11 @@ import { FakeNode, NamespaceResolver, Resurrect, ResurrectError, ResurrectOption
|
|
|
4
4
|
|
|
5
5
|
GlobalRegistrator.register();
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
describe.each([
|
|
8
|
+
["default options", {}],
|
|
9
|
+
["custom prefix", { prefix: "qwerty" }],
|
|
10
|
+
["no revive", { revive: false }]
|
|
11
|
+
])("%s", (_, opt: ResurrectOptions) => {
|
|
8
12
|
|
|
9
13
|
const defOpts = new Resurrect(opt);
|
|
10
14
|
|
|
@@ -20,8 +24,8 @@ function suite(opt?: ResurrectOptions) {
|
|
|
20
24
|
expect(roundtrip(undefined)).toBeUndefined();
|
|
21
25
|
expect(roundtrip("foo")).toBe("foo");
|
|
22
26
|
expect(roundtrip(NaN)).toBeNaN();
|
|
23
|
-
expect(roundtrip(Infinity)).
|
|
24
|
-
expect(roundtrip(-Infinity)).
|
|
27
|
+
expect(roundtrip(Infinity)).toEqual(Infinity);
|
|
28
|
+
expect(roundtrip(-Infinity)).toEqual(-Infinity);
|
|
25
29
|
});
|
|
26
30
|
|
|
27
31
|
test("basic JSON serialization", () => {
|
|
@@ -121,21 +125,18 @@ function suite(opt?: ResurrectOptions) {
|
|
|
121
125
|
test("no revive preserves own properties but not functionality", () => {
|
|
122
126
|
class Dog {
|
|
123
127
|
constructor(public loudness: number, public sound: string) { }
|
|
124
|
-
woof() {
|
|
128
|
+
woof() { }
|
|
125
129
|
}
|
|
126
130
|
const obj = new Dog(3, "wow");
|
|
127
131
|
const roundtripped = roundtrip(obj, {
|
|
128
132
|
resolver: new NamespaceResolver({ Dog }),
|
|
129
133
|
});
|
|
130
134
|
expect(roundtripped).not.toBeInstanceOf(Dog);
|
|
135
|
+
expect(roundtripped).toEqual({ loudness: 3, sound: "wow" } as Dog);
|
|
131
136
|
expect(roundtripped.woof).toBeUndefined();
|
|
132
137
|
});
|
|
133
138
|
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
describe("default options", () => suite());
|
|
137
|
-
describe("custom prefix", () => suite({ prefix: "qwerty" }));
|
|
138
|
-
describe("no revive", () => suite({ revive: false }));
|
|
139
|
+
});
|
|
139
140
|
|
|
140
141
|
test("malformed state bug", () => {
|
|
141
142
|
// Test for the bug found in skeeto/resurrect-js#11
|
package/resurrect.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* resurrect-esm, a fork of resurrect-ts, which is in turn a fork of resurrect.js
|
|
3
|
-
* @version 2.0.
|
|
3
|
+
* @version 2.0.7
|
|
4
4
|
* @license CC0
|
|
5
5
|
* @see http://nullprogram.com/blog/2013/03/28/
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { isArray } from "lib0/array";
|
|
9
|
+
import { isNumber, isString } from "lib0/function";
|
|
8
10
|
import { parse, stringify } from "lib0/json";
|
|
9
11
|
import { keys } from "lib0/object";
|
|
10
12
|
|
|
@@ -26,26 +28,23 @@ const GLOBAL: typeof globalThis = globalThis ?? (0, eval)("this");
|
|
|
26
28
|
*/
|
|
27
29
|
const escapeRegExp = RegExp.escape ?? ((string: string) => string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"));
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
const is = (type: string) => {
|
|
30
32
|
const string = `[object ${type}]`;
|
|
31
33
|
return (obj: any) => {
|
|
32
34
|
return {}.toString.call(obj) === string;
|
|
33
35
|
};
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
const isArray = is("Array") as (obj: any) => obj is any[];
|
|
37
|
-
const isString = is("String") as (obj: any) => obj is string;
|
|
38
38
|
const isBoolean = is("Boolean") as (obj: any) => obj is boolean;
|
|
39
|
-
const isNumber = is("Number") as (obj: any) => obj is number;
|
|
40
39
|
const isFunction = is("Function") as (obj: any) => obj is Function;
|
|
41
40
|
const isDate = is("Date") as (obj: any) => obj is Date;
|
|
42
41
|
const isURL = is("URL") as (obj: any) => obj is URL;
|
|
43
42
|
const isRegExp = is("RegExp") as (obj: any) => obj is RegExp;
|
|
44
43
|
const isObject = is("Object") as (obj: any) => obj is object;
|
|
45
|
-
|
|
44
|
+
const isAtom = (object: any) => {
|
|
46
45
|
return !isObject(object) && !isArray(object);
|
|
47
46
|
}
|
|
48
|
-
|
|
47
|
+
const isPrimitive = (object: any) => {
|
|
49
48
|
return object == null ||
|
|
50
49
|
isNumber(object) ||
|
|
51
50
|
isString(object) ||
|
|
@@ -109,7 +108,7 @@ export class Resurrect {
|
|
|
109
108
|
/**
|
|
110
109
|
* Put a temporary identifier on an object and store it in the table.
|
|
111
110
|
*/
|
|
112
|
-
#tagObject(object: any)
|
|
111
|
+
#tagObject(object: any) {
|
|
113
112
|
if (this.revive) {
|
|
114
113
|
const constructor = this.resolver.getName(object);
|
|
115
114
|
if (constructor) {
|
|
@@ -132,7 +131,7 @@ export class Resurrect {
|
|
|
132
131
|
* Create a builder object (encoding) for serialization.
|
|
133
132
|
* @param value The value to pass to the constructor.
|
|
134
133
|
*/
|
|
135
|
-
#createBuilderObject(name: string, value: any)
|
|
134
|
+
#createBuilderObject(name: string, value: any) {
|
|
136
135
|
return {
|
|
137
136
|
[this.#buildcode]: name,
|
|
138
137
|
[this.#valuecode]: value
|
|
@@ -144,7 +143,7 @@ export class Resurrect {
|
|
|
144
143
|
* @see http://stackoverflow.com/a/14378462
|
|
145
144
|
* @see http://nullprogram.com/blog/2013/03/24/
|
|
146
145
|
*/
|
|
147
|
-
#buildFromEncoding(ref: any)
|
|
146
|
+
#buildFromEncoding(ref: any) {
|
|
148
147
|
const type = this.resolver.getConstructor(ref[this.#buildcode]);
|
|
149
148
|
/* Brilliant hack by kybernetikos: */
|
|
150
149
|
const result: any = new (type.bind.apply(type, [null].concat(ref[this.#valuecode]) as [any, any[]]))();
|
|
@@ -159,7 +158,7 @@ export class Resurrect {
|
|
|
159
158
|
* Dereference or build an object or value from an encoding.
|
|
160
159
|
* @method
|
|
161
160
|
*/
|
|
162
|
-
#decodeReference(ref: object)
|
|
161
|
+
#decodeReference(ref: object) {
|
|
163
162
|
if (this.#backrefcode in ref) {
|
|
164
163
|
return this.#dereference(ref);
|
|
165
164
|
} else if (this.#buildcode in ref) {
|
|
@@ -181,7 +180,7 @@ export class Resurrect {
|
|
|
181
180
|
* Visit root and all its ancestors, visiting atoms with f.
|
|
182
181
|
* @returns A fresh copy of root to be serialized.
|
|
183
182
|
*/
|
|
184
|
-
#visit(root: any, transform: (obj: any) => any, replacer?: (k: string, v: any) => any)
|
|
183
|
+
#visit(root: any, transform: (obj: any) => any, replacer?: (k: string, v: any) => any) {
|
|
185
184
|
if (isAtom(root)) {
|
|
186
185
|
return transform(root);
|
|
187
186
|
} else if (!this.#isTagged(root)) {
|
|
@@ -218,7 +217,7 @@ export class Resurrect {
|
|
|
218
217
|
* Manage special atom values, possibly returning an encoding.
|
|
219
218
|
*/
|
|
220
219
|
#encodeAtom(atom: any): any {
|
|
221
|
-
const Node = GLOBAL.Node ||
|
|
220
|
+
const Node = GLOBAL.Node || class { };
|
|
222
221
|
if (isFunction(atom)) {
|
|
223
222
|
throw new ResurrectError("Can't serialize functions.");
|
|
224
223
|
} else if (atom instanceof Node) {
|