Skip to content

Commit

Permalink
Add DOMException
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Oct 10, 2023
1 parent b01810d commit a7ceb97
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ By default, all feature flags are enabled. The following are the feature flags a
| [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ||[^9][^13] |[^9][^13] |
| [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) |[^9] |[^9] |[^9] |
| [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) |[^9] |[^9] |[^9] |
| [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException) |[^9] |[^9] |[^9] |
| Cyclic references ||||
| Isomorphic references ||||

Expand Down
1 change: 1 addition & 0 deletions packages/seroval/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const enum SerovalNodeType {
Response = 36,
Event = 37,
CustomEvent = 38,
DOMException = 39,
}

export const enum SerovalObjectFlags {
Expand Down
4 changes: 3 additions & 1 deletion packages/seroval/src/core/cross/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import {
createRegExpNode,
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../web-api';
import promiseToResult from '../promise-to-result';
import {
createCustomEventOptions,
Expand Down Expand Up @@ -684,6 +684,8 @@ async function parseObject(
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
case DOMException:
return createDOMExceptionNode(id, current as unknown as DOMException);
default:
break;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/seroval/src/core/cross/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
SerovalResponseNode,
SerovalEventNode,
SerovalCustomEventNode,
SerovalDOMExceptionNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
Expand Down Expand Up @@ -808,6 +809,15 @@ function serializeCustomEvent(
);
}

function serializeDOMException(
node: SerovalDOMExceptionNode,
): string {
return assignIndexedValue(
node.i,
'new DOMException("' + node.s + '","' + node.c + '")',
);
}

export default function crossSerializeTree(
ctx: CrossSerializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -890,6 +900,8 @@ export default function crossSerializeTree(
return serializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return serializeCustomEvent(ctx, node);
case SerovalNodeType.DOMException:
return serializeDOMException(node);
default:
throw new Error('invariant');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/seroval/src/core/cross/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import {
createRegExpNode,
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../web-api';
import {
createCustomEventOptions,
createEventOptions,
Expand Down Expand Up @@ -805,6 +805,8 @@ function parseObject(
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
case DOMException:
return createDOMExceptionNode(id, current as unknown as DOMException);
default:
break;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/seroval/src/core/cross/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
createRegExpNode,
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../web-api';
import { createCustomEventOptions, createEventOptions } from '../constructors';

type ObjectLikeNode = SerovalObjectNode | SerovalNullConstructorNode;
Expand Down Expand Up @@ -568,6 +568,8 @@ function parseObject(
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
case DOMException:
return createDOMExceptionNode(id, current as unknown as DOMException);
default:
break;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/seroval/src/core/tree/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import {
createRegExpNode,
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../web-api';
import promiseToResult from '../promise-to-result';
import {
createCustomEventOptions,
Expand Down Expand Up @@ -683,6 +683,8 @@ async function parseObject(
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
case DOMException:
return createDOMExceptionNode(id, current as unknown as DOMException);
default:
break;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/seroval/src/core/tree/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
SerovalBlobNode,
SerovalBoxedNode,
SerovalCustomEventNode,
SerovalDOMExceptionNode,
SerovalDataViewNode,
SerovalDateNode,
SerovalErrorNode,
Expand Down Expand Up @@ -440,6 +441,20 @@ function deserializeCustomEvent(
);
}

function deserializeDOMException(
ctx: DeserializerContext,
node: SerovalDOMExceptionNode,
): DOMException {
return assignIndexedValue(
ctx,
node.i,
new DOMException(
deserializeString(node.s),
deserializeString(node.c),
),
);
}

export default function deserializeTree(
ctx: DeserializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -507,6 +522,8 @@ export default function deserializeTree(
return deserializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return deserializeCustomEvent(ctx, node);
case SerovalNodeType.DOMException:
return deserializeDOMException(ctx, node);
default:
throw new Error('invariant');
}
Expand Down
14 changes: 14 additions & 0 deletions packages/seroval/src/core/tree/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type {
SerovalResponseNode,
SerovalEventNode,
SerovalCustomEventNode,
SerovalDOMExceptionNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
Expand Down Expand Up @@ -769,6 +770,17 @@ function serializeCustomEvent(
);
}

function serializeDOMException(
ctx: SerializerContext,
node: SerovalDOMExceptionNode,
): string {
return assignIndexedValue(
ctx,
node.i,
'new DOMException("' + node.s + '","' + node.c + '")',
);
}

export default function serializeTree(
ctx: SerializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -837,6 +849,8 @@ export default function serializeTree(
return serializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return serializeCustomEvent(ctx, node);
case SerovalNodeType.DOMException:
return serializeDOMException(ctx, node);
default:
throw new Error('invariant');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/seroval/src/core/tree/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
createRegExpNode,
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../web-api';
import { createCustomEventOptions, createEventOptions } from '../constructors';

type ObjectLikeNode = SerovalObjectNode | SerovalNullConstructorNode;
Expand Down Expand Up @@ -568,6 +568,8 @@ function parseObject(
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
case DOMException:
return createDOMExceptionNode(id, current as unknown as DOMException);
default:
break;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/seroval/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ export interface SerovalCustomEventNode extends SerovalBaseNode {
f: SerovalNode;
}

export interface SerovalDOMExceptionNode extends SerovalBaseNode {
t: SerovalNodeType.DOMException;
i: number;
// Message
s: string;
// name
c: string;
}

export type SerovalSyncNode =
| SerovalPrimitiveNode
| SerovalIndexedValueNode
Expand All @@ -397,7 +406,8 @@ export type SerovalSyncNode =
| SerovalFormDataNode
| SerovalBoxedNode
| SerovalEventNode
| SerovalCustomEventNode;
| SerovalCustomEventNode
| SerovalDOMExceptionNode;

export type SerovalAsyncNode =
| SerovalPromiseNode
Expand Down
22 changes: 21 additions & 1 deletion packages/seroval/src/core/web-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SerovalNodeType } from './constants';
import { serializeString } from './string';
import type { SerovalURLNode, SerovalURLSearchParamsNode } from './types';
import type { SerovalDOMExceptionNode, SerovalURLNode, SerovalURLSearchParamsNode } from './types';

export function createURLNode(
id: number,
Expand Down Expand Up @@ -41,3 +41,23 @@ export function createURLSearchParamsNode(
o: undefined,
};
}

export function createDOMExceptionNode(
id: number,
current: DOMException,
): SerovalDOMExceptionNode {
return {
t: SerovalNodeType.DOMException,
i: id,
s: serializeString(current.message),
l: undefined,
c: serializeString(current.name),
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: undefined,
b: undefined,
o: undefined,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`DOMException > crossSerialize > scoped > supports DOMException 1`] = `"($R=>($R[0]=new DOMException(\\"This is an example message.\\",\\"Example\\")))($R[\\"example\\"])"`;

exports[`DOMException > crossSerialize > supports DOMException 1`] = `"$R[0]=new DOMException(\\"This is an example message.\\",\\"Example\\")"`;

exports[`DOMException > crossSerializeAsync > scoped > supports DOMException 1`] = `"($R=>($R[0]=Promise.resolve($R[1]=new DOMException(\\"This is an example message.\\",\\"Example\\"))))($R[\\"example\\"])"`;

exports[`DOMException > crossSerializeAsync > supports DOMException 1`] = `"$R[0]=Promise.resolve($R[1]=new DOMException(\\"This is an example message.\\",\\"Example\\"))"`;

exports[`DOMException > crossSerializeStream > scoped > supports DOMException 1`] = `"($R=>($R[0]=_$.P()))($R[\\"example\\"])"`;

exports[`DOMException > crossSerializeStream > scoped > supports DOMException 2`] = `"($R=>(_$.Ps($R[0],$R[1]=new DOMException(\\"This is an example message.\\",\\"Example\\"))))($R[\\"example\\"])"`;

exports[`DOMException > crossSerializeStream > supports DOMException 1`] = `"$R[0]=_$.P()"`;

exports[`DOMException > crossSerializeStream > supports DOMException 2`] = `"_$.Ps($R[0],$R[1]=new DOMException(\\"This is an example message.\\",\\"Example\\"))"`;

exports[`DOMException > serialize > supports DOMException 1`] = `"new DOMException(\\"This is an example message.\\",\\"Example\\")"`;

exports[`DOMException > serializeAsync > supports DOMException 1`] = `"Promise.resolve(new DOMException(\\"This is an example message.\\",\\"Example\\"))"`;

exports[`DOMException > toJSON > supports DOMException 1`] = `"{\\"t\\":{\\"t\\":39,\\"i\\":0,\\"s\\":\\"This is an example message.\\",\\"c\\":\\"Example\\"},\\"f\\":16383,\\"m\\":[]}"`;

exports[`DOMException > toJSONAsync > supports DOMException 1`] = `"{\\"t\\":{\\"t\\":12,\\"i\\":0,\\"s\\":1,\\"f\\":{\\"t\\":39,\\"i\\":1,\\"s\\":\\"This is an example message.\\",\\"c\\":\\"Example\\"}},\\"f\\":16383,\\"m\\":[]}"`;
Loading

0 comments on commit a7ceb97

Please sign in to comment.