sently 0.4.1 → 0.4.4

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +193 -94
  3. package/dist/{chunk-z3eq2t1d.js → chunk-7fqv71z1.js} +15 -8
  4. package/dist/chunk-7fqv71z1.js.map +10 -0
  5. package/dist/{chunk-bvxkmq94.js → chunk-f4c9ttmr.js} +34 -5
  6. package/dist/chunk-f4c9ttmr.js.map +11 -0
  7. package/dist/chunk-mp5c9bfd.js +270 -0
  8. package/dist/chunk-mp5c9bfd.js.map +11 -0
  9. package/dist/{chunk-tjsgb3qb.js → chunk-tymfm441.js} +10 -4
  10. package/dist/{chunk-tjsgb3qb.js.map → chunk-tymfm441.js.map} +3 -3
  11. package/dist/{chunk-j6qw8ms6.js → chunk-x3szga4k.js} +21 -7
  12. package/dist/chunk-x3szga4k.js.map +11 -0
  13. package/dist/core/address.d.ts +30 -0
  14. package/dist/core/smtp.js +32 -0
  15. package/dist/{index.js.map → core/smtp.js.map} +1 -1
  16. package/dist/core/types.d.ts +7 -0
  17. package/dist/detect.js +181 -0
  18. package/dist/detect.js.map +11 -0
  19. package/dist/index.js +14 -29
  20. package/dist/pool/pool.js +8 -268
  21. package/dist/pool/pool.js.map +3 -5
  22. package/dist/transports/brevo.js +1 -1
  23. package/dist/transports/mailgun.js +1 -1
  24. package/dist/transports/postmark.js +1 -1
  25. package/dist/transports/preview.js +2 -2
  26. package/dist/transports/resend.js +1 -1
  27. package/dist/transports/resolve-attachments.d.ts +10 -2
  28. package/dist/transports/retry.js +1 -1
  29. package/dist/transports/sendgrid.js +1 -1
  30. package/dist/transports/ses.js +4 -4
  31. package/dist/transports/ses.js.map +2 -2
  32. package/dist/transports/smtp.d.ts +1 -0
  33. package/dist/transports/smtp.js +6 -6
  34. package/dist/transports/smtp.js.map +1 -1
  35. package/package.json +16 -3
  36. package/dist/chunk-bvxkmq94.js.map +0 -11
  37. package/dist/chunk-j6qw8ms6.js.map +0 -11
  38. package/dist/chunk-z3eq2t1d.js.map +0 -10
package/CHANGELOG.md CHANGED
@@ -1,5 +1,60 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.4] — 2026-05-30
4
+
5
+ ### Security
6
+
7
+ - Fixed SigV4 date format: slice(0,15) not slice(0,16) — all real
8
+ SES requests were producing malformed x-amz-date headers
9
+ - Added requireTLS guard before SMTP AUTH (default: true when auth
10
+ is set) — prevents credential exposure on STARTTLS-stripping attacks
11
+ - Hardened email address validation against header and SMTP command
12
+ injection, enforced centrally in `parseAddresses()` (and re-asserted
13
+ at render time in `toMIMEHeader()`):
14
+ - Rejects CR, LF, NUL, all other C0 control characters (0x00–0x1F),
15
+ DEL (0x7F), and the Unicode line/paragraph separators U+2028/U+2029
16
+ - Fails closed: hostile input throws a clear error with the offending
17
+ code point instead of being accepted
18
+ - No repair or normalization of malicious input — addresses are never
19
+ transformed (e.g. CR/LF stripped) and then accepted
20
+ - Protects the display name as well as the address; an ASCII display
21
+ name such as `"Foo\r\nBcc: ..."` can no longer inject a header
22
+ - Enforced consistently across every address field (From, To, Cc, Bcc,
23
+ Reply-To) and every transport (SMTP, SES, Mailgun, Postmark, Resend,
24
+ SendGrid, Brevo), since all of them route through `parseAddresses()`
25
+ - Sanitized attachment filenames and custom attachment headers
26
+ against MIME header injection
27
+ - Fixed basePath startsWith sibling-directory bypass in
28
+ resolve-attachments (now appends path separator before comparison)
29
+ - Added CRLF guard on EHLO domain for consistency
30
+
31
+ ## [0.4.3] — 2026-05-30
32
+
33
+ ### Added
34
+
35
+ - `llms.txt` for LLM/agent discovery (install, quick example, subpath
36
+ exports, and when-to-use guidance)
37
+ - README: Nodemailer comparison table, 30-second tour, error handling,
38
+ and TypeScript sections
39
+ - `CLAUDE.md` repository map for agents (core entry, adapters,
40
+ transports, tests, build)
41
+
42
+ ### Changed
43
+
44
+ - README positioning, HTTP transport reference table, plugin docs
45
+ reorder, and tree-shaking callout
46
+ - `package.json` description and npm keywords for discoverability
47
+
48
+ ## [0.4.2] — 2026-05-30
49
+
50
+ ### Fixed
51
+
52
+ - CI: replaced node -e dynamic import with scripts/smoke.mjs
53
+ (top-level await ESM) for reliable Node.js smoke testing
54
+ - CI: integration test now imports from dist/index.js directly
55
+ instead of relying on package self-reference resolution
56
+ - CI: updated GitHub Actions to latest patch versions
57
+
3
58
  ## [0.4.1] — 2026-05-30
4
59
 
5
60
  ### Fixed
package/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # sently
2
2
 
3
- **Runtime-agnostic email library for Node.js, Bun, Deno, and Cloudflare Workers.**
3
+ > Nodemailer hasn't been updated in years, doesn't run on Bun or Deno, and ships at 220KB.
4
+ > sently is the modern replacement — same familiar API, runs everywhere, tree-shakes to ~6KB.
5
+
6
+ ```bash
7
+ bun add sently
8
+ ```
4
9
 
