@guren/server 2.25.1 → 2.26.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/dist/{Gate-YOt54xGF.js → Gate-BL_zW82W.js} +1 -1
- package/dist/{Job-COMzBNOL.js → Job-DsFMcXVk.js} +35 -3
- package/dist/{Resource-BdYd4J9a.js → Resource-gki0_2B6.js} +172 -37
- package/dist/agent/scopes.d.ts +1 -3
- package/dist/auth/email-verification.d.ts +6 -0
- package/dist/auth/index.js +1 -1
- package/dist/auth/password-reset.d.ts +8 -2
- package/dist/{auth-vFeem6uM.js → auth-DiyWALZT.js} +38 -9
- package/dist/authorization/index.js +2 -2
- package/dist/{authorization-BLhwA2Yh.js → authorization-E6X6Y-B6.js} +1 -1
- package/dist/broadcasting/index.js +1 -1
- package/dist/{broadcasting-BUBCPCnu.js → broadcasting-D0vMkKcq.js} +1 -1
- package/dist/cache/index.js +1 -1
- package/dist/cache/stores/FileStore.d.ts +2 -0
- package/dist/cache/stores/MemoryStore.d.ts +1 -0
- package/dist/cache/stores/RedisStore.d.ts +1 -0
- package/dist/cache/types.d.ts +2 -0
- package/dist/{cache-93n2PCnB.js → cache-DcnRufMe.js} +94 -25
- package/dist/contained-path-BkNSBljW.js +78 -0
- package/dist/container/Container.d.ts +6 -2
- package/dist/container/defineModule.d.ts +10 -0
- package/dist/{default-application-D5ezhhiV.js → default-application-DWwscxfW.js} +14 -18
- package/dist/encryption/index.js +1 -1
- package/dist/{encryption-D1dxfMqD.js → encryption-CWK5l22W.js} +1 -1
- package/dist/events/EventManager.d.ts +2 -0
- package/dist/events/index.js +1 -1
- package/dist/{events-c94CdUFc.js → events-DQavGm8W.js} +20 -7
- package/dist/health/HealthManager.d.ts +0 -1
- package/dist/health/index.js +1 -1
- package/dist/{health-CB0oS_UE.js → health-Bg0Xj3aD.js} +8 -8
- package/dist/http/Application.d.ts +12 -1
- package/dist/http/middleware/rate-limit.d.ts +1 -4
- package/dist/i18n/index.js +1 -1
- package/dist/{i18n-DqcRD4qL.js → i18n-DmMKcbqj.js} +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +61 -50
- package/dist/internal/app-conventions.d.ts +6 -0
- package/dist/internal/app-conventions.js +9 -0
- package/dist/internal/testing.d.ts +1 -1
- package/dist/internal/testing.js +2 -2
- package/dist/lambda/index.js +66 -28
- package/dist/logging/index.js +1 -1
- package/dist/{logging-YbKmbAd7.js → logging-Ci0mBk5k.js} +1 -1
- package/dist/mail/index.js +1 -1
- package/dist/{mail-uRBrUl9q.js → mail-CI2Kfe8P.js} +2 -2
- package/dist/mvc/Controller.d.ts +3 -2
- package/dist/mvc/inertia/InertiaEngine.d.ts +31 -0
- package/dist/mvc/inertia/props.d.ts +71 -0
- package/dist/notifications/index.js +1 -1
- package/dist/notifications/registry.d.ts +2 -2
- package/dist/{notifications-LHuujttf.js → notifications-Bp2uamzD.js} +7 -5
- package/dist/providers/ConfigServiceProvider.d.ts +5 -5
- package/dist/queue/Job.d.ts +5 -0
- package/dist/queue/Worker.d.ts +6 -1
- package/dist/queue/drivers/RedisDriver.d.ts +4 -2
- package/dist/queue/drivers/SqsDriver.d.ts +31 -2
- package/dist/queue/drivers/redis-queue-scripts.d.ts +83 -0
- package/dist/queue/index.js +2 -2
- package/dist/queue/serialize.d.ts +7 -0
- package/dist/queue/types.d.ts +13 -2
- package/dist/{queue-BiBn5Sff.js → queue-Bo4Q857L.js} +294 -125
- package/dist/redis/RedisEmailVerificationStore.d.ts +2 -0
- package/dist/redis/RedisPasswordResetStore.d.ts +2 -0
- package/dist/redis/auth-token-scripts.d.ts +4 -0
- package/dist/redis/index.js +50 -0
- package/dist/runtime/index.js +3 -36
- package/dist/scheduling/ScheduledTask.d.ts +2 -1
- package/dist/scheduling/index.js +1 -1
- package/dist/{scheduling-DhYfbJFj.js → scheduling-DNV5tj44.js} +32 -38
- package/dist/serialize-CKXek0Tr.js +17 -0
- package/dist/storage/drivers/LocalDriver.d.ts +15 -0
- package/dist/storage/index.js +1 -1
- package/dist/storage/url-path.d.ts +2 -0
- package/dist/{storage-6HLaUf9j.js → storage-wFsUJHZW.js} +78 -28
- package/dist/support/contained-path.d.ts +8 -0
- package/dist/support/registry-collision.d.ts +12 -0
- package/dist/{vite-manifest-DK55BTLk.js → vite-manifest-DlU94f3Z.js} +2 -2
- package/package.json +6 -2
- package/dist/trim-slashes-BLY9hKAa.js +0 -22
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as warnOnce } from "./warn-once-p-IlMjra.js";
|
|
2
|
+
import { a as getJob, i as enqueueJob } from "./Job-DsFMcXVk.js";
|
|
3
|
+
import { t as deserializeQueuedJob } from "./serialize-CKXek0Tr.js";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
2
5
|
//#region src/queue/drivers/MemoryDriver.ts
|
|
3
6
|
/** In-memory queue driver for development and testing. */
|
|
4
7
|
var MemoryDriver = class {
|
|
@@ -145,6 +148,120 @@ var SyncDriver = class {
|
|
|
145
148
|
}
|
|
146
149
|
};
|
|
147
150
|
//#endregion
|
|
151
|
+
//#region src/queue/drivers/redis-queue-scripts.ts
|
|
152
|
+
/** ARGV[1] = job id, ARGV[2] = reservation token ('' skips the ownership check). */
|
|
153
|
+
const OWNER = `
|
|
154
|
+
if redis.call('EXISTS', KEYS[1]) == 0 then return 0 end
|
|
155
|
+
if ARGV[2] ~= '' and redis.call('HGET', KEYS[1], 'reservationToken') ~= ARGV[2] then return 0 end
|
|
156
|
+
`;
|
|
157
|
+
const QUEUE_SCRIPTS = {
|
|
158
|
+
/** KEYS: job, pending. ARGV: id, availableAt, ...hash fields. */
|
|
159
|
+
gurenQueuePush: {
|
|
160
|
+
numberOfKeys: 2,
|
|
161
|
+
lua: `
|
|
162
|
+
redis.call('HSET', KEYS[1], unpack(ARGV, 3))
|
|
163
|
+
redis.call('ZADD', KEYS[2], ARGV[2], ARGV[1])
|
|
164
|
+
return 1
|
|
165
|
+
`
|
|
166
|
+
},
|
|
167
|
+
/** KEYS: pending, reserved. ARGV: now, expiresAt, reservedAt, token, job key prefix. */
|
|
168
|
+
gurenQueuePop: {
|
|
169
|
+
numberOfKeys: 2,
|
|
170
|
+
lua: `
|
|
171
|
+
local expired = redis.call('ZRANGEBYSCORE', KEYS[2], '-inf', ARGV[1], 'LIMIT', 0, 100)
|
|
172
|
+
for _, id in ipairs(expired) do
|
|
173
|
+
redis.call('ZREM', KEYS[2], id)
|
|
174
|
+
local key = ARGV[5] .. id
|
|
175
|
+
if redis.call('HEXISTS', key, 'id') == 1 then
|
|
176
|
+
redis.call('ZADD', KEYS[1], ARGV[1], id)
|
|
177
|
+
redis.call('HDEL', key, 'reservedAt', 'reservationToken')
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
while true do
|
|
181
|
+
local ids = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, 1)
|
|
182
|
+
if #ids == 0 then return {} end
|
|
183
|
+
local id = ids[1]
|
|
184
|
+
local key = ARGV[5] .. id
|
|
185
|
+
redis.call('ZREM', KEYS[1], id)
|
|
186
|
+
if redis.call('HEXISTS', key, 'id') == 1 then
|
|
187
|
+
redis.call('ZADD', KEYS[2], ARGV[2], id)
|
|
188
|
+
redis.call('HSET', key, 'reservedAt', ARGV[3], 'reservationToken', ARGV[4])
|
|
189
|
+
return redis.call('HGETALL', key)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
`
|
|
193
|
+
},
|
|
194
|
+
/** KEYS: job, pending, reserved. ARGV: id, token, attempts, availableAt ISO, lastError, availableAt ms. */
|
|
195
|
+
gurenQueueRelease: {
|
|
196
|
+
numberOfKeys: 3,
|
|
197
|
+
lua: OWNER + `
|
|
198
|
+
redis.call('HSET', KEYS[1], 'attempts', ARGV[3], 'availableAt', ARGV[4])
|
|
199
|
+
if ARGV[5] ~= '' then redis.call('HSET', KEYS[1], 'lastError', ARGV[5]) else redis.call('HDEL', KEYS[1], 'lastError') end
|
|
200
|
+
redis.call('HDEL', KEYS[1], 'reservedAt', 'reservationToken')
|
|
201
|
+
redis.call('ZREM', KEYS[3], ARGV[1])
|
|
202
|
+
redis.call('ZADD', KEYS[2], ARGV[6], ARGV[1])
|
|
203
|
+
return 1
|
|
204
|
+
`
|
|
205
|
+
},
|
|
206
|
+
/** KEYS: job. ARGV: id, token, queue key prefix. */
|
|
207
|
+
gurenQueueDelete: {
|
|
208
|
+
numberOfKeys: 1,
|
|
209
|
+
lua: OWNER + `
|
|
210
|
+
local queue = redis.call('HGET', KEYS[1], 'queue')
|
|
211
|
+
redis.call('ZREM', ARGV[3] .. queue .. ':pending', ARGV[1])
|
|
212
|
+
redis.call('ZREM', ARGV[3] .. queue .. ':reserved', ARGV[1])
|
|
213
|
+
redis.call('DEL', KEYS[1])
|
|
214
|
+
return 1
|
|
215
|
+
`
|
|
216
|
+
},
|
|
217
|
+
/** KEYS: job, pending, reserved, failed. ARGV: id, token, failedAt, error, stack, attempts. */
|
|
218
|
+
gurenQueueFail: {
|
|
219
|
+
numberOfKeys: 4,
|
|
220
|
+
lua: OWNER + `
|
|
221
|
+
redis.call('HSET', KEYS[1], 'failedAt', ARGV[3], 'error', ARGV[4], 'stack', ARGV[5], 'attempts', ARGV[6])
|
|
222
|
+
redis.call('HDEL', KEYS[1], 'reservedAt', 'reservationToken')
|
|
223
|
+
redis.call('ZREM', KEYS[2], ARGV[1])
|
|
224
|
+
redis.call('ZREM', KEYS[3], ARGV[1])
|
|
225
|
+
redis.call('LREM', KEYS[4], 0, ARGV[1])
|
|
226
|
+
redis.call('LPUSH', KEYS[4], ARGV[1])
|
|
227
|
+
return 1
|
|
228
|
+
`
|
|
229
|
+
},
|
|
230
|
+
/** KEYS: job, reserved. ARGV: id, token, expiresAt. */
|
|
231
|
+
gurenQueueExtend: {
|
|
232
|
+
numberOfKeys: 2,
|
|
233
|
+
lua: OWNER + `
|
|
234
|
+
if not redis.call('ZSCORE', KEYS[2], ARGV[1]) then return 0 end
|
|
235
|
+
redis.call('ZADD', KEYS[2], ARGV[3], ARGV[1])
|
|
236
|
+
return 1
|
|
237
|
+
`
|
|
238
|
+
},
|
|
239
|
+
/** KEYS: job. ARGV: id, now ISO, now ms, queue key prefix. */
|
|
240
|
+
gurenQueueRetry: {
|
|
241
|
+
numberOfKeys: 1,
|
|
242
|
+
lua: `
|
|
243
|
+
local queue = redis.call('HGET', KEYS[1], 'queue')
|
|
244
|
+
if not queue or redis.call('HEXISTS', KEYS[1], 'failedAt') == 0 then return 0 end
|
|
245
|
+
redis.call('LREM', ARGV[4] .. queue .. ':failed', 0, ARGV[1])
|
|
246
|
+
redis.call('HSET', KEYS[1], 'attempts', '0', 'availableAt', ARGV[2], 'createdAt', ARGV[2])
|
|
247
|
+
redis.call('HDEL', KEYS[1], 'reservedAt', 'reservationToken', 'failedAt', 'error', 'stack', 'lastError')
|
|
248
|
+
redis.call('ZADD', ARGV[4] .. queue .. ':pending', ARGV[3], ARGV[1])
|
|
249
|
+
return 1
|
|
250
|
+
`
|
|
251
|
+
},
|
|
252
|
+
/** KEYS: job. ARGV: id, queue key prefix. */
|
|
253
|
+
gurenQueueDeleteFailed: {
|
|
254
|
+
numberOfKeys: 1,
|
|
255
|
+
lua: `
|
|
256
|
+
local queue = redis.call('HGET', KEYS[1], 'queue')
|
|
257
|
+
if not queue or redis.call('HEXISTS', KEYS[1], 'failedAt') == 0 then return 0 end
|
|
258
|
+
redis.call('LREM', ARGV[2] .. queue .. ':failed', 0, ARGV[1])
|
|
259
|
+
redis.call('DEL', KEYS[1])
|
|
260
|
+
return 1
|
|
261
|
+
`
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
//#endregion
|
|
148
265
|
//#region src/queue/drivers/RedisDriver.ts
|
|
149
266
|
/**
|
|
150
267
|
* Redis-backed queue driver for production. Key layout:
|
|
@@ -160,6 +277,18 @@ var RedisDriver = class {
|
|
|
160
277
|
this.redis = redis;
|
|
161
278
|
this.prefix = options.prefix ?? "queue:";
|
|
162
279
|
this.visibilityTimeout = options.visibilityTimeout ?? 6e4;
|
|
280
|
+
if (!Number.isFinite(this.visibilityTimeout) || this.visibilityTimeout < 3) throw new Error("Redis queue visibilityTimeout must be at least 3 milliseconds");
|
|
281
|
+
for (const [name, script] of Object.entries(QUEUE_SCRIPTS)) redis.defineCommand(name, script);
|
|
282
|
+
}
|
|
283
|
+
run(name, ...args) {
|
|
284
|
+
return this.redis[name](...args);
|
|
285
|
+
}
|
|
286
|
+
get heartbeatInterval() {
|
|
287
|
+
return Math.max(1, Math.floor(this.visibilityTimeout / 3));
|
|
288
|
+
}
|
|
289
|
+
async extendReservation(job) {
|
|
290
|
+
if (!job.reservationToken) return false;
|
|
291
|
+
return await this.run("gurenQueueExtend", this.jobKey(job.id), this.reservedKey(job.queue), job.id, job.reservationToken, Date.now() + this.visibilityTimeout) === 1;
|
|
163
292
|
}
|
|
164
293
|
pendingKey(queue) {
|
|
165
294
|
return `${this.prefix}${queue}:pending`;
|
|
@@ -184,69 +313,25 @@ var RedisDriver = class {
|
|
|
184
313
|
availableAt: job.availableAt.toISOString(),
|
|
185
314
|
createdAt: job.createdAt.toISOString()
|
|
186
315
|
};
|
|
187
|
-
|
|
188
|
-
pipeline.hset(this.jobKey(job.id), jobData);
|
|
189
|
-
pipeline.zadd(this.pendingKey(job.queue), job.availableAt.getTime(), job.id);
|
|
190
|
-
await pipeline.exec();
|
|
316
|
+
await this.run("gurenQueuePush", this.jobKey(job.id), this.pendingKey(job.queue), job.id, job.availableAt.getTime(), ...Object.entries(jobData).flat());
|
|
191
317
|
}
|
|
192
318
|
async pop(queue) {
|
|
193
319
|
const now = Date.now();
|
|
194
|
-
await this.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const timeout = now + this.visibilityTimeout;
|
|
200
|
-
await this.redis.zadd(this.reservedKey(queue), timeout, jobId);
|
|
201
|
-
await this.redis.hset(this.jobKey(jobId), "reservedAt", (/* @__PURE__ */ new Date()).toISOString());
|
|
202
|
-
const jobData = await this.redis.hgetall(this.jobKey(jobId));
|
|
203
|
-
if (!jobData || !jobData.id) {
|
|
204
|
-
await this.redis.zrem(this.reservedKey(queue), jobId);
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
return this.parseJobData(jobData);
|
|
208
|
-
}
|
|
209
|
-
async releaseTimedOutJobs(queue) {
|
|
210
|
-
const now = Date.now();
|
|
211
|
-
const timedOutIds = await this.redis.zrangebyscore(this.reservedKey(queue), "-inf", now);
|
|
212
|
-
for (const jobId of timedOutIds) if (await this.redis.zrem(this.reservedKey(queue), jobId) > 0) {
|
|
213
|
-
await this.redis.zadd(this.pendingKey(queue), now, jobId);
|
|
214
|
-
await this.redis.hdel(this.jobKey(jobId), "reservedAt");
|
|
215
|
-
}
|
|
320
|
+
const fields = await this.run("gurenQueuePop", this.pendingKey(queue), this.reservedKey(queue), now, now + this.visibilityTimeout, new Date(now).toISOString(), randomUUID(), this.jobKey(""));
|
|
321
|
+
if (fields.length === 0) return null;
|
|
322
|
+
const data = {};
|
|
323
|
+
for (let i = 0; i < fields.length; i += 2) data[fields[i]] = fields[i + 1];
|
|
324
|
+
return this.parseJobData(data);
|
|
216
325
|
}
|
|
217
326
|
async release(job, delayMs = 0) {
|
|
218
327
|
const availableAt = Date.now() + delayMs;
|
|
219
|
-
await this.
|
|
220
|
-
const updates = {
|
|
221
|
-
attempts: String(job.attempts),
|
|
222
|
-
availableAt: new Date(availableAt).toISOString()
|
|
223
|
-
};
|
|
224
|
-
if (job.lastError) updates.lastError = job.lastError;
|
|
225
|
-
await this.redis.hset(this.jobKey(job.id), updates);
|
|
226
|
-
await this.redis.hdel(this.jobKey(job.id), "reservedAt");
|
|
227
|
-
await this.redis.zadd(this.pendingKey(job.queue), availableAt, job.id);
|
|
328
|
+
await this.run("gurenQueueRelease", this.jobKey(job.id), this.pendingKey(job.queue), this.reservedKey(job.queue), job.id, job.reservationToken ?? "", job.attempts, new Date(availableAt).toISOString(), job.lastError ?? "", availableAt);
|
|
228
329
|
}
|
|
229
|
-
async delete(jobId) {
|
|
230
|
-
|
|
231
|
-
if (jobData && jobData.queue) {
|
|
232
|
-
const pipeline = this.redis.pipeline();
|
|
233
|
-
pipeline.zrem(this.pendingKey(jobData.queue), jobId);
|
|
234
|
-
pipeline.zrem(this.reservedKey(jobData.queue), jobId);
|
|
235
|
-
pipeline.del(this.jobKey(jobId));
|
|
236
|
-
await pipeline.exec();
|
|
237
|
-
} else await this.redis.del(this.jobKey(jobId));
|
|
330
|
+
async delete(jobId, reservationToken) {
|
|
331
|
+
await this.run("gurenQueueDelete", this.jobKey(jobId), jobId, reservationToken ?? "", this.prefix);
|
|
238
332
|
}
|
|
239
333
|
async fail(job, error) {
|
|
240
|
-
|
|
241
|
-
failedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
242
|
-
error: error.message,
|
|
243
|
-
stack: error.stack ?? ""
|
|
244
|
-
};
|
|
245
|
-
await this.redis.hset(this.jobKey(job.id), failedData);
|
|
246
|
-
const pipeline = this.redis.pipeline();
|
|
247
|
-
pipeline.zrem(this.reservedKey(job.queue), job.id);
|
|
248
|
-
pipeline.lpush(this.failedKey(job.queue), job.id);
|
|
249
|
-
await pipeline.exec();
|
|
334
|
+
await this.run("gurenQueueFail", this.jobKey(job.id), this.pendingKey(job.queue), this.reservedKey(job.queue), this.failedKey(job.queue), job.id, job.reservationToken ?? "", (/* @__PURE__ */ new Date()).toISOString(), error.message, error.stack ?? "", job.attempts);
|
|
250
335
|
}
|
|
251
336
|
async size(queue) {
|
|
252
337
|
return this.redis.zcard(this.pendingKey(queue));
|
|
@@ -288,23 +373,11 @@ var RedisDriver = class {
|
|
|
288
373
|
return jobs;
|
|
289
374
|
}
|
|
290
375
|
async retryFailedJob(jobId) {
|
|
291
|
-
const jobData = await this.redis.hgetall(this.jobKey(jobId));
|
|
292
|
-
if (!jobData || !jobData.queue) throw new Error(`Failed job not found: ${jobId}`);
|
|
293
|
-
const queue = jobData.queue;
|
|
294
|
-
await this.redis.lrem(this.failedKey(queue), 1, jobId);
|
|
295
376
|
const now = /* @__PURE__ */ new Date();
|
|
296
|
-
await this.
|
|
297
|
-
attempts: "0",
|
|
298
|
-
availableAt: now.toISOString(),
|
|
299
|
-
createdAt: now.toISOString()
|
|
300
|
-
});
|
|
301
|
-
await this.redis.hdel(this.jobKey(jobId), "reservedAt", "failedAt", "error", "stack", "lastError");
|
|
302
|
-
await this.redis.zadd(this.pendingKey(queue), now.getTime(), jobId);
|
|
377
|
+
if (await this.run("gurenQueueRetry", this.jobKey(jobId), jobId, now.toISOString(), now.getTime(), this.prefix) === 0) throw new Error(`Failed job not found: ${jobId}`);
|
|
303
378
|
}
|
|
304
379
|
async deleteFailedJob(jobId) {
|
|
305
|
-
|
|
306
|
-
if (jobData && jobData.queue) await this.redis.lrem(this.failedKey(jobData.queue), 1, jobId);
|
|
307
|
-
await this.redis.del(this.jobKey(jobId));
|
|
380
|
+
await this.run("gurenQueueDeleteFailed", this.jobKey(jobId), jobId, this.prefix);
|
|
308
381
|
}
|
|
309
382
|
/** Testing only. */
|
|
310
383
|
async clear() {
|
|
@@ -329,12 +402,18 @@ var RedisDriver = class {
|
|
|
329
402
|
availableAt: new Date(data.availableAt),
|
|
330
403
|
createdAt: new Date(data.createdAt),
|
|
331
404
|
reservedAt: data.reservedAt ? new Date(data.reservedAt) : null,
|
|
332
|
-
lastError: data.lastError
|
|
405
|
+
lastError: data.lastError,
|
|
406
|
+
reservationToken: data.reservationToken
|
|
333
407
|
};
|
|
334
408
|
}
|
|
335
409
|
};
|
|
336
410
|
//#endregion
|
|
337
411
|
//#region src/queue/drivers/SqsDriver.ts
|
|
412
|
+
const EXPIRED_RECEIPT_ERRORS = /* @__PURE__ */ new Set(["ReceiptHandleIsInvalid", "MessageNotInflight"]);
|
|
413
|
+
/** SQS counts deliveries from 1; anything else is an adapter reporting none. */
|
|
414
|
+
function toReceiveCount(value) {
|
|
415
|
+
return Number.isSafeInteger(value) && value >= 1 ? value : void 0;
|
|
416
|
+
}
|
|
338
417
|
/** Builds an SqsAdapter over an @aws-sdk/client-sqs SQSClient. */
|
|
339
418
|
function createSqsAdapter(client) {
|
|
340
419
|
return {
|
|
@@ -356,22 +435,37 @@ function createSqsAdapter(client) {
|
|
|
356
435
|
const msg = (await client.send(new ReceiveMessageCommand({
|
|
357
436
|
QueueUrl: params.queueUrl,
|
|
358
437
|
MaxNumberOfMessages: 1,
|
|
438
|
+
MessageSystemAttributeNames: ["ApproximateReceiveCount"],
|
|
359
439
|
WaitTimeSeconds: params.waitTimeSeconds ?? 5
|
|
360
440
|
}))).Messages?.[0];
|
|
361
441
|
if (!msg?.Body || !msg.ReceiptHandle) return null;
|
|
362
442
|
return {
|
|
363
443
|
body: msg.Body,
|
|
364
|
-
receiptHandle: msg.ReceiptHandle
|
|
444
|
+
receiptHandle: msg.ReceiptHandle,
|
|
445
|
+
receiveCount: toReceiveCount(Number(msg.Attributes?.ApproximateReceiveCount))
|
|
365
446
|
};
|
|
366
447
|
},
|
|
367
|
-
async
|
|
368
|
-
const {
|
|
369
|
-
await client.send(new
|
|
448
|
+
async deleteMessage(params) {
|
|
449
|
+
const { DeleteMessageCommand } = await importSqs();
|
|
450
|
+
await client.send(new DeleteMessageCommand({
|
|
370
451
|
QueueUrl: params.queueUrl,
|
|
371
|
-
ReceiptHandle: params.receiptHandle
|
|
372
|
-
VisibilityTimeout: params.visibilityTimeout
|
|
452
|
+
ReceiptHandle: params.receiptHandle
|
|
373
453
|
}));
|
|
374
454
|
},
|
|
455
|
+
async changeMessageVisibility(params) {
|
|
456
|
+
const { ChangeMessageVisibilityCommand } = await importSqs();
|
|
457
|
+
try {
|
|
458
|
+
await client.send(new ChangeMessageVisibilityCommand({
|
|
459
|
+
QueueUrl: params.queueUrl,
|
|
460
|
+
ReceiptHandle: params.receiptHandle,
|
|
461
|
+
VisibilityTimeout: params.visibilityTimeout
|
|
462
|
+
}));
|
|
463
|
+
return true;
|
|
464
|
+
} catch (error) {
|
|
465
|
+
if (EXPIRED_RECEIPT_ERRORS.has(error.name ?? "")) return false;
|
|
466
|
+
throw error;
|
|
467
|
+
}
|
|
468
|
+
},
|
|
375
469
|
async getApproximateMessageCount(queueUrl) {
|
|
376
470
|
const { GetQueueAttributesCommand } = await importSqs();
|
|
377
471
|
const result = await client.send(new GetQueueAttributesCommand({
|
|
@@ -395,11 +489,26 @@ var SqsDriver = class {
|
|
|
395
489
|
adapter;
|
|
396
490
|
options;
|
|
397
491
|
failedJobs = /* @__PURE__ */ new Map();
|
|
398
|
-
|
|
492
|
+
reservations = /* @__PURE__ */ new Map();
|
|
399
493
|
constructor(adapter, options) {
|
|
400
494
|
this.adapter = adapter;
|
|
401
495
|
this.options = options;
|
|
402
496
|
}
|
|
497
|
+
get heartbeatInterval() {
|
|
498
|
+
return Math.max(1e3, Math.floor(this.visibilityTimeout * 1e3 / 3));
|
|
499
|
+
}
|
|
500
|
+
async extendReservation(job) {
|
|
501
|
+
const reservation = this.reservations.get(job.id);
|
|
502
|
+
if (!reservation) return false;
|
|
503
|
+
return await this.adapter.changeMessageVisibility({
|
|
504
|
+
queueUrl: reservation.queueUrl,
|
|
505
|
+
receiptHandle: reservation.receiptHandle,
|
|
506
|
+
visibilityTimeout: this.visibilityTimeout
|
|
507
|
+
}) !== false;
|
|
508
|
+
}
|
|
509
|
+
get visibilityTimeout() {
|
|
510
|
+
return this.options.visibilityTimeout ?? 30;
|
|
511
|
+
}
|
|
403
512
|
async push(job) {
|
|
404
513
|
const delaySeconds = Math.min(900, Math.max(0, Math.floor((job.availableAt.getTime() - Date.now()) / 1e3)));
|
|
405
514
|
await this.adapter.sendMessage({
|
|
@@ -411,22 +520,29 @@ var SqsDriver = class {
|
|
|
411
520
|
});
|
|
412
521
|
}
|
|
413
522
|
async pop(queue) {
|
|
414
|
-
const
|
|
523
|
+
const queueUrl = this.resolveQueueUrl(queue);
|
|
524
|
+
const result = await this.adapter.receiveMessage({ queueUrl });
|
|
415
525
|
if (!result) return null;
|
|
416
|
-
const job =
|
|
417
|
-
|
|
526
|
+
const job = deserializeQueuedJob(result.body);
|
|
527
|
+
const receiveCount = toReceiveCount(result.receiveCount);
|
|
528
|
+
if (receiveCount !== void 0) job.attempts += receiveCount - 1;
|
|
529
|
+
else warnOnce("sqs-adapter-missing-receive-count", "[guren] The SqsAdapter returned no receiveCount: retry counts restart from the message body on every redelivery, so a failing job retries until the queue drops it. Return ApproximateReceiveCount as receiveCount from receiveMessage(), or build the adapter with createSqsAdapter().");
|
|
530
|
+
this.reservations.set(job.id, {
|
|
531
|
+
receiptHandle: result.receiptHandle,
|
|
532
|
+
queueUrl
|
|
533
|
+
});
|
|
418
534
|
job.reservedAt = /* @__PURE__ */ new Date();
|
|
419
535
|
return job;
|
|
420
536
|
}
|
|
421
537
|
async release(job, delayMs = 0) {
|
|
422
|
-
const
|
|
423
|
-
if (
|
|
538
|
+
const reservation = this.reservations.get(job.id);
|
|
539
|
+
if (reservation) {
|
|
424
540
|
await this.adapter.changeMessageVisibility({
|
|
425
|
-
queueUrl:
|
|
426
|
-
receiptHandle,
|
|
541
|
+
queueUrl: reservation.queueUrl,
|
|
542
|
+
receiptHandle: reservation.receiptHandle,
|
|
427
543
|
visibilityTimeout: Math.ceil(delayMs / 1e3)
|
|
428
544
|
});
|
|
429
|
-
this.
|
|
545
|
+
this.reservations.delete(job.id);
|
|
430
546
|
} else {
|
|
431
547
|
job.reservedAt = null;
|
|
432
548
|
job.availableAt = new Date(Date.now() + delayMs);
|
|
@@ -434,7 +550,7 @@ var SqsDriver = class {
|
|
|
434
550
|
}
|
|
435
551
|
}
|
|
436
552
|
async delete(jobId) {
|
|
437
|
-
this.
|
|
553
|
+
await this.acknowledge(jobId);
|
|
438
554
|
}
|
|
439
555
|
async fail(job, error) {
|
|
440
556
|
const failedJob = {
|
|
@@ -444,7 +560,17 @@ var SqsDriver = class {
|
|
|
444
560
|
stack: error.stack
|
|
445
561
|
};
|
|
446
562
|
this.failedJobs.set(job.id, failedJob);
|
|
447
|
-
|
|
563
|
+
try {
|
|
564
|
+
await this.acknowledge(job.id);
|
|
565
|
+
} catch (deleteError) {
|
|
566
|
+
console.error(JSON.stringify({
|
|
567
|
+
level: "error",
|
|
568
|
+
msg: `Could not delete the SQS message for failed job: ${job.name}`,
|
|
569
|
+
job: job.name,
|
|
570
|
+
queue: job.queue,
|
|
571
|
+
error: deleteError.message
|
|
572
|
+
}));
|
|
573
|
+
}
|
|
448
574
|
}
|
|
449
575
|
async size(queue) {
|
|
450
576
|
return this.adapter.getApproximateMessageCount(this.resolveQueueUrl(queue));
|
|
@@ -476,23 +602,26 @@ var SqsDriver = class {
|
|
|
476
602
|
}
|
|
477
603
|
async clear() {
|
|
478
604
|
this.failedJobs.clear();
|
|
479
|
-
this.
|
|
605
|
+
this.reservations.clear();
|
|
606
|
+
}
|
|
607
|
+
/** Drops the reservation only once SQS has accepted the deletion. */
|
|
608
|
+
async acknowledge(jobId) {
|
|
609
|
+
const reservation = this.reservations.get(jobId);
|
|
610
|
+
if (!reservation) return;
|
|
611
|
+
if (this.adapter.deleteMessage) await this.adapter.deleteMessage({
|
|
612
|
+
queueUrl: reservation.queueUrl,
|
|
613
|
+
receiptHandle: reservation.receiptHandle
|
|
614
|
+
});
|
|
615
|
+
else warnOnce("sqs-adapter-missing-delete-message", "[guren] The SqsAdapter has no deleteMessage(): acknowledged jobs stay on the queue and SQS redelivers them once the visibility timeout expires. Implement deleteMessage() on your adapter, or build it with createSqsAdapter().");
|
|
616
|
+
this.reservations.delete(jobId);
|
|
480
617
|
}
|
|
481
618
|
resolveQueueUrl(queue) {
|
|
482
619
|
return this.options.queueUrls?.[queue] ?? this.options.queueUrl;
|
|
483
620
|
}
|
|
484
621
|
};
|
|
485
|
-
function deserializeJob(body) {
|
|
486
|
-
const raw = JSON.parse(body);
|
|
487
|
-
return {
|
|
488
|
-
...raw,
|
|
489
|
-
availableAt: new Date(raw.availableAt),
|
|
490
|
-
createdAt: new Date(raw.createdAt),
|
|
491
|
-
reservedAt: raw.reservedAt ? new Date(raw.reservedAt) : null
|
|
492
|
-
};
|
|
493
|
-
}
|
|
494
622
|
//#endregion
|
|
495
623
|
//#region src/queue/Worker.ts
|
|
624
|
+
var ReservationLostError = class extends Error {};
|
|
496
625
|
/**
|
|
497
626
|
* Pulls jobs off a driver's queues in priority order and runs each under a
|
|
498
627
|
* timeout, retrying through `driver.release()`.
|
|
@@ -525,20 +654,24 @@ var Worker = class {
|
|
|
525
654
|
this.running = true;
|
|
526
655
|
this.shouldStop = false;
|
|
527
656
|
this.processedJobs = 0;
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
657
|
+
try {
|
|
658
|
+
this.events.workerStarted?.();
|
|
659
|
+
while (!this.shouldStop) {
|
|
660
|
+
if (this.maxJobs > 0 && this.processedJobs >= this.maxJobs) break;
|
|
661
|
+
const job = await this.getNextJob();
|
|
662
|
+
if (job) {
|
|
663
|
+
await this.processJob(job);
|
|
664
|
+
this.processedJobs++;
|
|
665
|
+
} else {
|
|
666
|
+
if (this.stopWhenEmpty) break;
|
|
667
|
+
await this.sleepMs(this.sleep);
|
|
668
|
+
}
|
|
538
669
|
}
|
|
670
|
+
} finally {
|
|
671
|
+
this.currentJob = null;
|
|
672
|
+
this.running = false;
|
|
673
|
+
this.events.workerStopped?.();
|
|
539
674
|
}
|
|
540
|
-
this.running = false;
|
|
541
|
-
this.events.workerStopped?.();
|
|
542
675
|
}
|
|
543
676
|
async stop() {
|
|
544
677
|
this.shouldStop = true;
|
|
@@ -569,12 +702,22 @@ var Worker = class {
|
|
|
569
702
|
return;
|
|
570
703
|
}
|
|
571
704
|
try {
|
|
572
|
-
|
|
573
|
-
await this.
|
|
574
|
-
await this.driver.delete(job.id);
|
|
705
|
+
await this.runHandler(this.instantiate(JobClass), job);
|
|
706
|
+
await this.driver.delete(job.id, job.reservationToken);
|
|
575
707
|
this.events.jobProcessed?.(job);
|
|
576
708
|
} catch (error) {
|
|
577
|
-
|
|
709
|
+
if (error instanceof ReservationLostError) {
|
|
710
|
+
console.error(JSON.stringify({
|
|
711
|
+
level: "error",
|
|
712
|
+
msg: `Job reservation lost: ${job.name}`,
|
|
713
|
+
job: job.name,
|
|
714
|
+
queue: job.queue,
|
|
715
|
+
attempt: job.attempts
|
|
716
|
+
}));
|
|
717
|
+
this.events.jobFailed?.(job, error, true);
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
await this.handleFailedJob(job, error instanceof Error ? error : new Error(String(error)), JobClass);
|
|
578
721
|
} finally {
|
|
579
722
|
this.currentJob = null;
|
|
580
723
|
}
|
|
@@ -611,19 +754,45 @@ var Worker = class {
|
|
|
611
754
|
if (this.options.container) instance.setContainer(this.options.container);
|
|
612
755
|
return instance;
|
|
613
756
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
757
|
+
/**
|
|
758
|
+
* Runs `handle()` under the timeout while renewing the driver's reservation. A
|
|
759
|
+
* timeout requests cancellation through the job's signal and then waits for the
|
|
760
|
+
* handler to settle, so a retry never runs beside its predecessor.
|
|
761
|
+
*/
|
|
762
|
+
async runHandler(instance, job) {
|
|
763
|
+
const cancellation = new AbortController();
|
|
764
|
+
const timeout = setTimeout(() => {
|
|
765
|
+
cancellation.abort(/* @__PURE__ */ new Error(`Job timed out after ${this.timeout}ms`));
|
|
766
|
+
}, this.timeout);
|
|
767
|
+
const interval = this.driver.extendReservation ? this.driver.heartbeatInterval : void 0;
|
|
768
|
+
let heartbeat;
|
|
769
|
+
let renewal;
|
|
770
|
+
let reservationError;
|
|
771
|
+
let settled = false;
|
|
772
|
+
const renew = () => {
|
|
773
|
+
renewal = this.driver.extendReservation(job).then((owned) => {
|
|
774
|
+
if (owned) return;
|
|
775
|
+
reservationError = new ReservationLostError("Job reservation now belongs to another worker");
|
|
776
|
+
cancellation.abort(reservationError);
|
|
777
|
+
}, () => {}).finally(() => {
|
|
778
|
+
if (!settled && !reservationError && interval !== void 0) heartbeat = setTimeout(renew, interval);
|
|
625
779
|
});
|
|
626
|
-
}
|
|
780
|
+
};
|
|
781
|
+
if (interval !== void 0) heartbeat = setTimeout(renew, interval);
|
|
782
|
+
let failure;
|
|
783
|
+
try {
|
|
784
|
+
instance.setExecutionSignal(cancellation.signal);
|
|
785
|
+
await instance.handle(job.payload);
|
|
786
|
+
} catch (error) {
|
|
787
|
+
failure = { error };
|
|
788
|
+
} finally {
|
|
789
|
+
settled = true;
|
|
790
|
+
clearTimeout(timeout);
|
|
791
|
+
clearTimeout(heartbeat);
|
|
792
|
+
await renewal;
|
|
793
|
+
}
|
|
794
|
+
if (reservationError) throw reservationError;
|
|
795
|
+
if (failure) throw cancellation.signal.aborted ? cancellation.signal.reason : failure.error;
|
|
627
796
|
}
|
|
628
797
|
sleepMs(ms) {
|
|
629
798
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -13,6 +13,8 @@ export declare class RedisEmailVerificationStore implements EmailVerificationTok
|
|
|
13
13
|
private readonly prefix;
|
|
14
14
|
constructor(redis: Redis, options?: RedisEmailVerificationStoreOptions);
|
|
15
15
|
store(token: EmailVerificationToken): Promise<void>;
|
|
16
|
+
replace(token: EmailVerificationToken): Promise<void>;
|
|
17
|
+
consume(tokenId: string, email: string): Promise<boolean>;
|
|
16
18
|
findByTokenId(tokenId: string): Promise<EmailVerificationToken | null>;
|
|
17
19
|
delete(tokenId: string): Promise<void>;
|
|
18
20
|
deleteForEmail(email: string): Promise<void>;
|
|
@@ -13,6 +13,8 @@ export declare class RedisPasswordResetStore implements PasswordResetTokenStore
|
|
|
13
13
|
private readonly prefix;
|
|
14
14
|
constructor(redis: Redis, options?: RedisPasswordResetStoreOptions);
|
|
15
15
|
store(tokenId: string, email: string, expiresAt: Date): Promise<void>;
|
|
16
|
+
replace(tokenId: string, email: string, expiresAt: Date): Promise<void>;
|
|
17
|
+
consume(tokenId: string, email: string): Promise<boolean>;
|
|
16
18
|
find(tokenId: string): Promise<{
|
|
17
19
|
email: string;
|
|
18
20
|
expiresAt: Date;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Replace the indexed tokens and publish their successor as one Redis operation. */
|
|
2
|
+
export declare const REPLACE_AUTH_TOKEN = "\nlocal ids = redis.call('SMEMBERS', KEYS[1])\nfor _, id in ipairs(ids) do\n redis.call('DEL', ARGV[1] .. id)\nend\nredis.call('DEL', KEYS[1])\nlocal ttl = tonumber(ARGV[4])\nif ttl > 0 then\n redis.call('PSETEX', KEYS[2], ttl, ARGV[3])\n redis.call('SADD', KEYS[1], ARGV[2])\n redis.call('PEXPIRE', KEYS[1], ttl + 60000)\nend\nreturn 1\n";
|
|
3
|
+
/** Compare and consume without a GET/DEL race between different workers. */
|
|
4
|
+
export declare const CONSUME_AUTH_TOKEN = "\nlocal raw = redis.call('GET', KEYS[1])\nif not raw then return 0 end\nlocal ok, record = pcall(cjson.decode, raw)\nif not ok or type(record) ~= 'table' or record.email ~= ARGV[1] then return 0 end\nredis.call('DEL', KEYS[1])\nredis.call('SREM', KEYS[2], ARGV[2])\nreturn 1\n";
|
package/dist/redis/index.js
CHANGED
|
@@ -245,6 +245,33 @@ var RedisApiTokenStore = class {
|
|
|
245
245
|
}
|
|
246
246
|
};
|
|
247
247
|
//#endregion
|
|
248
|
+
//#region src/redis/auth-token-scripts.ts
|
|
249
|
+
/** Replace the indexed tokens and publish their successor as one Redis operation. */
|
|
250
|
+
const REPLACE_AUTH_TOKEN = `
|
|
251
|
+
local ids = redis.call('SMEMBERS', KEYS[1])
|
|
252
|
+
for _, id in ipairs(ids) do
|
|
253
|
+
redis.call('DEL', ARGV[1] .. id)
|
|
254
|
+
end
|
|
255
|
+
redis.call('DEL', KEYS[1])
|
|
256
|
+
local ttl = tonumber(ARGV[4])
|
|
257
|
+
if ttl > 0 then
|
|
258
|
+
redis.call('PSETEX', KEYS[2], ttl, ARGV[3])
|
|
259
|
+
redis.call('SADD', KEYS[1], ARGV[2])
|
|
260
|
+
redis.call('PEXPIRE', KEYS[1], ttl + 60000)
|
|
261
|
+
end
|
|
262
|
+
return 1
|
|
263
|
+
`;
|
|
264
|
+
/** Compare and consume without a GET/DEL race between different workers. */
|
|
265
|
+
const CONSUME_AUTH_TOKEN = `
|
|
266
|
+
local raw = redis.call('GET', KEYS[1])
|
|
267
|
+
if not raw then return 0 end
|
|
268
|
+
local ok, record = pcall(cjson.decode, raw)
|
|
269
|
+
if not ok or type(record) ~= 'table' or record.email ~= ARGV[1] then return 0 end
|
|
270
|
+
redis.call('DEL', KEYS[1])
|
|
271
|
+
redis.call('SREM', KEYS[2], ARGV[2])
|
|
272
|
+
return 1
|
|
273
|
+
`;
|
|
274
|
+
//#endregion
|
|
248
275
|
//#region src/redis/RedisPasswordResetStore.ts
|
|
249
276
|
/**
|
|
250
277
|
* Key layout: `pwreset:{tokenId}` JSON of email + expiresAt,
|
|
@@ -272,6 +299,17 @@ var RedisPasswordResetStore = class {
|
|
|
272
299
|
pipeline.pexpire(emailKey, ttlMs + 6e4);
|
|
273
300
|
await pipeline.exec();
|
|
274
301
|
}
|
|
302
|
+
async replace(tokenId, email, expiresAt) {
|
|
303
|
+
email = email.toLowerCase();
|
|
304
|
+
const data = JSON.stringify({
|
|
305
|
+
email,
|
|
306
|
+
expiresAt: expiresAt.toISOString()
|
|
307
|
+
});
|
|
308
|
+
await this.redis.eval(REPLACE_AUTH_TOKEN, 2, `${this.prefix}email:${email}`, `${this.prefix}${tokenId}`, this.prefix, tokenId, data, Math.max(0, expiresAt.getTime() - Date.now()));
|
|
309
|
+
}
|
|
310
|
+
async consume(tokenId, email) {
|
|
311
|
+
return await this.redis.eval(CONSUME_AUTH_TOKEN, 2, `${this.prefix}${tokenId}`, `${this.prefix}email:${email.toLowerCase()}`, email, tokenId) === 1;
|
|
312
|
+
}
|
|
275
313
|
async find(tokenId) {
|
|
276
314
|
const tokenKey = `${this.prefix}${tokenId}`;
|
|
277
315
|
const data = await this.redis.get(tokenKey);
|
|
@@ -342,6 +380,18 @@ var RedisEmailVerificationStore = class {
|
|
|
342
380
|
pipeline.pexpire(emailKey, ttlMs + 6e4);
|
|
343
381
|
await pipeline.exec();
|
|
344
382
|
}
|
|
383
|
+
async replace(token) {
|
|
384
|
+
const { tokenId, expiresAt } = token;
|
|
385
|
+
const email = token.email.toLowerCase();
|
|
386
|
+
const data = JSON.stringify({
|
|
387
|
+
...token,
|
|
388
|
+
email
|
|
389
|
+
});
|
|
390
|
+
await this.redis.eval(REPLACE_AUTH_TOKEN, 2, `${this.prefix}email:${email}`, `${this.prefix}${tokenId}`, this.prefix, tokenId, data, Math.max(0, expiresAt.getTime() - Date.now()));
|
|
391
|
+
}
|
|
392
|
+
async consume(tokenId, email) {
|
|
393
|
+
return await this.redis.eval(CONSUME_AUTH_TOKEN, 2, `${this.prefix}${tokenId}`, `${this.prefix}email:${email.toLowerCase()}`, email, tokenId) === 1;
|
|
394
|
+
}
|
|
345
395
|
async findByTokenId(tokenId) {
|
|
346
396
|
const tokenKey = `${this.prefix}${tokenId}`;
|
|
347
397
|
const data = await this.redis.get(tokenKey);
|