@web-ts-toolkit/email-service 0.72.0 → 0.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -10
- package/index.d.mts +15 -7
- package/index.d.ts +15 -7
- package/index.js +479 -246
- package/index.mjs +479 -246
- package/mongoose.d.mts +7 -4
- package/mongoose.d.ts +7 -4
- package/mongoose.js +546 -280
- package/mongoose.mjs +546 -280
- package/package.json +1 -1
- package/{provider-HisBc6hk.d.mts → provider-DC_av0SQ.d.mts} +17 -1
- package/{provider-HisBc6hk.d.ts → provider-DC_av0SQ.d.ts} +17 -1
- package/sendgrid.d.mts +1 -1
- package/sendgrid.d.ts +1 -1
- package/sendgrid.js +116 -32
- package/sendgrid.mjs +116 -32
- package/smtp.d.mts +3 -1
- package/smtp.d.ts +3 -1
- package/smtp.js +138 -38
- package/smtp.mjs +138 -38
- package/{store-D4YAI6Rk.d.mts → store-Bi5iZ910.d.ts} +18 -4
- package/{store-Vb63Gocz.d.ts → store-DlABKTjt.d.mts} +18 -4
package/mongoose.mjs
CHANGED
|
@@ -251,6 +251,12 @@ function scopeToken(value, code = "invalid_command") {
|
|
|
251
251
|
function identifier(value, code) {
|
|
252
252
|
return opaqueToken(value, MESSAGE_LIMITS.identifierBytes, code);
|
|
253
253
|
}
|
|
254
|
+
function uuidV4(value, code) {
|
|
255
|
+
if (typeof value !== "string" || value.length !== 36 || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value)) {
|
|
256
|
+
invalid(code);
|
|
257
|
+
}
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
254
260
|
function hashToken(value, code) {
|
|
255
261
|
if (typeof value !== "string" || value.length !== 64 || !/^[0-9a-f]{64}$/.test(value)) invalid(code);
|
|
256
262
|
return value;
|
|
@@ -264,9 +270,9 @@ function jsonKey(key, code) {
|
|
|
264
270
|
|
|
265
271
|
// src/internal/canonical-json.ts
|
|
266
272
|
var treeLimits = { depth: MESSAGE_LIMITS.jsonDepth, values: MESSAGE_LIMITS.jsonValues };
|
|
267
|
-
function canonicalJson(input, maxBytes, code = "invalid_snapshot",
|
|
273
|
+
function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits2 = treeLimits) {
|
|
268
274
|
let bytesLeft = maxBytes;
|
|
269
|
-
let valuesLeft =
|
|
275
|
+
let valuesLeft = limits2.values;
|
|
270
276
|
const chunks = [];
|
|
271
277
|
const ancestors = /* @__PURE__ */ new Set();
|
|
272
278
|
function append(text, bytes = text.length) {
|
|
@@ -285,7 +291,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
285
291
|
append(JSON.stringify(text), bytes);
|
|
286
292
|
}
|
|
287
293
|
function visit(value2, depth) {
|
|
288
|
-
if (depth >
|
|
294
|
+
if (depth > limits2.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
|
|
289
295
|
switch (typeof value2) {
|
|
290
296
|
case "string":
|
|
291
297
|
appendString(value2);
|
|
@@ -309,7 +315,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
309
315
|
if (ancestors.has(value2)) invalid(code);
|
|
310
316
|
ancestors.add(value2);
|
|
311
317
|
try {
|
|
312
|
-
const maxChildren = depth ===
|
|
318
|
+
const maxChildren = depth === limits2.depth ? 0 : valuesLeft;
|
|
313
319
|
if (isArray(value2, code)) {
|
|
314
320
|
const length = denseArrayLength(value2, maxChildren, code);
|
|
315
321
|
if (length && 2 * length + 1 > bytesLeft) invalid(code);
|
|
@@ -352,6 +358,71 @@ function sha256(input) {
|
|
|
352
358
|
function hashJson(input, maxBytes, code = "invalid_snapshot") {
|
|
353
359
|
return sha256(canonicalJson(input, maxBytes, code).encoded);
|
|
354
360
|
}
|
|
361
|
+
function encodeEvidence(input, code, capacity) {
|
|
362
|
+
return canonicalJson(input, capacity?.limits.evidenceBytes ?? 8192, code, capacity?.limits.evidenceTree).encoded;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// src/internal/smtp-capacity.ts
|
|
366
|
+
var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
|
|
367
|
+
var MAX_RECIPIENTS = 1e3;
|
|
368
|
+
var KIB = 1024;
|
|
369
|
+
var limits = Object.freeze({
|
|
370
|
+
snapshotBytes: 2 * KIB * KIB,
|
|
371
|
+
snapshotTree: Object.freeze({ depth: 4, values: 4096 }),
|
|
372
|
+
evidenceBytes: 64 * KIB,
|
|
373
|
+
evidenceTree: Object.freeze({ depth: 5, values: 4096 }),
|
|
374
|
+
jobBsonBytes: 4 * KIB * KIB,
|
|
375
|
+
activityBsonBytes: 128 * KIB,
|
|
376
|
+
registryCache: Object.freeze({ min: 0, default: 8, max: 16 }),
|
|
377
|
+
registryPending: Object.freeze({ min: 1, default: 2, max: 4 }),
|
|
378
|
+
servicePending: 4,
|
|
379
|
+
workerProfiles: 16,
|
|
380
|
+
workerConcurrency: Object.freeze({ min: 1, default: 1, max: 2 }),
|
|
381
|
+
runClaims: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
382
|
+
runMaintenance: Object.freeze({ min: 0, default: 4, max: 16 }),
|
|
383
|
+
adminMaintenance: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
384
|
+
page: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
385
|
+
memoryJobs: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
386
|
+
mongoInFlight: 4,
|
|
387
|
+
dueCandidates: 4
|
|
388
|
+
});
|
|
389
|
+
function readBinding(input, code) {
|
|
390
|
+
const { format, namespace, maxRecipients } = readRecord(input, ["format", "namespace", "maxRecipients"], [], code);
|
|
391
|
+
if (format !== "smtp-v2") invalid(code);
|
|
392
|
+
return Object.freeze({
|
|
393
|
+
format,
|
|
394
|
+
namespace: uuidV4(namespace, code),
|
|
395
|
+
maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_RECIPIENTS, code)
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function readCapacityOption(raw) {
|
|
399
|
+
if (!Object.hasOwn(raw, "smtpCapacity")) return;
|
|
400
|
+
return Object.freeze({ binding: readBinding(raw.smtpCapacity, "invalid_config"), limits });
|
|
401
|
+
}
|
|
402
|
+
function capacityTag(capacity) {
|
|
403
|
+
return capacity ? { smtpCapacity: capacity.binding } : {};
|
|
404
|
+
}
|
|
405
|
+
function recipientLimit(capacity) {
|
|
406
|
+
return capacity?.binding.maxRecipients ?? MESSAGE_LIMITS.recipients;
|
|
407
|
+
}
|
|
408
|
+
function matchesBinding(input, capacity, code) {
|
|
409
|
+
const binding = readBinding(input, code);
|
|
410
|
+
if (binding.namespace !== capacity.binding.namespace || binding.maxRecipients !== capacity.binding.maxRecipients) {
|
|
411
|
+
invalid(code);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
function assertCapacityTag(raw, capacity, code) {
|
|
415
|
+
const tagged = Object.hasOwn(raw, "smtpCapacity");
|
|
416
|
+
if (capacity) {
|
|
417
|
+
if (!tagged) invalid(code);
|
|
418
|
+
matchesBinding(raw.smtpCapacity, capacity, code);
|
|
419
|
+
} else if (tagged) {
|
|
420
|
+
invalid(code);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function assertCapacityProvider(provider2, capacity, code) {
|
|
424
|
+
if (capacity && provider2.kind !== "smtp") invalid(code);
|
|
425
|
+
}
|
|
355
426
|
|
|
356
427
|
// src/internal/attempt-codecs.ts
|
|
357
428
|
var smtpStages = ["connect", "tls", "auth", "envelope", "data"];
|
|
@@ -391,9 +462,19 @@ function readAttemptReason(value, code) {
|
|
|
391
462
|
if (typeof value !== "string" || !Object.hasOwn(evidence, value)) invalid(code);
|
|
392
463
|
return value;
|
|
393
464
|
}
|
|
465
|
+
var recipientGroups = ["to", "cc", "bcc"];
|
|
394
466
|
function recipientCounts(envelope) {
|
|
395
467
|
return Object.freeze({ to: envelope.to.length, cc: envelope.cc.length, bcc: envelope.bcc.length });
|
|
396
468
|
}
|
|
469
|
+
function readRecipientCounts(input, code, capacity) {
|
|
470
|
+
const limit = recipientLimit(capacity);
|
|
471
|
+
const counts = readRecord(input, recipientGroups, [], code);
|
|
472
|
+
const to = integer(counts.to, 0, limit, code);
|
|
473
|
+
const cc = integer(counts.cc, 0, limit - to, code);
|
|
474
|
+
const bcc = integer(counts.bcc, 0, limit - to - cc, code);
|
|
475
|
+
if (to + cc + bcc === 0) invalid(code);
|
|
476
|
+
return Object.freeze({ to, cc, bcc });
|
|
477
|
+
}
|
|
397
478
|
function smtpMessageId(attemptId) {
|
|
398
479
|
return `<${attemptId}@email.invalid>`;
|
|
399
480
|
}
|
|
@@ -410,7 +491,7 @@ function readHint(input, reason, code) {
|
|
|
410
491
|
...hasFloor ? { notBefore: unixMs(raw.notBefore, code) } : {}
|
|
411
492
|
});
|
|
412
493
|
}
|
|
413
|
-
function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
494
|
+
function readAttemptResult(input, code = "invalid_command", projected = false, capacity) {
|
|
414
495
|
const raw = readRecord(
|
|
415
496
|
input,
|
|
416
497
|
["outcome", "retry", "stage", "reason"],
|
|
@@ -421,6 +502,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
421
502
|
const [outcome, retry, stages, origin] = evidence[reason];
|
|
422
503
|
const stage = stages.find((value) => value === raw.stage);
|
|
423
504
|
if (raw.outcome !== outcome || raw.retry !== retry || !stage) invalid(code);
|
|
505
|
+
if (capacity && (origin === "sendgrid" || stage === "http")) invalid(code);
|
|
424
506
|
const hasHint = Object.hasOwn(raw, "retryHint");
|
|
425
507
|
if (projected && hasHint || !projected && reason === "rate_deferred" && !hasHint) invalid(code);
|
|
426
508
|
const smtpSuccess = reason === "smtp_accepted" || reason === "smtp_partial";
|
|
@@ -430,14 +512,22 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
430
512
|
if (hasRecipients) {
|
|
431
513
|
if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
|
|
432
514
|
const seen = /* @__PURE__ */ new Set();
|
|
433
|
-
|
|
515
|
+
const limit = recipientLimit(capacity);
|
|
516
|
+
let previous = -1;
|
|
517
|
+
recipients = readArray(raw.recipients, limit, code, (value) => {
|
|
434
518
|
const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
|
|
435
519
|
if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
|
|
436
520
|
if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
|
|
437
|
-
const index = integer(rawIndex, 0,
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
521
|
+
const index = integer(rawIndex, 0, limit - 1, code);
|
|
522
|
+
if (capacity) {
|
|
523
|
+
const position = recipientGroups.indexOf(group) * limit + index;
|
|
524
|
+
if (position <= previous) invalid(code);
|
|
525
|
+
previous = position;
|
|
526
|
+
} else {
|
|
527
|
+
const key = `${group}:${index}`;
|
|
528
|
+
if (seen.has(key)) invalid(code);
|
|
529
|
+
seen.add(key);
|
|
530
|
+
}
|
|
441
531
|
return Object.freeze({ group, index, status });
|
|
442
532
|
});
|
|
443
533
|
const accepted = recipients.filter(({ status }) => status === "accepted").length;
|
|
@@ -452,7 +542,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
452
542
|
if (hasProviderId && (reason !== "sendgrid_accepted" || typeof providerMessageId !== "string" || providerMessageId.length > 128 || !/^[A-Za-z0-9][A-Za-z0-9._=-]{0,127}$/.test(providerMessageId))) {
|
|
453
543
|
invalid(code);
|
|
454
544
|
}
|
|
455
|
-
|
|
545
|
+
const result = Object.freeze({
|
|
456
546
|
outcome,
|
|
457
547
|
retry,
|
|
458
548
|
stage,
|
|
@@ -462,6 +552,8 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
462
552
|
...typeof messageId === "string" ? { messageId } : {},
|
|
463
553
|
...typeof providerMessageId === "string" ? { providerMessageId } : {}
|
|
464
554
|
});
|
|
555
|
+
if (capacity && !projected) encodeEvidence(result, code, capacity);
|
|
556
|
+
return result;
|
|
465
557
|
}
|
|
466
558
|
function assertEvidencePhase(reason, phase, source, code) {
|
|
467
559
|
const origin = evidence[reason][3];
|
|
@@ -472,8 +564,8 @@ function assertEvidencePhase(reason, phase, source, code) {
|
|
|
472
564
|
const allowed = phase === "pre_handoff" ? origin === "pre" : origin === "smtp" || origin === "sendgrid" || origin === "provider" || origin === "launch";
|
|
473
565
|
if (!allowed) invalid(code);
|
|
474
566
|
}
|
|
475
|
-
function assertEvidenceContext(
|
|
476
|
-
const { reason, stage, recipients, messageId } =
|
|
567
|
+
function assertEvidenceContext(result, provider2, attemptId, counts, code) {
|
|
568
|
+
const { reason, stage, recipients, messageId } = result;
|
|
477
569
|
const origin = evidence[reason][3];
|
|
478
570
|
if (origin === "smtp" && provider2.kind !== "smtp" || origin === "sendgrid" && provider2.kind !== "sendgrid" || origin === "provider" && (provider2.kind === "smtp" ? stage === "http" : stage !== "http" && stage !== "connect" && stage !== "tls") || reason === "sendgrid_accepted" && provider2.mode !== "live" || reason === "sendgrid_validated" && provider2.mode !== "sandbox" || messageId !== void 0 && messageId !== smtpMessageId(attemptId)) {
|
|
479
571
|
invalid(code);
|
|
@@ -491,19 +583,19 @@ function lifecycleEvidence(reason) {
|
|
|
491
583
|
const [outcome, retry] = evidence[reason];
|
|
492
584
|
return Object.freeze({ outcome, retry, reason, stage: "pre_handoff" });
|
|
493
585
|
}
|
|
494
|
-
function attemptEffect(
|
|
495
|
-
const { outcome, reason } =
|
|
586
|
+
function attemptEffect(result, attemptId, ordinal2, manual, policy, expiresAt, now) {
|
|
587
|
+
const { outcome, reason } = result;
|
|
496
588
|
if (outcome !== "unsubmitted" && outcome !== "rejected") return { disposition: outcome, reason };
|
|
497
589
|
if (reason === "cancelled" || reason === "expired") return { disposition: reason, reason };
|
|
498
590
|
const disposition = outcome === "unsubmitted" ? "failed" : "rejected";
|
|
499
|
-
if (
|
|
591
|
+
if (result.retry === "never") return { disposition, reason };
|
|
500
592
|
if (now >= expiresAt) return { disposition: "expired", reason: "expired" };
|
|
501
593
|
if (manual || ordinal2 >= policy.maxAttempts) return { disposition, reason: "attempts_exhausted" };
|
|
502
594
|
const cap = Math.min(policy.maxDelayMs, policy.baseDelayMs * 2 ** (ordinal2 - 1));
|
|
503
595
|
const half = Math.floor(cap / 2);
|
|
504
596
|
const jitter = half + Number.parseInt(sha256(attemptId).slice(0, 8), 16) % (cap - half + 1);
|
|
505
|
-
const delay = Math.max(jitter,
|
|
506
|
-
const retryAt = Math.max(now + Math.min(delay, MAX_UNIX_MS - now),
|
|
597
|
+
const delay = Math.max(jitter, result.retryHint?.delayMs ?? 0);
|
|
598
|
+
const retryAt = Math.max(now + Math.min(delay, MAX_UNIX_MS - now), result.retryHint?.notBefore ?? 0);
|
|
507
599
|
if (retryAt >= expiresAt) return { disposition: "expired", reason: "expired" };
|
|
508
600
|
return { disposition: "retry_scheduled", reason, retryAt };
|
|
509
601
|
}
|
|
@@ -547,10 +639,11 @@ function validateMailbox(input, code = SNAPSHOT) {
|
|
|
547
639
|
...Object.hasOwn(mailbox, "name") ? { name: headerString(mailbox.name, L.displayNameBytes, code) } : {}
|
|
548
640
|
});
|
|
549
641
|
}
|
|
550
|
-
function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
642
|
+
function validateEnvelope(input, kind, code = SNAPSHOT, capacity) {
|
|
643
|
+
const limit = recipientLimit(capacity);
|
|
551
644
|
const envelope = readRecord(input, ["from", "to", "cc", "bcc"], ["replyTo"], code);
|
|
552
645
|
const seen = /* @__PURE__ */ new Set();
|
|
553
|
-
const
|
|
646
|
+
const recipient = (raw) => {
|
|
554
647
|
const mailbox = validateMailbox(raw, code);
|
|
555
648
|
const address = mailbox.address.toLowerCase();
|
|
556
649
|
if (seen.has(address)) invalid(code);
|
|
@@ -558,9 +651,9 @@ function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
|
558
651
|
return mailbox;
|
|
559
652
|
};
|
|
560
653
|
const from = validateMailbox(envelope.from, code);
|
|
561
|
-
const to = readArray(envelope.to,
|
|
562
|
-
const cc = readArray(envelope.cc,
|
|
563
|
-
const bcc = readArray(envelope.bcc,
|
|
654
|
+
const to = readArray(envelope.to, limit, code, recipient);
|
|
655
|
+
const cc = readArray(envelope.cc, limit - to.length, code, recipient);
|
|
656
|
+
const bcc = readArray(envelope.bcc, limit - to.length - cc.length, code, recipient);
|
|
564
657
|
if (seen.size === 0 || kind === "sendgrid" && to.length === 0) invalid(code);
|
|
565
658
|
return Object.freeze({
|
|
566
659
|
from,
|
|
@@ -635,12 +728,17 @@ function validateExpiry(input, preparedAt, scheduleAt, code = SNAPSHOT) {
|
|
|
635
728
|
}
|
|
636
729
|
return expiresAt;
|
|
637
730
|
}
|
|
638
|
-
function hashSnapshot(snapshot) {
|
|
731
|
+
function hashSnapshot(snapshot, capacity) {
|
|
639
732
|
return sha256(
|
|
640
|
-
canonicalJson(
|
|
733
|
+
canonicalJson(
|
|
734
|
+
snapshot,
|
|
735
|
+
capacity?.limits.snapshotBytes ?? L.snapshotBytes,
|
|
736
|
+
SNAPSHOT,
|
|
737
|
+
capacity?.limits.snapshotTree ?? { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }
|
|
738
|
+
).encoded
|
|
641
739
|
);
|
|
642
740
|
}
|
|
643
|
-
function readSnapshot(input) {
|
|
741
|
+
function readSnapshot(input, capacity) {
|
|
644
742
|
const dto = readRecord(
|
|
645
743
|
input,
|
|
646
744
|
[
|
|
@@ -655,17 +753,19 @@ function readSnapshot(input) {
|
|
|
655
753
|
"attachments",
|
|
656
754
|
"correlation"
|
|
657
755
|
],
|
|
658
|
-
["subject", "html", "text", "templateId", "data"],
|
|
756
|
+
["subject", "html", "text", "templateId", "data", "smtpCapacity"],
|
|
659
757
|
SNAPSHOT
|
|
660
758
|
);
|
|
661
759
|
if (dto.version !== EMAIL_FORMAT_VERSION || dto.kind !== "smtp" && dto.kind !== "sendgrid") invalid(SNAPSHOT);
|
|
760
|
+
assertCapacityTag(dto, capacity, SNAPSHOT);
|
|
662
761
|
const provider2 = validateProviderRef(dto.provider);
|
|
663
762
|
if (provider2.kind !== dto.kind) invalid("provider_mismatch");
|
|
763
|
+
assertCapacityProvider(provider2, capacity, "provider_mismatch");
|
|
664
764
|
const preparedAt = unixMs(dto.preparedAt, SNAPSHOT);
|
|
665
765
|
const common = {
|
|
666
766
|
version: EMAIL_FORMAT_VERSION,
|
|
667
767
|
template: validateTemplateIdentity(dto.template),
|
|
668
|
-
envelope: validateEnvelope(dto.envelope, provider2.kind),
|
|
768
|
+
envelope: validateEnvelope(dto.envelope, provider2.kind, SNAPSHOT, capacity),
|
|
669
769
|
preparedAt,
|
|
670
770
|
expiresAt: validateExpiry(dto.expiresAt, preparedAt, null),
|
|
671
771
|
priority: integer(dto.priority, PRIORITY.min, PRIORITY.max, SNAPSHOT),
|
|
@@ -679,7 +779,7 @@ function readSnapshot(input) {
|
|
|
679
779
|
...Object.hasOwn(dto, "html") ? { html: dto.html } : {},
|
|
680
780
|
...Object.hasOwn(dto, "text") ? { text: dto.text } : {}
|
|
681
781
|
});
|
|
682
|
-
return Object.freeze({ ...common, kind: provider2.kind, provider: provider2, ...content });
|
|
782
|
+
return Object.freeze({ ...common, kind: provider2.kind, provider: provider2, ...capacityTag(capacity), ...content });
|
|
683
783
|
}
|
|
684
784
|
if (Object.hasOwn(dto, "subject") || Object.hasOwn(dto, "html") || Object.hasOwn(dto, "text")) invalid(SNAPSHOT);
|
|
685
785
|
return Object.freeze({
|
|
@@ -690,9 +790,9 @@ function readSnapshot(input) {
|
|
|
690
790
|
data: copyJsonObject(dto.data, L.dataBytes, SNAPSHOT)
|
|
691
791
|
});
|
|
692
792
|
}
|
|
693
|
-
function validateSnapshot(input) {
|
|
694
|
-
const snapshot = readSnapshot(input);
|
|
695
|
-
return { snapshot, snapshotHash: hashSnapshot(snapshot) };
|
|
793
|
+
function validateSnapshot(input, capacity) {
|
|
794
|
+
const snapshot = readSnapshot(input, capacity);
|
|
795
|
+
return { snapshot, snapshotHash: hashSnapshot(snapshot, capacity) };
|
|
696
796
|
}
|
|
697
797
|
|
|
698
798
|
// src/internal/budget.ts
|
|
@@ -817,42 +917,56 @@ function validateAdmissionKey(input, code = "invalid_command") {
|
|
|
817
917
|
function encodeAdmissionKey(key) {
|
|
818
918
|
return key.kind === "single" ? `s:${key.idempotencyKey}` : `b:${key.batchId}:${key.itemId}`;
|
|
819
919
|
}
|
|
820
|
-
function fingerprintFor(identity) {
|
|
920
|
+
function fingerprintFor(identity, capacity) {
|
|
821
921
|
const { scope: scope2, key, templateKey, provider: provider2, scheduleAt, intentHash } = identity;
|
|
822
922
|
return hashJson(
|
|
823
|
-
[
|
|
923
|
+
[
|
|
924
|
+
...capacity ? ["smtp-v2-admission", capacity.binding] : [],
|
|
925
|
+
EMAIL_FORMAT_VERSION,
|
|
926
|
+
scope2,
|
|
927
|
+
key,
|
|
928
|
+
templateKey,
|
|
929
|
+
provider2,
|
|
930
|
+
scheduleAt,
|
|
931
|
+
intentHash
|
|
932
|
+
],
|
|
824
933
|
MESSAGE_LIMITS.intentBytes
|
|
825
934
|
);
|
|
826
935
|
}
|
|
827
|
-
function validatePreparedCommand(scope2, input) {
|
|
936
|
+
function validatePreparedCommand(scope2, input, capacity) {
|
|
828
937
|
const expectedScope = scopeToken(scope2);
|
|
829
938
|
const code = "invalid_snapshot";
|
|
830
939
|
const dto = readRecord(
|
|
831
940
|
input,
|
|
832
941
|
["version", "scope", "key", "scheduleAt", "intentHash", "fingerprint", "snapshotHash", "snapshot"],
|
|
833
|
-
[],
|
|
942
|
+
["smtpCapacity"],
|
|
834
943
|
code
|
|
835
944
|
);
|
|
836
945
|
if (dto.version !== EMAIL_FORMAT_VERSION || scopeToken(dto.scope, code) !== expectedScope) invalid(code);
|
|
946
|
+
assertCapacityTag(dto, capacity, code);
|
|
837
947
|
const key = validateAdmissionKey(dto.key, code);
|
|
838
948
|
const scheduleAt = dto.scheduleAt === null ? null : unixMs(dto.scheduleAt, code);
|
|
839
949
|
const intentHash = hashToken(dto.intentHash, code);
|
|
840
950
|
const fingerprint = hashToken(dto.fingerprint, code);
|
|
841
951
|
const storedSnapshotHash = hashToken(dto.snapshotHash, code);
|
|
842
|
-
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot);
|
|
952
|
+
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot, capacity);
|
|
843
953
|
validateExpiry(snapshot.expiresAt, snapshot.preparedAt, scheduleAt);
|
|
844
|
-
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
954
|
+
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
955
|
+
{
|
|
956
|
+
scope: expectedScope,
|
|
957
|
+
key,
|
|
958
|
+
templateKey: snapshot.template.key,
|
|
959
|
+
provider: snapshot.provider,
|
|
960
|
+
scheduleAt,
|
|
961
|
+
intentHash
|
|
962
|
+
},
|
|
963
|
+
capacity
|
|
964
|
+
)) {
|
|
852
965
|
invalid(code);
|
|
853
966
|
}
|
|
854
967
|
return Object.freeze({
|
|
855
968
|
version: EMAIL_FORMAT_VERSION,
|
|
969
|
+
...capacityTag(capacity),
|
|
856
970
|
scope: expectedScope,
|
|
857
971
|
key,
|
|
858
972
|
scheduleAt,
|
|
@@ -867,10 +981,7 @@ function validatePreparedCommand(scope2, input) {
|
|
|
867
981
|
var REQUEST = "invalid_command";
|
|
868
982
|
var LEASE = WORKER_LIMIT_BOUNDS.leaseMs;
|
|
869
983
|
function readStoreId(value, code = REQUEST) {
|
|
870
|
-
|
|
871
|
-
invalid(code);
|
|
872
|
-
}
|
|
873
|
-
return value;
|
|
984
|
+
return uuidV4(value, code);
|
|
874
985
|
}
|
|
875
986
|
function readLeaseToken(value, code = REQUEST) {
|
|
876
987
|
if (typeof value !== "string" || value.length < 22 || value.length > 128 || !/^[A-Za-z0-9_-]+$/.test(value) || /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value)) {
|
|
@@ -884,14 +995,18 @@ function readLeaseMs(value) {
|
|
|
884
995
|
function timeAfter(start, delayMs) {
|
|
885
996
|
return delayMs <= MAX_UNIX_MS - start ? start + delayMs : void 0;
|
|
886
997
|
}
|
|
887
|
-
function readStoreCall(input, worker = false) {
|
|
998
|
+
function readStoreCall(input, worker = false, capacity) {
|
|
888
999
|
const code = "invalid_config";
|
|
889
|
-
const raw = readRecord(input, ["signal", "timeoutMs"], [], code);
|
|
1000
|
+
const { smtpCapacity, ...raw } = readRecord(input, ["signal", "timeoutMs"], capacity ? ["smtpCapacity"] : [], code);
|
|
1001
|
+
if (capacity) assertCapacityTag({ smtpCapacity }, capacity, "store_incompatible");
|
|
890
1002
|
const max = worker ? WORKER_LIMIT_BOUNDS.storeTimeoutMs.max : CALL_BUDGETS.admissionOrAdmin.max;
|
|
891
1003
|
const timeoutMs = integer(raw.timeoutMs, 1, max, code);
|
|
892
1004
|
const call = readCallOptions(raw, { default: STORE_PRIMITIVE_TIMEOUT_MS, min: 1, max });
|
|
893
1005
|
if (!call.signal) invalid(code);
|
|
894
|
-
return {
|
|
1006
|
+
return {
|
|
1007
|
+
signal: call.signal,
|
|
1008
|
+
timeoutMs: worker && !capacity ? timeoutMs : Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS)
|
|
1009
|
+
};
|
|
895
1010
|
}
|
|
896
1011
|
function readRetryPolicy(input, code = REQUEST) {
|
|
897
1012
|
const raw = readRecord(input, ["maxAttempts", "baseDelayMs", "maxDelayMs"], [], code);
|
|
@@ -917,9 +1032,9 @@ function readRetentionPolicy(input, code = REQUEST) {
|
|
|
917
1032
|
idempotencyMs: integer(idempotencyMs, bounds.idempotencyMs.min, bounds.idempotencyMs.max, code)
|
|
918
1033
|
});
|
|
919
1034
|
}
|
|
920
|
-
function readStoreAdmission(scope2, input) {
|
|
1035
|
+
function readStoreAdmission(scope2, input, capacity) {
|
|
921
1036
|
const raw = readRecord(input, ["command", "jobId", "retry", "retention"], [], REQUEST);
|
|
922
|
-
const command = validatePreparedCommand(scope2, raw.command);
|
|
1037
|
+
const command = validatePreparedCommand(scope2, raw.command, capacity);
|
|
923
1038
|
return Object.freeze({
|
|
924
1039
|
command,
|
|
925
1040
|
admissionKey: encodeAdmissionKey(command.key),
|
|
@@ -940,15 +1055,15 @@ function readClaim(input) {
|
|
|
940
1055
|
leaseMs: readLeaseMs(leaseMs)
|
|
941
1056
|
});
|
|
942
1057
|
}
|
|
943
|
-
function readFinish(input) {
|
|
944
|
-
const { fence, phase, operationId, result
|
|
1058
|
+
function readFinish(input, capacity) {
|
|
1059
|
+
const { fence, phase, operationId, result } = readRecord(
|
|
945
1060
|
input,
|
|
946
1061
|
["fence", "phase", "operationId", "result"],
|
|
947
1062
|
[],
|
|
948
1063
|
REQUEST
|
|
949
1064
|
);
|
|
950
1065
|
if (phase !== "pre_handoff" && phase !== "handed_off") invalid(REQUEST);
|
|
951
|
-
const evidence2 = readAttemptResult(
|
|
1066
|
+
const evidence2 = readAttemptResult(result, REQUEST, false, capacity);
|
|
952
1067
|
assertEvidencePhase(evidence2.reason, phase, "owner", REQUEST);
|
|
953
1068
|
return Object.freeze({ fence: readFence(fence), phase, operationId: readStoreId(operationId), result: evidence2 });
|
|
954
1069
|
}
|
|
@@ -965,7 +1080,6 @@ function readResolution(input) {
|
|
|
965
1080
|
}
|
|
966
1081
|
|
|
967
1082
|
// src/internal/activity-codecs.ts
|
|
968
|
-
var ACTIVITY_BYTES = 8192;
|
|
969
1083
|
var dispositions = [
|
|
970
1084
|
"accepted",
|
|
971
1085
|
"partially_accepted",
|
|
@@ -982,17 +1096,17 @@ function readDisposition(input, code) {
|
|
|
982
1096
|
if (input === "retry_scheduled") return input;
|
|
983
1097
|
return invalid(code);
|
|
984
1098
|
}
|
|
985
|
-
function assertActivityDisposition(
|
|
986
|
-
const { outcome, reason } =
|
|
1099
|
+
function assertActivityDisposition(result, disposition, code) {
|
|
1100
|
+
const { outcome, reason } = result;
|
|
987
1101
|
if (outcome !== "rejected" && outcome !== "unsubmitted") {
|
|
988
1102
|
if (disposition !== outcome) invalid(code);
|
|
989
1103
|
} else if (reason === "expired" || reason === "cancelled") {
|
|
990
1104
|
if (disposition !== reason) invalid(code);
|
|
991
|
-
} else if (disposition !== (outcome === "rejected" ? "rejected" : "failed") && !(
|
|
1105
|
+
} else if (disposition !== (outcome === "rejected" ? "rejected" : "failed") && !(result.retry === "safe_transient" && (disposition === "expired" || disposition === "retry_scheduled"))) {
|
|
992
1106
|
invalid(code);
|
|
993
1107
|
}
|
|
994
1108
|
}
|
|
995
|
-
function
|
|
1109
|
+
function captureActivity(scope2, input, code, capacity) {
|
|
996
1110
|
const raw = readRecord(
|
|
997
1111
|
input,
|
|
998
1112
|
[
|
|
@@ -1009,26 +1123,32 @@ function readActivity(scope2, input, code = "store_incompatible") {
|
|
|
1009
1123
|
"result",
|
|
1010
1124
|
"disposition"
|
|
1011
1125
|
],
|
|
1012
|
-
["handedOffAt"],
|
|
1126
|
+
["handedOffAt", "smtpCapacity", "recipientCounts"],
|
|
1013
1127
|
code
|
|
1014
1128
|
);
|
|
1015
1129
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, code) !== scope2) invalid(code);
|
|
1130
|
+
assertCapacityTag(raw, capacity, code);
|
|
1131
|
+
if (!capacity && Object.hasOwn(raw, "recipientCounts")) invalid(code);
|
|
1132
|
+
const counts = capacity ? readRecipientCounts(raw.recipientCounts, code, capacity) : void 0;
|
|
1016
1133
|
const jobId = readStoreId(raw.jobId, code);
|
|
1017
1134
|
const attemptId = readStoreId(raw.attemptId, code);
|
|
1018
1135
|
const provider2 = validateProviderRef(raw.provider, code);
|
|
1136
|
+
assertCapacityProvider(provider2, capacity, code);
|
|
1019
1137
|
const startedAt = unixMs(raw.startedAt, code);
|
|
1020
1138
|
const finishedAt = unixMs(raw.finishedAt, code);
|
|
1021
1139
|
const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, code) : void 0;
|
|
1022
1140
|
if (finishedAt < startedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt > finishedAt)) {
|
|
1023
1141
|
invalid(code);
|
|
1024
1142
|
}
|
|
1025
|
-
const
|
|
1026
|
-
assertEvidencePhase(
|
|
1027
|
-
assertEvidenceContext(
|
|
1143
|
+
const result = readAttemptResult(raw.result, code, true, capacity);
|
|
1144
|
+
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", code);
|
|
1145
|
+
assertEvidenceContext(result, provider2, attemptId, counts, code);
|
|
1028
1146
|
const disposition = readDisposition(raw.disposition, code);
|
|
1029
|
-
assertActivityDisposition(
|
|
1147
|
+
assertActivityDisposition(result, disposition, code);
|
|
1030
1148
|
return Object.freeze({
|
|
1031
1149
|
version: EMAIL_FORMAT_VERSION,
|
|
1150
|
+
...capacityTag(capacity),
|
|
1151
|
+
...counts ? { recipientCounts: counts } : {},
|
|
1032
1152
|
scope: scope2,
|
|
1033
1153
|
jobId,
|
|
1034
1154
|
attemptId,
|
|
@@ -1039,54 +1159,66 @@ function readActivity(scope2, input, code = "store_incompatible") {
|
|
|
1039
1159
|
startedAt,
|
|
1040
1160
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1041
1161
|
finishedAt,
|
|
1042
|
-
result
|
|
1162
|
+
result,
|
|
1043
1163
|
disposition
|
|
1044
1164
|
});
|
|
1045
1165
|
}
|
|
1046
|
-
function
|
|
1166
|
+
function readActivity(scope2, input, code = "store_incompatible", capacity) {
|
|
1167
|
+
const activity = captureActivity(scope2, input, code, capacity);
|
|
1168
|
+
if (capacity) encodeEvidence(activity, code, capacity);
|
|
1169
|
+
return activity;
|
|
1170
|
+
}
|
|
1171
|
+
function activityFor(job, commit, capacity) {
|
|
1047
1172
|
const { outcome, retry, stage, reason, recipients, messageId, providerMessageId } = commit.result;
|
|
1048
|
-
return
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1173
|
+
return captureActivity(
|
|
1174
|
+
job.scope,
|
|
1175
|
+
{
|
|
1176
|
+
version: EMAIL_FORMAT_VERSION,
|
|
1177
|
+
...capacityTag(capacity),
|
|
1178
|
+
...capacity ? { recipientCounts: job.recipientCounts } : {},
|
|
1179
|
+
scope: job.scope,
|
|
1180
|
+
jobId: job.jobId,
|
|
1181
|
+
attemptId: commit.attemptId,
|
|
1182
|
+
ordinal: commit.ordinal,
|
|
1183
|
+
provider: job.provider,
|
|
1184
|
+
template: job.template,
|
|
1185
|
+
correlation: job.correlation,
|
|
1186
|
+
startedAt: commit.startedAt,
|
|
1187
|
+
...commit.handedOffAt === void 0 ? {} : { handedOffAt: commit.handedOffAt },
|
|
1188
|
+
finishedAt: commit.finishedAt,
|
|
1189
|
+
disposition: commit.disposition,
|
|
1190
|
+
result: {
|
|
1191
|
+
outcome,
|
|
1192
|
+
retry,
|
|
1193
|
+
stage,
|
|
1194
|
+
reason,
|
|
1195
|
+
...recipients === void 0 ? {} : { recipients },
|
|
1196
|
+
...messageId === void 0 ? {} : { messageId },
|
|
1197
|
+
...providerMessageId === void 0 ? {} : { providerMessageId }
|
|
1198
|
+
}
|
|
1199
|
+
},
|
|
1200
|
+
"store_incompatible",
|
|
1201
|
+
capacity
|
|
1202
|
+
);
|
|
1071
1203
|
}
|
|
1072
|
-
function activityDigest(activity) {
|
|
1073
|
-
return
|
|
1204
|
+
function activityDigest(activity, capacity) {
|
|
1205
|
+
return sha256(encodeEvidence(activity, "store_incompatible", capacity));
|
|
1074
1206
|
}
|
|
1075
|
-
function readStoredActivity(scope2, input, code = "store_incompatible") {
|
|
1207
|
+
function readStoredActivity(scope2, input, code = "store_incompatible", capacity) {
|
|
1076
1208
|
const raw = readRecord(input, ["activity", "digest"], [], code);
|
|
1077
|
-
const activity =
|
|
1209
|
+
const activity = captureActivity(scope2, raw.activity, code, capacity);
|
|
1078
1210
|
const digest2 = hashToken(raw.digest, code);
|
|
1079
|
-
if (activityDigest(activity) !== digest2) invalid(code);
|
|
1211
|
+
if (activityDigest(activity, capacity) !== digest2) invalid(code);
|
|
1080
1212
|
return Object.freeze({ activity, digest: digest2 });
|
|
1081
1213
|
}
|
|
1082
|
-
function readProjectionPermit(scope2, input) {
|
|
1214
|
+
function readProjectionPermit(scope2, input, capacity) {
|
|
1083
1215
|
const code = "invalid_command";
|
|
1084
1216
|
const {
|
|
1085
1217
|
activity,
|
|
1086
1218
|
digest: digest2,
|
|
1087
1219
|
permitUntil: rawUntil
|
|
1088
1220
|
} = readRecord(input, ["activity", "digest", "permitUntil"], [], code);
|
|
1089
|
-
const stored = readStoredActivity(scope2, { activity, digest: digest2 }, code);
|
|
1221
|
+
const stored = readStoredActivity(scope2, { activity, digest: digest2 }, code, capacity);
|
|
1090
1222
|
const permitUntil = unixMs(rawUntil, code);
|
|
1091
1223
|
if (permitUntil - stored.activity.finishedAt < PROJECTION_PERMIT_MS) invalid(code);
|
|
1092
1224
|
return Object.freeze({ ...stored, permitUntil });
|
|
@@ -1098,13 +1230,15 @@ function compareActivity(a, b) {
|
|
|
1098
1230
|
return b.finishedAt - a.finishedAt || compareAscii(a.jobId, b.jobId) || compareAscii(a.attemptId, b.attemptId);
|
|
1099
1231
|
}
|
|
1100
1232
|
function cursorPayload(criteria, position) {
|
|
1233
|
+
const binding = criteria.capacity?.binding;
|
|
1101
1234
|
return {
|
|
1102
1235
|
v: EMAIL_FORMAT_VERSION,
|
|
1103
1236
|
s: criteria.scope,
|
|
1104
1237
|
j: criteria.jobId,
|
|
1105
1238
|
f: criteria.from,
|
|
1106
1239
|
b: criteria.before,
|
|
1107
|
-
p: [position.finishedAt, position.jobId, position.attemptId]
|
|
1240
|
+
p: [position.finishedAt, position.jobId, position.attemptId],
|
|
1241
|
+
...binding ? { c: [binding.format, binding.namespace, binding.maxRecipients] } : {}
|
|
1108
1242
|
};
|
|
1109
1243
|
}
|
|
1110
1244
|
function encodeCursor(criteria, position) {
|
|
@@ -1125,7 +1259,7 @@ function decodeCursor(criteria, cursor) {
|
|
|
1125
1259
|
} catch {
|
|
1126
1260
|
invalid(code);
|
|
1127
1261
|
}
|
|
1128
|
-
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], [], code);
|
|
1262
|
+
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], criteria.capacity ? ["c"] : [], code);
|
|
1129
1263
|
const tuple = readArray(p, 3, code, (entry) => entry);
|
|
1130
1264
|
const position = Object.freeze({
|
|
1131
1265
|
finishedAt: unixMs(tuple[0], code),
|
|
@@ -1137,15 +1271,16 @@ function decodeCursor(criteria, cursor) {
|
|
|
1137
1271
|
}
|
|
1138
1272
|
return position;
|
|
1139
1273
|
}
|
|
1140
|
-
function readActivityCriteria(scope2, input) {
|
|
1274
|
+
function readActivityCriteria(scope2, input, capacity) {
|
|
1141
1275
|
const code = "invalid_command";
|
|
1142
1276
|
const raw = readRecord(input, [], ["jobId", "from", "before", "limit", "cursor"], code);
|
|
1143
1277
|
const jobId = Object.hasOwn(raw, "jobId") ? readStoreId(raw.jobId, code) : null;
|
|
1144
1278
|
const from = Object.hasOwn(raw, "from") ? unixMs(raw.from, code) : null;
|
|
1145
1279
|
const before = Object.hasOwn(raw, "before") ? unixMs(raw.before, code) : null;
|
|
1146
1280
|
if (from !== null && before !== null && from > before) invalid(code);
|
|
1147
|
-
const
|
|
1148
|
-
const
|
|
1281
|
+
const page = capacity?.limits.page ?? ACTIVITY_PAGE_LIMIT;
|
|
1282
|
+
const limit = Object.hasOwn(raw, "limit") ? integer(raw.limit, page.min, page.max, code) : page.default;
|
|
1283
|
+
const criteria = { scope: scope2, jobId, from, before, limit, ...capacity ? { capacity } : {} };
|
|
1149
1284
|
return Object.freeze({
|
|
1150
1285
|
...criteria,
|
|
1151
1286
|
...Object.hasOwn(raw, "cursor") ? { after: decodeCursor(criteria, raw.cursor) } : {}
|
|
@@ -1155,7 +1290,7 @@ function readOrderedActivities(criteria, input, maxRows) {
|
|
|
1155
1290
|
const code = "store_incompatible";
|
|
1156
1291
|
let previous = criteria.after;
|
|
1157
1292
|
return readArray(input, maxRows, code, (row) => {
|
|
1158
|
-
const activity = readActivity(criteria.scope, row);
|
|
1293
|
+
const activity = readActivity(criteria.scope, row, code, criteria.capacity);
|
|
1159
1294
|
if (previous && compareActivity(previous, activity) >= 0 || criteria.jobId !== null && criteria.jobId !== activity.jobId || criteria.from !== null && activity.finishedAt < criteria.from || criteria.before !== null && activity.finishedAt >= criteria.before) {
|
|
1160
1295
|
invalid(code);
|
|
1161
1296
|
}
|
|
@@ -1246,6 +1381,7 @@ function jobSummary(job) {
|
|
|
1246
1381
|
const latest = job.attempts.at(-1)?.commit;
|
|
1247
1382
|
return Object.freeze({
|
|
1248
1383
|
version: EMAIL_FORMAT_VERSION,
|
|
1384
|
+
...job.smtpCapacity ? { smtpCapacity: job.smtpCapacity } : {},
|
|
1249
1385
|
scope: job.scope,
|
|
1250
1386
|
jobId: job.jobId,
|
|
1251
1387
|
key: Object.freeze({ ...job.key }),
|
|
@@ -1270,13 +1406,13 @@ function jobSummary(job) {
|
|
|
1270
1406
|
...summaryState
|
|
1271
1407
|
});
|
|
1272
1408
|
}
|
|
1273
|
-
function copyCommit(commit) {
|
|
1274
|
-
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED2) });
|
|
1409
|
+
function copyCommit(commit, capacity) {
|
|
1410
|
+
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED2, false, capacity) });
|
|
1275
1411
|
}
|
|
1276
|
-
function readStoredSnapshot(job, input) {
|
|
1412
|
+
function readStoredSnapshot(job, input, capacity) {
|
|
1277
1413
|
let validated;
|
|
1278
1414
|
try {
|
|
1279
|
-
validated = validateSnapshot(input);
|
|
1415
|
+
validated = validateSnapshot(input, capacity);
|
|
1280
1416
|
} catch {
|
|
1281
1417
|
return invalid(STORED2);
|
|
1282
1418
|
}
|
|
@@ -1343,7 +1479,7 @@ function readState(input) {
|
|
|
1343
1479
|
}
|
|
1344
1480
|
return invalid(STORED2);
|
|
1345
1481
|
}
|
|
1346
|
-
function readLedgerAttempt(input, job) {
|
|
1482
|
+
function readLedgerAttempt(input, job, capacity) {
|
|
1347
1483
|
const entry = readRecord(input, ["commit", "digest"], ["ownerDigest", "permitUntil", "projectionAckAt"], STORED2);
|
|
1348
1484
|
const raw = readRecord(
|
|
1349
1485
|
entry.commit,
|
|
@@ -1356,14 +1492,14 @@ function readLedgerAttempt(input, job) {
|
|
|
1356
1492
|
const handedOffAt = optionalTime(raw, "handedOffAt");
|
|
1357
1493
|
const finishedAt = unixMs(raw.finishedAt, STORED2);
|
|
1358
1494
|
const retryAt = optionalTime(raw, "retryAt");
|
|
1359
|
-
const
|
|
1495
|
+
const result = readAttemptResult(raw.result, STORED2, false, capacity);
|
|
1360
1496
|
const ownerDigest = Object.hasOwn(entry, "ownerDigest") ? hashToken(entry.ownerDigest, STORED2) : void 0;
|
|
1361
|
-
const internal =
|
|
1362
|
-
if (raw.jobId !== job.jobId || startedAt >= job.expiresAt || finishedAt < startedAt || finishedAt > job.updatedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt >= job.expiresAt || handedOffAt > finishedAt) ||
|
|
1497
|
+
const internal = result.reason === "pre_handoff_lease_expired" || result.reason === "post_handoff_lease_expired" || result.reason === "cancelled";
|
|
1498
|
+
if (raw.jobId !== job.jobId || startedAt >= job.expiresAt || finishedAt < startedAt || finishedAt > job.updatedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt >= job.expiresAt || handedOffAt > finishedAt) || result.reason === "cancelled" && finishedAt >= job.expiresAt || result.reason === "expired" && finishedAt < job.expiresAt || result.reason === "pre_handoff_lease_expired" && finishedAt - startedAt < WORKER_LIMIT_BOUNDS.leaseMs.min || result.reason === "post_handoff_lease_expired" && (handedOffAt === void 0 || finishedAt - handedOffAt < WORKER_LIMIT_BOUNDS.leaseMs.min) || ownerDigest === void 0 !== internal) {
|
|
1363
1499
|
invalid(STORED2);
|
|
1364
1500
|
}
|
|
1365
|
-
assertEvidencePhase(
|
|
1366
|
-
assertEvidenceContext(
|
|
1501
|
+
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", STORED2);
|
|
1502
|
+
assertEvidenceContext(result, job.provider, attemptId, job.recipientCounts, STORED2);
|
|
1367
1503
|
const commit = Object.freeze({
|
|
1368
1504
|
jobId: job.jobId,
|
|
1369
1505
|
attemptId,
|
|
@@ -1372,7 +1508,7 @@ function readLedgerAttempt(input, job) {
|
|
|
1372
1508
|
startedAt,
|
|
1373
1509
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1374
1510
|
finishedAt,
|
|
1375
|
-
result
|
|
1511
|
+
result,
|
|
1376
1512
|
disposition: readDisposition(raw.disposition, STORED2),
|
|
1377
1513
|
reason: raw.reason === "attempts_exhausted" ? raw.reason : readAttemptReason(raw.reason, STORED2),
|
|
1378
1514
|
...retryAt === void 0 ? {} : { retryAt }
|
|
@@ -1383,7 +1519,7 @@ function readLedgerAttempt(input, job) {
|
|
|
1383
1519
|
invalid(STORED2);
|
|
1384
1520
|
}
|
|
1385
1521
|
const digest2 = hashToken(entry.digest, STORED2);
|
|
1386
|
-
if (digest2 !== activityDigest(activityFor(job, commit))) invalid(STORED2);
|
|
1522
|
+
if (digest2 !== activityDigest(activityFor(job, commit, capacity), capacity)) invalid(STORED2);
|
|
1387
1523
|
return Object.freeze({
|
|
1388
1524
|
commit,
|
|
1389
1525
|
digest: digest2,
|
|
@@ -1404,12 +1540,12 @@ function assertHistory(job) {
|
|
|
1404
1540
|
let pending = false;
|
|
1405
1541
|
let lastAck = 0;
|
|
1406
1542
|
for (const { commit, permitUntil, projectionAckAt } of job.attempts) {
|
|
1407
|
-
const { attemptId, ordinal: ordinal2, startedAt, finishedAt, result
|
|
1543
|
+
const { attemptId, ordinal: ordinal2, startedAt, finishedAt, result, disposition, reason, retryAt } = commit;
|
|
1408
1544
|
if (nextAt === void 0 || startedAt < nextAt || seen.has(attemptId) || ordinal2 !== seen.size + 1 || !manual && ordinal2 > retry.maxAttempts) {
|
|
1409
1545
|
invalid(STORED2);
|
|
1410
1546
|
}
|
|
1411
1547
|
seen.add(attemptId);
|
|
1412
|
-
const expected = attemptEffect(
|
|
1548
|
+
const expected = attemptEffect(result, attemptId, ordinal2, manual, retry, expiresAt, finishedAt);
|
|
1413
1549
|
if (disposition !== expected.disposition || reason !== expected.reason || retryAt !== expected.retryAt)
|
|
1414
1550
|
invalid(STORED2);
|
|
1415
1551
|
if (permitUntil !== void 0) {
|
|
@@ -1467,7 +1603,7 @@ function assertHistory(job) {
|
|
|
1467
1603
|
if (job.snapshot || retireAt === void 0 || retireAt > job.updatedAt) invalid(STORED2);
|
|
1468
1604
|
}
|
|
1469
1605
|
}
|
|
1470
|
-
function readJobRecord(scope2, input) {
|
|
1606
|
+
function readJobRecord(scope2, input, capacity) {
|
|
1471
1607
|
const raw = readRecord(
|
|
1472
1608
|
input,
|
|
1473
1609
|
[
|
|
@@ -1494,27 +1630,26 @@ function readJobRecord(scope2, input) {
|
|
|
1494
1630
|
"attempts",
|
|
1495
1631
|
"resolutions"
|
|
1496
1632
|
],
|
|
1497
|
-
["snapshot", "projectionPermitUntil", "retiring"],
|
|
1633
|
+
["snapshot", "projectionPermitUntil", "retiring", "smtpCapacity", "intentHash"],
|
|
1498
1634
|
STORED2
|
|
1499
1635
|
);
|
|
1500
1636
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, STORED2) !== scope2) invalid(STORED2);
|
|
1501
|
-
|
|
1502
|
-
const recipients =
|
|
1503
|
-
to: integer(counts.to, 0, MESSAGE_LIMITS.recipients, STORED2),
|
|
1504
|
-
cc: integer(counts.cc, 0, MESSAGE_LIMITS.recipients, STORED2),
|
|
1505
|
-
bcc: integer(counts.bcc, 0, MESSAGE_LIMITS.recipients, STORED2)
|
|
1506
|
-
});
|
|
1507
|
-
integer(recipients.to + recipients.cc + recipients.bcc, 1, MESSAGE_LIMITS.recipients, STORED2);
|
|
1637
|
+
assertCapacityTag(raw, capacity, STORED2);
|
|
1638
|
+
const recipients = readRecipientCounts(raw.recipientCounts, STORED2, capacity);
|
|
1508
1639
|
const key = validateAdmissionKey(raw.key, STORED2);
|
|
1509
1640
|
const admissionKey = encodeAdmissionKey(key);
|
|
1510
1641
|
if (raw.admissionKey !== admissionKey) invalid(STORED2);
|
|
1642
|
+
if (!capacity && Object.hasOwn(raw, "intentHash")) invalid(STORED2);
|
|
1643
|
+
const intentHash = capacity ? hashToken(raw.intentHash, STORED2) : void 0;
|
|
1511
1644
|
const base = {
|
|
1512
1645
|
version: EMAIL_FORMAT_VERSION,
|
|
1646
|
+
...capacityTag(capacity),
|
|
1513
1647
|
scope: scope2,
|
|
1514
1648
|
jobId: readStoreId(raw.jobId, STORED2),
|
|
1515
1649
|
key,
|
|
1516
1650
|
admissionKey,
|
|
1517
1651
|
fingerprint: hashToken(raw.fingerprint, STORED2),
|
|
1652
|
+
...intentHash === void 0 ? {} : { intentHash },
|
|
1518
1653
|
snapshotHash: hashToken(raw.snapshotHash, STORED2),
|
|
1519
1654
|
retry: readRetryPolicy(raw.retry, STORED2),
|
|
1520
1655
|
retention: readRetentionPolicy(raw.retention, STORED2),
|
|
@@ -1530,6 +1665,10 @@ function readJobRecord(scope2, input) {
|
|
|
1530
1665
|
revision: readStoreId(raw.revision, STORED2),
|
|
1531
1666
|
state: readState(raw.state)
|
|
1532
1667
|
};
|
|
1668
|
+
assertCapacityProvider(base.provider, capacity, STORED2);
|
|
1669
|
+
if (intentHash !== void 0 && base.fingerprint !== fingerprintFor({ ...base, templateKey: base.template.key, intentHash }, capacity)) {
|
|
1670
|
+
invalid(STORED2);
|
|
1671
|
+
}
|
|
1533
1672
|
if (base.createdAt > base.updatedAt || Math.max(base.createdAt, base.scheduleAt ?? 0) >= base.expiresAt || base.provider.kind === "sendgrid" && !recipients.to) {
|
|
1534
1673
|
invalid(STORED2);
|
|
1535
1674
|
}
|
|
@@ -1538,8 +1677,8 @@ function readJobRecord(scope2, input) {
|
|
|
1538
1677
|
if (retiring && raw.retiring !== true) invalid(STORED2);
|
|
1539
1678
|
const job = Object.freeze({
|
|
1540
1679
|
...base,
|
|
1541
|
-
...Object.hasOwn(raw, "snapshot") ? { snapshot: readStoredSnapshot(base, raw.snapshot) } : {},
|
|
1542
|
-
attempts: readArray(raw.attempts, MAX_ATTEMPT_LEDGER, STORED2, (entry) => readLedgerAttempt(entry, base)),
|
|
1680
|
+
...Object.hasOwn(raw, "snapshot") ? { snapshot: readStoredSnapshot(base, raw.snapshot, capacity) } : {},
|
|
1681
|
+
attempts: readArray(raw.attempts, MAX_ATTEMPT_LEDGER, STORED2, (entry) => readLedgerAttempt(entry, base, capacity)),
|
|
1543
1682
|
resolutions: readArray(raw.resolutions, 2, STORED2, readUnknownResolution),
|
|
1544
1683
|
...projectionPermitUntil === void 0 ? {} : { projectionPermitUntil },
|
|
1545
1684
|
...retiring ? { retiring: true } : {}
|
|
@@ -1571,7 +1710,7 @@ function replace(job, time2, patch, value, before, pendingAck) {
|
|
|
1571
1710
|
write: { kind: "replace", guard: pendingAck ? { ...expect, pendingAck } : expect, next }
|
|
1572
1711
|
};
|
|
1573
1712
|
}
|
|
1574
|
-
function newJob(admission, time2) {
|
|
1713
|
+
function newJob(admission, time2, capacity) {
|
|
1575
1714
|
const { command, jobId, retry, retention, admissionKey } = admission;
|
|
1576
1715
|
const { scope: scope2, key, scheduleAt, fingerprint, snapshotHash, snapshot } = command;
|
|
1577
1716
|
const now = unixMs(time2.now, "store_unavailable");
|
|
@@ -1580,6 +1719,8 @@ function newJob(admission, time2) {
|
|
|
1580
1719
|
if (nextAt >= snapshot.expiresAt) invalid("expired");
|
|
1581
1720
|
return Object.freeze({
|
|
1582
1721
|
version: EMAIL_FORMAT_VERSION,
|
|
1722
|
+
...capacityTag(capacity),
|
|
1723
|
+
...capacity ? { intentHash: command.intentHash } : {},
|
|
1583
1724
|
scope: scope2,
|
|
1584
1725
|
jobId,
|
|
1585
1726
|
key,
|
|
@@ -1618,14 +1759,14 @@ function liveLease(job, fence, phase, now) {
|
|
|
1618
1759
|
}
|
|
1619
1760
|
return lease;
|
|
1620
1761
|
}
|
|
1621
|
-
function claimJob(job, claim, time2) {
|
|
1762
|
+
function claimJob(job, claim, time2, capacity) {
|
|
1622
1763
|
const { now } = time2;
|
|
1623
1764
|
if (job.state.state !== "queued" || job.state.nextAt > now || now >= job.expiresAt) return null;
|
|
1624
1765
|
const manual = hasRedrive(job);
|
|
1625
1766
|
const ordinal2 = job.attempts.length + 1;
|
|
1626
1767
|
if (ordinal2 > MAX_ATTEMPT_LEDGER || !manual && ordinal2 > job.retry.maxAttempts) invalid("store_incompatible");
|
|
1627
1768
|
if (job.attempts.some(({ commit }) => commit.attemptId === claim.attemptId)) invalid("invalid_command");
|
|
1628
|
-
const snapshot = readStoredSnapshot(job, job.snapshot);
|
|
1769
|
+
const snapshot = readStoredSnapshot(job, job.snapshot, capacity);
|
|
1629
1770
|
const lease = Object.freeze({
|
|
1630
1771
|
attemptId: claim.attemptId,
|
|
1631
1772
|
token: claim.token,
|
|
@@ -1685,11 +1826,11 @@ function markJob(job, fence, leaseMs, time2) {
|
|
|
1685
1826
|
Math.min(job.expiresAt, lease.leaseEndsAt, grant.leaseEndsAt)
|
|
1686
1827
|
);
|
|
1687
1828
|
}
|
|
1688
|
-
function finalize(job, lease, operationId,
|
|
1829
|
+
function finalize(job, lease, operationId, result, time2, owner, capacity) {
|
|
1689
1830
|
const { now } = time2;
|
|
1690
1831
|
const { attemptId, startedAt, handedOffAt } = lease;
|
|
1691
1832
|
const ordinal2 = job.attempts.length + 1;
|
|
1692
|
-
const effect = attemptEffect(
|
|
1833
|
+
const effect = attemptEffect(result, attemptId, ordinal2, hasRedrive(job), job.retry, job.expiresAt, now);
|
|
1693
1834
|
const commit = Object.freeze({
|
|
1694
1835
|
jobId: job.jobId,
|
|
1695
1836
|
attemptId,
|
|
@@ -1698,31 +1839,31 @@ function finalize(job, lease, operationId, result2, time2, owner) {
|
|
|
1698
1839
|
startedAt,
|
|
1699
1840
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1700
1841
|
finishedAt: now,
|
|
1701
|
-
result
|
|
1842
|
+
result,
|
|
1702
1843
|
...effect
|
|
1703
1844
|
});
|
|
1704
1845
|
const entry = Object.freeze({
|
|
1705
1846
|
commit,
|
|
1706
|
-
digest: activityDigest(activityFor(job, commit)),
|
|
1847
|
+
digest: activityDigest(activityFor(job, commit, capacity), capacity),
|
|
1707
1848
|
...owner ? { ownerDigest: sha256(lease.token) } : {}
|
|
1708
1849
|
});
|
|
1709
1850
|
const state = effect.disposition === "retry_scheduled" ? Object.freeze({ state: "queued", nextAt: effect.retryAt }) : terminalState(effect.disposition, effect.reason, now);
|
|
1710
1851
|
let before = owner ? lease.leaseEndsAt : void 0;
|
|
1711
|
-
if (
|
|
1852
|
+
if (result.reason === "cancelled" || result.retry === "safe_transient" && effect.disposition !== "expired") {
|
|
1712
1853
|
before = Math.min(before ?? job.expiresAt, job.expiresAt);
|
|
1713
1854
|
}
|
|
1714
1855
|
return replace(
|
|
1715
1856
|
job,
|
|
1716
1857
|
time2,
|
|
1717
1858
|
{ ...job, state, attempts: Object.freeze([...job.attempts, entry]) },
|
|
1718
|
-
() => copyCommit(commit),
|
|
1859
|
+
() => copyCommit(commit, capacity),
|
|
1719
1860
|
before
|
|
1720
1861
|
);
|
|
1721
1862
|
}
|
|
1722
|
-
function finishJob(job, request, time2) {
|
|
1723
|
-
const { fence, phase, operationId, result
|
|
1863
|
+
function finishJob(job, request, time2, capacity) {
|
|
1864
|
+
const { fence, phase, operationId, result } = request;
|
|
1724
1865
|
if (job.jobId !== fence.jobId) return null;
|
|
1725
|
-
assertEvidencePhase(
|
|
1866
|
+
assertEvidencePhase(result.reason, phase, "owner", "invalid_command");
|
|
1726
1867
|
const previous = job.attempts.find(({ commit }) => commit.attemptId === fence.attemptId);
|
|
1727
1868
|
if (previous) {
|
|
1728
1869
|
const { commit } = previous;
|
|
@@ -1730,28 +1871,28 @@ function finishJob(job, request, time2) {
|
|
|
1730
1871
|
if (previous.ownerDigest !== sha256(fence.token) || commit.operationId !== operationId || previousPhase !== phase) {
|
|
1731
1872
|
return null;
|
|
1732
1873
|
}
|
|
1733
|
-
if (
|
|
1874
|
+
if (sha256(encodeEvidence(result, "invalid_command", capacity)) !== sha256(encodeEvidence(commit.result, "store_incompatible", capacity))) {
|
|
1734
1875
|
invalid("invalid_command");
|
|
1735
1876
|
}
|
|
1736
|
-
return { value: copyCommit(commit) };
|
|
1877
|
+
return { value: copyCommit(commit, capacity) };
|
|
1737
1878
|
}
|
|
1738
1879
|
const lease = liveLease(job, fence, phase, time2.now);
|
|
1739
1880
|
if (!lease) return null;
|
|
1740
|
-
if (
|
|
1741
|
-
assertEvidenceContext(
|
|
1742
|
-
return finalize(job, lease, operationId,
|
|
1881
|
+
if (result.reason === "expired" && time2.now < job.expiresAt) invalid("invalid_command");
|
|
1882
|
+
assertEvidenceContext(result, job.provider, fence.attemptId, job.recipientCounts, "invalid_command");
|
|
1883
|
+
return finalize(job, lease, operationId, result, time2, true, capacity);
|
|
1743
1884
|
}
|
|
1744
|
-
function recoverJob(job, operationId, time2) {
|
|
1885
|
+
function recoverJob(job, operationId, time2, capacity) {
|
|
1745
1886
|
if (job.state.state !== "leased" || time2.now < job.state.lease.leaseEndsAt) return null;
|
|
1746
1887
|
const { lease } = job.state;
|
|
1747
1888
|
const reason = lease.handedOffAt === void 0 ? "pre_handoff_lease_expired" : "post_handoff_lease_expired";
|
|
1748
|
-
return finalize(job, lease, operationId, lifecycleEvidence(reason), time2, false);
|
|
1889
|
+
return finalize(job, lease, operationId, lifecycleEvidence(reason), time2, false, capacity);
|
|
1749
1890
|
}
|
|
1750
1891
|
function expireJob(job, time2) {
|
|
1751
1892
|
if (job.state.state !== "queued" || time2.now < job.expiresAt) return null;
|
|
1752
1893
|
return replace(job, time2, { ...job, state: terminalState("expired", "expired", time2.now) }, jobSummary);
|
|
1753
1894
|
}
|
|
1754
|
-
function cancelJob(job, operationId, time2) {
|
|
1895
|
+
function cancelJob(job, operationId, time2, capacity) {
|
|
1755
1896
|
const { state } = job;
|
|
1756
1897
|
if (state.state === "terminal") {
|
|
1757
1898
|
return {
|
|
@@ -1765,7 +1906,7 @@ function cancelJob(job, operationId, time2) {
|
|
|
1765
1906
|
return { value: Object.freeze({ status: "too_late", job: jobSummary(job) }) };
|
|
1766
1907
|
}
|
|
1767
1908
|
if (state.state === "leased") {
|
|
1768
|
-
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time2, false);
|
|
1909
|
+
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time2, false, capacity);
|
|
1769
1910
|
const { write } = decision;
|
|
1770
1911
|
if (write?.kind !== "replace") invalid("store_incompatible");
|
|
1771
1912
|
return { write, value: Object.freeze({ status: "cancelled", job: jobSummary(write.next) }) };
|
|
@@ -1810,11 +1951,11 @@ function resolveJob(job, request, time2) {
|
|
|
1810
1951
|
function replaceAttempt(job, previous, entry) {
|
|
1811
1952
|
return Object.freeze(job.attempts.map((attempt) => attempt === previous ? Object.freeze(entry) : attempt));
|
|
1812
1953
|
}
|
|
1813
|
-
function reserveJobProjection(job, time2) {
|
|
1954
|
+
function reserveJobProjection(job, time2, capacity) {
|
|
1814
1955
|
const entry = pendingAttempt(job);
|
|
1815
1956
|
if (job.retiring || !entry || entry.commit.finishedAt > time2.now) return null;
|
|
1816
1957
|
const permitUntil = timeAfter(time2.now, PROJECTION_PERMIT_MS) ?? invalid("store_unavailable");
|
|
1817
|
-
const activity = activityFor(job, entry.commit);
|
|
1958
|
+
const activity = activityFor(job, entry.commit, capacity);
|
|
1818
1959
|
const permit = Object.freeze({ activity, digest: entry.digest, permitUntil });
|
|
1819
1960
|
const attempts = replaceAttempt(job, entry, { ...entry, permitUntil });
|
|
1820
1961
|
return replace(
|
|
@@ -1983,9 +2124,9 @@ var MongoConnection = class {
|
|
|
1983
2124
|
return { kind: "ack", reply };
|
|
1984
2125
|
}
|
|
1985
2126
|
async read(binding, budget, command, target) {
|
|
1986
|
-
const
|
|
1987
|
-
if (
|
|
1988
|
-
return
|
|
2127
|
+
const result = await this.exec(binding, budget, command, target);
|
|
2128
|
+
if (result.kind !== "ack") return invalid("store_unavailable");
|
|
2129
|
+
return result.reply;
|
|
1989
2130
|
}
|
|
1990
2131
|
async write(binding, budget, command, definite) {
|
|
1991
2132
|
return this.exec(binding, budget, command, "store", true, definite);
|
|
@@ -2095,9 +2236,9 @@ var jobOptional = [
|
|
|
2095
2236
|
"pendingAt",
|
|
2096
2237
|
"nextCleanupAt"
|
|
2097
2238
|
];
|
|
2098
|
-
function readJobBson(scope2, raw) {
|
|
2099
|
-
const { _id, lifecycle: lifecycle2, messageExpiresAt, state, nextAt, expiresAt, leaseEndsAt, pendingAt, nextCleanupAt, ...rest } = readRecord(raw, jobRequired, jobOptional, STORED3);
|
|
2100
|
-
const job = readJobRecord(scope2, { ...rest, state: lifecycle2, expiresAt: messageExpiresAt });
|
|
2239
|
+
function readJobBson(scope2, raw, capacity) {
|
|
2240
|
+
const { _id, lifecycle: lifecycle2, messageExpiresAt, state, nextAt, expiresAt, leaseEndsAt, pendingAt, nextCleanupAt, ...rest } = readRecord(raw, jobRequired, capacity ? [...jobOptional, "smtpCapacity", "intentHash"] : jobOptional, STORED3);
|
|
2241
|
+
const job = readJobRecord(scope2, { ...rest, state: lifecycle2, expiresAt: messageExpiresAt }, capacity);
|
|
2101
2242
|
const selectors = jobSelectors(job);
|
|
2102
2243
|
for (const [key, value, expected] of [
|
|
2103
2244
|
["state", state, selectors.state],
|
|
@@ -2128,14 +2269,15 @@ var activityRequired = [
|
|
|
2128
2269
|
"disposition"
|
|
2129
2270
|
];
|
|
2130
2271
|
var activityOptional = ["handedOffAt"];
|
|
2131
|
-
function readActivityBson(expectedScope, row) {
|
|
2272
|
+
function readActivityBson(expectedScope, row, capacity) {
|
|
2132
2273
|
if (!Object.hasOwn(row, "complete")) {
|
|
2133
|
-
const { _id: _id2, scope: scope2, jobId, attemptId, stubDeleteAt } = readRecord(
|
|
2274
|
+
const { _id: _id2, scope: scope2, jobId, attemptId, stubDeleteAt, ...tag } = readRecord(
|
|
2134
2275
|
row,
|
|
2135
2276
|
[...activityIdentity, "stubDeleteAt"],
|
|
2136
|
-
[],
|
|
2277
|
+
capacity ? ["smtpCapacity"] : [],
|
|
2137
2278
|
STORED3
|
|
2138
2279
|
);
|
|
2280
|
+
assertCapacityTag(tag, capacity, STORED3);
|
|
2139
2281
|
readStoreId(_id2, STORED3);
|
|
2140
2282
|
readStoreId(jobId, STORED3);
|
|
2141
2283
|
readStoreId(attemptId, STORED3);
|
|
@@ -2143,9 +2285,14 @@ function readActivityBson(expectedScope, row) {
|
|
|
2143
2285
|
unixMs(stubDeleteAt.getTime(), STORED3);
|
|
2144
2286
|
return;
|
|
2145
2287
|
}
|
|
2146
|
-
const { _id, complete, digest: digest2, ...activity } = readRecord(
|
|
2288
|
+
const { _id, complete, digest: digest2, ...activity } = readRecord(
|
|
2289
|
+
row,
|
|
2290
|
+
activityRequired,
|
|
2291
|
+
capacity ? [...activityOptional, "smtpCapacity", "recipientCounts"] : activityOptional,
|
|
2292
|
+
STORED3
|
|
2293
|
+
);
|
|
2147
2294
|
readStoreId(_id, STORED3);
|
|
2148
|
-
const stored = readStoredActivity(expectedScope, { activity, digest: digest2 });
|
|
2295
|
+
const stored = readStoredActivity(expectedScope, { activity, digest: digest2 }, STORED3, capacity);
|
|
2149
2296
|
if (complete !== true) invalid(STORED3);
|
|
2150
2297
|
return stored;
|
|
2151
2298
|
}
|
|
@@ -2175,49 +2322,58 @@ var provider = objectSchema({
|
|
|
2175
2322
|
});
|
|
2176
2323
|
var template = objectSchema({ key: string, revision: string });
|
|
2177
2324
|
var correlation = { ...bsonObject, maxProperties: MESSAGE_LIMITS.correlationEntries, additionalProperties: string };
|
|
2178
|
-
var
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
}
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
}
|
|
2325
|
+
var smtpProvider = objectSchema({ kind: { enum: ["smtp"] }, key: string, mode: { enum: ["live"] } });
|
|
2326
|
+
function resultFields(capacity) {
|
|
2327
|
+
const max = recipientLimit(capacity);
|
|
2328
|
+
const recipient = objectSchema({
|
|
2329
|
+
group: { enum: ["to", "cc", "bcc"] },
|
|
2330
|
+
index: { ...numeric, minimum: 0, maximum: max - 1 },
|
|
2331
|
+
status: { enum: ["accepted", "rejected"] }
|
|
2332
|
+
});
|
|
2333
|
+
return {
|
|
2334
|
+
outcome: string,
|
|
2335
|
+
retry: { enum: ["never", "safe_transient"] },
|
|
2336
|
+
stage: string,
|
|
2337
|
+
reason: string,
|
|
2338
|
+
recipients: { bsonType: "array", maxItems: max, items: recipient },
|
|
2339
|
+
messageId: { ...string, maxLength: "<00000000-0000-4000-8000-000000000000@email.invalid>".length },
|
|
2340
|
+
...capacity ? {} : { providerMessageId: { ...string, maxLength: 128 } }
|
|
2341
|
+
};
|
|
2342
|
+
}
|
|
2192
2343
|
var resultOptional = ["recipients", "messageId", "providerMessageId"];
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2344
|
+
function ledgerAttempt(capacity) {
|
|
2345
|
+
return objectSchema(
|
|
2346
|
+
{
|
|
2347
|
+
commit: objectSchema(
|
|
2348
|
+
{
|
|
2349
|
+
jobId: uuid,
|
|
2350
|
+
attemptId: uuid,
|
|
2351
|
+
ordinal,
|
|
2352
|
+
operationId: uuid,
|
|
2353
|
+
startedAt: time,
|
|
2354
|
+
handedOffAt: time,
|
|
2355
|
+
finishedAt: time,
|
|
2356
|
+
result: objectSchema(
|
|
2357
|
+
{
|
|
2358
|
+
...resultFields(capacity),
|
|
2359
|
+
retryHint: objectSchema({ delayMs: numeric, notBefore: time }, ["delayMs", "notBefore"])
|
|
2360
|
+
},
|
|
2361
|
+
[...resultOptional, "retryHint"]
|
|
2362
|
+
),
|
|
2363
|
+
disposition: string,
|
|
2364
|
+
reason: string,
|
|
2365
|
+
retryAt: time
|
|
2366
|
+
},
|
|
2367
|
+
["handedOffAt", "retryAt"]
|
|
2368
|
+
),
|
|
2369
|
+
digest,
|
|
2370
|
+
ownerDigest: digest,
|
|
2371
|
+
permitUntil: time,
|
|
2372
|
+
projectionAckAt: time
|
|
2373
|
+
},
|
|
2374
|
+
["ownerDigest", "permitUntil", "projectionAckAt"]
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2221
2377
|
var lifecycle = {
|
|
2222
2378
|
oneOf: [
|
|
2223
2379
|
objectSchema({ state: { enum: ["queued"] }, nextAt: time }),
|
|
@@ -2247,13 +2403,57 @@ var lifecycle = {
|
|
|
2247
2403
|
)
|
|
2248
2404
|
]
|
|
2249
2405
|
};
|
|
2250
|
-
function
|
|
2406
|
+
function capacityField({ binding }) {
|
|
2251
2407
|
return {
|
|
2408
|
+
smtpCapacity: objectSchema({
|
|
2409
|
+
format: { enum: [binding.format] },
|
|
2410
|
+
namespace: { enum: [binding.namespace] },
|
|
2411
|
+
maxRecipients: { ...numeric, enum: [binding.maxRecipients] }
|
|
2412
|
+
})
|
|
2413
|
+
};
|
|
2414
|
+
}
|
|
2415
|
+
function countsSchema(capacity) {
|
|
2416
|
+
const count = capacity ? { ...numeric, minimum: 0, maximum: recipientLimit(capacity) } : numeric;
|
|
2417
|
+
return objectSchema({ to: count, cc: count, bcc: count });
|
|
2418
|
+
}
|
|
2419
|
+
function smtpSnapshot(capacity) {
|
|
2420
|
+
const mailbox = objectSchema({ address: string, name: string }, ["name"]);
|
|
2421
|
+
const recipients = { bsonType: "array", maxItems: recipientLimit(capacity), items: mailbox };
|
|
2422
|
+
return objectSchema(
|
|
2423
|
+
{
|
|
2424
|
+
version,
|
|
2425
|
+
...capacityField(capacity),
|
|
2426
|
+
kind: { enum: ["smtp"] },
|
|
2427
|
+
provider: smtpProvider,
|
|
2428
|
+
template,
|
|
2429
|
+
envelope: objectSchema({ from: mailbox, to: recipients, cc: recipients, bcc: recipients, replyTo: mailbox }, [
|
|
2430
|
+
"replyTo"
|
|
2431
|
+
]),
|
|
2432
|
+
preparedAt: time,
|
|
2433
|
+
expiresAt: time,
|
|
2434
|
+
priority: { ...numeric, minimum: PRIORITY.min, maximum: PRIORITY.max },
|
|
2435
|
+
attachments: { bsonType: "array", maxItems: MESSAGE_LIMITS.attachments, items: bsonObject },
|
|
2436
|
+
correlation,
|
|
2437
|
+
subject: string,
|
|
2438
|
+
html: string,
|
|
2439
|
+
text: string
|
|
2440
|
+
},
|
|
2441
|
+
["html", "text"]
|
|
2442
|
+
);
|
|
2443
|
+
}
|
|
2444
|
+
function withBsonLimit(validator, maxBytes) {
|
|
2445
|
+
return maxBytes === void 0 ? validator : {
|
|
2446
|
+
$and: [validator, { $expr: { $lte: [{ $bsonSize: "$$ROOT" }, maxBytes] } }]
|
|
2447
|
+
};
|
|
2448
|
+
}
|
|
2449
|
+
function jobValidator(capacity) {
|
|
2450
|
+
const validator = {
|
|
2252
2451
|
$jsonSchema: {
|
|
2253
2452
|
...objectSchema(
|
|
2254
2453
|
{
|
|
2255
2454
|
_id: string,
|
|
2256
2455
|
version,
|
|
2456
|
+
...capacity ? { ...capacityField(capacity), intentHash: digest } : {},
|
|
2257
2457
|
scope,
|
|
2258
2458
|
jobId: uuid,
|
|
2259
2459
|
key: {
|
|
@@ -2265,13 +2465,13 @@ function jobValidator() {
|
|
|
2265
2465
|
admissionKey: string,
|
|
2266
2466
|
fingerprint: digest,
|
|
2267
2467
|
snapshotHash: digest,
|
|
2268
|
-
snapshot: bsonObject,
|
|
2468
|
+
snapshot: capacity ? smtpSnapshot(capacity) : bsonObject,
|
|
2269
2469
|
retry: objectSchema({ maxAttempts: numeric, baseDelayMs: numeric, maxDelayMs: numeric }),
|
|
2270
2470
|
retention: objectSchema({ payloadMs: numeric, historyMs: numeric, idempotencyMs: numeric }),
|
|
2271
|
-
provider,
|
|
2471
|
+
provider: capacity ? smtpProvider : provider,
|
|
2272
2472
|
template,
|
|
2273
2473
|
correlation,
|
|
2274
|
-
recipientCounts:
|
|
2474
|
+
recipientCounts: countsSchema(capacity),
|
|
2275
2475
|
scheduleAt: { anyOf: [time, { bsonType: "null" }] },
|
|
2276
2476
|
createdAt: time,
|
|
2277
2477
|
updatedAt: time,
|
|
@@ -2280,7 +2480,7 @@ function jobValidator() {
|
|
|
2280
2480
|
revision: uuid,
|
|
2281
2481
|
lifecycle,
|
|
2282
2482
|
state: { enum: ["queued", "leased", "terminal"] },
|
|
2283
|
-
attempts: { bsonType: "array", items: ledgerAttempt, maxItems: MAX_ATTEMPT_LEDGER },
|
|
2483
|
+
attempts: { bsonType: "array", items: ledgerAttempt(capacity), maxItems: MAX_ATTEMPT_LEDGER },
|
|
2284
2484
|
resolutions: {
|
|
2285
2485
|
bsonType: "array",
|
|
2286
2486
|
maxItems: 2,
|
|
@@ -2314,10 +2514,11 @@ function jobValidator() {
|
|
|
2314
2514
|
]
|
|
2315
2515
|
}
|
|
2316
2516
|
};
|
|
2517
|
+
return withBsonLimit(validator, capacity?.limits.jobBsonBytes);
|
|
2317
2518
|
}
|
|
2318
|
-
function activityValidator() {
|
|
2319
|
-
const identity = { _id: uuid, scope, jobId: uuid, attemptId: uuid };
|
|
2320
|
-
|
|
2519
|
+
function activityValidator(capacity) {
|
|
2520
|
+
const identity = { _id: uuid, scope, jobId: uuid, attemptId: uuid, ...capacity ? capacityField(capacity) : {} };
|
|
2521
|
+
const validator = {
|
|
2321
2522
|
$jsonSchema: {
|
|
2322
2523
|
oneOf: [
|
|
2323
2524
|
objectSchema(
|
|
@@ -2327,13 +2528,14 @@ function activityValidator() {
|
|
|
2327
2528
|
complete: { enum: [true] },
|
|
2328
2529
|
digest,
|
|
2329
2530
|
ordinal,
|
|
2330
|
-
provider,
|
|
2531
|
+
provider: capacity ? smtpProvider : provider,
|
|
2532
|
+
...capacity ? { recipientCounts: countsSchema(capacity) } : {},
|
|
2331
2533
|
template,
|
|
2332
2534
|
correlation,
|
|
2333
2535
|
startedAt: time,
|
|
2334
2536
|
handedOffAt: time,
|
|
2335
2537
|
finishedAt: time,
|
|
2336
|
-
result: objectSchema(
|
|
2538
|
+
result: objectSchema(resultFields(capacity), resultOptional),
|
|
2337
2539
|
disposition: string
|
|
2338
2540
|
},
|
|
2339
2541
|
activityOptional
|
|
@@ -2342,6 +2544,7 @@ function activityValidator() {
|
|
|
2342
2544
|
]
|
|
2343
2545
|
}
|
|
2344
2546
|
};
|
|
2547
|
+
return withBsonLimit(validator, capacity?.limits.activityBsonBytes);
|
|
2345
2548
|
}
|
|
2346
2549
|
|
|
2347
2550
|
// src/stores/mongoose/schema.ts
|
|
@@ -2381,18 +2584,24 @@ var activityIndexes = [
|
|
|
2381
2584
|
},
|
|
2382
2585
|
{ name: "v1_activity_stub_ttl", key: { stubDeleteAt: 1 }, expireAfterSeconds: 0 }
|
|
2383
2586
|
];
|
|
2384
|
-
function
|
|
2385
|
-
return
|
|
2587
|
+
function indexName(v1Name, capacity) {
|
|
2588
|
+
return capacity ? `v2_${v1Name.slice(3)}` : v1Name;
|
|
2589
|
+
}
|
|
2590
|
+
function laneIndexes(indexes, capacity) {
|
|
2591
|
+
return capacity ? indexes.map((index) => ({ ...index, name: indexName(index.name, capacity) })) : indexes;
|
|
2386
2592
|
}
|
|
2387
|
-
function
|
|
2388
|
-
return { name, validator:
|
|
2593
|
+
function jobSchema(name, capacity) {
|
|
2594
|
+
return { name, validator: jobValidator(capacity), indexes: laneIndexes(jobIndexes, capacity) };
|
|
2595
|
+
}
|
|
2596
|
+
function activitySchema(name, capacity) {
|
|
2597
|
+
return { name, validator: activityValidator(capacity), indexes: laneIndexes(activityIndexes, capacity) };
|
|
2389
2598
|
}
|
|
2390
2599
|
function simple(collation) {
|
|
2391
2600
|
return collation === void 0 || isDeepStrictEqual(collation, SIMPLE);
|
|
2392
2601
|
}
|
|
2393
2602
|
async function getCollection(io, binding, budget, name) {
|
|
2394
|
-
const
|
|
2395
|
-
const rows = batch(
|
|
2603
|
+
const result = await io.read(binding, budget, { listCollections: 1, filter: { name }, cursor: { batchSize: 2 } });
|
|
2604
|
+
const rows = batch(result, 1, `${binding.db.databaseName}.$cmd.listCollections`);
|
|
2396
2605
|
return rows[0] === void 0 ? void 0 : doc(rows[0], "store_incompatible");
|
|
2397
2606
|
}
|
|
2398
2607
|
function checkCollection(row, schema) {
|
|
@@ -2409,8 +2618,8 @@ function matchesIndex(raw, index) {
|
|
|
2409
2618
|
}
|
|
2410
2619
|
async function missingIndexes(io, binding, budget, { name, indexes }) {
|
|
2411
2620
|
const max = 64;
|
|
2412
|
-
const
|
|
2413
|
-
const defs = batch(
|
|
2621
|
+
const result = await io.read(binding, budget, { listIndexes: name, cursor: { batchSize: max + 1 } });
|
|
2622
|
+
const defs = batch(result, max, `${binding.db.databaseName}.${name}`);
|
|
2414
2623
|
const required = [{ name: "_id_", key: { _id: 1 } }, ...indexes];
|
|
2415
2624
|
const seen = /* @__PURE__ */ new Set();
|
|
2416
2625
|
for (const def of defs) {
|
|
@@ -2422,9 +2631,15 @@ async function missingIndexes(io, binding, budget, { name, indexes }) {
|
|
|
2422
2631
|
if (!seen.has("_id_")) invalid("store_incompatible");
|
|
2423
2632
|
return indexes.filter((index) => !seen.has(index.name));
|
|
2424
2633
|
}
|
|
2634
|
+
async function checkExistingCollection(io, binding, budget, schema) {
|
|
2635
|
+
const row = await getCollection(io, binding, budget, schema.name);
|
|
2636
|
+
if (!row) return;
|
|
2637
|
+
checkCollection(row, schema);
|
|
2638
|
+
await missingIndexes(io, binding, budget, schema);
|
|
2639
|
+
}
|
|
2425
2640
|
async function prepareCollection(io, binding, budget, schema) {
|
|
2426
2641
|
if (!await getCollection(io, binding, budget, schema.name)) {
|
|
2427
|
-
const
|
|
2642
|
+
const result = await io.write(
|
|
2428
2643
|
binding,
|
|
2429
2644
|
budget,
|
|
2430
2645
|
{
|
|
@@ -2436,12 +2651,12 @@ async function prepareCollection(io, binding, budget, schema) {
|
|
|
2436
2651
|
},
|
|
2437
2652
|
[48, 2, 9, 72, 115]
|
|
2438
2653
|
);
|
|
2439
|
-
if (
|
|
2654
|
+
if (result.kind === "definite_error" && result.code !== 48) invalid("store_incompatible");
|
|
2440
2655
|
}
|
|
2441
2656
|
checkCollection(await getCollection(io, binding, budget, schema.name), schema);
|
|
2442
2657
|
const missing = await missingIndexes(io, binding, budget, schema);
|
|
2443
2658
|
if (missing.length) {
|
|
2444
|
-
const
|
|
2659
|
+
const result = await io.write(
|
|
2445
2660
|
binding,
|
|
2446
2661
|
budget,
|
|
2447
2662
|
{
|
|
@@ -2450,7 +2665,7 @@ async function prepareCollection(io, binding, budget, schema) {
|
|
|
2450
2665
|
},
|
|
2451
2666
|
[2, 67, 68, 72, 85, 86, 11e3]
|
|
2452
2667
|
);
|
|
2453
|
-
if (
|
|
2668
|
+
if (result.kind === "definite_error") invalid("store_incompatible");
|
|
2454
2669
|
if ((await missingIndexes(io, binding, budget, schema)).length) invalid("store_incompatible");
|
|
2455
2670
|
}
|
|
2456
2671
|
}
|
|
@@ -2471,13 +2686,22 @@ function modifiedValue(reply) {
|
|
|
2471
2686
|
if (n !== (reply.value === null ? 0 : 1) || reply.value === void 0) invalid("write_unconfirmed");
|
|
2472
2687
|
return reply.value;
|
|
2473
2688
|
}
|
|
2474
|
-
function
|
|
2689
|
+
function capacityFilter(capacity) {
|
|
2690
|
+
const binding = capacity?.binding;
|
|
2691
|
+
return binding ? {
|
|
2692
|
+
"smtpCapacity.format": binding.format,
|
|
2693
|
+
"smtpCapacity.namespace": binding.namespace,
|
|
2694
|
+
"smtpCapacity.maxRecipients": binding.maxRecipients
|
|
2695
|
+
} : {};
|
|
2696
|
+
}
|
|
2697
|
+
function casFilter({ scope: scope2, jobId, revision, state, retiring, pendingAck, atOrAfter, before }, capacity) {
|
|
2475
2698
|
const lease = state.state === "leased" ? state.lease : void 0;
|
|
2476
2699
|
return {
|
|
2477
2700
|
_id: jobIdKey(scope2, jobId),
|
|
2478
2701
|
scope: scope2,
|
|
2479
2702
|
jobId,
|
|
2480
2703
|
version: EMAIL_FORMAT_VERSION,
|
|
2704
|
+
...capacityFilter(capacity),
|
|
2481
2705
|
revision,
|
|
2482
2706
|
state: state.state,
|
|
2483
2707
|
"lifecycle.state": state.state,
|
|
@@ -2504,16 +2728,18 @@ function casFilter({ scope: scope2, jobId, revision, state, retiring, pendingAck
|
|
|
2504
2728
|
};
|
|
2505
2729
|
}
|
|
2506
2730
|
var MongoOperations = class {
|
|
2507
|
-
constructor(io, binding, budget, names) {
|
|
2731
|
+
constructor(io, binding, budget, names, capacity) {
|
|
2508
2732
|
this.io = io;
|
|
2509
2733
|
this.binding = binding;
|
|
2510
2734
|
this.budget = budget;
|
|
2511
2735
|
this.names = names;
|
|
2736
|
+
this.capacity = capacity;
|
|
2512
2737
|
}
|
|
2513
2738
|
io;
|
|
2514
2739
|
binding;
|
|
2515
2740
|
budget;
|
|
2516
2741
|
names;
|
|
2742
|
+
capacity;
|
|
2517
2743
|
check() {
|
|
2518
2744
|
this.budget.check();
|
|
2519
2745
|
}
|
|
@@ -2527,7 +2753,7 @@ var MongoOperations = class {
|
|
|
2527
2753
|
const reply = await this.io.read(this.binding, this.budget, {
|
|
2528
2754
|
find,
|
|
2529
2755
|
filter,
|
|
2530
|
-
hint,
|
|
2756
|
+
hint: indexName(hint, this.capacity),
|
|
2531
2757
|
collation: SIMPLE,
|
|
2532
2758
|
readConcern: { level },
|
|
2533
2759
|
limit,
|
|
@@ -2541,7 +2767,7 @@ var MongoOperations = class {
|
|
|
2541
2767
|
async byJob(scope2, jobId) {
|
|
2542
2768
|
const [raw] = await this.find(this.names.jobsCollection, { scope: scope2, jobId }, JOB_ID, 1, "linearizable");
|
|
2543
2769
|
if (!raw) return;
|
|
2544
|
-
const job = readJobBson(scope2, raw);
|
|
2770
|
+
const job = readJobBson(scope2, raw, this.capacity);
|
|
2545
2771
|
if (job.jobId !== jobId) invalid("store_incompatible");
|
|
2546
2772
|
this.check();
|
|
2547
2773
|
return job;
|
|
@@ -2549,7 +2775,7 @@ var MongoOperations = class {
|
|
|
2549
2775
|
async byAdmission(scope2, admissionKey) {
|
|
2550
2776
|
const [raw] = await this.find(this.names.jobsCollection, { scope: scope2, admissionKey }, ADMISSION_ID, 1, "linearizable");
|
|
2551
2777
|
if (!raw) return;
|
|
2552
|
-
const job = readJobBson(scope2, raw);
|
|
2778
|
+
const job = readJobBson(scope2, raw, this.capacity);
|
|
2553
2779
|
if (job.admissionKey !== admissionKey) invalid("store_incompatible");
|
|
2554
2780
|
this.check();
|
|
2555
2781
|
return job;
|
|
@@ -2559,18 +2785,18 @@ var MongoOperations = class {
|
|
|
2559
2785
|
if (!raw) return;
|
|
2560
2786
|
const { jobId, attemptId } = doc(raw, "store_incompatible");
|
|
2561
2787
|
if (jobId !== identity.jobId || attemptId !== identity.attemptId) invalid("store_incompatible");
|
|
2562
|
-
const row = readActivityBson(identity.scope, raw);
|
|
2788
|
+
const row = readActivityBson(identity.scope, raw, this.capacity);
|
|
2563
2789
|
this.check();
|
|
2564
2790
|
return row;
|
|
2565
2791
|
}
|
|
2566
2792
|
async insert(job) {
|
|
2567
|
-
const
|
|
2793
|
+
const result = await this.io.write(this.binding, this.budget, {
|
|
2568
2794
|
insert: this.names.jobsCollection,
|
|
2569
2795
|
documents: [jobBson(job)]
|
|
2570
2796
|
});
|
|
2571
|
-
if (
|
|
2572
|
-
const { n, writeErrors } =
|
|
2573
|
-
if (n === 1 && !Object.hasOwn(
|
|
2797
|
+
if (result.kind !== "ack") invalid("write_unconfirmed");
|
|
2798
|
+
const { n, writeErrors } = result.reply;
|
|
2799
|
+
if (n === 1 && !Object.hasOwn(result.reply, "writeErrors")) return "inserted";
|
|
2574
2800
|
if (n === 0 && Array.isArray(writeErrors) && writeErrors.length === 1) {
|
|
2575
2801
|
const { code } = doc(writeErrors[0], "write_unconfirmed");
|
|
2576
2802
|
if (code === 121) invalid("store_incompatible");
|
|
@@ -2589,23 +2815,23 @@ var MongoOperations = class {
|
|
|
2589
2815
|
}
|
|
2590
2816
|
async replace(write) {
|
|
2591
2817
|
const { guard: guard2, next } = write;
|
|
2592
|
-
const
|
|
2818
|
+
const result = await this.io.write(
|
|
2593
2819
|
this.binding,
|
|
2594
2820
|
this.budget,
|
|
2595
2821
|
{
|
|
2596
2822
|
findAndModify: this.names.jobsCollection,
|
|
2597
|
-
query: casFilter(guard2),
|
|
2823
|
+
query: casFilter(guard2, this.capacity),
|
|
2598
2824
|
// Treat the whole replacement as literal, including rendered strings beginning with '$'.
|
|
2599
2825
|
update: [{ $replaceWith: { $literal: jobBson(next) } }],
|
|
2600
2826
|
fields: { _id: 1, revision: 1 },
|
|
2601
2827
|
new: true,
|
|
2602
|
-
hint: JOB_ID,
|
|
2828
|
+
hint: indexName(JOB_ID, this.capacity),
|
|
2603
2829
|
collation: SIMPLE
|
|
2604
2830
|
},
|
|
2605
2831
|
[121]
|
|
2606
2832
|
);
|
|
2607
|
-
if (
|
|
2608
|
-
const value = modifiedValue(
|
|
2833
|
+
if (result.kind === "definite_error") invalid("store_incompatible");
|
|
2834
|
+
const value = modifiedValue(result.reply);
|
|
2609
2835
|
if (value === null) return false;
|
|
2610
2836
|
const { _id, revision } = doc(value, "write_unconfirmed");
|
|
2611
2837
|
if (_id !== jobIdKey(next.scope, next.jobId) || revision !== next.revision) invalid("write_unconfirmed");
|
|
@@ -2620,7 +2846,7 @@ var MongoOperations = class {
|
|
|
2620
2846
|
const identity = { scope: scope2, jobId, attemptId };
|
|
2621
2847
|
const [raw] = await this.find(this.names.activitiesCollection, identity, ACTIVITY_ID, 1, "linearizable");
|
|
2622
2848
|
if (raw) {
|
|
2623
|
-
readActivityBson(scope2, raw);
|
|
2849
|
+
readActivityBson(scope2, raw, this.capacity);
|
|
2624
2850
|
exact.push({ ...identity, _id: readStoreId(doc(raw, "store_incompatible")._id, "store_incompatible") });
|
|
2625
2851
|
}
|
|
2626
2852
|
}
|
|
@@ -2631,9 +2857,9 @@ var MongoOperations = class {
|
|
|
2631
2857
|
delete: this.names.activitiesCollection,
|
|
2632
2858
|
// An exact indexed delete per attempt in one ordered command.
|
|
2633
2859
|
deletes: exact.map((identity) => ({
|
|
2634
|
-
q: { ...identity, $expr: serverWindow(guard2.atOrAfter) },
|
|
2860
|
+
q: { ...identity, ...capacityFilter(this.capacity), $expr: serverWindow(guard2.atOrAfter) },
|
|
2635
2861
|
limit: 1,
|
|
2636
|
-
hint: ACTIVITY_ID,
|
|
2862
|
+
hint: indexName(ACTIVITY_ID, this.capacity),
|
|
2637
2863
|
collation: SIMPLE
|
|
2638
2864
|
}))
|
|
2639
2865
|
});
|
|
@@ -2646,7 +2872,14 @@ var MongoOperations = class {
|
|
|
2646
2872
|
}
|
|
2647
2873
|
const outcome = await this.io.write(this.binding, this.budget, {
|
|
2648
2874
|
delete: this.names.jobsCollection,
|
|
2649
|
-
deletes: [
|
|
2875
|
+
deletes: [
|
|
2876
|
+
{
|
|
2877
|
+
q: casFilter(guard2, this.capacity),
|
|
2878
|
+
limit: 1,
|
|
2879
|
+
hint: indexName(JOB_ID, this.capacity),
|
|
2880
|
+
collation: SIMPLE
|
|
2881
|
+
}
|
|
2882
|
+
]
|
|
2650
2883
|
});
|
|
2651
2884
|
if (outcome.kind !== "ack") invalid("write_unconfirmed");
|
|
2652
2885
|
return writeCount(outcome.reply, 1) === 1;
|
|
@@ -2680,7 +2913,7 @@ var MongoOperations = class {
|
|
|
2680
2913
|
[due]: { $exists: true, $lte: selectionAt }
|
|
2681
2914
|
},
|
|
2682
2915
|
name,
|
|
2683
|
-
MAX_STORE_CANDIDATES,
|
|
2916
|
+
this.capacity?.limits.dueCandidates ?? MAX_STORE_CANDIDATES,
|
|
2684
2917
|
"majority",
|
|
2685
2918
|
sort,
|
|
2686
2919
|
{ _id: 0, jobId: 1 }
|
|
@@ -2715,15 +2948,15 @@ var MongoOperations = class {
|
|
|
2715
2948
|
const identity = activityKey(permit.activity);
|
|
2716
2949
|
const activity = activityBson(permit);
|
|
2717
2950
|
const candidateId = randomUUID();
|
|
2718
|
-
const
|
|
2951
|
+
const result = await this.io.write(
|
|
2719
2952
|
this.binding,
|
|
2720
2953
|
this.budget,
|
|
2721
2954
|
{
|
|
2722
2955
|
findAndModify: this.names.activitiesCollection,
|
|
2723
|
-
query: identity,
|
|
2956
|
+
query: { ...identity, ...capacityFilter(this.capacity) },
|
|
2724
2957
|
new: true,
|
|
2725
2958
|
upsert: true,
|
|
2726
|
-
hint: ACTIVITY_ID,
|
|
2959
|
+
hint: indexName(ACTIVITY_ID, this.capacity),
|
|
2727
2960
|
collation: SIMPLE,
|
|
2728
2961
|
update: [
|
|
2729
2962
|
{
|
|
@@ -2744,7 +2977,7 @@ var MongoOperations = class {
|
|
|
2744
2977
|
],
|
|
2745
2978
|
default: {
|
|
2746
2979
|
$mergeObjects: [
|
|
2747
|
-
{ $literal: identity },
|
|
2980
|
+
{ $literal: { ...identity, ...capacityTag(this.capacity) } },
|
|
2748
2981
|
{ _id: { $literal: candidateId }, stubDeleteAt: { $add: ["$$NOW", ACTIVITY_STUB_TTL_MS] } }
|
|
2749
2982
|
]
|
|
2750
2983
|
}
|
|
@@ -2755,10 +2988,14 @@ var MongoOperations = class {
|
|
|
2755
2988
|
},
|
|
2756
2989
|
[11e3, 121]
|
|
2757
2990
|
);
|
|
2758
|
-
if (
|
|
2991
|
+
if (result.kind === "definite_error" && result.code === 121) invalid("store_incompatible");
|
|
2759
2992
|
let confirmedComplete = false;
|
|
2760
|
-
if (
|
|
2761
|
-
const row = readActivityBson(
|
|
2993
|
+
if (result.kind === "ack") {
|
|
2994
|
+
const row = readActivityBson(
|
|
2995
|
+
identity.scope,
|
|
2996
|
+
doc(modifiedValue(result.reply), "write_unconfirmed"),
|
|
2997
|
+
this.capacity
|
|
2998
|
+
);
|
|
2762
2999
|
if (row && row.digest !== permit.digest) invalid("store_incompatible");
|
|
2763
3000
|
confirmedComplete = row !== void 0;
|
|
2764
3001
|
}
|
|
@@ -2816,7 +3053,7 @@ var MongoOperations = class {
|
|
|
2816
3053
|
await readRange(Object.keys(range).length ? { finishedAt: range } : {});
|
|
2817
3054
|
const page = activityPage(
|
|
2818
3055
|
criteria,
|
|
2819
|
-
rows.map((row) => readActivityBson(scope2, row)?.activity ?? invalid("store_incompatible"))
|
|
3056
|
+
rows.map((row) => readActivityBson(scope2, row, this.capacity)?.activity ?? invalid("store_incompatible"))
|
|
2820
3057
|
);
|
|
2821
3058
|
this.check();
|
|
2822
3059
|
return page;
|
|
@@ -2825,11 +3062,25 @@ var MongoOperations = class {
|
|
|
2825
3062
|
|
|
2826
3063
|
// src/mongoose.ts
|
|
2827
3064
|
var MAX_IN_FLIGHT = 32;
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
const input = readRecord(options, [],
|
|
3065
|
+
var NAME_KEYS = ["jobsCollection", "activitiesCollection", "jobsModelName", "activitiesModelName"];
|
|
3066
|
+
function readStoreConfig(options) {
|
|
3067
|
+
const input = readRecord(options, [], [...NAME_KEYS, "smtpCapacity"], "invalid_config");
|
|
3068
|
+
const capacity = readCapacityOption(input);
|
|
3069
|
+
if (capacity) {
|
|
3070
|
+
if (NAME_KEYS.some((key) => Object.hasOwn(input, key))) invalid("invalid_config");
|
|
3071
|
+
const { namespace } = capacity.binding;
|
|
3072
|
+
const prefix = `email_smtp_v2.${namespace}`;
|
|
3073
|
+
const modelId = namespace.replaceAll("-", "_");
|
|
3074
|
+
const names2 = Object.freeze({
|
|
3075
|
+
jobsCollection: `${prefix}.jobs`,
|
|
3076
|
+
activitiesCollection: `${prefix}.activities`,
|
|
3077
|
+
jobsModelName: `EmailSmtpV2Job_${modelId}`,
|
|
3078
|
+
activitiesModelName: `EmailSmtpV2Activity_${modelId}`
|
|
3079
|
+
});
|
|
3080
|
+
return { names: names2, capacity };
|
|
3081
|
+
}
|
|
2831
3082
|
const names = { ...MONGOOSE_DEFAULT_NAMES };
|
|
2832
|
-
for (const key of
|
|
3083
|
+
for (const key of NAME_KEYS) {
|
|
2833
3084
|
const value = input[key];
|
|
2834
3085
|
if (value === void 0) continue;
|
|
2835
3086
|
if (typeof value !== "string" || !/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(value)) invalid("invalid_config");
|
|
@@ -2837,18 +3088,18 @@ function readNames(options) {
|
|
|
2837
3088
|
}
|
|
2838
3089
|
if (names.jobsCollection === names.activitiesCollection || names.jobsModelName === names.activitiesModelName)
|
|
2839
3090
|
invalid("invalid_config");
|
|
2840
|
-
return Object.freeze(names);
|
|
3091
|
+
return { names: Object.freeze(names), capacity };
|
|
2841
3092
|
}
|
|
2842
3093
|
function createMongooseEmailStore(connection, options = {}) {
|
|
2843
3094
|
checkConnectionType(connection);
|
|
2844
|
-
const names =
|
|
3095
|
+
const { names, capacity } = readStoreConfig(options);
|
|
2845
3096
|
const io = new MongoConnection(connection, names);
|
|
2846
3097
|
let readyBinding;
|
|
2847
3098
|
let preparing = false;
|
|
2848
3099
|
let inFlight = 0;
|
|
2849
3100
|
async function run(call, worker, fn) {
|
|
2850
|
-
const { signal, timeoutMs } = readStoreCall(call, worker);
|
|
2851
|
-
if (inFlight >= MAX_IN_FLIGHT) invalid("busy");
|
|
3101
|
+
const { signal, timeoutMs } = readStoreCall(call, worker, capacity);
|
|
3102
|
+
if (inFlight >= (capacity?.limits.mongoInFlight ?? MAX_IN_FLIGHT)) invalid("busy");
|
|
2852
3103
|
const budget = createBudget(systemEmailClock, Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS), signal);
|
|
2853
3104
|
inFlight++;
|
|
2854
3105
|
try {
|
|
@@ -2856,13 +3107,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2856
3107
|
if (!readyBinding) invalid("store_unavailable");
|
|
2857
3108
|
const binding = io.bind();
|
|
2858
3109
|
if (binding.client !== readyBinding.client || binding.db !== readyBinding.db) invalid("store_unavailable");
|
|
2859
|
-
return await fn(new MongoOperations(io, binding, budget, names));
|
|
3110
|
+
return await fn(new MongoOperations(io, binding, budget, names, capacity));
|
|
2860
3111
|
} finally {
|
|
2861
3112
|
inFlight--;
|
|
2862
3113
|
budget.close();
|
|
2863
3114
|
}
|
|
2864
3115
|
}
|
|
2865
3116
|
const store = {
|
|
3117
|
+
...capacityTag(capacity),
|
|
2866
3118
|
async ready(options2 = {}) {
|
|
2867
3119
|
const { timeoutMs, signal } = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
|
|
2868
3120
|
if (preparing || inFlight) invalid("busy");
|
|
@@ -2873,8 +3125,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2873
3125
|
budget.check();
|
|
2874
3126
|
const binding = io.bind();
|
|
2875
3127
|
await io.verifyTopology(binding, budget);
|
|
2876
|
-
|
|
2877
|
-
|
|
3128
|
+
const jobs = jobSchema(names.jobsCollection, capacity);
|
|
3129
|
+
const activities = activitySchema(names.activitiesCollection, capacity);
|
|
3130
|
+
if (capacity) {
|
|
3131
|
+
await checkExistingCollection(io, binding, budget, jobs);
|
|
3132
|
+
await checkExistingCollection(io, binding, budget, activities);
|
|
3133
|
+
}
|
|
3134
|
+
await prepareCollection(io, binding, budget, jobs);
|
|
3135
|
+
await prepareCollection(io, binding, budget, activities);
|
|
2878
3136
|
const jobId = randomUUID2();
|
|
2879
3137
|
const probe = { scope: "!readiness", jobId };
|
|
2880
3138
|
for (const [find, filter, hint] of [
|
|
@@ -2885,7 +3143,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2885
3143
|
await io.read(binding, budget, {
|
|
2886
3144
|
find,
|
|
2887
3145
|
filter,
|
|
2888
|
-
hint,
|
|
3146
|
+
hint: indexName(hint, capacity),
|
|
2889
3147
|
collation: SIMPLE,
|
|
2890
3148
|
readConcern: { level: "linearizable" },
|
|
2891
3149
|
singleBatch: true,
|
|
@@ -2911,10 +3169,10 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2911
3169
|
},
|
|
2912
3170
|
insert(scope2, input, call) {
|
|
2913
3171
|
return run(call, false, async (ops) => {
|
|
2914
|
-
const safe = readStoreAdmission(scopeToken(scope2), input);
|
|
3172
|
+
const safe = readStoreAdmission(scopeToken(scope2), input, capacity);
|
|
2915
3173
|
if (await ops.byAdmission(scope2, safe.admissionKey)) return Object.freeze({ status: "duplicate" });
|
|
2916
3174
|
if (await ops.byJob(scope2, safe.jobId)) invalid("invalid_command");
|
|
2917
|
-
const job = newJob(safe, { now: await ops.now(), revision: randomUUID2() });
|
|
3175
|
+
const job = newJob(safe, { now: await ops.now(), revision: randomUUID2() }, capacity);
|
|
2918
3176
|
const summary = jobSummary(job);
|
|
2919
3177
|
return await ops.insert(job) === "inserted" ? Object.freeze({ status: "inserted", job: summary }) : Object.freeze({ status: "duplicate" });
|
|
2920
3178
|
});
|
|
@@ -2930,13 +3188,13 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2930
3188
|
const id = readStoreId(attemptId);
|
|
2931
3189
|
const job = await ops.byJob(scopeToken(scope2), readStoreId(jobId));
|
|
2932
3190
|
const commit = job?.attempts.find((entry) => entry.commit.attemptId === id)?.commit;
|
|
2933
|
-
return commit ? copyCommit(commit) : null;
|
|
3191
|
+
return commit ? copyCommit(commit, capacity) : null;
|
|
2934
3192
|
});
|
|
2935
3193
|
},
|
|
2936
3194
|
claimNext(scope2, input, call) {
|
|
2937
3195
|
return run(call, true, (ops) => {
|
|
2938
3196
|
const safe = readClaim(input);
|
|
2939
|
-
return ops.next(scopeToken(scope2), "nextAt", (job, time2) => claimJob(job, safe, time2));
|
|
3197
|
+
return ops.next(scopeToken(scope2), "nextAt", (job, time2) => claimJob(job, safe, time2, capacity));
|
|
2940
3198
|
});
|
|
2941
3199
|
},
|
|
2942
3200
|
renew(scope2, fence, leaseMs, call) {
|
|
@@ -2955,14 +3213,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2955
3213
|
},
|
|
2956
3214
|
finish(scope2, input, call) {
|
|
2957
3215
|
return run(call, true, (ops) => {
|
|
2958
|
-
const safe = readFinish(input);
|
|
2959
|
-
return ops.point(scopeToken(scope2), safe.fence.jobId, (job, time2) => finishJob(job, safe, time2));
|
|
3216
|
+
const safe = readFinish(input, capacity);
|
|
3217
|
+
return ops.point(scopeToken(scope2), safe.fence.jobId, (job, time2) => finishJob(job, safe, time2, capacity));
|
|
2960
3218
|
});
|
|
2961
3219
|
},
|
|
2962
3220
|
recoverNext(scope2, call) {
|
|
2963
3221
|
return run(call, true, (ops) => {
|
|
2964
3222
|
const operationId = randomUUID2();
|
|
2965
|
-
return ops.next(scopeToken(scope2), "leaseEndsAt", (job, time2) => recoverJob(job, operationId, time2));
|
|
3223
|
+
return ops.next(scopeToken(scope2), "leaseEndsAt", (job, time2) => recoverJob(job, operationId, time2, capacity));
|
|
2966
3224
|
});
|
|
2967
3225
|
},
|
|
2968
3226
|
expireNext(scope2, call) {
|
|
@@ -2971,7 +3229,11 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2971
3229
|
cancel(scope2, jobId, call) {
|
|
2972
3230
|
return run(call, false, async (ops) => {
|
|
2973
3231
|
const operationId = randomUUID2();
|
|
2974
|
-
return await ops.point(
|
|
3232
|
+
return await ops.point(
|
|
3233
|
+
scopeToken(scope2),
|
|
3234
|
+
readStoreId(jobId),
|
|
3235
|
+
(job, time2) => cancelJob(job, operationId, time2, capacity)
|
|
3236
|
+
) ?? Object.freeze({ status: "not_found" });
|
|
2975
3237
|
});
|
|
2976
3238
|
},
|
|
2977
3239
|
resolveUnknown(scope2, input, call) {
|
|
@@ -2981,11 +3243,15 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2981
3243
|
});
|
|
2982
3244
|
},
|
|
2983
3245
|
reserveProjection(scope2, call) {
|
|
2984
|
-
return run(
|
|
3246
|
+
return run(
|
|
3247
|
+
call,
|
|
3248
|
+
false,
|
|
3249
|
+
(ops) => ops.next(scopeToken(scope2), "pendingAt", (job, time2) => reserveJobProjection(job, time2, capacity))
|
|
3250
|
+
);
|
|
2985
3251
|
},
|
|
2986
3252
|
putActivity(scope2, input, call) {
|
|
2987
3253
|
return run(call, false, async (ops) => {
|
|
2988
|
-
const permit = readProjectionPermit(scopeToken(scope2), input);
|
|
3254
|
+
const permit = readProjectionPermit(scopeToken(scope2), input, capacity);
|
|
2989
3255
|
const identity = activityKey(permit.activity);
|
|
2990
3256
|
const job = await ops.byJob(scope2, identity.jobId);
|
|
2991
3257
|
const activity = await ops.byActivity(identity);
|
|
@@ -2997,7 +3263,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2997
3263
|
},
|
|
2998
3264
|
ackProjection(scope2, input, call) {
|
|
2999
3265
|
return run(call, false, async (ops) => {
|
|
3000
|
-
const permit = readProjectionPermit(scopeToken(scope2), input);
|
|
3266
|
+
const permit = readProjectionPermit(scopeToken(scope2), input, capacity);
|
|
3001
3267
|
const identity = activityKey(permit.activity);
|
|
3002
3268
|
return await ops.point(
|
|
3003
3269
|
scope2,
|
|
@@ -3007,7 +3273,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
3007
3273
|
});
|
|
3008
3274
|
},
|
|
3009
3275
|
listActivities(scope2, query, call) {
|
|
3010
|
-
return run(call, false, (ops) => ops.list(readActivityCriteria(scopeToken(scope2), query)));
|
|
3276
|
+
return run(call, false, (ops) => ops.list(readActivityCriteria(scopeToken(scope2), query, capacity)));
|
|
3011
3277
|
},
|
|
3012
3278
|
cleanupNext(scope2, call) {
|
|
3013
3279
|
return run(call, false, (ops) => ops.next(scopeToken(scope2), "nextCleanupAt", cleanupJob));
|