@upyo/smtp 0.1.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2025 Hong Minhee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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;
@@ -622,6 +665,12 @@ var SmtpTransport = class {
622
665
  signal?.throwIfAborted();
623
666
  await connection.ehlo(signal);
624
667
  signal?.throwIfAborted();
668
+ if (!this.config.secure && connection.capabilities.some((cap) => cap.toUpperCase().startsWith("STARTTLS"))) {
669
+ await connection.starttls(signal);
670
+ signal?.throwIfAborted();
671
+ await connection.ehlo(signal);
672
+ signal?.throwIfAborted();
673
+ }
625
674
  await connection.authenticate(signal);
626
675
  }
627
676
  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 { formatAddress } from "@upyo/core";
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;
@@ -599,6 +642,12 @@ var SmtpTransport = class {
599
642
  signal?.throwIfAborted();
600
643
  await connection.ehlo(signal);
601
644
  signal?.throwIfAborted();
645
+ if (!this.config.secure && connection.capabilities.some((cap) => cap.toUpperCase().startsWith("STARTTLS"))) {
646
+ await connection.starttls(signal);
647
+ signal?.throwIfAborted();
648
+ await connection.ehlo(signal);
649
+ signal?.throwIfAborted();
650
+ }
602
651
  await connection.authenticate(signal);
603
652
  }
604
653
  async returnConnection(connection) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upyo/smtp",
3
- "version": "0.1.1",
3
+ "version": "0.1.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.1.1"
56
+ "@upyo/core": "0.1.2"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@dotenvx/dotenvx": "^1.47.3",