Skip to content

Commit

Permalink
refactor(serializer): split serializers (#12)
Browse files Browse the repository at this point in the history
* feat(serializer): split serializers

* test: fix serializer test file path
  • Loading branch information
PaperStrike authored Oct 22, 2023
1 parent 6d8ba6f commit bfb7637
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 235 deletions.
15 changes: 5 additions & 10 deletions src/WS/WSServer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type http from 'node:http';
import WebSocket, { WebSocketServer } from 'ws';
import type playwright from 'playwright';
import {
parseEvaluateExpression,
parseSerializedValue,
serializeValue,
SerializedValue,
} from './handle/serializer.js';
import * as Serializer from '../serializer/index.js';
import {
RouteClientMeta,
HandleClientMeta,
Expand Down Expand Up @@ -235,9 +230,9 @@ export default class WSServer {
try {
switch (meta.action) {
case 'evaluate': {
const fn = parseEvaluateExpression(meta.fn);
const arg = parseSerializedValue(
JSON.parse(meta.arg) as SerializedValue,
const fn = Serializer.parseEvaluateExpression(meta.fn);
const arg = Serializer.parseSerializedValue(
JSON.parse(meta.arg) as Serializer.SerializedValue,
this.handleTargets,
);
const returned: unknown = await (typeof fn === 'function' ? fn(target, arg) : fn);
Expand Down Expand Up @@ -281,7 +276,7 @@ export default class WSServer {
action: 'resolve',
id,
resolveID,
result: JSON.stringify(serializeValue(result, null)),
result: JSON.stringify(Serializer.serializeValue(result, null)),
error,
}));
}
Expand Down
15 changes: 6 additions & 9 deletions src/WS/handle/NodeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {
HandleMetaBase,
HandleClientMeta,
} from '../message.js';
import Handle from './Handle.js';
import {
serializeValue,
parseSerializedValue,
SerializedValue,
} from './serializer.js';
import * as Serializer from '../../serializer/index.js';

export type Unboxed<Arg> =
Arg extends URL
Expand Down Expand Up @@ -52,7 +47,7 @@ const finalizationRegistry = new FinalizationRegistry(({ id, ws }: {
}));
});

export default class NodeHandle<T = unknown> extends Handle {
export default class NodeHandle<T = unknown> extends Serializer.Handle {
/**
* Share the handle ID with an object so that the matching handles will keep referencing the node
* target until all ID users are disposed or garbage-collected.
Expand Down Expand Up @@ -100,7 +95,9 @@ export default class NodeHandle<T = unknown> extends Handle {
|| meta.id !== id
|| meta.resolveID !== resolveID) return;
controller.abort();
const result = parseSerializedValue(JSON.parse(meta.result) as SerializedValue);
const result = Serializer.parseSerializedValue(
JSON.parse(meta.result) as Serializer.SerializedValue,
);
if (meta.error) {
reject(result);
} else {
Expand Down Expand Up @@ -128,7 +125,7 @@ export default class NodeHandle<T = unknown> extends Handle {
return this.act({
action: 'evaluate',
fn: String(nodeFunction),
arg: JSON.stringify(serializeValue(arg)),
arg: JSON.stringify(Serializer.serializeValue(arg)),
h: createHandle,
}, (result) => {
if (createHandle) return new NodeHandle(result as number, this.ws);
Expand Down
211 changes: 0 additions & 211 deletions src/WS/handle/serializer.ts

This file was deleted.

File renamed without changes.
30 changes: 30 additions & 0 deletions src/serializer/Serializable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Handle from './Handle.js';

export type SerializableValue =
| number | boolean | string | null | undefined | bigint | URL | Date | Error | RegExp | Handle
| SerializableValue[] | { [K: string]: SerializableValue };

export interface SerializedValue {
i: number; // ID
n?: number | boolean | string | null;
v?: 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0';
b?: string; // BigInt
u?: string; // URL
d?: string; // Date
r?: {
p: string;
f: string;
}, // RegExp
h?: number; // Handle ID
e?: {
n: string;
m: string;
c?: SerializedValue | undefined;
s?: string | undefined;
}; // Error
a?: SerializedValue[]; // Array
o?: {
k: string;
v: SerializedValue;
}[]; // Object
}
13 changes: 13 additions & 0 deletions src/serializer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Handle from './Handle.js';
import serializeValue, { noFallback } from './serializeValue.js';
import parseSerializedValue from './parseSerializedValue.js';
import parseEvaluateExpression from './parseEvaluateExpression.js';

export * from './Serializable.js';
export {
parseEvaluateExpression,
parseSerializedValue,
serializeValue,
noFallback,
Handle,
};
14 changes: 14 additions & 0 deletions src/serializer/parseEvaluateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function parseEvaluateExpression(expression: string): unknown {
const exp = expression.trim();
try {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
return new Function(`return (${exp})`)();
} catch {
try {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
return new Function(`return (${exp.replace(/^(async )?/, '$1function ')})`)();
} catch {
throw new Error('Passed function is not well-serializable!');
}
}
}
Loading

0 comments on commit bfb7637

Please sign in to comment.