@web-ts-toolkit/email-service 0.72.0 → 0.74.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 +601 -82
- package/index.d.mts +97 -18
- package/index.d.ts +97 -18
- package/index.js +627 -292
- package/index.mjs +624 -292
- package/mongoose.d.mts +7 -4
- package/mongoose.d.ts +7 -4
- package/mongoose.js +589 -286
- package/mongoose.mjs +589 -286
- package/package.json +1 -1
- package/{provider-HisBc6hk.d.mts → provider-CeUryYAf.d.mts} +64 -6
- package/{provider-HisBc6hk.d.ts → provider-CeUryYAf.d.ts} +64 -6
- package/sendgrid.d.mts +1 -1
- package/sendgrid.d.ts +1 -1
- package/sendgrid.js +130 -42
- package/sendgrid.mjs +130 -42
- package/smtp.d.mts +7 -2
- package/smtp.d.ts +7 -2
- package/smtp.js +214 -84
- package/smtp.mjs +214 -84
- package/{store-D4YAI6Rk.d.mts → store-CFasgH3C.d.ts} +88 -17
- package/{store-Vb63Gocz.d.ts → store-DjCqXbPv.d.mts} +88 -17
package/index.js
CHANGED
|
@@ -32,11 +32,14 @@ var src_exports = {};
|
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
EmailError: () => EmailError,
|
|
34
34
|
createEmailAdmin: () => createEmailAdmin,
|
|
35
|
+
createEmailBatchAdmission: () => createEmailBatchAdmission,
|
|
35
36
|
createEmailBatchService: () => createEmailBatchService,
|
|
37
|
+
createEmailRelay: () => createEmailRelay,
|
|
36
38
|
createEmailService: () => createEmailService,
|
|
37
39
|
createEmailTemplateRegistry: () => createEmailTemplateRegistry,
|
|
38
40
|
createEmailWorker: () => createEmailWorker,
|
|
39
41
|
createMemoryEmailStore: () => createMemoryEmailStore,
|
|
42
|
+
createSmtpAcceptanceResult: () => createSmtpAcceptanceResult,
|
|
40
43
|
defineLocalTemplate: () => defineLocalTemplate,
|
|
41
44
|
defineSendGridTemplate: () => defineSendGridTemplate
|
|
42
45
|
});
|
|
@@ -301,6 +304,12 @@ function scopeToken(value, code = "invalid_command") {
|
|
|
301
304
|
function identifier(value, code) {
|
|
302
305
|
return opaqueToken(value, MESSAGE_LIMITS.identifierBytes, code);
|
|
303
306
|
}
|
|
307
|
+
function uuidV4(value, code) {
|
|
308
|
+
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)) {
|
|
309
|
+
invalid(code);
|
|
310
|
+
}
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
304
313
|
function hashToken(value, code) {
|
|
305
314
|
if (typeof value !== "string" || value.length !== 64 || !/^[0-9a-f]{64}$/.test(value)) invalid(code);
|
|
306
315
|
return value;
|
|
@@ -622,9 +631,9 @@ function syncStage(budget, code, run) {
|
|
|
622
631
|
// src/internal/canonical-json.ts
|
|
623
632
|
var import_node_crypto = require("crypto");
|
|
624
633
|
var treeLimits = { depth: MESSAGE_LIMITS.jsonDepth, values: MESSAGE_LIMITS.jsonValues };
|
|
625
|
-
function canonicalJson(input, maxBytes, code = "invalid_snapshot",
|
|
634
|
+
function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits2 = treeLimits) {
|
|
626
635
|
let bytesLeft = maxBytes;
|
|
627
|
-
let valuesLeft =
|
|
636
|
+
let valuesLeft = limits2.values;
|
|
628
637
|
const chunks = [];
|
|
629
638
|
const ancestors = /* @__PURE__ */ new Set();
|
|
630
639
|
function append(text, bytes = text.length) {
|
|
@@ -643,7 +652,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
643
652
|
append(JSON.stringify(text), bytes);
|
|
644
653
|
}
|
|
645
654
|
function visit(value2, depth) {
|
|
646
|
-
if (depth >
|
|
655
|
+
if (depth > limits2.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
|
|
647
656
|
switch (typeof value2) {
|
|
648
657
|
case "string":
|
|
649
658
|
appendString(value2);
|
|
@@ -667,7 +676,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
667
676
|
if (ancestors.has(value2)) invalid(code);
|
|
668
677
|
ancestors.add(value2);
|
|
669
678
|
try {
|
|
670
|
-
const maxChildren = depth ===
|
|
679
|
+
const maxChildren = depth === limits2.depth ? 0 : valuesLeft;
|
|
671
680
|
if (isArray(value2, code)) {
|
|
672
681
|
const length = denseArrayLength(value2, maxChildren, code);
|
|
673
682
|
if (length && 2 * length + 1 > bytesLeft) invalid(code);
|
|
@@ -710,10 +719,83 @@ function sha256(input) {
|
|
|
710
719
|
function hashJson(input, maxBytes, code = "invalid_snapshot") {
|
|
711
720
|
return sha256(canonicalJson(input, maxBytes, code).encoded);
|
|
712
721
|
}
|
|
722
|
+
function encodeEvidence(input, code, capacity) {
|
|
723
|
+
return canonicalJson(input, capacity?.limits.evidenceBytes ?? 8192, code, capacity?.limits.evidenceTree).encoded;
|
|
724
|
+
}
|
|
713
725
|
|
|
714
726
|
// src/internal/message-codecs.ts
|
|
715
727
|
var import_node_net = require("net");
|
|
716
728
|
var import_node_util4 = require("util");
|
|
729
|
+
|
|
730
|
+
// src/internal/smtp-capacity.ts
|
|
731
|
+
var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
|
|
732
|
+
var MAX_SMTP_RECIPIENTS = 1e3;
|
|
733
|
+
var KIB = 1024;
|
|
734
|
+
var limits = Object.freeze({
|
|
735
|
+
snapshotBytes: 2 * KIB * KIB,
|
|
736
|
+
snapshotTree: Object.freeze({ depth: 4, values: 4096 }),
|
|
737
|
+
evidenceBytes: 64 * KIB,
|
|
738
|
+
evidenceTree: Object.freeze({ depth: 5, values: 4096 }),
|
|
739
|
+
jobBsonBytes: 4 * KIB * KIB,
|
|
740
|
+
activityBsonBytes: 128 * KIB,
|
|
741
|
+
registryCache: Object.freeze({ min: 0, default: 8, max: 16 }),
|
|
742
|
+
registryPending: Object.freeze({ min: 1, default: 2, max: 4 }),
|
|
743
|
+
servicePending: 4,
|
|
744
|
+
workerProfiles: 16,
|
|
745
|
+
workerConcurrency: Object.freeze({ min: 1, default: 1, max: 2 }),
|
|
746
|
+
runClaims: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
747
|
+
runMaintenance: Object.freeze({ min: 0, default: 4, max: 16 }),
|
|
748
|
+
adminMaintenance: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
749
|
+
page: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
750
|
+
memoryJobs: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
751
|
+
mongoInFlight: 4,
|
|
752
|
+
dueCandidates: 4
|
|
753
|
+
});
|
|
754
|
+
function readBinding(input, code) {
|
|
755
|
+
const { format, namespace, maxRecipients } = readRecord(input, ["format", "namespace", "maxRecipients"], [], code);
|
|
756
|
+
if (format !== "smtp-v2") invalid(code);
|
|
757
|
+
return Object.freeze({
|
|
758
|
+
format,
|
|
759
|
+
namespace: uuidV4(namespace, code),
|
|
760
|
+
maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_SMTP_RECIPIENTS, code)
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
function readCapacityOption(raw) {
|
|
764
|
+
if (!Object.hasOwn(raw, "smtpCapacity")) return;
|
|
765
|
+
return Object.freeze({ binding: readBinding(raw.smtpCapacity, "invalid_config"), limits });
|
|
766
|
+
}
|
|
767
|
+
function capacityTag(capacity) {
|
|
768
|
+
return capacity ? { smtpCapacity: capacity.binding } : {};
|
|
769
|
+
}
|
|
770
|
+
function recipientLimit(capacity) {
|
|
771
|
+
return capacity?.binding.maxRecipients ?? MESSAGE_LIMITS.recipients;
|
|
772
|
+
}
|
|
773
|
+
function matchesBinding(input, capacity, code) {
|
|
774
|
+
const binding = readBinding(input, code);
|
|
775
|
+
if (binding.namespace !== capacity.binding.namespace || binding.maxRecipients !== capacity.binding.maxRecipients) {
|
|
776
|
+
invalid(code);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function assertCapacityTag(raw, capacity, code) {
|
|
780
|
+
const tagged = Object.hasOwn(raw, "smtpCapacity");
|
|
781
|
+
if (capacity) {
|
|
782
|
+
if (!tagged) invalid(code);
|
|
783
|
+
matchesBinding(raw.smtpCapacity, capacity, code);
|
|
784
|
+
} else if (tagged) {
|
|
785
|
+
invalid(code);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
function assertPortCapacity(port, capacity) {
|
|
789
|
+
const code = "invalid_config";
|
|
790
|
+
const binding = readPortMember(port, "smtpCapacity");
|
|
791
|
+
if (capacity) matchesBinding(binding, capacity, code);
|
|
792
|
+
else if (binding !== void 0) invalid(code);
|
|
793
|
+
}
|
|
794
|
+
function assertCapacityProvider(provider, capacity, code) {
|
|
795
|
+
if (capacity && provider.kind !== "smtp") invalid(code);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// src/internal/message-codecs.ts
|
|
717
799
|
var L = MESSAGE_LIMITS;
|
|
718
800
|
var SNAPSHOT = "invalid_snapshot";
|
|
719
801
|
var DOT_ATOM = /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
@@ -750,7 +832,8 @@ function validateMailbox(input, code = SNAPSHOT) {
|
|
|
750
832
|
...Object.hasOwn(mailbox, "name") ? { name: headerString(mailbox.name, L.displayNameBytes, code) } : {}
|
|
751
833
|
});
|
|
752
834
|
}
|
|
753
|
-
function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
835
|
+
function validateEnvelope(input, kind, code = SNAPSHOT, capacity) {
|
|
836
|
+
const limit = recipientLimit(capacity);
|
|
754
837
|
const envelope = readRecord(input, ["from", "to", "cc", "bcc"], ["replyTo"], code);
|
|
755
838
|
const seen = /* @__PURE__ */ new Set();
|
|
756
839
|
const recipient = (raw) => {
|
|
@@ -761,9 +844,9 @@ function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
|
761
844
|
return mailbox;
|
|
762
845
|
};
|
|
763
846
|
const from = validateMailbox(envelope.from, code);
|
|
764
|
-
const to = readArray(envelope.to,
|
|
765
|
-
const cc = readArray(envelope.cc,
|
|
766
|
-
const bcc = readArray(envelope.bcc,
|
|
847
|
+
const to = readArray(envelope.to, limit, code, recipient);
|
|
848
|
+
const cc = readArray(envelope.cc, limit - to.length, code, recipient);
|
|
849
|
+
const bcc = readArray(envelope.bcc, limit - to.length - cc.length, code, recipient);
|
|
767
850
|
if (seen.size === 0 || kind === "sendgrid" && to.length === 0) invalid(code);
|
|
768
851
|
return Object.freeze({
|
|
769
852
|
from,
|
|
@@ -851,12 +934,17 @@ function validateExpiry(input, preparedAt, scheduleAt, code = SNAPSHOT) {
|
|
|
851
934
|
}
|
|
852
935
|
return expiresAt;
|
|
853
936
|
}
|
|
854
|
-
function hashSnapshot(snapshot) {
|
|
937
|
+
function hashSnapshot(snapshot, capacity) {
|
|
855
938
|
return sha256(
|
|
856
|
-
canonicalJson(
|
|
939
|
+
canonicalJson(
|
|
940
|
+
snapshot,
|
|
941
|
+
capacity?.limits.snapshotBytes ?? L.snapshotBytes,
|
|
942
|
+
SNAPSHOT,
|
|
943
|
+
capacity?.limits.snapshotTree ?? { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }
|
|
944
|
+
).encoded
|
|
857
945
|
);
|
|
858
946
|
}
|
|
859
|
-
function readSnapshot(input) {
|
|
947
|
+
function readSnapshot(input, capacity) {
|
|
860
948
|
const dto = readRecord(
|
|
861
949
|
input,
|
|
862
950
|
[
|
|
@@ -871,17 +959,19 @@ function readSnapshot(input) {
|
|
|
871
959
|
"attachments",
|
|
872
960
|
"correlation"
|
|
873
961
|
],
|
|
874
|
-
["subject", "html", "text", "templateId", "data"],
|
|
962
|
+
["subject", "html", "text", "templateId", "data", "smtpCapacity"],
|
|
875
963
|
SNAPSHOT
|
|
876
964
|
);
|
|
877
965
|
if (dto.version !== EMAIL_FORMAT_VERSION || dto.kind !== "smtp" && dto.kind !== "sendgrid") invalid(SNAPSHOT);
|
|
966
|
+
assertCapacityTag(dto, capacity, SNAPSHOT);
|
|
878
967
|
const provider = validateProviderRef(dto.provider);
|
|
879
968
|
if (provider.kind !== dto.kind) invalid("provider_mismatch");
|
|
969
|
+
assertCapacityProvider(provider, capacity, "provider_mismatch");
|
|
880
970
|
const preparedAt = unixMs(dto.preparedAt, SNAPSHOT);
|
|
881
971
|
const common = {
|
|
882
972
|
version: EMAIL_FORMAT_VERSION,
|
|
883
973
|
template: validateTemplateIdentity(dto.template),
|
|
884
|
-
envelope: validateEnvelope(dto.envelope, provider.kind),
|
|
974
|
+
envelope: validateEnvelope(dto.envelope, provider.kind, SNAPSHOT, capacity),
|
|
885
975
|
preparedAt,
|
|
886
976
|
expiresAt: validateExpiry(dto.expiresAt, preparedAt, null),
|
|
887
977
|
priority: integer(dto.priority, PRIORITY.min, PRIORITY.max, SNAPSHOT),
|
|
@@ -895,7 +985,7 @@ function readSnapshot(input) {
|
|
|
895
985
|
...Object.hasOwn(dto, "html") ? { html: dto.html } : {},
|
|
896
986
|
...Object.hasOwn(dto, "text") ? { text: dto.text } : {}
|
|
897
987
|
});
|
|
898
|
-
return Object.freeze({ ...common, kind: provider.kind, provider, ...content });
|
|
988
|
+
return Object.freeze({ ...common, kind: provider.kind, provider, ...capacityTag(capacity), ...content });
|
|
899
989
|
}
|
|
900
990
|
if (Object.hasOwn(dto, "subject") || Object.hasOwn(dto, "html") || Object.hasOwn(dto, "text")) invalid(SNAPSHOT);
|
|
901
991
|
return Object.freeze({
|
|
@@ -906,9 +996,9 @@ function readSnapshot(input) {
|
|
|
906
996
|
data: copyJsonObject(dto.data, L.dataBytes, SNAPSHOT)
|
|
907
997
|
});
|
|
908
998
|
}
|
|
909
|
-
function validateSnapshot(input) {
|
|
910
|
-
const snapshot = readSnapshot(input);
|
|
911
|
-
return { snapshot, snapshotHash: hashSnapshot(snapshot) };
|
|
999
|
+
function validateSnapshot(input, capacity) {
|
|
1000
|
+
const snapshot = readSnapshot(input, capacity);
|
|
1001
|
+
return { snapshot, snapshotHash: hashSnapshot(snapshot, capacity) };
|
|
912
1002
|
}
|
|
913
1003
|
|
|
914
1004
|
// src/internal/command-codecs.ts
|
|
@@ -932,25 +1022,35 @@ function encodeAdmissionKey(key) {
|
|
|
932
1022
|
function sameCommandMetadata(key, templateKey, provider, scheduleAt, expected) {
|
|
933
1023
|
return encodeAdmissionKey(key) === encodeAdmissionKey(expected.key) && templateKey === expected.templateKey && provider.kind === expected.provider.kind && provider.key === expected.provider.key && provider.mode === expected.provider.mode && scheduleAt === expected.scheduleAt;
|
|
934
1024
|
}
|
|
935
|
-
function fingerprintFor(identity) {
|
|
1025
|
+
function fingerprintFor(identity, capacity) {
|
|
936
1026
|
const { scope, key, templateKey, provider, scheduleAt, intentHash } = identity;
|
|
937
1027
|
return hashJson(
|
|
938
|
-
[
|
|
1028
|
+
[
|
|
1029
|
+
...capacity ? ["smtp-v2-admission", capacity.binding] : [],
|
|
1030
|
+
EMAIL_FORMAT_VERSION,
|
|
1031
|
+
scope,
|
|
1032
|
+
key,
|
|
1033
|
+
templateKey,
|
|
1034
|
+
provider,
|
|
1035
|
+
scheduleAt,
|
|
1036
|
+
intentHash
|
|
1037
|
+
],
|
|
939
1038
|
MESSAGE_LIMITS.intentBytes
|
|
940
1039
|
);
|
|
941
1040
|
}
|
|
942
|
-
function parseCommandIdentity(scope, command) {
|
|
1041
|
+
function parseCommandIdentity(scope, command, capacity) {
|
|
943
1042
|
const code = "invalid_command";
|
|
944
1043
|
const parsedScope = scopeToken(scope, code);
|
|
945
1044
|
const raw = readRecord(command, ["key", "templateKey", "input", "provider", "intent"], ["scheduleAt"], code);
|
|
946
1045
|
const key = validateAdmissionKey(raw.key, code);
|
|
947
1046
|
const templateKey = identifier(raw.templateKey, code);
|
|
948
1047
|
const provider = validateProviderRef(raw.provider, code);
|
|
1048
|
+
assertCapacityProvider(provider, capacity, "provider_mismatch");
|
|
949
1049
|
const scheduleAt = raw.scheduleAt === void 0 ? null : unixMs(raw.scheduleAt, code);
|
|
950
1050
|
const intent = canonicalJson(raw.intent, MESSAGE_LIMITS.intentBytes, code);
|
|
951
1051
|
const identity = { scope: parsedScope, key, templateKey, provider, scheduleAt, intentHash: sha256(intent.encoded) };
|
|
952
1052
|
return Object.freeze({
|
|
953
|
-
identity: Object.freeze({ ...identity, fingerprint: fingerprintFor(identity) }),
|
|
1053
|
+
identity: Object.freeze({ ...identity, fingerprint: fingerprintFor(identity, capacity) }),
|
|
954
1054
|
command: Object.freeze({
|
|
955
1055
|
key,
|
|
956
1056
|
templateKey,
|
|
@@ -961,35 +1061,40 @@ function parseCommandIdentity(scope, command) {
|
|
|
961
1061
|
})
|
|
962
1062
|
});
|
|
963
1063
|
}
|
|
964
|
-
function validatePreparedCommand(scope, input) {
|
|
1064
|
+
function validatePreparedCommand(scope, input, capacity) {
|
|
965
1065
|
const expectedScope = scopeToken(scope);
|
|
966
1066
|
const code = "invalid_snapshot";
|
|
967
1067
|
const dto = readRecord(
|
|
968
1068
|
input,
|
|
969
1069
|
["version", "scope", "key", "scheduleAt", "intentHash", "fingerprint", "snapshotHash", "snapshot"],
|
|
970
|
-
[],
|
|
1070
|
+
["smtpCapacity"],
|
|
971
1071
|
code
|
|
972
1072
|
);
|
|
973
1073
|
if (dto.version !== EMAIL_FORMAT_VERSION || scopeToken(dto.scope, code) !== expectedScope) invalid(code);
|
|
1074
|
+
assertCapacityTag(dto, capacity, code);
|
|
974
1075
|
const key = validateAdmissionKey(dto.key, code);
|
|
975
1076
|
const scheduleAt = dto.scheduleAt === null ? null : unixMs(dto.scheduleAt, code);
|
|
976
1077
|
const intentHash = hashToken(dto.intentHash, code);
|
|
977
1078
|
const fingerprint = hashToken(dto.fingerprint, code);
|
|
978
1079
|
const storedSnapshotHash = hashToken(dto.snapshotHash, code);
|
|
979
|
-
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot);
|
|
1080
|
+
const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot, capacity);
|
|
980
1081
|
validateExpiry(snapshot.expiresAt, snapshot.preparedAt, scheduleAt);
|
|
981
|
-
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1082
|
+
if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
|
|
1083
|
+
{
|
|
1084
|
+
scope: expectedScope,
|
|
1085
|
+
key,
|
|
1086
|
+
templateKey: snapshot.template.key,
|
|
1087
|
+
provider: snapshot.provider,
|
|
1088
|
+
scheduleAt,
|
|
1089
|
+
intentHash
|
|
1090
|
+
},
|
|
1091
|
+
capacity
|
|
1092
|
+
)) {
|
|
989
1093
|
invalid(code);
|
|
990
1094
|
}
|
|
991
1095
|
return Object.freeze({
|
|
992
1096
|
version: EMAIL_FORMAT_VERSION,
|
|
1097
|
+
...capacityTag(capacity),
|
|
993
1098
|
scope: expectedScope,
|
|
994
1099
|
key,
|
|
995
1100
|
scheduleAt,
|
|
@@ -1000,6 +1105,11 @@ function validatePreparedCommand(scope, input) {
|
|
|
1000
1105
|
});
|
|
1001
1106
|
}
|
|
1002
1107
|
|
|
1108
|
+
// src/internal/smtp-attachments.ts
|
|
1109
|
+
function assertSmtpAttachmentTypes(refs) {
|
|
1110
|
+
if (refs.some(({ contentType }) => /^(?:message|multipart)\//.test(contentType))) invalid("invalid_snapshot");
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1003
1113
|
// src/templates/local-sources.ts
|
|
1004
1114
|
var import_node_buffer2 = require("buffer");
|
|
1005
1115
|
var import_node_events3 = require("events");
|
|
@@ -1223,7 +1333,7 @@ function readDefinition(value) {
|
|
|
1223
1333
|
}
|
|
1224
1334
|
return invalid(code);
|
|
1225
1335
|
}
|
|
1226
|
-
function captureFields(raw, definition, context, scheduleAt, budget, slot) {
|
|
1336
|
+
function captureFields(raw, definition, context, scheduleAt, budget, slot, capacity) {
|
|
1227
1337
|
const code = "invalid_snapshot";
|
|
1228
1338
|
const fields = readRecord(
|
|
1229
1339
|
raw,
|
|
@@ -1241,7 +1351,9 @@ function captureFields(raw, definition, context, scheduleAt, budget, slot) {
|
|
|
1241
1351
|
bcc: fields.bcc === void 0 ? [] : fields.bcc,
|
|
1242
1352
|
...replyTo === void 0 ? {} : { replyTo }
|
|
1243
1353
|
},
|
|
1244
|
-
context.provider.kind
|
|
1354
|
+
context.provider.kind,
|
|
1355
|
+
code,
|
|
1356
|
+
capacity
|
|
1245
1357
|
);
|
|
1246
1358
|
const expiresAt = validateExpiry(
|
|
1247
1359
|
fields.expiresAt === void 0 ? context.now + DEFAULT_EXPIRY_MS : fields.expiresAt,
|
|
@@ -1250,6 +1362,7 @@ function captureFields(raw, definition, context, scheduleAt, budget, slot) {
|
|
|
1250
1362
|
);
|
|
1251
1363
|
const priority = boundedOption(fields.priority, PRIORITY, code);
|
|
1252
1364
|
const attachments = validateAttachmentRefs(fields.attachments === void 0 ? [] : fields.attachments);
|
|
1365
|
+
if (context.provider.kind === "smtp") assertSmtpAttachmentTypes(attachments);
|
|
1253
1366
|
const correlation = validateCorrelation(fields.correlation === void 0 ? {} : fields.correlation);
|
|
1254
1367
|
const values = copyJsonObject(fields.values, MESSAGE_LIMITS.dataBytes, "invalid_data");
|
|
1255
1368
|
const { parseData } = definition;
|
|
@@ -1264,18 +1377,25 @@ function findRegistryPreparation(registry) {
|
|
|
1264
1377
|
}
|
|
1265
1378
|
function createEmailTemplateRegistry(definitions, options = {}) {
|
|
1266
1379
|
const code = "invalid_config";
|
|
1267
|
-
const config = readRecord(options, [], ["clock", "cacheEntries", "maxPending", "helpers"], code);
|
|
1380
|
+
const config = readRecord(options, [], ["clock", "cacheEntries", "maxPending", "helpers", "smtpCapacity"], code);
|
|
1381
|
+
const capacity = readCapacityOption(config);
|
|
1268
1382
|
const clock = readClock(config.clock);
|
|
1269
|
-
const maxPending = boundedOption(config.maxPending, TEMPLATE_MAX_PENDING);
|
|
1270
|
-
const renderer = createLocalRenderer(
|
|
1383
|
+
const maxPending = boundedOption(config.maxPending, capacity?.limits.registryPending ?? TEMPLATE_MAX_PENDING);
|
|
1384
|
+
const renderer = createLocalRenderer(
|
|
1385
|
+
boundedOption(config.cacheEntries, capacity?.limits.registryCache ?? TEMPLATE_CACHE_ENTRIES),
|
|
1386
|
+
config.helpers
|
|
1387
|
+
);
|
|
1271
1388
|
const registered = /* @__PURE__ */ new Map();
|
|
1272
1389
|
for (const key of plainKeys(definitions, Infinity, code)) {
|
|
1273
|
-
|
|
1390
|
+
const definition = readDefinition(ownData(definitions, key, code));
|
|
1391
|
+
if (capacity && definition.kind !== "handlebars") invalid(code);
|
|
1392
|
+
registered.set(identifier(key, code), definition);
|
|
1274
1393
|
}
|
|
1275
1394
|
let pending = 0;
|
|
1276
1395
|
async function prepareParsed({ identity, command: safe }, budget) {
|
|
1277
1396
|
budget.check();
|
|
1278
1397
|
const { templateKey, provider, key, scheduleAt } = identity;
|
|
1398
|
+
assertCapacityProvider(provider, capacity, "provider_mismatch");
|
|
1279
1399
|
const definition = registered.get(templateKey);
|
|
1280
1400
|
if (!definition) invalid("template_not_found");
|
|
1281
1401
|
if ((definition.kind === "handlebars" ? "smtp" : "sendgrid") !== provider.kind) invalid("provider_mismatch");
|
|
@@ -1299,7 +1419,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
|
|
|
1299
1419
|
slot,
|
|
1300
1420
|
"preparation_failed",
|
|
1301
1421
|
() => prepare(input, context),
|
|
1302
|
-
(raw) => captureFields(raw, definition, context, scheduleAt, budget, slot)
|
|
1422
|
+
(raw) => captureFields(raw, definition, context, scheduleAt, budget, slot, capacity)
|
|
1303
1423
|
);
|
|
1304
1424
|
budget.check();
|
|
1305
1425
|
const base = {
|
|
@@ -1311,17 +1431,18 @@ function createEmailTemplateRegistry(definitions, options = {}) {
|
|
|
1311
1431
|
let snapshot;
|
|
1312
1432
|
if (provider.kind === "smtp" && definition.kind === "handlebars") {
|
|
1313
1433
|
const content = await renderer.render(templateKey, definition, data, budget, slot);
|
|
1314
|
-
snapshot = Object.freeze({ ...base, kind: provider.kind, provider, ...content });
|
|
1434
|
+
snapshot = Object.freeze({ ...base, kind: provider.kind, provider, ...capacityTag(capacity), ...content });
|
|
1315
1435
|
} else if (provider.kind === "sendgrid" && definition.kind === "sendgrid") {
|
|
1316
1436
|
snapshot = Object.freeze({ ...base, kind: provider.kind, provider, templateId: definition.templateId, data });
|
|
1317
1437
|
} else {
|
|
1318
1438
|
return invalid("provider_mismatch");
|
|
1319
1439
|
}
|
|
1320
1440
|
budget.check();
|
|
1321
|
-
const snapshotHash = hashSnapshot(snapshot);
|
|
1441
|
+
const snapshotHash = hashSnapshot(snapshot, capacity);
|
|
1322
1442
|
budget.check();
|
|
1323
1443
|
return Object.freeze({
|
|
1324
1444
|
version: EMAIL_FORMAT_VERSION,
|
|
1445
|
+
...capacityTag(capacity),
|
|
1325
1446
|
scope: identity.scope,
|
|
1326
1447
|
key,
|
|
1327
1448
|
scheduleAt,
|
|
@@ -1335,6 +1456,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
|
|
|
1335
1456
|
}
|
|
1336
1457
|
}
|
|
1337
1458
|
const registry = Object.freeze({
|
|
1459
|
+
...capacityTag(capacity),
|
|
1338
1460
|
invalidate(key) {
|
|
1339
1461
|
if (key !== void 0) identifier(key, code);
|
|
1340
1462
|
renderer.invalidate(key);
|
|
@@ -1344,7 +1466,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
|
|
|
1344
1466
|
const budget = createBudget(clock, call.timeoutMs, call.signal);
|
|
1345
1467
|
try {
|
|
1346
1468
|
budget.check();
|
|
1347
|
-
return await prepareParsed(parseCommandIdentity(scope, command), budget);
|
|
1469
|
+
return await prepareParsed(parseCommandIdentity(scope, command, capacity), budget);
|
|
1348
1470
|
} finally {
|
|
1349
1471
|
budget.close();
|
|
1350
1472
|
}
|
|
@@ -1412,12 +1534,73 @@ function attemptOutcome(reason) {
|
|
|
1412
1534
|
function isSafeTransient(reason) {
|
|
1413
1535
|
return evidence[reason][1] === "safe_transient";
|
|
1414
1536
|
}
|
|
1537
|
+
var recipientGroups = ["to", "cc", "bcc"];
|
|
1415
1538
|
function recipientCounts(envelope) {
|
|
1416
1539
|
return Object.freeze({ to: envelope.to.length, cc: envelope.cc.length, bcc: envelope.bcc.length });
|
|
1417
1540
|
}
|
|
1541
|
+
function readRecipientCounts(input, code, capacity) {
|
|
1542
|
+
const limit = recipientLimit(capacity);
|
|
1543
|
+
const counts = readRecord(input, recipientGroups, [], code);
|
|
1544
|
+
const to = integer(counts.to, 0, limit, code);
|
|
1545
|
+
const cc = integer(counts.cc, 0, limit - to, code);
|
|
1546
|
+
const bcc = integer(counts.bcc, 0, limit - to - cc, code);
|
|
1547
|
+
if (to + cc + bcc === 0) invalid(code);
|
|
1548
|
+
return Object.freeze({ to, cc, bcc });
|
|
1549
|
+
}
|
|
1418
1550
|
function smtpMessageId(attemptId) {
|
|
1419
1551
|
return `<${attemptId}@email.invalid>`;
|
|
1420
1552
|
}
|
|
1553
|
+
function readRecipientResults(input, code, { limit, allowAccepted, ordered }) {
|
|
1554
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1555
|
+
let previous = -1;
|
|
1556
|
+
return readArray(input, limit, code, (value) => {
|
|
1557
|
+
const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
|
|
1558
|
+
if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
|
|
1559
|
+
if (status !== "rejected" && (status !== "accepted" || !allowAccepted)) invalid(code);
|
|
1560
|
+
const index = integer(rawIndex, 0, limit - 1, code);
|
|
1561
|
+
if (ordered) {
|
|
1562
|
+
const position = recipientGroups.indexOf(group) * limit + index;
|
|
1563
|
+
if (position <= previous) invalid(code);
|
|
1564
|
+
previous = position;
|
|
1565
|
+
} else {
|
|
1566
|
+
const key = `${group}:${index}`;
|
|
1567
|
+
if (seen.has(key)) invalid(code);
|
|
1568
|
+
seen.add(key);
|
|
1569
|
+
}
|
|
1570
|
+
return Object.freeze({ group, index, status });
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
function allAccepted(recipients) {
|
|
1574
|
+
return recipients.length !== 0 && recipients.every(({ status }) => status === "accepted");
|
|
1575
|
+
}
|
|
1576
|
+
function createSmtpAcceptanceResult(recipients, messageId) {
|
|
1577
|
+
const code = "invalid_command";
|
|
1578
|
+
if (messageId !== void 0 && (typeof messageId !== "string" || messageId.length !== 52 || messageId !== smtpMessageId(uuidV4(messageId.slice(1, 37), code)))) {
|
|
1579
|
+
invalid(code);
|
|
1580
|
+
}
|
|
1581
|
+
const checked = readRecipientResults(recipients, code, {
|
|
1582
|
+
limit: MAX_SMTP_RECIPIENTS,
|
|
1583
|
+
allowAccepted: true,
|
|
1584
|
+
ordered: false
|
|
1585
|
+
});
|
|
1586
|
+
const data = { retry: "never", stage: "data", ...messageId === void 0 ? {} : { messageId } };
|
|
1587
|
+
if (allAccepted(checked)) {
|
|
1588
|
+
return Object.freeze({ ...data, reason: "smtp_accepted", outcome: "accepted", recipients: checked });
|
|
1589
|
+
}
|
|
1590
|
+
if (!checked.some(({ status }) => status === "accepted")) invalid(code);
|
|
1591
|
+
return Object.freeze({
|
|
1592
|
+
...data,
|
|
1593
|
+
reason: "smtp_partial",
|
|
1594
|
+
outcome: "partially_accepted",
|
|
1595
|
+
recipients: checked
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
function uncertainProviderResult(stage, reason) {
|
|
1599
|
+
const base = { outcome: "unknown", retry: "never", stage };
|
|
1600
|
+
if (reason === "provider_timeout") return { ...base, reason };
|
|
1601
|
+
if (reason === "provider_aborted") return { ...base, reason };
|
|
1602
|
+
return { ...base, reason };
|
|
1603
|
+
}
|
|
1421
1604
|
function readHint(input, reason, code) {
|
|
1422
1605
|
if (reason !== "rate_deferred" && reason !== "sendgrid_throttled") invalid(code);
|
|
1423
1606
|
const raw = readRecord(input, [], ["delayMs", "notBefore"], code);
|
|
@@ -1431,7 +1614,7 @@ function readHint(input, reason, code) {
|
|
|
1431
1614
|
...hasFloor ? { notBefore: unixMs(raw.notBefore, code) } : {}
|
|
1432
1615
|
});
|
|
1433
1616
|
}
|
|
1434
|
-
function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
1617
|
+
function readAttemptResult(input, code = "invalid_command", projected = false, capacity) {
|
|
1435
1618
|
const raw = readRecord(
|
|
1436
1619
|
input,
|
|
1437
1620
|
["outcome", "retry", "stage", "reason"],
|
|
@@ -1442,6 +1625,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
1442
1625
|
const [outcome, retry, stages2, origin] = evidence[reason];
|
|
1443
1626
|
const stage = stages2.find((value) => value === raw.stage);
|
|
1444
1627
|
if (raw.outcome !== outcome || raw.retry !== retry || !stage) invalid(code);
|
|
1628
|
+
if (capacity && (origin === "sendgrid" || stage === "http")) invalid(code);
|
|
1445
1629
|
const hasHint = Object.hasOwn(raw, "retryHint");
|
|
1446
1630
|
if (projected && hasHint || !projected && reason === "rate_deferred" && !hasHint) invalid(code);
|
|
1447
1631
|
const smtpSuccess = reason === "smtp_accepted" || reason === "smtp_partial";
|
|
@@ -1450,16 +1634,10 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
1450
1634
|
let recipients;
|
|
1451
1635
|
if (hasRecipients) {
|
|
1452
1636
|
if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
|
|
1458
|
-
const index = integer(rawIndex, 0, MESSAGE_LIMITS.recipients - 1, code);
|
|
1459
|
-
const key = `${group}:${index}`;
|
|
1460
|
-
if (seen.has(key)) invalid(code);
|
|
1461
|
-
seen.add(key);
|
|
1462
|
-
return Object.freeze({ group, index, status });
|
|
1637
|
+
recipients = readRecipientResults(raw.recipients, code, {
|
|
1638
|
+
limit: recipientLimit(capacity),
|
|
1639
|
+
allowAccepted: smtpSuccess,
|
|
1640
|
+
ordered: Boolean(capacity)
|
|
1463
1641
|
});
|
|
1464
1642
|
const accepted = recipients.filter(({ status }) => status === "accepted").length;
|
|
1465
1643
|
if (reason === "smtp_accepted" && (!accepted || accepted !== recipients.length) || reason === "smtp_partial" && (!accepted || accepted === recipients.length)) {
|
|
@@ -1473,7 +1651,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
1473
1651
|
if (hasProviderId && (reason !== "sendgrid_accepted" || typeof providerMessageId !== "string" || providerMessageId.length > 128 || !/^[A-Za-z0-9][A-Za-z0-9._=-]{0,127}$/.test(providerMessageId))) {
|
|
1474
1652
|
invalid(code);
|
|
1475
1653
|
}
|
|
1476
|
-
|
|
1654
|
+
const result = Object.freeze({
|
|
1477
1655
|
outcome,
|
|
1478
1656
|
retry,
|
|
1479
1657
|
stage,
|
|
@@ -1483,6 +1661,8 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
1483
1661
|
...typeof messageId === "string" ? { messageId } : {},
|
|
1484
1662
|
...typeof providerMessageId === "string" ? { providerMessageId } : {}
|
|
1485
1663
|
});
|
|
1664
|
+
if (capacity && !projected) encodeEvidence(result, code, capacity);
|
|
1665
|
+
return result;
|
|
1486
1666
|
}
|
|
1487
1667
|
function assertEvidencePhase(reason, phase, source, code) {
|
|
1488
1668
|
const origin = evidence[reason][3];
|
|
@@ -1508,9 +1688,9 @@ function assertEvidenceContext(result, provider, attemptId, counts, code) {
|
|
|
1508
1688
|
}
|
|
1509
1689
|
}
|
|
1510
1690
|
}
|
|
1511
|
-
function readProviderResult(input, provider, attemptId, envelope) {
|
|
1691
|
+
function readProviderResult(input, provider, attemptId, envelope, capacity) {
|
|
1512
1692
|
const code = "invalid_command";
|
|
1513
|
-
const result = readAttemptResult(input, code);
|
|
1693
|
+
const result = readAttemptResult(input, code, false, capacity);
|
|
1514
1694
|
const origin = evidence[result.reason][3];
|
|
1515
1695
|
if (origin !== "smtp" && origin !== "sendgrid" && origin !== "provider") invalid(code);
|
|
1516
1696
|
assertEvidenceContext(result, provider, attemptId, recipientCounts(envelope), code);
|
|
@@ -1541,10 +1721,7 @@ function attemptEffect(result, attemptId, ordinal, manual, policy, expiresAt, no
|
|
|
1541
1721
|
var REQUEST = "invalid_command";
|
|
1542
1722
|
var LEASE = WORKER_LIMIT_BOUNDS.leaseMs;
|
|
1543
1723
|
function readStoreId(value, code = REQUEST) {
|
|
1544
|
-
|
|
1545
|
-
invalid(code);
|
|
1546
|
-
}
|
|
1547
|
-
return value;
|
|
1724
|
+
return uuidV4(value, code);
|
|
1548
1725
|
}
|
|
1549
1726
|
function readLeaseToken(value, code = REQUEST) {
|
|
1550
1727
|
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)) {
|
|
@@ -1558,16 +1735,20 @@ function readLeaseMs(value) {
|
|
|
1558
1735
|
function timeAfter(start, delayMs) {
|
|
1559
1736
|
return delayMs <= MAX_UNIX_MS - start ? start + delayMs : void 0;
|
|
1560
1737
|
}
|
|
1561
|
-
function readStoreCall(input, worker = false) {
|
|
1738
|
+
function readStoreCall(input, worker = false, capacity) {
|
|
1562
1739
|
const code = "invalid_config";
|
|
1563
|
-
const raw = readRecord(input, ["signal", "timeoutMs"], [], code);
|
|
1740
|
+
const { smtpCapacity, ...raw } = readRecord(input, ["signal", "timeoutMs"], capacity ? ["smtpCapacity"] : [], code);
|
|
1741
|
+
if (capacity) assertCapacityTag({ smtpCapacity }, capacity, "store_incompatible");
|
|
1564
1742
|
const max = worker ? WORKER_LIMIT_BOUNDS.storeTimeoutMs.max : CALL_BUDGETS.admissionOrAdmin.max;
|
|
1565
1743
|
const timeoutMs = integer(raw.timeoutMs, 1, max, code);
|
|
1566
1744
|
const call = readCallOptions(raw, { default: STORE_PRIMITIVE_TIMEOUT_MS, min: 1, max });
|
|
1567
1745
|
if (!call.signal) invalid(code);
|
|
1568
|
-
return {
|
|
1746
|
+
return {
|
|
1747
|
+
signal: call.signal,
|
|
1748
|
+
timeoutMs: worker && !capacity ? timeoutMs : Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS)
|
|
1749
|
+
};
|
|
1569
1750
|
}
|
|
1570
|
-
function readStoreNext(value, read) {
|
|
1751
|
+
function readStoreNext(value, read, kind) {
|
|
1571
1752
|
const code = "store_incompatible";
|
|
1572
1753
|
const raw = readRecord(value, ["status"], ["value"], code);
|
|
1573
1754
|
if (raw.status === "applied" && Object.hasOwn(raw, "value")) {
|
|
@@ -1576,6 +1757,9 @@ function readStoreNext(value, read) {
|
|
|
1576
1757
|
if ((raw.status === "none" || raw.status === "contended") && !Object.hasOwn(raw, "value")) {
|
|
1577
1758
|
return Object.freeze({ status: raw.status });
|
|
1578
1759
|
}
|
|
1760
|
+
if (kind === "claim" && raw.status === "expiry_maintenance_required" && !Object.hasOwn(raw, "value")) {
|
|
1761
|
+
return Object.freeze({ status: raw.status });
|
|
1762
|
+
}
|
|
1579
1763
|
return invalid(code);
|
|
1580
1764
|
}
|
|
1581
1765
|
function readRetryPolicy(input, code = REQUEST) {
|
|
@@ -1602,9 +1786,9 @@ function readRetentionPolicy(input, code = REQUEST) {
|
|
|
1602
1786
|
idempotencyMs: integer(idempotencyMs, bounds.idempotencyMs.min, bounds.idempotencyMs.max, code)
|
|
1603
1787
|
});
|
|
1604
1788
|
}
|
|
1605
|
-
function readStoreAdmission(scope, input) {
|
|
1789
|
+
function readStoreAdmission(scope, input, capacity) {
|
|
1606
1790
|
const raw = readRecord(input, ["command", "jobId", "retry", "retention"], [], REQUEST);
|
|
1607
|
-
const command = validatePreparedCommand(scope, raw.command);
|
|
1791
|
+
const command = validatePreparedCommand(scope, raw.command, capacity);
|
|
1608
1792
|
return Object.freeze({
|
|
1609
1793
|
command,
|
|
1610
1794
|
admissionKey: encodeAdmissionKey(command.key),
|
|
@@ -1625,7 +1809,7 @@ function readClaim(input) {
|
|
|
1625
1809
|
leaseMs: readLeaseMs(leaseMs)
|
|
1626
1810
|
});
|
|
1627
1811
|
}
|
|
1628
|
-
function readFinish(input) {
|
|
1812
|
+
function readFinish(input, capacity) {
|
|
1629
1813
|
const { fence, phase, operationId, result } = readRecord(
|
|
1630
1814
|
input,
|
|
1631
1815
|
["fence", "phase", "operationId", "result"],
|
|
@@ -1633,7 +1817,7 @@ function readFinish(input) {
|
|
|
1633
1817
|
REQUEST
|
|
1634
1818
|
);
|
|
1635
1819
|
if (phase !== "pre_handoff" && phase !== "handed_off") invalid(REQUEST);
|
|
1636
|
-
const evidence2 = readAttemptResult(result);
|
|
1820
|
+
const evidence2 = readAttemptResult(result, REQUEST, false, capacity);
|
|
1637
1821
|
assertEvidencePhase(evidence2.reason, phase, "owner", REQUEST);
|
|
1638
1822
|
return Object.freeze({ fence: readFence(fence), phase, operationId: readStoreId(operationId), result: evidence2 });
|
|
1639
1823
|
}
|
|
@@ -1650,7 +1834,6 @@ function readResolution(input) {
|
|
|
1650
1834
|
}
|
|
1651
1835
|
|
|
1652
1836
|
// src/internal/activity-codecs.ts
|
|
1653
|
-
var ACTIVITY_BYTES = 8192;
|
|
1654
1837
|
var dispositions = [
|
|
1655
1838
|
"accepted",
|
|
1656
1839
|
"partially_accepted",
|
|
@@ -1677,7 +1860,7 @@ function assertActivityDisposition(result, disposition, code) {
|
|
|
1677
1860
|
invalid(code);
|
|
1678
1861
|
}
|
|
1679
1862
|
}
|
|
1680
|
-
function
|
|
1863
|
+
function captureActivity(scope, input, code, capacity) {
|
|
1681
1864
|
const raw = readRecord(
|
|
1682
1865
|
input,
|
|
1683
1866
|
[
|
|
@@ -1694,26 +1877,32 @@ function readActivity(scope, input, code = "store_incompatible") {
|
|
|
1694
1877
|
"result",
|
|
1695
1878
|
"disposition"
|
|
1696
1879
|
],
|
|
1697
|
-
["handedOffAt"],
|
|
1880
|
+
["handedOffAt", "smtpCapacity", "recipientCounts"],
|
|
1698
1881
|
code
|
|
1699
1882
|
);
|
|
1700
1883
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, code) !== scope) invalid(code);
|
|
1884
|
+
assertCapacityTag(raw, capacity, code);
|
|
1885
|
+
if (!capacity && Object.hasOwn(raw, "recipientCounts")) invalid(code);
|
|
1886
|
+
const counts = capacity ? readRecipientCounts(raw.recipientCounts, code, capacity) : void 0;
|
|
1701
1887
|
const jobId = readStoreId(raw.jobId, code);
|
|
1702
1888
|
const attemptId = readStoreId(raw.attemptId, code);
|
|
1703
1889
|
const provider = validateProviderRef(raw.provider, code);
|
|
1890
|
+
assertCapacityProvider(provider, capacity, code);
|
|
1704
1891
|
const startedAt = unixMs(raw.startedAt, code);
|
|
1705
1892
|
const finishedAt = unixMs(raw.finishedAt, code);
|
|
1706
1893
|
const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, code) : void 0;
|
|
1707
1894
|
if (finishedAt < startedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt > finishedAt)) {
|
|
1708
1895
|
invalid(code);
|
|
1709
1896
|
}
|
|
1710
|
-
const result = readAttemptResult(raw.result, code, true);
|
|
1897
|
+
const result = readAttemptResult(raw.result, code, true, capacity);
|
|
1711
1898
|
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", code);
|
|
1712
|
-
assertEvidenceContext(result, provider, attemptId,
|
|
1899
|
+
assertEvidenceContext(result, provider, attemptId, counts, code);
|
|
1713
1900
|
const disposition = readDisposition(raw.disposition, code);
|
|
1714
1901
|
assertActivityDisposition(result, disposition, code);
|
|
1715
1902
|
return Object.freeze({
|
|
1716
1903
|
version: EMAIL_FORMAT_VERSION,
|
|
1904
|
+
...capacityTag(capacity),
|
|
1905
|
+
...counts ? { recipientCounts: counts } : {},
|
|
1717
1906
|
scope,
|
|
1718
1907
|
jobId,
|
|
1719
1908
|
attemptId,
|
|
@@ -1728,50 +1917,62 @@ function readActivity(scope, input, code = "store_incompatible") {
|
|
|
1728
1917
|
disposition
|
|
1729
1918
|
});
|
|
1730
1919
|
}
|
|
1731
|
-
function
|
|
1920
|
+
function readActivity(scope, input, code = "store_incompatible", capacity) {
|
|
1921
|
+
const activity = captureActivity(scope, input, code, capacity);
|
|
1922
|
+
if (capacity) encodeEvidence(activity, code, capacity);
|
|
1923
|
+
return activity;
|
|
1924
|
+
}
|
|
1925
|
+
function activityFor(job, commit, capacity) {
|
|
1732
1926
|
const { outcome, retry, stage, reason, recipients, messageId, providerMessageId } = commit.result;
|
|
1733
|
-
return
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1927
|
+
return captureActivity(
|
|
1928
|
+
job.scope,
|
|
1929
|
+
{
|
|
1930
|
+
version: EMAIL_FORMAT_VERSION,
|
|
1931
|
+
...capacityTag(capacity),
|
|
1932
|
+
...capacity ? { recipientCounts: job.recipientCounts } : {},
|
|
1933
|
+
scope: job.scope,
|
|
1934
|
+
jobId: job.jobId,
|
|
1935
|
+
attemptId: commit.attemptId,
|
|
1936
|
+
ordinal: commit.ordinal,
|
|
1937
|
+
provider: job.provider,
|
|
1938
|
+
template: job.template,
|
|
1939
|
+
correlation: job.correlation,
|
|
1940
|
+
startedAt: commit.startedAt,
|
|
1941
|
+
...commit.handedOffAt === void 0 ? {} : { handedOffAt: commit.handedOffAt },
|
|
1942
|
+
finishedAt: commit.finishedAt,
|
|
1943
|
+
disposition: commit.disposition,
|
|
1944
|
+
result: {
|
|
1945
|
+
outcome,
|
|
1946
|
+
retry,
|
|
1947
|
+
stage,
|
|
1948
|
+
reason,
|
|
1949
|
+
...recipients === void 0 ? {} : { recipients },
|
|
1950
|
+
...messageId === void 0 ? {} : { messageId },
|
|
1951
|
+
...providerMessageId === void 0 ? {} : { providerMessageId }
|
|
1952
|
+
}
|
|
1953
|
+
},
|
|
1954
|
+
"store_incompatible",
|
|
1955
|
+
capacity
|
|
1956
|
+
);
|
|
1756
1957
|
}
|
|
1757
|
-
function activityDigest(activity) {
|
|
1758
|
-
return
|
|
1958
|
+
function activityDigest(activity, capacity) {
|
|
1959
|
+
return sha256(encodeEvidence(activity, "store_incompatible", capacity));
|
|
1759
1960
|
}
|
|
1760
|
-
function readStoredActivity(scope, input, code = "store_incompatible") {
|
|
1961
|
+
function readStoredActivity(scope, input, code = "store_incompatible", capacity) {
|
|
1761
1962
|
const raw = readRecord(input, ["activity", "digest"], [], code);
|
|
1762
|
-
const activity =
|
|
1963
|
+
const activity = captureActivity(scope, raw.activity, code, capacity);
|
|
1763
1964
|
const digest = hashToken(raw.digest, code);
|
|
1764
|
-
if (activityDigest(activity) !== digest) invalid(code);
|
|
1965
|
+
if (activityDigest(activity, capacity) !== digest) invalid(code);
|
|
1765
1966
|
return Object.freeze({ activity, digest });
|
|
1766
1967
|
}
|
|
1767
|
-
function readProjectionPermit(scope, input) {
|
|
1968
|
+
function readProjectionPermit(scope, input, capacity) {
|
|
1768
1969
|
const code = "invalid_command";
|
|
1769
1970
|
const {
|
|
1770
1971
|
activity,
|
|
1771
1972
|
digest,
|
|
1772
1973
|
permitUntil: rawUntil
|
|
1773
1974
|
} = readRecord(input, ["activity", "digest", "permitUntil"], [], code);
|
|
1774
|
-
const stored = readStoredActivity(scope, { activity, digest }, code);
|
|
1975
|
+
const stored = readStoredActivity(scope, { activity, digest }, code, capacity);
|
|
1775
1976
|
const permitUntil = unixMs(rawUntil, code);
|
|
1776
1977
|
if (permitUntil - stored.activity.finishedAt < PROJECTION_PERMIT_MS) invalid(code);
|
|
1777
1978
|
return Object.freeze({ ...stored, permitUntil });
|
|
@@ -1783,13 +1984,15 @@ function compareActivity(a, b) {
|
|
|
1783
1984
|
return b.finishedAt - a.finishedAt || compareAscii(a.jobId, b.jobId) || compareAscii(a.attemptId, b.attemptId);
|
|
1784
1985
|
}
|
|
1785
1986
|
function cursorPayload(criteria, position) {
|
|
1987
|
+
const binding = criteria.capacity?.binding;
|
|
1786
1988
|
return {
|
|
1787
1989
|
v: EMAIL_FORMAT_VERSION,
|
|
1788
1990
|
s: criteria.scope,
|
|
1789
1991
|
j: criteria.jobId,
|
|
1790
1992
|
f: criteria.from,
|
|
1791
1993
|
b: criteria.before,
|
|
1792
|
-
p: [position.finishedAt, position.jobId, position.attemptId]
|
|
1994
|
+
p: [position.finishedAt, position.jobId, position.attemptId],
|
|
1995
|
+
...binding ? { c: [binding.format, binding.namespace, binding.maxRecipients] } : {}
|
|
1793
1996
|
};
|
|
1794
1997
|
}
|
|
1795
1998
|
function encodeCursor(criteria, position) {
|
|
@@ -1810,7 +2013,7 @@ function decodeCursor(criteria, cursor) {
|
|
|
1810
2013
|
} catch {
|
|
1811
2014
|
invalid(code);
|
|
1812
2015
|
}
|
|
1813
|
-
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], [], code);
|
|
2016
|
+
const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], criteria.capacity ? ["c"] : [], code);
|
|
1814
2017
|
const tuple = readArray(p, 3, code, (entry) => entry);
|
|
1815
2018
|
const position = Object.freeze({
|
|
1816
2019
|
finishedAt: unixMs(tuple[0], code),
|
|
@@ -1822,15 +2025,16 @@ function decodeCursor(criteria, cursor) {
|
|
|
1822
2025
|
}
|
|
1823
2026
|
return position;
|
|
1824
2027
|
}
|
|
1825
|
-
function readActivityCriteria(scope, input) {
|
|
2028
|
+
function readActivityCriteria(scope, input, capacity) {
|
|
1826
2029
|
const code = "invalid_command";
|
|
1827
2030
|
const raw = readRecord(input, [], ["jobId", "from", "before", "limit", "cursor"], code);
|
|
1828
2031
|
const jobId = Object.hasOwn(raw, "jobId") ? readStoreId(raw.jobId, code) : null;
|
|
1829
2032
|
const from = Object.hasOwn(raw, "from") ? unixMs(raw.from, code) : null;
|
|
1830
2033
|
const before = Object.hasOwn(raw, "before") ? unixMs(raw.before, code) : null;
|
|
1831
2034
|
if (from !== null && before !== null && from > before) invalid(code);
|
|
1832
|
-
const
|
|
1833
|
-
const
|
|
2035
|
+
const page = capacity?.limits.page ?? ACTIVITY_PAGE_LIMIT;
|
|
2036
|
+
const limit = Object.hasOwn(raw, "limit") ? integer(raw.limit, page.min, page.max, code) : page.default;
|
|
2037
|
+
const criteria = { scope, jobId, from, before, limit, ...capacity ? { capacity } : {} };
|
|
1834
2038
|
return Object.freeze({
|
|
1835
2039
|
...criteria,
|
|
1836
2040
|
...Object.hasOwn(raw, "cursor") ? { after: decodeCursor(criteria, raw.cursor) } : {}
|
|
@@ -1850,7 +2054,7 @@ function readOrderedActivities(criteria, input, maxRows) {
|
|
|
1850
2054
|
const code = "store_incompatible";
|
|
1851
2055
|
let previous = criteria.after;
|
|
1852
2056
|
return readArray(input, maxRows, code, (row) => {
|
|
1853
|
-
const activity = readActivity(criteria.scope, row);
|
|
2057
|
+
const activity = readActivity(criteria.scope, row, code, criteria.capacity);
|
|
1854
2058
|
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) {
|
|
1855
2059
|
invalid(code);
|
|
1856
2060
|
}
|
|
@@ -1900,14 +2104,18 @@ function readUnknownResolution(input) {
|
|
|
1900
2104
|
if (action !== "dismiss" && action !== "redrive") invalid(STORED);
|
|
1901
2105
|
return Object.freeze({ attemptId: readStoreId(attemptId, STORED), at: unixMs(at, STORED), action });
|
|
1902
2106
|
}
|
|
1903
|
-
function readJobSummary(scope, input) {
|
|
2107
|
+
function readJobSummary(scope, input, capacity) {
|
|
1904
2108
|
const raw = readRecord(
|
|
1905
2109
|
input,
|
|
1906
2110
|
COMMON,
|
|
1907
|
-
["latestAttempt", "nextAt", "phase", "disposition", "terminalReason", "terminalAt"],
|
|
2111
|
+
["latestAttempt", "nextAt", "phase", "disposition", "terminalReason", "terminalAt", "smtpCapacity"],
|
|
1908
2112
|
STORED
|
|
1909
2113
|
);
|
|
1910
2114
|
if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, STORED) !== scope) invalid(STORED);
|
|
2115
|
+
assertCapacityTag(raw, capacity, STORED);
|
|
2116
|
+
const optional = capacity ? ["latestAttempt", "smtpCapacity"] : ["latestAttempt"];
|
|
2117
|
+
const provider = validateProviderRef(raw.provider, STORED);
|
|
2118
|
+
assertCapacityProvider(provider, capacity, STORED);
|
|
1911
2119
|
const createdAt = unixMs(raw.createdAt, STORED);
|
|
1912
2120
|
const updatedAt = unixMs(raw.updatedAt, STORED);
|
|
1913
2121
|
const expiresAt = unixMs(raw.expiresAt, STORED);
|
|
@@ -1916,16 +2124,16 @@ function readJobSummary(scope, input) {
|
|
|
1916
2124
|
if (updatedAt < createdAt || firstDue >= expiresAt) invalid(STORED);
|
|
1917
2125
|
let state;
|
|
1918
2126
|
if (raw.state === "queued") {
|
|
1919
|
-
readRecord(raw, [...COMMON, "nextAt"],
|
|
2127
|
+
readRecord(raw, [...COMMON, "nextAt"], optional, STORED);
|
|
1920
2128
|
const nextAt = unixMs(raw.nextAt, STORED);
|
|
1921
2129
|
if (nextAt < firstDue || nextAt >= expiresAt) invalid(STORED);
|
|
1922
2130
|
state = { state: raw.state, nextAt };
|
|
1923
2131
|
} else if (raw.state === "leased") {
|
|
1924
|
-
readRecord(raw, [...COMMON, "phase"],
|
|
2132
|
+
readRecord(raw, [...COMMON, "phase"], optional, STORED);
|
|
1925
2133
|
if (raw.phase !== "pre_handoff" && raw.phase !== "handed_off") invalid(STORED);
|
|
1926
2134
|
state = { state: raw.state, phase: raw.phase };
|
|
1927
2135
|
} else if (raw.state === "terminal") {
|
|
1928
|
-
readRecord(raw, [...COMMON, "disposition", "terminalAt", "terminalReason"],
|
|
2136
|
+
readRecord(raw, [...COMMON, "disposition", "terminalAt", "terminalReason"], optional, STORED);
|
|
1929
2137
|
const terminalAt = unixMs(raw.terminalAt, STORED);
|
|
1930
2138
|
const disposition = readDisposition(raw.disposition, STORED);
|
|
1931
2139
|
if (terminalAt < createdAt || terminalAt > updatedAt || disposition === "retry_scheduled") invalid(STORED);
|
|
@@ -1950,10 +2158,11 @@ function readJobSummary(scope, input) {
|
|
|
1950
2158
|
const resolutions = readArray(raw.resolutions, Math.min(2, attemptCount - active), STORED, readUnknownResolution);
|
|
1951
2159
|
return Object.freeze({
|
|
1952
2160
|
version: EMAIL_FORMAT_VERSION,
|
|
2161
|
+
...capacityTag(capacity),
|
|
1953
2162
|
scope,
|
|
1954
2163
|
jobId: readStoreId(raw.jobId, STORED),
|
|
1955
2164
|
key: validateAdmissionKey(raw.key, STORED),
|
|
1956
|
-
provider
|
|
2165
|
+
provider,
|
|
1957
2166
|
template: validateTemplateIdentity(raw.template, STORED),
|
|
1958
2167
|
correlation: validateCorrelation(raw.correlation, STORED),
|
|
1959
2168
|
scheduleAt,
|
|
@@ -2005,7 +2214,7 @@ async function runStoreCall(clock, parent, options) {
|
|
|
2005
2214
|
budget.check();
|
|
2006
2215
|
invalid("deadline");
|
|
2007
2216
|
}
|
|
2008
|
-
const call = Object.freeze({ timeoutMs, signal: budget.signal });
|
|
2217
|
+
const call = Object.freeze({ timeoutMs, signal: budget.signal, ...capacityTag(options.capacity) });
|
|
2009
2218
|
let raw;
|
|
2010
2219
|
try {
|
|
2011
2220
|
const pending = options.invoke(call, dispatchedAt);
|
|
@@ -2067,16 +2276,18 @@ var serviceAdmissions = /* @__PURE__ */ new WeakMap();
|
|
|
2067
2276
|
function findServiceAdmission(service) {
|
|
2068
2277
|
return serviceAdmissions.get(service);
|
|
2069
2278
|
}
|
|
2279
|
+
function createEmailRelay(options) {
|
|
2280
|
+
const raw = readRecord(options, ["scope", "store"], ["retry", "retention", "smtpCapacity"], CONFIG);
|
|
2281
|
+
const { enqueuePrepared } = createAdmissionCore(raw, systemEmailClock);
|
|
2282
|
+
return Object.freeze({ enqueuePrepared });
|
|
2283
|
+
}
|
|
2070
2284
|
function createEmailService(options) {
|
|
2071
2285
|
return createEmailServiceWithClock(options, systemEmailClock);
|
|
2072
2286
|
}
|
|
2073
2287
|
function createEmailServiceWithClock(options, clock) {
|
|
2074
|
-
const raw = readRecord(options, ["scope", "store", "registry"], ["retry", "retention"], CONFIG);
|
|
2075
|
-
const scope =
|
|
2076
|
-
|
|
2077
|
-
const retention = readRetentionPolicy(readPolicy(raw.retention, DEFAULT_RETENTION_POLICY), CONFIG);
|
|
2078
|
-
const find = bindMethod(raw.store, "findAdmission");
|
|
2079
|
-
const insert = bindMethod(raw.store, "insert");
|
|
2288
|
+
const raw = readRecord(options, ["scope", "store", "registry"], ["retry", "retention", "smtpCapacity"], CONFIG);
|
|
2289
|
+
const { scope, capacity, enqueuePrepared, admitParsed, inOperation, withAdmission } = createAdmissionCore(raw, clock);
|
|
2290
|
+
assertPortCapacity(raw.registry, capacity);
|
|
2080
2291
|
if (typeof raw.registry !== "object" || raw.registry === null || import_node_util7.types.isProxy(raw.registry)) invalid(CONFIG);
|
|
2081
2292
|
const registry = raw.registry;
|
|
2082
2293
|
const prepare = bindMethod(registry, "prepare");
|
|
@@ -2084,7 +2295,7 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2084
2295
|
async function prepareMiss(parsed, parent) {
|
|
2085
2296
|
const { identity, command } = parsed;
|
|
2086
2297
|
const accept = (value) => {
|
|
2087
|
-
const staged = validatePreparedCommand(scope, value);
|
|
2298
|
+
const staged = validatePreparedCommand(scope, value, capacity);
|
|
2088
2299
|
if (!sameCommandMetadata(
|
|
2089
2300
|
staged.key,
|
|
2090
2301
|
staged.snapshot.template.key,
|
|
@@ -2113,7 +2324,7 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2113
2324
|
slots = { pending: 0 };
|
|
2114
2325
|
injectedRegistrySlots.set(registry, slots);
|
|
2115
2326
|
}
|
|
2116
|
-
if (slots.pending >= TEMPLATE_MAX_PENDING.default) invalid("busy");
|
|
2327
|
+
if (slots.pending >= (capacity?.limits.registryPending.default ?? TEMPLATE_MAX_PENDING.default)) invalid("busy");
|
|
2117
2328
|
const budget = createChildBudget(clock, parent, CALL_BUDGETS.prepare.default);
|
|
2118
2329
|
const occupied = slots;
|
|
2119
2330
|
slots.pending++;
|
|
@@ -2140,15 +2351,37 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2140
2351
|
slot.close();
|
|
2141
2352
|
}
|
|
2142
2353
|
}
|
|
2354
|
+
const enqueueParsed = (parsed, budget) => admitParsed(parsed, budget, prepareMiss);
|
|
2355
|
+
const service = Object.freeze({
|
|
2356
|
+
scope,
|
|
2357
|
+
...capacityTag(capacity),
|
|
2358
|
+
enqueue(command, call = {}) {
|
|
2359
|
+
return inOperation(call, (budget) => enqueueParsed(parseCommandIdentity(scope, command, capacity), budget));
|
|
2360
|
+
},
|
|
2361
|
+
enqueuePrepared
|
|
2362
|
+
});
|
|
2363
|
+
serviceAdmissions.set(service, (parsed, budget) => withAdmission(() => enqueueParsed(parsed, budget)));
|
|
2364
|
+
return service;
|
|
2365
|
+
}
|
|
2366
|
+
function createAdmissionCore(raw, clock) {
|
|
2367
|
+
const capacity = readCapacityOption(raw);
|
|
2368
|
+
assertPortCapacity(raw.store, capacity);
|
|
2369
|
+
const scope = scopeToken(raw.scope, CONFIG);
|
|
2370
|
+
const retry = readRetryPolicy(readPolicy(raw.retry, DEFAULT_RETRY_POLICY), CONFIG);
|
|
2371
|
+
const retention = readRetentionPolicy(readPolicy(raw.retention, DEFAULT_RETENTION_POLICY), CONFIG);
|
|
2372
|
+
const find = bindMethod(raw.store, "findAdmission");
|
|
2373
|
+
const insert = bindMethod(raw.store, "insert");
|
|
2374
|
+
let admissions = 0;
|
|
2143
2375
|
function findWinner(key, budget) {
|
|
2144
2376
|
return runStoreCall(clock, budget, {
|
|
2145
2377
|
maxMs: STORE_PRIMITIVE_TIMEOUT_MS,
|
|
2378
|
+
capacity,
|
|
2146
2379
|
effect: "read",
|
|
2147
2380
|
invoke: (call) => find(scope, key, call),
|
|
2148
2381
|
accept(value) {
|
|
2149
2382
|
if (value === null) return null;
|
|
2150
2383
|
const raw2 = readRecord(value, ["job", "fingerprint"], [], STORED2);
|
|
2151
|
-
const job = readJobSummary(scope, raw2.job);
|
|
2384
|
+
const job = readJobSummary(scope, raw2.job, capacity);
|
|
2152
2385
|
if (encodeAdmissionKey(job.key) !== encodeAdmissionKey(key)) invalid(STORED2);
|
|
2153
2386
|
return { job, fingerprint: hashToken(raw2.fingerprint, STORED2) };
|
|
2154
2387
|
}
|
|
@@ -2162,10 +2395,12 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2162
2395
|
}
|
|
2163
2396
|
async function insertOrConfirm(command, identity, budget) {
|
|
2164
2397
|
budget.check();
|
|
2398
|
+
if (command.snapshot.kind === "smtp") assertSmtpAttachmentTypes(command.snapshot.attachments);
|
|
2165
2399
|
const admission = Object.freeze({ command, jobId: (0, import_node_crypto2.randomUUID)(), retry, retention });
|
|
2166
2400
|
try {
|
|
2167
2401
|
const job = await runStoreCall(clock, budget, {
|
|
2168
2402
|
maxMs: STORE_PRIMITIVE_TIMEOUT_MS,
|
|
2403
|
+
capacity,
|
|
2169
2404
|
effect: "mutation",
|
|
2170
2405
|
definite: INSERT_REJECTIONS,
|
|
2171
2406
|
invoke: (call) => insert(scope, admission, call),
|
|
@@ -2173,7 +2408,7 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2173
2408
|
const raw2 = readRecord(value, ["status"], ["job"], STORED2);
|
|
2174
2409
|
if (raw2.status === "duplicate" && !Object.hasOwn(raw2, "job")) return null;
|
|
2175
2410
|
if (raw2.status !== "inserted") invalid(STORED2);
|
|
2176
|
-
const job2 = readJobSummary(scope, raw2.job);
|
|
2411
|
+
const job2 = readJobSummary(scope, raw2.job, capacity);
|
|
2177
2412
|
const { snapshot } = command;
|
|
2178
2413
|
if (job2.jobId !== admission.jobId || !matchesAdmissionJob(job2, identity, "enqueued") || job2.template.revision !== snapshot.template.revision || snapshot.preparedAt - job2.createdAt > MAX_PREPARED_AT_SKEW_MS || job2.expiresAt !== snapshot.expiresAt || job2.priority !== snapshot.priority || !sameCorrelation(job2.correlation, snapshot.correlation)) {
|
|
2179
2414
|
invalid(STORED2);
|
|
@@ -2193,46 +2428,51 @@ function createEmailServiceWithClock(options, clock) {
|
|
|
2193
2428
|
}
|
|
2194
2429
|
return invalid("write_unconfirmed");
|
|
2195
2430
|
}
|
|
2196
|
-
async function
|
|
2431
|
+
async function admitParsed(parsed, budget, prepareMiss) {
|
|
2197
2432
|
if (parsed.identity.scope !== scope) invalid("invalid_command");
|
|
2198
2433
|
const winner = await findWinner(parsed.identity.key, budget);
|
|
2199
2434
|
if (winner) return replay(winner, parsed.identity);
|
|
2200
2435
|
return insertOrConfirm(await prepareMiss(parsed, budget), parsed.identity, budget);
|
|
2201
2436
|
}
|
|
2202
|
-
async function
|
|
2203
|
-
|
|
2204
|
-
|
|
2437
|
+
async function withAdmission(run) {
|
|
2438
|
+
if (!capacity) return run();
|
|
2439
|
+
if (admissions >= capacity.limits.servicePending) invalid("busy");
|
|
2440
|
+
admissions++;
|
|
2205
2441
|
try {
|
|
2206
|
-
|
|
2207
|
-
return await run(budget);
|
|
2442
|
+
return await run();
|
|
2208
2443
|
} finally {
|
|
2209
|
-
|
|
2444
|
+
admissions--;
|
|
2210
2445
|
}
|
|
2211
2446
|
}
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2447
|
+
function inOperation(call, run) {
|
|
2448
|
+
return withAdmission(async () => {
|
|
2449
|
+
const { timeoutMs, signal } = readCallOptions(call, CALL_BUDGETS.admissionOrAdmin);
|
|
2450
|
+
const budget = createBudget(clock, timeoutMs, signal);
|
|
2451
|
+
try {
|
|
2452
|
+
budget.check();
|
|
2453
|
+
return await run(budget);
|
|
2454
|
+
} finally {
|
|
2455
|
+
budget.close();
|
|
2456
|
+
}
|
|
2457
|
+
});
|
|
2458
|
+
}
|
|
2459
|
+
function enqueuePrepared(input, call = {}) {
|
|
2460
|
+
return inOperation(call, async (budget) => {
|
|
2461
|
+
const command = validatePreparedCommand(scope, input, capacity);
|
|
2462
|
+
const identity = {
|
|
2463
|
+
scope,
|
|
2464
|
+
key: command.key,
|
|
2465
|
+
templateKey: command.snapshot.template.key,
|
|
2466
|
+
provider: command.snapshot.provider,
|
|
2467
|
+
scheduleAt: command.scheduleAt,
|
|
2468
|
+
intentHash: command.intentHash,
|
|
2469
|
+
fingerprint: command.fingerprint
|
|
2470
|
+
};
|
|
2471
|
+
const winner = await findWinner(identity.key, budget);
|
|
2472
|
+
return winner ? replay(winner, identity) : insertOrConfirm(command, identity, budget);
|
|
2473
|
+
});
|
|
2474
|
+
}
|
|
2475
|
+
return { scope, capacity, enqueuePrepared, admitParsed, inOperation, withAdmission };
|
|
2236
2476
|
}
|
|
2237
2477
|
function readPolicy(input, defaults) {
|
|
2238
2478
|
const result = { ...defaults };
|
|
@@ -2256,30 +2496,36 @@ var import_node_util8 = require("util");
|
|
|
2256
2496
|
|
|
2257
2497
|
// src/internal/worker-config.ts
|
|
2258
2498
|
var CONFIG2 = "invalid_config";
|
|
2259
|
-
function readLimits(input) {
|
|
2499
|
+
function readLimits(input, capacity) {
|
|
2260
2500
|
const keys = Object.keys(WORKER_LIMIT_BOUNDS);
|
|
2261
2501
|
const raw = readRecord(input === void 0 ? {} : input, [], keys, CONFIG2);
|
|
2262
|
-
const
|
|
2263
|
-
for (const key of keys)
|
|
2264
|
-
|
|
2502
|
+
const limits2 = {};
|
|
2503
|
+
for (const key of keys) {
|
|
2504
|
+
const bounds = key === "maxConcurrency" ? capacity?.limits.workerConcurrency : void 0;
|
|
2505
|
+
limits2[key] = boundedOption(raw[key], bounds ?? WORKER_LIMIT_BOUNDS[key]);
|
|
2506
|
+
}
|
|
2507
|
+
const { leaseMs, storeTimeoutMs, providerTimeoutMs, finalizeGraceMs } = limits2;
|
|
2265
2508
|
if (leaseMs < Math.max(
|
|
2266
2509
|
3 * storeTimeoutMs + CLOCK_MARGIN_MS,
|
|
2267
2510
|
storeTimeoutMs + providerTimeoutMs + finalizeGraceMs + CLOCK_MARGIN_MS
|
|
2268
2511
|
)) {
|
|
2269
2512
|
invalid(CONFIG2);
|
|
2270
2513
|
}
|
|
2271
|
-
return Object.freeze(
|
|
2514
|
+
return Object.freeze(limits2);
|
|
2272
2515
|
}
|
|
2273
2516
|
function sameProvider(a, b) {
|
|
2274
2517
|
return a.kind === b.kind && a.key === b.key && a.mode === b.mode;
|
|
2275
2518
|
}
|
|
2276
|
-
function readProfiles(input) {
|
|
2519
|
+
function readProfiles(input, capacity) {
|
|
2277
2520
|
const profiles = /* @__PURE__ */ new Map();
|
|
2278
|
-
readArray(input, Number.MAX_SAFE_INTEGER, CONFIG2, (provider) => {
|
|
2521
|
+
readArray(input, capacity?.limits.workerProfiles ?? Number.MAX_SAFE_INTEGER, CONFIG2, (provider) => {
|
|
2279
2522
|
const ref = validateProviderRef(readPortMember(provider, "ref"), CONFIG2);
|
|
2523
|
+
assertPortCapacity(provider, capacity);
|
|
2524
|
+
assertCapacityProvider(ref, capacity, CONFIG2);
|
|
2280
2525
|
if (profiles.has(ref.key)) invalid(CONFIG2);
|
|
2281
2526
|
profiles.set(ref.key, Object.freeze({ ref, prepare: bindMethod(provider, "prepare") }));
|
|
2282
2527
|
});
|
|
2528
|
+
if (profiles.size === 0) invalid(CONFIG2);
|
|
2283
2529
|
return profiles;
|
|
2284
2530
|
}
|
|
2285
2531
|
function readResolvers(input) {
|
|
@@ -2317,15 +2563,18 @@ function readWorkerConfig(input) {
|
|
|
2317
2563
|
const raw = readRecord(
|
|
2318
2564
|
input,
|
|
2319
2565
|
["scope", "store", "providers"],
|
|
2320
|
-
["attachmentResolvers", "rateLimiter", "clock", "limits"],
|
|
2566
|
+
["attachmentResolvers", "rateLimiter", "clock", "limits", "smtpCapacity"],
|
|
2321
2567
|
CONFIG2
|
|
2322
2568
|
);
|
|
2323
2569
|
const { store } = raw;
|
|
2570
|
+
const capacity = readCapacityOption(raw);
|
|
2571
|
+
assertPortCapacity(store, capacity);
|
|
2324
2572
|
return Object.freeze({
|
|
2573
|
+
capacity,
|
|
2325
2574
|
scope: scopeToken(raw.scope, CONFIG2),
|
|
2326
|
-
limits: readLimits(raw.limits),
|
|
2575
|
+
limits: readLimits(raw.limits, capacity),
|
|
2327
2576
|
clock: readWorkerClock(raw.clock),
|
|
2328
|
-
profiles: readProfiles(raw.providers),
|
|
2577
|
+
profiles: readProfiles(raw.providers, capacity),
|
|
2329
2578
|
resolvers: readResolvers(raw.attachmentResolvers),
|
|
2330
2579
|
limiter: readLimiter(raw.rateLimiter),
|
|
2331
2580
|
store: Object.freeze({
|
|
@@ -2339,7 +2588,7 @@ function readWorkerConfig(input) {
|
|
|
2339
2588
|
})
|
|
2340
2589
|
});
|
|
2341
2590
|
}
|
|
2342
|
-
function readRunOptions(input,
|
|
2591
|
+
function readRunOptions(input, limits2, capacity) {
|
|
2343
2592
|
const { limit, concurrency, maintenanceLimit, ...call } = readRecord(
|
|
2344
2593
|
input,
|
|
2345
2594
|
[],
|
|
@@ -2348,9 +2597,9 @@ function readRunOptions(input, limits) {
|
|
|
2348
2597
|
);
|
|
2349
2598
|
return Object.freeze({
|
|
2350
2599
|
...readCallOptions(call, CALL_BUDGETS.batchOrRun),
|
|
2351
|
-
limit: boundedOption(limit, RUN_CLAIM_LIMIT),
|
|
2352
|
-
concurrency: boundedOption(concurrency, { min: 1, max:
|
|
2353
|
-
maintenanceLimit: boundedOption(maintenanceLimit, RUN_MAINTENANCE_LIMIT)
|
|
2600
|
+
limit: boundedOption(limit, capacity?.limits.runClaims ?? RUN_CLAIM_LIMIT),
|
|
2601
|
+
concurrency: boundedOption(concurrency, { min: 1, max: limits2.maxConcurrency, default: limits2.maxConcurrency }),
|
|
2602
|
+
maintenanceLimit: boundedOption(maintenanceLimit, capacity?.limits.runMaintenance ?? RUN_MAINTENANCE_LIMIT)
|
|
2354
2603
|
});
|
|
2355
2604
|
}
|
|
2356
2605
|
function readShutdownOptions(input) {
|
|
@@ -2366,10 +2615,10 @@ function readGrant(times, leaseMs) {
|
|
|
2366
2615
|
if (leaseEndsAt - grantedAt !== leaseMs) invalid(STORED3);
|
|
2367
2616
|
return Object.freeze({ grantedAt, leaseEndsAt });
|
|
2368
2617
|
}
|
|
2369
|
-
function readClaimed(value, scope, expected) {
|
|
2618
|
+
function readClaimed(value, scope, expected, capacity) {
|
|
2370
2619
|
const raw = readRecord(value, ["job", "snapshot", "fence", "lease", "ordinal", "manual"], [], STORED3);
|
|
2371
|
-
const job = readJobSummary(scope, raw.job);
|
|
2372
|
-
const { snapshot } = validateSnapshot(raw.snapshot);
|
|
2620
|
+
const job = readJobSummary(scope, raw.job, capacity);
|
|
2621
|
+
const { snapshot } = validateSnapshot(raw.snapshot, capacity);
|
|
2373
2622
|
const fence = readFence(raw.fence);
|
|
2374
2623
|
const lease = readGrant(readRecord(raw.lease, ["grantedAt", "leaseEndsAt"], [], STORED3), expected.leaseMs);
|
|
2375
2624
|
const ordinal = integer(raw.ordinal, 1, MAX_ATTEMPT_LEDGER, STORED3);
|
|
@@ -2404,7 +2653,7 @@ function readHandoff(value, last, claim, leaseMs, times) {
|
|
|
2404
2653
|
assertLaterGrant(grant, last, claim.job.expiresAt, times);
|
|
2405
2654
|
return Object.freeze({ ...grant, handoffAt });
|
|
2406
2655
|
}
|
|
2407
|
-
function readCommit(value) {
|
|
2656
|
+
function readCommit(value, capacity) {
|
|
2408
2657
|
const raw = readRecord(
|
|
2409
2658
|
value,
|
|
2410
2659
|
["jobId", "attemptId", "ordinal", "operationId", "startedAt", "finishedAt", "result", "disposition", "reason"],
|
|
@@ -2415,7 +2664,7 @@ function readCommit(value) {
|
|
|
2415
2664
|
const finishedAt = unixMs(raw.finishedAt, STORED3);
|
|
2416
2665
|
const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, STORED3) : void 0;
|
|
2417
2666
|
const retryAt = Object.hasOwn(raw, "retryAt") ? unixMs(raw.retryAt, STORED3) : void 0;
|
|
2418
|
-
const result = readAttemptResult(raw.result, STORED3);
|
|
2667
|
+
const result = readAttemptResult(raw.result, STORED3, false, capacity);
|
|
2419
2668
|
const disposition = readDisposition(raw.disposition, STORED3);
|
|
2420
2669
|
const reason = raw.reason === "attempts_exhausted" ? raw.reason : readAttemptReason(raw.reason, STORED3);
|
|
2421
2670
|
assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", STORED3);
|
|
@@ -2438,23 +2687,23 @@ function readCommit(value) {
|
|
|
2438
2687
|
...retryAt === void 0 ? {} : { retryAt }
|
|
2439
2688
|
});
|
|
2440
2689
|
}
|
|
2441
|
-
function readRecovery(value) {
|
|
2442
|
-
const commit = readCommit(value);
|
|
2690
|
+
function readRecovery(value, capacity) {
|
|
2691
|
+
const commit = readCommit(value, capacity);
|
|
2443
2692
|
const { result, startedAt, handedOffAt, finishedAt } = commit;
|
|
2444
2693
|
if (result.reason !== "pre_handoff_lease_expired" && result.reason !== "post_handoff_lease_expired" || finishedAt - (handedOffAt ?? startedAt) < WORKER_LIMIT_BOUNDS.leaseMs.min) {
|
|
2445
2694
|
invalid(STORED3);
|
|
2446
2695
|
}
|
|
2447
2696
|
return commit;
|
|
2448
2697
|
}
|
|
2449
|
-
function readQueuedExpiry(value, scope) {
|
|
2450
|
-
const job = readJobSummary(scope, value);
|
|
2698
|
+
function readQueuedExpiry(value, scope, capacity) {
|
|
2699
|
+
const job = readJobSummary(scope, value, capacity);
|
|
2451
2700
|
if (job.state !== "terminal" || job.disposition !== "expired" || job.terminalReason !== "expired" || job.terminalAt < job.expiresAt || job.updatedAt !== job.terminalAt) {
|
|
2452
2701
|
invalid(STORED3);
|
|
2453
2702
|
}
|
|
2454
2703
|
return job;
|
|
2455
2704
|
}
|
|
2456
|
-
function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvoked, direct) {
|
|
2457
|
-
const commit = readCommit(value);
|
|
2705
|
+
function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvoked, direct, capacity) {
|
|
2706
|
+
const commit = readCommit(value, capacity);
|
|
2458
2707
|
const { attemptId, startedAt, handedOffAt, finishedAt, result, disposition, retryAt } = commit;
|
|
2459
2708
|
const { job, snapshot, fence, ordinal, manual } = claim;
|
|
2460
2709
|
const { expiresAt } = job;
|
|
@@ -2467,7 +2716,7 @@ function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvo
|
|
|
2467
2716
|
assertEvidenceContext(result, snapshot.provider, attemptId, recipientCounts(snapshot.envelope), STORED3);
|
|
2468
2717
|
if (request && commit.operationId === request.operationId) {
|
|
2469
2718
|
const phase = handedOffAt === void 0 ? "pre_handoff" : "handed_off";
|
|
2470
|
-
if (phase !== request.phase || finishedAt >= last.leaseEndsAt ||
|
|
2719
|
+
if (phase !== request.phase || finishedAt >= last.leaseEndsAt || encodeEvidence(result, STORED3, capacity) !== encodeEvidence(request.result, STORED3, capacity)) {
|
|
2471
2720
|
invalid(STORED3);
|
|
2472
2721
|
}
|
|
2473
2722
|
} else if (direct) {
|
|
@@ -2479,7 +2728,7 @@ function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvo
|
|
|
2479
2728
|
// src/internal/worker-finish.ts
|
|
2480
2729
|
var import_node_crypto3 = require("crypto");
|
|
2481
2730
|
async function finishAttempt(run, evidence2, providerInvoked, result) {
|
|
2482
|
-
const { clock, store, scope, limits } = run.config;
|
|
2731
|
+
const { clock, store, scope, limits: limits2, capacity } = run.config;
|
|
2483
2732
|
const {
|
|
2484
2733
|
claim: { fence },
|
|
2485
2734
|
handoff,
|
|
@@ -2488,13 +2737,13 @@ async function finishAttempt(run, evidence2, providerInvoked, result) {
|
|
|
2488
2737
|
const now = monotonicNow(clock);
|
|
2489
2738
|
const request = result && handoff !== "unconfirmed" ? Object.freeze({ fence, phase: handoff ? "handed_off" : "pre_handoff", operationId: (0, import_node_crypto3.randomUUID)(), result }) : void 0;
|
|
2490
2739
|
const leaseEnd = safeLeaseEnd(handoff && handoff !== "unconfirmed" ? handoff : lease);
|
|
2491
|
-
const grace = createBudgetUntil(clock, Math.min(now, run.budget.deadline) +
|
|
2740
|
+
const grace = createBudgetUntil(clock, Math.min(now, run.budget.deadline) + limits2.finalizeGraceMs);
|
|
2492
2741
|
async function confirm() {
|
|
2493
2742
|
try {
|
|
2494
2743
|
return await run.callStore(grace, {
|
|
2495
2744
|
effect: "read",
|
|
2496
2745
|
invoke: (call) => store.findAttempt(scope, fence.jobId, fence.attemptId, call),
|
|
2497
|
-
accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, request, providerInvoked, false)
|
|
2746
|
+
accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, request, providerInvoked, false, capacity)
|
|
2498
2747
|
});
|
|
2499
2748
|
} catch {
|
|
2500
2749
|
return null;
|
|
@@ -2505,7 +2754,7 @@ async function finishAttempt(run, evidence2, providerInvoked, result) {
|
|
|
2505
2754
|
effect: "mutation",
|
|
2506
2755
|
deadline: leaseEnd,
|
|
2507
2756
|
invoke: (call) => store.finish(scope, exact, call),
|
|
2508
|
-
accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, exact, providerInvoked, true)
|
|
2757
|
+
accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, exact, providerInvoked, true, capacity)
|
|
2509
2758
|
});
|
|
2510
2759
|
}
|
|
2511
2760
|
try {
|
|
@@ -2682,8 +2931,8 @@ var AttemptOwner = class {
|
|
|
2682
2931
|
}
|
|
2683
2932
|
}
|
|
2684
2933
|
async renewWhile(budget, stopped) {
|
|
2685
|
-
const { clock, limits } = this.run.config;
|
|
2686
|
-
const cadence = Math.floor(
|
|
2934
|
+
const { clock, limits: limits2 } = this.run.config;
|
|
2935
|
+
const cadence = Math.floor(limits2.leaseMs / 3);
|
|
2687
2936
|
const waitSignal = AbortSignal.any([stopped, this.run.noLaunch]);
|
|
2688
2937
|
while (!waitSignal.aborted) {
|
|
2689
2938
|
try {
|
|
@@ -2753,7 +3002,7 @@ var AttemptOwner = class {
|
|
|
2753
3002
|
}
|
|
2754
3003
|
};
|
|
2755
3004
|
async function launch(run, owner, mark, submit, preflight) {
|
|
2756
|
-
const { clock, limits } = run.config;
|
|
3005
|
+
const { clock, limits: limits2, capacity } = run.config;
|
|
2757
3006
|
const { provider, envelope, expiresAt } = owner.claim.snapshot;
|
|
2758
3007
|
const { attemptId } = owner.claim.fence;
|
|
2759
3008
|
const launchEnd = Math.min(
|
|
@@ -2761,7 +3010,7 @@ async function launch(run, owner, mark, submit, preflight) {
|
|
|
2761
3010
|
mark.dispatchedAt + (expiresAt - mark.grant.handoffAt) - CLOCK_MARGIN_MS
|
|
2762
3011
|
);
|
|
2763
3012
|
const stage = provider.kind === "smtp" ? "data" : "http";
|
|
2764
|
-
const unknown = (reason) => Object.freeze(
|
|
3013
|
+
const unknown = (reason) => Object.freeze(uncertainProviderResult(stage, reason));
|
|
2765
3014
|
let io;
|
|
2766
3015
|
try {
|
|
2767
3016
|
run.checkLaunch();
|
|
@@ -2771,7 +3020,7 @@ async function launch(run, owner, mark, submit, preflight) {
|
|
|
2771
3020
|
io = createChildBudgetUntil(
|
|
2772
3021
|
clock,
|
|
2773
3022
|
run.budget,
|
|
2774
|
-
Math.min(now +
|
|
3023
|
+
Math.min(now + limits2.providerTimeoutMs, safeLeaseEnd(mark) - limits2.finalizeGraceMs)
|
|
2775
3024
|
);
|
|
2776
3025
|
run.checkLaunch();
|
|
2777
3026
|
io.check();
|
|
@@ -2787,7 +3036,7 @@ async function launch(run, owner, mark, submit, preflight) {
|
|
|
2787
3036
|
if (!import_node_util8.types.isPromise(returned)) return unknown("transport_uncertain");
|
|
2788
3037
|
let result;
|
|
2789
3038
|
try {
|
|
2790
|
-
result = readProviderResult(raw, provider, attemptId, envelope);
|
|
3039
|
+
result = readProviderResult(raw, provider, attemptId, envelope, capacity);
|
|
2791
3040
|
} catch {
|
|
2792
3041
|
return unknown("transport_uncertain");
|
|
2793
3042
|
}
|
|
@@ -2811,8 +3060,8 @@ async function launch(run, owner, mark, submit, preflight) {
|
|
|
2811
3060
|
}
|
|
2812
3061
|
async function processOwned(owner, slot) {
|
|
2813
3062
|
const { run, claim, lease } = owner;
|
|
2814
|
-
const { clock, limits } = run.config;
|
|
2815
|
-
const deadline = lease.dispatchedAt +
|
|
3063
|
+
const { clock, limits: limits2 } = run.config;
|
|
3064
|
+
const deadline = lease.dispatchedAt + limits2.preHandoffTimeoutMs;
|
|
2816
3065
|
let budget;
|
|
2817
3066
|
try {
|
|
2818
3067
|
budget = createChildBudgetUntil(clock, run.budget, deadline);
|
|
@@ -2891,11 +3140,16 @@ var WorkerRun = class {
|
|
|
2891
3140
|
if (this.noLaunch.aborted) invalid("aborted");
|
|
2892
3141
|
}
|
|
2893
3142
|
callStore(budget, operation) {
|
|
2894
|
-
const { clock, limits } = this.config;
|
|
3143
|
+
const { clock, limits: limits2, capacity } = this.config;
|
|
2895
3144
|
let invoked = false;
|
|
2896
3145
|
return runStoreCall(clock, budget, {
|
|
2897
3146
|
...operation,
|
|
2898
|
-
|
|
3147
|
+
capacity,
|
|
3148
|
+
maxMs: Math.min(
|
|
3149
|
+
operation.maxMs ?? limits2.storeTimeoutMs,
|
|
3150
|
+
limits2.storeTimeoutMs,
|
|
3151
|
+
capacity ? STORE_PRIMITIVE_TIMEOUT_MS : Infinity
|
|
3152
|
+
),
|
|
2899
3153
|
beforeInvoke: () => {
|
|
2900
3154
|
if (operation.authorize) this.checkLaunch();
|
|
2901
3155
|
operation.beforeInvoke?.();
|
|
@@ -2973,25 +3227,29 @@ async function sweep(run, limit, items, invoke, read, entry) {
|
|
|
2973
3227
|
return false;
|
|
2974
3228
|
}
|
|
2975
3229
|
function claimNext(run) {
|
|
2976
|
-
const { clock, scope, store, limits } = run.config;
|
|
3230
|
+
const { clock, scope, store, limits: limits2, capacity } = run.config;
|
|
2977
3231
|
const request = Object.freeze({
|
|
2978
3232
|
attemptId: (0, import_node_crypto4.randomUUID)(),
|
|
2979
3233
|
token: (0, import_node_crypto4.randomBytes)(16).toString("base64url"),
|
|
2980
|
-
leaseMs:
|
|
3234
|
+
leaseMs: limits2.leaseMs
|
|
2981
3235
|
});
|
|
2982
3236
|
let dispatchedAt;
|
|
2983
3237
|
return run.callStore(run.budget, {
|
|
2984
3238
|
effect: "mutation",
|
|
2985
3239
|
authorize: true,
|
|
2986
|
-
maxMs:
|
|
3240
|
+
maxMs: limits2.preHandoffTimeoutMs,
|
|
2987
3241
|
invoke(call, start) {
|
|
2988
3242
|
dispatchedAt = start;
|
|
2989
3243
|
return store.claimNext(scope, request, call);
|
|
2990
3244
|
},
|
|
2991
|
-
accept: (value) => readStoreNext(
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
3245
|
+
accept: (value) => readStoreNext(
|
|
3246
|
+
value,
|
|
3247
|
+
(raw) => {
|
|
3248
|
+
const claim = readClaimed(raw, scope, request, capacity);
|
|
3249
|
+
return { claim, dispatchedAt, confirmedAt: monotonicNow(clock) };
|
|
3250
|
+
},
|
|
3251
|
+
"claim"
|
|
3252
|
+
)
|
|
2995
3253
|
});
|
|
2996
3254
|
}
|
|
2997
3255
|
async function drainClaims(run, options, items, slots) {
|
|
@@ -3027,7 +3285,7 @@ async function drainClaims(run, options, items, slots) {
|
|
|
3027
3285
|
}
|
|
3028
3286
|
if (next.status !== "applied") {
|
|
3029
3287
|
slot.close();
|
|
3030
|
-
stop = next.status === "
|
|
3288
|
+
stop = next.status === "none" ? "idle" : next.status;
|
|
3031
3289
|
break;
|
|
3032
3290
|
}
|
|
3033
3291
|
const { claim } = next.value;
|
|
@@ -3067,13 +3325,13 @@ async function drainClaims(run, options, items, slots) {
|
|
|
3067
3325
|
}
|
|
3068
3326
|
async function runQueue(run, options, slots, shutdown) {
|
|
3069
3327
|
const items = [];
|
|
3070
|
-
const { scope, store } = run.config;
|
|
3328
|
+
const { scope, store, capacity } = run.config;
|
|
3071
3329
|
const contended = await sweep(
|
|
3072
3330
|
run,
|
|
3073
3331
|
options.maintenanceLimit,
|
|
3074
3332
|
items,
|
|
3075
3333
|
(call) => store.recoverNext(scope, call),
|
|
3076
|
-
readRecovery,
|
|
3334
|
+
(value) => readRecovery(value, capacity),
|
|
3077
3335
|
(commit) => ({
|
|
3078
3336
|
operation: "recover",
|
|
3079
3337
|
jobId: commit.jobId,
|
|
@@ -3087,7 +3345,7 @@ async function runQueue(run, options, slots, shutdown) {
|
|
|
3087
3345
|
options.maintenanceLimit,
|
|
3088
3346
|
items,
|
|
3089
3347
|
(call) => store.expireNext(scope, call),
|
|
3090
|
-
(value) => readQueuedExpiry(value, scope),
|
|
3348
|
+
(value) => readQueuedExpiry(value, scope, capacity),
|
|
3091
3349
|
(job) => ({
|
|
3092
3350
|
operation: "expire",
|
|
3093
3351
|
jobId: job.jobId,
|
|
@@ -3104,7 +3362,8 @@ async function runQueue(run, options, slots, shutdown) {
|
|
|
3104
3362
|
else if (allowedErrorCode(run.budget.signal.reason, ["deadline"])) stop = "deadline";
|
|
3105
3363
|
else if (run.persistenceFailures) stop = "store_error";
|
|
3106
3364
|
else if (run.budget.signal.aborted) return invalid("invalid_config");
|
|
3107
|
-
else if (queueStop
|
|
3365
|
+
else if (queueStop === "contended" || queueStop === "expiry_maintenance_required") stop = queueStop;
|
|
3366
|
+
else if (slots.size === run.config.limits.maxConcurrency) stop = "callback_capacity";
|
|
3108
3367
|
else if (queueStop === "idle" && timeLeft < 1)
|
|
3109
3368
|
stop = "deadline";
|
|
3110
3369
|
else stop = queueStop;
|
|
@@ -3112,16 +3371,17 @@ async function runQueue(run, options, slots, shutdown) {
|
|
|
3112
3371
|
}
|
|
3113
3372
|
function createEmailWorker(options) {
|
|
3114
3373
|
const config = readWorkerConfig(options);
|
|
3115
|
-
const { clock, scope, limits } = config;
|
|
3374
|
+
const { clock, scope, limits: limits2, capacity } = config;
|
|
3116
3375
|
const shutdown = new AbortController();
|
|
3117
3376
|
const slots = /* @__PURE__ */ new Set();
|
|
3118
3377
|
let active;
|
|
3119
3378
|
const worker = {
|
|
3120
3379
|
scope,
|
|
3380
|
+
...capacityTag(capacity),
|
|
3121
3381
|
async run(options2 = {}) {
|
|
3122
3382
|
if (shutdown.signal.aborted) invalid("closed");
|
|
3123
3383
|
if (active) invalid("busy");
|
|
3124
|
-
const settings = readRunOptions(options2,
|
|
3384
|
+
const settings = readRunOptions(options2, limits2, capacity);
|
|
3125
3385
|
const done = new AbortController();
|
|
3126
3386
|
active = done;
|
|
3127
3387
|
try {
|
|
@@ -3188,7 +3448,7 @@ var ADMISSION_ERRORS = [
|
|
|
3188
3448
|
function boundScope(port) {
|
|
3189
3449
|
return scopeToken(readPortMember(port, "scope"), "invalid_config");
|
|
3190
3450
|
}
|
|
3191
|
-
function readBatch(scope, input) {
|
|
3451
|
+
function readBatch(scope, input, capacity) {
|
|
3192
3452
|
const code = "invalid_command";
|
|
3193
3453
|
const raw = readRecord(input, ["batchId", "items"], [], code);
|
|
3194
3454
|
const batchId = identifier(raw.batchId, code);
|
|
@@ -3199,7 +3459,10 @@ function readBatch(scope, input) {
|
|
|
3199
3459
|
if (seen.has(itemId)) invalid(code);
|
|
3200
3460
|
seen.add(itemId);
|
|
3201
3461
|
const command = readRecord(item.command, ["templateKey", "input", "provider", "intent"], ["scheduleAt"], code);
|
|
3202
|
-
return {
|
|
3462
|
+
return {
|
|
3463
|
+
itemId,
|
|
3464
|
+
parsed: parseCommandIdentity(scope, { ...command, key: { kind: "batch", batchId, itemId } }, capacity)
|
|
3465
|
+
};
|
|
3203
3466
|
});
|
|
3204
3467
|
if (!items.length) invalid(code);
|
|
3205
3468
|
return { batchId, items };
|
|
@@ -3208,25 +3471,49 @@ function stopReason(budget) {
|
|
|
3208
3471
|
if (budget.remainingMs() > 0) return;
|
|
3209
3472
|
return allowedErrorCode(budget.signal.reason, STOP_ERRORS2) ?? invalid("invalid_config");
|
|
3210
3473
|
}
|
|
3211
|
-
function readAdmissionResult(input, identity) {
|
|
3474
|
+
function readAdmissionResult(input, identity, capacity) {
|
|
3212
3475
|
const code = "write_unconfirmed";
|
|
3213
3476
|
const { status, job: raw } = readRecord(input, ["status", "job"], [], code);
|
|
3214
3477
|
if (status !== "enqueued" && status !== "replayed") invalid(code);
|
|
3215
|
-
const job = readJobSummary(identity.scope, raw);
|
|
3478
|
+
const job = readJobSummary(identity.scope, raw, capacity);
|
|
3216
3479
|
if (!matchesAdmissionJob(job, identity, status)) invalid(code);
|
|
3217
3480
|
return { status, job };
|
|
3218
3481
|
}
|
|
3219
|
-
function
|
|
3220
|
-
return
|
|
3482
|
+
function createEmailBatchAdmission(options) {
|
|
3483
|
+
return createEmailBatchAdmissionWithClock(options, systemEmailClock);
|
|
3221
3484
|
}
|
|
3222
|
-
function
|
|
3223
|
-
const
|
|
3485
|
+
function createEmailBatchAdmissionWithClock(options, clock) {
|
|
3486
|
+
const raw = readRecord(options, ["service"], ["smtpCapacity"], "invalid_config");
|
|
3487
|
+
const { service } = raw;
|
|
3488
|
+
const capacity = readCapacityOption(raw);
|
|
3489
|
+
assertPortCapacity(service, capacity);
|
|
3490
|
+
return createBatchAdmission(service, boundScope(service), capacity, clock);
|
|
3491
|
+
}
|
|
3492
|
+
function createEmailBatchService(options) {
|
|
3493
|
+
const raw = readRecord(options, ["service", "worker"], ["smtpCapacity"], "invalid_config");
|
|
3494
|
+
const { service, worker } = raw;
|
|
3495
|
+
const capacity = readCapacityOption(raw);
|
|
3496
|
+
assertPortCapacity(service, capacity);
|
|
3497
|
+
assertPortCapacity(worker, capacity);
|
|
3224
3498
|
const scope = boundScope(service);
|
|
3225
3499
|
if (scope !== boundScope(worker)) invalid("invalid_config");
|
|
3226
3500
|
const run = bindMethod(worker, "run");
|
|
3501
|
+
const { enqueueBatch } = createBatchAdmission(service, scope, capacity, systemEmailClock);
|
|
3502
|
+
const batch = {
|
|
3503
|
+
enqueueBatch,
|
|
3504
|
+
async run(options2) {
|
|
3505
|
+
if (boundScope(worker) !== scope) invalid("invalid_config");
|
|
3506
|
+
assertPortCapacity(worker, capacity);
|
|
3507
|
+
return run(options2);
|
|
3508
|
+
}
|
|
3509
|
+
};
|
|
3510
|
+
return Object.freeze(batch);
|
|
3511
|
+
}
|
|
3512
|
+
function createBatchAdmission(service, scope, capacity, clock) {
|
|
3227
3513
|
const enqueue = bindMethod(service, "enqueue");
|
|
3228
3514
|
const admit = findServiceAdmission(service) ?? (async ({ command, identity }, budget) => {
|
|
3229
3515
|
if (boundScope(service) !== scope) invalid("invalid_config");
|
|
3516
|
+
assertPortCapacity(service, capacity);
|
|
3230
3517
|
const bounds = CALL_BUDGETS.admissionOrAdmin;
|
|
3231
3518
|
const timeoutMs = Math.max(bounds.min, Math.min(bounds.max, Math.floor(budget.remainingMs())));
|
|
3232
3519
|
budget.check();
|
|
@@ -3241,17 +3528,20 @@ function createEmailBatchServiceWithClock(options, clock) {
|
|
|
3241
3528
|
throw error;
|
|
3242
3529
|
}
|
|
3243
3530
|
try {
|
|
3244
|
-
return readAdmissionResult(raw, identity);
|
|
3531
|
+
return readAdmissionResult(raw, identity, capacity);
|
|
3245
3532
|
} catch {
|
|
3246
3533
|
return invalid("write_unconfirmed");
|
|
3247
3534
|
}
|
|
3248
3535
|
});
|
|
3536
|
+
let admitting = false;
|
|
3249
3537
|
const batch = {
|
|
3250
3538
|
async enqueueBatch(input, call = {}) {
|
|
3539
|
+
if (capacity && admitting) invalid("busy");
|
|
3251
3540
|
const { signal, timeoutMs } = readCallOptions(call, CALL_BUDGETS.batchOrRun);
|
|
3252
3541
|
const budget = createBudget(clock, timeoutMs, signal);
|
|
3542
|
+
admitting = true;
|
|
3253
3543
|
try {
|
|
3254
|
-
const { batchId, items: commands } = readBatch(scope, input);
|
|
3544
|
+
const { batchId, items: commands } = readBatch(scope, input, capacity);
|
|
3255
3545
|
const items = [];
|
|
3256
3546
|
const counts = { enqueued: 0, replayed: 0, failed: 0, notStarted: 0, persistenceFailures: 0 };
|
|
3257
3547
|
for (const { itemId, parsed } of commands) {
|
|
@@ -3273,12 +3563,9 @@ function createEmailBatchServiceWithClock(options, clock) {
|
|
|
3273
3563
|
}
|
|
3274
3564
|
return Object.freeze({ batchId, items: Object.freeze(items), counts: Object.freeze(counts) });
|
|
3275
3565
|
} finally {
|
|
3566
|
+
admitting = false;
|
|
3276
3567
|
budget.close();
|
|
3277
3568
|
}
|
|
3278
|
-
},
|
|
3279
|
-
async run(options2) {
|
|
3280
|
-
if (boundScope(worker) !== scope) invalid("invalid_config");
|
|
3281
|
-
return run(options2);
|
|
3282
3569
|
}
|
|
3283
3570
|
};
|
|
3284
3571
|
return Object.freeze(batch);
|
|
@@ -3298,7 +3585,10 @@ function createEmailAdmin(options) {
|
|
|
3298
3585
|
return createEmailAdminWithClock(options, systemEmailClock);
|
|
3299
3586
|
}
|
|
3300
3587
|
function createEmailAdminWithClock(options, clock) {
|
|
3301
|
-
const
|
|
3588
|
+
const raw = readRecord(options, ["scope", "store"], ["onActivity", "smtpCapacity"], CONFIG3);
|
|
3589
|
+
const { scope: input, store, onActivity: callback } = raw;
|
|
3590
|
+
const capacity = readCapacityOption(raw);
|
|
3591
|
+
assertPortCapacity(store, capacity);
|
|
3302
3592
|
const scope = scopeToken(input, CONFIG3);
|
|
3303
3593
|
const findJob = bindMethod(store, "findJob");
|
|
3304
3594
|
const cancel = bindMethod(store, "cancel");
|
|
@@ -3310,6 +3600,7 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3310
3600
|
const cleanupNext = bindMethod(store, "cleanupNext");
|
|
3311
3601
|
const onActivity = callback === void 0 ? void 0 : requireCallback(callback);
|
|
3312
3602
|
let observers = 0;
|
|
3603
|
+
let active = false;
|
|
3313
3604
|
function observe(activity, counts) {
|
|
3314
3605
|
if (!onActivity) return;
|
|
3315
3606
|
if (observers >= MAX_OBSERVER_PENDING) {
|
|
@@ -3328,29 +3619,35 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3328
3619
|
}
|
|
3329
3620
|
}
|
|
3330
3621
|
function readJob(jobId, value) {
|
|
3331
|
-
const job = readJobSummary(scope, value);
|
|
3622
|
+
const job = readJobSummary(scope, value, capacity);
|
|
3332
3623
|
if (job.jobId !== jobId) invalid(STORED4);
|
|
3333
3624
|
return job;
|
|
3334
3625
|
}
|
|
3335
3626
|
function callStore(budget, operation) {
|
|
3336
|
-
return runStoreCall(clock, budget, { ...operation, maxMs: STORE_PRIMITIVE_TIMEOUT_MS });
|
|
3627
|
+
return runStoreCall(clock, budget, { ...operation, maxMs: STORE_PRIMITIVE_TIMEOUT_MS, capacity });
|
|
3337
3628
|
}
|
|
3338
3629
|
function write(budget, invoke, accept) {
|
|
3339
3630
|
return callStore(budget, { effect: "mutation", invoke, accept });
|
|
3340
3631
|
}
|
|
3341
3632
|
async function inOperation(options2, run) {
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
const budget = createBudget(clock, timeoutMs, signal);
|
|
3633
|
+
if (capacity && active) invalid("busy");
|
|
3634
|
+
active = true;
|
|
3345
3635
|
try {
|
|
3346
|
-
|
|
3636
|
+
const { timeoutMs, signal: source } = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
|
|
3637
|
+
const signal = source && AbortSignal.any([source]);
|
|
3638
|
+
const budget = createBudget(clock, timeoutMs, signal);
|
|
3639
|
+
try {
|
|
3640
|
+
return await run(budget, signal);
|
|
3641
|
+
} finally {
|
|
3642
|
+
budget.close();
|
|
3643
|
+
}
|
|
3347
3644
|
} finally {
|
|
3348
|
-
|
|
3645
|
+
active = false;
|
|
3349
3646
|
}
|
|
3350
3647
|
}
|
|
3351
3648
|
async function maintain(options2, step) {
|
|
3352
3649
|
const { limit: input2, ...call } = readRecord(options2, [], ["limit", "signal", "timeoutMs"], CONFIG3);
|
|
3353
|
-
const limit = boundedOption(input2, ADMIN_MAINTENANCE_LIMIT);
|
|
3650
|
+
const limit = boundedOption(input2, capacity?.limits.adminMaintenance ?? ADMIN_MAINTENANCE_LIMIT);
|
|
3354
3651
|
return inOperation(call, async (budget, signal) => {
|
|
3355
3652
|
let stop = "limit";
|
|
3356
3653
|
for (let i = 0; i < limit; i++) {
|
|
@@ -3393,11 +3690,11 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3393
3690
|
budget,
|
|
3394
3691
|
(call) => cancel(scope, id, call),
|
|
3395
3692
|
(value) => {
|
|
3396
|
-
const
|
|
3397
|
-
const { status } =
|
|
3398
|
-
if (status === "not_found" && !Object.hasOwn(
|
|
3693
|
+
const raw2 = readRecord(value, ["status"], ["job"], STORED4);
|
|
3694
|
+
const { status } = raw2;
|
|
3695
|
+
if (status === "not_found" && !Object.hasOwn(raw2, "job")) return Object.freeze({ status });
|
|
3399
3696
|
if (status !== "cancelled" && status !== "too_late") invalid(STORED4);
|
|
3400
|
-
const job = readJob(id,
|
|
3697
|
+
const job = readJob(id, raw2.job);
|
|
3401
3698
|
if (status === "cancelled" !== (job.state === "terminal" && job.disposition === "cancelled")) {
|
|
3402
3699
|
invalid(STORED4);
|
|
3403
3700
|
}
|
|
@@ -3414,8 +3711,8 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3414
3711
|
definite: RESOLUTION_REJECTIONS,
|
|
3415
3712
|
invoke: (call) => resolveUnknown(scope, request, call),
|
|
3416
3713
|
accept(value) {
|
|
3417
|
-
const { status, job:
|
|
3418
|
-
const job = readJob(request.jobId,
|
|
3714
|
+
const { status, job: raw2 } = readRecord(value, ["status", "job"], [], STORED4);
|
|
3715
|
+
const job = readJob(request.jobId, raw2);
|
|
3419
3716
|
const decision = job.resolutions.find(({ attemptId }) => attemptId === request.attemptId);
|
|
3420
3717
|
if (decision?.action !== request.action || status !== "resolved" && status !== "replayed") {
|
|
3421
3718
|
invalid(STORED4);
|
|
@@ -3430,7 +3727,7 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3430
3727
|
},
|
|
3431
3728
|
listActivities(query = {}, options2 = {}) {
|
|
3432
3729
|
return inOperation(options2, (budget) => {
|
|
3433
|
-
const criteria = readActivityCriteria(scope, query);
|
|
3730
|
+
const criteria = readActivityCriteria(scope, query, capacity);
|
|
3434
3731
|
const request = activityQuery(criteria);
|
|
3435
3732
|
return callStore(budget, {
|
|
3436
3733
|
effect: "read",
|
|
@@ -3445,7 +3742,7 @@ function createEmailAdminWithClock(options, clock) {
|
|
|
3445
3742
|
const next = await write(
|
|
3446
3743
|
budget,
|
|
3447
3744
|
(call) => reserve(scope, call),
|
|
3448
|
-
(value) => readStoreNext(value, (permit2) => readProjectionPermit(scope, permit2))
|
|
3745
|
+
(value) => readStoreNext(value, (permit2) => readProjectionPermit(scope, permit2, capacity))
|
|
3449
3746
|
);
|
|
3450
3747
|
if (next.status !== "applied") return next.status === "none" ? "idle" : "contended";
|
|
3451
3748
|
counts.reserved++;
|
|
@@ -3560,6 +3857,7 @@ function jobSummary(job) {
|
|
|
3560
3857
|
const latest = job.attempts.at(-1)?.commit;
|
|
3561
3858
|
return Object.freeze({
|
|
3562
3859
|
version: EMAIL_FORMAT_VERSION,
|
|
3860
|
+
...job.smtpCapacity ? { smtpCapacity: job.smtpCapacity } : {},
|
|
3563
3861
|
scope: job.scope,
|
|
3564
3862
|
jobId: job.jobId,
|
|
3565
3863
|
key: Object.freeze({ ...job.key }),
|
|
@@ -3584,13 +3882,13 @@ function jobSummary(job) {
|
|
|
3584
3882
|
...summaryState
|
|
3585
3883
|
});
|
|
3586
3884
|
}
|
|
3587
|
-
function copyCommit(commit) {
|
|
3588
|
-
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED5) });
|
|
3885
|
+
function copyCommit(commit, capacity) {
|
|
3886
|
+
return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED5, false, capacity) });
|
|
3589
3887
|
}
|
|
3590
|
-
function readStoredSnapshot(job, input) {
|
|
3888
|
+
function readStoredSnapshot(job, input, capacity) {
|
|
3591
3889
|
let validated;
|
|
3592
3890
|
try {
|
|
3593
|
-
validated = validateSnapshot(input);
|
|
3891
|
+
validated = validateSnapshot(input, capacity);
|
|
3594
3892
|
} catch {
|
|
3595
3893
|
return invalid(STORED5);
|
|
3596
3894
|
}
|
|
@@ -3625,15 +3923,18 @@ function replace(job, time, patch, value, before, pendingAck) {
|
|
|
3625
3923
|
write: { kind: "replace", guard: pendingAck ? { ...expect, pendingAck } : expect, next }
|
|
3626
3924
|
};
|
|
3627
3925
|
}
|
|
3628
|
-
function newJob(admission, time) {
|
|
3926
|
+
function newJob(admission, time, capacity) {
|
|
3629
3927
|
const { command, jobId, retry, retention, admissionKey } = admission;
|
|
3630
3928
|
const { scope, key, scheduleAt, fingerprint, snapshotHash, snapshot } = command;
|
|
3631
3929
|
const now = unixMs(time.now, "store_unavailable");
|
|
3632
3930
|
if (snapshot.preparedAt - now > MAX_PREPARED_AT_SKEW_MS) invalid("invalid_snapshot");
|
|
3633
3931
|
const nextAt = Math.max(now, scheduleAt ?? now);
|
|
3634
3932
|
if (nextAt >= snapshot.expiresAt) invalid("expired");
|
|
3933
|
+
if (snapshot.kind === "smtp") assertSmtpAttachmentTypes(snapshot.attachments);
|
|
3635
3934
|
return Object.freeze({
|
|
3636
3935
|
version: EMAIL_FORMAT_VERSION,
|
|
3936
|
+
...capacityTag(capacity),
|
|
3937
|
+
...capacity ? { intentHash: command.intentHash } : {},
|
|
3637
3938
|
scope,
|
|
3638
3939
|
jobId,
|
|
3639
3940
|
key,
|
|
@@ -3672,14 +3973,18 @@ function liveLease(job, fence, phase, now) {
|
|
|
3672
3973
|
}
|
|
3673
3974
|
return lease;
|
|
3674
3975
|
}
|
|
3675
|
-
function
|
|
3976
|
+
function claimEligibility(job, now) {
|
|
3977
|
+
if (job.state.state !== "queued" || job.state.nextAt > now) return "not_due";
|
|
3978
|
+
return now >= job.expiresAt ? "expired" : "eligible";
|
|
3979
|
+
}
|
|
3980
|
+
function claimJob(job, claim, time, capacity) {
|
|
3676
3981
|
const { now } = time;
|
|
3677
|
-
if (job
|
|
3982
|
+
if (claimEligibility(job, now) !== "eligible") return null;
|
|
3678
3983
|
const manual = hasRedrive(job);
|
|
3679
3984
|
const ordinal = job.attempts.length + 1;
|
|
3680
3985
|
if (ordinal > MAX_ATTEMPT_LEDGER || !manual && ordinal > job.retry.maxAttempts) invalid("store_incompatible");
|
|
3681
3986
|
if (job.attempts.some(({ commit }) => commit.attemptId === claim.attemptId)) invalid("invalid_command");
|
|
3682
|
-
const snapshot = readStoredSnapshot(job, job.snapshot);
|
|
3987
|
+
const snapshot = readStoredSnapshot(job, job.snapshot, capacity);
|
|
3683
3988
|
const lease = Object.freeze({
|
|
3684
3989
|
attemptId: claim.attemptId,
|
|
3685
3990
|
token: claim.token,
|
|
@@ -3702,6 +4007,20 @@ function claimJob(job, claim, time) {
|
|
|
3702
4007
|
Math.min(job.expiresAt, lease.leaseEndsAt)
|
|
3703
4008
|
);
|
|
3704
4009
|
}
|
|
4010
|
+
var EXPIRY_MAINTENANCE_REQUIRED = Object.freeze({ status: "expiry_maintenance_required" });
|
|
4011
|
+
function claimWindow(claim, candidateLimit, capacity) {
|
|
4012
|
+
let expired = 0;
|
|
4013
|
+
return {
|
|
4014
|
+
decide(job, time) {
|
|
4015
|
+
const decision = claimJob(job, claim, time, capacity);
|
|
4016
|
+
if (decision === null && claimEligibility(job, time.now) === "expired") expired++;
|
|
4017
|
+
return decision;
|
|
4018
|
+
},
|
|
4019
|
+
result(next) {
|
|
4020
|
+
return next.status !== "applied" && expired === candidateLimit ? EXPIRY_MAINTENANCE_REQUIRED : next;
|
|
4021
|
+
}
|
|
4022
|
+
};
|
|
4023
|
+
}
|
|
3705
4024
|
function renewJob(job, fence, leaseMs, time) {
|
|
3706
4025
|
const lease = liveLease(job, fence, "pre_handoff", time.now);
|
|
3707
4026
|
if (!lease || time.now >= job.expiresAt) return null;
|
|
@@ -3739,7 +4058,7 @@ function markJob(job, fence, leaseMs, time) {
|
|
|
3739
4058
|
Math.min(job.expiresAt, lease.leaseEndsAt, grant.leaseEndsAt)
|
|
3740
4059
|
);
|
|
3741
4060
|
}
|
|
3742
|
-
function finalize(job, lease, operationId, result, time, owner) {
|
|
4061
|
+
function finalize(job, lease, operationId, result, time, owner, capacity) {
|
|
3743
4062
|
const { now } = time;
|
|
3744
4063
|
const { attemptId, startedAt, handedOffAt } = lease;
|
|
3745
4064
|
const ordinal = job.attempts.length + 1;
|
|
@@ -3757,7 +4076,7 @@ function finalize(job, lease, operationId, result, time, owner) {
|
|
|
3757
4076
|
});
|
|
3758
4077
|
const entry = Object.freeze({
|
|
3759
4078
|
commit,
|
|
3760
|
-
digest: activityDigest(activityFor(job, commit)),
|
|
4079
|
+
digest: activityDigest(activityFor(job, commit, capacity), capacity),
|
|
3761
4080
|
...owner ? { ownerDigest: sha256(lease.token) } : {}
|
|
3762
4081
|
});
|
|
3763
4082
|
const state = effect.disposition === "retry_scheduled" ? Object.freeze({ state: "queued", nextAt: effect.retryAt }) : terminalState(effect.disposition, effect.reason, now);
|
|
@@ -3769,11 +4088,11 @@ function finalize(job, lease, operationId, result, time, owner) {
|
|
|
3769
4088
|
job,
|
|
3770
4089
|
time,
|
|
3771
4090
|
{ ...job, state, attempts: Object.freeze([...job.attempts, entry]) },
|
|
3772
|
-
() => copyCommit(commit),
|
|
4091
|
+
() => copyCommit(commit, capacity),
|
|
3773
4092
|
before
|
|
3774
4093
|
);
|
|
3775
4094
|
}
|
|
3776
|
-
function finishJob(job, request, time) {
|
|
4095
|
+
function finishJob(job, request, time, capacity) {
|
|
3777
4096
|
const { fence, phase, operationId, result } = request;
|
|
3778
4097
|
if (job.jobId !== fence.jobId) return null;
|
|
3779
4098
|
assertEvidencePhase(result.reason, phase, "owner", "invalid_command");
|
|
@@ -3784,28 +4103,28 @@ function finishJob(job, request, time) {
|
|
|
3784
4103
|
if (previous.ownerDigest !== sha256(fence.token) || commit.operationId !== operationId || previousPhase !== phase) {
|
|
3785
4104
|
return null;
|
|
3786
4105
|
}
|
|
3787
|
-
if (
|
|
4106
|
+
if (sha256(encodeEvidence(result, "invalid_command", capacity)) !== sha256(encodeEvidence(commit.result, "store_incompatible", capacity))) {
|
|
3788
4107
|
invalid("invalid_command");
|
|
3789
4108
|
}
|
|
3790
|
-
return { value: copyCommit(commit) };
|
|
4109
|
+
return { value: copyCommit(commit, capacity) };
|
|
3791
4110
|
}
|
|
3792
4111
|
const lease = liveLease(job, fence, phase, time.now);
|
|
3793
4112
|
if (!lease) return null;
|
|
3794
4113
|
if (result.reason === "expired" && time.now < job.expiresAt) invalid("invalid_command");
|
|
3795
4114
|
assertEvidenceContext(result, job.provider, fence.attemptId, job.recipientCounts, "invalid_command");
|
|
3796
|
-
return finalize(job, lease, operationId, result, time, true);
|
|
4115
|
+
return finalize(job, lease, operationId, result, time, true, capacity);
|
|
3797
4116
|
}
|
|
3798
|
-
function recoverJob(job, operationId, time) {
|
|
4117
|
+
function recoverJob(job, operationId, time, capacity) {
|
|
3799
4118
|
if (job.state.state !== "leased" || time.now < job.state.lease.leaseEndsAt) return null;
|
|
3800
4119
|
const { lease } = job.state;
|
|
3801
4120
|
const reason = lease.handedOffAt === void 0 ? "pre_handoff_lease_expired" : "post_handoff_lease_expired";
|
|
3802
|
-
return finalize(job, lease, operationId, lifecycleEvidence(reason), time, false);
|
|
4121
|
+
return finalize(job, lease, operationId, lifecycleEvidence(reason), time, false, capacity);
|
|
3803
4122
|
}
|
|
3804
4123
|
function expireJob(job, time) {
|
|
3805
4124
|
if (job.state.state !== "queued" || time.now < job.expiresAt) return null;
|
|
3806
4125
|
return replace(job, time, { ...job, state: terminalState("expired", "expired", time.now) }, jobSummary);
|
|
3807
4126
|
}
|
|
3808
|
-
function cancelJob(job, operationId, time) {
|
|
4127
|
+
function cancelJob(job, operationId, time, capacity) {
|
|
3809
4128
|
const { state } = job;
|
|
3810
4129
|
if (state.state === "terminal") {
|
|
3811
4130
|
return {
|
|
@@ -3819,7 +4138,7 @@ function cancelJob(job, operationId, time) {
|
|
|
3819
4138
|
return { value: Object.freeze({ status: "too_late", job: jobSummary(job) }) };
|
|
3820
4139
|
}
|
|
3821
4140
|
if (state.state === "leased") {
|
|
3822
|
-
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time, false);
|
|
4141
|
+
const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time, false, capacity);
|
|
3823
4142
|
const { write } = decision;
|
|
3824
4143
|
if (write?.kind !== "replace") invalid("store_incompatible");
|
|
3825
4144
|
return { write, value: Object.freeze({ status: "cancelled", job: jobSummary(write.next) }) };
|
|
@@ -3864,11 +4183,11 @@ function resolveJob(job, request, time) {
|
|
|
3864
4183
|
function replaceAttempt(job, previous, entry) {
|
|
3865
4184
|
return Object.freeze(job.attempts.map((attempt) => attempt === previous ? Object.freeze(entry) : attempt));
|
|
3866
4185
|
}
|
|
3867
|
-
function reserveJobProjection(job, time) {
|
|
4186
|
+
function reserveJobProjection(job, time, capacity) {
|
|
3868
4187
|
const entry = pendingAttempt(job);
|
|
3869
4188
|
if (job.retiring || !entry || entry.commit.finishedAt > time.now) return null;
|
|
3870
4189
|
const permitUntil = timeAfter(time.now, PROJECTION_PERMIT_MS) ?? invalid("store_unavailable");
|
|
3871
|
-
const activity = activityFor(job, entry.commit);
|
|
4190
|
+
const activity = activityFor(job, entry.commit, capacity);
|
|
3872
4191
|
const permit = Object.freeze({ activity, digest: entry.digest, permitUntil });
|
|
3873
4192
|
const attempts = replaceAttempt(job, entry, { ...entry, permitUntil });
|
|
3874
4193
|
return replace(
|
|
@@ -4012,12 +4331,14 @@ var TIME_MISS = /* @__PURE__ */ Symbol("time_miss");
|
|
|
4012
4331
|
var NONE = Object.freeze({ status: "none" });
|
|
4013
4332
|
var CONTENDED = Object.freeze({ status: "contended" });
|
|
4014
4333
|
function createMemoryEmailStore(options = {}) {
|
|
4015
|
-
const config = readRecord(options, [], ["clock", "maxJobs"], "invalid_config");
|
|
4334
|
+
const config = readRecord(options, [], ["clock", "maxJobs", "smtpCapacity"], "invalid_config");
|
|
4335
|
+
const capacity = readCapacityOption(config);
|
|
4016
4336
|
const clock = readClock(config.clock);
|
|
4017
|
-
const maxJobs = boundedOption(config.maxJobs, MEMORY_MAX_JOBS);
|
|
4337
|
+
const maxJobs = boundedOption(config.maxJobs, capacity?.limits.memoryJobs ?? MEMORY_MAX_JOBS);
|
|
4018
4338
|
const scopes = /* @__PURE__ */ new Map();
|
|
4019
4339
|
let count = 0;
|
|
4020
4340
|
let active = false;
|
|
4341
|
+
let ready = false;
|
|
4021
4342
|
let lastWall = 0;
|
|
4022
4343
|
function wallNow() {
|
|
4023
4344
|
let now;
|
|
@@ -4058,8 +4379,13 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4058
4379
|
active = false;
|
|
4059
4380
|
}
|
|
4060
4381
|
}
|
|
4382
|
+
function primitive(call, worker, run) {
|
|
4383
|
+
const safe = readStoreCall(call, worker, capacity);
|
|
4384
|
+
if (capacity && !ready) invalid("store_unavailable");
|
|
4385
|
+
return atomic(safe, run);
|
|
4386
|
+
}
|
|
4061
4387
|
function read(call, worker, get) {
|
|
4062
|
-
return
|
|
4388
|
+
return primitive(call, worker, (check) => {
|
|
4063
4389
|
const value = get();
|
|
4064
4390
|
check();
|
|
4065
4391
|
return value;
|
|
@@ -4124,12 +4450,14 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4124
4450
|
return misses ? CONTENDED : NONE;
|
|
4125
4451
|
}
|
|
4126
4452
|
const store = {
|
|
4453
|
+
...capacityTag(capacity),
|
|
4127
4454
|
async ready(options2 = {}) {
|
|
4128
4455
|
const call = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
|
|
4129
4456
|
atomic({ ...call, timeoutMs: Math.min(call.timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS) }, (check) => {
|
|
4130
4457
|
wallNow();
|
|
4131
4458
|
check();
|
|
4132
4459
|
});
|
|
4460
|
+
ready = true;
|
|
4133
4461
|
},
|
|
4134
4462
|
async findAdmission(scope, key, call) {
|
|
4135
4463
|
return read(call, false, () => {
|
|
@@ -4141,15 +4469,15 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4141
4469
|
});
|
|
4142
4470
|
},
|
|
4143
4471
|
async insert(scope, admission, call) {
|
|
4144
|
-
return
|
|
4145
|
-
const parsed = readStoreAdmission(scope, admission);
|
|
4472
|
+
return primitive(call, false, (check) => {
|
|
4473
|
+
const parsed = readStoreAdmission(scope, admission, capacity);
|
|
4146
4474
|
const rows = scopes.get(parsed.command.scope);
|
|
4147
4475
|
if (rows?.identities.has(parsed.admissionKey)) {
|
|
4148
4476
|
check();
|
|
4149
4477
|
return Object.freeze({ status: "duplicate" });
|
|
4150
4478
|
}
|
|
4151
4479
|
if (rows?.jobs.has(parsed.jobId)) invalid("invalid_command");
|
|
4152
|
-
const row = newJob(parsed, { now: wallNow(), revision: (0, import_node_crypto5.randomUUID)() });
|
|
4480
|
+
const row = newJob(parsed, { now: wallNow(), revision: (0, import_node_crypto5.randomUUID)() }, capacity);
|
|
4153
4481
|
if (count >= maxJobs) invalid("store_capacity");
|
|
4154
4482
|
const result = Object.freeze({ status: "inserted", job: jobSummary(row) });
|
|
4155
4483
|
check();
|
|
@@ -4176,62 +4504,66 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4176
4504
|
const job = readStoreId(jobId);
|
|
4177
4505
|
const id = readStoreId(attemptId);
|
|
4178
4506
|
const entry = rows?.jobs.get(job)?.attempts.find(({ commit }) => commit.attemptId === id);
|
|
4179
|
-
return entry ? copyCommit(entry.commit) : null;
|
|
4507
|
+
return entry ? copyCommit(entry.commit, capacity) : null;
|
|
4180
4508
|
});
|
|
4181
4509
|
},
|
|
4182
4510
|
async claimNext(scope, claim, call) {
|
|
4183
|
-
return
|
|
4184
|
-
const
|
|
4185
|
-
return next(scope, "nextAt", check,
|
|
4511
|
+
return primitive(call, true, (check) => {
|
|
4512
|
+
const window = claimWindow(readClaim(claim), MAX_STORE_CANDIDATES, capacity);
|
|
4513
|
+
return window.result(next(scope, "nextAt", check, window.decide));
|
|
4186
4514
|
});
|
|
4187
4515
|
},
|
|
4188
4516
|
async renew(scope, fence, leaseMs, call) {
|
|
4189
|
-
return
|
|
4517
|
+
return primitive(call, true, (check) => {
|
|
4190
4518
|
const safe = readFence(fence);
|
|
4191
4519
|
const duration = readLeaseMs(leaseMs);
|
|
4192
4520
|
return point(scope, safe.jobId, check, (row, time) => renewJob(row, safe, duration, time));
|
|
4193
4521
|
});
|
|
4194
4522
|
},
|
|
4195
4523
|
async markHandoff(scope, fence, leaseMs, call) {
|
|
4196
|
-
return
|
|
4524
|
+
return primitive(call, true, (check) => {
|
|
4197
4525
|
const safe = readFence(fence);
|
|
4198
4526
|
const duration = readLeaseMs(leaseMs);
|
|
4199
4527
|
return point(scope, safe.jobId, check, (row, time) => markJob(row, safe, duration, time));
|
|
4200
4528
|
});
|
|
4201
4529
|
},
|
|
4202
4530
|
async finish(scope, finish, call) {
|
|
4203
|
-
return
|
|
4204
|
-
const safe = readFinish(finish);
|
|
4205
|
-
return point(scope, safe.fence.jobId, check, (row, time) => finishJob(row, safe, time));
|
|
4531
|
+
return primitive(call, true, (check) => {
|
|
4532
|
+
const safe = readFinish(finish, capacity);
|
|
4533
|
+
return point(scope, safe.fence.jobId, check, (row, time) => finishJob(row, safe, time, capacity));
|
|
4206
4534
|
});
|
|
4207
4535
|
},
|
|
4208
4536
|
async recoverNext(scope, call) {
|
|
4209
|
-
return
|
|
4537
|
+
return primitive(call, true, (check) => {
|
|
4210
4538
|
const operationId = (0, import_node_crypto5.randomUUID)();
|
|
4211
|
-
return next(scope, "leaseEndsAt", check, (row, time) => recoverJob(row, operationId, time));
|
|
4539
|
+
return next(scope, "leaseEndsAt", check, (row, time) => recoverJob(row, operationId, time, capacity));
|
|
4212
4540
|
});
|
|
4213
4541
|
},
|
|
4214
4542
|
async expireNext(scope, call) {
|
|
4215
|
-
return
|
|
4543
|
+
return primitive(call, true, (check) => next(scope, "queuedExpiresAt", check, expireJob));
|
|
4216
4544
|
},
|
|
4217
4545
|
async cancel(scope, jobId, call) {
|
|
4218
|
-
return
|
|
4546
|
+
return primitive(call, false, (check) => {
|
|
4219
4547
|
const operationId = (0, import_node_crypto5.randomUUID)();
|
|
4220
|
-
return point(scope, readStoreId(jobId), check, (row, time) => cancelJob(row, operationId, time)) ?? Object.freeze({ status: "not_found" });
|
|
4548
|
+
return point(scope, readStoreId(jobId), check, (row, time) => cancelJob(row, operationId, time, capacity)) ?? Object.freeze({ status: "not_found" });
|
|
4221
4549
|
});
|
|
4222
4550
|
},
|
|
4223
4551
|
async resolveUnknown(scope, command, call) {
|
|
4224
|
-
return
|
|
4552
|
+
return primitive(call, false, (check) => {
|
|
4225
4553
|
const safe = readResolution(command);
|
|
4226
4554
|
return point(scope, safe.jobId, check, (row, time) => resolveJob(row, safe, time)) ?? invalid("not_found");
|
|
4227
4555
|
});
|
|
4228
4556
|
},
|
|
4229
4557
|
async reserveProjection(scope, call) {
|
|
4230
|
-
return
|
|
4558
|
+
return primitive(
|
|
4559
|
+
call,
|
|
4560
|
+
false,
|
|
4561
|
+
(check) => next(scope, "pendingAt", check, (row, time) => reserveJobProjection(row, time, capacity))
|
|
4562
|
+
);
|
|
4231
4563
|
},
|
|
4232
4564
|
async putActivity(scope, permit, call) {
|
|
4233
|
-
return
|
|
4234
|
-
const safe = readProjectionPermit(scopeToken(scope), permit);
|
|
4565
|
+
return primitive(call, false, (check) => {
|
|
4566
|
+
const safe = readProjectionPermit(scopeToken(scope), permit, capacity);
|
|
4235
4567
|
const rows = scopes.get(scope);
|
|
4236
4568
|
const { jobId, attemptId } = safe.activity;
|
|
4237
4569
|
const existing = rows?.byJob.get(jobId)?.byId.get(attemptId);
|
|
@@ -4247,8 +4579,8 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4247
4579
|
});
|
|
4248
4580
|
},
|
|
4249
4581
|
async ackProjection(scope, permit, call) {
|
|
4250
|
-
return
|
|
4251
|
-
const safe = readProjectionPermit(scopeToken(scope), permit);
|
|
4582
|
+
return primitive(call, false, (check) => {
|
|
4583
|
+
const safe = readProjectionPermit(scopeToken(scope), permit, capacity);
|
|
4252
4584
|
const { jobId, attemptId } = safe.activity;
|
|
4253
4585
|
const proof = scopes.get(scope)?.byJob.get(jobId)?.byId.get(attemptId);
|
|
4254
4586
|
return point(scope, jobId, check, (row, time) => acknowledgeJobProjection(row, safe, proof, time)) ?? "stale";
|
|
@@ -4256,7 +4588,7 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4256
4588
|
},
|
|
4257
4589
|
async listActivities(scope, query, call) {
|
|
4258
4590
|
return read(call, false, () => {
|
|
4259
|
-
const criteria = readActivityCriteria(scopeToken(scope), query);
|
|
4591
|
+
const criteria = readActivityCriteria(scopeToken(scope), query, capacity);
|
|
4260
4592
|
const rows = scopes.get(scope);
|
|
4261
4593
|
const { jobId, before, from, after, limit } = criteria;
|
|
4262
4594
|
const index = (jobId === null ? rows?.activities : rows?.byJob.get(jobId)?.ordered) ?? [];
|
|
@@ -4270,7 +4602,7 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4270
4602
|
});
|
|
4271
4603
|
},
|
|
4272
4604
|
async cleanupNext(scope, call) {
|
|
4273
|
-
return
|
|
4605
|
+
return primitive(call, false, (check) => next(scope, "nextCleanupAt", check, cleanupJob));
|
|
4274
4606
|
}
|
|
4275
4607
|
};
|
|
4276
4608
|
return Object.freeze(store);
|
|
@@ -4279,11 +4611,14 @@ function createMemoryEmailStore(options = {}) {
|
|
|
4279
4611
|
0 && (module.exports = {
|
|
4280
4612
|
EmailError,
|
|
4281
4613
|
createEmailAdmin,
|
|
4614
|
+
createEmailBatchAdmission,
|
|
4282
4615
|
createEmailBatchService,
|
|
4616
|
+
createEmailRelay,
|
|
4283
4617
|
createEmailService,
|
|
4284
4618
|
createEmailTemplateRegistry,
|
|
4285
4619
|
createEmailWorker,
|
|
4286
4620
|
createMemoryEmailStore,
|
|
4621
|
+
createSmtpAcceptanceResult,
|
|
4287
4622
|
defineLocalTemplate,
|
|
4288
4623
|
defineSendGridTemplate
|
|
4289
4624
|
});
|