Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.13.0 #30

Merged
merged 84 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
b805747
Rework Iterable serialization and function string output
lxsmnsyc Nov 9, 2023
e516712
Fix iterator output
lxsmnsyc Nov 9, 2023
c351ff4
Add special reference state
lxsmnsyc Nov 9, 2023
a0054f7
Remove special reference state, add new `x` field
lxsmnsyc Nov 9, 2023
ae33b94
Remove `MethodShorthand` and `ArrayPrototypeValues`
lxsmnsyc Nov 14, 2023
81ef387
Rename`SPECIAL_REF_SYMBOL`
lxsmnsyc Nov 14, 2023
96a3b32
Change `SPECIAL_REFS` to a mapper function
lxsmnsyc Nov 14, 2023
b3fbdb5
Remove use of symbols
lxsmnsyc Nov 14, 2023
c538439
Revert special refs map
lxsmnsyc Nov 14, 2023
70481ee
Merge remote-tracking branch 'origin/main' into next
lxsmnsyc Nov 15, 2023
f31b058
Merge remote-tracking branch 'origin/main' into next
lxsmnsyc Nov 15, 2023
ccc3538
Rework SpecialReference node
lxsmnsyc Nov 15, 2023
2dc97b4
Dedupe Boxed node
lxsmnsyc Nov 15, 2023
73c1e49
Dedupe TypedArray node
lxsmnsyc Nov 15, 2023
25d67a6
Dedupe BigIntTypedArray node
lxsmnsyc Nov 15, 2023
c3b4a50
Dedupe DataView
lxsmnsyc Nov 15, 2023
8f44c1d
Dedupe Error node
lxsmnsyc Nov 15, 2023
2ed83e5
Dedupe Map node
lxsmnsyc Nov 15, 2023
5173557
Dedupe Set node
lxsmnsyc Nov 15, 2023
4488954
Dedupe Event node
lxsmnsyc Nov 15, 2023
2bd5a97
Dedupe CustomEvent node
lxsmnsyc Nov 15, 2023
f6881d7
Dedupe AggregateError node
lxsmnsyc Nov 15, 2023
4a3439f
Change `ReadableStream` serialization
lxsmnsyc Nov 15, 2023
4b78094
Fix snapshots
lxsmnsyc Nov 15, 2023
c7308f7
Add initial support for `AsyncIterable`
lxsmnsyc Nov 15, 2023
3a04d32
Fix `AsyncIterable` serialization
lxsmnsyc Nov 15, 2023
2f7619b
Fix tests and AsyncIterable deserialization
lxsmnsyc Nov 16, 2023
d11a178
Remove unused file
lxsmnsyc Nov 16, 2023
20cd59c
Move files
lxsmnsyc Nov 16, 2023
cbdeba5
Move files pt. 2
lxsmnsyc Nov 16, 2023
f19085b
Move files pt. 3
lxsmnsyc Nov 16, 2023
d3858ec
Move files pt. 4
lxsmnsyc Nov 16, 2023
be8c025
Fix vanilla constructors
lxsmnsyc Nov 16, 2023
dc9fe3a
Add streaming Promise to deserializer
lxsmnsyc Nov 16, 2023
43dc146
Add `CrossDeserializerContext`
lxsmnsyc Nov 16, 2023
5ae19bf
Add `ReadableStream` support to deserializer
lxsmnsyc Nov 16, 2023
aab35b0
Add Cross JSON APIs
lxsmnsyc Nov 16, 2023
517f535
Fix Cross JSON output
lxsmnsyc Nov 16, 2023
51b3420
Update web-api tests
lxsmnsyc Nov 16, 2023
771ccce
Update array test
lxsmnsyc Nov 16, 2023
9caba20
Update async-iterable test
lxsmnsyc Nov 16, 2023
55902d8
Update bigint test
lxsmnsyc Nov 16, 2023
747f981
Fix boolean test
lxsmnsyc Nov 17, 2023
5329f1a
Fix boxed-bigint test
lxsmnsyc Nov 17, 2023
5f63c21
Fix boxed-boolean test
lxsmnsyc Nov 17, 2023
5d8b827
Fix boxed-number test
lxsmnsyc Nov 17, 2023
66762bd
Fix boxed-string test
lxsmnsyc Nov 17, 2023
8deea9f
Fix string test
lxsmnsyc Nov 17, 2023
14820e5
Fix number test
lxsmnsyc Nov 17, 2023
ab88689
Fix data-view test
lxsmnsyc Nov 17, 2023
100c096
Fix date test
lxsmnsyc Nov 17, 2023
8bd783f
Fix error test
lxsmnsyc Nov 17, 2023
59d8ae1
Fix frozen-object test
lxsmnsyc Nov 17, 2023
1c0c47b
Fix sealed-object test
lxsmnsyc Nov 17, 2023
f87636a
Fix object test
lxsmnsyc Nov 17, 2023
956388c
Fix null-constructor test
lxsmnsyc Nov 17, 2023
7a2aef1
Fix iterable test
lxsmnsyc Nov 17, 2023
ebc3039
Fix map test
lxsmnsyc Nov 17, 2023
9aded45
Fix mutual-cycle test
lxsmnsyc Nov 17, 2023
5e76dcf
Fix iterator deserialization
lxsmnsyc Nov 17, 2023
8ad6de0
Fix plugins parsing and reference assignment
lxsmnsyc Nov 17, 2023
608aecc
Fix plugin test
lxsmnsyc Nov 17, 2023
afabe2e
Fix reference test
lxsmnsyc Nov 17, 2023
42ed6d0
Fix regexp test
lxsmnsyc Nov 17, 2023
6ea5ae0
Fix set test
lxsmnsyc Nov 17, 2023
e56b5cd
Fix sparse-array test
lxsmnsyc Nov 17, 2023
eab6451
Fix typed-array test
lxsmnsyc Nov 17, 2023
0e0bad6
Fix wk-symbols test
lxsmnsyc Nov 17, 2023
cb8fa0b
Fix async-iterable deserializer on cross mode
lxsmnsyc Nov 17, 2023
6eadab7
Fix async-iterable test
lxsmnsyc Nov 17, 2023
7654b7f
Fix function output
lxsmnsyc Nov 17, 2023
6c7b56f
Fix tests
lxsmnsyc Nov 17, 2023
0828d65
Rename special references
lxsmnsyc Nov 17, 2023
0ae7e6f
Rework special references again
lxsmnsyc Nov 17, 2023
dab428a
Add support for ReadableStream in async mode
lxsmnsyc Nov 18, 2023
1d3fc00
Fix ReadableStream in async mode
lxsmnsyc Nov 18, 2023
66f5440
Delete iterator.js
lxsmnsyc Nov 18, 2023
7b03f61
Create theory.js
lxsmnsyc Nov 18, 2023
37dae53
Update docs
lxsmnsyc Nov 18, 2023
0d68d71
Add `Symbol.toStringTag` and `Symbol.isConcatSpreadable` support
lxsmnsyc Nov 18, 2023
3c2bf67
Fix symbol property parsing
lxsmnsyc Nov 18, 2023
1ba379e
minor fix
lxsmnsyc Nov 18, 2023
6e99a5b
Fix loop guard on streaming
lxsmnsyc Nov 18, 2023
c3e253e
Update deserializer.ts
lxsmnsyc Nov 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,17 @@ 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`.
- Uses function expressions for `Iterable`
- Uses function expressions for `AsyncIterable`
- [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
- Disables serialization of `BigInt`, `BigInt64Array` and `BigUint64Array`
- [`ErrorPrototypeStack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack)
- Skipped when detected.
- 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)`
Expand Down Expand Up @@ -91,9 +87,9 @@ By default, all feature flags are enabled. The following are the feature flags a

## Supported Types

- sync = `serialize`, `toJSON`, `crossSerialize`
- async = `serializeAsync`, `toJSONAsync`, `crossSerializeAsync`
- streaming = `crossSerializeStream`, `Serializer`
- sync = `serialize`, `toJSON`, `crossSerialize`, `toCrossJSON`
- async = `serializeAsync`, `toJSONAsync`, `crossSerializeAsync`, `toCrossJSONAsync`
- streaming = `crossSerializeStream`, `toCrossJSONStream`, `Serializer`

| Type | sync | async | streaming |
| --- | --- | --- | --- |
Expand Down
6 changes: 3 additions & 3 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- `crossSerialize`
- `crossSerializeAsync`
- `crossSerializeStream`
- `toCrossJSON`
- `toCrossJSONAsync`
- `toCrossJSONStream`

## Basic serialization

Expand Down Expand Up @@ -117,8 +120,5 @@ const BufferPlugin = createPlugin<Buffer, SerovalNode>({
deserialize(node, ctx) {
return Buffer.from(ctx.deserialize(node) as string, 'base64');
},
isIterable() {
return true;
},
});
```
21 changes: 10 additions & 11 deletions packages/seroval/assets/global-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ self._$ = self._$ || {
uS(stream) {
delete stream.c;
},
Se(stream, type, data, controller) {
controller = stream.c;
switch (type) {
case 0: return controller.enqueue(data);
case 1:
this.uS(stream);
return controller.error(data);
case 2:
this.uS(stream);
return controller.close();
}
Se(stream, data) {
stream.c.enqueue(data);
},
St(stream, data) {
stream.c.error(data);
this.uS(stream);
},
Sc(stream) {
stream.c.close();
this.uS(stream);
},
// ReadableStream constructor
S(stream, controller) {
Expand Down
230 changes: 229 additions & 1 deletion packages/seroval/src/core/base-primitives.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from './assert';
import type { BigIntTypedArrayValue, TypedArrayValue } from '../types';
import assert from './utils/assert';
import type { WellKnownSymbols } from './constants';
import { INV_SYMBOL_REF, SerovalNodeType } from './constants';
import {
Expand All @@ -8,20 +9,34 @@ import {
NEG_ZERO_NODE,
} from './literals';
import { getReferenceID } from './reference';
import { getErrorConstructor } from './utils/error';
import { serializeString } from './string';
import type {
SerovalAggregateErrorNode,
SerovalArrayBufferNode,
SerovalArrayNode,
SerovalAsyncIteratorFactoryInstanceNode,
SerovalBigIntNode,
SerovalBigIntTypedArrayNode,
SerovalBoxedNode,
SerovalConstantNode,
SerovalDataViewNode,
SerovalDateNode,
SerovalErrorNode,
SerovalIndexedValueNode,
SerovalIteratorFactoryInstanceNode,
SerovalNode,
SerovalNumberNode,
SerovalObjectRecordNode,
SerovalPluginNode,
SerovalReferenceNode,
SerovalRegExpNode,
SerovalSetNode,
SerovalStringNode,
SerovalTypedArrayNode,
SerovalWKSymbolNode,
} from './types';
import { getObjectFlag } from './utils/get-object-flag';

export function createNumberNode(value: number): SerovalConstantNode | SerovalNumberNode {
switch (value) {
Expand Down Expand Up @@ -228,3 +243,216 @@ export function createPluginNode(
o: undefined,
};
}

export function createArrayNode(
id: number,
current: unknown[],
parsedItems: SerovalNode[],
): SerovalArrayNode {
return {
t: SerovalNodeType.Array,
i: id,
s: undefined,
l: current.length,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: parsedItems,
f: undefined,
b: undefined,
o: getObjectFlag(current),
};
}

export function createBoxedNode(
id: number,
boxed: SerovalNode,
): SerovalBoxedNode {
return {
t: SerovalNodeType.Boxed,
i: id,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: boxed,
b: undefined,
o: undefined,
};
}

export function createTypedArrayNode(
id: number,
current: TypedArrayValue,
buffer: SerovalNode,
): SerovalTypedArrayNode {
return {
t: SerovalNodeType.TypedArray,
i: id,
s: undefined,
l: current.length,
c: current.constructor.name,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: buffer,
b: current.byteOffset,
o: undefined,
};
}

export function createBigIntTypedArrayNode(
id: number,
current: BigIntTypedArrayValue,
buffer: SerovalNode,
): SerovalBigIntTypedArrayNode {
return {
t: SerovalNodeType.BigIntTypedArray,
i: id,
s: undefined,
l: current.length,
c: current.constructor.name,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: buffer,
b: current.byteOffset,
o: undefined,
};
}

export function createDataViewNode(
id: number,
current: DataView,
buffer: SerovalNode,
): SerovalDataViewNode {
return {
t: SerovalNodeType.DataView,
i: id,
s: undefined,
l: current.byteLength,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: buffer,
b: current.byteOffset,
o: undefined,
};
}

export function createErrorNode(
id: number,
current: Error,
options: SerovalObjectRecordNode | undefined,
): SerovalErrorNode {
return {
t: SerovalNodeType.Error,
i: id,
s: getErrorConstructor(current),
l: undefined,
c: undefined,
m: serializeString(current.message),
p: options,
e: undefined,
a: undefined,
f: undefined,
b: undefined,
o: undefined,
};
}

export function createAggregateErrorNode(
id: number,
current: AggregateError,
options: SerovalObjectRecordNode | undefined,
): SerovalAggregateErrorNode {
return {
t: SerovalNodeType.AggregateError,
i: id,
s: getErrorConstructor(current),
l: undefined,
c: undefined,
m: serializeString(current.message),
p: options,
e: undefined,
a: undefined,
f: undefined,
b: undefined,
o: undefined,
};
}

export function createSetNode(
id: number,
size: number,
items: SerovalNode[],
): SerovalSetNode {
return {
t: SerovalNodeType.Set,
i: id,
s: undefined,
l: size,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: items,
f: undefined,
b: undefined,
o: undefined,
};
}

export function createIteratorFactoryInstanceNode(
factory: SerovalNode,
items: SerovalNode,
): SerovalIteratorFactoryInstanceNode {
return {
t: SerovalNodeType.IteratorFactoryInstance,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: [
factory,
items,
],
f: undefined,
b: undefined,
o: undefined,
};
}

export function createAsyncIteratorFactoryInstanceNode(
factory: SerovalNode,
items: SerovalNode,
): SerovalAsyncIteratorFactoryInstanceNode {
return {
t: SerovalNodeType.AsyncIteratorFactoryInstance,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: [
factory,
items,
],
f: undefined,
b: undefined,
o: undefined,
};
}
2 changes: 0 additions & 2 deletions packages/seroval/src/core/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions packages/seroval/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export const enum SerovalNodeType {
CustomEvent = 38,
DOMException = 39,
Plugin = 40,
MapSentinel = 41,
IteratorFactory = 42,
IteratorFactoryInstance = 43,
AsyncIteratorFactory = 44,
AsyncIteratorFactoryInstance = 45,
ReadableStream = 46,
ReadableStreamFactory = 47,
}

export const enum SerovalObjectFlags {
Expand Down Expand Up @@ -186,5 +193,3 @@ export const ERROR_CONSTRUCTOR: Record<ErrorConstructorTag, ErrorConstructors> =
[ErrorConstructorTag.TypeError]: TypeError,
[ErrorConstructorTag.URIError]: URIError,
};

export const UNIVERSAL_SENTINEL = Symbol('why');
Loading
Loading