@the-w1zard/singularity 1.0.10 → 1.0.12
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.html +2946 -5
- package/README.md +2974 -0
- package/dist/comps/DISRUPTOR_CRYPTO.js +293 -0
- package/dist/comps/binary.js +611 -0
- package/dist/comps/byte_id.js +40 -0
- package/dist/comps/cell-worker.js +120 -0
- package/dist/comps/cell.js +2478 -0
- package/dist/comps/create_live_self.js +22 -0
- package/dist/comps/queue.js +532 -0
- package/dist/comps/vm-types.js +1 -0
- package/dist/db/client/kv_idb.js +590 -0
- package/dist/db/server/bench.js +28 -0
- package/dist/db/server/binary.js +43 -0
- package/dist/db/server/constants.js +8 -0
- package/dist/db/server/crc.js +15 -0
- package/dist/db/server/db.js +1475 -0
- package/dist/db/server/manifest.js +34 -0
- package/dist/db/server/pages.js +100 -0
- package/dist/db/server/storage-worker.js +152 -0
- package/dist/db/server/types.js +14 -0
- package/dist/db/server/wal.js +81 -0
- package/dist/index.js +1 -0
- package/dist/signals.js +18435 -0
- package/dist/y.js +1324 -0
- package/package.json +14 -6
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
const TAG = {
|
|
2
|
+
UNDEFINED: 0,
|
|
3
|
+
NULL: 1,
|
|
4
|
+
FALSE: 2,
|
|
5
|
+
TRUE: 3,
|
|
6
|
+
INT32: 4,
|
|
7
|
+
FLOAT64: 5,
|
|
8
|
+
STRING: 6,
|
|
9
|
+
ARRAY: 7,
|
|
10
|
+
OBJECT: 8,
|
|
11
|
+
UINT8ARRAY: 9,
|
|
12
|
+
REF: 10,
|
|
13
|
+
};
|
|
14
|
+
const encoder = new TextEncoder();
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
export class BINARY_CODEC {
|
|
17
|
+
encodeJSONEnvelope(envelope) {
|
|
18
|
+
return new TextEncoder().encode(JSON.stringify(envelope));
|
|
19
|
+
}
|
|
20
|
+
decodeJSONEnvelope(bytes) {
|
|
21
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
22
|
+
}
|
|
23
|
+
/* =============================================================================
|
|
24
|
+
Encodes ordinary JavaScript values into a collider-owned byte slab.
|
|
25
|
+
================================= ⇦✦──⇦✦──✦──✦──←'pew pew' 🧙 [BINARY_CODEC] */
|
|
26
|
+
bytes;
|
|
27
|
+
view;
|
|
28
|
+
tail = 0;
|
|
29
|
+
strings = new Map();
|
|
30
|
+
stringBytes = [];
|
|
31
|
+
refs = new Map();
|
|
32
|
+
nextRef = 0;
|
|
33
|
+
encode_js(value) {
|
|
34
|
+
this.tail = 0;
|
|
35
|
+
this.strings.clear();
|
|
36
|
+
this.stringBytes.length = 0;
|
|
37
|
+
this.refs.clear();
|
|
38
|
+
this.nextRef = 0;
|
|
39
|
+
this.writeValue(value);
|
|
40
|
+
return this.bytes.subarray(0, this.tail);
|
|
41
|
+
}
|
|
42
|
+
/* HC_BINARY_CODEC */
|
|
43
|
+
decode_js(binary) {
|
|
44
|
+
const savedBytes = this.bytes;
|
|
45
|
+
const savedView = this.view;
|
|
46
|
+
const savedTail = this.tail;
|
|
47
|
+
try {
|
|
48
|
+
this.bytes = binary;
|
|
49
|
+
this.view = new DataView(binary.buffer, binary.byteOffset, binary.byteLength);
|
|
50
|
+
this.tail = binary.length;
|
|
51
|
+
this.refs.clear();
|
|
52
|
+
this.nextRef = 0;
|
|
53
|
+
const cursor = { at: 0 };
|
|
54
|
+
return this.readValue(cursor);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
// Restore the encode-side buffer so the next encode()
|
|
58
|
+
// never writes into the disruptor pool.
|
|
59
|
+
this.bytes = savedBytes;
|
|
60
|
+
this.view = savedView;
|
|
61
|
+
this.tail = savedTail;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
need(count) {
|
|
65
|
+
if (this.tail + count <= this.bytes.length)
|
|
66
|
+
return;
|
|
67
|
+
let size = this.bytes.length;
|
|
68
|
+
while (size < this.tail + count)
|
|
69
|
+
size *= 2;
|
|
70
|
+
const next = new Uint8Array(size);
|
|
71
|
+
next.set(this.bytes);
|
|
72
|
+
this.bytes = next;
|
|
73
|
+
this.view = new DataView(next.buffer);
|
|
74
|
+
}
|
|
75
|
+
u8(value) {
|
|
76
|
+
this.need(1);
|
|
77
|
+
this.bytes[this.tail++] = value;
|
|
78
|
+
}
|
|
79
|
+
i32(value) {
|
|
80
|
+
this.need(4);
|
|
81
|
+
this.view.setInt32(this.tail, value, true);
|
|
82
|
+
this.tail += 4;
|
|
83
|
+
}
|
|
84
|
+
f64(value) {
|
|
85
|
+
this.need(8);
|
|
86
|
+
this.view.setFloat64(this.tail, value, true);
|
|
87
|
+
this.tail += 8;
|
|
88
|
+
}
|
|
89
|
+
writeValue(value) {
|
|
90
|
+
if (value === undefined)
|
|
91
|
+
return this.u8(TAG.UNDEFINED);
|
|
92
|
+
if (value === null)
|
|
93
|
+
return this.u8(TAG.NULL);
|
|
94
|
+
if (value === false)
|
|
95
|
+
return this.u8(TAG.FALSE);
|
|
96
|
+
if (value === true)
|
|
97
|
+
return this.u8(TAG.TRUE);
|
|
98
|
+
const type = typeof value;
|
|
99
|
+
if (type === "number") {
|
|
100
|
+
const n = value;
|
|
101
|
+
if (Number.isSafeInteger(n) && n >= -2147483648 && n <= 2147483647) {
|
|
102
|
+
this.u8(TAG.INT32);
|
|
103
|
+
return this.i32(n);
|
|
104
|
+
}
|
|
105
|
+
this.u8(TAG.FLOAT64);
|
|
106
|
+
return this.f64(n);
|
|
107
|
+
}
|
|
108
|
+
if (type === "string") {
|
|
109
|
+
return this.writeString(value);
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(value)) {
|
|
112
|
+
return this.writeArray(value);
|
|
113
|
+
}
|
|
114
|
+
if (value instanceof Uint8Array) {
|
|
115
|
+
return this.writeBytes(value);
|
|
116
|
+
}
|
|
117
|
+
if (type === "object") {
|
|
118
|
+
return this.writeObject(value);
|
|
119
|
+
}
|
|
120
|
+
throw new Error(`HC_BINARY_CODEC: unsupported value type: ${type}`);
|
|
121
|
+
}
|
|
122
|
+
writeString(value) {
|
|
123
|
+
const encoded = new TextEncoder().encode(value);
|
|
124
|
+
this.u8(TAG.STRING);
|
|
125
|
+
this.i32(encoded.length);
|
|
126
|
+
this.need(encoded.length);
|
|
127
|
+
this.bytes.set(encoded, this.tail);
|
|
128
|
+
this.tail += encoded.length;
|
|
129
|
+
}
|
|
130
|
+
writeArray(value) {
|
|
131
|
+
const ref = this.beginRef(value);
|
|
132
|
+
this.u8(TAG.ARRAY);
|
|
133
|
+
this.i32(value.length);
|
|
134
|
+
for (let i = 0; i < value.length; i++) {
|
|
135
|
+
this.writeValue(value[i]);
|
|
136
|
+
}
|
|
137
|
+
this.endRef(ref);
|
|
138
|
+
}
|
|
139
|
+
writeBytes(value) {
|
|
140
|
+
this.u8(TAG.UINT8ARRAY);
|
|
141
|
+
this.i32(value.length);
|
|
142
|
+
this.need(value.length);
|
|
143
|
+
this.bytes.set(value, this.tail);
|
|
144
|
+
this.tail += value.length;
|
|
145
|
+
}
|
|
146
|
+
writeObject(value) {
|
|
147
|
+
const ref = this.beginRef(value);
|
|
148
|
+
const keys = Object.keys(value);
|
|
149
|
+
this.u8(TAG.OBJECT);
|
|
150
|
+
this.i32(keys.length);
|
|
151
|
+
for (const key of keys) {
|
|
152
|
+
this.writeString(key);
|
|
153
|
+
this.writeValue(value[key]);
|
|
154
|
+
}
|
|
155
|
+
this.endRef(ref);
|
|
156
|
+
}
|
|
157
|
+
beginRef(value) {
|
|
158
|
+
const existing = this.refs.get(value);
|
|
159
|
+
if (existing !== undefined) {
|
|
160
|
+
this.u8(TAG.REF);
|
|
161
|
+
this.i32(existing);
|
|
162
|
+
return -1;
|
|
163
|
+
}
|
|
164
|
+
const ref = this.nextRef++;
|
|
165
|
+
this.refs.set(value, ref);
|
|
166
|
+
return ref;
|
|
167
|
+
}
|
|
168
|
+
endRef(ref) {
|
|
169
|
+
// Reserved for future back-reference patching.
|
|
170
|
+
void ref;
|
|
171
|
+
}
|
|
172
|
+
readU8(cursor) {
|
|
173
|
+
return this.bytes[cursor.at++];
|
|
174
|
+
}
|
|
175
|
+
readI32(cursor) {
|
|
176
|
+
const value = this.view.getInt32(cursor.at, true);
|
|
177
|
+
cursor.at += 4;
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
readF64(cursor) {
|
|
181
|
+
const value = this.view.getFloat64(cursor.at, true);
|
|
182
|
+
cursor.at += 8;
|
|
183
|
+
return value;
|
|
184
|
+
}
|
|
185
|
+
readValue(cursor) {
|
|
186
|
+
const tag = this.readU8(cursor);
|
|
187
|
+
switch (tag) {
|
|
188
|
+
case TAG.UNDEFINED:
|
|
189
|
+
return undefined;
|
|
190
|
+
case TAG.NULL:
|
|
191
|
+
return null;
|
|
192
|
+
case TAG.FALSE:
|
|
193
|
+
return false;
|
|
194
|
+
case TAG.TRUE:
|
|
195
|
+
return true;
|
|
196
|
+
case TAG.INT32:
|
|
197
|
+
return this.readI32(cursor);
|
|
198
|
+
case TAG.FLOAT64:
|
|
199
|
+
return this.readF64(cursor);
|
|
200
|
+
case TAG.STRING:
|
|
201
|
+
return this.readString(cursor);
|
|
202
|
+
case TAG.ARRAY:
|
|
203
|
+
return this.readArray(cursor);
|
|
204
|
+
case TAG.OBJECT:
|
|
205
|
+
return this.readObject(cursor);
|
|
206
|
+
case TAG.UINT8ARRAY:
|
|
207
|
+
return this.readBytes(cursor);
|
|
208
|
+
default:
|
|
209
|
+
throw new Error(`HC_BINARY_CODEC: unknown tag ${tag}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
readString(cursor) {
|
|
213
|
+
const length = this.readI32(cursor);
|
|
214
|
+
const bytes = this.bytes.subarray(cursor.at, cursor.at + length);
|
|
215
|
+
cursor.at += length;
|
|
216
|
+
return new TextDecoder().decode(bytes);
|
|
217
|
+
}
|
|
218
|
+
readArray(cursor) {
|
|
219
|
+
const length = this.readI32(cursor);
|
|
220
|
+
const out = new Array(length);
|
|
221
|
+
for (let i = 0; i < length; i++) {
|
|
222
|
+
out[i] = this.readValue(cursor);
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
readObject(cursor) {
|
|
227
|
+
const length = this.readI32(cursor);
|
|
228
|
+
const out = {};
|
|
229
|
+
for (let i = 0; i < length; i++) {
|
|
230
|
+
const key = this.readString(cursor);
|
|
231
|
+
out[key] = this.readValue(cursor);
|
|
232
|
+
}
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
readBytes(cursor) {
|
|
236
|
+
const length = this.readI32(cursor);
|
|
237
|
+
const out = this.bytes.slice(cursor.at, cursor.at + length);
|
|
238
|
+
cursor.at += length;
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
/* =============================================================================
|
|
242
|
+
BINARY ENC, WITH JSON ABOVE
|
|
243
|
+
================================= ⇦✦──⇦✦──✦──✦──←'pew pew' 🧙 [BINARY_CODEC] */
|
|
244
|
+
ensureAvailable(buf, pos, length) {
|
|
245
|
+
if (pos < 0 || length < 0 || pos + length > buf.length)
|
|
246
|
+
throw new Error("CBOR: truncated buffer");
|
|
247
|
+
}
|
|
248
|
+
readUint(buf, pos) {
|
|
249
|
+
this.ensureAvailable(buf, pos, 1);
|
|
250
|
+
const info = buf[pos] & 0x1f;
|
|
251
|
+
if (info <= 23)
|
|
252
|
+
return [info, pos + 1];
|
|
253
|
+
if (info === 24) {
|
|
254
|
+
this.ensureAvailable(buf, pos + 1, 1);
|
|
255
|
+
return [buf[pos + 1], pos + 2];
|
|
256
|
+
}
|
|
257
|
+
if (info === 25) {
|
|
258
|
+
this.ensureAvailable(buf, pos + 1, 2);
|
|
259
|
+
return [new DataView(buf.buffer, buf.byteOffset + pos + 1, 2).getUint16(0, false), pos + 3];
|
|
260
|
+
}
|
|
261
|
+
if (info === 26) {
|
|
262
|
+
this.ensureAvailable(buf, pos + 1, 4);
|
|
263
|
+
return [new DataView(buf.buffer, buf.byteOffset + pos + 1, 4).getUint32(0, false), pos + 5];
|
|
264
|
+
}
|
|
265
|
+
if (info === 27) {
|
|
266
|
+
this.ensureAvailable(buf, pos + 1, 8);
|
|
267
|
+
const value = new DataView(buf.buffer, buf.byteOffset + pos + 1, 8).getBigUint64(0, false);
|
|
268
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER))
|
|
269
|
+
throw new Error("CBOR: integer exceeds Number.MAX_SAFE_INTEGER");
|
|
270
|
+
return [Number(value), pos + 9];
|
|
271
|
+
}
|
|
272
|
+
throw new Error(`CBOR: unsupported additional info ${info}`);
|
|
273
|
+
}
|
|
274
|
+
encodeUint64(majorShifted, n) {
|
|
275
|
+
if (n <= 23n)
|
|
276
|
+
return new Uint8Array([majorShifted | Number(n)]);
|
|
277
|
+
if (n <= 0xffn) {
|
|
278
|
+
return new Uint8Array([majorShifted | 24, Number(n)]);
|
|
279
|
+
}
|
|
280
|
+
if (n <= 0xffffn) {
|
|
281
|
+
const b = new Uint8Array(3);
|
|
282
|
+
b[0] = majorShifted | 25;
|
|
283
|
+
new DataView(b.buffer).setUint16(1, Number(n), false);
|
|
284
|
+
return b;
|
|
285
|
+
}
|
|
286
|
+
if (n <= 0xffffffffn) {
|
|
287
|
+
const b = new Uint8Array(5);
|
|
288
|
+
b[0] = majorShifted | 26;
|
|
289
|
+
new DataView(b.buffer).setUint32(1, Number(n), false);
|
|
290
|
+
return b;
|
|
291
|
+
}
|
|
292
|
+
const b = new Uint8Array(9);
|
|
293
|
+
b[0] = majorShifted | 27;
|
|
294
|
+
new DataView(b.buffer).setBigUint64(1, n, false);
|
|
295
|
+
return b;
|
|
296
|
+
}
|
|
297
|
+
encode(value) {
|
|
298
|
+
const parts = [];
|
|
299
|
+
this.encodeValue(value, parts);
|
|
300
|
+
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
301
|
+
const out = new Uint8Array(total);
|
|
302
|
+
let offset = 0;
|
|
303
|
+
for (const p of parts) {
|
|
304
|
+
out.set(p, offset);
|
|
305
|
+
offset += p.length;
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
encodeValue(v, out) {
|
|
310
|
+
if (v === null) {
|
|
311
|
+
out.push(new Uint8Array([0xf6]));
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (v === undefined) {
|
|
315
|
+
out.push(new Uint8Array([0xf7]));
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (v === true) {
|
|
319
|
+
out.push(new Uint8Array([0xf5]));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (v === false) {
|
|
323
|
+
out.push(new Uint8Array([0xf4]));
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (typeof v === "number") {
|
|
327
|
+
if (Number.isSafeInteger(v)) {
|
|
328
|
+
if (v >= 0)
|
|
329
|
+
out.push(this.encodeUint64(0x00, BigInt(v)));
|
|
330
|
+
else
|
|
331
|
+
out.push(this.encodeUint64(0x20, BigInt(-1 - v)));
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
const b = new Uint8Array(9);
|
|
335
|
+
b[0] = 0xfb;
|
|
336
|
+
new DataView(b.buffer).setFloat64(1, v, false);
|
|
337
|
+
out.push(b);
|
|
338
|
+
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (typeof v === "string") {
|
|
342
|
+
const encoded = encoder.encode(v);
|
|
343
|
+
out.push(this.encodeUint(0x60, encoded.length));
|
|
344
|
+
out.push(encoded);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (v instanceof Uint8Array) {
|
|
348
|
+
out.push(this.encodeUint(0x40, v.length));
|
|
349
|
+
out.push(v);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (Array.isArray(v)) {
|
|
353
|
+
out.push(this.encodeUint(0x80, v.length));
|
|
354
|
+
for (const item of v) {
|
|
355
|
+
this.encodeValue(item, out);
|
|
356
|
+
}
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (typeof v === "object") {
|
|
360
|
+
const proto = Object.getPrototypeOf(v);
|
|
361
|
+
if (proto !== Object.prototype && proto !== null)
|
|
362
|
+
throw new Error(`CBOR: unsupported object type ${v.constructor?.name ?? "unknown"}`);
|
|
363
|
+
const keys = Object.keys(v);
|
|
364
|
+
out.push(this.encodeUint(0xa0, keys.length));
|
|
365
|
+
for (const key of keys) {
|
|
366
|
+
this.encodeValue(key, out);
|
|
367
|
+
this.encodeValue(v[key], out);
|
|
368
|
+
}
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
throw new Error(`CBOR: unsupported value type ${typeof v}`);
|
|
372
|
+
}
|
|
373
|
+
encodeUint(majorShifted, n) {
|
|
374
|
+
if (n <= 23)
|
|
375
|
+
return new Uint8Array([majorShifted | n]);
|
|
376
|
+
if (n <= 0xff)
|
|
377
|
+
return new Uint8Array([majorShifted | 24, n]);
|
|
378
|
+
if (n <= 0xffff) {
|
|
379
|
+
const b = new Uint8Array(3);
|
|
380
|
+
b[0] = majorShifted | 25;
|
|
381
|
+
new DataView(b.buffer).setUint16(1, n, false);
|
|
382
|
+
return b;
|
|
383
|
+
}
|
|
384
|
+
const b = new Uint8Array(5);
|
|
385
|
+
b[0] = majorShifted | 26;
|
|
386
|
+
new DataView(b.buffer).setUint32(1, n, false);
|
|
387
|
+
return b;
|
|
388
|
+
}
|
|
389
|
+
decode(buf) {
|
|
390
|
+
const [value, end] = this.decodeValue(buf, 0);
|
|
391
|
+
if (end !== buf.length) {
|
|
392
|
+
throw new Error("CBOR: trailing bytes");
|
|
393
|
+
}
|
|
394
|
+
return value;
|
|
395
|
+
}
|
|
396
|
+
decodeValue(buf, pos) {
|
|
397
|
+
this.ensureAvailable(buf, pos, 1);
|
|
398
|
+
const first = buf[pos];
|
|
399
|
+
const major = first >> 5;
|
|
400
|
+
const info = first & 0x1f;
|
|
401
|
+
if (major === 7) {
|
|
402
|
+
if (info === 20)
|
|
403
|
+
return [false, pos + 1];
|
|
404
|
+
if (info === 21)
|
|
405
|
+
return [true, pos + 1];
|
|
406
|
+
if (info === 22)
|
|
407
|
+
return [null, pos + 1];
|
|
408
|
+
if (info === 23)
|
|
409
|
+
return [undefined, pos + 1];
|
|
410
|
+
if (info === 27) {
|
|
411
|
+
this.ensureAvailable(buf, pos + 1, 8);
|
|
412
|
+
const dv = new DataView(buf.buffer, buf.byteOffset + pos + 1, 8);
|
|
413
|
+
return [dv.getFloat64(0, false), pos + 9];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
const [n, nextPos] = this.readUint(buf, pos);
|
|
417
|
+
if (major === 0)
|
|
418
|
+
return [n, nextPos];
|
|
419
|
+
if (major === 1)
|
|
420
|
+
return [-1 - n, nextPos];
|
|
421
|
+
if (major === 2) {
|
|
422
|
+
this.ensureAvailable(buf, nextPos, n);
|
|
423
|
+
const bytes = buf.slice(nextPos, nextPos + n);
|
|
424
|
+
return [bytes, nextPos + n];
|
|
425
|
+
}
|
|
426
|
+
if (major === 3) {
|
|
427
|
+
this.ensureAvailable(buf, nextPos, n);
|
|
428
|
+
const bytes = buf.slice(nextPos, nextPos + n);
|
|
429
|
+
return [decoder.decode(bytes), nextPos + n];
|
|
430
|
+
}
|
|
431
|
+
if (major === 4) {
|
|
432
|
+
const arr = [];
|
|
433
|
+
let p = nextPos;
|
|
434
|
+
for (let i = 0; i < n; i++) {
|
|
435
|
+
const [item, np] = this.decodeValue(buf, p);
|
|
436
|
+
arr.push(item);
|
|
437
|
+
p = np;
|
|
438
|
+
}
|
|
439
|
+
return [arr, p];
|
|
440
|
+
}
|
|
441
|
+
if (major === 5) {
|
|
442
|
+
const obj = {};
|
|
443
|
+
let p = nextPos;
|
|
444
|
+
for (let i = 0; i < n; i++) {
|
|
445
|
+
const [k, kp] = this.decodeValue(buf, p);
|
|
446
|
+
const [v, vp] = this.decodeValue(buf, kp);
|
|
447
|
+
obj[k] = v;
|
|
448
|
+
p = vp;
|
|
449
|
+
}
|
|
450
|
+
return [obj, p];
|
|
451
|
+
}
|
|
452
|
+
throw new Error(`CBOR: unsupported major type ${major}`);
|
|
453
|
+
}
|
|
454
|
+
encode_msg(msg) {
|
|
455
|
+
// Instead of leaking a raw object, encode an explicit fallback/error frame
|
|
456
|
+
if (msg instanceof Uint8Array)
|
|
457
|
+
return msg; // If it's already an encoded Uint8Array, don't re-encode it
|
|
458
|
+
try {
|
|
459
|
+
return this.encode(msg);
|
|
460
|
+
}
|
|
461
|
+
catch (err) {
|
|
462
|
+
console.log("[BINARY_PIPE] Failed to encode outbound packet strictly:", err);
|
|
463
|
+
return this.encode({ type: "error", error: "Encoding failed", id: msg?.id || "unknown" });
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
decode_msg(raw) {
|
|
467
|
+
// Guard: If it didn't arrive as binary, it's a protocol violation for this core layer
|
|
468
|
+
if (!(raw instanceof Uint8Array) && !(typeof Buffer !== "undefined" && Buffer.isBuffer(raw))) {
|
|
469
|
+
console.error("[BINARY_PIPE] Protocol violation: Expected encoded Uint8Array data stream.");
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
const uint8 = raw instanceof Uint8Array ? raw : new Uint8Array(raw.buffer, raw.byteOffset, raw.length);
|
|
474
|
+
return this.decode(uint8);
|
|
475
|
+
}
|
|
476
|
+
catch (err) {
|
|
477
|
+
console.error("[BINARY_PIPE] Failed to decode inbound packet:", err);
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/*=============================================================================
|
|
482
|
+
BINARY FRAME SERIOALIZER, HEADER VAILDATION, CALCULATE FRAME FOOT PRINT
|
|
483
|
+
================================= ⇦✦──⇦✦──✦──✦──←'pew pew' 🧙 [BINARY_CODEC] */
|
|
484
|
+
HEADER_SIZE_BYTES = 64; // Int32 Array Indices (4 bytes per element)
|
|
485
|
+
HEADER_IDX_WRITE_HEAD = 0; // Byte offset 0
|
|
486
|
+
HEADER_IDX_READ_HEAD = 1; // Byte offset 4
|
|
487
|
+
HEADER_IDX_MUTEX = 2; // Byte offset 8
|
|
488
|
+
HEADER_IDX_CAPACITY = 3; // Byte offset 12
|
|
489
|
+
MUTEX_UNLOCKED = 0; // Mutex States
|
|
490
|
+
MUTEX_LOCKED = 1;
|
|
491
|
+
SENTINEL_WRAP_MARKER = 0xffffffff; // 0xFFFFFFFF (Uint32 max) written to length prefix slot when p wraps to offset 0
|
|
492
|
+
static topicToIdMap = new Map();
|
|
493
|
+
static idToTopicMap = new Map();
|
|
494
|
+
static headerBuffer = new ArrayBuffer(4); // Reusable scratch buffer & DataView for header manipulations (Zero GC)
|
|
495
|
+
static headerView = new DataView(BINARY_CODEC.headerBuffer);
|
|
496
|
+
static headerBytes = new Uint8Array(BINARY_CODEC.headerBuffer);
|
|
497
|
+
FLYWEIGHT_FRAME() {
|
|
498
|
+
return {
|
|
499
|
+
topicId: 0,
|
|
500
|
+
topicName: undefined,
|
|
501
|
+
p: new Uint8Array(0),
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
static hashTopic(topic) {
|
|
505
|
+
let hash = 0x811c9dc5;
|
|
506
|
+
for (let i = 0; i < topic.length; i++) {
|
|
507
|
+
hash ^= topic.charCodeAt(i);
|
|
508
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
509
|
+
}
|
|
510
|
+
return hash;
|
|
511
|
+
}
|
|
512
|
+
static getTopicId(topic) {
|
|
513
|
+
const existingId = this.topicToIdMap.get(topic);
|
|
514
|
+
if (existingId !== undefined) {
|
|
515
|
+
return existingId;
|
|
516
|
+
}
|
|
517
|
+
const topicId = this.hashTopic(topic);
|
|
518
|
+
const collisionTopic = this.idToTopicMap.get(topicId);
|
|
519
|
+
if (collisionTopic !== undefined && collisionTopic !== topic) {
|
|
520
|
+
throw new Error(`Topic hash collision detected between "${topic}" and "${collisionTopic}" (ID: ${topicId})`);
|
|
521
|
+
}
|
|
522
|
+
this.topicToIdMap.set(topic, topicId);
|
|
523
|
+
this.idToTopicMap.set(topicId, topic);
|
|
524
|
+
return topicId;
|
|
525
|
+
}
|
|
526
|
+
static getTopicName(topicId) {
|
|
527
|
+
return this.idToTopicMap.get(topicId);
|
|
528
|
+
}
|
|
529
|
+
static registerTopics(topics) {
|
|
530
|
+
for (const topic of topics) {
|
|
531
|
+
this.getTopicId(topic);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
static resetRegistry() {
|
|
535
|
+
this.topicToIdMap.clear();
|
|
536
|
+
this.idToTopicMap.clear();
|
|
537
|
+
}
|
|
538
|
+
static encodeFrame(topic, p) {
|
|
539
|
+
const topicId = BINARY_CODEC.getTopicId(topic);
|
|
540
|
+
let payloadBytes;
|
|
541
|
+
if (p instanceof Uint8Array) {
|
|
542
|
+
payloadBytes = p;
|
|
543
|
+
}
|
|
544
|
+
else if (typeof p === "string") {
|
|
545
|
+
payloadBytes = encoder.encode(p);
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
payloadBytes = encoder.encode(JSON.stringify(p));
|
|
549
|
+
}
|
|
550
|
+
const frame = new Uint8Array(4 + payloadBytes.byteLength);
|
|
551
|
+
// 1. Write Uint32 Topic ID (Little-Endian) via byte copies (Zero DataView allocation)
|
|
552
|
+
this.headerView.setUint32(0, topicId, true);
|
|
553
|
+
frame.set(this.headerBytes, 0);
|
|
554
|
+
// 2. Copy Payload Bytes
|
|
555
|
+
frame.set(payloadBytes, 4);
|
|
556
|
+
return frame;
|
|
557
|
+
}
|
|
558
|
+
decodeFrame(frameBytes) {
|
|
559
|
+
if (frameBytes.byteLength < 4) {
|
|
560
|
+
throw new Error("Invalid frame: buffer smaller than minimum header size (4 bytes).");
|
|
561
|
+
}
|
|
562
|
+
// Direct byte copy into static DataView to read Uint32 safely without memory allocation
|
|
563
|
+
BINARY_CODEC.headerBytes[0] = frameBytes[0];
|
|
564
|
+
BINARY_CODEC.headerBytes[1] = frameBytes[1];
|
|
565
|
+
BINARY_CODEC.headerBytes[2] = frameBytes[2];
|
|
566
|
+
BINARY_CODEC.headerBytes[3] = frameBytes[3];
|
|
567
|
+
const topicId = BINARY_CODEC.headerView.getUint32(0, true);
|
|
568
|
+
this.FLYWEIGHT_FRAME().topicId = topicId;
|
|
569
|
+
this.FLYWEIGHT_FRAME().topicName = BINARY_CODEC.getTopicName(topicId);
|
|
570
|
+
this.FLYWEIGHT_FRAME().p = frameBytes.subarray(4); // Zero-copy view
|
|
571
|
+
return this.FLYWEIGHT_FRAME();
|
|
572
|
+
}
|
|
573
|
+
createSharedDepotBuffer(capacityBytes) {
|
|
574
|
+
// Enforce 64-byte alignment on capacity
|
|
575
|
+
const alignedCapacity = Math.ceil(capacityBytes / 64) * 64;
|
|
576
|
+
const totalSizeBytes = this.HEADER_SIZE_BYTES + alignedCapacity;
|
|
577
|
+
const sab = new SharedArrayBuffer(totalSizeBytes);
|
|
578
|
+
const headerView = new Int32Array(sab, 0, 4);
|
|
579
|
+
// Initialize Header State
|
|
580
|
+
Atomics.store(headerView, this.HEADER_IDX_WRITE_HEAD, 0);
|
|
581
|
+
Atomics.store(headerView, this.HEADER_IDX_READ_HEAD, 0);
|
|
582
|
+
Atomics.store(headerView, this.HEADER_IDX_MUTEX, this.MUTEX_UNLOCKED);
|
|
583
|
+
Atomics.store(headerView, this.HEADER_IDX_CAPACITY, alignedCapacity);
|
|
584
|
+
return sab;
|
|
585
|
+
}
|
|
586
|
+
mapDepotBuffer(sab) {
|
|
587
|
+
// Map control slots (first 16 bytes containing 4 x Int32 values)
|
|
588
|
+
const headerInt32 = new Int32Array(sab, 0, 4);
|
|
589
|
+
// Read capacity directly from header slot
|
|
590
|
+
const capacity = Atomics.load(headerInt32, this.HEADER_IDX_CAPACITY);
|
|
591
|
+
// Map p slice strictly starting at byte 64
|
|
592
|
+
const payloadUint8 = new Uint8Array(sab, this.HEADER_SIZE_BYTES, capacity);
|
|
593
|
+
return {
|
|
594
|
+
headerInt32,
|
|
595
|
+
payloadUint8,
|
|
596
|
+
capacity,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
validateHeader(sab) {
|
|
600
|
+
if (sab.byteLength < this.HEADER_SIZE_BYTES)
|
|
601
|
+
return false;
|
|
602
|
+
const headerInt32 = new Int32Array(sab, 0, 4);
|
|
603
|
+
const capacity = Atomics.load(headerInt32, this.HEADER_IDX_CAPACITY);
|
|
604
|
+
// Buffer length must equal 64 bytes header + registered capacity
|
|
605
|
+
return sab.byteLength === this.HEADER_SIZE_BYTES + capacity;
|
|
606
|
+
}
|
|
607
|
+
calculateFrameFootprint(payloadSizeBytes) {
|
|
608
|
+
const rawEnvelopeBytes = 4 + payloadSizeBytes;
|
|
609
|
+
return Math.ceil(rawEnvelopeBytes / 4) * 4;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class BYTE_ID {
|
|
2
|
+
/* ==========================================================================================================================
|
|
3
|
+
provides lightweight numeric and string ID generation, using a Uint32Array counter for incrementing byte-level IDs and
|
|
4
|
+
a separate counter for string IDs. It also preallocates fixed-size timestamp and resolver arrays to
|
|
5
|
+
track up to 4,096 pending operations.
|
|
6
|
+
|
|
7
|
+
The update byteid function now shares the counter with sid, the string variant. At the smae time, a rollover counter that
|
|
8
|
+
increments whenever the 32-bit counter wraps. Keeping the fact U32Array counter while producing ids that remain unique.
|
|
9
|
+
================================================================================ ⇦✦──⇦✦──✦──✦──← 'pew pew' 🧙 [BYTE_ID] */
|
|
10
|
+
MAX_PENDING = 4096;
|
|
11
|
+
sentTimestamps;
|
|
12
|
+
pendingResolvers;
|
|
13
|
+
cursor = 0;
|
|
14
|
+
counter;
|
|
15
|
+
epoch = 0;
|
|
16
|
+
_byteid() {
|
|
17
|
+
const g = globalThis;
|
|
18
|
+
const c = g._sauronIdCounter ?? (g._sauronIdCounter = new Uint32Array(1));
|
|
19
|
+
if (++c[0] === 0)
|
|
20
|
+
this.epoch++;
|
|
21
|
+
g._sauronIdCounter = c;
|
|
22
|
+
return `${this.epoch}:${c[0]}`;
|
|
23
|
+
}
|
|
24
|
+
b() {
|
|
25
|
+
return this._byteid();
|
|
26
|
+
}
|
|
27
|
+
constructor() {
|
|
28
|
+
this.counter = new Uint32Array(1);
|
|
29
|
+
this.sentTimestamps = new Float64Array(this.MAX_PENDING);
|
|
30
|
+
this.pendingResolvers = new Array(this.MAX_PENDING);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function _sid() {
|
|
34
|
+
const g = globalThis;
|
|
35
|
+
const t = String((g._sauronIdCounter ?? 0) + 1);
|
|
36
|
+
g._sauronIdCounter = t;
|
|
37
|
+
return t;
|
|
38
|
+
}
|
|
39
|
+
// public _stringIdCounter = 0;
|
|
40
|
+
// const id = this.counter[0] + 1;
|