@upyo/smtp 0.1.0 → 0.1.2-dev.50
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 +20 -0
- package/README.md +1 -6
- package/dist/index.cjs +91 -11
- package/dist/index.js +92 -12
- package/package.json +2 -2
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;
|
|
@@ -278,10 +321,10 @@ async function buildRawMessage(message) {
|
|
|
278
321
|
const hasHtml = "html" in message.content;
|
|
279
322
|
const hasText = "text" in message.content;
|
|
280
323
|
const isMultipart = hasAttachments || hasHtml && hasText;
|
|
281
|
-
lines.push(`From: ${(0, __upyo_core.formatAddress)(message.sender)}`);
|
|
282
|
-
lines.push(`To: ${message.recipients.map(__upyo_core.formatAddress).join(", ")}`);
|
|
283
|
-
if (message.ccRecipients.length > 0) lines.push(`Cc: ${message.ccRecipients.map(__upyo_core.formatAddress).join(", ")}`);
|
|
284
|
-
if (message.replyRecipients.length > 0) lines.push(`Reply-To: ${message.replyRecipients.map(__upyo_core.formatAddress).join(", ")}`);
|
|
324
|
+
lines.push(`From: ${encodeHeaderValue((0, __upyo_core.formatAddress)(message.sender))}`);
|
|
325
|
+
lines.push(`To: ${encodeHeaderValue(message.recipients.map(__upyo_core.formatAddress).join(", "))}`);
|
|
326
|
+
if (message.ccRecipients.length > 0) lines.push(`Cc: ${encodeHeaderValue(message.ccRecipients.map(__upyo_core.formatAddress).join(", "))}`);
|
|
327
|
+
if (message.replyRecipients.length > 0) lines.push(`Reply-To: ${encodeHeaderValue(message.replyRecipients.map(__upyo_core.formatAddress).join(", "))}`);
|
|
285
328
|
lines.push(`Subject: ${encodeHeaderValue(message.subject)}`);
|
|
286
329
|
lines.push(`Date: ${(/* @__PURE__ */ new Date()).toUTCString()}`);
|
|
287
330
|
lines.push(`Message-ID: <${generateMessageId()}>`);
|
|
@@ -365,17 +408,48 @@ function encodeHeaderValue(value) {
|
|
|
365
408
|
if (!/^[\x20-\x7E]*$/.test(value)) {
|
|
366
409
|
const utf8Bytes = new TextEncoder().encode(value);
|
|
367
410
|
const base64 = btoa(String.fromCharCode(...utf8Bytes));
|
|
368
|
-
|
|
411
|
+
const maxEncodedLength = 75;
|
|
412
|
+
const encodedWord = `=?UTF-8?B?${base64}?=`;
|
|
413
|
+
if (encodedWord.length <= maxEncodedLength) return encodedWord;
|
|
414
|
+
const words = [];
|
|
415
|
+
let currentBase64 = "";
|
|
416
|
+
for (let i = 0; i < base64.length; i += 4) {
|
|
417
|
+
const chunk = base64.slice(i, i + 4);
|
|
418
|
+
const testWord = `=?UTF-8?B?${currentBase64}${chunk}?=`;
|
|
419
|
+
if (testWord.length <= maxEncodedLength) currentBase64 += chunk;
|
|
420
|
+
else {
|
|
421
|
+
if (currentBase64) words.push(`=?UTF-8?B?${currentBase64}?=`);
|
|
422
|
+
currentBase64 = chunk;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (currentBase64) words.push(`=?UTF-8?B?${currentBase64}?=`);
|
|
426
|
+
return words.join(" ");
|
|
369
427
|
}
|
|
370
428
|
return value;
|
|
371
429
|
}
|
|
372
430
|
function encodeQuotedPrintable(text) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
431
|
+
const utf8Bytes = new TextEncoder().encode(text);
|
|
432
|
+
let result = "";
|
|
433
|
+
let lineLength = 0;
|
|
434
|
+
const maxLineLength = 76;
|
|
435
|
+
for (let i = 0; i < utf8Bytes.length; i++) {
|
|
436
|
+
const byte = utf8Bytes[i];
|
|
437
|
+
let encoded = "";
|
|
438
|
+
if (byte < 32 || byte > 126 || byte === 61 || byte === 46 && lineLength === 0) encoded = `=${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
439
|
+
else encoded = String.fromCharCode(byte);
|
|
440
|
+
if (lineLength + encoded.length > maxLineLength) {
|
|
441
|
+
result += "=\r\n";
|
|
442
|
+
lineLength = 0;
|
|
443
|
+
}
|
|
444
|
+
result += encoded;
|
|
445
|
+
lineLength += encoded.length;
|
|
446
|
+
if (byte === 13 && i + 1 < utf8Bytes.length && utf8Bytes[i + 1] === 10) {
|
|
447
|
+
i++;
|
|
448
|
+
result += String.fromCharCode(10);
|
|
449
|
+
lineLength = 0;
|
|
450
|
+
} else if (byte === 10 && (i === 0 || utf8Bytes[i - 1] !== 13)) lineLength = 0;
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
379
453
|
}
|
|
380
454
|
function encodeBase64(data) {
|
|
381
455
|
const base64 = btoa(String.fromCharCode(...data));
|
|
@@ -591,6 +665,12 @@ var SmtpTransport = class {
|
|
|
591
665
|
signal?.throwIfAborted();
|
|
592
666
|
await connection.ehlo(signal);
|
|
593
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
|
+
}
|
|
594
674
|
await connection.authenticate(signal);
|
|
595
675
|
}
|
|
596
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;
|
|
@@ -255,10 +298,10 @@ async function buildRawMessage(message) {
|
|
|
255
298
|
const hasHtml = "html" in message.content;
|
|
256
299
|
const hasText = "text" in message.content;
|
|
257
300
|
const isMultipart = hasAttachments || hasHtml && hasText;
|
|
258
|
-
lines.push(`From: ${formatAddress(message.sender)}`);
|
|
259
|
-
lines.push(`To: ${message.recipients.map(formatAddress).join(", ")}`);
|
|
260
|
-
if (message.ccRecipients.length > 0) lines.push(`Cc: ${message.ccRecipients.map(formatAddress).join(", ")}`);
|
|
261
|
-
if (message.replyRecipients.length > 0) lines.push(`Reply-To: ${message.replyRecipients.map(formatAddress).join(", ")}`);
|
|
301
|
+
lines.push(`From: ${encodeHeaderValue(formatAddress(message.sender))}`);
|
|
302
|
+
lines.push(`To: ${encodeHeaderValue(message.recipients.map(formatAddress).join(", "))}`);
|
|
303
|
+
if (message.ccRecipients.length > 0) lines.push(`Cc: ${encodeHeaderValue(message.ccRecipients.map(formatAddress).join(", "))}`);
|
|
304
|
+
if (message.replyRecipients.length > 0) lines.push(`Reply-To: ${encodeHeaderValue(message.replyRecipients.map(formatAddress).join(", "))}`);
|
|
262
305
|
lines.push(`Subject: ${encodeHeaderValue(message.subject)}`);
|
|
263
306
|
lines.push(`Date: ${(/* @__PURE__ */ new Date()).toUTCString()}`);
|
|
264
307
|
lines.push(`Message-ID: <${generateMessageId()}>`);
|
|
@@ -342,17 +385,48 @@ function encodeHeaderValue(value) {
|
|
|
342
385
|
if (!/^[\x20-\x7E]*$/.test(value)) {
|
|
343
386
|
const utf8Bytes = new TextEncoder().encode(value);
|
|
344
387
|
const base64 = btoa(String.fromCharCode(...utf8Bytes));
|
|
345
|
-
|
|
388
|
+
const maxEncodedLength = 75;
|
|
389
|
+
const encodedWord = `=?UTF-8?B?${base64}?=`;
|
|
390
|
+
if (encodedWord.length <= maxEncodedLength) return encodedWord;
|
|
391
|
+
const words = [];
|
|
392
|
+
let currentBase64 = "";
|
|
393
|
+
for (let i = 0; i < base64.length; i += 4) {
|
|
394
|
+
const chunk = base64.slice(i, i + 4);
|
|
395
|
+
const testWord = `=?UTF-8?B?${currentBase64}${chunk}?=`;
|
|
396
|
+
if (testWord.length <= maxEncodedLength) currentBase64 += chunk;
|
|
397
|
+
else {
|
|
398
|
+
if (currentBase64) words.push(`=?UTF-8?B?${currentBase64}?=`);
|
|
399
|
+
currentBase64 = chunk;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (currentBase64) words.push(`=?UTF-8?B?${currentBase64}?=`);
|
|
403
|
+
return words.join(" ");
|
|
346
404
|
}
|
|
347
405
|
return value;
|
|
348
406
|
}
|
|
349
407
|
function encodeQuotedPrintable(text) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
408
|
+
const utf8Bytes = new TextEncoder().encode(text);
|
|
409
|
+
let result = "";
|
|
410
|
+
let lineLength = 0;
|
|
411
|
+
const maxLineLength = 76;
|
|
412
|
+
for (let i = 0; i < utf8Bytes.length; i++) {
|
|
413
|
+
const byte = utf8Bytes[i];
|
|
414
|
+
let encoded = "";
|
|
415
|
+
if (byte < 32 || byte > 126 || byte === 61 || byte === 46 && lineLength === 0) encoded = `=${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
416
|
+
else encoded = String.fromCharCode(byte);
|
|
417
|
+
if (lineLength + encoded.length > maxLineLength) {
|
|
418
|
+
result += "=\r\n";
|
|
419
|
+
lineLength = 0;
|
|
420
|
+
}
|
|
421
|
+
result += encoded;
|
|
422
|
+
lineLength += encoded.length;
|
|
423
|
+
if (byte === 13 && i + 1 < utf8Bytes.length && utf8Bytes[i + 1] === 10) {
|
|
424
|
+
i++;
|
|
425
|
+
result += String.fromCharCode(10);
|
|
426
|
+
lineLength = 0;
|
|
427
|
+
} else if (byte === 10 && (i === 0 || utf8Bytes[i - 1] !== 13)) lineLength = 0;
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
356
430
|
}
|
|
357
431
|
function encodeBase64(data) {
|
|
358
432
|
const base64 = btoa(String.fromCharCode(...data));
|
|
@@ -568,6 +642,12 @@ var SmtpTransport = class {
|
|
|
568
642
|
signal?.throwIfAborted();
|
|
569
643
|
await connection.ehlo(signal);
|
|
570
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
|
+
}
|
|
571
651
|
await connection.authenticate(signal);
|
|
572
652
|
}
|
|
573
653
|
async returnConnection(connection) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@upyo/smtp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2-dev.50+2a829bef",
|
|
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.
|
|
56
|
+
"@upyo/core": "0.1.2-dev.50+2a829bef"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@dotenvx/dotenvx": "^1.47.3",
|