psionic 0.3.3 → 0.3.4
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/adapters/reconnect.mjs +41 -1
- package/adapters/reconnect.mjs.map +1 -1
- package/adapters/resend.mjs +25 -1
- package/adapters/resend.mjs.map +1 -1
- package/adapters/socket.mjs +159 -2
- package/adapters/socket.mjs.map +1 -1
- package/adapters/websocket.mjs +134 -1
- package/adapters/websocket.mjs.map +1 -1
- package/adapters/webworker.mjs +41 -1
- package/adapters/webworker.mjs.map +1 -1
- package/codecs/cbor.mjs +197 -1
- package/codecs/cbor.mjs.map +1 -1
- package/codecs/psionic.mjs +56 -1
- package/codecs/psionic.mjs.map +1 -1
- package/package.json +1 -1
- package/psionic.mjs +670 -1
- package/psionic.mjs.map +1 -1
- package/session.mjs +91 -1
- package/session.mjs.map +1 -1
- package/utils/react-hooks.mjs +36 -1
- package/utils/react-hooks.mjs.map +1 -1
package/codecs/cbor.mjs
CHANGED
|
@@ -1,2 +1,198 @@
|
|
|
1
|
-
import{AsyncFnKey
|
|
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 };
|
|
2
198
|
//# sourceMappingURL=cbor.mjs.map
|
package/codecs/cbor.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":"6DAEA,MAAM,EAAgB,MAChB,EAAgB,MAOT,EAAY,SAAU,EAA4B,CAC3D,IAAM,EAAc,GAAS,aAAe,KAE5C,SAAS,EAAc,EAA4B,CAC/C,IAAM,EAAU,IAAI,WAAW,GAAK,CAC9B,EAAc,IAAI,SAAS,EAAQ,OAAO,CAC5C,EAAS,IAAI,WAAW,IAAM,CAC9B,EAAK,EACT,SAAS,EAAY,EAAe,CAChC,GAAI,EAAK,GAAS,EAAO,OAAQ,OACjC,IAAI,EAAU,EAAO,OAAS,EAC9B,KAAO,EAAU,EAAK,GAAO,GAAW,EACxC,GAAI,GAAW,SACX,MAAU,MAAM,8BAA8B,CAElD,IAAM,EAAY,IAAI,WAAW,EAAQ,CACzC,EAAU,IAAI,EAAO,CACrB,EAAS,EAEb,SAAS,EAAO,EAAiB,CAC7B,EAAY,EAAM,OAAO,CACzB,EAAO,IAAI,EAAO,EAAG,CACrB,GAAM,EAAM,OAEhB,IAAM,EAAU,IAAI,YACpB,SAAS,EAA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AG,IAAI,CAAC,CAE9F,OAAO,EAGX,SAAS,EAAS,EAA2B,CACzC,GAAI,EAAY,GAEZ,MADA,IAAK,EACE,EAIX,IAAM,EAAQ,CAAC,EAAE,EAAE,EAAE,EAAE,CADV,EAAY,IAEzB,GAAI,IAAU,IAAA,GAAW,MAAU,MAAM,qCAAqC,CAC9E,EAAY,EAAQ,EAAE,CACtB,IAAI,EAEJ,GADA,GAAK,EACD,IAAU,EACV,EAAM,EAAK,SAAS,EAAE,SACf,IAAU,EACjB,EAAM,EAAK,UAAU,EAAG,GAAM,SACvB,IAAU,EACjB,EAAM,EAAK,UAAU,EAAG,GAAM,SACvB,IAAU,EAAG,CACpB,IAAM,EAAS,EAAK,aAAa,EAAG,GAAM,CAC1C,GAAI,EAAS,eAA+B,CACxC,MAAU,MAAM,4DAA4D,CAEhF,EAAM,OAAO,EAAO,MAEpB,MAAU,MAAM,yCAAyC,CAG7D,MADA,IAAK,EACE,GAIf,MAAO,CACH,KAAM,SACN,kBACA,gBACH"}
|
|
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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f,QAAO;EACH,MAAM;EACN;EACA;EACH"}
|
package/codecs/psionic.mjs
CHANGED
|
@@ -1,2 +1,57 @@
|
|
|
1
|
-
import{ErrorProtocolViolation
|
|
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 };
|
|
2
57
|
//# sourceMappingURL=psionic.mjs.map
|
package/codecs/psionic.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":"
|
|
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,aAAa,GAAG,WAAW,GAAG,cAAc;EAChE,IAAI,YAAY,EAAE;EAClB,IAAI,QAAQ;EACZ,I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