Skip to content

Commit

Permalink
Rework special references again
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Nov 17, 2023
1 parent 0828d65 commit 0ae7e6f
Show file tree
Hide file tree
Showing 29 changed files with 627 additions and 700 deletions.
66 changes: 48 additions & 18 deletions packages/seroval/src/core/base-primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
SerovalAggregateErrorNode,
SerovalArrayBufferNode,
SerovalArrayNode,
SerovalAsyncIteratorFactoryInstanceNode,
SerovalBigIntNode,
SerovalBigIntTypedArrayNode,
SerovalBoxedNode,
Expand All @@ -23,6 +24,7 @@ import type {
SerovalDateNode,
SerovalErrorNode,
SerovalIndexedValueNode,
SerovalIteratorFactoryInstanceNode,
SerovalNode,
SerovalNumberNode,
SerovalObjectRecordNode,
Expand Down Expand Up @@ -63,7 +65,6 @@ export function createNumberNode(value: number): SerovalConstantNode | SerovalNu
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}
}
Expand All @@ -82,7 +83,6 @@ export function createStringNode(value: string): SerovalStringNode {
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -102,7 +102,6 @@ export function createBigIntNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -120,7 +119,6 @@ export function createIndexedValueNode(id: number): SerovalIndexedValueNode {
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -138,7 +136,6 @@ export function createDateNode(id: number, current: Date): SerovalDateNode {
a: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -156,7 +153,6 @@ export function createRegExpNode(id: number, current: RegExp): SerovalRegExpNode
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -183,7 +179,6 @@ export function createArrayBufferNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -205,7 +200,6 @@ export function createWKSymbolNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -226,7 +220,6 @@ export function createReferenceNode<T>(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -248,7 +241,6 @@ export function createPluginNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -270,7 +262,6 @@ export function createArrayNode(
f: undefined,
b: undefined,
o: getObjectFlag(current),
x: undefined,
};
}

Expand All @@ -291,7 +282,6 @@ export function createBoxedNode(
f: boxed,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -313,7 +303,6 @@ export function createTypedArrayNode(
f: buffer,
b: current.byteOffset,
o: undefined,
x: undefined,
};
}

Expand All @@ -335,7 +324,6 @@ export function createBigIntTypedArrayNode(
f: buffer,
b: current.byteOffset,
o: undefined,
x: undefined,
};
}

Expand All @@ -357,7 +345,6 @@ export function createDataViewNode(
f: buffer,
b: current.byteOffset,
o: undefined,
x: undefined,
};
}

Expand All @@ -379,7 +366,6 @@ export function createErrorNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -401,7 +387,6 @@ export function createAggregateErrorNode(
f: undefined,
b: undefined,
o: undefined,
x: undefined,
};
}

Expand All @@ -423,6 +408,51 @@ export function createSetNode(
f: undefined,
b: undefined,
o: undefined,
x: 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,
};
}
6 changes: 5 additions & 1 deletion packages/seroval/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export const enum SerovalNodeType {
Plugin = 40,
MapSentinel = 41,
IteratorFactory = 42,
AsyncIteratorFactory = 43,
IteratorFactoryInstance = 43,
AsyncIteratorFactory = 44,
AsyncIteratorFactoryInstance = 45,
ReadableStream = 46,
ReadableStreamFactory = 47,
}

export const enum SerovalObjectFlags {
Expand Down
57 changes: 30 additions & 27 deletions packages/seroval/src/core/context/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
SerovalAggregateErrorNode,
SerovalArrayBufferNode,
SerovalArrayNode,
SerovalAsyncIteratorFactoryInstanceNode,
SerovalBigIntTypedArrayNode,
SerovalBlobNode,
SerovalBoxedNode,
Expand All @@ -18,6 +19,7 @@ import type {
SerovalFileNode,
SerovalFormDataNode,
SerovalHeadersNode,
SerovalIteratorFactoryInstanceNode,
SerovalMapNode,
SerovalNode,
SerovalNullConstructorNode,
Expand All @@ -42,9 +44,6 @@ import type {
SerovalURLNode,
SerovalURLSearchParamsNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
} from '../types';
import {
CONSTANT_VAL,
ERROR_CONSTRUCTOR,
Expand Down Expand Up @@ -132,33 +131,14 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
): Record<string | symbol, unknown> {
const len = node.s;
if (len) {
let key: SerovalObjectRecordKey;
let value: unknown;
const keys = node.k;
const vals = node.v;
for (let i = 0; i < len; i++) {
for (let i = 0, key: SerovalObjectRecordKey; i < len; i++) {
key = keys[i];
value = this.deserialize(vals[i]);
switch (key) {
case SerovalObjectRecordSpecialKey.SymbolIterator: {
const current = value as Sequence;
result[Symbol.iterator] = sequenceToIterator(current);
}
break;
case SerovalObjectRecordSpecialKey.SymbolAsyncIterator: {
const current = value;
if ((current as object).constructor === ReadableStream) {
result[Symbol.asyncIterator] = readableStreamToAsyncIterator(
current as ReadableStream<SerializedAsyncIteratorResult<unknown>>,
);
} else {
result[Symbol.asyncIterator] = sequenceToAsyncIterator(current as Sequence);
}
}
break;
default:
result[deserializeString(key)] = value;
break;
if (typeof key === 'string') {
result[deserializeString(key)] = this.deserialize(vals[i]);
} else {
result[this.deserialize(key) as symbol] = this.deserialize(vals[i]);
}
}
}
Expand Down Expand Up @@ -511,6 +491,25 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
return undefined;
}

private deserializeIteratorFactoryInstance(
node: SerovalIteratorFactoryInstanceNode,
): unknown {
const source = this.deserialize(node.a[1]);
return sequenceToIterator(source as Sequence);
}

private deserializeAsyncIteratorFactoryInstance(
node: SerovalAsyncIteratorFactoryInstanceNode,
): unknown {
const source = this.deserialize(node.a[1]);
if ((source as object).constructor === ReadableStream) {
return readableStreamToAsyncIterator(
source as ReadableStream<SerializedAsyncIteratorResult<unknown>>,
);
}
return sequenceToAsyncIterator(source as Sequence);
}

deserialize(node: SerovalNode): unknown {
switch (node.t) {
case SerovalNodeType.Constant:
Expand Down Expand Up @@ -593,6 +592,10 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
return this.deserializeReadableStreamError(node);
case SerovalNodeType.ReadableStreamClose:
return this.deserializeReadableStreamClose(node);
case SerovalNodeType.IteratorFactoryInstance:
return this.deserializeIteratorFactoryInstance(node);
case SerovalNodeType.AsyncIteratorFactoryInstance:
return this.deserializeAsyncIteratorFactoryInstance(node);
case SerovalNodeType.MapSentinel:
case SerovalNodeType.IteratorFactory:
case SerovalNodeType.AsyncIteratorFactory:
Expand Down
Loading

0 comments on commit 0ae7e6f

Please sign in to comment.