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
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ErrorProtocolViolation, Keyword, OperationError} from "../psionic";
|
|
2
|
+
|
|
3
|
+
export type PsionicCodecOptions = {
|
|
4
|
+
/** Max entities per message. Default: 4096. */
|
|
5
|
+
maxEntities?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const psionicCodec = (options: PsionicCodecOptions = {}) => {
|
|
9
|
+
const maxEntities = options.maxEntities ?? 0x1000;
|
|
10
|
+
|
|
11
|
+
function messageToWire(message: any[]): Uint8Array | string {
|
|
12
|
+
let str = String(message[0]);
|
|
13
|
+
let j: number;
|
|
14
|
+
for (j = message.length - 1; j >= 0; --j) {
|
|
15
|
+
if (message[j] !== undefined) break;
|
|
16
|
+
}
|
|
17
|
+
for (let i = 1; i <= j; i++) {
|
|
18
|
+
str += ' ' + JSON.stringify(message[i]);
|
|
19
|
+
}
|
|
20
|
+
return str;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function messageFromWire(data: Uint8Array | string): any {
|
|
24
|
+
const wire = typeof data === 'string' ? data : new TextDecoder().decode(data);
|
|
25
|
+
let braceStack = 0, arrayStack = 0, inString = 0, entityCount = 0;
|
|
26
|
+
let formatted = [];
|
|
27
|
+
let start = 0;
|
|
28
|
+
let escaping = false;
|
|
29
|
+
// Extract the op code (first space-delimited token).
|
|
30
|
+
for (let i = 0; i <= wire.length; i++) {
|
|
31
|
+
if (wire[i] === ' ') {
|
|
32
|
+
const value = wire.slice(0, i);
|
|
33
|
+
formatted.push(value);
|
|
34
|
+
start = i + 1;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Parse remaining space-delimited values, respecting JSON nesting.
|
|
39
|
+
for (let i = start; i <= wire.length; i++) {
|
|
40
|
+
if (entityCount > maxEntities) {
|
|
41
|
+
throw new OperationError(`${Keyword}: Maximum entity count exceeded`, ErrorProtocolViolation.code);
|
|
42
|
+
}
|
|
43
|
+
if (wire[i] === '{' && !inString) {
|
|
44
|
+
braceStack++;
|
|
45
|
+
entityCount++;
|
|
46
|
+
} else if (wire[i] === '}' && !inString) {
|
|
47
|
+
braceStack--;
|
|
48
|
+
} else if (wire[i] === '[' && !inString) {
|
|
49
|
+
arrayStack++;
|
|
50
|
+
entityCount++;
|
|
51
|
+
} else if (wire[i] === ']' && !inString) {
|
|
52
|
+
arrayStack--;
|
|
53
|
+
} else if (wire[i] === '\\' && inString) {
|
|
54
|
+
escaping = !escaping;
|
|
55
|
+
} else if (wire[i] === '"') {
|
|
56
|
+
if (!escaping) inString = inString ? 0 : 1;
|
|
57
|
+
escaping = false;
|
|
58
|
+
} else if ((i === wire.length && start < i) || (wire[i] === ' ' && braceStack === 0 && arrayStack === 0 && !inString)) {
|
|
59
|
+
entityCount++;
|
|
60
|
+
const value = wire.slice(start, i);
|
|
61
|
+
formatted.push(JSON.parse(value));
|
|
62
|
+
start = i + 1;
|
|
63
|
+
} else {
|
|
64
|
+
escaping = false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return formatted;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
type: 'text' as const,
|
|
72
|
+
messageFromWire,
|
|
73
|
+
messageToWire
|
|
74
|
+
};
|
|
75
|
+
};
|