@the-w1zard/singularity 1.0.12 → 1.0.14
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 +450 -0
- package/README.md +327 -2
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts +139 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts.map +1 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.js +502 -0
- package/dist/class.d.ts +161 -0
- package/dist/class.d.ts.map +1 -0
- package/dist/class.js +804 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts +47 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts +175 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.js +482 -0
- package/dist/comps/binary.d.ts +71 -0
- package/dist/comps/binary.d.ts.map +1 -0
- package/dist/comps/byte_id.d.ts +12 -0
- package/dist/comps/byte_id.d.ts.map +1 -0
- package/dist/comps/cell-worker.d.ts +9 -0
- package/dist/comps/cell-worker.d.ts.map +1 -0
- package/dist/comps/cell.d.ts +349 -0
- package/dist/comps/cell.d.ts.map +1 -0
- package/dist/comps/create_live_self.d.ts +22 -0
- package/dist/comps/create_live_self.d.ts.map +1 -0
- package/dist/comps/queue.d.ts +103 -0
- package/dist/comps/queue.d.ts.map +1 -0
- package/dist/comps/serializer.d.ts +30 -0
- package/dist/comps/serializer.d.ts.map +1 -0
- package/dist/comps/serializer.js +131 -0
- package/dist/comps/vm-types.d.ts +20 -0
- package/dist/comps/vm-types.d.ts.map +1 -0
- package/dist/db/binary.d.ts +76 -0
- package/dist/db/binary.d.ts.map +1 -0
- package/dist/db/binary.js +441 -0
- package/dist/db/client.d.ts +28 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +82 -0
- package/dist/db/db.d.ts +141 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.js +898 -0
- package/dist/db/op.d.ts +13 -0
- package/dist/db/op.d.ts.map +1 -0
- package/dist/db/op.js +11 -0
- package/dist/db/storage-worker.d.ts +2 -0
- package/dist/db/storage-worker.d.ts.map +1 -0
- package/dist/db/storage-worker.js +152 -0
- package/dist/db/types.d.ts +134 -0
- package/dist/db/types.d.ts.map +1 -0
- package/dist/db/types.js +14 -0
- package/dist/db/wal.d.ts +30 -0
- package/dist/db/wal.d.ts.map +1 -0
- package/dist/db/wal.js +217 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/signals.js +10740 -5498
- package/dist/slab.d.ts +95 -0
- package/dist/slab.d.ts.map +1 -0
- package/dist/slab.js +575 -0
- package/dist/slab.types.d.ts +5 -0
- package/dist/slab.types.d.ts.map +1 -0
- package/dist/slab.types.js +1 -0
- package/dist/test.d.ts +152 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +1183 -0
- package/dist/testt.d.ts +61 -0
- package/dist/testt.d.ts.map +1 -0
- package/dist/testt.js +143 -0
- package/dist/y.d.ts +152 -0
- package/dist/y.d.ts.map +1 -0
- package/dist/y.js +36 -65
- package/package.json +22 -19
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// SAME NODE, DIFF APP TRANSPORT LAYER - BASED OFF OF THE POWER OF 2 DISRUPTOR
|
|
3
|
+
// REPLACING THE BASIC RING BUFFER THAT WAS HERE BEFORE IT.
|
|
4
|
+
// ============================================================================
|
|
5
|
+
const FLYWEIGHT_FRAME = {
|
|
6
|
+
topicId: 0,
|
|
7
|
+
topicName: undefined,
|
|
8
|
+
p: new Uint8Array(0)
|
|
9
|
+
};
|
|
10
|
+
export class BinaryEncoder {
|
|
11
|
+
static topicToId = Object.create(null);
|
|
12
|
+
static idToTopic = Object.create(null);
|
|
13
|
+
static hashTopic(topic) {
|
|
14
|
+
let hash = 0x811c9dc5;
|
|
15
|
+
for (let i = 0; i < topic.length; i++) {
|
|
16
|
+
hash ^= topic.charCodeAt(i);
|
|
17
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
18
|
+
}
|
|
19
|
+
return hash;
|
|
20
|
+
}
|
|
21
|
+
static getTopicId(topic) {
|
|
22
|
+
const existingId = this.topicToId[topic];
|
|
23
|
+
if (existingId !== undefined)
|
|
24
|
+
return existingId;
|
|
25
|
+
const topicId = this.hashTopic(topic);
|
|
26
|
+
const collisionTopic = this.idToTopic[topicId];
|
|
27
|
+
if (collisionTopic !== undefined && collisionTopic !== topic) {
|
|
28
|
+
throw new Error(`Topic hash collision between "${topic}" and "${collisionTopic}" (ID: ${topicId})`);
|
|
29
|
+
}
|
|
30
|
+
this.topicToId[topic] = topicId;
|
|
31
|
+
this.idToTopic[topicId] = topic;
|
|
32
|
+
return topicId;
|
|
33
|
+
}
|
|
34
|
+
static getTopicName(topicId) {
|
|
35
|
+
return this.idToTopic[topicId];
|
|
36
|
+
}
|
|
37
|
+
static registerTopics(topics) {
|
|
38
|
+
for (const topic of topics)
|
|
39
|
+
this.getTopicId(topic);
|
|
40
|
+
}
|
|
41
|
+
static resetRegistry() {
|
|
42
|
+
this.topicToId = Object.create(null);
|
|
43
|
+
this.idToTopic = Object.create(null);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export class BinaryFrameSerializer {
|
|
47
|
+
static textEncoder = new TextEncoder(); // Reusable scratch buffer & DataView for header manipulations (Zero GC)
|
|
48
|
+
static headerBuffer = new ArrayBuffer(4);
|
|
49
|
+
static headerView = new DataView(BinaryFrameSerializer.headerBuffer);
|
|
50
|
+
static headerBytes = new Uint8Array(BinaryFrameSerializer.headerBuffer);
|
|
51
|
+
static encodeFrame(topic, p) {
|
|
52
|
+
const topicId = BinaryEncoder.getTopicId(topic);
|
|
53
|
+
let payloadBytes;
|
|
54
|
+
if (p instanceof Uint8Array) {
|
|
55
|
+
payloadBytes = p;
|
|
56
|
+
}
|
|
57
|
+
else if (typeof p === "string") {
|
|
58
|
+
payloadBytes = this.textEncoder.encode(p);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
payloadBytes = this.textEncoder.encode(JSON.stringify(p));
|
|
62
|
+
}
|
|
63
|
+
const frame = new Uint8Array(4 + payloadBytes.byteLength);
|
|
64
|
+
// 1. Write Uint32 Topic ID (Little-Endian) via byte copies (Zero DataView allocation)
|
|
65
|
+
this.headerView.setUint32(0, topicId, true);
|
|
66
|
+
frame.set(this.headerBytes, 0);
|
|
67
|
+
// 2. Copy Payload Bytes
|
|
68
|
+
frame.set(payloadBytes, 4);
|
|
69
|
+
return frame;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Zero-GC binary frame decode.
|
|
73
|
+
* Mutates and returns a shared FLYWEIGHT_FRAME object.
|
|
74
|
+
*/
|
|
75
|
+
static decodeFrame(frameBytes) {
|
|
76
|
+
if (frameBytes.byteLength < 4) {
|
|
77
|
+
throw new Error("Invalid frame: buffer smaller than minimum header size (4 bytes).");
|
|
78
|
+
}
|
|
79
|
+
// Direct byte copy into static DataView to read Uint32 safely without memory allocation
|
|
80
|
+
this.headerBytes[0] = frameBytes[0];
|
|
81
|
+
this.headerBytes[1] = frameBytes[1];
|
|
82
|
+
this.headerBytes[2] = frameBytes[2];
|
|
83
|
+
this.headerBytes[3] = frameBytes[3];
|
|
84
|
+
const topicId = this.headerView.getUint32(0, true);
|
|
85
|
+
FLYWEIGHT_FRAME.topicId = topicId;
|
|
86
|
+
FLYWEIGHT_FRAME.topicName = BinaryEncoder.getTopicName(topicId);
|
|
87
|
+
FLYWEIGHT_FRAME.p = frameBytes.subarray(4); // Zero-copy view
|
|
88
|
+
return FLYWEIGHT_FRAME;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export const HEADER_SIZE_BYTES = 64;
|
|
92
|
+
export const HEADER_IDX_WRITE_HEAD = 0;
|
|
93
|
+
export const HEADER_IDX_READ_HEAD = 1;
|
|
94
|
+
export const HEADER_IDX_MUTEX = 2;
|
|
95
|
+
export const HEADER_IDX_CAPACITY = 3;
|
|
96
|
+
export const MUTEX_UNLOCKED = 0;
|
|
97
|
+
export const MUTEX_LOCKED = 1;
|
|
98
|
+
export const SENTINEL_WRAP_MARKER = 0xFFFFFFFF;
|
|
99
|
+
export function createSharedDepotBuffer(capacityBytes) {
|
|
100
|
+
const alignedCapacity = Math.ceil(capacityBytes / 64) * 64;
|
|
101
|
+
const totalSizeBytes = HEADER_SIZE_BYTES + alignedCapacity;
|
|
102
|
+
const sab = new SharedArrayBuffer(totalSizeBytes);
|
|
103
|
+
const headerView = new Int32Array(sab, 0, 4);
|
|
104
|
+
Atomics.store(headerView, HEADER_IDX_WRITE_HEAD, 0);
|
|
105
|
+
Atomics.store(headerView, HEADER_IDX_READ_HEAD, 0);
|
|
106
|
+
Atomics.store(headerView, HEADER_IDX_MUTEX, MUTEX_UNLOCKED);
|
|
107
|
+
Atomics.store(headerView, HEADER_IDX_CAPACITY, alignedCapacity);
|
|
108
|
+
return sab;
|
|
109
|
+
}
|
|
110
|
+
export function mapDepotBuffer(sab) {
|
|
111
|
+
const headerInt32 = new Int32Array(sab, 0, 4);
|
|
112
|
+
const capacity = Atomics.load(headerInt32, HEADER_IDX_CAPACITY);
|
|
113
|
+
const payloadUint8 = new Uint8Array(sab, HEADER_SIZE_BYTES, capacity);
|
|
114
|
+
return { headerInt32, payloadUint8, capacity };
|
|
115
|
+
}
|
|
116
|
+
export function validateHeader(sab) {
|
|
117
|
+
if (sab.byteLength < HEADER_SIZE_BYTES)
|
|
118
|
+
return false;
|
|
119
|
+
const headerInt32 = new Int32Array(sab, 0, 4);
|
|
120
|
+
const capacity = Atomics.load(headerInt32, HEADER_IDX_CAPACITY);
|
|
121
|
+
return sab.byteLength === HEADER_SIZE_BYTES + capacity;
|
|
122
|
+
}
|
|
123
|
+
export function calculateFrameFootprint(payloadSizeBytes) {
|
|
124
|
+
return Math.ceil((4 + payloadSizeBytes) / 4) * 4;
|
|
125
|
+
} // ---- RING_BUFFER — your existing overflow-lane class, unchanged ----
|
|
126
|
+
export class RING_BUFFER {
|
|
127
|
+
headerInt32;
|
|
128
|
+
payloadUint8;
|
|
129
|
+
payloadDataView;
|
|
130
|
+
capacity;
|
|
131
|
+
constructor(sab) {
|
|
132
|
+
const views = mapDepotBuffer(sab);
|
|
133
|
+
this.headerInt32 = views.headerInt32;
|
|
134
|
+
this.payloadUint8 = views.payloadUint8;
|
|
135
|
+
this.capacity = views.capacity;
|
|
136
|
+
this.payloadDataView = new DataView(sab, HEADER_SIZE_BYTES, this.capacity);
|
|
137
|
+
}
|
|
138
|
+
acquireLock() {
|
|
139
|
+
while (Atomics.compareExchange(this.headerInt32, HEADER_IDX_MUTEX, MUTEX_UNLOCKED, MUTEX_LOCKED) !== MUTEX_UNLOCKED) { }
|
|
140
|
+
}
|
|
141
|
+
releaseLock() { Atomics.store(this.headerInt32, HEADER_IDX_MUTEX, MUTEX_UNLOCKED); }
|
|
142
|
+
write(p) {
|
|
143
|
+
const footprint = calculateFrameFootprint(p.byteLength);
|
|
144
|
+
if (footprint >= this.capacity)
|
|
145
|
+
return false;
|
|
146
|
+
this.acquireLock();
|
|
147
|
+
try {
|
|
148
|
+
const writeHead = Atomics.load(this.headerInt32, HEADER_IDX_WRITE_HEAD);
|
|
149
|
+
const readHead = Atomics.load(this.headerInt32, HEADER_IDX_READ_HEAD);
|
|
150
|
+
let target = writeHead;
|
|
151
|
+
if (readHead === writeHead) {
|
|
152
|
+
Atomics.store(this.headerInt32, HEADER_IDX_READ_HEAD, 0);
|
|
153
|
+
target = 0;
|
|
154
|
+
}
|
|
155
|
+
else if (writeHead > readHead) {
|
|
156
|
+
const tail = this.capacity - writeHead;
|
|
157
|
+
if (footprint <= tail) {
|
|
158
|
+
if (footprint === tail && readHead === 0)
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
if (footprint >= readHead)
|
|
163
|
+
return false;
|
|
164
|
+
this.payloadDataView.setUint32(writeHead, SENTINEL_WRAP_MARKER, true);
|
|
165
|
+
target = 0;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
if (footprint >= readHead - writeHead)
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
this.payloadDataView.setUint32(target, p.byteLength, true);
|
|
173
|
+
this.payloadUint8.set(p, target + 4);
|
|
174
|
+
Atomics.store(this.headerInt32, HEADER_IDX_WRITE_HEAD, (target + footprint) % this.capacity);
|
|
175
|
+
Atomics.notify(this.headerInt32, HEADER_IDX_WRITE_HEAD, 1);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
this.releaseLock();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
read() {
|
|
183
|
+
this.acquireLock();
|
|
184
|
+
try {
|
|
185
|
+
let readHead = Atomics.load(this.headerInt32, HEADER_IDX_READ_HEAD);
|
|
186
|
+
const writeHead = Atomics.load(this.headerInt32, HEADER_IDX_WRITE_HEAD);
|
|
187
|
+
if (readHead === writeHead)
|
|
188
|
+
return null;
|
|
189
|
+
let frameHeaderVal = this.payloadDataView.getUint32(readHead, true);
|
|
190
|
+
if (frameHeaderVal === SENTINEL_WRAP_MARKER) {
|
|
191
|
+
readHead = 0;
|
|
192
|
+
Atomics.store(this.headerInt32, HEADER_IDX_READ_HEAD, 0);
|
|
193
|
+
if (readHead === writeHead)
|
|
194
|
+
return null;
|
|
195
|
+
frameHeaderVal = this.payloadDataView.getUint32(readHead, true);
|
|
196
|
+
}
|
|
197
|
+
const payloadLength = frameHeaderVal;
|
|
198
|
+
const footprint = calculateFrameFootprint(payloadLength);
|
|
199
|
+
const p = this.payloadUint8.slice(readHead + 4, readHead + 4 + payloadLength);
|
|
200
|
+
const nextReadHead = (readHead + footprint) % this.capacity;
|
|
201
|
+
Atomics.store(this.headerInt32, HEADER_IDX_READ_HEAD, nextReadHead);
|
|
202
|
+
return p;
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
this.releaseLock();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
waitForDataAsync(timeoutMs = 1000) {
|
|
209
|
+
const writeHead = Atomics.load(this.headerInt32, HEADER_IDX_WRITE_HEAD);
|
|
210
|
+
const readHead = Atomics.load(this.headerInt32, HEADER_IDX_READ_HEAD);
|
|
211
|
+
if (writeHead !== readHead)
|
|
212
|
+
return Promise.resolve('ok');
|
|
213
|
+
const res = Atomics.waitAsync(this.headerInt32, HEADER_IDX_WRITE_HEAD, writeHead, timeoutMs);
|
|
214
|
+
return res.async ? res.value : Promise.resolve(res.value);
|
|
215
|
+
}
|
|
216
|
+
} // ---- POWER_OF_TWO — now SAB-backed, fixed-width payload lane ----
|
|
217
|
+
const DISRUPTOR_HEADER_SIZE = 64;
|
|
218
|
+
const H_WRITE = 0, H_READ = 1, H_CAPACITY = 2, H_SLOT_BYTES = 3;
|
|
219
|
+
const EMPTY_PAYLOAD = new Uint8Array(0);
|
|
220
|
+
export function createSharedDisruptorBuffer(capacity, maxPayloadBytes) {
|
|
221
|
+
if ((capacity & (capacity - 1)) !== 0)
|
|
222
|
+
throw new Error("Disruptor capacity must be a power of two.");
|
|
223
|
+
const slotPayloadBytes = Math.ceil(maxPayloadBytes / 8) * 8;
|
|
224
|
+
const total = DISRUPTOR_HEADER_SIZE + capacity * 4 + capacity * 8 + capacity * 4 + capacity + capacity * slotPayloadBytes;
|
|
225
|
+
const sab = new SharedArrayBuffer(total);
|
|
226
|
+
const header = new Int32Array(sab, 0, 4);
|
|
227
|
+
Atomics.store(header, H_WRITE, 0);
|
|
228
|
+
Atomics.store(header, H_READ, 0);
|
|
229
|
+
Atomics.store(header, H_CAPACITY, capacity);
|
|
230
|
+
Atomics.store(header, H_SLOT_BYTES, slotPayloadBytes);
|
|
231
|
+
return sab;
|
|
232
|
+
}
|
|
233
|
+
export function validateDisruptorHeader(sab) {
|
|
234
|
+
if (sab.byteLength < DISRUPTOR_HEADER_SIZE)
|
|
235
|
+
return false;
|
|
236
|
+
const header = new Int32Array(sab, 0, 4);
|
|
237
|
+
const capacity = Atomics.load(header, H_CAPACITY);
|
|
238
|
+
const slotPayloadBytes = Atomics.load(header, H_SLOT_BYTES);
|
|
239
|
+
if (capacity <= 0 || (capacity & (capacity - 1)) !== 0)
|
|
240
|
+
return false;
|
|
241
|
+
const expected = DISRUPTOR_HEADER_SIZE + capacity * 4 + capacity * 8 + capacity * 4 + capacity + capacity * slotPayloadBytes;
|
|
242
|
+
return sab.byteLength === expected;
|
|
243
|
+
}
|
|
244
|
+
export class POWER_OF_TWO {
|
|
245
|
+
capacity;
|
|
246
|
+
mask;
|
|
247
|
+
slotPayloadBytes;
|
|
248
|
+
pids;
|
|
249
|
+
values;
|
|
250
|
+
lengths;
|
|
251
|
+
flags;
|
|
252
|
+
payload;
|
|
253
|
+
header;
|
|
254
|
+
constructor(sab) {
|
|
255
|
+
this.header = new Int32Array(sab, 0, 4);
|
|
256
|
+
const capacity = Atomics.load(this.header, H_CAPACITY);
|
|
257
|
+
if ((capacity & (capacity - 1)) !== 0)
|
|
258
|
+
throw new Error("Disruptor capacity must be a power of two.");
|
|
259
|
+
this.capacity = capacity;
|
|
260
|
+
this.mask = capacity - 1;
|
|
261
|
+
this.slotPayloadBytes = Atomics.load(this.header, H_SLOT_BYTES);
|
|
262
|
+
let offset = DISRUPTOR_HEADER_SIZE;
|
|
263
|
+
this.pids = new Int32Array(sab, offset, capacity);
|
|
264
|
+
offset += capacity * 4;
|
|
265
|
+
this.values = new Float64Array(sab, offset, capacity);
|
|
266
|
+
offset += capacity * 8;
|
|
267
|
+
this.lengths = new Int32Array(sab, offset, capacity);
|
|
268
|
+
offset += capacity * 4;
|
|
269
|
+
this.flags = new Uint8Array(sab, offset, capacity);
|
|
270
|
+
offset += capacity;
|
|
271
|
+
this.payload = new Uint8Array(sab, offset, capacity * this.slotPayloadBytes);
|
|
272
|
+
}
|
|
273
|
+
publish(pid, value, flag = 0, payload) {
|
|
274
|
+
const payloadLen = payload?.byteLength ?? 0;
|
|
275
|
+
if (payloadLen > this.slotPayloadBytes)
|
|
276
|
+
return false;
|
|
277
|
+
const w = Atomics.load(this.header, H_WRITE);
|
|
278
|
+
const r = Atomics.load(this.header, H_READ);
|
|
279
|
+
if (w - r >= this.capacity)
|
|
280
|
+
return false;
|
|
281
|
+
const index = w & this.mask;
|
|
282
|
+
this.pids[index] = pid;
|
|
283
|
+
this.values[index] = value;
|
|
284
|
+
this.flags[index] = flag;
|
|
285
|
+
this.lengths[index] = payloadLen;
|
|
286
|
+
if (payloadLen > 0)
|
|
287
|
+
this.payload.set(payload, index * this.slotPayloadBytes);
|
|
288
|
+
Atomics.store(this.header, H_WRITE, w + 1);
|
|
289
|
+
Atomics.notify(this.header, H_WRITE);
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
consume(handler) {
|
|
293
|
+
const targetWrite = Atomics.load(this.header, H_WRITE);
|
|
294
|
+
let currentRead = Atomics.load(this.header, H_READ);
|
|
295
|
+
if (currentRead === targetWrite)
|
|
296
|
+
return 0;
|
|
297
|
+
let processed = 0;
|
|
298
|
+
while (currentRead < targetWrite) {
|
|
299
|
+
const index = currentRead & this.mask;
|
|
300
|
+
const len = this.lengths[index];
|
|
301
|
+
const base = index * this.slotPayloadBytes;
|
|
302
|
+
const payloadView = len > 0 ? this.payload.subarray(base, base + len) : EMPTY_PAYLOAD;
|
|
303
|
+
handler(this.pids[index], this.values[index], this.flags[index], payloadView);
|
|
304
|
+
currentRead++;
|
|
305
|
+
processed++;
|
|
306
|
+
}
|
|
307
|
+
Atomics.store(this.header, H_READ, currentRead);
|
|
308
|
+
return processed;
|
|
309
|
+
}
|
|
310
|
+
waitForDataAsync(timeoutMs = 1000) {
|
|
311
|
+
const w = Atomics.load(this.header, H_WRITE);
|
|
312
|
+
const r = Atomics.load(this.header, H_READ);
|
|
313
|
+
if (w !== r)
|
|
314
|
+
return Promise.resolve('ok');
|
|
315
|
+
const res = Atomics.waitAsync(this.header, H_WRITE, w, timeoutMs);
|
|
316
|
+
return res.async ? res.value : Promise.resolve(res.value);
|
|
317
|
+
}
|
|
318
|
+
reset() {
|
|
319
|
+
Atomics.store(this.header, H_WRITE, 0);
|
|
320
|
+
Atomics.store(this.header, H_READ, 0);
|
|
321
|
+
}
|
|
322
|
+
} // ---- HybridChannel: fast lane + overflow lane, one API ----
|
|
323
|
+
function encodeTopicIdFrame(topicId, payload) {
|
|
324
|
+
const frame = new Uint8Array(4 + payload.byteLength);
|
|
325
|
+
new DataView(frame.buffer).setUint32(0, topicId, true);
|
|
326
|
+
frame.set(payload, 4);
|
|
327
|
+
return frame;
|
|
328
|
+
}
|
|
329
|
+
function decodeTopicIdFrame(frame) {
|
|
330
|
+
const topicId = new DataView(frame.buffer, frame.byteOffset, 4).getUint32(0, true);
|
|
331
|
+
return { topicId, payload: frame.subarray(4) };
|
|
332
|
+
}
|
|
333
|
+
export class HybridChannel {
|
|
334
|
+
fast;
|
|
335
|
+
overflow;
|
|
336
|
+
fastDrops = 0;
|
|
337
|
+
overflowDrops = 0;
|
|
338
|
+
constructor(fast, overflow) {
|
|
339
|
+
this.fast = fast;
|
|
340
|
+
this.overflow = overflow;
|
|
341
|
+
}
|
|
342
|
+
publish(topicId, payload, value = 0, flag = 0) {
|
|
343
|
+
if (payload.byteLength <= this.fast.slotPayloadBytes) {
|
|
344
|
+
const ok = this.fast.publish(topicId, value, flag, payload);
|
|
345
|
+
if (!ok)
|
|
346
|
+
this.fastDrops++;
|
|
347
|
+
return ok;
|
|
348
|
+
}
|
|
349
|
+
const ok = this.overflow.write(encodeTopicIdFrame(topicId, payload));
|
|
350
|
+
if (!ok)
|
|
351
|
+
this.overflowDrops++;
|
|
352
|
+
return ok;
|
|
353
|
+
}
|
|
354
|
+
consume(handler) {
|
|
355
|
+
let processed = this.fast.consume((pid, value, flag, payload) => handler(pid, payload, value, flag));
|
|
356
|
+
let frame;
|
|
357
|
+
while ((frame = this.overflow.read()) !== null) {
|
|
358
|
+
const { topicId, payload } = decodeTopicIdFrame(frame);
|
|
359
|
+
handler(topicId, payload, 0, 0);
|
|
360
|
+
processed++;
|
|
361
|
+
}
|
|
362
|
+
return processed;
|
|
363
|
+
}
|
|
364
|
+
async drainLoop(handler, signal) {
|
|
365
|
+
while (!signal?.aborted) {
|
|
366
|
+
const processed = this.consume(handler);
|
|
367
|
+
if (processed === 0) {
|
|
368
|
+
await Promise.race([this.fast.waitForDataAsync(1000), this.overflow.waitForDataAsync(1000)]);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
get dropStats() {
|
|
373
|
+
return { fastDrops: this.fastDrops, overflowDrops: this.overflowDrops };
|
|
374
|
+
}
|
|
375
|
+
} // ---- RingPair / ControlPlaneBootstrapper ----
|
|
376
|
+
const g = globalThis;
|
|
377
|
+
function localProcessId() {
|
|
378
|
+
return typeof process !== 'undefined' && process.pid != null ? `${process.pid}` : 'worker';
|
|
379
|
+
}
|
|
380
|
+
function detectContainer() {
|
|
381
|
+
if (typeof g.importScripts === 'function' && typeof g.window === 'undefined')
|
|
382
|
+
return 'WORKER';
|
|
383
|
+
if (g.__IS_WORKER_THREAD__ === true)
|
|
384
|
+
return 'WORKER';
|
|
385
|
+
return 'PROCESS';
|
|
386
|
+
}
|
|
387
|
+
export class RingPair {
|
|
388
|
+
tx;
|
|
389
|
+
rx;
|
|
390
|
+
descriptor;
|
|
391
|
+
constructor(tx, rx, descriptor) {
|
|
392
|
+
this.tx = tx;
|
|
393
|
+
this.rx = rx;
|
|
394
|
+
this.descriptor = descriptor;
|
|
395
|
+
}
|
|
396
|
+
static create(capacity, maxPayloadBytes, overflowCapacityBytes) {
|
|
397
|
+
const descriptor = {
|
|
398
|
+
aToB_fast: createSharedDisruptorBuffer(capacity, maxPayloadBytes),
|
|
399
|
+
bToA_fast: createSharedDisruptorBuffer(capacity, maxPayloadBytes),
|
|
400
|
+
aToB_overflow: createSharedDepotBuffer(overflowCapacityBytes),
|
|
401
|
+
bToA_overflow: createSharedDepotBuffer(overflowCapacityBytes),
|
|
402
|
+
};
|
|
403
|
+
const tx = new HybridChannel(new POWER_OF_TWO(descriptor.aToB_fast), new RING_BUFFER(descriptor.aToB_overflow));
|
|
404
|
+
const rx = new HybridChannel(new POWER_OF_TWO(descriptor.bToA_fast), new RING_BUFFER(descriptor.bToA_overflow));
|
|
405
|
+
return new RingPair(tx, rx, descriptor);
|
|
406
|
+
}
|
|
407
|
+
static attach(descriptor) {
|
|
408
|
+
if (!validateDisruptorHeader(descriptor.aToB_fast) || !validateDisruptorHeader(descriptor.bToA_fast) ||
|
|
409
|
+
!validateHeader(descriptor.aToB_overflow) || !validateHeader(descriptor.bToA_overflow)) {
|
|
410
|
+
throw new Error('[RingPair] invalid depot header');
|
|
411
|
+
}
|
|
412
|
+
const tx = new HybridChannel(new POWER_OF_TWO(descriptor.bToA_fast), new RING_BUFFER(descriptor.bToA_overflow));
|
|
413
|
+
const rx = new HybridChannel(new POWER_OF_TWO(descriptor.aToB_fast), new RING_BUFFER(descriptor.aToB_overflow));
|
|
414
|
+
return new RingPair(tx, rx, descriptor);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const localDepots = Object.create(null);
|
|
418
|
+
export class ControlPlaneBootstrapper {
|
|
419
|
+
localAddressSpaceId;
|
|
420
|
+
k;
|
|
421
|
+
s;
|
|
422
|
+
m;
|
|
423
|
+
r;
|
|
424
|
+
onPromote;
|
|
425
|
+
send;
|
|
426
|
+
sub;
|
|
427
|
+
peerRegistry = Object.create(null);
|
|
428
|
+
constructor(localAddressSpaceId, k, s, m, r, onPromote, send, sub) {
|
|
429
|
+
this.localAddressSpaceId = localAddressSpaceId;
|
|
430
|
+
this.k = k;
|
|
431
|
+
this.s = s;
|
|
432
|
+
this.m = m;
|
|
433
|
+
this.r = r;
|
|
434
|
+
this.onPromote = onPromote;
|
|
435
|
+
this.send = send;
|
|
436
|
+
this.sub = sub;
|
|
437
|
+
this.sub((msg) => {
|
|
438
|
+
if (msg.type !== 'BMRF_DEPOT_REGISTER')
|
|
439
|
+
return;
|
|
440
|
+
const peer = msg.p;
|
|
441
|
+
if (peer.addressSpaceId === this.localAddressSpaceId)
|
|
442
|
+
return;
|
|
443
|
+
this.peerRegistry[peer.addressSpaceId] = peer;
|
|
444
|
+
const ring = localDepots[peer.addressSpaceId];
|
|
445
|
+
if (ring) {
|
|
446
|
+
this.promoteRouteToDataPlane({ ...peer, ring });
|
|
447
|
+
}
|
|
448
|
+
}, this.k, this.s, this.m, this.r);
|
|
449
|
+
}
|
|
450
|
+
announceDepot(capacity, maxPayloadBytes, overflowCapacityBytes) {
|
|
451
|
+
const pair = RingPair.create(capacity, maxPayloadBytes, overflowCapacityBytes);
|
|
452
|
+
localDepots[this.localAddressSpaceId] = pair.descriptor;
|
|
453
|
+
this.send({
|
|
454
|
+
type: 'BMRF_DEPOT_REGISTER',
|
|
455
|
+
p: {
|
|
456
|
+
addressSpaceId: this.localAddressSpaceId,
|
|
457
|
+
processId: localProcessId(),
|
|
458
|
+
boundaryType: 'SAME_NODE',
|
|
459
|
+
container: detectContainer(),
|
|
460
|
+
k: this.k, s: this.s, m: this.m, r: this.r,
|
|
461
|
+
capacityBytes: overflowCapacityBytes,
|
|
462
|
+
},
|
|
463
|
+
}, this.k, this.s, this.m, this.r);
|
|
464
|
+
return pair;
|
|
465
|
+
}
|
|
466
|
+
promoteRouteToDataPlane(peer) {
|
|
467
|
+
if (!peer.ring)
|
|
468
|
+
return;
|
|
469
|
+
if (peer.boundaryType === 'CROSS_NODE')
|
|
470
|
+
return;
|
|
471
|
+
if (peer.processId !== localProcessId())
|
|
472
|
+
return;
|
|
473
|
+
try {
|
|
474
|
+
const pair = RingPair.attach(peer.ring);
|
|
475
|
+
this.onPromote(peer, pair);
|
|
476
|
+
console.log(`[BMRF] Route to AddressSpace ${peer.addressSpaceId} PROMOTED to Layer 2 Data Plane.`);
|
|
477
|
+
}
|
|
478
|
+
catch (e) {
|
|
479
|
+
console.error('[BMRF] promotion failed, staying on baseline transport', e);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type CBORValue = undefined | null | boolean | number | string | Uint8Array | CBORValue[] | {
|
|
2
|
+
[k: string]: CBORValue;
|
|
3
|
+
};
|
|
4
|
+
export declare class BINARY_CODEC {
|
|
5
|
+
encodeJSONEnvelope(envelope: any): Uint8Array;
|
|
6
|
+
decodeJSONEnvelope(bytes: Uint8Array): any;
|
|
7
|
+
private bytes;
|
|
8
|
+
private view;
|
|
9
|
+
private tail;
|
|
10
|
+
private strings;
|
|
11
|
+
private stringBytes;
|
|
12
|
+
private refs;
|
|
13
|
+
private nextRef;
|
|
14
|
+
encode_js(value: U): U8;
|
|
15
|
+
decode_js(binary: U8): U;
|
|
16
|
+
private need;
|
|
17
|
+
private u8;
|
|
18
|
+
private i32;
|
|
19
|
+
private f64;
|
|
20
|
+
private writeValue;
|
|
21
|
+
private writeString;
|
|
22
|
+
private writeArray;
|
|
23
|
+
private writeBytes;
|
|
24
|
+
private writeObject;
|
|
25
|
+
private beginRef;
|
|
26
|
+
private endRef;
|
|
27
|
+
private readU8;
|
|
28
|
+
private readI32;
|
|
29
|
+
private readF64;
|
|
30
|
+
private readValue;
|
|
31
|
+
private readString;
|
|
32
|
+
private readArray;
|
|
33
|
+
private readObject;
|
|
34
|
+
private readBytes;
|
|
35
|
+
private ensureAvailable;
|
|
36
|
+
private readUint;
|
|
37
|
+
private encodeUint64;
|
|
38
|
+
private encode;
|
|
39
|
+
private encodeValue;
|
|
40
|
+
private encodeUint;
|
|
41
|
+
private decode;
|
|
42
|
+
private decodeValue;
|
|
43
|
+
encode_msg(msg: any): Uint8Array;
|
|
44
|
+
decode_msg(raw: any): any;
|
|
45
|
+
HEADER_SIZE_BYTES: number;
|
|
46
|
+
HEADER_IDX_WRITE_HEAD: number;
|
|
47
|
+
HEADER_IDX_READ_HEAD: number;
|
|
48
|
+
HEADER_IDX_MUTEX: number;
|
|
49
|
+
HEADER_IDX_CAPACITY: number;
|
|
50
|
+
MUTEX_UNLOCKED: number;
|
|
51
|
+
MUTEX_LOCKED: number;
|
|
52
|
+
SENTINEL_WRAP_MARKER: number;
|
|
53
|
+
private static topicToIdMap;
|
|
54
|
+
private static idToTopicMap;
|
|
55
|
+
private static headerBuffer;
|
|
56
|
+
private static headerView;
|
|
57
|
+
private static headerBytes;
|
|
58
|
+
FLYWEIGHT_FRAME(): DecodedFrame;
|
|
59
|
+
static hashTopic(topic: string): number;
|
|
60
|
+
static getTopicId(topic: string): number;
|
|
61
|
+
static getTopicName(topicId: number): string | undefined;
|
|
62
|
+
static registerTopics(topics: string[]): void;
|
|
63
|
+
static resetRegistry(): void;
|
|
64
|
+
static encodeFrame(topic: string, p: Uint8Array | string | Record<string, unknown>): Uint8Array;
|
|
65
|
+
decodeFrame(frameBytes: Uint8Array): DecodedFrame;
|
|
66
|
+
createSharedDepotBuffer(capacityBytes: number): SharedArrayBuffer;
|
|
67
|
+
mapDepotBuffer(sab: SharedArrayBuffer): HeaderViews;
|
|
68
|
+
validateHeader(sab: SharedArrayBuffer): boolean;
|
|
69
|
+
calculateFrameFootprint(payloadSizeBytes: number): number;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../src/comps/binary.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAgB7H,qBAAa,YAAY;IAExB,kBAAkB,CAAC,QAAQ,EAAE,GAAG,GAAG,UAAU;IAG7C,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,GAAG;IAOnC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,IAAI,CAAW;IACvB,OAAO,CAAC,IAAI,CAAK;IAEjB,OAAO,CAAC,OAAO,CAAmB;IAClC,OAAO,CAAC,WAAW,CAAY;IAC/B,OAAO,CAAC,IAAI,CAAwB;IACpC,OAAO,CAAC,OAAO,CAAK;IACpB,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE;IAYvB,SAAS,CAAC,MAAM,EAAE,EAAE,GAAG,CAAC;IAwBxB,OAAO,CAAC,IAAI;IAaZ,OAAO,CAAC,EAAE;IAIV,OAAO,CAAC,GAAG;IAKX,OAAO,CAAC,GAAG;IAKX,OAAO,CAAC,UAAU;IAmClB,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,UAAU;IAYlB,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,MAAM;IAGd,OAAO,CAAC,OAAO;IAMf,OAAO,CAAC,OAAO;IAMf,OAAO,CAAC,SAAS;IAsCjB,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,SAAS;IAUjB,OAAO,CAAC,UAAU;IAWlB,OAAO,CAAC,SAAS;IAWxB,OAAO,CAAC,eAAe;IAGvB,OAAO,CAAC,QAAQ;IAwBhB,OAAO,CAAC,YAAY;IAsBpB,OAAO,CAAC,MAAM;IAYd,OAAO,CAAC,WAAW;IA8DnB,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,MAAM;IAOd,OAAO,CAAC,WAAW;IAqDZ,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,UAAU;IAUhC,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAiBzB,iBAAiB,SAAM;IACvB,qBAAqB,SAAK;IAC1B,oBAAoB,SAAK;IACzB,gBAAgB,SAAK;IACrB,mBAAmB,SAAK;IACxB,cAAc,SAAK;IACnB,YAAY,SAAK;IACjB,oBAAoB,SAAc;IACzC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAkC;IAC7D,OAAO,CAAC,MAAM,CAAC,YAAY,CAAkC;IAC7D,OAAO,CAAC,MAAM,CAAC,YAAY,CAAsB;IACjD,OAAO,CAAC,MAAM,CAAC,UAAU,CAA2C;IACpE,OAAO,CAAC,MAAM,CAAC,WAAW,CAA6C;IAEhE,eAAe,IAAI,YAAY;WAOxB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;WAQhC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;WAgBjC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;WAGjD,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI;WAKtC,aAAa,IAAI,IAAI;WAKrB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU;IAuB7F,WAAW,CAAC,UAAU,EAAE,UAAU,GAAG,YAAY;IAmBnD,uBAAuB,CAAC,aAAa,EAAE,MAAM,GAAG,iBAAiB;IAgBjE,cAAc,CAAC,GAAG,EAAE,iBAAiB,GAAG,WAAW;IAgBnD,cAAc,CAAC,GAAG,EAAE,iBAAiB,GAAG,OAAO;IAS/C,uBAAuB,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM;CAIhE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class BYTE_ID {
|
|
2
|
+
MAX_PENDING: number;
|
|
3
|
+
sentTimestamps: any;
|
|
4
|
+
pendingResolvers: any;
|
|
5
|
+
cursor: number;
|
|
6
|
+
private readonly counter;
|
|
7
|
+
private epoch;
|
|
8
|
+
_byteid(): string;
|
|
9
|
+
b(): string;
|
|
10
|
+
constructor();
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=byte_id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"byte_id.d.ts","sourceRoot":"","sources":["../../src/comps/byte_id.ts"],"names":[],"mappings":"AAAA,qBAAa,OAAO;IAST,WAAW,SAAQ;IACnB,cAAc,MAAC;IACf,gBAAgB,MAAC;IACjB,MAAM,SAAK;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAM;IAC9B,OAAO,CAAC,KAAK,CAAK;IACX,OAAO,IAAI,MAAM;IAOjB,CAAC,IAAI,MAAM;;CAQrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cell-worker.d.ts","sourceRoot":"","sources":["../../src/comps/cell-worker.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,KAAK,OAAO,EAAE,MAAM,YAAY,CAAC;AAKjD,qBAAa,MAAM;;IAEN,OAAO,CAAC,GAAG;IAAgB,OAAO,CAAC,SAAS;gBAApC,GAAG,EAAE,YAAY,EAAU,SAAS,EAAE,UAAU;IACpE,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC;CA0BlB"}
|