durability 5.1.0 → 5.2.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.
Files changed (86) hide show
  1. package/README.md +145 -12
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
  3. package/dist/{durability-alarms.js → alarms/index.js} +69 -55
  4. package/dist/alarms/migrations.d.ts +9 -0
  5. package/dist/alarms/migrations.js +133 -0
  6. package/dist/experimental.d.ts +10 -10
  7. package/dist/experimental.js +7 -7
  8. package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
  9. package/dist/{fanout.js → fanout/index.js} +54 -46
  10. package/dist/fanout/migrations.d.ts +9 -0
  11. package/dist/fanout/migrations.js +181 -0
  12. package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
  13. package/dist/index.d.ts +14 -9
  14. package/dist/index.js +13 -8
  15. package/dist/{log.d.ts → log/index.d.ts} +15 -3
  16. package/dist/{log.js → log/index.js} +152 -124
  17. package/dist/log/migrations.d.ts +18 -0
  18. package/dist/log/migrations.js +297 -0
  19. package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
  20. package/dist/{memoize.js → memoize/index.js} +14 -11
  21. package/dist/memoize/migrations.d.ts +10 -0
  22. package/dist/memoize/migrations.js +21 -0
  23. package/dist/migrations.d.ts +0 -111
  24. package/dist/migrations.js +0 -612
  25. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  26. package/dist/{durability.js → operations/index.js} +59 -47
  27. package/dist/operations/migrations.d.ts +10 -0
  28. package/dist/operations/migrations.js +150 -0
  29. package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
  30. package/dist/{outbox.js → outbox/index.js} +16 -60
  31. package/dist/outbox/migrations.d.ts +9 -0
  32. package/dist/outbox/migrations.js +43 -0
  33. package/dist/{routing-client.js → routing/client.js} +1 -1
  34. package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
  35. package/dist/{routing.js → routing/index.js} +3 -3
  36. package/dist/{load.d.ts → routing/load.d.ts} +1 -1
  37. package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
  38. package/dist/{schedule.js → schedule/index.js} +65 -49
  39. package/dist/schedule/migrations.d.ts +9 -0
  40. package/dist/schedule/migrations.js +118 -0
  41. package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
  42. package/dist/{scheduler.js → scheduler/index.js} +300 -63
  43. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  44. package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
  45. package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
  46. package/dist/{semaphore.js → semaphore/index.js} +7 -7
  47. package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
  48. package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
  49. package/dist/semaphore/migrations.d.ts +18 -0
  50. package/dist/semaphore/migrations.js +444 -0
  51. package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
  52. package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
  53. package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
  54. package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +69 -61
  55. package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
  56. package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
  57. package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
  58. package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
  59. package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
  60. package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
  61. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  62. package/dist/{client.js → socket/client.js} +4 -4
  63. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  64. package/dist/{socket.js → socket/index.js} +66 -20
  65. package/dist/socket/migrations.d.ts +9 -0
  66. package/dist/socket/migrations.js +57 -0
  67. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  68. package/dist/storage.d.ts +4 -0
  69. package/dist/storage.js +13 -6
  70. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  71. package/dist/{topology-client.js → topology/client.js} +3 -3
  72. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  73. package/dist/{topology.js → topology/index.js} +92 -58
  74. package/dist/topology/migrations.d.ts +10 -0
  75. package/dist/topology/migrations.js +138 -0
  76. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  77. package/dist/{tracker.js → tracker/index.js} +2 -2
  78. package/package.json +11 -11
  79. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  80. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  81. /package/dist/{load.js → routing/load.js} +0 -0
  82. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  83. /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
  84. /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
  85. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  86. /package/dist/{tracked-source.js → tracker/source.js} +0 -0
@@ -9,20 +9,23 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _DurabilityCore_instances, _DurabilityCore_engine, _DurabilityCore_handlers, _DurabilityCore_policy, _DurabilityCore_lifecycle, _DurabilityCore_emit, _DurabilityCore_includeResults, _DurabilityCore_active, _DurabilityCore_executionStarts, _DurabilityCore_trackedSource, _DurabilityCore_runDue, _DurabilityCore_getResult, _DurabilityCore_run, _DurabilityCore_execute, _DurabilityCore_start;
12
+ var _DurabilityCore_instances, _DurabilityCore_engine, _DurabilityCore_channel, _DurabilityCore_table, _DurabilityCore_handlers, _DurabilityCore_policy, _DurabilityCore_lifecycle, _DurabilityCore_emit, _DurabilityCore_includeResults, _DurabilityCore_active, _DurabilityCore_executionStarts, _DurabilityCore_trackedSource, _DurabilityCore_runDue, _DurabilityCore_getResult, _DurabilityCore_run, _DurabilityCore_execute, _DurabilityCore_start;
13
13
  import { z } from 'zod';
