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,17 +9,26 @@ 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 _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
12
+ var _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_ownerChannel, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
13
13
  import { z } from 'zod';
14
- import { reportFailure, TopologyError } from './errors.js';
15
- import { durabilityTopologyMigrations, migrateCapability, } from './migrations.js';
16
- import { DurabilityOutbox, discardWithin, publishNow, stageWithin, } from './outbox.js';
17
- import { assertPositiveInteger } from './policy.js';
18
- import { DurabilityScheduler, engineFor, } from './scheduler.js';
19
- import { canonical, deserialize, serialize } from './serialization.js';
20
- import { TopologyClient, } from './topology-client.js';
21
- import { encodeSnapshot, memberInputSchema, membersSchema, } from './topology-snapshot.js';
22
- const publicationChannel = 'durability.topology';
14
+ import { reportFailure, TopologyError } from '../errors.js';
15
+ import { durabilityTopologyMigrations } from './migrations.js';
16
+ import { migrateCapability } from '../migrations.js';
17
+ import { DurabilityOutbox, discardWithin, publishNow, stageWithin, } from '../outbox/index.js';
18
+ import { assertPositiveInteger } from '../policy.js';
19
+ import { assertChannel, defaultChannel, DurabilityScheduler, engineFor, } from '../scheduler/index.js';
20
+ import { canonical, deserialize, serialize } from '../serialization.js';
21
+ import { TopologyClient } from './client.js';
22
+ import { encodeSnapshot, memberInputSchema, membersSchema, } from './snapshot.js';
23
+ const outboxChannelPrefix = 'durability.topology';
24
+ // The default instance keeps the names it used before instance channels
25
+ // existed, so outbox rows staged by an older version are still delivered.
26
+ const publicationChannelFor = (ownerChannel) => ownerChannel === defaultChannel
27
+ ? outboxChannelPrefix
28
+ : JSON.stringify([outboxChannelPrefix, ownerChannel, 'publication']);
29
+ const stateChannelFor = (ownerChannel, state) => ownerChannel === defaultChannel
30
+ ? `${outboxChannelPrefix}.${state}`
31
+ : JSON.stringify([outboxChannelPrefix, ownerChannel, 'state', state]);
23
32
  const groupRowSchema = z.object({
24
33
  group_id: z.string(),
25
34
  version: z.number().int().positive(),
@@ -76,15 +85,16 @@ const outboxOptions = (timeoutMs, options, fallback) => {
76
85
  * land. KV has no conditional write, so some bound is required.
77
86
  */
78
87
  const landingWindowMs = 5 * 60000;
79
- const forgetWrite = (sql, id) => {
80
- sql.exec('DELETE FROM durability_topology_writes WHERE id = ?', id);
88
+ const forgetWrite = (sql, ownerChannel, id) => {
89
+ sql.exec('DELETE FROM durability_topology_writes WHERE owner_channel = ? AND id = ?', ownerChannel, id);
81
90
  };
82
91
  /** Drops expired records and returns when the group's last outstanding write can no longer land. */
83
- const outstandingWritesUntil = (sql, group, now) => {
84
- sql.exec('DELETE FROM durability_topology_writes WHERE group_id = ? AND land_by <= ?', group, now);
92
+ const outstandingWritesUntil = (sql, ownerChannel, group, now) => {
93
+ sql.exec(`DELETE FROM durability_topology_writes
94
+ WHERE owner_channel = ? AND group_id = ? AND land_by <= ?`, ownerChannel, group, now);
85
95
  const { until } = z.object({ until: z.number().int().nullable() }).parse(sql
86
96
  .exec(`SELECT MAX(land_by) AS until FROM durability_topology_writes
87
- WHERE group_id = ?`, group)
97
+ WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
88
98
  .toArray()[0]);
89
99
  return until ?? undefined;
90
100
  };
@@ -97,13 +107,14 @@ const deliveryKey = (group, member) => JSON.stringify([group, member]);
97
107
  const isThenable = (value) => value !== null &&
98
108
  typeof value === 'object' &&
99
109
  typeof Reflect.get(value, 'then') === 'function';
100
- const readGroup = (sql, group) => groupRowSchema.optional().parse(sql
110
+ const readGroup = (sql, ownerChannel, group) => groupRowSchema.optional().parse(sql
101
111
  .exec(`SELECT group_id, version, members, deleted
102
- FROM durability_topology_groups WHERE group_id = ?`, group)
112
+ FROM durability_topology_groups
113
+ WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
103
114
  .toArray()[0]);
104
- const readStates = (sql, group) => stateRowSchema.array().parse(sql
115
+ const readStates = (sql, ownerChannel, group) => stateRowSchema.array().parse(sql
105
116
  .exec(`SELECT channel, value FROM durability_topology_states
106
- WHERE group_id = ?`, group)
117
+ WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
107
118
  .toArray());
108
119
  const parseMembers = (row) => membersSchema.parse(JSON.parse(row.members));
109
120
  const inRole = (member, role) => role === 'read' || member.state === 'active';
@@ -117,9 +128,11 @@ export class DurabilityTopology {
117
128
  constructor(config) {
118
129
  _DurabilityTopology_instances.add(this);
119
130
  _DurabilityTopology_engine.set(this, void 0);
131
+ _DurabilityTopology_ownerChannel.set(this, void 0);
120
132
  _DurabilityTopology_coordinator.set(this, void 0);
121
133
  _DurabilityTopology_publication.set(this, void 0);
122
134
  _DurabilityTopology_channels.set(this, new Map());
135
+ const ownerChannel = assertChannel('DurabilityTopology', config.channel ?? defaultChannel);
123
136
  // The outboxes must share this helper's engine, so a private scheduler is
124
137
  // created explicitly instead of letting each attach its own.
125
138
  const scheduler = config.scheduler ?? new DurabilityScheduler(config);
@@ -128,37 +141,51 @@ export class DurabilityTopology {
128
141
  if (coordinator === undefined) {
129
142
  throw new TopologyError('DurabilityTopology needs the Durable Object context, which carries its ID');
130
143
  }
144
+ __classPrivateFieldSet(this, _DurabilityTopology_ownerChannel, ownerChannel, "f");
131
145
  __classPrivateFieldSet(this, _DurabilityTopology_coordinator, coordinator, "f");
132
146
  __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").migrateSchema('topology', durabilityTopologyMigrations);
147
+ const publisher = config.publish;
148
+ if (publisher !== undefined &&
149
+ (typeof publisher.write !== 'function' ||
150
+ typeof publisher.delete !== 'function')) {
151
+ throw new TopologyError('publish needs write and delete functions');
152
+ }
153
+ const publication = publisher === undefined
154
+ ? undefined
155
+ : { publisher, timeoutMs: timeoutOf(publisher, config) };
156
+ const states = config.states ?? {};
157
+ const channels = Object.entries(states).map(([name, channel]) => {
158
+ if (!name || typeof channel.deliver !== 'function') {
159
+ throw new TopologyError(`Invalid topology channel "${name}"`);
160
+ }
161
+ return {
162
+ name,
163
+ channel,
164
+ timeoutMs: timeoutOf(channel, config),
165
+ };
166
+ });
167
+ // Validate before claiming so config validation failures leave the channel
168
+ // free. Even without outboxes, an instance still owns its channel's groups.
169
+ __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").claimChannel('durability_topology_groups', ownerChannel, 'DurabilityTopology');
133
170
  // The outbox only emits outbox events, which is all this hook accepts.
134
171
  const onLifecycleEvent = config.onLifecycleEvent;
135
172
  const lifecycle = onLifecycleEvent === undefined ? {} : { onLifecycleEvent };
136
- const publisher = config.publish;
137
- if (publisher !== undefined) {
138
- if (typeof publisher.write !== 'function' ||
139
- typeof publisher.delete !== 'function') {
140
- throw new TopologyError('publish needs write and delete functions');
141
- }
142
- const timeoutMs = timeoutOf(publisher, config);
173
+ if (publication !== undefined) {
174
+ const { timeoutMs } = publication;
143
175
  __classPrivateFieldSet(this, _DurabilityTopology_publication, new DurabilityOutbox({
144
176
  scheduler,
145
- channel: publicationChannel,
146
- ...outboxOptions(timeoutMs, publisher, config),
177
+ channel: publicationChannelFor(ownerChannel),
178
+ ...outboxOptions(timeoutMs, publication.publisher, config),
147
179
  ...lifecycle,
148
- publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publisher, entries, timeoutMs),
180
+ publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publication.publisher, entries, timeoutMs),
149
181
  }), "f");
150
182
  }
151
- const channels = config.states ?? {};
152
- for (const [name, channel] of Object.entries(channels)) {
153
- if (!name || typeof channel.deliver !== 'function') {
154
- throw new TopologyError(`Invalid topology channel "${name}"`);
155
- }
156
- const timeoutMs = timeoutOf(channel, config);
183
+ for (const { name, channel, timeoutMs } of channels) {
157
184
  __classPrivateFieldGet(this, _DurabilityTopology_channels, "f").set(name, {
158
185
  role: channel.members ?? 'write',
159
186
  outbox: new DurabilityOutbox({
160
187
  scheduler,
161
- channel: `${publicationChannel}.${name}`,
188
+ channel: stateChannelFor(ownerChannel, name),
162
189
  ...outboxOptions(timeoutMs, channel, config),
163
190
  ...lifecycle,
164
191
  publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_deliver).call(this, name, channel, entries, timeoutMs),
@@ -188,10 +215,10 @@ export class DurabilityTopology {
188
215
  async update(group, next) {
189
216
  assertGroup(group);
190
217
  const snapshot = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
191
- const row = readGroup(sql, group);
218
+ const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
192
219
  const live = row?.deleted === 0 ? row : undefined;
193
220
  const before = live ? parseMembers(live) : [];
194
- const stateRows = readStates(sql, group);
221
+ const stateRows = readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
195
222
  const states = Object.fromEntries(stateRows.map((state) => [state.channel, deserialize(state.value)]));
196
223
  // A copy, so an updater that mutates `current` cannot make a change look like a no-op.
197
224
  const draft = next({ members: structuredClone(before), states });
@@ -215,13 +242,13 @@ export class DurabilityTopology {
215
242
  };
216
243
  if (changed) {
217
244
  sql.exec(`INSERT INTO durability_topology_groups
218
- (group_id, version, members, deleted, updated_at)
219
- VALUES (?, ?, ?, 0, ?)
220
- ON CONFLICT(group_id) DO UPDATE SET
245
+ (owner_channel, group_id, version, members, deleted, updated_at)
246
+ VALUES (?, ?, ?, ?, 0, ?)
247
+ ON CONFLICT(owner_channel, group_id) DO UPDATE SET
221
248
  version = excluded.version,
222
249
  members = excluded.members,
223
250
  deleted = 0,
224
- updated_at = excluded.updated_at`, group, version, JSON.stringify(after), now);
251
+ updated_at = excluded.updated_at`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, version, JSON.stringify(after), now);
225
252
  __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, { snapshot: current });
226
253
  }
227
254
  const changedChannels = __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_writeStates).call(this, sql, group, changes, stateRows);
@@ -239,15 +266,16 @@ export class DurabilityTopology {
239
266
  async delete(group) {
240
267
  assertGroup(group);
241
268
  await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
242
- const row = readGroup(sql, group);
269
+ const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
243
270
  if (row?.deleted !== 0) {
244
271
  return;
245
272
  }
246
273
  const version = row.version + 1;
247
274
  sql.exec(`UPDATE durability_topology_groups
248
275
  SET version = ?, members = '[]', deleted = 1, updated_at = ?
249
- WHERE group_id = ?`, version, Date.now(), group);
250
- sql.exec('DELETE FROM durability_topology_states WHERE group_id = ?', group);
276
+ WHERE owner_channel = ? AND group_id = ?`, version, Date.now(), __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
277
+ sql.exec(`DELETE FROM durability_topology_states
278
+ WHERE owner_channel = ? AND group_id = ?`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
251
279
  for (const member of parseMembers(row)) {
252
280
  for (const channel of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f").values()) {
253
281
  discardWithin(channel.outbox, sql, deliveryKey(group, member.id));
@@ -260,7 +288,7 @@ export class DurabilityTopology {
260
288
  /** Coordinator-local read. Readers elsewhere use the published copy. */
261
289
  snapshot(group) {
262
290
  assertGroup(group);
263
- const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, group);
291
+ const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
264
292
  if (row?.deleted !== 0) {
265
293
  return undefined;
266
294
  }
@@ -272,7 +300,7 @@ export class DurabilityTopology {
272
300
  };
273
301
  }
274
302
  }
275
- _DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
303
+ _DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_ownerChannel = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
276
304
  return __classPrivateFieldGet(this, _DurabilityTopology_publication, "f")
277
305
  ? publishNow(__classPrivateFieldGet(this, _DurabilityTopology_publication, "f"), group)
278
306
  : Promise.resolve(true);
@@ -296,8 +324,9 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
296
324
  }));
297
325
  await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
298
326
  for (const { entry, id } of writes) {
299
- sql.exec(`INSERT INTO durability_topology_writes (id, group_id, land_by)
300
- VALUES (?, ?, ?)`, id, entry.key, landBy);
327
+ sql.exec(`INSERT INTO durability_topology_writes
328
+ (owner_channel, id, group_id, land_by)
329
+ VALUES (?, ?, ?, ?)`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id, entry.key, landBy);
301
330
  }
302
331
  });
303
332
  await Promise.all(writes.map(async ({ entry, id }) => {
@@ -314,15 +343,15 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
314
343
  }
315
344
  else {
316
345
  // A rejected write settled before any newer write of this group started.
317
- await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, id));
346
+ await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id));
318
347
  }
319
348
  reportFailure('durability.topology.publish_failed', { group: entry.key, attempt: entry.attempt }, error);
320
349
  entry.retry();
321
350
  return;
322
351
  }
323
352
  const outstandingUntil = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
324
- forgetWrite(sql, id);
325
- return outstandingWritesUntil(sql, entry.key, Date.now());
353
+ forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
354
+ return outstandingWritesUntil(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), entry.key, Date.now());
326
355
  });
327
356
  if (outstandingUntil !== undefined) {
328
357
  entry.retry(Math.min(outstandingUntil - Date.now(), maxTimeoutMs));
@@ -333,13 +362,13 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
333
362
  const settled = write
334
363
  .then(() => undefined, () => undefined)
335
364
  .then(() => __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
336
- forgetWrite(sql, id);
365
+ forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
337
366
  __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stageLatest).call(this, sql, group);
338
367
  }))
339
368
  .catch(onError);
340
369
  __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").waitUntil(settled, onError);
341
370
  }, _DurabilityTopology_stageLatest = function _DurabilityTopology_stageLatest(sql, group) {
342
- const row = readGroup(sql, group);
371
+ const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
343
372
  if (!row) {
344
373
  return;
345
374
  }
@@ -430,14 +459,19 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
430
459
  if (previous !== undefined && canonical(previous) === canonical(stored)) {
431
460
  continue;
432
461
  }
433
- sql.exec(`INSERT INTO durability_topology_states (group_id, channel, value)
434
- VALUES (?, ?, ?)
435
- ON CONFLICT(group_id, channel) DO UPDATE SET value = excluded.value`, group, channel, stored);
462
+ sql.exec(`INSERT INTO durability_topology_states
463
+ (owner_channel, group_id, channel, value)
464
+ VALUES (?, ?, ?, ?)
465
+ ON CONFLICT(owner_channel, group_id, channel) DO UPDATE SET
466
+ value = excluded.value`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, channel, stored);
436
467
  changed.add(channel);
437
468
  }
438
469
  return changed;
439
470
  }, _DurabilityTopology_stageDeliveries = function _DurabilityTopology_stageDeliveries(sql, group, topologyVersion, before, after, changed) {
440
- const values = new Map(readStates(sql, group).map((row) => [row.channel, row.value]));
471
+ const values = new Map(readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group).map((row) => [
472
+ row.channel,
473
+ row.value,
474
+ ]));
441
475
  for (const [name, channel] of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f")) {
442
476
  const previous = new Set(before
443
477
  .filter((member) => inRole(member, channel.role))
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Ordered SQLite migrations for the experimental topology coordinator.
3
+ * Publication and sync ride on outbox channels. Applied automatically on
4
+ * construction.
5
+ */
6
+ export declare const durabilityTopologyMigrations: {
7
+ name: string;
8
+ up: string;
9
+ down: string;
10
+ }[];
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Ordered SQLite migrations for the experimental topology coordinator.
3
+ * Publication and sync ride on outbox channels. Applied automatically on
4
+ * construction.
5
+ */
6
+ export const durabilityTopologyMigrations = [
7
+ {
8
+ name: 'durability_0011_create_topology',
9
+ up: `
10
+ CREATE TABLE IF NOT EXISTS durability_topology_groups (
11
+ group_id TEXT PRIMARY KEY,
12
+ version INTEGER NOT NULL,
13
+ members TEXT NOT NULL,
14
+ deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
15
+ updated_at INTEGER NOT NULL
16
+ );
17
+ CREATE TABLE IF NOT EXISTS durability_topology_states (
18
+ group_id TEXT NOT NULL,
19
+ channel TEXT NOT NULL,
20
+ value TEXT NOT NULL,
21
+ PRIMARY KEY (group_id, channel)
22
+ );
23
+ CREATE TABLE IF NOT EXISTS durability_topology_writes (
24
+ id TEXT PRIMARY KEY,
25
+ group_id TEXT NOT NULL,
26
+ land_by INTEGER NOT NULL
27
+ );
28
+ CREATE INDEX IF NOT EXISTS durability_topology_writes_group_idx
29
+ ON durability_topology_writes (group_id, land_by);
30
+ `,
31
+ down: `
32
+ DROP INDEX IF EXISTS durability_topology_writes_group_idx;
33
+ DROP TABLE IF EXISTS durability_topology_writes;
34
+ DROP TABLE IF EXISTS durability_topology_states;
35
+ DROP TABLE IF EXISTS durability_topology_groups;
36
+ `,
37
+ },
38
+ {
39
+ // `owner_channel` is the topology instance; the existing `channel` column
40
+ // of durability_topology_states stays the state name.
41
+ name: 'durability_0022_topology_channels',
42
+ up: `
43
+ ALTER TABLE durability_topology_groups RENAME TO durability_topology_groups_before_channels;
44
+ CREATE TABLE durability_topology_groups (
45
+ group_id TEXT NOT NULL,
46
+ version INTEGER NOT NULL,
47
+ members TEXT NOT NULL,
48
+ deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
49
+ updated_at INTEGER NOT NULL,
50
+ owner_channel TEXT NOT NULL DEFAULT 'default',
51
+ PRIMARY KEY (owner_channel, group_id)
52
+ );
53
+ INSERT INTO durability_topology_groups
54
+ (group_id, version, members, deleted, updated_at, owner_channel)
55
+ SELECT group_id, version, members, deleted, updated_at, 'default'
56
+ FROM durability_topology_groups_before_channels;
57
+ DROP TABLE durability_topology_groups_before_channels;
58
+
59
+ ALTER TABLE durability_topology_states RENAME TO durability_topology_states_before_channels;
60
+ CREATE TABLE durability_topology_states (
61
+ group_id TEXT NOT NULL,
62
+ channel TEXT NOT NULL,
63
+ value TEXT NOT NULL,
64
+ owner_channel TEXT NOT NULL DEFAULT 'default',
65
+ PRIMARY KEY (owner_channel, group_id, channel)
66
+ );
67
+ INSERT INTO durability_topology_states
68
+ (group_id, channel, value, owner_channel)
69
+ SELECT group_id, channel, value, 'default'
70
+ FROM durability_topology_states_before_channels;
71
+ DROP TABLE durability_topology_states_before_channels;
72
+
73
+ ALTER TABLE durability_topology_writes RENAME TO durability_topology_writes_before_channels;
74
+ CREATE TABLE durability_topology_writes (
75
+ id TEXT NOT NULL,
76
+ group_id TEXT NOT NULL,
77
+ land_by INTEGER NOT NULL,
78
+ owner_channel TEXT NOT NULL DEFAULT 'default',
79
+ PRIMARY KEY (owner_channel, id)
80
+ );
81
+ INSERT INTO durability_topology_writes
82
+ (id, group_id, land_by, owner_channel)
83
+ SELECT id, group_id, land_by, 'default'
84
+ FROM durability_topology_writes_before_channels;
85
+ DROP TABLE durability_topology_writes_before_channels;
86
+ CREATE INDEX durability_topology_writes_group_idx
87
+ ON durability_topology_writes (owner_channel, group_id, land_by);
88
+ `,
89
+ down: `
90
+ CREATE TABLE durability_topology_channel_guard (
91
+ owner_channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (owner_channel = 'default')
92
+ );
93
+ INSERT INTO durability_topology_channel_guard
94
+ SELECT owner_channel FROM durability_topology_groups
95
+ UNION SELECT owner_channel FROM durability_topology_states
96
+ UNION SELECT owner_channel FROM durability_topology_writes;
97
+ DROP TABLE durability_topology_channel_guard;
98
+
99
+ ALTER TABLE durability_topology_groups RENAME TO durability_topology_groups_with_channels;
100
+ CREATE TABLE durability_topology_groups (
101
+ group_id TEXT PRIMARY KEY,
102
+ version INTEGER NOT NULL,
103
+ members TEXT NOT NULL,
104
+ deleted INTEGER NOT NULL CHECK (deleted IN (0, 1)),
105
+ updated_at INTEGER NOT NULL
106
+ );
107
+ INSERT INTO durability_topology_groups
108
+ SELECT group_id, version, members, deleted, updated_at
109
+ FROM durability_topology_groups_with_channels;
110
+ DROP TABLE durability_topology_groups_with_channels;
111
+
112
+ ALTER TABLE durability_topology_states RENAME TO durability_topology_states_with_channels;
113
+ CREATE TABLE durability_topology_states (
114
+ group_id TEXT NOT NULL,
115
+ channel TEXT NOT NULL,
116
+ value TEXT NOT NULL,
117
+ PRIMARY KEY (group_id, channel)
118
+ );
119
+ INSERT INTO durability_topology_states
120
+ SELECT group_id, channel, value
121
+ FROM durability_topology_states_with_channels;
122
+ DROP TABLE durability_topology_states_with_channels;
123
+
124
+ ALTER TABLE durability_topology_writes RENAME TO durability_topology_writes_with_channels;
125
+ CREATE TABLE durability_topology_writes (
126
+ id TEXT PRIMARY KEY,
127
+ group_id TEXT NOT NULL,
128
+ land_by INTEGER NOT NULL
129
+ );
130
+ INSERT INTO durability_topology_writes
131
+ SELECT id, group_id, land_by
132
+ FROM durability_topology_writes_with_channels;
133
+ DROP TABLE durability_topology_writes_with_channels;
134
+ CREATE INDEX durability_topology_writes_group_idx
135
+ ON durability_topology_writes (group_id, land_by);
136
+ `,
137
+ },
138
+ ];
@@ -1,6 +1,6 @@
1
- import type { AnyDurabilityAlarms, DurabilityAlarms } from './durability-alarms.js';
2
- import type { AnyDurability, Durability, OperationResults } from './durability.js';
3
- import { type Names, type TrackedEntry, type TrackedResults, type TrackedSource, type TrackedState, type TrackedStatus, type UntypedSource } from './tracked-source.js';
1
+ import type { AnyDurabilityAlarms, DurabilityAlarms } from '../alarms/index.js';
2
+ import type { AnyDurability, Durability, OperationResults } from '../operations/index.js';
3
+ import { type Names, type TrackedEntry, type TrackedResults, type TrackedSource, type TrackedState, type TrackedStatus, type UntypedSource } from './source.js';
4
4
  /** A tracked state, or `not_found` when the record or its generation is gone. */
5
5
  export type TrackedResult<Result> = TrackedState<Result> | {
6
6
  status: 'not_found';
@@ -11,8 +11,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
13
13
  import { z } from 'zod';
14
- import { traceMethods } from './dev.js';
15
- import { trackedSourceOf, } from './tracked-source.js';
14
+ import { traceMethods } from '../dev.js';
15
+ import { trackedSourceOf, } from './source.js';
16
16
  const limitSchema = z.number().int().min(1).max(100);
17
17
  const cursorSchema = z.tuple([
18
18
  z.number().int().nonnegative(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durability",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
5
5
  "type": "module",
6
6
  "license": "ISC",
@@ -16,24 +16,24 @@
16
16
  "import": "./dist/index.js"
17
17
  },
18
18
  "./client": {
19
- "types": "./dist/client.d.ts",
20
- "import": "./dist/client.js"
19
+ "types": "./dist/socket/client.d.ts",
20
+ "import": "./dist/socket/client.js"
21
21
  },
22
22
  "./experimental": {
23
23
  "types": "./dist/experimental.d.ts",
24
24
  "import": "./dist/experimental.js"
25
25
  },
26
26
  "./routing": {
27
- "types": "./dist/routing.d.ts",
28
- "import": "./dist/routing.js"
27
+ "types": "./dist/routing/index.d.ts",
28
+ "import": "./dist/routing/index.js"
29
29
  },
30
30
  "./socket": {
31
- "types": "./dist/socket.d.ts",
32
- "import": "./dist/socket.js"
31
+ "types": "./dist/socket/index.d.ts",
32
+ "import": "./dist/socket/index.js"
33
33
  },
34
34
  "./experimental/semaphore": {
35
- "types": "./dist/semaphore.d.ts",
36
- "import": "./dist/semaphore.js"
35
+ "types": "./dist/semaphore/index.d.ts",
36
+ "import": "./dist/semaphore/index.js"
37
37
  },
38
38
  "./utils": {
39
39
  "types": "./dist/utils.d.ts",
@@ -64,8 +64,8 @@
64
64
  "dependencies": {
65
65
  "@standard-schema/spec": "^1.1.0",
66
66
  "zod": "^4.4.3",
67
- "@durability/storage": "^0.1.1",
68
- "@durability/transforms": "^0.6.0"
67
+ "@durability/transforms": "^0.6.1",
68
+ "@durability/storage": "^0.1.1"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "@cloudflare/workers-types": "^4.20260702.1"
File without changes
File without changes
File without changes