All files / connectors ses.ts

97.14% Statements 34/35
91.66% Branches 11/12
100% Functions 7/7
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                                                    15x   15x     15x       11x   11x 11x 9x     9x 8x   9x 9x   3x               9x 6x   6x 6x               6x 6x   9x               2x 2x 2x 2x 2x 1x   1x               2x 1x 1x                     6x 6x   6x                   1x             6x    
import type { OutgoingMessage, SendConnector, SendResult } from "./types.js";
 
export interface SesConfig {
	/** AWS region (e.g., "us-east-1") */
	region: string;
	/** AWS credentials. If omitted, uses the default credential chain (env vars, instance profile, etc.) */
	credentials?: {
		accessKeyId: string;
		secretAccessKey: string;
	};
}
 
/**
 * Lazy-loaded AWS SES v2 client types.
 * We dynamically import @aws-sdk/client-sesv2 to keep it an optional dependency —
 * users who don't need SES don't need to install the AWS SDK.
 */
interface SesClient {
	send(command: unknown): Promise<unknown>;
	destroy(): void;
}
 
/**
 * SendConnector implementation backed by AWS SES v2.
 *
 * Uses the raw email sending API (SendEmail with Raw content) so we get full
 * control over MIME structure, threading headers, and attachments. The raw
 * message is built with Nodemailer's createTransport({ streamTransport: true })
 * which generates the RFC 5322 message without actually sending it.
 *
 * Requires @aws-sdk/client-sesv2 as a peer dependency. If not installed,
 * construction succeeds but send()/verify() will throw a clear error.
 */
export class SesSendConnector implements SendConnector {
	readonly name = "ses";
	private config: SesConfig;
	private client: SesClient | null = null;
 
	constructor(config: SesConfig) {
		this.config = config;
	}
 
	private async getClient(): Promise<SesClient> {
		Iif (this.client) return this.client;
 
		try {
			const sdk = await import("@aws-sdk/client-sesv2");
			const clientConfig: Record<string, unknown> = {
				region: this.config.region,
			};
			if (this.config.credentials) {
				clientConfig.credentials = this.config.credentials;
			}
			this.client = new sdk.SESv2Client(clientConfig) as unknown as SesClient;
			return this.client;
		} catch {
			throw new Error(
				"@aws-sdk/client-sesv2 is required for the SES connector. " +
					"Install it with: npm install @aws-sdk/client-sesv2",
			);
		}
	}
 
	async send(message: OutgoingMessage): Promise<SendResult> {
		const client = await this.getClient();
		const rawMessage = await buildRawMessage(message);
 
		const sdk = await import("@aws-sdk/client-sesv2");
		const command = new sdk.SendEmailCommand({
			Content: {
				Raw: {
					Data: rawMessage,
				},
			},
		});
 
		const response = (await client.send(command)) as { MessageId?: string };
		const allRecipients = [...message.to, ...(message.cc ?? []), ...(message.bcc ?? [])];
 
		return {
			messageId: response.MessageId ?? "",
			accepted: allRecipients,
			rejected: [],
		};
	}
 
	async verify(): Promise<boolean> {
		try {
			const client = await this.getClient();
			const sdk = await import("@aws-sdk/client-sesv2");
			const command = new sdk.GetAccountCommand({});
			await client.send(command);
			return true;
		} catch {
			return false;
		}
	}
 
	/**
	 * Destroys the underlying SES client, freeing resources.
	 */
	destroy(): void {
		if (this.client) {
			this.client.destroy();
			this.client = null;
		}
	}
}
 
/**
 * Builds a raw RFC 5322 message using Nodemailer's stream transport.
 * This generates the full MIME message (headers, body, attachments)
 * without actually sending it over SMTP.
 */
async function buildRawMessage(message: OutgoingMessage): Promise<Uint8Array> {
	const { createTransport } = await import("nodemailer");
	const transport = createTransport({ streamTransport: true, buffer: true });
 
	const result = await transport.sendMail({
		from: message.from,
		to: message.to.join(", "),
		cc: message.cc?.join(", "),
		bcc: message.bcc?.join(", "),
		subject: message.subject,
		text: message.textBody,
		html: message.htmlBody,
		inReplyTo: message.inReplyTo,
		references: message.references?.join(" "),
		attachments: message.attachments?.map((a) => ({
			filename: a.filename,
			contentType: a.contentType,
			content: a.content,
		})),
	});
 
	return result.message as Uint8Array;
}