imapflow 2.0.7 → 2.1.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/CHANGELOG.md +29 -0
- package/dist/cjs/commands/append.js +12 -12
- package/dist/cjs/commands/authenticate.d.ts +3 -8
- package/dist/cjs/commands/close.js +2 -1
- package/dist/cjs/commands/copy.js +4 -4
- package/dist/cjs/commands/create.js +2 -3
- package/dist/cjs/commands/delete.js +4 -4
- package/dist/cjs/commands/expunge.js +8 -5
- package/dist/cjs/commands/fetch.js +12 -10
- package/dist/cjs/commands/idle.js +6 -2
- package/dist/cjs/commands/list.js +22 -18
- package/dist/cjs/commands/move.js +11 -6
- package/dist/cjs/commands/namespace.js +1 -1
- package/dist/cjs/commands/quota.js +10 -9
- package/dist/cjs/commands/rename.js +4 -4
- package/dist/cjs/commands/search.js +7 -8
- package/dist/cjs/commands/select.js +12 -9
- package/dist/cjs/commands/status.js +11 -11
- package/dist/cjs/commands/store.js +5 -5
- package/dist/cjs/commands/subscribe.js +2 -17
- package/dist/cjs/commands/subscription.d.ts +10 -0
- package/dist/cjs/commands/subscription.js +29 -0
- package/dist/cjs/commands/unsubscribe.js +2 -17
- package/dist/cjs/download.d.ts +22 -0
- package/dist/cjs/download.js +588 -0
- package/dist/cjs/errors.d.ts +50 -1
- package/dist/cjs/errors.js +53 -1
- package/dist/cjs/handler/imap-compiler.js +1 -1
- package/dist/cjs/handler/imap-stream.d.ts +13 -2
- package/dist/cjs/handler/imap-stream.js +51 -30
- package/dist/cjs/handler/parser-instance.js +2 -2
- package/dist/cjs/handler/token-parser.js +15 -9
- package/dist/cjs/imap-flow.d.ts +30 -84
- package/dist/cjs/imap-flow.js +282 -734
- package/dist/cjs/jp-decoder.js +1 -1
- package/dist/cjs/package-info.d.ts +1 -1
- package/dist/cjs/package-info.js +3 -3
- package/dist/cjs/search-compiler.js +5 -12
- package/dist/cjs/tools.d.ts +52 -11
- package/dist/cjs/tools.js +86 -23
- package/dist/cjs/types.d.ts +30 -16
- package/dist/esm/commands/append.js +13 -13
- package/dist/esm/commands/authenticate.d.ts +3 -8
- package/dist/esm/commands/close.js +2 -1
- package/dist/esm/commands/copy.js +5 -5
- package/dist/esm/commands/create.js +3 -4
- package/dist/esm/commands/delete.js +5 -5
- package/dist/esm/commands/expunge.js +9 -6
- package/dist/esm/commands/fetch.js +13 -11
- package/dist/esm/commands/idle.js +7 -3
- package/dist/esm/commands/list.js +22 -18
- package/dist/esm/commands/move.js +12 -7
- package/dist/esm/commands/namespace.js +2 -2
- package/dist/esm/commands/quota.js +11 -10
- package/dist/esm/commands/rename.js +5 -5
- package/dist/esm/commands/search.js +8 -9
- package/dist/esm/commands/select.js +13 -10
- package/dist/esm/commands/status.js +12 -12
- package/dist/esm/commands/store.js +6 -6
- package/dist/esm/commands/subscribe.js +2 -17
- package/dist/esm/commands/subscription.d.ts +10 -0
- package/dist/esm/commands/subscription.js +26 -0
- package/dist/esm/commands/unsubscribe.js +2 -17
- package/dist/esm/download.d.ts +22 -0
- package/dist/esm/download.js +581 -0
- package/dist/esm/errors.d.ts +50 -1
- package/dist/esm/errors.js +52 -0
- package/dist/esm/handler/imap-compiler.js +1 -1
- package/dist/esm/handler/imap-stream.d.ts +13 -2
- package/dist/esm/handler/imap-stream.js +51 -30
- package/dist/esm/handler/parser-instance.js +2 -2
- package/dist/esm/handler/token-parser.js +15 -9
- package/dist/esm/imap-flow.d.ts +30 -84
- package/dist/esm/imap-flow.js +282 -735
- package/dist/esm/jp-decoder.js +1 -1
- package/dist/esm/package-info.d.ts +1 -1
- package/dist/esm/package-info.js +3 -3
- package/dist/esm/search-compiler.js +5 -12
- package/dist/esm/tools.d.ts +52 -11
- package/dist/esm/tools.js +79 -21
- package/dist/esm/types.d.ts +30 -16
- package/package.json +4 -4
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The download() and downloadMany() implementations of ImapFlow. Both are built on the public
|
|
3
|
+
// fetchOne(): download() streams a message or one body part through the decoding pipeline,
|
|
4
|
+
// fetching it in chunks, and downloadMany() buffers several body parts from one FETCH.
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.downloadMessage = downloadMessage;
|
|
10
|
+
exports.downloadMessageParts = downloadMessageParts;
|
|
11
|
+
const node_stream_1 = require("node:stream");
|
|
12
|
+
const libmime_1 = __importDefault(require("libmime"));
|
|
13
|
+
const libqp_1 = __importDefault(require("libqp"));
|
|
14
|
+
const libbase64_1 = __importDefault(require("libbase64"));
|
|
15
|
+
const mailsplit_1 = require("@zone-eu/mailsplit");
|
|
16
|
+
const flowed_decoder_js_1 = __importDefault(require("@zone-eu/mailsplit/lib/flowed-decoder.js"));
|
|
17
|
+
const limited_passthrough_js_1 = require("./limited-passthrough.js");
|
|
18
|
+
const tools_js_1 = require("./tools.js");
|
|
19
|
+
/**
|
|
20
|
+
* Implements ImapFlow.download(), see its documentation
|
|
21
|
+
*
|
|
22
|
+
* @param client - Connection to download from
|
|
23
|
+
* @param range - UID or sequence number of the message
|
|
24
|
+
* @param part - Body part to download, the whole message when not set
|
|
25
|
+
* @param options - Download options
|
|
26
|
+
* @returns The download, or an empty object when there is nothing to download
|
|
27
|
+
*/
|
|
28
|
+
async function downloadMessage(client, range, part, options) {
|
|
29
|
+
if (!client.mailbox) {
|
|
30
|
+
// no mailbox selected, nothing to do
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
let downloadOptions = Object.assign({
|
|
34
|
+
chunkSize: 64 * 1024,
|
|
35
|
+
maxBytes: Infinity
|
|
36
|
+
}, options || {});
|
|
37
|
+
let hasMore = true;
|
|
38
|
+
let processed = 0;
|
|
39
|
+
let chunkSize = Number(downloadOptions.chunkSize) || 64 * 1024;
|
|
40
|
+
// Normalized once here so every bounded stage of the pipeline below agrees on the budget
|
|
41
|
+
let maxBytes = (0, limited_passthrough_js_1.normalizeByteLimit)(downloadOptions.maxBytes);
|
|
42
|
+
let uid = false;
|
|
43
|
+
if (part === '1') {
|
|
44
|
+
// Special handling for part "1": in single-node emails (no childNodes),
|
|
45
|
+
// the body is accessed via "TEXT" rather than "1", and headers via
|
|
46
|
+
// "HEADER" instead of "1.MIME". Check bodyStructure to detect this.
|
|
47
|
+
let response = await client.fetchOne(range, { uid: true, bodyStructure: true }, downloadOptions);
|
|
48
|
+
if (!response) {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
if (!uid && response.uid) {
|
|
52
|
+
uid = response.uid;
|
|
53
|
+
// force UID from now on even if first range was a sequence number
|
|
54
|
+
range = uid;
|
|
55
|
+
downloadOptions.uid = true;
|
|
56
|
+
}
|
|
57
|
+
if (!response.bodyStructure.childNodes) {
|
|
58
|
+
// single text message
|
|
59
|
+
part = 'TEXT';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
let getNextPart = async (query) => {
|
|
63
|
+
query = query || {};
|
|
64
|
+
let mimeKey;
|
|
65
|
+
if (!part) {
|
|
66
|
+
query.source = {
|
|
67
|
+
start: processed,
|
|
68
|
+
maxLength: chunkSize
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
part = part.toString().toLowerCase().trim();
|
|
73
|
+
if (!query.bodyParts) {
|
|
74
|
+
query.bodyParts = [];
|
|
75
|
+
}
|
|
76
|
+
if (query.size) {
|
|
77
|
+
if (/^[\d.]+$/.test(part)) {
|
|
78
|
+
// fetch meta as well
|
|
79
|
+
mimeKey = part + '.mime';
|
|
80
|
+
query.bodyParts.push(mimeKey);
|
|
81
|
+
}
|
|
82
|
+
else if (part === 'text') {
|
|
83
|
+
mimeKey = 'header';
|
|
84
|
+
query.bodyParts.push(mimeKey);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
query.bodyParts.push({
|
|
88
|
+
key: part,
|
|
89
|
+
start: processed,
|
|
90
|
+
maxLength: chunkSize
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
let response = await client.fetchOne(range, query, downloadOptions);
|
|
94
|
+
if (!response) {
|
|
95
|
+
return { response: false, chunk: false };
|
|
96
|
+
}
|
|
97
|
+
if (!uid && response.uid) {
|
|
98
|
+
uid = response.uid;
|
|
99
|
+
// force UID from now on even if first range was a sequence number
|
|
100
|
+
range = uid;
|
|
101
|
+
downloadOptions.uid = true;
|
|
102
|
+
}
|
|
103
|
+
let chunk = !part ? response.source : response.bodyParts && response.bodyParts.get(part);
|
|
104
|
+
if (!chunk) {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
processed += chunk.length;
|
|
108
|
+
// A compliant server returns at most `chunkSize` bytes for a partial
|
|
109
|
+
// request. Some servers (Tencent Exmail among them) ignore the partial
|
|
110
|
+
// spec and answer every request with the complete part. That chunk is
|
|
111
|
+
// then larger than requested, so treating it as "full, keep going"
|
|
112
|
+
// would advance the offset past the end forever and never see a short
|
|
113
|
+
// chunk. An oversized answer already contains the whole part - stop.
|
|
114
|
+
hasMore = chunk.length === chunkSize;
|
|
115
|
+
if (chunk.length > chunkSize) {
|
|
116
|
+
client.log.warn({
|
|
117
|
+
msg: 'Server returned more than the requested window, treating the part as complete',
|
|
118
|
+
chunkSize,
|
|
119
|
+
received: chunk.length,
|
|
120
|
+
processed,
|
|
121
|
+
cid: client.id
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
let result = { chunk };
|
|
125
|
+
if (query.size) {
|
|
126
|
+
result.response = response;
|
|
127
|
+
}
|
|
128
|
+
if (query.bodyParts) {
|
|
129
|
+
if (mimeKey === 'header') {
|
|
130
|
+
result.mime = response.headers;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
result.mime = response.bodyParts && mimeKey ? response.bodyParts.get(mimeKey) : undefined;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
let { response, chunk, mime } = await getNextPart({
|
|
139
|
+
size: true,
|
|
140
|
+
uid: true
|
|
141
|
+
});
|
|
142
|
+
if (!response || !chunk) {
|
|
143
|
+
// the message or the part does not exist
|
|
144
|
+
return {};
|
|
145
|
+
}
|
|
146
|
+
let meta = {
|
|
147
|
+
expectedSize: response.size
|
|
148
|
+
};
|
|
149
|
+
if (!part) {
|
|
150
|
+
meta.contentType = 'message/rfc822';
|
|
151
|
+
}
|
|
152
|
+
else if (mime) {
|
|
153
|
+
let headers = new mailsplit_1.Headers(mime);
|
|
154
|
+
let contentType = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Type'));
|
|
155
|
+
let transferEncoding = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Transfer-Encoding'));
|
|
156
|
+
let disposition = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Disposition'));
|
|
157
|
+
if (contentType.value.toLowerCase().trim()) {
|
|
158
|
+
meta.contentType = contentType.value.toLowerCase().trim();
|
|
159
|
+
}
|
|
160
|
+
if (contentType.params.charset) {
|
|
161
|
+
meta.charset = contentType.params.charset.toLowerCase().trim();
|
|
162
|
+
}
|
|
163
|
+
if (transferEncoding.value) {
|
|
164
|
+
meta.encoding = transferEncoding.value
|
|
165
|
+
.replace(/\(.*\)/g, '')
|
|
166
|
+
.toLowerCase()
|
|
167
|
+
.trim();
|
|
168
|
+
}
|
|
169
|
+
if (disposition.value) {
|
|
170
|
+
/* c8 ignore next */ // a parsed disposition value is never all-whitespace, so the `false` fallback is unreachable
|
|
171
|
+
meta.disposition = disposition.value.toLowerCase().trim() || false;
|
|
172
|
+
try {
|
|
173
|
+
meta.disposition = libmime_1.default.decodeWords(meta.disposition);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// failed to parse disposition, keep as is (most probably an unknown charset is used)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (contentType.params.format && contentType.params.format.toLowerCase().trim() === 'flowed') {
|
|
180
|
+
meta.flowed = true;
|
|
181
|
+
if (contentType.params.delsp && contentType.params.delsp.toLowerCase().trim() === 'yes') {
|
|
182
|
+
meta.delSp = true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
let filename = disposition.params.filename || contentType.params.name || false;
|
|
186
|
+
if (filename) {
|
|
187
|
+
try {
|
|
188
|
+
filename = libmime_1.default.decodeWords(filename);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// failed to parse filename, keep as is (most probably an unknown charset is used)
|
|
192
|
+
}
|
|
193
|
+
meta.filename = filename;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
let stream;
|
|
197
|
+
let output;
|
|
198
|
+
let fetchAborted = false;
|
|
199
|
+
// Build a decoder pipeline that progressively transforms the raw FETCH data:
|
|
200
|
+
// 1. Transfer-encoding decoder (base64 or quoted-printable -> binary)
|
|
201
|
+
// 2. Format decoder (format=flowed -> plain text, if applicable)
|
|
202
|
+
// 3. Charset decoder (non-UTF-8 -> UTF-8, for text parts only)
|
|
203
|
+
// 4. Byte limiter (enforces maxBytes cap)
|
|
204
|
+
// `stream` is the head of the pipeline (where raw chunks are written),
|
|
205
|
+
// `output` is the tail (what the caller reads from).
|
|
206
|
+
// Parts that arrived via FETCH BINARY (response.binaryParts) are already
|
|
207
|
+
// decoded by the server - decoding again would corrupt the data, so stage 1
|
|
208
|
+
// is skipped for them.
|
|
209
|
+
let clientEncoding = response.binaryParts && part && response.binaryParts.has(part) ? false : meta.encoding;
|
|
210
|
+
switch (clientEncoding) {
|
|
211
|
+
case 'base64':
|
|
212
|
+
output = stream = new libbase64_1.default.Decoder();
|
|
213
|
+
break;
|
|
214
|
+
case 'quoted-printable':
|
|
215
|
+
output = stream = new libqp_1.default.Decoder();
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
output = stream = new node_stream_1.PassThrough();
|
|
219
|
+
}
|
|
220
|
+
// Every byte-bounded stage of the pipeline. The fetch loop below stops as soon as any of
|
|
221
|
+
// them has taken all it will accept. The limiter at the tail is not enough on its own: a
|
|
222
|
+
// transform in the middle that buffers its whole input before emitting anything (the
|
|
223
|
+
// format=flowed decoder, the Japanese charset decoder) leaves the tail limiter reporting
|
|
224
|
+
// `limited === false` however much the server sends, so a download with a small maxBytes
|
|
225
|
+
// would still pull the entire part off the wire.
|
|
226
|
+
let limiters = [];
|
|
227
|
+
let isLimited = () => limiters.some(entry => entry.limited);
|
|
228
|
+
// Appending a stage means forwarding the current tail's errors to it before piping, so a
|
|
229
|
+
// failure anywhere reaches the stream the caller is reading
|
|
230
|
+
let pipeStage = (stage) => {
|
|
231
|
+
output.on('error', err => {
|
|
232
|
+
stage.emit('error', err);
|
|
233
|
+
});
|
|
234
|
+
output = output.pipe(stage);
|
|
235
|
+
return stage;
|
|
236
|
+
};
|
|
237
|
+
let isTextNode = ['text/html', 'text/plain', 'text/x-amp-html'].includes(meta.contentType) || (part === '1' && !meta.contentType);
|
|
238
|
+
if ((!meta.disposition || meta.disposition === 'inline') && isTextNode) {
|
|
239
|
+
// RFC 3676 format=flowed text: unwrap soft line breaks
|
|
240
|
+
if (meta.flowed) {
|
|
241
|
+
// FlowedDecoder buffers its whole input before emitting, and being third party it
|
|
242
|
+
// carries no bound of its own, so bound what it can ever be handed. Unwrapping only
|
|
243
|
+
// removes bytes, so capping its input at maxBytes cannot push the delivered output
|
|
244
|
+
// above the cap either.
|
|
245
|
+
limiters.push(pipeStage(new limited_passthrough_js_1.LimitedPassthrough({ maxBytes })));
|
|
246
|
+
pipeStage(new flowed_decoder_js_1.default(meta.delSp ? { delSp: true } : {}));
|
|
247
|
+
}
|
|
248
|
+
// Convert non-UTF-8 charsets to UTF-8 via a streaming decoder.
|
|
249
|
+
// ASCII and UTF-8 need no conversion. Unknown charsets are left as-is.
|
|
250
|
+
if (meta.charset && !['ascii', 'usascii', 'utf8'].includes(meta.charset.toLowerCase().replace(/[^a-z0-9]+/g, ''))) {
|
|
251
|
+
try {
|
|
252
|
+
let decoder = (0, tools_js_1.getDecoder)(meta.charset, maxBytes);
|
|
253
|
+
// Safety listener attached first so the decoder always has at least
|
|
254
|
+
// one 'error' listener. Prevents Node.js from throwing
|
|
255
|
+
// ERR_UNHANDLED_ERROR if a later pipe setup step throws and leaves
|
|
256
|
+
// the source-forwarding closure attached without a downstream
|
|
257
|
+
// listener wired up. Any real listener the caller attaches still
|
|
258
|
+
// fires in addition to this one.
|
|
259
|
+
decoder.on('error', err => {
|
|
260
|
+
client.log.warn({ err, charset: meta.charset, cid: client.id });
|
|
261
|
+
});
|
|
262
|
+
// The Japanese decoder buffers its whole input as well, and reports the same
|
|
263
|
+
// `limited` flag the limiters do so the fetch loop can stop once it is full.
|
|
264
|
+
// A streaming decoder has no such flag, which reads as false and is correct.
|
|
265
|
+
limiters.push(pipeStage(decoder));
|
|
266
|
+
// force to utf-8 for output
|
|
267
|
+
meta.charset = 'utf-8';
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// do not decode charset
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
let limiter = pipeStage(new limited_passthrough_js_1.LimitedPassthrough({ maxBytes }));
|
|
275
|
+
limiters.push(limiter);
|
|
276
|
+
// Cleanup function
|
|
277
|
+
const cleanup = () => {
|
|
278
|
+
fetchAborted = true;
|
|
279
|
+
if (stream && !stream.destroyed) {
|
|
280
|
+
stream.destroy();
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
// Listen for stream destruction
|
|
284
|
+
output.once('error', cleanup);
|
|
285
|
+
output.once('close', cleanup);
|
|
286
|
+
let writeChunk = (chunk) => {
|
|
287
|
+
if (isLimited() || fetchAborted || stream.destroyed) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
return stream.write(chunk);
|
|
291
|
+
};
|
|
292
|
+
// Ceiling on how many bytes one download may pull off the wire, as the backstop for the
|
|
293
|
+
// partial-ignoring servers above: a part whose size happens to equal chunkSize exactly
|
|
294
|
+
// comes back looking like a full window every time, so no test over chunk lengths can end
|
|
295
|
+
// that loop. RFC822.SIZE bounds any part of the message; doubled for servers that count
|
|
296
|
+
// line endings differently than they deliver, plus one window so a download sitting right
|
|
297
|
+
// at the bound still gets its terminating chunk. Infinity when the server reported no
|
|
298
|
+
// size, which leaves the loop bounded by maxBytes alone.
|
|
299
|
+
let maxTotalBytes = (0, limited_passthrough_js_1.normalizeByteLimit)(meta.expectedSize ? meta.expectedSize * 2 + chunkSize : 0);
|
|
300
|
+
// Fetch remaining chunks in a loop, writing each to the decoder stream.
|
|
301
|
+
// Stops when the server returns a short chunk (< chunkSize), answers with more than the
|
|
302
|
+
// requested window, the byte limiter is satisfied, or the consumer destroys the output
|
|
303
|
+
// stream. Throws when the ceiling above is crossed.
|
|
304
|
+
let fetchAllParts = async () => {
|
|
305
|
+
while (hasMore && !isLimited() && !fetchAborted) {
|
|
306
|
+
if (processed >= maxTotalBytes) {
|
|
307
|
+
// Loud on purpose. Everything written downstream by this point holds
|
|
308
|
+
// duplicated content, and a quiet stop is indistinguishable from a clean EOF,
|
|
309
|
+
// so the consumer would store a corrupt body believing it intact.
|
|
310
|
+
let err = new Error('Download exceeded the expected message size');
|
|
311
|
+
err.code = 'DownloadOverflow';
|
|
312
|
+
err.maxSize = maxTotalBytes;
|
|
313
|
+
err.cid = client.id;
|
|
314
|
+
throw err;
|
|
315
|
+
}
|
|
316
|
+
let { response, chunk } = await getNextPart();
|
|
317
|
+
if (fetchAborted) {
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
if (response === false) {
|
|
321
|
+
// The message is gone mid-download (expunged by another client, or the
|
|
322
|
+
// mailbox was closed). Ending the stream here would pass the truncated body
|
|
323
|
+
// off as complete, so the consumer is told the same way as for an overflow.
|
|
324
|
+
let err = new Error('Message disappeared before the download completed');
|
|
325
|
+
err.code = 'DownloadIncomplete';
|
|
326
|
+
err.cid = client.id;
|
|
327
|
+
throw err;
|
|
328
|
+
}
|
|
329
|
+
if (!chunk) {
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
// Handle backpressure
|
|
333
|
+
if (writeChunk(chunk) === false) {
|
|
334
|
+
// Wait for drain event before continuing
|
|
335
|
+
try {
|
|
336
|
+
await new Promise((resolve, reject) => {
|
|
337
|
+
// finish() is the listener itself, as settle() is for the TLS upgrade:
|
|
338
|
+
// 'drain' and 'close' emit no arguments, 'error' emits the error, and
|
|
339
|
+
// removal needs no separate handler references. It removes only the
|
|
340
|
+
// three listeners this wait installed - removeAllListeners('error')
|
|
341
|
+
// also took off the forwarder pipeStage() attached to the head stream
|
|
342
|
+
// when the pipeline was built, and the head must keep that forwarder
|
|
343
|
+
// for the life of the download or a chunk failure has nowhere to go.
|
|
344
|
+
const finish = (err) => {
|
|
345
|
+
for (let event of ['drain', 'error', 'close']) {
|
|
346
|
+
stream.removeListener(event, finish);
|
|
347
|
+
}
|
|
348
|
+
/* c8 ignore next 2 */ // stream error during a backpressure drain wait is timing-dependent
|
|
349
|
+
if (err) {
|
|
350
|
+
reject(err);
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
resolve();
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
stream.once('drain', finish);
|
|
357
|
+
stream.once('error', finish);
|
|
358
|
+
stream.once('close', finish);
|
|
359
|
+
});
|
|
360
|
+
/* c8 ignore start */ // re-throw path only triggers on a stream error mid-drain, which is timing-dependent
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
// Re-throw only if not aborted
|
|
364
|
+
if (!fetchAborted) {
|
|
365
|
+
throw err;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/* c8 ignore stop */
|
|
369
|
+
// Check if we should abort after waiting
|
|
370
|
+
if (fetchAborted) {
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
// A download is a sequence of chunk FETCHes with a backpressure wait in between. Those
|
|
377
|
+
// gaps look exactly like an inactive connection, so without this auto-IDLE would start
|
|
378
|
+
// between chunks and the next chunk would have to break it again - two extra round
|
|
379
|
+
// trips per chunk, for as long as the consumer is slow. Counted before control returns
|
|
380
|
+
// to the event loop: the head chunk's own FETCH already armed the auto-IDLE timer, and
|
|
381
|
+
// with a very short autoIdleDelay that timer could otherwise fire before the deferred
|
|
382
|
+
// chunk loop below has marked the download open.
|
|
383
|
+
client._openDownloads++;
|
|
384
|
+
let downloadDone = false;
|
|
385
|
+
let finishDownload = () => {
|
|
386
|
+
if (!downloadDone) {
|
|
387
|
+
downloadDone = true;
|
|
388
|
+
client._openDownloads--;
|
|
389
|
+
client.autoidle();
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
// Kick off the download pipeline asynchronously. The first chunk was
|
|
393
|
+
// already fetched above (to get metadata); write it to the decoder
|
|
394
|
+
// stream and then fetch remaining chunks via fetchAllParts().
|
|
395
|
+
// setImmediate ensures the caller gets the {meta, content} return
|
|
396
|
+
// value before streaming begins.
|
|
397
|
+
let runFetchAllParts = () => {
|
|
398
|
+
fetchAllParts()
|
|
399
|
+
.catch(err => {
|
|
400
|
+
if (!fetchAborted && stream && !stream.destroyed) {
|
|
401
|
+
stream.emit('error', err);
|
|
402
|
+
/* c8 ignore start */ // the else logs when a fetch error arrives after the stream was already torn down (timing-dependent)
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
// Log when error cannot be emitted to stream
|
|
406
|
+
client.log.warn({
|
|
407
|
+
msg: 'Download error after stream closed',
|
|
408
|
+
err,
|
|
409
|
+
fetchAborted,
|
|
410
|
+
streamDestroyed: stream?.destroyed,
|
|
411
|
+
cid: client.id
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
/* c8 ignore stop */
|
|
415
|
+
})
|
|
416
|
+
.finally(() => {
|
|
417
|
+
finishDownload();
|
|
418
|
+
if (!fetchAborted && stream && !stream.destroyed) {
|
|
419
|
+
stream.end();
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
// Terminal guard: nothing consumes this chain, so a throw from either handler
|
|
423
|
+
// above rejects a promise nobody holds and takes the process down on
|
|
424
|
+
// unhandledRejection. Reaching it always means an invariant broke - the head
|
|
425
|
+
// stream kept pipeStage()'s error forwarder for the life of the download, so
|
|
426
|
+
// emit('error') above has somewhere to go - which is why it logs at error even
|
|
427
|
+
// for a routine-looking connection code.
|
|
428
|
+
.catch(err => client.log.error({ msg: 'Failed to fail the download stream', err, cid: client.id }));
|
|
429
|
+
};
|
|
430
|
+
setImmediate(() => {
|
|
431
|
+
let writeResult;
|
|
432
|
+
try {
|
|
433
|
+
writeResult = writeChunk(chunk);
|
|
434
|
+
}
|
|
435
|
+
catch (err) {
|
|
436
|
+
stream.emit('error', err);
|
|
437
|
+
finishDownload();
|
|
438
|
+
/* c8 ignore next 3 */ // emitting the error above triggers cleanup (fetchAborted=true), so this end() guard is already false here
|
|
439
|
+
if (!fetchAborted && stream && !stream.destroyed) {
|
|
440
|
+
stream.end();
|
|
441
|
+
}
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
/* c8 ignore next 9 */ // `stream` is piped to the limiter before this runs, so the head write drains synchronously and always returns true (verified for chunkSize up to 8MB); the drain-wait branch is unreachable
|
|
445
|
+
if (!writeResult) {
|
|
446
|
+
// Initial chunk filled the buffer, wait for drain
|
|
447
|
+
stream.once('drain', () => {
|
|
448
|
+
if (!fetchAborted) {
|
|
449
|
+
runFetchAllParts();
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
finishDownload();
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
runFetchAllParts();
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
return {
|
|
461
|
+
meta,
|
|
462
|
+
content: output
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Implements ImapFlow.downloadMany(), see its documentation
|
|
467
|
+
*
|
|
468
|
+
* @param client - Connection to download from
|
|
469
|
+
* @param range - UID or sequence number of the message
|
|
470
|
+
* @param parts - Body parts to download
|
|
471
|
+
* @param options - Download options
|
|
472
|
+
* @returns Downloaded parts keyed by part number
|
|
473
|
+
*/
|
|
474
|
+
async function downloadMessageParts(client, range, parts, options) {
|
|
475
|
+
if (!client.mailbox) {
|
|
476
|
+
// no mailbox selected, nothing to do
|
|
477
|
+
return {};
|
|
478
|
+
}
|
|
479
|
+
let downloadOptions = Object.assign({
|
|
480
|
+
chunkSize: 64 * 1024,
|
|
481
|
+
maxBytes: Infinity
|
|
482
|
+
}, options || {});
|
|
483
|
+
let query = { bodyParts: [] };
|
|
484
|
+
for (let part of parts) {
|
|
485
|
+
query.bodyParts.push(part + '.mime');
|
|
486
|
+
query.bodyParts.push(part);
|
|
487
|
+
}
|
|
488
|
+
let response = await client.fetchOne(range, query, downloadOptions);
|
|
489
|
+
if (!response || !response.bodyParts) {
|
|
490
|
+
return {};
|
|
491
|
+
}
|
|
492
|
+
let data = {};
|
|
493
|
+
for (let [part, content] of response.bodyParts) {
|
|
494
|
+
let keyParts = part.split('.mime');
|
|
495
|
+
// The server chooses the BODY[...] keys it answers with: never let one be a
|
|
496
|
+
// prototype-chain name, or the assignments below write onto Object.prototype
|
|
497
|
+
// (process-wide pollution) instead of the result object.
|
|
498
|
+
if ((0, tools_js_1.isUnsafeKey)(keyParts[0])) {
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
if (keyParts.length === 1) {
|
|
502
|
+
// content
|
|
503
|
+
let key = keyParts[0];
|
|
504
|
+
if (!data[key]) {
|
|
505
|
+
data[key] = { content };
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
data[key].content = content;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
else if (keyParts.length === 2) {
|
|
512
|
+
// header
|
|
513
|
+
let key = keyParts[0];
|
|
514
|
+
if (!data[key]) {
|
|
515
|
+
data[key] = {};
|
|
516
|
+
}
|
|
517
|
+
let entry = data[key];
|
|
518
|
+
if (!entry.meta) {
|
|
519
|
+
entry.meta = {};
|
|
520
|
+
}
|
|
521
|
+
let meta = entry.meta;
|
|
522
|
+
let headers = new mailsplit_1.Headers(content);
|
|
523
|
+
let contentType = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Type'));
|
|
524
|
+
let transferEncoding = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Transfer-Encoding'));
|
|
525
|
+
let disposition = libmime_1.default.parseHeaderValue(headers.getFirst('Content-Disposition'));
|
|
526
|
+
if (contentType.value.toLowerCase().trim()) {
|
|
527
|
+
meta.contentType = contentType.value.toLowerCase().trim();
|
|
528
|
+
}
|
|
529
|
+
if (contentType.params.charset) {
|
|
530
|
+
meta.charset = contentType.params.charset.toLowerCase().trim();
|
|
531
|
+
}
|
|
532
|
+
if (transferEncoding.value) {
|
|
533
|
+
meta.encoding = transferEncoding.value
|
|
534
|
+
.replace(/\(.*\)/g, '')
|
|
535
|
+
.toLowerCase()
|
|
536
|
+
.trim();
|
|
537
|
+
}
|
|
538
|
+
if (disposition.value) {
|
|
539
|
+
/* c8 ignore next */ // a parsed disposition value is never all-whitespace, so the `false` fallback is unreachable
|
|
540
|
+
meta.disposition = disposition.value.toLowerCase().trim() || false;
|
|
541
|
+
try {
|
|
542
|
+
meta.disposition = libmime_1.default.decodeWords(meta.disposition);
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
// failed to parse disposition, keep as is (most probably an unknown charset is used)
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
if (contentType.params.format && contentType.params.format.toLowerCase().trim() === 'flowed') {
|
|
549
|
+
meta.flowed = true;
|
|
550
|
+
if (contentType.params.delsp && contentType.params.delsp.toLowerCase().trim() === 'yes') {
|
|
551
|
+
meta.delSp = true;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
let filename = disposition.params.filename || contentType.params.name || false;
|
|
555
|
+
if (filename) {
|
|
556
|
+
try {
|
|
557
|
+
filename = libmime_1.default.decodeWords(filename);
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
// failed to parse filename, keep as is (most probably an unknown charset is used)
|
|
561
|
+
}
|
|
562
|
+
meta.filename = filename;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
for (let part of Object.keys(data)) {
|
|
567
|
+
let entry = data[part];
|
|
568
|
+
// `meta` is only built from the companion BODY[<part>.MIME] item. A server may
|
|
569
|
+
// legally answer with fewer items than were requested, and one part arriving
|
|
570
|
+
// without its MIME headers must not cost the caller the whole download.
|
|
571
|
+
let meta = entry.meta || {};
|
|
572
|
+
entry.meta = meta;
|
|
573
|
+
// parts that arrived via FETCH BINARY (response.binaryParts) are already
|
|
574
|
+
// decoded by the server - decoding again would corrupt the data
|
|
575
|
+
let clientEncoding = response.binaryParts && response.binaryParts.has(part) ? false : meta.encoding;
|
|
576
|
+
switch (clientEncoding) {
|
|
577
|
+
case 'base64':
|
|
578
|
+
entry.content = entry.content ? libbase64_1.default.decode(entry.content.toString()) : null;
|
|
579
|
+
break;
|
|
580
|
+
case 'quoted-printable':
|
|
581
|
+
entry.content = entry.content ? libqp_1.default.decode(entry.content.toString()) : null;
|
|
582
|
+
break;
|
|
583
|
+
default:
|
|
584
|
+
// keep as is, already a buffer
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return data;
|
|
588
|
+
}
|
package/dist/cjs/errors.d.ts
CHANGED
|
@@ -1,11 +1,58 @@
|
|
|
1
1
|
import type { ImapResponse } from './handler/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `code` values ImapFlow sets on the errors it raises, so they can be matched without
|
|
4
|
+
* string literals: `if (err.code === ImapFlowErrorCode.NoConnection)`.
|
|
5
|
+
*
|
|
6
|
+
* Parser failures use `ParserError` followed by a number (`ParserError11`) and are not listed
|
|
7
|
+
* one by one, test them with `err.code?.startsWith('ParserError')`. Errors from the socket, TLS
|
|
8
|
+
* or DNS layer pass through with Node's own code (`ECONNREFUSED`, `ENOTFOUND`, ...).
|
|
9
|
+
*/
|
|
10
|
+
export declare const ImapFlowErrorCode: {
|
|
11
|
+
readonly NoConnection: "NoConnection";
|
|
12
|
+
readonly EConnectionClosed: "EConnectionClosed";
|
|
13
|
+
readonly StateLogout: "StateLogout";
|
|
14
|
+
readonly ClosedAfterConnectText: "ClosedAfterConnectText";
|
|
15
|
+
readonly ClosedAfterConnectTLS: "ClosedAfterConnectTLS";
|
|
16
|
+
readonly CONNECT_TIMEOUT: "CONNECT_TIMEOUT";
|
|
17
|
+
readonly GREETING_TIMEOUT: "GREETING_TIMEOUT";
|
|
18
|
+
readonly UPGRADE_TIMEOUT: "UPGRADE_TIMEOUT";
|
|
19
|
+
readonly ETIMEOUT: "ETIMEOUT";
|
|
20
|
+
readonly LockTimeout: "LockTimeout";
|
|
21
|
+
readonly ETHROTTLE: "ETHROTTLE";
|
|
22
|
+
readonly UnexpectedTag: "UnexpectedTag";
|
|
23
|
+
readonly InvalidResponse: "InvalidResponse";
|
|
24
|
+
readonly ResponseProcessingFailed: "ResponseProcessingFailed";
|
|
25
|
+
readonly STARTTLS_INJECTION: "STARTTLS_INJECTION";
|
|
26
|
+
readonly COMPRESS_TRAILING_DATA: "COMPRESS_TRAILING_DATA";
|
|
27
|
+
readonly PollFailed: "PollFailed";
|
|
28
|
+
readonly NotFound: "NotFound";
|
|
29
|
+
readonly MissingServerExtension: "MissingServerExtension";
|
|
30
|
+
readonly ParserError: "ParserError";
|
|
31
|
+
readonly ParserErrorExchange: "ParserErrorExchange";
|
|
32
|
+
readonly MAX_IMAP_NESTING_REACHED: "MAX_IMAP_NESTING_REACHED";
|
|
33
|
+
readonly LineTooLarge: "LineTooLarge";
|
|
34
|
+
readonly LiteralTooLarge: "LiteralTooLarge";
|
|
35
|
+
readonly ResponseTooLarge: "ResponseTooLarge";
|
|
36
|
+
readonly InvalidStringValue: "InvalidStringValue";
|
|
37
|
+
readonly InvalidTokenValue: "InvalidTokenValue";
|
|
38
|
+
readonly InvalidTextValue: "InvalidTextValue";
|
|
39
|
+
readonly InvalidSequenceSet: "InvalidSequenceSet";
|
|
40
|
+
readonly DownloadOverflow: "DownloadOverflow";
|
|
41
|
+
readonly DownloadIncomplete: "DownloadIncomplete";
|
|
42
|
+
readonly ProxyError: "ProxyError";
|
|
43
|
+
readonly EPROXY: "EPROXY";
|
|
44
|
+
readonly UnsupportedProxyAddress: "UnsupportedProxyAddress";
|
|
45
|
+
readonly ERR_INVALID_URL: "ERR_INVALID_URL";
|
|
46
|
+
};
|
|
47
|
+
/** One of the {@link ImapFlowErrorCode} values */
|
|
48
|
+
export type ImapFlowErrorCode = (typeof ImapFlowErrorCode)[keyof typeof ImapFlowErrorCode];
|
|
2
49
|
/**
|
|
3
50
|
* An Error raised by ImapFlow, with the extra properties the library attaches to describe
|
|
4
51
|
* the failure. Every property is optional: which ones are present depends on where the
|
|
5
52
|
* error came from.
|
|
6
53
|
*/
|
|
7
54
|
export interface ImapFlowError extends Error {
|
|
8
|
-
/** Error code,
|
|
55
|
+
/** Error code, one of {@link ImapFlowErrorCode}, a parser error code or a code from Node */
|
|
9
56
|
code?: string | undefined;
|
|
10
57
|
/** Connection id the error belongs to */
|
|
11
58
|
cid?: string | undefined;
|
|
@@ -33,6 +80,8 @@ export interface ImapFlowError extends Error {
|
|
|
33
80
|
tlsFailed?: boolean | undefined;
|
|
34
81
|
/** Server suggested back-off in milliseconds for an ETHROTTLE error */
|
|
35
82
|
throttleReset?: number | undefined;
|
|
83
|
+
/** Milliseconds of the ETHROTTLE back-off the connection already waited before rejecting */
|
|
84
|
+
throttleWaited?: number | undefined;
|
|
36
85
|
/** Additional details, e.g. the timeouts that applied */
|
|
37
86
|
details?: {
|
|
38
87
|
[key: string]: any;
|