@party-stack/ontology-backend 0.5.0 → 0.7.0
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/lib/actions.d.ts.map +1 -1
- package/lib/actions.js +8 -7
- package/lib/actions.js.map +1 -1
- package/lib/delivery.d.ts +8 -0
- package/lib/delivery.d.ts.map +1 -1
- package/lib/delivery.js +433 -374
- package/lib/delivery.js.map +1 -1
- package/lib/description.d.ts +14 -4
- package/lib/description.d.ts.map +1 -1
- package/lib/description.js +15 -6
- package/lib/description.js.map +1 -1
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/meta.d.ts +34 -7
- package/lib/meta.d.ts.map +1 -1
- package/lib/meta.js +50 -52
- package/lib/meta.js.map +1 -1
- package/lib/owned.d.ts.map +1 -1
- package/lib/owned.js +2 -1
- package/lib/owned.js.map +1 -1
- package/lib/storage.d.ts +1 -0
- package/lib/storage.d.ts.map +1 -1
- package/package.json +3 -3
package/lib/delivery.js
CHANGED
|
@@ -219,6 +219,14 @@ const makeDeliveryManager = Effect.fnUntraced(function* (options) {
|
|
|
219
219
|
const { tables, namespace } = options;
|
|
220
220
|
const sessions = sessionTable(namespace);
|
|
221
221
|
const memories = /* @__PURE__ */ new Map();
|
|
222
|
+
const refOf = (encoded) => {
|
|
223
|
+
try {
|
|
224
|
+
const change = valueCodec.decode(encoded);
|
|
225
|
+
return change.kind === "put" ? deliveryRef(change.record) : change.ref;
|
|
226
|
+
} catch {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
222
230
|
const admission = Semaphore.makeUnsafe(1);
|
|
223
231
|
let closed = false;
|
|
224
232
|
const forget = Effect.fnUntraced(function* (stream, kind = "HistoryExpired") {
|
|
@@ -251,406 +259,457 @@ const makeDeliveryManager = Effect.fnUntraced(function* (options) {
|
|
|
251
259
|
const memory = memories.get(position.streamId);
|
|
252
260
|
if (memory) yield* Queue.offer(memory.acknowledgments, void 0);
|
|
253
261
|
});
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (prior.format !== 3 || !priorMemory || prior.pending && priorMemory.pending?.sequence !== prior.pending.sequence) {
|
|
294
|
-
yield* clearLegacy(tx, streamId);
|
|
295
|
-
yield* tx.delete(sessions, streamId);
|
|
296
|
-
return {
|
|
297
|
-
kind: "expired",
|
|
298
|
-
streamId
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
if (prior.identity === options.identity) {
|
|
302
|
-
const position = options.resume.sequence;
|
|
303
|
-
if (position < prior.installed || position > prior.expected) return yield* failure("Delivery position cannot resume.", "HistoryExpired");
|
|
304
|
-
const header = {
|
|
305
|
-
...prior,
|
|
306
|
-
producer,
|
|
307
|
-
acknowledged: Math.max(prior.acknowledged, position),
|
|
308
|
-
expiresAt: now + activeRetentionMs
|
|
309
|
-
};
|
|
310
|
-
yield* tx.put(sessions, streamId, header);
|
|
311
|
-
return {
|
|
312
|
-
kind: "resumed",
|
|
313
|
-
streamId,
|
|
314
|
-
memory: priorMemory,
|
|
315
|
-
header
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
if (prior.pending && prior.rowSelection !== void 0 && prior.rowSelection === options.rowSelection && options.resume.sequence === prior.installed) {
|
|
319
|
-
const header = {
|
|
320
|
-
...prior,
|
|
321
|
-
identity: options.identity,
|
|
322
|
-
producer,
|
|
323
|
-
expiresAt: now + activeRetentionMs
|
|
324
|
-
};
|
|
325
|
-
yield* tx.put(sessions, streamId, header);
|
|
326
|
-
return {
|
|
327
|
-
kind: "resumed",
|
|
328
|
-
streamId,
|
|
329
|
-
memory: priorMemory,
|
|
330
|
-
header
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
if (options.resume.sequence > 0 && options.resume.sequence === prior.expected && (!prior.pending || prior.pending.sequence === options.resume.sequence)) {
|
|
334
|
-
const header = {
|
|
335
|
-
...prior,
|
|
336
|
-
identity: options.identity,
|
|
337
|
-
rowSelection: options.rowSelection,
|
|
338
|
-
producer,
|
|
339
|
-
acknowledged: options.resume.sequence,
|
|
340
|
-
installed: options.resume.sequence,
|
|
341
|
-
signature: prior.pending?.nextSignature ?? prior.signature,
|
|
342
|
-
pending: void 0,
|
|
343
|
-
expiresAt: now + activeRetentionMs
|
|
344
|
-
};
|
|
345
|
-
yield* tx.put(sessions, streamId, header);
|
|
346
|
-
return {
|
|
347
|
-
kind: "promoted",
|
|
348
|
-
streamId,
|
|
349
|
-
memory: priorMemory,
|
|
350
|
-
header
|
|
351
|
-
};
|
|
352
|
-
}
|
|
262
|
+
const open = Effect.fn("Delivery.open")(function* (options) {
|
|
263
|
+
const limits = {
|
|
264
|
+
maxRecords: 1e4,
|
|
265
|
+
maxBytes: 16777216,
|
|
266
|
+
frameBytes: 262144,
|
|
267
|
+
frameRecords: 128,
|
|
268
|
+
maxSubscriptions: 32,
|
|
269
|
+
ackTimeoutMs: 3e4,
|
|
270
|
+
heartbeatMs: 15e3,
|
|
271
|
+
...options.policy,
|
|
272
|
+
retentionMs: options.policy?.retentionMs ?? 6e4
|
|
273
|
+
};
|
|
274
|
+
if (Object.values(limits).some((n) => !Number.isSafeInteger(n) || n < 1)) return yield* failure("Delivery limits must be positive integers.", "InvalidConfiguration");
|
|
275
|
+
const activeRetentionMs = Math.max(limits.retentionMs, limits.ackTimeoutMs + limits.heartbeatMs * 3);
|
|
276
|
+
const producer = crypto.randomUUID();
|
|
277
|
+
const acquire = admission.withPermits(1)(Effect.gen(function* () {
|
|
278
|
+
if (closed) return yield* failure("Delivery owner closed.", "HistoryExpired");
|
|
279
|
+
yield* prune();
|
|
280
|
+
const gone = yield* Deferred.make();
|
|
281
|
+
const priorMemory = options.resume && memories.get(options.resume.streamId);
|
|
282
|
+
const configure = Effect.gen(function* () {
|
|
283
|
+
const outcome = yield* boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
284
|
+
yield* tx.ensureIndex(sessions, {
|
|
285
|
+
name: "expiry",
|
|
286
|
+
field: field("expiresAt")
|
|
287
|
+
});
|
|
288
|
+
const now = yield* Clock.currentTimeMillis;
|
|
289
|
+
for (const expired of yield* tx.query(sessions, {
|
|
290
|
+
where: lte(field("expiresAt"), now),
|
|
291
|
+
limit: limits.maxSubscriptions
|
|
292
|
+
})) {
|
|
293
|
+
yield* clearLegacy(tx, String(expired.key));
|
|
294
|
+
yield* tx.delete(sessions, expired.key);
|
|
295
|
+
}
|
|
296
|
+
let streamId = options.resume?.streamId ?? crypto.randomUUID();
|
|
297
|
+
if (options.resume) {
|
|
298
|
+
const prior = yield* tx.get(sessions, streamId);
|
|
299
|
+
if (!prior || prior.expiresAt <= now || prior.binding !== options.binding) return yield* failure("Delivery history expired.", "HistoryExpired");
|
|
300
|
+
if (prior.format !== 3 || !priorMemory || prior.pending && priorMemory.pending?.sequence !== prior.pending.sequence) {
|
|
353
301
|
yield* clearLegacy(tx, streamId);
|
|
354
302
|
yield* tx.delete(sessions, streamId);
|
|
355
|
-
|
|
303
|
+
return {
|
|
304
|
+
kind: "expired",
|
|
305
|
+
streamId
|
|
306
|
+
};
|
|
356
307
|
}
|
|
357
|
-
if (
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
binding: options.binding,
|
|
365
|
-
identity: options.identity,
|
|
366
|
-
rowSelection: options.rowSelection,
|
|
367
|
-
producer
|
|
368
|
-
};
|
|
369
|
-
yield* tx.put(sessions, streamId, header);
|
|
370
|
-
return {
|
|
371
|
-
kind: "fresh",
|
|
372
|
-
streamId,
|
|
373
|
-
header,
|
|
374
|
-
memory: {
|
|
375
|
-
gate: Semaphore.makeUnsafe(1),
|
|
376
|
-
acknowledgments: yield* Queue.sliding(1),
|
|
377
|
-
gone,
|
|
378
|
-
producer,
|
|
379
|
-
expiresAt: header.expiresAt,
|
|
380
|
-
witnesses: /* @__PURE__ */ new Map()
|
|
308
|
+
if (priorMemory.redefined && prior.pending && options.resume.sequence === prior.installed) {
|
|
309
|
+
for (const encoded of priorMemory.pending?.changes ?? []) {
|
|
310
|
+
const ref = refOf(encoded);
|
|
311
|
+
if (ref) priorMemory.witnesses.set(deliveryKey(ref), {
|
|
312
|
+
ref,
|
|
313
|
+
hash: ""
|
|
314
|
+
});
|
|
381
315
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
316
|
+
const header = {
|
|
317
|
+
...prior,
|
|
318
|
+
identity: options.identity,
|
|
319
|
+
rowSelection: options.rowSelection,
|
|
320
|
+
producer,
|
|
321
|
+
expected: prior.installed,
|
|
322
|
+
pending: void 0,
|
|
323
|
+
expiresAt: now + activeRetentionMs
|
|
324
|
+
};
|
|
325
|
+
yield* tx.put(sessions, streamId, header);
|
|
326
|
+
return {
|
|
327
|
+
kind: "promoted",
|
|
328
|
+
streamId,
|
|
329
|
+
memory: priorMemory,
|
|
330
|
+
header
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
if (prior.identity === options.identity) {
|
|
334
|
+
const position = options.resume.sequence;
|
|
335
|
+
if (position < prior.installed || position > prior.expected) return yield* failure("Delivery position cannot resume.", "HistoryExpired");
|
|
336
|
+
const header = {
|
|
337
|
+
...prior,
|
|
338
|
+
producer,
|
|
339
|
+
acknowledged: Math.max(prior.acknowledged, position),
|
|
340
|
+
expiresAt: now + activeRetentionMs
|
|
341
|
+
};
|
|
342
|
+
yield* tx.put(sessions, streamId, header);
|
|
343
|
+
return {
|
|
344
|
+
kind: "resumed",
|
|
345
|
+
streamId,
|
|
346
|
+
memory: priorMemory,
|
|
347
|
+
header
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
if (prior.pending && prior.rowSelection !== void 0 && prior.rowSelection === options.rowSelection && options.resume.sequence === prior.installed) {
|
|
351
|
+
const header = {
|
|
352
|
+
...prior,
|
|
353
|
+
identity: options.identity,
|
|
354
|
+
producer,
|
|
355
|
+
expiresAt: now + activeRetentionMs
|
|
356
|
+
};
|
|
357
|
+
yield* tx.put(sessions, streamId, header);
|
|
358
|
+
return {
|
|
359
|
+
kind: "resumed",
|
|
360
|
+
streamId,
|
|
361
|
+
memory: priorMemory,
|
|
362
|
+
header
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
if (options.resume.sequence > 0 && options.resume.sequence === prior.expected && (!prior.pending || prior.pending.sequence === options.resume.sequence)) {
|
|
366
|
+
const header = {
|
|
367
|
+
...prior,
|
|
368
|
+
identity: options.identity,
|
|
369
|
+
rowSelection: options.rowSelection,
|
|
370
|
+
producer,
|
|
371
|
+
acknowledged: options.resume.sequence,
|
|
372
|
+
installed: options.resume.sequence,
|
|
373
|
+
signature: prior.pending?.nextSignature ?? prior.signature,
|
|
374
|
+
pending: void 0,
|
|
375
|
+
expiresAt: now + activeRetentionMs
|
|
376
|
+
};
|
|
377
|
+
yield* tx.put(sessions, streamId, header);
|
|
378
|
+
return {
|
|
379
|
+
kind: "promoted",
|
|
380
|
+
streamId,
|
|
381
|
+
memory: priorMemory,
|
|
382
|
+
header
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
yield* clearLegacy(tx, streamId);
|
|
386
|
+
yield* tx.delete(sessions, streamId);
|
|
387
|
+
streamId = crypto.randomUUID();
|
|
387
388
|
}
|
|
388
|
-
if (
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
389
|
+
if ((yield* tx.query(sessions, { limit: limits.maxSubscriptions })).length >= limits.maxSubscriptions) return yield* failure("Delivery subscription capacity reached.", "RateLimited");
|
|
390
|
+
const header = {
|
|
391
|
+
format: 3,
|
|
392
|
+
expiresAt: now + activeRetentionMs,
|
|
393
|
+
expected: 0,
|
|
394
|
+
acknowledged: 0,
|
|
395
|
+
installed: 0,
|
|
396
|
+
binding: options.binding,
|
|
397
|
+
identity: options.identity,
|
|
398
|
+
rowSelection: options.rowSelection,
|
|
399
|
+
producer
|
|
400
|
+
};
|
|
401
|
+
yield* tx.put(sessions, streamId, header);
|
|
398
402
|
return {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
403
|
+
kind: "fresh",
|
|
404
|
+
streamId,
|
|
405
|
+
header,
|
|
406
|
+
memory: {
|
|
407
|
+
gate: Semaphore.makeUnsafe(1),
|
|
408
|
+
acknowledgments: yield* Queue.sliding(1),
|
|
409
|
+
gone,
|
|
410
|
+
producer,
|
|
411
|
+
expiresAt: header.expiresAt,
|
|
412
|
+
witnesses: /* @__PURE__ */ new Map()
|
|
413
|
+
}
|
|
402
414
|
};
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
if (
|
|
409
|
-
|
|
415
|
+
})));
|
|
416
|
+
if (outcome.kind === "expired") {
|
|
417
|
+
yield* forget(outcome.streamId);
|
|
418
|
+
return yield* failure("Delivery view owner changed; acquire a new snapshot.", "HistoryExpired");
|
|
419
|
+
}
|
|
420
|
+
if (closed) return yield* failure("Delivery owner closed.", "HistoryExpired");
|
|
421
|
+
if (options.resume && options.resume.streamId !== outcome.streamId) yield* forget(options.resume.streamId, "SessionReplaced");
|
|
422
|
+
if (outcome.kind !== "fresh") yield* Deferred.fail(outcome.memory.gone, failure("Delivery connection was superseded.", "SessionReplaced"));
|
|
423
|
+
if (closed) return yield* failure("Delivery owner closed.", "HistoryExpired");
|
|
424
|
+
const memory = outcome.memory;
|
|
425
|
+
memory.gone = gone;
|
|
426
|
+
memory.producer = producer;
|
|
427
|
+
memory.expiresAt = outcome.header.expiresAt;
|
|
428
|
+
if (outcome.kind === "promoted") {
|
|
429
|
+
memory.pending = void 0;
|
|
430
|
+
memory.redefined = false;
|
|
431
|
+
}
|
|
432
|
+
memories.set(outcome.streamId, memory);
|
|
433
|
+
return {
|
|
434
|
+
streamId: outcome.streamId,
|
|
435
|
+
memory,
|
|
436
|
+
gone
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
return yield* priorMemory ? priorMemory.gate.withPermits(1)(configure) : configure;
|
|
440
|
+
}));
|
|
441
|
+
const { streamId, memory, gone } = yield* Effect.acquireRelease(acquire, ({ streamId, memory }) => memory.gate.withPermits(1)(boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
442
|
+
const session = yield* tx.get(sessions, streamId);
|
|
443
|
+
if (session?.producer !== producer) return;
|
|
444
|
+
const expiresAt = (yield* Clock.currentTimeMillis) + limits.retentionMs;
|
|
445
|
+
yield* tx.put(sessions, streamId, {
|
|
446
|
+
...session,
|
|
447
|
+
expiresAt
|
|
448
|
+
});
|
|
449
|
+
return expiresAt;
|
|
450
|
+
}))).pipe(Effect.tap((expiresAt) => Effect.sync(() => {
|
|
451
|
+
if (expiresAt !== void 0 && memory.producer === producer) memory.expiresAt = expiresAt;
|
|
452
|
+
})), Effect.catch(() => Effect.void), Effect.ensuring(Effect.suspend(() => memories.get(streamId) !== memory ? Queue.shutdown(memory.acknowledgments) : Effect.void)))));
|
|
453
|
+
const current = (tx) => Effect.gen(function* () {
|
|
454
|
+
if (closed || memories.get(streamId) !== memory) return yield* failure("Delivery view owner changed.", "HistoryExpired");
|
|
455
|
+
const session = yield* tx.get(sessions, streamId);
|
|
456
|
+
if (!session || session.producer !== producer || memory.producer !== producer) return yield* failure("Delivery connection was superseded.", "SessionReplaced");
|
|
457
|
+
return session;
|
|
458
|
+
});
|
|
459
|
+
const changes = yield* options.changes;
|
|
460
|
+
const dirty = yield* Queue.sliding(1);
|
|
461
|
+
yield* Effect.addFinalizer(() => Queue.shutdown(dirty));
|
|
462
|
+
yield* Stream.runForEach(changes, () => Queue.offer(dirty, void 0)).pipe(Effect.forkScoped);
|
|
463
|
+
let consumed = false;
|
|
464
|
+
const renew = memory.gate.withPermits(1)(Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
465
|
+
const expiresAt = yield* restore(boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
466
|
+
const session = yield* current(tx);
|
|
467
|
+
if (options.renew) yield* options.renew(tx);
|
|
468
|
+
const expiresAt = (yield* Clock.currentTimeMillis) + activeRetentionMs;
|
|
410
469
|
yield* tx.put(sessions, streamId, {
|
|
411
470
|
...session,
|
|
412
471
|
expiresAt
|
|
413
472
|
});
|
|
414
473
|
return expiresAt;
|
|
415
|
-
})))
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
if (
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const changes = yield* options.changes;
|
|
425
|
-
const dirty = yield* Queue.sliding(1);
|
|
426
|
-
yield* Effect.addFinalizer(() => Queue.shutdown(dirty));
|
|
427
|
-
yield* Stream.runForEach(changes, () => Queue.offer(dirty, void 0)).pipe(Effect.forkScoped);
|
|
428
|
-
let consumed = false;
|
|
429
|
-
const renew = memory.gate.withPermits(1)(Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
430
|
-
const expiresAt = yield* restore(boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
431
|
-
const session = yield* current(tx);
|
|
432
|
-
if (options.renew) yield* options.renew(tx);
|
|
433
|
-
const expiresAt = (yield* Clock.currentTimeMillis) + activeRetentionMs;
|
|
434
|
-
yield* tx.put(sessions, streamId, {
|
|
435
|
-
...session,
|
|
436
|
-
expiresAt
|
|
437
|
-
});
|
|
438
|
-
return expiresAt;
|
|
439
|
-
}))));
|
|
440
|
-
if (closed || memories.get(streamId) !== memory || memory.producer !== producer) return yield* failure("Delivery view owner changed.", "HistoryExpired");
|
|
441
|
-
memory.expiresAt = expiresAt;
|
|
442
|
-
}))).pipe(Effect.withSpan("Delivery.renew", { attributes: { streamId } }));
|
|
443
|
-
const waitForChange = Effect.gen(function* () {
|
|
444
|
-
for (;;) {
|
|
445
|
-
if (yield* Effect.raceFirst(Queue.peek(dirty).pipe(Effect.as(true)), Effect.sleep(limits.heartbeatMs).pipe(Effect.as(false)))) {
|
|
446
|
-
yield* Queue.take(dirty);
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
yield* renew;
|
|
474
|
+
}))));
|
|
475
|
+
if (closed || memories.get(streamId) !== memory || memory.producer !== producer) return yield* failure("Delivery view owner changed.", "HistoryExpired");
|
|
476
|
+
memory.expiresAt = expiresAt;
|
|
477
|
+
}))).pipe(Effect.withSpan("Delivery.renew", { attributes: { streamId } }));
|
|
478
|
+
const waitForChange = Effect.gen(function* () {
|
|
479
|
+
for (;;) {
|
|
480
|
+
if (yield* Effect.raceFirst(Queue.peek(dirty).pipe(Effect.as(true)), Effect.sleep(limits.heartbeatMs).pipe(Effect.as(false)))) {
|
|
481
|
+
yield* Queue.take(dirty);
|
|
482
|
+
return;
|
|
450
483
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
484
|
+
yield* renew;
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
const prepare = memory.gate.withPermits(1)(Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
488
|
+
let prepared;
|
|
489
|
+
const result = yield* restore(boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
490
|
+
const session = yield* current(tx);
|
|
491
|
+
const installed = session.installed;
|
|
492
|
+
const signature = session.signature;
|
|
493
|
+
yield* Effect.annotateCurrentSpan({
|
|
494
|
+
streamId,
|
|
495
|
+
installed,
|
|
496
|
+
acknowledged: session.acknowledged,
|
|
497
|
+
expected: session.expected,
|
|
498
|
+
replay: Boolean(session.pending)
|
|
499
|
+
});
|
|
500
|
+
if (session.pending) {
|
|
501
|
+
if (memory.pending?.sequence !== session.pending.sequence) return yield* failure("Delivery view buffer expired.", "HistoryExpired");
|
|
502
|
+
yield* Queue.offer(dirty, void 0);
|
|
503
|
+
return session.pending;
|
|
504
|
+
}
|
|
505
|
+
const sequence = installed + 1;
|
|
506
|
+
const witnesses = new Map(memory.witnesses);
|
|
507
|
+
const patches = /* @__PURE__ */ new Map();
|
|
508
|
+
const seen = /* @__PURE__ */ new Map();
|
|
509
|
+
let count = 0;
|
|
510
|
+
let bytes = 0;
|
|
511
|
+
const buffered = /* @__PURE__ */ new Map();
|
|
512
|
+
const flush = Effect.gen(function* () {
|
|
513
|
+
const page = [...buffered];
|
|
514
|
+
buffered.clear();
|
|
515
|
+
const hashed = yield* Effect.forEach(page, ([ref, value]) => Effect.map(digestBytes({
|
|
516
|
+
type: "application/json",
|
|
517
|
+
stream: Stream.make(value.encoded)
|
|
518
|
+
}), (hash) => ({
|
|
519
|
+
ref,
|
|
520
|
+
record: value.record,
|
|
521
|
+
hash
|
|
522
|
+
})), { concurrency: 128 });
|
|
523
|
+
for (const { ref, record, hash } of hashed) {
|
|
524
|
+
if (witnesses.get(ref)?.hash === hash) continue;
|
|
525
|
+
witnesses.set(ref, {
|
|
526
|
+
ref: deliveryRef(record),
|
|
486
527
|
hash
|
|
487
|
-
})), { concurrency: 128 });
|
|
488
|
-
for (const { ref, record, hash } of hashed) {
|
|
489
|
-
if (witnesses.get(ref)?.hash === hash) continue;
|
|
490
|
-
witnesses.set(ref, {
|
|
491
|
-
ref: deliveryRef(record),
|
|
492
|
-
hash
|
|
493
|
-
});
|
|
494
|
-
patches.set(ref, {
|
|
495
|
-
kind: "put",
|
|
496
|
-
record
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
});
|
|
500
|
-
const emit = Effect.fnUntraced(function* (record) {
|
|
501
|
-
const ref = deliveryKey(deliveryRef(record));
|
|
502
|
-
const encoded = yield* Effect.try({
|
|
503
|
-
try: () => recordCodec.encode(record),
|
|
504
|
-
catch: () => failure("Selected data cannot be encoded.", "InvalidResponse")
|
|
505
528
|
});
|
|
506
|
-
const encodedBytes = new TextEncoder().encode(encoded);
|
|
507
|
-
const previousSize = seen.get(ref);
|
|
508
|
-
if (previousSize === void 0) count++;
|
|
509
|
-
bytes += encodedBytes.length - (previousSize ?? 0);
|
|
510
|
-
seen.set(ref, encodedBytes.length);
|
|
511
|
-
if (count > limits.maxRecords || bytes > limits.maxBytes) return yield* failure("Selected delivery exceeds its configured size budget.", "RateLimited");
|
|
512
|
-
const canonical = yield* Effect.try({
|
|
513
|
-
try: () => recordCodec.decode(encoded),
|
|
514
|
-
catch: () => failure("Selected data cannot be decoded.", "InvalidResponse")
|
|
515
|
-
});
|
|
516
|
-
buffered.set(ref, {
|
|
517
|
-
record: canonical,
|
|
518
|
-
encoded: encodedBytes
|
|
519
|
-
});
|
|
520
|
-
if (buffered.size === 128) yield* flush;
|
|
521
|
-
});
|
|
522
|
-
const captured = yield* options.capture(tx, (record) => boundary(emit(record))).pipe(Effect.withSpan("Delivery.capture"));
|
|
523
|
-
yield* flush;
|
|
524
|
-
const encodedMetadata = valueCodec.encode(captured);
|
|
525
|
-
if (new TextEncoder().encode(encodedMetadata).length > limits.frameBytes / 2) return yield* failure("Delivery metadata exceeds the frame budget.", "RateLimited");
|
|
526
|
-
const metadata = valueCodec.decode(encodedMetadata);
|
|
527
|
-
const { client: _client, ...identity } = metadata;
|
|
528
|
-
const nextSignature = valueCodec.encode(identity);
|
|
529
|
-
for (const [ref, witness] of witnesses) {
|
|
530
|
-
if (seen.has(ref)) continue;
|
|
531
529
|
patches.set(ref, {
|
|
532
|
-
kind: "
|
|
533
|
-
|
|
530
|
+
kind: "put",
|
|
531
|
+
record
|
|
534
532
|
});
|
|
535
|
-
witnesses.delete(ref);
|
|
536
533
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
};
|
|
544
|
-
yield* Effect.annotateCurrentSpan({
|
|
545
|
-
sequence,
|
|
546
|
-
changes: order,
|
|
547
|
-
records: count,
|
|
548
|
-
generation: metadata.generation,
|
|
549
|
-
revision: metadata.revision
|
|
534
|
+
});
|
|
535
|
+
const emit = Effect.fnUntraced(function* (record) {
|
|
536
|
+
const ref = deliveryKey(deliveryRef(record));
|
|
537
|
+
const encoded = yield* Effect.try({
|
|
538
|
+
try: () => recordCodec.encode(record),
|
|
539
|
+
catch: () => failure("Selected data cannot be encoded.", "InvalidResponse")
|
|
550
540
|
});
|
|
551
|
-
const
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
541
|
+
const encodedBytes = new TextEncoder().encode(encoded);
|
|
542
|
+
const previousSize = seen.get(ref);
|
|
543
|
+
if (previousSize === void 0) count++;
|
|
544
|
+
bytes += encodedBytes.length - (previousSize ?? 0);
|
|
545
|
+
seen.set(ref, encodedBytes.length);
|
|
546
|
+
if (count > limits.maxRecords || bytes > limits.maxBytes) return yield* failure("Selected delivery exceeds its configured size budget.", "RateLimited");
|
|
547
|
+
const canonical = yield* Effect.try({
|
|
548
|
+
try: () => recordCodec.decode(encoded),
|
|
549
|
+
catch: () => failure("Selected data cannot be decoded.", "InvalidResponse")
|
|
557
550
|
});
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
551
|
+
buffered.set(ref, {
|
|
552
|
+
record: canonical,
|
|
553
|
+
encoded: encodedBytes
|
|
554
|
+
});
|
|
555
|
+
if (buffered.size === 128) yield* flush;
|
|
556
|
+
});
|
|
557
|
+
const captured = yield* options.capture(tx, (record) => boundary(emit(record))).pipe(Effect.withSpan("Delivery.capture"));
|
|
558
|
+
yield* flush;
|
|
559
|
+
const encodedMetadata = valueCodec.encode(captured);
|
|
560
|
+
if (new TextEncoder().encode(encodedMetadata).length > limits.frameBytes / 2) return yield* failure("Delivery metadata exceeds the frame budget.", "RateLimited");
|
|
561
|
+
const metadata = valueCodec.decode(encodedMetadata);
|
|
562
|
+
const { client: _client, ...identity } = metadata;
|
|
563
|
+
const nextSignature = valueCodec.encode(identity);
|
|
564
|
+
for (const [ref, witness] of witnesses) {
|
|
565
|
+
if (seen.has(ref)) continue;
|
|
566
|
+
patches.set(ref, {
|
|
567
|
+
kind: "remove",
|
|
568
|
+
ref: witness.ref
|
|
569
|
+
});
|
|
570
|
+
witnesses.delete(ref);
|
|
575
571
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
572
|
+
const order = patches.size;
|
|
573
|
+
const pending = installed && order === 0 && signature === nextSignature ? void 0 : {
|
|
574
|
+
sequence,
|
|
575
|
+
metadata,
|
|
576
|
+
nextSignature,
|
|
577
|
+
count: order
|
|
578
|
+
};
|
|
579
|
+
yield* Effect.annotateCurrentSpan({
|
|
580
|
+
sequence,
|
|
581
|
+
changes: order,
|
|
582
|
+
records: count,
|
|
583
|
+
generation: metadata.generation,
|
|
584
|
+
revision: metadata.revision
|
|
585
|
+
});
|
|
586
|
+
const expiresAt = (yield* Clock.currentTimeMillis) + activeRetentionMs;
|
|
587
|
+
yield* tx.put(sessions, streamId, {
|
|
588
|
+
...session,
|
|
589
|
+
expiresAt,
|
|
590
|
+
pending,
|
|
591
|
+
expected: pending ? sequence : installed
|
|
592
|
+
});
|
|
593
|
+
const payload = [...patches.values()].map((change) => valueCodec.encode(change));
|
|
594
|
+
prepared = {
|
|
595
|
+
witnesses,
|
|
596
|
+
expiresAt,
|
|
597
|
+
pending: pending ? {
|
|
598
|
+
sequence,
|
|
599
|
+
changes: payload,
|
|
600
|
+
sizes: payload.map((encoded) => new TextEncoder().encode(encoded).length)
|
|
601
|
+
} : void 0
|
|
602
|
+
};
|
|
603
|
+
return pending;
|
|
604
|
+
}))));
|
|
605
|
+
if (prepared) {
|
|
606
|
+
if (closed || memories.get(streamId) !== memory || memory.producer !== producer) return yield* failure("Delivery view owner changed.", "HistoryExpired");
|
|
607
|
+
memory.witnesses = prepared.witnesses;
|
|
608
|
+
memory.pending = prepared.pending;
|
|
609
|
+
memory.expiresAt = prepared.expiresAt;
|
|
610
|
+
}
|
|
611
|
+
return result;
|
|
612
|
+
}))).pipe(Effect.withSpan("Delivery.prepare"));
|
|
613
|
+
const loop = () => Stream.unwrap(Effect.gen(function* () {
|
|
614
|
+
const next = yield* prepare;
|
|
615
|
+
if (!next) {
|
|
616
|
+
yield* waitForChange;
|
|
617
|
+
return Stream.suspend(loop);
|
|
618
|
+
}
|
|
619
|
+
let part = 0;
|
|
620
|
+
const frames = Stream.paginate(0, (start) => boundary(tables.read((tx) => Effect.gen(function* () {
|
|
621
|
+
const head = yield* current(tx);
|
|
622
|
+
const payload = memory.pending;
|
|
623
|
+
if (!payload || payload.sequence !== next.sequence) return yield* failure("Delivery view buffer expired.", "HistoryExpired");
|
|
624
|
+
const changes = [];
|
|
625
|
+
let bytes = 256;
|
|
626
|
+
let order = start;
|
|
627
|
+
while (order < next.count && changes.length < limits.frameRecords) {
|
|
628
|
+
const length = payload.sizes[order];
|
|
629
|
+
if (changes.length && bytes + length > limits.frameBytes) break;
|
|
630
|
+
changes.push(yield* Effect.try({
|
|
631
|
+
try: () => valueCodec.decode(payload.changes[order]),
|
|
632
|
+
catch: () => failure("Delivery buffer cannot be decoded.", "InvalidResponse")
|
|
633
|
+
}));
|
|
634
|
+
bytes += length;
|
|
635
|
+
order++;
|
|
583
636
|
}
|
|
584
|
-
|
|
585
|
-
|
|
637
|
+
const end = order === next.count;
|
|
638
|
+
return [[{
|
|
639
|
+
streamId,
|
|
640
|
+
sequence: next.sequence,
|
|
641
|
+
previous: head.installed,
|
|
642
|
+
part: part++,
|
|
643
|
+
kind: head.installed ? "patch" : "snapshot",
|
|
644
|
+
changes,
|
|
645
|
+
end,
|
|
646
|
+
...end ? { metadata: next.metadata } : {}
|
|
647
|
+
}], end ? Option.none() : Option.some(order)];
|
|
648
|
+
}))));
|
|
649
|
+
const advance = Effect.gen(function* () {
|
|
650
|
+
for (;;) {
|
|
651
|
+
if ((yield* boundary(tables.read((tx) => current(tx)))).acknowledged >= next.sequence) return;
|
|
652
|
+
yield* Effect.raceFirst(Queue.take(memory.acknowledgments), Effect.sleep(250));
|
|
653
|
+
}
|
|
654
|
+
}).pipe(Effect.withSpan("Delivery.awaitAcknowledgment", { attributes: {
|
|
655
|
+
streamId,
|
|
656
|
+
sequence: next.sequence
|
|
657
|
+
} })).pipe(Effect.timeoutOrElse({
|
|
658
|
+
duration: limits.ackTimeoutMs,
|
|
659
|
+
orElse: () => Effect.fail(failure("Delivery acknowledgment timed out."))
|
|
660
|
+
}), Effect.andThen(memory.gate.withPermits(1)(Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
661
|
+
const expiresAt = yield* restore(boundary(tables.transaction((tx) => Effect.gen(function* () {
|
|
586
662
|
const head = yield* current(tx);
|
|
587
|
-
const
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
try: () => valueCodec.decode(payload.changes[order]),
|
|
597
|
-
catch: () => failure("Delivery buffer cannot be decoded.", "InvalidResponse")
|
|
598
|
-
}));
|
|
599
|
-
bytes += length;
|
|
600
|
-
order++;
|
|
601
|
-
}
|
|
602
|
-
const end = order === next.count;
|
|
603
|
-
return [[{
|
|
604
|
-
streamId,
|
|
605
|
-
sequence: next.sequence,
|
|
606
|
-
previous: head.installed,
|
|
607
|
-
part: part++,
|
|
608
|
-
kind: head.installed ? "patch" : "snapshot",
|
|
609
|
-
changes,
|
|
610
|
-
end,
|
|
611
|
-
...end ? { metadata: next.metadata } : {}
|
|
612
|
-
}], end ? Option.none() : Option.some(order)];
|
|
663
|
+
const expiresAt = (yield* Clock.currentTimeMillis) + activeRetentionMs;
|
|
664
|
+
yield* tx.put(sessions, streamId, {
|
|
665
|
+
...head,
|
|
666
|
+
installed: next.sequence,
|
|
667
|
+
signature: next.nextSignature,
|
|
668
|
+
pending: void 0,
|
|
669
|
+
expiresAt
|
|
670
|
+
});
|
|
671
|
+
return expiresAt;
|
|
613
672
|
}))));
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
673
|
+
memory.pending = void 0;
|
|
674
|
+
memory.expiresAt = expiresAt;
|
|
675
|
+
})))), Effect.andThen(waitForChange));
|
|
676
|
+
return Stream.concat(Stream.concat(frames, Stream.fromEffect(advance).pipe(Stream.drain)), Stream.suspend(loop));
|
|
677
|
+
}));
|
|
678
|
+
return {
|
|
679
|
+
frames: Stream.suspend(() => {
|
|
680
|
+
if (consumed) return Stream.fail(failure("A delivery subscription has one consumer.", "Conflict"));
|
|
681
|
+
consumed = true;
|
|
682
|
+
return loop();
|
|
683
|
+
}).pipe(Stream.interruptWhen(Deferred.await(gone))),
|
|
684
|
+
acknowledge: (position) => position.streamId === streamId ? acknowledge(position) : Effect.fail(failure("Invalid delivery acknowledgment.", "Conflict"))
|
|
685
|
+
};
|
|
686
|
+
});
|
|
687
|
+
const invalidate = (types) => admission.withPermits(1)(Effect.gen(function* () {
|
|
688
|
+
for (const [stream, memory] of [...memories]) {
|
|
689
|
+
if (types === "all") {
|
|
690
|
+
yield* forget(stream);
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
const pending = memory.pending?.changes.map(refOf) ?? [];
|
|
694
|
+
if (pending.includes(void 0)) {
|
|
695
|
+
yield* forget(stream);
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
const changed = (ref) => "objectType" in ref && types.has(ref.objectType);
|
|
699
|
+
yield* memory.gate.withPermits(1)(Effect.sync(() => {
|
|
700
|
+
for (const [key, witness] of memory.witnesses) if (changed(witness.ref)) memory.witnesses.set(key, {
|
|
701
|
+
ref: witness.ref,
|
|
702
|
+
hash: ""
|
|
703
|
+
});
|
|
704
|
+
if (pending.some((ref) => changed(ref))) memory.redefined = true;
|
|
642
705
|
}));
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
return loop();
|
|
648
|
-
}).pipe(Stream.interruptWhen(Deferred.await(gone))),
|
|
649
|
-
acknowledge: (position) => position.streamId === streamId ? acknowledge(position) : Effect.fail(failure("Invalid delivery acknowledgment.", "Conflict"))
|
|
650
|
-
};
|
|
651
|
-
}),
|
|
706
|
+
}
|
|
707
|
+
}));
|
|
708
|
+
return {
|
|
709
|
+
open,
|
|
652
710
|
acknowledge,
|
|
653
|
-
collectGarbage: admission.withPermits(1)(prune().pipe(Effect.andThen(collectDeliveryGarbage(tables, namespace))))
|
|
711
|
+
collectGarbage: admission.withPermits(1)(prune().pipe(Effect.andThen(collectDeliveryGarbage(tables, namespace)))),
|
|
712
|
+
invalidate
|
|
654
713
|
};
|
|
655
714
|
});
|
|
656
715
|
const bufferedFrameHash = (encoded) => boundary(digestBytes({
|