durability 5.1.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.
Files changed (98) hide show
  1. package/README.md +154 -21
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
  3. package/dist/{durability-alarms.js → alarms/index.js} +85 -74
  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} +91 -83
  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} +27 -20
  16. package/dist/{log.js → log/index.js} +200 -166
  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} +23 -21
  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 +12 -628
  25. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  26. package/dist/{durability.js → operations/index.js} +76 -70
  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} +35 -77
  31. package/dist/outbox/migrations.d.ts +9 -0
  32. package/dist/outbox/migrations.js +43 -0
  33. package/dist/policy.d.ts +19 -19
  34. package/dist/policy.js +13 -12
  35. package/dist/{routing-client.js → routing/client.js} +1 -1
  36. package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
  37. package/dist/{routing.js → routing/index.js} +3 -3
  38. package/dist/{load.d.ts → routing/load.d.ts} +1 -1
  39. package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
  40. package/dist/{schedule.js → schedule/index.js} +97 -83
  41. package/dist/schedule/migrations.d.ts +9 -0
  42. package/dist/schedule/migrations.js +118 -0
  43. package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
  44. package/dist/{scheduler.js → scheduler/index.js} +308 -70
  45. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  46. package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
  47. package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
  48. package/dist/{semaphore.js → semaphore/index.js} +7 -7
  49. package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
  50. package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
  51. package/dist/semaphore/migrations.d.ts +18 -0
  52. package/dist/semaphore/migrations.js +444 -0
  53. package/dist/semaphore/protocol.d.ts +451 -0
  54. package/dist/semaphore/protocol.js +114 -0
  55. package/dist/semaphore/queue-store.d.ts +107 -0
  56. package/dist/semaphore/queue-store.js +258 -0
  57. package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
  58. package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
  59. package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
  60. package/dist/{semaphore-server.js → semaphore/server.js} +56 -28
  61. package/dist/semaphore/store.d.ts +53 -0
  62. package/dist/semaphore/store.js +107 -0
  63. package/dist/serialization.d.ts +5 -0
  64. package/dist/serialization.js +41 -16
  65. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  66. package/dist/{client.js → socket/client.js} +4 -4
  67. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  68. package/dist/{socket.js → socket/index.js} +66 -20
  69. package/dist/socket/migrations.d.ts +9 -0
  70. package/dist/socket/migrations.js +57 -0
  71. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  72. package/dist/storage.d.ts +4 -0
  73. package/dist/storage.js +22 -15
  74. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  75. package/dist/{topology-client.js → topology/client.js} +3 -3
  76. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  77. package/dist/{topology.js → topology/index.js} +106 -72
  78. package/dist/topology/migrations.d.ts +10 -0
  79. package/dist/topology/migrations.js +138 -0
  80. package/dist/topology/snapshot.d.ts +50 -0
  81. package/dist/topology/snapshot.js +38 -0
  82. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  83. package/dist/{tracker.js → tracker/index.js} +9 -9
  84. package/package.json +11 -11
  85. package/dist/semaphore-protocol.d.ts +0 -231
  86. package/dist/semaphore-protocol.js +0 -106
  87. package/dist/semaphore-queue-store.d.ts +0 -95
  88. package/dist/semaphore-queue-store.js +0 -252
  89. package/dist/semaphore-store.d.ts +0 -43
  90. package/dist/semaphore-store.js +0 -108
  91. package/dist/topology-snapshot.d.ts +0 -70
  92. package/dist/topology-snapshot.js +0 -48
  93. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  94. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  95. /package/dist/{load.js → routing/load.js} +0 -0
  96. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  97. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  98. /package/dist/{tracked-source.js → tracker/source.js} +0 -0
