imapflow 1.7.8 → 2.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/CHANGELOG.md +20 -0
- package/README.md +8 -2
- package/dist/cjs/charsets.d.ts +1 -0
- package/dist/cjs/charsets.js +294 -0
- package/dist/cjs/commands/append.d.ts +22 -0
- package/dist/cjs/commands/append.js +151 -0
- package/dist/cjs/commands/authenticate.d.ts +24 -0
- package/dist/cjs/commands/authenticate.js +223 -0
- package/dist/cjs/commands/capability.d.ts +8 -0
- package/dist/cjs/commands/capability.js +32 -0
- package/dist/cjs/commands/close.d.ts +8 -0
- package/dist/cjs/commands/close.js +39 -0
- package/dist/cjs/commands/compress.d.ts +8 -0
- package/dist/cjs/commands/compress.js +56 -0
- package/dist/cjs/commands/copy.d.ts +13 -0
- package/dist/cjs/commands/copy.js +44 -0
- package/dist/cjs/commands/copyuid-parser.d.ts +11 -0
- package/dist/cjs/commands/copyuid-parser.js +32 -0
- package/dist/cjs/commands/create.d.ts +11 -0
- package/dist/cjs/commands/create.js +80 -0
- package/dist/cjs/commands/delete.d.ts +11 -0
- package/dist/cjs/commands/delete.js +40 -0
- package/dist/cjs/commands/enable.d.ts +9 -0
- package/dist/cjs/commands/enable.js +61 -0
- package/dist/cjs/commands/esearch-parser.d.ts +17 -0
- package/dist/cjs/commands/esearch-parser.js +91 -0
- package/dist/cjs/commands/expunge.d.ts +12 -0
- package/dist/cjs/commands/expunge.js +60 -0
- package/dist/cjs/commands/fetch.d.ts +30 -0
- package/dist/cjs/commands/fetch.js +241 -0
- package/dist/cjs/commands/id.d.ts +10 -0
- package/dist/cjs/commands/id.js +80 -0
- package/dist/cjs/commands/idle.d.ts +9 -0
- package/dist/cjs/commands/idle.js +347 -0
- package/dist/cjs/commands/list.d.ts +16 -0
- package/dist/cjs/commands/list.js +518 -0
- package/dist/cjs/commands/login.d.ts +11 -0
- package/dist/cjs/commands/login.js +42 -0
- package/dist/cjs/commands/logout.d.ts +8 -0
- package/dist/cjs/commands/logout.js +47 -0
- package/dist/cjs/commands/move.d.ts +13 -0
- package/dist/cjs/commands/move.js +57 -0
- package/dist/cjs/commands/namespace.d.ts +25 -0
- package/dist/cjs/commands/namespace.js +139 -0
- package/dist/cjs/commands/noop.d.ts +8 -0
- package/dist/cjs/commands/noop.js +22 -0
- package/dist/cjs/commands/quota.d.ts +10 -0
- package/dist/cjs/commands/quota.js +119 -0
- package/dist/cjs/commands/rename.d.ts +12 -0
- package/dist/cjs/commands/rename.js +48 -0
- package/dist/cjs/commands/search.d.ts +15 -0
- package/dist/cjs/commands/search.js +228 -0
- package/dist/cjs/commands/select.d.ts +25 -0
- package/dist/cjs/commands/select.js +250 -0
- package/dist/cjs/commands/starttls.d.ts +8 -0
- package/dist/cjs/commands/starttls.js +30 -0
- package/dist/cjs/commands/status-fields.d.ts +14 -0
- package/dist/cjs/commands/status-fields.js +61 -0
- package/dist/cjs/commands/status.d.ts +12 -0
- package/dist/cjs/commands/status.js +108 -0
- package/dist/cjs/commands/store.d.ts +19 -0
- package/dist/cjs/commands/store.js +93 -0
- package/dist/cjs/commands/subscribe.d.ts +9 -0
- package/dist/cjs/commands/subscribe.js +31 -0
- package/dist/cjs/commands/unsubscribe.d.ts +9 -0
- package/dist/cjs/commands/unsubscribe.js +31 -0
- package/dist/cjs/connection-deadline.d.ts +49 -0
- package/dist/cjs/connection-deadline.js +91 -0
- package/dist/cjs/errors.d.ts +83 -0
- package/dist/cjs/errors.js +13 -0
- package/dist/cjs/handler/imap-compiler.d.ts +24 -0
- package/dist/cjs/handler/imap-compiler.js +285 -0
- package/dist/cjs/handler/imap-formal-syntax.d.ts +28 -0
- package/dist/cjs/handler/imap-formal-syntax.js +121 -0
- package/dist/cjs/handler/imap-handler.d.ts +9 -0
- package/dist/cjs/handler/imap-handler.js +10 -0
- package/dist/cjs/handler/imap-parser.d.ts +16 -0
- package/dist/cjs/handler/imap-parser.js +90 -0
- package/dist/cjs/handler/imap-stream.d.ts +181 -0
- package/dist/cjs/handler/imap-stream.js +446 -0
- package/dist/cjs/handler/limits.d.ts +25 -0
- package/dist/cjs/handler/limits.js +51 -0
- package/dist/cjs/handler/parser-instance.d.ts +68 -0
- package/dist/cjs/handler/parser-instance.js +223 -0
- package/dist/cjs/handler/token-parser.d.ts +91 -0
- package/dist/cjs/handler/token-parser.js +673 -0
- package/dist/cjs/handler/types.d.ts +91 -0
- package/dist/cjs/handler/types.js +4 -0
- package/dist/cjs/imap-commands.d.ts +16 -0
- package/dist/cjs/imap-commands.js +74 -0
- package/dist/cjs/imap-flow.d.ts +676 -0
- package/dist/cjs/imap-flow.js +3949 -0
- package/dist/cjs/jp-decoder.d.ts +12 -0
- package/dist/cjs/jp-decoder.js +79 -0
- package/dist/cjs/limited-passthrough.d.ts +25 -0
- package/dist/cjs/limited-passthrough.js +54 -0
- package/dist/cjs/logger.d.ts +3 -0
- package/dist/cjs/logger.js +11 -0
- package/dist/cjs/package-info.d.ts +3 -0
- package/dist/cjs/package-info.js +7 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/proxy-connection.d.ts +33 -0
- package/dist/cjs/proxy-connection.js +392 -0
- package/dist/cjs/search-compiler.d.ts +34 -0
- package/dist/cjs/search-compiler.js +476 -0
- package/dist/cjs/special-use.d.ts +22 -0
- package/dist/cjs/special-use.js +911 -0
- package/dist/cjs/tools.d.ts +427 -0
- package/dist/cjs/tools.js +1496 -0
- package/{lib/imap-flow.d.ts → dist/cjs/types.d.ts} +386 -516
- package/dist/cjs/types.js +5 -0
- package/dist/esm/charsets.d.ts +1 -0
- package/{lib → dist/esm}/charsets.js +1 -6
- package/dist/esm/commands/append.d.ts +22 -0
- package/{lib → dist/esm}/commands/append.js +22 -52
- package/dist/esm/commands/authenticate.d.ts +24 -0
- package/{lib → dist/esm}/commands/authenticate.js +62 -87
- package/dist/esm/commands/capability.d.ts +8 -0
- package/{lib → dist/esm}/commands/capability.js +6 -9
- package/dist/esm/commands/close.d.ts +8 -0
- package/{lib → dist/esm}/commands/close.js +6 -10
- package/dist/esm/commands/compress.d.ts +8 -0
- package/{lib → dist/esm}/commands/compress.js +7 -11
- package/dist/esm/commands/copy.d.ts +13 -0
- package/{lib → dist/esm}/commands/copy.js +12 -20
- package/dist/esm/commands/copyuid-parser.d.ts +11 -0
- package/{lib → dist/esm}/commands/copyuid-parser.js +9 -15
- package/dist/esm/commands/create.d.ts +11 -0
- package/{lib → dist/esm}/commands/create.js +13 -27
- package/dist/esm/commands/delete.d.ts +11 -0
- package/{lib → dist/esm}/commands/delete.js +9 -14
- package/dist/esm/commands/enable.d.ts +9 -0
- package/{lib → dist/esm}/commands/enable.js +23 -30
- package/dist/esm/commands/esearch-parser.d.ts +17 -0
- package/dist/esm/commands/esearch-parser.js +88 -0
- package/dist/esm/commands/expunge.d.ts +12 -0
- package/{lib → dist/esm}/commands/expunge.js +17 -22
- package/dist/esm/commands/fetch.d.ts +30 -0
- package/{lib → dist/esm}/commands/fetch.js +32 -64
- package/dist/esm/commands/id.d.ts +10 -0
- package/{lib → dist/esm}/commands/id.js +17 -23
- package/dist/esm/commands/idle.d.ts +9 -0
- package/{lib → dist/esm}/commands/idle.js +47 -81
- package/dist/esm/commands/list.d.ts +16 -0
- package/{lib → dist/esm}/commands/list.js +56 -121
- package/dist/esm/commands/login.d.ts +11 -0
- package/{lib → dist/esm}/commands/login.js +10 -15
- package/dist/esm/commands/logout.d.ts +8 -0
- package/{lib → dist/esm}/commands/logout.js +9 -11
- package/dist/esm/commands/move.d.ts +13 -0
- package/{lib → dist/esm}/commands/move.js +13 -21
- package/dist/esm/commands/namespace.d.ts +25 -0
- package/{lib → dist/esm}/commands/namespace.js +34 -44
- package/dist/esm/commands/noop.d.ts +8 -0
- package/{lib → dist/esm}/commands/noop.js +6 -7
- package/dist/esm/commands/quota.d.ts +10 -0
- package/{lib → dist/esm}/commands/quota.js +18 -36
- package/dist/esm/commands/rename.d.ts +12 -0
- package/{lib → dist/esm}/commands/rename.js +10 -15
- package/dist/esm/commands/search.d.ts +15 -0
- package/{lib → dist/esm}/commands/search.js +36 -135
- package/dist/esm/commands/select.d.ts +25 -0
- package/{lib → dist/esm}/commands/select.js +33 -64
- package/dist/esm/commands/starttls.d.ts +8 -0
- package/{lib → dist/esm}/commands/starttls.js +6 -8
- package/dist/esm/commands/status-fields.d.ts +14 -0
- package/{lib → dist/esm}/commands/status-fields.js +5 -16
- package/dist/esm/commands/status.d.ts +12 -0
- package/{lib → dist/esm}/commands/status.js +18 -29
- package/dist/esm/commands/store.d.ts +19 -0
- package/{lib → dist/esm}/commands/store.js +24 -37
- package/dist/esm/commands/subscribe.d.ts +9 -0
- package/{lib → dist/esm}/commands/subscribe.js +8 -12
- package/dist/esm/commands/unsubscribe.d.ts +9 -0
- package/{lib → dist/esm}/commands/unsubscribe.js +8 -12
- package/dist/esm/connection-deadline.d.ts +49 -0
- package/{lib → dist/esm}/connection-deadline.js +14 -25
- package/dist/esm/errors.d.ts +83 -0
- package/dist/esm/errors.js +9 -0
- package/dist/esm/handler/imap-compiler.d.ts +24 -0
- package/{lib → dist/esm}/handler/imap-compiler.js +22 -80
- package/dist/esm/handler/imap-formal-syntax.d.ts +28 -0
- package/dist/esm/handler/imap-formal-syntax.js +117 -0
- package/dist/esm/handler/imap-handler.d.ts +9 -0
- package/dist/esm/handler/imap-handler.js +9 -0
- package/dist/esm/handler/imap-parser.d.ts +16 -0
- package/{lib → dist/esm}/handler/imap-parser.js +31 -44
- package/dist/esm/handler/imap-stream.d.ts +181 -0
- package/{lib → dist/esm}/handler/imap-stream.js +29 -121
- package/dist/esm/handler/limits.d.ts +25 -0
- package/{lib → dist/esm}/handler/limits.js +13 -22
- package/dist/esm/handler/parser-instance.d.ts +68 -0
- package/{lib → dist/esm}/handler/parser-instance.js +19 -47
- package/dist/esm/handler/token-parser.d.ts +91 -0
- package/{lib → dist/esm}/handler/token-parser.js +71 -155
- package/dist/esm/handler/types.d.ts +91 -0
- package/dist/esm/handler/types.js +3 -0
- package/dist/esm/imap-commands.d.ts +16 -0
- package/dist/esm/imap-commands.js +67 -0
- package/dist/esm/imap-flow.d.ts +676 -0
- package/{lib → dist/esm}/imap-flow.js +761 -1789
- package/dist/esm/jp-decoder.d.ts +12 -0
- package/{lib → dist/esm}/jp-decoder.js +6 -21
- package/dist/esm/limited-passthrough.d.ts +25 -0
- package/{lib → dist/esm}/limited-passthrough.js +7 -20
- package/dist/esm/logger.d.ts +3 -0
- package/dist/esm/logger.js +4 -0
- package/dist/esm/package-info.d.ts +3 -0
- package/dist/esm/package-info.js +4 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/proxy-connection.d.ts +33 -0
- package/{lib → dist/esm}/proxy-connection.js +56 -127
- package/dist/esm/search-compiler.d.ts +34 -0
- package/{lib → dist/esm}/search-compiler.js +54 -110
- package/dist/esm/special-use.d.ts +22 -0
- package/dist/esm/special-use.js +907 -0
- package/dist/esm/tools.d.ts +427 -0
- package/dist/esm/tools.js +1446 -0
- package/dist/esm/types.d.ts +828 -0
- package/dist/esm/types.js +4 -0
- package/package.json +60 -20
- package/.gitattributes +0 -1
- package/.github/CODE_OF_CONDUCT.md +0 -76
- package/.github/FUNDING.yml +0 -4
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -19
- package/.github/contributing.md +0 -17
- package/.github/workflows/release.yaml +0 -36
- package/.github/workflows/stale.yml +0 -29
- package/.github/workflows/test.yml +0 -51
- package/.ncurc.js +0 -4
- package/.prettierignore +0 -4
- package/.prettierrc.js +0 -8
- package/.release-please-manifest.json +0 -3
- package/CLAUDE.md +0 -104
- package/Gruntfile.js +0 -23
- package/eslint.config.js +0 -45
- package/lib/handler/imap-formal-syntax.js +0 -189
- package/lib/handler/imap-handler.js +0 -17
- package/lib/imap-commands.js +0 -45
- package/lib/logger.js +0 -5
- package/lib/special-use.js +0 -923
- package/lib/tools.js +0 -1612
- package/release-please-config.json +0 -10
- package/test/authentication-test.js +0 -101
- package/test/auto-idle-test.js +0 -470
- package/test/bodystructure-test.js +0 -899
- package/test/charsets-test.js +0 -161
- package/test/commands-branches-test.js +0 -1095
- package/test/commands-integration-test.js +0 -11124
- package/test/commands-test.js +0 -73
- package/test/connection-edge-cases-test.js +0 -1828
- package/test/connection-test.js +0 -162
- package/test/copyuid-parser-test.js +0 -173
- package/test/fetch-generator-test.js +0 -218
- package/test/fixtures/fake-timers.js +0 -115
- package/test/fixtures/serialized-mimetorture.js +0 -2738
- package/test/fixtures/test-client.js +0 -101
- package/test/fixtures/test-tls.js +0 -8
- package/test/handler-branches-test.js +0 -310
- package/test/idle-polling-test.js +0 -518
- package/test/imap-compiler-test.js +0 -809
- package/test/imap-flow-compress-test.js +0 -166
- package/test/imap-flow-coverage-test.js +0 -612
- package/test/imap-flow-fetch-download-test.js +0 -909
- package/test/imap-flow-internals-test.js +0 -725
- package/test/imap-flow-methods-test.js +0 -889
- package/test/imap-flow-proxy-paths-test.js +0 -366
- package/test/imap-flow-secure-test.js +0 -573
- package/test/imap-flow-server-test.js +0 -1474
- package/test/imap-formal-syntax-test.js +0 -293
- package/test/imap-parser-test.js +0 -1474
- package/test/imap-stream-edge-cases-test.js +0 -666
- package/test/imap-stream-test.js +0 -177
- package/test/imapflow-test.js +0 -258
- package/test/integration/README.md +0 -52
- package/test/integration/dovecot-test.conf +0 -27
- package/test/integration/rev2-live-test.js +0 -431
- package/test/integration/run-rev2-tests.sh +0 -75
- package/test/integration-test.js +0 -83
- package/test/jp-decoder-test.js +0 -304
- package/test/limited-passthrough-test.js +0 -299
- package/test/memory-cleanup-test.js +0 -144
- package/test/memory-leak-test.js +0 -667
- package/test/parser-limits-test.js +0 -292
- package/test/proxy-connection-test.js +0 -738
- package/test/reliability-improvements-test.js +0 -548
- package/test/search-compiler-test.js +0 -1300
- package/test/search-test.js +0 -329
- package/test/special-use-test.js +0 -418
- package/test/starttls-injection-test.js +0 -181
- package/test/tag-correlation-test.js +0 -333
- package/test/timer-policy-test.js +0 -227
- package/test/token-parser-test.js +0 -456
- package/test/tools-test.js +0 -2013
- package/test/unhandled-rejection-test.js +0 -661
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
|
+
export declare class JPDecoder extends Transform {
|
|
3
|
+
charset: string;
|
|
4
|
+
chunks: Buffer[];
|
|
5
|
+
chunklen: number;
|
|
6
|
+
maxBytes: number;
|
|
7
|
+
limited: boolean;
|
|
8
|
+
constructor(charset: string, maxBytes?: number | undefined);
|
|
9
|
+
_transform(chunk: Buffer | string, encoding: BufferEncoding, done: TransformCallback): void;
|
|
10
|
+
_flush(done: TransformCallback): void;
|
|
11
|
+
_destroy(err: Error | null, callback: (error: Error | null) => void): void;
|
|
12
|
+
}
|
|
@@ -1,59 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const encodingJapanese = require('encoding-japanese');
|
|
5
|
-
const { normalizeByteLimit } = require('./limited-passthrough.js');
|
|
6
|
-
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
import encodingJapanese from 'encoding-japanese';
|
|
3
|
+
import { normalizeByteLimit } from './limited-passthrough.js';
|
|
7
4
|
// A Transform stream for decoding Japanese character sets (Shift_JIS, EUC-JP, ISO-2022-JP).
|
|
8
5
|
// Unlike iconv-lite which can decode incrementally, encoding-japanese requires the complete
|
|
9
6
|
// input buffer for accurate charset detection and stateful decoding (especially ISO-2022-JP
|
|
10
7
|
// which uses escape sequences to switch between ASCII and multi-byte modes). Therefore,
|
|
11
8
|
// this stream buffers all input during _transform and performs the actual decoding in _flush.
|
|
12
|
-
class JPDecoder extends Transform {
|
|
9
|
+
export class JPDecoder extends Transform {
|
|
13
10
|
constructor(charset, maxBytes) {
|
|
14
11
|
super();
|
|
15
|
-
|
|
16
12
|
this.charset = charset;
|
|
17
13
|
this.chunks = [];
|
|
18
14
|
this.chunklen = 0;
|
|
19
|
-
|
|
20
15
|
// Upper bound for the buffered bytes, normalized the same way LimitedPassthrough
|
|
21
16
|
// normalizes its own. The whole-input buffering defeats a downstream maxBytes limiter
|
|
22
17
|
// (nothing is emitted until _flush), so without an internal bound a server could force
|
|
23
18
|
// unbounded memory use through a caller that asked for a limited download. Excess input
|
|
24
19
|
// is truncated, mirroring the truncation a maxBytes download applies anyway.
|
|
25
20
|
this.maxBytes = normalizeByteLimit(maxBytes);
|
|
26
|
-
|
|
27
21
|
// Also mirroring LimitedPassthrough: true once the bound is reached and every further
|
|
28
22
|
// chunk is being discarded. The download loop reads this to stop pulling from the
|
|
29
23
|
// server, which the limiter at the tail of the pipeline cannot tell it, because nothing
|
|
30
24
|
// is emitted from here until _flush().
|
|
31
25
|
this.limited = false;
|
|
32
26
|
}
|
|
33
|
-
|
|
34
27
|
// Buffer all incoming chunks (up to maxBytes); no decoding happens here because
|
|
35
28
|
// Japanese charsets require the complete input for accurate conversion.
|
|
36
29
|
_transform(chunk, encoding, done) {
|
|
37
30
|
if (typeof chunk === 'string') {
|
|
38
31
|
chunk = Buffer.from(chunk, encoding);
|
|
39
32
|
}
|
|
40
|
-
|
|
41
33
|
if (this.chunklen + chunk.length > this.maxBytes) {
|
|
42
34
|
chunk = chunk.slice(0, Math.max(0, this.maxBytes - this.chunklen));
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
if (chunk.length) {
|
|
46
37
|
this.chunks.push(chunk);
|
|
47
38
|
this.chunklen += chunk.length;
|
|
48
39
|
}
|
|
49
|
-
|
|
50
40
|
if (this.chunklen >= this.maxBytes) {
|
|
51
41
|
this.limited = true;
|
|
52
42
|
}
|
|
53
|
-
|
|
54
43
|
done();
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
// Perform the actual charset conversion once all input has been received.
|
|
58
46
|
// Uses the encoding-japanese library to convert from the source charset to Unicode.
|
|
59
47
|
// On failure (corrupt or unrecognizable data), passes through the raw bytes unchanged.
|
|
@@ -69,19 +57,16 @@ class JPDecoder extends Transform {
|
|
|
69
57
|
output = Buffer.from(output);
|
|
70
58
|
}
|
|
71
59
|
this.push(output);
|
|
72
|
-
}
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
73
62
|
// keep as is on errors
|
|
74
63
|
this.push(input);
|
|
75
64
|
}
|
|
76
|
-
|
|
77
65
|
done();
|
|
78
66
|
}
|
|
79
|
-
|
|
80
67
|
_destroy(err, callback) {
|
|
81
68
|
this.chunks = [];
|
|
82
69
|
this.chunklen = 0;
|
|
83
70
|
callback(err);
|
|
84
71
|
}
|
|
85
72
|
}
|
|
86
|
-
|
|
87
|
-
module.exports.JPDecoder = JPDecoder;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a byte budget for the download pipeline. Any finite positive number is honored and
|
|
4
|
+
* floored, because byte counts are integers: with a fractional bound a counter can only ever
|
|
5
|
+
* reach its floor, so a stage would never report itself full and a loop polling that flag would
|
|
6
|
+
* keep pulling forever. Anything else - 0, NaN, a non-numeric value - means "no limit".
|
|
7
|
+
*
|
|
8
|
+
* Lives here rather than in tools.ts because tools.ts imports jp-decoder.ts, which needs this.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The configured budget.
|
|
11
|
+
* @returns The normalized budget, or Infinity when unbounded.
|
|
12
|
+
*/
|
|
13
|
+
export declare const normalizeByteLimit: (value: unknown) => number;
|
|
14
|
+
export interface LimitedPassthroughOptions {
|
|
15
|
+
/** Maximum number of bytes to pass through. Anything else means "no limit" */
|
|
16
|
+
maxBytes?: number | undefined;
|
|
17
|
+
}
|
|
18
|
+
export declare class LimitedPassthrough extends Transform {
|
|
19
|
+
options: LimitedPassthroughOptions;
|
|
20
|
+
maxBytes: number;
|
|
21
|
+
processed: number;
|
|
22
|
+
limited: boolean;
|
|
23
|
+
constructor(options?: LimitedPassthroughOptions | undefined);
|
|
24
|
+
_transform(chunk: Buffer, encoding: BufferEncoding, done: TransformCallback): void;
|
|
25
|
+
}
|
|
@@ -1,62 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { Transform } = require('stream');
|
|
4
|
-
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
5
2
|
/**
|
|
6
3
|
* Normalizes a byte budget for the download pipeline. Any finite positive number is honored and
|
|
7
4
|
* floored, because byte counts are integers: with a fractional bound a counter can only ever
|
|
8
5
|
* reach its floor, so a stage would never report itself full and a loop polling that flag would
|
|
9
6
|
* keep pulling forever. Anything else - 0, NaN, a non-numeric value - means "no limit".
|
|
10
7
|
*
|
|
11
|
-
* Lives here rather than in tools.
|
|
8
|
+
* Lives here rather than in tools.ts because tools.ts imports jp-decoder.ts, which needs this.
|
|
12
9
|
*
|
|
13
|
-
* @param
|
|
14
|
-
* @returns
|
|
10
|
+
* @param value - The configured budget.
|
|
11
|
+
* @returns The normalized budget, or Infinity when unbounded.
|
|
15
12
|
*/
|
|
16
|
-
const normalizeByteLimit = value => {
|
|
13
|
+
export const normalizeByteLimit = (value) => {
|
|
17
14
|
let bytes = Number(value);
|
|
18
15
|
// Math.max keeps a sub-1 budget from flooring to 0, which would read back as "no limit"
|
|
19
16
|
return Number.isFinite(bytes) && bytes > 0 ? Math.max(Math.floor(bytes), 1) : Infinity;
|
|
20
17
|
};
|
|
21
|
-
|
|
22
18
|
// A Transform stream that passes through data up to a maximum byte limit,
|
|
23
19
|
// then silently discards all subsequent chunks. Used to enforce download
|
|
24
20
|
// size limits when fetching message content from the IMAP server.
|
|
25
|
-
class LimitedPassthrough extends Transform {
|
|
21
|
+
export class LimitedPassthrough extends Transform {
|
|
26
22
|
constructor(options) {
|
|
27
23
|
super();
|
|
28
24
|
this.options = options || {};
|
|
29
25
|
this.maxBytes = normalizeByteLimit(this.options.maxBytes);
|
|
30
26
|
this.processed = 0;
|
|
31
|
-
// Once set to true, all subsequent chunks are dropped without error
|
|
32
27
|
this.limited = false;
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
_transform(chunk, encoding, done) {
|
|
36
30
|
// If the limit was already reached, discard the chunk immediately
|
|
37
31
|
if (this.limited) {
|
|
38
32
|
return done();
|
|
39
33
|
}
|
|
40
|
-
|
|
41
34
|
const remainingBytes = this.maxBytes - this.processed;
|
|
42
35
|
if (remainingBytes < 1) {
|
|
43
36
|
return done();
|
|
44
37
|
}
|
|
45
|
-
|
|
46
38
|
// Slice the chunk to fit within the remaining byte budget
|
|
47
39
|
if (chunk.length > remainingBytes) {
|
|
48
|
-
chunk = chunk.
|
|
40
|
+
chunk = chunk.subarray(0, remainingBytes);
|
|
49
41
|
}
|
|
50
|
-
|
|
51
42
|
this.processed += chunk.length;
|
|
52
43
|
if (this.processed >= this.maxBytes) {
|
|
53
44
|
this.limited = true;
|
|
54
45
|
}
|
|
55
|
-
|
|
56
46
|
this.push(chunk);
|
|
57
47
|
done();
|
|
58
48
|
}
|
|
59
49
|
}
|
|
60
|
-
|
|
61
|
-
module.exports.LimitedPassthrough = LimitedPassthrough;
|
|
62
|
-
module.exports.normalizeByteLimit = normalizeByteLimit;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import { ConnectionDeadline } from './connection-deadline.js';
|
|
3
|
+
import type { InternalLogger } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A socket handed out by the proxy helpers, carrying the early error handler installed by
|
|
6
|
+
* attachEarlyErrorHandler() until the caller takes ownership
|
|
7
|
+
*/
|
|
8
|
+
export type ProxySocket = net.Socket & {
|
|
9
|
+
_earlyErrorHandler?: ((err: Error) => void) | null | undefined;
|
|
10
|
+
};
|
|
11
|
+
export interface ProxyConnectionOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Shared connection deadline. Proxy DNS and negotiation run inside it, so a stalled proxy
|
|
14
|
+
* cannot exceed the configured connectionTimeout.
|
|
15
|
+
*/
|
|
16
|
+
deadline?: ConnectionDeadline | undefined;
|
|
17
|
+
/** Used to build a deadline when none was passed */
|
|
18
|
+
connectionTimeout?: number | undefined;
|
|
19
|
+
}
|
|
20
|
+
declare const detachEarlyErrorHandler: (socket: ProxySocket | false | null | undefined) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Opens a socket to `host`:`port` through the configured proxy.
|
|
23
|
+
*
|
|
24
|
+
* @param logger Logger instance.
|
|
25
|
+
* @param connectionUrl Proxy URL (http, https, socks, socks4, socks4a, socks5).
|
|
26
|
+
* @param host Destination host, passed through unresolved wherever the proxy protocol
|
|
27
|
+
* can resolve it itself.
|
|
28
|
+
* @param port Destination port.
|
|
29
|
+
* @param options Deadline options, see ProxyConnectionOptions.
|
|
30
|
+
* @returns The tunnelled socket, or undefined for an unknown protocol.
|
|
31
|
+
*/
|
|
32
|
+
declare const proxyConnection: (logger: InternalLogger, connectionUrl: string, host: string, port: number, options?: ProxyConnectionOptions | undefined) => Promise<ProxySocket | undefined>;
|
|
33
|
+
export { proxyConnection, detachEarlyErrorHandler };
|