psionic 0.3.3 → 0.3.5

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/codecs/cbor.mjs CHANGED
@@ -1,2 +1,198 @@
1
- import{AsyncFnKey as e,AsyncGenKey as t}from"../psionic.mjs";const n=32768,r=32769,i=function(i){let a=i?.maxEntities??4096;function o(i){let a=new Uint8Array(16),o=new DataView(a.buffer),s=new Uint8Array(512),c=0;function l(e){if(c+e<=s.length)return;let t=s.length*2;for(;t<c+e;)t*=2;if(t>=50331648)throw Error(`Message too large to encode`);let n=new Uint8Array(t);n.set(s),s=n}function u(e){l(e.length),s.set(e,c),c+=e.length}let d=new TextEncoder;function f(i){if(typeof i==`number`&&Number.isInteger(i)&&i>=0)p(0,i);else if(typeof i==`number`&&Number.isInteger(i)&&i<0)p(1,-1-i);else if(typeof i==`string`){let e=d.encode(i);p(3,e.length),l(e.length),s.set(e,c),c+=e.length}else if(Array.isArray(i)){p(4,i.length);for(let e of i)f(e)}else if(i===e)p(6,n),u([246]);else if(i===t)p(6,r),u([246]);else if(i&&typeof i==`object`){let e=Object.entries(i);p(5,e.length);for(let[t,n]of e){let e=d.encode(t);p(3,e.length),l(e.length),s.set(e,c),c+=e.length,f(n)}}else if(i===!1)u([244]);else if(i===!0)u([245]);else if(i===null)u([246]);else if(i===void 0)u([247]);else if(typeof i==`number`){let e=Math.fround(i),t=8;Number.isFinite(e)&&e===i&&(t=4),l(t+1),t===4?o.setFloat32(0,i,!1):o.setFloat64(0,i,!1),s[c++]=224|(t===4?26:27);for(let e=0;e<t;e++)s[c++]=a[e]}else throw Error(`Unsupported type`)}return f(i),s.subarray(0,c);function p(e,t){let n=e<<5;if(t<24){u([n|t]);return}let r=0;t>4294967295?r=3:t>65535?r=2:t>255&&(r=1);let i=[1,2,4,8][r];if(i===1)o.setUint8(0,t);else if(i===2)o.setUint16(0,t,!1);else if(i===4)o.setUint32(0,t,!1);else if(i===8)o.setBigUint64(0,BigInt(t),!1);else throw Error(`Number too large for encoding`);l(i+1),s[c++]=n|r+24;for(let e=0;e<i;e++)s[c++]=a[e]}}function s(i){function o(e){if(u+e>s.length)throw Error(`CBOR - Unexpected end of data`)}let s=typeof i==`string`?new TextEncoder().encode(i):i,c=new DataView(s.buffer,s.byteOffset,s.byteLength),l=new TextDecoder,u=0,d=0;return f();function f(){if(d>a)throw Error(`CBOR - Maximum entities exceeded`);d+=1,o(1);let i=null,m=(s[u]&224)>>5,h=s[u]&31;if(m===0)i=p(h);else if(m===1)i=-1-p(h);else if(m===3){let e=p(h);o(e),i=l.decode(s.subarray(u,u+e)),u+=e}else if(m===4){let e=p(h);i=[];for(let t=0;t<e;t++){let e=f();i.push(e)}}else if(m===5){let e=p(h);i={};for(let t=0;t<e;t++){let e=String(f()),t=f();i[e]=t}}else if(m===6){let i=p(h),a=f();return i===n?e:i===r?t:a}else if(m===7)if(h===20)i=!1;else if(h===21)i=!0;else if(h===22)i=null;else if(h===23)i=void 0;else if(h===26)o(5),u+=1,i=c.getFloat32(u,!1),u+=4;else if(h===27)o(9),u+=1,i=c.getFloat64(u,!1),u+=8;else throw Error(`CBOR - Unsupported simple value`);else throw Error(`CBOR - Unsupported major type `+m.toString(2).padStart(3,`0`));return i}function p(e){if(e<24)return u+=1,e;let t=[1,2,4,8][e-24];if(t===void 0)throw Error(`CBOR - Integer too large to decode`);o(t+1);let n;if(u+=1,t===1)n=c.getUint8(u);else if(t===2)n=c.getUint16(u,!1);else if(t===4)n=c.getUint32(u,!1);else if(t===8){let e=c.getBigUint64(u,!1);if(e>BigInt(2**53-1))throw Error(`CBOR - Number too large to safely represent in JavaScript`);n=Number(e)}else throw Error(`CBOR - Unsupported integer byte length`);return u+=t,n}}return{type:`binary`,messageFromWire:s,messageToWire:o}};export{i as cborCodec};
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
@@ -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,8B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,EAAQ,EAAK,WAAW,EAAG,GAAM,CACjC,GAAK,UACE,IAAc,GACrB,EAAY,EAAE,CACd,GAAK,EACL,EAAQ,EAAK,WAAW,EAAG,GAAM,CACjC,GAAK,OAEL,MAAU,MAAM,kCAAkC,MAGtD,MAAU,MAAM,iCAAmC,EAAU,SAAS,EAAE,CAAC,SAAS,EAAG,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;AAC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U,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"}
@@ -1,2 +1,57 @@
1
- import{ErrorProtocolViolation as e,Keyword as t,OperationError as n}from"../psionic.mjs";const r=(r={})=>{let i=r.maxEntities??4096;function a(e){let t=String(e[0]),n;for(n=e.length-1;n>=0&&e[n]===void 0;--n);for(let r=1;r<=n;r++)t+=` `+JSON.stringify(e[r]);return t}function o(r){let a=typeof r==`string`?r:new TextDecoder().decode(r),o=0,s=0,c=0,l=0,u=[],d=0,f=!1;for(let e=0;e<=a.length;e++)if(a[e]===` `){let t=a.slice(0,e);u.push(t),d=e+1;break}for(let r=d;r<=a.length;r++){if(l>i)throw new n(`${t}: Maximum entity count exceeded`,e.code);if(a[r]===`{`&&!c)o++,l++;else if(a[r]===`}`&&!c)o--;else if(a[r]===`[`&&!c)s++,l++;else if(a[r]===`]`&&!c)s--;else if(a[r]===`\\`&&c)f=!f;else if(a[r]===`"`)f||(c=c?0:1),f=!1;else if(r===a.length&&d<r||a[r]===` `&&o===0&&s===0&&!c){l++;let e=a.slice(d,r);u.push(JSON.parse(e)),d=r+1}else f=!1}return u}return{type:`text`,messageFromWire:o,messageToWire:a}};export{r as psionicCodec};
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
@@ -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":"yFAOA,MAAa,GAAgB,EAA+B,EAAE,GAAK,CAC/D,IAAM,EAAc,EAAQ,aAAe,KAE3C,SAAS,EAAc,EAAqC,CACxD,IAAI,EAAM,OAAO,EAAQ,GAAG,CACxB,EACJ,IAAK,EAAI,EAAQ,OAAS,EAAG,GAAK,GAC1B,EAAQ,KAAO,IAAA,GADc,EAAE,GAGvC,IAAK,IAAI,EAAI,EAAG,GAAK,EAAG,IACpB,GAAO,IAAM,KAAK,UAAU,EAAQ,GAAG,CAE3C,OAAO,EAGX,SAAS,EAAgB,EAAgC,CACrD,IAAM,EAAO,OAAO,GAAS,SAAW,EAAO,IAAI,aAAa,CAAC,OAAO,EAAK,CACzE,EAAa,EAAG,EAAa,EAAG,EAAW,EAAG,EAAc,EAC5D,EAAY,EAAE,CACd,EAAQ,EACR,EAAW,GAEf,IAAK,IAAI,EAAI,EAAG,GAAK,EAAK,OAAQ,IAC9B,GAAI,EAAK,KAAO,IAAK,CACjB,IAAM,EAAQ,EAAK,MAAM,EAAG,EAAE,CAC9B,EAAU,KAAK,EAAM,CACrB,EAAQ,EAAI,EACZ,MAIR,IAAK,IAAI,EAAI,EAAO,GAAK,EAAK,OAAQ,IAAK,CACvC,GAAI,EAAc,EACd,MAAM,IAAI,EAAe,GAAG,EAAQ,iCAAkC,EAAuB,KAAK,CAEtG,GAAI,EAAK,KAAO,KAAO,CAAC,EACpB,IACA,YACO,EAAK,KAAO,KAAO,CAAC,EAC3B,YACO,EAAK,KAAO,KAAO,CAAC,EAC3B,IACA,YACO,EAAK,KAAO,KAAO,CAAC,EAC3B,YACO,EAAK,KAAO,MAAQ,EAC3B,EAAW,CAAC,UACL,EAAK,KAAO,IACd,IAAU,EAAW,EAAW,EAAI,GACzC,EAAW,WACH,IAAM,EAAK,QAAU,EAAQ,GAAO,EAAK,KAAO,KAAO,IAAe,GAAK,IAAe,GAAK,CAAC,EAAW,CACnH,IACA,IAAM,EAAQ,EAAK,MAAM,EAAO,EAAE,CAClC,EAAU,KAAK,KAAK,MAAM,EAAM,CAAC,CACjC,EAAQ,EAAI,OAEZ,EAAW,GAGnB,OAAO,EAGX,MAAO,CACH,KAAM,OACN,kBACA,gBACH"}
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,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;AAClC,cAAU,KAAK,KAAK,MAAM,MAAM,CAAC;AACjC,YAAQ,IAAI;SAEZ,YAAW;;AAGnB,SAAO;;AAGX,QAAO;EACH,MAAM;EACN;EACA;EACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "psionic",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "Light-weight streaming RPC library for JavaScript",
5
5
  "license": "ISC",
6
6
  "exports": {