ras-stack 5.4.0 → 5.5.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/dist/email/index.d.ts +4 -0
- package/dist/email/index.js +20 -4
- package/dist/email/index.js.map +1 -1
- package/package.json +1 -1
package/dist/email/index.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ export type SmtpConfig = {
|
|
|
22
22
|
secure: boolean;
|
|
23
23
|
user?: string;
|
|
24
24
|
password?: string;
|
|
25
|
+
/** The name TLS presents and verifies when `host` is an address the application already resolved. */
|
|
26
|
+
servername?: string;
|
|
27
|
+
/** Fails a send or verify that runs longer than this, and bounds each wait on the server to the same limit. */
|
|
28
|
+
timeoutMs?: number;
|
|
25
29
|
};
|
|
26
30
|
export type SmtpEnvironmentOptions = {
|
|
27
31
|
host?: string;
|
package/dist/email/index.js
CHANGED
|
@@ -27,19 +27,35 @@ export function smtpConfigFromEnvironment(environment = process.env, keys = {})
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
export function createSmtpDelivery(config) {
|
|
30
|
+
const timeout = config.timeoutMs;
|
|
30
31
|
const transport = createTransport({
|
|
31
32
|
host: config.host,
|
|
32
33
|
port: config.port,
|
|
33
34
|
secure: config.secure,
|
|
35
|
+
servername: config.servername,
|
|
34
36
|
auth: config.user ? { user: config.user, pass: config.password } : undefined,
|
|
37
|
+
...(timeout === undefined ? {} : { connectionTimeout: timeout, greetingTimeout: timeout, socketTimeout: timeout }),
|
|
35
38
|
});
|
|
36
39
|
return {
|
|
37
|
-
send: async ({ fromName, ...message }) => transport
|
|
38
|
-
|
|
39
|
-
.then(() => undefined),
|
|
40
|
-
verify: async () => transport.verify().then(() => undefined),
|
|
40
|
+
send: async ({ fromName, ...message }) => withinDeadline(transport.sendMail({ from: fromName ? { name: fromName, address: senderAddress(config.from) } : config.from, ...message }), timeout),
|
|
41
|
+
verify: async () => withinDeadline(transport.verify(), timeout),
|
|
41
42
|
};
|
|
42
43
|
}
|
|
44
|
+
// A server that trickles bytes keeps each socket wait short of its timeout, so the whole exchange needs its own deadline.
|
|
45
|
+
async function withinDeadline(work, timeoutMs) {
|
|
46
|
+
if (timeoutMs === undefined)
|
|
47
|
+
return void (await work);
|
|
48
|
+
let timer;
|
|
49
|
+
const deadline = new Promise((_resolve, reject) => {
|
|
50
|
+
timer = setTimeout(() => reject(new Error(`SMTP server did not finish within ${timeoutMs} ms`)), timeoutMs);
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
await Promise.race([work, deadline]);
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
43
59
|
function senderAddress(from) {
|
|
44
60
|
return /<([^<>]+)>\s*$/.exec(from)?.[1] ?? from;
|
|
45
61
|
}
|
package/dist/email/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/email/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/email/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAiD5C,MAAM,UAAU,yBAAyB,CACvC,WAAW,GAAsB,OAAO,CAAC,GAAG,EAC5C,IAAI,GAA2B,EAAE;IAEjC,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,IAAI,WAAW,CAAC,EAAE,IAAI,EAAE,CAAA;IAC1D,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,IAAI,YAAY,CAAC,EAAE,IAAI,EAAE,CAAA;IAC3D,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI;QAAE,OAAO,SAAS,CAAA;IACpC,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAA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AC,GAAG,EAAE,OAAO,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;AACjH,CAAC","sourcesContent":["import { createTransport } from 'nodemailer'\n\n/** `cid` lets the HTML body reference the attachment inline as `cid:<value>`. */\nexport type EmailAttachment = { filename: string; content: Buffer; contentType: string; cid?: string }\n\nexport type EmailMessage = {\n to: string\n subject: string\n text: string\n html?: string\n attachments?: EmailAttachment[]\n /** Replaces the display name of the configured sender; the address stays the configured one so SPF and DKIM still pass. */\n fromName?: string\n replyTo?: string\n}\n\nexport type SmtpConfig = {\n from: string\n host: string\n port: number\n secure: boolean\n user?: string\n password?: string\n /** The name TLS presents and verifies when `host` is an address the application already resolved. */\n servername?: string\n /** Fails a send or verify that runs longer than this, and bounds each wait on the server to the same limit. */\n timeoutMs?: number\n}\n\nexport type SmtpEnvironmentOptions = {\n host?: string\n port?: string\n secure?: string\n user?: string\n password?: string\n from?: string\n}\n\nexport type EmailDelivery = {\n send(message: EmailMessage): Promise<void>\n verify(): Promise<void>\n}\n\nexport type AuthEmailInput<User extends { email: string } = { email: string }> = {\n user: User\n url: string\n token: string\n}\n\nexport function smtpConfigFromEnvironment(\n environment: NodeJS.ProcessEnv = process.env,\n keys: SmtpEnvironmentOptions = {},\n): SmtpConfig | undefined {\n const host = environment[keys.host ?? 'SMTP_HOST']?.trim()\n const from = environment[keys.from ?? 'EMAIL_FROM']?.trim()\n if (!host && !from) return undefined\n if (!host) throw new Error(`${keys.host ?? 'SMTP_HOST'} is required for SMTP email`)\n if (!from) throw new Error(`${keys.from ?? 'EMAIL_FROM'} is required for SMTP email`)\n\n const portKey = keys.port ?? 'SMTP_PORT'\n const port = Number(environment[portKey] ?? 587)\n if (!Number.isInteger(port) || port < 1 || port > 65_535) throw new Error(`${portKey} must be a valid TCP port`)\n\n const userKey = keys.user ?? 'SMTP_USER'\n const passwordKey = keys.password ?? 'SMTP_PASSWORD'\n const user = environment[userKey]?.trim()\n const password = environment[passwordKey]\n if (Boolean(user) !== Boolean(password)) throw new Error(`${userKey} and ${passwordKey} must be configured together`)\n\n return {\n from,\n host,\n port,\n secure: environment[keys.secure ?? 'SMTP_SECURE']?.trim().toLowerCase() === 'true',\n ...(user && password ? { user, password } : {}),\n }\n}\n\nexport function createSmtpDelivery(config: SmtpConfig): EmailDelivery {\n const timeout = config.timeoutMs\n const transport = createTransport({\n host: config.host,\n port: config.port,\n secure: config.secure,\n servername: config.servername,\n auth: config.user ? { user: config.user, pass: config.password } : undefined,\n ...(timeout === undefined ? {} : { connectionTimeout: timeout, greetingTimeout: timeout, socketTimeout: timeout }),\n })\n return {\n send: async ({ fromName, ...message }) =>\n withinDeadline(\n transport.sendMail({ from: fromName ? { name: fromName, address: senderAddress(config.from) } : config.from, ...message }),\n timeout,\n ),\n verify: async () => withinDeadline(transport.verify(), timeout),\n }\n}\n\n// A server that trickles bytes keeps each socket wait short of its timeout, so the whole exchange needs its own deadline.\nasync function withinDeadline(work: Promise<unknown>, timeoutMs: number | undefined) {\n if (timeoutMs === undefined) return void (await work)\n let timer: ReturnType<typeof setTimeout> | undefined\n const deadline = new Promise<never>((_resolve, reject) => {\n timer = setTimeout(() => reject(new Error(`SMTP server did not finish within ${timeoutMs} ms`)), timeoutMs)\n })\n try {\n await Promise.race([work, deadline])\n } finally {\n clearTimeout(timer)\n }\n}\n\nfunction senderAddress(from: string) {\n return /<([^<>]+)>\\s*$/.exec(from)?.[1] ?? from\n}\n\nexport type StandardAuthEmailOptions = { productName: string }\n\nexport function standardAuthEmails<User extends { email: string } = { email: string }>(\n delivery: EmailDelivery,\n options: StandardAuthEmailOptions,\n) {\n const expiry = 'This link expires in one hour.'\n const send =\n (subject: string, label: string) =>\n async ({ user, url }: AuthEmailInput<User>) =>\n delivery.send({\n to: user.email,\n subject,\n text: `${subject} using this link: ${url}\\n\\n${expiry}`,\n html: `<p>${escapeHtml(subject)} using the link below.</p><p><a href=\"${escapeHtml(url)}\">${label}</a></p><p>${expiry}</p>`,\n })\n return {\n sendResetPassword: send(`Reset your ${options.productName} password`, 'Reset password'),\n sendVerificationEmail: send(`Verify your ${options.productName} email address`, 'Verify email address'),\n }\n}\n\nfunction escapeHtml(value: string) {\n return value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('\"', '"')\n}\n"]}
|