seroval 0.4.0-alpha.0 → 0.4.0-alpha.1

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/src/compat.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable guard-for-in */
1
2
  /**
2
3
  * References
3
4
  * - https://kangax.github.io/compat-table/es6/
@@ -38,25 +39,27 @@ export interface PlatformVersion {
38
39
 
39
40
  export type Platform = keyof PlatformVersion;
40
41
 
41
- interface VersionTable {
42
- 'aggregate-error': PlatformVersion;
43
- 'array-values': PlatformVersion;
44
- 'arrow-function': PlatformVersion;
45
- 'bigint': PlatformVersion;
46
- 'map': PlatformVersion;
47
- 'method-shorthand': PlatformVersion;
48
- 'object-assign': PlatformVersion;
49
- 'promise': PlatformVersion;
50
- 'set': PlatformVersion;
51
- 'symbol-iterator': PlatformVersion;
52
- 'typed-arrays': PlatformVersion;
42
+ export const enum Feature {
43
+ AggregateError,
44
+ ArrayPrototypeValues,
45
+ ArrowFunction,
46
+ BigInt,
47
+ Map,
48
+ MethodShorthand,
49
+ ObjectAssign,
50
+ Promise,
51
+ Set,
52
+ SymbolIterator,
53
+ TypedArray,
53
54
  }
54
55
 
55
- export type Feature = keyof VersionTable;
56
+ type VersionTable = {
57
+ [key in Feature]: PlatformVersion;
58
+ }
56
59
 
57
60
  const VERSION_TABLE: VersionTable = {
58
61
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
59
- 'aggregate-error': {
62
+ [Feature.AggregateError]: {
60
63
  es: [2021, 0, 0],
61
64
  chrome: [85, 0, 0],
62
65
  edge: [85, 0, 0],
@@ -69,7 +72,7 @@ const VERSION_TABLE: VersionTable = {
69
72
  node: [15, 0, 0],
70
73
  },
71
74
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
72
- 'array-values': {
75
+ [Feature.ArrayPrototypeValues]: {
73
76
  es: [6, 0, 0],
74
77
  chrome: [66, 0, 0],
75
78
  edge: [14, 0, 0],
@@ -82,7 +85,7 @@ const VERSION_TABLE: VersionTable = {
82
85
  node: [10, 9, 0],
83
86
  },
84
87
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
85
- 'arrow-function': {
88
+ [Feature.ArrowFunction]: {
86
89
  es: [6, 0, 0],
87
90
  chrome: [45, 0, 0],
88
91
  edge: [12, 0, 0],
@@ -95,7 +98,7 @@ const VERSION_TABLE: VersionTable = {
95
98
  node: [4, 0, 0],
96
99
  },
97
100
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
98
- bigint: {
101
+ [Feature.BigInt]: {
99
102
  es: [2020, 0, 0],
100
103
  chrome: [67, 0, 0],
101
104
  edge: [79, 0, 0],
@@ -108,7 +111,7 @@ const VERSION_TABLE: VersionTable = {
108
111
  node: [10, 4, 0],
109
112
  },
110
113
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
111
- map: {
114
+ [Feature.Map]: {
112
115
  es: [6, 0, 0],
113
116
  chrome: [38, 0, 0],
114
117
  edge: [12, 0, 0],
@@ -121,7 +124,7 @@ const VERSION_TABLE: VersionTable = {
121
124
  node: [0, 12, 0],
122
125
  },
123
126
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
124
- 'method-shorthand': {
127
+ [Feature.MethodShorthand]: {
125
128
  es: [6, 0, 0],
126
129
  chrome: [39, 0, 0],
127
130
  edge: [12, 0, 0],
@@ -134,7 +137,7 @@ const VERSION_TABLE: VersionTable = {
134
137
  node: [4, 0, 0],
135
138
  },
136
139
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
137
- 'object-assign': {
140
+ [Feature.ObjectAssign]: {
138
141
  es: [6, 0, 0],
139
142
  chrome: [45, 0, 0],
140
143
  edge: [12, 0, 0],
@@ -147,7 +150,7 @@ const VERSION_TABLE: VersionTable = {
147
150
  node: [4, 0, 0],
148
151
  },
149
152
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
150
- promise: {
153
+ [Feature.Promise]: {
151
154
  es: [6, 0, 0],
152
155
  chrome: [32, 0, 0],
153
156
  edge: [12, 0, 0],
@@ -160,7 +163,7 @@ const VERSION_TABLE: VersionTable = {
160
163
  node: [0, 12, 0],
161
164
  },
162
165
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
163
- set: {
166
+ [Feature.Set]: {
164
167
  es: [6, 0, 0],
165
168
  chrome: [38, 0, 0],
166
169
  edge: [12, 0, 0],
@@ -173,7 +176,7 @@ const VERSION_TABLE: VersionTable = {
173
176
  node: [0, 12, 0],
174
177
  },
175
178
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
176
- 'symbol-iterator': {
179
+ [Feature.SymbolIterator]: {
177
180
  es: [6, 0, 0],
178
181
  chrome: [43, 0, 0],
179
182
  edge: [12, 0, 0],
@@ -186,7 +189,7 @@ const VERSION_TABLE: VersionTable = {
186
189
  node: [0, 12, 0],
187
190
  },
188
191
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
189
- 'typed-arrays': {
192
+ [Feature.TypedArray]: {
190
193
  es: [6, 0, 0],
191
194
  chrome: [7, 0, 0],
192
195
  edge: [12, 0, 0],
@@ -223,23 +226,19 @@ function getTargetVersions(targets: string | string[]): Target[] {
223
226
  return [parseTarget(targets)];
224
227
  }
225
228
 
226
- function isFeatureSupported(key: Feature, targets: Target[]) {
227
- const base = VERSION_TABLE[key];
228
- let flag = true;
229
- for (const [platform, version] of targets) {
230
- const baseVersion = base[platform];
231
- flag = flag && !!baseVersion && (compareVersion(baseVersion, version) <= 0);
232
- }
233
- return flag;
234
- }
235
-
236
229
  export function parseTargets(targets: string | string[]): Set<Feature> {
237
230
  const parsed = getTargetVersions(targets);
238
231
  const flags = new Set<Feature>();
239
232
 
240
233
  for (const key in VERSION_TABLE) {
241
- if (isFeatureSupported(key as Feature, parsed)) {
242
- flags.add(key as Feature);
234
+ const base = VERSION_TABLE[key as unknown as Feature];
235
+ let flag = true;
236
+ for (const [platform, version] of parsed) {
237
+ const baseVersion = base[platform];
238
+ flag = flag && !!baseVersion && (compareVersion(baseVersion, version) <= 0);
239
+ }
240
+ if (flag) {
241
+ flags.add(Number(key) as unknown as Feature);
243
242
  }
244
243
  }
245
244
 
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable no-await-in-loop */
2
2
  import { isPrimitive } from './checks';