14
- import { traceFunction, traceMethods } from './dev.js';
15
- import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from './errors.js';
16
- import { createLifecycleHooks, } from './lifecycle.js';
17
- import { durabilityOperationMigrations, durabilityMemoMigrations, migrateCapability, } from './migrations.js';
18
- import { createPolicyResolver, methodOptionsSchema, } from './policy.js';
19
- import { engineFor, } from './scheduler.js';
20
- import { canonical, deserialize, serialize } from './serialization.js';
21
- import { createMemoize, deleteMemosOfDroppedTable, memoizedKeys, } from './memoize.js';
22
- import { listCreated, updateAttempt } from './storage.js';
23
- import { registerTrackedSource, } from './tracked-source.js';
14
+ import { traceFunction, traceMethods } from '../dev.js';
15
+ import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from '../errors.js';
16
+ import { createLifecycleHooks, } from '../lifecycle.js';
17
+ import { durabilityOperationMigrations } from './migrations.js';
18
+ import { durabilityMemoMigrations } from '../memoize/migrations.js';
19
+ import { migrateCapability, } from '../migrations.js';
20
+ import { createPolicyResolver, methodOptionsSchema, } from '../policy.js';
21
+ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
22
+ import { canonical, deserialize, serialize } from '../serialization.js';
23
+ import { createMemoize, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
24
+ import { listCreated, updateAttempt } from '../storage.js';
25
+ import { registerTrackedSource, } from '../tracker/source.js';
24
26
  const callRowSchema = z.object({
25
27
  id: z.string(),
28
+ channel: z.string(),
26
29
  operation: z.string(),
27
30
  payload: z.string(),
28
31
  status: z.enum(['pending', 'completed', 'failed']),
@@ -36,9 +39,11 @@ const callRowSchema = z.object({
36
39
  generation_id: z.string(),
37
40
  });
38
41
  const callsTable = { name: 'durability_calls', key: 'id' };
39
- const readCall = (sql, id) => callRowSchema
42
+ const readCall = (sql, channel, id) => callRowSchema
40
43
  .optional()
41
- .parse(sql.exec('SELECT * FROM durability_calls WHERE id = ?', id).toArray()[0]);
44
+ .parse(sql
45
+ .exec('SELECT * FROM durability_calls WHERE channel = ? AND id = ?', channel, id)
46
+ .toArray()[0]);
42
47
  const assertOperation = (id, existingOperation, requestedOperation) => {
43
48
  if (existingOperation !== requestedOperation) {
44
49
  throw new DuplicateDurableCallError(id, existingOperation, requestedOperation);
@@ -78,7 +83,7 @@ const callRecord = (sql, call) => {
78
83
  state: state.status === 'pending'
79
84
  ? {
80
85
  ...state,
81
- memoized: memoizedKeys(sql, callsTable, call.id, call.generation_id),
86
+ memoized: memoizedKeys(sql, { ...callsTable, channel: call.channel }, call.id, call.generation_id),
82
87
  }
83
88
  : state,
84
89
  };
@@ -93,6 +98,8 @@ class DurabilityCore {
93
98
  constructor(config) {
94
99
  _DurabilityCore_instances.add(this);
95
100
  _DurabilityCore_engine.set(this, void 0);
101
+ _DurabilityCore_channel.set(this, void 0);
102
+ _DurabilityCore_table.set(this, void 0);
96
103
  _DurabilityCore_handlers.set(this, void 0);
97
104
  _DurabilityCore_policy.set(this, void 0);
98
105
  _DurabilityCore_lifecycle.set(this, void 0);
@@ -101,6 +108,8 @@ class DurabilityCore {
101
108
  _DurabilityCore_active.set(this, new Map());
102
109
  _DurabilityCore_executionStarts.set(this, new Map());
103
110
  const { handlers } = config;
111
+ __classPrivateFieldSet(this, _DurabilityCore_channel, assertChannel('Durability', config.channel ?? defaultChannel), "f");
112
+ __classPrivateFieldSet(this, _DurabilityCore_table, { ...callsTable, channel: __classPrivateFieldGet(this, _DurabilityCore_channel, "f") }, "f");
104
113
  __classPrivateFieldSet(this, _DurabilityCore_engine, engineFor(config), "f");
105
114
  __classPrivateFieldSet(this, _DurabilityCore_handlers, handlers, "f");
106
115
  __classPrivateFieldSet(this, _DurabilityCore_policy, createPolicyResolver(config, config.methods ?? {}, methodOptionsSchema), "f");
@@ -122,8 +131,10 @@ class DurabilityCore {
122
131
  }
123
132
  __classPrivateFieldGet(this, _DurabilityCore_engine, "f").migrateSchema('operations', durabilityOperationMigrations);
124
133
  __classPrivateFieldGet(this, _DurabilityCore_engine, "f").migrateSchema('memos', durabilityMemoMigrations);
125
- __classPrivateFieldGet(this, _DurabilityCore_engine, "f").register({
126
- table: callsTable,
134
+ __classPrivateFieldGet(this, _DurabilityCore_engine, "f").registerChannel({
135
+ table: __classPrivateFieldGet(this, _DurabilityCore_table, "f"),
136
+ channel: __classPrivateFieldGet(this, _DurabilityCore_channel, "f"),
137
+ helper: 'Durability',
127
138
  runDue: (now) => __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_runDue).call(this, now),
128
139
  });
129
140
  registerTrackedSource(this, __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_trackedSource).call(this));
@@ -150,7 +161,7 @@ class DurabilityCore {
150
161
  * timestamp, regardless of status, and returns how many were removed.
151
162
  */
152
163
  async purgeBefore(before) {
153
- const count = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").purge(callsTable, before, __classPrivateFieldGet(this, _DurabilityCore_active, "f"), 'Durable operation was purged');
164
+ const count = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").purge(__classPrivateFieldGet(this, _DurabilityCore_table, "f"), before, __classPrivateFieldGet(this, _DurabilityCore_active, "f"), 'Durable operation was purged');
154
165
  __classPrivateFieldGet(this, _DurabilityCore_emit, "f").call(this, {
155
166
  type: 'purged',
156
167
  entityKind: 'operation',
@@ -161,15 +172,15 @@ class DurabilityCore {
161
172
  return count;
162
173
  }
163
174
  }
164
- _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap(), _DurabilityCore_policy = new WeakMap(), _DurabilityCore_lifecycle = new WeakMap(), _DurabilityCore_emit = new WeakMap(), _DurabilityCore_includeResults = new WeakMap(), _DurabilityCore_active = new WeakMap(), _DurabilityCore_executionStarts = new WeakMap(), _DurabilityCore_instances = new WeakSet(), _DurabilityCore_trackedSource = function _DurabilityCore_trackedSource() {
175
+ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_channel = new WeakMap(), _DurabilityCore_table = new WeakMap(), _DurabilityCore_handlers = new WeakMap(), _DurabilityCore_policy = new WeakMap(), _DurabilityCore_lifecycle = new WeakMap(), _DurabilityCore_emit = new WeakMap(), _DurabilityCore_includeResults = new WeakMap(), _DurabilityCore_active = new WeakMap(), _DurabilityCore_executionStarts = new WeakMap(), _DurabilityCore_instances = new WeakSet(), _DurabilityCore_trackedSource = function _DurabilityCore_trackedSource() {
165
176
  const sql = __classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql;
166
177
  return {
167
178
  get: async (id) => {
168
- const call = readCall(sql, id);
179
+ const call = readCall(sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), id);
169
180
  return call && callRecord(sql, call);
170
181
  },
171
182
  list: async ({ limit, cursor, status, name }) => {
172
- const page = listCreated(sql, callsTable, 'operation', {
183
+ const page = listCreated(sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), 'operation', {
173
184
  limit,
174
185
  after: cursor,
175
186
  status,
@@ -183,7 +194,7 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap()
183
194
  ? { records }
184
195
  : { records, cursor: page.next };
185
196
  },
186
- watch: (id, listener) => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").watch(callsTable, id, () => listener()),
197
+ watch: (id, listener) => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").watch(__classPrivateFieldGet(this, _DurabilityCore_table, "f"), id, () => listener()),
187
198
  };
188
199
  }, _DurabilityCore_runDue = async function _DurabilityCore_runDue(now) {
189
200
  const due = z
@@ -191,44 +202,45 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap()
191
202
  .array()
192
203
  .parse(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql
193
204
  .exec(`SELECT id FROM durability_calls
194
- WHERE status = 'pending' AND next_attempt_at <= ?
205
+ WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
195
206
  ORDER BY next_attempt_at ASC
196
- LIMIT 100`, now)
207
+ LIMIT 100`, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), now)
197
208
  .toArray());
198
209
  // With liveTimers an active operation reschedules itself when it settles, so
199
210
  // skipping it keeps a long handler from blocking a concurrent alarm pass.
200
211
  const pending = __classPrivateFieldGet(this, _DurabilityCore_engine, "f").liveTimers
201
- ? due.filter(({ id }) => !__classPrivateFieldGet(this, _DurabilityCore_active, "f").has(id))
212
+ ? due.filter(({ id: key }) => !__classPrivateFieldGet(this, _DurabilityCore_active, "f").has(key))
202
213
  : due;
203
- await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").runConcurrent(pending, async ({ id }) => {
214
+ await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").runConcurrent(pending, async ({ id: key }) => {
204
215
  try {
205
- await __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, id);
216
+ await __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, key);
206
217
  }
207
218
  catch {
208
219
  return;
209
220
  }
210
221
  });
211
222
  }, _DurabilityCore_getResult = async function _DurabilityCore_getResult(operation, idempotencyKey) {
212
- const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, idempotencyKey);
223
+ const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), idempotencyKey);
213
224
  if (!call) {
214
225
  return { status: 'not_found' };
215
226
  }
216
227
  assertOperation(idempotencyKey, call.operation, operation);
217
228
  return callState(call);
218
229
  }, _DurabilityCore_run = async function _DurabilityCore_run(input) {
230
+ const key = input.id;
219
231
  const payload = serialize(input.payload);
220
232
  const now = Date.now();
221
233
  const generation = crypto.randomUUID();
222
234
  const { inserted, winner } = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").transaction((sql) => {
223
235
  const created = sql
224
236
  .exec(`INSERT INTO durability_calls
225
- (id, operation, payload, status, attempt, next_attempt_at,
226
- created_at, generation_id)
227
- VALUES (?, ?, ?, 'pending', 0, ?, ?, ?)
228
- ON CONFLICT(id) DO NOTHING
229
- RETURNING id`, input.id, input.operation, payload, now, now, generation)
237
+ (id, channel, operation, payload, status, attempt,
238
+ next_attempt_at, created_at, generation_id)
239
+ VALUES (?, ?, ?, ?, 'pending', 0, ?, ?, ?)
240
+ ON CONFLICT(channel, id) DO NOTHING
241
+ RETURNING id`, key, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), input.operation, payload, now, now, generation)
230
242
  .toArray().length > 0;
231
- const current = readCall(sql, input.id);
243
+ const current = readCall(sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), key);
232
244
  if (!current) {
233
245
  throw new Error(`Durable call "${input.id}" was not persisted`);
234
246
  }
@@ -255,31 +267,31 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap()
255
267
  return;
256
268
  }
257
269
  const onError = (error) => reportFailure('durability.background_execution.failed', { operation: input.operation, id: input.id }, error);
258
- const background = __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, input.id, eagerPermit)
270
+ const background = __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, key, eagerPermit)
259
271
  .then(() => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").scheduleNextAlarm())
260
272
  .catch(onError);
261
273
  __classPrivateFieldGet(this, _DurabilityCore_engine, "f").waitUntil(background, onError);
262
274
  }, _DurabilityCore_execute =
263
275
  /**
264
- * Looks up a call and starts it unless the same generation is already
265
- * running. Lookups for one ID are serialized so two concurrent callers
266
- * cannot both miss the active map while awaiting storage.
276
+ * Looks up a call by row key and starts it unless the same generation is
277
+ * already running. Lookups for one key are serialized so two concurrent
278
+ * callers cannot both miss the active map while awaiting storage.
267
279
  */
268
- async function _DurabilityCore_execute(id, eagerPermit) {
269
- const previousStart = __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(id) ?? Promise.resolve();
280
+ async function _DurabilityCore_execute(key, eagerPermit) {
281
+ const previousStart = __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(key) ?? Promise.resolve();
270
282
  let releaseStart;
271
283
  const currentStart = new Promise((resolve) => {
272
284
  releaseStart = resolve;
273
285
  });
274
- __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").set(id, currentStart);
286
+ __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").set(key, currentStart);
275
287
  let permitTransferred = false;
276
288
  try {
277
289
  await previousStart;
278
- const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, id);
290
+ const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), key);
279
291
  if (!call || call.status !== 'pending') {
280
292
  return;
281
293
  }
282
- const running = __classPrivateFieldGet(this, _DurabilityCore_active, "f").get(id);
294
+ const running = __classPrivateFieldGet(this, _DurabilityCore_active, "f").get(key);
283
295
  if (running?.generation === call.generation_id) {
284
296
  releaseStart();
285
297
  await running.settled;
@@ -292,8 +304,8 @@ async function _DurabilityCore_execute(id, eagerPermit) {
292
304
  }
293
305
  finally {
294
306
  releaseStart();
295
- if (__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(id) === currentStart) {
296
- __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").delete(id);
307
+ if (__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(key) === currentStart) {
308
+ __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").delete(key);
297
309
  }
298
310
  if (!permitTransferred) {
299
311
  eagerPermit?.();
@@ -303,7 +315,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
303
315
  const policy = __classPrivateFieldGet(this, _DurabilityCore_policy, "f").call(this, call.operation);
304
316
  const handler = __classPrivateFieldGet(this, _DurabilityCore_handlers, "f")[call.operation];
305
317
  return __classPrivateFieldGet(this, _DurabilityCore_engine, "f").startExecution({
306
- table: callsTable,
318
+ table: __classPrivateFieldGet(this, _DurabilityCore_table, "f"),
307
319
  key: call.id,
308
320
  row: call,
309
321
  label: `operation "${call.operation}"`,
@@ -324,7 +336,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
324
336
  payload: deserialize(call.payload),
325
337
  attempt,
326
338
  signal,
327
- memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, callsTable, {
339
+ memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), {
328
340
  key: call.id,
329
341
  generation: call.generation_id,
330
342
  attempt,
@@ -340,7 +352,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
340
352
  throw new DurableResultSerializationError(call.operation, error);
341
353
  }
342
354
  const timestamp = Date.now();
343
- const updated = updateAttempt(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, callsTable, { key: call.id, generation: call.generation_id, attempt }, `status = 'completed', result = ?, last_error = NULL,
355
+ const updated = updateAttempt(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), { key: call.id, generation: call.generation_id, attempt }, `status = 'completed', result = ?, last_error = NULL,
344
356
  last_error_name = NULL, completed_at = ?`, serialized, timestamp);
345
357
  return updated ? timestamp : undefined;
346
358
  },
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Ordered SQLite migrations for the `durability_calls` table owned by
3
+ * {@link Durability}. Applied automatically on construction and tracked in the
4
+ * namespaced `durability_migrations` table alongside application migrations.
5
+ */
6
+ export declare const durabilityOperationMigrations: {
7
+ name: string;
8
+ up: string;
9
+ down: string;
10
+ }[];
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Ordered SQLite migrations for the `durability_calls` table owned by
3
+ * {@link Durability}. Applied automatically on construction and tracked in the
4
+ * namespaced `durability_migrations` table alongside application migrations.
5
+ */
6
+ export const durabilityOperationMigrations = [
7
+ {
8
+ name: 'durability_0001_create_calls',
9
+ up: `
10
+ CREATE TABLE IF NOT EXISTS durability_calls (
11
+ id TEXT PRIMARY KEY,
12
+ operation TEXT NOT NULL,
13
+ payload TEXT NOT NULL,
14
+ status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
15
+ result TEXT,
16
+ attempt INTEGER NOT NULL DEFAULT 0,
17
+ next_attempt_at INTEGER NOT NULL,
18
+ last_error TEXT,
19
+ last_error_name TEXT,
20
+ completed_at INTEGER
21
+ );
22
+ `,
23
+ down: 'DROP TABLE IF EXISTS durability_calls;',
24
+ },
25
+ {
26
+ name: 'durability_0002_pending_index',
27
+ up: `
28
+ CREATE INDEX IF NOT EXISTS durability_calls_pending_idx
29
+ ON durability_calls (status, next_attempt_at);
30
+ `,
31
+ down: 'DROP INDEX IF EXISTS durability_calls_pending_idx;',
32
+ },
33
+ {
34
+ name: 'durability_0004_calls_generation_and_created_at',
35
+ up: `
36
+ DROP INDEX IF EXISTS durability_calls_pending_idx;
37
+ ALTER TABLE durability_calls RENAME TO durability_calls_v3;
38
+ CREATE TABLE durability_calls (
39
+ id TEXT PRIMARY KEY,
40
+ operation TEXT NOT NULL,
41
+ payload TEXT NOT NULL,
42
+ status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
43
+ result TEXT,
44
+ attempt INTEGER NOT NULL DEFAULT 0,
45
+ next_attempt_at INTEGER NOT NULL,
46
+ last_error TEXT,
47
+ last_error_name TEXT,
48
+ completed_at INTEGER,
49
+ generation_id TEXT NOT NULL DEFAULT 'legacy',
50
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
51
+ );
52
+ INSERT INTO durability_calls
53
+ SELECT id, operation, payload, status, result, attempt, next_attempt_at,
54
+ last_error, last_error_name, completed_at, 'legacy:' || id,
55
+ CAST(unixepoch('subsec') * 1000 AS INTEGER)
56
+ FROM durability_calls_v3;
57
+ DROP TABLE durability_calls_v3;
58
+ CREATE INDEX durability_calls_pending_idx
59
+ ON durability_calls (status, next_attempt_at);
60
+ CREATE INDEX durability_calls_created_idx
61
+ ON durability_calls (created_at);
62
+ `,
63
+ down: `
64
+ DROP INDEX IF EXISTS durability_calls_pending_idx;
65
+ DROP INDEX IF EXISTS durability_calls_created_idx;
66
+ ALTER TABLE durability_calls RENAME TO durability_calls_v4;
67
+ CREATE TABLE durability_calls (
68
+ id TEXT PRIMARY KEY,
69
+ operation TEXT NOT NULL,
70
+ payload TEXT NOT NULL,
71
+ status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
72
+ result TEXT,
73
+ attempt INTEGER NOT NULL DEFAULT 0,
74
+ next_attempt_at INTEGER NOT NULL,
75
+ last_error TEXT,
76
+ last_error_name TEXT,
77
+ completed_at INTEGER
78
+ );
79
+ INSERT INTO durability_calls
80
+ SELECT id, operation, payload, status, result, attempt, next_attempt_at,
81
+ last_error, last_error_name, completed_at
82
+ FROM durability_calls_v4;
83
+ DROP TABLE durability_calls_v4;
84
+ CREATE INDEX durability_calls_pending_idx
85
+ ON durability_calls (status, next_attempt_at);
86
+ `,
87
+ },
88
+ {
89
+ name: 'durability_0015_calls_channel',
90
+ up: `
91
+ ALTER TABLE durability_calls RENAME TO durability_calls_before_channels;
92
+ CREATE TABLE durability_calls (
93
+ id TEXT NOT NULL,
94
+ operation TEXT NOT NULL,
95
+ payload TEXT NOT NULL,
96
+ status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
97
+ result TEXT,
98
+ attempt INTEGER NOT NULL DEFAULT 0,
99
+ next_attempt_at INTEGER NOT NULL,
100
+ last_error TEXT,
101
+ last_error_name TEXT,
102
+ completed_at INTEGER,
103
+ generation_id TEXT NOT NULL DEFAULT 'legacy',
104
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER)),
105
+ channel TEXT NOT NULL DEFAULT 'default',
106
+ PRIMARY KEY (channel, id)
107
+ );
108
+ INSERT INTO durability_calls
109
+ SELECT *, 'default' FROM durability_calls_before_channels;
110
+ DROP TABLE durability_calls_before_channels;
111
+ CREATE INDEX durability_calls_pending_idx
112
+ ON durability_calls (status, next_attempt_at);
113
+ CREATE INDEX durability_calls_created_idx
114
+ ON durability_calls (created_at);
115
+ CREATE INDEX durability_calls_channel_pending_idx
116
+ ON durability_calls (channel, status, next_attempt_at);
117
+ `,
118
+ down: `
119
+ CREATE TABLE durability_calls_channel_guard (
120
+ channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
121
+ );
122
+ INSERT INTO durability_calls_channel_guard SELECT DISTINCT channel FROM durability_calls;
123
+ DROP TABLE durability_calls_channel_guard;
124
+ ALTER TABLE durability_calls RENAME TO durability_calls_with_channels;
125
+ CREATE TABLE durability_calls (
126
+ id TEXT PRIMARY KEY,
127
+ operation TEXT NOT NULL,
128
+ payload TEXT NOT NULL,
129
+ status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
130
+ result TEXT,
131
+ attempt INTEGER NOT NULL DEFAULT 0,
132
+ next_attempt_at INTEGER NOT NULL,
133
+ last_error TEXT,
134
+ last_error_name TEXT,
135
+ completed_at INTEGER,
136
+ generation_id TEXT NOT NULL DEFAULT 'legacy',
137
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
138
+ );
139
+ INSERT INTO durability_calls
140
+ SELECT id, operation, payload, status, result, attempt, next_attempt_at,
141
+ last_error, last_error_name, completed_at, generation_id, created_at
142
+ FROM durability_calls_with_channels;
143
+ DROP TABLE durability_calls_with_channels;
144
+ CREATE INDEX durability_calls_pending_idx
145
+ ON durability_calls (status, next_attempt_at);
146
+ CREATE INDEX durability_calls_created_idx
147
+ ON durability_calls (created_at);
148
+ `,
149
+ },
150
+ ];
@@ -1,7 +1,7 @@
1
- import type { DurabilityLifecycleEvent, LifecycleHook } from './lifecycle.js';
2
- import { type DurabilityMigrationResult } from './migrations.js';
3
- import { type DurabilityRetryOptions } from './policy.js';
4
- import { type SchedulerAttachment } from './scheduler.js';
1
+ import type { DurabilityLifecycleEvent, LifecycleHook } from '../lifecycle.js';
2
+ import { type DurabilityMigrationResult } from '../migrations.js';
3
+ import { type DurabilityRetryOptions } from '../policy.js';
4
+ import { type SchedulerAttachment } from '../scheduler/index.js';
5
5
  /** The newest staged version of one key, handed to `publish`. */
6
6
  export type OutboxEntry<Value> = {
7
7
  key: string;
@@ -10,14 +10,15 @@ 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 _DurabilityOutbox_instances, _DurabilityOutbox_engine, _DurabilityOutbox_channel, _DurabilityOutbox_publish, _DurabilityOutbox_maxBatchSize, _DurabilityOutbox_batchDelayMs, _DurabilityOutbox_attemptTimeoutMs, _DurabilityOutbox_delay, _DurabilityOutbox_emit, _DurabilityOutbox_running, _DurabilityOutbox_stageIn, _DurabilityOutbox_emitRegistered, _DurabilityOutbox_attach, _DurabilityOutbox_firstAttemptAt, _DurabilityOutbox_runDue, _DurabilityOutbox_publishNow, _DurabilityOutbox_exclusive, _DurabilityOutbox_runBatch, _DurabilityOutbox_publishBatch, _DurabilityOutbox_retryDelay, _DurabilityOutbox_deadline;
13
- import { traceLifecycle, traceMethods } from './dev.js';
14
- import { OutboxTimeoutError, reportFailure, serializeError, } from './errors.js';
15
- import { durabilityOutboxMigrations, migrateCapability, } from './migrations.js';
16
- import { assertPositiveInteger, } from './policy.js';
17
- import { engineFor, } from './scheduler.js';
18
- import { deserialize, serialize } from './serialization.js';
19
- import { claimAttempt, deleteAttempt, updateAttempt, } from './storage.js';
20
- import { exponential, jitter } from './utils.js';
13
+ import { traceLifecycle, traceMethods } from '../dev.js';
14
+ import { OutboxTimeoutError, reportFailure, serializeError, } from '../errors.js';
15
+ import { durabilityOutboxMigrations } from './migrations.js';
16
+ import { migrateCapability, } from '../migrations.js';
17
+ import { assertPositiveInteger, } from '../policy.js';
18
+ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
19
+ import { deserialize, serialize } from '../serialization.js';
20
+ import { claimAttempt, deleteAttempt, updateAttempt, } from '../storage.js';
21
+ import { exponential, jitter } from '../utils.js';
21
22
  import { z } from 'zod';
22
23
  const outboxTable = { name: 'durability_outbox', key: 'id' };
23
24
  const outboxRowSchema = z.object({
@@ -35,7 +36,6 @@ const outboxRowSchema = z.object({
35
36
  generation_id: z.string(),
36
37
  });
37
38
  const versionRowSchema = z.object({ version: z.number().int().positive() });
38
- const channelRowSchema = z.object({ channel: z.string() });
39
39
  const listDueForChannel = (sql, channel, now, limit, key) => outboxRowSchema.array().parse(sql
40
40
  .exec(`SELECT * FROM durability_outbox
41
41
  WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
@@ -79,8 +79,6 @@ const infrastructureRetryMs = 60000;
79
79
  // setTimeout overflows past 2^31 - 1 ms, and it keeps every derived timestamp
80
80
  // far inside the safe-integer range the storage indexes accept.
81
81
  const maxDelayMs = 2 ** 31 - 1;
82
- const orphanBatchSize = 50;
83
- const channelsByEngine = new WeakMap();
84
82
  const rowId = (channel, key) => JSON.stringify([channel, key]);
85
83
  const entity = (row) => ({
86
84
  entityKind: 'outbox_entry',
@@ -88,33 +86,6 @@ const entity = (row) => ({
88
86
  id: row.entry_key,
89
87
  generation: row.generation_id,
90
88
  });
91
- /**
92
- * Rows of a channel no outbox registers would stay due forever and keep the
93
- * alarm awake, so they are postponed like a removed fanout target's work.
94
- */
95
- const postponeOrphans = async (engine, channels) => {
96
- const orphans = channelRowSchema
97
- .array()
98
- .parse(engine.sql
99
- .exec('SELECT DISTINCT channel FROM durability_outbox ORDER BY channel')
100
- .toArray())
101
- .map((row) => row.channel)
102
- .filter((channel) => !channels.has(channel));
103
- for (const channel of orphans) {
104
- const due = listDueForChannel(engine.sql, channel, Date.now(), orphanBatchSize);
105
- if (due.length === 0) {
106
- continue;
107
- }
108
- reportFailure('durability.outbox.channel_missing', { channel }, new Error(`Outbox channel "${channel}" is no longer configured; retaining entries`));
109
- // eslint-disable-next-line no-await-in-loop
110
- await engine.transaction((sql) => {
111
- const postponedTo = Date.now() + infrastructureRetryMs;
112
- for (const row of due) {
113
- updateAttempt(sql, outboxTable, { key: row.id, generation: row.generation_id, attempt: row.attempt }, 'next_attempt_at = ?', postponedTo);
114
- }
115
- });
116
- }
117
- };
118
89
  /**
119
90
  * A composable DO capability: keyed latest-state publication. Stage the
120
91
  * newest value for a key in the same transaction as the state it describes;
@@ -134,10 +105,7 @@ export class DurabilityOutbox {
134
105
  _DurabilityOutbox_emit.set(this, void 0);
135
106
  /** The channel's one publish run, never one still waiting for a permit. */
136
107
  _DurabilityOutbox_running.set(this, void 0);
137
- __classPrivateFieldSet(this, _DurabilityOutbox_channel, config.channel ?? 'default', "f");
138
- if (typeof __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f") !== 'string' || __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f").length === 0) {
139
- throw new TypeError('Outbox channel must be a non-empty string');
140
- }
108
+ __classPrivateFieldSet(this, _DurabilityOutbox_channel, assertChannel('Outbox', config.channel ?? defaultChannel), "f");
141
109
  if (typeof config.publish !== 'function') {
142
110
  throw new TypeError('Outbox requires a publish function');
143
111
  }
@@ -215,24 +183,12 @@ _DurabilityOutbox_engine = new WeakMap(), _DurabilityOutbox_channel = new WeakMa
215
183
  attempt: 0,
216
184
  });
217
185
  }, _DurabilityOutbox_attach = function _DurabilityOutbox_attach() {
218
- let channels = channelsByEngine.get(__classPrivateFieldGet(this, _DurabilityOutbox_engine, "f"));
219
- if (!channels) {
220
- const created = new Map();
221
- const engine = __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f");
222
- engine.register({
223
- table: outboxTable,
224
- runDue: async () => {
225
- await Promise.all([...created.values()].map((run) => run()));
226
- await postponeOrphans(engine, created);
227
- },
228
- });
229
- channelsByEngine.set(engine, created);
230
- channels = created;
231
- }
232
- if (channels.has(__classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"))) {
233
- throw new Error(`An outbox for channel "${__classPrivateFieldGet(this, _DurabilityOutbox_channel, "f")}" is already attached to this scheduler`);
234
- }
235
- channels.set(__classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"), () => __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_runDue).call(this));
186
+ __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").registerChannel({
187
+ table: outboxTable,
188
+ channel: __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"),
189
+ helper: 'An outbox',
190
+ runDue: () => __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_runDue).call(this),
191
+ });
236
192
  }, _DurabilityOutbox_firstAttemptAt = function _DurabilityOutbox_firstAttemptAt(createdAt) {
237
193
  if (__classPrivateFieldGet(this, _DurabilityOutbox_batchDelayMs, "f") === 0) {
238
194
  return createdAt;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Ordered SQLite migrations for outbox channels. Applied automatically on
3
+ * construction.
4
+ */
5
+ export declare const durabilityOutboxMigrations: {
6
+ name: string;
7
+ up: string;
8
+ down: string;
9
+ }[];
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Ordered SQLite migrations for outbox channels. Applied automatically on
3
+ * construction.
4
+ */
5
+ export const durabilityOutboxMigrations = [
6
+ {
7
+ name: 'durability_0006_create_outbox',
8
+ up: `
9
+ CREATE TABLE IF NOT EXISTS durability_outbox (
10
+ id TEXT PRIMARY KEY,
11
+ channel TEXT NOT NULL,
12
+ entry_key TEXT NOT NULL,
13
+ version INTEGER NOT NULL,
14
+ payload TEXT NOT NULL,
15
+ status TEXT NOT NULL CHECK (status IN ('pending')),
16
+ attempt INTEGER NOT NULL DEFAULT 0,
17
+ next_attempt_at INTEGER NOT NULL,
18
+ last_error TEXT,
19
+ last_error_name TEXT,
20
+ created_at INTEGER NOT NULL,
21
+ generation_id TEXT NOT NULL
22
+ );
23
+ CREATE INDEX IF NOT EXISTS durability_outbox_due_idx
24
+ ON durability_outbox (channel, status, next_attempt_at, version);
25
+ CREATE INDEX IF NOT EXISTS durability_outbox_pending_idx
26
+ ON durability_outbox (status, next_attempt_at);
27
+ CREATE INDEX IF NOT EXISTS durability_outbox_created_idx
28
+ ON durability_outbox (created_at);
29
+ CREATE TABLE IF NOT EXISTS durability_outbox_seq (
30
+ id INTEGER PRIMARY KEY CHECK (id = 1),
31
+ next_version INTEGER NOT NULL
32
+ );
33
+ INSERT OR IGNORE INTO durability_outbox_seq (id, next_version) VALUES (1, 1);
34
+ `,
35
+ down: `
36
+ DROP INDEX IF EXISTS durability_outbox_due_idx;
37
+ DROP INDEX IF EXISTS durability_outbox_pending_idx;
38
+ DROP INDEX IF EXISTS durability_outbox_created_idx;
39
+ DROP TABLE IF EXISTS durability_outbox;
40
+ DROP TABLE IF EXISTS durability_outbox_seq;
41
+ `,
42
+ },
43
+ ];
@@ -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 _RoutingClient_instances, _RoutingClient_config, _RoutingClient_observations, _RoutingClient_limit, _RoutingClient_width, _RoutingClient_lastResize, _RoutingClient_select, _RoutingClient_resize;
13
- import { RoutingError } from './errors.js';
13
+ import { RoutingError } from '../errors.js';
14
14
  /**
15
15
  * Transport-independent selection and observation. Every push invokes ONE
16
16
  * shard once. A false result is returned unchanged; transport failures throw.
@@ -1,9 +1,9 @@
1
1
  import { type TransformContext, type TransformStub } from '@durability/transforms';
2
- import { type DurableObjectTarget } from './durable-object-target.js';
2
+ import { type DurableObjectTarget } from '../durable-object-target.js';
3
3
  import { RoutingLoad, type EnqueueResult } from './load.js';
4
- import { type Sharding } from './routing-client.js';
4
+ import { type Sharding } from './client.js';
5
5
  export type { EnqueueResult, LoadSnapshot, ProcessingLoad } from './load.js';
6
- export type { RoutingAddress, RoutingTarget, Sharding, } from './routing-client.js';
6
+ export type { RoutingAddress, RoutingTarget, Sharding } from './client.js';
7
7
  export type RoutingSession<Input, Value, Context = unknown> = {
8
8
  push(input: Input, context?: Context): Promise<EnqueueResult<Value>>;
9
9
  setContext(context: TransformContext): {
@@ -1,8 +1,8 @@
1
1
  import { createTransformStub, } from '@durability/transforms';
2
- import { resolveNamespace, } from './durable-object-target.js';
3
- import { RoutingError } from './errors.js';
2
+ import { resolveNamespace, } from '../durable-object-target.js';
3
+ import { RoutingError } from '../errors.js';
4
4
  import { RoutingLoad } from './load.js';
5
- import { RoutingClient } from './routing-client.js';
5
+ import { RoutingClient } from './client.js';
6
6
  /**
7
7
  * Inside a DO, attach this local load observer to a fanout capability. Outside
8
8
  * the DO, client() routes calls to an application-owned class using ordinary