@@ -9,34 +9,35 @@ 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 _DurabilityAlarmsCore_instances, _DurabilityAlarmsCore_engine, _DurabilityAlarmsCore_handlers, _DurabilityAlarmsCore_policy, _DurabilityAlarmsCore_lifecycle, _DurabilityAlarmsCore_emit, _DurabilityAlarmsCore_active, _DurabilityAlarmsCore_trackedSource, _DurabilityAlarmsCore_runDue, _DurabilityAlarmsCore_schedule, _DurabilityAlarmsCore_cancel, _DurabilityAlarmsCore_execute;
13
- import { z } from 'zod';
14
- import { traceFunction, traceMethods } from './dev.js';
15
- import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from './errors.js';
16
- import { createLifecycleHooks, } from './lifecycle.js';
17
- import { durabilityNamedAlarmMigrations, durabilityMemoMigrations, migrateCapability, } from './migrations.js';
18
- import { alarmMethodOptionsSchema, createPolicyResolver, } from './policy.js';
19
- import { engineFor, } from './scheduler.js';
20
- import { createMemoize, deleteMemos, deleteMemosOfDroppedTable, memoizedKeys, } from './memoize.js';
21
- import { deleteAttempt, listCreated } from './storage.js';
22
- import { registerTrackedSource, } from './tracked-source.js';
23
- const alarmRowSchema = z.object({
24
- name: z.string(),
25
- generation_id: z.string(),
26
- status: z.enum(['pending', 'failed']),
27
- scheduled_at: z.number().int().nonnegative(),
28
- next_attempt_at: z.number().int().nonnegative(),
29
- attempt: z.number().int().nonnegative(),
30
- last_error: z.string().nullable(),
31
- last_error_name: z.string().nullable(),
32
- created_at: z.number().int().nonnegative(),
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 * as v from 'valibot';
14
+ import { traceFunction, traceMethods } from '../dev.js';
15
+ import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from '../errors.js';
16
+ import { createLifecycleHooks, } from '../lifecycle.js';
17
+ import { durabilityNamedAlarmMigrations } from './migrations.js';
18
+ import { durabilityMemoMigrations } from '../memoize/migrations.js';
19
+ import { migrateCapability, } from '../migrations.js';
20
+ import { alarmMethodOptionsSchema, createPolicyResolver, } from '../policy.js';
21
+ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
22
+ import { createMemoize, deleteMemos, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
23
+ import { deleteAttempt, listCreated } from '../storage.js';
24
+ import { registerTrackedSource, } from '../tracker/source.js';
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)),
33
36
  });
34
37
  const alarmsTable = { name: 'durability_alarms', key: 'name' };
35
- const alarmHandlerSchema = z.custom((value) => typeof value === 'function');
36
- const readAlarm = (sql, name) => alarmRowSchema
37
- .optional()
38
- .parse(sql
39
- .exec('SELECT * FROM durability_alarms WHERE name = ?', name)
38
+ const alarmHandlerSchema = v.custom((value) => typeof value === 'function');
39
+ const readAlarm = (sql, channel, name) => v.parse(v.optional(alarmRowSchema), sql
40
+ .exec('SELECT * FROM durability_alarms WHERE channel = ? AND name = ?', channel, name)
40
41
  .toArray()[0]);
41
42
  const alarmRecord = (sql, row) => ({
42
43
  id: row.name,
@@ -49,7 +50,7 @@ const alarmRecord = (sql, row) => ({
49
50
  attempt: row.attempt,
50
51
  nextAttemptAt: row.next_attempt_at,
51
52
  lastError: row.last_error,
52
- memoized: memoizedKeys(sql, alarmsTable, row.name, row.generation_id),
53
+ memoized: memoizedKeys(sql, { ...alarmsTable, channel: row.channel }, row.name, row.generation_id),
53
54
  }
54
55
  : {
55
56
  status: 'failed',
@@ -70,13 +71,17 @@ class DurabilityAlarmsCore {
70
71
  constructor(config) {
71
72
  _DurabilityAlarmsCore_instances.add(this);
72
73
  _DurabilityAlarmsCore_engine.set(this, void 0);
74
+ _DurabilityAlarmsCore_channel.set(this, void 0);
75
+ _DurabilityAlarmsCore_table.set(this, void 0);
73
76
  _DurabilityAlarmsCore_handlers.set(this, void 0);
74
77
  _DurabilityAlarmsCore_policy.set(this, void 0);
75
78
  _DurabilityAlarmsCore_lifecycle.set(this, void 0);
76
79
  _DurabilityAlarmsCore_emit.set(this, void 0);
77
80
  _DurabilityAlarmsCore_active.set(this, new Map());
81
+ __classPrivateFieldSet(this, _DurabilityAlarmsCore_channel, assertChannel('DurabilityAlarms', config.channel ?? defaultChannel), "f");
82
+ __classPrivateFieldSet(this, _DurabilityAlarmsCore_table, { ...alarmsTable, channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f") }, "f");
78
83
  __classPrivateFieldSet(this, _DurabilityAlarmsCore_engine, engineFor(config), "f");
79
- __classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name, alarmHandlerSchema.parse(handler)])), "f");
84
+ __classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name, v.parse(alarmHandlerSchema, handler)])), "f");
80
85
  __classPrivateFieldSet(this, _DurabilityAlarmsCore_policy, createPolicyResolver(config, config.methods ?? {}, alarmMethodOptionsSchema), "f");
81
86
  __classPrivateFieldSet(this, _DurabilityAlarmsCore_lifecycle, createLifecycleHooks((hook) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").emitter(hook), config.onLifecycleEvent), "f");
82
87
  __classPrivateFieldSet(this, _DurabilityAlarmsCore_emit, __classPrivateFieldGet(this, _DurabilityAlarmsCore_lifecycle, "f").emit, "f");
@@ -95,8 +100,10 @@ class DurabilityAlarmsCore {
95
100
  }
96
101
  __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").migrateSchema('namedAlarms', durabilityNamedAlarmMigrations);
97
102
  __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").migrateSchema('memos', durabilityMemoMigrations);
98
- __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").register({
99
- table: alarmsTable,
103
+ __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").registerChannel({
104
+ table: __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"),
105
+ channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"),
106
+ helper: 'DurabilityAlarms',
100
107
  runDue: (now, info) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_instances, "m", _DurabilityAlarmsCore_runDue).call(this, now, info),
101
108
  });
