durability 3.0.0 → 4.0.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 +403 -27
- 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 +32 -1
- package/dist/durability.js +119 -61
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +31 -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 +40 -17
- package/dist/scheduler.js +150 -74
- 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 -64
- 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/sqlite-storage.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { alarmRowSchema, callRowSchema, deliveryRowSchema, fanoutMessageRowSchema, } from './storage.js';
|
|
3
|
-
const callTable = {
|
|
4
|
-
table: 'durability_calls',
|
|
5
|
-
key: 'id',
|
|
6
|
-
schema: callRowSchema,
|
|
7
|
-
columns: callRowSchema.keyof().options,
|
|
8
|
-
};
|
|
9
|
-
const alarmTable = {
|
|
10
|
-
table: 'durability_alarms',
|
|
11
|
-
key: 'name',
|
|
12
|
-
schema: alarmRowSchema,
|
|
13
|
-
columns: alarmRowSchema.keyof().options,
|
|
14
|
-
};
|
|
15
|
-
const deliveryTable = {
|
|
16
|
-
table: 'durability_fanout_deliveries',
|
|
17
|
-
key: 'id',
|
|
18
|
-
schema: deliveryRowSchema,
|
|
19
|
-
columns: deliveryRowSchema.keyof().options,
|
|
20
|
-
dueOrder: 'attempt ASC, seq ASC',
|
|
21
|
-
};
|
|
22
|
-
const attemptRowSchema = z.object({
|
|
23
|
-
attempt: z.number().int().nonnegative(),
|
|
24
|
-
});
|
|
25
|
-
const countRowSchema = z.object({ count: z.number().int().nonnegative() });
|
|
26
|
-
const messageKeyRowSchema = z.object({ message_key: z.string() });
|
|
27
|
-
const messageCountRowSchema = z.object({
|
|
28
|
-
message_key: z.string(),
|
|
29
|
-
count: z.number().int().positive(),
|
|
30
|
-
});
|
|
31
|
-
const seqRowSchema = z.object({ seq: z.number().int().nonnegative() });
|
|
32
|
-
const definedEntries = (patch) => Object.entries(patch).filter((entry) => entry[1] !== undefined);
|
|
33
|
-
class SqliteTable {
|
|
34
|
-
constructor(sql, spec, atomically) {
|
|
35
|
-
this.sql = sql;
|
|
36
|
-
this.spec = spec;
|
|
37
|
-
this.atomically = atomically;
|
|
38
|
-
}
|
|
39
|
-
async get(key) {
|
|
40
|
-
return this.rows(`SELECT * FROM ${this.spec.table} WHERE ${this.spec.key} = ? LIMIT 1`, key)[0];
|
|
41
|
-
}
|
|
42
|
-
async listDue(now, limit) {
|
|
43
|
-
return this.rows(`SELECT * FROM ${this.spec.table}
|
|
44
|
-
WHERE status = 'pending' AND next_attempt_at <= ?
|
|
45
|
-
ORDER BY ${this.spec.dueOrder ?? 'next_attempt_at ASC'}
|
|
46
|
-
LIMIT ?`, now, limit);
|
|
47
|
-
}
|
|
48
|
-
async nextPendingAt() {
|
|
49
|
-
return this.rows(`SELECT * FROM ${this.spec.table}
|
|
50
|
-
WHERE status = 'pending'
|
|
51
|
-
ORDER BY next_attempt_at ASC
|
|
52
|
-
LIMIT 1`)[0]?.next_attempt_at;
|
|
53
|
-
}
|
|
54
|
-
async insert(row) {
|
|
55
|
-
const { table, key, columns } = this.spec;
|
|
56
|
-
return this.changed(`INSERT INTO ${table} (${columns.join(', ')})
|
|
57
|
-
VALUES (${columns.map(() => '?').join(', ')})
|
|
58
|
-
ON CONFLICT(${key}) DO NOTHING
|
|
59
|
-
RETURNING ${key}`, ...this.values(row));
|
|
60
|
-
}
|
|
61
|
-
async upsert(row) {
|
|
62
|
-
const { table, key, columns } = this.spec;
|
|
63
|
-
const replacements = columns
|
|
64
|
-
.filter((column) => column !== key)
|
|
65
|
-
.map((column) => `${column} = excluded.${column}`);
|
|
66
|
-
this.sql.exec(`INSERT INTO ${table} (${columns.join(', ')})
|
|
67
|
-
VALUES (${columns.map(() => '?').join(', ')})
|
|
68
|
-
ON CONFLICT(${key}) DO UPDATE SET ${replacements.join(', ')}`, ...this.values(row));
|
|
69
|
-
}
|
|
70
|
-
async claimAttempt(key, generation, maxAttempts) {
|
|
71
|
-
return attemptRowSchema.optional().parse(this.sql
|
|
72
|
-
.exec(`UPDATE ${this.spec.table}
|
|
73
|
-
SET attempt = attempt + 1
|
|
74
|
-
WHERE ${this.spec.key} = ? AND generation_id = ?
|
|
75
|
-
AND status = 'pending' AND attempt < ?
|
|
76
|
-
RETURNING attempt`, key, generation, maxAttempts)
|
|
77
|
-
.toArray()[0])?.attempt;
|
|
78
|
-
}
|
|
79
|
-
async settle(key, generation, attempt, patch) {
|
|
80
|
-
return this.update(key, generation, '=', attempt, patch);
|
|
81
|
-
}
|
|
82
|
-
async exhaust(key, generation, maxAttempts, patch) {
|
|
83
|
-
return this.update(key, generation, '>=', maxAttempts, patch);
|
|
84
|
-
}
|
|
85
|
-
async remove(key, generation, attempt) {
|
|
86
|
-
return this.changed(`DELETE FROM ${this.spec.table}
|
|
87
|
-
WHERE ${this.spec.key} = ? AND generation_id = ?
|
|
88
|
-
AND status = 'pending' AND attempt = ?
|
|
89
|
-
RETURNING ${this.spec.key}`, key, generation, attempt);
|
|
90
|
-
}
|
|
91
|
-
async deleteCreatedBefore(before) {
|
|
92
|
-
return this.atomically((sql) => {
|
|
93
|
-
const { count } = countRowSchema.parse(sql
|
|
94
|
-
.exec(`SELECT COUNT(*) AS count FROM ${this.spec.table} WHERE created_at < ?`, before)
|
|
95
|
-
.toArray()[0]);
|
|
96
|
-
sql.exec(`DELETE FROM ${this.spec.table} WHERE created_at < ?`, before);
|
|
97
|
-
return count;
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
update(key, generation, attemptOperator, attemptBound, patch) {
|
|
101
|
-
const entries = definedEntries(patch);
|
|
102
|
-
const unknown = entries
|
|
103
|
-
.map(([column]) => column)
|
|
104
|
-
.filter((column) => !this.spec.columns.includes(column));
|
|
105
|
-
if (unknown.length > 0) {
|
|
106
|
-
throw new Error(`Unknown ${this.spec.table} columns: ${unknown.join(', ')}`);
|
|
107
|
-
}
|
|
108
|
-
return this.changed(`UPDATE ${this.spec.table}
|
|
109
|
-
SET ${entries.map(([column]) => `${column} = ?`).join(', ')}
|
|
110
|
-
WHERE ${this.spec.key} = ? AND generation_id = ?
|
|
111
|
-
AND status = 'pending' AND attempt ${attemptOperator} ?
|
|
112
|
-
RETURNING ${this.spec.key}`, ...entries.map(([, value]) => value), key, generation, attemptBound);
|
|
113
|
-
}
|
|
114
|
-
values(row) {
|
|
115
|
-
// Rows are schema-validated, so every column is present; `?? null` only satisfies the index type.
|
|
116
|
-
return this.spec.columns.map((column) => row[column] ?? null);
|
|
117
|
-
}
|
|
118
|
-
rows(query, ...bindings) {
|
|
119
|
-
return this.spec.schema
|
|
120
|
-
.array()
|
|
121
|
-
.parse(this.sql.exec(query, ...bindings).toArray());
|
|
122
|
-
}
|
|
123
|
-
changed(query, ...bindings) {
|
|
124
|
-
return this.sql.exec(query, ...bindings).toArray().length > 0;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
class SqliteDeliveryTable extends SqliteTable {
|
|
128
|
-
constructor(sql, atomically) {
|
|
129
|
-
super(sql, deliveryTable, atomically);
|
|
130
|
-
}
|
|
131
|
-
async nextSeq() {
|
|
132
|
-
return this.atomically((sql) => seqRowSchema.parse(sql
|
|
133
|
-
.exec(`UPDATE durability_fanout_seq
|
|
134
|
-
SET next_seq = next_seq + 1
|
|
135
|
-
WHERE id = 1
|
|
136
|
-
RETURNING next_seq - 1 AS seq`)
|
|
137
|
-
.toArray()[0]).seq);
|
|
138
|
-
}
|
|
139
|
-
async getMessage(key) {
|
|
140
|
-
return fanoutMessageRowSchema
|
|
141
|
-
.optional()
|
|
142
|
-
.parse(this.sql
|
|
143
|
-
.exec('SELECT * FROM durability_fanout_messages WHERE key = ? LIMIT 1', key)
|
|
144
|
-
.toArray()[0]);
|
|
145
|
-
}
|
|
146
|
-
async insertMessage(row) {
|
|
147
|
-
const columns = fanoutMessageRowSchema.keyof().options;
|
|
148
|
-
return this.changed(`INSERT INTO durability_fanout_messages (${columns.join(', ')})
|
|
149
|
-
VALUES (${columns.map(() => '?').join(', ')})
|
|
150
|
-
ON CONFLICT(key) DO NOTHING
|
|
151
|
-
RETURNING key`, row.key, row.id, row.payload, row.targets, row.remaining, row.seq, row.created_at, row.generation_id);
|
|
152
|
-
}
|
|
153
|
-
async listTargets() {
|
|
154
|
-
return z
|
|
155
|
-
.string()
|
|
156
|
-
.array()
|
|
157
|
-
.parse(this.sql
|
|
158
|
-
.exec(`SELECT DISTINCT target_id FROM durability_fanout_deliveries
|
|
159
|
-
ORDER BY target_id`)
|
|
160
|
-
.toArray()
|
|
161
|
-
.map((row) => row['target_id']));
|
|
162
|
-
}
|
|
163
|
-
async listDueForTarget(target, now, limit) {
|
|
164
|
-
return this.rows(`SELECT * FROM durability_fanout_deliveries
|
|
165
|
-
WHERE target_id = ? AND status = 'pending'
|
|
166
|
-
AND next_attempt_at <= ?
|
|
167
|
-
ORDER BY CASE WHEN attempt = 0 THEN 0 ELSE 1 END ASC, seq ASC
|
|
168
|
-
LIMIT ?`, target, now, limit);
|
|
169
|
-
}
|
|
170
|
-
async pendingCount() {
|
|
171
|
-
return countRowSchema.parse(this.sql
|
|
172
|
-
.exec(`SELECT COUNT(*) AS count FROM durability_fanout_deliveries
|
|
173
|
-
WHERE status = 'pending'`)
|
|
174
|
-
.toArray()[0]).count;
|
|
175
|
-
}
|
|
176
|
-
async listDue(now, limit) {
|
|
177
|
-
return this.rows(`SELECT * FROM durability_fanout_deliveries
|
|
178
|
-
WHERE status = 'pending' AND next_attempt_at <= ?
|
|
179
|
-
ORDER BY CASE WHEN attempt = 0 THEN 0 ELSE 1 END ASC, seq ASC
|
|
180
|
-
LIMIT ?`, now, limit);
|
|
181
|
-
}
|
|
182
|
-
async nextPendingAt() {
|
|
183
|
-
return this.rows(`SELECT * FROM durability_fanout_deliveries
|
|
184
|
-
WHERE status = 'pending'
|
|
185
|
-
ORDER BY next_attempt_at ASC
|
|
186
|
-
LIMIT 1`)[0]?.next_attempt_at;
|
|
187
|
-
}
|
|
188
|
-
async remove(key, generation, attempt) {
|
|
189
|
-
return this.atomically((sql) => {
|
|
190
|
-
const messageKey = messageKeyRowSchema.optional().parse(sql
|
|
191
|
-
.exec(`DELETE FROM durability_fanout_deliveries
|
|
192
|
-
WHERE id = ? AND generation_id = ?
|
|
193
|
-
AND status = 'pending' AND attempt = ?
|
|
194
|
-
RETURNING message_key`, key, generation, attempt)
|
|
195
|
-
.toArray()[0])?.message_key;
|
|
196
|
-
if (messageKey === undefined) {
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
sql.exec(`UPDATE durability_fanout_messages
|
|
200
|
-
SET remaining = remaining - 1
|
|
201
|
-
WHERE key = ?`, messageKey);
|
|
202
|
-
sql.exec(`DELETE FROM durability_fanout_messages
|
|
203
|
-
WHERE key = ? AND remaining = 0`, messageKey);
|
|
204
|
-
return true;
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
async deleteCreatedBefore(before) {
|
|
208
|
-
return this.atomically((sql) => {
|
|
209
|
-
const messageCounts = messageCountRowSchema.array().parse(sql
|
|
210
|
-
.exec(`SELECT message_key, COUNT(*) AS count
|
|
211
|
-
FROM durability_fanout_deliveries
|
|
212
|
-
WHERE created_at < ?
|
|
213
|
-
GROUP BY message_key`, before)
|
|
214
|
-
.toArray());
|
|
215
|
-
if (messageCounts.length === 0) {
|
|
216
|
-
return 0;
|
|
217
|
-
}
|
|
218
|
-
const { count } = countRowSchema.parse(sql
|
|
219
|
-
.exec(`SELECT COUNT(*) AS count FROM durability_fanout_deliveries
|
|
220
|
-
WHERE created_at < ?`, before)
|
|
221
|
-
.toArray()[0]);
|
|
222
|
-
sql.exec('DELETE FROM durability_fanout_deliveries WHERE created_at < ?', before);
|
|
223
|
-
for (const { message_key: messageKey, count: removed } of messageCounts) {
|
|
224
|
-
sql.exec(`UPDATE durability_fanout_messages
|
|
225
|
-
SET remaining = remaining - ?
|
|
226
|
-
WHERE key = ?`, removed, messageKey);
|
|
227
|
-
sql.exec(`DELETE FROM durability_fanout_messages
|
|
228
|
-
WHERE key = ? AND remaining = 0`, messageKey);
|
|
229
|
-
}
|
|
230
|
-
return count;
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
class SqliteSession {
|
|
235
|
-
constructor(sql, physicalAlarm, atomically) {
|
|
236
|
-
this.physicalAlarm = physicalAlarm;
|
|
237
|
-
this.calls = new SqliteTable(sql, callTable, atomically);
|
|
238
|
-
this.alarms = new SqliteTable(sql, alarmTable, atomically);
|
|
239
|
-
this.deliveries = new SqliteDeliveryTable(sql, atomically);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
export const createSqliteDurabilityStorage = (storage) => {
|
|
243
|
-
const transaction = (callback) => storage.transaction((durableTransaction) => callback(new SqliteSession(storage.sql, durableTransaction, (run) => Promise.resolve(run(storage.sql)))));
|
|
244
|
-
const session = new SqliteSession(storage.sql, storage, (run) => storage.transaction(() => Promise.resolve(run(storage.sql))));
|
|
245
|
-
return {
|
|
246
|
-
calls: session.calls,
|
|
247
|
-
alarms: session.alarms,
|
|
248
|
-
deliveries: session.deliveries,
|
|
249
|
-
transaction,
|
|
250
|
-
};
|
|
251
|
-
};
|