@guren/server 1.4.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{AuthManager-C84xF5ks.d.ts → AuthManager-DBP4KYsx.d.ts} +1 -1
- package/dist/ConsoleKernel-DlK9j2Lu.d.ts +101 -0
- package/dist/DocsViewerServiceProvider-D7RGJPGM.js +75 -0
- package/dist/{I18nManager-CmUqj6bU.d.ts → I18nManager-BiSoczfV.d.ts} +1 -1
- package/dist/McpServiceProvider-7BB5FPF3.js +8 -0
- package/dist/auth/index.d.ts +39 -29
- package/dist/auth/index.js +2 -2
- package/dist/broadcasting/index.js +2 -2
- package/dist/cache/index.js +2 -2
- package/dist/chunk-2V3YID4H.js +20 -0
- package/dist/{chunk-JIT7QVMJ.js → chunk-4DW4GRWF.js} +3 -3
- package/dist/{chunk-R2TCP7D7.js → chunk-6J6B5KOB.js} +18 -2
- package/dist/chunk-AZOYI43J.js +18 -0
- package/dist/{chunk-DCYKSYAT.js → chunk-B5E5VBDI.js} +54 -2
- package/dist/{chunk-NYQGAWDN.js → chunk-BZMZEFRG.js} +7 -11
- package/dist/{chunk-4SBGSJNT.js → chunk-CWI4AJ47.js} +2 -1
- package/dist/{chunk-NQCZQAJL.js → chunk-DSDX3ASM.js} +21 -2
- package/dist/{chunk-MC752CQT.js → chunk-EI5GYMAT.js} +2 -1
- package/dist/{chunk-AUXQISW5.js → chunk-HONCNQXZ.js} +38 -5
- package/dist/{chunk-CSRQTEQA.js → chunk-HWEWL6TE.js} +6 -1
- package/dist/chunk-JBG4AR24.js +18 -0
- package/dist/{chunk-EDRGAM6G.js → chunk-K6CUBJX6.js} +11 -16
- package/dist/{chunk-WJJ5CTNI.js → chunk-KTNJG7R7.js} +6 -2
- package/dist/{chunk-74HTZG3V.js → chunk-LAJQCSSF.js} +24 -0
- package/dist/{chunk-H32L2NE3.js → chunk-LVJCZCAV.js} +20 -13
- package/dist/{chunk-CSDKWLFD.js → chunk-N7PLD7MB.js} +120 -43
- package/dist/{chunk-ONSDE37A.js → chunk-SFQP6O52.js} +1 -0
- package/dist/{chunk-XMQZENBP.js → chunk-TVY72CLV.js} +3 -1
- package/dist/chunk-WIXGT4TN.js +18 -0
- package/dist/{email-verification-H_WlpsVH.d.ts → email-verification-D8N7P6Pu.d.ts} +1 -1
- package/dist/encryption/index.js +2 -2
- package/dist/events/index.js +1 -1
- package/dist/i18n/index.d.ts +2 -2
- package/dist/index.d.ts +348 -330
- package/dist/index.js +332 -110
- package/dist/lambda/index.d.ts +5 -5
- package/dist/lambda/index.js +1 -1
- package/dist/mail/index.js +4 -3
- package/dist/mcp/index.d.ts +14 -4
- package/dist/mcp/index.js +3 -2
- package/dist/notifications/index.d.ts +55 -6
- package/dist/notifications/index.js +12 -3
- package/dist/queue/index.d.ts +5 -5
- package/dist/queue/index.js +4 -2
- package/dist/redis/index.d.ts +4 -4
- package/dist/redis/index.js +7 -0
- package/dist/runtime/index.d.ts +5 -5
- package/dist/runtime/index.js +24 -9
- package/dist/scheduling/index.js +2 -2
- package/dist/storage/index.js +2 -1
- package/dist/{Application-BvBqgc2k.d.ts → validation-CDR7lRE8.d.ts} +873 -654
- package/package.json +3 -3
- package/dist/ConsoleKernel-Dw9X9qZY.d.ts +0 -207
- package/dist/McpServiceProvider-TZWQWXCT.js +0 -7
- package/dist/{api-token-COQHXyM_.d.ts → api-token-D24VPr_E.d.ts} +101 -101
- package/dist/{client-BQwF2b9Y.d.ts → rate-limit-D9J2b4S1.d.ts} +36 -36
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
MessageSigner,
|
|
6
6
|
deriveAppKeyring,
|
|
7
7
|
getAppKeyringFromEnv
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-SFQP6O52.js";
|
|
9
9
|
import {
|
|
10
10
|
hashPassword,
|
|
11
11
|
needsRehash,
|
|
@@ -19,6 +19,7 @@ var MemorySessionStore = class {
|
|
|
19
19
|
constructor(now = () => Date.now()) {
|
|
20
20
|
this.now = now;
|
|
21
21
|
}
|
|
22
|
+
now;
|
|
22
23
|
store = /* @__PURE__ */ new Map();
|
|
23
24
|
async read(id) {
|
|
24
25
|
const entry = this.store.get(id);
|
|
@@ -65,6 +66,7 @@ var SessionImpl = class {
|
|
|
65
66
|
};
|
|
66
67
|
}
|
|
67
68
|
}
|
|
69
|
+
isNew;
|
|
68
70
|
currentId;
|
|
69
71
|
originalId;
|
|
70
72
|
data;
|
|
@@ -319,6 +321,10 @@ var RequestAuthContext = class {
|
|
|
319
321
|
this.resolveSession = resolveSession;
|
|
320
322
|
this.resolveGuard = resolveGuard;
|
|
321
323
|
}
|
|
324
|
+
manager;
|
|
325
|
+
ctx;
|
|
326
|
+
resolveSession;
|
|
327
|
+
resolveGuard;
|
|
322
328
|
guardCache = /* @__PURE__ */ new Map();
|
|
323
329
|
guard(name) {
|
|
324
330
|
const key = name ?? this.manager.getDefaultGuard();
|
|
@@ -443,17 +449,23 @@ var BaseUserProvider = class {
|
|
|
443
449
|
};
|
|
444
450
|
|
|
445
451
|
// src/auth/providers/ModelUserProvider.ts
|
|
452
|
+
function credentialColumnSource(model) {
|
|
453
|
+
const candidate = model;
|
|
454
|
+
return typeof candidate.resolvePasswordHashField === "function" && typeof candidate.resolveRememberTokenField === "function" ? candidate : null;
|
|
455
|
+
}
|
|
446
456
|
var ModelUserProvider = class extends BaseUserProvider {
|
|
447
457
|
constructor(model, options = {}) {
|
|
448
458
|
super();
|
|
449
459
|
this.model = model;
|
|
460
|
+
const authModel = credentialColumnSource(model);
|
|
450
461
|
this.idColumn = options.idColumn ?? "id";
|
|
451
462
|
this.usernameColumn = options.usernameColumn ?? "email";
|
|
452
|
-
this.passwordColumn = options.passwordColumn ?? "
|
|
453
|
-
this.rememberTokenColumn = options.rememberTokenColumn ?? "
|
|
463
|
+
this.passwordColumn = options.passwordColumn ?? authModel?.resolvePasswordHashField() ?? "passwordHash";
|
|
464
|
+
this.rememberTokenColumn = options.rememberTokenColumn ?? authModel?.resolveRememberTokenField() ?? "rememberToken";
|
|
454
465
|
this.hasher = options.hasher ?? new ScryptHasher();
|
|
455
466
|
this.credentialsPasswordField = options.credentialsPasswordField ?? "password";
|
|
456
467
|
}
|
|
468
|
+
model;
|
|
457
469
|
idColumn;
|
|
458
470
|
usernameColumn;
|
|
459
471
|
passwordColumn;
|
|
@@ -505,9 +517,11 @@ var ModelUserProvider = class extends BaseUserProvider {
|
|
|
505
517
|
* never affects login — only what `auth.user()` exposes.
|
|
506
518
|
*/
|
|
507
519
|
sanitize(user) {
|
|
520
|
+
const authModel = credentialColumnSource(this.model);
|
|
508
521
|
const blocked = /* @__PURE__ */ new Set([
|
|
509
522
|
this.passwordColumn,
|
|
510
523
|
this.rememberTokenColumn,
|
|
524
|
+
...authModel ? [authModel.resolvePasswordHashField(), authModel.resolveRememberTokenField()] : [],
|
|
511
525
|
...this.model.hidden ?? []
|
|
512
526
|
]);
|
|
513
527
|
const clean = {};
|
|
@@ -586,6 +600,7 @@ var SessionGuard = class {
|
|
|
586
600
|
constructor(options) {
|
|
587
601
|
this.options = options;
|
|
588
602
|
}
|
|
603
|
+
options;
|
|
589
604
|
cachedUser;
|
|
590
605
|
get currentSession() {
|
|
591
606
|
return this.options.session;
|
|
@@ -801,8 +816,6 @@ var AuthManager = class {
|
|
|
801
816
|
useModel(model, options = {}, providerName = "users", guardName = "web") {
|
|
802
817
|
const defaultOptions = {
|
|
803
818
|
usernameColumn: "email",
|
|
804
|
-
passwordColumn: "passwordHash",
|
|
805
|
-
rememberTokenColumn: "rememberToken",
|
|
806
819
|
credentialsPasswordField: "password",
|
|
807
820
|
hasher: new ScryptHasher(),
|
|
808
821
|
...options
|
|
@@ -844,6 +857,7 @@ var AuthenticatableModel = class extends Model {
|
|
|
844
857
|
static createType = void 0;
|
|
845
858
|
static passwordField = "password";
|
|
846
859
|
static passwordHashField = "passwordHash";
|
|
860
|
+
static rememberTokenField = "rememberToken";
|
|
847
861
|
static passwordHasher = null;
|
|
848
862
|
static resolvePasswordField() {
|
|
849
863
|
return this.passwordField ?? "password";
|
|
@@ -851,6 +865,25 @@ var AuthenticatableModel = class extends Model {
|
|
|
851
865
|
static resolvePasswordHashField() {
|
|
852
866
|
return this.passwordHashField ?? "passwordHash";
|
|
853
867
|
}
|
|
868
|
+
static resolveRememberTokenField() {
|
|
869
|
+
return this.rememberTokenField ?? "rememberToken";
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Credential columns can never be mass-assigned: the resolved password-hash
|
|
873
|
+
* column (unless the model hashes in place into the password field itself)
|
|
874
|
+
* and the remember-token column. Resolved at call time so a renamed column
|
|
875
|
+
* stays covered. Use `forceCreate()`/`forceUpdate()` for trusted
|
|
876
|
+
* server-side values such as `passwordHash: 'oauth:...'`.
|
|
877
|
+
*/
|
|
878
|
+
static deniedFields() {
|
|
879
|
+
const denied = [...super.deniedFields()];
|
|
880
|
+
const hashField = this.resolvePasswordHashField();
|
|
881
|
+
if (hashField !== this.resolvePasswordField()) {
|
|
882
|
+
denied.push(hashField);
|
|
883
|
+
}
|
|
884
|
+
denied.push(this.resolveRememberTokenField());
|
|
885
|
+
return denied;
|
|
886
|
+
}
|
|
854
887
|
static resolvePasswordHasher() {
|
|
855
888
|
if (this.passwordHasher) {
|
|
856
889
|
return this.passwordHasher;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
trimSlashes
|
|
3
|
+
} from "./chunk-AZOYI43J.js";
|
|
4
|
+
|
|
1
5
|
// src/storage/drivers/LocalDriver.ts
|
|
2
6
|
import {
|
|
3
7
|
readFile,
|
|
@@ -203,6 +207,7 @@ var S3Driver = class {
|
|
|
203
207
|
this.baseUrl = options.url ?? `https://${this.bucket}.s3.${this.region}.amazonaws.com`;
|
|
204
208
|
this.defaultVisibility = options.visibility ?? "private";
|
|
205
209
|
}
|
|
210
|
+
options;
|
|
206
211
|
client = null;
|
|
207
212
|
bucket;
|
|
208
213
|
region;
|
|
@@ -520,7 +525,7 @@ var MemoryDriver = class {
|
|
|
520
525
|
* Normalize path (remove leading/trailing slashes).
|
|
521
526
|
*/
|
|
522
527
|
normalizePath(path) {
|
|
523
|
-
return path
|
|
528
|
+
return trimSlashes(path);
|
|
524
529
|
}
|
|
525
530
|
/**
|
|
526
531
|
* Get directory from path.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLoopbackGuard
|
|
3
|
+
} from "./chunk-BZMZEFRG.js";
|
|
4
|
+
|
|
5
|
+
// src/mcp/endpoint.ts
|
|
6
|
+
var MCP_ENDPOINT_PATH = "/_guren/mcp";
|
|
7
|
+
function isMcpEndpointEnabled() {
|
|
8
|
+
return typeof process !== "undefined" && process.env?.NODE_ENV !== "production" && process.env?.GUREN_MCP === "1";
|
|
9
|
+
}
|
|
10
|
+
function createMcpAccessGuard() {
|
|
11
|
+
return createLoopbackGuard("the MCP endpoint");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
MCP_ENDPOINT_PATH,
|
|
16
|
+
isMcpEndpointEnabled,
|
|
17
|
+
createMcpAccessGuard
|
|
18
|
+
};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseMailAddress
|
|
3
|
+
} from "./chunk-2V3YID4H.js";
|
|
1
4
|
import {
|
|
2
5
|
Job,
|
|
3
6
|
getQueueDriver,
|
|
4
7
|
registerJob
|
|
5
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-6J6B5KOB.js";
|
|
6
9
|
|
|
7
10
|
// src/mail/transports/SmtpTransport.ts
|
|
8
11
|
import nodemailer from "nodemailer";
|
|
@@ -417,16 +420,6 @@ function createMailManager(config) {
|
|
|
417
420
|
}
|
|
418
421
|
|
|
419
422
|
// src/mail/Mail.ts
|
|
420
|
-
function parseAddress(input) {
|
|
421
|
-
if (typeof input === "string") {
|
|
422
|
-
const match = input.match(/^(.+)\s*<(.+)>$/);
|
|
423
|
-
if (match) {
|
|
424
|
-
return { name: match[1].trim(), email: match[2].trim() };
|
|
425
|
-
}
|
|
426
|
-
return { email: input };
|
|
427
|
-
}
|
|
428
|
-
return input;
|
|
429
|
-
}
|
|
430
423
|
var Mail = class {
|
|
431
424
|
constructor(manager) {
|
|
432
425
|
this.manager = manager;
|
|
@@ -435,6 +428,7 @@ var Mail = class {
|
|
|
435
428
|
this.message.from = defaultFrom;
|
|
436
429
|
}
|
|
437
430
|
}
|
|
431
|
+
manager;
|
|
438
432
|
message = {
|
|
439
433
|
to: [],
|
|
440
434
|
cc: [],
|
|
@@ -446,14 +440,14 @@ var Mail = class {
|
|
|
446
440
|
* Set the sender address.
|
|
447
441
|
*/
|
|
448
442
|
from(address) {
|
|
449
|
-
this.message.from =
|
|
443
|
+
this.message.from = parseMailAddress(address);
|
|
450
444
|
return this;
|
|
451
445
|
}
|
|
452
446
|
/**
|
|
453
447
|
* Add a recipient.
|
|
454
448
|
*/
|
|
455
449
|
to(address) {
|
|
456
|
-
this.message.to.push(
|
|
450
|
+
this.message.to.push(parseMailAddress(address));
|
|
457
451
|
return this;
|
|
458
452
|
}
|
|
459
453
|
/**
|
|
@@ -469,21 +463,21 @@ var Mail = class {
|
|
|
469
463
|
* Add a CC recipient.
|
|
470
464
|
*/
|
|
471
465
|
cc(address) {
|
|
472
|
-
this.message.cc.push(
|
|
466
|
+
this.message.cc.push(parseMailAddress(address));
|
|
473
467
|
return this;
|
|
474
468
|
}
|
|
475
469
|
/**
|
|
476
470
|
* Add a BCC recipient.
|
|
477
471
|
*/
|
|
478
472
|
bcc(address) {
|
|
479
|
-
this.message.bcc.push(
|
|
473
|
+
this.message.bcc.push(parseMailAddress(address));
|
|
480
474
|
return this;
|
|
481
475
|
}
|
|
482
476
|
/**
|
|
483
477
|
* Set the reply-to address.
|
|
484
478
|
*/
|
|
485
479
|
replyTo(address) {
|
|
486
|
-
this.message.replyTo =
|
|
480
|
+
this.message.replyTo = parseMailAddress(address);
|
|
487
481
|
return this;
|
|
488
482
|
}
|
|
489
483
|
/**
|
|
@@ -614,6 +608,7 @@ function getMailManager() {
|
|
|
614
608
|
return globalMailManager;
|
|
615
609
|
}
|
|
616
610
|
var SendMailJob = class extends Job {
|
|
611
|
+
static jobName = "SendMailJob";
|
|
617
612
|
static queue = "default";
|
|
618
613
|
static maxAttempts = 3;
|
|
619
614
|
static backoff = "exponential";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getJob,
|
|
3
3
|
setQueueDriver
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-6J6B5KOB.js";
|
|
5
5
|
|
|
6
6
|
// src/queue/drivers/MemoryDriver.ts
|
|
7
7
|
var MemoryDriver = class {
|
|
@@ -139,7 +139,7 @@ var SyncDriver = class {
|
|
|
139
139
|
const JobClass = getJob(job.name);
|
|
140
140
|
if (!JobClass) {
|
|
141
141
|
throw new Error(
|
|
142
|
-
`SyncDriver: job class "${job.name}" is not registered. Call registerJob(${job.name}
|
|
142
|
+
`SyncDriver: job class "${job.name}" is not registered. Call registerJob() with the class whose jobName (or class name) is "${job.name}".`
|
|
143
143
|
);
|
|
144
144
|
}
|
|
145
145
|
job.attempts += 1;
|
|
@@ -198,6 +198,7 @@ var RedisDriver = class {
|
|
|
198
198
|
this.prefix = options.prefix ?? "queue:";
|
|
199
199
|
this.visibilityTimeout = options.visibilityTimeout ?? 6e4;
|
|
200
200
|
}
|
|
201
|
+
redis;
|
|
201
202
|
prefix;
|
|
202
203
|
visibilityTimeout;
|
|
203
204
|
pendingKey(queue) {
|
|
@@ -623,6 +624,9 @@ var Worker = class {
|
|
|
623
624
|
this.timeout = options.timeout ?? 6e4;
|
|
624
625
|
this.stopWhenEmpty = options.stopWhenEmpty ?? false;
|
|
625
626
|
}
|
|
627
|
+
driver;
|
|
628
|
+
options;
|
|
629
|
+
events;
|
|
626
630
|
running = false;
|
|
627
631
|
shouldStop = false;
|
|
628
632
|
processedJobs = 0;
|
|
@@ -257,6 +257,9 @@ var RequestReceived = class extends Event {
|
|
|
257
257
|
this.path = path;
|
|
258
258
|
this.requestId = requestId;
|
|
259
259
|
}
|
|
260
|
+
method;
|
|
261
|
+
path;
|
|
262
|
+
requestId;
|
|
260
263
|
};
|
|
261
264
|
var RequestFinished = class extends Event {
|
|
262
265
|
constructor(method, path, status, durationMs, requestId) {
|
|
@@ -267,6 +270,11 @@ var RequestFinished = class extends Event {
|
|
|
267
270
|
this.durationMs = durationMs;
|
|
268
271
|
this.requestId = requestId;
|
|
269
272
|
}
|
|
273
|
+
method;
|
|
274
|
+
path;
|
|
275
|
+
status;
|
|
276
|
+
durationMs;
|
|
277
|
+
requestId;
|
|
270
278
|
};
|
|
271
279
|
var UserAuthenticated = class extends Event {
|
|
272
280
|
constructor(userId, guard = "default") {
|
|
@@ -274,6 +282,8 @@ var UserAuthenticated = class extends Event {
|
|
|
274
282
|
this.userId = userId;
|
|
275
283
|
this.guard = guard;
|
|
276
284
|
}
|
|
285
|
+
userId;
|
|
286
|
+
guard;
|
|
277
287
|
};
|
|
278
288
|
var UserLoggedOut = class extends Event {
|
|
279
289
|
constructor(userId, guard = "default") {
|
|
@@ -281,6 +291,8 @@ var UserLoggedOut = class extends Event {
|
|
|
281
291
|
this.userId = userId;
|
|
282
292
|
this.guard = guard;
|
|
283
293
|
}
|
|
294
|
+
userId;
|
|
295
|
+
guard;
|
|
284
296
|
};
|
|
285
297
|
var JobProcessed = class extends Event {
|
|
286
298
|
constructor(jobId, jobName, queue, durationMs) {
|
|
@@ -290,6 +302,10 @@ var JobProcessed = class extends Event {
|
|
|
290
302
|
this.queue = queue;
|
|
291
303
|
this.durationMs = durationMs;
|
|
292
304
|
}
|
|
305
|
+
jobId;
|
|
306
|
+
jobName;
|
|
307
|
+
queue;
|
|
308
|
+
durationMs;
|
|
293
309
|
};
|
|
294
310
|
var JobFailed = class extends Event {
|
|
295
311
|
constructor(jobId, jobName, queue, error, attempts) {
|
|
@@ -300,6 +316,11 @@ var JobFailed = class extends Event {
|
|
|
300
316
|
this.error = error;
|
|
301
317
|
this.attempts = attempts;
|
|
302
318
|
}
|
|
319
|
+
jobId;
|
|
320
|
+
jobName;
|
|
321
|
+
queue;
|
|
322
|
+
error;
|
|
323
|
+
attempts;
|
|
303
324
|
};
|
|
304
325
|
var ApplicationStarted = class extends Event {
|
|
305
326
|
constructor(port, host) {
|
|
@@ -307,12 +328,15 @@ var ApplicationStarted = class extends Event {
|
|
|
307
328
|
this.port = port;
|
|
308
329
|
this.host = host;
|
|
309
330
|
}
|
|
331
|
+
port;
|
|
332
|
+
host;
|
|
310
333
|
};
|
|
311
334
|
var ApplicationShutdown = class extends Event {
|
|
312
335
|
constructor(reason) {
|
|
313
336
|
super();
|
|
314
337
|
this.reason = reason;
|
|
315
338
|
}
|
|
339
|
+
reason;
|
|
316
340
|
};
|
|
317
341
|
|
|
318
342
|
export {
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
MessageSigner,
|
|
3
3
|
generateAppKey,
|
|
4
4
|
normalizeAppKey
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-SFQP6O52.js";
|
|
6
6
|
|
|
7
7
|
// src/encryption/Encrypter.ts
|
|
8
8
|
import { createCipheriv, createDecipheriv, randomBytes, createHmac } from "crypto";
|
|
@@ -264,16 +264,28 @@ var CHARSETS = {
|
|
|
264
264
|
"base64": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
|
265
265
|
"url-safe": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
266
266
|
};
|
|
267
|
-
function
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
267
|
+
function unbiasedRandomChars(length, chars) {
|
|
268
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
269
|
+
throw new RangeError(`Random string length must be a non-negative integer, received ${length}.`);
|
|
270
|
+
}
|
|
271
|
+
const limit = 256 - 256 % chars.length;
|
|
271
272
|
let result = "";
|
|
272
|
-
|
|
273
|
-
|
|
273
|
+
while (result.length < length) {
|
|
274
|
+
const remaining = length - result.length;
|
|
275
|
+
const bytes = randomBytes2(Math.ceil(remaining * 256 / limit) + 8);
|
|
276
|
+
for (const byte of bytes) {
|
|
277
|
+
if (byte < limit) {
|
|
278
|
+
result += chars[byte % chars.length];
|
|
279
|
+
if (result.length === length) break;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
274
282
|
}
|
|
275
283
|
return result;
|
|
276
284
|
}
|
|
285
|
+
function randomString(length, options = {}) {
|
|
286
|
+
const { charset = "alphanumeric" } = options;
|
|
287
|
+
return unbiasedRandomChars(length, CHARSETS[charset]);
|
|
288
|
+
}
|
|
277
289
|
function random(length) {
|
|
278
290
|
return randomBytes2(length);
|
|
279
291
|
}
|
|
@@ -310,12 +322,7 @@ function generatePassword(length = 16, options = {}) {
|
|
|
310
322
|
if (chars.length === 0) {
|
|
311
323
|
chars = CHARSETS.alphanumeric;
|
|
312
324
|
}
|
|
313
|
-
|
|
314
|
-
let result = "";
|
|
315
|
-
for (let i = 0; i < length; i++) {
|
|
316
|
-
result += chars[bytes[i] % chars.length];
|
|
317
|
-
}
|
|
318
|
-
return result;
|
|
325
|
+
return unbiasedRandomChars(length, chars);
|
|
319
326
|
}
|
|
320
327
|
function generateOtp(length = 6) {
|
|
321
328
|
return randomString(length, { charset: "numeric" });
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseMailAddress
|
|
3
|
+
} from "./chunk-2V3YID4H.js";
|
|
1
4
|
import {
|
|
2
5
|
Job,
|
|
3
6
|
registerJob
|
|
4
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-6J6B5KOB.js";
|
|
5
8
|
|
|
6
9
|
// src/notifications/Notification.ts
|
|
7
10
|
var Notification = class {
|
|
@@ -62,6 +65,36 @@ var Notification = class {
|
|
|
62
65
|
}
|
|
63
66
|
};
|
|
64
67
|
|
|
68
|
+
// src/notifications/registry.ts
|
|
69
|
+
var notificationRegistry = /* @__PURE__ */ new Map();
|
|
70
|
+
function resolveRegistryKey(notificationClass) {
|
|
71
|
+
try {
|
|
72
|
+
const { type } = Object.create(notificationClass.prototype);
|
|
73
|
+
if (typeof type === "string" && type.length > 0) {
|
|
74
|
+
return type;
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
}
|
|
78
|
+
return notificationClass.name;
|
|
79
|
+
}
|
|
80
|
+
function registerNotification(notificationClass, type) {
|
|
81
|
+
notificationRegistry.set(
|
|
82
|
+
type ?? resolveRegistryKey(notificationClass),
|
|
83
|
+
notificationClass
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
function getNotification(type) {
|
|
87
|
+
return notificationRegistry.get(type);
|
|
88
|
+
}
|
|
89
|
+
function clearNotificationRegistry() {
|
|
90
|
+
notificationRegistry.clear();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/notifications/notifiable-type.ts
|
|
94
|
+
function resolveNotifiableType(notifiable) {
|
|
95
|
+
return notifiable.notifiableType ?? notifiable.constructor?.name ?? "Unknown";
|
|
96
|
+
}
|
|
97
|
+
|
|
65
98
|
// src/notifications/NotificationManager.ts
|
|
66
99
|
var NotificationManager = class {
|
|
67
100
|
channels = /* @__PURE__ */ new Map();
|
|
@@ -173,10 +206,16 @@ var NotificationManager = class {
|
|
|
173
206
|
* Queue a notification for later delivery.
|
|
174
207
|
*/
|
|
175
208
|
async queue(notifiable, notification, queueConfig) {
|
|
176
|
-
const job = this.
|
|
177
|
-
|
|
209
|
+
const job = this.registerQueueJob();
|
|
210
|
+
registerNotification(
|
|
211
|
+
notification.constructor,
|
|
212
|
+
notification.type
|
|
213
|
+
);
|
|
178
214
|
const payload = {
|
|
179
|
-
notifiableData: this.serializeNotifiable(
|
|
215
|
+
notifiableData: this.serializeNotifiable(
|
|
216
|
+
notifiable,
|
|
217
|
+
notification.via(notifiable)
|
|
218
|
+
),
|
|
180
219
|
notificationData: this.serializeNotification(notification),
|
|
181
220
|
notificationType: notification.type
|
|
182
221
|
};
|
|
@@ -206,32 +245,47 @@ var NotificationManager = class {
|
|
|
206
245
|
}
|
|
207
246
|
/**
|
|
208
247
|
* Serialize notifiable for queue storage.
|
|
209
|
-
|
|
210
|
-
|
|
248
|
+
*
|
|
249
|
+
* Routing is resolved here rather than in the worker: `routeNotificationFor`
|
|
250
|
+
* is arbitrary user code — often a closure on an object literal — and cannot
|
|
251
|
+
* be reconstructed from a payload.
|
|
252
|
+
*
|
|
253
|
+
* @param channels - Channels to resolve routes for
|
|
254
|
+
*/
|
|
255
|
+
serializeNotifiable(notifiable, channels = []) {
|
|
256
|
+
const routes = {};
|
|
257
|
+
for (const channel of channels) {
|
|
258
|
+
routes[channel] = notifiable.routeNotificationFor(channel);
|
|
259
|
+
}
|
|
211
260
|
return {
|
|
212
|
-
type: notifiable
|
|
261
|
+
type: resolveNotifiableType(notifiable),
|
|
262
|
+
routes,
|
|
213
263
|
data: { ...notifiable }
|
|
214
264
|
};
|
|
215
265
|
}
|
|
216
266
|
/**
|
|
217
267
|
* Serialize notification for queue storage.
|
|
268
|
+
*
|
|
269
|
+
* Only own enumerable properties are captured. Behaviour is restored by
|
|
270
|
+
* rebuilding the registered class in the job handler, not by serializing it.
|
|
218
271
|
*/
|
|
219
272
|
serializeNotification(notification) {
|
|
220
273
|
return {
|
|
221
|
-
type: notification.type,
|
|
222
274
|
id: notification.id,
|
|
275
|
+
createdAt: notification.createdAt.toISOString(),
|
|
223
276
|
data: { ...notification }
|
|
224
277
|
};
|
|
225
278
|
}
|
|
226
279
|
/**
|
|
227
|
-
*
|
|
280
|
+
* Bind this manager to the queued-notification job and register it.
|
|
281
|
+
*
|
|
282
|
+
* Call this during boot in a process that runs a worker but may never send
|
|
283
|
+
* a notification itself: without it, the worker cannot resolve the job.
|
|
228
284
|
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
return BoundSendNotificationJob;
|
|
285
|
+
registerQueueJob() {
|
|
286
|
+
SendNotificationJob.notificationManager = this;
|
|
287
|
+
registerJob(SendNotificationJob);
|
|
288
|
+
return SendNotificationJob;
|
|
235
289
|
}
|
|
236
290
|
};
|
|
237
291
|
var SendNotificationJob = class extends Job {
|
|
@@ -245,33 +299,54 @@ var SendNotificationJob = class extends Job {
|
|
|
245
299
|
if (!manager) {
|
|
246
300
|
throw new Error("NotificationManager not set on SendNotificationJob");
|
|
247
301
|
}
|
|
248
|
-
const notifiable =
|
|
249
|
-
|
|
302
|
+
const notifiable = this.rebuildNotifiable(payload.notifiableData);
|
|
303
|
+
const notification = this.rebuildNotification(
|
|
304
|
+
payload.notificationType,
|
|
305
|
+
payload.notificationData
|
|
306
|
+
);
|
|
307
|
+
await manager.sendNow(notifiable, notification);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Rebuild the notifiable from its serialized data.
|
|
311
|
+
*/
|
|
312
|
+
rebuildNotifiable(serialized) {
|
|
313
|
+
const { data, routes } = serialized;
|
|
314
|
+
return {
|
|
315
|
+
...data,
|
|
316
|
+
notifiableType: serialized.type,
|
|
250
317
|
routeNotificationFor(channel) {
|
|
251
|
-
|
|
252
|
-
|
|
318
|
+
if (routes && channel in routes) {
|
|
319
|
+
return routes[channel] ?? null;
|
|
320
|
+
}
|
|
321
|
+
const value = data[`${channel}Route`];
|
|
253
322
|
if (typeof value === "string") return value;
|
|
254
323
|
if (channel === "mail") {
|
|
255
|
-
const email =
|
|
324
|
+
const email = data["email"];
|
|
256
325
|
return typeof email === "string" ? email : null;
|
|
257
326
|
}
|
|
258
327
|
return null;
|
|
259
328
|
}
|
|
260
329
|
};
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Rebuild a real notification instance from its serialized data.
|
|
333
|
+
*
|
|
334
|
+
* The class is looked up in the notification registry and instantiated via
|
|
335
|
+
* its prototype, so prototype methods (`via`, `toMail`, `toDatabase`,
|
|
336
|
+
* `toSlack`, `shouldSend`) are restored without running the constructor.
|
|
337
|
+
*/
|
|
338
|
+
rebuildNotification(type, serialized) {
|
|
339
|
+
const NotificationClass = getNotification(type);
|
|
340
|
+
if (!NotificationClass) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
`Notification type "${type}" is not registered. Call registerNotification(${type}) before running the queue worker.`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return Object.assign(
|
|
346
|
+
Object.create(NotificationClass.prototype),
|
|
347
|
+
serialized.data,
|
|
348
|
+
{ id: serialized.id, createdAt: new Date(serialized.createdAt) }
|
|
349
|
+
);
|
|
275
350
|
}
|
|
276
351
|
};
|
|
277
352
|
var globalNotificationManager = null;
|
|
@@ -293,14 +368,7 @@ function parseAddress(input) {
|
|
|
293
368
|
if (!input) {
|
|
294
369
|
return void 0;
|
|
295
370
|
}
|
|
296
|
-
|
|
297
|
-
return input;
|
|
298
|
-
}
|
|
299
|
-
const match = input.match(/^(.+)\s*<(.+)>$/);
|
|
300
|
-
if (match) {
|
|
301
|
-
return { name: match[1].trim(), email: match[2].trim() };
|
|
302
|
-
}
|
|
303
|
-
return { email: input };
|
|
371
|
+
return parseMailAddress(input);
|
|
304
372
|
}
|
|
305
373
|
function parseAddressList(input) {
|
|
306
374
|
if (!input) {
|
|
@@ -315,6 +383,8 @@ var MailChannel = class {
|
|
|
315
383
|
this.mailManager = mailManager;
|
|
316
384
|
this.options = options;
|
|
317
385
|
}
|
|
386
|
+
mailManager;
|
|
387
|
+
options;
|
|
318
388
|
name = "mail";
|
|
319
389
|
/**
|
|
320
390
|
* Send the notification via mail.
|
|
@@ -357,6 +427,7 @@ var DatabaseChannel = class {
|
|
|
357
427
|
constructor(options = {}) {
|
|
358
428
|
this.options = options;
|
|
359
429
|
}
|
|
430
|
+
options;
|
|
360
431
|
name = "database";
|
|
361
432
|
stored = [];
|
|
362
433
|
/**
|
|
@@ -399,7 +470,7 @@ var DatabaseChannel = class {
|
|
|
399
470
|
* Get the notifiable type.
|
|
400
471
|
*/
|
|
401
472
|
getNotifiableType(notifiable) {
|
|
402
|
-
return notifiable
|
|
473
|
+
return resolveNotifiableType(notifiable);
|
|
403
474
|
}
|
|
404
475
|
/**
|
|
405
476
|
* Get all stored notifications (for testing).
|
|
@@ -431,6 +502,8 @@ var SlackChannel = class {
|
|
|
431
502
|
this.webhookUrl = webhookUrl;
|
|
432
503
|
this.options = options;
|
|
433
504
|
}
|
|
505
|
+
webhookUrl;
|
|
506
|
+
options;
|
|
434
507
|
name = "slack";
|
|
435
508
|
/**
|
|
436
509
|
* Send the notification to Slack.
|
|
@@ -641,6 +714,10 @@ var MemoryChannel = class {
|
|
|
641
714
|
|
|
642
715
|
export {
|
|
643
716
|
Notification,
|
|
717
|
+
registerNotification,
|
|
718
|
+
getNotification,
|
|
719
|
+
clearNotificationRegistry,
|
|
720
|
+
resolveNotifiableType,
|
|
644
721
|
NotificationManager,
|
|
645
722
|
setNotificationManager,
|
|
646
723
|
getNotificationManager,
|