tina4-nodejs 3.13.31 → 3.13.33
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/CLAUDE.md +1 -1
- package/package.json +1 -1
- package/packages/core/src/cache.ts +4 -0
- package/packages/core/src/index.ts +3 -2
- package/packages/core/src/job.ts +6 -1
- package/packages/core/src/queue.ts +20 -4
- package/packages/core/src/queueBackends/kafkaBackend.ts +21 -2
- package/packages/core/src/queueBackends/liteBackend.ts +186 -101
- package/packages/core/src/queueBackends/mongoBackend.ts +18 -4
- package/packages/core/src/queueBackends/rabbitmqBackend.ts +79 -10
- package/packages/core/src/router.ts +89 -37
- package/packages/core/src/types.ts +16 -1
- package/packages/orm/src/cachedDatabase.ts +15 -8
- package/packages/orm/src/database.ts +26 -11
package/CLAUDE.md
CHANGED
package/package.json
CHANGED
|
@@ -1269,6 +1269,9 @@ export function responseCache(config?: ResponseCacheConfig): Middleware {
|
|
|
1269
1269
|
if (cached && typeof cached === "object" && typeof cached.body === "string") {
|
|
1270
1270
|
// Cache HIT — serve from the (possibly distributed) backend.
|
|
1271
1271
|
res.header("X-Cache", "HIT");
|
|
1272
|
+
// X-Cache-TTL advertises the configured cache lifetime in seconds
|
|
1273
|
+
// (parity with Python/PHP/Ruby, which set it alongside X-Cache).
|
|
1274
|
+
res.header("X-Cache-TTL", String(ttl));
|
|
1272
1275
|
res.header("Content-Type", cached.contentType);
|
|
1273
1276
|
res(cached.body, cached.statusCode, cached.contentType);
|
|
1274
1277
|
return;
|
|
@@ -1297,6 +1300,7 @@ export function responseCache(config?: ResponseCacheConfig): Middleware {
|
|
|
1297
1300
|
}
|
|
1298
1301
|
|
|
1299
1302
|
res.header("X-Cache", "MISS");
|
|
1303
|
+
res.header("X-Cache-TTL", String(ttl));
|
|
1300
1304
|
return originalEnd(chunk, ...args);
|
|
1301
1305
|
} as any;
|
|
1302
1306
|
|
|
@@ -6,6 +6,7 @@ export type {
|
|
|
6
6
|
RouteMeta,
|
|
7
7
|
Tina4Config,
|
|
8
8
|
Middleware,
|
|
9
|
+
MiddlewareSpec,
|
|
9
10
|
UploadedFile,
|
|
10
11
|
CookieOptions,
|
|
11
12
|
WebSocketRouteHandler,
|
|
@@ -14,7 +15,7 @@ export type {
|
|
|
14
15
|
|
|
15
16
|
export { startServer, resolvePortAndHost, handle, start, stop, httpReason, resolveTemplate, resetTemplateCache, templateAutoRoutingEnabled, isBannerSuppressed } from "./server.js";
|
|
16
17
|
export { background, stopAllBackgroundTasks, backgroundTaskCount } from "./background.js";
|
|
17
|
-
export { Router, RouteGroup, RouteRef, defaultRouter, runRouteMiddlewares, isTrailingSlashRedirectEnabled } from "./router.js";
|
|
18
|
+
export { Router, RouteGroup, RouteRef, defaultRouter, runRouteMiddlewares, resolveStringMiddleware, isTrailingSlashRedirectEnabled } from "./router.js";
|
|
18
19
|
export { get, post, put, patch, del, any, websocket, del as delete } from "./router.js";
|
|
19
20
|
export type { RouteInfo } from "./router.js";
|
|
20
21
|
export { discoverRoutes } from "./routeDiscovery.js";
|
|
@@ -95,7 +96,7 @@ export { AI_TOOLS, isInstalled, showMenu, installSelected, installAll, generateC
|
|
|
95
96
|
export type { AiTool } from "./ai.js";
|
|
96
97
|
export type { ImapMessage, ImapFullMessage } from "./messenger.js";
|
|
97
98
|
export { LiteBackend } from "./queueBackends/liteBackend.js";
|
|
98
|
-
export { RabbitMQBackend } from "./queueBackends/rabbitmqBackend.js";
|
|
99
|
+
export { RabbitMQBackend, parseAmqpUrl } from "./queueBackends/rabbitmqBackend.js";
|
|
99
100
|
export type { RabbitMQConfig } from "./queueBackends/rabbitmqBackend.js";
|
|
100
101
|
export { KafkaBackend } from "./queueBackends/kafkaBackend.js";
|
|
101
102
|
export type { KafkaConfig } from "./queueBackends/kafkaBackend.js";
|
package/packages/core/src/job.ts
CHANGED
|
@@ -48,12 +48,17 @@ export function createJob(data: JobData, queue: JobQueueBridge): QueueJob {
|
|
|
48
48
|
const job: QueueJob = {
|
|
49
49
|
...data,
|
|
50
50
|
complete() {
|
|
51
|
+
// Terminal — the job was already removed from the queue on pop().
|
|
51
52
|
job.status = "completed";
|
|
52
53
|
},
|
|
53
54
|
fail(reason = "") {
|
|
55
|
+
// Record a failed attempt. `attempts` is incremented exactly once, inside
|
|
56
|
+
// the backend's failJob() — NOT here — so a persistently-failing job runs
|
|
57
|
+
// exactly maxRetries times before it is dead-lettered. The backend decides
|
|
58
|
+
// whether to re-enqueue (attempts < maxRetries) or dead-letter
|
|
59
|
+
// (attempts >= maxRetries).
|
|
54
60
|
job.status = "failed";
|
|
55
61
|
job.error = reason;
|
|
56
|
-
job.attempts = (job.attempts || 0) + 1;
|
|
57
62
|
queue._failJob(job.topic, job, reason, queue.getMaxRetries());
|
|
58
63
|
},
|
|
59
64
|
reject(reason = "") {
|
|
@@ -45,6 +45,12 @@ export interface QueueConfig {
|
|
|
45
45
|
path?: string;
|
|
46
46
|
topic?: string;
|
|
47
47
|
maxRetries?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Seconds to delay a failed job's automatic re-enqueue. 0 (the default)
|
|
50
|
+
* means retry immediately — the next pop()/consume() iteration picks it up
|
|
51
|
+
* straight away. Parity with Python's retry_backoff.
|
|
52
|
+
*/
|
|
53
|
+
retryBackoff?: number;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
export interface ProcessOptions {
|
|
@@ -75,6 +81,7 @@ export class Queue {
|
|
|
75
81
|
private basePath: string;
|
|
76
82
|
private topic: string;
|
|
77
83
|
private _maxRetries: number;
|
|
84
|
+
private _retryBackoff: number;
|
|
78
85
|
private externalBackend: QueueBackendInterface | null = null;
|
|
79
86
|
private liteBackend!: LiteBackend;
|
|
80
87
|
|
|
@@ -104,6 +111,7 @@ export class Queue {
|
|
|
104
111
|
?? "data/queue";
|
|
105
112
|
this.topic = resolvedConfig.topic ?? "default";
|
|
106
113
|
this._maxRetries = resolvedConfig.maxRetries ?? 3;
|
|
114
|
+
this._retryBackoff = resolvedConfig.retryBackoff ?? 0;
|
|
107
115
|
this.liteBackend = new LiteBackend(this.basePath);
|
|
108
116
|
|
|
109
117
|
// Initialize external backends
|
|
@@ -234,10 +242,12 @@ export class Queue {
|
|
|
234
242
|
}
|
|
235
243
|
|
|
236
244
|
/**
|
|
237
|
-
* Get
|
|
245
|
+
* Get jobs that failed at least once but are still being retried
|
|
246
|
+
* (0 < attempts < maxRetries). These live in the pending queue under the
|
|
247
|
+
* auto-retry lifecycle; dead-lettered jobs are returned by deadLetters().
|
|
238
248
|
*/
|
|
239
249
|
failed(): QueueJob[] {
|
|
240
|
-
return this.liteBackend.failed(this.topic);
|
|
250
|
+
return this.liteBackend.failed(this.topic, this._maxRetries);
|
|
241
251
|
}
|
|
242
252
|
|
|
243
253
|
/**
|
|
@@ -396,11 +406,17 @@ export class Queue {
|
|
|
396
406
|
return this._maxRetries;
|
|
397
407
|
}
|
|
398
408
|
|
|
409
|
+
getRetryBackoff(): number {
|
|
410
|
+
return this._retryBackoff;
|
|
411
|
+
}
|
|
412
|
+
|
|
399
413
|
/**
|
|
400
|
-
*
|
|
414
|
+
* Record a failed attempt for a job. The backend increments `attempts`
|
|
415
|
+
* exactly once and decides whether to re-enqueue (attempts < maxRetries,
|
|
416
|
+
* after retryBackoff seconds) or dead-letter (attempts >= maxRetries).
|
|
401
417
|
*/
|
|
402
418
|
_failJob(queue: string, job: QueueJob, error: string, maxRetries: number): void {
|
|
403
|
-
this.liteBackend.failJob(queue, job, error, maxRetries);
|
|
419
|
+
this.liteBackend.failJob(queue, job, error, maxRetries, this._retryBackoff);
|
|
404
420
|
}
|
|
405
421
|
|
|
406
422
|
/**
|
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
* for message storage and delivery.
|
|
6
6
|
*
|
|
7
7
|
* Configure via environment variables:
|
|
8
|
-
*
|
|
8
|
+
* TINA4_QUEUE_URL — broker list (strips a leading kafka:// if present)
|
|
9
|
+
* TINA4_KAFKA_BROKERS (override; default: "localhost:9092")
|
|
9
10
|
* TINA4_KAFKA_GROUP_ID (default: "tina4_consumer_group")
|
|
11
|
+
*
|
|
12
|
+
* Precedence for brokers: specific TINA4_KAFKA_BROKERS var (if set)
|
|
13
|
+
* > value derived from TINA4_QUEUE_URL > existing default.
|
|
10
14
|
*/
|
|
11
15
|
import net from "node:net";
|
|
12
16
|
import { execFileSync } from "node:child_process";
|
|
@@ -54,10 +58,25 @@ export class KafkaBackend implements QueueBackend {
|
|
|
54
58
|
private groupId: string;
|
|
55
59
|
|
|
56
60
|
constructor(config?: KafkaConfig) {
|
|
57
|
-
|
|
61
|
+
// Base layer: brokers derived from TINA4_QUEUE_URL (strip a leading
|
|
62
|
+
// kafka:// scheme if present; otherwise use the value as-is, e.g.
|
|
63
|
+
// "localhost:9092").
|
|
64
|
+
const url = process.env.TINA4_QUEUE_URL;
|
|
65
|
+
const fromUrl = url ? url.replace(/^kafka:\/\//, "") : undefined;
|
|
66
|
+
|
|
67
|
+
// Precedence: explicit config arg > specific TINA4_KAFKA_BROKERS var
|
|
68
|
+
// > value from TINA4_QUEUE_URL > existing default.
|
|
69
|
+
this.brokers = config?.brokers ?? process.env.TINA4_KAFKA_BROKERS ?? fromUrl ?? "localhost:9092";
|
|
58
70
|
this.groupId = config?.groupId ?? process.env.TINA4_KAFKA_GROUP_ID ?? "tina4_consumer_group";
|
|
59
71
|
}
|
|
60
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Resolved connection config — exposed for testing/introspection.
|
|
75
|
+
*/
|
|
76
|
+
getConfig(): Required<KafkaConfig> {
|
|
77
|
+
return { brokers: this.brokers, groupId: this.groupId };
|
|
78
|
+
}
|
|
79
|
+
|
|
61
80
|
/**
|
|
62
81
|
* Parse broker string into host:port.
|
|
63
82
|
*/
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LiteBackend — file-based queue backend for Tina4 Queue.
|
|
3
3
|
* Stores jobs as JSON files on disk. Zero dependencies.
|
|
4
|
+
*
|
|
5
|
+
* Dequeue policy (parity with Python master): the highest-priority AVAILABLE
|
|
6
|
+
* job is returned first; ties are broken oldest-first by createdAt. The file
|
|
7
|
+
* *name* is no longer the ordering key — the stored `priority` and `createdAt`
|
|
8
|
+
* fields are. Delayed jobs (delayUntil in the future) are skipped until due.
|
|
9
|
+
*
|
|
10
|
+
* Failure lifecycle (parity with Python master): job.fail() records one failed
|
|
11
|
+
* attempt — `attempts` is incremented exactly once, here in failJob(). If the
|
|
12
|
+
* job still has retries left (attempts < maxRetries) it is automatically
|
|
13
|
+
* re-enqueued to the pending queue (immediately, or after retryBackoff seconds
|
|
14
|
+
* if configured) so the next pop()/consume() picks it up again. Once it has
|
|
15
|
+
* been attempted maxRetries times (attempts >= maxRetries) it is moved to the
|
|
16
|
+
* dead-letter (failed/) directory, where deadLetters() returns it.
|
|
4
17
|
*/
|
|
5
18
|
import { mkdirSync, readdirSync, readFileSync, writeFileSync, unlinkSync, existsSync } from "node:fs";
|
|
6
19
|
import { join } from "node:path";
|
|
@@ -28,11 +41,15 @@ export class LiteBackend {
|
|
|
28
41
|
return dir;
|
|
29
42
|
}
|
|
30
43
|
|
|
44
|
+
private nextPrefix(): string {
|
|
45
|
+
this.seq++;
|
|
46
|
+
return `${Date.now()}-${String(this.seq).padStart(6, "0")}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
31
49
|
push(queue: string, payload: unknown, delay?: number, priority?: number): string {
|
|
32
50
|
const dir = this.ensureDir(queue);
|
|
33
51
|
const id = randomUUID();
|
|
34
52
|
const now = new Date().toISOString();
|
|
35
|
-
this.seq++;
|
|
36
53
|
|
|
37
54
|
const job = {
|
|
38
55
|
id,
|
|
@@ -42,48 +59,74 @@ export class LiteBackend {
|
|
|
42
59
|
attempts: 0,
|
|
43
60
|
delayUntil: delay ? new Date(Date.now() + delay * 1000).toISOString() : null,
|
|
44
61
|
priority: priority ?? 0,
|
|
62
|
+
topic: queue,
|
|
63
|
+
error: undefined as string | undefined,
|
|
45
64
|
};
|
|
46
65
|
|
|
47
|
-
const prefix =
|
|
66
|
+
const prefix = this.nextPrefix();
|
|
48
67
|
writeFileSync(join(dir, `${prefix}_${id}.queue-data`), JSON.stringify(job, null, 2));
|
|
49
68
|
return id;
|
|
50
69
|
}
|
|
51
70
|
|
|
52
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Return [filename, jobData] for every pending, non-delayed job, ordered by
|
|
73
|
+
* the dequeue policy: highest priority first, ties broken oldest-first by
|
|
74
|
+
* createdAt. createdAt is an ISO-8601 string, so lexicographic comparison ==
|
|
75
|
+
* chronological order.
|
|
76
|
+
*/
|
|
77
|
+
private availableCandidates(queue: string, now: string): Array<[string, any]> {
|
|
53
78
|
const dir = this.ensureDir(queue);
|
|
54
79
|
|
|
55
|
-
let
|
|
80
|
+
let filenames: string[];
|
|
56
81
|
try {
|
|
57
|
-
|
|
82
|
+
filenames = readdirSync(dir).filter(f => f.endsWith(".queue-data"));
|
|
58
83
|
} catch {
|
|
59
|
-
return
|
|
84
|
+
return [];
|
|
60
85
|
}
|
|
61
86
|
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
let job: QueueJob;
|
|
87
|
+
const candidates: Array<[string, any]> = [];
|
|
88
|
+
for (const filename of filenames) {
|
|
89
|
+
const filePath = join(dir, filename);
|
|
90
|
+
let job: any;
|
|
67
91
|
try {
|
|
68
92
|
job = JSON.parse(readFileSync(filePath, "utf-8"));
|
|
69
93
|
} catch {
|
|
70
94
|
continue;
|
|
71
95
|
}
|
|
72
|
-
|
|
73
96
|
if (job.status !== "pending") continue;
|
|
74
|
-
if (job.delayUntil && job.delayUntil > now) continue;
|
|
97
|
+
if (job.delayUntil && job.delayUntil > now) continue; // still delayed
|
|
98
|
+
candidates.push([filename, job]);
|
|
99
|
+
}
|
|
75
100
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
101
|
+
// priority DESC, then createdAt ASC (oldest first).
|
|
102
|
+
candidates.sort((a, b) => {
|
|
103
|
+
const pa = Number(a[1].priority ?? 0) || 0;
|
|
104
|
+
const pb = Number(b[1].priority ?? 0) || 0;
|
|
105
|
+
if (pb !== pa) return pb - pa;
|
|
106
|
+
const ca = (a[1].createdAt ?? "") as string;
|
|
107
|
+
const cb = (b[1].createdAt ?? "") as string;
|
|
108
|
+
return ca < cb ? -1 : ca > cb ? 1 : 0;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return candidates;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
pop(queue: string, bridge: JobQueueBridge): QueueJob | null {
|
|
115
|
+
const dir = this.ensureDir(queue);
|
|
116
|
+
const now = new Date().toISOString();
|
|
80
117
|
|
|
118
|
+
for (const [filename, job] of this.availableCandidates(queue, now)) {
|
|
119
|
+
const filePath = join(dir, filename);
|
|
120
|
+
// Claim the job by deleting the file.
|
|
81
121
|
try {
|
|
82
122
|
unlinkSync(filePath);
|
|
83
123
|
} catch {
|
|
84
|
-
// Already consumed by another worker
|
|
124
|
+
continue; // Already consumed by another worker
|
|
85
125
|
}
|
|
86
126
|
|
|
127
|
+
job.status = "reserved";
|
|
128
|
+
job.topic = queue;
|
|
129
|
+
job.priority = job.priority ?? 0;
|
|
87
130
|
return createJob(job as any, bridge);
|
|
88
131
|
}
|
|
89
132
|
|
|
@@ -92,71 +135,52 @@ export class LiteBackend {
|
|
|
92
135
|
|
|
93
136
|
popBatch(queue: string, bridge: JobQueueBridge, count: number): QueueJob[] {
|
|
94
137
|
const dir = this.ensureDir(queue);
|
|
95
|
-
|
|
96
|
-
let files: string[];
|
|
97
|
-
try {
|
|
98
|
-
files = readdirSync(dir).filter(f => f.endsWith(".queue-data")).sort();
|
|
99
|
-
} catch {
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
138
|
const now = new Date().toISOString();
|
|
104
139
|
const results: QueueJob[] = [];
|
|
105
140
|
|
|
106
|
-
for (const
|
|
141
|
+
for (const [filename, job] of this.availableCandidates(queue, now)) {
|
|
107
142
|
if (results.length >= count) break;
|
|
108
|
-
const filePath = join(dir,
|
|
109
|
-
let job: QueueJob;
|
|
143
|
+
const filePath = join(dir, filename);
|
|
110
144
|
try {
|
|
111
|
-
|
|
145
|
+
unlinkSync(filePath);
|
|
112
146
|
} catch {
|
|
113
|
-
continue;
|
|
147
|
+
continue; // Already consumed by another worker
|
|
114
148
|
}
|
|
115
149
|
|
|
116
|
-
if (job.status !== "pending") continue;
|
|
117
|
-
if (job.delayUntil && job.delayUntil > now) continue;
|
|
118
|
-
|
|
119
150
|
job.status = "reserved";
|
|
120
151
|
job.topic = queue;
|
|
121
152
|
job.priority = job.priority ?? 0;
|
|
122
|
-
writeFileSync(filePath, JSON.stringify(job, null, 2));
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
unlinkSync(filePath);
|
|
126
|
-
} catch {
|
|
127
|
-
// Already consumed by another worker
|
|
128
|
-
}
|
|
129
|
-
|
|
130
153
|
results.push(createJob(job as any, bridge));
|
|
131
154
|
}
|
|
132
155
|
|
|
133
156
|
return results;
|
|
134
157
|
}
|
|
135
158
|
|
|
159
|
+
// Status aliases that live in the failed/ directory (dead-lettered jobs)
|
|
160
|
+
// rather than as pending files in the queue directory.
|
|
161
|
+
private static readonly DEAD_STATES = ["failed", "dead", "dead_letter"];
|
|
162
|
+
|
|
136
163
|
size(queue: string, status: string = "pending"): number {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
let files: string[];
|
|
140
|
-
try {
|
|
141
|
-
files = readdirSync(failedDir).filter(f => f.endsWith(".queue-data"));
|
|
142
|
-
} catch {
|
|
143
|
-
return 0;
|
|
144
|
-
}
|
|
145
|
-
return files.length;
|
|
146
|
-
}
|
|
164
|
+
const isDead = LiteBackend.DEAD_STATES.includes(status);
|
|
165
|
+
const scanDir = isDead ? this.ensureFailedDir(queue) : this.ensureDir(queue);
|
|
147
166
|
|
|
148
|
-
const dir = this.ensureDir(queue);
|
|
149
167
|
let files: string[];
|
|
150
168
|
try {
|
|
151
|
-
files = readdirSync(
|
|
169
|
+
files = readdirSync(scanDir).filter(f => f.endsWith(".queue-data"));
|
|
152
170
|
} catch {
|
|
153
171
|
return 0;
|
|
154
172
|
}
|
|
155
173
|
|
|
174
|
+
if (isDead) {
|
|
175
|
+
// Every file in failed/ is a dead-letter; count them all regardless of
|
|
176
|
+
// the exact stored status string.
|
|
177
|
+
return files.length;
|
|
178
|
+
}
|
|
179
|
+
|
|
156
180
|
let count = 0;
|
|
157
181
|
for (const file of files) {
|
|
158
182
|
try {
|
|
159
|
-
const job = JSON.parse(readFileSync(join(
|
|
183
|
+
const job = JSON.parse(readFileSync(join(scanDir, file), "utf-8"));
|
|
160
184
|
if (job.status === status) count++;
|
|
161
185
|
} catch {
|
|
162
186
|
// skip corrupt files
|
|
@@ -178,7 +202,7 @@ export class LiteBackend {
|
|
|
178
202
|
// directory might not exist
|
|
179
203
|
}
|
|
180
204
|
|
|
181
|
-
// Also clear
|
|
205
|
+
// Also clear dead-letter jobs.
|
|
182
206
|
const failedDir = join(dir, "failed");
|
|
183
207
|
try {
|
|
184
208
|
if (existsSync(failedDir)) {
|
|
@@ -194,16 +218,27 @@ export class LiteBackend {
|
|
|
194
218
|
return count;
|
|
195
219
|
}
|
|
196
220
|
|
|
197
|
-
|
|
198
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Jobs that have failed at least once but are still being retried.
|
|
223
|
+
*
|
|
224
|
+
* Under the auto-retry lifecycle a failed-but-retryable job lives in the
|
|
225
|
+
* pending queue (not the dead-letter dir), so this scans the queue dir for
|
|
226
|
+
* pending jobs with attempts > 0 that have not yet exhausted their retries.
|
|
227
|
+
* Dead-lettered jobs are returned by deadLetters().
|
|
228
|
+
*/
|
|
229
|
+
failed(queue: string, maxRetries: number = 3): QueueJob[] {
|
|
230
|
+
const dir = this.ensureDir(queue);
|
|
199
231
|
const results: QueueJob[] = [];
|
|
200
232
|
|
|
201
233
|
try {
|
|
202
|
-
const files = readdirSync(
|
|
234
|
+
const files = readdirSync(dir).filter(f => f.endsWith(".queue-data")).sort();
|
|
203
235
|
for (const file of files) {
|
|
204
236
|
try {
|
|
205
|
-
const job: QueueJob = JSON.parse(readFileSync(join(
|
|
206
|
-
|
|
237
|
+
const job: QueueJob = JSON.parse(readFileSync(join(dir, file), "utf-8"));
|
|
238
|
+
const attempts = job.attempts || 0;
|
|
239
|
+
if (attempts > 0 && attempts < maxRetries) {
|
|
240
|
+
results.push(job);
|
|
241
|
+
}
|
|
207
242
|
} catch {
|
|
208
243
|
// skip corrupt files
|
|
209
244
|
}
|
|
@@ -215,6 +250,13 @@ export class LiteBackend {
|
|
|
215
250
|
return results;
|
|
216
251
|
}
|
|
217
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Revive a specific dead-letter job by id back to the pending queue.
|
|
255
|
+
*
|
|
256
|
+
* Manual override (Queue.retry(jobId) / job.retry()) — always revives a
|
|
257
|
+
* dead-letter regardless of attempt count. Returns false only if no
|
|
258
|
+
* dead-letter with that id exists.
|
|
259
|
+
*/
|
|
218
260
|
retry(queue: string, jobId: string, delaySeconds?: number): boolean {
|
|
219
261
|
try {
|
|
220
262
|
const queues = readdirSync(this.basePath);
|
|
@@ -227,10 +269,10 @@ export class LiteBackend {
|
|
|
227
269
|
job.status = "pending";
|
|
228
270
|
job.attempts = (job.attempts || 0) + 1;
|
|
229
271
|
job.error = undefined;
|
|
272
|
+
job.createdAt = new Date().toISOString();
|
|
230
273
|
job.delayUntil = delaySeconds ? new Date(Date.now() + delaySeconds * 1000).toISOString() : null;
|
|
231
274
|
|
|
232
|
-
this.
|
|
233
|
-
const prefix = `${Date.now()}-${String(this.seq).padStart(6, "0")}`;
|
|
275
|
+
const prefix = this.nextPrefix();
|
|
234
276
|
const queueDir = join(this.basePath, q);
|
|
235
277
|
writeFileSync(join(queueDir, `${prefix}_${jobId}.queue-data`), JSON.stringify(job, null, 2));
|
|
236
278
|
unlinkSync(filePath);
|
|
@@ -270,38 +312,19 @@ export class LiteBackend {
|
|
|
270
312
|
|
|
271
313
|
purge(queue: string, status: string, maxRetries: number = 3): number {
|
|
272
314
|
let count = 0;
|
|
315
|
+
const isDead = LiteBackend.DEAD_STATES.includes(status);
|
|
273
316
|
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
try {
|
|
277
|
-
const files = readdirSync(failedDir).filter(f => f.endsWith(".queue-data"));
|
|
278
|
-
for (const file of files) {
|
|
279
|
-
try {
|
|
280
|
-
const job: QueueJob = JSON.parse(readFileSync(join(failedDir, file), "utf-8"));
|
|
281
|
-
if ((job.attempts || 0) >= maxRetries) {
|
|
282
|
-
unlinkSync(join(failedDir, file));
|
|
283
|
-
count++;
|
|
284
|
-
}
|
|
285
|
-
} catch {
|
|
286
|
-
// skip corrupt files
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
} catch {
|
|
290
|
-
// directory might not exist
|
|
291
|
-
}
|
|
292
|
-
} else if (status === "failed") {
|
|
317
|
+
if (isDead) {
|
|
318
|
+
// Every file in failed/ is a dead-letter — purge them all.
|
|
293
319
|
const failedDir = this.ensureFailedDir(queue);
|
|
294
320
|
try {
|
|
295
321
|
const files = readdirSync(failedDir).filter(f => f.endsWith(".queue-data"));
|
|
296
322
|
for (const file of files) {
|
|
297
323
|
try {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
unlinkSync(join(failedDir, file));
|
|
301
|
-
count++;
|
|
302
|
-
}
|
|
324
|
+
unlinkSync(join(failedDir, file));
|
|
325
|
+
count++;
|
|
303
326
|
} catch {
|
|
304
|
-
//
|
|
327
|
+
// already removed
|
|
305
328
|
}
|
|
306
329
|
}
|
|
307
330
|
} catch {
|
|
@@ -330,6 +353,11 @@ export class LiteBackend {
|
|
|
330
353
|
return count;
|
|
331
354
|
}
|
|
332
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Re-queue dead-letter jobs that are under the (possibly raised) limit back
|
|
358
|
+
* to pending. Mirrors Python retry_failed(): a job dead-lettered at the
|
|
359
|
+
* original maxRetries needs a raised limit to qualify again.
|
|
360
|
+
*/
|
|
333
361
|
retryFailed(queue: string, maxRetries: number = 3): number {
|
|
334
362
|
const failedDir = this.ensureFailedDir(queue);
|
|
335
363
|
const queueDir = this.ensureDir(queue);
|
|
@@ -348,9 +376,10 @@ export class LiteBackend {
|
|
|
348
376
|
|
|
349
377
|
job.status = "pending";
|
|
350
378
|
job.error = undefined;
|
|
379
|
+
job.createdAt = new Date().toISOString();
|
|
380
|
+
job.delayUntil = null;
|
|
351
381
|
|
|
352
|
-
this.
|
|
353
|
-
const prefix = `${Date.now()}-${String(this.seq).padStart(6, "0")}`;
|
|
382
|
+
const prefix = this.nextPrefix();
|
|
354
383
|
writeFileSync(join(queueDir, `${prefix}_${job.id}.queue-data`), JSON.stringify(job, null, 2));
|
|
355
384
|
unlinkSync(filePath);
|
|
356
385
|
count++;
|
|
@@ -376,6 +405,7 @@ export class LiteBackend {
|
|
|
376
405
|
}
|
|
377
406
|
|
|
378
407
|
for (const file of files) {
|
|
408
|
+
if (!file.includes(id)) continue;
|
|
379
409
|
const filePath = join(dir, file);
|
|
380
410
|
let job: QueueJob;
|
|
381
411
|
try {
|
|
@@ -394,24 +424,79 @@ export class LiteBackend {
|
|
|
394
424
|
return null;
|
|
395
425
|
}
|
|
396
426
|
|
|
397
|
-
|
|
427
|
+
/**
|
|
428
|
+
* Write the job back to the pending queue (queue dir).
|
|
429
|
+
*
|
|
430
|
+
* Re-enqueued jobs get a fresh createdAt so that within a priority tier they
|
|
431
|
+
* sort behind jobs that have not yet been attempted. `attempts` already
|
|
432
|
+
* reflects the latest failure count. The job carries its prior error.
|
|
433
|
+
*/
|
|
434
|
+
private requeue(queue: string, job: QueueJob, delaySeconds: number = 0, error?: string): void {
|
|
435
|
+
const dir = this.ensureDir(queue);
|
|
436
|
+
const jobData = {
|
|
437
|
+
id: job.id,
|
|
438
|
+
payload: job.payload,
|
|
439
|
+
status: "pending" as const,
|
|
440
|
+
createdAt: new Date().toISOString(),
|
|
441
|
+
attempts: job.attempts ?? 0,
|
|
442
|
+
delayUntil: delaySeconds > 0 ? new Date(Date.now() + delaySeconds * 1000).toISOString() : null,
|
|
443
|
+
priority: job.priority ?? 0,
|
|
444
|
+
topic: job.topic,
|
|
445
|
+
error,
|
|
446
|
+
};
|
|
447
|
+
const prefix = this.nextPrefix();
|
|
448
|
+
writeFileSync(join(dir, `${prefix}_${job.id}.queue-data`), JSON.stringify(jobData, null, 2));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Move the job to the dead-letter (failed/) directory. Terminal until a
|
|
453
|
+
* manual retryFailed()/retry() revives it.
|
|
454
|
+
*/
|
|
455
|
+
private deadLetter(queue: string, job: QueueJob, error?: string): void {
|
|
398
456
|
const failedDir = this.ensureFailedDir(queue);
|
|
399
|
-
|
|
457
|
+
const jobData = {
|
|
458
|
+
id: job.id,
|
|
459
|
+
payload: job.payload,
|
|
460
|
+
status: "dead" as const,
|
|
461
|
+
createdAt: job.createdAt,
|
|
462
|
+
attempts: job.attempts ?? 0,
|
|
463
|
+
delayUntil: null,
|
|
464
|
+
priority: job.priority ?? 0,
|
|
465
|
+
topic: job.topic,
|
|
466
|
+
error,
|
|
467
|
+
failedAt: new Date().toISOString(),
|
|
468
|
+
};
|
|
469
|
+
writeFileSync(join(failedDir, `${job.id}.queue-data`), JSON.stringify(jobData, null, 2));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Record a failed attempt.
|
|
474
|
+
*
|
|
475
|
+
* Increments `attempts` exactly once (the increment lives here, NOT in
|
|
476
|
+
* job.ts — see the double-increment fix). If the job still has retries left
|
|
477
|
+
* (attempts < maxRetries) it is automatically re-enqueued to pending, after
|
|
478
|
+
* an optional retryBackoff delay. Once it has been attempted maxRetries times
|
|
479
|
+
* (attempts >= maxRetries) it is moved to the dead-letter store.
|
|
480
|
+
*/
|
|
481
|
+
failJob(queue: string, job: QueueJob, error: string, maxRetries: number, retryBackoff: number = 0): void {
|
|
400
482
|
job.attempts = (job.attempts || 0) + 1;
|
|
401
483
|
job.error = error;
|
|
402
|
-
|
|
403
|
-
|
|
484
|
+
if (job.attempts < maxRetries) {
|
|
485
|
+
this.requeue(queue, job, retryBackoff, error);
|
|
486
|
+
} else {
|
|
487
|
+
this.deadLetter(queue, job, error);
|
|
488
|
+
}
|
|
404
489
|
}
|
|
405
490
|
|
|
491
|
+
/**
|
|
492
|
+
* Explicit re-queue requested by the caller (job.retry()).
|
|
493
|
+
*
|
|
494
|
+
* Always re-enqueues regardless of the retry limit — manual override,
|
|
495
|
+
* distinct from the automatic failJob() path.
|
|
496
|
+
*/
|
|
406
497
|
retryJob(queue: string, job: QueueJob, delaySeconds?: number): void {
|
|
407
|
-
const dir = this.ensureDir(queue);
|
|
408
|
-
job.status = "pending";
|
|
409
498
|
job.attempts = (job.attempts || 0) + 1;
|
|
410
499
|
job.error = undefined;
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
this.seq++;
|
|
414
|
-
const prefix = `${Date.now()}-${String(this.seq).padStart(6, "0")}`;
|
|
415
|
-
writeFileSync(join(dir, `${prefix}_${job.id}.queue-data`), JSON.stringify(job, null, 2));
|
|
500
|
+
this.requeue(queue, job, delaySeconds ?? 0, undefined);
|
|
416
501
|
}
|
|
417
502
|
}
|
|
@@ -5,13 +5,18 @@
|
|
|
5
5
|
* for message storage and delivery. Atomic pop via findOneAndUpdate.
|
|
6
6
|
*
|
|
7
7
|
* Configure via environment variables:
|
|
8
|
+
* TINA4_QUEUE_URL — connection URI (mongodb://...)
|
|
9
|
+
* TINA4_MONGO_URI (override; wins over TINA4_QUEUE_URL)
|
|
8
10
|
* TINA4_MONGO_HOST (default: "localhost")
|
|
9
11
|
* TINA4_MONGO_PORT (default: 27017)
|
|
10
|
-
* TINA4_MONGO_URI (overrides host/port/username/password)
|
|
11
12
|
* TINA4_MONGO_USERNAME (optional)
|
|
12
13
|
* TINA4_MONGO_PASSWORD (optional)
|
|
13
14
|
* TINA4_MONGO_DB (default: "tina4")
|
|
14
15
|
* TINA4_MONGO_COLLECTION (default: "tina4_queue")
|
|
16
|
+
*
|
|
17
|
+
* Precedence for the connection URI: explicit config.uri
|
|
18
|
+
* > TINA4_MONGO_URI > TINA4_QUEUE_URL > a URI built from the
|
|
19
|
+
* TINA4_MONGO_HOST/PORT/USERNAME/PASSWORD field vars (existing defaults).
|
|
15
20
|
*/
|
|
16
21
|
import { randomUUID } from "node:crypto";
|
|
17
22
|
import { execFileSync } from "node:child_process";
|
|
@@ -63,9 +68,11 @@ export class MongoBackend implements QueueBackend {
|
|
|
63
68
|
this.database = config?.database ?? process.env.TINA4_MONGO_DB ?? "tina4";
|
|
64
69
|
this.collection = config?.collection ?? process.env.TINA4_MONGO_COLLECTION ?? "tina4_queue";
|
|
65
70
|
|
|
66
|
-
// URI
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
// Connection URI precedence: explicit config.uri > TINA4_MONGO_URI
|
|
72
|
+
// > TINA4_QUEUE_URL > a URI built from the host/port/auth field vars.
|
|
73
|
+
const explicitUri = config?.uri ?? process.env.TINA4_MONGO_URI ?? process.env.TINA4_QUEUE_URL;
|
|
74
|
+
if (explicitUri) {
|
|
75
|
+
this.uri = explicitUri;
|
|
69
76
|
} else {
|
|
70
77
|
const auth = this.username
|
|
71
78
|
? `${encodeURIComponent(this.username)}:${encodeURIComponent(this.password)}@`
|
|
@@ -74,6 +81,13 @@ export class MongoBackend implements QueueBackend {
|
|
|
74
81
|
}
|
|
75
82
|
}
|
|
76
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Resolved connection config — exposed for testing/introspection.
|
|
86
|
+
*/
|
|
87
|
+
getConfig(): { uri: string; database: string; collection: string } {
|
|
88
|
+
return { uri: this.uri, database: this.database, collection: this.collection };
|
|
89
|
+
}
|
|
90
|
+
|
|
77
91
|
/**
|
|
78
92
|
* Execute a MongoDB operation synchronously via a child process.
|
|
79
93
|
*/
|
|
@@ -5,11 +5,15 @@
|
|
|
5
5
|
* for message storage and delivery.
|
|
6
6
|
*
|
|
7
7
|
* Configure via environment variables:
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
8
|
+
* TINA4_QUEUE_URL — AMQP URL (amqp://[user:pass@]host:port[/vhost])
|
|
9
|
+
* TINA4_RABBITMQ_HOST (override; default: "localhost")
|
|
10
|
+
* TINA4_RABBITMQ_PORT (override; default: 5672)
|
|
11
|
+
* TINA4_RABBITMQ_USERNAME (override; default: "guest")
|
|
12
|
+
* TINA4_RABBITMQ_PASSWORD (override; default: "guest")
|
|
13
|
+
* TINA4_RABBITMQ_VHOST (override; default: "/")
|
|
14
|
+
*
|
|
15
|
+
* Precedence per field: specific TINA4_RABBITMQ_* var (if set)
|
|
16
|
+
* > value derived from TINA4_QUEUE_URL > existing default.
|
|
13
17
|
*/
|
|
14
18
|
import net from "node:net";
|
|
15
19
|
import { execFileSync } from "node:child_process";
|
|
@@ -26,6 +30,51 @@ export interface RabbitMQConfig {
|
|
|
26
30
|
vhost?: string;
|
|
27
31
|
}
|
|
28
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Parse an AMQP URL (amqp://[user:pass@]host[:port][/vhost]) into a partial
|
|
35
|
+
* RabbitMQConfig. Mirrors the Python/PHP/Ruby `parse_amqp_url` semantics:
|
|
36
|
+
* strips a leading amqp:// or amqps:// scheme, splits optional credentials,
|
|
37
|
+
* and prepends a leading "/" to the vhost when missing. Only fields present
|
|
38
|
+
* in the URL are populated.
|
|
39
|
+
*/
|
|
40
|
+
export function parseAmqpUrl(url: string): RabbitMQConfig {
|
|
41
|
+
const config: RabbitMQConfig = {};
|
|
42
|
+
let rest = url.replace(/^amqps:\/\//, "").replace(/^amqp:\/\//, "");
|
|
43
|
+
|
|
44
|
+
const atIndex = rest.indexOf("@");
|
|
45
|
+
if (atIndex !== -1) {
|
|
46
|
+
const creds = rest.slice(0, atIndex);
|
|
47
|
+
rest = rest.slice(atIndex + 1);
|
|
48
|
+
const colonIndex = creds.indexOf(":");
|
|
49
|
+
if (colonIndex !== -1) {
|
|
50
|
+
config.username = creds.slice(0, colonIndex);
|
|
51
|
+
config.password = creds.slice(colonIndex + 1);
|
|
52
|
+
} else {
|
|
53
|
+
config.username = creds;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let hostport = rest;
|
|
58
|
+
const slashIndex = rest.indexOf("/");
|
|
59
|
+
if (slashIndex !== -1) {
|
|
60
|
+
hostport = rest.slice(0, slashIndex);
|
|
61
|
+
const vhost = rest.slice(slashIndex + 1);
|
|
62
|
+
if (vhost) {
|
|
63
|
+
config.vhost = vhost.startsWith("/") ? vhost : "/" + vhost;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const portColon = hostport.indexOf(":");
|
|
68
|
+
if (portColon !== -1) {
|
|
69
|
+
config.host = hostport.slice(0, portColon);
|
|
70
|
+
config.port = parseInt(hostport.slice(portColon + 1), 10);
|
|
71
|
+
} else if (hostport) {
|
|
72
|
+
config.host = hostport;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return config;
|
|
76
|
+
}
|
|
77
|
+
|
|
29
78
|
export interface QueueBackend {
|
|
30
79
|
push(queue: string, payload: unknown, delay?: number): string;
|
|
31
80
|
pop(queue: string): QueueJob | null;
|
|
@@ -133,12 +182,32 @@ export class RabbitMQBackend implements QueueBackend {
|
|
|
133
182
|
private vhost: string;
|
|
134
183
|
|
|
135
184
|
constructor(config?: RabbitMQConfig) {
|
|
136
|
-
|
|
185
|
+
// Base layer: values derived from TINA4_QUEUE_URL (parsed as an AMQP URL).
|
|
186
|
+
const url = process.env.TINA4_QUEUE_URL;
|
|
187
|
+
const fromUrl = url ? parseAmqpUrl(url) : {};
|
|
188
|
+
|
|
189
|
+
// Precedence per field: explicit config arg > specific TINA4_RABBITMQ_* var
|
|
190
|
+
// > value from TINA4_QUEUE_URL > existing default.
|
|
191
|
+
this.host = config?.host ?? process.env.TINA4_RABBITMQ_HOST ?? fromUrl.host ?? "localhost";
|
|
137
192
|
this.port = config?.port
|
|
138
|
-
?? (process.env.TINA4_RABBITMQ_PORT ? parseInt(process.env.TINA4_RABBITMQ_PORT, 10) :
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
this.
|
|
193
|
+
?? (process.env.TINA4_RABBITMQ_PORT ? parseInt(process.env.TINA4_RABBITMQ_PORT, 10) : undefined)
|
|
194
|
+
?? fromUrl.port ?? 5672;
|
|
195
|
+
this.username = config?.username ?? process.env.TINA4_RABBITMQ_USERNAME ?? fromUrl.username ?? "guest";
|
|
196
|
+
this.password = config?.password ?? process.env.TINA4_RABBITMQ_PASSWORD ?? fromUrl.password ?? "guest";
|
|
197
|
+
this.vhost = config?.vhost ?? process.env.TINA4_RABBITMQ_VHOST ?? fromUrl.vhost ?? "/";
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Resolved connection config — exposed for testing/introspection.
|
|
202
|
+
*/
|
|
203
|
+
getConfig(): Required<RabbitMQConfig> {
|
|
204
|
+
return {
|
|
205
|
+
host: this.host,
|
|
206
|
+
port: this.port,
|
|
207
|
+
username: this.username,
|
|
208
|
+
password: this.password,
|
|
209
|
+
vhost: this.vhost,
|
|
210
|
+
};
|
|
142
211
|
}
|
|
143
212
|
|
|
144
213
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RouteHandler, RouteDefinition, RouteMeta, Middleware, Tina4Request, Tina4Response, WebSocketRouteHandler, WebSocketRouteDefinition } from "./types.js";
|
|
1
|
+
import type { RouteHandler, RouteDefinition, RouteMeta, Middleware, MiddlewareSpec, Tina4Request, Tina4Response, WebSocketRouteHandler, WebSocketRouteDefinition } from "./types.js";
|
|
2
2
|
import { isTruthy } from "./dotenv.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -43,7 +43,7 @@ interface MatchResult {
|
|
|
43
43
|
params: Record<string, string | number>;
|
|
44
44
|
pattern: string;
|
|
45
45
|
meta?: RouteMeta;
|
|
46
|
-
middlewares?:
|
|
46
|
+
middlewares?: MiddlewareSpec[];
|
|
47
47
|
template?: string;
|
|
48
48
|
secure?: boolean;
|
|
49
49
|
cached?: boolean;
|
|
@@ -58,7 +58,7 @@ interface CompiledRoute {
|
|
|
58
58
|
handler: RouteHandler;
|
|
59
59
|
meta?: RouteMeta;
|
|
60
60
|
filePath?: string;
|
|
61
|
-
middlewares?:
|
|
61
|
+
middlewares?: MiddlewareSpec[];
|
|
62
62
|
secure?: boolean;
|
|
63
63
|
cached?: boolean;
|
|
64
64
|
noAuth?: boolean;
|
|
@@ -94,8 +94,11 @@ export class RouteRef {
|
|
|
94
94
|
return this;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
/**
|
|
98
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Append middleware to this route. Accepts middleware functions and/or
|
|
99
|
+
* string specs (e.g. `"ResponseCache:300"`), resolved when the route runs.
|
|
100
|
+
*/
|
|
101
|
+
middleware(...middlewareClasses: MiddlewareSpec[]): this {
|
|
99
102
|
this.route.middlewares = [...(this.route.middlewares ?? []), ...middlewareClasses];
|
|
100
103
|
return this;
|
|
101
104
|
}
|
|
@@ -187,35 +190,35 @@ export class Router {
|
|
|
187
190
|
/**
|
|
188
191
|
* Register a GET route programmatically.
|
|
189
192
|
*/
|
|
190
|
-
get(path: string, handler: RouteHandler, middlewares?:
|
|
193
|
+
get(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
191
194
|
return this.addRoute({ method: "GET", pattern: path, handler, middlewares, meta });
|
|
192
195
|
}
|
|
193
196
|
|
|
194
197
|
/**
|
|
195
198
|
* Register a POST route programmatically.
|
|
196
199
|
*/
|
|
197
|
-
post(path: string, handler: RouteHandler, middlewares?:
|
|
200
|
+
post(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
198
201
|
return this.addRoute({ method: "POST", pattern: path, handler, middlewares, meta });
|
|
199
202
|
}
|
|
200
203
|
|
|
201
204
|
/**
|
|
202
205
|
* Register a PUT route programmatically.
|
|
203
206
|
*/
|
|
204
|
-
put(path: string, handler: RouteHandler, middlewares?:
|
|
207
|
+
put(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
205
208
|
return this.addRoute({ method: "PUT", pattern: path, handler, middlewares, meta });
|
|
206
209
|
}
|
|
207
210
|
|
|
208
211
|
/**
|
|
209
212
|
* Register a PATCH route programmatically.
|
|
210
213
|
*/
|
|
211
|
-
patch(path: string, handler: RouteHandler, middlewares?:
|
|
214
|
+
patch(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
212
215
|
return this.addRoute({ method: "PATCH", pattern: path, handler, middlewares, meta });
|
|
213
216
|
}
|
|
214
217
|
|
|
215
218
|
/**
|
|
216
219
|
* Register a DELETE route programmatically.
|
|
217
220
|
*/
|
|
218
|
-
delete(path: string, handler: RouteHandler, middlewares?:
|
|
221
|
+
delete(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
219
222
|
return this.addRoute({ method: "DELETE", pattern: path, handler, middlewares, meta });
|
|
220
223
|
}
|
|
221
224
|
|
|
@@ -227,7 +230,7 @@ export class Router {
|
|
|
227
230
|
* logic, custom validator headers without the cost of building the body.
|
|
228
231
|
* The framework still strips the response body for you on the way out.
|
|
229
232
|
*/
|
|
230
|
-
head(path: string, handler: RouteHandler, middlewares?:
|
|
233
|
+
head(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
231
234
|
return this.addRoute({ method: "HEAD", pattern: path, handler, middlewares, meta });
|
|
232
235
|
}
|
|
233
236
|
|
|
@@ -237,14 +240,14 @@ export class Router {
|
|
|
237
240
|
* registered for the path and returning 204 (RFC 9110 §9.3.7). Use
|
|
238
241
|
* this to take over that behaviour.
|
|
239
242
|
*/
|
|
240
|
-
options(path: string, handler: RouteHandler, middlewares?:
|
|
243
|
+
options(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
241
244
|
return this.addRoute({ method: "OPTIONS", pattern: path, handler, middlewares, meta });
|
|
242
245
|
}
|
|
243
246
|
|
|
244
247
|
/**
|
|
245
248
|
* Register a route that matches ANY HTTP method.
|
|
246
249
|
*/
|
|
247
|
-
any(path: string, handler: RouteHandler, middlewares?:
|
|
250
|
+
any(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
248
251
|
let lastRef!: RouteRef;
|
|
249
252
|
for (const method of ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]) {
|
|
250
253
|
lastRef = this.addRoute({ method, pattern: path, handler, middlewares, meta });
|
|
@@ -255,7 +258,7 @@ export class Router {
|
|
|
255
258
|
/**
|
|
256
259
|
* Create a route group with a shared prefix and optional middlewares.
|
|
257
260
|
*/
|
|
258
|
-
group(prefix: string, callback: (group: RouteGroup) => void, middlewares?:
|
|
261
|
+
group(prefix: string, callback: (group: RouteGroup) => void, middlewares?: MiddlewareSpec[]): void {
|
|
259
262
|
const group = new RouteGroup(this, prefix, middlewares);
|
|
260
263
|
callback(group);
|
|
261
264
|
}
|
|
@@ -447,7 +450,7 @@ export class Router {
|
|
|
447
450
|
* Register a route for a specific HTTP method.
|
|
448
451
|
* Core registration method — all convenience methods delegate here.
|
|
449
452
|
*/
|
|
450
|
-
static add(method: string, path: string, handler: RouteHandler, middleware?:
|
|
453
|
+
static add(method: string, path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
451
454
|
const m = method.toUpperCase();
|
|
452
455
|
if (m === "ANY") {
|
|
453
456
|
return defaultRouter.any(path, handler, middleware, swaggerMeta);
|
|
@@ -458,42 +461,42 @@ export class Router {
|
|
|
458
461
|
/**
|
|
459
462
|
* Register a GET route on the default global router.
|
|
460
463
|
*/
|
|
461
|
-
static get(path: string, handler: RouteHandler, middleware?:
|
|
464
|
+
static get(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
462
465
|
return defaultRouter.get(path, handler, middleware, swaggerMeta);
|
|
463
466
|
}
|
|
464
467
|
|
|
465
468
|
/**
|
|
466
469
|
* Register a POST route on the default global router.
|
|
467
470
|
*/
|
|
468
|
-
static post(path: string, handler: RouteHandler, middleware?:
|
|
471
|
+
static post(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
469
472
|
return defaultRouter.post(path, handler, middleware, swaggerMeta);
|
|
470
473
|
}
|
|
471
474
|
|
|
472
475
|
/**
|
|
473
476
|
* Register a PUT route on the default global router.
|
|
474
477
|
*/
|
|
475
|
-
static put(path: string, handler: RouteHandler, middleware?:
|
|
478
|
+
static put(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
476
479
|
return defaultRouter.put(path, handler, middleware, swaggerMeta);
|
|
477
480
|
}
|
|
478
481
|
|
|
479
482
|
/**
|
|
480
483
|
* Register a PATCH route on the default global router.
|
|
481
484
|
*/
|
|
482
|
-
static patch(path: string, handler: RouteHandler, middleware?:
|
|
485
|
+
static patch(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
483
486
|
return defaultRouter.patch(path, handler, middleware, swaggerMeta);
|
|
484
487
|
}
|
|
485
488
|
|
|
486
489
|
/**
|
|
487
490
|
* Register a DELETE route on the default global router.
|
|
488
491
|
*/
|
|
489
|
-
static delete(path: string, handler: RouteHandler, middleware?:
|
|
492
|
+
static delete(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
490
493
|
return defaultRouter.delete(path, handler, middleware, swaggerMeta);
|
|
491
494
|
}
|
|
492
495
|
|
|
493
496
|
/**
|
|
494
497
|
* Register a route that matches ANY HTTP method on the default global router.
|
|
495
498
|
*/
|
|
496
|
-
static any(path: string, handler: RouteHandler, middleware?:
|
|
499
|
+
static any(path: string, handler: RouteHandler, middleware?: MiddlewareSpec[], swaggerMeta?: RouteMeta, template?: string): RouteRef {
|
|
497
500
|
return defaultRouter.any(path, handler, middleware, swaggerMeta);
|
|
498
501
|
}
|
|
499
502
|
|
|
@@ -507,7 +510,7 @@ export class Router {
|
|
|
507
510
|
/**
|
|
508
511
|
* Create a route group on the default global router.
|
|
509
512
|
*/
|
|
510
|
-
static group(prefix: string, callback: (group: RouteGroup) => void, middlewares?:
|
|
513
|
+
static group(prefix: string, callback: (group: RouteGroup) => void, middlewares?: MiddlewareSpec[]): void {
|
|
511
514
|
defaultRouter.group(prefix, callback, middlewares);
|
|
512
515
|
}
|
|
513
516
|
|
|
@@ -622,7 +625,7 @@ export class RouteGroup {
|
|
|
622
625
|
return merged.length > 0 ? merged : undefined;
|
|
623
626
|
}
|
|
624
627
|
|
|
625
|
-
get(path: string, handler: RouteHandler, middlewares?:
|
|
628
|
+
get(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
626
629
|
return this.router.addRoute({
|
|
627
630
|
method: "GET",
|
|
628
631
|
pattern: this.prefix + path,
|
|
@@ -632,7 +635,7 @@ export class RouteGroup {
|
|
|
632
635
|
});
|
|
633
636
|
}
|
|
634
637
|
|
|
635
|
-
post(path: string, handler: RouteHandler, middlewares?:
|
|
638
|
+
post(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
636
639
|
return this.router.addRoute({
|
|
637
640
|
method: "POST",
|
|
638
641
|
pattern: this.prefix + path,
|
|
@@ -642,7 +645,7 @@ export class RouteGroup {
|
|
|
642
645
|
});
|
|
643
646
|
}
|
|
644
647
|
|
|
645
|
-
put(path: string, handler: RouteHandler, middlewares?:
|
|
648
|
+
put(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
646
649
|
return this.router.addRoute({
|
|
647
650
|
method: "PUT",
|
|
648
651
|
pattern: this.prefix + path,
|
|
@@ -652,7 +655,7 @@ export class RouteGroup {
|
|
|
652
655
|
});
|
|
653
656
|
}
|
|
654
657
|
|
|
655
|
-
patch(path: string, handler: RouteHandler, middlewares?:
|
|
658
|
+
patch(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
656
659
|
return this.router.addRoute({
|
|
657
660
|
method: "PATCH",
|
|
658
661
|
pattern: this.prefix + path,
|
|
@@ -662,7 +665,7 @@ export class RouteGroup {
|
|
|
662
665
|
});
|
|
663
666
|
}
|
|
664
667
|
|
|
665
|
-
delete(path: string, handler: RouteHandler, middlewares?:
|
|
668
|
+
delete(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
666
669
|
return this.router.addRoute({
|
|
667
670
|
method: "DELETE",
|
|
668
671
|
pattern: this.prefix + path,
|
|
@@ -672,7 +675,7 @@ export class RouteGroup {
|
|
|
672
675
|
});
|
|
673
676
|
}
|
|
674
677
|
|
|
675
|
-
any(path: string, handler: RouteHandler, middlewares?:
|
|
678
|
+
any(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
676
679
|
let lastRef!: RouteRef;
|
|
677
680
|
for (const method of ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]) {
|
|
678
681
|
lastRef = this.router.addRoute({
|
|
@@ -689,7 +692,7 @@ export class RouteGroup {
|
|
|
689
692
|
/**
|
|
690
693
|
* Nested groups.
|
|
691
694
|
*/
|
|
692
|
-
group(prefix: string, callback: (group: RouteGroup) => void, middlewares?:
|
|
695
|
+
group(prefix: string, callback: (group: RouteGroup) => void, middlewares?: MiddlewareSpec[]): void {
|
|
693
696
|
const nestedGroup = new RouteGroup(
|
|
694
697
|
this.router,
|
|
695
698
|
this.prefix + prefix,
|
|
@@ -699,15 +702,64 @@ export class RouteGroup {
|
|
|
699
702
|
}
|
|
700
703
|
}
|
|
701
704
|
|
|
705
|
+
/**
|
|
706
|
+
* Resolve a string-form middleware spec to a middleware function.
|
|
707
|
+
*
|
|
708
|
+
* Forms (parity with Python/PHP/Ruby):
|
|
709
|
+
* "ResponseCache" → responseCache() with the default/env TTL
|
|
710
|
+
* "ResponseCache:300" → responseCache({ ttl: 300 })
|
|
711
|
+
*
|
|
712
|
+
* The head before the first ":" names the middleware; any trailing
|
|
713
|
+
* colon-separated parts are its arguments (numeric parts are parsed as
|
|
714
|
+
* integers). Unknown names throw so a typo surfaces instead of silently
|
|
715
|
+
* dropping the middleware. `responseCache` is loaded via a dynamic import so
|
|
716
|
+
* the router carries no import-time dependency on the cache module.
|
|
717
|
+
*
|
|
718
|
+
* Exported so route dispatch (and tests) can turn a spec into a runnable
|
|
719
|
+
* middleware.
|
|
720
|
+
*/
|
|
721
|
+
export async function resolveStringMiddleware(spec: string): Promise<Middleware> {
|
|
722
|
+
const colon = spec.indexOf(":");
|
|
723
|
+
const name = colon >= 0 ? spec.slice(0, colon) : spec;
|
|
724
|
+
const rawArgs = colon >= 0 ? spec.slice(colon + 1).split(":") : [];
|
|
725
|
+
|
|
726
|
+
switch (name) {
|
|
727
|
+
case "ResponseCache": {
|
|
728
|
+
const { responseCache } = await import("./cache.js");
|
|
729
|
+
// First arg (if any) is the TTL in seconds.
|
|
730
|
+
const ttlArg = rawArgs[0];
|
|
731
|
+
const ttl = ttlArg !== undefined && /^\d+$/.test(ttlArg) ? parseInt(ttlArg, 10) : undefined;
|
|
732
|
+
return responseCache(ttl !== undefined ? { ttl } : undefined);
|
|
733
|
+
}
|
|
734
|
+
default:
|
|
735
|
+
throw new Error(
|
|
736
|
+
`Unknown middleware "${name}". Known string middleware: ResponseCache. ` +
|
|
737
|
+
`For custom middleware, pass the function directly to .middleware(fn).`,
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Resolve a single route-middleware spec to a middleware function. Functions
|
|
744
|
+
* pass through unchanged; strings are resolved via resolveStringMiddleware.
|
|
745
|
+
*/
|
|
746
|
+
async function resolveMiddlewareSpec(spec: MiddlewareSpec): Promise<Middleware> {
|
|
747
|
+
return typeof spec === "string" ? resolveStringMiddleware(spec) : spec;
|
|
748
|
+
}
|
|
749
|
+
|
|
702
750
|
/**
|
|
703
751
|
* Run per-route middleware chain, then call the handler.
|
|
752
|
+
*
|
|
753
|
+
* Accepts middleware functions and/or string specs (e.g. "ResponseCache:300").
|
|
754
|
+
* Each spec is resolved to a middleware function just before it runs.
|
|
704
755
|
*/
|
|
705
756
|
export async function runRouteMiddlewares(
|
|
706
|
-
middlewares:
|
|
757
|
+
middlewares: MiddlewareSpec[],
|
|
707
758
|
req: Tina4Request,
|
|
708
759
|
res: Tina4Response,
|
|
709
760
|
): Promise<boolean> {
|
|
710
|
-
for (const
|
|
761
|
+
for (const spec of middlewares) {
|
|
762
|
+
const mw = await resolveMiddlewareSpec(spec);
|
|
711
763
|
let nextCalled = false;
|
|
712
764
|
await mw(req, res, () => {
|
|
713
765
|
nextCalled = true;
|
|
@@ -739,28 +791,28 @@ export const defaultRouter = new Router();
|
|
|
739
791
|
* res.json({ id: req.params.id }, 201);
|
|
740
792
|
* });
|
|
741
793
|
*/
|
|
742
|
-
export function get(path: string, handler: RouteHandler, middlewares?:
|
|
794
|
+
export function get(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
743
795
|
return defaultRouter.get(path, handler, middlewares, meta);
|
|
744
796
|
}
|
|
745
797
|
|
|
746
|
-
export function post(path: string, handler: RouteHandler, middlewares?:
|
|
798
|
+
export function post(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
747
799
|
return defaultRouter.post(path, handler, middlewares, meta);
|
|
748
800
|
}
|
|
749
801
|
|
|
750
|
-
export function put(path: string, handler: RouteHandler, middlewares?:
|
|
802
|
+
export function put(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
751
803
|
return defaultRouter.put(path, handler, middlewares, meta);
|
|
752
804
|
}
|
|
753
805
|
|
|
754
|
-
export function patch(path: string, handler: RouteHandler, middlewares?:
|
|
806
|
+
export function patch(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
755
807
|
return defaultRouter.patch(path, handler, middlewares, meta);
|
|
756
808
|
}
|
|
757
809
|
|
|
758
810
|
// Named "del" to avoid conflict with the "delete" keyword; also exported as "delete" alias below.
|
|
759
|
-
export function del(path: string, handler: RouteHandler, middlewares?:
|
|
811
|
+
export function del(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
760
812
|
return defaultRouter.delete(path, handler, middlewares, meta);
|
|
761
813
|
}
|
|
762
814
|
|
|
763
|
-
export function any(path: string, handler: RouteHandler, middlewares?:
|
|
815
|
+
export function any(path: string, handler: RouteHandler, middlewares?: MiddlewareSpec[], meta?: RouteMeta): RouteRef {
|
|
764
816
|
return defaultRouter.any(path, handler, middlewares, meta);
|
|
765
817
|
}
|
|
766
818
|
|
|
@@ -117,7 +117,8 @@ export interface RouteDefinition {
|
|
|
117
117
|
handler: RouteHandler;
|
|
118
118
|
filePath?: string;
|
|
119
119
|
meta?: RouteMeta;
|
|
120
|
-
|
|
120
|
+
/** Middleware functions and/or string specs (e.g. "ResponseCache:300"). */
|
|
121
|
+
middlewares?: MiddlewareSpec[];
|
|
121
122
|
/** Template file to render when handler returns a plain object */
|
|
122
123
|
template?: string;
|
|
123
124
|
/** Whether this route requires bearer-token authentication */
|
|
@@ -158,6 +159,20 @@ export type Middleware = (
|
|
|
158
159
|
next: () => void
|
|
159
160
|
) => void | Promise<void>;
|
|
160
161
|
|
|
162
|
+
/**
|
|
163
|
+
* A route middleware entry: either a middleware function, or a string spec
|
|
164
|
+
* resolved by the router to a built-in middleware.
|
|
165
|
+
*
|
|
166
|
+
* String forms (parity with Python/PHP/Ruby):
|
|
167
|
+
* "ResponseCache" → responseCache() with the default/env TTL
|
|
168
|
+
* "ResponseCache:300" → responseCache({ ttl: 300 })
|
|
169
|
+
*
|
|
170
|
+
* The router resolves string specs to middleware functions when the route
|
|
171
|
+
* runs, so callers can register a response-cache middleware without importing
|
|
172
|
+
* `responseCache`.
|
|
173
|
+
*/
|
|
174
|
+
export type MiddlewareSpec = Middleware | string;
|
|
175
|
+
|
|
161
176
|
/**
|
|
162
177
|
* Handler for WebSocket routes.
|
|
163
178
|
* connection — object with send/broadcast/close methods and route params.
|
|
@@ -317,8 +317,12 @@ export class CachedDatabaseAdapter implements DatabaseAdapter {
|
|
|
317
317
|
return this.adapter.query(sql, params);
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
-
fetch<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, skip?: number): T[] {
|
|
321
|
-
|
|
320
|
+
fetch<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, skip?: number, noCache?: boolean): T[] {
|
|
321
|
+
// `noCache` bypasses the query cache for this one call — no lookup, no
|
|
322
|
+
// store, run straight against the underlying adapter (mirrors the Python
|
|
323
|
+
// master's `no_cache`). Counters are left untouched so a bypass read isn't
|
|
324
|
+
// misreported as a hit or a miss.
|
|
325
|
+
if (this.enabled && !noCache) {
|
|
322
326
|
const key = QueryCache.queryKey(sql + `:L${limit}:S${skip}`, params as unknown[] | undefined);
|
|
323
327
|
const cached = this.cache.get<T[]>(key);
|
|
324
328
|
if (cached !== undefined) {
|
|
@@ -333,8 +337,8 @@ export class CachedDatabaseAdapter implements DatabaseAdapter {
|
|
|
333
337
|
return this.adapter.fetch(sql, params, limit, skip);
|
|
334
338
|
}
|
|
335
339
|
|
|
336
|
-
fetchOne<T = Record<string, unknown>>(sql: string, params?: unknown[]): T | null {
|
|
337
|
-
if (this.enabled) {
|
|
340
|
+
fetchOne<T = Record<string, unknown>>(sql: string, params?: unknown[], noCache?: boolean): T | null {
|
|
341
|
+
if (this.enabled && !noCache) {
|
|
338
342
|
const key = QueryCache.queryKey(sql + ":ONE", params as unknown[] | undefined);
|
|
339
343
|
const cached = this.cache.get<T | null>(key);
|
|
340
344
|
if (cached !== undefined) {
|
|
@@ -417,11 +421,13 @@ export class CachedDatabaseAdapter implements DatabaseAdapter {
|
|
|
417
421
|
// ORM read/write path prefer those when present. We mirror them here so the
|
|
418
422
|
// cache sits in front of the async path too. Reads cache; writes flush.
|
|
419
423
|
|
|
420
|
-
async fetchAsync<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, skip?: number): Promise<T[]> {
|
|
424
|
+
async fetchAsync<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, skip?: number, noCache?: boolean): Promise<T[]> {
|
|
421
425
|
const run = async (): Promise<T[]> => (this.adapter as any).fetchAsync
|
|
422
426
|
? await (this.adapter as any).fetchAsync(sql, params, limit, skip)
|
|
423
427
|
: this.adapter.fetch<T>(sql, params, limit, skip);
|
|
424
|
-
|
|
428
|
+
// `noCache` bypasses both cache layers for this one call — no lookup, no
|
|
429
|
+
// store, run directly (mirrors the Python master's `no_cache`).
|
|
430
|
+
if (this.enabled && !noCache) {
|
|
425
431
|
const key = QueryCache.queryKey(sql + `:L${limit}:S${skip}`, params as unknown[] | undefined);
|
|
426
432
|
// Persistent distributed backend is AUTHORITATIVE (mirrors Python, where a
|
|
427
433
|
// configured _cache_backend bypasses the in-process dict). This keeps
|
|
@@ -448,11 +454,12 @@ export class CachedDatabaseAdapter implements DatabaseAdapter {
|
|
|
448
454
|
return run();
|
|
449
455
|
}
|
|
450
456
|
|
|
451
|
-
async fetchOneAsync<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T | null> {
|
|
457
|
+
async fetchOneAsync<T = Record<string, unknown>>(sql: string, params?: unknown[], noCache?: boolean): Promise<T | null> {
|
|
452
458
|
const run = async (): Promise<T | null> => (this.adapter as any).fetchOneAsync
|
|
453
459
|
? await (this.adapter as any).fetchOneAsync(sql, params)
|
|
454
460
|
: this.adapter.fetchOne<T>(sql, params);
|
|
455
|
-
|
|
461
|
+
// `noCache` bypasses both cache layers for this one call (see fetchAsync).
|
|
462
|
+
if (this.enabled && !noCache) {
|
|
456
463
|
const key = QueryCache.queryKey(sql + ":ONE", params as unknown[] | undefined);
|
|
457
464
|
if (this.usesPersistentBackend()) {
|
|
458
465
|
const shared = await this.backendGetOne<T>(key);
|
|
@@ -38,11 +38,13 @@ export function stripTrailingSemicolons(sql: string): string {
|
|
|
38
38
|
* the single chokepoint every public read/write flows through.
|
|
39
39
|
*/
|
|
40
40
|
export async function adapterFetch<T = Record<string, unknown>>(
|
|
41
|
-
adapter: DatabaseAdapter, sql: string, params?: unknown[], limit?: number, skip?: number,
|
|
41
|
+
adapter: DatabaseAdapter, sql: string, params?: unknown[], limit?: number, skip?: number, noCache?: boolean,
|
|
42
42
|
): Promise<T[]> {
|
|
43
|
+
// `noCache` is forwarded to the CachedDatabaseAdapter so a single read can
|
|
44
|
+
// bypass the query cache; raw adapters ignore the extra trailing arg.
|
|
43
45
|
return (adapter as any).fetchAsync
|
|
44
|
-
? await (adapter as any).fetchAsync(sql, params, limit, skip)
|
|
45
|
-
: adapter.fetch<T>(sql, params, limit, skip);
|
|
46
|
+
? await (adapter as any).fetchAsync(sql, params, limit, skip, noCache)
|
|
47
|
+
: adapter.fetch<T>(sql, params, limit, skip, noCache);
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
export async function adapterQuery<T = Record<string, unknown>>(
|
|
@@ -633,14 +635,16 @@ export class Database {
|
|
|
633
635
|
* the fallback resolves instantly). This is the breaking change that makes
|
|
634
636
|
* the wrapper work uniformly across every engine.
|
|
635
637
|
*/
|
|
636
|
-
async fetch(sql: string, params?: unknown[], limit?: number, offset?: number): Promise<DatabaseResult> {
|
|
638
|
+
async fetch(sql: string, params?: unknown[], limit?: number, offset?: number, opts?: { noCache?: boolean }): Promise<DatabaseResult> {
|
|
637
639
|
// v3.13.12: strip trailing `;` before the adapter wraps with COUNT(*)
|
|
638
640
|
// or appends LIMIT/OFFSET. Without this, `"SELECT * FROM t;"` becomes
|
|
639
641
|
// `"SELECT * FROM t; LIMIT 100 OFFSET 0"` — a syntax error.
|
|
640
642
|
sql = stripTrailingSemicolons(sql);
|
|
641
643
|
const adapter = this.getNextAdapter();
|
|
642
644
|
try {
|
|
643
|
-
|
|
645
|
+
// `opts.noCache` bypasses the query cache for this one call — no lookup,
|
|
646
|
+
// no store, run directly (mirrors the Python master's `no_cache`).
|
|
647
|
+
const rows = await adapterFetch(adapter, sql, params, limit, offset, opts?.noCache);
|
|
644
648
|
this.lastError = null;
|
|
645
649
|
return new DatabaseResult(rows, undefined, undefined, limit, offset, adapter, sql);
|
|
646
650
|
} catch (e: any) {
|
|
@@ -650,13 +654,19 @@ export class Database {
|
|
|
650
654
|
}
|
|
651
655
|
}
|
|
652
656
|
|
|
653
|
-
/**
|
|
654
|
-
|
|
657
|
+
/**
|
|
658
|
+
* Fetch a single row or null.
|
|
659
|
+
*
|
|
660
|
+
* Pass `{ noCache: true }` as the trailing options object to bypass the
|
|
661
|
+
* query cache for this one call — no lookup, no store, run directly
|
|
662
|
+
* (mirrors the Python master's `no_cache`). Default preserves caching.
|
|
663
|
+
*/
|
|
664
|
+
async fetchOne<T = Record<string, unknown>>(sql: string, params?: unknown[], opts?: { noCache?: boolean }): Promise<T | null> {
|
|
655
665
|
sql = stripTrailingSemicolons(sql);
|
|
656
666
|
const adapter = this.getNextAdapter();
|
|
657
667
|
return (adapter as any).fetchOneAsync
|
|
658
|
-
? await (adapter as any).fetchOneAsync<T>(sql, params)
|
|
659
|
-
: adapter.fetchOne<T>(sql, params);
|
|
668
|
+
? await (adapter as any).fetchOneAsync<T>(sql, params, opts?.noCache)
|
|
669
|
+
: adapter.fetchOne<T>(sql, params, opts?.noCache);
|
|
660
670
|
}
|
|
661
671
|
|
|
662
672
|
/**
|
|
@@ -671,9 +681,14 @@ export class Database {
|
|
|
671
681
|
*
|
|
672
682
|
* Returns `[]` (not `null`) when no rows match. Cross-framework parity
|
|
673
683
|
* with Python `db.fetch_all()`, PHP `$db->fetchAll()`, and Ruby `db.fetch_all`.
|
|
684
|
+
*
|
|
685
|
+
* Pass `{ noCache: true }` as the trailing options object to bypass the
|
|
686
|
+
* query cache for this one call — no lookup, no store, run directly
|
|
687
|
+
* (mirrors the Python master's `no_cache`). The options object is a
|
|
688
|
+
* SEPARATE trailing argument, never the params array.
|
|
674
689
|
*/
|
|
675
|
-
async fetchAll<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, offset?: number): Promise<T[]> {
|
|
676
|
-
return (await this.fetch(sql, params, limit, offset)).records as T[];
|
|
690
|
+
async fetchAll<T = Record<string, unknown>>(sql: string, params?: unknown[], limit?: number, offset?: number, opts?: { noCache?: boolean }): Promise<T[]> {
|
|
691
|
+
return (await this.fetch(sql, params, limit, offset, opts)).records as T[];
|
|
677
692
|
}
|
|
678
693
|
|
|
679
694
|
/**
|