5
10
  [![npm version](https://img.shields.io/npm/v/sently.svg)](https://www.npmjs.com/package/sently)
6
11
  [![JSR](https://jsr.io/badges/@alialnaghmoush/sently)](https://jsr.io/@alialnaghmoush/sently)
@@ -11,17 +16,62 @@
11
16
 
12
17
  ---
13
18
 
14
- ## Why sently
19
+ ## Why not Nodemailer?
20
+
21
+ | Feature | Nodemailer | sently |
22
+ |---------|-----------|--------|
23
+ | Bundle size | ~220 KB | ~6 KB core |
24
+ | Runtimes | Node.js only | Node, Bun, Deno, CF Workers |
25
+ | Module format | CommonJS | ESM only |
26
+ | Dependencies | 3 | 0 |
27
+ | DKIM signing | ✓ via `nodemailer-dkim` | ✓ built-in (Web Crypto) |
28
+ | OAuth2 / XOAUTH2 | ✓ via plugin | ✓ built-in |
29
+ | Connection pooling | ✓ | ✓ |
30
+ | HTTP transports | ✓ via plugins | ✓ built-in (6 providers) |
31
+ | Retry transport | ✗ | ✓ |
32
+ | Preview transport | ✗ | ✓ |
33
+ | Template engine | ✗ | ✓ |
34
+ | `sendBulk()` | ✗ | ✓ |
35
+ | TypeScript | via `@types/nodemailer` | ✓ built-in |
36
+ | Last release | 2021 | 2026 |
37
+
38
+ ---
39
+
40
+ ## The 30-second tour
41
+
42
+ ```typescript
43
+ import { createMailer, type MailOptions } from "sently";
44
+ import { ResendTransport } from "sently/transports/resend";
45
+ import { PreviewTransport } from "sently/transports/preview";
46
+
47
+ const addFooter = (options: MailOptions): MailOptions => ({
48
+ ...options,
49
+ html: (options.html ?? "") + '<p style="color:#999">Unsubscribe</p>',
50
+ });
51
+
52
+ // Swap providers without changing send code
53
+ const mailer = await createMailer({
54
+ transport: new ResendTransport({ apiKey: process.env.RESEND_API_KEY! }),
55
+ plugins: [addFooter],
56
+ });
57
+
58
+ await mailer.send({
59
+ from: "you@example.com",
60
+ to: "recipient@example.com",
61
+ subject: "Hello from sently",
62
+ html: "<p>Hello!</p>",
63
+ });
64
+
65
+ // Bulk send with concurrency control
66
+ await mailer.sendBulk(recipients, { concurrency: 5 });
15
67
 
16
- - **Works everywhere**Node.js, Bun, Deno, Cloudflare Workers, and any environment with Web APIs
17
- - **True tree-shaking** import only what you need; unused adapters and transports stay out of your bundle
18
- - **Zero dependencies in core** — MIME, SMTP protocol, and encoding use pure Web APIs only
19
- - **Plugin system** transform `MailOptions` before send with composable middleware
20
- - **HTTP transports** Resend, SendGrid, Postmark, Mailgun, AWS SES, and Brevo
21
- - **DKIM signing** — RSA-SHA256 and Ed25519-SHA256 via Web Crypto
22
- - **OAuth2 / XOAUTH2** — Gmail and Microsoft 365 SMTP auth with automatic token refresh
23
- - **Connection pooling** — reuse SMTP sessions with optional rate limiting
24
- - **TypeScript-first** — strict types, subpath exports, and full IDE support
68
+ // Local devwrite to disk instead of sending
69
+ const devMailer = await createMailer({
70
+ transport: process.env.CI
71
+ ? new ResendTransport({ apiKey: process.env.RESEND_API_KEY! })
72
+ : new PreviewTransport({ outDir: ".emails", open: true }),
73
+ });
74
+ ```
25
75
 
26
76
  ---
27
77
 
@@ -164,6 +214,8 @@ await mailer.verify(); // test connection + auth
164
214
 
165
215
  **AUTH methods:** XOAUTH2, CRAM-MD5, LOGIN, and PLAIN (auto-negotiated from EHLO unless `auth.type` is set).
166
216
 
217
+ **`requireTLS` (default `true` when `auth` is set):** sently refuses to send credentials over an unencrypted connection. If the link is not secured by direct TLS (`secure: true`) or a successful `STARTTLS` upgrade, authentication throws an `SMTPError` instead of leaking credentials — this defends against STARTTLS-stripping MITM attacks. Set `requireTLS: false` only if you fully trust the network (not recommended).
218
+
167
219
  #### DKIM signing
168
220
 
169
221
  ```typescript
@@ -228,63 +280,19 @@ const pool = new SMTPPool({
228
280
 
229
281
  ### HTTP APIs
230
282
 
231
- #### Resend
232
-
233
- ```typescript
234
- import { ResendTransport } from "sently/transports/resend";
235
-
236
- const transport = new ResendTransport({ apiKey: "re_..." });
237
- ```
238
-
239
- #### SendGrid
283
+ | Transport | Import path | Required config |
284
+ |-----------|-------------|-----------------|
285
+ | Resend | `sently/transports/resend` | `apiKey` |
286
+ | SendGrid | `sently/transports/sendgrid` | `apiKey` |
287
+ | Postmark | `sently/transports/postmark` | `serverToken` |
288
+ | Mailgun | `sently/transports/mailgun` | `apiKey`, `domain` |
289
+ | AWS SES | `sently/transports/ses` | `accessKeyId`, `secretAccessKey`, `region` |
290
+ | Brevo | `sently/transports/brevo` | `apiKey` |
240
291
 
241
- ```typescript
242
- import { SendGridTransport } from "sently/transports/sendgrid";
243
-
244
- const transport = new SendGridTransport({ apiKey: "SG...." });
245
- ```
246
-
247
- #### Postmark
248
-
249
- ```typescript
250
- import { PostmarkTransport } from "sently/transports/postmark";
251
-
252
- const transport = new PostmarkTransport({ serverToken: "..." });
253
- ```
254
-
255
- #### Mailgun
256
-
257
- ```typescript
258
- import { MailgunTransport } from "sently/transports/mailgun";
259
-
260
- const transport = new MailgunTransport({
261
- apiKey: "key-...",
262
- domain: "mg.example.com",
263
- });
264
- ```
265
-
266
- #### AWS SES
267
-
268
- ```typescript
269
- import { SESTransport } from "sently/transports/ses";
270
-
271
- const transport = new SESTransport({
272
- accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
273
- secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
274
- region: "us-east-1",
275
- });
276
- ```
292
+ All transports implement the same interface — swap without changing your send code.
277
293
 
278
294
  Messages with attachments are sent as raw MIME (`Content.Raw`); simple messages use `Content.Simple`.
279
295
 
280
- #### Brevo
281
-
282
- ```typescript
283
- import { BrevoTransport } from "sently/transports/brevo";
284
-
285
- const transport = new BrevoTransport({ apiKey: "xkeysib-..." });
286
- ```
287
-
288
296
  ### PreviewTransport
289
297
 
290
298
  Write emails to disk during local development instead of sending them:
@@ -346,6 +354,38 @@ const result = await mailer.sendBulk(
346
354
  console.log(result.sent, result.failed);
347
355
  ```
348
356
 
357
+ ---
358
+
359
+ ## Plugin system
360
+
361
+ Plugins transform `MailOptions` before the transport builds and sends the message. They run sequentially — each receives the output of the previous plugin.
362
+
363
+ ```typescript
364
+ import { createMailer, type MailOptions } from "sently";
365
+
366
+ const addFooter = (options: MailOptions) => ({
367
+ ...options,
368
+ html: (options.html ?? "") + '<p style="color:#999">Unsubscribe</p>',
369
+ });
370
+
371
+ const mailer = await createMailer({
372
+ host: "smtp.resend.com",
373
+ port: 465,
374
+ secure: true,
375
+ auth: { user: "resend", pass: process.env.RESEND_API_KEY! },
376
+ plugins: [addFooter],
377
+ });
378
+ ```
379
+
380
+ Works with SMTP config or custom transports:
381
+
382
+ ```typescript
383
+ const mailer = await createMailer({
384
+ transport: new ResendTransport({ apiKey: "re_..." }),
385
+ plugins: [addFooter],
386
+ });
387
+ ```
388
+
349
389
  ### TemplatePlugin
350
390
 
351
391
  Render HTML from named templates with zero dependencies:
@@ -378,36 +418,6 @@ await mailer.send({
378
418
 
379
419
  Use a custom engine by passing any `(template, data) => string` function to `templatePlugin`.
380
420
 
381
- ### Plugin system
382
-
383
- Plugins transform `MailOptions` before the transport builds and sends the message. They run sequentially — each receives the output of the previous plugin.
384
-
385
- ```typescript
386
- import { createMailer, type MailOptions } from "sently";
387
-
388
- const addFooter = (options: MailOptions) => ({
389
- ...options,
390
- html: (options.html ?? "") + '<p style="color:#999">Unsubscribe</p>',
391
- });
392
-
393
- const mailer = await createMailer({
394
- host: "smtp.resend.com",
395
- port: 465,
396
- secure: true,
397
- auth: { user: "resend", pass: process.env.RESEND_API_KEY! },
398
- plugins: [addFooter],
399
- });
400
- ```
401
-
402
- Works with SMTP config or custom transports:
403
-
404
- ```typescript
405
- const mailer = await createMailer({
406
- transport: new ResendTransport({ apiKey: "re_..." }),
407
- plugins: [addFooter],
408
- });
409
- ```
410
-
411
421
  ---
412
422
 
413
423
  ## MailOptions Reference
@@ -469,6 +479,77 @@ On Cloudflare Workers and browsers, use `content: Uint8Array` — `attachment.pa
469
479
 
470
480
  ---
471
481
 
482
+ ## Error Handling
483
+
484
+ ```typescript
485
+ import { SMTPError } from "sently";
486
+ import { ResendError } from "sently/transports/resend";
487
+ // Each HTTP transport exports its own error class:
488
+ // SendGridError → sently/transports/sendgrid
489
+ // PostmarkError → sently/transports/postmark
490
+ // MailgunError → sently/transports/mailgun
491
+ // SESError → sently/transports/ses
492
+ // BrevoError → sently/transports/brevo
493
+
494
+ try {
495
+ await mailer.send({ ... });
496
+ } catch (err) {
497
+ if (err instanceof SMTPError) {
498
+ console.error(err.code); // SMTP response code, e.g. 550
499
+ console.error(err.command); // failed command, e.g. "RCPT TO"
500
+ }
501
+ if (err instanceof ResendError) {
502
+ console.error(err.statusCode); // HTTP status code
503
+ }
504
+ }
505
+ ```
506
+
507
+ Import error classes from their transport subpath — not from `sently` core. Each exports a `statusCode` property on HTTP failures.
508
+
509
+ ---
510
+
511
+ ## Security
512
+
513
+ sently is built to be secure by default — protections are enforced at the library's core chokepoints, so they apply to every transport and every address field without any extra configuration.
514
+
515
+ ### Email header & SMTP command injection
516
+
517
+ All addresses **and** display names are validated centrally in `parseAddresses()` (and re-asserted when rendering headers), before any normalization:
518
+
519
+ - Rejects CR, LF, NUL, every other C0 control (`0x00`–`0x1F`), DEL (`0x7F`), and the Unicode line/paragraph separators `U+2028`/`U+2029`.
520
+ - **Fails closed:** hostile input throws a clear error (with the offending code point) — it is never stripped, repaired, and then accepted.
521
+ - Protects the **display name** too, so an ASCII name like `"Foo\r\nBcc: attacker@evil.com"` can no longer inject a header.
522
+ - Enforced consistently across **From, To, Cc, Bcc, and Reply-To**, and across **every transport** (SMTP, SES, Mailgun, Postmark, Resend, SendGrid, Brevo).
523
+
524
+ ```typescript
525
+ await mailer.send({
526
+ from: "you@example.com",
527
+ to: { address: "victim@x.com\r\nBcc: attacker@evil.com" },
528
+ subject: "Hi",
529
+ text: "...",
530
+ });
531
+ // → throws: Email address contains a forbidden control character (0x0d)
532
+ ```
533
+
534
+ MIME attachment filenames and custom attachment headers are likewise sanitized against header injection.
535
+
536
+ ### Credential protection
537
+
538
+ - **`requireTLS`** (default `true` when `auth` is set) refuses to authenticate over a cleartext connection, defeating STARTTLS-stripping downgrade attacks.
539
+ - **OAuth2 / XOAUTH2** and DKIM signing are built in via Web Crypto — no plaintext secrets in transit beyond what the protocol requires.
540
+
541
+ ### Attachments
542
+
543
+ > ⚠️ `attachment.path` reads files from disk. Never pass user-controlled paths without validation.
544
+
545
+ `resolveAttachments()` accepts an opt-in `basePath` that confines reads to an allowed directory and rejects path-traversal (including sibling-directory prefix tricks like `/var/data-secret` vs `/var/data`). Note: `basePath` does not dereference symlinks — use `fs.realpath()` first if symlink traversal is a concern.
546
+
547
+ ### Supply chain
548
+
549
+ **Zero runtime dependencies** — there is no transitive dependency tree to audit or to be compromised.
550
+
551
+ ---
552
+
472
553
  ## Tree-Shaking
473
554
 
474
555
  Each import path is a separate build entry point:
@@ -518,6 +599,24 @@ Approximate gzip sizes per subpath export:
518
599
  | `sently/adapters/deno` | ~2 KB |
519
600
  | `sently/adapters/cf` | ~2 KB |
520
601
 
602
+ > **Example:** Resend only = core (~6 KB) + transport (~2 KB) = **~8 KB total**. Nodemailer ships 220 KB regardless of which transport you use.
603
+
604
+ ---
605
+
606
+ ## TypeScript
607
+
608
+ ```typescript
609
+ import type {
610
+ MailOptions,
611
+ MailPlugin,
612
+ SendResult,
613
+ Attachment,
614
+ SMTPConfig,
615
+ } from "sently";
616
+ ```
617
+
618
+ All types ship with the package — no separate `@types/` install needed.
619
+
521
620
  ---
522
621
 
523
622
  ## Links
@@ -1,6 +1,9 @@
1
+ import {
2
+ OAuth2Client
3
+ } from "./chunk-ym3zzv8b.js";
1
4
  import {
2
5
  buildMIME
3
- } from "./chunk-j6qw8ms6.js";
6
+ } from "./chunk-x3szga4k.js";
4
7
  import {
5
8
  SMTPError,
6
9
  accumulateResponse,
@@ -13,13 +16,10 @@ import {
13
16
  parseEHLO,
14
17
  parseResponse,
15
18
  selectAuthMethod
16
- } from "./chunk-tjsgb3qb.js";
17
- import {
18
- OAuth2Client
19
- } from "./chunk-ym3zzv8b.js";
19
+ } from "./chunk-tymfm441.js";
20
20
  import {
21
21
  resolveAttachments
22
- } from "./chunk-bvxkmq94.js";
22
+ } from "./chunk-f4c9ttmr.js";
23
23
  import {
24
24
  __require
25
25
  } from "./chunk-v0bahtg2.js";
@@ -87,6 +87,7 @@ function resolveSMTPConfig(config) {
87
87
  port: config.port ?? (secure ? 465 : 587),
88
88
  secure,
89
89
  ...config.auth !== undefined ? { auth: config.auth } : {},
90
+ ...config.requireTLS !== undefined ? { requireTLS: config.requireTLS } : {},
90
91
  ...config.dkim !== undefined ? { dkim: config.dkim } : {},
91
92
  ...config.tls !== undefined ? { tls: config.tls } : {},
92
93
  ...config.connectionTimeout !== undefined ? { connectionTimeout: config.connectionTimeout } : {},
@@ -100,7 +101,8 @@ async function openSMTPSession(adapter, config) {
100
101
  const greeting = await readSMTPResponse(adapter);
101
102
  assertResponse(greeting, [220], "greeting");
102
103
  let capabilities = await ehlo(adapter, config.host);
103
- if (!config.secure && !adapter.secure) {
104
+ const supportsStartTls = capabilities.some((cap) => cap.toUpperCase() === "STARTTLS");
105
+ if (!config.secure && !adapter.secure && supportsStartTls) {
104
106
  await sendRaw(adapter, encodeCommand({ type: "STARTTLS" }));
105
107
  const starttlsResp = await readSMTPResponse(adapter);
106
108
  assertResponse(starttlsResp, [220], "STARTTLS");
@@ -108,6 +110,11 @@ async function openSMTPSession(adapter, config) {
108
110
  capabilities = await ehlo(adapter, config.host);
109
111
  }
110
112
  if (config.auth) {
113
+ const tlsRequired = config.requireTLS ?? true;
114
+ const encrypted = adapter.secure || config.secure;
115
+ if (tlsRequired && !encrypted) {
116
+ throw new SMTPError("Refusing to authenticate over unencrypted connection. " + "Set requireTLS: false to disable this check (not recommended).", 0, "AUTH", "");
117
+ }
111
118
  await authenticate(adapter, config.auth, capabilities);
112
119
  }
113
120
  }
@@ -241,4 +248,4 @@ async function resolveMX(domain) {
241
248
  }
242
249
  export { SMTPTransport, resolveSMTPConfig, openSMTPSession, deliverSMTPMessage, closeSMTPSession, readSMTPResponse };
243
250
 
244
- //# debugId=58BDA0CC5C272AF664756E2164756E21
251
+ //# debugId=736A9106E3164FC464756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/transports/smtp.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * @module\n * SMTP transport — orchestrates socket adapter, MIME builder, and protocol logic.\n *\n * @example\n * ```ts\n * import { SMTPTransport } from \"sently/transports/smtp\";\n * import { NodeAdapter } from \"sently/adapters/node\";\n * import { createMailer } from \"sently\";\n *\n * const mailer = await createMailer({\n * transport: new SMTPTransport({\n * host: \"smtp.example.com\",\n * auth: { user: \"you@example.com\", pass: \"secret\" },\n * adapter: new NodeAdapter(),\n * }),\n * });\n * ```\n */\nimport { OAuth2Client } from \"../auth/oauth2.js\";\nimport { buildMIME, type MIMEBuildResult } from \"../core/mime.js\";\nimport type { SMTPResponse } from \"../core/smtp.js\";\nimport {\n accumulateResponse,\n assertResponse,\n computeCRAMMD5,\n encodeAuthLoginPass,\n encodeAuthLoginUser,\n encodeCommand,\n encodeLine,\n parseEHLO,\n parseResponse,\n SMTPError,\n selectAuthMethod,\n} from \"../core/smtp.js\";\nimport type {\n MailOptions,\n SendResult,\n SMTPConfig,\n SocketAdapter,\n Transport,\n VerifyResult,\n} from \"../core/types.js\";\nimport { resolveAttachments } from \"./resolve-attachments.js\";\n\n/**\n * SMTP transport orchestrating adapter, MIME builder, and protocol logic.\n */\nexport class SMTPTransport implements Transport {\n private readonly config: ResolvedSMTPConfig;\n private adapter: SocketAdapter | null = null;\n\n /** Creates an SMTP transport with the given configuration. */\n constructor(config: SMTPConfig) {\n this.config = resolveSMTPConfig(config);\n }\n\n /** Sends an email via SMTP using the configured adapter. */\n async send(options: MailOptions): Promise<SendResult> {\n const resolvedOptions = {\n ...options,\n attachments: await resolveAttachments(options.attachments),\n };\n const mime = await buildMIME(resolvedOptions, this.config.dkim);\n const adapter = await this.getAdapter();\n\n const host = this.config.direct\n ? await resolveMX(mime.envelope.from.split(\"@\")[1] ?? this.config.host)\n : this.config.host;\n\n await adapter.connect(host, this.config.port);\n this.adapter = adapter;\n\n try {\n await openSMTPSession(adapter, this.config);\n return await deliverSMTPMessage(adapter, mime);\n } finally {\n await closeSMTPSession(adapter);\n this.adapter = null;\n }\n }\n\n /** Verifies SMTP connectivity and authentication without sending mail. */\n async verify(): Promise<VerifyResult> {\n try {\n const adapter = await this.getAdapter();\n await adapter.connect(this.config.host, this.config.port);\n\n try {\n await openSMTPSession(adapter, this.config);\n return { ok: true, provider: \"smtp\" };\n } finally {\n await closeSMTPSession(adapter);\n }\n } catch (err) {\n return {\n ok: false,\n provider: \"smtp\",\n message: err instanceof Error ? err.message : String(err),\n };\n }\n }\n\n /** Closes the underlying socket adapter if connected. */\n async close(): Promise<void> {\n if (this.adapter) {\n await this.adapter.close();\n this.adapter = null;\n }\n }\n\n private async getAdapter(): Promise<SocketAdapter> {\n if (!this.config.adapter) {\n throw new SMTPError(\"No socket adapter configured\", 0, \"CONNECT\", \"\");\n }\n return this.config.adapter;\n }\n}\n\n/** Resolved SMTP transport configuration with defaults applied. */\nexport interface ResolvedSMTPConfig {\n host: string;\n port: number;\n secure: boolean;\n auth?: SMTPConfig[\"auth\"];\n requireTLS?: boolean;\n tls?: SMTPConfig[\"tls\"];\n dkim?: SMTPConfig[\"dkim\"];\n connectionTimeout?: number;\n greetingTimeout?: number;\n socketTimeout?: number;\n direct?: boolean;\n adapter?: SocketAdapter;\n}\n\n/** Apply defaults to SMTP configuration. */\nexport function resolveSMTPConfig(config: SMTPConfig): ResolvedSMTPConfig {\n const secure = config.secure ?? false;\n return {\n host: config.host,\n port: config.port ?? (secure ? 465 : 587),\n secure,\n ...(config.auth !== undefined ? { auth: config.auth } : {}),\n ...(config.requireTLS !== undefined ? { requireTLS: config.requireTLS } : {}),\n ...(config.dkim !== undefined ? { dkim: config.dkim } : {}),\n ...(config.tls !== undefined ? { tls: config.tls } : {}),\n ...(config.connectionTimeout !== undefined\n ? { connectionTimeout: config.connectionTimeout }\n : {}),\n ...(config.greetingTimeout !== undefined ? { greetingTimeout: config.greetingTimeout } : {}),\n ...(config.socketTimeout !== undefined ? { socketTimeout: config.socketTimeout } : {}),\n ...(config.direct !== undefined ? { direct: config.direct } : {}),\n ...(config.adapter !== undefined ? { adapter: config.adapter } : {}),\n };\n}\n\n/**\n * Connect greeting, EHLO, optional STARTTLS, and AUTH on an open adapter.\n */\nexport async function openSMTPSession(\n adapter: SocketAdapter,\n config: ResolvedSMTPConfig,\n): Promise<void> {\n const greeting = await readSMTPResponse(adapter);\n assertResponse(greeting, [220], \"greeting\");\n\n let capabilities = await ehlo(adapter, config.host);\n const supportsStartTls = capabilities.some((cap) => cap.toUpperCase() === \"STARTTLS\");\n if (!config.secure && !adapter.secure && supportsStartTls) {\n await sendRaw(adapter, encodeCommand({ type: \"STARTTLS\" }));\n const starttlsResp = await readSMTPResponse(adapter);\n assertResponse(starttlsResp, [220], \"STARTTLS\");\n await adapter.startTLS(config.tls);\n capabilities = await ehlo(adapter, config.host);\n }\n\n if (config.auth) {\n const tlsRequired = config.requireTLS ?? true;\n const encrypted = adapter.secure || config.secure;\n if (tlsRequired && !encrypted) {\n throw new SMTPError(\n \"Refusing to authenticate over unencrypted connection. \" +\n \"Set requireTLS: false to disable this check (not recommended).\",\n 0,\n \"AUTH\",\n \"\",\n );\n }\n await authenticate(adapter, config.auth, capabilities);\n }\n}\n\n/**\n * MAIL FROM, RCPT TO, and DATA for a built MIME message on an authenticated session.\n */\nexport async function deliverSMTPMessage(\n adapter: SocketAdapter,\n mime: MIMEBuildResult,\n): Promise<SendResult> {\n await sendCommand(adapter, { type: \"MAIL_FROM\", address: mime.envelope.from });\n const mailResp = await readSMTPResponse(adapter);\n assertResponse(mailResp, [250], \"MAIL FROM\");\n\n const accepted: string[] = [];\n const rejected: string[] = [];\n\n for (const recipient of mime.envelope.to) {\n await sendRaw(adapter, encodeCommand({ type: \"RCPT_TO\", address: recipient }));\n const rcptResp = await readSMTPResponse(adapter);\n if (rcptResp.isSuccess) {\n accepted.push(recipient);\n } else {\n rejected.push(recipient);\n }\n }\n\n await sendCommand(adapter, { type: \"DATA\" });\n const dataResp = await readSMTPResponse(adapter);\n assertResponse(dataResp, [354], \"DATA\");\n\n let finalResp: SMTPResponse;\n try {\n await sendRaw(adapter, encodeCommand({ type: \"DATA_BODY\", content: mime.raw }));\n finalResp = await readSMTPResponse(adapter);\n } catch (err) {\n await sendRaw(adapter, encodeCommand({ type: \"DATA_BODY\", content: mime.raw }));\n finalResp = await readSMTPResponse(adapter);\n if (finalResp.isError) {\n throw err;\n }\n }\n assertResponse(finalResp, [250], \"DATA end\");\n\n return {\n messageId: mime.messageId,\n accepted,\n rejected,\n response: finalResp.message,\n envelope: mime.envelope,\n };\n}\n\n/**\n * QUIT and close an SMTP session adapter.\n */\nexport async function closeSMTPSession(adapter: SocketAdapter): Promise<void> {\n try {\n await sendCommand(adapter, { type: \"QUIT\" });\n await readSMTPResponse(adapter);\n } catch {\n // ignore errors during shutdown\n } finally {\n await adapter.close();\n }\n}\n\nasync function ehlo(adapter: SocketAdapter, host: string): Promise<string[]> {\n await sendCommand(adapter, { type: \"EHLO\", domain: host });\n const response = await readSMTPResponse(adapter);\n assertResponse(response, [250], \"EHLO\");\n return parseEHLO(response);\n}\n\nasync function authenticate(\n adapter: SocketAdapter,\n auth: NonNullable<SMTPConfig[\"auth\"]>,\n capabilities: string[],\n): Promise<void> {\n if (auth.type === \"OAUTH2\" && auth.oauth2) {\n const client = new OAuth2Client(auth.oauth2);\n const xoauth2 = await client.buildXOAUTH2();\n await sendCommand(adapter, { type: \"AUTH_XOAUTH2\", xoauth2String: xoauth2 });\n let resp = await readSMTPResponse(adapter);\n if (resp.code === 334) {\n await sendRaw(adapter, encodeLine(\"\"));\n resp = await readSMTPResponse(adapter);\n }\n assertResponse(resp, [235], \"AUTH XOAUTH2\");\n return;\n }\n\n const method = auth.type ?? selectAuthMethod(capabilities);\n\n if (method === \"CRAM-MD5\") {\n const pass = requirePassword(auth, \"CRAM-MD5\");\n await sendCommand(adapter, { type: \"AUTH_CRAM_MD5_INIT\" });\n let resp = await readSMTPResponse(adapter);\n assertResponse(resp, [334], \"AUTH CRAM-MD5\");\n const challenge = resp.message.trim();\n const response = await computeCRAMMD5(challenge, auth.user, pass);\n await sendCommand(adapter, { type: \"AUTH_CRAM_MD5_RESPONSE\", response });\n resp = await readSMTPResponse(adapter);\n assertResponse(resp, [235], \"AUTH CRAM-MD5 response\");\n return;\n }\n\n if (method === \"PLAIN\") {\n const pass = requirePassword(auth, \"PLAIN\");\n await sendRaw(adapter, encodeCommand({ type: \"AUTH_PLAIN\", user: auth.user, pass }));\n const resp = await readSMTPResponse(adapter);\n assertResponse(resp, [235], \"AUTH PLAIN\");\n return;\n }\n\n const pass = requirePassword(auth, \"LOGIN\");\n await sendRaw(adapter, encodeCommand({ type: \"AUTH_LOGIN\", user: auth.user, pass }));\n let resp = await readSMTPResponse(adapter);\n assertResponse(resp, [334], \"AUTH LOGIN\");\n\n await sendRaw(adapter, encodeAuthLoginUser(auth.user));\n resp = await readSMTPResponse(adapter);\n assertResponse(resp, [334], \"AUTH LOGIN user\");\n\n await sendRaw(adapter, encodeAuthLoginPass(pass));\n resp = await readSMTPResponse(adapter);\n assertResponse(resp, [235], \"AUTH LOGIN pass\");\n}\n\nfunction requirePassword(auth: NonNullable<SMTPConfig[\"auth\"]>, method: string): string {\n if (!auth.pass) {\n throw new SMTPError(`Password required for ${method} authentication`, 0, `AUTH ${method}`, \"\");\n }\n return auth.pass;\n}\n\nasync function sendCommand(\n adapter: SocketAdapter,\n command: Parameters<typeof encodeCommand>[0],\n): Promise<void> {\n await sendRaw(adapter, encodeCommand(command));\n}\n\nasync function sendRaw(adapter: SocketAdapter, data: Uint8Array): Promise<void> {\n await adapter.write(data);\n}\n\n/** Reads and parses a complete SMTP response from the adapter. */\nasync function readSMTPResponse(adapter: SocketAdapter): Promise<SMTPResponse> {\n const chunks: Uint8Array[] = [];\n for await (const chunk of adapter.read()) {\n chunks.push(chunk);\n const complete = accumulateResponse(chunks);\n if (complete) {\n return parseResponse(complete);\n }\n }\n throw new SMTPError(\"Connection closed while reading SMTP response\", 0, \"READ\", \"\");\n}\n\nasync function resolveMX(domain: string): Promise<string> {\n const dns = await import(\"node:dns/promises\");\n const records = await dns.resolveMx(domain);\n if (records.length === 0) {\n throw new SMTPError(`No MX records for ${domain}`, 0, \"MX\", \"\");\n }\n records.sort((a: { priority: number }, b: { priority: number }) => a.priority - b.priority);\n return records[0]?.exchange ?? domain;\n}\n\n/** @internal Test helper for raw line writes. */\nexport { encodeLine, readSMTPResponse };\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDO,MAAM,cAAmC;AAAA,EAC7B;AAAA,EACT,UAAgC;AAAA,EAGxC,WAAW,CAAC,QAAoB;AAAA,IAC9B,KAAK,SAAS,kBAAkB,MAAM;AAAA;AAAA,OAIlC,KAAI,CAAC,SAA2C;AAAA,IACpD,MAAM,kBAAkB;AAAA,SACnB;AAAA,MACH,aAAa,MAAM,mBAAmB,QAAQ,WAAW;AA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WAAW,GAAG;AAAA,IACxB,MAAM,IAAI,UAAU,qBAAqB,UAAU,GAAG,MAAM,EAAE;AAAA,EAChE;AAAA,EACA,QAAQ,KAAK,CAAC,GAAyB,MAA4B,EAAE,WAAW,EAAE,QAAQ;AAAA,EAC1F,OAAO,QAAQ,IAAI,YAAY;AAAA;",
8
+ "debugId": "736A9106E3164FC464756E2164756E21",
9
+ "names": []
10
+ }
@@ -6,13 +6,34 @@ import {
6
6
  } from "./chunk-v0bahtg2.js";
7
7
 
8
8
  // src/core/address.ts
9
+ function findForbiddenChar(value) {
10
+ for (let i = 0;i < value.length; i++) {
11
+ const code = value.charCodeAt(i);
12
+ if (code <= 31 || code === 127 || code === 8232 || code === 8233) {
13
+ return code;
14
+ }
15
+ }
16
+ return -1;
17
+ }
18
+ function assertSafeAddress(value, label = "address") {
19
+ const code = findForbiddenChar(value);
20
+ if (code !== -1) {
21
+ const hex = code.toString(16).padStart(2, "0");
22
+ throw new Error(`Email ${label} contains a forbidden control character (0x${hex}). ` + "CR, LF, NUL, and other control characters are not allowed.");
23
+ }
24
+ }
9
25
  function parseAddresses(input) {
10
26
  if (Array.isArray(input)) {
11
27
  return input.flatMap((item) => parseAddresses(item));
12
28
  }
13
29
  if (typeof input === "object") {
30
+ assertSafeAddress(input.address, "address");
31
+ if (input.name !== undefined) {
32
+ assertSafeAddress(input.name, "display name");
33
+ }
14
34
  return [{ ...input }];
15
35
  }
36
+ assertSafeAddress(input, "address");
16
37
  const trimmed = input.trim();
17
38
  if (!trimmed) {
18
39
  return [];
@@ -20,7 +41,9 @@ function parseAddresses(input) {
20
41
  return splitAddressList(trimmed).map(parseSingleAddress);
21
42
  }
22
43
  function toMIMEHeader(address) {
44
+ assertSafeAddress(address.address, "address");
23
45
  if (address.name) {
46
+ assertSafeAddress(address.name, "display name");
24
47
  const name = encodeHeader(address.name);
25
48
  return `${name} <${address.address}>`;
26
49
  }
@@ -29,6 +52,11 @@ function toMIMEHeader(address) {
29
52
  function extractEmails(input) {
30
53
  return parseAddresses(input).map((addr) => addr.address);
31
54
  }
55
+ function isValidEmail(email) {
56
+ if (findForbiddenChar(email) !== -1)
57
+ return false;
58
+ return /^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/.test(email);
59
+ }
32
60
  function splitAddressList(input) {
33
61
  const parts = [];
34
62
  let current = "";
@@ -99,11 +127,12 @@ async function resolveAttachments(attachments, options) {
99
127
  throw new Error("attachment.path is not supported on this runtime — use attachment.content (Uint8Array) instead");
100
128
  }
101
129
  if (options?.basePath) {
102
- const { resolve } = await import("node:path");
130
+ const { resolve, sep } = await import("node:path");
103
131
  const resolvedPath = resolve(attachment.path);
104
132
  const resolvedBase = resolve(options.basePath);
105
- if (!resolvedPath.startsWith(resolvedBase)) {
106
- throw new Error(`[sently] Attachment path "${resolvedPath}" escapes basePath "${options.basePath}". Use absolute paths within the allowed directory.`);
133
+ const isWithin = resolvedPath === resolvedBase || resolvedPath.startsWith(resolvedBase + sep);
134
+ if (!isWithin) {
135
+ throw new Error(`[sently] Attachment path "${resolvedPath}" escapes basePath "${resolvedBase}". Use absolute paths within the allowed directory.`);
107
136
  }
108
137
  }
109
138
  const data = await fs.readFile(attachment.path);
@@ -120,6 +149,6 @@ async function resolveAttachments(attachments, options) {
120
149
  return resolved;
121
150
  }
122
151
 
123
- export { parseAddresses, toMIMEHeader, extractEmails, resolveAttachments };
152
+ export { assertSafeAddress, parseAddresses, toMIMEHeader, extractEmails, isValidEmail, resolveAttachments };
124
153
 
125
- //# debugId=7C59D40B68EA994E64756E2164756E21
154
+ //# debugId=419CD2BE8A029CA164756E2164756E21
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/core/address.ts", "../src/transports/resolve-attachments.ts"],
4
+ "sourcesContent": [
5
+ "// src/core/address.ts\nimport { encodeHeader } from \"./base64.js\";\nimport type { Address, AddressInput } from \"./types.js\";\n\n/**\n * Find the first forbidden control character in a string.\n *\n * Forbidden characters must never appear in an email address or display name,\n * because each enables a distinct attack:\n * - CR (0x0D) / LF (0x0A): email header injection and SMTP command injection\n * - NUL (0x00): C-string truncation / parser confusion in downstream agents\n * - other C0 controls (0x01–0x1F), DEL (0x7F): header/parser confusion\n * - U+2028 / U+2029: line separators that some parsers treat as newlines\n *\n * @returns The char code of the first forbidden character, or -1 if none.\n */\nfunction findForbiddenChar(value: string): number {\n for (let i = 0; i < value.length; i++) {\n const code = value.charCodeAt(i);\n if (code <= 0x1f || code === 0x7f || code === 0x2028 || code === 0x2029) {\n return code;\n }\n }\n return -1;\n}\n\n/**\n * Assert that a raw address or display-name value contains no forbidden\n * control characters. Throws immediately (fail closed) — the library never\n * attempts to strip or rewrite hostile input into an accepted value.\n *\n * The check is intentionally performed on the RAW input, before any trimming\n * or normalization, so hostile values are rejected rather than repaired.\n *\n * @param value - The raw, untransformed string to validate.\n * @param label - Field label used in the error message (e.g. \"address\").\n * @throws {Error} If the value contains any forbidden control character.\n */\nexport function assertSafeAddress(value: string, label = \"address\"): void {\n const code = findForbiddenChar(value);\n if (code !== -1) {\n const hex = code.toString(16).padStart(2, \"0\");\n throw new Error(\n `Email ${label} contains a forbidden control character (0x${hex}). ` +\n \"CR, LF, NUL, and other control characters are not allowed.\",\n );\n }\n}\n\n/**\n * Normalize any AddressInput form into Address[].\n *\n * Every address and display name is validated against control-character\n * injection before any transformation. This is the single chokepoint shared\n * by all transports and address fields (From, To, Cc, Bcc, Reply-To), so the\n * protection is uniform and secure by default.\n *\n * @throws {Error} If any address or name contains a forbidden control character.\n */\nexport function parseAddresses(input: AddressInput): Address[] {\n if (Array.isArray(input)) {\n return input.flatMap((item) => parseAddresses(item));\n }\n\n if (typeof input === \"object\") {\n assertSafeAddress(input.address, \"address\");\n if (input.name !== undefined) {\n assertSafeAddress(input.name, \"display name\");\n }\n return [{ ...input }];\n }\n\n // Validate the raw input string before splitting or trimming so injected\n // newlines cannot hide inside a multi-address list.\n assertSafeAddress(input, \"address\");\n\n const trimmed = input.trim();\n if (!trimmed) {\n return [];\n }\n\n return splitAddressList(trimmed).map(parseSingleAddress);\n}\n\n/**\n * Format an Address for SMTP envelope commands (MAIL FROM / RCPT TO).\n */\nexport function toEnvelope(address: Address): string {\n return address.address;\n}\n\n/**\n * Format an Address for use in a MIME header (From, To, CC, etc.).\n *\n * Re-validates the address and name at render time so a header can never be\n * emitted with an embedded control character, even if the {@link Address} was\n * constructed without going through {@link parseAddresses}.\n *\n * @throws {Error} If the address or name contains a forbidden control character.\n */\nexport function toMIMEHeader(address: Address): string {\n assertSafeAddress(address.address, \"address\");\n if (address.name) {\n assertSafeAddress(address.name, \"display name\");\n const name = encodeHeader(address.name);\n return `${name} <${address.address}>`;\n }\n return address.address;\n}\n\n/**\n * Extract plain email strings from any AddressInput.\n */\nexport function extractEmails(input: AddressInput): string[] {\n return parseAddresses(input).map((addr) => addr.address);\n}\n\n/**\n * Basic email format validation (format only, no DNS lookup).\n *\n * Rejects any control character (including CR, LF, tab, and NUL) before\n * applying the structural check, so a \"valid\" result is always safe to place\n * into a header or SMTP command.\n */\nexport function isValidEmail(email: string): boolean {\n if (findForbiddenChar(email) !== -1) return false;\n return /^[^\\s@<>]+@[^\\s@<>]+\\.[^\\s@<>]+$/.test(email);\n}\n\nfunction splitAddressList(input: string): string[] {\n const parts: string[] = [];\n let current = \"\";\n let inQuotes = false;\n let inAngle = false;\n\n for (let i = 0; i < input.length; i++) {\n const char = input[i] ?? \"\";\n if (char === '\"' && input[i - 1] !== \"\\\\\") {\n inQuotes = !inQuotes;\n current += char;\n continue;\n }\n if (char === \"<\" && !inQuotes) {\n inAngle = true;\n current += char;\n continue;\n }\n if (char === \">\" && !inQuotes) {\n inAngle = false;\n current += char;\n continue;\n }\n if (char === \",\" && !inQuotes && !inAngle) {\n if (current.trim()) {\n parts.push(current.trim());\n }\n current = \"\";\n continue;\n }\n current += char;\n }\n\n if (current.trim()) {\n parts.push(current.trim());\n }\n\n return parts;\n}\n\nfunction parseSingleAddress(input: string): Address {\n const trimmed = input.trim();\n\n const angleMatch = trimmed.match(/^(?:\"([^\"]*)\"|([^<]*?))\\s*<([^>]+)>$/);\n if (angleMatch) {\n const name = (angleMatch[1] ?? angleMatch[2] ?? \"\").trim();\n const address = (angleMatch[3] ?? \"\").trim();\n if (name) {\n return { name, address };\n }\n return { address };\n }\n\n if (trimmed.startsWith('\"') && trimmed.endsWith('\"')) {\n return { address: trimmed.slice(1, -1) };\n }\n\n return { address: trimmed };\n}\n",
6
+ "import type { Attachment } from \"../core/types.js\";\n\n/** Options for {@link resolveAttachments}. */\nexport interface ResolveAttachmentsOptions {\n /**\n * If set, attachment paths must resolve within this directory.\n * Prevents path traversal via `..` segments and sibling-directory\n * prefix matches. Opt-in only.\n *\n * Note: this check uses `node:path` `resolve()`, which does NOT\n * dereference symlinks. A symlink located inside `basePath` that\n * points outside of it will pass this check. If symlink traversal is\n * a concern, resolve paths with `fs.realpath()` before passing them in.\n */\n basePath?: string;\n}\n\n/**\n * Resolve attachment.path to in-memory Uint8Array content.\n * @throws When attachment.path is used on runtimes without node:fs/promises\n */\nexport async function resolveAttachments(\n attachments: Attachment[] | undefined,\n options?: ResolveAttachmentsOptions,\n): Promise<Attachment[]> {\n const list = attachments ?? [];\n const resolved: Attachment[] = [];\n\n for (const attachment of list) {\n if (attachment.content instanceof Uint8Array) {\n resolved.push(attachment);\n continue;\n }\n\n if (attachment.path) {\n let fs: typeof import(\"node:fs/promises\");\n try {\n fs = await import(\"node:fs/promises\");\n } catch {\n throw new Error(\n \"attachment.path is not supported on this runtime — use attachment.content (Uint8Array) instead\",\n );\n }\n\n if (options?.basePath) {\n const { resolve, sep } = await import(\"node:path\");\n const resolvedPath = resolve(attachment.path);\n const resolvedBase = resolve(options.basePath);\n // startsWith alone is vulnerable: \"/var/data-secret\" passes \"/var/data\".\n // Require an exact match or a trailing path separator.\n const isWithin =\n resolvedPath === resolvedBase || resolvedPath.startsWith(resolvedBase + sep);\n if (!isWithin) {\n throw new Error(\n `[sently] Attachment path \"${resolvedPath}\" escapes basePath \"${resolvedBase}\". ` +\n \"Use absolute paths within the allowed directory.\",\n );\n }\n }\n\n const data = await fs.readFile(attachment.path);\n const { path: _path, ...rest } = attachment;\n resolved.push({ ...rest, content: new Uint8Array(data) });\n continue;\n }\n\n if (typeof attachment.content === \"string\") {\n resolved.push(attachment);\n continue;\n }\n\n resolved.push(attachment);\n }\n\n return resolved;\n}\n"
7
+ ],
8
+ "mappings": ";;;;;;;;AAgBA,SAAS,iBAAiB,CAAC,OAAuB;AAAA,EAChD,SAAS,IAAI,EAAG,IAAI,MAAM,QAAQ,KAAK;AAAA,IACrC,MAAM,OAAO,MAAM,WAAW,CAAC;AAAA,IAC/B,IAAI,QAAQ,MAAQ,SAAS,OAAQ,SAAS,QAAU,SAAS,MAAQ;AAAA,MACvE,OAAO;AAAA,IACT;AAAA,EACF;AAAA,EACA,OAAO;AAAA;AAeF,SAAS,iBAAiB,CAAC,OAAe,QAAQ,WAAiB;AAAA,EACxE,MAAM,OAAO,kBAAkB,KAAK;AAAA,EACpC,IAAI,SAAS,IAAI;AAAA,IACf,MAAM,MAAM,KAAK,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAAA,IAC7C,MAAM,IAAI,MACR,SAAS,mDAAmD,WAC1D,4DACJ;AAAA,EACF;AAAA;AAaK,SAAS,cAAc,CAAC,OAAgC;AAAA,EAC7D,IAAI,MAAM,QAAQ,KAAK,GAAG;AAAA,IACxB,OAAO,MAAM,QAAQ,CAAC,SAAS,eAAe,IAAI,CAAC;AAAA,EACrD;AAAA,EAEA,IAAI,OAAO,UAAU,UAAU;AAAA,IAC7B,kBAAkB,MAAM,SAAS,SAAS;AAAA,IAC1C,IAAI,MAAM,SAAS,WAAW;AAAA,MAC5B,kBAAkB,MAAM,MAAM,cAAc;AAAA,IAC9C;AAAA,IACA,OAAO,CAAC,KAAK,MAAM,CAAC;AAAA,EACtB;AAAA,EAIA,kBAAkB,OAAO,SAAS;AAAA,EAElC,MAAM,UAAU,MAAM,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