@ac-kit/net-smtp 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 ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @ac-kit/net-smtp
2
+
3
+ An SMTP client — the protocol for handing a message to a mail server — plus its
4
+ LMTP variant, spoken command by command rather than hidden behind a `sendMail`
5
+ facade.
6
+
7
+ That matters when you need to see what the server said: which extensions it
8
+ advertised in its `EHLO` response, which recipient it rejected and with what
9
+ code, whether it offered `STARTTLS`.
10
+
11
+ ```ts
12
+ import { SmtpClient } from "@ac-kit/net-smtp";
13
+ import { DuplexTransport } from "@ac-kit/net-transport-node";
14
+
15
+ const client = new SmtpClient(new DuplexTransport(socket.stream));
16
+
17
+ await client.ehlo("client.example.com");
18
+ await client.mailFrom("sender@example.com");
19
+ await client.rcptTo("recipient@example.com");
20
+ await client.data(messageBody);
21
+ await client.quit();
22
+ ```
23
+
24
+ Every command returns the server's `SmtpResponse` and takes optional
25
+ `ExchangeOptions`, including an `AbortSignal`. `upgradeTransport` swaps in a TLS
26
+ transport after `STARTTLS`. `buildSmtpMessageBody` applies dot-stuffing so a
27
+ line of a single `.` in your message cannot terminate the `DATA` block.
28
+
29
+ Built on `@ac-kit/net-core`'s `Transport`, so the same client runs over TCP, TLS
30
+ or an in-memory double in a test. It imports no socket API itself.
31
+
32
+ See the
33
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/net-smtp/)
34
+ for the full reference.
@@ -0,0 +1,3 @@
1
+ export * from "./lmtp/lmtp-client.js";
2
+ export * from "./smtp/smtp-client.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./lmtp/lmtp-client.js";
2
+ export * from "./smtp/smtp-client.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,46 @@
1
+ import type { ExchangeOptions, Transport } from "@ac-kit/net-core";
2
+ import { buildSmtpMessageBody, SmtpCore, SmtpProtocolError, type SmtpResponse } from "../smtp/smtp-client.js";
3
+ export { buildSmtpMessageBody, SmtpProtocolError };
4
+ export type { SmtpResponse };
5
+ /**
6
+ * LMTP client over any transport.
7
+ *
8
+ * LMTP (RFC 2033) is SMTP with `LHLO` in place of `EHLO` and, critically, one
9
+ * reply per recipient after the message body instead of a single reply.
10
+ *
11
+ * It extends {@link SmtpCore} rather than `SmtpClient` precisely because of that
12
+ * difference: inheriting `SmtpClient.data()` would expose a method that reads
13
+ * only the first recipient's reply and leaves the rest to be mis-attributed to
14
+ * the next command.
15
+ */
16
+ export declare class LmtpClient extends SmtpCore {
17
+ /**
18
+ * @param source Any {@link Transport}. Dovecot LMTP is commonly reached over
19
+ * a Unix socket — for a Node.js `Duplex`/`net.Socket`, wrap it first: `new
20
+ * DuplexTransport(socket)` from `@ac-kit/net-transport-node`.
21
+ * @param options Buffer, line-length and timeout tuning.
22
+ */
23
+ constructor(source: Transport, options?: ConstructorParameters<typeof SmtpCore>[1]);
24
+ /**
25
+ * Send `LHLO domain`, the LMTP greeting.
26
+ *
27
+ * @param domain The client's own domain name.
28
+ * @param options Abort, timeout and abort-policy overrides.
29
+ * @returns The parsed reply, one entry per capability line.
30
+ */
31
+ lhlo(domain: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
32
+ /**
33
+ * Run the `DATA` exchange and read one reply per accepted recipient.
34
+ *
35
+ * Replies arrive in the order the corresponding `RCPT TO` commands were
36
+ * issued (RFC 2033 §4.2).
37
+ *
38
+ * @param message The raw message body; dot-stuffing is applied automatically.
39
+ * @param recipientCount Number of `RCPT TO` commands the server accepted.
40
+ * @param options Abort, timeout and abort-policy overrides.
41
+ * @returns One reply per recipient, in `RCPT TO` order.
42
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
43
+ */
44
+ dataMulti(message: string, recipientCount: number, options?: ExchangeOptions<string>): Promise<SmtpResponse[]>;
45
+ }
46
+ //# sourceMappingURL=lmtp-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lmtp-client.d.ts","sourceRoot":"","sources":["../../src/lmtp/lmtp-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAEnE,OAAO,EACN,oBAAoB,EACpB,QAAQ,EACR,iBAAiB,EACjB,KAAK,YAAY,EACjB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,CAAC;AACnD,YAAY,EAAE,YAAY,EAAE,CAAC;AAE7B;;;;;;;;;;GAUG;AACH,qBAAa,UAAW,SAAQ,QAAQ;IACvC;;;;;OAKG;IACH,YACC,MAAM,EAAE,SAAS,EACjB,OAAO,CAAC,EAAE,qBAAqB,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EAGnD;IAED;;;;;;OAMG;IACH,IAAI,CACH,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAC/B,OAAO,CAAC,YAAY,CAAC,CAEvB;IAED;;;;;;;;;;;OAWG;IACH,SAAS,CACR,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAC/B,OAAO,CAAC,YAAY,EAAE,CAAC,CAEzB;CACD"}
@@ -0,0 +1,50 @@
1
+ import { buildSmtpMessageBody, SmtpCore, SmtpProtocolError, } from "../smtp/smtp-client.js";
2
+ export { buildSmtpMessageBody, SmtpProtocolError };
3
+ /**
4
+ * LMTP client over any transport.
5
+ *
6
+ * LMTP (RFC 2033) is SMTP with `LHLO` in place of `EHLO` and, critically, one
7
+ * reply per recipient after the message body instead of a single reply.
8
+ *
9
+ * It extends {@link SmtpCore} rather than `SmtpClient` precisely because of that
10
+ * difference: inheriting `SmtpClient.data()` would expose a method that reads
11
+ * only the first recipient's reply and leaves the rest to be mis-attributed to
12
+ * the next command.
13
+ */
14
+ export class LmtpClient extends SmtpCore {
15
+ /**
16
+ * @param source Any {@link Transport}. Dovecot LMTP is commonly reached over
17
+ * a Unix socket — for a Node.js `Duplex`/`net.Socket`, wrap it first: `new
18
+ * DuplexTransport(socket)` from `@ac-kit/net-transport-node`.
19
+ * @param options Buffer, line-length and timeout tuning.
20
+ */
21
+ constructor(source, options) {
22
+ super(source, options);
23
+ }
24
+ /**
25
+ * Send `LHLO domain`, the LMTP greeting.
26
+ *
27
+ * @param domain The client's own domain name.
28
+ * @param options Abort, timeout and abort-policy overrides.
29
+ * @returns The parsed reply, one entry per capability line.
30
+ */
31
+ lhlo(domain, options) {
32
+ return this.command(`LHLO ${domain}`, options);
33
+ }
34
+ /**
35
+ * Run the `DATA` exchange and read one reply per accepted recipient.
36
+ *
37
+ * Replies arrive in the order the corresponding `RCPT TO` commands were
38
+ * issued (RFC 2033 §4.2).
39
+ *
40
+ * @param message The raw message body; dot-stuffing is applied automatically.
41
+ * @param recipientCount Number of `RCPT TO` commands the server accepted.
42
+ * @param options Abort, timeout and abort-policy overrides.
43
+ * @returns One reply per recipient, in `RCPT TO` order.
44
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
45
+ */
46
+ dataMulti(message, recipientCount, options) {
47
+ return this.runData(message, recipientCount, options);
48
+ }
49
+ }
50
+ //# sourceMappingURL=lmtp-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lmtp-client.js","sourceRoot":"","sources":["../../src/lmtp/lmtp-client.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,oBAAoB,EACpB,QAAQ,EACR,iBAAiB,GAEjB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,CAAC;AAGnD;;;;;;;;;;GAUG;AACH,MAAM,OAAO,UAAW,SAAQ,QAAQ;IACvC;;;;;OAKG;IACH,YACC,MAAiB,EACjB,OAAmD;QAEnD,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxB,CAAC;IAED;;;;;;OAMG;IACH,IAAI,CACH,MAAc,EACd,OAAiC;QAEjC,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,MAAM,EAAE,EAAE,OAAO,CAAC,CAAC;IAChD,CAAC;IAED;;;;;;;;;;;OAWG;IACH,SAAS,CACR,OAAe,EACf,cAAsB,EACtB,OAAiC;QAEjC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;CACD"}
@@ -0,0 +1,171 @@
1
+ import type { Disposition, ExchangeOptions, Transport } from "@ac-kit/net-core";
2
+ import { type BaseSessionEvents, Session } from "@ac-kit/net-core";
3
+ export type SmtpResponse = {
4
+ /** The three-digit SMTP reply code. */
5
+ code: number;
6
+ /**
7
+ * Text lines from the response, one per continuation line, with the code
8
+ * prefix stripped.
9
+ */
10
+ lines: string[];
11
+ };
12
+ export type SmtpClientEvents = BaseSessionEvents & {
13
+ /**
14
+ * An unsolicited server reply, including the initial greeting.
15
+ *
16
+ * Subscribe before the first byte arrives to avoid missing the greeting.
17
+ */
18
+ push: [response: SmtpResponse];
19
+ };
20
+ /** Thrown when a required intermediate SMTP reply carries an unexpected code. */
21
+ export declare class SmtpProtocolError extends Error {
22
+ readonly response: SmtpResponse;
23
+ constructor(response: SmtpResponse, message: string);
24
+ }
25
+ /** Construction options shared by {@link SmtpClient} and its subclasses. */
26
+ export interface SmtpClientOptions {
27
+ /**
28
+ * Ceiling on undecoded received bytes. Defaults to 64 KiB, ample for the
29
+ * longest realistic `EHLO` capability list while still bounding a misbehaving
30
+ * server.
31
+ */
32
+ maxBufferSize?: number;
33
+ /**
34
+ * Maximum reply line length. Defaults to 1000 bytes, the limit RFC 5321
35
+ * §4.5.3.1.5 places on a reply line including its terminator.
36
+ */
37
+ maxLineLength?: number;
38
+ /** Applied to every command that does not specify its own timeout. */
39
+ defaultTimeoutMs?: number;
40
+ }
41
+ /**
42
+ * Dot-stuffs a message body per RFC 5321 §4.5.2 and appends the CRLF.CRLF
43
+ * terminator.
44
+ */
45
+ export declare function buildSmtpMessageBody(message: string): string;
46
+ /**
47
+ * Commands shared by SMTP and LMTP.
48
+ *
49
+ * The two protocols differ only in their greeting verb and in how the message
50
+ * body is acknowledged, so everything else lives here. LMTP deliberately does
51
+ * not inherit {@link SmtpClient}: `DATA` yields one reply per recipient in LMTP
52
+ * and exactly one reply in SMTP, so inheriting `data()` would expose a method
53
+ * that silently misreads the wire.
54
+ */
55
+ export declare abstract class SmtpCore extends Session<string, string, SmtpClientEvents> {
56
+ private readonly defaultTimeoutMs;
57
+ /**
58
+ * @param source Any {@link Transport}. For a Node.js `Duplex`/`net.Socket`,
59
+ * wrap it first: `new DuplexTransport(socket)` from
60
+ * `@ac-kit/net-transport-node`.
61
+ * @param options Buffer, line-length and timeout tuning.
62
+ */
63
+ constructor(source: Transport, options?: SmtpClientOptions);
64
+ protected routeFrame(frame: string): void;
65
+ /**
66
+ * Send `EHLO domain` and return the advertised extensions.
67
+ *
68
+ * @param domain The client's own domain name.
69
+ * @param options Abort, timeout and abort-policy overrides.
70
+ * @returns The parsed reply, one entry per capability line.
71
+ */
72
+ ehlo(domain: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
73
+ /**
74
+ * Send `MAIL FROM:<address>`.
75
+ *
76
+ * @param address The reverse-path, without angle brackets.
77
+ * @param params Optional ESMTP parameters, for example `SIZE=1234`.
78
+ * @param options Abort, timeout and abort-policy overrides.
79
+ * @returns The parsed reply.
80
+ */
81
+ mailFrom(address: string, params?: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
82
+ /**
83
+ * Send `RCPT TO:<address>`.
84
+ *
85
+ * @param address The forward-path, without angle brackets.
86
+ * @param options Abort, timeout and abort-policy overrides.
87
+ * @returns The parsed reply.
88
+ */
89
+ rcptTo(address: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
90
+ /**
91
+ * Run the full `DATA` exchange: command, `354` intermediate reply, body, then
92
+ * final reply.
93
+ *
94
+ * @param message The raw message body; dot-stuffing is applied automatically.
95
+ * @param options Abort, timeout and abort-policy overrides.
96
+ * @returns The final reply for the message.
97
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
98
+ */
99
+ protected runData(message: string, collectCount: number, options?: ExchangeOptions<string>): Promise<SmtpResponse[]>;
100
+ /**
101
+ * Send `QUIT` and read the farewell reply.
102
+ *
103
+ * @param options Abort, timeout and abort-policy overrides.
104
+ * @returns The parsed reply, normally `221`.
105
+ */
106
+ quit(options?: ExchangeOptions<string>): Promise<SmtpResponse>;
107
+ /**
108
+ * Send a raw command line and read its reply.
109
+ *
110
+ * @param command The command line, without a terminator.
111
+ * @param options Abort, timeout and abort-policy overrides.
112
+ * @returns The parsed reply.
113
+ */
114
+ command(command: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
115
+ /**
116
+ * Replace the transport after a successful `STARTTLS`.
117
+ *
118
+ * Buffered plaintext is discarded rather than carried across: bytes a peer
119
+ * sent before the handshake must never be read as part of the secured
120
+ * session, which is the STARTTLS command-injection flaw.
121
+ *
122
+ * @param transport The TLS transport wrapping the same connection.
123
+ */
124
+ upgradeTransport(transport: Transport): void;
125
+ /**
126
+ * Classify a reply line: `CODE-` continues the reply, anything else ends it.
127
+ *
128
+ * @param frame One decoded reply line.
129
+ * @returns The disposition for the pending exchange.
130
+ */
131
+ protected static multiLineAccept(frame: string): Disposition;
132
+ /**
133
+ * Assemble collected reply lines into a {@link SmtpResponse}.
134
+ *
135
+ * @param lines Reply lines in arrival order.
136
+ * @returns The reply code and its text lines.
137
+ */
138
+ protected static parseLines(lines: string[]): SmtpResponse;
139
+ private static buildOptions;
140
+ /** Applies the client-wide timeout when the call site did not set one. */
141
+ private withDefaults;
142
+ }
143
+ /**
144
+ * SMTP client over any transport.
145
+ *
146
+ * Handles CRLF framing, multi-line reply assembly (`CODE-` continuations) and
147
+ * dot-stuffing for `DATA`. Each method maps to one command; sequencing them
148
+ * correctly per RFC 5321 is the caller's responsibility.
149
+ *
150
+ * Replies are strictly ordered, so the default FIFO correlation applies and
151
+ * pipelining works with no further configuration.
152
+ *
153
+ * Unsolicited server lines, including the initial greeting, are dispatched as
154
+ * `push` events; subscribe before the first byte arrives to catch the
155
+ * greeting.
156
+ *
157
+ * The connection is not established here — pass an already-connected stream or
158
+ * transport.
159
+ */
160
+ export declare class SmtpClient extends SmtpCore {
161
+ /**
162
+ * Run the `DATA` exchange and read the single reply for the message.
163
+ *
164
+ * @param message The raw message body; dot-stuffing is applied automatically.
165
+ * @param options Abort, timeout and abort-policy overrides.
166
+ * @returns The final reply.
167
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
168
+ */
169
+ data(message: string, options?: ExchangeOptions<string>): Promise<SmtpResponse>;
170
+ }
171
+ //# sourceMappingURL=smtp-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"smtp-client.d.ts","sourceRoot":"","sources":["../../src/smtp/smtp-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EACN,KAAK,iBAAiB,EACtB,OAAO,EAEP,MAAM,kBAAkB,CAAC;AAE1B,MAAM,MAAM,YAAY,GAAG;IAC1B,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,KAAK,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,GAAG;IAClD;;;;OAIG;IACH,IAAI,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;CAC/B,CAAC;AAEF,iFAAiF;AACjF,qBAAa,iBAAkB,SAAQ,KAAK;IAE1C,QAAQ,CAAC,QAAQ,EAAE,YAAY;IADhC,YACU,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C,CAwBzB;IAED;;;;;OAKG;IACH,IAAI,CAAC,OAAO,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAE7D;IAED;;;;;;OAMG;IACG,OAAO,CACZ,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAC/B,OAAO,CAAC,YAAY,CAAC,CAOvB;IAED;;;;;;;;OAQG;IACH,gBAAgB,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CAE3C;IAED;;;;;OAKG;IACH,SAAS,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE3D;IAED;;;;;OAKG;IACH,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,CAOzD;IAED,OAAO,CAAC,MAAM,CAAC,YAAY;IAY3B,0EAA0E;IAC1E,OAAO,CAAC,YAAY;CAWpB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,UAAW,SAAQ,QAAQ;IACvC;;;;;;;OAOG;IACG,IAAI,CACT,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAC/B,OAAO,CAAC,YAAY,CAAC,CAGvB;CACD"}
@@ -0,0 +1,212 @@
1
+ import { encodeText } from "@ac-kit/core";
2
+ import { createLineCodec } from "@ac-kit/format-core";
3
+ import { Session, } from "@ac-kit/net-core";
4
+ /** Thrown when a required intermediate SMTP reply carries an unexpected code. */
5
+ export class SmtpProtocolError extends Error {
6
+ response;
7
+ constructor(response, message) {
8
+ super(message);
9
+ this.response = response;
10
+ this.name = "SmtpProtocolError";
11
+ }
12
+ }
13
+ /**
14
+ * Dot-stuffs a message body per RFC 5321 §4.5.2 and appends the CRLF.CRLF
15
+ * terminator.
16
+ */
17
+ export function buildSmtpMessageBody(message) {
18
+ const dotStuffed = message
19
+ .split("\r\n")
20
+ .map((line) => (line.startsWith(".") ? `.${line}` : line))
21
+ .join("\r\n");
22
+ const body = dotStuffed.endsWith("\r\n") ? dotStuffed : `${dotStuffed}\r\n`;
23
+ return `${body}.\r\n`;
24
+ }
25
+ /**
26
+ * Commands shared by SMTP and LMTP.
27
+ *
28
+ * The two protocols differ only in their greeting verb and in how the message
29
+ * body is acknowledged, so everything else lives here. LMTP deliberately does
30
+ * not inherit {@link SmtpClient}: `DATA` yields one reply per recipient in LMTP
31
+ * and exactly one reply in SMTP, so inheriting `data()` would expose a method
32
+ * that silently misreads the wire.
33
+ */
34
+ export class SmtpCore extends Session {
35
+ defaultTimeoutMs;
36
+ /**
37
+ * @param source Any {@link Transport}. For a Node.js `Duplex`/`net.Socket`,
38
+ * wrap it first: `new DuplexTransport(socket)` from
39
+ * `@ac-kit/net-transport-node`.
40
+ * @param options Buffer, line-length and timeout tuning.
41
+ */
42
+ constructor(source, options) {
43
+ super(SmtpCore.buildOptions(source, options));
44
+ this.defaultTimeoutMs = options?.defaultTimeoutMs;
45
+ }
46
+ routeFrame(frame) {
47
+ if (!this.offer(frame)) {
48
+ this.dispatch("push", [SmtpCore.parseLines([frame])]);
49
+ }
50
+ }
51
+ /**
52
+ * Send `EHLO domain` and return the advertised extensions.
53
+ *
54
+ * @param domain The client's own domain name.
55
+ * @param options Abort, timeout and abort-policy overrides.
56
+ * @returns The parsed reply, one entry per capability line.
57
+ */
58
+ ehlo(domain, options) {
59
+ return this.command(`EHLO ${domain}`, options);
60
+ }
61
+ /**
62
+ * Send `MAIL FROM:<address>`.
63
+ *
64
+ * @param address The reverse-path, without angle brackets.
65
+ * @param params Optional ESMTP parameters, for example `SIZE=1234`.
66
+ * @param options Abort, timeout and abort-policy overrides.
67
+ * @returns The parsed reply.
68
+ */
69
+ mailFrom(address, params, options) {
70
+ const command = params
71
+ ? `MAIL FROM:<${address}> ${params}`
72
+ : `MAIL FROM:<${address}>`;
73
+ return this.command(command, options);
74
+ }
75
+ /**
76
+ * Send `RCPT TO:<address>`.
77
+ *
78
+ * @param address The forward-path, without angle brackets.
79
+ * @param options Abort, timeout and abort-policy overrides.
80
+ * @returns The parsed reply.
81
+ */
82
+ rcptTo(address, options) {
83
+ return this.command(`RCPT TO:<${address}>`, options);
84
+ }
85
+ /**
86
+ * Run the full `DATA` exchange: command, `354` intermediate reply, body, then
87
+ * final reply.
88
+ *
89
+ * @param message The raw message body; dot-stuffing is applied automatically.
90
+ * @param options Abort, timeout and abort-policy overrides.
91
+ * @returns The final reply for the message.
92
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
93
+ */
94
+ async runData(message, collectCount, options) {
95
+ const begin = await this.command("DATA", options);
96
+ if (begin.code !== 354) {
97
+ throw new SmtpProtocolError(begin, `DATA command failed: ${begin.code} ${begin.lines.join(" ")}`);
98
+ }
99
+ // Every exchange is registered before the body is written, so no reply can
100
+ // arrive while nothing is waiting for it.
101
+ const pending = [];
102
+ for (let index = 0; index < collectCount; index++) {
103
+ pending.push(this.collect(SmtpCore.multiLineAccept, this.withDefaults(options)));
104
+ }
105
+ await this.sendRaw(encodeText(buildSmtpMessageBody(message), "utf-8"), options?.signal);
106
+ return (await Promise.all(pending)).map((lines) => SmtpCore.parseLines(lines));
107
+ }
108
+ /**
109
+ * Send `QUIT` and read the farewell reply.
110
+ *
111
+ * @param options Abort, timeout and abort-policy overrides.
112
+ * @returns The parsed reply, normally `221`.
113
+ */
114
+ quit(options) {
115
+ return this.command("QUIT", options);
116
+ }
117
+ /**
118
+ * Send a raw command line and read its reply.
119
+ *
120
+ * @param command The command line, without a terminator.
121
+ * @param options Abort, timeout and abort-policy overrides.
122
+ * @returns The parsed reply.
123
+ */
124
+ async command(command, options) {
125
+ const lines = await this.request(command, SmtpCore.multiLineAccept, this.withDefaults(options));
126
+ return SmtpCore.parseLines(lines);
127
+ }
128
+ /**
129
+ * Replace the transport after a successful `STARTTLS`.
130
+ *
131
+ * Buffered plaintext is discarded rather than carried across: bytes a peer
132
+ * sent before the handshake must never be read as part of the secured
133
+ * session, which is the STARTTLS command-injection flaw.
134
+ *
135
+ * @param transport The TLS transport wrapping the same connection.
136
+ */
137
+ upgradeTransport(transport) {
138
+ this.swapTransport(transport);
139
+ }
140
+ /**
141
+ * Classify a reply line: `CODE-` continues the reply, anything else ends it.
142
+ *
143
+ * @param frame One decoded reply line.
144
+ * @returns The disposition for the pending exchange.
145
+ */
146
+ static multiLineAccept(frame) {
147
+ return frame.length >= 4 && frame[3] === "-" ? "accumulate" : "complete";
148
+ }
149
+ /**
150
+ * Assemble collected reply lines into a {@link SmtpResponse}.
151
+ *
152
+ * @param lines Reply lines in arrival order.
153
+ * @returns The reply code and its text lines.
154
+ */
155
+ static parseLines(lines) {
156
+ const first = lines[0] ?? "";
157
+ const code = Number.parseInt(first.slice(0, 3), 10);
158
+ return {
159
+ code,
160
+ lines: lines.map((line) => (line.length > 4 ? line.slice(4) : "")),
161
+ };
162
+ }
163
+ static buildOptions(source, options) {
164
+ return {
165
+ transport: source,
166
+ codec: createLineCodec("utf-8", {
167
+ maxLineLength: options?.maxLineLength ?? 1000,
168
+ }),
169
+ maxBufferSize: options?.maxBufferSize ?? 64 * 1024,
170
+ };
171
+ }
172
+ /** Applies the client-wide timeout when the call site did not set one. */
173
+ withDefaults(options) {
174
+ if (this.defaultTimeoutMs === undefined ||
175
+ options?.timeoutMs !== undefined) {
176
+ return options;
177
+ }
178
+ return { ...options, timeoutMs: this.defaultTimeoutMs };
179
+ }
180
+ }
181
+ /**
182
+ * SMTP client over any transport.
183
+ *
184
+ * Handles CRLF framing, multi-line reply assembly (`CODE-` continuations) and
185
+ * dot-stuffing for `DATA`. Each method maps to one command; sequencing them
186
+ * correctly per RFC 5321 is the caller's responsibility.
187
+ *
188
+ * Replies are strictly ordered, so the default FIFO correlation applies and
189
+ * pipelining works with no further configuration.
190
+ *
191
+ * Unsolicited server lines, including the initial greeting, are dispatched as
192
+ * `push` events; subscribe before the first byte arrives to catch the
193
+ * greeting.
194
+ *
195
+ * The connection is not established here — pass an already-connected stream or
196
+ * transport.
197
+ */
198
+ export class SmtpClient extends SmtpCore {
199
+ /**
200
+ * Run the `DATA` exchange and read the single reply for the message.
201
+ *
202
+ * @param message The raw message body; dot-stuffing is applied automatically.
203
+ * @param options Abort, timeout and abort-policy overrides.
204
+ * @returns The final reply.
205
+ * @throws {SmtpProtocolError} If the server does not answer `354`.
206
+ */
207
+ async data(message, options) {
208
+ const [response] = await this.runData(message, 1, options);
209
+ return response;
210
+ }
211
+ }
212
+ //# sourceMappingURL=smtp-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"smtp-client.js","sourceRoot":"","sources":["../../src/smtp/smtp-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAEN,OAAO,GAEP,MAAM,kBAAkB,CAAC;AAqB1B,iFAAiF;AACjF,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAEjC,QAAQ;IADlB,YACU,QAAsB,EAC/B,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;wBAHN,QAAQ;QAIjB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IACjC,CAAC;CACD;AAqBD;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAe;IACnD,MAAM,UAAU,GAAG,OAAO;SACxB,KAAK,CAAC,MAAM,CAAC;SACb,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SACzD,IAAI,CAAC,MAAM,CAAC,CAAC;IACf,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,MAAM,CAAC;IA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AAC;QAChB,CAAC;QACD,OAAO,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,gBAAgB,EAAE,CAAC;IACzD,CAAC;CACD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,UAAW,SAAQ,QAAQ;IACvC;;;;;;;OAOG;IACH,KAAK,CAAC,IAAI,CACT,OAAe,EACf,OAAiC;QAEjC,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;QAC3D,OAAO,QAAS,CAAC;IAClB,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@ac-kit/net-smtp",
3
+ "version": "0.1.0",
4
+ "description": "An SMTP client built on the @ac-kit/net-core Transport.",
5
+ "keywords": [
6
+ "client",
7
+ "email",
8
+ "protocol",
9
+ "smtp"
10
+ ],
11
+ "homepage": "https://anthochamp.github.io/node-essentials/api/net-smtp/",
12
+ "bugs": {
13
+ "url": "https://github.com/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
20
+ "directory": "packages/net-smtp"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build"
45
+ },
46
+ "dependencies": {
47
+ "@ac-kit/core": "^0.1.0",
48
+ "@ac-kit/format-core": "^0.1.0",
49
+ "@ac-kit/net-core": "^0.1.0"
50
+ },
51
+ "devDependencies": {
52
+ "@ac-kit/net-transport-node": "0.1.0",
53
+ "@ac-kit/tsconfig": "0.1.0",
54
+ "@ac-kit/typedoc-config": "0.1.0",
55
+ "@ac-kit/vitest-config": "0.1.0",
56
+ "@types/node": "^26.5.1",
57
+ "@typescript/native": "typescript@7.0.2",
58
+ "rimraf": "6.1.3",
59
+ "typedoc": "0.28.20",
60
+ "typescript": "@typescript/typescript6@6.0.2"
61
+ },
62
+ "engines": {
63
+ "node": ">=24"
64
+ }
65
+ }