102
109
  registerTrackedSource(this, __classPrivateFieldGet(this, _DurabilityAlarmsCore_instances, "m", _DurabilityAlarmsCore_trackedSource).call(this));
@@ -123,7 +130,7 @@ class DurabilityAlarmsCore {
123
130
  * timestamp, regardless of status, and returns how many were removed.
124
131
  */
125
132
  async purgeBefore(before) {
126
- const count = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").purge(alarmsTable, before, __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f"), 'Durable alarm was purged');
133
+ const count = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").purge(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), before, __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f"), 'Durable alarm was purged');
127
134
  __classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f").call(this, {
128
135
  type: 'purged',
129
136
  entityKind: 'named_alarm',
@@ -134,48 +141,49 @@ class DurabilityAlarmsCore {
134
141
  return count;
135
142
  }
136
143
  }
137
- _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = new WeakMap(), _DurabilityAlarmsCore_policy = new WeakMap(), _DurabilityAlarmsCore_lifecycle = new WeakMap(), _DurabilityAlarmsCore_emit = new WeakMap(), _DurabilityAlarmsCore_active = new WeakMap(), _DurabilityAlarmsCore_instances = new WeakSet(), _DurabilityAlarmsCore_trackedSource = function _DurabilityAlarmsCore_trackedSource() {
144
+ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = new WeakMap(), _DurabilityAlarmsCore_table = new WeakMap(), _DurabilityAlarmsCore_handlers = new WeakMap(), _DurabilityAlarmsCore_policy = new WeakMap(), _DurabilityAlarmsCore_lifecycle = new WeakMap(), _DurabilityAlarmsCore_emit = new WeakMap(), _DurabilityAlarmsCore_active = new WeakMap(), _DurabilityAlarmsCore_instances = new WeakSet(), _DurabilityAlarmsCore_trackedSource = function _DurabilityAlarmsCore_trackedSource() {
138
145
  const sql = __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql;
139
146
  return {
140
147
  get: async (name) => {
141
- const row = readAlarm(sql, name);
148
+ const row = readAlarm(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), name);
142
149
  return row && alarmRecord(sql, row);
143
150
  },
144
151
  list: async ({ limit, cursor, status, name }) => {
145
- const page = listCreated(sql, alarmsTable, 'name', {
152
+ const page = listCreated(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), 'name', {
146
153
  limit,
147
154
  after: cursor,
148
155
  status,
149
156
  name,
150
157
  });
151
- const records = alarmRowSchema
152
- .array()
153
- .parse(page.rows)
158
+ const records = v
159
+ .parse(v.array(alarmRowSchema), page.rows)
154
160
  .map((row) => alarmRecord(sql, row));
155
161
  return page.next === undefined
156
162
  ? { records }
157
163
  : { records, cursor: page.next };
158
164
  },
159
- watch: (name, listener) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").watch(alarmsTable, name, (completed) => {
160
- if (!completed) {
161
- listener();
162
- return;
163
- }
164
- listener({
165
- id: name,
166
- name,
167
- generation: completed.generation_id,
168
- createdAt: completed.created_at,
169
- state: { status: 'completed', result: undefined },
165
+ watch: (name, listener) => {
166
+ return __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").watch(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), name, (completed) => {
167
+ if (!completed) {
168
+ listener();
169
+ return;
170
+ }
171
+ listener({
172
+ id: name,
173
+ name,
174
+ generation: completed.generation_id,
175
+ createdAt: completed.created_at,
176
+ state: { status: 'completed', result: undefined },
177
+ });
170
178
  });
171
- }),
179
+ },
172
180
  };
