@sayedhfatimi/convex-email 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +700 -2
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +9 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +20 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/resend/inbound.d.ts +5 -0
- package/dist/adapters/resend/inbound.d.ts.map +1 -0
- package/dist/adapters/resend/inbound.js +207 -0
- package/dist/adapters/resend/inbound.js.map +1 -0
- package/dist/adapters/resend/index.d.ts +4 -0
- package/dist/adapters/resend/index.d.ts.map +1 -0
- package/dist/adapters/resend/index.js +27 -0
- package/dist/adapters/resend/index.js.map +1 -0
- package/dist/adapters/resend/parse.d.ts +3 -0
- package/dist/adapters/resend/parse.d.ts.map +1 -0
- package/dist/adapters/resend/parse.js +94 -0
- package/dist/adapters/resend/parse.js.map +1 -0
- package/dist/adapters/resend/send.d.ts +15 -0
- package/dist/adapters/resend/send.d.ts.map +1 -0
- package/dist/adapters/resend/send.js +117 -0
- package/dist/adapters/resend/send.js.map +1 -0
- package/dist/adapters/resend/webhook.d.ts +6 -0
- package/dist/adapters/resend/webhook.d.ts.map +1 -0
- package/dist/adapters/resend/webhook.js +66 -0
- package/dist/adapters/resend/webhook.js.map +1 -0
- package/dist/adapters/ses/inbound.d.ts +3 -0
- package/dist/adapters/ses/inbound.d.ts.map +1 -0
- package/dist/adapters/ses/inbound.js +52 -0
- package/dist/adapters/ses/inbound.js.map +1 -0
- package/dist/adapters/ses/index.d.ts +4 -0
- package/dist/adapters/ses/index.d.ts.map +1 -0
- package/dist/adapters/ses/index.js +28 -0
- package/dist/adapters/ses/index.js.map +1 -0
- package/dist/adapters/ses/parse.d.ts +7 -0
- package/dist/adapters/ses/parse.d.ts.map +1 -0
- package/dist/adapters/ses/parse.js +209 -0
- package/dist/adapters/ses/parse.js.map +1 -0
- package/dist/adapters/ses/send.d.ts +10 -0
- package/dist/adapters/ses/send.d.ts.map +1 -0
- package/dist/adapters/ses/send.js +179 -0
- package/dist/adapters/ses/send.js.map +1 -0
- package/dist/adapters/ses/sigv4.d.ts +19 -0
- package/dist/adapters/ses/sigv4.d.ts.map +1 -0
- package/dist/adapters/ses/sigv4.js +50 -0
- package/dist/adapters/ses/sigv4.js.map +1 -0
- package/dist/adapters/ses/sns.d.ts +23 -0
- package/dist/adapters/ses/sns.d.ts.map +1 -0
- package/dist/adapters/ses/sns.js +281 -0
- package/dist/adapters/ses/sns.js.map +1 -0
- package/dist/adapters/types.d.ts +11 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +2 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/client/index.d.ts +214 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +304 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +62 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +345 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/attachments.d.ts +21 -0
- package/dist/component/attachments.d.ts.map +1 -0
- package/dist/component/attachments.js +66 -0
- package/dist/component/attachments.js.map +1 -0
- package/dist/component/cleanup.d.ts +15 -0
- package/dist/component/cleanup.d.ts.map +1 -0
- package/dist/component/cleanup.js +152 -0
- package/dist/component/cleanup.js.map +1 -0
- package/dist/component/config.d.ts +34 -0
- package/dist/component/config.d.ts.map +1 -0
- package/dist/component/config.js +33 -0
- package/dist/component/config.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +8 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/credentials.d.ts +19 -0
- package/dist/component/credentials.d.ts.map +1 -0
- package/dist/component/credentials.js +45 -0
- package/dist/component/credentials.js.map +1 -0
- package/dist/component/deliver.d.ts +58 -0
- package/dist/component/deliver.d.ts.map +1 -0
- package/dist/component/deliver.js +328 -0
- package/dist/component/deliver.js.map +1 -0
- package/dist/component/events.d.ts +15 -0
- package/dist/component/events.d.ts.map +1 -0
- package/dist/component/events.js +141 -0
- package/dist/component/events.js.map +1 -0
- package/dist/component/inbound.d.ts +354 -0
- package/dist/component/inbound.d.ts.map +1 -0
- package/dist/component/inbound.js +933 -0
- package/dist/component/inbound.js.map +1 -0
- package/dist/component/queries.d.ts +76 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +175 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +473 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +185 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/send.d.ts +112 -0
- package/dist/component/send.d.ts.map +1 -0
- package/dist/component/send.js +393 -0
- package/dist/component/send.js.map +1 -0
- package/dist/component/status.d.ts +2 -0
- package/dist/component/status.d.ts.map +1 -0
- package/dist/component/status.js +8 -0
- package/dist/component/status.js.map +1 -0
- package/dist/component/suppression.d.ts +36 -0
- package/dist/component/suppression.d.ts.map +1 -0
- package/dist/component/suppression.js +89 -0
- package/dist/component/suppression.js.map +1 -0
- package/dist/component/unsubscribe.d.ts +58 -0
- package/dist/component/unsubscribe.d.ts.map +1 -0
- package/dist/component/unsubscribe.js +129 -0
- package/dist/component/unsubscribe.js.map +1 -0
- package/dist/component/webhooks.d.ts +36 -0
- package/dist/component/webhooks.d.ts.map +1 -0
- package/dist/component/webhooks.js +71 -0
- package/dist/component/webhooks.js.map +1 -0
- package/dist/shared/address.d.ts +9 -0
- package/dist/shared/address.d.ts.map +1 -0
- package/dist/shared/address.js +76 -0
- package/dist/shared/address.js.map +1 -0
- package/dist/shared/authResults.d.ts +15 -0
- package/dist/shared/authResults.d.ts.map +1 -0
- package/dist/shared/authResults.js +86 -0
- package/dist/shared/authResults.js.map +1 -0
- package/dist/shared/htmlToText.d.ts +13 -0
- package/dist/shared/htmlToText.d.ts.map +1 -0
- package/dist/shared/htmlToText.js +221 -0
- package/dist/shared/htmlToText.js.map +1 -0
- package/dist/shared/mime.d.ts +4 -0
- package/dist/shared/mime.d.ts.map +1 -0
- package/dist/shared/mime.js +63 -0
- package/dist/shared/mime.js.map +1 -0
- package/dist/shared/readCapped.d.ts +7 -0
- package/dist/shared/readCapped.d.ts.map +1 -0
- package/dist/shared/readCapped.js +38 -0
- package/dist/shared/readCapped.js.map +1 -0
- package/dist/shared/sanitize.d.ts +10 -0
- package/dist/shared/sanitize.d.ts.map +1 -0
- package/dist/shared/sanitize.js +68 -0
- package/dist/shared/sanitize.js.map +1 -0
- package/dist/shared/threading.d.ts +6 -0
- package/dist/shared/threading.d.ts.map +1 -0
- package/dist/shared/threading.js +23 -0
- package/dist/shared/threading.js.map +1 -0
- package/dist/shared/types.d.ts +260 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +2 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/shared/unsubToken.d.ts +15 -0
- package/dist/shared/unsubToken.d.ts.map +1 -0
- package/dist/shared/unsubToken.js +83 -0
- package/dist/shared/unsubToken.js.map +1 -0
- package/dist/shared/validators.d.ts +760 -0
- package/dist/shared/validators.d.ts.map +1 -0
- package/dist/shared/validators.js +200 -0
- package/dist/shared/validators.js.map +1 -0
- package/package.json +88 -4
- package/src/adapters/index.ts +8 -0
- package/src/adapters/registry.ts +28 -0
- package/src/adapters/resend/inbound.ts +245 -0
- package/src/adapters/resend/index.ts +29 -0
- package/src/adapters/resend/parse.ts +118 -0
- package/src/adapters/resend/send.ts +125 -0
- package/src/adapters/resend/webhook.ts +77 -0
- package/src/adapters/ses/inbound.ts +63 -0
- package/src/adapters/ses/index.ts +30 -0
- package/src/adapters/ses/parse.ts +236 -0
- package/src/adapters/ses/send.ts +206 -0
- package/src/adapters/ses/sigv4.ts +56 -0
- package/src/adapters/ses/sns.ts +321 -0
- package/src/adapters/types.ts +27 -0
- package/src/client/index.ts +549 -0
- package/src/component/_generated/api.ts +78 -0
- package/src/component/_generated/component.ts +475 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/attachments.ts +69 -0
- package/src/component/cleanup.ts +173 -0
- package/src/component/config.ts +52 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/credentials.ts +63 -0
- package/src/component/deliver.ts +380 -0
- package/src/component/events.ts +179 -0
- package/src/component/inbound.ts +1107 -0
- package/src/component/queries.ts +203 -0
- package/src/component/schema.ts +222 -0
- package/src/component/send.ts +467 -0
- package/src/component/status.ts +8 -0
- package/src/component/suppression.ts +114 -0
- package/src/component/unsubscribe.ts +154 -0
- package/src/component/webhooks.ts +84 -0
- package/src/shared/address.ts +75 -0
- package/src/shared/authResults.ts +94 -0
- package/src/shared/htmlToText.ts +218 -0
- package/src/shared/mime.ts +80 -0
- package/src/shared/readCapped.ts +39 -0
- package/src/shared/sanitize.ts +73 -0
- package/src/shared/threading.ts +26 -0
- package/src/shared/types.ts +254 -0
- package/src/shared/unsubToken.ts +95 -0
- package/src/shared/validators.ts +282 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads at most `limit` bytes of a response body; null when it is larger.
|
|
3
|
+
* The Content-Length header is checked first and the stream is cancelled the
|
|
4
|
+
* moment it passes the cap, so an oversize body is never buffered.
|
|
5
|
+
*/
|
|
6
|
+
export async function readCapped(
|
|
7
|
+
res: Response,
|
|
8
|
+
limit: number,
|
|
9
|
+
): Promise<ArrayBuffer | null> {
|
|
10
|
+
const declared = Number(res.headers.get("content-length"));
|
|
11
|
+
if (Number.isFinite(declared) && declared > limit) {
|
|
12
|
+
await res.body?.cancel();
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (!res.body) {
|
|
16
|
+
const whole = await res.arrayBuffer();
|
|
17
|
+
return whole.byteLength > limit ? null : whole;
|
|
18
|
+
}
|
|
19
|
+
const reader = res.body.getReader();
|
|
20
|
+
const chunks: Uint8Array[] = [];
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (;;) {
|
|
23
|
+
const { done, value } = await reader.read();
|
|
24
|
+
if (done) break;
|
|
25
|
+
total += value.byteLength;
|
|
26
|
+
if (total > limit) {
|
|
27
|
+
await reader.cancel();
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
chunks.push(value);
|
|
31
|
+
}
|
|
32
|
+
const out = new Uint8Array(total);
|
|
33
|
+
let at = 0;
|
|
34
|
+
for (const c of chunks) {
|
|
35
|
+
out.set(c, at);
|
|
36
|
+
at += c.byteLength;
|
|
37
|
+
}
|
|
38
|
+
return out.buffer;
|
|
39
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import sanitizeHtml from "sanitize-html";
|
|
2
|
+
|
|
3
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: mirrors the control-char stripping browsers and sanitize-html apply to URLs
|
|
4
|
+
const STRIP = /[\x00-\x20]/g;
|
|
5
|
+
const REMOTE_SRC = /^https?:\/\//;
|
|
6
|
+
// Raster images only, base64, no parameters. Tested on the raw value, so any
|
|
7
|
+
// whitespace or control character fails it.
|
|
8
|
+
const DATA_IMAGE_SRC =
|
|
9
|
+
/^data:image\/(png|jpeg|gif|webp);base64,[A-Za-z0-9+/]+=*$/i;
|
|
10
|
+
const CID_SRC = /^cid:[^\s:]+$/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Clean attacker-controlled inbound HTML. Runs in Convex's default runtime
|
|
14
|
+
* (proven in Task 1). Allow-list modelled on vemail's inbound sanitiser, but
|
|
15
|
+
* images are limited to http(s) sources.
|
|
16
|
+
*/
|
|
17
|
+
export function sanitizeInboundHtml(html: string): {
|
|
18
|
+
html: string;
|
|
19
|
+
hasRemoteImages: boolean;
|
|
20
|
+
} {
|
|
21
|
+
let hasRemoteImages = false;
|
|
22
|
+
const clean = sanitizeHtml(html, {
|
|
23
|
+
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img", "h1", "h2"]),
|
|
24
|
+
allowedAttributes: {
|
|
25
|
+
a: ["href", "name", "target", "rel"],
|
|
26
|
+
img: ["src", "alt", "width", "height", "data-remote-image"],
|
|
27
|
+
},
|
|
28
|
+
allowedSchemes: ["http", "https", "mailto"],
|
|
29
|
+
allowedSchemesByTag: { img: ["http", "https", "cid", "data"] },
|
|
30
|
+
allowProtocolRelative: false,
|
|
31
|
+
transformTags: {
|
|
32
|
+
a: (tagName, attribs) => ({
|
|
33
|
+
tagName,
|
|
34
|
+
attribs: { ...attribs, target: "_blank", rel: "noopener noreferrer" },
|
|
35
|
+
}),
|
|
36
|
+
img: (tagName, attribs) => {
|
|
37
|
+
// The marker comes only from the sanitiser, never from input.
|
|
38
|
+
const { "data-remote-image": _drop, src: rawSrc, ...rest } = attribs;
|
|
39
|
+
// A data: source stays only when it is a plain raster image; it is
|
|
40
|
+
// local, so it is not marked remote. Anything else loses its src.
|
|
41
|
+
if (/^data:/i.test((rawSrc ?? "").replace(STRIP, ""))) {
|
|
42
|
+
return {
|
|
43
|
+
tagName,
|
|
44
|
+
attribs: DATA_IMAGE_SRC.test(rawSrc ?? "")
|
|
45
|
+
? { ...rest, src: rawSrc as string }
|
|
46
|
+
: rest,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// Decide on the same normalised value the scheme filter keeps.
|
|
50
|
+
const src = (rawSrc ?? "").replace(STRIP, "").toLowerCase();
|
|
51
|
+
if (CID_SRC.test(src)) {
|
|
52
|
+
return {
|
|
53
|
+
tagName,
|
|
54
|
+
attribs: { ...rest, src: (rawSrc ?? "").replace(STRIP, "") },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (REMOTE_SRC.test(src)) {
|
|
58
|
+
hasRemoteImages = true;
|
|
59
|
+
return {
|
|
60
|
+
tagName,
|
|
61
|
+
attribs: {
|
|
62
|
+
...rest,
|
|
63
|
+
src: (rawSrc ?? "").replace(STRIP, ""),
|
|
64
|
+
"data-remote-image": "",
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return { tagName, attribs: rest };
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
return { html: clean, hasRemoteImages };
|
|
73
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const MAX_REFERENCES = 20;
|
|
2
|
+
|
|
3
|
+
export function generateMessageId(domain: string): string {
|
|
4
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
5
|
+
let hex = "";
|
|
6
|
+
for (const b of bytes) hex += b.toString(16).padStart(2, "0");
|
|
7
|
+
return `<${hex}@${domain}>`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function buildReferences(parent: {
|
|
11
|
+
rfcMessageId: string;
|
|
12
|
+
references: string[];
|
|
13
|
+
}): string[] {
|
|
14
|
+
// De-duplicate keeping the LAST occurrence, so the parent id is always last.
|
|
15
|
+
const all = [...parent.references, parent.rfcMessageId];
|
|
16
|
+
const seen = new Set<string>();
|
|
17
|
+
const ordered: string[] = [];
|
|
18
|
+
for (let i = all.length - 1; i >= 0; i--) {
|
|
19
|
+
const id = all[i] as string;
|
|
20
|
+
if (!seen.has(id)) {
|
|
21
|
+
seen.add(id);
|
|
22
|
+
ordered.push(id);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return ordered.reverse().slice(-MAX_REFERENCES);
|
|
26
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type { Verdict } from "./authResults.js";
|
|
2
|
+
|
|
3
|
+
export type ProviderName = "resend" | "ses";
|
|
4
|
+
export type Stream = "transactional" | "bulk";
|
|
5
|
+
export type CredentialPurpose =
|
|
6
|
+
| "send"
|
|
7
|
+
| "fetchInbound"
|
|
8
|
+
| "verifyWebhook"
|
|
9
|
+
| "unsubscribe";
|
|
10
|
+
|
|
11
|
+
export type ResendCredentials = {
|
|
12
|
+
provider: "resend";
|
|
13
|
+
apiKey: string;
|
|
14
|
+
webhookSecret: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type SesCredentials = {
|
|
18
|
+
provider: "ses";
|
|
19
|
+
region: string;
|
|
20
|
+
accessKeyId: string;
|
|
21
|
+
secretAccessKey: string;
|
|
22
|
+
sessionToken?: string;
|
|
23
|
+
configurationSets: { transactional: string; bulk?: string };
|
|
24
|
+
trustedTopicArns: string[];
|
|
25
|
+
/** S3 buckets `fetchInbound` may read; absent or empty refuses all. */
|
|
26
|
+
inboundBuckets?: string[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type UnsubscribeCredentials = { unsubscribeSecret: string };
|
|
30
|
+
|
|
31
|
+
export type EventType =
|
|
32
|
+
| "sent"
|
|
33
|
+
| "delivered"
|
|
34
|
+
| "delivery_delayed"
|
|
35
|
+
| "bounced"
|
|
36
|
+
| "complained"
|
|
37
|
+
| "failed"
|
|
38
|
+
| "rejected";
|
|
39
|
+
|
|
40
|
+
export type Capabilities = {
|
|
41
|
+
tenants: boolean;
|
|
42
|
+
listManagement: boolean;
|
|
43
|
+
inbound: boolean;
|
|
44
|
+
maxMessageBytes: number;
|
|
45
|
+
maxAttachmentBytes: number;
|
|
46
|
+
events: EventType[];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type NormalizedEvent = {
|
|
50
|
+
providerId: string;
|
|
51
|
+
providerEventId: string;
|
|
52
|
+
type: EventType;
|
|
53
|
+
bounceType?: "hard" | "soft" | "undetermined";
|
|
54
|
+
/** Epoch milliseconds. */
|
|
55
|
+
at: number;
|
|
56
|
+
recipients: string[];
|
|
57
|
+
detail?: string;
|
|
58
|
+
rfcMessageId?: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** What the app's `onEvent` handler receives for each stored delivery event. */
|
|
62
|
+
export type EventForApp = {
|
|
63
|
+
messageId: string;
|
|
64
|
+
type: EventType;
|
|
65
|
+
bounceType?: "hard" | "soft" | "undetermined";
|
|
66
|
+
/** Epoch milliseconds. */
|
|
67
|
+
at: number;
|
|
68
|
+
tags: Record<string, string>;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type OutboundPayload = {
|
|
72
|
+
from: string;
|
|
73
|
+
to: string[];
|
|
74
|
+
cc: string[];
|
|
75
|
+
bcc: string[];
|
|
76
|
+
replyTo: string[];
|
|
77
|
+
subject: string;
|
|
78
|
+
html?: string;
|
|
79
|
+
text?: string;
|
|
80
|
+
headers: { name: string; value: string }[];
|
|
81
|
+
attachments: {
|
|
82
|
+
filename: string;
|
|
83
|
+
contentType: string;
|
|
84
|
+
content: ArrayBuffer;
|
|
85
|
+
contentId?: string;
|
|
86
|
+
disposition: "attachment" | "inline";
|
|
87
|
+
}[];
|
|
88
|
+
tenant?: string;
|
|
89
|
+
stream: Stream;
|
|
90
|
+
tags: Record<string, string>;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* `rfcMessageId` is set when the provider chose the Message-ID (SES does).
|
|
95
|
+
* `ambiguous` marks a failure with no definite answer from the provider (a
|
|
96
|
+
* network error, a timeout, a 2xx without an id): it may have the message.
|
|
97
|
+
*/
|
|
98
|
+
export type SendResult =
|
|
99
|
+
| { ok: true; providerId: string; rfcMessageId?: string }
|
|
100
|
+
| { ok: false; retryable: boolean; error: string; ambiguous?: true };
|
|
101
|
+
|
|
102
|
+
export type WebhookRequest = {
|
|
103
|
+
headers: Record<string, string>;
|
|
104
|
+
body: string;
|
|
105
|
+
tenant?: string;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** The `handled` branch is for SNS subscription confirmation. */
|
|
109
|
+
export type VerifyResult =
|
|
110
|
+
| { ok: true; payload: unknown; deliveryId: string }
|
|
111
|
+
| { ok: true; handled: true; status: number }
|
|
112
|
+
| { ok: false; status: 401 | 400; reason: string };
|
|
113
|
+
|
|
114
|
+
export type InboundNotification = {
|
|
115
|
+
providerId: string;
|
|
116
|
+
recipients: string[];
|
|
117
|
+
from?: string;
|
|
118
|
+
location:
|
|
119
|
+
| { kind: "resend"; emailId: string }
|
|
120
|
+
| { kind: "s3"; bucket: string; key: string };
|
|
121
|
+
verdicts?: Partial<Record<"spf" | "dkim" | "dmarc", Verdict>>;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export type ParsedWebhook = {
|
|
125
|
+
events: NormalizedEvent[];
|
|
126
|
+
inbound: InboundNotification[];
|
|
127
|
+
ignored: number;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export type Addr = { address: string; name?: string };
|
|
131
|
+
|
|
132
|
+
export type ParsedAttachment = {
|
|
133
|
+
filename: string;
|
|
134
|
+
contentType: string;
|
|
135
|
+
size: number;
|
|
136
|
+
contentId?: string;
|
|
137
|
+
disposition: "attachment" | "inline";
|
|
138
|
+
content: ArrayBuffer;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export type ParsedInbound = {
|
|
142
|
+
messageId?: string;
|
|
143
|
+
inReplyTo?: string;
|
|
144
|
+
references: string[];
|
|
145
|
+
from: Addr;
|
|
146
|
+
to: Addr[];
|
|
147
|
+
cc: Addr[];
|
|
148
|
+
replyTo: Addr[];
|
|
149
|
+
subject: string;
|
|
150
|
+
text?: string;
|
|
151
|
+
html?: string;
|
|
152
|
+
headers: { name: string; value: string }[];
|
|
153
|
+
/** Epoch milliseconds. */
|
|
154
|
+
date?: number;
|
|
155
|
+
attachments: ParsedAttachment[];
|
|
156
|
+
/** True when the adapter skipped content (attachment caps, size limits). */
|
|
157
|
+
truncated?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Verdicts the provider itself reported at fetch time (Resend's Receiving
|
|
160
|
+
* API). They rank with a notification's verdicts: above any
|
|
161
|
+
* Authentication-Results header.
|
|
162
|
+
*/
|
|
163
|
+
verdicts?: Partial<Record<"spf" | "dkim" | "dmarc", Verdict>>;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** What the app's `onInbound` handler receives for each stored inbound message. */
|
|
167
|
+
export type InboundForApp = {
|
|
168
|
+
messageId: string;
|
|
169
|
+
tenant?: string;
|
|
170
|
+
provider: ProviderName | "raw";
|
|
171
|
+
from: Addr;
|
|
172
|
+
to: Addr[];
|
|
173
|
+
cc: Addr[];
|
|
174
|
+
subject: string;
|
|
175
|
+
/**
|
|
176
|
+
* Present whenever the message had any body: the sender's text part, or,
|
|
177
|
+
* for HTML-only mail, plain text derived from the cleaned `html`.
|
|
178
|
+
*/
|
|
179
|
+
text?: string;
|
|
180
|
+
/** True when `text` was derived from the HTML (the sender sent no text part). */
|
|
181
|
+
textFromHtml?: true;
|
|
182
|
+
html?: string;
|
|
183
|
+
/** True when text + html were over 256 KiB: read them with the client's `inboundBody`. */
|
|
184
|
+
bodyByReference: boolean;
|
|
185
|
+
hasRemoteImages: boolean;
|
|
186
|
+
attachments: {
|
|
187
|
+
id: string;
|
|
188
|
+
filename: string;
|
|
189
|
+
contentType: string;
|
|
190
|
+
size: number;
|
|
191
|
+
}[];
|
|
192
|
+
verdicts: { spf?: Verdict; dkim?: Verdict; dmarc?: Verdict };
|
|
193
|
+
threadId: string;
|
|
194
|
+
/** True when sender-controlled content was cut to fit storage limits. */
|
|
195
|
+
truncated?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Set when the message arrived with no tenant (an account-level route and
|
|
198
|
+
* no default tenant) and took `tenant` from the outbound message it
|
|
199
|
+
* answers, because the sender was one of that message's recipients.
|
|
200
|
+
*/
|
|
201
|
+
tenantAdopted?: true;
|
|
202
|
+
/**
|
|
203
|
+
* The outbound message this answers. `senderWasRecipient` is true when the
|
|
204
|
+
* inbound From is one of that message's recipients; a reply from anyone
|
|
205
|
+
* else merely quotes our Message-ID, so the app decides whether to trust it.
|
|
206
|
+
*/
|
|
207
|
+
parent?: {
|
|
208
|
+
messageId: string;
|
|
209
|
+
tags: Record<string, string>;
|
|
210
|
+
senderWasRecipient: boolean;
|
|
211
|
+
};
|
|
212
|
+
headers: { name: string; value: string }[];
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export type AdapterOptions = { resend?: { ownDomains: string[] } };
|
|
216
|
+
|
|
217
|
+
export type MessageStatus =
|
|
218
|
+
| "received"
|
|
219
|
+
| "queued"
|
|
220
|
+
| "test"
|
|
221
|
+
| "suppressed"
|
|
222
|
+
| "sending"
|
|
223
|
+
| "sent"
|
|
224
|
+
| "delivery_delayed"
|
|
225
|
+
| "delivered"
|
|
226
|
+
| "bounced"
|
|
227
|
+
| "complained"
|
|
228
|
+
| "failed"
|
|
229
|
+
| "rejected";
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Per-call runtime configuration the client passes to the component. It never
|
|
233
|
+
* carries credentials: those come from the `credentialsHandle` callback.
|
|
234
|
+
* Function handles are strings.
|
|
235
|
+
*/
|
|
236
|
+
export type RuntimeConfig = {
|
|
237
|
+
providers: ProviderName[];
|
|
238
|
+
sendVia: ProviderName;
|
|
239
|
+
credentialsHandle: string;
|
|
240
|
+
onInboundHandle?: string;
|
|
241
|
+
onEventHandle?: string;
|
|
242
|
+
testMode: boolean;
|
|
243
|
+
testAllowlist: string[];
|
|
244
|
+
defaultTenant?: string;
|
|
245
|
+
adapterOptions: AdapterOptions;
|
|
246
|
+
unsubscribe?: { url: string; mailto?: string };
|
|
247
|
+
keepRawMime: boolean;
|
|
248
|
+
messageIdDomain?: string;
|
|
249
|
+
/**
|
|
250
|
+
* `Authentication-Results` authserv-ids this deployment trusts (the host
|
|
251
|
+
* that added the header). Without a match no header verdicts are recorded.
|
|
252
|
+
*/
|
|
253
|
+
trustedAuthServIds?: string[];
|
|
254
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export type UnsubPayload = {
|
|
2
|
+
tenant?: string;
|
|
3
|
+
address: string;
|
|
4
|
+
category: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
const decoder = new TextDecoder();
|
|
9
|
+
|
|
10
|
+
function toBase64Url(bytes: Uint8Array): string {
|
|
11
|
+
let bin = "";
|
|
12
|
+
for (const b of bytes) bin += String.fromCharCode(b);
|
|
13
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function fromBase64Url(s: string): Uint8Array {
|
|
17
|
+
const b64 = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
18
|
+
const bin = atob(b64.padEnd(Math.ceil(b64.length / 4) * 4, "="));
|
|
19
|
+
return Uint8Array.from(bin, (c) => c.charCodeAt(0));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function hmac(secret: string, data: string): Promise<string> {
|
|
23
|
+
const key = await crypto.subtle.importKey(
|
|
24
|
+
"raw",
|
|
25
|
+
encoder.encode(secret),
|
|
26
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
27
|
+
false,
|
|
28
|
+
["sign"],
|
|
29
|
+
);
|
|
30
|
+
const sig = await crypto.subtle.sign("HMAC", key, encoder.encode(data));
|
|
31
|
+
return toBase64Url(new Uint8Array(sig));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function constantTimeEqual(a: string, b: string): boolean {
|
|
35
|
+
const len = Math.max(a.length, b.length);
|
|
36
|
+
let diff = a.length ^ b.length;
|
|
37
|
+
for (let i = 0; i < len; i++) {
|
|
38
|
+
diff |= (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0);
|
|
39
|
+
}
|
|
40
|
+
return diff === 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Token = base64url(JSON payload) "." base64url(HMAC-SHA256 of the payload part). */
|
|
44
|
+
export async function signUnsubToken(
|
|
45
|
+
secret: string,
|
|
46
|
+
p: UnsubPayload,
|
|
47
|
+
): Promise<string> {
|
|
48
|
+
const body: UnsubPayload = { address: p.address, category: p.category };
|
|
49
|
+
if (p.tenant !== undefined) body.tenant = p.tenant;
|
|
50
|
+
const payload = toBase64Url(encoder.encode(JSON.stringify(body)));
|
|
51
|
+
return `${payload}.${await hmac(secret, payload)}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function verifyUnsubToken(
|
|
55
|
+
secret: string,
|
|
56
|
+
token: string,
|
|
57
|
+
): Promise<UnsubPayload | null> {
|
|
58
|
+
const parts = token.split(".");
|
|
59
|
+
if (parts.length !== 2) return null;
|
|
60
|
+
const [payload, sig] = parts as [string, string];
|
|
61
|
+
if (!payload || !sig) return null;
|
|
62
|
+
if (!constantTimeEqual(await hmac(secret, payload), sig)) return null;
|
|
63
|
+
try {
|
|
64
|
+
const parsed: unknown = JSON.parse(decoder.decode(fromBase64Url(payload)));
|
|
65
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
66
|
+
const { tenant, address, category } = parsed as Record<string, unknown>;
|
|
67
|
+
if (typeof address !== "string" || typeof category !== "string")
|
|
68
|
+
return null;
|
|
69
|
+
if (tenant !== undefined && typeof tenant !== "string") return null;
|
|
70
|
+
return tenant === undefined
|
|
71
|
+
? { address, category }
|
|
72
|
+
: { tenant, address, category };
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The tenant a token CLAIMS, read without checking the signature. It only
|
|
80
|
+
* picks which secret to verify with; nothing else may use it. `null` means
|
|
81
|
+
* the token is not shaped like one.
|
|
82
|
+
*/
|
|
83
|
+
export function peekUnsubTenant(token: string): string | undefined | null {
|
|
84
|
+
const parts = token.split(".");
|
|
85
|
+
if (parts.length !== 2 || !parts[0] || !parts[1]) return null;
|
|
86
|
+
try {
|
|
87
|
+
const parsed: unknown = JSON.parse(decoder.decode(fromBase64Url(parts[0])));
|
|
88
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
89
|
+
const { tenant } = parsed as Record<string, unknown>;
|
|
90
|
+
if (tenant === undefined) return undefined;
|
|
91
|
+
return typeof tenant === "string" ? tenant : null;
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|