3
+ import { Feature } from './compat';
3
4
  import {
4
5
  createRef,
5
6
  createSerializationContext,
@@ -53,7 +54,7 @@ function finalize<T extends NonPrimitiveServerValue<ServerValue | AsyncServerVal
53
54
  ? ctx.vars.join(',')
54
55
  : ctx.vars[0];
55
56
  // Source is probably already assigned
56
- if (ctx.features.has('arrow-function')) {
57
+ if (ctx.features.has(Feature.ArrowFunction)) {
57
58
  params = ctx.vars.length > 1 || ctx.vars.length === 0
58
59
  ? `(${params})`
59
60
  : params;
package/src/tree.ts CHANGED
@@ -481,53 +481,53 @@ function generateSemiPrimitiveValue(
481
481
  return [SerovalNodeType.RegExp, current, id];
482
482
  }
483
483
  if (constructorCheck<Int8Array>(current, Int8Array)) {
484
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Int8Array"');
484
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Int8Array"');
485
485
  return [SerovalNodeType.TypedArray, ['Int8Array', current], id];
486
486
  }
487
487
  if (constructorCheck<Int16Array>(current, Int16Array)) {
488
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Int16Array"');
488
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Int16Array"');
489
489
  return [SerovalNodeType.TypedArray, ['Int16Array', current], id];
490
490
  }
491
491
  if (constructorCheck<Int32Array>(current, Int32Array)) {
492
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Int32Array"');
492
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Int32Array"');
493
493
  return [SerovalNodeType.TypedArray, ['Int32Array', current], id];
494
494
  }
495
495
  if (constructorCheck<Uint8Array>(current, Uint8Array)) {
496
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Uint8Array"');
496
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Uint8Array"');
497
497
  return [SerovalNodeType.TypedArray, ['Uint8Array', current], id];
498
498
  }
499
499
  if (constructorCheck<Uint16Array>(current, Uint16Array)) {
500
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Uint16Array"');
500
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Uint16Array"');
501
501
  return [SerovalNodeType.TypedArray, ['Uint16Array', current], id];
502
502
  }
503
503
  if (constructorCheck<Uint32Array>(current, Uint32Array)) {
504
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Uint32Array"');
504
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Uint32Array"');
505
505
  return [SerovalNodeType.TypedArray, ['Uint32Array', current], id];
506
506
  }
507
507
  if (constructorCheck<Uint8ClampedArray>(current, Uint8ClampedArray)) {
508
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Uint8ClampedArray"');
508
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Uint8ClampedArray"');
509
509
  return [SerovalNodeType.TypedArray, ['Uint8ClampedArray', current], id];
510
510
  }
511
511
  if (constructorCheck<Float32Array>(current, Float32Array)) {
512
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Float32Array"');
512
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Float32Array"');
513
513
  return [SerovalNodeType.TypedArray, ['Float32Array', current], id];
514
514
  }
515
515
  if (constructorCheck<Float64Array>(current, Float64Array)) {
516
- assert(ctx.features.has('typed-arrays'), 'Unsupported value type "Float64Array"');
516
+ assert(ctx.features.has(Feature.TypedArray), 'Unsupported value type "Float64Array"');
517
517
  return [SerovalNodeType.TypedArray, ['Float64Array', current], id];
518
518
  }
519
519
  if (constructorCheck<BigInt64Array>(current, BigInt64Array)) {
520
520
  assert(
521
- ctx.features.has('typed-arrays')
522
- && ctx.features.has('bigint'),
521
+ ctx.features.has(Feature.TypedArray)
522
+ && ctx.features.has(Feature.BigInt),
523
523
  'Unsupported value type "BigInt64Array"',
524
524
  );
525
525
  return [SerovalNodeType.TypedArray, ['BigInt64Array', current], id];
526
526
  }
527
527
  if (constructorCheck<BigUint64Array>(current, BigUint64Array)) {
528
528
  assert(
529
- ctx.features.has('typed-arrays')
530
- && ctx.features.has('bigint'),
529
+ ctx.features.has(Feature.TypedArray)
530
+ && ctx.features.has(Feature.BigInt),
531
531
  'Unsupported value type "BigUint64Array"',
532
532
  );
533
533
  return [SerovalNodeType.TypedArray, ['BigUint64Array', current], id];
@@ -578,7 +578,7 @@ export function generateTreeSync(
578
578
  return semiPrimitive;
579
579
  }
580
580
  if (constructorCheck<Set<ServerValue>>(current, Set)) {
581
- assert(ctx.features.has('set'), 'Unsupported type "Set"');
581
+ assert(ctx.features.has(Feature.Set), 'Unsupported type "Set"');
582
582
  const nodes: SerovalNode[] = [];
583
583
  const deferred: ServerValue[] = [];
584
584
  for (const item of current.keys()) {
@@ -596,7 +596,7 @@ export function generateTreeSync(
596
596
  return [SerovalNodeType.Set, nodes, id];
597
597
  }
598
598
  if (constructorCheck<Map<ServerValue, ServerValue>>(current, Map)) {
599
- assert(ctx.features.has('map'), 'Unsupported type "Map"');
599
+ assert(ctx.features.has(Feature.Map), 'Unsupported type "Map"');
600
600
  const keyNodes: SerovalNode[] = [];
601
601
  const valueNodes: SerovalNode[] = [];
602
602
  const deferredKey: ServerValue[] = [];
@@ -639,7 +639,7 @@ export function generateTreeSync(
639
639
  }
640
640
  return [SerovalNodeType.Array, nodes, id];
641
641
  }
642
- if (current instanceof AggregateError && ctx.features.has('aggregate-error')) {
642
+ if (current instanceof AggregateError && ctx.features.has(Feature.AggregateError)) {
643
643
  const options = getErrorOptions(current);
644
644
  const optionsNode = options
645
645
  ? serializeProperties(options)
@@ -659,7 +659,7 @@ export function generateTreeSync(
659
659
  ];
660
660
  }
661
661
  if (isIterable(current)) {
662
- assert(ctx.features.has('symbol-iterator'), 'Unsupported type "Iterable"');
662
+ assert(ctx.features.has(Feature.SymbolIterator), 'Unsupported type "Iterable"');
663
663
  const options = getIterableOptions(current);
664
664
  return [SerovalNodeType.Iterable, [
665
665
  // Parse options first before the items
@@ -719,7 +719,7 @@ export async function generateTreeAsync(
719
719
  return semiPrimitive;
720
720
  }
721
721
  if (isPromise(current)) {
722
- assert(ctx.features.has('promise'), 'Unsupported type "Promise"');
722
+ assert(ctx.features.has(Feature.Promise), 'Unsupported type "Promise"');
723
723
  return current.then(async (value) => [
724
724
  SerovalNodeType.Promise,
725
725
  await generateTreeAsync(ctx, value),
@@ -727,7 +727,7 @@ export async function generateTreeAsync(
727
727
  ]);
728
728
  }
729
729
  if (constructorCheck<Set<AsyncServerValue>>(current, Set)) {
730
- assert(ctx.features.has('set'), 'Unsupported type "Set"');
730
+ assert(ctx.features.has(Feature.Set), 'Unsupported type "Set"');
731
731
  const nodes: SerovalNode[] = [];
732
732
  const deferred: AsyncServerValue[] = [];
733
733
  for (const item of current.keys()) {
@@ -743,7 +743,7 @@ export async function generateTreeAsync(
743
743
  return [SerovalNodeType.Set, nodes, id];
744
744
  }
745
745
  if (constructorCheck<Map<AsyncServerValue, AsyncServerValue>>(current, Map)) {
746
- assert(ctx.features.has('map'), 'Unsupported type "Map"');
746
+ assert(ctx.features.has(Feature.Map), 'Unsupported type "Map"');
747
747
  const keyNodes: SerovalNode[] = [];
748
748
  const valueNodes: SerovalNode[] = [];
749
749
  const deferredKey: AsyncServerValue[] = [];
@@ -806,7 +806,7 @@ export async function generateTreeAsync(
806
806
  ];
807
807
  }
808
808
  if (isIterable(current)) {
809
- assert(ctx.features.has('symbol-iterator'), 'Unsupported type "Iterable"');
809
+ assert(ctx.features.has(Feature.SymbolIterator), 'Unsupported type "Iterable"');
810
810
  const options = getIterableOptions(current);
811
811
  return [SerovalNodeType.Iterable, [
812
812
  options
@@ -844,7 +844,7 @@ export function serializePrimitive(
844
844
  return 'null';
845
845
  }
846
846
  if (typeof value === 'bigint') {
847
- assert(ctx.features.has('bigint'), 'Unsupported type "BigInt"');
847
+ assert(ctx.features.has(Feature.BigInt), 'Unsupported type "BigInt"');
848
848
  return `${value}n`;
849
849
  }
850
850
  if (typeof value === 'string') {
@@ -1089,7 +1089,7 @@ export function serializeTree(
1089
1089
  // either through class or custom properties
1090
1090
  // Make sure to assign extra properties
1091
1091
  if (value[1]) {
1092
- if (ctx.features.has('object-assign')) {
1092
+ if (ctx.features.has(Feature.ObjectAssign)) {
1093
1093
  const options = serializeObject(id, value[1]);
1094
1094
  serialized = `Object.assign(${serialized},${options})`;
1095
1095
  } else {
@@ -1106,7 +1106,7 @@ export function serializeTree(
1106
1106
  case SerovalNodeType.Error: {
1107
1107
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1108
1108
  if (value[2]) {
1109
- if (ctx.features.has('object-assign')) {
1109
+ if (ctx.features.has(Feature.ObjectAssign)) {
1110
1110
  const options = serializeObject(id, value[2]);
1111
1111
  serialized = `Object.assign(${serialized},${options})`;
1112
1112
  } else {
@@ -1126,20 +1126,20 @@ export function serializeTree(
1126
1126
  const values = serializeTree(ctx, value[1]);
1127
1127
  ctx.stack = parent;
1128
1128
  let serialized: string;
1129
- if (ctx.features.has('array-values')) {
1129
+ if (ctx.features.has(Feature.ArrayPrototypeValues)) {
1130
1130
  serialized = `${values}.values()`;
1131
1131
  } else {
1132
1132
  serialized = `${values}[Symbol.iterator]()`;
1133
1133
  }
1134
- if (ctx.features.has('arrow-function')) {
1134
+ if (ctx.features.has(Feature.ArrowFunction)) {
1135
1135
  serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1136
- } else if (ctx.features.has('method-shorthand')) {
1136
+ } else if (ctx.features.has(Feature.MethodShorthand)) {
1137
1137
  serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1138
1138
  } else {
1139
1139
  serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1140
1140
  }
1141
1141
  if (value[0]) {
1142
- if (ctx.features.has('object-assign')) {
1142
+ if (ctx.features.has(Feature.ObjectAssign)) {
1143
1143
  const options = serializeObject(id, value[0]);
1144
1144
  serialized = `Object.assign(${serialized},${options})`;
1145
1145
  } else {
@@ -1155,7 +1155,7 @@ export function serializeTree(
1155
1155
  }
1156
1156
  case SerovalNodeType.NullConstructor: {
1157
1157
  let serialized = 'Object.create(null)';
1158
- if (ctx.features.has('object-assign')) {
1158
+ if (ctx.features.has(Feature.ObjectAssign)) {
1159
1159
  const fields = serializeObject(id, value);
1160
1160
  if (fields !== '{}') {
1161
1161
  serialized = `Object.assign(${serialized},${fields})`;