@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/smtp.mjs
CHANGED
|
@@ -236,6 +236,12 @@ function opaqueToken(value, maxBytes, code) {
|
|
|
236
236
|
function identifier(value, code) {
|
|
237
237
|
return opaqueToken(value, MESSAGE_LIMITS.identifierBytes, code);
|
|
238
238
|
}
|
|
239
|
+
function uuidV4(value, code) {
|
|
240
|
+
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)) {
|
|
241
|
+
invalid(code);
|
|
242
|
+
}
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
239
245
|
function hashToken(value, code) {
|
|
240
246
|
if (typeof value !== "string" || value.length !== 64 || !/^[0-9a-f]{64}$/.test(value)) invalid(code);
|
|
241
247
|
return value;
|
|
@@ -249,9 +255,9 @@ function jsonKey(key, code) {
|
|
|
249
255
|
|
|
250
256
|
// src/internal/canonical-json.ts
|
|
251
257
|
var treeLimits = { depth: MESSAGE_LIMITS.jsonDepth, values: MESSAGE_LIMITS.jsonValues };
|
|
252
|
-
function canonicalJson(input, maxBytes, code = "invalid_snapshot",
|
|
258
|
+
function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits2 = treeLimits) {
|
|
253
259
|
let bytesLeft = maxBytes;
|
|
254
|
-
let valuesLeft =
|
|
260
|
+
let valuesLeft = limits2.values;
|
|
255
261
|
const chunks = [];
|
|
256
262
|
const ancestors = /* @__PURE__ */ new Set();
|
|
257
263
|
function append(text, bytes = text.length) {
|
|
@@ -270,7 +276,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
270
276
|
append(JSON.stringify(text), bytes);
|
|
271
277
|
}
|
|
272
278
|
function visit(value2, depth) {
|
|
273
|
-
if (depth >
|
|
279
|
+
if (depth > limits2.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
|
|
274
280
|
switch (typeof value2) {
|
|
275
281
|
case "string":
|
|
276
282
|
appendString(value2);
|
|
@@ -294,7 +300,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
|
|
|
294
300
|
if (ancestors.has(value2)) invalid(code);
|
|
295
301
|
ancestors.add(value2);
|
|
296
302
|
try {
|
|
297
|
-
const maxChildren = depth ===
|
|
303
|
+
const maxChildren = depth === limits2.depth ? 0 : valuesLeft;
|
|
298
304
|
if (isArray(value2, code)) {
|
|
299
305
|
const length = denseArrayLength(value2, maxChildren, code);
|
|
300
306
|
if (length && 2 * length + 1 > bytesLeft) invalid(code);
|
|
@@ -334,6 +340,71 @@ function copyJsonObject(input, maxBytes, code) {
|
|
|
334
340
|
function sha256(input) {
|
|
335
341
|
return createHash("sha256").update(input).digest("hex");
|
|
336
342
|
}
|
|
343
|
+
function encodeEvidence(input, code, capacity) {
|
|
344
|
+
return canonicalJson(input, capacity?.limits.evidenceBytes ?? 8192, code, capacity?.limits.evidenceTree).encoded;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// src/internal/smtp-capacity.ts
|
|
348
|
+
var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
|
|
349
|
+
var MAX_SMTP_RECIPIENTS = 1e3;
|
|
350
|
+
var KIB = 1024;
|
|
351
|
+
var limits = Object.freeze({
|
|
352
|
+
snapshotBytes: 2 * KIB * KIB,
|
|
353
|
+
snapshotTree: Object.freeze({ depth: 4, values: 4096 }),
|
|
354
|
+
evidenceBytes: 64 * KIB,
|
|
355
|
+
evidenceTree: Object.freeze({ depth: 5, values: 4096 }),
|
|
356
|
+
jobBsonBytes: 4 * KIB * KIB,
|
|
357
|
+
activityBsonBytes: 128 * KIB,
|
|
358
|
+
registryCache: Object.freeze({ min: 0, default: 8, max: 16 }),
|
|
359
|
+
registryPending: Object.freeze({ min: 1, default: 2, max: 4 }),
|
|
360
|
+
servicePending: 4,
|
|
361
|
+
workerProfiles: 16,
|
|
362
|
+
workerConcurrency: Object.freeze({ min: 1, default: 1, max: 2 }),
|
|
363
|
+
runClaims: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
364
|
+
runMaintenance: Object.freeze({ min: 0, default: 4, max: 16 }),
|
|
365
|
+
adminMaintenance: Object.freeze({ min: 1, default: 4, max: 16 }),
|
|
366
|
+
page: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
367
|
+
memoryJobs: Object.freeze({ min: 1, default: 8, max: 16 }),
|
|
368
|
+
mongoInFlight: 4,
|
|
369
|
+
dueCandidates: 4
|
|
370
|
+
});
|
|
371
|
+
function readBinding(input, code) {
|
|
372
|
+
const { format, namespace, maxRecipients } = readRecord(input, ["format", "namespace", "maxRecipients"], [], code);
|
|
373
|
+
if (format !== "smtp-v2") invalid(code);
|
|
374
|
+
return Object.freeze({
|
|
375
|
+
format,
|
|
376
|
+
namespace: uuidV4(namespace, code),
|
|
377
|
+
maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_SMTP_RECIPIENTS, code)
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function readCapacityOption(raw) {
|
|
381
|
+
if (!Object.hasOwn(raw, "smtpCapacity")) return;
|
|
382
|
+
return Object.freeze({ binding: readBinding(raw.smtpCapacity, "invalid_config"), limits });
|
|
383
|
+
}
|
|
384
|
+
function capacityTag(capacity) {
|
|
385
|
+
return capacity ? { smtpCapacity: capacity.binding } : {};
|
|
386
|
+
}
|
|
387
|
+
function recipientLimit(capacity) {
|
|
388
|
+
return capacity?.binding.maxRecipients ?? MESSAGE_LIMITS.recipients;
|
|
389
|
+
}
|
|
390
|
+
function matchesBinding(input, capacity, code) {
|
|
391
|
+
const binding = readBinding(input, code);
|
|
392
|
+
if (binding.namespace !== capacity.binding.namespace || binding.maxRecipients !== capacity.binding.maxRecipients) {
|
|
393
|
+
invalid(code);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function assertCapacityTag(raw, capacity, code) {
|
|
397
|
+
const tagged = Object.hasOwn(raw, "smtpCapacity");
|
|
398
|
+
if (capacity) {
|
|
399
|
+
if (!tagged) invalid(code);
|
|
400
|
+
matchesBinding(raw.smtpCapacity, capacity, code);
|
|
401
|
+
} else if (tagged) {
|
|
402
|
+
invalid(code);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function assertCapacityProvider(provider, capacity, code) {
|
|
406
|
+
if (capacity && provider.kind !== "smtp") invalid(code);
|
|
407
|
+
}
|
|
337
408
|
|
|
338
409
|
// src/internal/attempt-codecs.ts
|
|
339
410
|
var smtpStages = ["connect", "tls", "auth", "envelope", "data"];
|
|
@@ -380,6 +451,57 @@ function recipientCounts(envelope) {
|
|
|
380
451
|
function smtpMessageId(attemptId) {
|
|
381
452
|
return `<${attemptId}@email.invalid>`;
|
|
382
453
|
}
|
|
454
|
+
function readRecipientResults(input, code, { limit, allowAccepted, ordered }) {
|
|
455
|
+
const seen = /* @__PURE__ */ new Set();
|
|
456
|
+
let previous = -1;
|
|
457
|
+
return readArray(input, limit, code, (value) => {
|
|
458
|
+
const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
|
|
459
|
+
if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
|
|
460
|
+
if (status !== "rejected" && (status !== "accepted" || !allowAccepted)) invalid(code);
|
|
461
|
+
const index = integer(rawIndex, 0, limit - 1, code);
|
|
462
|
+
if (ordered) {
|
|
463
|
+
const position = recipientGroups.indexOf(group) * limit + index;
|
|
464
|
+
if (position <= previous) invalid(code);
|
|
465
|
+
previous = position;
|
|
466
|
+
} else {
|
|
467
|
+
const key = `${group}:${index}`;
|
|
468
|
+
if (seen.has(key)) invalid(code);
|
|
469
|
+
seen.add(key);
|
|
470
|
+
}
|
|
471
|
+
return Object.freeze({ group, index, status });
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
function allAccepted(recipients) {
|
|
475
|
+
return recipients.length !== 0 && recipients.every(({ status }) => status === "accepted");
|
|
476
|
+
}
|
|
477
|
+
function createSmtpAcceptanceResult(recipients, messageId) {
|
|
478
|
+
const code = "invalid_command";
|
|
479
|
+
if (messageId !== void 0 && (typeof messageId !== "string" || messageId.length !== 52 || messageId !== smtpMessageId(uuidV4(messageId.slice(1, 37), code)))) {
|
|
480
|
+
invalid(code);
|
|
481
|
+
}
|
|
482
|
+
const checked = readRecipientResults(recipients, code, {
|
|
483
|
+
limit: MAX_SMTP_RECIPIENTS,
|
|
484
|
+
allowAccepted: true,
|
|
485
|
+
ordered: false
|
|
486
|
+
});
|
|
487
|
+
const data = { retry: "never", stage: "data", ...messageId === void 0 ? {} : { messageId } };
|
|
488
|
+
if (allAccepted(checked)) {
|
|
489
|
+
return Object.freeze({ ...data, reason: "smtp_accepted", outcome: "accepted", recipients: checked });
|
|
490
|
+
}
|
|
491
|
+
if (!checked.some(({ status }) => status === "accepted")) invalid(code);
|
|
492
|
+
return Object.freeze({
|
|
493
|
+
...data,
|
|
494
|
+
reason: "smtp_partial",
|
|
495
|
+
outcome: "partially_accepted",
|
|
496
|
+
recipients: checked
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
function uncertainProviderResult(stage, reason) {
|
|
500
|
+
const base = { outcome: "unknown", retry: "never", stage };
|
|
501
|
+
if (reason === "provider_timeout") return { ...base, reason };
|
|
502
|
+
if (reason === "provider_aborted") return { ...base, reason };
|
|
503
|
+
return { ...base, reason };
|
|
504
|
+
}
|
|
383
505
|
function readHint(input, reason, code) {
|
|
384
506
|
if (reason !== "rate_deferred" && reason !== "sendgrid_throttled") invalid(code);
|
|
385
507
|
const raw = readRecord(input, [], ["delayMs", "notBefore"], code);
|
|
@@ -393,7 +515,7 @@ function readHint(input, reason, code) {
|
|
|
393
515
|
...hasFloor ? { notBefore: unixMs(raw.notBefore, code) } : {}
|
|
394
516
|
});
|
|
395
517
|
}
|
|
396
|
-
function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
518
|
+
function readAttemptResult(input, code = "invalid_command", projected = false, capacity) {
|
|
397
519
|
const raw = readRecord(
|
|
398
520
|
input,
|
|
399
521
|
["outcome", "retry", "stage", "reason"],
|
|
@@ -404,6 +526,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
404
526
|
const [outcome, retry, stages, origin] = evidence[reason];
|
|
405
527
|
const stage = stages.find((value) => value === raw.stage);
|
|
406
528
|
if (raw.outcome !== outcome || raw.retry !== retry || !stage) invalid(code);
|
|
529
|
+
if (capacity && (origin === "sendgrid" || stage === "http")) invalid(code);
|
|
407
530
|
const hasHint = Object.hasOwn(raw, "retryHint");
|
|
408
531
|
if (projected && hasHint || !projected && reason === "rate_deferred" && !hasHint) invalid(code);
|
|
409
532
|
const smtpSuccess = reason === "smtp_accepted" || reason === "smtp_partial";
|
|
@@ -412,16 +535,10 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
412
535
|
let recipients;
|
|
413
536
|
if (hasRecipients) {
|
|
414
537
|
if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
|
|
420
|
-
const index = integer(rawIndex, 0, MESSAGE_LIMITS.recipients - 1, code);
|
|
421
|
-
const key = `${group}:${index}`;
|
|
422
|
-
if (seen.has(key)) invalid(code);
|
|
423
|
-
seen.add(key);
|
|
424
|
-
return Object.freeze({ group, index, status });
|
|
538
|
+
recipients = readRecipientResults(raw.recipients, code, {
|
|
539
|
+
limit: recipientLimit(capacity),
|
|
540
|
+
allowAccepted: smtpSuccess,
|
|
541
|
+
ordered: Boolean(capacity)
|
|
425
542
|
});
|
|
426
543
|
const accepted = recipients.filter(({ status }) => status === "accepted").length;
|
|
427
544
|
if (reason === "smtp_accepted" && (!accepted || accepted !== recipients.length) || reason === "smtp_partial" && (!accepted || accepted === recipients.length)) {
|
|
@@ -435,7 +552,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
435
552
|
if (hasProviderId && (reason !== "sendgrid_accepted" || typeof providerMessageId !== "string" || providerMessageId.length > 128 || !/^[A-Za-z0-9][A-Za-z0-9._=-]{0,127}$/.test(providerMessageId))) {
|
|
436
553
|
invalid(code);
|
|
437
554
|
}
|
|
438
|
-
|
|
555
|
+
const result = Object.freeze({
|
|
439
556
|
outcome,
|
|
440
557
|
retry,
|
|
441
558
|
stage,
|
|
@@ -445,6 +562,8 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
|
|
|
445
562
|
...typeof messageId === "string" ? { messageId } : {},
|
|
446
563
|
...typeof providerMessageId === "string" ? { providerMessageId } : {}
|
|
447
564
|
});
|
|
565
|
+
if (capacity && !projected) encodeEvidence(result, code, capacity);
|
|
566
|
+
return result;
|
|
448
567
|
}
|
|
449
568
|
function assertEvidenceContext(result, provider, attemptId, counts, code) {
|
|
450
569
|
const { reason, stage, recipients, messageId } = result;
|
|
@@ -461,9 +580,9 @@ function assertEvidenceContext(result, provider, attemptId, counts, code) {
|
|
|
461
580
|
}
|
|
462
581
|
}
|
|
463
582
|
}
|
|
464
|
-
function readProviderResult(input, provider, attemptId, envelope) {
|
|
583
|
+
function readProviderResult(input, provider, attemptId, envelope, capacity) {
|
|
465
584
|
const code = "invalid_command";
|
|
466
|
-
const result = readAttemptResult(input, code);
|
|
585
|
+
const result = readAttemptResult(input, code, false, capacity);
|
|
467
586
|
const origin = evidence[result.reason][3];
|
|
468
587
|
if (origin !== "smtp" && origin !== "sendgrid" && origin !== "provider") invalid(code);
|
|
469
588
|
assertEvidenceContext(result, provider, attemptId, recipientCounts(envelope), code);
|
|
@@ -613,7 +732,8 @@ function validateMailbox(input, code = SNAPSHOT) {
|
|
|
613
732
|
...Object.hasOwn(mailbox, "name") ? { name: headerString(mailbox.name, L.displayNameBytes, code) } : {}
|
|
614
733
|
});
|
|
615
734
|
}
|
|
616
|
-
function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
735
|
+
function validateEnvelope(input, kind, code = SNAPSHOT, capacity) {
|
|
736
|
+
const limit = recipientLimit(capacity);
|
|
617
737
|
const envelope = readRecord(input, ["from", "to", "cc", "bcc"], ["replyTo"], code);
|
|
618
738
|
const seen = /* @__PURE__ */ new Set();
|
|
619
739
|
const recipient = (raw) => {
|
|
@@ -624,9 +744,9 @@ function validateEnvelope(input, kind, code = SNAPSHOT) {
|
|
|
624
744
|
return mailbox;
|
|
625
745
|
};
|
|
626
746
|
const from = validateMailbox(envelope.from, code);
|
|
627
|
-
const to = readArray(envelope.to,
|
|
628
|
-
const cc = readArray(envelope.cc,
|
|
629
|
-
const bcc = readArray(envelope.bcc,
|
|
747
|
+
const to = readArray(envelope.to, limit, code, recipient);
|
|
748
|
+
const cc = readArray(envelope.cc, limit - to.length, code, recipient);
|
|
749
|
+
const bcc = readArray(envelope.bcc, limit - to.length - cc.length, code, recipient);
|
|
630
750
|
if (seen.size === 0 || kind === "sendgrid" && to.length === 0) invalid(code);
|
|
631
751
|
return Object.freeze({
|
|
632
752
|
from,
|
|
@@ -714,12 +834,17 @@ function validateExpiry(input, preparedAt, scheduleAt, code = SNAPSHOT) {
|
|
|
714
834
|
}
|
|
715
835
|
return expiresAt;
|
|
716
836
|
}
|
|
717
|
-
function hashSnapshot(snapshot) {
|
|
837
|
+
function hashSnapshot(snapshot, capacity) {
|
|
718
838
|
return sha256(
|
|
719
|
-
canonicalJson(
|
|
839
|
+
canonicalJson(
|
|
840
|
+
snapshot,
|
|
841
|
+
capacity?.limits.snapshotBytes ?? L.snapshotBytes,
|
|
842
|
+
SNAPSHOT,
|
|
843
|
+
capacity?.limits.snapshotTree ?? { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }
|
|
844
|
+
).encoded
|
|
720
845
|
);
|
|
721
846
|
}
|
|
722
|
-
function readSnapshot(input) {
|
|
847
|
+
function readSnapshot(input, capacity) {
|
|
723
848
|
const dto = readRecord(
|
|
724
849
|
input,
|
|
725
850
|
[
|
|
@@ -734,17 +859,19 @@ function readSnapshot(input) {
|
|
|
734
859
|
"attachments",
|
|
735
860
|
"correlation"
|
|
736
861
|
],
|
|
737
|
-
["subject", "html", "text", "templateId", "data"],
|
|
862
|
+
["subject", "html", "text", "templateId", "data", "smtpCapacity"],
|
|
738
863
|
SNAPSHOT
|
|
739
864
|
);
|
|
740
865
|
if (dto.version !== EMAIL_FORMAT_VERSION || dto.kind !== "smtp" && dto.kind !== "sendgrid") invalid(SNAPSHOT);
|
|
866
|
+
assertCapacityTag(dto, capacity, SNAPSHOT);
|
|
741
867
|
const provider = validateProviderRef(dto.provider);
|
|
742
868
|
if (provider.kind !== dto.kind) invalid("provider_mismatch");
|
|
869
|
+
assertCapacityProvider(provider, capacity, "provider_mismatch");
|
|
743
870
|
const preparedAt = unixMs(dto.preparedAt, SNAPSHOT);
|
|
744
871
|
const common = {
|
|
745
872
|
version: EMAIL_FORMAT_VERSION,
|
|
746
873
|
template: validateTemplateIdentity(dto.template),
|
|
747
|
-
envelope: validateEnvelope(dto.envelope, provider.kind),
|
|
874
|
+
envelope: validateEnvelope(dto.envelope, provider.kind, SNAPSHOT, capacity),
|
|
748
875
|
preparedAt,
|
|
749
876
|
expiresAt: validateExpiry(dto.expiresAt, preparedAt, null),
|
|
750
877
|
priority: integer(dto.priority, PRIORITY.min, PRIORITY.max, SNAPSHOT),
|
|
@@ -758,7 +885,7 @@ function readSnapshot(input) {
|
|
|
758
885
|
...Object.hasOwn(dto, "html") ? { html: dto.html } : {},
|
|
759
886
|
...Object.hasOwn(dto, "text") ? { text: dto.text } : {}
|
|
760
887
|
});
|
|
761
|
-
return Object.freeze({ ...common, kind: provider.kind, provider, ...content });
|
|
888
|
+
return Object.freeze({ ...common, kind: provider.kind, provider, ...capacityTag(capacity), ...content });
|
|
762
889
|
}
|
|
763
890
|
if (Object.hasOwn(dto, "subject") || Object.hasOwn(dto, "html") || Object.hasOwn(dto, "text")) invalid(SNAPSHOT);
|
|
764
891
|
return Object.freeze({
|
|
@@ -769,31 +896,28 @@ function readSnapshot(input) {
|
|
|
769
896
|
data: copyJsonObject(dto.data, L.dataBytes, SNAPSHOT)
|
|
770
897
|
});
|
|
771
898
|
}
|
|
772
|
-
function validateSnapshot(input) {
|
|
773
|
-
const snapshot = readSnapshot(input);
|
|
774
|
-
return { snapshot, snapshotHash: hashSnapshot(snapshot) };
|
|
899
|
+
function validateSnapshot(input, capacity) {
|
|
900
|
+
const snapshot = readSnapshot(input, capacity);
|
|
901
|
+
return { snapshot, snapshotHash: hashSnapshot(snapshot, capacity) };
|
|
775
902
|
}
|
|
776
903
|
|
|
777
904
|
// src/internal/store-codecs.ts
|
|
778
905
|
var REQUEST = "invalid_command";
|
|
779
906
|
var LEASE = WORKER_LIMIT_BOUNDS.leaseMs;
|
|
780
907
|
function readStoreId(value, code = REQUEST) {
|
|
781
|
-
|
|
782
|
-
invalid(code);
|
|
783
|
-
}
|
|
784
|
-
return value;
|
|
908
|
+
return uuidV4(value, code);
|
|
785
909
|
}
|
|
786
910
|
|
|
787
911
|
// src/internal/provider-preparation.ts
|
|
788
912
|
function sameRef(a, b) {
|
|
789
913
|
return a.resolverKey === b.resolverKey && a.id === b.id && a.version === b.version && a.filename === b.filename && a.contentType === b.contentType && a.byteLength === b.byteLength && a.sha256 === b.sha256;
|
|
790
914
|
}
|
|
791
|
-
function readProviderSubmission(input, ref) {
|
|
915
|
+
function readProviderSubmission(input, ref, capacity) {
|
|
792
916
|
const code = "invalid_snapshot";
|
|
793
917
|
const raw = readRecord(input, ["jobId", "attemptId", "snapshot", "attachments"], [], code);
|
|
794
918
|
const jobId = readStoreId(raw.jobId, code);
|
|
795
919
|
const attemptId = readStoreId(raw.attemptId, code);
|
|
796
|
-
const { snapshot } = validateSnapshot(raw.snapshot);
|
|
920
|
+
const { snapshot } = validateSnapshot(raw.snapshot, capacity);
|
|
797
921
|
if (ref.kind !== snapshot.kind || ref.key !== snapshot.provider.key || ref.mode !== snapshot.provider.mode) {
|
|
798
922
|
invalid("provider_mismatch");
|
|
799
923
|
}
|
|
@@ -824,6 +948,13 @@ function readProviderCall(input) {
|
|
|
824
948
|
// src/providers/smtp/mime.ts
|
|
825
949
|
import MailComposer from "nodemailer/lib/mail-composer";
|
|
826
950
|
import { encodeWord } from "nodemailer/lib/mime-funcs";
|
|
951
|
+
|
|
952
|
+
// src/internal/smtp-attachments.ts
|
|
953
|
+
function assertSmtpAttachmentTypes(refs) {
|
|
954
|
+
if (refs.some(({ contentType }) => /^(?:message|multipart)\//.test(contentType))) invalid("invalid_snapshot");
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
// src/providers/smtp/mime.ts
|
|
827
958
|
function body(content) {
|
|
828
959
|
if (content !== void 0) return { content: Buffer.from(content, "utf8"), contentTransferEncoding: "base64" };
|
|
829
960
|
}
|
|
@@ -835,6 +966,7 @@ function prepareSmtpMessage({
|
|
|
835
966
|
attemptId,
|
|
836
967
|
attachments
|
|
837
968
|
}) {
|
|
969
|
+
assertSmtpAttachmentTypes(attachments.map(({ ref }) => ref));
|
|
838
970
|
const { from, to, cc, replyTo } = snapshot.envelope;
|
|
839
971
|
const destinations = recipientGroups.flatMap(
|
|
840
972
|
(group) => snapshot.envelope[group].map(({ address }, index) => Object.freeze({ address, group, index }))
|
|
@@ -852,16 +984,13 @@ function prepareSmtpMessage({
|
|
|
852
984
|
html: body(snapshot.html),
|
|
853
985
|
date: new Date(snapshot.preparedAt),
|
|
854
986
|
messageId,
|
|
855
|
-
attachments: attachments.map(({ ref, bytes }) => {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
contentTransferEncoding: "base64"
|
|
863
|
-
};
|
|
864
|
-
}),
|
|
987
|
+
attachments: attachments.map(({ ref, bytes }) => ({
|
|
988
|
+
filename: ref.filename,
|
|
989
|
+
contentType: ref.contentType,
|
|
990
|
+
content: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
|
991
|
+
contentDisposition: "attachment",
|
|
992
|
+
contentTransferEncoding: "base64"
|
|
993
|
+
})),
|
|
865
994
|
disableFileAccess: true,
|
|
866
995
|
disableUrlAccess: true,
|
|
867
996
|
newline: "win"
|
|
@@ -921,7 +1050,8 @@ function readAuth(input) {
|
|
|
921
1050
|
return Object.freeze({ user, pass });
|
|
922
1051
|
}
|
|
923
1052
|
function readSmtpConfig(input) {
|
|
924
|
-
const raw = readRecord(input, ["key", "host", "tls"], ["port", "servername", "ca", "auth"], CONFIG);
|
|
1053
|
+
const raw = readRecord(input, ["key", "host", "tls"], ["port", "servername", "ca", "auth", "smtpCapacity"], CONFIG);
|
|
1054
|
+
const capacity = readCapacityOption(raw);
|
|
925
1055
|
const ref = Object.freeze({ kind: "smtp", key: identifier(raw.key, CONFIG), mode: "live" });
|
|
926
1056
|
const { tls } = raw;
|
|
927
1057
|
if (tls !== "implicit" && tls !== "starttls") invalid(CONFIG);
|
|
@@ -936,7 +1066,17 @@ function readSmtpConfig(input) {
|
|
|
936
1066
|
if (ca?.length === 0) invalid(CONFIG);
|
|
937
1067
|
try {
|
|
938
1068
|
const secureContext = createSecureContext({ minVersion: SMTP_MIN_TLS_VERSION, ...ca ? { ca: [...ca] } : {} });
|
|
939
|
-
return Object.freeze({
|
|
1069
|
+
return Object.freeze({
|
|
1070
|
+
ref,
|
|
1071
|
+
host,
|
|
1072
|
+
port,
|
|
1073
|
+
tls,
|
|
1074
|
+
identity: servername ?? host,
|
|
1075
|
+
servername,
|
|
1076
|
+
secureContext,
|
|
1077
|
+
auth,
|
|
1078
|
+
capacity
|
|
1079
|
+
});
|
|
940
1080
|
} catch {
|
|
941
1081
|
return invalid(CONFIG);
|
|
942
1082
|
}
|
|
@@ -1257,18 +1397,12 @@ var SmtpConnection = class {
|
|
|
1257
1397
|
function failed(stage, reason) {
|
|
1258
1398
|
if (reason === "not_transmitted") return { outcome: "unsubmitted", retry: "safe_transient", stage, reason };
|
|
1259
1399
|
if (reason === "provider_configuration") return { outcome: "unsubmitted", retry: "never", stage, reason };
|
|
1260
|
-
return
|
|
1400
|
+
return uncertainProviderResult(stage, reason);
|
|
1261
1401
|
}
|
|
1262
|
-
function negative(
|
|
1402
|
+
function negative(at, code, permanentRcptRefusal = false) {
|
|
1263
1403
|
if (code < 400 || code > 599) throw new SmtpFailure("transport_uncertain");
|
|
1264
|
-
const
|
|
1265
|
-
return {
|
|
1266
|
-
outcome: "rejected",
|
|
1267
|
-
retry: permanent ? "never" : "safe_transient",
|
|
1268
|
-
stage,
|
|
1269
|
-
reason: permanent ? "smtp_permanent_reply" : "smtp_transient_reply",
|
|
1270
|
-
...recipients.length ? { recipients } : {}
|
|
1271
|
-
};
|
|
1404
|
+
const details = at.recipients?.length ? at : { stage: at.stage };
|
|
1405
|
+
return code >= 500 || permanentRcptRefusal ? { ...details, outcome: "rejected", retry: "never", reason: "smtp_permanent_reply" } : { ...details, outcome: "rejected", retry: "safe_transient", reason: "smtp_transient_reply" };
|
|
1272
1406
|
}
|
|
1273
1407
|
function extensions({ lines }) {
|
|
1274
1408
|
let starttls = false;
|
|
@@ -1289,34 +1423,34 @@ async function authenticate(connection, { user, pass }, mechanisms) {
|
|
|
1289
1423
|
if (command.length <= 510) response = await connection.command(command);
|
|
1290
1424
|
else {
|
|
1291
1425
|
response = await connection.command("AUTH PLAIN");
|
|
1292
|
-
if (response.code !== 334) return negative("auth", response.code);
|
|
1426
|
+
if (response.code !== 334) return negative({ stage: "auth" }, response.code);
|
|
1293
1427
|
response = await connection.command(encoded);
|
|
1294
1428
|
}
|
|
1295
1429
|
} else if (mechanisms.has("LOGIN")) {
|
|
1296
1430
|
response = await connection.command("AUTH LOGIN");
|
|
1297
|
-
if (response.code !== 334) return negative("auth", response.code);
|
|
1431
|
+
if (response.code !== 334) return negative({ stage: "auth" }, response.code);
|
|
1298
1432
|
response = await connection.command(Buffer.from(user, "utf8").toString("base64"));
|
|
1299
|
-
if (response.code !== 334) return negative("auth", response.code);
|
|
1433
|
+
if (response.code !== 334) return negative({ stage: "auth" }, response.code);
|
|
1300
1434
|
response = await connection.command(Buffer.from(pass, "utf8").toString("base64"));
|
|
1301
1435
|
} else return failed("auth", "provider_configuration");
|
|
1302
|
-
if (response.code !== 235) return negative("auth", response.code);
|
|
1436
|
+
if (response.code !== 235) return negative({ stage: "auth" }, response.code);
|
|
1303
1437
|
}
|
|
1304
1438
|
async function exchange(connection, config, message) {
|
|
1305
1439
|
await connection.connect();
|
|
1306
1440
|
if (config.tls === "implicit") await connection.startTls();
|
|
1307
1441
|
connection.stage = "connect";
|
|
1308
1442
|
const greeting = await connection.greeting();
|
|
1309
|
-
if (greeting.code !== 220) return negative("connect", greeting.code);
|
|
1443
|
+
if (greeting.code !== 220) return negative({ stage: "connect" }, greeting.code);
|
|
1310
1444
|
let ehlo = await connection.command("EHLO email.invalid");
|
|
1311
|
-
if (ehlo.code !== 250) return negative("connect", ehlo.code);
|
|
1445
|
+
if (ehlo.code !== 250) return negative({ stage: "connect" }, ehlo.code);
|
|
1312
1446
|
if (config.tls === "starttls") {
|
|
1313
1447
|
connection.stage = "tls";
|
|
1314
1448
|
if (!extensions(ehlo).starttls) return failed("tls", "provider_configuration");
|
|
1315
1449
|
const upgrade = await connection.command("STARTTLS");
|
|
1316
|
-
if (upgrade.code !== 220) return negative("tls", upgrade.code);
|
|
1450
|
+
if (upgrade.code !== 220) return negative({ stage: "tls" }, upgrade.code);
|
|
1317
1451
|
await connection.startTls();
|
|
1318
1452
|
ehlo = await connection.command("EHLO email.invalid");
|
|
1319
|
-
if (ehlo.code !== 250) return negative("tls", ehlo.code);
|
|
1453
|
+
if (ehlo.code !== 250) return negative({ stage: "tls" }, ehlo.code);
|
|
1320
1454
|
}
|
|
1321
1455
|
if (config.auth) {
|
|
1322
1456
|
const failure = await authenticate(connection, config.auth, extensions(ehlo).mechanisms);
|
|
@@ -1324,7 +1458,7 @@ async function exchange(connection, config, message) {
|
|
|
1324
1458
|
}
|
|
1325
1459
|
connection.stage = "envelope";
|
|
1326
1460
|
let { code } = await connection.command(`MAIL FROM:<${message.from}>`);
|
|
1327
|
-
if (code !== 250) return negative("envelope", code);
|
|
1461
|
+
if (code !== 250) return negative({ stage: "envelope" }, code);
|
|
1328
1462
|
const recipients = [];
|
|
1329
1463
|
let accepted = 0;
|
|
1330
1464
|
for (const { address, group, index } of message.destinations) {
|
|
@@ -1338,26 +1472,20 @@ async function exchange(connection, config, message) {
|
|
|
1338
1472
|
if (code === 421) break;
|
|
1339
1473
|
} else throw new SmtpFailure("transport_uncertain");
|
|
1340
1474
|
}
|
|
1341
|
-
const rejected = recipients.filter((
|
|
1475
|
+
const rejected = recipients.filter((recipient) => recipient.status === "rejected");
|
|
1342
1476
|
if (accepted === 0 && recipients.length === message.destinations.length) {
|
|
1343
|
-
return negative("envelope", code, connection.permanentRcptRefusal
|
|
1477
|
+
return negative({ stage: "envelope", recipients: rejected }, code, connection.permanentRcptRefusal);
|
|
1344
1478
|
}
|
|
1345
1479
|
if (code === 421) {
|
|
1346
1480
|
return failed("envelope", connection.permanentRcptRefusal ? "transport_uncertain" : "not_transmitted");
|
|
1347
1481
|
}
|
|
1348
1482
|
connection.stage = "data";
|
|
1483
|
+
const refusal = { stage: "data", recipients: rejected };
|
|
1349
1484
|
({ code } = await connection.command("DATA"));
|
|
1350
|
-
if (code !== 354) return negative(
|
|
1485
|
+
if (code !== 354) return negative(refusal, code, connection.permanentRcptRefusal);
|
|
1351
1486
|
({ code } = await connection.data(message.openMimeStream));
|
|
1352
|
-
if (code !== 250) return negative(
|
|
1353
|
-
return
|
|
1354
|
-
outcome: rejected.length ? "partially_accepted" : "accepted",
|
|
1355
|
-
retry: "never",
|
|
1356
|
-
stage: "data",
|
|
1357
|
-
reason: rejected.length ? "smtp_partial" : "smtp_accepted",
|
|
1358
|
-
messageId: message.messageId,
|
|
1359
|
-
recipients
|
|
1360
|
-
};
|
|
1487
|
+
if (code !== 250) return negative(refusal, code, connection.permanentRcptRefusal);
|
|
1488
|
+
return createSmtpAcceptanceResult(recipients, message.messageId);
|
|
1361
1489
|
}
|
|
1362
1490
|
async function submitSmtp(config, message, call, resolverFactory) {
|
|
1363
1491
|
const connection = new SmtpConnection(config, call, resolverFactory);
|
|
@@ -1374,11 +1502,12 @@ async function submitSmtp(config, message, call, resolverFactory) {
|
|
|
1374
1502
|
// src/smtp.ts
|
|
1375
1503
|
function createSmtpProvider(options) {
|
|
1376
1504
|
const config = readSmtpConfig(options);
|
|
1377
|
-
const { ref } = config;
|
|
1505
|
+
const { ref, capacity } = config;
|
|
1378
1506
|
const provider = {
|
|
1379
1507
|
ref,
|
|
1508
|
+
...capacityTag(capacity),
|
|
1380
1509
|
prepare(input) {
|
|
1381
|
-
const prepared = readProviderSubmission(input, ref);
|
|
1510
|
+
const prepared = readProviderSubmission(input, ref, capacity);
|
|
1382
1511
|
const {
|
|
1383
1512
|
attemptId,
|
|
1384
1513
|
snapshot: { envelope }
|
|
@@ -1392,13 +1521,14 @@ function createSmtpProvider(options) {
|
|
|
1392
1521
|
message = void 0;
|
|
1393
1522
|
const result = await submitSmtp(config, send, call);
|
|
1394
1523
|
try {
|
|
1395
|
-
return readProviderResult(result, ref, attemptId, envelope);
|
|
1524
|
+
return readProviderResult(result, ref, attemptId, envelope, capacity);
|
|
1396
1525
|
} catch {
|
|
1397
1526
|
return readProviderResult(
|
|
1398
1527
|
{ outcome: "unknown", retry: "never", stage: result.stage, reason: "transport_uncertain" },
|
|
1399
1528
|
ref,
|
|
1400
1529
|
attemptId,
|
|
1401
|
-
envelope
|
|
1530
|
+
envelope,
|
|
1531
|
+
capacity
|
|
1402
1532
|
);
|
|
1403
1533
|
}
|
|
1404
1534
|
}
|