173
181
  }, _DurabilityAlarmsCore_runDue = async function _DurabilityAlarmsCore_runDue(now, info) {
174
- const due = alarmRowSchema.array().parse(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql
182
+ const due = v.parse(v.array(alarmRowSchema), __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql
175
183
  .exec(`SELECT * FROM durability_alarms
176
- WHERE status = 'pending' AND next_attempt_at <= ?
184
+ WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
177
185
  ORDER BY next_attempt_at ASC
178
- LIMIT 100`, now)
186
+ LIMIT 100`, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), now)
179
187
  .toArray());
180
188
  // With liveTimers a running occurrence re-arms itself when it settles, so
181
189
  // skipping it keeps a long handler from blocking a concurrent alarm pass.
@@ -198,14 +206,15 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
198
206
  if (!Number.isSafeInteger(scheduledTime) || scheduledTime < 0) {
199
207
  throw new RangeError('scheduledTime must be a non-negative safe integer');
200
208
  }
209
+ const key = name;
201
210
  const generation = crypto.randomUUID();
202
211
  const createdAt = Date.now();
203
212
  await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").transaction((sql) => {
204
213
  sql.exec(`INSERT INTO durability_alarms
205
- (name, generation_id, status, scheduled_at, next_attempt_at,
206
- attempt, last_error, last_error_name, created_at)
207
- VALUES (?, ?, 'pending', ?, ?, 0, NULL, NULL, ?)
208
- ON CONFLICT(name) DO UPDATE SET
214
+ (name, channel, generation_id, status, scheduled_at,
215
+ next_attempt_at, attempt, last_error, last_error_name, created_at)
216
+ VALUES (?, ?, ?, 'pending', ?, ?, 0, NULL, NULL, ?)
217
+ ON CONFLICT(channel, name) DO UPDATE SET
209
218
  generation_id = excluded.generation_id,
210
219
  status = excluded.status,
211
220
  scheduled_at = excluded.scheduled_at,
@@ -213,12 +222,12 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
213
222
  attempt = excluded.attempt,
214
223
  last_error = excluded.last_error,
215
224
  last_error_name = excluded.last_error_name,
216
- created_at = excluded.created_at`, name, generation, scheduledTime, scheduledTime, createdAt);
225
+ created_at = excluded.created_at`, key, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), generation, scheduledTime, scheduledTime, createdAt);
217
226
  // Memoized results belong to the replaced occurrence, never to the new one.
218
- deleteMemos(sql, alarmsTable, name);
227
+ deleteMemos(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
219
228
  });
220
229
  // A waiter on the replaced occurrence resolves now instead of at its timeout.
221
- __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(alarmsTable, name);
230
+ __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
222
231
  __classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f").call(this, {
223
232
  type: 'scheduled',
224
233
  entityKind: 'named_alarm',
@@ -230,14 +239,15 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
230
239
  scheduledTime,
231
240
  });
