@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.js
CHANGED
|
@@ -275,6 +275,12 @@ function scopeToken(value, code = "invalid_command") {
|
|
|
275
275
|
function identifier(value, code) {
|
|
276
276
|
return opaqueToken(value, MESSAGE_LIMITS.identifierBytes, code);
|
|
277
277
|
}
|
|
278
|
+
function uuidV4(value, code) {
|
|
279
|
+
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)) {
|
|
280
|
+
invalid(code);
|
|
281
|
+
}
|
|
282
|
+
return value;
|
|
283
|
+
}
|
|
278
284
|
function hashToken(value, code) {
|
|
279
285
|
if (typeof value !== "string" || value.length !== 64 || !/^[0-9a-f]{64}$/.test(value)) invalid(code);
|
|
280
286
|
return value;
|
|
@@ -288,9 +294,9 @@ function jsonKey(key, code) {
|
|
|
288
294
|
|
|
289
295
|
// src/internal/canonical-json.ts
|
|
290
296
|
var treeLimits = { depth: MESSAGE_LIMITS.jsonDepth, values: MESSAGE_LIMITS.jsonValues };
|
|
291
|
-
function canonicalJson(input, maxBytes, code = "invalid_snapshot",
|
|
297
|
+
function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits2 = treeLimits) {
|
|
292
298
|
let bytesLeft = maxBytes;
|
|
293
|
-
let valuesLeft =
|
|
299
|
+
let valuesLeft = limits2.values;
|
|
294
300
|
const chunks = [];
|
|
295
301
|
const ancestors = /* @__PURE__ */ new Set();
|
|
296
302
|
function append(text, bytes = text.length) {
|
|
@@ -309,7 +315,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
309
315
|
append(JSON.stringify(text), bytes);
|
|
310
316
|
}
|
|
311
317
|
function visit(value2, depth) {
|
|
312
|
-
if (depth >
|
|
318
|
+
if (depth > limits2.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
|
|
313
319
|
switch (typeof value2) {
|
|
314
320
|
case "string":
|
|
315
321
|
appendString(value2);
|
|
@@ -333,7 +339,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
333
339
|
if (ancestors.has(value2)) invalid(code);
|
|
334
340
|
ancestors.add(value2);
|
|
335
341
|
try {
|
|
336
|
-
const maxChildren = depth ===
|
|
342
|
+
const maxChildren = depth === limits2.depth ? 0 : valuesLeft;
|
|
337
343
|
if (isArray(value2, code)) {
|
|
338
344
|
const length = denseArrayLength(value2, maxChildren, code);
|
|
339
345
|
if (length && 2 * length + 1 > bytesLeft) invalid(code);
|
|
@@ -376,6 +382,71 @@ function sha256(input) {
|
|
|
376
382
|
function hashJson(input, maxBytes, code = "invalid_snapshot") {
|
|
377
383
|
return sha256(canonicalJson(input, maxBytes, code).encoded);
|
|
378
384
|
}
|
|
385
|
+
function encodeEvidence(input, code, capacity) {
|
|
386
|
+
return canonicalJson(input, capacity?.limits.evidenceBytes ?? 8192, code, capacity?.limits.evidenceTree).encoded;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/internal/smtp-capacity.ts
|
|
390
|
+
var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
|
|
391
|
+
var MAX_RECIPIENTS = 1e3;
|
|
392
|
+
var KIB = 1024;
|
|
393
|
+
var limits = Object.freeze({
|
|
394
|
+
snapshotBytes: 2 * KIB * KIB,
|
|
395
|
+
snapshotTree: Object.freeze({ depth: 4, values: 4096 }),
|
|
396
|
+
evidenceBytes: 64 * KIB,
|
|
397
|
+
evidenceTree: Object.freeze({ depth: 5, values: 4096 }),
|
|
398
|
+
jobBsonBytes: 4 * KIB * KIB,
|
|
399
|
+
activityBsonBytes: 128 * KIB,
|
|
400
|
+
registryCache: Object.freeze({ min: 0, default: 8, max: 16 }),
|
|
401
|
+
registryPending: Object.freeze({ min: 1, default: 2, max: 4 }),
|
|
402
|
+
servicePending: 4,
|
|
403
|
+
workerProfiles: 16,
|
|
404
|
+
workerConcurrency: Object.freeze({ min: 1, default: 1, max: 2 }),
|
|
405
|
+
runClaims: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
406
|
+
runMaintenance: Object.freeze({ min: 0, default: 4, max: 16 }),
|
|
407
|
+
adminMaintenance: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
408
|
+
page: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
409
|
+
memoryJobs: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
410
|
+
mongoInFlight: 4,
|
|
411
|
+
dueCandidates: 4
|
|
412
|
+
});
|
|
413
|
+
function readBinding(input, code) {
|
|
414
|
+
const { format, namespace, maxRecipients } = readRecord(input, ["format", "namespace", "maxRecipients"], [], code);
|
|
415
|
+
if (format !== "smtp-v2") invalid(code);
|
|
416
|
+
return Object.freeze({
|
|
417
|
+
format,
|
|
418
|
+
namespace: uuidV4(namespace, code),
|
|
419
|
+
maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_RECIPIENTS, code)
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
function readCapacityOption(raw) {
|
|
423
|
+
if (!Object.hasOwn(raw, "smtpCapacity")) return;
|
|
424
|
+
return Object.freeze({ binding: readBinding(raw.smtpCapacity, "invalid_config"), limits });
|
|
425
|
+
}
|
|
426
|
+
function capacityTag(capacity) {
|
|
427
|
+
return capacity ? { smtpCapacity: capacity.binding } : {};
|
|
428
|
+
}
|
|
429
|
+
function recipientLimit(capacity) {
|
|
430
|
+
return capacity?.binding.maxRecipients ?? MESSAGE_LIMITS.recipients;
|
|
431
|
+
}
|
|
432
|
+
function matchesBinding(input, capacity, code) {
|
|
433
|
+
const binding = readBinding(input, code);
|
|
434
|
+
if (binding.namespace !== capacity.binding.namespace || binding.maxRecipients !== capacity.binding.maxRecipients) {
|
|
435
|
+
invalid(code);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function assertCapacityTag(raw, capacity, code) {
|
|
439
|
+
const tagged = Object.hasOwn(raw, "smtpCapacity");
|
|
440
|
+
if (capacity) {
|
|
441
|
+
if (!tagged) invalid(code);
|
|
442
|
+
matchesBinding(raw.smtpCapacity, capacity, code);
|
|
443
|
+
} else if (tagged) {
|
|
444
|
+
invalid(code);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function assertCapacityProvider(provider2, capacity, code) {
|
|
448
|
+
if (capacity && provider2.kind !== "smtp") invalid(code);
|
|
449
|
+
}
|
|
379
450
|
|
|
380
451
|
// src/internal/attempt-codecs.ts
|
|
381
452
|
var smtpStages = ["connect", "tls", "auth", "envelope", "data"];
|
|
@@ -415,9 +486,19 @@ function readAttemptReason(value, code) {
|
|
|
415
486
|
if (typeof value !== "string" || !Object.hasOwn(evidence, value)) invalid(code);
|
|
416
487
|
return value;
|
|
417
488
|
}
|
|
489
|
+
var recipientGroups = ["to", "cc", "bcc"];
|
|
418
490
|
function recipientCounts(envelope) {
|
|
419
491
|
return Object.freeze({ to: envelope.to.length, cc: envelope.cc.length, bcc: envelope.bcc.length });
|
|
420
492
|
}
|
|
493
|
+
function readRecipientCounts(input, code, capacity) {
|
|
494
|
+
const limit = recipientLimit(capacity);
|
|
495
|
+
const counts = readRecord(input, recipientGroups, [], code);
|
|
496
|
+
const to = integer(counts.to, 0, limit, code);
|
|
497
|
+
const cc = integer(counts.cc, 0, limit - to, code);
|
|
498
|
+
const bcc = integer(counts.bcc, 0, limit - to - cc, code);
|
|
499
|
+
if (to + cc + bcc === 0) invalid(code);
|
|
500
|
+
return Object.freeze({ to, cc, bcc });
|
|
501
|
+
}
|
|
421
502
|
function smtpMessageId(attemptId) {
|
|
422
503
|
return `<${attemptId}@email.invalid>`;
|
|
423
504
|
}
|
|
@@ -434,7 +515,7 @@ function readHint(input, reason, code) {
|
|
|
434
515
|
...hasFloor ? { notBefore: unixMs(raw.notBefore, code) } : {}
|
|
435
516
|
});
|
|
436
517
|
}
|
|
437
|
-
function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
518
|
+
function readAttemptResult(input, code = "invalid_command", projected = false, capacity) {
|
|
438
519
|
const raw = readRecord(
|
|
439
520
|
input,
|
|
440
521
|
["outcome", "retry", "stage", "reason"],
|
|
@@ -445,6 +526,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
445
526
|
const [outcome, retry, stages, origin] = evidence[reason];
|
|
446
527
|
const stage = stages.find((value) => value === raw.stage);
|
|
447
528
|
if (raw.outcome !== outcome || raw.retry !== retry || !stage) invalid(code);
|
|
529
|
+
if (capacity && (origin === "sendgrid" || stage === "http")) invalid(code);
|
|
448
530
|
const hasHint = Object.hasOwn(raw, "retryHint");
|
|
449
531
|
if (projected && hasHint || !projected && reason === "rate_deferred" && !hasHint) invalid(code);
|
|
450
532
|
const smtpSuccess = reason === "smtp_accepted" || reason === "smtp_partial";
|
|
@@ -454,14 +536,22 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
454
536
|
if (hasRecipients) {
|
|
455
537
|
if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
|
|
456
538
|
const seen = /* @__PURE__ */ new Set();
|
|
457
|
-
|
|
539
|
+
const limit = recipientLimit(capacity);
|
|
540
|
+
let previous = -1;
|
|
541
|
+
recipients = readArray(raw.recipients, limit, code, (value) => {
|
|
458
542
|
const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
|
|
459
543
|
if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
|
|
460
544
|
if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
|
|
461
|
-
const index = integer(rawIndex, 0,
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
545
|
+
const index = integer(rawIndex, 0, limit - 1, code);
|
|
546
|
+
if (capacity) {
|
|
547
|
+
const position = recipientGroups.indexOf(group) * limit + index;
|
|
548
|
+
if (position <= previous) invalid(code);
|
|
549
|
+
previous = position;
|
|
550
|
+
} else {
|
|
551
|
+
const key = `${group}:${index}`;
|
|
552
|
+
if (seen.has(key)) invalid(code);
|
|
553
|
+
seen.add(key);
|
|
554
|
+
}
|
|
465
555
|
return Object.freeze({ group, index, status });
|
|
466
556
|
});
|
|
467
557
|
const accepted = recipients.filter(({ status }) => status === "accepted").length;
|
|
@@ -476,7 +566,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
476
566
|
if (hasProviderId && (reason !== "sendgrid_accepted" || typeof providerMessageId !== "string" || providerMessageId.length > 128 || !/^[A-Za-z0-9][A-Za-z0-9._=-]{0,127}$/.test(providerMessageId))) {
|
|
477
567
|
invalid(code);
|
|
478
568
|
}
|
|
479
|
-
|
|
569
|
+
const result = Object.freeze({
|
|
480
570
|
outcome,
|
|
481
571
|
retry,
|
|
482
572
|
stage,
|
|
@@ -486,6 +576,8 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
486
576
|
...typeof messageId === "string" ? { messageId } : {},
|
|
487
577
|
...typeof providerMessageId === "string" ? { providerMessageId } : {}
|
|
488
578
|
});
|
|
579
|
+
if (capacity && !projected) encodeEvidence(result, code, capacity);
|
|
580
|
+
return result;
|
|
489
581
|
}
|
|
490
582
|
function assertEvidencePhase(reason, phase, source, code) {
|
|
491
583
|
const origin = evidence[reason][3];
|
|
@@ -496,8 +588,8 @@ function assertEvidencePhase(reason, phase, source, code) {
|
|
|
496
588
|
const allowed = phase === "pre_handoff" ? origin === "pre" : origin === "smtp" || origin === "sendgrid" || origin === "provider" || origin === "launch";
|
|
497
589
|
if (!allowed) invalid(code);
|
|
498
590
|
}
|
|
499
|
-
function assertEvidenceContext(
|
|
500
|
-
const { reason, stage, recipients, messageId } =
|
|
591
|
+
function assertEvidenceContext(result, provider2, attemptId, counts, code) {
|
|
592
|
+
const { reason, stage, recipients, messageId } = result;
|
|
501
593
|
const origin = evidence[reason][3];
|
|
502
594
|
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)) {
|
|
503
595
|
invalid(code);
|
|
@@ -515,19 +607,19 @@ function lifecycleEvidence(reason) {
|
|
|
515
607
|
const [outcome, retry] = evidence[reason];
|
|
516
608
|
return Object.freeze({ outcome, retry, reason, stage: "pre_handoff" });
|
|
517
609
|
}
|
|
518
|
-
function attemptEffect(
|
|
519
|
-
const { outcome, reason } =
|
|
610
|
+
function attemptEffect(result, attemptId, ordinal2, manual, policy, expiresAt, now) {
|
|
611
|
+
const { outcome, reason } = result;
|
|
520
612
|
if (outcome !== "unsubmitted" && outcome !== "rejected") return { disposition: outcome, reason };
|
|
521
613
|
if (reason === "cancelled" || reason === "expired") return { disposition: reason, reason };
|
|
522
614
|
const disposition = outcome === "unsubmitted" ? "failed" : "rejected";
|
|
523
|
-
if (
|
|
615
|
+
if (result.retry === "never") return { disposition, reason };
|
|
524
616
|
if (now >= expiresAt) return { disposition: "expired", reason: "expired" };
|
|
525
617
|
if (manual || ordinal2 >= policy.maxAttempts) return { disposition, reason: "attempts_exhausted" };
|
|
526
618
|
const cap = Math.min(policy.maxDelayMs, policy.baseDelayMs * 2 ** (ordinal2 - 1));
|
|
527
619
|
const half = Math.floor(cap / 2);
|
|
528
620
|
const jitter = half + Number.parseInt(sha256(attemptId).slice(0, 8), 16) % (cap - half + 1);
|
|
529
|
-
const delay = Math.max(jitter,
|
|
530
|
-
const retryAt = Math.max(now + Math.min(delay, MAX_UNIX_MS - now),
|
|
621
|
+
const delay = Math.max(jitter, result.retryHint?.delayMs ?? 0);
|
|
622
|
+
const retryAt = Math.max(now + Math.min(delay, MAX_UNIX_MS - now), result.retryHint?.notBefore ?? 0);
|
|
531
623
|
if (retryAt >= expiresAt) return { disposition: "expired", reason: "expired" };
|
|
532
624
|
return { disposition: "retry_scheduled", reason, retryAt };
|
|
533
625
|
}
|
|
@@ -571,10 +663,11 @@ function validateMailbox(input, code = SNAPSHOT) {
|
|
|
571
663
|
...Object.hasOwn(mailbox, "name") ? { name: headerString(mailbox.name, L.displayNameBytes, code) } : {}
|
|
572
664
|
});
|
|
573
665
|
}
|
|
574
|
-
function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
666
|
+
function validateEnvelope(input, kind, code = SNAPSHOT, capacity) {
|
|
667
|
+
const limit = recipientLimit(capacity);
|
|
575
668
|
const envelope = readRecord(input, ["from", "to", "cc", "bcc"], ["replyTo"], code);
|
|
576
669
|
const seen = /* @__PURE__ */ new Set();
|
|
577
|
-
const
|
|
670
|
+
const recipient = (raw) => {
|
|
578
671
|
const mailbox = validateMailbox(raw, code);
|
|
579
672
|
const address = mailbox.address.toLowerCase();
|
|
580
673
|
if (seen.has(address)) invalid(code);
|
|
@@ -582,9 +675,9 @@ function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
|
582
675
|
return mailbox;
|
|
583
676
|
};
|
|
584
677
|
const from = validateMailbox(envelope.from, code);
|
|
585
|
-
const to = readArray(envelope.to,
|
|
586
|
-
const cc = readArray(envelope.cc,
|
|
587
|
-
const bcc = readArray(envelope.bcc,
|
|
678
|
+
const to = readArray(envelope.to, limit, code, recipient);
|
|
679
|
+
const cc = readArray(envelope.cc, limit - to.length, code, recipient);
|
|
680
|
+
const bcc = readArray(envelope.bcc, limit - to.length - cc.length, code, recipient);
|
|
588
681
|
if (seen.size === 0 || kind === "sendgrid" && to.length === 0) invalid(code);
|
|
589
682
|
return Object.freeze({
|
|
590
683
|
from,
|
|
@@ -659,12 +752,17 @@ function validateExpiry(input, preparedAt, scheduleAt, code = SNAPSHOT) {
|
|
|
659
752
|
}
|
|
660
753
|
return expiresAt;
|
|
661
754
|
}
|
|
662
|
-
function hashSnapshot(snapshot) {
|
|
755
|
+
function hashSnapshot(snapshot, capacity) {
|
|
663
756
|
return sha256(
|
|
664
|
-
canonicalJson(
|
|
757
|
+
canonicalJson(
|
|
758
|
+
snapshot,
|
|
759
|
+
capacity?.limits.snapshotBytes ?? L.snapshotBytes,
|
|
760
|
+
SNAPSHOT,
|
|
761
|
+
capacity?.limits.snapshotTree ?? { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }
|
|
762
|
+
).encoded
|
|
665
763
|
);
|
|
666
764
|
}
|
|
667
|
-
function readSnapshot(input) {
|
|
765
|
+
function readSnapshot(input, capacity) {
|
|
668
766
|
const dto = readRecord(
|
|
669
767
|
input,
|
|
670
768
|
[
|
|
@@ -679,17 +777,19 @@ function readSnapshot(input) {
|
|
|
679
777
|
"attachments",
|
|
680
778
|
"correlation"
|
|
681
779
|
],
|
|
682
|
-
["subject", "html", "text", "templateId", "data"],
|
|
780
|
+
["subject", "html", "text", "templateId", "data", "smtpCapacity"],
|
|
683
781
|
SNAPSHOT
|
|
684
782
|
);
|
|
685
783
|
if (dto.version !== EMAIL_FORMAT_VERSION || dto.kind !== "smtp" && dto.kind !== "sendgrid") invalid(SNAPSHOT);
|
|
784
|
+
assertCapacityTag(dto, capacity, SNAPSHOT);
|
|
686
785
|
const provider2 = validateProviderRef(dto.provider);
|
|
687
786
|
if (provider2.kind !== dto.kind) invalid("provider_mismatch");
|
|
787
|
+
assertCapacityProvider(provider2, capacity, "provider_mismatch");
|
|
688
788
|
const preparedAt = unixMs(dto.preparedAt, SNAPSHOT);
|
|
689
789
|
const common = {
|
|
690
790
|
version: EMAIL_FORMAT_VERSION,
|
|
691
791
|
template: validateTemplateIdentity(dto.template),
|
|
692
|
-
envelope: validateEnvelope(dto.envelope, provider2.kind),
|
|
792
|
+
envelope: validateEnvelope(dto.envelope, provider2.kind, SNAPSHOT, capacity),
|
|
693
793
|
preparedAt,
|
|
694
794
|
expiresAt: validateExpiry(dto.expiresAt, preparedAt, null),
|
|
695
795
|
priority: integer(dto.priority, PRIORITY.min, PRIORITY.max, SNAPSHOT),
|
|
@@ -703,7 +803,7 @@ function readSnapshot(input) {
|
|
|
703
803
|
...Object.hasOwn(dto, "html") ? { html: dto.html } : {},
|
|
704
804
|
...Object.hasOwn(dto, "text") ? { text: dto.text } : {}
|
|
705
805
|
});
|
|
706
|
-
return Object.freeze({ ...common, kind: provider2.kind, provider: provider2, ...content });
|
|
806
|
+
return Object.freeze({ ...common, kind: provider2.kind, provider: provider2, ...capacityTag(capacity), ...content });
|
|
707
807
|
}
|
|
708
808
|
if (Object.hasOwn(dto, "subject") || Object.hasOwn(dto, "html") || Object.hasOwn(dto, "text")) invalid(SNAPSHOT);
|
|
709
809
|
return Object.freeze({
|
|
@@ -714,9 +814,9 @@ function readSnapshot(input) {
|
|
|
714
814
|
data: copyJsonObject(dto.data, L.dataBytes, SNAPSHOT)
|
|
715
815
|
});
|
|
716
816
|
}
|
|
717
|
-
function validateSnapshot(input) {
|
|
718
|
-
const snapshot = readSnapshot(input);
|
|
719
|
-
return { snapshot, snapshotHash: hashSnapshot(snapshot) };
|
|
817
|
+
function validateSnapshot(input, capacity) {
|
|
818
|
+
const snapshot = readSnapshot(input, capacity);
|
|
819
|
+
return { snapshot, snapshotHash: hashSnapshot(snapshot, capacity) };
|
|
720
820
|
}
|
|
721
821
|
|
|
722
822
|
// src/internal/budget.ts
|
|
@@ -841,42 +941,56 @@ function validateAdmissionKey(input, code = "invalid_command") {
|
|
|
841
941
|
function encodeAdmissionKey(key) {
|
|
842
942
|
return key.kind === "single" ? `s:${key.idempotencyKey}` : `b:${key.batchId}:${key.itemId}`;
|
|
843
943
|
}
|
|
844
|
-
function fingerprintFor(identity) {
|
|
944
|
+
function fingerprintFor(identity, capacity) {
|
|
845
945
|
const { scope: scope2, key, templateKey, provider: provider2, scheduleAt, intentHash } = identity;
|
|
846
946
|
return hashJson(
|
|
847
|
-
[
|
|
947
|
+
[
|
|
948
|
+
...capacity ? ["smtp-v2-admission", capacity.binding] : [],
|
|
949
|
+
EMAIL_FORMAT_VERSION,
|
|
950
|
+
scope2,
|
|
951
|
+
key,
|
|
952
|
+
templateKey,
|
|
953
|
+
provider2,
|
|
954
|
+
scheduleAt,
|
|
955
|
+
intentHash
|
|
956
|
+
],
|
|
848
957
|
MESSAGE_LIMITS.intentBytes
|
|
849
958
|
);
|
|
850
959
|
}
|
|
851
|
-
function validatePreparedCommand(scope2, input) {
|
|
960
|
+
function validatePreparedCommand(scope2, input, capacity) {
|
|
852
961
|
const expectedScope = scopeToken(scope2);
|
|
853
962
|
const code = "invalid_snapshot";
|
|
854
963
|
const dto = readRecord(
|
|
855
964
|
input,
|
|
856
965
|
["version", "scope", "key", "scheduleAt", "intentHash", "fingerprint", "snapshotHash", "snapshot"],
|
|
857
|
-
[],
|
|
966
|
+
["smtpCapacity"],
|
|
858
967
|
code
|
|
859
968
|
);
|
|
860
969
|
if (dto.version !== EMAIL_FORMAT_VERSION || scopeToken(dto.scope, code) !== expectedScope) invalid(code);
|
|
970
|
+
assertCapacityTag(dto, capacity, code);
|
|
861
971
|
const key = validateAdmissionKey(dto.key, code);
|
|
862
972
|
const scheduleAt = dto.scheduleAt === null ? null : unixMs(dto.scheduleAt, code);
|
|
863
973
|
const intentHash = hashToken(dto.intentHash, code);
|
|
864
974
|
const fingerprint = hashToken(dto.fingerprint, code);
|
|
865
975
|
const storedSnapshotHash = hashToken(dto.snapshotHash, code);
|
|
866
|
-
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot);
|
|
976
|
+
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot, capacity);
|
|
867
977
|
validateExpiry(snapshot.expiresAt, snapshot.preparedAt, scheduleAt);
|
|
868
|
-
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
978
|
+
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
979
|
+
{
|
|
980
|
+
scope: expectedScope,
|
|
981
|
+
key,
|
|
982
|
+
templateKey: snapshot.template.key,
|
|
983
|
+
provider: snapshot.provider,
|
|
984
|
+
scheduleAt,
|
|
985
|
+
intentHash
|
|
986
|
+
},
|
|
987
|
+
capacity
|
|
988
|
+
)) {
|
|
876
989
|
invalid(code);
|
|
877
990
|
}
|
|
878
991
|
return Object.freeze({
|
|
879
992
|
version: EMAIL_FORMAT_VERSION,
|
|
993
|
+
...capacityTag(capacity),
|
|
880
994
|
scope: expectedScope,
|
|
881
995
|
key,
|
|
882
996
|
scheduleAt,
|
|
@@ -891,10 +1005,7 @@ function validatePreparedCommand(scope2, input) {
|
|
|
891
1005
|
var REQUEST = "invalid_command";
|
|
892
1006
|
var LEASE = WORKER_LIMIT_BOUNDS.leaseMs;
|
|
893
1007
|
function readStoreId(value, code = REQUEST) {
|
|
894
|
-
|
|
895
|
-
invalid(code);
|
|
896
|
-
}
|
|
897
|
-
return value;
|
|
1008
|
+
return uuidV4(value, code);
|
|
898
1009
|
}
|
|
899
1010
|
function readLeaseToken(value, code = REQUEST) {
|
|
900
1011
|
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)) {
|
|
@@ -908,14 +1019,18 @@ function readLeaseMs(value) {
|
|
|
908
1019
|
function timeAfter(start, delayMs) {
|
|
909
1020
|
return delayMs <= MAX_UNIX_MS - start ? start + delayMs : void 0;
|
|
910
1021
|
}
|
|
911
|
-
function readStoreCall(input, worker = false) {
|
|
1022
|
+
function readStoreCall(input, worker = false, capacity) {
|
|
912
1023
|
const code = "invalid_config";
|
|
913
|
-
const raw = readRecord(input, ["signal", "timeoutMs"], [], code);
|
|
1024
|
+
const { smtpCapacity, ...raw } = readRecord(input, ["signal", "timeoutMs"], capacity ? ["smtpCapacity"] : [], code);
|
|
1025
|
+
if (capacity) assertCapacityTag({ smtpCapacity }, capacity, "store_incompatible");
|
|
914
1026
|
const max = worker ? WORKER_LIMIT_BOUNDS.storeTimeoutMs.max : CALL_BUDGETS.admissionOrAdmin.max;
|
|
915
1027
|
const timeoutMs = integer(raw.timeoutMs, 1, max, code);
|
|
916
1028
|
const call = readCallOptions(raw, { default: STORE_PRIMITIVE_TIMEOUT_MS, min: 1, max });
|
|
917
1029
|
if (!call.signal) invalid(code);
|
|
918
|
-
return {
|
|
1030
|
+
return {
|
|
1031
|
+
signal: call.signal,
|
|
1032
|
+
timeoutMs: worker && !capacity ? timeoutMs : Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS)
|
|
1033
|
+
};
|
|
919
1034
|
}
|
|
920
1035
|
function readRetryPolicy(input, code = REQUEST) {
|
|
921
1036
|
const raw = readRecord(input, ["maxAttempts", "baseDelayMs", "maxDelayMs"], [], code);
|
|
@@ -941,9 +1056,9 @@ function readRetentionPolicy(input, code = REQUEST) {
|
|
|
941
1056
|
idempotencyMs: integer(idempotencyMs, bounds.idempotencyMs.min, bounds.idempotencyMs.max, code)
|
|
942
1057
|
});
|
|
943
1058
|
}
|
|
944
|
-
function readStoreAdmission(scope2, input) {
|
|
1059
|
+
function readStoreAdmission(scope2, input, capacity) {
|
|
945
1060
|
const raw = readRecord(input, ["command", "jobId", "retry", "retention"], [], REQUEST);
|
|
946
|
-
const command = validatePreparedCommand(scope2, raw.command);
|
|
1061
|
+
const command = validatePreparedCommand(scope2, raw.command, capacity);
|
|
947
1062
|
return Object.freeze({
|
|
948
1063
|
command,
|
|
949
1064
|
admissionKey: encodeAdmissionKey(command.key),
|
|
@@ -964,15 +1079,15 @@ function readClaim(input) {
|
|
|
964
1079
|
leaseMs: readLeaseMs(leaseMs)
|
|
965
1080
|
});
|
|
966
1081
|
}
|
|
967
|
-
function readFinish(input) {
|
|
968
|
-
const { fence, phase, operationId, result
|
|
1082
|
+
function readFinish(input, capacity) {
|
|
1083
|
+
const { fence, phase, operationId, result } = readRecord(
|
|
969
1084
|
input,
|
|
970
1085
|
["fence", "phase", "operationId", "result"],
|
|
971
1086
|
[],
|
|
972
1087
|
REQUEST
|
|
973
1088
|
);
|
|
974
1089
|
if (phase !== "pre_handoff" && phase !== "handed_off") invalid(REQUEST);
|
|
975
|
-
const evidence2 = readAttemptResult(
|
|
1090
|
+
const evidence2 = readAttemptResult(result, REQUEST, false, capacity);
|
|
976
1091
|
assertEvidencePhase(evidence2.reason, phase, "owner", REQUEST);
|
|
977
1092
|
return Object.freeze({ fence: readFence(fence), phase, operationId: readStoreId(operationId), result: evidence2 });
|
|
978
1093
|
}
|
|
@@ -989,7 +1104,6 @@ function readResolution(input) {
|
|
|
989
1104
|
}
|
|
990
1105
|
|
|
991
1106
|
// src/internal/activity-codecs.ts
|
|
992
|
-
var ACTIVITY_BYTES = 8192;
|
|
993
1107
|
var dispositions = [
|
|
994
1108
|
"accepted",
|
|
995
1109
|
"partially_accepted",
|
|
@@ -1006,17 +1120,17 @@ function readDisposition(input, code) {
|
|
|
1006
1120
|
if (input === "retry_scheduled") return input;
|
|
1007
1121
|
return invalid(code);
|
|
1008
1122
|
}
|
|
1009
|
-
function assertActivityDisposition(
|
|
1010
|
-
const { outcome, reason } =
|
|
1123
|
+
function assertActivityDisposition(result, disposition, code) {
|
|
1124
|
+
const { outcome, reason } = result;
|
|
1011
1125
|
if (outcome !== "rejected" && outcome !== "unsubmitted") {
|
|
1012
1126
|
if (disposition !== outcome) invalid(code);
|
|
1013
1127
|
} else if (reason === "expired" || reason === "cancelled") {
|
|
1014
1128
|
if (disposition !== reason) invalid(code);
|
|
1015
|
-
} else if (disposition !== (outcome === "rejected" ? "rejected" : "failed") && !(
|
|
1129
|
+
} else if (disposition !== (outcome === "rejected" ? "rejected" : "failed") && !(result.retry === "safe_transient" && (disposition === "expired" || disposition === "retry_scheduled"))) {
|
|
1016
1130
|
invalid(code);
|
|
1017
1131
|
}
|
|
1018
1132
|
}
|
|
1019
|
-
function
|
|
1133
|
+
function captureActivity(scope2, input, code, capacity) {
|
|
1020
1134
|
const raw = readRecord(
|
|
1021
1135
|
input,
|
|
1022
1136
|
[
|
|
@@ -1033,26 +1147,32 @@ function readActivity(scope2, input, code = "store_incompatible") {
|
|
|
1033
1147
|
"result",
|
|
1034
1148
|
"disposition"
|
|
1035
1149
|
],
|
|
1036
|
-
["handedOffAt"],
|
|
1150
|
+
["handedOffAt", "smtpCapacity", "recipientCounts"],
|
|
1037
1151
|
code
|
|
1038
1152
|
);
|
|
1039
1153
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, code) !== scope2) invalid(code);
|
|
1154
|
+
assertCapacityTag(raw, capacity, code);
|
|
1155
|
+
if (!capacity && Object.hasOwn(raw, "recipientCounts")) invalid(code);
|
|
1156
|
+
const counts = capacity ? readRecipientCounts(raw.recipientCounts, code, capacity) : void 0;
|
|
1040
1157
|
const jobId = readStoreId(raw.jobId, code);
|
|
1041
1158
|
const attemptId = readStoreId(raw.attemptId, code);
|
|
1042
1159
|
const provider2 = validateProviderRef(raw.provider, code);
|
|
1160
|
+
assertCapacityProvider(provider2, capacity, code);
|
|
1043
1161
|
const startedAt = unixMs(raw.startedAt, code);
|
|
1044
1162
|
const finishedAt = unixMs(raw.finishedAt, code);
|
|
1045
1163
|
const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, code) : void 0;
|
|
1046
1164
|
if (finishedAt < startedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt > finishedAt)) {
|
|
1047
1165
|
invalid(code);
|
|
1048
1166
|
}
|
|
1049
|
-
const
|
|
1050
|
-
assertEvidencePhase(
|
|
1051
|
-
assertEvidenceContext(
|
|
1167
|
+
const result = readAttemptResult(raw.result, code, true, capacity);
|
|
1168
|
+
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", code);
|
|
1169
|
+
assertEvidenceContext(result, provider2, attemptId, counts, code);
|
|
1052
1170
|
const disposition = readDisposition(raw.disposition, code);
|
|
1053
|
-
assertActivityDisposition(
|
|
1171
|
+
assertActivityDisposition(result, disposition, code);
|
|
1054
1172
|
return Object.freeze({
|
|
1055
1173
|
version: EMAIL_FORMAT_VERSION,
|
|
1174
|
+
...capacityTag(capacity),
|
|
1175
|
+
...counts ? { recipientCounts: counts } : {},
|
|
1056
1176
|
scope: scope2,
|
|
1057
1177
|
jobId,
|
|
1058
1178
|
attemptId,
|
|
@@ -1063,54 +1183,66 @@ function readActivity(scope2, input, code = "store_incompatible") {
|
|
|
1063
1183
|
startedAt,
|
|
1064
1184
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1065
1185
|
finishedAt,
|
|
1066
|
-
result
|
|
1186
|
+
result,
|
|
1067
1187
|
disposition
|
|
1068
1188
|
});
|
|
1069
1189
|
}
|
|
1070
|
-
function
|
|
1190
|
+
function readActivity(scope2, input, code = "store_incompatible", capacity) {
|
|
1191
|
+
const activity = captureActivity(scope2, input, code, capacity);
|
|
1192
|
+
if (capacity) encodeEvidence(activity, code, capacity);
|
|
1193
|
+
return activity;
|
|
1194
|
+
}
|
|
1195
|
+
function activityFor(job, commit, capacity) {
|
|
1071
1196
|
const { outcome, retry, stage, reason, recipients, messageId, providerMessageId } = commit.result;
|
|
1072
|
-
return
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1197
|
+
return captureActivity(
|
|
1198
|
+
job.scope,
|
|
1199
|
+
{
|
|
1200
|
+
version: EMAIL_FORMAT_VERSION,
|
|
1201
|
+
...capacityTag(capacity),
|
|
1202
|
+
...capacity ? { recipientCounts: job.recipientCounts } : {},
|
|
1203
|
+
scope: job.scope,
|
|
1204
|
+
jobId: job.jobId,
|
|
1205
|
+
attemptId: commit.attemptId,
|
|
1206
|
+
ordinal: commit.ordinal,
|
|
1207
|
+
provider: job.provider,
|
|
1208
|
+
template: job.template,
|
|
1209
|
+
correlation: job.correlation,
|
|
1210
|
+
startedAt: commit.startedAt,
|
|
1211
|
+
...commit.handedOffAt === void 0 ? {} : { handedOffAt: commit.handedOffAt },
|
|
1212
|
+
finishedAt: commit.finishedAt,
|
|
1213
|
+
disposition: commit.disposition,
|
|
1214
|
+
result: {
|
|
1215
|
+
outcome,
|
|
1216
|
+
retry,
|
|
1217
|
+
stage,
|
|
1218
|
+
reason,
|
|
1219
|
+
...recipients === void 0 ? {} : { recipients },
|
|
1220
|
+
...messageId === void 0 ? {} : { messageId },
|
|
1221
|
+
...providerMessageId === void 0 ? {} : { providerMessageId }
|
|
1222
|
+
}
|
|
1223
|
+
},
|
|
1224
|
+
"store_incompatible",
|
|
1225
|
+
capacity
|
|
1226
|
+
);
|
|
1095
1227
|
}
|
|
1096
|
-
function activityDigest(activity) {
|
|
1097
|
-
return
|
|
1228
|
+
function activityDigest(activity, capacity) {
|
|
1229
|
+
return sha256(encodeEvidence(activity, "store_incompatible", capacity));
|
|
1098
1230
|
}
|
|
1099
|
-
function readStoredActivity(scope2, input, code = "store_incompatible") {
|
|
1231
|
+
function readStoredActivity(scope2, input, code = "store_incompatible", capacity) {
|
|
1100
1232
|
const raw = readRecord(input, ["activity", "digest"], [], code);
|
|
1101
|
-
const activity =
|
|
1233
|
+
const activity = captureActivity(scope2, raw.activity, code, capacity);
|
|
1102
1234
|
const digest2 = hashToken(raw.digest, code);
|
|
1103
|
-
if (activityDigest(activity) !== digest2) invalid(code);
|
|
1235
|
+
if (activityDigest(activity, capacity) !== digest2) invalid(code);
|
|
1104
1236
|
return Object.freeze({ activity, digest: digest2 });
|
|
1105
1237
|
}
|
|
1106
|
-
function readProjectionPermit(scope2, input) {
|
|
1238
|
+
function readProjectionPermit(scope2, input, capacity) {
|
|
1107
1239
|
const code = "invalid_command";
|
|
1108
1240
|
const {
|
|
1109
1241
|
activity,
|
|
1110
1242
|
digest: digest2,
|
|
1111
1243
|
permitUntil: rawUntil
|
|
1112
1244
|
} = readRecord(input, ["activity", "digest", "permitUntil"], [], code);
|
|
1113
|
-
const stored = readStoredActivity(scope2, { activity, digest: digest2 }, code);
|
|
1245
|
+
const stored = readStoredActivity(scope2, { activity, digest: digest2 }, code, capacity);
|
|
1114
1246
|
const permitUntil = unixMs(rawUntil, code);
|
|
1115
1247
|
if (permitUntil - stored.activity.finishedAt < PROJECTION_PERMIT_MS) invalid(code);
|
|
1116
1248
|
return Object.freeze({ ...stored, permitUntil });
|
|
@@ -1122,13 +1254,15 @@ function compareActivity(a, b) {
|
|
|
1122
1254
|
return b.finishedAt - a.finishedAt || compareAscii(a.jobId, b.jobId) || compareAscii(a.attemptId, b.attemptId);
|
|
1123
1255
|
}
|
|
1124
1256
|
function cursorPayload(criteria, position) {
|
|
1257
|
+
const binding = criteria.capacity?.binding;
|
|
1125
1258
|
return {
|
|
1126
1259
|
v: EMAIL_FORMAT_VERSION,
|
|
1127
1260
|
s: criteria.scope,
|
|
1128
1261
|
j: criteria.jobId,
|
|
1129
1262
|
f: criteria.from,
|
|
1130
1263
|
b: criteria.before,
|
|
1131
|
-
p: [position.finishedAt, position.jobId, position.attemptId]
|
|
1264
|
+
p: [position.finishedAt, position.jobId, position.attemptId],
|
|
1265
|
+
...binding ? { c: [binding.format, binding.namespace, binding.maxRecipients] } : {}
|
|
1132
1266
|
};
|
|
1133
1267
|
}
|
|
1134
1268
|
function encodeCursor(criteria, position) {
|
|
@@ -1149,7 +1283,7 @@ function decodeCursor(criteria, cursor) {
|
|
|
1149
1283
|
} catch {
|
|
1150
1284
|
invalid(code);
|
|
1151
1285
|
}
|
|
1152
|
-
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], [], code);
|
|
1286
|
+
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], criteria.capacity ? ["c"] : [], code);
|
|
1153
1287
|
const tuple = readArray(p, 3, code, (entry) => entry);
|
|
1154
1288
|
const position = Object.freeze({
|
|
1155
1289
|
finishedAt: unixMs(tuple[0], code),
|
|
@@ -1161,15 +1295,16 @@ function decodeCursor(criteria, cursor) {
|
|
|
1161
1295
|
}
|
|
1162
1296
|
return position;
|
|
1163
1297
|
}
|
|
1164
|
-
function readActivityCriteria(scope2, input) {
|
|
1298
|
+
function readActivityCriteria(scope2, input, capacity) {
|
|
1165
1299
|
const code = "invalid_command";
|
|
1166
1300
|
const raw = readRecord(input, [], ["jobId", "from", "before", "limit", "cursor"], code);
|
|
1167
1301
|
const jobId = Object.hasOwn(raw, "jobId") ? readStoreId(raw.jobId, code) : null;
|
|
1168
1302
|
const from = Object.hasOwn(raw, "from") ? unixMs(raw.from, code) : null;
|
|
1169
1303
|
const before = Object.hasOwn(raw, "before") ? unixMs(raw.before, code) : null;
|
|
1170
1304
|
if (from !== null && before !== null && from > before) invalid(code);
|
|
1171
|
-
const
|
|
1172
|
-
const
|
|
1305
|
+
const page = capacity?.limits.page ?? ACTIVITY_PAGE_LIMIT;
|
|
1306
|
+
const limit = Object.hasOwn(raw, "limit") ? integer(raw.limit, page.min, page.max, code) : page.default;
|
|
1307
|
+
const criteria = { scope: scope2, jobId, from, before, limit, ...capacity ? { capacity } : {} };
|
|
1173
1308
|
return Object.freeze({
|
|
1174
1309
|
...criteria,
|
|
1175
1310
|
...Object.hasOwn(raw, "cursor") ? { after: decodeCursor(criteria, raw.cursor) } : {}
|
|
@@ -1179,7 +1314,7 @@ function readOrderedActivities(criteria, input, maxRows) {
|
|
|
1179
1314
|
const code = "store_incompatible";
|
|
1180
1315
|
let previous = criteria.after;
|
|
1181
1316
|
return readArray(input, maxRows, code, (row) => {
|
|
1182
|
-
const activity = readActivity(criteria.scope, row);
|
|
1317
|
+
const activity = readActivity(criteria.scope, row, code, criteria.capacity);
|
|
1183
1318
|
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) {
|
|
1184
1319
|
invalid(code);
|
|
1185
1320
|
}
|
|
@@ -1270,6 +1405,7 @@ function jobSummary(job) {
|
|
|
1270
1405
|
const latest = job.attempts.at(-1)?.commit;
|
|
1271
1406
|
return Object.freeze({
|
|
1272
1407
|
version: EMAIL_FORMAT_VERSION,
|
|
1408
|
+
...job.smtpCapacity ? { smtpCapacity: job.smtpCapacity } : {},
|
|
1273
1409
|
scope: job.scope,
|
|
1274
1410
|
jobId: job.jobId,
|
|
1275
1411
|
key: Object.freeze({ ...job.key }),
|
|
@@ -1294,13 +1430,13 @@ function jobSummary(job) {
|
|
|
1294
1430
|
...summaryState
|
|
1295
1431
|
});
|
|
1296
1432
|
}
|
|
1297
|
-
function copyCommit(commit) {
|
|
1298
|
-
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED2) });
|
|
1433
|
+
function copyCommit(commit, capacity) {
|
|
1434
|
+
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED2, false, capacity) });
|
|
1299
1435
|
}
|
|
1300
|
-
function readStoredSnapshot(job, input) {
|
|
1436
|
+
function readStoredSnapshot(job, input, capacity) {
|
|
1301
1437
|
let validated;
|
|
1302
1438
|
try {
|
|
1303
|
-
validated = validateSnapshot(input);
|
|
1439
|
+
validated = validateSnapshot(input, capacity);
|
|
1304
1440
|
} catch {
|
|
1305
1441
|
return invalid(STORED2);
|
|
1306
1442
|
}
|
|
@@ -1367,7 +1503,7 @@ function readState(input) {
|
|
|
1367
1503
|
}
|
|
1368
1504
|
return invalid(STORED2);
|
|
1369
1505
|
}
|
|
1370
|
-
function readLedgerAttempt(input, job) {
|
|
1506
|
+
function readLedgerAttempt(input, job, capacity) {
|
|
1371
1507
|
const entry = readRecord(input, ["commit", "digest"], ["ownerDigest", "permitUntil", "projectionAckAt"], STORED2);
|
|
1372
1508
|
const raw = readRecord(
|
|
1373
1509
|
entry.commit,
|
|
@@ -1380,14 +1516,14 @@ function readLedgerAttempt(input, job) {
|
|
|
1380
1516
|
const handedOffAt = optionalTime(raw, "handedOffAt");
|
|
1381
1517
|
const finishedAt = unixMs(raw.finishedAt, STORED2);
|
|
1382
1518
|
const retryAt = optionalTime(raw, "retryAt");
|
|
1383
|
-
const
|
|
1519
|
+
const result = readAttemptResult(raw.result, STORED2, false, capacity);
|
|
1384
1520
|
const ownerDigest = Object.hasOwn(entry, "ownerDigest") ? hashToken(entry.ownerDigest, STORED2) : void 0;
|
|
1385
|
-
const internal =
|
|
1386
|
-
if (raw.jobId !== job.jobId || startedAt >= job.expiresAt || finishedAt < startedAt || finishedAt > job.updatedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt >= job.expiresAt || handedOffAt > finishedAt) ||
|
|
1521
|
+
const internal = result.reason === "pre_handoff_lease_expired" || result.reason === "post_handoff_lease_expired" || result.reason === "cancelled";
|
|
1522
|
+
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) {
|
|
1387
1523
|
invalid(STORED2);
|
|
1388
1524
|
}
|
|
1389
|
-
assertEvidencePhase(
|
|
1390
|
-
assertEvidenceContext(
|
|
1525
|
+
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", STORED2);
|
|
1526
|
+
assertEvidenceContext(result, job.provider, attemptId, job.recipientCounts, STORED2);
|
|
1391
1527
|
const commit = Object.freeze({
|
|
1392
1528
|
jobId: job.jobId,
|
|
1393
1529
|
attemptId,
|
|
@@ -1396,7 +1532,7 @@ function readLedgerAttempt(input, job) {
|
|
|
1396
1532
|
startedAt,
|
|
1397
1533
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1398
1534
|
finishedAt,
|
|
1399
|
-
result
|
|
1535
|
+
result,
|
|
1400
1536
|
disposition: readDisposition(raw.disposition, STORED2),
|
|
1401
1537
|
reason: raw.reason === "attempts_exhausted" ? raw.reason : readAttemptReason(raw.reason, STORED2),
|
|
1402
1538
|
...retryAt === void 0 ? {} : { retryAt }
|
|
@@ -1407,7 +1543,7 @@ function readLedgerAttempt(input, job) {
|
|
|
1407
1543
|
invalid(STORED2);
|
|
1408
1544
|
}
|
|
1409
1545
|
const digest2 = hashToken(entry.digest, STORED2);
|
|
1410
|
-
if (digest2 !== activityDigest(activityFor(job, commit))) invalid(STORED2);
|
|
1546
|
+
if (digest2 !== activityDigest(activityFor(job, commit, capacity), capacity)) invalid(STORED2);
|
|
1411
1547
|
return Object.freeze({
|
|
1412
1548
|
commit,
|
|
1413
1549
|
digest: digest2,
|
|
@@ -1428,12 +1564,12 @@ function assertHistory(job) {
|
|
|
1428
1564
|
let pending = false;
|
|
1429
1565
|
let lastAck = 0;
|
|
1430
1566
|
for (const { commit, permitUntil, projectionAckAt } of job.attempts) {
|
|
1431
|
-
const { attemptId, ordinal: ordinal2, startedAt, finishedAt, result
|
|
1567
|
+
const { attemptId, ordinal: ordinal2, startedAt, finishedAt, result, disposition, reason, retryAt } = commit;
|
|
1432
1568
|
if (nextAt === void 0 || startedAt < nextAt || seen.has(attemptId) || ordinal2 !== seen.size + 1 || !manual && ordinal2 > retry.maxAttempts) {
|
|
1433
1569
|
invalid(STORED2);
|
|
1434
1570
|
}
|
|
1435
1571
|
seen.add(attemptId);
|
|
1436
|
-
const expected = attemptEffect(
|
|
1572
|
+
const expected = attemptEffect(result, attemptId, ordinal2, manual, retry, expiresAt, finishedAt);
|
|
1437
1573
|
if (disposition !== expected.disposition || reason !== expected.reason || retryAt !== expected.retryAt)
|
|
1438
1574
|
invalid(STORED2);
|
|
1439
1575
|
if (permitUntil !== void 0) {
|
|
@@ -1491,7 +1627,7 @@ function assertHistory(job) {
|
|
|
1491
1627
|
if (job.snapshot || retireAt === void 0 || retireAt > job.updatedAt) invalid(STORED2);
|
|
1492
1628
|
}
|
|
1493
1629
|
}
|
|
1494
|
-
function readJobRecord(scope2, input) {
|
|
1630
|
+
function readJobRecord(scope2, input, capacity) {
|
|
1495
1631
|
const raw = readRecord(
|
|
1496
1632
|
input,
|
|
1497
1633
|
[
|
|
@@ -1518,27 +1654,26 @@ function readJobRecord(scope2, input) {
|
|
|
1518
1654
|
"attempts",
|
|
1519
1655
|
"resolutions"
|
|
1520
1656
|
],
|
|
1521
|
-
["snapshot", "projectionPermitUntil", "retiring"],
|
|
1657
|
+
["snapshot", "projectionPermitUntil", "retiring", "smtpCapacity", "intentHash"],
|
|
1522
1658
|
STORED2
|
|
1523
1659
|
);
|
|
1524
1660
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, STORED2) !== scope2) invalid(STORED2);
|
|
1525
|
-
|
|
1526
|
-
const recipients =
|
|
1527
|
-
to: integer(counts.to, 0, MESSAGE_LIMITS.recipients, STORED2),
|
|
1528
|
-
cc: integer(counts.cc, 0, MESSAGE_LIMITS.recipients, STORED2),
|
|
1529
|
-
bcc: integer(counts.bcc, 0, MESSAGE_LIMITS.recipients, STORED2)
|
|
1530
|
-
});
|
|
1531
|
-
integer(recipients.to + recipients.cc + recipients.bcc, 1, MESSAGE_LIMITS.recipients, STORED2);
|
|
1661
|
+
assertCapacityTag(raw, capacity, STORED2);
|
|
1662
|
+
const recipients = readRecipientCounts(raw.recipientCounts, STORED2, capacity);
|
|
1532
1663
|
const key = validateAdmissionKey(raw.key, STORED2);
|
|
1533
1664
|
const admissionKey = encodeAdmissionKey(key);
|
|
1534
1665
|
if (raw.admissionKey !== admissionKey) invalid(STORED2);
|
|
1666
|
+
if (!capacity && Object.hasOwn(raw, "intentHash")) invalid(STORED2);
|
|
1667
|
+
const intentHash = capacity ? hashToken(raw.intentHash, STORED2) : void 0;
|
|
1535
1668
|
const base = {
|
|
1536
1669
|
version: EMAIL_FORMAT_VERSION,
|
|
1670
|
+
...capacityTag(capacity),
|
|
1537
1671
|
scope: scope2,
|
|
1538
1672
|
jobId: readStoreId(raw.jobId, STORED2),
|
|
1539
1673
|
key,
|
|
1540
1674
|
admissionKey,
|
|
1541
1675
|
fingerprint: hashToken(raw.fingerprint, STORED2),
|
|
1676
|
+
...intentHash === void 0 ? {} : { intentHash },
|
|
1542
1677
|
snapshotHash: hashToken(raw.snapshotHash, STORED2),
|
|
1543
1678
|
retry: readRetryPolicy(raw.retry, STORED2),
|
|
1544
1679
|
retention: readRetentionPolicy(raw.retention, STORED2),
|
|
@@ -1554,6 +1689,10 @@ function readJobRecord(scope2, input) {
|
|
|
1554
1689
|
revision: readStoreId(raw.revision, STORED2),
|
|
1555
1690
|
state: readState(raw.state)
|
|
1556
1691
|
};
|
|
1692
|
+
assertCapacityProvider(base.provider, capacity, STORED2);
|
|
1693
|
+
if (intentHash !== void 0 && base.fingerprint !== fingerprintFor({ ...base, templateKey: base.template.key, intentHash }, capacity)) {
|
|
1694
|
+
invalid(STORED2);
|
|
1695
|
+
}
|
|
1557
1696
|
if (base.createdAt > base.updatedAt || Math.max(base.createdAt, base.scheduleAt ?? 0) >= base.expiresAt || base.provider.kind === "sendgrid" && !recipients.to) {
|
|
1558
1697
|
invalid(STORED2);
|
|
1559
1698
|
}
|
|
@@ -1562,8 +1701,8 @@ function readJobRecord(scope2, input) {
|
|
|
1562
1701
|
if (retiring && raw.retiring !== true) invalid(STORED2);
|
|
1563
1702
|
const job = Object.freeze({
|
|
1564
1703
|
...base,
|
|
1565
|
-
...Object.hasOwn(raw, "snapshot") ? { snapshot: readStoredSnapshot(base, raw.snapshot) } : {},
|
|
1566
|
-
attempts: readArray(raw.attempts, MAX_ATTEMPT_LEDGER, STORED2, (entry) => readLedgerAttempt(entry, base)),
|
|
1704
|
+
...Object.hasOwn(raw, "snapshot") ? { snapshot: readStoredSnapshot(base, raw.snapshot, capacity) } : {},
|
|
1705
|
+
attempts: readArray(raw.attempts, MAX_ATTEMPT_LEDGER, STORED2, (entry) => readLedgerAttempt(entry, base, capacity)),
|
|
1567
1706
|
resolutions: readArray(raw.resolutions, 2, STORED2, readUnknownResolution),
|
|
1568
1707
|
...projectionPermitUntil === void 0 ? {} : { projectionPermitUntil },
|
|
1569
1708
|
...retiring ? { retiring: true } : {}
|
|
@@ -1595,7 +1734,7 @@ function replace(job, time2, patch, value, before, pendingAck) {
|
|
|
1595
1734
|
write: { kind: "replace", guard: pendingAck ? { ...expect, pendingAck } : expect, next }
|
|
1596
1735
|
};
|
|
1597
1736
|
}
|
|
1598
|
-
function newJob(admission, time2) {
|
|
1737
|
+
function newJob(admission, time2, capacity) {
|
|
1599
1738
|
const { command, jobId, retry, retention, admissionKey } = admission;
|
|
1600
1739
|
const { scope: scope2, key, scheduleAt, fingerprint, snapshotHash, snapshot } = command;
|
|
1601
1740
|
const now = unixMs(time2.now, "store_unavailable");
|
|
@@ -1604,6 +1743,8 @@ function newJob(admission, time2) {
|
|
|
1604
1743
|
if (nextAt >= snapshot.expiresAt) invalid("expired");
|
|
1605
1744
|
return Object.freeze({
|
|
1606
1745
|
version: EMAIL_FORMAT_VERSION,
|
|
1746
|
+
...capacityTag(capacity),
|
|
1747
|
+
...capacity ? { intentHash: command.intentHash } : {},
|
|
1607
1748
|
scope: scope2,
|
|
1608
1749
|
jobId,
|
|
1609
1750
|
key,
|
|
@@ -1642,14 +1783,14 @@ function liveLease(job, fence, phase, now) {
|
|
|
1642
1783
|
}
|
|
1643
1784
|
return lease;
|
|
1644
1785
|
}
|
|
1645
|
-
function claimJob(job, claim, time2) {
|
|
1786
|
+
function claimJob(job, claim, time2, capacity) {
|
|
1646
1787
|
const { now } = time2;
|
|
1647
1788
|
if (job.state.state !== "queued" || job.state.nextAt > now || now >= job.expiresAt) return null;
|
|
1648
1789
|
const manual = hasRedrive(job);
|
|
1649
1790
|
const ordinal2 = job.attempts.length + 1;
|
|
1650
1791
|
if (ordinal2 > MAX_ATTEMPT_LEDGER || !manual && ordinal2 > job.retry.maxAttempts) invalid("store_incompatible");
|
|
1651
1792
|
if (job.attempts.some(({ commit }) => commit.attemptId === claim.attemptId)) invalid("invalid_command");
|
|
1652
|
-
const snapshot = readStoredSnapshot(job, job.snapshot);
|
|
1793
|
+
const snapshot = readStoredSnapshot(job, job.snapshot, capacity);
|
|
1653
1794
|
const lease = Object.freeze({
|
|
1654
1795
|
attemptId: claim.attemptId,
|
|
1655
1796
|
token: claim.token,
|
|
@@ -1709,11 +1850,11 @@ function markJob(job, fence, leaseMs, time2) {
|
|
|
1709
1850
|
Math.min(job.expiresAt, lease.leaseEndsAt, grant.leaseEndsAt)
|
|
1710
1851
|
);
|
|
1711
1852
|
}
|
|
1712
|
-
function finalize(job, lease, operationId,
|
|
1853
|
+
function finalize(job, lease, operationId, result, time2, owner, capacity) {
|
|
1713
1854
|
const { now } = time2;
|
|
1714
1855
|
const { attemptId, startedAt, handedOffAt } = lease;
|
|
1715
1856
|
const ordinal2 = job.attempts.length + 1;
|
|
1716
|
-
const effect = attemptEffect(
|
|
1857
|
+
const effect = attemptEffect(result, attemptId, ordinal2, hasRedrive(job), job.retry, job.expiresAt, now);
|
|
1717
1858
|
const commit = Object.freeze({
|
|
1718
1859
|
jobId: job.jobId,
|
|
1719
1860
|
attemptId,
|
|
@@ -1722,31 +1863,31 @@ function finalize(job, lease, operationId, result2, time2, owner) {
|
|
|
1722
1863
|
startedAt,
|
|
1723
1864
|
...handedOffAt === void 0 ? {} : { handedOffAt },
|
|
1724
1865
|
finishedAt: now,
|
|
1725
|
-
result
|
|
1866
|
+
result,
|
|
1726
1867
|
...effect
|
|
1727
1868
|
});
|
|
1728
1869
|
const entry = Object.freeze({
|
|
1729
1870
|
commit,
|
|
1730
|
-
digest: activityDigest(activityFor(job, commit)),
|
|
1871
|
+
digest: activityDigest(activityFor(job, commit, capacity), capacity),
|
|
1731
1872
|
...owner ? { ownerDigest: sha256(lease.token) } : {}
|
|
1732
1873
|
});
|
|
1733
1874
|
const state = effect.disposition === "retry_scheduled" ? Object.freeze({ state: "queued", nextAt: effect.retryAt }) : terminalState(effect.disposition, effect.reason, now);
|
|
1734
1875
|
let before = owner ? lease.leaseEndsAt : void 0;
|
|
1735
|
-
if (
|
|
1876
|
+
if (result.reason === "cancelled" || result.retry === "safe_transient" && effect.disposition !== "expired") {
|
|
1736
1877
|
before = Math.min(before ?? job.expiresAt, job.expiresAt);
|
|
1737
1878
|
}
|
|
1738
1879
|
return replace(
|
|
1739
1880
|
job,
|
|
1740
1881
|
time2,
|
|
1741
1882
|
{ ...job, state, attempts: Object.freeze([...job.attempts, entry]) },
|
|
1742
|
-
() => copyCommit(commit),
|
|
1883
|
+
() => copyCommit(commit, capacity),
|
|
1743
1884
|
before
|
|
1744
1885
|
);
|
|
1745
1886
|
}
|
|
1746
|
-
function finishJob(job, request, time2) {
|
|
1747
|
-
const { fence, phase, operationId, result
|
|
1887
|
+
function finishJob(job, request, time2, capacity) {
|
|
1888
|
+
const { fence, phase, operationId, result } = request;
|
|
1748
1889
|
if (job.jobId !== fence.jobId) return null;
|
|
1749
|
-
assertEvidencePhase(
|
|
1890
|
+
assertEvidencePhase(result.reason, phase, "owner", "invalid_command");
|
|
1750
1891
|
const previous = job.attempts.find(({ commit }) => commit.attemptId === fence.attemptId);
|
|
1751
1892
|
if (previous) {
|
|
1752
1893
|
const { commit } = previous;
|
|
@@ -1754,28 +1895,28 @@ function finishJob(job, request, time2) {
|
|
|
1754
1895
|
if (previous.ownerDigest !== sha256(fence.token) || commit.operationId !== operationId || previousPhase !== phase) {
|
|
1755
1896
|
return null;
|
|
1756
1897
|
}
|
|
1757
|
-
if (
|
|
1898
|
+
if (sha256(encodeEvidence(result, "invalid_command", capacity)) !== sha256(encodeEvidence(commit.result, "store_incompatible", capacity))) {
|
|
1758
1899
|
invalid("invalid_command");
|
|
1759
1900
|
}
|
|
1760
|
-
return { value: copyCommit(commit) };
|
|
1901
|
+
return { value: copyCommit(commit, capacity) };
|
|
1761
1902
|
}
|
|
1762
1903
|
const lease = liveLease(job, fence, phase, time2.now);
|
|
1763
1904
|
if (!lease) return null;
|
|
1764
|
-
if (
|
|
1765
|
-
assertEvidenceContext(
|
|
1766
|
-
return finalize(job, lease, operationId,
|
|
1905
|
+
if (result.reason === "expired" && time2.now < job.expiresAt) invalid("invalid_command");
|
|
1906
|
+
assertEvidenceContext(result, job.provider, fence.attemptId, job.recipientCounts, "invalid_command");
|
|
1907
|
+
return finalize(job, lease, operationId, result, time2, true, capacity);
|
|
1767
1908
|
}
|
|
1768
|
-
function recoverJob(job, operationId, time2) {
|
|
1909
|
+
function recoverJob(job, operationId, time2, capacity) {
|
|
1769
1910
|
if (job.state.state !== "leased" || time2.now < job.state.lease.leaseEndsAt) return null;
|
|
1770
1911
|
const { lease } = job.state;
|
|
1771
1912
|
const reason = lease.handedOffAt === void 0 ? "pre_handoff_lease_expired" : "post_handoff_lease_expired";
|
|
1772
|
-
return finalize(job, lease, operationId, lifecycleEvidence(reason), time2, false);
|
|
1913
|
+
return finalize(job, lease, operationId, lifecycleEvidence(reason), time2, false, capacity);
|
|
1773
1914
|
}
|
|
1774
1915
|
function expireJob(job, time2) {
|
|
1775
1916
|
if (job.state.state !== "queued" || time2.now < job.expiresAt) return null;
|
|
1776
1917
|
return replace(job, time2, { ...job, state: terminalState("expired", "expired", time2.now) }, jobSummary);
|
|
1777
1918
|
}
|
|
1778
|
-
function cancelJob(job, operationId, time2) {
|
|
1919
|
+
function cancelJob(job, operationId, time2, capacity) {
|
|
1779
1920
|
const { state } = job;
|
|
1780
1921
|
if (state.state === "terminal") {
|
|
1781
1922
|
return {
|
|
@@ -1789,7 +1930,7 @@ function cancelJob(job, operationId, time2) {
|
|
|
1789
1930
|
return { value: Object.freeze({ status: "too_late", job: jobSummary(job) }) };
|
|
1790
1931
|
}
|
|
1791
1932
|
if (state.state === "leased") {
|
|
1792
|
-
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time2, false);
|
|
1933
|
+
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time2, false, capacity);
|
|
1793
1934
|
const { write } = decision;
|
|
1794
1935
|
if (write?.kind !== "replace") invalid("store_incompatible");
|
|
1795
1936
|
return { write, value: Object.freeze({ status: "cancelled", job: jobSummary(write.next) }) };
|
|
@@ -1834,11 +1975,11 @@ function resolveJob(job, request, time2) {
|
|
|
1834
1975
|
function replaceAttempt(job, previous, entry) {
|
|
1835
1976
|
return Object.freeze(job.attempts.map((attempt) => attempt === previous ? Object.freeze(entry) : attempt));
|
|
1836
1977
|
}
|
|
1837
|
-
function reserveJobProjection(job, time2) {
|
|
1978
|
+
function reserveJobProjection(job, time2, capacity) {
|
|
1838
1979
|
const entry = pendingAttempt(job);
|
|
1839
1980
|
if (job.retiring || !entry || entry.commit.finishedAt > time2.now) return null;
|
|
1840
1981
|
const permitUntil = timeAfter(time2.now, PROJECTION_PERMIT_MS) ?? invalid("store_unavailable");
|
|
1841
|
-
const activity = activityFor(job, entry.commit);
|
|
1982
|
+
const activity = activityFor(job, entry.commit, capacity);
|
|
1842
1983
|
const permit = Object.freeze({ activity, digest: entry.digest, permitUntil });
|
|
1843
1984
|
const attempts = replaceAttempt(job, entry, { ...entry, permitUntil });
|
|
1844
1985
|
return replace(
|
|
@@ -2007,9 +2148,9 @@ var MongoConnection = class {
|
|
|
2007
2148
|
return { kind: "ack", reply };
|
|
2008
2149
|
}
|
|
2009
2150
|
async read(binding, budget, command, target) {
|
|
2010
|
-
const
|
|
2011
|
-
if (
|
|
2012
|
-
return
|
|
2151
|
+
const result = await this.exec(binding, budget, command, target);
|
|
2152
|
+
if (result.kind !== "ack") return invalid("store_unavailable");
|
|
2153
|
+
return result.reply;
|
|
2013
2154
|
}
|
|
2014
2155
|
async write(binding, budget, command, definite) {
|
|
2015
2156
|
return this.exec(binding, budget, command, "store", true, definite);
|
|
@@ -2119,9 +2260,9 @@ var jobOptional = [
|
|
|
2119
2260
|
"pendingAt",
|
|
2120
2261
|
"nextCleanupAt"
|
|
2121
2262
|
];
|
|
2122
|
-
function readJobBson(scope2, raw) {
|
|
2123
|
-
const { _id, lifecycle: lifecycle2, messageExpiresAt, state, nextAt, expiresAt, leaseEndsAt, pendingAt, nextCleanupAt, ...rest } = readRecord(raw, jobRequired, jobOptional, STORED3);
|
|
2124
|
-
const job = readJobRecord(scope2, { ...rest, state: lifecycle2, expiresAt: messageExpiresAt });
|
|
2263
|
+
function readJobBson(scope2, raw, capacity) {
|
|
2264
|
+
const { _id, lifecycle: lifecycle2, messageExpiresAt, state, nextAt, expiresAt, leaseEndsAt, pendingAt, nextCleanupAt, ...rest } = readRecord(raw, jobRequired, capacity ? [...jobOptional, "smtpCapacity", "intentHash"] : jobOptional, STORED3);
|
|
2265
|
+
const job = readJobRecord(scope2, { ...rest, state: lifecycle2, expiresAt: messageExpiresAt }, capacity);
|
|
2125
2266
|
const selectors = jobSelectors(job);
|
|
2126
2267
|
for (const [key, value, expected] of [
|
|
2127
2268
|
["state", state, selectors.state],
|
|
@@ -2152,14 +2293,15 @@ var activityRequired = [
|
|
|
2152
2293
|
"disposition"
|
|
2153
2294
|
];
|
|
2154
2295
|
var activityOptional = ["handedOffAt"];
|
|
2155
|
-
function readActivityBson(expectedScope, row) {
|
|
2296
|
+
function readActivityBson(expectedScope, row, capacity) {
|
|
2156
2297
|
if (!Object.hasOwn(row, "complete")) {
|
|
2157
|
-
const { _id: _id2, scope: scope2, jobId, attemptId, stubDeleteAt } = readRecord(
|
|
2298
|
+
const { _id: _id2, scope: scope2, jobId, attemptId, stubDeleteAt, ...tag } = readRecord(
|
|
2158
2299
|
row,
|
|
2159
2300
|
[...activityIdentity, "stubDeleteAt"],
|
|
2160
|
-
[],
|
|
2301
|
+
capacity ? ["smtpCapacity"] : [],
|
|
2161
2302
|
STORED3
|
|
2162
2303
|
);
|
|
2304
|
+
assertCapacityTag(tag, capacity, STORED3);
|
|
2163
2305
|
readStoreId(_id2, STORED3);
|
|
2164
2306
|
readStoreId(jobId, STORED3);
|
|
2165
2307
|
readStoreId(attemptId, STORED3);
|
|
@@ -2167,9 +2309,14 @@ function readActivityBson(expectedScope, row) {
|
|
|
2167
2309
|
unixMs(stubDeleteAt.getTime(), STORED3);
|
|
2168
2310
|
return;
|
|
2169
2311
|
}
|
|
2170
|
-
const { _id, complete, digest: digest2, ...activity } = readRecord(
|
|
2312
|
+
const { _id, complete, digest: digest2, ...activity } = readRecord(
|
|
2313
|
+
row,
|
|
2314
|
+
activityRequired,
|
|
2315
|
+
capacity ? [...activityOptional, "smtpCapacity", "recipientCounts"] : activityOptional,
|
|
2316
|
+
STORED3
|
|
2317
|
+
);
|
|
2171
2318
|
readStoreId(_id, STORED3);
|
|
2172
|
-
const stored = readStoredActivity(expectedScope, { activity, digest: digest2 });
|
|
2319
|
+
const stored = readStoredActivity(expectedScope, { activity, digest: digest2 }, STORED3, capacity);
|
|
2173
2320
|
if (complete !== true) invalid(STORED3);
|
|
2174
2321
|
return stored;
|
|
2175
2322
|
}
|
|
@@ -2199,49 +2346,58 @@ var provider = objectSchema({
|
|
|
2199
2346
|
});
|
|
2200
2347
|
var template = objectSchema({ key: string, revision: string });
|
|
2201
2348
|
var correlation = { ...bsonObject, maxProperties: MESSAGE_LIMITS.correlationEntries, additionalProperties: string };
|
|
2202
|
-
var
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
}
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
}
|
|
2349
|
+
var smtpProvider = objectSchema({ kind: { enum: ["smtp"] }, key: string, mode: { enum: ["live"] } });
|
|
2350
|
+
function resultFields(capacity) {
|
|
2351
|
+
const max = recipientLimit(capacity);
|
|
2352
|
+
const recipient = objectSchema({
|
|
2353
|
+
group: { enum: ["to", "cc", "bcc"] },
|
|
2354
|
+
index: { ...numeric, minimum: 0, maximum: max - 1 },
|
|
2355
|
+
status: { enum: ["accepted", "rejected"] }
|
|
2356
|
+
});
|
|
2357
|
+
return {
|
|
2358
|
+
outcome: string,
|
|
2359
|
+
retry: { enum: ["never", "safe_transient"] },
|
|
2360
|
+
stage: string,
|
|
2361
|
+
reason: string,
|
|
2362
|
+
recipients: { bsonType: "array", maxItems: max, items: recipient },
|
|
2363
|
+
messageId: { ...string, maxLength: "<00000000-0000-4000-8000-000000000000@email.invalid>".length },
|
|
2364
|
+
...capacity ? {} : { providerMessageId: { ...string, maxLength: 128 } }
|
|
2365
|
+
};
|
|
2366
|
+
}
|
|
2216
2367
|
var resultOptional = ["recipients", "messageId", "providerMessageId"];
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2368
|
+
function ledgerAttempt(capacity) {
|
|
2369
|
+
return objectSchema(
|
|
2370
|
+
{
|
|
2371
|
+
commit: objectSchema(
|
|
2372
|
+
{
|
|
2373
|
+
jobId: uuid,
|
|
2374
|
+
attemptId: uuid,
|
|
2375
|
+
ordinal,
|
|
2376
|
+
operationId: uuid,
|
|
2377
|
+
startedAt: time,
|
|
2378
|
+
handedOffAt: time,
|
|
2379
|
+
finishedAt: time,
|
|
2380
|
+
result: objectSchema(
|
|
2381
|
+
{
|
|
2382
|
+
...resultFields(capacity),
|
|
2383
|
+
retryHint: objectSchema({ delayMs: numeric, notBefore: time }, ["delayMs", "notBefore"])
|
|
2384
|
+
},
|
|
2385
|
+
[...resultOptional, "retryHint"]
|
|
2386
|
+
),
|
|
2387
|
+
disposition: string,
|
|
2388
|
+
reason: string,
|
|
2389
|
+
retryAt: time
|
|
2390
|
+
},
|
|
2391
|
+
["handedOffAt", "retryAt"]
|
|
2392
|
+
),
|
|
2393
|
+
digest,
|
|
2394
|
+
ownerDigest: digest,
|
|
2395
|
+
permitUntil: time,
|
|
2396
|
+
projectionAckAt: time
|
|
2397
|
+
},
|
|
2398
|
+
["ownerDigest", "permitUntil", "projectionAckAt"]
|
|
2399
|
+
);
|
|
2400
|
+
}
|
|
2245
2401
|
var lifecycle = {
|
|
2246
2402
|
oneOf: [
|
|
2247
2403
|
objectSchema({ state: { enum: ["queued"] }, nextAt: time }),
|
|
@@ -2271,13 +2427,57 @@ var lifecycle = {
|
|
|
2271
2427
|
)
|
|
2272
2428
|
]
|
|
2273
2429
|
};
|
|
2274
|
-
function
|
|
2430
|
+
function capacityField({ binding }) {
|
|
2275
2431
|
return {
|
|
2432
|
+
smtpCapacity: objectSchema({
|
|
2433
|
+
format: { enum: [binding.format] },
|
|
2434
|
+
namespace: { enum: [binding.namespace] },
|
|
2435
|
+
maxRecipients: { ...numeric, enum: [binding.maxRecipients] }
|
|
2436
|
+
})
|
|
2437
|
+
};
|
|
2438
|
+
}
|
|
2439
|
+
function countsSchema(capacity) {
|
|
2440
|
+
const count = capacity ? { ...numeric, minimum: 0, maximum: recipientLimit(capacity) } : numeric;
|
|
2441
|
+
return objectSchema({ to: count, cc: count, bcc: count });
|
|
2442
|
+
}
|
|
2443
|
+
function smtpSnapshot(capacity) {
|
|
2444
|
+
const mailbox = objectSchema({ address: string, name: string }, ["name"]);
|
|
2445
|
+
const recipients = { bsonType: "array", maxItems: recipientLimit(capacity), items: mailbox };
|
|
2446
|
+
return objectSchema(
|
|
2447
|
+
{
|
|
2448
|
+
version,
|
|
2449
|
+
...capacityField(capacity),
|
|
2450
|
+
kind: { enum: ["smtp"] },
|
|
2451
|
+
provider: smtpProvider,
|
|
2452
|
+
template,
|
|
2453
|
+
envelope: objectSchema({ from: mailbox, to: recipients, cc: recipients, bcc: recipients, replyTo: mailbox }, [
|
|
2454
|
+
"replyTo"
|
|
2455
|
+
]),
|
|
2456
|
+
preparedAt: time,
|
|
2457
|
+
expiresAt: time,
|
|
2458
|
+
priority: { ...numeric, minimum: PRIORITY.min, maximum: PRIORITY.max },
|
|
2459
|
+
attachments: { bsonType: "array", maxItems: MESSAGE_LIMITS.attachments, items: bsonObject },
|
|
2460
|
+
correlation,
|
|
2461
|
+
subject: string,
|
|
2462
|
+
html: string,
|
|
2463
|
+
text: string
|
|
2464
|
+
},
|
|
2465
|
+
["html", "text"]
|
|
2466
|
+
);
|
|
2467
|
+
}
|
|
2468
|
+
function withBsonLimit(validator, maxBytes) {
|
|
2469
|
+
return maxBytes === void 0 ? validator : {
|
|
2470
|
+
$and: [validator, { $expr: { $lte: [{ $bsonSize: "$$ROOT" }, maxBytes] } }]
|
|
2471
|
+
};
|
|
2472
|
+
}
|
|
2473
|
+
function jobValidator(capacity) {
|
|
2474
|
+
const validator = {
|
|
2276
2475
|
$jsonSchema: {
|
|
2277
2476
|
...objectSchema(
|
|
2278
2477
|
{
|
|
2279
2478
|
_id: string,
|
|
2280
2479
|
version,
|
|
2480
|
+
...capacity ? { ...capacityField(capacity), intentHash: digest } : {},
|
|
2281
2481
|
scope,
|
|
2282
2482
|
jobId: uuid,
|
|
2283
2483
|
key: {
|
|
@@ -2289,13 +2489,13 @@ function jobValidator() {
|
|
|
2289
2489
|
admissionKey: string,
|
|
2290
2490
|
fingerprint: digest,
|
|
2291
2491
|
snapshotHash: digest,
|
|
2292
|
-
snapshot: bsonObject,
|
|
2492
|
+
snapshot: capacity ? smtpSnapshot(capacity) : bsonObject,
|
|
2293
2493
|
retry: objectSchema({ maxAttempts: numeric, baseDelayMs: numeric, maxDelayMs: numeric }),
|
|
2294
2494
|
retention: objectSchema({ payloadMs: numeric, historyMs: numeric, idempotencyMs: numeric }),
|
|
2295
|
-
provider,
|
|
2495
|
+
provider: capacity ? smtpProvider : provider,
|
|
2296
2496
|
template,
|
|
2297
2497
|
correlation,
|
|
2298
|
-
recipientCounts:
|
|
2498
|
+
recipientCounts: countsSchema(capacity),
|
|
2299
2499
|
scheduleAt: { anyOf: [time, { bsonType: "null" }] },
|
|
2300
2500
|
createdAt: time,
|
|
2301
2501
|
updatedAt: time,
|
|
@@ -2304,7 +2504,7 @@ function jobValidator() {
|
|
|
2304
2504
|
revision: uuid,
|
|
2305
2505
|
lifecycle,
|
|
2306
2506
|
state: { enum: ["queued", "leased", "terminal"] },
|
|
2307
|
-
attempts: { bsonType: "array", items: ledgerAttempt, maxItems: MAX_ATTEMPT_LEDGER },
|
|
2507
|
+
attempts: { bsonType: "array", items: ledgerAttempt(capacity), maxItems: MAX_ATTEMPT_LEDGER },
|
|
2308
2508
|
resolutions: {
|
|
2309
2509
|
bsonType: "array",
|
|
2310
2510
|
maxItems: 2,
|
|
@@ -2338,10 +2538,11 @@ function jobValidator() {
|
|
|
2338
2538
|
]
|
|
2339
2539
|
}
|
|
2340
2540
|
};
|
|
2541
|
+
return withBsonLimit(validator, capacity?.limits.jobBsonBytes);
|
|
2341
2542
|
}
|
|
2342
|
-
function activityValidator() {
|
|
2343
|
-
const identity = { _id: uuid, scope, jobId: uuid, attemptId: uuid };
|
|
2344
|
-
|
|
2543
|
+
function activityValidator(capacity) {
|
|
2544
|
+
const identity = { _id: uuid, scope, jobId: uuid, attemptId: uuid, ...capacity ? capacityField(capacity) : {} };
|
|
2545
|
+
const validator = {
|
|
2345
2546
|
$jsonSchema: {
|
|
2346
2547
|
oneOf: [
|
|
2347
2548
|
objectSchema(
|
|
@@ -2351,13 +2552,14 @@ function activityValidator() {
|
|
|
2351
2552
|
complete: { enum: [true] },
|
|
2352
2553
|
digest,
|
|
2353
2554
|
ordinal,
|
|
2354
|
-
provider,
|
|
2555
|
+
provider: capacity ? smtpProvider : provider,
|
|
2556
|
+
...capacity ? { recipientCounts: countsSchema(capacity) } : {},
|
|
2355
2557
|
template,
|
|
2356
2558
|
correlation,
|
|
2357
2559
|
startedAt: time,
|
|
2358
2560
|
handedOffAt: time,
|
|
2359
2561
|
finishedAt: time,
|
|
2360
|
-
result: objectSchema(
|
|
2562
|
+
result: objectSchema(resultFields(capacity), resultOptional),
|
|
2361
2563
|
disposition: string
|
|
2362
2564
|
},
|
|
2363
2565
|
activityOptional
|
|
@@ -2366,6 +2568,7 @@ function activityValidator() {
|
|
|
2366
2568
|
]
|
|
2367
2569
|
}
|
|
2368
2570
|
};
|
|
2571
|
+
return withBsonLimit(validator, capacity?.limits.activityBsonBytes);
|
|
2369
2572
|
}
|
|
2370
2573
|
|
|
2371
2574
|
// src/stores/mongoose/schema.ts
|
|
@@ -2405,18 +2608,24 @@ var activityIndexes = [
|
|
|
2405
2608
|
},
|
|
2406
2609
|
{ name: "v1_activity_stub_ttl", key: { stubDeleteAt: 1 }, expireAfterSeconds: 0 }
|
|
2407
2610
|
];
|
|
2408
|
-
function
|
|
2409
|
-
return
|
|
2611
|
+
function indexName(v1Name, capacity) {
|
|
2612
|
+
return capacity ? `v2_${v1Name.slice(3)}` : v1Name;
|
|
2613
|
+
}
|
|
2614
|
+
function laneIndexes(indexes, capacity) {
|
|
2615
|
+
return capacity ? indexes.map((index) => ({ ...index, name: indexName(index.name, capacity) })) : indexes;
|
|
2410
2616
|
}
|
|
2411
|
-
function
|
|
2412
|
-
return { name, validator:
|
|
2617
|
+
function jobSchema(name, capacity) {
|
|
2618
|
+
return { name, validator: jobValidator(capacity), indexes: laneIndexes(jobIndexes, capacity) };
|
|
2619
|
+
}
|
|
2620
|
+
function activitySchema(name, capacity) {
|
|
2621
|
+
return { name, validator: activityValidator(capacity), indexes: laneIndexes(activityIndexes, capacity) };
|
|
2413
2622
|
}
|
|
2414
2623
|
function simple(collation) {
|
|
2415
2624
|
return collation === void 0 || (0, import_node_util5.isDeepStrictEqual)(collation, SIMPLE);
|
|
2416
2625
|
}
|
|
2417
2626
|
async function getCollection(io, binding, budget, name) {
|
|
2418
|
-
const
|
|
2419
|
-
const rows = batch(
|
|
2627
|
+
const result = await io.read(binding, budget, { listCollections: 1, filter: { name }, cursor: { batchSize: 2 } });
|
|
2628
|
+
const rows = batch(result, 1, `${binding.db.databaseName}.$cmd.listCollections`);
|
|
2420
2629
|
return rows[0] === void 0 ? void 0 : doc(rows[0], "store_incompatible");
|
|
2421
2630
|
}
|
|
2422
2631
|
function checkCollection(row, schema) {
|
|
@@ -2433,8 +2642,8 @@ function matchesIndex(raw, index) {
|
|
|
2433
2642
|
}
|
|
2434
2643
|
async function missingIndexes(io, binding, budget, { name, indexes }) {
|
|
2435
2644
|
const max = 64;
|
|
2436
|
-
const
|
|
2437
|
-
const defs = batch(
|
|
2645
|
+
const result = await io.read(binding, budget, { listIndexes: name, cursor: { batchSize: max + 1 } });
|
|
2646
|
+
const defs = batch(result, max, `${binding.db.databaseName}.${name}`);
|
|
2438
2647
|
const required = [{ name: "_id_", key: { _id: 1 } }, ...indexes];
|
|
2439
2648
|
const seen = /* @__PURE__ */ new Set();
|
|
2440
2649
|
for (const def of defs) {
|
|
@@ -2446,9 +2655,15 @@ async function missingIndexes(io, binding, budget, { name, indexes }) {
|
|
|
2446
2655
|
if (!seen.has("_id_")) invalid("store_incompatible");
|
|
2447
2656
|
return indexes.filter((index) => !seen.has(index.name));
|
|
2448
2657
|
}
|
|
2658
|
+
async function checkExistingCollection(io, binding, budget, schema) {
|
|
2659
|
+
const row = await getCollection(io, binding, budget, schema.name);
|
|
2660
|
+
if (!row) return;
|
|
2661
|
+
checkCollection(row, schema);
|
|
2662
|
+
await missingIndexes(io, binding, budget, schema);
|
|
2663
|
+
}
|
|
2449
2664
|
async function prepareCollection(io, binding, budget, schema) {
|
|
2450
2665
|
if (!await getCollection(io, binding, budget, schema.name)) {
|
|
2451
|
-
const
|
|
2666
|
+
const result = await io.write(
|
|
2452
2667
|
binding,
|
|
2453
2668
|
budget,
|
|
2454
2669
|
{
|
|
@@ -2460,12 +2675,12 @@ async function prepareCollection(io, binding, budget, schema) {
|
|
|
2460
2675
|
},
|
|
2461
2676
|
[48, 2, 9, 72, 115]
|
|
2462
2677
|
);
|
|
2463
|
-
if (
|
|
2678
|
+
if (result.kind === "definite_error" && result.code !== 48) invalid("store_incompatible");
|
|
2464
2679
|
}
|
|
2465
2680
|
checkCollection(await getCollection(io, binding, budget, schema.name), schema);
|
|
2466
2681
|
const missing = await missingIndexes(io, binding, budget, schema);
|
|
2467
2682
|
if (missing.length) {
|
|
2468
|
-
const
|
|
2683
|
+
const result = await io.write(
|
|
2469
2684
|
binding,
|
|
2470
2685
|
budget,
|
|
2471
2686
|
{
|
|
@@ -2474,7 +2689,7 @@ async function prepareCollection(io, binding, budget, schema) {
|
|
|
2474
2689
|
},
|
|
2475
2690
|
[2, 67, 68, 72, 85, 86, 11e3]
|
|
2476
2691
|
);
|
|
2477
|
-
if (
|
|
2692
|
+
if (result.kind === "definite_error") invalid("store_incompatible");
|
|
2478
2693
|
if ((await missingIndexes(io, binding, budget, schema)).length) invalid("store_incompatible");
|
|
2479
2694
|
}
|
|
2480
2695
|
}
|
|
@@ -2495,13 +2710,22 @@ function modifiedValue(reply) {
|
|
|
2495
2710
|
if (n !== (reply.value === null ? 0 : 1) || reply.value === void 0) invalid("write_unconfirmed");
|
|
2496
2711
|
return reply.value;
|
|
2497
2712
|
}
|
|
2498
|
-
function
|
|
2713
|
+
function capacityFilter(capacity) {
|
|
2714
|
+
const binding = capacity?.binding;
|
|
2715
|
+
return binding ? {
|
|
2716
|
+
"smtpCapacity.format": binding.format,
|
|
2717
|
+
"smtpCapacity.namespace": binding.namespace,
|
|
2718
|
+
"smtpCapacity.maxRecipients": binding.maxRecipients
|
|
2719
|
+
} : {};
|
|
2720
|
+
}
|
|
2721
|
+
function casFilter({ scope: scope2, jobId, revision, state, retiring, pendingAck, atOrAfter, before }, capacity) {
|
|
2499
2722
|
const lease = state.state === "leased" ? state.lease : void 0;
|
|
2500
2723
|
return {
|
|
2501
2724
|
_id: jobIdKey(scope2, jobId),
|
|
2502
2725
|
scope: scope2,
|
|
2503
2726
|
jobId,
|
|
2504
2727
|
version: EMAIL_FORMAT_VERSION,
|
|
2728
|
+
...capacityFilter(capacity),
|
|
2505
2729
|
revision,
|
|
2506
2730
|
state: state.state,
|
|
2507
2731
|
"lifecycle.state": state.state,
|
|
@@ -2528,16 +2752,18 @@ function casFilter({ scope: scope2, jobId, revision, state, retiring, pendingAck
|
|
|
2528
2752
|
};
|
|
2529
2753
|
}
|
|
2530
2754
|
var MongoOperations = class {
|
|
2531
|
-
constructor(io, binding, budget, names) {
|
|
2755
|
+
constructor(io, binding, budget, names, capacity) {
|
|
2532
2756
|
this.io = io;
|
|
2533
2757
|
this.binding = binding;
|
|
2534
2758
|
this.budget = budget;
|
|
2535
2759
|
this.names = names;
|
|
2760
|
+
this.capacity = capacity;
|
|
2536
2761
|
}
|
|
2537
2762
|
io;
|
|
2538
2763
|
binding;
|
|
2539
2764
|
budget;
|
|
2540
2765
|
names;
|
|
2766
|
+
capacity;
|
|
2541
2767
|
check() {
|
|
2542
2768
|
this.budget.check();
|
|
2543
2769
|
}
|
|
@@ -2551,7 +2777,7 @@ var MongoOperations = class {
|
|
|
2551
2777
|
const reply = await this.io.read(this.binding, this.budget, {
|
|
2552
2778
|
find,
|
|
2553
2779
|
filter,
|
|
2554
|
-
hint,
|
|
2780
|
+
hint: indexName(hint, this.capacity),
|
|
2555
2781
|
collation: SIMPLE,
|
|
2556
2782
|
readConcern: { level },
|
|
2557
2783
|
limit,
|
|
@@ -2565,7 +2791,7 @@ var MongoOperations = class {
|
|
|
2565
2791
|
async byJob(scope2, jobId) {
|
|
2566
2792
|
const [raw] = await this.find(this.names.jobsCollection, { scope: scope2, jobId }, JOB_ID, 1, "linearizable");
|
|
2567
2793
|
if (!raw) return;
|
|
2568
|
-
const job = readJobBson(scope2, raw);
|
|
2794
|
+
const job = readJobBson(scope2, raw, this.capacity);
|
|
2569
2795
|
if (job.jobId !== jobId) invalid("store_incompatible");
|
|
2570
2796
|
this.check();
|
|
2571
2797
|
return job;
|
|
@@ -2573,7 +2799,7 @@ var MongoOperations = class {
|
|
|
2573
2799
|
async byAdmission(scope2, admissionKey) {
|
|
2574
2800
|
const [raw] = await this.find(this.names.jobsCollection, { scope: scope2, admissionKey }, ADMISSION_ID, 1, "linearizable");
|
|
2575
2801
|
if (!raw) return;
|
|
2576
|
-
const job = readJobBson(scope2, raw);
|
|
2802
|
+
const job = readJobBson(scope2, raw, this.capacity);
|
|
2577
2803
|
if (job.admissionKey !== admissionKey) invalid("store_incompatible");
|
|
2578
2804
|
this.check();
|
|
2579
2805
|
return job;
|
|
@@ -2583,18 +2809,18 @@ var MongoOperations = class {
|
|
|
2583
2809
|
if (!raw) return;
|
|
2584
2810
|
const { jobId, attemptId } = doc(raw, "store_incompatible");
|
|
2585
2811
|
if (jobId !== identity.jobId || attemptId !== identity.attemptId) invalid("store_incompatible");
|
|
2586
|
-
const row = readActivityBson(identity.scope, raw);
|
|
2812
|
+
const row = readActivityBson(identity.scope, raw, this.capacity);
|
|
2587
2813
|
this.check();
|
|
2588
2814
|
return row;
|
|
2589
2815
|
}
|
|
2590
2816
|
async insert(job) {
|
|
2591
|
-
const
|
|
2817
|
+
const result = await this.io.write(this.binding, this.budget, {
|
|
2592
2818
|
insert: this.names.jobsCollection,
|
|
2593
2819
|
documents: [jobBson(job)]
|
|
2594
2820
|
});
|
|
2595
|
-
if (
|
|
2596
|
-
const { n, writeErrors } =
|
|
2597
|
-
if (n === 1 && !Object.hasOwn(
|
|
2821
|
+
if (result.kind !== "ack") invalid("write_unconfirmed");
|
|
2822
|
+
const { n, writeErrors } = result.reply;
|
|
2823
|
+
if (n === 1 && !Object.hasOwn(result.reply, "writeErrors")) return "inserted";
|
|
2598
2824
|
if (n === 0 && Array.isArray(writeErrors) && writeErrors.length === 1) {
|
|
2599
2825
|
const { code } = doc(writeErrors[0], "write_unconfirmed");
|
|
2600
2826
|
if (code === 121) invalid("store_incompatible");
|
|
@@ -2613,23 +2839,23 @@ var MongoOperations = class {
|
|
|
2613
2839
|
}
|
|
2614
2840
|
async replace(write) {
|
|
2615
2841
|
const { guard: guard2, next } = write;
|
|
2616
|
-
const
|
|
2842
|
+
const result = await this.io.write(
|
|
2617
2843
|
this.binding,
|
|
2618
2844
|
this.budget,
|
|
2619
2845
|
{
|
|
2620
2846
|
findAndModify: this.names.jobsCollection,
|
|
2621
|
-
query: casFilter(guard2),
|
|
2847
|
+
query: casFilter(guard2, this.capacity),
|
|
2622
2848
|
// Treat the whole replacement as literal, including rendered strings beginning with '$'.
|
|
2623
2849
|
update: [{ $replaceWith: { $literal: jobBson(next) } }],
|
|
2624
2850
|
fields: { _id: 1, revision: 1 },
|
|
2625
2851
|
new: true,
|
|
2626
|
-
hint: JOB_ID,
|
|
2852
|
+
hint: indexName(JOB_ID, this.capacity),
|
|
2627
2853
|
collation: SIMPLE
|
|
2628
2854
|
},
|
|
2629
2855
|
[121]
|
|
2630
2856
|
);
|
|
2631
|
-
if (
|
|
2632
|
-
const value = modifiedValue(
|
|
2857
|
+
if (result.kind === "definite_error") invalid("store_incompatible");
|
|
2858
|
+
const value = modifiedValue(result.reply);
|
|
2633
2859
|
if (value === null) return false;
|
|
2634
2860
|
const { _id, revision } = doc(value, "write_unconfirmed");
|
|
2635
2861
|
if (_id !== jobIdKey(next.scope, next.jobId) || revision !== next.revision) invalid("write_unconfirmed");
|
|
@@ -2644,7 +2870,7 @@ var MongoOperations = class {
|
|
|
2644
2870
|
const identity = { scope: scope2, jobId, attemptId };
|
|
2645
2871
|
const [raw] = await this.find(this.names.activitiesCollection, identity, ACTIVITY_ID, 1, "linearizable");
|
|
2646
2872
|
if (raw) {
|
|
2647
|
-
readActivityBson(scope2, raw);
|
|
2873
|
+
readActivityBson(scope2, raw, this.capacity);
|
|
2648
2874
|
exact.push({ ...identity, _id: readStoreId(doc(raw, "store_incompatible")._id, "store_incompatible") });
|
|
2649
2875
|
}
|
|
2650
2876
|
}
|
|
@@ -2655,9 +2881,9 @@ var MongoOperations = class {
|
|
|
2655
2881
|
delete: this.names.activitiesCollection,
|
|
2656
2882
|
// An exact indexed delete per attempt in one ordered command.
|
|
2657
2883
|
deletes: exact.map((identity) => ({
|
|
2658
|
-
q: { ...identity, $expr: serverWindow(guard2.atOrAfter) },
|
|
2884
|
+
q: { ...identity, ...capacityFilter(this.capacity), $expr: serverWindow(guard2.atOrAfter) },
|
|
2659
2885
|
limit: 1,
|
|
2660
|
-
hint: ACTIVITY_ID,
|
|
2886
|
+
hint: indexName(ACTIVITY_ID, this.capacity),
|
|
2661
2887
|
collation: SIMPLE
|
|
2662
2888
|
}))
|
|
2663
2889
|
});
|
|
@@ -2670,7 +2896,14 @@ var MongoOperations = class {
|
|
|
2670
2896
|
}
|
|
2671
2897
|
const outcome = await this.io.write(this.binding, this.budget, {
|
|
2672
2898
|
delete: this.names.jobsCollection,
|
|
2673
|
-
deletes: [
|
|
2899
|
+
deletes: [
|
|
2900
|
+
{
|
|
2901
|
+
q: casFilter(guard2, this.capacity),
|
|
2902
|
+
limit: 1,
|
|
2903
|
+
hint: indexName(JOB_ID, this.capacity),
|
|
2904
|
+
collation: SIMPLE
|
|
2905
|
+
}
|
|
2906
|
+
]
|
|
2674
2907
|
});
|
|
2675
2908
|
if (outcome.kind !== "ack") invalid("write_unconfirmed");
|
|
2676
2909
|
return writeCount(outcome.reply, 1) === 1;
|
|
@@ -2704,7 +2937,7 @@ var MongoOperations = class {
|
|
|
2704
2937
|
[due]: { $exists: true, $lte: selectionAt }
|
|
2705
2938
|
},
|
|
2706
2939
|
name,
|
|
2707
|
-
MAX_STORE_CANDIDATES,
|
|
2940
|
+
this.capacity?.limits.dueCandidates ?? MAX_STORE_CANDIDATES,
|
|
2708
2941
|
"majority",
|
|
2709
2942
|
sort,
|
|
2710
2943
|
{ _id: 0, jobId: 1 }
|
|
@@ -2739,15 +2972,15 @@ var MongoOperations = class {
|
|
|
2739
2972
|
const identity = activityKey(permit.activity);
|
|
2740
2973
|
const activity = activityBson(permit);
|
|
2741
2974
|
const candidateId = (0, import_node_crypto2.randomUUID)();
|
|
2742
|
-
const
|
|
2975
|
+
const result = await this.io.write(
|
|
2743
2976
|
this.binding,
|
|
2744
2977
|
this.budget,
|
|
2745
2978
|
{
|
|
2746
2979
|
findAndModify: this.names.activitiesCollection,
|
|
2747
|
-
query: identity,
|
|
2980
|
+
query: { ...identity, ...capacityFilter(this.capacity) },
|
|
2748
2981
|
new: true,
|
|
2749
2982
|
upsert: true,
|
|
2750
|
-
hint: ACTIVITY_ID,
|
|
2983
|
+
hint: indexName(ACTIVITY_ID, this.capacity),
|
|
2751
2984
|
collation: SIMPLE,
|
|
2752
2985
|
update: [
|
|
2753
2986
|
{
|
|
@@ -2768,7 +3001,7 @@ var MongoOperations = class {
|
|
|
2768
3001
|
],
|
|
2769
3002
|
default: {
|
|
2770
3003
|
$mergeObjects: [
|
|
2771
|
-
{ $literal: identity },
|
|
3004
|
+
{ $literal: { ...identity, ...capacityTag(this.capacity) } },
|
|
2772
3005
|
{ _id: { $literal: candidateId }, stubDeleteAt: { $add: ["$$NOW", ACTIVITY_STUB_TTL_MS] } }
|
|
2773
3006
|
]
|
|
2774
3007
|
}
|
|
@@ -2779,10 +3012,14 @@ var MongoOperations = class {
|
|
|
2779
3012
|
},
|
|
2780
3013
|
[11e3, 121]
|
|
2781
3014
|
);
|
|
2782
|
-
if (
|
|
3015
|
+
if (result.kind === "definite_error" && result.code === 121) invalid("store_incompatible");
|
|
2783
3016
|
let confirmedComplete = false;
|
|
2784
|
-
if (
|
|
2785
|
-
const row = readActivityBson(
|
|
3017
|
+
if (result.kind === "ack") {
|
|
3018
|
+
const row = readActivityBson(
|
|
3019
|
+
identity.scope,
|
|
3020
|
+
doc(modifiedValue(result.reply), "write_unconfirmed"),
|
|
3021
|
+
this.capacity
|
|
3022
|
+
);
|
|
2786
3023
|
if (row && row.digest !== permit.digest) invalid("store_incompatible");
|
|
2787
3024
|
confirmedComplete = row !== void 0;
|
|
2788
3025
|
}
|
|
@@ -2840,7 +3077,7 @@ var MongoOperations = class {
|
|
|
2840
3077
|
await readRange(Object.keys(range).length ? { finishedAt: range } : {});
|
|
2841
3078
|
const page = activityPage(
|
|
2842
3079
|
criteria,
|
|
2843
|
-
rows.map((row) => readActivityBson(scope2, row)?.activity ?? invalid("store_incompatible"))
|
|
3080
|
+
rows.map((row) => readActivityBson(scope2, row, this.capacity)?.activity ?? invalid("store_incompatible"))
|
|
2844
3081
|
);
|
|
2845
3082
|
this.check();
|
|
2846
3083
|
return page;
|
|
@@ -2849,11 +3086,25 @@ var MongoOperations = class {
|
|
|
2849
3086
|
|
|
2850
3087
|
// src/mongoose.ts
|
|
2851
3088
|
var MAX_IN_FLIGHT = 32;
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
const input = readRecord(options, [],
|
|
3089
|
+
var NAME_KEYS = ["jobsCollection", "activitiesCollection", "jobsModelName", "activitiesModelName"];
|
|
3090
|
+
function readStoreConfig(options) {
|
|
3091
|
+
const input = readRecord(options, [], [...NAME_KEYS, "smtpCapacity"], "invalid_config");
|
|
3092
|
+
const capacity = readCapacityOption(input);
|
|
3093
|
+
if (capacity) {
|
|
3094
|
+
if (NAME_KEYS.some((key) => Object.hasOwn(input, key))) invalid("invalid_config");
|
|
3095
|
+
const { namespace } = capacity.binding;
|
|
3096
|
+
const prefix = `email_smtp_v2.${namespace}`;
|
|
3097
|
+
const modelId = namespace.replaceAll("-", "_");
|
|
3098
|
+
const names2 = Object.freeze({
|
|
3099
|
+
jobsCollection: `${prefix}.jobs`,
|
|
3100
|
+
activitiesCollection: `${prefix}.activities`,
|
|
3101
|
+
jobsModelName: `EmailSmtpV2Job_${modelId}`,
|
|
3102
|
+
activitiesModelName: `EmailSmtpV2Activity_${modelId}`
|
|
3103
|
+
});
|
|
3104
|
+
return { names: names2, capacity };
|
|
3105
|
+
}
|
|
2855
3106
|
const names = { ...MONGOOSE_DEFAULT_NAMES };
|
|
2856
|
-
for (const key of
|
|
3107
|
+
for (const key of NAME_KEYS) {
|
|
2857
3108
|
const value = input[key];
|
|
2858
3109
|
if (value === void 0) continue;
|
|
2859
3110
|
if (typeof value !== "string" || !/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(value)) invalid("invalid_config");
|
|
@@ -2861,18 +3112,18 @@ function readNames(options) {
|
|
|
2861
3112
|
}
|
|
2862
3113
|
if (names.jobsCollection === names.activitiesCollection || names.jobsModelName === names.activitiesModelName)
|
|
2863
3114
|
invalid("invalid_config");
|
|
2864
|
-
return Object.freeze(names);
|
|
3115
|
+
return { names: Object.freeze(names), capacity };
|
|
2865
3116
|
}
|
|
2866
3117
|
function createMongooseEmailStore(connection, options = {}) {
|
|
2867
3118
|
checkConnectionType(connection);
|
|
2868
|
-
const names =
|
|
3119
|
+
const { names, capacity } = readStoreConfig(options);
|
|
2869
3120
|
const io = new MongoConnection(connection, names);
|
|
2870
3121
|
let readyBinding;
|
|
2871
3122
|
let preparing = false;
|
|
2872
3123
|
let inFlight = 0;
|
|
2873
3124
|
async function run(call, worker, fn) {
|
|
2874
|
-
const { signal, timeoutMs } = readStoreCall(call, worker);
|
|
2875
|
-
if (inFlight >= MAX_IN_FLIGHT) invalid("busy");
|
|
3125
|
+
const { signal, timeoutMs } = readStoreCall(call, worker, capacity);
|
|
3126
|
+
if (inFlight >= (capacity?.limits.mongoInFlight ?? MAX_IN_FLIGHT)) invalid("busy");
|
|
2876
3127
|
const budget = createBudget(systemEmailClock, Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS), signal);
|
|
2877
3128
|
inFlight++;
|
|
2878
3129
|
try {
|
|
@@ -2880,13 +3131,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2880
3131
|
if (!readyBinding) invalid("store_unavailable");
|
|
2881
3132
|
const binding = io.bind();
|
|
2882
3133
|
if (binding.client !== readyBinding.client || binding.db !== readyBinding.db) invalid("store_unavailable");
|
|
2883
|
-
return await fn(new MongoOperations(io, binding, budget, names));
|
|
3134
|
+
return await fn(new MongoOperations(io, binding, budget, names, capacity));
|
|
2884
3135
|
} finally {
|
|
2885
3136
|
inFlight--;
|
|
2886
3137
|
budget.close();
|
|
2887
3138
|
}
|
|
2888
3139
|
}
|
|
2889
3140
|
const store = {
|
|
3141
|
+
...capacityTag(capacity),
|
|
2890
3142
|
async ready(options2 = {}) {
|
|
2891
3143
|
const { timeoutMs, signal } = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
|
|
2892
3144
|
if (preparing || inFlight) invalid("busy");
|
|
@@ -2897,8 +3149,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2897
3149
|
budget.check();
|
|
2898
3150
|
const binding = io.bind();
|
|
2899
3151
|
await io.verifyTopology(binding, budget);
|
|
2900
|
-
|
|
2901
|
-
|
|
3152
|
+
const jobs = jobSchema(names.jobsCollection, capacity);
|
|
3153
|
+
const activities = activitySchema(names.activitiesCollection, capacity);
|
|
3154
|
+
if (capacity) {
|
|
3155
|
+
await checkExistingCollection(io, binding, budget, jobs);
|
|
3156
|
+
await checkExistingCollection(io, binding, budget, activities);
|
|
3157
|
+
}
|
|
3158
|
+
await prepareCollection(io, binding, budget, jobs);
|
|
3159
|
+
await prepareCollection(io, binding, budget, activities);
|
|
2902
3160
|
const jobId = (0, import_node_crypto3.randomUUID)();
|
|
2903
3161
|
const probe = { scope: "!readiness", jobId };
|
|
2904
3162
|
for (const [find, filter, hint] of [
|
|
@@ -2909,7 +3167,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2909
3167
|
await io.read(binding, budget, {
|
|
2910
3168
|
find,
|
|
2911
3169
|
filter,
|
|
2912
|
-
hint,
|
|
3170
|
+
hint: indexName(hint, capacity),
|
|
2913
3171
|
collation: SIMPLE,
|
|
2914
3172
|
readConcern: { level: "linearizable" },
|
|
2915
3173
|
singleBatch: true,
|
|
@@ -2935,10 +3193,10 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2935
3193
|
},
|
|
2936
3194
|
insert(scope2, input, call) {
|
|
2937
3195
|
return run(call, false, async (ops) => {
|
|
2938
|
-
const safe = readStoreAdmission(scopeToken(scope2), input);
|
|
3196
|
+
const safe = readStoreAdmission(scopeToken(scope2), input, capacity);
|
|
2939
3197
|
if (await ops.byAdmission(scope2, safe.admissionKey)) return Object.freeze({ status: "duplicate" });
|
|
2940
3198
|
if (await ops.byJob(scope2, safe.jobId)) invalid("invalid_command");
|
|
2941
|
-
const job = newJob(safe, { now: await ops.now(), revision: (0, import_node_crypto3.randomUUID)() });
|
|
3199
|
+
const job = newJob(safe, { now: await ops.now(), revision: (0, import_node_crypto3.randomUUID)() }, capacity);
|
|
2942
3200
|
const summary = jobSummary(job);
|
|
2943
3201
|
return await ops.insert(job) === "inserted" ? Object.freeze({ status: "inserted", job: summary }) : Object.freeze({ status: "duplicate" });
|
|
2944
3202
|
});
|
|
@@ -2954,13 +3212,13 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2954
3212
|
const id = readStoreId(attemptId);
|
|
2955
3213
|
const job = await ops.byJob(scopeToken(scope2), readStoreId(jobId));
|
|
2956
3214
|
const commit = job?.attempts.find((entry) => entry.commit.attemptId === id)?.commit;
|
|
2957
|
-
return commit ? copyCommit(commit) : null;
|
|
3215
|
+
return commit ? copyCommit(commit, capacity) : null;
|
|
2958
3216
|
});
|
|
2959
3217
|
},
|
|
2960
3218
|
claimNext(scope2, input, call) {
|
|
2961
3219
|
return run(call, true, (ops) => {
|
|
2962
3220
|
const safe = readClaim(input);
|
|
2963
|
-
return ops.next(scopeToken(scope2), "nextAt", (job, time2) => claimJob(job, safe, time2));
|
|
3221
|
+
return ops.next(scopeToken(scope2), "nextAt", (job, time2) => claimJob(job, safe, time2, capacity));
|
|
2964
3222
|
});
|
|
2965
3223
|
},
|
|
2966
3224
|
renew(scope2, fence, leaseMs, call) {
|
|
@@ -2979,14 +3237,14 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2979
3237
|
},
|
|
2980
3238
|
finish(scope2, input, call) {
|
|
2981
3239
|
return run(call, true, (ops) => {
|
|
2982
|
-
const safe = readFinish(input);
|
|
2983
|
-
return ops.point(scopeToken(scope2), safe.fence.jobId, (job, time2) => finishJob(job, safe, time2));
|
|
3240
|
+
const safe = readFinish(input, capacity);
|
|
3241
|
+
return ops.point(scopeToken(scope2), safe.fence.jobId, (job, time2) => finishJob(job, safe, time2, capacity));
|
|
2984
3242
|
});
|
|
2985
3243
|
},
|
|
2986
3244
|
recoverNext(scope2, call) {
|
|
2987
3245
|
return run(call, true, (ops) => {
|
|
2988
3246
|
const operationId = (0, import_node_crypto3.randomUUID)();
|
|
2989
|
-
return ops.next(scopeToken(scope2), "leaseEndsAt", (job, time2) => recoverJob(job, operationId, time2));
|
|
3247
|
+
return ops.next(scopeToken(scope2), "leaseEndsAt", (job, time2) => recoverJob(job, operationId, time2, capacity));
|
|
2990
3248
|
});
|
|
2991
3249
|
},
|
|
2992
3250
|
expireNext(scope2, call) {
|
|
@@ -2995,7 +3253,11 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
2995
3253
|
cancel(scope2, jobId, call) {
|
|
2996
3254
|
return run(call, false, async (ops) => {
|
|
2997
3255
|
const operationId = (0, import_node_crypto3.randomUUID)();
|
|
2998
|
-
return await ops.point(
|
|
3256
|
+
return await ops.point(
|
|
3257
|
+
scopeToken(scope2),
|
|
3258
|
+
readStoreId(jobId),
|
|
3259
|
+
(job, time2) => cancelJob(job, operationId, time2, capacity)
|
|
3260
|
+
) ?? Object.freeze({ status: "not_found" });
|
|
2999
3261
|
});
|
|
3000
3262
|
},
|
|
3001
3263
|
resolveUnknown(scope2, input, call) {
|
|
@@ -3005,11 +3267,15 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
3005
3267
|
});
|
|
3006
3268
|
},
|
|
3007
3269
|
reserveProjection(scope2, call) {
|
|
3008
|
-
return run(
|
|
3270
|
+
return run(
|
|
3271
|
+
call,
|
|
3272
|
+
false,
|
|
3273
|
+
(ops) => ops.next(scopeToken(scope2), "pendingAt", (job, time2) => reserveJobProjection(job, time2, capacity))
|
|
3274
|
+
);
|
|
3009
3275
|
},
|
|
3010
3276
|
putActivity(scope2, input, call) {
|
|
3011
3277
|
return run(call, false, async (ops) => {
|
|
3012
|
-
const permit = readProjectionPermit(scopeToken(scope2), input);
|
|
3278
|
+
const permit = readProjectionPermit(scopeToken(scope2), input, capacity);
|
|
3013
3279
|
const identity = activityKey(permit.activity);
|
|
3014
3280
|
const job = await ops.byJob(scope2, identity.jobId);
|
|
3015
3281
|
const activity = await ops.byActivity(identity);
|
|
@@ -3021,7 +3287,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
3021
3287
|
},
|
|
3022
3288
|
ackProjection(scope2, input, call) {
|
|
3023
3289
|
return run(call, false, async (ops) => {
|
|
3024
|
-
const permit = readProjectionPermit(scopeToken(scope2), input);
|
|
3290
|
+
const permit = readProjectionPermit(scopeToken(scope2), input, capacity);
|
|
3025
3291
|
const identity = activityKey(permit.activity);
|
|
3026
3292
|
return await ops.point(
|
|
3027
3293
|
scope2,
|
|
@@ -3031,7 +3297,7 @@ function createMongooseEmailStore(connection, options = {}) {
|
|
|
3031
3297
|
});
|
|
3032
3298
|
},
|
|
3033
3299
|
listActivities(scope2, query, call) {
|
|
3034
|
-
return run(call, false, (ops) => ops.list(readActivityCriteria(scopeToken(scope2), query)));
|
|
3300
|
+
return run(call, false, (ops) => ops.list(readActivityCriteria(scopeToken(scope2), query, capacity)));
|
|
3035
3301
|
},
|
|
3036
3302
|
cleanupNext(scope2, call) {
|
|
3037
3303
|
return run(call, false, (ops) => ops.next(scopeToken(scope2), "nextCleanupAt", cleanupJob));
|