durability 3.0.1 → 4.1.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 +400 -25
- package/dist/client.d.ts +94 -0
- package/dist/client.js +445 -0
- package/dist/durability-alarms.d.ts +14 -1
- package/dist/durability-alarms.js +121 -22
- package/dist/durability.d.ts +33 -2
- package/dist/durability.js +126 -63
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +38 -0
- package/dist/experimental.d.ts +8 -0
- package/dist/experimental.js +7 -0
- package/dist/fanout.d.ts +25 -2
- package/dist/fanout.js +273 -93
- package/dist/index.d.ts +8 -7
- package/dist/index.js +3 -2
- package/dist/lifecycle.d.ts +8 -0
- package/dist/lifecycle.js +22 -0
- package/dist/log.d.ts +201 -0
- package/dist/log.js +705 -0
- package/dist/migrations.d.ts +28 -1
- package/dist/migrations.js +100 -0
- package/dist/scheduler.d.ts +35 -20
- package/dist/scheduler.js +112 -90
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +16 -0
- package/dist/socket-protocol.d.ts +95 -0
- package/dist/socket-protocol.js +71 -0
- package/dist/socket.d.ts +92 -0
- package/dist/socket.js +273 -0
- package/dist/storage.d.ts +39 -128
- package/dist/storage.js +64 -68
- package/dist/tracked-source.d.ts +87 -0
- package/dist/tracked-source.js +7 -0
- package/dist/tracker.d.ts +96 -0
- package/dist/tracker.js +171 -0
- package/package.json +19 -5
- package/dist/kv-storage.d.ts +0 -2
- package/dist/kv-storage.js +0 -421
- package/dist/sqlite-storage.d.ts +0 -2
- package/dist/sqlite-storage.js +0 -251
package/dist/tracker.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
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 _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { trackedSourceOf, } from './tracked-source.js';
|
|
15
|
+
const limitSchema = z.number().int().min(1).max(100);
|
|
16
|
+
const cursorSchema = z.tuple([
|
|
17
|
+
z.number().int().nonnegative(),
|
|
18
|
+
z.string().nullable(),
|
|
19
|
+
]);
|
|
20
|
+
const resultFor = (entry, record) => record?.generation === entry.generation
|
|
21
|
+
? record.state
|
|
22
|
+
: { status: 'not_found' };
|
|
23
|
+
// Copy only contract fields so extras from custom sources never leave the object.
|
|
24
|
+
const itemFor = (key, record) => ({
|
|
25
|
+
source: key,
|
|
26
|
+
id: record.id,
|
|
27
|
+
name: record.name,
|
|
28
|
+
generation: record.generation,
|
|
29
|
+
createdAt: record.createdAt,
|
|
30
|
+
state: record.state,
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Lists work across `Durability`, `DurabilityAlarms`, and custom sources.
|
|
34
|
+
* `list` and `get` return handles that can be read or waited on, locally or
|
|
35
|
+
* over RPC; `records` returns the same pages as plain data.
|
|
36
|
+
*
|
|
37
|
+
* @experimental
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* class ImageJobs extends DurableObject<Env> {
|
|
42
|
+
* private readonly tracker = new DurabilityTracker({
|
|
43
|
+
* sources: { jobs: this.durability, alarms: this.alarms },
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* listWork(cursor?: string) {
|
|
47
|
+
* return this.tracker.list({ status: 'pending', cursor });
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export class DurabilityTracker {
|
|
53
|
+
constructor(config) {
|
|
54
|
+
_DurabilityTracker_instances.add(this);
|
|
55
|
+
_DurabilityTracker_sources.set(this, void 0);
|
|
56
|
+
__classPrivateFieldSet(this, _DurabilityTracker_sources, Object.entries(config.sources).map(([key, input]) => [
|
|
57
|
+
key,
|
|
58
|
+
trackedSourceOf(input) ?? input,
|
|
59
|
+
]), "f");
|
|
60
|
+
}
|
|
61
|
+
/** Returns one page of handles; pass `cursor` back with the same query. */
|
|
62
|
+
async list(query = {}) {
|
|
63
|
+
const { found, cursor } = await __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_collect).call(this, query);
|
|
64
|
+
const items = found.map(({ source, item }) => __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_handle).call(this, source, item));
|
|
65
|
+
const page = cursor === undefined ? { items } : { items, cursor };
|
|
66
|
+
return page;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns one page of plain records, for transports that cannot carry
|
|
70
|
+
* functions; pass `cursor` back with the same query.
|
|
71
|
+
*/
|
|
72
|
+
async records(query = {}) {
|
|
73
|
+
const { found, cursor } = await __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_collect).call(this, query);
|
|
74
|
+
const records = found.map(({ item }) => item);
|
|
75
|
+
const page = cursor === undefined ? { records } : { records, cursor };
|
|
76
|
+
return page;
|
|
77
|
+
}
|
|
78
|
+
/** Reacquires a handle by id, for example after an RPC disconnect. */
|
|
79
|
+
async get(source, id) {
|
|
80
|
+
const [key, resolved] = __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_source).call(this, source);
|
|
81
|
+
const record = await resolved.get(id);
|
|
82
|
+
if (!record) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_handle).call(this, resolved, itemFor(key, record));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
_DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
|
|
89
|
+
const limit = limitSchema.parse(query.limit ?? 50);
|
|
90
|
+
const candidates = query.source === undefined ? __classPrivateFieldGet(this, _DurabilityTracker_sources, "f") : [__classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_source).call(this, query.source)];
|
|
91
|
+
const [start, startCursor] = query.cursor === undefined
|
|
92
|
+
? [0, null]
|
|
93
|
+
: cursorSchema.parse(JSON.parse(query.cursor));
|
|
94
|
+
const found = [];
|
|
95
|
+
for (const [offset, [key, source]] of candidates.slice(start).entries()) {
|
|
96
|
+
const index = start + offset;
|
|
97
|
+
if (found.length >= limit) {
|
|
98
|
+
return { found, cursor: JSON.stringify([index, null]) };
|
|
99
|
+
}
|
|
100
|
+
// Sources are paged in order so the cursor only tracks one position.
|
|
101
|
+
// eslint-disable-next-line no-await-in-loop
|
|
102
|
+
const page = await source.list({
|
|
103
|
+
limit: limit - found.length,
|
|
104
|
+
cursor: offset === 0 ? (startCursor ?? undefined) : undefined,
|
|
105
|
+
status: query.status,
|
|
106
|
+
name: query.name,
|
|
107
|
+
});
|
|
108
|
+
for (const record of page.records) {
|
|
109
|
+
found.push({ source, item: itemFor(key, record) });
|
|
110
|
+
}
|
|
111
|
+
if (page.cursor !== undefined) {
|
|
112
|
+
return { found, cursor: JSON.stringify([index, page.cursor]) };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return { found, cursor: undefined };
|
|
116
|
+
}, _DurabilityTracker_source = function _DurabilityTracker_source(key) {
|
|
117
|
+
const found = __classPrivateFieldGet(this, _DurabilityTracker_sources, "f").find(([candidate]) => candidate === key);
|
|
118
|
+
if (!found) {
|
|
119
|
+
throw new Error(`Unknown tracked source "${key}"`);
|
|
120
|
+
}
|
|
121
|
+
return found;
|
|
122
|
+
}, _DurabilityTracker_handle = function _DurabilityTracker_handle(source, item) {
|
|
123
|
+
return {
|
|
124
|
+
...item,
|
|
125
|
+
get: async () => resultFor(item, await source.get(item.id)),
|
|
126
|
+
wait: (options) => __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_wait).call(this, source, item, options),
|
|
127
|
+
};
|
|
128
|
+
}, _DurabilityTracker_wait = function _DurabilityTracker_wait(source, entry, options) {
|
|
129
|
+
const timeoutMs = options?.timeoutMs;
|
|
130
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 0) {
|
|
131
|
+
return Promise.reject(new RangeError('timeoutMs must be a non-negative safe integer'));
|
|
132
|
+
}
|
|
133
|
+
const read = async () => resultFor(entry, await source.get(entry.id));
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
let done = false;
|
|
136
|
+
let unwatch;
|
|
137
|
+
let timer;
|
|
138
|
+
const finish = (settle) => {
|
|
139
|
+
if (done) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
done = true;
|
|
143
|
+
clearTimeout(timer);
|
|
144
|
+
unwatch?.();
|
|
145
|
+
settle();
|
|
146
|
+
};
|
|
147
|
+
const fail = (error) => finish(() => reject(error));
|
|
148
|
+
const resolveIfSettled = (result) => {
|
|
149
|
+
if (result.status !== 'pending') {
|
|
150
|
+
finish(() => resolve(result));
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const check = (settled) => {
|
|
154
|
+
if (settled) {
|
|
155
|
+
resolveIfSettled(resultFor(entry, settled));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
read().then(resolveIfSettled, fail);
|
|
159
|
+
};
|
|
160
|
+
// Subscribe before the first read so a settlement in between is not missed.
|
|
161
|
+
unwatch = source.watch?.(entry.id, check);
|
|
162
|
+
if (done) {
|
|
163
|
+
unwatch?.();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
timer = setTimeout(() => {
|
|
167
|
+
read().then((result) => finish(() => resolve(result)), fail);
|
|
168
|
+
}, timeoutMs);
|
|
169
|
+
check();
|
|
170
|
+
});
|
|
171
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "durability",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Alarm-backed durable operations for Cloudflare Durable Objects",
|
|
3
|
+
"version": "4.1.0",
|
|
4
|
+
"description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -15,10 +15,22 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./client": {
|
|
19
|
+
"types": "./dist/client.d.ts",
|
|
20
|
+
"import": "./dist/client.js"
|
|
21
|
+
},
|
|
22
|
+
"./experimental": {
|
|
23
|
+
"types": "./dist/experimental.d.ts",
|
|
24
|
+
"import": "./dist/experimental.js"
|
|
25
|
+
},
|
|
18
26
|
"./routing": {
|
|
19
27
|
"types": "./dist/routing.d.ts",
|
|
20
28
|
"import": "./dist/routing.js"
|
|
21
29
|
},
|
|
30
|
+
"./socket": {
|
|
31
|
+
"types": "./dist/socket.d.ts",
|
|
32
|
+
"import": "./dist/socket.js"
|
|
33
|
+
},
|
|
22
34
|
"./utils": {
|
|
23
35
|
"types": "./dist/utils.d.ts",
|
|
24
36
|
"import": "./dist/utils.js"
|
|
@@ -46,9 +58,10 @@
|
|
|
46
58
|
"workers"
|
|
47
59
|
],
|
|
48
60
|
"dependencies": {
|
|
61
|
+
"@standard-schema/spec": "^1.1.0",
|
|
49
62
|
"zod": "^4.4.3",
|
|
50
|
-
"@durability/
|
|
51
|
-
"@durability/
|
|
63
|
+
"@durability/storage": "^0.1.1",
|
|
64
|
+
"@durability/transforms": "^0.4.0"
|
|
52
65
|
},
|
|
53
66
|
"peerDependencies": {
|
|
54
67
|
"@cloudflare/workers-types": "^4.20260702.1"
|
|
@@ -62,6 +75,7 @@
|
|
|
62
75
|
"@cloudflare/vitest-pool-workers": "^0.17.0",
|
|
63
76
|
"@cloudflare/workers-types": "^4.20260702.1",
|
|
64
77
|
"@types/node": "^24.12.0",
|
|
78
|
+
"capnweb": "^0.12.0",
|
|
65
79
|
"typescript": "6.0.3",
|
|
66
80
|
"vite": "^8.0.0",
|
|
67
81
|
"vitest": "^4.1.5",
|
|
@@ -71,6 +85,6 @@
|
|
|
71
85
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
72
86
|
"lint": "oxlint",
|
|
73
87
|
"test": "vitest run && vitest run --config vitest.workers.config.ts",
|
|
74
|
-
"typecheck": "tsc --noEmit"
|
|
88
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.client.json"
|
|
75
89
|
}
|
|
76
90
|
}
|
package/dist/kv-storage.d.ts
DELETED
package/dist/kv-storage.js
DELETED
|
@@ -1,421 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { alarmRowSchema, callRowSchema, deliveryRowSchema, fanoutMessageRowSchema, } from './storage.js';
|
|
3
|
-
const callTable = {
|
|
4
|
-
name: 'call',
|
|
5
|
-
keyOf: (row) => row.id,
|
|
6
|
-
schema: callRowSchema,
|
|
7
|
-
};
|
|
8
|
-
const alarmTable = {
|
|
9
|
-
name: 'alarm',
|
|
10
|
-
keyOf: (row) => row.name,
|
|
11
|
-
schema: alarmRowSchema,
|
|
12
|
-
};
|
|
13
|
-
const deliveryTable = {
|
|
14
|
-
name: 'fanout-delivery',
|
|
15
|
-
keyOf: (row) => row.id,
|
|
16
|
-
schema: deliveryRowSchema,
|
|
17
|
-
};
|
|
18
|
-
const keyPrefix = '__durability:kv:v1:';
|
|
19
|
-
const timestampWidth = 16;
|
|
20
|
-
const timestampKey = (timestamp) => timestamp.toString().padStart(timestampWidth, '0');
|
|
21
|
-
const indexTimestampSchema = z
|
|
22
|
-
.string()
|
|
23
|
-
.regex(/^\d{16}$/)
|
|
24
|
-
.transform(Number)
|
|
25
|
-
.pipe(z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER));
|
|
26
|
-
const keySchema = z.string().array();
|
|
27
|
-
const counterSchema = z.number().int().nonnegative().optional();
|
|
28
|
-
/** Deleting more keys than this in one KV call exceeds the Durable Object limit. */
|
|
29
|
-
const purgeBatchSize = 20;
|
|
30
|
-
const encodeIndexPart = (value) => encodeURIComponent(value);
|
|
31
|
-
const deliveryAttemptClass = (row) => row.attempt === 0 ? 0 : 1;
|
|
32
|
-
/**
|
|
33
|
-
* One record kind stored as a primary row plus two ordered index keys:
|
|
34
|
-
* `pending-<name>:<next_attempt_at>:<key>` for due work and
|
|
35
|
-
* `created-<name>:<created_at>:<key>` for retention purges.
|
|
36
|
-
*/
|
|
37
|
-
class KvTable {
|
|
38
|
-
constructor(kv, spec, atomically) {
|
|
39
|
-
this.kv = kv;
|
|
40
|
-
this.spec = spec;
|
|
41
|
-
this.atomically = atomically;
|
|
42
|
-
this.rowPrefix = `${keyPrefix}${spec.name}:`;
|
|
43
|
-
this.pendingPrefix = `${keyPrefix}pending-${spec.name}:`;
|
|
44
|
-
this.createdPrefix = `${keyPrefix}created-${spec.name}:`;
|
|
45
|
-
}
|
|
46
|
-
async get(key) {
|
|
47
|
-
return this.spec.schema
|
|
48
|
-
.optional()
|
|
49
|
-
.parse(await this.kv.get(this.rowKey(key)));
|
|
50
|
-
}
|
|
51
|
-
async listDue(now, limit) {
|
|
52
|
-
const index = await this.kv.list({
|
|
53
|
-
prefix: this.pendingPrefix,
|
|
54
|
-
end: `${this.pendingPrefix}${timestampKey(now)};`,
|
|
55
|
-
limit,
|
|
56
|
-
});
|
|
57
|
-
return this.rows(keySchema.parse([...index.values()]));
|
|
58
|
-
}
|
|
59
|
-
async nextPendingAt() {
|
|
60
|
-
const index = await this.kv.list({ prefix: this.pendingPrefix, limit: 1 });
|
|
61
|
-
const first = index.keys().next().value;
|
|
62
|
-
if (typeof first !== 'string') {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
return indexTimestampSchema.parse(first.slice(this.pendingPrefix.length, this.pendingPrefix.length + timestampWidth));
|
|
66
|
-
}
|
|
67
|
-
insert(row) {
|
|
68
|
-
return this.atomically(async (table) => {
|
|
69
|
-
if ((await table.get(this.spec.keyOf(row))) !== undefined) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
await table.write(undefined, row);
|
|
73
|
-
return true;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
upsert(row) {
|
|
77
|
-
return this.atomically(async (table) => {
|
|
78
|
-
await table.write(await table.get(this.spec.keyOf(row)), row);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
claimAttempt(key, generation, maxAttempts) {
|
|
82
|
-
return this.atomically(async (table) => {
|
|
83
|
-
const row = await table.pending(key, generation);
|
|
84
|
-
if (!row || row.attempt >= maxAttempts) {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
const attempt = row.attempt + 1;
|
|
88
|
-
await table.write(row, { ...row, attempt });
|
|
89
|
-
return attempt;
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
settle(key, generation, attempt, patch) {
|
|
93
|
-
return this.atomically(async (table) => {
|
|
94
|
-
const row = await table.pending(key, generation);
|
|
95
|
-
if (!row || row.attempt !== attempt) {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
await table.write(row, { ...row, ...patch });
|
|
99
|
-
return true;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
exhaust(key, generation, maxAttempts, patch) {
|
|
103
|
-
return this.atomically(async (table) => {
|
|
104
|
-
const row = await table.pending(key, generation);
|
|
105
|
-
if (!row || row.attempt < maxAttempts) {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
await table.write(row, { ...row, ...patch });
|
|
109
|
-
return true;
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
remove(key, generation, attempt) {
|
|
113
|
-
return this.atomically(async (table) => {
|
|
114
|
-
const row = await table.pending(key, generation);
|
|
115
|
-
if (!row || row.attempt !== attempt) {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
await table.kv.delete([table.rowKey(key), ...table.indexKeys(row)]);
|
|
119
|
-
return true;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
/** Deletes one bounded batch so a purge never exceeds the KV delete limit. */
|
|
123
|
-
async deleteCreatedBefore(before) {
|
|
124
|
-
const index = await this.kv.list({
|
|
125
|
-
prefix: this.createdPrefix,
|
|
126
|
-
end: `${this.createdPrefix}${timestampKey(before)}:`,
|
|
127
|
-
limit: purgeBatchSize,
|
|
128
|
-
});
|
|
129
|
-
const rows = await this.rows(keySchema.parse([...index.values()]));
|
|
130
|
-
if (rows.length === 0) {
|
|
131
|
-
return 0;
|
|
132
|
-
}
|
|
133
|
-
await this.kv.delete(rows.flatMap((row) => [
|
|
134
|
-
this.rowKey(this.spec.keyOf(row)),
|
|
135
|
-
...this.indexKeys(row),
|
|
136
|
-
]));
|
|
137
|
-
return rows.length;
|
|
138
|
-
}
|
|
139
|
-
async pending(key, generation) {
|
|
140
|
-
const row = await this.get(key);
|
|
141
|
-
if (row?.generation_id !== generation || row.status !== 'pending') {
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
return row;
|
|
145
|
-
}
|
|
146
|
-
async rows(keys) {
|
|
147
|
-
if (keys.length === 0) {
|
|
148
|
-
return [];
|
|
149
|
-
}
|
|
150
|
-
const found = await this.kv.get(keys.map((key) => this.rowKey(key)));
|
|
151
|
-
// The batch result is keyed, not ordered; keep the index order.
|
|
152
|
-
return this.spec.schema
|
|
153
|
-
.array()
|
|
154
|
-
.parse(keys.map((key) => found.get(this.rowKey(key))));
|
|
155
|
-
}
|
|
156
|
-
async write(previous, next) {
|
|
157
|
-
const key = this.spec.keyOf(next);
|
|
158
|
-
const previousIndexes = previous ? this.indexKeys(previous) : [];
|
|
159
|
-
const nextIndexes = this.indexKeys(next);
|
|
160
|
-
const stale = previousIndexes.filter((index) => !nextIndexes.includes(index));
|
|
161
|
-
if (stale.length > 0) {
|
|
162
|
-
await this.kv.delete(stale);
|
|
163
|
-
}
|
|
164
|
-
const entries = { [this.rowKey(key)]: next };
|
|
165
|
-
for (const index of nextIndexes) {
|
|
166
|
-
if (!previousIndexes.includes(index)) {
|
|
167
|
-
entries[index] = key;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
await this.kv.put(entries);
|
|
171
|
-
}
|
|
172
|
-
rowKey(key) {
|
|
173
|
-
return `${this.rowPrefix}${key}`;
|
|
174
|
-
}
|
|
175
|
-
indexKeys(row) {
|
|
176
|
-
const key = this.spec.keyOf(row);
|
|
177
|
-
const created = `${this.createdPrefix}${timestampKey(row.created_at)}:${key}`;
|
|
178
|
-
if (row.status !== 'pending') {
|
|
179
|
-
return [created];
|
|
180
|
-
}
|
|
181
|
-
return [
|
|
182
|
-
created,
|
|
183
|
-
`${this.pendingPrefix}${timestampKey(row.next_attempt_at)}:${key}`,
|
|
184
|
-
];
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
const fanoutMessagePrefix = `${keyPrefix}fanout-message:`;
|
|
188
|
-
const fanoutSeqCounterKey = `${keyPrefix}fanout-seq`;
|
|
189
|
-
const fanoutPendingCounterKey = `${keyPrefix}fanout-pending-count`;
|
|
190
|
-
const fanoutTargetPrefix = `${keyPrefix}fanout-target:`;
|
|
191
|
-
const fanoutTargetIndexPrefix = `${keyPrefix}fanout-target-index:`;
|
|
192
|
-
const fanoutTargetPendingPrefix = `${keyPrefix}pending-fanout-target:`;
|
|
193
|
-
class KvDeliveryTable extends KvTable {
|
|
194
|
-
constructor(kv, atomically) {
|
|
195
|
-
super(kv, deliveryTable, atomically);
|
|
196
|
-
this.globalPendingPrefix = this.pendingPrefix;
|
|
197
|
-
}
|
|
198
|
-
nextSeq() {
|
|
199
|
-
return this.atomically(async (raw) => {
|
|
200
|
-
const table = raw;
|
|
201
|
-
const seq = counterSchema.parse(await table.kv.get(fanoutSeqCounterKey)) ?? 0;
|
|
202
|
-
if (!Number.isSafeInteger(seq + 1)) {
|
|
203
|
-
throw new RangeError('Fanout sequence exhausted');
|
|
204
|
-
}
|
|
205
|
-
await table.kv.put(fanoutSeqCounterKey, seq + 1);
|
|
206
|
-
return seq;
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
indexKeys(row) {
|
|
210
|
-
const created = `${this.createdPrefix}${timestampKey(row.created_at)}:${row.id}`;
|
|
211
|
-
const attemptClass = deliveryAttemptClass(row);
|
|
212
|
-
const sequence = timestampKey(row.seq);
|
|
213
|
-
const target = encodeIndexPart(row.target_id);
|
|
214
|
-
const id = encodeIndexPart(row.id);
|
|
215
|
-
return [
|
|
216
|
-
created,
|
|
217
|
-
`${this.globalPendingPrefix}${attemptClass}:${timestampKey(row.next_attempt_at)}:${sequence}:${target}:${id}`,
|
|
218
|
-
`${fanoutTargetPendingPrefix}${target}:${attemptClass}:${timestampKey(row.next_attempt_at)}:${sequence}:${id}`,
|
|
219
|
-
];
|
|
220
|
-
}
|
|
221
|
-
async getMessage(key) {
|
|
222
|
-
return fanoutMessageRowSchema
|
|
223
|
-
.optional()
|
|
224
|
-
.parse(await this.kv.get(this.messageKey(key)));
|
|
225
|
-
}
|
|
226
|
-
insertMessage(row) {
|
|
227
|
-
return this.atomically(async (raw) => {
|
|
228
|
-
const table = raw;
|
|
229
|
-
if ((await table.getMessage(row.key)) !== undefined) {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
await table.kv.put(table.messageKey(row.key), row);
|
|
233
|
-
return true;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
async listTargets() {
|
|
237
|
-
const targets = [];
|
|
238
|
-
let startAfter;
|
|
239
|
-
for (;;) {
|
|
240
|
-
// eslint-disable-next-line no-await-in-loop
|
|
241
|
-
const page = await this.kv.list({
|
|
242
|
-
prefix: fanoutTargetIndexPrefix,
|
|
243
|
-
limit: 128,
|
|
244
|
-
...(startAfter === undefined ? {} : { startAfter }),
|
|
245
|
-
});
|
|
246
|
-
targets.push(...page.values());
|
|
247
|
-
if (page.size < 128) {
|
|
248
|
-
return targets;
|
|
249
|
-
}
|
|
250
|
-
startAfter = [...page.keys()][page.size - 1];
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
async listDueForTarget(target, now, limit) {
|
|
254
|
-
const firstPrefix = `${fanoutTargetPendingPrefix}${encodeIndexPart(target)}:0:`;
|
|
255
|
-
const first = await this.dueIndex(firstPrefix, now, limit);
|
|
256
|
-
if (first.length >= limit) {
|
|
257
|
-
return first;
|
|
258
|
-
}
|
|
259
|
-
const retryPrefix = `${fanoutTargetPendingPrefix}${encodeIndexPart(target)}:1:`;
|
|
260
|
-
const retries = await this.dueIndex(retryPrefix, now, limit - first.length);
|
|
261
|
-
return [...first, ...retries];
|
|
262
|
-
}
|
|
263
|
-
async pendingCount() {
|
|
264
|
-
return counterSchema.parse(await this.kv.get(fanoutPendingCounterKey)) ?? 0;
|
|
265
|
-
}
|
|
266
|
-
async listDue(now, limit) {
|
|
267
|
-
const firstPrefix = `${this.globalPendingPrefix}0:`;
|
|
268
|
-
const first = await this.dueIndex(firstPrefix, now, limit);
|
|
269
|
-
if (first.length >= limit) {
|
|
270
|
-
return first;
|
|
271
|
-
}
|
|
272
|
-
const retryPrefix = `${this.globalPendingPrefix}1:`;
|
|
273
|
-
const retries = await this.dueIndex(retryPrefix, now, limit - first.length);
|
|
274
|
-
return [...first, ...retries];
|
|
275
|
-
}
|
|
276
|
-
async nextPendingAt() {
|
|
277
|
-
const values = await Promise.all([0, 1].map(async (attemptClass) => {
|
|
278
|
-
const prefix = `${this.globalPendingPrefix}${attemptClass}:`;
|
|
279
|
-
const index = await this.kv.list({ prefix, limit: 1 });
|
|
280
|
-
const first = index.keys().next().value;
|
|
281
|
-
if (typeof first !== 'string') {
|
|
282
|
-
return undefined;
|
|
283
|
-
}
|
|
284
|
-
const offset = prefix.length;
|
|
285
|
-
return indexTimestampSchema.parse(first.slice(offset, offset + timestampWidth));
|
|
286
|
-
}));
|
|
287
|
-
const pending = values.filter((value) => value !== undefined);
|
|
288
|
-
return pending.length === 0 ? undefined : Math.min(...pending);
|
|
289
|
-
}
|
|
290
|
-
remove(key, generation, attempt) {
|
|
291
|
-
return this.atomically(async (raw) => {
|
|
292
|
-
const table = raw;
|
|
293
|
-
const row = await table.get(key);
|
|
294
|
-
if (row?.generation_id !== generation ||
|
|
295
|
-
row.status !== 'pending' ||
|
|
296
|
-
row.attempt !== attempt) {
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
await table.kv.delete([table.rowKey(key), ...table.indexKeys(row)]);
|
|
300
|
-
await table.adjustTargetCount(row.target_id, -1);
|
|
301
|
-
await table.adjustPendingCount(-1);
|
|
302
|
-
await table.decrementManifest(row.message_key);
|
|
303
|
-
return true;
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
deleteCreatedBefore(before) {
|
|
307
|
-
return this.atomically(async (raw) => {
|
|
308
|
-
const table = raw;
|
|
309
|
-
const index = await table.kv.list({
|
|
310
|
-
prefix: table.createdPrefix,
|
|
311
|
-
end: `${table.createdPrefix}${timestampKey(before)}:`,
|
|
312
|
-
limit: purgeBatchSize,
|
|
313
|
-
});
|
|
314
|
-
const rows = await table.rows(keySchema.parse([...index.values()]));
|
|
315
|
-
for (const row of rows) {
|
|
316
|
-
// Already the transaction's table; remove cannot nest a transaction.
|
|
317
|
-
// eslint-disable-next-line no-await-in-loop
|
|
318
|
-
await table.remove(row.id, row.generation_id, row.attempt);
|
|
319
|
-
}
|
|
320
|
-
return rows.length;
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
async write(previous, next) {
|
|
324
|
-
await super.write(previous, next);
|
|
325
|
-
if (previous === undefined) {
|
|
326
|
-
await this.adjustTargetCount(next.target_id, 1);
|
|
327
|
-
await this.adjustPendingCount(1);
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
if (previous.target_id !== next.target_id) {
|
|
331
|
-
await this.adjustTargetCount(previous.target_id, -1);
|
|
332
|
-
await this.adjustTargetCount(next.target_id, 1);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
async dueIndex(prefix, now, limit) {
|
|
336
|
-
if (limit <= 0) {
|
|
337
|
-
return [];
|
|
338
|
-
}
|
|
339
|
-
const rows = [];
|
|
340
|
-
let startAfter;
|
|
341
|
-
for (;;) {
|
|
342
|
-
const pageSize = Math.min(128, limit - rows.length);
|
|
343
|
-
// eslint-disable-next-line no-await-in-loop
|
|
344
|
-
const index = await this.kv.list({
|
|
345
|
-
prefix,
|
|
346
|
-
end: `${prefix}${timestampKey(now)};`,
|
|
347
|
-
limit: pageSize,
|
|
348
|
-
...(startAfter === undefined ? {} : { startAfter }),
|
|
349
|
-
});
|
|
350
|
-
// eslint-disable-next-line no-await-in-loop
|
|
351
|
-
rows.push(...(await this.rows(keySchema.parse([...index.values()]))));
|
|
352
|
-
if (index.size < pageSize || rows.length >= limit) {
|
|
353
|
-
return rows.sort((left, right) => left.seq - right.seq);
|
|
354
|
-
}
|
|
355
|
-
startAfter = [...index.keys()][index.size - 1];
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
messageKey(key) {
|
|
359
|
-
return `${fanoutMessagePrefix}${key}`;
|
|
360
|
-
}
|
|
361
|
-
targetCountKey(target) {
|
|
362
|
-
return `${fanoutTargetPrefix}${encodeIndexPart(target)}`;
|
|
363
|
-
}
|
|
364
|
-
targetIndexKey(target) {
|
|
365
|
-
return `${fanoutTargetIndexPrefix}${encodeIndexPart(target)}`;
|
|
366
|
-
}
|
|
367
|
-
async adjustTargetCount(target, delta) {
|
|
368
|
-
const count = counterSchema.parse(await this.kv.get(this.targetCountKey(target))) ?? 0;
|
|
369
|
-
const next = count + delta;
|
|
370
|
-
if (next <= 0) {
|
|
371
|
-
await this.kv.delete([
|
|
372
|
-
this.targetCountKey(target),
|
|
373
|
-
this.targetIndexKey(target),
|
|
374
|
-
]);
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
await this.kv.put({
|
|
378
|
-
[this.targetCountKey(target)]: next,
|
|
379
|
-
[this.targetIndexKey(target)]: target,
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
async adjustPendingCount(delta) {
|
|
383
|
-
const count = counterSchema.parse(await this.kv.get(fanoutPendingCounterKey)) ?? 0;
|
|
384
|
-
await this.kv.put(fanoutPendingCounterKey, count + delta);
|
|
385
|
-
}
|
|
386
|
-
async decrementManifest(messageKey, amount = 1) {
|
|
387
|
-
const manifest = fanoutMessageRowSchema
|
|
388
|
-
.optional()
|
|
389
|
-
.parse(await this.kv.get(this.messageKey(messageKey)));
|
|
390
|
-
if (manifest === undefined || manifest.remaining <= amount) {
|
|
391
|
-
await this.kv.delete(this.messageKey(messageKey));
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
await this.kv.put(this.messageKey(messageKey), {
|
|
395
|
-
...manifest,
|
|
396
|
-
remaining: manifest.remaining - amount,
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
class KvSession {
|
|
401
|
-
constructor(physicalAlarm, atomically) {
|
|
402
|
-
this.physicalAlarm = physicalAlarm;
|
|
403
|
-
this.calls = new KvTable(physicalAlarm, callTable, (callback) => atomically((session) => callback(session.calls)));
|
|
404
|
-
this.alarms = new KvTable(physicalAlarm, alarmTable, (callback) => atomically((session) => callback(session.alarms)));
|
|
405
|
-
this.deliveries = new KvDeliveryTable(physicalAlarm, (callback) => atomically((session) => callback(session.deliveries)));
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
const transactionalSession = (kv) => {
|
|
409
|
-
const session = new KvSession(kv, (callback) => callback(session));
|
|
410
|
-
return session;
|
|
411
|
-
};
|
|
412
|
-
export const createKvDurabilityStorage = (storage) => {
|
|
413
|
-
const transaction = (callback) => storage.transaction((durableTransaction) => callback(transactionalSession(durableTransaction)));
|
|
414
|
-
const session = new KvSession(storage, transaction);
|
|
415
|
-
return {
|
|
416
|
-
calls: session.calls,
|
|
417
|
-
alarms: session.alarms,
|
|
418
|
-
deliveries: session.deliveries,
|
|
419
|
-
transaction,
|
|
420
|
-
};
|
|
421
|
-
};
|
package/dist/sqlite-storage.d.ts
DELETED