durability 5.2.0 → 5.2.1
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.md +9 -9
- package/dist/alarms/index.js +18 -21
- package/dist/fanout/index.js +39 -39
- package/dist/log/index.d.ts +12 -17
- package/dist/log/index.js +59 -53
- package/dist/memoize/index.js +9 -10
- package/dist/migrations.js +12 -16
- package/dist/operations/index.js +22 -28
- package/dist/outbox/index.js +19 -17
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/schedule/index.js +34 -36
- package/dist/scheduler/index.js +13 -12
- package/dist/semaphore/client.js +6 -5
- package/dist/semaphore/protocol.d.ts +446 -234
- package/dist/semaphore/protocol.js +46 -61
- package/dist/semaphore/queue-store.d.ts +58 -52
- package/dist/semaphore/queue-store.js +55 -57
- package/dist/semaphore/server.js +5 -4
- package/dist/semaphore/store.d.ts +30 -25
- package/dist/semaphore/store.js +31 -34
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/storage.js +9 -9
- package/dist/topology/index.js +16 -16
- package/dist/topology/snapshot.d.ts +20 -40
- package/dist/topology/snapshot.js +18 -28
- package/dist/tracker/index.js +7 -7
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -623,14 +623,14 @@ import {
|
|
|
623
623
|
type DurabilityLifecycleEvent,
|
|
624
624
|
} from 'durability';
|
|
625
625
|
import { DurabilitySocket, event, type SocketCall } from 'durability/socket';
|
|
626
|
-
import
|
|
626
|
+
import * as v from 'valibot';
|
|
627
627
|
|
|
628
|
-
const TodoSchema =
|
|
629
|
-
id:
|
|
630
|
-
title:
|
|
631
|
-
done:
|
|
628
|
+
const TodoSchema = v.object({
|
|
629
|
+
id: v.string(),
|
|
630
|
+
title: v.string(),
|
|
631
|
+
done: v.boolean(),
|
|
632
632
|
});
|
|
633
|
-
type Todo =
|
|
633
|
+
type Todo = v.InferOutput<typeof TodoSchema>;
|
|
634
634
|
type Metadata = { userId: string };
|
|
635
635
|
|
|
636
636
|
export class TodoList extends DurableObject<Env> {
|
|
@@ -655,11 +655,11 @@ export class TodoList extends DurableObject<Env> {
|
|
|
655
655
|
list: () => [...this.todos.values()],
|
|
656
656
|
// Typed input requires a Standard Schema; here the handler is a durable operation.
|
|
657
657
|
createTodo: {
|
|
658
|
-
input:
|
|
658
|
+
input: v.object({ id: v.string(), payload: TodoSchema }),
|
|
659
659
|
handle: this.durability.createTodo,
|
|
660
660
|
},
|
|
661
661
|
toggle: {
|
|
662
|
-
input:
|
|
662
|
+
input: v.string(),
|
|
663
663
|
handle: (id: string, call: SocketCall<Metadata>) => {
|
|
664
664
|
const todo = this.todos.get(id);
|
|
665
665
|
if (!todo) {
|
|
@@ -851,7 +851,7 @@ readonly socket = new DurabilitySocket({
|
|
|
851
851
|
context: this.ctx,
|
|
852
852
|
inbound: {
|
|
853
853
|
work: {
|
|
854
|
-
input:
|
|
854
|
+
input: v.object({ cursor: v.optional(v.string()) }),
|
|
855
855
|
handle: (query: { cursor?: string }) => this.tracker.records(query),
|
|
856
856
|
},
|
|
857
857
|
},
|
package/dist/alarms/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilityAlarmsCore_instances, _DurabilityAlarmsCore_engine, _DurabilityAlarmsCore_channel, _DurabilityAlarmsCore_table, _DurabilityAlarmsCore_handlers, _DurabilityAlarmsCore_policy, _DurabilityAlarmsCore_lifecycle, _DurabilityAlarmsCore_emit, _DurabilityAlarmsCore_active, _DurabilityAlarmsCore_trackedSource, _DurabilityAlarmsCore_runDue, _DurabilityAlarmsCore_schedule, _DurabilityAlarmsCore_cancel, _DurabilityAlarmsCore_execute;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
15
|
import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from '../errors.js';
|
|
16
16
|
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
@@ -22,23 +22,21 @@ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js
|
|
|
22
22
|
import { createMemoize, deleteMemos, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
|
|
23
23
|
import { deleteAttempt, listCreated } from '../storage.js';
|
|
24
24
|
import { registerTrackedSource, } from '../tracker/source.js';
|
|
25
|
-
const alarmRowSchema =
|
|
26
|
-
name:
|
|
27
|
-
channel:
|
|
28
|
-
generation_id:
|
|
29
|
-
status:
|
|
30
|
-
scheduled_at:
|
|
31
|
-
next_attempt_at:
|
|
32
|
-
attempt:
|
|
33
|
-
last_error:
|
|
34
|
-
last_error_name:
|
|
35
|
-
created_at:
|
|
25
|
+
const alarmRowSchema = v.object({
|
|
26
|
+
name: v.string(),
|
|
27
|
+
channel: v.string(),
|
|
28
|
+
generation_id: v.string(),
|
|
29
|
+
status: v.picklist(['pending', 'failed']),
|
|
30
|
+
scheduled_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
31
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
|
+
last_error: v.nullable(v.string()),
|
|
34
|
+
last_error_name: v.nullable(v.string()),
|
|
35
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
36
36
|
});
|
|
37
37
|
const alarmsTable = { name: 'durability_alarms', key: 'name' };
|
|
38
|
-
const alarmHandlerSchema =
|
|
39
|
-
const readAlarm = (sql, channel, name) => alarmRowSchema
|
|
40
|
-
.optional()
|
|
41
|
-
.parse(sql
|
|
38
|
+
const alarmHandlerSchema = v.custom((value) => typeof value === 'function');
|
|
39
|
+
const readAlarm = (sql, channel, name) => v.parse(v.optional(alarmRowSchema), sql
|
|
42
40
|
.exec('SELECT * FROM durability_alarms WHERE channel = ? AND name = ?', channel, name)
|
|
43
41
|
.toArray()[0]);
|
|
44
42
|
const alarmRecord = (sql, row) => ({
|
|
@@ -83,7 +81,7 @@ class DurabilityAlarmsCore {
|
|
|
83
81
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_channel, assertChannel('DurabilityAlarms', config.channel ?? defaultChannel), "f");
|
|
84
82
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_table, { ...alarmsTable, channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f") }, "f");
|
|
85
83
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_engine, engineFor(config), "f");
|
|
86
|
-
__classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name,
|
|
84
|
+
__classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name, v.parse(alarmHandlerSchema, handler)])), "f");
|
|
87
85
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_policy, createPolicyResolver(config, config.methods ?? {}, alarmMethodOptionsSchema), "f");
|
|
88
86
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_lifecycle, createLifecycleHooks((hook) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").emitter(hook), config.onLifecycleEvent), "f");
|
|
89
87
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_emit, __classPrivateFieldGet(this, _DurabilityAlarmsCore_lifecycle, "f").emit, "f");
|
|
@@ -157,9 +155,8 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = ne
|
|
|
157
155
|
status,
|
|
158
156
|
name,
|
|
159
157
|
});
|
|
160
|
-
const records =
|
|
161
|
-
.array()
|
|
162
|
-
.parse(page.rows)
|
|
158
|
+
const records = v
|
|
159
|
+
.parse(v.array(alarmRowSchema), page.rows)
|
|
163
160
|
.map((row) => alarmRecord(sql, row));
|
|
164
161
|
return page.next === undefined
|
|
165
162
|
? { records }
|
|
@@ -182,7 +179,7 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = ne
|
|
|
182
179
|
},
|
|
183
180
|
};
|
|
184
181
|
}, _DurabilityAlarmsCore_runDue = async function _DurabilityAlarmsCore_runDue(now, info) {
|
|
185
|
-
const due =
|
|
182
|
+
const due = v.parse(v.array(alarmRowSchema), __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql
|
|
186
183
|
.exec(`SELECT * FROM durability_alarms
|
|
187
184
|
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
188
185
|
ORDER BY next_attempt_at ASC
|
package/dist/fanout/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilityFanout_instances, _DurabilityFanout_engine, _DurabilityFanout_channel, _DurabilityFanout_table, _DurabilityFanout_routing, _DurabilityFanout_targets, _DurabilityFanout_policies, _DurabilityFanout_dlq, _DurabilityFanout_maxBatchSize, _DurabilityFanout_batchDelayMs, _DurabilityFanout_lifecycle, _DurabilityFanout_emit, _DurabilityFanout_running, _DurabilityFanout_pending, _DurabilityFanout_firstAttemptAt, _DurabilityFanout_runDue, _DurabilityFanout_runTarget, _DurabilityFanout_postpone, _DurabilityFanout_deliver, _DurabilityFanout_ackUnsettled, _DurabilityFanout_message, _DurabilityFanout_remove, _DurabilityFanout_removeDelivery, _DurabilityFanout_retry, _DurabilityFanout_deadLetter, _DurabilityFanout_emitTerminal, _DurabilityFanout_transaction, _DurabilityFanout_pendingCount, _DurabilityFanout_adjustPending, _DurabilityFanout_listDue, _DurabilityFanout_deadline;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
15
|
import { DurableRetryPolicyError, FanoutEnqueueError, FanoutSettlementError, FanoutTimeoutError, reportFailure, serializeError, } from '../errors.js';
|
|
16
16
|
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
@@ -23,41 +23,43 @@ import { assertPositiveInteger, createPolicyResolver, methodOptionsSchema, } fro
|
|
|
23
23
|
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
24
24
|
import { deserialize, serialize } from '../serialization.js';
|
|
25
25
|
import { claimAttempt, deleteAttempt, updateAttempt, } from '../storage.js';
|
|
26
|
-
const fanoutMessageRowSchema =
|
|
27
|
-
key:
|
|
28
|
-
id:
|
|
29
|
-
payload:
|
|
30
|
-
targets:
|
|
31
|
-
remaining:
|
|
32
|
-
seq:
|
|
33
|
-
created_at:
|
|
34
|
-
generation_id:
|
|
26
|
+
const fanoutMessageRowSchema = v.object({
|
|
27
|
+
key: v.string(),
|
|
28
|
+
id: v.string(),
|
|
29
|
+
payload: v.string(),
|
|
30
|
+
targets: v.string(),
|
|
31
|
+
remaining: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
34
|
+
generation_id: v.string(),
|
|
35
35
|
});
|
|
36
|
-
const deliveryRowSchema =
|
|
37
|
-
id:
|
|
38
|
-
message_key:
|
|
39
|
-
target_id:
|
|
40
|
-
seq:
|
|
41
|
-
status:
|
|
42
|
-
phase:
|
|
43
|
-
attempt:
|
|
44
|
-
next_attempt_at:
|
|
45
|
-
last_error:
|
|
46
|
-
last_error_name:
|
|
47
|
-
created_at:
|
|
48
|
-
generation_id:
|
|
49
|
-
dead_lettered_at:
|
|
50
|
-
dead_letter_reason:
|
|
36
|
+
const deliveryRowSchema = v.object({
|
|
37
|
+
id: v.string(),
|
|
38
|
+
message_key: v.string(),
|
|
39
|
+
target_id: v.string(),
|
|
40
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
41
|
+
status: v.literal('pending'),
|
|
42
|
+
phase: v.picklist(['delivery', 'dead_letter']),
|
|
43
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
44
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
45
|
+
last_error: v.nullable(v.string()),
|
|
46
|
+
last_error_name: v.nullable(v.string()),
|
|
47
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
48
|
+
generation_id: v.string(),
|
|
49
|
+
dead_lettered_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
50
|
+
dead_letter_reason: v.nullable(v.picklist(['explicit', 'exhausted'])),
|
|
51
51
|
});
|
|
52
52
|
const deliveriesTable = {
|
|
53
53
|
name: 'durability_fanout_deliveries',
|
|
54
54
|
key: 'id',
|
|
55
55
|
};
|
|
56
|
-
const countRowSchema =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
.
|
|
56
|
+
const countRowSchema = v.object({
|
|
57
|
+
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
58
|
+
});
|
|
59
|
+
const seqRowSchema = v.object({
|
|
60
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
61
|
+
});
|
|
62
|
+
const readManifest = (sql, channel, key) => v.parse(v.optional(fanoutMessageRowSchema), sql
|
|
61
63
|
.exec('SELECT * FROM durability_fanout_messages WHERE channel = ? AND key = ?', channel, key)
|
|
62
64
|
.toArray()[0]);
|
|
63
65
|
const attemptOf = (row) => ({
|
|
@@ -204,7 +206,7 @@ export class DurabilityFanout {
|
|
|
204
206
|
}
|
|
205
207
|
continue;
|
|
206
208
|
}
|
|
207
|
-
const { seq } =
|
|
209
|
+
const { seq } = v.parse(seqRowSchema, sql
|
|
208
210
|
.exec(`UPDATE durability_fanout_seq
|
|
209
211
|
SET next_seq = next_seq + 1
|
|
210
212
|
WHERE id = 1
|
|
@@ -297,10 +299,8 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
297
299
|
}
|
|
298
300
|
return Math.ceil(createdAt / __classPrivateFieldGet(this, _DurabilityFanout_batchDelayMs, "f")) * __classPrivateFieldGet(this, _DurabilityFanout_batchDelayMs, "f");
|
|
299
301
|
}, _DurabilityFanout_runDue = async function _DurabilityFanout_runDue() {
|
|
300
|
-
const targets =
|
|
301
|
-
.object({ target_id:
|
|
302
|
-
.array()
|
|
303
|
-
.parse(__classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
302
|
+
const targets = v
|
|
303
|
+
.parse(v.array(v.object({ target_id: v.string() })), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
304
304
|
.exec(`SELECT DISTINCT target_id FROM durability_fanout_deliveries
|
|
305
305
|
WHERE channel = ?
|
|
306
306
|
ORDER BY target_id`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"))
|
|
@@ -457,7 +457,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
457
457
|
if (!state.finished) {
|
|
458
458
|
continue;
|
|
459
459
|
}
|
|
460
|
-
const current =
|
|
460
|
+
const current = v.parse(v.optional(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
461
461
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
462
462
|
WHERE channel = ? AND id = ?`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"), state.claim.row.id)
|
|
463
463
|
.toArray()[0]);
|
|
@@ -678,7 +678,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
678
678
|
throw error;
|
|
679
679
|
}
|
|
680
680
|
}, _DurabilityFanout_pendingCount = function _DurabilityFanout_pendingCount() {
|
|
681
|
-
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") ??
|
|
681
|
+
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") ?? v.parse(countRowSchema, __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
682
682
|
.exec(`SELECT COUNT(*) AS count FROM durability_fanout_deliveries
|
|
683
683
|
WHERE channel = ? AND status = 'pending'`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"))
|
|
684
684
|
.toArray()[0]).count, "f");
|
|
@@ -688,7 +688,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
688
688
|
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") + delta, "f");
|
|
689
689
|
}
|
|
690
690
|
}, _DurabilityFanout_listDue = function _DurabilityFanout_listDue(target, now) {
|
|
691
|
-
const first =
|
|
691
|
+
const first = v.parse(v.array(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
692
692
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
693
693
|
WHERE channel = ? AND target_id = ? AND phase = 'delivery'
|
|
694
694
|
AND status = 'pending' AND attempt = 0 AND next_attempt_at <= ?
|
|
@@ -698,7 +698,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
698
698
|
if (first.length >= __classPrivateFieldGet(this, _DurabilityFanout_maxBatchSize, "f")) {
|
|
699
699
|
return first;
|
|
700
700
|
}
|
|
701
|
-
const retries =
|
|
701
|
+
const retries = v.parse(v.array(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
702
702
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
703
703
|
WHERE channel = ? AND target_id = ? AND status = 'pending'
|
|
704
704
|
AND attempt > 0 AND next_attempt_at <= ?
|
package/dist/log/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { RoutingLoad, type EnqueueResult, type LoadSnapshot } from '../routing/load.js';
|
|
3
3
|
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
4
4
|
/**
|
|
@@ -6,22 +6,17 @@ import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
|
6
6
|
* redelivered under a fresh `batch_id`, so an ack from a previous holder is
|
|
7
7
|
* rejected rather than advancing a range someone else now owns.
|
|
8
8
|
*/
|
|
9
|
-
declare const logLeaseRowSchema:
|
|
10
|
-
batch_id:
|
|
11
|
-
consumer:
|
|
12
|
-
first_offset:
|
|
13
|
-
last_offset:
|
|
14
|
-
state:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
attempt: z.ZodNumber;
|
|
21
|
-
expires_at: z.ZodNumber;
|
|
22
|
-
created_at: z.ZodNumber;
|
|
23
|
-
}, z.core.$strip>;
|
|
24
|
-
export type LogLeaseRow = z.infer<typeof logLeaseRowSchema>;
|
|
9
|
+
declare const logLeaseRowSchema: v.ObjectSchema<{
|
|
10
|
+
readonly batch_id: v.StringSchema<undefined>;
|
|
11
|
+
readonly consumer: v.StringSchema<undefined>;
|
|
12
|
+
readonly first_offset: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
13
|
+
readonly last_offset: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
14
|
+
readonly state: v.PicklistSchema<["active", "pending", "acked", "skipped"], undefined>;
|
|
15
|
+
readonly attempt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
|
|
16
|
+
readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
17
|
+
readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
18
|
+
}, undefined>;
|
|
19
|
+
export type LogLeaseRow = v.InferOutput<typeof logLeaseRowSchema>;
|
|
25
20
|
export type LogBounds = {
|
|
26
21
|
/**
|
|
27
22
|
* Lowest readable offset, counting flushed segments that can be rehydrated.
|
package/dist/log/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilityLog_instances, _DurabilityLog_engine, _DurabilityLog_channel, _DurabilityLog_routing, _DurabilityLog_retention, _DurabilityLog_cold, _DurabilityLog_leases, _DurabilityLog_maxParallelism, _DurabilityLog_leaseMs, _DurabilityLog_maxBatchSize, _DurabilityLog_encoder, _DurabilityLog_rehydrate, _DurabilityLog_record, _DurabilityLog_settle, _DurabilityLog_dropTrimmed, _DurabilityLog_advance, _DurabilityLog_between, _DurabilityLog_readFrom, _DurabilityLog_requireLeases;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceMethods } from '../dev.js';
|
|
15
15
|
import { LogAppendError, LogLeaseError, LogTruncatedError, reportFailure, serializeError, } from '../errors.js';
|
|
16
16
|
import { RoutingLoad, } from '../routing/load.js';
|
|
@@ -19,54 +19,60 @@ import { migrateCapability } from '../migrations.js';
|
|
|
19
19
|
import { assertPositiveInteger } from '../policy.js';
|
|
20
20
|
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
21
21
|
import { deserialize, serialize } from '../serialization.js';
|
|
22
|
-
const logRecordRowSchema =
|
|
23
|
-
offset:
|
|
24
|
-
dedup_key:
|
|
25
|
-
payload:
|
|
26
|
-
bytes:
|
|
27
|
-
appended_at:
|
|
22
|
+
const logRecordRowSchema = v.object({
|
|
23
|
+
offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
24
|
+
dedup_key: v.string(),
|
|
25
|
+
payload: v.string(),
|
|
26
|
+
bytes: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
27
|
+
appended_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
28
28
|
});
|
|
29
|
-
const logCursorRowSchema =
|
|
30
|
-
consumer:
|
|
31
|
-
offset:
|
|
32
|
-
committed_at:
|
|
29
|
+
const logCursorRowSchema = v.object({
|
|
30
|
+
consumer: v.string(),
|
|
31
|
+
offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
committed_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
33
|
});
|
|
34
34
|
/** One contiguous run of records moved out of the object into cold storage. */
|
|
35
|
-
const logSegmentRowSchema =
|
|
36
|
-
first_offset:
|
|
37
|
-
last_offset:
|
|
38
|
-
locator:
|
|
39
|
-
bytes:
|
|
40
|
-
flushed_at:
|
|
35
|
+
const logSegmentRowSchema = v.object({
|
|
36
|
+
first_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
37
|
+
last_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
38
|
+
locator: v.string(),
|
|
39
|
+
bytes: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
40
|
+
flushed_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
41
41
|
});
|
|
42
42
|
/**
|
|
43
43
|
* One in-flight or settled range of offsets held by a consumer. A range is
|
|
44
44
|
* redelivered under a fresh `batch_id`, so an ack from a previous holder is
|
|
45
45
|
* rejected rather than advancing a range someone else now owns.
|
|
46
46
|
*/
|
|
47
|
-
const logLeaseRowSchema =
|
|
48
|
-
batch_id:
|
|
49
|
-
consumer:
|
|
50
|
-
first_offset:
|
|
51
|
-
last_offset:
|
|
52
|
-
state:
|
|
53
|
-
attempt:
|
|
54
|
-
expires_at:
|
|
55
|
-
created_at:
|
|
47
|
+
const logLeaseRowSchema = v.object({
|
|
48
|
+
batch_id: v.string(),
|
|
49
|
+
consumer: v.string(),
|
|
50
|
+
first_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
51
|
+
last_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
52
|
+
state: v.picklist(['active', 'pending', 'acked', 'skipped']),
|
|
53
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
54
|
+
expires_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
55
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
56
56
|
});
|
|
57
|
-
const allocationRowSchema =
|
|
58
|
-
allocated_offset:
|
|
57
|
+
const allocationRowSchema = v.object({
|
|
58
|
+
allocated_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
59
59
|
});
|
|
60
|
-
const offsetRowSchema =
|
|
61
|
-
|
|
62
|
-
const countRowSchema = z.object({ count: z.number().int().nonnegative() });
|
|
63
|
-
const totalBytesRowSchema = z.object({
|
|
64
|
-
total_bytes: z.number().int().nonnegative(),
|
|
60
|
+
const offsetRowSchema = v.object({
|
|
61
|
+
offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
65
62
|
});
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
const seqRowSchema = v.object({
|
|
64
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
65
|
+
});
|
|
66
|
+
const countRowSchema = v.object({
|
|
67
|
+
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
68
|
+
});
|
|
69
|
+
const totalBytesRowSchema = v.object({
|
|
70
|
+
total_bytes: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
71
|
+
});
|
|
72
|
+
const boundsRowSchema = v.object({
|
|
73
|
+
hot: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
74
|
+
cold: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
75
|
+
next_offset: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
70
76
|
});
|
|
71
77
|
/**
|
|
72
78
|
* Inserts the batch and returns each record's offset in input order. A record
|
|
@@ -77,7 +83,7 @@ const boundsRowSchema = z.object({
|
|
|
77
83
|
const appendRecords = (sql, channel, records, appendedAt) => {
|
|
78
84
|
const assigned = new Map();
|
|
79
85
|
for (const record of records) {
|
|
80
|
-
const existing =
|
|
86
|
+
const existing = v.parse(v.optional(offsetRowSchema), sql
|
|
81
87
|
.exec(`SELECT "offset" FROM durability_log_records
|
|
82
88
|
WHERE channel = ? AND dedup_key = ? LIMIT 1`, channel, record.dedup_key)
|
|
83
89
|
.toArray()[0]);
|
|
@@ -91,7 +97,7 @@ const appendRecords = (sql, channel, records, appendedAt) => {
|
|
|
91
97
|
return records.map((record) => assigned.get(record.dedup_key));
|
|
92
98
|
}
|
|
93
99
|
// A channel's counter row is created by its first append.
|
|
94
|
-
const first =
|
|
100
|
+
const first = v.parse(seqRowSchema, sql
|
|
95
101
|
.exec(`INSERT INTO durability_log_seq (channel, next_offset, total_bytes)
|
|
96
102
|
VALUES (?, ?, 0)
|
|
97
103
|
ON CONFLICT(channel) DO UPDATE
|
|
@@ -112,14 +118,14 @@ const appendRecords = (sql, channel, records, appendedAt) => {
|
|
|
112
118
|
WHERE channel = ?`, added, channel);
|
|
113
119
|
return records.map((record) => assigned.get(record.dedup_key));
|
|
114
120
|
};
|
|
115
|
-
const readRecords = (sql, channel, from, limit) =>
|
|
121
|
+
const readRecords = (sql, channel, from, limit) => v.parse(v.array(logRecordRowSchema), sql
|
|
116
122
|
.exec(`SELECT * FROM durability_log_records
|
|
117
123
|
WHERE channel = ? AND "offset" >= ?
|
|
118
124
|
ORDER BY "offset" ASC
|
|
119
125
|
LIMIT ?`, channel, from, limit)
|
|
120
126
|
.toArray());
|
|
121
127
|
const readBounds = (sql, channel) => {
|
|
122
|
-
const row =
|
|
128
|
+
const row = v.parse(boundsRowSchema, sql
|
|
123
129
|
.exec(`SELECT
|
|
124
130
|
(SELECT MIN("offset") FROM durability_log_records
|
|
125
131
|
WHERE channel = ?1) AS hot,
|
|
@@ -137,18 +143,18 @@ const readBounds = (sql, channel) => {
|
|
|
137
143
|
nextOffset: row.next_offset,
|
|
138
144
|
};
|
|
139
145
|
};
|
|
140
|
-
const countRecords = (sql, channel) =>
|
|
146
|
+
const countRecords = (sql, channel) => v.parse(countRowSchema, sql
|
|
141
147
|
.exec('SELECT COUNT(*) AS count FROM durability_log_records WHERE channel = ?', channel)
|
|
142
148
|
.toArray()[0]).count;
|
|
143
149
|
/** Bytes of record payloads held in the object, excluding flushed segments. */
|
|
144
|
-
const totalBytes = (sql, channel) =>
|
|
150
|
+
const totalBytes = (sql, channel) => v.parse(totalBytesRowSchema, sql
|
|
145
151
|
.exec(`SELECT COALESCE(
|
|
146
152
|
(SELECT total_bytes FROM durability_log_seq WHERE channel = ?),
|
|
147
153
|
0
|
|
148
154
|
) AS total_bytes`, channel)
|
|
149
155
|
.toArray()[0]).total_bytes;
|
|
150
156
|
/** Oldest retained records in offset order, for flush-or-delete decisions. */
|
|
151
|
-
const listOldest = (sql, channel, limit) =>
|
|
157
|
+
const listOldest = (sql, channel, limit) => v.parse(v.array(logRecordRowSchema), sql
|
|
152
158
|
.exec(`SELECT * FROM durability_log_records
|
|
153
159
|
WHERE channel = ?
|
|
154
160
|
ORDER BY "offset" ASC
|
|
@@ -159,7 +165,7 @@ const listOldest = (sql, channel, limit) => logRecordRowSchema.array().parse(sql
|
|
|
159
165
|
* how many were removed. Runs inside the caller's transaction.
|
|
160
166
|
*/
|
|
161
167
|
const trimThrough = (sql, channel, through, limit) => {
|
|
162
|
-
const removed =
|
|
168
|
+
const removed = v.parse(v.array(logRecordRowSchema), sql
|
|
163
169
|
.exec(`DELETE FROM durability_log_records
|
|
164
170
|
WHERE channel = ?1 AND "offset" IN (
|
|
165
171
|
SELECT "offset" FROM durability_log_records
|
|
@@ -188,17 +194,17 @@ const insertSegment = (sql, channel, row) => {
|
|
|
188
194
|
flushed_at = excluded.flushed_at`, channel, row.first_offset, row.last_offset, row.locator, row.bytes, row.flushed_at);
|
|
189
195
|
};
|
|
190
196
|
/** The segment holding an offset, if that offset was flushed. */
|
|
191
|
-
const findSegment = (sql, channel, offset) =>
|
|
197
|
+
const findSegment = (sql, channel, offset) => v.parse(v.optional(logSegmentRowSchema), sql
|
|
192
198
|
.exec(`SELECT * FROM durability_log_segments
|
|
193
199
|
WHERE channel = ?1 AND first_offset <= ?2 AND last_offset >= ?2
|
|
194
200
|
LIMIT 1`, channel, offset)
|
|
195
201
|
.toArray()[0]);
|
|
196
|
-
const readCursors = (sql, channel) =>
|
|
202
|
+
const readCursors = (sql, channel) => v.parse(v.array(logCursorRowSchema), sql
|
|
197
203
|
.exec(`SELECT * FROM durability_log_cursors
|
|
198
204
|
WHERE channel = ?
|
|
199
205
|
ORDER BY consumer`, channel)
|
|
200
206
|
.toArray());
|
|
201
|
-
const readCursor = (sql, channel, consumer) =>
|
|
207
|
+
const readCursor = (sql, channel, consumer) => v.parse(v.optional(offsetRowSchema), sql
|
|
202
208
|
.exec(`SELECT "offset" FROM durability_log_cursors
|
|
203
209
|
WHERE channel = ? AND consumer = ? LIMIT 1`, channel, consumer)
|
|
204
210
|
.toArray()[0])?.offset;
|
|
@@ -212,7 +218,7 @@ const commitCursor = (sql, channel, consumer, offset, committedAt) => {
|
|
|
212
218
|
WHERE excluded."offset" > durability_log_cursors."offset"`, channel, consumer, offset, committedAt);
|
|
213
219
|
};
|
|
214
220
|
/** Highest offset handed to a lease, which runs ahead of the commit. */
|
|
215
|
-
const readAllocation = (sql, channel, consumer) =>
|
|
221
|
+
const readAllocation = (sql, channel, consumer) => v.parse(v.optional(allocationRowSchema), sql
|
|
216
222
|
.exec(`SELECT allocated_offset FROM durability_log_allocations
|
|
217
223
|
WHERE channel = ? AND consumer = ? LIMIT 1`, channel, consumer)
|
|
218
224
|
.toArray()[0])?.allocated_offset;
|
|
@@ -233,7 +239,7 @@ const insertLease = (sql, channel, row) => {
|
|
|
233
239
|
* Oldest range owed redelivery: explicitly nacked, or held past its expiry.
|
|
234
240
|
* Claiming rewrites its `batch_id`, fencing the previous holder.
|
|
235
241
|
*/
|
|
236
|
-
const claimExpired = (sql, channel, consumer, now, batchId, expiresAt) =>
|
|
242
|
+
const claimExpired = (sql, channel, consumer, now, batchId, expiresAt) => v.parse(v.optional(logLeaseRowSchema), sql
|
|
237
243
|
.exec(`UPDATE durability_log_leases
|
|
238
244
|
SET batch_id = ?1, state = 'active', attempt = attempt + 1,
|
|
239
245
|
expires_at = ?2
|
|
@@ -249,7 +255,7 @@ const claimExpired = (sql, channel, consumer, now, batchId, expiresAt) => logLea
|
|
|
249
255
|
RETURNING *`, batchId, expiresAt, channel, consumer, now)
|
|
250
256
|
.toArray()[0]);
|
|
251
257
|
/** Ranges still held, so parallelism can be bounded per consumer. */
|
|
252
|
-
const countHeld = (sql, channel, consumer, now) =>
|
|
258
|
+
const countHeld = (sql, channel, consumer, now) => v.parse(countRowSchema, sql
|
|
253
259
|
.exec(`SELECT COUNT(*) AS count FROM durability_log_leases
|
|
254
260
|
WHERE channel = ?
|
|
255
261
|
AND consumer = ?
|
|
@@ -266,7 +272,7 @@ const settleLease = (sql, channel, batchId, state, expiresAt) => sql
|
|
|
266
272
|
* Ranges from `from` in offset order, so a commit can advance over the
|
|
267
273
|
* unbroken run of settled ones and no further.
|
|
268
274
|
*/
|
|
269
|
-
const leasesFrom = (sql, channel, consumer, from, limit) =>
|
|
275
|
+
const leasesFrom = (sql, channel, consumer, from, limit) => v.parse(v.array(logLeaseRowSchema), sql
|
|
270
276
|
.exec(`SELECT * FROM durability_log_leases
|
|
271
277
|
WHERE channel = ? AND consumer = ? AND last_offset >= ?
|
|
272
278
|
ORDER BY first_offset ASC
|
|
@@ -639,7 +645,7 @@ export class DurabilityLog {
|
|
|
639
645
|
/** In-flight and settled-but-uncommitted ranges, for operators and tests. */
|
|
640
646
|
async leases(consumer) {
|
|
641
647
|
__classPrivateFieldGet(this, _DurabilityLog_instances, "m", _DurabilityLog_requireLeases).call(this);
|
|
642
|
-
return
|
|
648
|
+
return v.parse(v.array(logLeaseRowSchema), __classPrivateFieldGet(this, _DurabilityLog_engine, "f").sql
|
|
643
649
|
.exec(`SELECT * FROM durability_log_leases
|
|
644
650
|
WHERE channel = ? AND consumer = ?
|
|
645
651
|
ORDER BY first_offset ASC`, __classPrivateFieldGet(this, _DurabilityLog_channel, "f"), consumer)
|
package/dist/memoize/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { traceFunction } from '../dev.js';
|
|
3
3
|
import { DurableMemoLimitError, DurableMemoSerializationError, } from '../errors.js';
|
|
4
4
|
import { deserialize, serialize } from '../serialization.js';
|
|
@@ -7,10 +7,10 @@ import { channelBindings, pendingGeneration, tableOwner, } from '../storage.js';
|
|
|
7
7
|
export const maxMemosPerRecord = 1000;
|
|
8
8
|
/** The largest serialized memoized result, in UTF-8 bytes. */
|
|
9
9
|
export const maxMemoBytes = 1048576;
|
|
10
|
-
const memoLookupSchema =
|
|
11
|
-
result:
|
|
12
|
-
owned:
|
|
13
|
-
saved:
|
|
10
|
+
const memoLookupSchema = v.object({
|
|
11
|
+
result: v.nullable(v.string()),
|
|
12
|
+
owned: v.pipe(v.number(), v.safeInteger()),
|
|
13
|
+
saved: v.pipe(v.number(), v.safeInteger()),
|
|
14
14
|
});
|
|
15
15
|
const supersededError = (key) => new Error(`Memoized key ${JSON.stringify(key)} belongs to a superseded attempt`);
|
|
16
16
|
/** Memoization for one attempt; only that attempt may persist new results. */
|
|
@@ -53,7 +53,7 @@ export const createMemoize = (sql, table, ref) => {
|
|
|
53
53
|
if (typeof key !== 'string' || key === '') {
|
|
54
54
|
throw new TypeError('Memoize keys must be non-empty strings');
|
|
55
55
|
}
|
|
56
|
-
const lookup =
|
|
56
|
+
const lookup = v.parse(memoLookupSchema, sql
|
|
57
57
|
.exec(`SELECT
|
|
58
58
|
(SELECT result FROM durability_memos
|
|
59
59
|
WHERE owner = ? AND key = ? AND generation_id = ? AND memo_key = ?)
|
|
@@ -97,11 +97,10 @@ export const createMemoize = (sql, table, ref) => {
|
|
|
97
97
|
memoKey: key,
|
|
98
98
|
})();
|
|
99
99
|
};
|
|
100
|
-
const memoKeySchema =
|
|
100
|
+
const memoKeySchema = v.object({ memo_key: v.string() });
|
|
101
101
|
/** Keys a record's current generation has results for, in the order they were saved. */
|
|
102
|
-
export const memoizedKeys = (sql, table, key, generation) =>
|
|
103
|
-
.array()
|
|
104
|
-
.parse(sql
|
|
102
|
+
export const memoizedKeys = (sql, table, key, generation) => v
|
|
103
|
+
.parse(v.array(memoKeySchema), sql
|
|
105
104
|
.exec(`SELECT memo_key FROM durability_memos
|
|
106
105
|
WHERE owner = ? AND key = ? AND generation_id = ?
|
|
107
106
|
ORDER BY rowid`, tableOwner(table), key, generation)
|