Test1 (Date): const src = new Date(); src: 2022-08-07T07:33:35.296Z ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Date", "_SAtimestamp": 1659857615296 } }' deser: Date: Sun Aug 07 2022 00:33:35 GMT-0700 (Pacific Daylight Time) Test2 (Map): let src = new Map(); let key1 = "key1"; let key2 = {foo: "bar"}; src.set(key1, "key1 value"); src.set(key2, new Date()); src: Map(2) { 'key1' => 'key1 value', { foo: 'bar' } => 2022-08-07T07:33:35.299Z } ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Map", "_SAkvPairs": [ [ "key1", "key1 value" ], [ { "foo": "bar" }, { "_SAType": "Date", "_SAtimestamp": 1659857615299 } ] ] } }' deser: Map(2) { 'key1' => 'key1 value', { foo: 'bar' } => 2022-08-07T07:33:35.299Z } Test3 (Function): let src = function foo (x) { return x + 1;}; src: [Function: foo] src.toString: function foo (x) { return x + 1;} ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Function", "_SAfunction": "function foo (x) { return x + 1;}" } }' deser: [Function: foo] Test4: (RegExp) let src = [1,2,/abc/i]; src: [ 1, 2, /abc/i ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": [ 1, 2, { "_SAType": "RegExp", "_SAsource": "abc", "_SAflags": "i" } ] }' deser: [ 1, 2, /abc/i ] Test5 (Int8Array): let src = Int8Array.from([3, 4, 42]); src: Int8Array(3) [ 3, 4, 42 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Int8Array", "_SAvalues": [ 3, 4, 42 ] } }' deser: Int8Array(3) [ 3, 4, 42 ] Test6 (Uint8Array): let src = Uint8Array.from([3, 128, 64]); src: Uint8Array(3) [ 3, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Uint8Array", "_SAvalues": [ 3, 128, 64 ] } }' deser: Uint8Array(3) [ 3, 128, 64 ] Test7 (Int16Array): let src = Int16Array.from([32, 128, 64]); src: Int16Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Int16Array", "_SAvalues": [ 32, 128, 64 ] } }' deser: Int16Array(3) [ 32, 128, 64 ] Test8 (Uint16Array): let src = Uint16Array.from([32, 128, 64]); src: Uint16Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Uint16Array", "_SAvalues": [ 32, 128, 64 ] } }' deser: Uint16Array(3) [ 32, 128, 64 ] Test9 (Int32Array): let src = Int32Array.from([32, 128, 64]); src: Int32Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Int32Array", "_SAvalues": [ 32, 128, 64 ] } }' deser: Int32Array(3) [ 32, 128, 64 ] Test10 (Uint32Array): let src = Uint32Array.from([32, 128, 64]); src: Uint32Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Uint32Array", "_SAvalues": [ 32, 128, 64 ] } }' deser: Uint32Array(3) [ 32, 128, 64 ] Test11 (Float32Array): let src = Float32Array.from([32, 128, 64]); src: Float32Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Float32Array", "_SAvalues": [ 32, 128, 64 ] } }' deser: Float32Array(3) [ 32, 128, 64 ] Test12 (Float64Array): let src = Float64Array.from([32, 128, 64]); src: Float64Array(3) [ 32, 128, 64 ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Float64Array", "_SAvalues": [ "32", "128", "64" ] } }' deser: Float64Array(3) [ 32, 128, 64 ] Test13 (BigInt64Array): let src = BigInt64Array.from([32, 128, 64]); src: BigInt64Array(3) [ 3n, 4n, 42n ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "BigInt64Array", "_SAvalues": [ "3", "4", "42" ] } }' deser: BigInt64Array(3) [ 3n, 4n, 42n ] Test14 (BigUint64Array): let src = BigUint64Array.from([3000000000000000000n, 4n, 42n]); src: BigUint64Array(3) [ 3000000000000000000n, 4n, 42n ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "BigUint64Array", "_SAvalues": [ "3000000000000000000", "4", "42" ] } }' deser: BigUint64Array(3) [ 3000000000000000000n, 4n, 42n ] Test15 (ArrayBuffer): let aBuf = new ArrayBuffer(8); let src = new Uint8Array(aBuf); let data = [1, 2, 3, 4, 5, 127]; data: [ 1, 2, 3, 4, 5, 127 ] src: ArrayBuffer { [Uint8Contents]: <01 02 03 04 05 7f 00 00>, byteLength: 8 } aBuf: ArrayBuffer { [Uint8Contents]: <01 02 03 04 05 7f 00 00>, byteLength: 8 } ser: '{"_Serialize_Any_Encoded":true,"_SA_Content":{"_SAType":"ArrayBuffer","_SAvalues":[1,2,3,4,5,127,0,0]}}' deser: ArrayBuffer { [Uint8Contents]: <01 02 03 04 05 7f 00 00>, byteLength: 8 } Test16 (Set): let src = new Set([1,2,"3",{createdAt: new Date()}]); src: Set(4) { 1, 2, '3', { createdAt: 2022-08-07T07:33:35.316Z } } ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Set", "_SAvalues": [ 1, 2, "3", { "createdAt": { "_SAType": "Date", "_SAtimestamp": 1659857615316 }, "_SAId": 2 } ] } }' deser: Set(4) { 1, 2, '3', { createdAt: 2022-08-07T07:33:35.316Z } } Test17 (WeakSet): let src = new WeakSet(); let obj = {value: "in the set"} src.add(obj); src: WeakSet { } *** TEST FAILED: Error: serialize WeakSet not supported Test18 (WeakMap): let src = new WeakMap(); let obj = { foo: "I am foo" }; src.set(obj, 42); src: WeakMap { } *** TEST FAILED: Error: serialize WeakMap not supported Test19 (Buffer): let src = Buffer.from("hello world"); src: ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Buffer", "_SAutf8String": "hello world" } }' deser: Test20 (CustomObject): class CustomObject extends Object { custom () {return "I am a custom Object";} } let src = new CustomObject(); src: CustomObject {} ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "_SACustomObject", "_SAconstructorName": "CustomObject", "_SAobject": { "_SAId": 1 } } }' deser: CustomObject {} deser.constructor.name: CustomObject deser.custom(): I am a custom Object Test21 (CustomArray with RegExp): class CustomArray extends Array { custom () {return "I am a custom Array";} } let src = CustomArray.from([1,2,"foo",{key: [1,2,3, /abc/g]}]); src: CustomArray(4) [ 1, 2, 'foo', { key: [ 1, 2, 3, /abc/g ] } ] src.custom(): I am a custom Array ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "_SACustomArray", "_SAconstructorName": "CustomArray", "_SAvalues": [ 1, 2, "foo", { "key": [ 1, 2, 3, { "_SAType": "RegExp", "_SAsource": "abc", "_SAflags": "g" } ], "_SAId": 2 } ] } }' deser: CustomArray(4) [ 1, 2, 'foo', { key: [ 1, 2, 3, /abc/g ] } ] deser.custom(): I am a custom Array Test22 (Array containing BigInt): let src = [1, 2, 3000000000n]; src: [ 1, 2, 3000000000n ] ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": [ 1, 2, { "_SAType": "BigInt", "_SAnum": "3000000000" } ] }' deser: [ 1, 2, 3000000000n ] Test23 (Object containing undefined prop): let src = {foo: "bar", notSet: undefined}; src: { foo: 'bar', notSet: undefined } ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "foo": "bar", "notSet": { "_SAType": "undef" }, "_SAId": 1 } }' deser: { foo: 'bar', notSet: undefined } Test24 (undefined variable): let src = undefined; src: undefined ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "undef" } }' deser: undefined Test25 (Error): let src = new Error("this is a sample error"); src: Error: this is a sample error at test25 (/Users/jric/dev/patches/serialize-anything/test/test-suite.js:662:15) at /Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:39:3 ... 7 lines matching cause stack trace ... at node:internal/main/run_main_module:17:47 { [cause]: Error: this is a sample cause at test25 (/Users/jric/dev/patches/serialize-anything/test/test-suite.js:660:11) at /Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:39:3 at Array.forEach () at Object. (/Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:38:7) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Module._load (node:internal/modules/cjs/loader:827:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 } ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "Error", "_SAmessage": "this is a sample error", "_SAstack": "Error: this is a sample error\n at test25 (/Users/jric/dev/patches/serialize-anything/test/test-suite.js:662:15)\n at /Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:39:3\n at Array.forEach ()\n at Object. (/Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:38:7)\n at Module._compile (node:internal/modules/cjs/loader:1105:14)\n at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)\n at Module.load (node:internal/modules/cjs/loader:981:32)\n at Module._load (node:internal/modules/cjs/loader:827:12)\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)\n at node:internal/main/run_main_module:17:47" } }' deser: Error: this is a sample error at test25 (/Users/jric/dev/patches/serialize-anything/test/test-suite.js:662:15) at /Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:39:3 at Array.forEach () at Object. (/Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:38:7) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Module._load (node:internal/modules/cjs/loader:827:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { cause: undefined } *** TEST FAILED: Error: No cause serialized for error at test25 (/Users/jric/dev/patches/serialize-anything/test/test-suite.js:671:17) at /Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:39:3 at Array.forEach () at Object. (/Users/jric/dev/patches/serialize-anything/test/test-serialize-anything.js:38:7) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Module._load (node:internal/modules/cjs/loader:827:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 Test26 (demo): class CustomObj extends Object {} const custom = new CustomObj(); Object.assign(custom,{foo:"bar"}); let src = { undef: undefined, regexp: /abc/gi bignum: 4000000000000000000n map: new Map([ [1, 'one'], [2, 'two'] ]), custom: custom, buffer: Buffer.from('hello world') }; src: { undef: undefined, regexp: /abc/gi, bignum: 4000000000000000000n, map: Map(2) { 1 => 'one', 2 => 'two' }, custom: CustomObj { foo: 'bar' }, buffer: } ser: '{ "_Serialize_Any_Encoded": true, "_SA_Content": { "undef": { "_SAType": "undef" }, "regexp": { "_SAType": "RegExp", "_SAsource": "abc", "_SAflags": "gi" }, "bignum": { "_SAType": "BigInt", "_SAnum": "4000000000000000000" }, "map": { "_SAType": "Map", "_SAkvPairs": [ [ 1, "one" ], [ 2, "two" ] ] }, "custom": { "_SAType": "_SACustomObject", "_SAconstructorName": "CustomObj", "_SAobject": { "foo": "bar", "_SAId": 3 } }, "buffer": { "_SAType": "Buffer", "_SAutf8String": "hello world" }, "_SAId": 1 } }' deser: { undef: undefined, regexp: /abc/gi, bignum: 4000000000000000000n, map: Map(2) { 1 => 'one', 2 => 'two' }, custom: CustomObj { foo: 'bar' }, buffer: } Test27 (circular object reference): const src = { foo: "Foo", bar: {bar: "Bar"}}; src.bar.baz = src; src: { foo: 'Foo', bar: { bar: 'Bar', baz: [Circular *1] } } ser: { "_Serialize_Any_Encoded": true, "_SA_Content": { "foo": "Foo", "bar": { "bar": "Bar", "baz": { "_SAType": "_SAObjectRef", "_SAId": 1 }, "_SAId": 2 }, "_SAId": 1 } } deser: { foo: 'Foo', bar: { bar: 'Bar', baz: [Circular *1] } } deser.foo = "FOO_FOO"; deser: { foo: 'FOO_FOO', bar: { bar: 'Bar', baz: [Circular *1] } } Test28 (max depth): let wideObject = Array.from({length: options.maxDepth - 5}, (x, i) => { return new Object(); } ); let extraWideObject = [...wideObject, Array.from({length: 10}, (x, i) => { return new Object(); })]; let generateDeepObject = (deepObject) => { deepObject.deepestChild = deepObject.deepestChild.child = new Object(); return deepObject; }; let deepObject = wideObject.reduce(generateDeepObject, new function () { this.deepestChild = this; }() ); tooDeepObject = extraWideObject.reduce(generateDeepObject, new function () { this.deepestChild = this; }() ); assertDoesNotThrow { serialize(wideObject); serialize(deepObject); serialize(extraWideObject); } assertThrows { serialize(tooDeepObject); } Test29 (anonymous custom object) const src = new function() { this.foo = "bar"; }(); const ser = serialize(src); const des = deserialize(ser, function() { return new Object(); }); src: { foo: 'bar' } ser: { "_Serialize_Any_Encoded": true, "_SA_Content": { "_SAType": "_SACustomObject", "_SAconstructorName": "", "_SAobject": { "foo": "bar", "_SAId": 1 } } } deser: { foo: 'bar' } End of test. Errors: [ 'Test17 Error: serialize WeakSet not supported', 'Test18 Error: serialize WeakMap not supported', 'Test25 Error: No cause serialized for error' ]