232
241
  }, _DurabilityAlarmsCore_cancel = async function _DurabilityAlarmsCore_cancel(name) {
242
+ const key = name;
233
243
  const cancelled = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").transaction((sql) => {
234
244
  const removed = sql
235
245
  .exec(`DELETE FROM durability_alarms
236
- WHERE name = ? AND status = 'pending'
237
- RETURNING name`, name)
246
+ WHERE channel = ? AND name = ? AND status = 'pending'
247
+ RETURNING name`, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), key)
238
248
  .toArray().length > 0;
239
249
  if (removed) {
240
- deleteMemos(sql, alarmsTable, name);
250
+ deleteMemos(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
241
251
  }
242
252
  return removed;
243
253
  });
@@ -247,42 +257,43 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
247
257
  // Every settlement write is conditional on the deleted row, so a running
248
258
  // attempt can no longer retry; abort it as purging does.
249
259
  __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f")
250
- .get(name)
260
+ .get(key)
251
261
  ?.controller?.abort(new Error(`Durable alarm "${name}" was cancelled`));
252
- __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(alarmsTable, name);
262
+ __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
253
263
  return true;
254
264
  }, _DurabilityAlarmsCore_execute = function _DurabilityAlarmsCore_execute(row, platform) {
255
265
  const running = __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f").get(row.name);
256
266
  if (running) {
257
267
  return running.settled;
258
268
  }
259
- const policy = __classPrivateFieldGet(this, _DurabilityAlarmsCore_policy, "f").call(this, row.name);
269
+ const name = row.name;
270
+ const policy = __classPrivateFieldGet(this, _DurabilityAlarmsCore_policy, "f").call(this, name);
260
271
  const retryTimeouts = policy.method?.retryTimeouts ?? false;
261
272
  return __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").startExecution({
262
- table: alarmsTable,
273
+ table: __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"),
263
274
  key: row.name,
264
275
  row,
265
- label: `named alarm "${row.name}"`,
276
+ label: `named alarm "${name}"`,
266
277
  entity: alarmEntity(row),
267
278
  active: __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f"),
268
279
  policy,
269
280
  emit: __classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f"),
270
- exhaustedError: () => new DurableAttemptsExhaustedError('named alarm', row.name, policy.maxAttempts),
271
- timeoutError: () => new DurableAlarmTimeoutError(row.name, policy.attemptTimeoutMs),
281
+ exhaustedError: () => new DurableAttemptsExhaustedError('named alarm', name, policy.maxAttempts),
282
+ timeoutError: () => new DurableAlarmTimeoutError(name, policy.attemptTimeoutMs),
272
283
  invoke: (attempt, signal) => {
273
- const handler = __classPrivateFieldGet(this, _DurabilityAlarmsCore_handlers, "f").get(row.name);
284
+ const handler = __classPrivateFieldGet(this, _DurabilityAlarmsCore_handlers, "f").get(name);
274
285
  if (!handler) {
275
- throw new NonRetryableError(`No named alarm handler registered for "${row.name}"`);
286
+ throw new NonRetryableError(`No named alarm handler registered for "${name}"`);
276
287
  }
277
288
  return handler({
278
- name: row.name,
289
+ name,
279
290
  scheduledTime: row.scheduled_at,
280
291
  attempt,
281
292
  isRetry: attempt > 1,
282
293
  retryCount: attempt - 1,
283
294
  idempotencyKey: `durability-alarm:v1:${row.generation_id}`,
284
295
  signal,
285
- memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, alarmsTable, {
296
+ memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), {
286
297
  key: row.name,
287
298
  generation: row.generation_id,
288
299
  attempt,
@@ -292,7 +303,7 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
292
303
  },
293
304
  complete: (_result, attempt) => {
294
305
  const timestamp = Date.now();
295
- const removed = deleteAttempt(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, alarmsTable, {
306
+ const removed = deleteAttempt(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), {
296
307
  key: row.name,
297
308
  generation: row.generation_id,
298
309
  attempt,
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Ordered SQLite migrations for the `durability_alarms` table owned by
3
+ * {@link DurabilityAlarms}. Applied automatically on construction.
4
+ */
5
+ export declare const durabilityNamedAlarmMigrations: {
6
+ name: string;
7
+ up: string;
8
+ down: string;
9
+ }[];
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Ordered SQLite migrations for the `durability_alarms` table owned by
3
+ * {@link DurabilityAlarms}. Applied automatically on construction.
4
+ */
5
+ export const durabilityNamedAlarmMigrations = [
6
+ {
7
+ name: 'durability_0003_create_alarms',
8
+ up: `
9
+ CREATE TABLE IF NOT EXISTS durability_alarms (
10
+ name TEXT PRIMARY KEY,
11
+ generation_id TEXT NOT NULL,
12
+ status TEXT NOT NULL CHECK (status IN ('pending', 'failed')),
13
+ scheduled_at INTEGER NOT NULL,
14
+ next_attempt_at INTEGER NOT NULL,
15
+ attempt INTEGER NOT NULL DEFAULT 0,
16
+ last_error TEXT,
17
+ last_error_name TEXT
18
+ );
19
+ CREATE INDEX IF NOT EXISTS durability_alarms_pending_idx
20
+ ON durability_alarms (status, next_attempt_at);
21
+ `,
22
+ down: `
23
+ DROP INDEX IF EXISTS durability_alarms_pending_idx;
24
+ DROP TABLE IF EXISTS durability_alarms;
25
+ `,
26
+ },
27
+ {
28
+ name: 'durability_0004_alarms_generation_and_created_at',
29
+ up: `
30
+ DROP INDEX IF EXISTS durability_alarms_pending_idx;
31
+ ALTER TABLE durability_alarms RENAME TO durability_alarms_v3;
32
+ CREATE TABLE durability_alarms (
33
+ name TEXT PRIMARY KEY,
34
+ generation_id TEXT NOT NULL,
35
+ status TEXT NOT NULL CHECK (status IN ('pending', 'failed')),
36
+ scheduled_at INTEGER NOT NULL,
37
+ next_attempt_at INTEGER NOT NULL,
38
+ attempt INTEGER NOT NULL DEFAULT 0,
39
+ last_error TEXT,
40
+ last_error_name TEXT,
41
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
42
+ );
43
+ INSERT INTO durability_alarms
44
+ SELECT name, generation_id, status, scheduled_at, next_attempt_at,
45
+ attempt, last_error, last_error_name,
46
+ CAST(unixepoch('subsec') * 1000 AS INTEGER)
47
+ FROM durability_alarms_v3;
48
+ DROP TABLE durability_alarms_v3;
49
+ CREATE INDEX durability_alarms_pending_idx
50
+ ON durability_alarms (status, next_attempt_at);
51
+ CREATE INDEX durability_alarms_created_idx
52
+ ON durability_alarms (created_at);
53
+ `,
54
+ down: `
55
+ DROP INDEX IF EXISTS durability_alarms_pending_idx;
56
+ DROP INDEX IF EXISTS durability_alarms_created_idx;
57
+ ALTER TABLE durability_alarms RENAME TO durability_alarms_v4;
58
+ CREATE TABLE durability_alarms (
59
+ name TEXT PRIMARY KEY,
60
+ generation_id TEXT NOT NULL,
61
+ status TEXT NOT NULL CHECK (status IN ('pending', 'failed')),
62
+ scheduled_at INTEGER NOT NULL,
63
+ next_attempt_at INTEGER NOT NULL,
64
+ attempt INTEGER NOT NULL DEFAULT 0,
65
+ last_error TEXT,
66
+ last_error_name TEXT
67
+ );
68
+ INSERT INTO durability_alarms
69
+ SELECT name, generation_id, status, scheduled_at, next_attempt_at,
70
+ attempt, last_error, last_error_name
71
+ FROM durability_alarms_v4;
72
+ DROP TABLE durability_alarms_v4;
73
+ CREATE INDEX durability_alarms_pending_idx
74
+ ON durability_alarms (status, next_attempt_at);
75
+ `,
76
+ },
77
+ {
78
+ name: 'durability_0016_alarms_channel',
79
+ up: `
80
+ ALTER TABLE durability_alarms RENAME TO durability_alarms_before_channels;
81
+ CREATE TABLE durability_alarms (
82
+ name TEXT NOT NULL,
83
+ generation_id TEXT NOT NULL,
84
+ status TEXT NOT NULL CHECK (status IN ('pending', 'failed')),
85
+ scheduled_at INTEGER NOT NULL,
86
+ next_attempt_at INTEGER NOT NULL,
87
+ attempt INTEGER NOT NULL DEFAULT 0,
88
+ last_error TEXT,
89
+ last_error_name TEXT,
90
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER)),
91
+ channel TEXT NOT NULL DEFAULT 'default',
92
+ PRIMARY KEY (channel, name)
93
+ );
94
+ INSERT INTO durability_alarms
95
+ SELECT *, 'default' FROM durability_alarms_before_channels;
96
+ DROP TABLE durability_alarms_before_channels;
97
+ CREATE INDEX durability_alarms_pending_idx
98
+ ON durability_alarms (status, next_attempt_at);
99
+ CREATE INDEX durability_alarms_created_idx
100
+ ON durability_alarms (created_at);
101
+ CREATE INDEX durability_alarms_channel_pending_idx
102
+ ON durability_alarms (channel, status, next_attempt_at);
103
+ `,
104
+ down: `
105
+ CREATE TABLE durability_alarms_channel_guard (
106
+ channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
107
+ );
108
+ INSERT INTO durability_alarms_channel_guard SELECT DISTINCT channel FROM durability_alarms;
109
+ DROP TABLE durability_alarms_channel_guard;
110
+ ALTER TABLE durability_alarms RENAME TO durability_alarms_with_channels;
111
+ CREATE TABLE durability_alarms (
112
+ name TEXT PRIMARY KEY,
113
+ generation_id TEXT NOT NULL,
114
+ status TEXT NOT NULL CHECK (status IN ('pending', 'failed')),
115
+ scheduled_at INTEGER NOT NULL,
116
+ next_attempt_at INTEGER NOT NULL,
117
+ attempt INTEGER NOT NULL DEFAULT 0,
118
+ last_error TEXT,
119
+ last_error_name TEXT,
120
+ created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
121
+ );
122
+ INSERT INTO durability_alarms
123
+ SELECT name, generation_id, status, scheduled_at, next_attempt_at,
124
+ attempt, last_error, last_error_name, created_at
125
+ FROM durability_alarms_with_channels;
126
+ DROP TABLE durability_alarms_with_channels;
127
+ CREATE INDEX durability_alarms_pending_idx
128
+ ON durability_alarms (status, next_attempt_at);
129
+ CREATE INDEX durability_alarms_created_idx
130
+ ON durability_alarms (created_at);
131
+ `,
132
+ },
133
+ ];
@@ -4,16 +4,16 @@
4
4
  *
5
5
  * @packageDocumentation
6
6
  */
7
- export { DurabilityTracker, type DurabilityTrackerConfig, type TrackedHandle, type TrackedItem, type TrackedResult, type TrackedWaitOptions, type TrackerHandle, type TrackerPage, type TrackerQuery, type TrackerRecord, type TrackerRecordPage, } from './tracker.js';
8
- export type { TrackedEntry, TrackedPage, TrackedQuery, TrackedRecord, TrackedResults, TrackedSource, TrackedState, TrackedStatus, } from './tracked-source.js';
9
- export { DurabilityOutbox, type DurabilityOutboxConfig, type DurabilityOutboxEvent, type OutboxEntry, } from './outbox.js';
7
+ export { DurabilityTracker, type DurabilityTrackerConfig, type TrackedHandle, type TrackedItem, type TrackedResult, type TrackedWaitOptions, type TrackerHandle, type TrackerPage, type TrackerQuery, type TrackerRecord, type TrackerRecordPage, } from './tracker/index.js';
8
+ export type { TrackedEntry, TrackedPage, TrackedQuery, TrackedRecord, TrackedResults, TrackedSource, TrackedState, TrackedStatus, } from './tracker/source.js';
9
+ export { DurabilityOutbox, type DurabilityOutboxConfig, type DurabilityOutboxEvent, type OutboxEntry, } from './outbox/index.js';
10
10
  export { OutboxTimeoutError } from './errors.js';
11
- export { durabilityOutboxMigrations } from './migrations.js';
12
- export { DurabilityTopology, type DurabilityTopologyConfig, type DurabilityTopologyEvent, type TopologyChannel, type TopologyCurrent, type TopologyDraft, type TopologyPublisher, type TopologyRetryOptions, type TopologyRole, type TopologyStateUpdate, } from './topology.js';
13
- export type { TopologyClient, TopologyClientConfig, TopologySource, TopologyView, } from './topology-client.js';
14
- export type { TopologyMember, TopologyMemberInput, TopologyMemberState, TopologySnapshot, } from './topology-snapshot.js';
11
+ export { durabilityOutboxMigrations } from './outbox/migrations.js';
12
+ export { DurabilityTopology, type DurabilityTopologyConfig, type DurabilityTopologyEvent, type TopologyChannel, type TopologyCurrent, type TopologyDraft, type TopologyPublisher, type TopologyRetryOptions, type TopologyRole, type TopologyStateUpdate, } from './topology/index.js';
13
+ export type { TopologyClient, TopologyClientConfig, TopologySource, TopologyView, } from './topology/client.js';
14
+ export type { TopologyMember, TopologyMemberInput, TopologyMemberState, TopologySnapshot, } from './topology/snapshot.js';
15
15
  export { TopologyError, TopologyUnavailableError, type TopologyFallbackReason, } from './errors.js';
16
- export { durabilityTopologyMigrations } from './migrations.js';
17
- export { DurabilitySchedule, type DurabilityScheduleConfig, type DurabilityScheduleEvent, type ScheduleDefinition, type ScheduledOutcomeDelivery, type ScheduledRun, type ScheduledRunContext, type ScheduledRunOutcome, type ScheduleState, } from './schedule.js';
16
+ export { durabilityTopologyMigrations } from './topology/migrations.js';
17
+ export { DurabilitySchedule, type DurabilityScheduleConfig, type DurabilityScheduleEvent, type ScheduleDefinition, type ScheduledOutcomeDelivery, type ScheduledRun, type ScheduledRunContext, type ScheduledRunOutcome, type ScheduleState, } from './schedule/index.js';
18
18
  export { ScheduledOutcomeTimeoutError, ScheduledRunInterruptedError, ScheduledRunTimeoutError, } from './errors.js';
19
- export { durabilityScheduleMigrations } from './migrations.js';
19
+ export { durabilityScheduleMigrations } from './schedule/migrations.js';
@@ -4,13 +4,13 @@
4
4
  *
5
5
  * @packageDocumentation
6
6
  */
7
- export { DurabilityTracker, } from './tracker.js';
8
- export { DurabilityOutbox, } from './outbox.js';
7
+ export { DurabilityTracker, } from './tracker/index.js';
8
+ export { DurabilityOutbox, } from './outbox/index.js';
9
9
  export { OutboxTimeoutError } from './errors.js';
10
- export { durabilityOutboxMigrations } from './migrations.js';
11
- export { DurabilityTopology, } from './topology.js';
10
+ export { durabilityOutboxMigrations } from './outbox/migrations.js';
11
+ export { DurabilityTopology, } from './topology/index.js';
12
12
  export { TopologyError, TopologyUnavailableError, } from './errors.js';
13
- export { durabilityTopologyMigrations } from './migrations.js';
14
- export { DurabilitySchedule, } from './schedule.js';
13
+ export { durabilityTopologyMigrations } from './topology/migrations.js';
14
+ export { DurabilitySchedule, } from './schedule/index.js';
15
15
  export { ScheduledOutcomeTimeoutError, ScheduledRunInterruptedError, ScheduledRunTimeoutError, } from './errors.js';
16
- export { durabilityScheduleMigrations } from './migrations.js';
16
+ export { durabilityScheduleMigrations } from './schedule/migrations.js';
@@ -1,9 +1,9 @@
1
- import { type DurabilityLifecycleEvent, type LifecycleHook } from './lifecycle.js';
2
- import { RoutingLoad, type EnqueueResult, type LoadSnapshot } from './load.js';
1
+ import { type DurabilityLifecycleEvent, type LifecycleHook } from '../lifecycle.js';
2
+ import { RoutingLoad, type EnqueueResult, type LoadSnapshot } from '../routing/load.js';
3
3
  import type { MessageWrite } from './stored-message.js';
4
- import { type DurableMemoize } from './memoize.js';
5
- import { type ExecutionPolicyOptions } from './policy.js';
6
- import { type SchedulerAttachment } from './scheduler.js';
4
+ import { type DurableMemoize } from '../memoize/index.js';
5
+ import { type ExecutionPolicyOptions } from '../policy.js';
6
+ import { type SchedulerAttachment } from '../scheduler/index.js';
7
7
  export type FanoutInput<Body = unknown> = {
8
8
  id?: string;
9
9
  body: Body;
@@ -46,6 +46,13 @@ export type FanoutEnqueueOptions = {
46
46
  targets?: readonly string[];
47
47
  };
48
48
  export type DurabilityFanoutConfig<Body> = SchedulerAttachment & ExecutionPolicyOptions & {
49
+ /**
50
+ * Stable channel ID. Instances on one scheduler share storage, partitioned
51
+ * by channel; target IDs and deduplication keys are independent in each
52
+ * channel, so a library can attach its own fanout beside the application's.
53
+ * Defaults to `default`.
54
+ */
55
+ channel?: string;
49
56
  targets: Record<string, FanoutTarget<Body>>;
50
57
  routing?: RoutingLoad;
51
58
  /** Receives terminal failures. Without it, terminal deliveries are dropped. */
@@ -72,7 +79,7 @@ export type DurabilityFanoutEvent = Extract<DurabilityLifecycleEvent, {
72
79
  export declare class DurabilityFanout<Body = unknown> {
73
80
  #private;
74
81
  constructor(config: DurabilityFanoutConfig<Body>);
75
- static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("./migrations.js").DurabilityMigrationResult;
82
+ static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("../migrations.js").DurabilityMigrationResult;
76
83
  alarm(info?: AlarmInvocationInfo): Promise<void>;
77
84
  /**
78
85
  * Attaches a best-effort lifecycle hook alongside `onLifecycleEvent` and