durability 4.2.0 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -2
- package/dist/dev.d.ts +22 -0
- package/dist/dev.js +149 -0
- package/dist/durability-alarms.d.ts +1 -1
- package/dist/durability-alarms.js +6 -4
- package/dist/durability.d.ts +1 -1
- package/dist/durability.js +8 -5
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +22 -0
- package/dist/experimental.d.ts +5 -0
- package/dist/experimental.js +3 -0
- package/dist/fanout.js +14 -9
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/lifecycle.js +3 -2
- package/dist/log.js +26 -4
- package/dist/migrations.d.ts +47 -2
- package/dist/migrations.js +132 -36
- package/dist/outbox.d.ts +13 -0
- package/dist/outbox.js +78 -30
- package/dist/scheduler.d.ts +7 -0
- package/dist/scheduler.js +26 -7
- package/dist/semaphore-leases.js +15 -9
- package/dist/semaphore-queue-store.d.ts +12 -2
- package/dist/semaphore-queue-store.js +32 -5
- package/dist/semaphore-queue.js +109 -43
- package/dist/semaphore-server.js +7 -9
- package/dist/socket.js +19 -10
- package/dist/topology-client.d.ts +40 -0
- package/dist/topology-client.js +180 -0
- package/dist/topology-snapshot.d.ts +70 -0
- package/dist/topology-snapshot.js +48 -0
- package/dist/topology.d.ts +94 -0
- package/dist/topology.js +468 -0
- package/dist/tracker.js +2 -0
- package/package.json +2 -2
package/dist/topology.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
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;
|
|
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';
|
|
23
|
+
const groupRowSchema = z.object({
|
|
24
|
+
group_id: z.string(),
|
|
25
|
+
version: z.number().int().positive(),
|
|
26
|
+
members: z.string(),
|
|
27
|
+
deleted: z.union([z.literal(0), z.literal(1)]),
|
|
28
|
+
});
|
|
29
|
+
const stateRowSchema = z.object({
|
|
30
|
+
channel: z.string(),
|
|
31
|
+
value: z.string(),
|
|
32
|
+
});
|
|
33
|
+
class TopologyTimeoutError extends Error {
|
|
34
|
+
constructor(label, timeoutMs) {
|
|
35
|
+
super(`Topology ${label} timed out after ${timeoutMs}ms`);
|
|
36
|
+
this.name = 'TopologyTimeoutError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const withTimeout = async (work, timeoutMs, label) => {
|
|
40
|
+
let timer;
|
|
41
|
+
try {
|
|
42
|
+
return await Promise.race([
|
|
43
|
+
work,
|
|
44
|
+
new Promise((_resolve, reject) => {
|
|
45
|
+
timer = setTimeout(() => reject(new TopologyTimeoutError(label, timeoutMs)), timeoutMs);
|
|
46
|
+
}),
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
// setTimeout overflows past this, and the outbox enforces the same ceiling.
|
|
54
|
+
const maxTimeoutMs = 2 ** 31 - 1;
|
|
55
|
+
const timeoutOf = (options, fallback) => {
|
|
56
|
+
const timeoutMs = options?.attemptTimeoutMs ?? fallback.attemptTimeoutMs ?? 30000;
|
|
57
|
+
assertPositiveInteger('attemptTimeoutMs', timeoutMs);
|
|
58
|
+
if (timeoutMs > maxTimeoutMs) {
|
|
59
|
+
throw new RangeError(`attemptTimeoutMs must be at most ${maxTimeoutMs}`);
|
|
60
|
+
}
|
|
61
|
+
return timeoutMs;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Each write or delivery has its own deadline inside a batch, so the outbox's
|
|
65
|
+
* batch deadline only bounds the callback itself.
|
|
66
|
+
*/
|
|
67
|
+
const outboxOptions = (timeoutMs, options, fallback) => {
|
|
68
|
+
const delay = options?.retries?.delay ?? fallback.retries?.delay;
|
|
69
|
+
return {
|
|
70
|
+
attemptTimeoutMs: Math.min(timeoutMs * 2, maxTimeoutMs),
|
|
71
|
+
...(delay === undefined ? {} : { retries: { delay } }),
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* How long after its timeout an abandoned publisher write is assumed able to
|
|
76
|
+
* land. KV has no conditional write, so some bound is required.
|
|
77
|
+
*/
|
|
78
|
+
const landingWindowMs = 5 * 60000;
|
|
79
|
+
const forgetWrite = (sql, id) => {
|
|
80
|
+
sql.exec('DELETE FROM durability_topology_writes WHERE id = ?', id);
|
|
81
|
+
};
|
|
82
|
+
/** 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);
|
|
85
|
+
const { until } = z.object({ until: z.number().int().nullable() }).parse(sql
|
|
86
|
+
.exec(`SELECT MAX(land_by) AS until FROM durability_topology_writes
|
|
87
|
+
WHERE group_id = ?`, group)
|
|
88
|
+
.toArray()[0]);
|
|
89
|
+
return until ?? undefined;
|
|
90
|
+
};
|
|
91
|
+
const assertGroup = (group) => {
|
|
92
|
+
if (typeof group !== 'string' || group.length === 0) {
|
|
93
|
+
throw new TopologyError('Group must be a non-empty string');
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const deliveryKey = (group, member) => JSON.stringify([group, member]);
|
|
97
|
+
const isThenable = (value) => value !== null &&
|
|
98
|
+
typeof value === 'object' &&
|
|
99
|
+
typeof Reflect.get(value, 'then') === 'function';
|
|
100
|
+
const readGroup = (sql, group) => groupRowSchema.optional().parse(sql
|
|
101
|
+
.exec(`SELECT group_id, version, members, deleted
|
|
102
|
+
FROM durability_topology_groups WHERE group_id = ?`, group)
|
|
103
|
+
.toArray()[0]);
|
|
104
|
+
const readStates = (sql, group) => stateRowSchema.array().parse(sql
|
|
105
|
+
.exec(`SELECT channel, value FROM durability_topology_states
|
|
106
|
+
WHERE group_id = ?`, group)
|
|
107
|
+
.toArray());
|
|
108
|
+
const parseMembers = (row) => membersSchema.parse(JSON.parse(row.members));
|
|
109
|
+
const inRole = (member, role) => role === 'read' || member.state === 'active';
|
|
110
|
+
/**
|
|
111
|
+
* Experimental. Holds the membership of shard groups inside one coordinator
|
|
112
|
+
* Durable Object, publishes each group's snapshot to a read store, and drives
|
|
113
|
+
* every member to the latest value of each state channel. Only this instance
|
|
114
|
+
* can change or publish its groups; readers use {@link DurabilityTopology.client}.
|
|
115
|
+
*/
|
|
116
|
+
export class DurabilityTopology {
|
|
117
|
+
constructor(config) {
|
|
118
|
+
_DurabilityTopology_instances.add(this);
|
|
119
|
+
_DurabilityTopology_engine.set(this, void 0);
|
|
120
|
+
_DurabilityTopology_coordinator.set(this, void 0);
|
|
121
|
+
_DurabilityTopology_publication.set(this, void 0);
|
|
122
|
+
_DurabilityTopology_channels.set(this, new Map());
|
|
123
|
+
// The outboxes must share this helper's engine, so a private scheduler is
|
|
124
|
+
// created explicitly instead of letting each attach its own.
|
|
125
|
+
const scheduler = config.scheduler ?? new DurabilityScheduler(config);
|
|
126
|
+
__classPrivateFieldSet(this, _DurabilityTopology_engine, engineFor({ scheduler }), "f");
|
|
127
|
+
const coordinator = __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").objectId();
|
|
128
|
+
if (coordinator === undefined) {
|
|
129
|
+
throw new TopologyError('DurabilityTopology needs the Durable Object context, which carries its ID');
|
|
130
|
+
}
|
|
131
|
+
__classPrivateFieldSet(this, _DurabilityTopology_coordinator, coordinator, "f");
|
|
132
|
+
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").migrateSchema('topology', durabilityTopologyMigrations);
|
|
133
|
+
// The outbox only emits outbox events, which is all this hook accepts.
|
|
134
|
+
const onLifecycleEvent = config.onLifecycleEvent;
|
|
135
|
+
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);
|
|
143
|
+
__classPrivateFieldSet(this, _DurabilityTopology_publication, new DurabilityOutbox({
|
|
144
|
+
scheduler,
|
|
145
|
+
channel: publicationChannel,
|
|
146
|
+
...outboxOptions(timeoutMs, publisher, config),
|
|
147
|
+
...lifecycle,
|
|
148
|
+
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publisher, entries, timeoutMs),
|
|
149
|
+
}), "f");
|
|
150
|
+
}
|
|
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);
|
|
157
|
+
__classPrivateFieldGet(this, _DurabilityTopology_channels, "f").set(name, {
|
|
158
|
+
role: channel.members ?? 'write',
|
|
159
|
+
outbox: new DurabilityOutbox({
|
|
160
|
+
scheduler,
|
|
161
|
+
channel: `${publicationChannel}.${name}`,
|
|
162
|
+
...outboxOptions(timeoutMs, channel, config),
|
|
163
|
+
...lifecycle,
|
|
164
|
+
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_deliver).call(this, name, channel, entries, timeoutMs),
|
|
165
|
+
}),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** A read-only client for stateless Workers. It never contacts the coordinator. */
|
|
170
|
+
static client(config) {
|
|
171
|
+
return new TopologyClient(config);
|
|
172
|
+
}
|
|
173
|
+
/** Reverts only the topology tables; outbox channels migrateCapability with `DurabilityOutbox.migrate`. */
|
|
174
|
+
static migrate(context, target) {
|
|
175
|
+
return migrateCapability(context.storage, 'topology', durabilityTopologyMigrations, target);
|
|
176
|
+
}
|
|
177
|
+
alarm(info) {
|
|
178
|
+
return __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").alarm(info);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Applies one change in a single transaction. `next` runs inside it and must
|
|
182
|
+
* return synchronously, so nothing can interleave between reading `current`
|
|
183
|
+
* and writing the draft. After the commit, one publish is attempted:
|
|
184
|
+
* `published: false` means the snapshot is not yet final in the store:
|
|
185
|
+
* the write failed, or an older write may still land over it. The outbox
|
|
186
|
+
* keeps going until it is.
|
|
187
|
+
*/
|
|
188
|
+
async update(group, next) {
|
|
189
|
+
assertGroup(group);
|
|
190
|
+
const snapshot = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
191
|
+
const row = readGroup(sql, group);
|
|
192
|
+
const live = row?.deleted === 0 ? row : undefined;
|
|
193
|
+
const before = live ? parseMembers(live) : [];
|
|
194
|
+
const stateRows = readStates(sql, group);
|
|
195
|
+
const states = Object.fromEntries(stateRows.map((state) => [state.channel, deserialize(state.value)]));
|
|
196
|
+
// A copy, so an updater that mutates `current` cannot make a change look like a no-op.
|
|
197
|
+
const draft = next({ members: structuredClone(before), states });
|
|
198
|
+
if (isThenable(draft)) {
|
|
199
|
+
throw new TopologyError('update must return its draft synchronously');
|
|
200
|
+
}
|
|
201
|
+
const { members, states: changes } = __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_parseDraft).call(this, draft);
|
|
202
|
+
if (!live && members === undefined) {
|
|
203
|
+
throw new TopologyError(`Group "${group}" does not exist; set members to create it`);
|
|
204
|
+
}
|
|
205
|
+
const now = Date.now();
|
|
206
|
+
const after = members === undefined ? before : __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_normalize).call(this, members, before, now);
|
|
207
|
+
const changed = !live ||
|
|
208
|
+
canonical(JSON.stringify(after)) !== canonical(JSON.stringify(before));
|
|
209
|
+
const version = changed ? (row?.version ?? 0) + 1 : live.version;
|
|
210
|
+
const current = {
|
|
211
|
+
group,
|
|
212
|
+
version,
|
|
213
|
+
coordinator: __classPrivateFieldGet(this, _DurabilityTopology_coordinator, "f"),
|
|
214
|
+
members: after,
|
|
215
|
+
};
|
|
216
|
+
if (changed) {
|
|
217
|
+
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
|
|
221
|
+
version = excluded.version,
|
|
222
|
+
members = excluded.members,
|
|
223
|
+
deleted = 0,
|
|
224
|
+
updated_at = excluded.updated_at`, group, version, JSON.stringify(after), now);
|
|
225
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, { snapshot: current });
|
|
226
|
+
}
|
|
227
|
+
const changedChannels = __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_writeStates).call(this, sql, group, changes, stateRows);
|
|
228
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stageDeliveries).call(this, sql, group, version, before, after, changedChannels);
|
|
229
|
+
return current;
|
|
230
|
+
});
|
|
231
|
+
return { snapshot, published: await __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishNow).call(this, group) };
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Deletes the group in one transaction: members and state values are dropped
|
|
235
|
+
* and pending deliveries discarded. Versions keep counting, so a recreated
|
|
236
|
+
* group is newer than anything readers cached. One publisher delete is
|
|
237
|
+
* attempted after the commit.
|
|
238
|
+
*/
|
|
239
|
+
async delete(group) {
|
|
240
|
+
assertGroup(group);
|
|
241
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
242
|
+
const row = readGroup(sql, group);
|
|
243
|
+
if (row?.deleted !== 0) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const version = row.version + 1;
|
|
247
|
+
sql.exec(`UPDATE durability_topology_groups
|
|
248
|
+
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);
|
|
251
|
+
for (const member of parseMembers(row)) {
|
|
252
|
+
for (const channel of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f").values()) {
|
|
253
|
+
discardWithin(channel.outbox, sql, deliveryKey(group, member.id));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, { deleted: { group, version } });
|
|
257
|
+
});
|
|
258
|
+
return { published: await __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishNow).call(this, group) };
|
|
259
|
+
}
|
|
260
|
+
/** Coordinator-local read. Readers elsewhere use the published copy. */
|
|
261
|
+
snapshot(group) {
|
|
262
|
+
assertGroup(group);
|
|
263
|
+
const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, group);
|
|
264
|
+
if (row?.deleted !== 0) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
group,
|
|
269
|
+
version: row.version,
|
|
270
|
+
coordinator: __classPrivateFieldGet(this, _DurabilityTopology_coordinator, "f"),
|
|
271
|
+
members: parseMembers(row),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
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) {
|
|
276
|
+
return __classPrivateFieldGet(this, _DurabilityTopology_publication, "f")
|
|
277
|
+
? publishNow(__classPrivateFieldGet(this, _DurabilityTopology_publication, "f"), group)
|
|
278
|
+
: Promise.resolve(true);
|
|
279
|
+
}, _DurabilityTopology_stagePublication = function _DurabilityTopology_stagePublication(sql, group, publication) {
|
|
280
|
+
if (__classPrivateFieldGet(this, _DurabilityTopology_publication, "f")) {
|
|
281
|
+
stageWithin(__classPrivateFieldGet(this, _DurabilityTopology_publication, "f"), sql, group, publication);
|
|
282
|
+
}
|
|
283
|
+
}, _DurabilityTopology_publishSnapshots =
|
|
284
|
+
/**
|
|
285
|
+
* Every write is recorded before it starts and forgotten once it settles.
|
|
286
|
+
* A record that outlives its write (a timeout, or eviction mid-write) means
|
|
287
|
+
* an older snapshot may still land, so a newer write is not acknowledged
|
|
288
|
+
* until that record's landing deadline passes and it has been written again.
|
|
289
|
+
* Records live in storage, so this survives eviction.
|
|
290
|
+
*/
|
|
291
|
+
async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutMs) {
|
|
292
|
+
const landBy = Date.now() + timeoutMs + landingWindowMs;
|
|
293
|
+
const writes = entries.map((entry) => ({
|
|
294
|
+
entry,
|
|
295
|
+
id: crypto.randomUUID(),
|
|
296
|
+
}));
|
|
297
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
298
|
+
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);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
await Promise.all(writes.map(async ({ entry, id }) => {
|
|
304
|
+
const publication = entry.value;
|
|
305
|
+
const write = Promise.resolve().then(() => 'deleted' in publication
|
|
306
|
+
? publisher.delete(publication.deleted.group, publication.deleted.version)
|
|
307
|
+
: publisher.write(publication.snapshot, encodeSnapshot(publication.snapshot)));
|
|
308
|
+
try {
|
|
309
|
+
await withTimeout(write, timeoutMs, 'publish');
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
if (error instanceof TopologyTimeoutError) {
|
|
313
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_settleLate).call(this, entry.key, id, write);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
// A rejected write settled before any newer write of this group started.
|
|
317
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, id));
|
|
318
|
+
}
|
|
319
|
+
reportFailure('durability.topology.publish_failed', { group: entry.key, attempt: entry.attempt }, error);
|
|
320
|
+
entry.retry();
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const outstandingUntil = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
324
|
+
forgetWrite(sql, id);
|
|
325
|
+
return outstandingWritesUntil(sql, entry.key, Date.now());
|
|
326
|
+
});
|
|
327
|
+
if (outstandingUntil !== undefined) {
|
|
328
|
+
entry.retry(Math.min(outstandingUntil - Date.now(), maxTimeoutMs));
|
|
329
|
+
}
|
|
330
|
+
}));
|
|
331
|
+
}, _DurabilityTopology_settleLate = function _DurabilityTopology_settleLate(group, id, write) {
|
|
332
|
+
const onError = (error) => reportFailure('durability.topology.restage_failed', { group }, error);
|
|
333
|
+
const settled = write
|
|
334
|
+
.then(() => undefined, () => undefined)
|
|
335
|
+
.then(() => __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
336
|
+
forgetWrite(sql, id);
|
|
337
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stageLatest).call(this, sql, group);
|
|
338
|
+
}))
|
|
339
|
+
.catch(onError);
|
|
340
|
+
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").waitUntil(settled, onError);
|
|
341
|
+
}, _DurabilityTopology_stageLatest = function _DurabilityTopology_stageLatest(sql, group) {
|
|
342
|
+
const row = readGroup(sql, group);
|
|
343
|
+
if (!row) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, row.deleted === 1
|
|
347
|
+
? { deleted: { group, version: row.version } }
|
|
348
|
+
: {
|
|
349
|
+
snapshot: {
|
|
350
|
+
group,
|
|
351
|
+
version: row.version,
|
|
352
|
+
coordinator: __classPrivateFieldGet(this, _DurabilityTopology_coordinator, "f"),
|
|
353
|
+
members: parseMembers(row),
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
}, _DurabilityTopology_deliver = async function _DurabilityTopology_deliver(name, channel, entries, timeoutMs) {
|
|
357
|
+
await Promise.all(entries.map(async (entry) => {
|
|
358
|
+
const { group, member, topologyVersion, value } = entry.value;
|
|
359
|
+
const update = {
|
|
360
|
+
group,
|
|
361
|
+
channel: name,
|
|
362
|
+
version: entry.version,
|
|
363
|
+
topologyVersion,
|
|
364
|
+
value,
|
|
365
|
+
};
|
|
366
|
+
try {
|
|
367
|
+
// Members ignore versions they have passed, so an abandoned delivery landing late is harmless.
|
|
368
|
+
await withTimeout(Promise.resolve().then(() => channel.deliver(member, update)), timeoutMs, 'delivery');
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
reportFailure('durability.topology.delivery_failed', { group, channel: name, member: member.id, attempt: entry.attempt }, error);
|
|
372
|
+
entry.retry();
|
|
373
|
+
}
|
|
374
|
+
}));
|
|
375
|
+
}, _DurabilityTopology_parseDraft = function _DurabilityTopology_parseDraft(draft) {
|
|
376
|
+
if (draft === null || typeof draft !== 'object') {
|
|
377
|
+
throw new TopologyError('update must return a draft object');
|
|
378
|
+
}
|
|
379
|
+
const members = Reflect.get(draft, 'members');
|
|
380
|
+
const states = Reflect.get(draft, 'states') ?? {};
|
|
381
|
+
if (members !== undefined && !Array.isArray(members)) {
|
|
382
|
+
throw new TopologyError('Draft members must be an array');
|
|
383
|
+
}
|
|
384
|
+
if (states === null ||
|
|
385
|
+
typeof states !== 'object' ||
|
|
386
|
+
Array.isArray(states)) {
|
|
387
|
+
throw new TopologyError('Draft states must be an object');
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
members: members,
|
|
391
|
+
states: states,
|
|
392
|
+
};
|
|
393
|
+
}, _DurabilityTopology_normalize = function _DurabilityTopology_normalize(inputs, before, now) {
|
|
394
|
+
if (inputs.length === 0) {
|
|
395
|
+
throw new TopologyError('A group needs at least one member; use delete() to remove it');
|
|
396
|
+
}
|
|
397
|
+
const joinedAt = new Map(before.map((member) => [member.id, member.joinedAt]));
|
|
398
|
+
const seen = new Set();
|
|
399
|
+
const members = inputs.map((input) => {
|
|
400
|
+
const parsed = memberInputSchema.safeParse(input);
|
|
401
|
+
if (!parsed.success) {
|
|
402
|
+
throw new TopologyError(`Invalid member: ${parsed.error.message}`);
|
|
403
|
+
}
|
|
404
|
+
const { id, state, locationHint, meta } = parsed.data;
|
|
405
|
+
if (seen.has(id)) {
|
|
406
|
+
throw new TopologyError(`Duplicate member "${id}"`);
|
|
407
|
+
}
|
|
408
|
+
seen.add(id);
|
|
409
|
+
return {
|
|
410
|
+
id,
|
|
411
|
+
state,
|
|
412
|
+
...(locationHint === undefined ? {} : { locationHint }),
|
|
413
|
+
...(meta === undefined ? {} : { meta }),
|
|
414
|
+
joinedAt: joinedAt.get(id) ?? now,
|
|
415
|
+
};
|
|
416
|
+
});
|
|
417
|
+
return members.sort((a, b) => (a.id < b.id ? -1 : 1));
|
|
418
|
+
}, _DurabilityTopology_writeStates = function _DurabilityTopology_writeStates(sql, group, changes, current) {
|
|
419
|
+
const existing = new Map(current.map((row) => [row.channel, row.value]));
|
|
420
|
+
const changed = new Set();
|
|
421
|
+
for (const [channel, value] of Object.entries(changes)) {
|
|
422
|
+
if (value === undefined) {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (!__classPrivateFieldGet(this, _DurabilityTopology_channels, "f").has(channel)) {
|
|
426
|
+
throw new TopologyError(`Unknown topology channel "${channel}"`);
|
|
427
|
+
}
|
|
428
|
+
const stored = serialize(value);
|
|
429
|
+
const previous = existing.get(channel);
|
|
430
|
+
if (previous !== undefined && canonical(previous) === canonical(stored)) {
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
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);
|
|
436
|
+
changed.add(channel);
|
|
437
|
+
}
|
|
438
|
+
return changed;
|
|
439
|
+
}, _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]));
|
|
441
|
+
for (const [name, channel] of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f")) {
|
|
442
|
+
const previous = new Set(before
|
|
443
|
+
.filter((member) => inRole(member, channel.role))
|
|
444
|
+
.map(({ id }) => id));
|
|
445
|
+
const targets = after.filter((member) => inRole(member, channel.role));
|
|
446
|
+
const kept = new Set(targets.map(({ id }) => id));
|
|
447
|
+
for (const id of previous) {
|
|
448
|
+
if (!kept.has(id)) {
|
|
449
|
+
discardWithin(channel.outbox, sql, deliveryKey(group, id));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const value = values.get(name);
|
|
453
|
+
if (value === undefined) {
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
for (const member of targets) {
|
|
457
|
+
if (!changed.has(name) && previous.has(member.id)) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
stageWithin(channel.outbox, sql, deliveryKey(group, member.id), {
|
|
461
|
+
group,
|
|
462
|
+
member,
|
|
463
|
+
topologyVersion,
|
|
464
|
+
value: deserialize(value),
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
package/dist/tracker.js
CHANGED
|
@@ -11,6 +11,7 @@ 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';
|
|
14
15
|
import { trackedSourceOf, } from './tracked-source.js';
|
|
15
16
|
const limitSchema = z.number().int().min(1).max(100);
|
|
16
17
|
const cursorSchema = z.tuple([
|
|
@@ -169,3 +170,4 @@ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new W
|
|
|
169
170
|
check();
|
|
170
171
|
});
|
|
171
172
|
};
|
|
173
|
+
traceMethods(DurabilityTracker.prototype, 'DurabilityTracker');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "durability",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@standard-schema/spec": "^1.1.0",
|
|
66
66
|
"zod": "^4.4.3",
|
|
67
67
|
"@durability/storage": "^0.1.1",
|
|
68
|
-
"@durability/transforms": "^0.
|
|
68
|
+
"@durability/transforms": "^0.6.0"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
71
|
"@cloudflare/workers-types": "^4.20260702.1"
|