durability 2.0.0 → 3.0.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/dist/index.js CHANGED
@@ -1,551 +1,7 @@
1
- import { z } from 'zod';
2
- import { DOQB } from 'workers-qb';
3
- import { exponential, jitter } from './utils.js';
4
- const tableName = 'durability_calls';
5
- const migrationTableName = 'durability_migrations';
6
- const storedValueSchema = z.discriminatedUnion('kind', [
7
- z.object({ kind: z.literal('value'), value: z.json() }),
8
- z.object({ kind: z.literal('undefined') }),
9
- ]);
10
- /**
11
- * Ordered SQLite migrations for the tables owned by durability.
12
- *
13
- * Every SQLite-backed Durable Object instance has an independent database, so
14
- * the durability schema must exist in each instance before calls can be stored.
15
- * {@link createDurability} applies these migrations automatically on instance
16
- * initialization. They are tracked in the namespaced `durability_migrations`
17
- * table so they can coexist with application-owned migrations in the same
18
- * database.
19
- *
20
- * The list is exported so migration names can be inspected before an explicit
21
- * rollback with {@link migrateDurability}. Most consumers should not execute
22
- * the SQL directly.
23
- *
24
- * @example
25
- * ```ts
26
- * const currentTarget = durabilityMigrations.at(-1)?.name ?? null;
27
- * migrateDurability(this.ctx, currentTarget);
28
- * ```
29
- */
30
- export const durabilityMigrations = [
31
- {
32
- name: 'durability_0001_create_calls',
33
- up: `
34
- CREATE TABLE IF NOT EXISTS durability_calls (
35
- id TEXT PRIMARY KEY,
36
- operation TEXT NOT NULL,
37
- payload TEXT NOT NULL,
38
- status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
39
- result TEXT,
40
- attempt INTEGER NOT NULL DEFAULT 0,
41
- next_attempt_at INTEGER NOT NULL,
42
- last_error TEXT,
43
- last_error_name TEXT,
44
- completed_at INTEGER
45
- );
46
- `,
47
- down: 'DROP TABLE IF EXISTS durability_calls;',
48
- },
49
- {
50
- name: 'durability_0002_pending_index',
51
- up: `
52
- CREATE INDEX IF NOT EXISTS durability_calls_pending_idx
53
- ON durability_calls (status, next_attempt_at);
54
- `,
55
- down: 'DROP INDEX IF EXISTS durability_calls_pending_idx;',
56
- },
57
- ];
58
- /**
59
- * Moves the durability schema in one Durable Object instance to a target.
60
- *
61
- * {@link createDurability} calls this with the latest target automatically. Call
62
- * it directly only for explicit schema management, especially rollbacks. Passing
63
- * `null` removes every durability table and permanently deletes stored calls.
64
- *
65
- * @example Apply all pending durability migrations.
66
- * ```ts
67
- * migrateDurability(this.ctx);
68
- * ```
69
- *
70
- * @example Roll back to a specific version, then remove the schema entirely.
71
- * ```ts
72
- * migrateDurability(this.ctx, 'durability_0001_create_calls');
73
- * migrateDurability(this.ctx, null);
74
- * ```
75
- *
76
- * @param context Durable Object context containing SQLite-backed storage.
77
- * @param target Migration name to migrate to, or null to roll back all migrations.
78
- */
79
- export const migrateDurability = (context, target = durabilityMigrations[durabilityMigrations.length - 1]
80
- ?.name ?? null) => {
81
- const targetIndex = target === null
82
- ? -1
83
- : durabilityMigrations.findIndex((migration) => migration.name === target);
84
- if (target !== null && targetIndex === -1) {
85
- throw new Error(`Unknown durability migration target "${target}"`);
86
- }
87
- const qb = new DOQB(context.storage.sql);
88
- const workersQbMigrations = durabilityMigrations.map(({ name, up }) => ({ name, sql: up }));
89
- const migrationBuilder = qb.migrations({
90
- migrations: workersQbMigrations,
91
- tableName: 'durability_migrations',
92
- });
93
- const appliedNames = new Set(migrationBuilder.getApplied().map((migration) => migration.name));
94
- const rolledBack = [];
95
- for (let index = durabilityMigrations.length - 1; index > targetIndex; index--) {
96
- const migration = durabilityMigrations[index];
97
- if (!migration || !appliedNames.has(migration.name)) {
98
- continue;
99
- }
100
- qb.raw({
101
- query: `${migration.down}\nDELETE FROM ${migrationTableName} WHERE name = '${migration.name}';`,
102
- }).execute();
103
- rolledBack.push(migration.name);
104
- }
105
- const applied = qb
106
- .migrations({
107
- migrations: workersQbMigrations.slice(0, targetIndex + 1),
108
- tableName: 'durability_migrations',
109
- })
110
- .apply()
111
- .map((migration) => migration.name);
112
- return { applied, rolledBack };
113
- };
114
- /**
115
- * Marks a handler failure as terminal so it is persisted without another retry.
116
- *
117
- * Use this for permanent failures such as invalid input. Transient errors should
118
- * be thrown normally so the configured retry policy can handle them.
119
- *
120
- * @example
121
- * ```ts
122
- * if (!recipient.isValid) {
123
- * throw new NonRetryableError('Recipient is invalid');
124
- * }
125
- * ```
126
- */
127
- export class NonRetryableError extends Error {
128
- constructor(message) {
129
- super(message);
130
- this.name = 'NonRetryableError';
131
- }
132
- }
133
- /**
134
- * Error recorded when a handler attempt exceeds its configured timeout.
135
- *
136
- * Durability creates this error and aborts the attempt's signal; consumers do
137
- * not need to throw it themselves. Timeout failures follow the normal retry
138
- * policy and are exposed by `getResult` if they become terminal.
139
- *
140
- * @example
141
- * ```ts
142
- * const state = await durability.resizeImage.getResult('resize:image-1');
143
- * if (
144
- * state.status === 'failed' &&
145
- * state.error.name === 'DurableAttemptTimeoutError'
146
- * ) {
147
- * console.error('Resize timed out');
148
- * }
149
- * ```
150
- */
151
- export class DurableAttemptTimeoutError extends Error {
152
- constructor(operation, timeoutMs) {
153
- super(`Durable operation "${operation}" timed out after ${timeoutMs}ms`);
154
- this.name = 'DurableAttemptTimeoutError';
155
- }
156
- }
157
- /**
158
- * Error thrown when an idempotency key is reused for another operation.
159
- *
160
- * A call ID permanently identifies its original operation so `getResult` cannot
161
- * return a value with the wrong inferred type.
162
- *
163
- * @example
164
- * ```ts
165
- * await durability.resizeImage({ id: 'job:1', payload: resizeInput });
166
- * await durability.sendEmail({ id: 'job:1', payload: emailInput });
167
- * // Throws DuplicateDurableCallError because job:1 belongs to resizeImage.
168
- * ```
169
- */
170
- export class DuplicateDurableCallError extends Error {
171
- constructor(id, existingOperation, requestedOperation) {
172
- super(`Durable call "${id}" already belongs to operation "${existingOperation}", not "${requestedOperation}"`);
173
- this.name = 'DuplicateDurableCallError';
174
- }
175
- }
176
- /**
177
- * Creates typed, alarm-backed durable methods from an operation handler map.
178
- *
179
- * Registered calls persist before execution and are deduplicated by their IDs.
180
- * Generated methods resolve after registration; read handler results with each
181
- * method's `getResult` function.
182
- *
183
- * The Durable Object class must use SQLite storage, and its `alarm` method must
184
- * delegate to the returned alarm handler. External side effects should use the
185
- * call ID as an idempotency key because a crash can occur after a side effect but
186
- * before the completion record commits.
187
- *
188
- * @example
189
- * ```ts
190
- * class ImageJobs extends DurableObject<Env> {
191
- * private readonly durability = createDurability(this.ctx, {
192
- * resizeImage: async ({ id, payload, signal }) =>
193
- * this.env.IMAGES.resize(payload.imageId, {
194
- * idempotencyKey: id,
195
- * signal,
196
- * }),
197
- * });
198
- *
199
- * resize(imageId: string) {
200
- * return this.durability.resizeImage({
201
- * id: `resize:${imageId}`,
202
- * payload: { imageId },
203
- * });
204
- * }
205
- *
206
- * alarm(info: AlarmInvocationInfo) {
207
- * return this.durability.alarm(info);
208
- * }
209
- * }
210
- * ```
211
- *
212
- * @param context Durable Object context containing SQLite-backed storage.
213
- * @param handlers Operation handlers keyed by their public method names.
214
- * @param options Concurrency, timeout, and retry policy configuration.
215
- */
216
- export const createDurability = (context, handlers, options = {}) => {
217
- migrateDurability(context);
218
- const qb = new DOQB(context.storage.sql);
219
- const active = new Map();
220
- const alarmConcurrency = options.alarmConcurrency ?? 10;
221
- const alarmHandoffMs = options.alarmHandoffMs ?? 14 * 60000;
222
- const defaultAttemptTimeoutMs = options.attemptTimeoutMs ?? 5 * 60000;
223
- const defaultRetryDelay = options.retries?.delay ??
224
- ((attempt) => jitter(exponential(attempt)));
225
- const defaultMaxAttempts = options.retries?.maxAttempts ?? 5;
226
- if (!Number.isInteger(alarmConcurrency) || alarmConcurrency < 1) {
227
- throw new RangeError('alarmConcurrency must be a positive integer');
228
- }
229
- if (!Number.isInteger(defaultAttemptTimeoutMs) ||
230
- defaultAttemptTimeoutMs < 1) {
231
- throw new RangeError('attemptTimeoutMs must be a positive integer');
232
- }
233
- if (!Number.isInteger(defaultMaxAttempts) || defaultMaxAttempts < 1) {
234
- throw new RangeError('retries.maxAttempts must be a positive integer');
235
- }
236
- const methodOptions = (options.methods ?? {});
237
- for (const [operation, method] of Object.entries(methodOptions)) {
238
- if (method?.attemptTimeoutMs !== undefined &&
239
- (!Number.isInteger(method.attemptTimeoutMs) ||
240
- method.attemptTimeoutMs < 1)) {
241
- throw new RangeError(`methods.${operation}.attemptTimeoutMs must be a positive integer`);
242
- }
243
- if (method?.retries?.maxAttempts !== undefined &&
244
- (!Number.isInteger(method.retries.maxAttempts) ||
245
- method.retries.maxAttempts < 1)) {
246
- throw new RangeError(`methods.${operation}.retries.maxAttempts must be a positive integer`);
247
- }
248
- }
249
- const executionPolicy = (operation) => {
250
- const method = methodOptions[operation];
251
- return {
252
- attemptTimeoutMs: method?.attemptTimeoutMs ?? defaultAttemptTimeoutMs,
253
- delay: method?.retries?.delay ?? defaultRetryDelay,
254
- maxAttempts: method?.retries?.maxAttempts ?? defaultMaxAttempts,
255
- };
256
- };
257
- const isNonRetryable = (error) => {
258
- if (error instanceof NonRetryableError) {
259
- return true;
260
- }
261
- if (typeof error !== 'object' || error === null) {
262
- return false;
263
- }
264
- const value = error;
265
- return (value.name === 'NonRetryableError' ||
266
- value.constructor?.name === 'NonRetryableError');
267
- };
268
- const serialize = (value) => JSON.stringify(value === undefined
269
- ? { kind: 'undefined' }
270
- : { kind: 'value', value: z.json().parse(value) });
271
- const deserialize = (value) => {
272
- const parsed = JSON.parse(value);
273
- const stored = storedValueSchema.parse(parsed);
274
- return stored.kind === 'undefined' ? undefined : stored.value;
275
- };
276
- const getCall = (id) => qb
277
- .fetchOne({
278
- tableName,
279
- where: { conditions: 'id = ?', params: [id] },
280
- })
281
- .execute().results;
282
- const getResult = (operation, idempotencyKey) => {
283
- const call = getCall(idempotencyKey);
284
- if (!call) {
285
- return { status: 'not_found' };
286
- }
287
- assertOperation(idempotencyKey, call.operation, operation);
288
- if (call.status === 'pending') {
289
- return {
290
- status: 'pending',
291
- attempt: call.attempt,
292
- nextAttemptAt: call.next_attempt_at,
293
- lastError: call.last_error,
294
- };
295
- }
296
- if (call.status === 'failed') {
297
- return {
298
- status: 'failed',
299
- attempt: call.attempt,
300
- error: {
301
- name: call.last_error_name ?? 'Error',
302
- message: call.last_error ?? 'Durable operation failed',
303
- },
304
- };
305
- }
306
- if (call.result === null) {
307
- throw new Error(`Completed durable call "${idempotencyKey}" has no result`);
308
- }
309
- return { status: 'completed', result: deserialize(call.result) };
310
- };
311
- const listDueIds = (now, limit) => (qb
312
- .fetchAll({
313
- tableName,
314
- fields: 'id',
315
- where: {
316
- conditions: "status = 'pending' AND next_attempt_at <= ?",
317
- params: [now],
318
- },
319
- orderBy: 'next_attempt_at ASC',
320
- limit,
321
- })
322
- .execute().results ?? []).map((call) => call.id);
323
- const getNextPendingAt = () => qb
324
- .fetchOne({
325
- tableName,
326
- fields: 'next_attempt_at',
327
- where: { conditions: "status = 'pending'" },
328
- orderBy: 'next_attempt_at ASC',
329
- })
330
- .execute().results?.next_attempt_at;
331
- const runConcurrent = async (items, concurrency, worker) => {
332
- let nextIndex = 0;
333
- await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, async () => {
334
- while (nextIndex < items.length) {
335
- const item = items[nextIndex++];
336
- if (item !== undefined) {
337
- // eslint-disable-next-line no-await-in-loop
338
- await worker(item);
339
- }
340
- }
341
- }));
342
- };
343
- const assertOperation = (id, existingOperation, requestedOperation) => {
344
- if (existingOperation !== requestedOperation) {
345
- throw new DuplicateDurableCallError(id, existingOperation, requestedOperation);
346
- }
347
- };
348
- const armIfMissing = async (timestamp) => {
349
- const currentAlarm = await context.storage.getAlarm();
350
- if (currentAlarm === null) {
351
- await context.storage.setAlarm(timestamp);
352
- }
353
- };
354
- let alarmRefresh;
355
- const scheduleNextAlarm = () => {
356
- if (alarmRefresh) {
357
- return alarmRefresh;
358
- }
359
- alarmRefresh = context.storage
360
- .transaction(async (transaction) => {
361
- const nextAttemptAt = getNextPendingAt();
362
- const currentAlarm = await transaction.getAlarm();
363
- if (nextAttemptAt === undefined) {
364
- if (currentAlarm !== null) {
365
- await transaction.deleteAlarm();
366
- }
367
- }
368
- else if (currentAlarm === null) {
369
- await transaction.setAlarm(nextAttemptAt);
370
- }
371
- })
372
- .finally(() => {
373
- alarmRefresh = undefined;
374
- });
375
- return alarmRefresh;
376
- };
377
- const execute = (id) => {
378
- const running = active.get(id);
379
- if (running) {
380
- return running;
381
- }
382
- const execution = (async () => {
383
- const call = getCall(id);
384
- if (!call) {
385
- return undefined;
386
- }
387
- if (call.status === 'completed') {
388
- if (call.result === null) {
389
- throw new Error(`Completed durable call "${id}" has no result`);
390
- }
391
- return deserialize(call.result);
392
- }
393
- if (call.status === 'failed') {
394
- return undefined;
395
- }
396
- const handler = handlers[call.operation];
397
- const attempt = call.attempt + 1;
398
- const policy = executionPolicy(call.operation);
399
- qb.update({
400
- tableName,
401
- data: { attempt },
402
- where: { conditions: 'id = ?', params: [id] },
403
- }).execute();
404
- let timeout;
405
- try {
406
- if (!handler) {
407
- throw new NonRetryableError(`No handler registered for operation "${call.operation}"`);
408
- }
409
- const controller = new AbortController();
410
- const handlerResult = Promise.resolve().then(() => handler({
411
- id,
412
- operation: call.operation,
413
- payload: deserialize(call.payload),
414
- attempt,
415
- signal: controller.signal,
416
- }));
417
- const timeoutResult = new Promise((_resolve, reject) => {
418
- timeout = setTimeout(() => {
419
- const error = new DurableAttemptTimeoutError(call.operation, policy.attemptTimeoutMs);
420
- controller.abort(error);
421
- reject(error);
422
- }, policy.attemptTimeoutMs);
423
- });
424
- const result = await Promise.race([handlerResult, timeoutResult]);
425
- qb.update({
426
- tableName,
427
- data: {
428
- status: 'completed',
429
- result: serialize(result),
430
- last_error: null,
431
- last_error_name: null,
432
- completed_at: Date.now(),
433
- },
434
- where: { conditions: 'id = ?', params: [id] },
435
- }).execute();
436
- return result;
437
- }
438
- catch (error) {
439
- const terminal = isNonRetryable(error) || attempt >= policy.maxAttempts;
440
- const baseDelay = terminal ? 0 : policy.delay(attempt);
441
- if (!Number.isFinite(baseDelay) || baseDelay < 0) {
442
- throw new RangeError(`Retry delay for operation "${call.operation}" must be a non-negative finite number`);
443
- }
444
- const nextAttemptAt = Date.now() + Math.round(baseDelay);
445
- await context.storage.transaction(async (transaction) => {
446
- qb.update({
447
- tableName,
448
- data: {
449
- status: terminal ? 'failed' : 'pending',
450
- attempt,
451
- next_attempt_at: nextAttemptAt,
452
- last_error: error instanceof Error ? error.message : String(error),
453
- last_error_name: error instanceof Error ? error.name : 'Error',
454
- },
455
- where: { conditions: 'id = ?', params: [id] },
456
- }).execute();
457
- if (!terminal && (await transaction.getAlarm()) === null) {
458
- await transaction.setAlarm(nextAttemptAt);
459
- }
460
- });
461
- throw error;
462
- }
463
- finally {
464
- if (timeout !== undefined) {
465
- clearTimeout(timeout);
466
- }
467
- }
468
- })();
469
- active.set(id, execution);
470
- void execution.finally(() => active.delete(id)).catch(() => undefined);
471
- return execution;
472
- };
473
- const run = async (input) => {
474
- const existing = getCall(input.id);
475
- if (existing) {
476
- assertOperation(input.id, existing.operation, input.operation);
477
- if (existing.status === 'completed' || existing.status === 'failed') {
478
- return;
479
- }
480
- await context.storage.transaction(async (transaction) => {
481
- if ((await transaction.getAlarm()) === null) {
482
- await transaction.setAlarm(existing.next_attempt_at);
483
- }
484
- });
485
- }
486
- else {
487
- const now = Date.now();
488
- await context.storage.transaction(async (transaction) => {
489
- qb.insert({
490
- tableName,
491
- data: {
492
- id: input.id,
493
- operation: input.operation,
494
- payload: serialize(input.payload),
495
- status: 'pending',
496
- attempt: 0,
497
- next_attempt_at: now,
498
- },
499
- }).execute();
500
- if ((await transaction.getAlarm()) === null) {
501
- await transaction.setAlarm(now);
502
- }
503
- });
504
- }
505
- const execution = execute(input.id);
506
- void execution.then(() => scheduleNextAlarm()).catch(() => undefined);
507
- };
508
- const alarm = async (_alarmInfo) => {
509
- const startedAt = Date.now();
510
- const due = listDueIds(startedAt, 100);
511
- if (due.length === 0) {
512
- await scheduleNextAlarm();
513
- return;
514
- }
515
- const execution = runConcurrent(due, alarmConcurrency, async (id) => {
516
- try {
517
- await execute(id);
518
- }
519
- catch {
520
- return;
521
- }
522
- });
523
- let timeout;
524
- const handoff = Symbol('alarm handoff');
525
- const remaining = alarmHandoffMs - (Date.now() - startedAt);
526
- const result = await Promise.race([
527
- execution,
528
- new Promise((resolve) => {
529
- timeout = setTimeout(() => resolve(handoff), Math.max(remaining, 0));
530
- }),
531
- ]);
532
- if (timeout !== undefined) {
533
- clearTimeout(timeout);
534
- }
535
- if (result === handoff) {
536
- await armIfMissing(Date.now());
537
- return;
538
- }
539
- await scheduleNextAlarm();
540
- };
541
- const durability = { alarm };
542
- for (const operation of Object.keys(handlers)) {
543
- if (operation === 'alarm') {
544
- throw new Error(`"${operation}" is reserved by durability`);
545
- }
546
- const durableOperation = (input) => run({ ...input, operation });
547
- durableOperation.getResult = async (idempotencyKey) => getResult(operation, idempotencyKey);
548
- durability[operation] = durableOperation;
549
- }
550
- return durability;
551
- };
1
+ export { Durability, } from './durability.js';
2
+ export { DurabilityAlarms, } from './durability-alarms.js';
3
+ export { DurabilityFanout, } from './fanout.js';
4
+ export { RoutingLoad, } from './load.js';
5
+ export { DurabilityScheduler, } from './scheduler.js';
6
+ export { DuplicateDurableCallError, DurableAlarmTimeoutError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, DurableRetryPolicyError, NonRetryableError, FanoutTimeoutError, FanoutSettlementError, FanoutEnqueueError, RoutingError, } from './errors.js';
7
+ export { durabilityNamedAlarmMigrations, durabilityOperationMigrations, durabilityFanoutMigrations, } from './migrations.js';
@@ -0,0 +1,2 @@
1
+ import { type DurabilityStorage } from './storage.js';
2
+ export declare const createKvDurabilityStorage: (storage: DurableObjectStorage) => DurabilityStorage;