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
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
import { Message } from './message.js';
|
|
4
|
+
describe('Message', () => {
|
|
5
|
+
it('message validation fails without `from` header', () => {
|
|
6
|
+
const msg = new Message({});
|
|
7
|
+
const { isValid, validationError } = msg.checkValidity();
|
|
8
|
+
expect(isValid).toBe(false);
|
|
9
|
+
expect(validationError).toBe('Message must have a `from` header');
|
|
10
|
+
});
|
|
11
|
+
it('message validation fails without `to`, `cc`, or `bcc` header', () => {
|
|
12
|
+
const { isValid, validationError } = new Message({
|
|
13
|
+
from: 'piglet@gmail.com',
|
|
14
|
+
}).checkValidity();
|
|
15
|
+
expect(isValid).toBe(false);
|
|
16
|
+
expect(validationError).toBe('Message must have at least one `to`, `cc`, or `bcc` header');
|
|
17
|
+
});
|
|
18
|
+
it('message validation succeeds with only `to` recipient header (string)', () => {
|
|
19
|
+
const { isValid, validationError } = new Message({
|
|
20
|
+
from: 'piglet@gmail.com',
|
|
21
|
+
to: 'pooh@gmail.com',
|
|
22
|
+
}).checkValidity();
|
|
23
|
+
expect(isValid).toBe(true);
|
|
24
|
+
expect(validationError).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
it('message validation succeeds with only `to` recipient header (array)', () => {
|
|
27
|
+
const { isValid, validationError } = new Message({
|
|
28
|
+
from: 'piglet@gmail.com',
|
|
29
|
+
to: ['pooh@gmail.com'],
|
|
30
|
+
}).checkValidity();
|
|
31
|
+
expect(isValid).toBe(true);
|
|
32
|
+
expect(validationError).toBeUndefined();
|
|
33
|
+
});
|
|
34
|
+
it('message validation succeeds with only `cc` recipient header (string)', () => {
|
|
35
|
+
const { isValid, validationError } = new Message({
|
|
36
|
+
from: 'piglet@gmail.com',
|
|
37
|
+
cc: 'pooh@gmail.com',
|
|
38
|
+
}).checkValidity();
|
|
39
|
+
expect(isValid).toBe(true);
|
|
40
|
+
expect(validationError).toBeUndefined();
|
|
41
|
+
});
|
|
42
|
+
it('message validation succeeds with only `cc` recipient header (array)', () => {
|
|
43
|
+
const { isValid, validationError } = new Message({
|
|
44
|
+
from: 'piglet@gmail.com',
|
|
45
|
+
cc: ['pooh@gmail.com'],
|
|
46
|
+
}).checkValidity();
|
|
47
|
+
expect(isValid).toBe(true);
|
|
48
|
+
expect(validationError).toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
it('message validation succeeds with only `bcc` recipient header (string)', () => {
|
|
51
|
+
const { isValid, validationError } = new Message({
|
|
52
|
+
from: 'piglet@gmail.com',
|
|
53
|
+
bcc: 'pooh@gmail.com',
|
|
54
|
+
}).checkValidity();
|
|
55
|
+
expect(isValid).toBe(true);
|
|
56
|
+
expect(validationError).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
it('message validation succeeds with only `bcc` recipient header (array)', () => {
|
|
59
|
+
const { isValid, validationError } = new Message({
|
|
60
|
+
from: 'piglet@gmail.com',
|
|
61
|
+
bcc: ['pooh@gmail.com'],
|
|
62
|
+
}).checkValidity();
|
|
63
|
+
expect(isValid).toBe(true);
|
|
64
|
+
expect(validationError).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
it('message validation fails with non-existent file', () => {
|
|
67
|
+
const msg = new Message({
|
|
68
|
+
from: 'me',
|
|
69
|
+
to: 'you',
|
|
70
|
+
attachment: { path: 'non-existent-file.txt' },
|
|
71
|
+
});
|
|
72
|
+
const { isValid, validationError } = msg.checkValidity();
|
|
73
|
+
expect(isValid).toBe(false);
|
|
74
|
+
expect(validationError).toContain('does not exist');
|
|
75
|
+
});
|
|
76
|
+
it('message validation fails with unreadable stream', () => {
|
|
77
|
+
const stream = new Readable();
|
|
78
|
+
stream.readable = false;
|
|
79
|
+
const msg = new Message({
|
|
80
|
+
from: 'me',
|
|
81
|
+
to: 'you',
|
|
82
|
+
attachment: { stream },
|
|
83
|
+
});
|
|
84
|
+
const { isValid, validationError } = msg.checkValidity();
|
|
85
|
+
expect(isValid).toBe(false);
|
|
86
|
+
expect(validationError).toContain('stream is not readable');
|
|
87
|
+
});
|
|
88
|
+
it('reads large message body correctly', async () => {
|
|
89
|
+
const largeText = 'a'.repeat(1024 * 100); // 100KB
|
|
90
|
+
const msg = new Message({
|
|
91
|
+
from: 'me',
|
|
92
|
+
to: 'you',
|
|
93
|
+
text: largeText,
|
|
94
|
+
});
|
|
95
|
+
const output = await msg.readAsync();
|
|
96
|
+
expect(output).toContain(largeText);
|
|
97
|
+
});
|
|
98
|
+
it('generates correct structure for related items inside alternative', async () => {
|
|
99
|
+
const msg = new Message({
|
|
100
|
+
from: 'me',
|
|
101
|
+
to: 'you',
|
|
102
|
+
text: 'plain text',
|
|
103
|
+
attachment: [
|
|
104
|
+
{
|
|
105
|
+
data: '<html><img src="cid:1"></html>',
|
|
106
|
+
alternative: true,
|
|
107
|
+
related: [
|
|
108
|
+
{
|
|
109
|
+
data: 'image data',
|
|
110
|
+
type: 'image/png',
|
|
111
|
+
headers: { 'Content-ID': '<1>' },
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
const output = await msg.readAsync();
|
|
118
|
+
expect(output).toContain('multipart/alternative');
|
|
119
|
+
expect(output).toContain('multipart/related');
|
|
120
|
+
expect(output).toContain('Content-Id: <1>');
|
|
121
|
+
});
|
|
122
|
+
it('excludes BCC from output headers', async () => {
|
|
123
|
+
const msg = new Message({
|
|
124
|
+
from: 'me',
|
|
125
|
+
to: 'you',
|
|
126
|
+
bcc: 'secret',
|
|
127
|
+
text: 'hi'
|
|
128
|
+
});
|
|
129
|
+
const output = await msg.readAsync();
|
|
130
|
+
// Header should not be present
|
|
131
|
+
expect(output).not.toMatch(/^Bcc:/m);
|
|
132
|
+
});
|
|
133
|
+
it('includes standard and custom headers in output', async () => {
|
|
134
|
+
const msg = new Message({
|
|
135
|
+
from: 'me@example.com',
|
|
136
|
+
to: 'you@example.com',
|
|
137
|
+
subject: 'test subject',
|
|
138
|
+
'X-Custom': 'custom value',
|
|
139
|
+
});
|
|
140
|
+
const output = await msg.readAsync();
|
|
141
|
+
expect(output).toContain('From: me@example.com');
|
|
142
|
+
expect(output).toContain('To: you@example.com');
|
|
143
|
+
expect(output).toContain('Subject: =?UTF-8?Q?test_subject?=');
|
|
144
|
+
expect(output).toContain('X-Custom: custom value');
|
|
145
|
+
});
|
|
146
|
+
it('accepts array of attachments in constructor', async () => {
|
|
147
|
+
const msg = new Message({
|
|
148
|
+
from: 'me',
|
|
149
|
+
to: 'you',
|
|
150
|
+
attachment: [
|
|
151
|
+
{ data: 'a', name: 'a.txt' },
|
|
152
|
+
{ data: 'b', name: 'b.txt' },
|
|
153
|
+
],
|
|
154
|
+
});
|
|
155
|
+
const output = await msg.readAsync();
|
|
156
|
+
expect(output).toContain('filename="=?UTF-8?Q?a=2Etxt?="');
|
|
157
|
+
expect(output).toContain('filename="=?UTF-8?Q?b=2Etxt?="');
|
|
158
|
+
});
|
|
159
|
+
it('reads file attachment successfully', async () => {
|
|
160
|
+
const msg = new Message({
|
|
161
|
+
from: 'me',
|
|
162
|
+
to: 'you',
|
|
163
|
+
attachment: { path: 'test/attachments/smtp.txt', name: 'smtp.txt' },
|
|
164
|
+
});
|
|
165
|
+
const output = await msg.readAsync();
|
|
166
|
+
expect(output).toContain('filename="=?UTF-8?Q?smtp=2Etxt?="');
|
|
167
|
+
});
|
|
168
|
+
it('message validation fails if attachment has no data', () => {
|
|
169
|
+
const msg = new Message({
|
|
170
|
+
from: 'me',
|
|
171
|
+
to: 'you',
|
|
172
|
+
attachment: [{ name: 'empty' }],
|
|
173
|
+
});
|
|
174
|
+
const { isValid, validationError } = msg.checkValidity();
|
|
175
|
+
expect(isValid).toBe(false);
|
|
176
|
+
expect(validationError).toContain('attachment has no data associated with it');
|
|
177
|
+
});
|
|
178
|
+
it('MessageStream handles pause and resume', async () => {
|
|
179
|
+
const msg = new Message({
|
|
180
|
+
from: 'me',
|
|
181
|
+
to: 'you',
|
|
182
|
+
text: 'content',
|
|
183
|
+
});
|
|
184
|
+
const stream = msg.stream();
|
|
185
|
+
stream.pause();
|
|
186
|
+
expect(stream.paused).toBe(true);
|
|
187
|
+
stream.resume();
|
|
188
|
+
expect(stream.paused).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
it('outputs alternative content correctly', async () => {
|
|
191
|
+
const msg = new Message({
|
|
192
|
+
from: 'me',
|
|
193
|
+
to: 'you',
|
|
194
|
+
text: 'plain',
|
|
195
|
+
attachment: {
|
|
196
|
+
data: '<b>html</b>',
|
|
197
|
+
alternative: true,
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
const output = await msg.readAsync();
|
|
201
|
+
expect(output).toContain('multipart/alternative');
|
|
202
|
+
expect(output).toContain('text/plain');
|
|
203
|
+
expect(output).toContain('text/html');
|
|
204
|
+
});
|
|
205
|
+
it('handles pause during output', () => new Promise((done) => {
|
|
206
|
+
const msg = new Message({
|
|
207
|
+
from: 'me',
|
|
208
|
+
to: 'you',
|
|
209
|
+
text: 'a'.repeat(100000),
|
|
210
|
+
});
|
|
211
|
+
const stream = msg.stream();
|
|
212
|
+
let paused = false;
|
|
213
|
+
stream.on('data', () => {
|
|
214
|
+
if (!paused) {
|
|
215
|
+
stream.pause();
|
|
216
|
+
paused = true;
|
|
217
|
+
setTimeout(() => {
|
|
218
|
+
stream.resume();
|
|
219
|
+
}, 10);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
stream.on('end', () => {
|
|
223
|
+
done();
|
|
224
|
+
});
|
|
225
|
+
}));
|
|
226
|
+
it('message validation succeeds with valid attachments', () => {
|
|
227
|
+
const msg = new Message({
|
|
228
|
+
from: 'me',
|
|
229
|
+
to: 'you',
|
|
230
|
+
attachment: [{ data: 'content' }, { path: 'test/attachments/smtp.txt' }],
|
|
231
|
+
});
|
|
232
|
+
const { isValid, validationError } = msg.checkValidity();
|
|
233
|
+
expect(isValid).toBe(true);
|
|
234
|
+
expect(validationError).toBeUndefined();
|
|
235
|
+
});
|
|
236
|
+
it('streams buffer overflow correctly', async () => {
|
|
237
|
+
const hugeText = 'a'.repeat(20000);
|
|
238
|
+
const msg = new Message({
|
|
239
|
+
from: 'me',
|
|
240
|
+
to: 'you',
|
|
241
|
+
text: hugeText,
|
|
242
|
+
});
|
|
243
|
+
const output = await msg.readAsync();
|
|
244
|
+
expect(output).toContain(hugeText);
|
|
245
|
+
});
|
|
246
|
+
it('outputs headers correctly excluding BCC', async () => {
|
|
247
|
+
const msg = new Message({
|
|
248
|
+
from: 'me',
|
|
249
|
+
to: 'you',
|
|
250
|
+
bcc: 'secret',
|
|
251
|
+
'X-Custom': 'value',
|
|
252
|
+
});
|
|
253
|
+
const output = await msg.readAsync();
|
|
254
|
+
expect(output).toContain('X-Custom: value');
|
|
255
|
+
expect(output).not.toContain('Bcc:');
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
//# sourceMappingURL=message.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.test.js","sourceRoot":"","sources":["../src/message.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;IACxB,EAAE,CAAC,gDAAgD,EAAE,GAAG,EAAE;QACzD,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC,EAAE,CAAC,CAAA;QAC3B,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,GAAG,CAAC,aAAa,EAAE,CAAA;QACxD,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,MAAM,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAA;IAClE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,8DAA8D,EAAE,GAAG,EAAE;QACvE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,IAAI,OAAO,CAAC;YAChD,IAAI,EAAE,kBAAkB;SACxB,CAAC,CAAC,aAAa,EAAE,CAAA;QAElB,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,MAAM,CAAC,eAAe,CAAC,CAAC,IAAI,CAC3B,4DAA4D,CAC5D,CAAA;IACF,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,sEAAsE,EAAE,GAAG,EAAE;QAC/E,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,G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AAC,CAAC,aAAa,EAAE,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,mCAAmC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAClC,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC;YACvB,IAAI,EAAE,IAAI;YACV,EAAE,EAAE,KAAK;YACT,IAAI,EAAE,QAAQ;SACd,CAAC,CAAA;QACF,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,SAAS,EAAE,CAAA;QACpC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACxD,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC;YACvB,IAAI,EAAE,IAAI;YACV,EAAE,EAAE,KAAK;YACT,GAAG,EAAE,QAAQ;YACb,UAAU,EAAE,OAAO;SACnB,CAAC,CAAA;QACF,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,SAAS,EAAE,CAAA;QACpC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAA;QAC3C,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,CAAA;IACrC,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
package/dist/mime.d.ts
ADDED
package/dist/mime.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// adapted from https://github.com/emailjs/emailjs-mime-codec/blob/6909c706b9f09bc0e5c3faf48f723cca53e5b352/src/mimecodec.js
|
|
2
|
+
import { TextDecoder, TextEncoder } from 'util';
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const RANGES = [
|
|
5
|
+
[0x09], // <TAB>
|
|
6
|
+
[0x0a], // <LF>
|
|
7
|
+
[0x0d], // <CR>
|
|
8
|
+
[0x20, 0x3c], // <SP>!"#$%&'()*+,-./0123456789:;
|
|
9
|
+
[0x3e, 0x7e], // >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
|
|
10
|
+
];
|
|
11
|
+
const LOOKUP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
|
12
|
+
const MAX_CHUNK_LENGTH = 16383; // must be multiple of 3
|
|
13
|
+
const MAX_MIME_WORD_LENGTH = 52;
|
|
14
|
+
const MAX_B64_MIME_WORD_BYTE_LENGTH = 39;
|
|
15
|
+
function tripletToBase64(num) {
|
|
16
|
+
return (LOOKUP[(num >> 18) & 0x3f] +
|
|
17
|
+
LOOKUP[(num >> 12) & 0x3f] +
|
|
18
|
+
LOOKUP[(num >> 6) & 0x3f] +
|
|
19
|
+
LOOKUP[num & 0x3f]);
|
|
20
|
+
}
|
|
21
|
+
function encodeChunk(uint8, start, end) {
|
|
22
|
+
let output = '';
|
|
23
|
+
for (let i = start; i < end; i += 3) {
|
|
24
|
+
output += tripletToBase64((uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2]);
|
|
25
|
+
}
|
|
26
|
+
return output;
|
|
27
|
+
}
|
|
28
|
+
function encodeBase64(data) {
|
|
29
|
+
const len = data.length;
|
|
30
|
+
const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
|
|
31
|
+
let output = '';
|
|
32
|
+
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
33
|
+
for (let i = 0, len2 = len - extraBytes; i < len2; i += MAX_CHUNK_LENGTH) {
|
|
34
|
+
output += encodeChunk(data, i, i + MAX_CHUNK_LENGTH > len2 ? len2 : i + MAX_CHUNK_LENGTH);
|
|
35
|
+
}
|
|
36
|
+
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
37
|
+
if (extraBytes === 1) {
|
|
38
|
+
const tmp = data[len - 1];
|
|
39
|
+
output += LOOKUP[tmp >> 2];
|
|
40
|
+
output += LOOKUP[(tmp << 4) & 0x3f];
|
|
41
|
+
output += '==';
|
|
42
|
+
}
|
|
43
|
+
else if (extraBytes === 2) {
|
|
44
|
+
const tmp = (data[len - 2] << 8) + data[len - 1];
|
|
45
|
+
output += LOOKUP[tmp >> 10];
|
|
46
|
+
output += LOOKUP[(tmp >> 4) & 0x3f];
|
|
47
|
+
output += LOOKUP[(tmp << 2) & 0x3f];
|
|
48
|
+
output += '=';
|
|
49
|
+
}
|
|
50
|
+
return output;
|
|
51
|
+
}
|
|
52
|
+
function splitMimeEncodedString(str, maxlen = 12) {
|
|
53
|
+
const minWordLength = 12; // require at least 12 symbols to fit possible 4 octet UTF-8 sequences
|
|
54
|
+
const maxWordLength = Math.max(maxlen, minWordLength);
|
|
55
|
+
const lines = [];
|
|
56
|
+
while (str.length) {
|
|
57
|
+
let curLine = str.slice(0, maxWordLength);
|
|
58
|
+
const match = curLine.match(/=[0-9A-F]?$/i); // skip incomplete escaped char
|
|
59
|
+
if (match) {
|
|
60
|
+
curLine = curLine.slice(0, match.index);
|
|
61
|
+
}
|
|
62
|
+
let done = false;
|
|
63
|
+
while (!done) {
|
|
64
|
+
let chr;
|
|
65
|
+
done = true;
|
|
66
|
+
const match = str.slice(curLine.length).match(/^=([0-9A-F]{2})/i); // check if not middle of a unicode char sequence
|
|
67
|
+
if (match) {
|
|
68
|
+
chr = parseInt(match[1], 16);
|
|
69
|
+
// invalid sequence, move one char back anc recheck
|
|
70
|
+
if (chr < 0xc2 && chr > 0x7f) {
|
|
71
|
+
curLine = curLine.slice(0, curLine.length - 3);
|
|
72
|
+
done = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (curLine.length) {
|
|
77
|
+
lines.push(curLine);
|
|
78
|
+
}
|
|
79
|
+
str = str.slice(curLine.length);
|
|
80
|
+
}
|
|
81
|
+
return lines;
|
|
82
|
+
}
|
|
83
|
+
function checkRanges(nr) {
|
|
84
|
+
return RANGES.reduce((val, range) => val ||
|
|
85
|
+
(range.length === 1 && nr === range[0]) ||
|
|
86
|
+
(range.length === 2 && nr >= range[0] && nr <= range[1]), false);
|
|
87
|
+
}
|
|
88
|
+
export function mimeEncode(data = '', encoding = 'utf-8') {
|
|
89
|
+
const decoder = new TextDecoder(encoding);
|
|
90
|
+
const buffer = typeof data === 'string'
|
|
91
|
+
? encoder.encode(data)
|
|
92
|
+
: encoder.encode(decoder.decode(data));
|
|
93
|
+
return buffer.reduce((aggregate, ord, index) => checkRanges(ord) &&
|
|
94
|
+
!((ord === 0x20 || ord === 0x09) &&
|
|
95
|
+
(index === buffer.length - 1 ||
|
|
96
|
+
buffer[index + 1] === 0x0a ||
|
|
97
|
+
buffer[index + 1] === 0x0d))
|
|
98
|
+
? // if the char is in allowed range, then keep as is, unless it is a ws in the end of a line
|
|
99
|
+
aggregate + String.fromCharCode(ord)
|
|
100
|
+
: `${aggregate}=${ord < 0x10 ? '0' : ''}${ord
|
|
101
|
+
.toString(16)
|
|
102
|
+
.toUpperCase()}`, '');
|
|
103
|
+
}
|
|
104
|
+
export function mimeWordEncode(data, mimeWordEncoding = 'Q', encoding = 'utf-8') {
|
|
105
|
+
let parts = [];
|
|
106
|
+
const decoder = new TextDecoder(encoding);
|
|
107
|
+
const str = typeof data === 'string' ? data : decoder.decode(data);
|
|
108
|
+
if (mimeWordEncoding === 'Q') {
|
|
109
|
+
const encodedStr = mimeEncode(str, encoding).replace(/[^a-z0-9!*+\-/=]/gi, (chr) => chr === ' '
|
|
110
|
+
? '_'
|
|
111
|
+
: '=' +
|
|
112
|
+
(chr.charCodeAt(0) < 0x10 ? '0' : '') +
|
|
113
|
+
chr.charCodeAt(0).toString(16).toUpperCase());
|
|
114
|
+
parts =
|
|
115
|
+
encodedStr.length < MAX_MIME_WORD_LENGTH
|
|
116
|
+
? [encodedStr]
|
|
117
|
+
: splitMimeEncodedString(encodedStr, MAX_MIME_WORD_LENGTH);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// Fits as much as possible into every line without breaking utf-8 multibyte characters' octets up across lines
|
|
121
|
+
let j = 0;
|
|
122
|
+
let i = 0;
|
|
123
|
+
while (i < str.length) {
|
|
124
|
+
if (encoder.encode(str.substring(j, i)).length >
|
|
125
|
+
MAX_B64_MIME_WORD_BYTE_LENGTH) {
|
|
126
|
+
// we went one character too far, substring at the char before
|
|
127
|
+
parts.push(str.substring(j, i - 1));
|
|
128
|
+
j = i - 1;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// add the remainder of the string
|
|
135
|
+
if (str.substring(j)) {
|
|
136
|
+
parts.push(str.substring(j));
|
|
137
|
+
}
|
|
138
|
+
parts = parts.map((x) => encoder.encode(x)).map((x) => encodeBase64(x));
|
|
139
|
+
}
|
|
140
|
+
return parts
|
|
141
|
+
.map((p) => `=?UTF-8?${mimeWordEncoding}?${p}?= `)
|
|
142
|
+
.join('')
|
|
143
|
+
.trim();
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=mime.js.map
|
package/dist/mime.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime.js","sourceRoot":"","sources":["../src/mime.ts"],"names":[],"mappings":"AAAA,4HAA4H;AAC5H,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAE/C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC,MAAM,MAAM,GAAG;IACd,CAAC,IAAI,CAAC,EAAE,QAAQ;IAChB,CAAC,IAAI,CAAC,EAAE,OAAO;IACf,CAAC,IAAI,CAAC,EAAE,OAAO;IACf,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,kCAAkC;IAChD,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,mEAAmE;CACjF,CAAA;AACD,MAAM,MAAM,GACX,kEAAkE,CAAC,K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,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;gBACnC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACV,CAAC;iBAAM,CAAC;gBACP,CAAC,EAAE,CAAA;YACJ,CAAC;QACF,CAAC;QACD,kCAAkC;QAClC,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;YACtB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA;QAC7B,CAAC;QACD,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;IACxE,CAAC;IAED,OAAO,KAAK;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,gBAAgB,IAAI,CAAC,KAAK,CAAC;SACjD,IAAI,CAAC,EAAE,CAAC;SACR,IAAI,EAAE,CAAA;AACT,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { mimeEncode, mimeWordEncode } from './mime.js';
|
|
3
|
+
describe('mime', () => {
|
|
4
|
+
it('mimeEncode should encode UTF-8', () => {
|
|
5
|
+
expect(mimeEncode('tere ÕÄÖÕ')).toBe('tere =C3=95=C3=84=C3=96=C3=95');
|
|
6
|
+
});
|
|
7
|
+
it('mimeEncode should encode trailing whitespace', () => {
|
|
8
|
+
expect(mimeEncode('tere ')).toBe('tere =20');
|
|
9
|
+
});
|
|
10
|
+
it('mimeEncode should encode non UTF-8', () => {
|
|
11
|
+
expect(mimeEncode(new Uint8Array([0xbd, 0xc5]), 'utf-16be')).toBe('=EB=B7=85');
|
|
12
|
+
});
|
|
13
|
+
it('mimeWordEncode should encode', () => {
|
|
14
|
+
expect(mimeWordEncode('See on õhin test')).toBe('=?UTF-8?Q?See_on_=C3=B5hin_test?=');
|
|
15
|
+
});
|
|
16
|
+
it('mimeWordEncode should QP-encode mime word', () => {
|
|
17
|
+
expect(mimeWordEncode(new Uint8Array([0x4a, 0xf5, 0x67, 0x65, 0x2d, 0x76, 0x61, 0xde]), 'Q', 'utf-16be')).toBe('=?UTF-8?Q?=E4=AB=B5=E6=9D=A5=E2=B5=B6=E6=87=9E?=');
|
|
18
|
+
});
|
|
19
|
+
it('mimeWordEncode should Base64-encode mime word', () => {
|
|
20
|
+
expect(mimeWordEncode('Привет и до свидания', 'B')).toBe('=?UTF-8?B?0J/RgNC40LLQtdGCINC4INC00L4g0YHQstC40LTQsNC90LjRjw==?=');
|
|
21
|
+
});
|
|
22
|
+
it('mimeWordEncode should Base64-encode a long mime word', () => {
|
|
23
|
+
const payload = 'üöß‹€Привет и до свиданияПривет и до свиданияПривет и до свиданияПривет и до свиданияПривет и до свиданияПривет и до свиданияПривет и до свиданияПривет и до свидания';
|
|
24
|
+
const expected = '=?UTF-8?B?w7zDtsOf4oC54oKs0J/RgNC40LLQtdGCINC4INC00L4g0YHQstC4?= ' +
|
|
25
|
+
'=?UTF-8?B?0LTQsNC90LjRj9Cf0YDQuNCy0LXRgiDQuCDQtNC+INGB0LLQuNC0?= ' +
|
|
26
|
+
'=?UTF-8?B?0LDQvdC40Y/Qn9GA0LjQstC10YIg0Lgg0LTQviDRgdCy0LjQtNCw?= ' +
|
|
27
|
+
'=?UTF-8?B?0L3QuNGP0J/RgNC40LLQtdGCINC4INC00L4g0YHQstC40LTQsNC9?= ' +
|
|
28
|
+
'=?UTF-8?B?0LjRj9Cf0YDQuNCy0LXRgiDQuCDQtNC+INGB0LLQuNC00LDQvdC4?= ' +
|
|
29
|
+
'=?UTF-8?B?0Y/Qn9GA0LjQstC10YIg0Lgg0LTQviDRgdCy0LjQtNCw0L3QuNGP?= ' +
|
|
30
|
+
'=?UTF-8?B?0J/RgNC40LLQtdGCINC4INC00L4g0YHQstC40LTQsNC90LjRj9Cf?= ' +
|
|
31
|
+
'=?UTF-8?B?0YDQuNCy0LXRgiDQuCDQtNC+INGB0LLQuNC00LDQvdC40Y8=?=';
|
|
32
|
+
expect(mimeWordEncode(payload, 'B')).toBe(expected);
|
|
33
|
+
});
|
|
34
|
+
it('splitMimeEncodedString should split long encoded strings', () => {
|
|
35
|
+
const input = 'This is a very long encoded string that needs to be split into multiple parts because it exceeds the maximum length allowed for mime words in email headers';
|
|
36
|
+
const output = mimeWordEncode(input, 'Q');
|
|
37
|
+
expect(output).toContain('?= =?UTF-8?Q?');
|
|
38
|
+
expect(output.length).toBeGreaterThan(100);
|
|
39
|
+
});
|
|
40
|
+
it('handles invalid QP sequences in split', () => {
|
|
41
|
+
const input = 'a'.repeat(60) + '€';
|
|
42
|
+
const output = mimeWordEncode(input, 'Q');
|
|
43
|
+
expect(output).toContain('?= =?UTF-8?Q?');
|
|
44
|
+
});
|
|
45
|
+
it('encodes base64 with 1 byte padding', () => {
|
|
46
|
+
expect(mimeWordEncode('a', 'B')).toContain('YQ==');
|
|
47
|
+
});
|
|
48
|
+
it('encodes base64 with 2 byte padding', () => {
|
|
49
|
+
expect(mimeWordEncode('ab', 'B')).toContain('YWI=');
|
|
50
|
+
});
|
|
51
|
+
it('encodes base64 with 0 byte padding', () => {
|
|
52
|
+
expect(mimeWordEncode('abc', 'B')).toContain('YWJj');
|
|
53
|
+
});
|
|
54
|
+
it('mimeEncode encodes specific control characters and spaces correctly', () => {
|
|
55
|
+
expect(mimeEncode('a b ')).toBe('a b=20');
|
|
56
|
+
expect(mimeEncode('a\t')).toBe('a=09');
|
|
57
|
+
expect(mimeEncode(' \n')).toBe('=20\n');
|
|
58
|
+
expect(mimeEncode('\t\r')).toBe('=09\r');
|
|
59
|
+
});
|
|
60
|
+
it('handles split inside incomplete escape sequence (case 1: ends with =)', () => {
|
|
61
|
+
const input = 'a'.repeat(51) + ' ';
|
|
62
|
+
const output = mimeWordEncode(input, 'Q');
|
|
63
|
+
expect(output).toContain('?= =?UTF-8?Q?=20');
|
|
64
|
+
});
|
|
65
|
+
it('handles split inside incomplete escape sequence (case 2: ends with =X)', () => {
|
|
66
|
+
const input = 'a'.repeat(50) + ' ';
|
|
67
|
+
const output = mimeWordEncode(input, 'Q');
|
|
68
|
+
expect(output).toContain('?= =?UTF-8?Q?=20');
|
|
69
|
+
});
|
|
70
|
+
it('handles split inside UTF-8 multibyte sequence', () => {
|
|
71
|
+
const input = 'a'.repeat(49) + 'Õ';
|
|
72
|
+
const output = mimeWordEncode(input, 'Q');
|
|
73
|
+
expect(output).toContain('?= =?UTF-8?Q?=C3=95');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
//# sourceMappingURL=mime.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime.test.js","sourceRoot":"","sources":["../src/mime.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAEtD,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACrB,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACzC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAA;IACtE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACvD,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAC9C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC7C,MAAM,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,CA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AO,CAAC,CAAA;QACvC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACzC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,uEAAuE,EAAE,GAAG,EAAE;QAChF,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,GAAG,CAAA;QAClC,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,wEAAwE,EAAE,GAAG,EAAE;QACjF,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,GAAG,CAAA;QAClC,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,+CAA+C,EAAE,GAAG,EAAE;QACxD,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,GAAG,CAAA;QAClC,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAA;IAChD,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { SMTPError, SMTPErrorStates } from './error.js';
|
|
2
|
+
const CRLF = '\r\n'; // Define CRLF here for consistency
|
|
3
|
+
export class SMTPResponseMonitor {
|
|
4
|
+
stop;
|
|
5
|
+
constructor(stream, timeout, onerror) {
|
|
6
|
+
let buffer = '';
|
|
7
|
+
const notify = () => {
|
|
8
|
+
const lines = buffer.split(CRLF);
|
|
9
|
+
let processedChars = 0;
|
|
10
|
+
let responseFound = false;
|
|
11
|
+
for (let i = 0; i < lines.length; i++) {
|
|
12
|
+
const line = lines[i];
|
|
13
|
+
// If it's an empty line and not the very last line, it's just padding, skip it.
|
|
14
|
+
// If it's the very last line and empty, it means we have `\r\n\r\n` and should not process further.
|
|
15
|
+
if (!line.length && i < lines.length - 1) {
|
|
16
|
+
processedChars += line.length + CRLF.length;
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
// Look for an SMTP response line pattern:
|
|
20
|
+
// ^(\d{3}) -> 3-digit code
|
|
21
|
+
// ([ -]) -> space for final line, hyphen for multi-line continuation
|
|
22
|
+
// (.*)$ -> rest of the message
|
|
23
|
+
const match = line.match(/^(\d{3})([ -])(.*)$/);
|
|
24
|
+
if (match) {
|
|
25
|
+
const code = match[1];
|
|
26
|
+
const separator = match[2];
|
|
27
|
+
const message = match[3];
|
|
28
|
+
if (separator === ' ') {
|
|
29
|
+
// This is a final line of an SMTP response
|
|
30
|
+
// If this is the very last line in the current buffer, and the buffer does not end with CRLF,
|
|
31
|
+
// then this line is incomplete and we should not process it yet.
|
|
32
|
+
if (i === lines.length - 1 && !buffer.endsWith(CRLF)) {
|
|
33
|
+
// Defer emission as the line is not fully received yet
|
|
34
|
+
processedChars += line.length + CRLF.length;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
stream.emit('response', null, {
|
|
38
|
+
code: code,
|
|
39
|
+
message: message.trim(),
|
|
40
|
+
data: buffer
|
|
41
|
+
.substring(0, processedChars + line.length + CRLF.length)
|
|
42
|
+
.replace(/\r/g, ''), // Send the full data for this response
|
|
43
|
+
});
|
|
44
|
+
responseFound = true;
|
|
45
|
+
processedChars += line.length + CRLF.length;
|
|
46
|
+
break; // Only process one full response at a time
|
|
47
|
+
}
|
|
48
|
+
// If it's a multi-line response (separator is '-'), we continue accumulating in the buffer.
|
|
49
|
+
// We only emit a response when a final line (' ') is encountered.
|
|
50
|
+
}
|
|
51
|
+
processedChars += line.length + CRLF.length;
|
|
52
|
+
}
|
|
53
|
+
if (responseFound) {
|
|
54
|
+
buffer = buffer.substring(processedChars); // Remove the processed part from the buffer
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const error = (err) => {
|
|
58
|
+
stream.emit('response', SMTPError.create('connection encountered an error', SMTPErrorStates.ERROR, err));
|
|
59
|
+
};
|
|
60
|
+
const timedout = (err) => {
|
|
61
|
+
stream.end();
|
|
62
|
+
stream.emit('response', SMTPError.create('timedout while connecting to smtp server', SMTPErrorStates.TIMEDOUT, err));
|
|
63
|
+
};
|
|
64
|
+
const watch = (data) => {
|
|
65
|
+
if (data !== null) {
|
|
66
|
+
buffer += data.toString();
|
|
67
|
+
notify();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const close = (err) => {
|
|
71
|
+
stream.emit('response', SMTPError.create('connection has closed', SMTPErrorStates.CONNECTIONCLOSED, err));
|
|
72
|
+
};
|
|
73
|
+
const end = (err) => {
|
|
74
|
+
stream.emit('response', SMTPError.create('connection has ended', SMTPErrorStates.CONNECTIONENDED, err));
|
|
75
|
+
};
|
|
76
|
+
this.stop = (err) => {
|
|
77
|
+
stream.removeAllListeners('response');
|
|
78
|
+
stream.removeListener('data', watch);
|
|
79
|
+
stream.removeListener('end', end);
|
|
80
|
+
stream.removeListener('close', close);
|
|
81
|
+
stream.removeListener('error', error);
|
|
82
|
+
if (err != null && typeof onerror === 'function') {
|
|
83
|
+
onerror(err);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
stream.on('data', watch);
|
|
87
|
+
stream.on('end', end);
|
|
88
|
+
stream.on('close', close);
|
|
89
|
+
stream.on('error', error);
|
|
90
|
+
stream.setTimeout(timeout, timedout);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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