@upyo/smtp 0.2.2-dev.45 → 0.2.2

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/README.md CHANGED
@@ -30,12 +30,7 @@ Features
30
30
  - Comprehensive testing utilities
31
31
  - TypeScript support
32
32
  - Cross-runtime compatibility (Node.js, Bun, Deno)
33
-
34
-
35
- TODO
36
- ----
37
-
38
- - [ ] STARTTLS support (currently only supports direct TLS)
33
+ - STARTTLS support for secure connection upgrade
39
34
 
40
35
 
41
36
  Installation
package/dist/index.cjs CHANGED
@@ -186,6 +186,49 @@ var SmtpConnection = class {
186
186
  if (response.code !== 250) throw new Error(`EHLO failed: ${response.message}`);
187
187
  this.capabilities = response.raw.split("\r\n").filter((line) => line.startsWith("250-") || line.startsWith("250 ")).map((line) => line.substring(4)).filter((line) => line.length > 0);
188
188
  }
189
+ async starttls(signal) {
190
+ if (!this.socket) throw new Error("Not connected");
191
+ if (this.socket instanceof node_tls.TLSSocket) throw new Error("Connection is already using TLS");
192
+ signal?.throwIfAborted();
193
+ const response = await this.sendCommand("STARTTLS", signal);
194
+ if (response.code !== 220) throw new Error(`STARTTLS failed: ${response.message}`);
195
+ signal?.throwIfAborted();
196
+ return new Promise((resolve, reject) => {
197
+ const timeout = setTimeout(() => {
198
+ this.socket?.destroy();
199
+ reject(/* @__PURE__ */ new Error("STARTTLS upgrade timeout"));
200
+ }, this.config.connectionTimeout);
201
+ const plainSocket = this.socket;
202
+ const tlsSocket = (0, node_tls.connect)({
203
+ socket: plainSocket,
204
+ host: this.config.host,
205
+ rejectUnauthorized: this.config.tls?.rejectUnauthorized ?? true,
206
+ ca: this.config.tls?.ca,
207
+ key: this.config.tls?.key,
208
+ cert: this.config.tls?.cert,
209
+ minVersion: this.config.tls?.minVersion,
210
+ maxVersion: this.config.tls?.maxVersion
211
+ });
212
+ const onSecureConnect = () => {
213
+ clearTimeout(timeout);
214
+ this.socket = tlsSocket;
215
+ this.socket.setTimeout(this.config.socketTimeout);
216
+ resolve();
217
+ };
218
+ const onError = (error) => {
219
+ clearTimeout(timeout);
220
+ tlsSocket.destroy();
221
+ reject(error);
222
+ };
223
+ tlsSocket.once("secureConnect", onSecureConnect);
224
+ tlsSocket.once("error", onError);
225
+ tlsSocket.once("timeout", () => {
226
+ clearTimeout(timeout);
227
+ tlsSocket.destroy();
228
+ reject(/* @__PURE__ */ new Error("TLS upgrade timeout"));
229
+ });
230
+ });
231
+ }
189
232
  async authenticate(signal) {
190
233
  if (!this.config.auth) return;
191
234
  if (this.authenticated) return;
@@ -627,6 +670,12 @@ var SmtpTransport = class {
627
670
  signal?.throwIfAborted();
628
671
  await connection.ehlo(signal);
629
672
  signal?.throwIfAborted();
673
+ if (!this.config.secure && connection.capabilities.some((cap) => cap.toUpperCase().startsWith("STARTTLS"))) {
674
+ await connection.starttls(signal);
675
+ signal?.throwIfAborted();
676
+ await connection.ehlo(signal);
677
+ signal?.throwIfAborted();
678
+ }
630
679
  await connection.authenticate(signal);
631
680
  }
632
681
  async returnConnection(connection) {
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Socket } from "node:net";
2
- import { connect } from "node:tls";
2
+ import { TLSSocket, connect } from "node:tls";
3
3
  import { Buffer } from "node:buffer";
4
4
 
5
5
  //#region src/config.ts
@@ -163,6 +163,49 @@ var SmtpConnection = class {
163
163
  if (response.code !== 250) throw new Error(`EHLO failed: ${response.message}`);
164
164
  this.capabilities = response.raw.split("\r\n").filter((line) => line.startsWith("250-") || line.startsWith("250 ")).map((line) => line.substring(4)).filter((line) => line.length > 0);
165
165
  }
166
+ async starttls(signal) {
167
+ if (!this.socket) throw new Error("Not connected");
168
+ if (this.socket instanceof TLSSocket) throw new Error("Connection is already using TLS");
169
+ signal?.throwIfAborted();
170
+ const response = await this.sendCommand("STARTTLS", signal);
171
+ if (response.code !== 220) throw new Error(`STARTTLS failed: ${response.message}`);
172
+ signal?.throwIfAborted();
173
+ return new Promise((resolve, reject) => {
174
+ const timeout = setTimeout(() => {
175
+ this.socket?.destroy();
176
+ reject(/* @__PURE__ */ new Error("STARTTLS upgrade timeout"));
177
+ }, this.config.connectionTimeout);
178
+ const plainSocket = this.socket;
179
+ const tlsSocket = connect({
180
+ socket: plainSocket,
181
+ host: this.config.host,
182
+ rejectUnauthorized: this.config.tls?.rejectUnauthorized ?? true,
183
+ ca: this.config.tls?.ca,
184
+ key: this.config.tls?.key,
185
+ cert: this.config.tls?.cert,
186
+ minVersion: this.config.tls?.minVersion,
187
+ maxVersion: this.config.tls?.maxVersion
188
+ });
189
+ const onSecureConnect = () => {
190
+ clearTimeout(timeout);
191
+ this.socket = tlsSocket;
192
+ this.socket.setTimeout(this.config.socketTimeout);
193
+ resolve();
194
+ };
195
+ const onError = (error) => {
196
+ clearTimeout(timeout);
197
+ tlsSocket.destroy();
198
+ reject(error);
199
+ };
200
+ tlsSocket.once("secureConnect", onSecureConnect);
201
+ tlsSocket.once("error", onError);
202
+ tlsSocket.once("timeout", () => {
203
+ clearTimeout(timeout);
204
+ tlsSocket.destroy();
205
+ reject(/* @__PURE__ */ new Error("TLS upgrade timeout"));
206
+ });
207
+ });
208
+ }
166
209
  async authenticate(signal) {
167
210
  if (!this.config.auth) return;
168
211
  if (this.authenticated) return;
@@ -604,6 +647,12 @@ var SmtpTransport = class {
604
647
  signal?.throwIfAborted();
605
648
  await connection.ehlo(signal);
606
649
  signal?.throwIfAborted();
650
+ if (!this.config.secure && connection.capabilities.some((cap) => cap.toUpperCase().startsWith("STARTTLS"))) {
651
+ await connection.starttls(signal);
652
+ signal?.throwIfAborted();
653
+ await connection.ehlo(signal);
654
+ signal?.throwIfAborted();
655
+ }
607
656
  await connection.authenticate(signal);
608
657
  }
609
658
  async returnConnection(connection) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upyo/smtp",
3
- "version": "0.2.2-dev.45+42644e2f",
3
+ "version": "0.2.2",
4
4
  "description": "SMTP transport for Upyo email library",
5
5
  "keywords": [
6
6
  "email",
@@ -53,7 +53,7 @@
53
53
  },
54
54
  "sideEffects": false,
55
55
  "peerDependencies": {
56
- "@upyo/core": "0.2.2-dev.45+42644e2f"
56
+ "@upyo/core": "0.2.2"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@dotenvx/dotenvx": "^1.47.3",