Skip to content

Commit

Permalink
Merge pull request #32 from lxsmnsyc/feature-lazy-script
Browse files Browse the repository at this point in the history
Deprecate global header
  • Loading branch information
lxsmnsyc authored Nov 22, 2023
2 parents 7740de3 + 48d25f0 commit e0d928b
Show file tree
Hide file tree
Showing 53 changed files with 883 additions and 889 deletions.
4 changes: 2 additions & 2 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"next-json": "^0.2.2",
"o-son": "^1.0.1",
"serialize-javascript": "^6.0.1",
"seroval": "workspace:^0.13.1",
"seroval": "workspace:^0.13.2",
"superjson": "^1.13.3",
"tosource": "2.0.0-alpha.3",
"warp10": "^2.1.0"
Expand All @@ -43,5 +43,5 @@
"typesVersions": {
"*": {}
},
"version": "0.13.1"
"version": "0.13.2"
}
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "0.13.1"
"version": "0.13.2"
}
2 changes: 1 addition & 1 deletion packages/seroval/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "seroval",
"type": "module",
"version": "0.13.1",
"version": "0.13.2",
"files": [
"dist",
"src"
Expand Down
5 changes: 3 additions & 2 deletions packages/seroval/src/core/base-primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
SerovalIndexedValueNode,
SerovalIteratorFactoryInstanceNode,
SerovalNode,
SerovalNodeWithID,
SerovalNumberNode,
SerovalObjectRecordNode,
SerovalPluginNode,
Expand Down Expand Up @@ -412,7 +413,7 @@ export function createSetNode(
}

export function createIteratorFactoryInstanceNode(
factory: SerovalNode,
factory: SerovalNodeWithID,
items: SerovalNode,
): SerovalIteratorFactoryInstanceNode {
return {
Expand All @@ -435,7 +436,7 @@ export function createIteratorFactoryInstanceNode(
}

export function createAsyncIteratorFactoryInstanceNode(
factory: SerovalNode,
factory: SerovalNodeWithID,
items: SerovalNode,
): SerovalAsyncIteratorFactoryInstanceNode {
return {
Expand Down
3 changes: 1 addition & 2 deletions packages/seroval/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ export const enum SerovalNodeType {
CustomEvent = 38,
DOMException = 39,
Plugin = 40,
MapSentinel = 41,
SpecialReference = 41,
IteratorFactory = 42,
IteratorFactoryInstance = 43,
AsyncIteratorFactory = 44,
AsyncIteratorFactoryInstance = 45,
ReadableStream = 46,
ReadableStreamFactory = 47,
}

export const enum SerovalObjectFlags {
Expand Down
13 changes: 6 additions & 7 deletions packages/seroval/src/core/context/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
const deferred = this.refs.get(node.i) as Deferred | undefined;
assert(deferred, new Error('Missing Promise instance.'));
deferred.resolve(
this.deserialize(node.f),
this.deserialize(node.a[1]),
);
return undefined;
}
Expand All @@ -458,7 +458,7 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
const deferred = this.refs.get(node.i) as Deferred | undefined;
assert(deferred, new Error('Missing Promise instance.'));
deferred.reject(
this.deserialize(node.f),
this.deserialize(node.a[1]),
);
return undefined;
}
Expand All @@ -476,7 +476,7 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
const deferred = this.refs.get(node.i) as DeferredStream | undefined;
assert(deferred, new Error('Missing ReadableStream instance.'));
deferred.enqueue(
this.deserialize(node.f),
this.deserialize(node.a[1]),
);
return undefined;
}
Expand All @@ -485,7 +485,7 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
const deferred = this.refs.get(node.i) as DeferredStream | undefined;
assert(deferred, new Error('Missing Promise instance.'));
deferred.error(
this.deserialize(node.f),
this.deserialize(node.a[1]),
);
return undefined;
}
Expand All @@ -508,7 +508,7 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
node: SerovalAsyncIteratorFactoryInstanceNode,
): unknown {
const source = this.deserialize(node.a[1]);
if ((source as object).constructor === ReadableStream) {
if (node.s) {
return readableStreamToAsyncIterator(
source as ReadableStream<SerializedAsyncIteratorResult<unknown>>,
);
Expand Down Expand Up @@ -615,10 +615,9 @@ export default abstract class BaseDeserializerContext implements PluginAccessOpt
return this.deserializeAsyncIteratorFactoryInstance(node);
case SerovalNodeType.ReadableStream:
return this.deserializeReadableStream(node);
case SerovalNodeType.MapSentinel:
case SerovalNodeType.SpecialReference:
case SerovalNodeType.IteratorFactory:
case SerovalNodeType.AsyncIteratorFactory:
case SerovalNodeType.ReadableStreamFactory:
default:
throw new Error('invariant');
}
Expand Down
54 changes: 16 additions & 38 deletions packages/seroval/src/core/context/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ import {
import type { Plugin, PluginAccessOptions, SerovalMode } from '../plugin';
import { hasReferenceID } from '../reference';
import {
SpecialReference,
ASYNC_ITERATOR,
ITERATOR,
MAP_SENTINEL,
READABLE_STREAM,
SPECIAL_REFS,
} from '../special-reference';
import type {
SerovalAsyncIteratorFactoryNode,
SerovalIndexedValueNode,
SerovalIteratorFactoryNode,
SerovalMapNode,
SerovalMapSentinelNode,
SerovalNode,
SerovalNullConstructorNode,
SerovalObjectNode,
SerovalObjectRecordNode,
SerovalReadableStreamFactoryNode,
SerovalReferenceNode,
SerovalSpecialReferenceNode,
SerovalWKSymbolNode,
} from '../types';
import { getObjectFlag } from '../utils/get-object-flag';
Expand Down Expand Up @@ -115,18 +114,21 @@ export abstract class BaseParserContext implements PluginAccessOptions {
return createReferenceNode(id, current);
}

protected parseMapSentinel(): SerovalIndexedValueNode | SerovalMapSentinelNode {
const registeredID = this.refs.get(MAP_SENTINEL);
protected parseSpecialReference(
ref: SpecialReference,
): SerovalIndexedValueNode | SerovalSpecialReferenceNode {
const specialRef = SPECIAL_REFS[ref];
const registeredID = this.refs.get(specialRef);
if (registeredID != null) {
this.markRef(registeredID);
return createIndexedValueNode(registeredID);
}
const id = this.refs.size;
this.refs.set(MAP_SENTINEL, id);
this.refs.set(specialRef, id);
return {
t: SerovalNodeType.MapSentinel,
t: SerovalNodeType.SpecialReference,
i: id,
s: undefined,
s: ref,
l: undefined,
c: undefined,
m: undefined,
Expand Down Expand Up @@ -163,7 +165,9 @@ export abstract class BaseParserContext implements PluginAccessOptions {
};
}

protected parseAsyncIteratorFactory(): SerovalIndexedValueNode | SerovalAsyncIteratorFactoryNode {
protected parseAsyncIteratorFactory(
streaming: 0 | 1,
): SerovalIndexedValueNode | SerovalAsyncIteratorFactoryNode {
const registeredID = this.refs.get(ASYNC_ITERATOR);
if (registeredID != null) {
this.markRef(registeredID);
Expand All @@ -174,7 +178,7 @@ export abstract class BaseParserContext implements PluginAccessOptions {
return {
t: SerovalNodeType.AsyncIteratorFactory,
i: id,
s: undefined,
s: streaming,
l: undefined,
c: undefined,
m: undefined,
Expand Down Expand Up @@ -225,33 +229,7 @@ export abstract class BaseParserContext implements PluginAccessOptions {
p: undefined,
e: { k, v, s },
a: undefined,
f: this.parseMapSentinel(),
b: undefined,
o: undefined,
};
}

protected parseReadableStreamFactory(): (
SerovalIndexedValueNode | SerovalReadableStreamFactoryNode
) {
const registeredID = this.refs.get(READABLE_STREAM);
if (registeredID != null) {
this.markRef(registeredID);
return createIndexedValueNode(registeredID);
}
const id = this.refs.size;
this.refs.set(READABLE_STREAM, id);
return {
t: SerovalNodeType.ReadableStreamFactory,
i: id,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: undefined,
f: this.parseSpecialReference(SpecialReference.MapSentinel),
b: undefined,
o: undefined,
};
Expand Down
6 changes: 3 additions & 3 deletions packages/seroval/src/core/context/parser/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import {
createCustomEventNode,
createReadableStreamNode,
} from '../../web-api';
import { UNIVERSAL_SENTINEL } from '../../special-reference';
import { SpecialReference, UNIVERSAL_SENTINEL } from '../../special-reference';

type ObjectLikeNode =
| SerovalObjectNode
Expand Down Expand Up @@ -147,7 +147,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
);
valueNodes.push(
createAsyncIteratorFactoryInstanceNode(
this.parseAsyncIteratorFactory(),
this.parseAsyncIteratorFactory(0),
await this.parse(
await asyncIteratorToSequence(properties as unknown as AsyncIterable<unknown>),
),
Expand Down Expand Up @@ -489,7 +489,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
): Promise<SerovalReadableStreamNode> {
return createReadableStreamNode(
id,
this.parseReadableStreamFactory(),
this.parseSpecialReference(SpecialReference.ReadableStream),
await this.parse(
await readableStreamToSequence(current),
),
Expand Down
44 changes: 30 additions & 14 deletions packages/seroval/src/core/context/parser/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
} from '../../types';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../../web-api';
import { asyncIteratorToReadableStream, iteratorToSequence } from '../../utils/iterator-to-sequence';
import { UNIVERSAL_SENTINEL } from '../../special-reference';
import { SpecialReference, UNIVERSAL_SENTINEL } from '../../special-reference';

export interface BaseStreamParserContextOptions extends BaseSyncParserContextOptions {
onParse: (node: SerovalNode, initial: boolean) => void;
Expand Down Expand Up @@ -57,7 +57,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
}

private onParse(node: SerovalNode, initial: boolean): void {
this.onParseCallback(node, initial);
try {
this.onParseCallback(node, initial);
} catch (error) {
this.onError(error);
}
}

private onError(error: unknown): void {
Expand Down Expand Up @@ -128,7 +132,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
);
valueNodes.push(
createAsyncIteratorFactoryInstanceNode(
this.parseAsyncIteratorFactory(),
this.parseAsyncIteratorFactory(1),
this.parse(
asyncIteratorToReadableStream(
properties as unknown as AsyncIterable<unknown>,
Expand Down Expand Up @@ -174,7 +178,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
p: undefined,
e: undefined,
a: undefined,
f: undefined,
f: this.parseSpecialReference(SpecialReference.ReadableStreamClose),
b: undefined,
o: undefined,
}, false);
Expand All @@ -191,8 +195,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parsed,
a: [
this.parseSpecialReference(SpecialReference.ReadableStreamEnqueue),
parsed,
],
f: undefined,
b: undefined,
o: undefined,
}, false);
Expand All @@ -214,8 +221,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parsed,
a: [
this.parseSpecialReference(SpecialReference.ReadableStreamError),
parsed,
],
f: undefined,
b: undefined,
o: undefined,
}, false);
Expand Down Expand Up @@ -244,7 +254,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
p: undefined,
e: undefined,
a: undefined,
f: undefined,
f: this.parseSpecialReference(SpecialReference.ReadableStreamConstructor),
b: undefined,
o: undefined,
};
Expand Down Expand Up @@ -314,8 +324,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parsed,
a: [
this.parseSpecialReference(SpecialReference.PromiseResolve),
parsed,
],
f: undefined,
b: undefined,
o: undefined,
}, false);
Expand All @@ -335,8 +348,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parsed,
a: [
this.parseSpecialReference(SpecialReference.PromiseReject),
parsed,
],
f: undefined,
b: undefined,
o: undefined,
}, false);
Expand All @@ -356,7 +372,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
p: undefined,
e: undefined,
a: undefined,
f: undefined,
f: this.parseSpecialReference(SpecialReference.PromiseConstructor),
b: undefined,
o: undefined,
};
Expand Down
Loading

0 comments on commit e0d928b

Please sign in to comment.