emailjs 4.0.3 → 5.0.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/README.md +215 -247
- package/dist/address.d.ts +6 -0
- package/dist/address.js +183 -0
- package/dist/address.js.map +1 -0
- package/dist/address.test.d.ts +1 -0
- package/dist/address.test.js +120 -0
- package/dist/address.test.js.map +1 -0
- package/dist/auth.integration.test.d.ts +1 -0
- package/dist/auth.integration.test.js +137 -0
- package/dist/auth.integration.test.js.map +1 -0
- package/dist/client.d.ts +39 -0
- package/dist/client.integration.test.d.ts +1 -0
- package/dist/client.integration.test.js +198 -0
- package/dist/client.integration.test.js.map +1 -0
- package/dist/client.js +196 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +260 -0
- package/dist/client.test.js.map +1 -0
- package/dist/connection.commands.integration.test.d.ts +1 -0
- package/dist/connection.commands.integration.test.js +79 -0
- package/dist/connection.commands.integration.test.js.map +1 -0
- package/dist/connection.d.ts +83 -0
- package/dist/connection.errors.integration.test.d.ts +1 -0
- package/dist/connection.errors.integration.test.js +186 -0
- package/dist/connection.errors.integration.test.js.map +1 -0
- package/dist/connection.greylist.integration.test.d.ts +1 -0
- package/dist/connection.greylist.integration.test.js +53 -0
- package/dist/connection.greylist.integration.test.js.map +1 -0
- package/dist/connection.integration.test.d.ts +1 -0
- package/dist/connection.integration.test.js +223 -0
- package/dist/connection.integration.test.js.map +1 -0
- package/dist/connection.js +491 -0
- package/dist/connection.js.map +1 -0
- package/dist/connection.test.d.ts +1 -0
- package/dist/connection.test.js +50 -0
- package/dist/connection.test.js.map +1 -0
- package/dist/date.d.ts +3 -0
- package/dist/date.js +26 -0
- package/dist/date.js.map +1 -0
- package/dist/date.test.d.ts +1 -0
- package/dist/date.test.js +26 -0
- package/dist/date.test.js.map +1 -0
- package/dist/error.d.ts +20 -0
- package/dist/error.js +31 -0
- package/dist/error.js.map +1 -0
- package/dist/error.test.d.ts +1 -0
- package/dist/error.test.js +23 -0
- package/dist/error.test.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/message.d.ts +71 -0
- package/dist/message.integration.test.d.ts +1 -0
- package/dist/message.integration.test.js +401 -0
- package/dist/message.integration.test.js.map +1 -0
- package/dist/message.js +456 -0
- package/dist/message.js.map +1 -0
- package/dist/message.test.d.ts +1 -0
- package/dist/message.test.js +258 -0
- package/dist/message.test.js.map +1 -0
- package/dist/mime.d.ts +2 -0
- package/dist/mime.js +145 -0
- package/dist/mime.js.map +1 -0
- package/dist/mime.test.d.ts +1 -0
- package/dist/mime.test.js +76 -0
- package/dist/mime.test.js.map +1 -0
- package/dist/response.d.ts +6 -0
- package/dist/response.js +93 -0
- package/dist/response.js.map +1 -0
- package/dist/response.test.d.ts +1 -0
- package/dist/response.test.js +177 -0
- package/dist/response.test.js.map +1 -0
- package/package.json +30 -28
- package/email.js +0 -2126
- package/email.ts +0 -8
- package/smtp/address.ts +0 -237
- package/smtp/client.ts +0 -365
- package/smtp/connection.ts +0 -937
- package/smtp/date.ts +0 -51
- package/smtp/error.ts +0 -57
- package/smtp/message.ts +0 -774
- package/smtp/mime.ts +0 -234
- package/smtp/response.ts +0 -112
package/smtp/message.ts
DELETED
|
@@ -1,774 +0,0 @@
|
|
|
1
|
-
import type { PathLike } from 'fs';
|
|
2
|
-
import {
|
|
3
|
-
existsSync,
|
|
4
|
-
open as openFile,
|
|
5
|
-
close as closeFile,
|
|
6
|
-
closeSync as closeFileSync,
|
|
7
|
-
read as readFile,
|
|
8
|
-
} from 'fs';
|
|
9
|
-
import { hostname } from 'os';
|
|
10
|
-
import { Stream } from 'stream';
|
|
11
|
-
import type { Readable } from 'stream';
|
|
12
|
-
|
|
13
|
-
import { addressparser } from './address.js';
|
|
14
|
-
import { getRFC2822Date } from './date.js';
|
|
15
|
-
import { mimeWordEncode } from './mime.js';
|
|
16
|
-
|
|
17
|
-
const CRLF = '\r\n' as const;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* MIME standard wants 76 char chunks when sending out.
|
|
21
|
-
*/
|
|
22
|
-
export const MIMECHUNK = 76 as const;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* meets both base64 and mime divisibility
|
|
26
|
-
*/
|
|
27
|
-
export const MIME64CHUNK = (MIMECHUNK * 6) as 456;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* size of the message stream buffer
|
|
31
|
-
*/
|
|
32
|
-
export const BUFFERSIZE = (MIMECHUNK * 24 * 7) as 12768;
|
|
33
|
-
|
|
34
|
-
export interface MessageAttachmentHeaders {
|
|
35
|
-
[index: string]: string | undefined;
|
|
36
|
-
'content-type'?: string;
|
|
37
|
-
'content-transfer-encoding'?: BufferEncoding | '7bit' | '8bit';
|
|
38
|
-
'content-disposition'?: string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface MessageAttachment {
|
|
42
|
-
[index: string]:
|
|
43
|
-
| string
|
|
44
|
-
| boolean
|
|
45
|
-
| MessageAttachment
|
|
46
|
-
| MessageAttachment[]
|
|
47
|
-
| MessageAttachmentHeaders
|
|
48
|
-
| Readable
|
|
49
|
-
| PathLike
|
|
50
|
-
| undefined;
|
|
51
|
-
name?: string;
|
|
52
|
-
headers?: MessageAttachmentHeaders;
|
|
53
|
-
inline?: boolean;
|
|
54
|
-
alternative?: MessageAttachment | boolean;
|
|
55
|
-
related?: MessageAttachment[];
|
|
56
|
-
data?: string;
|
|
57
|
-
encoded?: boolean;
|
|
58
|
-
stream?: Readable;
|
|
59
|
-
path?: PathLike;
|
|
60
|
-
type?: string;
|
|
61
|
-
charset?: string;
|
|
62
|
-
method?: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface MessageHeaders {
|
|
66
|
-
[index: string]:
|
|
67
|
-
| boolean
|
|
68
|
-
| string
|
|
69
|
-
| string[]
|
|
70
|
-
| null
|
|
71
|
-
| undefined
|
|
72
|
-
| MessageAttachment
|
|
73
|
-
| MessageAttachment[];
|
|
74
|
-
'content-type'?: string;
|
|
75
|
-
'message-id'?: string;
|
|
76
|
-
'return-path'?: string | null;
|
|
77
|
-
date?: string;
|
|
78
|
-
from: string | string[];
|
|
79
|
-
to: string | string[];
|
|
80
|
-
cc?: string | string[];
|
|
81
|
-
bcc?: string | string[];
|
|
82
|
-
subject: string;
|
|
83
|
-
text: string | null;
|
|
84
|
-
attachment?: MessageAttachment | MessageAttachment[];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let counter = 0;
|
|
88
|
-
|
|
89
|
-
function generateBoundary() {
|
|
90
|
-
let text = '';
|
|
91
|
-
const possible =
|
|
92
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+_,-./:=?";
|
|
93
|
-
|
|
94
|
-
for (let i = 0; i < 69; i++) {
|
|
95
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return text;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function convertPersonToAddress(person: string | string[]) {
|
|
102
|
-
return addressparser(person)
|
|
103
|
-
.map(({ name, address }) => {
|
|
104
|
-
return name
|
|
105
|
-
? `${mimeWordEncode(name).replace(/,/g, '=2C')} <${address}>`
|
|
106
|
-
: address;
|
|
107
|
-
})
|
|
108
|
-
.join(', ');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function convertDashDelimitedTextToSnakeCase(text: string) {
|
|
112
|
-
return text
|
|
113
|
-
.toLowerCase()
|
|
114
|
-
.replace(/^(.)|-(.)/g, (match) => match.toUpperCase());
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export class Message {
|
|
118
|
-
public readonly attachments: MessageAttachment[] = [];
|
|
119
|
-
public readonly header: Partial<MessageHeaders> = {
|
|
120
|
-
'message-id': `<${new Date().getTime()}.${counter++}.${
|
|
121
|
-
process.pid
|
|
122
|
-
}@${hostname()}>`,
|
|
123
|
-
date: getRFC2822Date(),
|
|
124
|
-
};
|
|
125
|
-
public readonly content: string = 'text/plain; charset=utf-8';
|
|
126
|
-
public readonly text?: string;
|
|
127
|
-
public alternative: MessageAttachment | null = null;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Construct an rfc2822-compliant message object.
|
|
131
|
-
*
|
|
132
|
-
* Special notes:
|
|
133
|
-
* - The `from` field is required.
|
|
134
|
-
* - At least one `to`, `cc`, or `bcc` header is also required.
|
|
135
|
-
* - You can also add whatever other headers you want.
|
|
136
|
-
*
|
|
137
|
-
* @see https://tools.ietf.org/html/rfc2822
|
|
138
|
-
* @param {Partial<MessageHeaders>} headers Message headers
|
|
139
|
-
*/
|
|
140
|
-
constructor(headers: Partial<MessageHeaders>) {
|
|
141
|
-
for (const header in headers) {
|
|
142
|
-
// allow user to override default content-type to override charset or send a single non-text message
|
|
143
|
-
if (/^content-type$/i.test(header)) {
|
|
144
|
-
this.content = headers[header] as string;
|
|
145
|
-
} else if (header === 'text') {
|
|
146
|
-
this.text = headers[header] as string;
|
|
147
|
-
} else if (
|
|
148
|
-
header === 'attachment' &&
|
|
149
|
-
typeof headers[header] === 'object'
|
|
150
|
-
) {
|
|
151
|
-
const attachment = headers[header];
|
|
152
|
-
if (Array.isArray(attachment)) {
|
|
153
|
-
for (let i = 0; i < attachment.length; i++) {
|
|
154
|
-
this.attach(attachment[i]);
|
|
155
|
-
}
|
|
156
|
-
} else if (attachment != null) {
|
|
157
|
-
this.attach(attachment);
|
|
158
|
-
}
|
|
159
|
-
} else if (header === 'subject') {
|
|
160
|
-
this.header.subject = mimeWordEncode(headers.subject as string);
|
|
161
|
-
} else if (/^(cc|bcc|to|from)/i.test(header)) {
|
|
162
|
-
this.header[header.toLowerCase()] = convertPersonToAddress(
|
|
163
|
-
headers[header] as string | string[]
|
|
164
|
-
);
|
|
165
|
-
} else {
|
|
166
|
-
// allow any headers the user wants to set??
|
|
167
|
-
this.header[header.toLowerCase()] = headers[header];
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Attach a file to the message.
|
|
174
|
-
*
|
|
175
|
-
* Can be called multiple times, each adding a new attachment.
|
|
176
|
-
*
|
|
177
|
-
* @public
|
|
178
|
-
* @param {MessageAttachment} options attachment options
|
|
179
|
-
* @returns {Message} the current instance for chaining
|
|
180
|
-
*/
|
|
181
|
-
public attach(options: MessageAttachment) {
|
|
182
|
-
// sender can specify an attachment as an alternative
|
|
183
|
-
if (options.alternative) {
|
|
184
|
-
this.alternative = options;
|
|
185
|
-
this.alternative.charset = options.charset || 'utf-8';
|
|
186
|
-
this.alternative.type = options.type || 'text/html';
|
|
187
|
-
this.alternative.inline = true;
|
|
188
|
-
} else {
|
|
189
|
-
this.attachments.push(options);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* @public
|
|
197
|
-
* @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
|
|
198
|
-
*/
|
|
199
|
-
public checkValidity() {
|
|
200
|
-
if (
|
|
201
|
-
typeof this.header.from !== 'string' &&
|
|
202
|
-
Array.isArray(this.header.from) === false
|
|
203
|
-
) {
|
|
204
|
-
return {
|
|
205
|
-
isValid: false,
|
|
206
|
-
validationError: 'Message must have a `from` header',
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
typeof this.header.to !== 'string' &&
|
|
212
|
-
Array.isArray(this.header.to) === false &&
|
|
213
|
-
typeof this.header.cc !== 'string' &&
|
|
214
|
-
Array.isArray(this.header.cc) === false &&
|
|
215
|
-
typeof this.header.bcc !== 'string' &&
|
|
216
|
-
Array.isArray(this.header.bcc) === false
|
|
217
|
-
) {
|
|
218
|
-
return {
|
|
219
|
-
isValid: false,
|
|
220
|
-
validationError:
|
|
221
|
-
'Message must have at least one `to`, `cc`, or `bcc` header',
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (this.attachments.length > 0) {
|
|
226
|
-
const failed: string[] = [];
|
|
227
|
-
|
|
228
|
-
this.attachments.forEach((attachment) => {
|
|
229
|
-
if (attachment.path) {
|
|
230
|
-
if (existsSync(attachment.path) === false) {
|
|
231
|
-
failed.push(`${attachment.path} does not exist`);
|
|
232
|
-
}
|
|
233
|
-
} else if (attachment.stream) {
|
|
234
|
-
if (!attachment.stream.readable) {
|
|
235
|
-
failed.push('attachment stream is not readable');
|
|
236
|
-
}
|
|
237
|
-
} else if (!attachment.data) {
|
|
238
|
-
failed.push('attachment has no data associated with it');
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
return {
|
|
242
|
-
isValid: failed.length === 0,
|
|
243
|
-
validationError: failed.join(', '),
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return { isValid: true, validationError: undefined };
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* @public
|
|
252
|
-
* @deprecated does not conform to the `errback` style followed by the rest of the library, and will be removed in the next major version. use `checkValidity` instead.
|
|
253
|
-
* @param {function(isValid: boolean, invalidReason: (string | undefined)): void} callback .
|
|
254
|
-
* @returns {void}
|
|
255
|
-
*/
|
|
256
|
-
public valid(callback: (isValid: boolean, invalidReason?: string) => void) {
|
|
257
|
-
const { isValid, validationError } = this.checkValidity();
|
|
258
|
-
callback(isValid, validationError);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* @public
|
|
263
|
-
* @returns {MessageStream} a stream of the current message
|
|
264
|
-
*/
|
|
265
|
-
public stream() {
|
|
266
|
-
return new MessageStream(this);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* @public
|
|
271
|
-
* @param {function(Error, string): void} callback the function to call with the error and buffer
|
|
272
|
-
* @returns {void}
|
|
273
|
-
*/
|
|
274
|
-
public read(callback: (err: Error, buffer: string) => void) {
|
|
275
|
-
let buffer = '';
|
|
276
|
-
const str = this.stream();
|
|
277
|
-
str.on('data', (data) => (buffer += data));
|
|
278
|
-
str.on('end', (err) => callback(err, buffer));
|
|
279
|
-
str.on('error', (err) => callback(err, buffer));
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
public readAsync() {
|
|
283
|
-
return new Promise<string>((resolve, reject) => {
|
|
284
|
-
this.read((err, buffer) => {
|
|
285
|
-
if (err != null) {
|
|
286
|
-
reject(err);
|
|
287
|
-
} else {
|
|
288
|
-
resolve(buffer);
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
class MessageStream extends Stream {
|
|
296
|
-
readable = true;
|
|
297
|
-
paused = false;
|
|
298
|
-
buffer: Buffer | null = Buffer.alloc(MIMECHUNK * 24 * 7);
|
|
299
|
-
bufferIndex = 0;
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* @param {Message} message the message to stream
|
|
303
|
-
*/
|
|
304
|
-
constructor(private message: Message) {
|
|
305
|
-
super();
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* @param {string} [data] the data to output
|
|
309
|
-
* @param {Function} [callback] the function
|
|
310
|
-
* @param {any[]} [args] array of arguments to pass to the callback
|
|
311
|
-
* @returns {void}
|
|
312
|
-
*/
|
|
313
|
-
const output = (data: string) => {
|
|
314
|
-
// can we buffer the data?
|
|
315
|
-
if (this.buffer != null) {
|
|
316
|
-
const bytes = Buffer.byteLength(data);
|
|
317
|
-
|
|
318
|
-
if (bytes + this.bufferIndex < this.buffer.length) {
|
|
319
|
-
this.buffer.write(data, this.bufferIndex);
|
|
320
|
-
this.bufferIndex += bytes;
|
|
321
|
-
}
|
|
322
|
-
// we can't buffer the data, so ship it out!
|
|
323
|
-
else if (bytes > this.buffer.length) {
|
|
324
|
-
if (this.bufferIndex) {
|
|
325
|
-
this.emit(
|
|
326
|
-
'data',
|
|
327
|
-
this.buffer.toString('utf-8', 0, this.bufferIndex)
|
|
328
|
-
);
|
|
329
|
-
this.bufferIndex = 0;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const loops = Math.ceil(data.length / this.buffer.length);
|
|
333
|
-
let loop = 0;
|
|
334
|
-
while (loop < loops) {
|
|
335
|
-
this.emit(
|
|
336
|
-
'data',
|
|
337
|
-
data.substring(
|
|
338
|
-
this.buffer.length * loop,
|
|
339
|
-
this.buffer.length * (loop + 1)
|
|
340
|
-
)
|
|
341
|
-
);
|
|
342
|
-
loop++;
|
|
343
|
-
}
|
|
344
|
-
} // we need to clean out the buffer, it is getting full
|
|
345
|
-
else {
|
|
346
|
-
if (!this.paused) {
|
|
347
|
-
this.emit(
|
|
348
|
-
'data',
|
|
349
|
-
this.buffer.toString('utf-8', 0, this.bufferIndex)
|
|
350
|
-
);
|
|
351
|
-
this.buffer.write(data, 0);
|
|
352
|
-
this.bufferIndex = bytes;
|
|
353
|
-
} else {
|
|
354
|
-
// we can't empty out the buffer, so let's wait till we resume before adding to it
|
|
355
|
-
this.once('resume', () => output(data));
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @param {MessageAttachment} [attachment] the attachment whose headers you would like to output
|
|
363
|
-
* @returns {void}
|
|
364
|
-
*/
|
|
365
|
-
const outputAttachmentHeaders = (attachment: MessageAttachment) => {
|
|
366
|
-
let data: string[] = [];
|
|
367
|
-
const headers: Partial<MessageHeaders> = {
|
|
368
|
-
'content-type':
|
|
369
|
-
attachment.type +
|
|
370
|
-
(attachment.charset ? `; charset=${attachment.charset}` : '') +
|
|
371
|
-
(attachment.method ? `; method=${attachment.method}` : ''),
|
|
372
|
-
'content-transfer-encoding': 'base64',
|
|
373
|
-
'content-disposition': attachment.inline
|
|
374
|
-
? 'inline'
|
|
375
|
-
: `attachment; filename="${mimeWordEncode(
|
|
376
|
-
attachment.name as string
|
|
377
|
-
)}"`,
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
// allow sender to override default headers
|
|
381
|
-
if (attachment.headers != null) {
|
|
382
|
-
for (const header in attachment.headers) {
|
|
383
|
-
headers[header.toLowerCase()] = attachment.headers[header];
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
for (const header in headers) {
|
|
388
|
-
data = data.concat([
|
|
389
|
-
convertDashDelimitedTextToSnakeCase(header),
|
|
390
|
-
': ',
|
|
391
|
-
headers[header] as string,
|
|
392
|
-
CRLF,
|
|
393
|
-
]);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
output(data.concat([CRLF]).join(''));
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* @param {string} data the data to output as base64
|
|
401
|
-
* @param {function(): void} [callback] the function to call after output is finished
|
|
402
|
-
* @returns {void}
|
|
403
|
-
*/
|
|
404
|
-
const outputBase64 = (data: string, callback?: () => void) => {
|
|
405
|
-
const loops = Math.ceil(data.length / MIMECHUNK);
|
|
406
|
-
let loop = 0;
|
|
407
|
-
while (loop < loops) {
|
|
408
|
-
output(data.substring(MIMECHUNK * loop, MIMECHUNK * (loop + 1)) + CRLF);
|
|
409
|
-
loop++;
|
|
410
|
-
}
|
|
411
|
-
if (callback) {
|
|
412
|
-
callback();
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const outputFile = (
|
|
417
|
-
attachment: MessageAttachment,
|
|
418
|
-
next: (err: NodeJS.ErrnoException | null) => void
|
|
419
|
-
) => {
|
|
420
|
-
const chunk = MIME64CHUNK * 16;
|
|
421
|
-
const buffer = Buffer.alloc(chunk);
|
|
422
|
-
|
|
423
|
-
const inputEncoding =
|
|
424
|
-
attachment?.headers?.['content-transfer-encoding'] || 'base64';
|
|
425
|
-
const encoding =
|
|
426
|
-
inputEncoding === '7bit'
|
|
427
|
-
? 'ascii'
|
|
428
|
-
: inputEncoding === '8bit'
|
|
429
|
-
? 'binary'
|
|
430
|
-
: inputEncoding;
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* @param {Error} err the error to emit
|
|
434
|
-
* @param {number} fd the file descriptor
|
|
435
|
-
* @returns {void}
|
|
436
|
-
*/
|
|
437
|
-
const opened = (err: NodeJS.ErrnoException | null, fd: number) => {
|
|
438
|
-
if (err) {
|
|
439
|
-
this.emit('error', err);
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
const readBytes = (
|
|
443
|
-
err: NodeJS.ErrnoException | null,
|
|
444
|
-
bytes: number
|
|
445
|
-
) => {
|
|
446
|
-
if (err || this.readable === false) {
|
|
447
|
-
this.emit(
|
|
448
|
-
'error',
|
|
449
|
-
err || new Error('message stream was interrupted somehow!')
|
|
450
|
-
);
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
// guaranteed to be encoded without padding unless it is our last read
|
|
454
|
-
outputBase64(buffer.toString(encoding, 0, bytes), () => {
|
|
455
|
-
if (bytes == chunk) {
|
|
456
|
-
// we read a full chunk, there might be more
|
|
457
|
-
readFile(fd, buffer, 0, chunk, null, readBytes);
|
|
458
|
-
} // that was the last chunk, we are done reading the file
|
|
459
|
-
else {
|
|
460
|
-
this.removeListener('error', closeFileSync);
|
|
461
|
-
closeFile(fd, next);
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
};
|
|
465
|
-
readFile(fd, buffer, 0, chunk, null, readBytes);
|
|
466
|
-
this.once('error', closeFileSync);
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
openFile(attachment.path as PathLike, 'r', opened);
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
474
|
-
* @param {function(): void} callback the function to call after output is finished
|
|
475
|
-
* @returns {void}
|
|
476
|
-
*/
|
|
477
|
-
const outputStream = (
|
|
478
|
-
attachment: MessageAttachment,
|
|
479
|
-
callback: () => void
|
|
480
|
-
) => {
|
|
481
|
-
const { stream } = attachment;
|
|
482
|
-
if (stream?.readable) {
|
|
483
|
-
let previous = Buffer.alloc(0);
|
|
484
|
-
|
|
485
|
-
stream.resume();
|
|
486
|
-
|
|
487
|
-
stream.on('end', () => {
|
|
488
|
-
outputBase64(previous.toString('base64'), callback);
|
|
489
|
-
this.removeListener('pause', stream.pause);
|
|
490
|
-
this.removeListener('resume', stream.resume);
|
|
491
|
-
this.removeListener('error', stream.resume);
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
stream.on('data', (buff) => {
|
|
495
|
-
// do we have bytes from a previous stream data event?
|
|
496
|
-
let buffer = Buffer.isBuffer(buff) ? buff : Buffer.from(buff);
|
|
497
|
-
|
|
498
|
-
if (previous.byteLength > 0) {
|
|
499
|
-
buffer = Buffer.concat([previous, buffer]);
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
const padded = buffer.length % MIME64CHUNK;
|
|
503
|
-
previous = Buffer.alloc(padded);
|
|
504
|
-
|
|
505
|
-
// encode as much of the buffer to base64 without empty bytes
|
|
506
|
-
if (padded > 0) {
|
|
507
|
-
// copy dangling bytes into previous buffer
|
|
508
|
-
buffer.copy(previous, 0, buffer.length - padded);
|
|
509
|
-
}
|
|
510
|
-
outputBase64(buffer.toString('base64', 0, buffer.length - padded));
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
this.on('pause', stream.pause);
|
|
514
|
-
this.on('resume', stream.resume);
|
|
515
|
-
this.on('error', stream.resume);
|
|
516
|
-
} else {
|
|
517
|
-
this.emit('error', { message: 'stream not readable' });
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
const outputAttachment = (
|
|
522
|
-
attachment: MessageAttachment,
|
|
523
|
-
callback: () => void
|
|
524
|
-
) => {
|
|
525
|
-
const build = attachment.path
|
|
526
|
-
? outputFile
|
|
527
|
-
: attachment.stream
|
|
528
|
-
? outputStream
|
|
529
|
-
: outputData;
|
|
530
|
-
outputAttachmentHeaders(attachment);
|
|
531
|
-
build(attachment, callback);
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* @param {string} boundary the boundary text between outputs
|
|
536
|
-
* @param {MessageAttachment[]} list the list of potential messages to output
|
|
537
|
-
* @param {number} index the index of the list item to output
|
|
538
|
-
* @param {function(): void} callback the function to call if index is greater than upper bound
|
|
539
|
-
* @returns {void}
|
|
540
|
-
*/
|
|
541
|
-
const outputMessage = (
|
|
542
|
-
boundary: string,
|
|
543
|
-
list: MessageAttachment[],
|
|
544
|
-
index: number,
|
|
545
|
-
callback: () => void
|
|
546
|
-
) => {
|
|
547
|
-
if (index < list.length) {
|
|
548
|
-
output(`--${boundary}${CRLF}`);
|
|
549
|
-
if (list[index].related) {
|
|
550
|
-
outputRelated(list[index], () =>
|
|
551
|
-
outputMessage(boundary, list, index + 1, callback)
|
|
552
|
-
);
|
|
553
|
-
} else {
|
|
554
|
-
outputAttachment(list[index], () =>
|
|
555
|
-
outputMessage(boundary, list, index + 1, callback)
|
|
556
|
-
);
|
|
557
|
-
}
|
|
558
|
-
} else {
|
|
559
|
-
output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
|
|
560
|
-
callback();
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
const outputMixed = () => {
|
|
565
|
-
const boundary = generateBoundary();
|
|
566
|
-
output(
|
|
567
|
-
`Content-Type: multipart/mixed; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
|
|
568
|
-
);
|
|
569
|
-
|
|
570
|
-
if (this.message.alternative == null) {
|
|
571
|
-
outputText(this.message);
|
|
572
|
-
outputMessage(boundary, this.message.attachments, 0, close);
|
|
573
|
-
} else {
|
|
574
|
-
outputAlternative(
|
|
575
|
-
// typescript bug; should narrow to { alternative: MessageAttachment }
|
|
576
|
-
this.message as Parameters<typeof outputAlternative>[0],
|
|
577
|
-
() => outputMessage(boundary, this.message.attachments, 0, close)
|
|
578
|
-
);
|
|
579
|
-
}
|
|
580
|
-
};
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
584
|
-
* @param {function(): void} callback the function to call after output is finished
|
|
585
|
-
* @returns {void}
|
|
586
|
-
*/
|
|
587
|
-
const outputData = (
|
|
588
|
-
attachment: MessageAttachment,
|
|
589
|
-
callback: () => void
|
|
590
|
-
) => {
|
|
591
|
-
outputBase64(
|
|
592
|
-
attachment.encoded
|
|
593
|
-
? attachment.data ?? ''
|
|
594
|
-
: Buffer.from(attachment.data ?? '').toString('base64'),
|
|
595
|
-
callback
|
|
596
|
-
);
|
|
597
|
-
};
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* @param {Message} message the message to output
|
|
601
|
-
* @returns {void}
|
|
602
|
-
*/
|
|
603
|
-
const outputText = (message: Message) => {
|
|
604
|
-
let data: string[] = [];
|
|
605
|
-
|
|
606
|
-
data = data.concat([
|
|
607
|
-
'Content-Type:',
|
|
608
|
-
message.content,
|
|
609
|
-
CRLF,
|
|
610
|
-
'Content-Transfer-Encoding: 7bit',
|
|
611
|
-
CRLF,
|
|
612
|
-
]);
|
|
613
|
-
data = data.concat(['Content-Disposition: inline', CRLF, CRLF]);
|
|
614
|
-
data = data.concat([message.text || '', CRLF, CRLF]);
|
|
615
|
-
|
|
616
|
-
output(data.join(''));
|
|
617
|
-
};
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* @param {MessageAttachment} message the message to output
|
|
621
|
-
* @param {function(): void} callback the function to call after output is finished
|
|
622
|
-
* @returns {void}
|
|
623
|
-
*/
|
|
624
|
-
const outputRelated = (
|
|
625
|
-
message: MessageAttachment,
|
|
626
|
-
callback: () => void
|
|
627
|
-
) => {
|
|
628
|
-
const boundary = generateBoundary();
|
|
629
|
-
output(
|
|
630
|
-
`Content-Type: multipart/related; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
|
|
631
|
-
);
|
|
632
|
-
outputAttachment(message, () => {
|
|
633
|
-
outputMessage(boundary, message.related ?? [], 0, () => {
|
|
634
|
-
output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
|
|
635
|
-
callback();
|
|
636
|
-
});
|
|
637
|
-
});
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* @param {Message} message the message to output
|
|
642
|
-
* @param {function(): void} callback the function to call after output is finished
|
|
643
|
-
* @returns {void}
|
|
644
|
-
*/
|
|
645
|
-
const outputAlternative = (
|
|
646
|
-
message: Message & { alternative: MessageAttachment },
|
|
647
|
-
callback: () => void
|
|
648
|
-
) => {
|
|
649
|
-
const boundary = generateBoundary();
|
|
650
|
-
output(
|
|
651
|
-
`Content-Type: multipart/alternative; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
|
|
652
|
-
);
|
|
653
|
-
outputText(message);
|
|
654
|
-
output(`--${boundary}${CRLF}`);
|
|
655
|
-
|
|
656
|
-
/**
|
|
657
|
-
* @returns {void}
|
|
658
|
-
*/
|
|
659
|
-
const finish = () => {
|
|
660
|
-
output([CRLF, '--', boundary, '--', CRLF, CRLF].join(''));
|
|
661
|
-
callback();
|
|
662
|
-
};
|
|
663
|
-
|
|
664
|
-
if (message.alternative.related) {
|
|
665
|
-
outputRelated(message.alternative, finish);
|
|
666
|
-
} else {
|
|
667
|
-
outputAttachment(message.alternative, finish);
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
const close = (err?: Error) => {
|
|
672
|
-
if (err) {
|
|
673
|
-
this.emit('error', err);
|
|
674
|
-
} else {
|
|
675
|
-
this.emit(
|
|
676
|
-
'data',
|
|
677
|
-
this.buffer?.toString('utf-8', 0, this.bufferIndex) ?? ''
|
|
678
|
-
);
|
|
679
|
-
this.emit('end');
|
|
680
|
-
}
|
|
681
|
-
this.buffer = null;
|
|
682
|
-
this.bufferIndex = 0;
|
|
683
|
-
this.readable = false;
|
|
684
|
-
this.removeAllListeners('resume');
|
|
685
|
-
this.removeAllListeners('pause');
|
|
686
|
-
this.removeAllListeners('error');
|
|
687
|
-
this.removeAllListeners('data');
|
|
688
|
-
this.removeAllListeners('end');
|
|
689
|
-
};
|
|
690
|
-
|
|
691
|
-
/**
|
|
692
|
-
* @returns {void}
|
|
693
|
-
*/
|
|
694
|
-
const outputHeaderData = () => {
|
|
695
|
-
if (this.message.attachments.length || this.message.alternative) {
|
|
696
|
-
output(`MIME-Version: 1.0${CRLF}`);
|
|
697
|
-
outputMixed();
|
|
698
|
-
} // you only have a text message!
|
|
699
|
-
else {
|
|
700
|
-
outputText(this.message);
|
|
701
|
-
close();
|
|
702
|
-
}
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
/**
|
|
706
|
-
* @returns {void}
|
|
707
|
-
*/
|
|
708
|
-
const outputHeader = () => {
|
|
709
|
-
let data: string[] = [];
|
|
710
|
-
|
|
711
|
-
for (const header in this.message.header) {
|
|
712
|
-
// do not output BCC in the headers (regex) nor custom Object.prototype functions...
|
|
713
|
-
if (
|
|
714
|
-
!/bcc/i.test(header) &&
|
|
715
|
-
Object.prototype.hasOwnProperty.call(this.message.header, header)
|
|
716
|
-
) {
|
|
717
|
-
data = data.concat([
|
|
718
|
-
convertDashDelimitedTextToSnakeCase(header),
|
|
719
|
-
': ',
|
|
720
|
-
this.message.header[header] as string,
|
|
721
|
-
CRLF,
|
|
722
|
-
]);
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
output(data.join(''));
|
|
727
|
-
outputHeaderData();
|
|
728
|
-
};
|
|
729
|
-
|
|
730
|
-
this.once('destroy', close);
|
|
731
|
-
process.nextTick(outputHeader);
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
/**
|
|
735
|
-
* @public
|
|
736
|
-
* pause the stream
|
|
737
|
-
* @returns {void}
|
|
738
|
-
*/
|
|
739
|
-
public pause() {
|
|
740
|
-
this.paused = true;
|
|
741
|
-
this.emit('pause');
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* @public
|
|
746
|
-
* resume the stream
|
|
747
|
-
* @returns {void}
|
|
748
|
-
*/
|
|
749
|
-
public resume() {
|
|
750
|
-
this.paused = false;
|
|
751
|
-
this.emit('resume');
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* @public
|
|
756
|
-
* destroy the stream
|
|
757
|
-
* @returns {void}
|
|
758
|
-
*/
|
|
759
|
-
public destroy() {
|
|
760
|
-
this.emit(
|
|
761
|
-
'destroy',
|
|
762
|
-
this.bufferIndex > 0 ? { message: 'message stream destroyed' } : null
|
|
763
|
-
);
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* @public
|
|
768
|
-
* destroy the stream at first opportunity
|
|
769
|
-
* @returns {void}
|
|
770
|
-
*/
|
|
771
|
-
public destroySoon() {
|
|
772
|
-
this.emit('destroy');
|
|
773
|
-
}
|
|
774
|
-
}
|