@fedify/sqlite 2.0.0-dev.215 → 2.0.0-dev.226
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/deno.json +4 -3
- package/dist/dist/sqlite.node.d.cts +2 -0
- package/dist/kv.d.cts +3 -3
- package/dist/mod.cjs +3 -1
- package/dist/mod.d.cts +2 -1
- package/dist/mod.d.ts +2 -1
- package/dist/mod.js +2 -1
- package/dist/mq.cjs +369 -0
- package/dist/mq.d.cts +112 -0
- package/dist/mq.d.ts +113 -0
- package/dist/mq.js +368 -0
- package/package.json +14 -3
- package/src/mod.ts +1 -0
- package/src/mq.test.ts +23 -0
- package/src/mq.ts +579 -0
- package/tsdown.config.ts +7 -1
package/deno.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/sqlite",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.226+c28b813d",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/mod.ts",
|
|
7
|
-
"./kv": "./src/kv.ts"
|
|
7
|
+
"./kv": "./src/kv.ts",
|
|
8
|
+
"./mq": "./src/mq.ts"
|
|
8
9
|
},
|
|
9
10
|
"imports": {
|
|
10
11
|
"#sqlite": "./src/sqlite.node.ts"
|
|
@@ -22,6 +23,6 @@
|
|
|
22
23
|
},
|
|
23
24
|
"tasks": {
|
|
24
25
|
"check": "deno fmt --check && deno lint && deno check",
|
|
25
|
-
"test": "deno test --allow-net --allow-env --doc --no-check=leaks"
|
|
26
|
+
"test": "deno test --allow-net --allow-env --allow-read --allow-write --doc --no-check=leaks"
|
|
26
27
|
}
|
|
27
28
|
}
|
package/dist/kv.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DatabaseSync } from "./dist/sqlite.node.cjs";
|
|
2
2
|
import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions } from "@fedify/fedify";
|
|
3
3
|
|
|
4
4
|
//#region src/kv.d.ts
|
|
@@ -37,14 +37,14 @@ interface SqliteKvStoreOptions {
|
|
|
37
37
|
*/
|
|
38
38
|
declare class SqliteKvStore implements KvStore {
|
|
39
39
|
#private;
|
|
40
|
-
readonly db:
|
|
40
|
+
readonly db: DatabaseSync;
|
|
41
41
|
readonly options: SqliteKvStoreOptions;
|
|
42
42
|
/**
|
|
43
43
|
* Creates a new SQLite key–value store.
|
|
44
44
|
* @param db The SQLite database to use. Supports `node:sqlite` and `bun:sqlite`.
|
|
45
45
|
* @param options The options for the key–value store.
|
|
46
46
|
*/
|
|
47
|
-
constructor(db:
|
|
47
|
+
constructor(db: DatabaseSync, options?: SqliteKvStoreOptions);
|
|
48
48
|
/**
|
|
49
49
|
* {@inheritDoc KvStore.get}
|
|
50
50
|
*/
|
package/dist/mod.cjs
CHANGED
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
const { Temporal } = require("@js-temporal/polyfill");
|
|
3
3
|
|
|
4
4
|
const require_kv = require('./kv.cjs');
|
|
5
|
+
const require_mq = require('./mq.cjs');
|
|
5
6
|
|
|
6
|
-
exports.SqliteKvStore = require_kv.SqliteKvStore;
|
|
7
|
+
exports.SqliteKvStore = require_kv.SqliteKvStore;
|
|
8
|
+
exports.SqliteMessageQueue = require_mq.SqliteMessageQueue;
|
package/dist/mod.d.cts
CHANGED
package/dist/mod.d.ts
CHANGED
package/dist/mod.js
CHANGED
package/dist/mq.cjs
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
|
|
2
|
+
const { Temporal } = require("@js-temporal/polyfill");
|
|
3
|
+
|
|
4
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
5
|
+
const __sqlite = require_rolldown_runtime.__toESM(require("#sqlite"));
|
|
6
|
+
const __logtape_logtape = require_rolldown_runtime.__toESM(require("@logtape/logtape"));
|
|
7
|
+
|
|
8
|
+
//#region src/mq.ts
|
|
9
|
+
const logger = (0, __logtape_logtape.getLogger)([
|
|
10
|
+
"fedify",
|
|
11
|
+
"sqlite",
|
|
12
|
+
"mq"
|
|
13
|
+
]);
|
|
14
|
+
var EnqueueEvent = class extends Event {
|
|
15
|
+
delayMs;
|
|
16
|
+
constructor(delayMs) {
|
|
17
|
+
super("enqueue");
|
|
18
|
+
this.delayMs = delayMs;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A message queue that uses SQLite as the underlying storage.
|
|
23
|
+
*
|
|
24
|
+
* This implementation is designed for single-node deployments and uses
|
|
25
|
+
* polling to check for new messages. It is not suitable for high-throughput
|
|
26
|
+
* scenarios or distributed environments.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts ignore
|
|
30
|
+
* import { createFederation } from "@fedify/fedify";
|
|
31
|
+
* import { SqliteMessageQueue } from "@fedify/sqlite";
|
|
32
|
+
* import { DatabaseSync } from "node:sqlite";
|
|
33
|
+
*
|
|
34
|
+
* const db = new DatabaseSync(":memory:");
|
|
35
|
+
* const federation = createFederation({
|
|
36
|
+
* // ...
|
|
37
|
+
* queue: new SqliteMessageQueue(db),
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var SqliteMessageQueue = class SqliteMessageQueue {
|
|
42
|
+
static #defaultTableName = "fedify_message";
|
|
43
|
+
static #tableNameRegex = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
44
|
+
static #notifyChannels = /* @__PURE__ */ new Map();
|
|
45
|
+
static #activeInstances = /* @__PURE__ */ new Map();
|
|
46
|
+
static #getNotifyChannel(tableName) {
|
|
47
|
+
let channel = SqliteMessageQueue.#notifyChannels.get(tableName);
|
|
48
|
+
if (channel == null) {
|
|
49
|
+
channel = new EventTarget();
|
|
50
|
+
SqliteMessageQueue.#notifyChannels.set(tableName, channel);
|
|
51
|
+
}
|
|
52
|
+
return channel;
|
|
53
|
+
}
|
|
54
|
+
#db;
|
|
55
|
+
#tableName;
|
|
56
|
+
#pollIntervalMs;
|
|
57
|
+
#instanceId;
|
|
58
|
+
#maxRetries;
|
|
59
|
+
#retryDelayMs;
|
|
60
|
+
#journalMode;
|
|
61
|
+
#initialized;
|
|
62
|
+
/**
|
|
63
|
+
* SQLite message queue does not provide native retry mechanisms.
|
|
64
|
+
*/
|
|
65
|
+
nativeRetrial = false;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new SQLite message queue.
|
|
68
|
+
* @param db The SQLite database to use. Supports `node:sqlite`, `bun:sqlite`.
|
|
69
|
+
* @param options The options for the message queue.
|
|
70
|
+
*/
|
|
71
|
+
constructor(db, options = {}) {
|
|
72
|
+
this.db = db;
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.#db = new __sqlite.SqliteDatabase(db);
|
|
75
|
+
this.#initialized = options.initialized ?? false;
|
|
76
|
+
this.#tableName = options.tableName ?? SqliteMessageQueue.#defaultTableName;
|
|
77
|
+
this.#instanceId = crypto.randomUUID();
|
|
78
|
+
this.#pollIntervalMs = Temporal.Duration.from(options.pollInterval ?? { seconds: 5 }).total("millisecond");
|
|
79
|
+
this.#maxRetries = options.maxRetries ?? 5;
|
|
80
|
+
this.#retryDelayMs = options.retryDelayMs ?? 100;
|
|
81
|
+
this.#journalMode = options.journalMode ?? "WAL";
|
|
82
|
+
if (!SqliteMessageQueue.#tableNameRegex.test(this.#tableName)) throw new Error(`Invalid table name for the message queue: ${this.#tableName}.\
|
|
83
|
+
Only letters, digits, and underscores are allowed.`);
|
|
84
|
+
this.#registerInstance();
|
|
85
|
+
}
|
|
86
|
+
#registerInstance() {
|
|
87
|
+
let instances = SqliteMessageQueue.#activeInstances.get(this.#tableName);
|
|
88
|
+
if (instances == null) {
|
|
89
|
+
instances = /* @__PURE__ */ new Set();
|
|
90
|
+
SqliteMessageQueue.#activeInstances.set(this.#tableName, instances);
|
|
91
|
+
}
|
|
92
|
+
instances.add(this.#instanceId);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* {@inheritDoc MessageQueue.enqueue}
|
|
96
|
+
*/
|
|
97
|
+
enqueue(message, options) {
|
|
98
|
+
this.initialize();
|
|
99
|
+
const id = crypto.randomUUID();
|
|
100
|
+
const encodedMessage = this.#encodeMessage(message);
|
|
101
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
102
|
+
const delay = options?.delay ?? Temporal.Duration.from({ seconds: 0 });
|
|
103
|
+
const scheduled = now + delay.total({ unit: "milliseconds" });
|
|
104
|
+
if (options?.delay) logger.debug("Enqueuing a message with a delay of {delay}...", {
|
|
105
|
+
delay,
|
|
106
|
+
message
|
|
107
|
+
});
|
|
108
|
+
else logger.debug("Enqueuing a message...", { message });
|
|
109
|
+
return this.#retryOnBusy(() => {
|
|
110
|
+
this.#db.prepare(`INSERT INTO "${this.#tableName}" (id, message, created, scheduled)
|
|
111
|
+
VALUES (?, ?, ?, ?)`).run(id, encodedMessage, now, scheduled);
|
|
112
|
+
logger.debug("Enqueued a message.", { message });
|
|
113
|
+
const delayMs = delay.total("millisecond");
|
|
114
|
+
SqliteMessageQueue.#getNotifyChannel(this.#tableName).dispatchEvent(new EnqueueEvent(delayMs));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* {@inheritDoc MessageQueue.enqueueMany}
|
|
119
|
+
*/
|
|
120
|
+
enqueueMany(messages, options) {
|
|
121
|
+
if (messages.length === 0) return Promise.resolve();
|
|
122
|
+
this.initialize();
|
|
123
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
124
|
+
const delay = options?.delay ?? Temporal.Duration.from({ seconds: 0 });
|
|
125
|
+
const scheduled = now + delay.total({ unit: "milliseconds" });
|
|
126
|
+
if (options?.delay) logger.debug("Enqueuing messages with a delay of {delay}...", {
|
|
127
|
+
delay,
|
|
128
|
+
messages
|
|
129
|
+
});
|
|
130
|
+
else logger.debug("Enqueuing messages...", { messages });
|
|
131
|
+
return this.#withTransactionRetries(() => {
|
|
132
|
+
const stmt = this.#db.prepare(`INSERT INTO "${this.#tableName}" (id, message, created, scheduled)
|
|
133
|
+
VALUES (?, ?, ?, ?)`);
|
|
134
|
+
for (const message of messages) {
|
|
135
|
+
const id = crypto.randomUUID();
|
|
136
|
+
const encodedMessage = this.#encodeMessage(message);
|
|
137
|
+
stmt.run(id, encodedMessage, now, scheduled);
|
|
138
|
+
}
|
|
139
|
+
logger.debug("Enqueued messages.", { messages });
|
|
140
|
+
const delayMs = delay.total("millisecond");
|
|
141
|
+
SqliteMessageQueue.#getNotifyChannel(this.#tableName).dispatchEvent(new EnqueueEvent(delayMs));
|
|
142
|
+
}).catch((error) => {
|
|
143
|
+
logger.error("Failed to enqueue messages to table {tableName}: {error}", {
|
|
144
|
+
tableName: this.#tableName,
|
|
145
|
+
messageCount: messages.length,
|
|
146
|
+
error
|
|
147
|
+
});
|
|
148
|
+
throw error;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* {@inheritDoc MessageQueue.listen}
|
|
153
|
+
*/
|
|
154
|
+
async listen(handler, options) {
|
|
155
|
+
this.initialize();
|
|
156
|
+
const { signal } = options ?? {};
|
|
157
|
+
logger.debug("Starting to listen for messages on table {tableName}...", { tableName: this.#tableName });
|
|
158
|
+
const channel = SqliteMessageQueue.#getNotifyChannel(this.#tableName);
|
|
159
|
+
const timeouts = /* @__PURE__ */ new Set();
|
|
160
|
+
const poll = async () => {
|
|
161
|
+
while (signal == null || !signal.aborted) {
|
|
162
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
163
|
+
const result = await this.#withTransactionRetries(() => {
|
|
164
|
+
return this.#db.prepare(`DELETE FROM "${this.#tableName}"
|
|
165
|
+
WHERE id = (
|
|
166
|
+
SELECT id FROM "${this.#tableName}"
|
|
167
|
+
WHERE scheduled <= ?
|
|
168
|
+
ORDER BY scheduled
|
|
169
|
+
LIMIT 1
|
|
170
|
+
)
|
|
171
|
+
RETURNING id, message`).get(now);
|
|
172
|
+
});
|
|
173
|
+
if (result) {
|
|
174
|
+
const message = this.#decodeMessage(result.message);
|
|
175
|
+
logger.debug("Processing message {id}...", {
|
|
176
|
+
id: result.id,
|
|
177
|
+
message
|
|
178
|
+
});
|
|
179
|
+
try {
|
|
180
|
+
await handler(message);
|
|
181
|
+
logger.debug("Processed message {id}.", { id: result.id });
|
|
182
|
+
} catch (error) {
|
|
183
|
+
logger.error("Failed to process message {id} from table {tableName}: {error}", {
|
|
184
|
+
id: result.id,
|
|
185
|
+
tableName: this.#tableName,
|
|
186
|
+
message,
|
|
187
|
+
error
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const onEnqueue = (event) => {
|
|
196
|
+
const delayMs = event.delayMs;
|
|
197
|
+
if (delayMs < 1) poll();
|
|
198
|
+
else timeouts.add(setTimeout(poll, delayMs));
|
|
199
|
+
};
|
|
200
|
+
channel.addEventListener("enqueue", onEnqueue);
|
|
201
|
+
signal?.addEventListener("abort", () => {
|
|
202
|
+
channel.removeEventListener("enqueue", onEnqueue);
|
|
203
|
+
for (const timeout of timeouts) clearTimeout(timeout);
|
|
204
|
+
});
|
|
205
|
+
await poll();
|
|
206
|
+
while (signal == null || !signal.aborted) {
|
|
207
|
+
let timeout;
|
|
208
|
+
await new Promise((resolve) => {
|
|
209
|
+
const onAbort = () => {
|
|
210
|
+
signal?.removeEventListener("abort", onAbort);
|
|
211
|
+
resolve(void 0);
|
|
212
|
+
};
|
|
213
|
+
signal?.addEventListener("abort", onAbort);
|
|
214
|
+
timeout = setTimeout(() => {
|
|
215
|
+
signal?.removeEventListener("abort", onAbort);
|
|
216
|
+
resolve(0);
|
|
217
|
+
}, this.#pollIntervalMs);
|
|
218
|
+
timeouts.add(timeout);
|
|
219
|
+
});
|
|
220
|
+
if (timeout != null) timeouts.delete(timeout);
|
|
221
|
+
await poll();
|
|
222
|
+
}
|
|
223
|
+
logger.debug("Stopped listening for messages on table {tableName}.", { tableName: this.#tableName });
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Creates the message queue table if it does not already exist.
|
|
227
|
+
* Does nothing if the table already exists.
|
|
228
|
+
*
|
|
229
|
+
* This method also configures the SQLite journal mode for better concurrency.
|
|
230
|
+
* WAL (Write-Ahead Logging) mode is enabled by default to improve
|
|
231
|
+
* concurrent access in multi-process environments.
|
|
232
|
+
*/
|
|
233
|
+
initialize() {
|
|
234
|
+
if (this.#initialized) return;
|
|
235
|
+
logger.debug("Initializing the message queue table {tableName}...", { tableName: this.#tableName });
|
|
236
|
+
this.#db.exec(`PRAGMA journal_mode=${this.#journalMode}`);
|
|
237
|
+
this.#withTransaction(() => {
|
|
238
|
+
this.#db.exec(`
|
|
239
|
+
CREATE TABLE IF NOT EXISTS "${this.#tableName}" (
|
|
240
|
+
id TEXT PRIMARY KEY,
|
|
241
|
+
message TEXT NOT NULL,
|
|
242
|
+
created INTEGER NOT NULL,
|
|
243
|
+
scheduled INTEGER NOT NULL
|
|
244
|
+
)
|
|
245
|
+
`);
|
|
246
|
+
this.#db.exec(`
|
|
247
|
+
CREATE INDEX IF NOT EXISTS "idx_${this.#tableName}_scheduled"
|
|
248
|
+
ON "${this.#tableName}" (scheduled)
|
|
249
|
+
`);
|
|
250
|
+
});
|
|
251
|
+
this.#initialized = true;
|
|
252
|
+
logger.debug("Initialized the message queue table {tableName}.", { tableName: this.#tableName });
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Drops the table used by the message queue. Does nothing if the table
|
|
256
|
+
* does not exist.
|
|
257
|
+
*/
|
|
258
|
+
drop() {
|
|
259
|
+
this.#db.exec(`DROP TABLE IF EXISTS "${this.#tableName}"`);
|
|
260
|
+
this.#initialized = false;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Closes the database connection.
|
|
264
|
+
*/
|
|
265
|
+
[Symbol.dispose]() {
|
|
266
|
+
try {
|
|
267
|
+
this.#db.close();
|
|
268
|
+
this.#unregisterInstance();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
logger.error("Failed to close the database connection for table {tableName}: {error}", {
|
|
271
|
+
tableName: this.#tableName,
|
|
272
|
+
error
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
#unregisterInstance() {
|
|
277
|
+
const instances = SqliteMessageQueue.#activeInstances.get(this.#tableName);
|
|
278
|
+
if (instances == null) return;
|
|
279
|
+
instances.delete(this.#instanceId);
|
|
280
|
+
if (instances.size === 0) {
|
|
281
|
+
SqliteMessageQueue.#activeInstances.delete(this.#tableName);
|
|
282
|
+
SqliteMessageQueue.#notifyChannels.delete(this.#tableName);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Checks if an error is a SQLITE_BUSY error or transaction conflict.
|
|
287
|
+
* Handles different error formats from node:sqlite and bun:sqlite.
|
|
288
|
+
*/
|
|
289
|
+
#isBusyError(error) {
|
|
290
|
+
if (!(error instanceof Error)) return false;
|
|
291
|
+
if (error.message.includes("SQLITE_BUSY") || error.message.includes("database is locked") || error.message.includes("transaction within a transaction")) return true;
|
|
292
|
+
const errorWithCode = error;
|
|
293
|
+
if (errorWithCode.code === "SQLITE_BUSY") return true;
|
|
294
|
+
const errorWithErrno = error;
|
|
295
|
+
if (errorWithErrno.errno === 5) return true;
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Retries a database operation with exponential backoff on SQLITE_BUSY errors.
|
|
300
|
+
*/
|
|
301
|
+
async #retryOnBusy(operation) {
|
|
302
|
+
let lastError;
|
|
303
|
+
for (let attempt = 0; attempt <= this.#maxRetries; attempt++) try {
|
|
304
|
+
return operation();
|
|
305
|
+
} catch (error) {
|
|
306
|
+
lastError = error;
|
|
307
|
+
if (!this.#isBusyError(error)) {
|
|
308
|
+
logger.error("Database operation failed on table {tableName}: {error}", {
|
|
309
|
+
tableName: this.#tableName,
|
|
310
|
+
error
|
|
311
|
+
});
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
if (attempt === this.#maxRetries) {
|
|
315
|
+
logger.error("Max retries ({maxRetries}) reached for SQLITE_BUSY error on table {tableName}.", {
|
|
316
|
+
maxRetries: this.#maxRetries,
|
|
317
|
+
tableName: this.#tableName,
|
|
318
|
+
error
|
|
319
|
+
});
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
const delayMs = this.#retryDelayMs * Math.pow(2, attempt);
|
|
323
|
+
logger.debug("SQLITE_BUSY error on table {tableName}, retrying in {delayMs}ms (attempt {attempt}/{maxRetries})...", {
|
|
324
|
+
tableName: this.#tableName,
|
|
325
|
+
delayMs,
|
|
326
|
+
attempt: attempt + 1,
|
|
327
|
+
maxRetries: this.#maxRetries
|
|
328
|
+
});
|
|
329
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
330
|
+
}
|
|
331
|
+
throw lastError;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Executes a database operation within a transaction.
|
|
335
|
+
* Automatically handles BEGIN IMMEDIATE, COMMIT, and ROLLBACK.
|
|
336
|
+
*/
|
|
337
|
+
#withTransaction(operation) {
|
|
338
|
+
let transactionStarted = false;
|
|
339
|
+
try {
|
|
340
|
+
this.#db.exec("BEGIN IMMEDIATE");
|
|
341
|
+
transactionStarted = true;
|
|
342
|
+
const result = operation();
|
|
343
|
+
this.#db.exec("COMMIT");
|
|
344
|
+
return result;
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (transactionStarted) try {
|
|
347
|
+
this.#db.exec("ROLLBACK");
|
|
348
|
+
} catch {}
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Executes a database operation within a transaction with retry logic.
|
|
354
|
+
* Automatically handles BEGIN IMMEDIATE, COMMIT, and ROLLBACK.
|
|
355
|
+
* Retries on SQLITE_BUSY errors with exponential backoff.
|
|
356
|
+
*/
|
|
357
|
+
async #withTransactionRetries(operation) {
|
|
358
|
+
return await this.#retryOnBusy(() => this.#withTransaction(operation));
|
|
359
|
+
}
|
|
360
|
+
#encodeMessage(message) {
|
|
361
|
+
return JSON.stringify(message);
|
|
362
|
+
}
|
|
363
|
+
#decodeMessage(message) {
|
|
364
|
+
return JSON.parse(message);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
//#endregion
|
|
369
|
+
exports.SqliteMessageQueue = SqliteMessageQueue;
|
package/dist/mq.d.cts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { DatabaseSync } from "./dist/sqlite.node.cjs";
|
|
2
|
+
import { MessageQueue, MessageQueueEnqueueOptions, MessageQueueListenOptions } from "@fedify/fedify";
|
|
3
|
+
|
|
4
|
+
//#region src/mq.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options for the SQLite message queue.
|
|
7
|
+
*/
|
|
8
|
+
interface SqliteMessageQueueOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The table name to use for the message queue.
|
|
11
|
+
* Only letters, digits, and underscores are allowed.
|
|
12
|
+
* `"fedify_message"` by default.
|
|
13
|
+
* @default `"fedify_message"`
|
|
14
|
+
*/
|
|
15
|
+
tableName?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the table has been initialized. `false` by default.
|
|
18
|
+
* @default `false`
|
|
19
|
+
*/
|
|
20
|
+
initialized?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The poll interval for the message queue.
|
|
23
|
+
* @default `{ seconds: 5 }`
|
|
24
|
+
*/
|
|
25
|
+
pollInterval?: Temporal.Duration | Temporal.DurationLike;
|
|
26
|
+
/**
|
|
27
|
+
* Maximum number of retries for SQLITE_BUSY errors.
|
|
28
|
+
* @default `5`
|
|
29
|
+
*/
|
|
30
|
+
maxRetries?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Initial retry delay in milliseconds for SQLITE_BUSY errors.
|
|
33
|
+
* Uses exponential backoff.
|
|
34
|
+
* @default `100`
|
|
35
|
+
*/
|
|
36
|
+
retryDelayMs?: number;
|
|
37
|
+
/**
|
|
38
|
+
* SQLite journal mode to use.
|
|
39
|
+
* WAL (Write-Ahead Logging) mode is recommended for better concurrency
|
|
40
|
+
* in multi-process environments.
|
|
41
|
+
* Note: WAL mode is persistent per database file, not per connection.
|
|
42
|
+
* @default `"WAL"`
|
|
43
|
+
*/
|
|
44
|
+
journalMode?: "WAL" | "DELETE" | "TRUNCATE" | "PERSIST" | "MEMORY";
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A message queue that uses SQLite as the underlying storage.
|
|
48
|
+
*
|
|
49
|
+
* This implementation is designed for single-node deployments and uses
|
|
50
|
+
* polling to check for new messages. It is not suitable for high-throughput
|
|
51
|
+
* scenarios or distributed environments.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts ignore
|
|
55
|
+
* import { createFederation } from "@fedify/fedify";
|
|
56
|
+
* import { SqliteMessageQueue } from "@fedify/sqlite";
|
|
57
|
+
* import { DatabaseSync } from "node:sqlite";
|
|
58
|
+
*
|
|
59
|
+
* const db = new DatabaseSync(":memory:");
|
|
60
|
+
* const federation = createFederation({
|
|
61
|
+
* // ...
|
|
62
|
+
* queue: new SqliteMessageQueue(db),
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare class SqliteMessageQueue implements MessageQueue, Disposable {
|
|
67
|
+
#private;
|
|
68
|
+
readonly db: DatabaseSync;
|
|
69
|
+
readonly options: SqliteMessageQueueOptions;
|
|
70
|
+
/**
|
|
71
|
+
* SQLite message queue does not provide native retry mechanisms.
|
|
72
|
+
*/
|
|
73
|
+
readonly nativeRetrial = false;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new SQLite message queue.
|
|
76
|
+
* @param db The SQLite database to use. Supports `node:sqlite`, `bun:sqlite`.
|
|
77
|
+
* @param options The options for the message queue.
|
|
78
|
+
*/
|
|
79
|
+
constructor(db: DatabaseSync, options?: SqliteMessageQueueOptions);
|
|
80
|
+
/**
|
|
81
|
+
* {@inheritDoc MessageQueue.enqueue}
|
|
82
|
+
*/
|
|
83
|
+
enqueue(message: any, options?: MessageQueueEnqueueOptions): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* {@inheritDoc MessageQueue.enqueueMany}
|
|
86
|
+
*/
|
|
87
|
+
enqueueMany(messages: readonly any[], options?: MessageQueueEnqueueOptions): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* {@inheritDoc MessageQueue.listen}
|
|
90
|
+
*/
|
|
91
|
+
listen(handler: (message: any) => Promise<void> | void, options?: MessageQueueListenOptions): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Creates the message queue table if it does not already exist.
|
|
94
|
+
* Does nothing if the table already exists.
|
|
95
|
+
*
|
|
96
|
+
* This method also configures the SQLite journal mode for better concurrency.
|
|
97
|
+
* WAL (Write-Ahead Logging) mode is enabled by default to improve
|
|
98
|
+
* concurrent access in multi-process environments.
|
|
99
|
+
*/
|
|
100
|
+
initialize(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Drops the table used by the message queue. Does nothing if the table
|
|
103
|
+
* does not exist.
|
|
104
|
+
*/
|
|
105
|
+
drop(): void;
|
|
106
|
+
/**
|
|
107
|
+
* Closes the database connection.
|
|
108
|
+
*/
|
|
109
|
+
[Symbol.dispose](): void;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { SqliteMessageQueue, SqliteMessageQueueOptions };
|
package/dist/mq.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { PlatformDatabase } from "#sqlite";
|
|
3
|
+
import { MessageQueue, MessageQueueEnqueueOptions, MessageQueueListenOptions } from "@fedify/fedify";
|
|
4
|
+
|
|
5
|
+
//#region src/mq.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Options for the SQLite message queue.
|
|
8
|
+
*/
|
|
9
|
+
interface SqliteMessageQueueOptions {
|
|
10
|
+
/**
|
|
11
|
+
* The table name to use for the message queue.
|
|
12
|
+
* Only letters, digits, and underscores are allowed.
|
|
13
|
+
* `"fedify_message"` by default.
|
|
14
|
+
* @default `"fedify_message"`
|
|
15
|
+
*/
|
|
16
|
+
tableName?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the table has been initialized. `false` by default.
|
|
19
|
+
* @default `false`
|
|
20
|
+
*/
|
|
21
|
+
initialized?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The poll interval for the message queue.
|
|
24
|
+
* @default `{ seconds: 5 }`
|
|
25
|
+
*/
|
|
26
|
+
pollInterval?: Temporal.Duration | Temporal.DurationLike;
|
|
27
|
+
/**
|
|
28
|
+
* Maximum number of retries for SQLITE_BUSY errors.
|
|
29
|
+
* @default `5`
|
|
30
|
+
*/
|
|
31
|
+
maxRetries?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Initial retry delay in milliseconds for SQLITE_BUSY errors.
|
|
34
|
+
* Uses exponential backoff.
|
|
35
|
+
* @default `100`
|
|
36
|
+
*/
|
|
37
|
+
retryDelayMs?: number;
|
|
38
|
+
/**
|
|
39
|
+
* SQLite journal mode to use.
|
|
40
|
+
* WAL (Write-Ahead Logging) mode is recommended for better concurrency
|
|
41
|
+
* in multi-process environments.
|
|
42
|
+
* Note: WAL mode is persistent per database file, not per connection.
|
|
43
|
+
* @default `"WAL"`
|
|
44
|
+
*/
|
|
45
|
+
journalMode?: "WAL" | "DELETE" | "TRUNCATE" | "PERSIST" | "MEMORY";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A message queue that uses SQLite as the underlying storage.
|
|
49
|
+
*
|
|
50
|
+
* This implementation is designed for single-node deployments and uses
|
|
51
|
+
* polling to check for new messages. It is not suitable for high-throughput
|
|
52
|
+
* scenarios or distributed environments.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts ignore
|
|
56
|
+
* import { createFederation } from "@fedify/fedify";
|
|
57
|
+
* import { SqliteMessageQueue } from "@fedify/sqlite";
|
|
58
|
+
* import { DatabaseSync } from "node:sqlite";
|
|
59
|
+
*
|
|
60
|
+
* const db = new DatabaseSync(":memory:");
|
|
61
|
+
* const federation = createFederation({
|
|
62
|
+
* // ...
|
|
63
|
+
* queue: new SqliteMessageQueue(db),
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare class SqliteMessageQueue implements MessageQueue, Disposable {
|
|
68
|
+
#private;
|
|
69
|
+
readonly db: PlatformDatabase;
|
|
70
|
+
readonly options: SqliteMessageQueueOptions;
|
|
71
|
+
/**
|
|
72
|
+
* SQLite message queue does not provide native retry mechanisms.
|
|
73
|
+
*/
|
|
74
|
+
readonly nativeRetrial = false;
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new SQLite message queue.
|
|
77
|
+
* @param db The SQLite database to use. Supports `node:sqlite`, `bun:sqlite`.
|
|
78
|
+
* @param options The options for the message queue.
|
|
79
|
+
*/
|
|
80
|
+
constructor(db: PlatformDatabase, options?: SqliteMessageQueueOptions);
|
|
81
|
+
/**
|
|
82
|
+
* {@inheritDoc MessageQueue.enqueue}
|
|
83
|
+
*/
|
|
84
|
+
enqueue(message: any, options?: MessageQueueEnqueueOptions): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* {@inheritDoc MessageQueue.enqueueMany}
|
|
87
|
+
*/
|
|
88
|
+
enqueueMany(messages: readonly any[], options?: MessageQueueEnqueueOptions): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* {@inheritDoc MessageQueue.listen}
|
|
91
|
+
*/
|
|
92
|
+
listen(handler: (message: any) => Promise<void> | void, options?: MessageQueueListenOptions): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Creates the message queue table if it does not already exist.
|
|
95
|
+
* Does nothing if the table already exists.
|
|
96
|
+
*
|
|
97
|
+
* This method also configures the SQLite journal mode for better concurrency.
|
|
98
|
+
* WAL (Write-Ahead Logging) mode is enabled by default to improve
|
|
99
|
+
* concurrent access in multi-process environments.
|
|
100
|
+
*/
|
|
101
|
+
initialize(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Drops the table used by the message queue. Does nothing if the table
|
|
104
|
+
* does not exist.
|
|
105
|
+
*/
|
|
106
|
+
drop(): void;
|
|
107
|
+
/**
|
|
108
|
+
* Closes the database connection.
|
|
109
|
+
*/
|
|
110
|
+
[Symbol.dispose](): void;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
export { SqliteMessageQueue, SqliteMessageQueueOptions };
|