imapflow 2.3.0 → 2.4.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/commands/append.d.ts +1 -1
  3. package/dist/cjs/commands/append.js +15 -4
  4. package/dist/cjs/commands/compress.js +2 -2
  5. package/dist/cjs/commands/idle.js +2 -3
  6. package/dist/cjs/commands/status.js +2 -4
  7. package/dist/cjs/connection-deadline.d.ts +1 -1
  8. package/dist/cjs/connection-deadline.js +2 -4
  9. package/dist/cjs/download.js +22 -19
  10. package/dist/cjs/errors.d.ts +15 -0
  11. package/dist/cjs/errors.js +22 -0
  12. package/dist/cjs/handler/imap-compiler.d.ts +4 -4
  13. package/dist/cjs/handler/imap-compiler.js +18 -18
  14. package/dist/cjs/handler/imap-parser.js +28 -3
  15. package/dist/cjs/handler/imap-stream.js +9 -8
  16. package/dist/cjs/handler/limits.d.ts +1 -1
  17. package/dist/cjs/handler/limits.js +5 -7
  18. package/dist/cjs/handler/parser-instance.js +23 -38
  19. package/dist/cjs/handler/token-parser.d.ts +12 -0
  20. package/dist/cjs/handler/token-parser.js +44 -104
  21. package/dist/cjs/imap-flow.d.ts +4 -3
  22. package/dist/cjs/imap-flow.js +72 -72
  23. package/dist/cjs/package-info.d.ts +1 -1
  24. package/dist/cjs/package-info.js +1 -1
  25. package/dist/cjs/proxy-connection.js +2 -5
  26. package/dist/cjs/search-compiler.js +2 -3
  27. package/dist/cjs/tools.d.ts +2 -2
  28. package/dist/cjs/tools.js +8 -11
  29. package/dist/esm/commands/append.d.ts +1 -1
  30. package/dist/esm/commands/append.js +15 -4
  31. package/dist/esm/commands/compress.js +2 -2
  32. package/dist/esm/commands/idle.js +2 -3
  33. package/dist/esm/commands/status.js +2 -4
  34. package/dist/esm/connection-deadline.d.ts +1 -1
  35. package/dist/esm/connection-deadline.js +2 -4
  36. package/dist/esm/download.js +22 -19
  37. package/dist/esm/errors.d.ts +15 -0
  38. package/dist/esm/errors.js +21 -0
  39. package/dist/esm/handler/imap-compiler.d.ts +4 -4
  40. package/dist/esm/handler/imap-compiler.js +18 -18
  41. package/dist/esm/handler/imap-parser.js +28 -3
  42. package/dist/esm/handler/imap-stream.js +9 -8
  43. package/dist/esm/handler/limits.d.ts +1 -1
  44. package/dist/esm/handler/limits.js +5 -7
  45. package/dist/esm/handler/parser-instance.js +23 -38
  46. package/dist/esm/handler/token-parser.d.ts +12 -0
  47. package/dist/esm/handler/token-parser.js +44 -104
  48. package/dist/esm/imap-flow.d.ts +4 -3
  49. package/dist/esm/imap-flow.js +73 -73
  50. package/dist/esm/package-info.d.ts +1 -1
  51. package/dist/esm/package-info.js +1 -1
  52. package/dist/esm/proxy-connection.js +2 -5
  53. package/dist/esm/search-compiler.js +2 -3
  54. package/dist/esm/tools.d.ts +2 -2
  55. package/dist/esm/tools.js +9 -12
  56. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.4.0](https://github.com/postalsys/imapflow/compare/v2.3.0...v2.4.0) (2026-10-10)
4
+
5
+
6
+ ### Features
7
+
8
+ * re-raise unexpected parser exceptions as coded ParserErrorInternal errors ([dda3f7e](https://github.com/postalsys/imapflow/commit/dda3f7e71e3c227479763a8c6af617fd4f57679a))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * keep append literals in sync with their data and fail downloads cleanly ([752248e](https://github.com/postalsys/imapflow/commit/752248e07fd8930cb0943287c32e3bab88484d80))
14
+
3
15
  ## [2.3.0](https://github.com/postalsys/imapflow/compare/v2.2.11...v2.3.0) (2026-10-08)
4
16
 
5
17
 
@@ -19,4 +19,4 @@ export interface AppendResult extends AppendResponseObject {
19
19
  * @returns Append result with UID info if available, or undefined if preconditions not met
20
20
  * @throws {Error} If the APPEND command fails or message exceeds APPENDLIMIT
21
21
  */
22
- export default function append(connection: ImapFlow, destination: string | string[], content: Buffer | string, flags?: string | string[] | undefined, idate?: Date | string | false | undefined): Promise<AppendResult | undefined>;
22
+ export default function append(connection: ImapFlow, destination: string | string[], content: Buffer | Uint8Array | string, flags?: string | string[] | undefined, idate?: Date | string | false | undefined): Promise<AppendResult | undefined>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = append;
4
4
  const tools_js_1 = require("../tools.js");
5
+ const errors_js_1 = require("../errors.js");
5
6
  /**
6
7
  * Appends a message to a mailbox.
7
8
  *
@@ -18,14 +19,24 @@ async function append(connection, destination, content, flags, idate) {
18
19
  // nothing to do here
19
20
  return;
20
21
  }
22
+ // The literal size marker is announced from these bytes, so anything that is not
23
+ // bytes (a Readable, a plain object) is refused here instead of desyncing the session
24
+ let message;
21
25
  if (typeof content === 'string') {
22
- content = Buffer.from(content);
26
+ message = Buffer.from(content);
27
+ }
28
+ else if (content instanceof Uint8Array) {
29
+ // a Buffer as is, any other Uint8Array as a view over the same memory, not a copy
30
+ message = Buffer.isBuffer(content) ? content : Buffer.from(content.buffer, content.byteOffset, content.byteLength);
31
+ }
32
+ else {
33
+ throw (0, errors_js_1.createImapError)('Message content must be a Buffer, Uint8Array or string', 'InvalidMessageContent');
23
34
  }
24
35
  // APPENDLIMIT capability (RFC 7889): server may advertise the maximum message
25
36
  // size it accepts. Check before sending to avoid a wasted round-trip.
26
37
  if (connection.capabilities.has('APPENDLIMIT')) {
27
38
  let appendLimit = connection.capabilities.get('APPENDLIMIT');
28
- if (typeof appendLimit === 'number' && appendLimit < content.length) {
39
+ if (typeof appendLimit === 'number' && appendLimit < message.length) {
29
40
  let err = new Error('Message content too big for APPENDLIMIT=' + appendLimit);
30
41
  err.serverResponseCode = 'APPENDLIMIT';
31
42
  throw err;
@@ -82,9 +93,9 @@ async function append(connection, destination, content, flags, idate) {
82
93
  // Regular literals cannot contain NUL bytes per the IMAP grammar.
83
94
  let isLiteral8 = false;
84
95
  if (connection.capabilities.has('BINARY') && !connection.disableBinary) {
85
- isLiteral8 = content.indexOf(Buffer.from([0])) >= 0;
96
+ isLiteral8 = message.indexOf(0) >= 0;
86
97
  }
87
- attributes.push({ type: 'LITERAL', value: content, isLiteral8 });
98
+ attributes.push({ type: 'LITERAL', value: message, isLiteral8 });
88
99
  let map = { destination };
89
100
  if (connection.mailbox && connection.mailbox.path) {
90
101
  map.path = connection.mailbox.path;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = compress;
4
+ const errors_js_1 = require("../errors.js");
4
5
  /**
5
6
  * Requests DEFLATE compression from the server.
6
7
  *
@@ -36,8 +37,7 @@ async function compress(connection) {
36
37
  // back its unconsumed tail on unpipe so the transport can feed it into the
37
38
  // inflater - not something a command module can reach from here.
38
39
  if (response.hasTrailingData) {
39
- let error = new Error('Server sent data between the COMPRESS response and the compression layer switch');
40
- error.code = 'COMPRESS_TRAILING_DATA';
40
+ let error = (0, errors_js_1.createImapError)('Server sent data between the COMPRESS response and the compression layer switch', 'COMPRESS_TRAILING_DATA');
41
41
  connection.log.error({ err: error, cid: connection.id });
42
42
  // Schedule the close before releasing parser backpressure, so the buffered
43
43
  // deflate-framed bytes cannot settle anything before teardown begins. This is
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = idle;
4
4
  const tools_js_1 = require("../tools.js");
5
+ const errors_js_1 = require("../errors.js");
5
6
  const NOOP_INTERVAL = 2 * 60 * 1000;
6
7
  /**
7
8
  * Marks the connection as idling on behalf of one session and returns a release function.
@@ -192,9 +193,7 @@ async function pollOnce(connection, session) {
192
193
  highestModseq: true
193
194
  });
194
195
  if (!status) {
195
- let err = new Error('STATUS poll failed');
196
- err.code = 'PollFailed';
197
- throw err;
196
+ throw (0, errors_js_1.createImapError)('STATUS poll failed', 'PollFailed');
198
197
  }
199
198
  break;
200
199
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = status;
4
4
  const tools_js_1 = require("../tools.js");
5
5
  const status_fields_js_1 = require("./status-fields.js");
6
+ const errors_js_1 = require("../errors.js");
6
7
  // STATUS fields that also refresh the live mailbox state when the queried mailbox is the
7
8
  // currently selected one. Keyed by the output property name parseStatusList() reports.
8
9
  const MAILBOX_UPDATERS = {
@@ -99,10 +100,7 @@ async function status(connection, path, query) {
99
100
  return false;
100
101
  });
101
102
  if (folders && !folders.length) {
102
- let error = new Error(`Mailbox doesn't exist: ${path}`);
103
- error.code = 'NotFound';
104
- error.response = err;
105
- throw error;
103
+ throw (0, errors_js_1.createImapError)(`Mailbox doesn't exist: ${path}`, 'NotFound', { response: err });
106
104
  }
107
105
  }
108
106
  connection.log.warn({ err, cid: connection.id });
@@ -1,4 +1,4 @@
1
- import type { ImapFlowError } from './errors.js';
1
+ import { type ImapFlowError } from './errors.js';
2
2
  export declare const CONNECT_TIMEOUT: number;
3
3
  /**
4
4
  * One deadline for an entire connection attempt.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConnectionDeadline = exports.CONNECT_TIMEOUT = void 0;
4
4
  const tools_js_1 = require("./tools.js");
5
+ const errors_js_1 = require("./errors.js");
5
6
  // Default upper bound for establishing a usable transport, including DNS and proxy negotiation.
6
7
  exports.CONNECT_TIMEOUT = 90 * 1000;
7
8
  /**
@@ -31,10 +32,7 @@ class ConnectionDeadline {
31
32
  * @returns The shared `CONNECT_TIMEOUT` error.
32
33
  */
33
34
  error() {
34
- let err = new Error('Failed to establish connection in required time');
35
- err.code = 'CONNECT_TIMEOUT';
36
- err.details = { connectionTimeout: this.timeout };
37
- return err;
35
+ return (0, errors_js_1.createImapError)('Failed to establish connection in required time', 'CONNECT_TIMEOUT', { details: { connectionTimeout: this.timeout } });
38
36
  }
39
37
  /**
40
38
  * Maps a dependency's own expiry onto the shared `CONNECT_TIMEOUT` shape, so callers see one
@@ -18,6 +18,7 @@ const limited_passthrough_js_1 = require("./limited-passthrough.js");
18
18
  // What a wait on the head stream listens for: it can take more input, it failed, or it went
19
19
  // away (a consumer destroying it closes it without a 'drain')
20
20
  const DRAIN_WAIT_EVENTS = ['drain', 'error', 'close'];
21
+ const errors_js_1 = require("./errors.js");
21
22
  const tools_js_1 = require("./tools.js");
22
23
  const isEmptySection = (value) => !value?.length;
23
24
  /**
@@ -88,10 +89,7 @@ async function fetchExpected(client, range, query, options, expected) {
88
89
  cid: client.id
89
90
  });
90
91
  if (attempt >= MAX_FETCH_ATTEMPTS) {
91
- let err = new Error('Server kept answering with data of another request');
92
- err.code = 'DownloadIncomplete';
93
- err.cid = client.id;
94
- throw err;
92
+ throw (0, errors_js_1.createImapError)('Server kept answering with data of another request', 'DownloadIncomplete', { cid: client.id });
95
93
  }
96
94
  }
97
95
  }
@@ -238,6 +236,12 @@ async function downloadMessage(client, range, part, options) {
238
236
  };
239
237
  let response = await fetchExpected(client, range, query, downloadOptions, expected);
240
238
  if (!response) {
239
+ // An empty answer also comes from a connection that closed while the consumer was
240
+ // paused between chunks (fetchOne() finds no mailbox then), and that is not a
241
+ // missing message: a caller that retries would skip it
242
+ if (!client.usable) {
243
+ throw client.createNoConnectionError(false, { rejectedFrom: 'download' });
244
+ }
241
245
  return { response: false, chunk: false };
242
246
  }
243
247
  if (!uid && response.uid) {
@@ -374,13 +378,19 @@ async function downloadMessage(client, range, part, options) {
374
378
  // would still pull the entire part off the wire.
375
379
  let limiters = [];
376
380
  let isLimited = () => limiters.some(entry => entry.limited);
381
+ // Every stage after the head, so a download that ends early can release all of them
382
+ let stages = [];
377
383
  // Appending a stage means forwarding the current tail's errors to it before piping, so a
378
- // failure anywhere reaches the stream the caller is reading
384
+ // failure anywhere reaches the stream the caller is reading. The stage is destroyed with
385
+ // the error rather than only emitting it: pipe() does not end its destination on a source
386
+ // error, so a consumer that only listens for 'finish' or 'close' (a plain .pipe() into a
387
+ // file) would otherwise wait forever.
379
388
  let pipeStage = (stage) => {
380
389
  output.on('error', err => {
381
- stage.emit('error', err);
390
+ stage.destroy(err);
382
391
  });
383
392
  output = output.pipe(stage);
393
+ stages.push(stage);
384
394
  return stage;
385
395
  };
386
396
  let isTextNode = ['text/html', 'text/plain', 'text/x-amp-html'].includes(meta.contentType) || (part === '1' && !meta.contentType);
@@ -425,8 +435,8 @@ async function downloadMessage(client, range, part, options) {
425
435
  // Cleanup function
426
436
  const cleanup = () => {
427
437
  fetchAborted = true;
428
- if (stream && !stream.destroyed) {
429
- stream.destroy();
438
+ for (let entry of [stream, ...stages]) {
439
+ entry.destroy();
430
440
  }
431
441
  };
432
442
  // Listen for stream destruction
@@ -499,11 +509,7 @@ async function downloadMessage(client, range, part, options) {
499
509
  // Loud on purpose. Everything written downstream by this point holds
500
510
  // duplicated content, and a quiet stop is indistinguishable from a clean EOF,
501
511
  // so the consumer would store a corrupt body believing it intact.
502
- let err = new Error('Download exceeded the expected message size');
503
- err.code = 'DownloadOverflow';
504
- err.maxSize = maxTotalBytes;
505
- err.cid = client.id;
506
- throw err;
512
+ throw (0, errors_js_1.createImapError)('Download exceeded the expected message size', 'DownloadOverflow', { maxSize: maxTotalBytes, cid: client.id });
507
513
  }
508
514
  let { response, chunk } = await getNextPart();
509
515
  // A consumer that gave up while the chunk was in flight
@@ -514,10 +520,7 @@ async function downloadMessage(client, range, part, options) {
514
520
  // The message is gone mid-download (expunged by another client, or the
515
521
  // mailbox was closed). Ending the stream here would pass the truncated body
516
522
  // off as complete, so the consumer is told the same way as for an overflow.
517
- let err = new Error('Message disappeared before the download completed');
518
- err.code = 'DownloadIncomplete';
519
- err.cid = client.id;
520
- throw err;
523
+ throw (0, errors_js_1.createImapError)('Message disappeared before the download completed', 'DownloadIncomplete', { cid: client.id });
521
524
  }
522
525
  if (!chunk) {
523
526
  break;
@@ -547,7 +550,7 @@ async function downloadMessage(client, range, part, options) {
547
550
  fetchAllParts(head)
548
551
  .catch(err => {
549
552
  if (!fetchAborted && stream && !stream.destroyed) {
550
- stream.emit('error', err);
553
+ stream.destroy(err);
551
554
  /* c8 ignore start */ // the else logs when a fetch error arrives after the stream was already torn down (timing-dependent)
552
555
  }
553
556
  else {
@@ -572,7 +575,7 @@ async function downloadMessage(client, range, part, options) {
572
575
  // above rejects a promise nobody holds and takes the process down on
573
576
  // unhandledRejection. Reaching it always means an invariant broke - the head
574
577
  // stream kept pipeStage()'s error forwarder for the life of the download, so
575
- // emit('error') above has somewhere to go - which is why it logs at error even
578
+ // destroy(err) above has somewhere to go - which is why it logs at error even
576
579
  // for a routine-looking connection code.
577
580
  .catch(err => client.log.error({ msg: 'Failed to fail the download stream', err, cid: client.id }));
578
581
  };
@@ -29,6 +29,7 @@ export declare const ImapFlowErrorCode: {
29
29
  readonly MissingServerExtension: "MissingServerExtension";
30
30
  readonly ParserError: "ParserError";
31
31
  readonly ParserErrorExchange: "ParserErrorExchange";
32
+ readonly ParserErrorInternal: "ParserErrorInternal";
32
33
  readonly MAX_IMAP_NESTING_REACHED: "MAX_IMAP_NESTING_REACHED";
33
34
  readonly LineTooLarge: "LineTooLarge";
34
35
  readonly LiteralTooLarge: "LiteralTooLarge";
@@ -38,6 +39,7 @@ export declare const ImapFlowErrorCode: {
38
39
  readonly InvalidTextValue: "InvalidTextValue";
39
40
  readonly InvalidSequenceSet: "InvalidSequenceSet";
40
41
  readonly InvalidSearchQuery: "InvalidSearchQuery";
42
+ readonly InvalidMessageContent: "InvalidMessageContent";
41
43
  readonly DownloadOverflow: "DownloadOverflow";
42
44
  readonly DownloadIncomplete: "DownloadIncomplete";
43
45
  readonly ProxyError: "ProxyError";
@@ -48,6 +50,8 @@ export declare const ImapFlowErrorCode: {
48
50
  };
49
51
  /** One of the {@link ImapFlowErrorCode} values */
50
52
  export type ImapFlowErrorCode = (typeof ImapFlowErrorCode)[keyof typeof ImapFlowErrorCode];
53
+ /** A numbered parser error code, `ParserError1` and up */
54
+ export type ParserErrorCode = `ParserError${number}`;
51
55
  /**
52
56
  * An Error raised by ImapFlow, with the extra properties the library attaches to describe
53
57
  * the failure. Every property is optional: which ones are present depends on where the
@@ -132,3 +136,14 @@ export declare class AuthenticationFailure extends Error implements ImapFlowErro
132
136
  response?: string | undefined;
133
137
  oauthError?: any;
134
138
  }
139
+ /**
140
+ * Creates an {@link ImapFlowError} with a code and optional extra properties. The code is
141
+ * checked by the compiler against {@link ImapFlowErrorCode}, so a typo or an undeclared
142
+ * code fails the build instead of reaching consumers.
143
+ *
144
+ * @param message - The error message
145
+ * @param code - The error code
146
+ * @param props - Extra properties to set on the error
147
+ * @returns The error, ready to throw or reject with
148
+ */
149
+ export declare function createImapError(message: string, code: ImapFlowErrorCode | ParserErrorCode, props?: Omit<ImapFlowError, 'name' | 'message' | 'stack' | 'code'> | undefined): ImapFlowError;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthenticationFailure = exports.ImapFlowErrorCode = void 0;
4
+ exports.createImapError = createImapError;
4
5
  /**
5
6
  * The `code` values ImapFlow sets on the errors it raises, so they can be matched without
6
7
  * string literals: `if (err.code === ImapFlowErrorCode.NoConnection)`.
@@ -35,6 +36,8 @@ exports.ImapFlowErrorCode = {
35
36
  // response parsing and size limits
36
37
  ParserError: 'ParserError',
37
38
  ParserErrorExchange: 'ParserErrorExchange',
39
+ // an unexpected exception inside the parser, re-raised with a code; a parser bug
40
+ ParserErrorInternal: 'ParserErrorInternal',
38
41
  MAX_IMAP_NESTING_REACHED: 'MAX_IMAP_NESTING_REACHED',
39
42
  LineTooLarge: 'LineTooLarge',
40
43
  LiteralTooLarge: 'LiteralTooLarge',
@@ -45,6 +48,7 @@ exports.ImapFlowErrorCode = {
45
48
  InvalidTextValue: 'InvalidTextValue',
46
49
  InvalidSequenceSet: 'InvalidSequenceSet',
47
50
  InvalidSearchQuery: 'InvalidSearchQuery',
51
+ InvalidMessageContent: 'InvalidMessageContent',
48
52
  // download()
49
53
  DownloadOverflow: 'DownloadOverflow',
50
54
  DownloadIncomplete: 'DownloadIncomplete',
@@ -66,3 +70,21 @@ class AuthenticationFailure extends Error {
66
70
  }
67
71
  }
68
72
  exports.AuthenticationFailure = AuthenticationFailure;
73
+ /**
74
+ * Creates an {@link ImapFlowError} with a code and optional extra properties. The code is
75
+ * checked by the compiler against {@link ImapFlowErrorCode}, so a typo or an undeclared
76
+ * code fails the build instead of reaching consumers.
77
+ *
78
+ * @param message - The error message
79
+ * @param code - The error code
80
+ * @param props - Extra properties to set on the error
81
+ * @returns The error, ready to throw or reject with
82
+ */
83
+ function createImapError(message, code, props) {
84
+ const error = new Error(message);
85
+ error.code = code;
86
+ if (props) {
87
+ Object.assign(error, props);
88
+ }
89
+ return error;
90
+ }
@@ -12,13 +12,13 @@ import type { CompilerOptions, ImapCompileInput } from './types.js';
12
12
  * @param options.isLogging - If true, redacts sensitive values and truncates long strings/literals for logging purposes.
13
13
  * @param options.literalPlus - If true, uses the LITERAL+ extension (appends "+" to literal length markers).
14
14
  * @param options.literalMinus - If true, uses the LITERAL- extension for literals up to 4096 bytes.
15
- * @returns A promise that resolves to an array of Buffers (if asArray is true) or a single concatenated Buffer.
15
+ * @returns An array of Buffers (if asArray is true) or a single concatenated Buffer.
16
16
  */
17
17
  declare function compiler(response: ImapCompileInput, options: CompilerOptions & {
18
18
  asArray: true;
19
- }): Promise<Buffer[]>;
19
+ }): Buffer[];
20
20
  declare function compiler(response: ImapCompileInput, options?: (CompilerOptions & {
21
21
  asArray?: false | undefined;
22
- }) | undefined): Promise<Buffer>;
23
- declare function compiler(response: ImapCompileInput, options?: CompilerOptions | undefined): Promise<Buffer | Buffer[]>;
22
+ }) | undefined): Buffer;
23
+ declare function compiler(response: ImapCompileInput, options?: CompilerOptions | undefined): Buffer | Buffer[];
24
24
  export default compiler;
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const imap_formal_syntax_js_1 = __importDefault(require("./imap-formal-syntax.js"));
8
+ const errors_js_1 = require("../errors.js");
8
9
  // A single element of a sequence-set as defined by the RFC 9051 grammar: a number
9
10
  // or a range, where "*" stands for the largest number in use. Digit strings are not
10
11
  // range-checked here (a server rejects "0" or an overlong number on its own); the
@@ -46,13 +47,11 @@ const CRLF = /[\r\n]/;
46
47
  */
47
48
  const quoteString = (value) => {
48
49
  if (NOT_QUOTABLE.test(value)) {
49
- let error = new Error('Unquotable character in IMAP string value');
50
- error.code = 'InvalidStringValue';
51
- throw error;
50
+ throw (0, errors_js_1.createImapError)('Unquotable character in IMAP string value', 'InvalidStringValue');
52
51
  }
53
52
  return '"' + value.replace(/["\\]/g, char => '\\' + char) + '"';
54
53
  };
55
- async function compiler(response, options) {
54
+ function compiler(response, options) {
56
55
  let { asArray, isLogging, literalPlus, literalMinus } = options || {};
57
56
  const respParts = [];
58
57
  // Formats an entry (string, number or Buffer) into the Buffer that is written to
@@ -67,9 +66,7 @@ async function compiler(response, options) {
67
66
  const emitEntry = (entry, opts) => {
68
67
  let { returnEmpty, raw } = opts || {};
69
68
  if (!raw && !isLogging && (typeof entry === 'string' || Buffer.isBuffer(entry)) && CRLF.test(entry.toString('latin1'))) {
70
- let error = new Error('Line terminator in IMAP token');
71
- error.code = 'InvalidTokenValue';
72
- throw error;
69
+ throw (0, errors_js_1.createImapError)('Line terminator in IMAP token', 'InvalidTokenValue');
73
70
  }
74
71
  if (typeof entry === 'string') {
75
72
  return Buffer.from(entry);
@@ -93,7 +90,7 @@ async function compiler(response, options) {
93
90
  // Set right after the compiler writes "(" or "[" itself, so the first element inside gets no
94
91
  // leading space
95
92
  let afterOpener = false;
96
- let walk = async (node, options) => {
93
+ let walk = (node, options) => {
97
94
  options = options || {};
98
95
  // Add a space separator when:
99
96
  // - The previous token was a LITERAL. Literal data ends exactly at its declared length, so
@@ -125,7 +122,7 @@ async function compiler(response, options) {
125
122
  if (subArray && !Array.isArray(child)) {
126
123
  subArray = false;
127
124
  }
128
- await walk(child, { subArray });
125
+ walk(child, { subArray });
129
126
  }
130
127
  resp.push(emitEntry(')'));
131
128
  afterOpener = false;
@@ -159,6 +156,11 @@ async function compiler(response, options) {
159
156
  resp.push(emitEntry(logPlaceholder(node.value.length, 'literal')));
160
157
  }
161
158
  else {
159
+ // The size marker must match the bytes emitEntry() writes, and it writes
160
+ // nothing for a value that is not a Buffer or a string
161
+ if (node.value && !Buffer.isBuffer(node.value) && typeof node.value !== 'string') {
162
+ throw (0, errors_js_1.createImapError)('Literal value must be a Buffer or a string', 'InvalidTokenValue');
163
+ }
162
164
  // The literal size marker counts octets - string values are written as
163
165
  // UTF-8, so their UTF-16 .length would undercount multi-byte characters
164
166
  let literalLength = !node.value ? 0 : Buffer.isBuffer(node.value) ? node.value.length : Buffer.byteLength(node.value.toString());
@@ -213,9 +215,7 @@ async function compiler(response, options) {
213
215
  if (!isLogging) {
214
216
  val = node.value === null || node.value === undefined ? '' : node.value.toString();
215
217
  if (!isValidSequenceSet(val)) {
216
- let error = new Error('Invalid sequence set value');
217
- error.code = 'InvalidSequenceSet';
218
- throw error;
218
+ throw (0, errors_js_1.createImapError)('Invalid sequence set value', 'InvalidSequenceSet');
219
219
  }
220
220
  resp.push(emitEntry(val, { raw: true }));
221
221
  }
@@ -230,9 +230,7 @@ async function compiler(response, options) {
230
230
  // this check runs first only to raise the more specific InvalidTextValue code.
231
231
  if (node.value) {
232
232
  if (!isLogging && CRLF.test(node.value.toString())) {
233
- let error = new Error('Line terminator in IMAP text value');
234
- error.code = 'InvalidTextValue';
235
- throw error;
233
+ throw (0, errors_js_1.createImapError)('Line terminator in IMAP text value', 'InvalidTextValue');
236
234
  }
237
235
  resp.push(emitEntry(node.value));
238
236
  }
@@ -267,7 +265,7 @@ async function compiler(response, options) {
267
265
  resp.push(emitEntry('['));
268
266
  afterOpener = true;
269
267
  for (let child of node.section) {
270
- await walk(child);
268
+ walk(child);
271
269
  }
272
270
  resp.push(emitEntry(']'));
273
271
  afterOpener = false;
@@ -286,13 +284,15 @@ async function compiler(response, options) {
286
284
  if (response.attributes) {
287
285
  let attributes = Array.isArray(response.attributes) ? response.attributes : [].concat(response.attributes);
288
286
  for (let child of attributes) {
289
- await walk(child);
287
+ walk(child);
290
288
  }
291
289
  }
292
290
  if (resp.length) {
293
291
  respParts.push(resp);
294
292
  }
295
- const compiled = respParts.map(part => Buffer.concat(part));
293
+ // A part with a single entry (the data of a synchronizing literal) is used as is: copying a
294
+ // large message only to send it doubles its memory
295
+ const compiled = respParts.map(part => (part.length === 1 ? part[0] : Buffer.concat(part)));
296
296
  // without asArray there is a single part, returned as is instead of copied
297
297
  return asArray ? compiled : compiled.length === 1 ? compiled[0] : Buffer.concat(compiled);
298
298
  }
@@ -6,6 +6,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.default = parser;
7
7
  const imap_formal_syntax_js_1 = __importDefault(require("./imap-formal-syntax.js"));
8
8
  const parser_instance_js_1 = require("./parser-instance.js");
9
+ const errors_js_1 = require("../errors.js");
10
+ const limits_js_1 = require("./limits.js");
11
+ // The codes the parser raises on purpose: the numbered ParserErrorN codes, ParserErrorExchange,
12
+ // the nesting limit and the inline literal size limit
13
+ const PARSER_ERROR_CODES = /^(?:ParserError\d+|ParserErrorExchange|MAX_IMAP_NESTING_REACHED|LiteralTooLarge)$/;
14
+ /**
15
+ * Anything the parser throws other than one of its own coded errors - a TypeError from a parser
16
+ * bug, a RangeError from runaway recursion or a bad Buffer offset (which carries a Node code), a
17
+ * thrown non-Error - is re-raised as a coded `ParserErrorInternal`, so callers that match on
18
+ * codes still handle it and settle the command the line belonged to. The fuzz suite treats this
19
+ * code as a failure, so the guard does not hide parser bugs from the tests.
20
+ *
21
+ * @param err - The thrown value
22
+ * @param input - The line that was being parsed
23
+ * @returns The error to rethrow
24
+ */
25
+ function asParserError(err, input) {
26
+ if (err instanceof Error && PARSER_ERROR_CODES.test(err.code || '')) {
27
+ return err;
28
+ }
29
+ return (0, errors_js_1.createImapError)(`Unexpected parser failure: ${err instanceof Error ? err.message : String(err)}`, 'ParserErrorInternal', {
30
+ parserContext: (0, limits_js_1.boundedInput)(input.toString()),
31
+ ...(err instanceof Error ? { _err: err } : {})
32
+ });
33
+ }
9
34
  /**
10
35
  * Parses a raw IMAP command or response buffer into a structured object.
11
36
  * Handles edge cases such as null-byte-padded responses from buggy servers and
@@ -26,7 +51,7 @@ async function parser(command, options) {
26
51
  // Workaround for buggy IMAP servers that pad responses with leading NUL (\x00) bytes.
27
52
  // Some servers (observed in the wild) prepend null bytes to their output, which would
28
53
  // cause parsing to fail. We strip them and note how many were removed for diagnostics.
29
- if (command[0] === 0) {
54
+ if (Buffer.isBuffer(command) && command[0] === 0) {
30
55
  // find the first non null byte and trim
31
56
  let firstNonNull = -1;
32
57
  for (let i = 0; i < command.length; i++) {
@@ -39,7 +64,7 @@ async function parser(command, options) {
39
64
  // All bytes are null, treat as a BAD response
40
65
  return { tag: '*', command: 'BAD', attributes: [] };
41
66
  }
42
- command = command.slice(firstNonNull);
67
+ command = command.subarray(firstNonNull);
43
68
  nullBytesRemoved = firstNonNull;
44
69
  }
45
70
  const parserInstance = new parser_instance_js_1.ParserInstance(command, options);
@@ -71,7 +96,7 @@ async function parser(command, options) {
71
96
  }
72
97
  }
73
98
  catch (err) {
74
- let error = err;
99
+ let error = asParserError(err, command);
75
100
  if (error.code === 'ParserErrorExchange' && error.parserContext && error.parserContext.value) {
76
101
  return error.parserContext.value;
77
102
  }
@@ -4,6 +4,7 @@ exports.ImapStream = void 0;
4
4
  const node_stream_1 = require("node:stream");
5
5
  const logger_js_1 = require("../logger.js");
6
6
  const limits_js_1 = require("./limits.js");
7
+ const errors_js_1 = require("../errors.js");
7
8
  const LINE = 0x01;
8
9
  const LITERAL = 0x02;
9
10
  const LF = 0x0a;
@@ -180,10 +181,10 @@ class ImapStream extends node_stream_1.Transform {
180
181
  if (lineLength <= this.maxLineLength) {
181
182
  return true;
182
183
  }
183
- const err = new Error(`Line length ${lineLength} exceeds maximum allowed size of ${this.maxLineLength} bytes`);
184
- err.code = 'LineTooLarge';
185
- err.lineLength = lineLength;
186
- err.maxSize = this.maxLineLength;
184
+ const err = (0, errors_js_1.createImapError)(`Line length ${lineLength} exceeds maximum allowed size of ${this.maxLineLength} bytes`, 'LineTooLarge', {
185
+ lineLength,
186
+ maxSize: this.maxLineLength
187
+ });
187
188
  return this.failStream(err);
188
189
  }
189
190
  /**
@@ -206,10 +207,10 @@ class ImapStream extends node_stream_1.Transform {
206
207
  }
207
208
  return true;
208
209
  }
209
- const err = new Error(`Response size ${total} exceeds maximum allowed size of ${this.maxResponseSize} bytes`);
210
- err.code = 'ResponseTooLarge';
211
- err.responseSize = total;
212
- err.maxSize = this.maxResponseSize;
210
+ const err = (0, errors_js_1.createImapError)(`Response size ${total} exceeds maximum allowed size of ${this.maxResponseSize} bytes`, 'ResponseTooLarge', {
211
+ responseSize: total,
212
+ maxSize: this.maxResponseSize
213
+ });
213
214
  return this.failStream(err);
214
215
  }
215
216
  /**
@@ -1,4 +1,4 @@
1
- import type { ImapFlowError } from '../errors.js';
1
+ import { type ImapFlowError } from '../errors.js';
2
2
  export declare const MAX_LITERAL_SIZE: number;
3
3
  export declare const MAX_LINE_SIZE: number;
4
4
  export declare const MAX_RESPONSE_SIZE: number;
@@ -4,6 +4,7 @@
4
4
  // defaults in the ImapFlowOptions type describe both paths.
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createLiteralTooLargeError = exports.normalizeLimit = exports.boundedInput = exports.ERROR_CONTEXT_LENGTH = exports.MAX_RESPONSE_SIZE = exports.MAX_LINE_SIZE = exports.MAX_LITERAL_SIZE = void 0;
7
+ const errors_js_1 = require("../errors.js");
7
8
  // Maximum allowed literal size: 1GB (1073741824 bytes)
8
9
  exports.MAX_LITERAL_SIZE = 1024 * 1024 * 1024;
9
10
  // Default maximum length of a single line (a response without a literal). Matches the literal cap:
@@ -56,11 +57,8 @@ exports.normalizeLimit = normalizeLimit;
56
57
  * @param reason - What the bound was, when it is not the configured maximum.
57
58
  * @returns The error to emit or throw.
58
59
  */
59
- const createLiteralTooLargeError = (literalSize, maxSize, reason) => {
60
- const err = new Error(`Literal size ${literalSize} exceeds ${reason || `maximum allowed size of ${maxSize} bytes`}`);
61
- err.code = 'LiteralTooLarge';
62
- err.literalSize = literalSize;
63
- err.maxSize = maxSize;
64
- return err;
65
- };
60
+ const createLiteralTooLargeError = (literalSize, maxSize, reason) => (0, errors_js_1.createImapError)(`Literal size ${literalSize} exceeds ${reason || `maximum allowed size of ${maxSize} bytes`}`, 'LiteralTooLarge', {
61
+ literalSize,
62
+ maxSize
63
+ });
66
64
  exports.createLiteralTooLargeError = createLiteralTooLargeError;