From ae33b9497cdbd323ca7195c7fcd12790cbd4a9df Mon Sep 17 00:00:00 2001 From: "Alexis H. Munsayac" Date: Tue, 14 Nov 2023 12:03:20 +0800 Subject: [PATCH] Remove `MethodShorthand` and `ArrayPrototypeValues` --- docs/compatibility.md | 5 -- packages/seroval/src/core/compat.ts | 2 - .../test/__snapshots__/iterable.test.ts.snap | 8 +--- packages/seroval/test/iterable.test.ts | 26 +--------- packages/seroval/test2.js | 48 ++++++++++++------- 5 files changed, 35 insertions(+), 54 deletions(-) diff --git a/docs/compatibility.md b/docs/compatibility.md index d745a90f..02a4ab9c 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -46,8 +46,6 @@ By default, all feature flags are enabled. The following are the feature flags a - [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) - Compiles down to `Error` instead. -- [`ArrayPrototypeValues`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values) - - Used for `Iterable`, uses `Symbol.iterator` instead. - [`ArrowFunction`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) - Uses function expressions for top-level and for deferred `Promise` values - method shorthands (if `MethodShortand` is not set) or function expressions for `Iterable`. @@ -58,9 +56,6 @@ By default, all feature flags are enabled. The following are the feature flags a - Affects both `Error` and `AggregateError` - [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) - Disables serialization of `Map` -- [`MethodShorthand`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions) - - Uses function expressions instead. - - Only affects `Iterable` - [`ObjectAssign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) - Uses manual object assignments instead. - Affects `Iterable`, `Error`, `AggregateError` and `Object.create(null)` diff --git a/packages/seroval/src/core/compat.ts b/packages/seroval/src/core/compat.ts index 9b341462..32eaff9d 100644 --- a/packages/seroval/src/core/compat.ts +++ b/packages/seroval/src/core/compat.ts @@ -6,12 +6,10 @@ export const enum Feature { AggregateError = 0x01, - ArrayPrototypeValues = 0x02, ArrowFunction = 0x04, BigInt = 0x08, ErrorPrototypeStack = 0x10, Map = 0x20, - MethodShorthand = 0x40, ObjectAssign = 0x80, Promise = 0x100, Set = 0x200, diff --git a/packages/seroval/test/__snapshots__/iterable.test.ts.snap b/packages/seroval/test/__snapshots__/iterable.test.ts.snap index 32eb774e..4f9a8c91 100644 --- a/packages/seroval/test/__snapshots__/iterable.test.ts.snap +++ b/packages/seroval/test/__snapshots__/iterable.test.ts.snap @@ -1,16 +1,10 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`Iterable > compat > should use Symbol.iterator instead of Array.values. 1`] = `"(h=>(h={[Symbol.iterator]:(s=>((i,c,d)=>(i=0,{next:()=>{c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})))({v:[h,void 0],t:-1,d:1})}))()"`; - exports[`Iterable > compat > should use functions instead of method shorthand. 1`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`; exports[`Iterable > compat > should use method shorthand instead of arrow functions. 1`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`; -exports[`Iterable > compat#toJSON > should use Symbol.iterator instead of Array.values. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16381,\\"m\\":[0]}"`; - -exports[`Iterable > compat#toJSON > should use Symbol.iterator instead of Array.values. 2`] = `"(h=>(h={[Symbol.iterator]:(s=>((i,c,d)=>(i=0,{next:()=>{c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})))({v:[h,void 0],t:-1,d:1})}))()"`; - -exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16315,\\"m\\":[0]}"`; +exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16379,\\"m\\":[0]}"`; exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 2`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`; diff --git a/packages/seroval/test/iterable.test.ts b/packages/seroval/test/iterable.test.ts index e38cc653..596e5f00 100644 --- a/packages/seroval/test/iterable.test.ts +++ b/packages/seroval/test/iterable.test.ts @@ -196,16 +196,6 @@ describe('Iterable', () => { }); }); describe('compat', () => { - it('should use Symbol.iterator instead of Array.values.', () => { - const example = { - * [Symbol.iterator](): unknown { - yield example; - }, - }; - expect(serialize(example, { - disabledFeatures: Feature.ArrayPrototypeValues, - })).toMatchSnapshot(); - }); it('should use method shorthand instead of arrow functions.', () => { const example = { * [Symbol.iterator](): unknown { @@ -223,23 +213,11 @@ describe('Iterable', () => { }, }; expect(serialize(example, { - disabledFeatures: Feature.MethodShorthand | Feature.ArrowFunction, + disabledFeatures: Feature.ArrowFunction, })).toMatchSnapshot(); }); }); describe('compat#toJSON', () => { - it('should use Symbol.iterator instead of Array.values.', () => { - const example = { - * [Symbol.iterator](): unknown { - yield example; - }, - }; - const result = toJSON(example, { - disabledFeatures: Feature.ArrayPrototypeValues, - }); - expect(JSON.stringify(result)).toMatchSnapshot(); - expect(compileJSON(result)).toMatchSnapshot(); - }); it('should use method shorthand instead of arrow functions.', () => { const example = { * [Symbol.iterator](): unknown { @@ -259,7 +237,7 @@ describe('Iterable', () => { }, }; const result = toJSON(example, { - disabledFeatures: Feature.MethodShorthand | Feature.ArrowFunction, + disabledFeatures: Feature.ArrowFunction, }); expect(JSON.stringify(result)).toMatchSnapshot(); expect(compileJSON(result)).toMatchSnapshot(); diff --git a/packages/seroval/test2.js b/packages/seroval/test2.js index 665407ff..b9d4d16d 100644 --- a/packages/seroval/test2.js +++ b/packages/seroval/test2.js @@ -1,17 +1,33 @@ -import { crossSerializeStream } from './dist/esm/development/index.mjs'; - -const source = new ReadableStream({ - start(controller) { - controller.enqueue('Hello'); - controller.enqueue('World'); - controller.close(); - } -}) - -crossSerializeStream(source, { - scopeId: 'example', - onSerialize(data) { - console.log([data]); - } -}); +import { serialize } from './dist/esm/development/index.mjs'; + +// const instance = new Serializer({ +// globalIdentifier: 'test', +// onData: console.log, +// }); + +// const example = { foo: 'bar' }; + +// function* hello() { +// yield example; +// } + +// instance.write('foo', hello()); +// instance.write('bar', hello()); + +const example = { + a: new Map(), + b: new Set(), +}; + +example.a.set(example.a, example.b); + +const example2 = { + foo: new Map(), + bar: new Set(), +}; + +example2.foo.set(example2.foo, example2.bar); +example2.heck = example; + +console.log(serialize(example2)); \ No newline at end of file