psionic 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +321 -0
- package/__tests__/async-functions.ts +43 -0
- package/__tests__/async-generator-functions.ts +137 -0
- package/__tests__/convert-from-wire-format.ts +65 -0
- package/__tests__/convert-to-wire-format.ts +66 -0
- package/__tests__/describe.ts +26 -0
- package/__tests__/lib/adapter.ts +47 -0
- package/docs/extending.md +19 -0
- package/docs/protocol.md +154 -0
- package/examples/cli-chat/client.mjs +21 -0
- package/examples/cli-chat/package.json +17 -0
- package/examples/cli-chat/server.mjs +48 -0
- package/examples/cloudflare/README.md +37 -0
- package/examples/cloudflare/eslint.config.js +19 -0
- package/examples/cloudflare/index.html +13 -0
- package/examples/cloudflare/package.json +29 -0
- package/examples/cloudflare/public/vite.svg +1 -0
- package/examples/cloudflare/src/App.svelte +450 -0
- package/examples/cloudflare/src/api.ts +33 -0
- package/examples/cloudflare/src/main.ts +4 -0
- package/examples/cloudflare/tsconfig.app.json +27 -0
- package/examples/cloudflare/tsconfig.json +14 -0
- package/examples/cloudflare/tsconfig.node.json +26 -0
- package/examples/cloudflare/tsconfig.worker.json +8 -0
- package/examples/cloudflare/vite.config.ts +8 -0
- package/examples/cloudflare/worker/cloudflare.ts +115 -0
- package/examples/cloudflare/worker/index.ts +17 -0
- package/examples/cloudflare/worker/peer.ts +47 -0
- package/examples/cloudflare/worker-configuration.d.ts +10922 -0
- package/examples/cloudflare/wrangler.jsonc +58 -0
- package/examples/nodestreams/adapter.mjs +36 -0
- package/examples/nodestreams/child.mjs +20 -0
- package/examples/nodestreams/package.json +14 -0
- package/examples/nodestreams/parent.mjs +20 -0
- package/examples/reconnect/client.mjs +22 -0
- package/examples/reconnect/package.json +15 -0
- package/examples/reconnect/server.mjs +30 -0
- package/examples/tokens/client.mjs +26 -0
- package/examples/tokens/package.json +20 -0
- package/examples/tokens/server.mjs +46 -0
- package/make-package.js +23 -0
- package/package.json +24 -42
- package/src/adapters/reconnect.ts +54 -0
- package/src/adapters/resend.ts +34 -0
- package/src/adapters/socket.ts +183 -0
- package/src/adapters/websocket.ts +157 -0
- package/src/adapters/webworker.ts +48 -0
- package/src/codecs/cbor.ts +239 -0
- package/src/codecs/psionic.ts +75 -0
- package/src/psionic.ts +876 -0
- package/src/session.ts +164 -0
- package/src/utils/react-hooks.ts +48 -0
- package/tests +8 -0
- package/tsdown.config.ts +19 -0
- package/adapters/reconnect.d.mts +0 -10
- package/adapters/reconnect.mjs +0 -42
- package/adapters/reconnect.mjs.map +0 -1
- package/adapters/resend.d.mts +0 -13
- package/adapters/resend.mjs +0 -29
- package/adapters/resend.mjs.map +0 -1
- package/adapters/socket.d.mts +0 -14
- package/adapters/socket.mjs +0 -160
- package/adapters/socket.mjs.map +0 -1
- package/adapters/websocket.d.mts +0 -20
- package/adapters/websocket.mjs +0 -124
- package/adapters/websocket.mjs.map +0 -1
- package/adapters/webworker.d.mts +0 -8
- package/adapters/webworker.mjs +0 -42
- package/adapters/webworker.mjs.map +0 -1
- package/codecs/cbor.d.mts +0 -12
- package/codecs/cbor.mjs +0 -198
- package/codecs/cbor.mjs.map +0 -1
- package/codecs/psionic.d.mts +0 -12
- package/codecs/psionic.mjs +0 -57
- package/codecs/psionic.mjs.map +0 -1
- package/psionic.d.mts +0 -203
- package/psionic.mjs +0 -671
- package/psionic.mjs.map +0 -1
- package/session.d.mts +0 -63
- package/session.mjs +0 -92
- package/session.mjs.map +0 -1
- package/utils/react-hooks.d.mts +0 -24
- package/utils/react-hooks.mjs +0 -37
- package/utils/react-hooks.mjs.map +0 -1
package/adapters/websocket.mjs
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { psionicCodec } from "../codecs/psionic.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/adapters/websocket.ts
|
|
4
|
-
const CONNECTING = 0;
|
|
5
|
-
const OPEN = 1;
|
|
6
|
-
const CLOSING = 2;
|
|
7
|
-
const CLOSED = 3;
|
|
8
|
-
const pingText = "ping";
|
|
9
|
-
const pongText = "pong";
|
|
10
|
-
/** Adapter for browser and standards-compatible WebSockets. */
|
|
11
|
-
function createWebSocketAdapter(socket, opts) {
|
|
12
|
-
const pingInterval = opts?.pingInterval ?? 1e4;
|
|
13
|
-
return async function connectPeer(peer) {
|
|
14
|
-
let latestMessage = Date.now();
|
|
15
|
-
const codec = opts?.codec ?? psionicCodec();
|
|
16
|
-
const maxFrameSize = opts?.maxFrameSize ?? 1 * 1048576;
|
|
17
|
-
let dead = false;
|
|
18
|
-
if (socket.readyState === CLOSING || socket.readyState === CLOSED) throw new Error("WebSocket is already closed or closing");
|
|
19
|
-
socket.addEventListener("close", onClose);
|
|
20
|
-
let pingTimer = void 0;
|
|
21
|
-
function startPing() {
|
|
22
|
-
if (pingTimer) {
|
|
23
|
-
clearInterval(pingTimer);
|
|
24
|
-
pingTimer = void 0;
|
|
25
|
-
}
|
|
26
|
-
if (!dead) pingTimer = setInterval(onPingTimer, pingInterval);
|
|
27
|
-
}
|
|
28
|
-
function onPingTimer() {
|
|
29
|
-
if (dead) {
|
|
30
|
-
clearInterval(pingTimer);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const elapsed = Date.now() - latestMessage;
|
|
34
|
-
if (elapsed < pingInterval) return;
|
|
35
|
-
if (elapsed > pingInterval * 2) {
|
|
36
|
-
clearInterval(pingTimer);
|
|
37
|
-
onError(/* @__PURE__ */ new Error("No response to ping, assuming connection is dead"));
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
socket.send(pingText);
|
|
42
|
-
} catch (ex) {
|
|
43
|
-
onError(ex instanceof Error ? ex : new Error(String(ex)));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function onMessage(message) {
|
|
47
|
-
latestMessage = Date.now();
|
|
48
|
-
if (message.data === pongText) return;
|
|
49
|
-
if (message.data === pingText) {
|
|
50
|
-
socket.send(pongText);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
const msg = getMessageBytes(message);
|
|
54
|
-
if (msg.byteLength > maxFrameSize) {
|
|
55
|
-
onError(/* @__PURE__ */ new Error("Frame size exceeds maximum allowed"));
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
const messageData = codec.messageFromWire(msg);
|
|
60
|
-
peer.handleMessage(messageData);
|
|
61
|
-
} catch (ex) {
|
|
62
|
-
onError(ex instanceof Error ? ex : new Error(String(ex)));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function onSend(message) {
|
|
66
|
-
if (dead) return;
|
|
67
|
-
try {
|
|
68
|
-
const messageData = codec.messageToWire(message);
|
|
69
|
-
socket.send(messageData);
|
|
70
|
-
} catch (ex) {
|
|
71
|
-
onError(ex instanceof Error ? ex : new Error(String(ex)));
|
|
72
|
-
throw ex;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function onError(error) {
|
|
76
|
-
if (dead) return;
|
|
77
|
-
onClose(error);
|
|
78
|
-
}
|
|
79
|
-
function onClose(error) {
|
|
80
|
-
if (dead) return;
|
|
81
|
-
dead = true;
|
|
82
|
-
clearInterval(pingTimer);
|
|
83
|
-
socket.removeEventListener("message", onMessage);
|
|
84
|
-
socket.removeEventListener("close", onClose);
|
|
85
|
-
if (socket.readyState === OPEN || socket.readyState === CONNECTING) socket.close(error instanceof Error ? 3e3 : 1e3, error instanceof Error ? error.message : "Normal Closure");
|
|
86
|
-
peer.close(error);
|
|
87
|
-
}
|
|
88
|
-
async function waitUntilOpen() {
|
|
89
|
-
if (socket.readyState === OPEN) return;
|
|
90
|
-
await new Promise((resolve, reject) => {
|
|
91
|
-
if (dead) return closer();
|
|
92
|
-
socket.addEventListener("open", opener);
|
|
93
|
-
socket.addEventListener("close", closer);
|
|
94
|
-
function opener() {
|
|
95
|
-
socket.removeEventListener("open", opener);
|
|
96
|
-
socket.removeEventListener("close", closer);
|
|
97
|
-
resolve(void 0);
|
|
98
|
-
}
|
|
99
|
-
function closer() {
|
|
100
|
-
socket.removeEventListener("open", opener);
|
|
101
|
-
socket.removeEventListener("close", closer);
|
|
102
|
-
reject(/* @__PURE__ */ new Error("WebSocket closed before connection could be established"));
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
await waitUntilOpen();
|
|
107
|
-
startPing();
|
|
108
|
-
socket.addEventListener("message", onMessage);
|
|
109
|
-
return {
|
|
110
|
-
send: onSend,
|
|
111
|
-
close: onClose
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function getMessageBytes(message) {
|
|
116
|
-
if (typeof message.data === "string") return new TextEncoder().encode(message.data);
|
|
117
|
-
if (message.data instanceof ArrayBuffer) return new Uint8Array(message.data);
|
|
118
|
-
if (message.data instanceof Uint8Array) return message.data;
|
|
119
|
-
throw new Error("Unsupported message data type");
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
//#endregion
|
|
123
|
-
export { createWebSocketAdapter };
|
|
124
|
-
//# sourceMappingURL=websocket.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.mjs","names":[],"sources":["../../src/adapters/websocket.ts"],"sourcesContent":["import type { WireCodec, Frame, Connection, AdapterNext } from \"../psionic\";\nimport { psionicCodec } from \"../codecs/psionic\";\n\nexport type WebSocketAdapterOptions = {\n /** Optional codec to use for encoding/decoding messages. */\n codec?: WireCodec;\n /** Max frame size in bytes. Default: 1MB. */\n maxFrameSize?: number;\n /** Interval in milliseconds for sending ping messages. Default: 10000 (10 seconds). */\n pingInterval?: number;\n};\n\nexport type AdaptableWebSocket = {\n readyState: number;\n addEventListener: (type: any, listener: (event: any) => void) => void;\n removeEventListener: (type: any, listener: (event: any) => void) => void;\n send: (message: string | ArrayBufferView | Blob | ArrayBufferLike) => void;\n close: (code?: number, reason?: string) => void;\n};\n\nconst CONNECTING = 0;\nconst OPEN = 1;\nconst CLOSING = 2;\nconst CLOSED = 3;\n\nconst pingText = 'ping';\nconst pongText = 'pong';\n\n/** Adapter for browser and standards-compatible WebSockets. */\nexport function createWebSocketAdapter(socket: AdaptableWebSocket, opts?: WebSocketAdapterOptions): (peer: AdapterNext) => Promise<Connection> {\n const pingInterval = opts?.pingInterval ?? 10000;\n return async function connectPeer(peer: AdapterNext) {\n let latestMessage = Date.now();\n const codec = opts?.codec ?? psionicCodec();\n const maxFrameSize = opts?.maxFrameSize ?? (1 * 1_048_576);\n\n let dead = false;\n\n if (socket.readyState === CLOSING || socket.readyState === CLOSED) {\n throw new Error('WebSocket is already closed or closing');\n }\n\n socket.addEventListener('close', onClose);\n\n let pingTimer: ReturnType<typeof setInterval> | undefined = undefined;\n function startPing() {\n if (pingTimer) {\n clearInterval(pingTimer);\n pingTimer = undefined;\n }\n if (!dead) {\n pingTimer = setInterval(onPingTimer, pingInterval);\n }\n }\n\n function onPingTimer() {\n if (dead) {\n clearInterval(pingTimer!);\n return;\n }\n const elapsed = Date.now() - latestMessage;\n if (elapsed < pingInterval) return; // no need, we heard from them recently\n if (elapsed > pingInterval * 2) {\n clearInterval(pingTimer!);\n onError(new Error('No response to ping, assuming connection is dead'));\n return;\n }\n try {\n socket.send(pingText);\n } catch (ex) {\n onError(ex instanceof Error ? ex : new Error(String(ex)));\n }\n }\n\n function onMessage(message: MessageEvent) {\n latestMessage = Date.now();\n if (message.data === pongText) return; // Ignore pong responses\n if (message.data === pingText) { socket.send(pongText); return; } // Respond to pings\n const msg = getMessageBytes(message);\n if (msg.byteLength > maxFrameSize) {\n onError(new Error('Frame size exceeds maximum allowed'));\n return;\n }\n try {\n const messageData = codec.messageFromWire(msg);\n peer.handleMessage(messageData);\n } catch (ex) {\n onError(ex instanceof Error ? ex : new Error(String(ex)));\n }\n }\n\n function onSend(message: Frame) {\n if (dead) return;\n try {\n const messageData = codec.messageToWire(message);\n socket.send(messageData);\n } catch (ex) {\n onError(ex instanceof Error ? ex : new Error(String(ex)));\n throw ex;\n }\n }\n\n function onError(error: Error) {\n if (dead) return;\n onClose(error);\n }\n\n function onClose(error?: any) {\n if (dead) return;\n dead = true;\n clearInterval(pingTimer!);\n socket.removeEventListener('message', onMessage);\n socket.removeEventListener('close', onClose);\n if (socket.readyState === OPEN || socket.readyState === CONNECTING) {\n socket.close(error instanceof Error ? 3000 : 1000, error instanceof Error ? error.message : 'Normal Closure');\n }\n peer.close(error);\n }\n\n async function waitUntilOpen() {\n if (socket.readyState === OPEN) return;\n await new Promise((resolve, reject) => {\n if (dead) return closer();\n socket.addEventListener('open', opener);\n socket.addEventListener('close', closer);\n function opener() {\n socket.removeEventListener('open', opener);\n socket.removeEventListener('close', closer);\n resolve(undefined);\n }\n function closer() {\n socket.removeEventListener('open', opener);\n socket.removeEventListener('close', closer);\n reject(new Error('WebSocket closed before connection could be established'));\n }\n });\n }\n\n await waitUntilOpen();\n startPing();\n socket.addEventListener('message', onMessage);\n return {\n send: onSend,\n close: onClose\n };\n }\n}\n\nfunction getMessageBytes(message: MessageEvent): Uint8Array {\n if (typeof message.data === 'string') return new TextEncoder().encode(message.data);\n if (message.data instanceof ArrayBuffer) return new Uint8Array(message.data);\n if (message.data instanceof Uint8Array) return message.data;\n // no support for blobs, too much async overhead for something that can be easily handled at the application level if needed\n throw new Error('Unsupported message data type');\n}\n"],"mappings":";;;AAoBA,MAAM,aAAa;AACnB,MAAM,OAAO;AACb,MAAM,UAAU;AAChB,MAAM,SAAS;AAEf,MAAM,WAAW;AACjB,MAAM,WAAW;;AAGjB,SAAgB,uBAAuB,QAA4B,MAA4E;CAC3I,MAAM,eAAe,MAAM,gBAAgB;AAC3C,QAAO,eAAe,YAAY,MAAmB;EACjD,IAAI,gBAAgB,KAAK,KAAK;EAC9B,MAAM,QAAQ,MAAM,SAAS,cAAc;EAC3C,MAAM,eAAe,MAAM,gBAAiB,IAAI;EAEhD,IAAI,OAAO;AAEX,MAAI,OAAO,eAAe,WAAW,OAAO,eAAe,OACvD,OAAM,IAAI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ptions = {};
|
|
5
|
-
declare function createWorkerAdapter(port: MessagePort, options?: WorkerAdapterOptions): Adapter;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { WorkerAdapterOptions, createWorkerAdapter };
|
|
8
|
-
//# sourceMappingURL=webworker.d.mts.map
|
package/adapters/webworker.mjs
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { psionicCodec } from "../codecs/psionic.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/adapters/webworker.ts
|
|
4
|
-
const codec = psionicCodec({ maxEntities: 0xffffffffffff });
|
|
5
|
-
function createWorkerAdapter(port, options) {
|
|
6
|
-
return async function connectPeer(peer) {
|
|
7
|
-
let dead = false;
|
|
8
|
-
function onSend(message) {
|
|
9
|
-
if (dead) return;
|
|
10
|
-
port.postMessage(codec.messageToWire(message));
|
|
11
|
-
}
|
|
12
|
-
function onMessage(event) {
|
|
13
|
-
if (event.data === "STARTUP") return;
|
|
14
|
-
peer.handleMessage(codec.messageFromWire(event.data));
|
|
15
|
-
}
|
|
16
|
-
function onClose(_err) {
|
|
17
|
-
if (dead) return;
|
|
18
|
-
dead = true;
|
|
19
|
-
port.removeEventListener("message", onMessage);
|
|
20
|
-
port.close();
|
|
21
|
-
}
|
|
22
|
-
await new Promise((resolve) => {
|
|
23
|
-
port.addEventListener("message", onReadyMessage);
|
|
24
|
-
port.postMessage("STARTUP");
|
|
25
|
-
port.start();
|
|
26
|
-
function onReadyMessage(d) {
|
|
27
|
-
if (d.data !== "STARTUP") return;
|
|
28
|
-
port.addEventListener("message", onMessage);
|
|
29
|
-
port.removeEventListener("message", onReadyMessage);
|
|
30
|
-
resolve(null);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
return {
|
|
34
|
-
send: onSend,
|
|
35
|
-
close: onClose
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
//#endregion
|
|
41
|
-
export { createWorkerAdapter };
|
|
42
|
-
//# sourceMappingURL=webworker.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webworker.mjs","names":[],"sources":["../../src/adapters/webworker.ts"],"sourcesContent":["import type { Frame, Adapter, AdapterNext } from \"../psionic\";\nimport { psionicCodec } from \"../codecs/psionic\";\n\nconst codec = psionicCodec({ maxEntities: 0xFFFFFFFFFFFF });\n\nexport type WorkerAdapterOptions = {\n};\n\nexport function createWorkerAdapter(port: MessagePort, options?: WorkerAdapterOptions): Adapter{\n return async function connectPeer(peer: AdapterNext) {\n let dead = false;\n\n function onSend(message: Frame) {\n if (dead) return;\n port.postMessage(codec.messageToWire(message));\n }\n\n function onMessage(event: MessageEvent) {\n if (event.data === 'STARTUP') return;\n peer.handleMessage(codec.messageFromWire(event.data));\n }\n\n function onClose(_err?: any) {\n if (dead) return;\n dead = true;\n port.removeEventListener('message', onMessage);\n port.close();\n }\n\n // Handshake: wait for worker to echo STARTUP before exchanging frames.\n await new Promise((resolve) => {\n port.addEventListener('message', onReadyMessage);\n port.postMessage('STARTUP');\n port.start();\n function onReadyMessage(d: MessageEvent) {\n if (d.data !== 'STARTUP') return;\n port.addEventListener('message', onMessage);\n port.removeEventListener('message', onReadyMessage);\n resolve(null);\n }\n });\n\n return {\n send: onSend,\n close: onClose\n };\n }\n};\n"],"mappings":";;;AAGA,MAAM,QAAQ,aAAa,EAAE,aAAa,gBAAgB,CAAC;AAK3D,SAAgB,oBAAoB,MAAmB,SAAwC;AAC3F,QAAO,eAAe,YAAY,MAAmB;EACjD,IAAI,OAAO;EAEX,SAAS,OAAO,SAAgB;AAC5B,OAAI,KAAM;AACV,QAAK,YAAY,MAAM,cAAc,QAAQ,CAAC;;EAGlD,SAAS,UAAU,OAAqB;AACpC,OAAI,MAAM,SAAS,UAAW;AAC9B,QAAK,cAAc,MAAM,gBAAgB,MAAM,KAAK,CAAC;;EAGzD,SAAS,QAAQ,MAAY;AACzB,OAAI,KAAM;AACV,UAAO;AACP,QAAK,oBAAoB,WAAW,UAAU;AAC9C,QAAK,OAAO;;AAIhB,QAAM,IAAI,SAAS,YAAY;AAC3B,QAAK,iBAAiB,WAAW,eAAe;AAChD,QAAK,YAAY,UAAU;AAC3B,QAAK,OAAO;GACZ,SAAS,eAAe,GAAiB;AACrC,QAAI,EAAE,SAAS,UAAW;AAC1B,SAAK,iBAAiB,WAAW,UAAU;AAC3C,SAAK,oBAAoB,WAAW,eAAe;AACnD,YAAQ,KAAK;;IAEnB;AAEF,SAAO;GACH,MAAM;GACN,OAAO;GACV"}
|
package/codecs/cbor.d.mts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
//#region src/codecs/cbor.d.ts
|
|
2
|
-
type CborCodecOptions = {
|
|
3
|
-
/** Max entities per message. Default: 4096. */maxEntities?: number;
|
|
4
|
-
};
|
|
5
|
-
declare const cborCodec: (options?: CborCodecOptions) => {
|
|
6
|
-
type: "binary";
|
|
7
|
-
messageFromWire: (data: string | Uint8Array) => any;
|
|
8
|
-
messageToWire: (message: any[]) => Uint8Array;
|
|
9
|
-
};
|
|
10
|
-
//#endregion
|
|
11
|
-
export { CborCodecOptions, cborCodec };
|
|
12
|
-
//# sourceMappingURL=cbor.d.mts.map
|
package/codecs/cbor.mjs
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import { AsyncFnKey, AsyncGenKey } from "../psionic.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/codecs/cbor.ts
|
|
4
|
-
const TAG_ASYNC_FN = 32768;
|
|
5
|
-
const TAG_ASYNC_GEN = 32769;
|
|
6
|
-
const cborCodec = function(options) {
|
|
7
|
-
const maxEntities = options?.maxEntities ?? 4096;
|
|
8
|
-
function messageToWire(message) {
|
|
9
|
-
const scratch = new Uint8Array(16);
|
|
10
|
-
const scratchView = new DataView(scratch.buffer);
|
|
11
|
-
let output = new Uint8Array(512);
|
|
12
|
-
let at = 0;
|
|
13
|
-
function ensureSpace(bytes) {
|
|
14
|
-
if (at + bytes <= output.length) return;
|
|
15
|
-
let newsize = output.length * 2;
|
|
16
|
-
while (newsize < at + bytes) newsize *= 2;
|
|
17
|
-
if (newsize >= 50331648) throw new Error("Message too large to encode");
|
|
18
|
-
const newBuffer = new Uint8Array(newsize);
|
|
19
|
-
newBuffer.set(output);
|
|
20
|
-
output = newBuffer;
|
|
21
|
-
}
|
|
22
|
-
function append(bytes) {
|
|
23
|
-
ensureSpace(bytes.length);
|
|
24
|
-
output.set(bytes, at);
|
|
25
|
-
at += bytes.length;
|
|
26
|
-
}
|
|
27
|
-
const encoder = new TextEncoder();
|
|
28
|
-
function writeItem(item) {
|
|
29
|
-
if (typeof item === "number" && Number.isInteger(item) && item >= 0) writeUInt(0, item);
|
|
30
|
-
else if (typeof item === "number" && Number.isInteger(item) && item < 0) writeUInt(1, -1 - item);
|
|
31
|
-
else if (typeof item === "string") {
|
|
32
|
-
const bytes = encoder.encode(item);
|
|
33
|
-
writeUInt(3, bytes.length);
|
|
34
|
-
ensureSpace(bytes.length);
|
|
35
|
-
output.set(bytes, at);
|
|
36
|
-
at += bytes.length;
|
|
37
|
-
} else if (Array.isArray(item)) {
|
|
38
|
-
writeUInt(4, item.length);
|
|
39
|
-
for (const subitem of item) writeItem(subitem);
|
|
40
|
-
} else if (item === AsyncFnKey) {
|
|
41
|
-
writeUInt(6, TAG_ASYNC_FN);
|
|
42
|
-
append([246]);
|
|
43
|
-
} else if (item === AsyncGenKey) {
|
|
44
|
-
writeUInt(6, TAG_ASYNC_GEN);
|
|
45
|
-
append([246]);
|
|
46
|
-
} else if (item && typeof item === "object") {
|
|
47
|
-
const entries = Object.entries(item);
|
|
48
|
-
writeUInt(5, entries.length);
|
|
49
|
-
for (const [key, value] of entries) {
|
|
50
|
-
const keyBytes = encoder.encode(key);
|
|
51
|
-
writeUInt(3, keyBytes.length);
|
|
52
|
-
ensureSpace(keyBytes.length);
|
|
53
|
-
output.set(keyBytes, at);
|
|
54
|
-
at += keyBytes.length;
|
|
55
|
-
writeItem(value);
|
|
56
|
-
}
|
|
57
|
-
} else if (item === false) append([244]);
|
|
58
|
-
else if (item === true) append([245]);
|
|
59
|
-
else if (item === null) append([246]);
|
|
60
|
-
else if (item === void 0) append([247]);
|
|
61
|
-
else if (typeof item === "number") {
|
|
62
|
-
const f32 = Math.fround(item);
|
|
63
|
-
let bytes = 8;
|
|
64
|
-
if (Number.isFinite(f32) && f32 === item) bytes = 4;
|
|
65
|
-
ensureSpace(bytes + 1);
|
|
66
|
-
if (bytes === 4) scratchView.setFloat32(0, item, false);
|
|
67
|
-
else scratchView.setFloat64(0, item, false);
|
|
68
|
-
output[at++] = 224 | (bytes === 4 ? 26 : 27);
|
|
69
|
-
for (let i = 0; i < bytes; i++) output[at++] = scratch[i];
|
|
70
|
-
} else throw new Error("Unsupported type");
|
|
71
|
-
}
|
|
72
|
-
writeItem(message);
|
|
73
|
-
return output.subarray(0, at);
|
|
74
|
-
function writeUInt(majorTypeValue, num) {
|
|
75
|
-
const majorType = majorTypeValue << 5;
|
|
76
|
-
if (num < 24) {
|
|
77
|
-
append([majorType | num]);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
let length = 0;
|
|
81
|
-
if (num > 4294967295) length = 3;
|
|
82
|
-
else if (num > 65535) length = 2;
|
|
83
|
-
else if (num > 255) length = 1;
|
|
84
|
-
const bytesNeeded = [
|
|
85
|
-
1,
|
|
86
|
-
2,
|
|
87
|
-
4,
|
|
88
|
-
8
|
|
89
|
-
][length];
|
|
90
|
-
if (bytesNeeded === 1) scratchView.setUint8(0, num);
|
|
91
|
-
else if (bytesNeeded === 2) scratchView.setUint16(0, num, false);
|
|
92
|
-
else if (bytesNeeded === 4) scratchView.setUint32(0, num, false);
|
|
93
|
-
else if (bytesNeeded === 8) scratchView.setBigUint64(0, BigInt(num), false);
|
|
94
|
-
else throw new Error("Number too large for encoding");
|
|
95
|
-
ensureSpace(bytesNeeded + 1);
|
|
96
|
-
output[at++] = majorType | length + 24;
|
|
97
|
-
for (let i = 0; i < bytesNeeded; i++) output[at++] = scratch[i];
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
function messageFromWire(data) {
|
|
101
|
-
function ensureBytes(bytes) {
|
|
102
|
-
if (i + bytes > wire.length) throw new Error("CBOR - Unexpected end of data");
|
|
103
|
-
}
|
|
104
|
-
const wire = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
105
|
-
const view = new DataView(wire.buffer, wire.byteOffset, wire.byteLength);
|
|
106
|
-
const decoder = new TextDecoder();
|
|
107
|
-
let i = 0, entities = 0;
|
|
108
|
-
return takeNextValue();
|
|
109
|
-
function takeNextValue() {
|
|
110
|
-
if (entities > maxEntities) throw new Error("CBOR - Maximum entities exceeded");
|
|
111
|
-
entities += 1;
|
|
112
|
-
ensureBytes(1);
|
|
113
|
-
let value = null;
|
|
114
|
-
const majorType = (wire[i] & 224) >> 5;
|
|
115
|
-
const minorType = wire[i] & 31;
|
|
116
|
-
if (majorType === 0) value = takeUInt(minorType);
|
|
117
|
-
else if (majorType === 1) value = -1 - takeUInt(minorType);
|
|
118
|
-
else if (majorType === 3) {
|
|
119
|
-
let length = takeUInt(minorType);
|
|
120
|
-
ensureBytes(length);
|
|
121
|
-
value = decoder.decode(wire.subarray(i, i + length));
|
|
122
|
-
i += length;
|
|
123
|
-
} else if (majorType === 4) {
|
|
124
|
-
const length = takeUInt(minorType);
|
|
125
|
-
value = [];
|
|
126
|
-
for (let j = 0; j < length; j++) {
|
|
127
|
-
const item = takeNextValue();
|
|
128
|
-
value.push(item);
|
|
129
|
-
}
|
|
130
|
-
} else if (majorType === 5) {
|
|
131
|
-
const length = takeUInt(minorType);
|
|
132
|
-
value = {};
|
|
133
|
-
for (let j = 0; j < length; j++) {
|
|
134
|
-
const key = String(takeNextValue());
|
|
135
|
-
const val = takeNextValue();
|
|
136
|
-
value[key] = val;
|
|
137
|
-
}
|
|
138
|
-
} else if (majorType === 6) {
|
|
139
|
-
const tag = takeUInt(minorType);
|
|
140
|
-
const taggedvalue = takeNextValue();
|
|
141
|
-
if (tag === TAG_ASYNC_FN) return AsyncFnKey;
|
|
142
|
-
if (tag === TAG_ASYNC_GEN) return AsyncGenKey;
|
|
143
|
-
return taggedvalue;
|
|
144
|
-
} else if (majorType === 7) if (minorType === 20) value = false;
|
|
145
|
-
else if (minorType === 21) value = true;
|
|
146
|
-
else if (minorType === 22) value = null;
|
|
147
|
-
else if (minorType === 23) value = void 0;
|
|
148
|
-
else if (minorType === 26) {
|
|
149
|
-
ensureBytes(5);
|
|
150
|
-
i += 1;
|
|
151
|
-
value = view.getFloat32(i, false);
|
|
152
|
-
i += 4;
|
|
153
|
-
} else if (minorType === 27) {
|
|
154
|
-
ensureBytes(9);
|
|
155
|
-
i += 1;
|
|
156
|
-
value = view.getFloat64(i, false);
|
|
157
|
-
i += 8;
|
|
158
|
-
} else throw new Error("CBOR - Unsupported simple value");
|
|
159
|
-
else throw new Error("CBOR - Unsupported major type " + majorType.toString(2).padStart(3, "0"));
|
|
160
|
-
return value;
|
|
161
|
-
}
|
|
162
|
-
function takeUInt(minorType) {
|
|
163
|
-
if (minorType < 24) {
|
|
164
|
-
i += 1;
|
|
165
|
-
return minorType;
|
|
166
|
-
}
|
|
167
|
-
const bytes = [
|
|
168
|
-
1,
|
|
169
|
-
2,
|
|
170
|
-
4,
|
|
171
|
-
8
|
|
172
|
-
][minorType - 24];
|
|
173
|
-
if (bytes === void 0) throw new Error("CBOR - Integer too large to decode");
|
|
174
|
-
ensureBytes(bytes + 1);
|
|
175
|
-
let num;
|
|
176
|
-
i += 1;
|
|
177
|
-
if (bytes === 1) num = view.getUint8(i);
|
|
178
|
-
else if (bytes === 2) num = view.getUint16(i, false);
|
|
179
|
-
else if (bytes === 4) num = view.getUint32(i, false);
|
|
180
|
-
else if (bytes === 8) {
|
|
181
|
-
const bigNum = view.getBigUint64(i, false);
|
|
182
|
-
if (bigNum > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error("CBOR - Number too large to safely represent in JavaScript");
|
|
183
|
-
num = Number(bigNum);
|
|
184
|
-
} else throw new Error("CBOR - Unsupported integer byte length");
|
|
185
|
-
i += bytes;
|
|
186
|
-
return num;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return {
|
|
190
|
-
type: "binary",
|
|
191
|
-
messageFromWire,
|
|
192
|
-
messageToWire
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
//#endregion
|
|
197
|
-
export { cborCodec };
|
|
198
|
-
//# sourceMappingURL=cbor.mjs.map
|
package/codecs/cbor.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cbor.mjs","names":[],"sources":["../../src/codecs/cbor.ts"],"sourcesContent":["import { AsyncFnKey, AsyncGenKey } from \"../../src/psionic\";\n\nconst TAG_ASYNC_FN = 32768;\nconst TAG_ASYNC_GEN = 32769;\n\nexport type CborCodecOptions = {\n /** Max entities per message. Default: 4096. */\n maxEntities?: number;\n}\n\nexport const cborCodec = function (options?: CborCodecOptions) {\n const maxEntities = options?.maxEntities ?? 0x1000;\n\n function messageToWire(message: any[]): Uint8Array {\n const scratch = new Uint8Array(0x10);\n const scratchView = new DataView(scratch.buffer);\n let output = new Uint8Array(0x200);\n let at = 0;\n function ensureSpace(bytes: number) {\n if (at + bytes <= output.length) return;\n let newsize = output.length * 2;\n while (newsize < at + bytes) newsize *= 2;\n if (newsize >= 0x3000000) {\n throw new Error('Message too large to encode');\n }\n const newBuffer = new Uint8Array(newsize);\n newBuffer.set(output);\n output = newBuffer;\n }\n function append(bytes: number[]) {\n ensureSpace(bytes.length);\n output.set(bytes, at);\n at += bytes.length;\n }\n const encoder = new TextEncoder();\n function writeItem(item: any) {\n if (typeof item === 'number' && Number.isInteger(item) && item >= 0) {\n writeUInt(0b000, item);\n } else if (typeof item === 'number' && Number.isInteger(item) && item < 0) {\n writeUInt(0b001, -1 - item);\n } else if (typeof item === 'string') {\n const bytes = encoder.encode(item);\n writeUInt(0b011, bytes.length);\n ensureSpace(bytes.length);\n output.set(bytes, at);\n at += bytes.length;\n } else if (Array.isArray(item)) {\n writeUInt(0b100, item.length);\n for (const subitem of item) {\n writeItem(subitem);\n }\n } else if (item === AsyncFnKey) {\n writeUInt(0b110, TAG_ASYNC_FN);\n append([(0b111 << 5) | 22]); // null payload — only the tag matters\n } else if (item === AsyncGenKey) {\n writeUInt(0b110, TAG_ASYNC_GEN);\n append([(0b111 << 5) | 22]); // null payload — only the tag matters\n } else if (item && typeof item === 'object') {\n const entries = Object.entries(item);\n writeUInt(0b101, entries.length);\n for (const [key, value] of entries) {\n const keyBytes = encoder.encode(key);\n writeUInt(0b011, keyBytes.length);\n ensureSpace(keyBytes.length);\n output.set(keyBytes, at);\n at += keyBytes.length;\n writeItem(value);\n }\n } else if (item === false) {\n append([0b111 << 5 | 20]);\n } else if (item === true) {\n append([0b111 << 5 | 21]);\n } else if (item === null) {\n append([0b111 << 5 | 22]);\n } else if (item === undefined) {\n append([0b111 << 5 | 23]);\n } else if (typeof item === 'number') {\n const f32 = Math.fround(item);\n let bytes = 8;\n if (Number.isFinite(f32) && f32 === item) {\n bytes = 4;\n }\n ensureSpace(bytes + 1);\n if (bytes === 4) scratchView.setFloat32(0, item, false);\n else scratchView.setFloat64(0, item, false);\n output[at++] = (0b111 << 5) | (bytes === 4 ? 26 : 27);\n for (let i = 0; i < bytes; i++) output[at++] = scratch[i];\n } else {\n throw new Error('Unsupported type');\n }\n }\n writeItem(message);\n return output.subarray(0, at);\n\n function writeUInt(majorTypeValue: number, num: number) {\n const majorType = majorTypeValue << 5;\n if (num < 24) {\n append([majorType | num]);\n return;\n }\n\n let length = 0;\n if (num > 0xFFFFFFFF) {\n length = 3;\n } else if (num > 0xFFFF) {\n length = 2;\n } else if (num > 0xFF) {\n length = 1;\n }\n const bytesNeeded = [1,2,4,8][length];\n if (bytesNeeded === 1) {\n scratchView.setUint8(0, num);\n } else if (bytesNeeded === 2) {\n scratchView.setUint16(0, num, false);\n } else if (bytesNeeded === 4) {\n scratchView.setUint32(0, num, false);\n } else if (bytesNeeded === 8) {\n scratchView.setBigUint64(0, BigInt(num), false);\n } else {\n throw new Error('Number too large for encoding');\n }\n ensureSpace(bytesNeeded + 1);\n output[at++] = majorType | (length + 24);\n for (let i = 0; i < bytesNeeded; i++) output[at++] = scratch[i];\n }\n };\n\n function messageFromWire(data: string | Uint8Array): any {\n function ensureBytes(bytes: number) {\n if (i + bytes > wire.length) {\n throw new Error('CBOR - Unexpected end of data');\n }\n }\n const wire = typeof data === 'string' ? new TextEncoder().encode(data) : data;\n const view = new DataView(wire.buffer, wire.byteOffset, wire.byteLength);\n const decoder = new TextDecoder();\n let i = 0, entities = 0;\n const result = takeNextValue();\n return result;\n\n function takeNextValue(): any {\n if (entities > maxEntities) throw new Error('CBOR - Maximum entities exceeded');\n entities += 1;\n ensureBytes(1);\n let value: any = null;\n const majorType = (wire[i] & 0b11100000) >> 5;\n const minorType = (wire[i] & 0b00011111);\n if (majorType === 0b000) {\n value = takeUInt(minorType);\n } else if (majorType === 0b001) {\n value = -1 - takeUInt(minorType);\n } else if (majorType === 0b011) {\n let length = takeUInt(minorType);\n ensureBytes(length);\n value = decoder.decode(wire.subarray(i, i + length));\n i += length;\n } else if (majorType === 0b100) {\n const length = takeUInt(minorType);\n value = [];\n for (let j = 0; j < length; j++) {\n const item = takeNextValue();\n value.push(item);\n }\n } else if (majorType === 0b101) {\n const length = takeUInt(minorType);\n value = {};\n for (let j = 0; j < length; j++) {\n const key = String(takeNextValue());\n const val = takeNextValue();\n value[key] = val;\n }\n } else if (majorType === 0b110) {\n const tag = takeUInt(minorType);\n const taggedvalue = takeNextValue();\n if (tag === TAG_ASYNC_FN) return AsyncFnKey;\n if (tag === TAG_ASYNC_GEN) return AsyncGenKey;\n return taggedvalue;\n } else if (majorType === 0b111) {\n if (minorType === 20) value = false;\n else if (minorType === 21) value = true;\n else if (minorType === 22) value = null;\n else if (minorType === 23) value = undefined;\n else if (minorType === 26) {\n ensureBytes(5);\n i += 1;\n value = view.getFloat32(i, false);\n i += 4;\n } else if (minorType === 27) {\n ensureBytes(9);\n i += 1;\n value = view.getFloat64(i, false);\n i += 8;\n } else {\n throw new Error('CBOR - Unsupported simple value');\n }\n } else {\n throw new Error('CBOR - Unsupported major type ' + majorType.toString(2).padStart(3, '0'));\n }\n return value;\n }\n\n function takeUInt(minorType: number): number {\n if (minorType < 24) {\n i += 1;\n return minorType;\n }\n\n const addl = minorType - 24;\n const bytes = [1,2,4,8][addl];\n if (bytes === undefined) throw new Error('CBOR - Integer too large to decode');\n ensureBytes(bytes + 1);\n let num: number;\n i += 1;\n if (bytes === 1) {\n num = view.getUint8(i);\n } else if (bytes === 2) {\n num = view.getUint16(i, false);\n } else if (bytes === 4) {\n num = view.getUint32(i, false);\n } else if (bytes === 8) {\n const bigNum = view.getBigUint64(i, false);\n if (bigNum > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new Error('CBOR - Number too large to safely represent in JavaScript');\n }\n num = Number(bigNum);\n } else {\n throw new Error('CBOR - Unsupported integer byte length');\n }\n i += bytes;\n return num;\n }\n }\n\n return {\n type: 'binary' as const,\n messageFromWire,\n messageToWire\n };\n}\n"],"mappings":";;;AAEA,MAAM,eAAgB;AACtB,MAAM,gBAAgB;AAOtB,MAAa,YAAY,SAAU,SAA4B;CAC3D,MAAM,cAAc,SAAS,eAAe;CAE5C,SAAS,cAAc,SAA4B;EAC/C,MAAM,UAAU,IAAI,WAAW,GAAK;EACpC,MAAM,cAAc,IAAI,SAAS,QAAQ,OAAO;EAChD,IAAI,SAAS,IAAI,WAAW,IAAM;EAClC,IAAI,KAAK;EACT,SAAS,YAAY,OAAe;AAChC,OAAI,KAAK,SAAS,OAAO,OAAQ;GACjC,IAAI,UAAU,OAAO,SAAS;AAC9B,UAAO,UAAU,KAAK,MAAO,YAAW;AACxC,OAAI,WAAW,SACX,OAAM,IAAI,MAAM,8BAA8B;GAElD,MAAM,YAAY,IAAI,WAAW,QAAQ;AACzC,aAAU,IAAI,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1B,cAAc,GAAI,SAAQ;YAC1B,cAAc,GAAI,SAAQ;YAC1B,cAAc,IAAI;AACvB,gBAAY,EAAE;AACd,SAAK;AACL,YAAQ,KAAK,WAAW,GAAG,MAAM;AACjC,SAAK;cACE,cAAc,IAAI;AACzB,gBAAY,EAAE;AACd,SAAK;AACL,YAAQ,KAAK,WAAW,GAAG,MAAM;AACjC,SAAK;SAEL,OAAM,IAAI,MAAM,kCAAkC;OAGtD,OAAM,IAAI,MAAM,mCAAmC,UAAU,SAAS,EAAE,CAAC,SAAS,GAAG,IAAI,CAAC;AAE9F,UAAO;;EAGX,SAAS,SAAS,WAA2B;AACzC,OAAI,YAAY,IAAI;AAChB,SAAK;AACL,WAAO;;GAIX,MAAM,QAAQ;IAAC;IAAE;IAAE;IAAE;IAAE,CADV,YAAY;AAEzB,OAAI,UAAU,OAAW,OAAM,IAAI,MAAM,qCAAqC;AAC9E,eAAY,QAAQ,EAAE;GACtB,IAAI;AACJ,QAAK;AACL,OAAI,UAAU,EACV,OAAM,KAAK,SAAS,EAAE;YACf,UAAU,EACjB,OAAM,KAAK,UAAU,GAAG,MAAM;YACvB,UAAU,EACjB,OAAM,KAAK,UAAU,GAAG,MAAM;YACvB,UAAU,GAAG;IACpB,MAAM,SAAS,KAAK,aAAa,GAAG,MAAM;AAC1C,QAAI,SAAS,OAAO,OAAO,iBAAiB,CACxC,OAAM,IAAI,MAAM,4DAA4D;AAEhF,UAAM,OAAO,OAAO;SAEpB,OAAM,IAAI,MAAM,yCAAyC;AAE7D,QAAK;AACL,UAAO;;;AAIf,QAAO;EACH,MAAM;EACN;EACA;EACH"}
|
package/codecs/psionic.d.mts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
//#region src/codecs/psionic.d.ts
|
|
2
|
-
type PsionicCodecOptions = {
|
|
3
|
-
/** Max entities per message. Default: 4096. */maxEntities?: number;
|
|
4
|
-
};
|
|
5
|
-
declare const psionicCodec: (options?: PsionicCodecOptions) => {
|
|
6
|
-
type: "text";
|
|
7
|
-
messageFromWire: (data: Uint8Array | string) => any;
|
|
8
|
-
messageToWire: (message: any[]) => Uint8Array | string;
|
|
9
|
-
};
|
|
10
|
-
//#endregion
|
|
11
|
-
export { PsionicCodecOptions, psionicCodec };
|
|
12
|
-
//# sourceMappingURL=psionic.d.mts.map
|
package/codecs/psionic.mjs
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { ErrorProtocolViolation, Keyword, OperationError } from "../psionic.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/codecs/psionic.ts
|
|
4
|
-
const psionicCodec = (options = {}) => {
|
|
5
|
-
const maxEntities = options.maxEntities ?? 4096;
|
|
6
|
-
function messageToWire(message) {
|
|
7
|
-
let str = String(message[0]);
|
|
8
|
-
let j;
|
|
9
|
-
for (j = message.length - 1; j >= 0; --j) if (message[j] !== void 0) break;
|
|
10
|
-
for (let i = 1; i <= j; i++) str += " " + JSON.stringify(message[i]);
|
|
11
|
-
return str;
|
|
12
|
-
}
|
|
13
|
-
function messageFromWire(data) {
|
|
14
|
-
const wire = typeof data === "string" ? data : new TextDecoder().decode(data);
|
|
15
|
-
let braceStack = 0, arrayStack = 0, inString = 0, entityCount = 0;
|
|
16
|
-
let formatted = [];
|
|
17
|
-
let start = 0;
|
|
18
|
-
let escaping = false;
|
|
19
|
-
for (let i = 0; i <= wire.length; i++) if (wire[i] === " ") {
|
|
20
|
-
const value = wire.slice(0, i);
|
|
21
|
-
formatted.push(value);
|
|
22
|
-
start = i + 1;
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
for (let i = start; i <= wire.length; i++) {
|
|
26
|
-
if (entityCount > maxEntities) throw new OperationError(`${Keyword}: Maximum entity count exceeded`, ErrorProtocolViolation.code);
|
|
27
|
-
if (wire[i] === "{" && !inString) {
|
|
28
|
-
braceStack++;
|
|
29
|
-
entityCount++;
|
|
30
|
-
} else if (wire[i] === "}" && !inString) braceStack--;
|
|
31
|
-
else if (wire[i] === "[" && !inString) {
|
|
32
|
-
arrayStack++;
|
|
33
|
-
entityCount++;
|
|
34
|
-
} else if (wire[i] === "]" && !inString) arrayStack--;
|
|
35
|
-
else if (wire[i] === "\\" && inString) escaping = !escaping;
|
|
36
|
-
else if (wire[i] === "\"") {
|
|
37
|
-
if (!escaping) inString = inString ? 0 : 1;
|
|
38
|
-
escaping = false;
|
|
39
|
-
} else if (i === wire.length && start < i || wire[i] === " " && braceStack === 0 && arrayStack === 0 && !inString) {
|
|
40
|
-
entityCount++;
|
|
41
|
-
const value = wire.slice(start, i);
|
|
42
|
-
formatted.push(JSON.parse(value));
|
|
43
|
-
start = i + 1;
|
|
44
|
-
} else escaping = false;
|
|
45
|
-
}
|
|
46
|
-
return formatted;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
type: "text",
|
|
50
|
-
messageFromWire,
|
|
51
|
-
messageToWire
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
//#endregion
|
|
56
|
-
export { psionicCodec };
|
|
57
|
-
//# sourceMappingURL=psionic.mjs.map
|
package/codecs/psionic.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"psionic.mjs","names":[],"sources":["../../src/codecs/psionic.ts"],"sourcesContent":["import { ErrorProtocolViolation, Keyword, OperationError} from \"../psionic\";\n\nexport type PsionicCodecOptions = {\n /** Max entities per message. Default: 4096. */\n maxEntities?: number;\n}\n\nexport const psionicCodec = (options: PsionicCodecOptions = {}) => {\n const maxEntities = options.maxEntities ?? 0x1000;\n\n function messageToWire(message: any[]): Uint8Array | string {\n let str = String(message[0]);\n let j: number;\n for (j = message.length - 1; j >= 0; --j) {\n if (message[j] !== undefined) break;\n }\n for (let i = 1; i <= j; i++) {\n str += ' ' + JSON.stringify(message[i]);\n }\n return str;\n };\n\n function messageFromWire(data: Uint8Array | string): any {\n const wire = typeof data === 'string' ? data : new TextDecoder().decode(data);\n let braceStack = 0, arrayStack = 0, inString = 0, entityCount = 0;\n let formatted = [];\n let start = 0;\n let escaping = false;\n // Extract the op code (first space-delimited token).\n for (let i = 0; i <= wire.length; i++) {\n if (wire[i] === ' ') {\n const value = wire.slice(0, i);\n formatted.push(value);\n start = i + 1;\n break;\n }\n }\n // Parse remaining space-delimited values, respecting JSON nesting.\n for (let i = start; i <= wire.length; i++) {\n if (entityCount > maxEntities) {\n throw new OperationError(`${Keyword}: Maximum entity count exceeded`, ErrorProtocolViolation.code);\n }\n if (wire[i] === '{' && !inString) {\n braceStack++;\n entityCount++;\n } else if (wire[i] === '}' && !inString) {\n braceStack--;\n } else if (wire[i] === '[' && !inString) {\n arrayStack++;\n entityCount++;\n } else if (wire[i] === ']' && !inString) {\n arrayStack--;\n } else if (wire[i] === '\\\\' && inString) {\n escaping = !escaping;\n } else if (wire[i] === '\"') {\n if (!escaping) inString = inString ? 0 : 1;\n escaping = false;\n } else if ((i === wire.length && start < i) || (wire[i] === ' ' && braceStack === 0 && arrayStack === 0 && !inString)) {\n entityCount++;\n const value = wire.slice(start, i);\n formatted.push(JSON.parse(value));\n start = i + 1;\n } else {\n escaping = false;\n }\n }\n return formatted;\n }\n\n return {\n type: 'text' as const,\n messageFromWire,\n messageToWire\n };\n};\n"],"mappings":";;;AAOA,MAAa,gBAAgB,UAA+B,EAAE,KAAK;CAC/D,MAAM,cAAc,QAAQ,eAAe;CAE3C,SAAS,cAAc,SAAqC;EACxD,IAAI,MAAM,OAAO,QAAQ,GAAG;EAC5B,IAAI;AACJ,OAAK,IAAI,QAAQ,SAAS,GAAG,KAAK,GAAG,EAAE,EACnC,KAAI,QAAQ,OAAO,OAAW;AAElC,OAAK,IAAI,IAAI,GAAG,KAAK,GAAG,IACpB,QAAO,MAAM,KAAK,UAAU,QAAQ,GAAG;AAE3C,SAAO;;CAGX,SAAS,gBAAgB,MAAgC;EACrD,MAAM,OAAO,OAAO,SAAS,WAAW,OAAO,IAAI,aAAa,CAAC,OAAO,KAAK;EAC7E,IAAI,aAAa,GAAG,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