imapflow 2.2.1 → 2.2.3

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.2.3](https://github.com/postalsys/imapflow/compare/v2.2.2...v2.2.3) (2026-10-03)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * reject NUL in a non-ASCII search value instead of sending it as a literal ([8c35084](https://github.com/postalsys/imapflow/commit/8c35084f12376a1195989bf99621c2587f548aca))
9
+ * send non-ASCII search values as literals ([b58d93e](https://github.com/postalsys/imapflow/commit/b58d93e49dd37ef008418c2670afaf53b3d78034)), closes [#417](https://github.com/postalsys/imapflow/issues/417)
10
+
11
+ ## [2.2.2](https://github.com/postalsys/imapflow/compare/v2.2.1...v2.2.2) (2026-10-03)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * do not let a flag update that reduces to nothing clear every flag ([686a04b](https://github.com/postalsys/imapflow/commit/686a04b6c29d5f97afdbb03779e7b29d102cdef3))
17
+ * keep append flags when the destination is not an open read-write mailbox ([d7f8da3](https://github.com/postalsys/imapflow/commit/d7f8da3131d8df5b5607021dbbca27445bb973c2)), closes [#415](https://github.com/postalsys/imapflow/issues/415)
18
+
3
19
  ## [2.2.1](https://github.com/postalsys/imapflow/compare/v2.2.0...v2.2.1) (2026-10-01)
4
20
 
5
21
 
@@ -37,10 +37,34 @@ async function append(connection, destination, content, flags, idate) {
37
37
  let selected = (0, tools_js_1.getSelectedMailbox)(connection);
38
38
  // The selected mailbox when appending to it, false otherwise
39
39
  const targetMailbox = selected && (0, tools_js_1.comparePaths)(connection, selected.path, destination) ? selected : false;
40
- // Validate and format flags. Only flags allowed by the mailbox's permanentFlags are included.
40
+ // Flags are only filtered when a read-write selection of the destination itself says what it
41
+ // accepts: the permanentFlags of an unrelated mailbox say nothing about the destination, and a
42
+ // read-only selection reports an empty PERMANENTFLAGS list (Dovecot does), which canUseFlag()
43
+ // reads as deny-all. Otherwise they go to the server unfiltered and it decides (RFC 9051).
44
+ const flagSource = targetMailbox && !targetMailbox.readOnly ? targetMailbox : false;
45
+ const dropped = [];
41
46
  flags = (Array.isArray(flags) ? flags : [].concat(flags || []))
42
- .map(flag => flag && (0, tools_js_1.formatFlag)(flag.toString()))
43
- .filter((flag) => !!flag && (0, tools_js_1.canUseFlag)(connection.mailbox, flag));
47
+ .map(flag => {
48
+ const formatted = flag && (0, tools_js_1.formatFlag)(flag.toString());
49
+ if (!formatted || !(0, tools_js_1.canUseFlag)(flagSource, formatted)) {
50
+ if (flag) {
51
+ dropped.push(flag.toString());
52
+ }
53
+ return false;
54
+ }
55
+ return formatted;
56
+ })
57
+ .filter((flag) => !!flag);
58
+ // The stored message simply ends up without them, and the caller is told nothing, so leave
59
+ // a trail for the flags that never reached the server.
60
+ if (dropped.length) {
61
+ connection.log.warn({
62
+ msg: 'Dropped flags the mailbox does not accept',
63
+ cid: connection.id,
64
+ path: destination,
65
+ dropped
66
+ });
67
+ }
44
68
  // APPEND command format: APPEND <mailbox> [<flags>] [<date-time>] <literal>
45
69
  let attributes = [{ type: 'ATOM', value: (0, tools_js_1.encodePath)(connection, destination) }];
46
70
  // Internal date: the date the server should record for this message.
@@ -31,8 +31,11 @@ async function store(connection, range, flags, options) {
31
31
  else if (options.silent) {
32
32
  operation = `${operation}.SILENT`;
33
33
  }
34
+ // Normalized once: the raw value is also compared below, and a mixed-case 'SET' must not
35
+ // take the set branch here and the add branch there.
36
+ const operationName = (options.operation || '').toLowerCase();
34
37
  // Prefix determines the operation: none = set (replace), + = add, - = remove
35
- switch ((options.operation || '').toLowerCase()) {
38
+ switch (operationName) {
36
39
  case 'set':
37
40
  break;
38
41
  case 'remove':
@@ -43,20 +46,42 @@ async function store(connection, range, flags, options) {
43
46
  operation = `+${operation}`;
44
47
  break;
45
48
  }
46
- // Validate each flag: format it (normalize backslash prefix for system flags),
47
- // then check if the mailbox's permanentFlags allow it. Removal is always allowed
49
+ // Only an explicitly empty array asks for every flag to be cleared. A missing value is not
50
+ // that request, and neither is a 'set' whose flags all get dropped below: either would
51
+ // compile to "FLAGS ()" and wipe the message instead of storing what was asked for. The
52
+ // line is drawn at destroying flags, not at fidelity, so a set that keeps some of the
53
+ // requested flags still runs, as the documented contract for those methods says.
54
+ const clearAll = operationName === 'set' && Array.isArray(flags) && !flags.length;
55
+ // permanentFlags lists the IMAP keywords the mailbox accepts and says nothing about Gmail
56
+ // labels, so it is not consulted for X-GM-LABELS - the same reason an unrelated mailbox's
57
+ // flags are not consulted for APPEND (issue #415).
58
+ const flagSource = options.useLabels ? false : mailbox;
59
+ // Validate each flag: format it (normalize backslash prefix for system flags, reject the
60
+ // server-owned \Recent), then check that the mailbox allows it. Removal is always allowed
48
61
  // since it doesn't require the flag to be in permanentFlags.
62
+ const dropped = [];
49
63
  flags = (Array.isArray(flags) ? flags : [].concat(flags || []))
50
64
  .map(flag => {
51
65
  let formatted = (0, tools_js_1.formatFlag)(flag);
52
- if (!(0, tools_js_1.canUseFlag)(mailbox, formatted) && options.operation !== 'remove') {
66
+ if (!formatted || (!(0, tools_js_1.canUseFlag)(flagSource, formatted) && operationName !== 'remove')) {
67
+ dropped.push(flag);
53
68
  return false;
54
69
  }
55
70
  return formatted;
56
71
  })
57
72
  .filter((flag) => !!flag);
58
- // Allow empty flags only for 'set' operation (which clears all flags)
59
- if (!flags.length && options.operation !== 'set') {
73
+ // The caller is told nothing by the boolean this returns, so leave a trail for the flags
74
+ // that never reached the server.
75
+ if (dropped.length) {
76
+ connection.log.warn({
77
+ msg: 'Dropped flags the mailbox does not accept',
78
+ cid: connection.id,
79
+ path: mailbox.path,
80
+ operation,
81
+ dropped
82
+ });
83
+ }
84
+ if (!flags.length && !clearAll) {
60
85
  return false;
61
86
  }
62
87
  let attributes = [
@@ -356,7 +356,7 @@ export declare class ImapFlow extends EventEmitter {
356
356
  * Sets flags for a message or message range
357
357
  *
358
358
  * @param range Range to filter the messages
359
- * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored
359
+ * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored. An empty array clears every flag, but a non-empty array with nothing usable in it is refused rather than clearing the message
360
360
  * @param options Store options
361
361
  * @returns Did the operation succeed or not
362
362
  *
@@ -2631,7 +2631,7 @@ class ImapFlow extends node_events_1.EventEmitter {
2631
2631
  * Sets flags for a message or message range
2632
2632
  *
2633
2633
  * @param range Range to filter the messages
2634
- * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored
2634
+ * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored. An empty array clears every flag, but a non-empty array with nothing usable in it is refused rather than clearing the message
2635
2635
  * @param options Store options
2636
2636
  * @returns Did the operation succeed or not
2637
2637
  *
@@ -1,3 +1,3 @@
1
1
  export declare const name = "imapflow";
2
- export declare const version = "2.2.1";
2
+ export declare const version = "2.2.3";
3
3
  export declare const homepage = "https://imapflow.com/";
@@ -3,5 +3,5 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.homepage = exports.version = exports.name = void 0;
5
5
  exports.name = "imapflow";
6
- exports.version = "2.2.1";
6
+ exports.version = "2.2.3";
7
7
  exports.homepage = "https://imapflow.com/";
@@ -3,6 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.searchCompiler = void 0;
5
5
  const tools_js_1 = require("./tools.js");
6
+ // Matches any character outside the ASCII range
7
+ const UNICODE_PATTERN = /[^\x00-\x7F]/;
6
8
  /**
7
9
  * Sets a boolean flag in the IMAP search attributes.
8
10
  * Automatically handles UN- prefixing for falsy values.
@@ -38,6 +40,17 @@ let setBoolOpt = (attributes, term, value) => {
38
40
  * @returns The joined sequence set string
39
41
  */
40
42
  let toSequenceValue = (value) => [].concat(value).join(',');
43
+ /**
44
+ * Builds the token for a search value. A quoted string may only carry 7-bit
45
+ * characters (RFC 3501 section 9), so a non-ASCII value is sent as a literal;
46
+ * strict servers reply BAD to UTF-8 inside a quoted string. A literal can not
47
+ * carry NUL either (CHAR8 is %x01-ff), so such a value stays an ATOM and the
48
+ * compiler rejects it.
49
+ *
50
+ * @param value - The search value
51
+ * @returns An ATOM token (quoted by the compiler when needed), or a LITERAL token
52
+ */
53
+ let toSearchValue = (value) => UNICODE_PATTERN.test(value) && !value.includes('\0') ? { type: 'LITERAL', value: Buffer.from(value) } : { type: 'ATOM', value };
41
54
  /**
42
55
  * Adds a search option with its value(s) to the attributes array.
43
56
  * Handles NOT operations and array values.
@@ -54,10 +67,10 @@ let setOpt = (attributes, term, value) => {
54
67
  attributes.push({ type: 'ATOM', value: term.toUpperCase() });
55
68
  // Handle array values (e.g. HEADER name/value pairs)
56
69
  if (Array.isArray(value)) {
57
- value.forEach(entry => attributes.push({ type: 'ATOM', value: (entry || '').toString() }));
70
+ value.forEach(entry => attributes.push(toSearchValue((entry || '').toString())));
58
71
  }
59
72
  else {
60
- attributes.push({ type: 'ATOM', value: value.toString() });
73
+ attributes.push(toSearchValue(value.toString()));
61
74
  }
62
75
  };
63
76
  /**
@@ -89,8 +102,6 @@ let processDateField = (attributes, term, value) => {
89
102
  }
90
103
  setOpt(attributes, term, formatted);
91
104
  };
92
- // Pre-compiled regex for better performance
93
- const UNICODE_PATTERN = /[^\x00-\x7F]/;
94
105
  /**
95
106
  * Throws a coded search compilation error.
96
107
  *
@@ -103,20 +114,13 @@ let fail = (code, message) => {
103
114
  throw error;
104
115
  };
105
116
  /**
106
- * Checks if a string contains Unicode characters.
107
- * Used to determine if CHARSET UTF-8 needs to be specified.
117
+ * Checks whether any search value was compiled into a literal, which only
118
+ * happens for non-ASCII values and means CHARSET UTF-8 needs to be specified.
108
119
  *
109
- * @param str - String to check
110
- * @returns True if string contains non-ASCII characters
120
+ * @param attributes - Compiled search attributes
121
+ * @returns True if a LITERAL token is present
111
122
  */
112
- let isUnicodeString = (str) => {
113
- if (!str || typeof str !== 'string') {
114
- return false;
115
- }
116
- // Regex test is ~3-5x faster than Buffer.byteLength
117
- // Matches any character outside ASCII range (0x00-0x7F)
118
- return UNICODE_PATTERN.test(str);
119
- };
123
+ let hasLiteral = (attributes) => attributes.some(attr => (Array.isArray(attr) ? hasLiteral(attr) : attr.type === 'LITERAL'));
120
124
  /**
121
125
  * Compiles a JavaScript object query into IMAP search command attributes.
122
126
  * Supports standard IMAP search criteria and extensions like OBJECTID and Gmail extensions.
@@ -145,8 +149,6 @@ let isUnicodeString = (str) => {
145
149
  */
146
150
  const searchCompiler = (connection, query) => {
147
151
  const attributes = [];
148
- // Track if we need to specify UTF-8 charset
149
- let hasUnicode = false;
150
152
  /**
151
153
  * Recursively walks through the query object and builds IMAP attributes.
152
154
  * @param params - Query parameters to process
@@ -236,9 +238,6 @@ const searchCompiler = (connection, query) => {
236
238
  case 'SUBJECT':
237
239
  case 'TEXT':
238
240
  case 'TO':
239
- if (isUnicodeString(params[term])) {
240
- hasUnicode = true;
241
- }
242
241
  if (params[term]) {
243
242
  setOpt(attributes, term, params[term]);
244
243
  }
@@ -286,9 +285,6 @@ const searchCompiler = (connection, query) => {
286
285
  case 'GMRAW':
287
286
  case 'GMAILRAW': // alias for GMRAW
288
287
  if (connection.capabilities.has('X-GM-EXT-1')) {
289
- if (isUnicodeString(params[term])) {
290
- hasUnicode = true;
291
- }
292
288
  setOpt(attributes, 'X-GM-RAW', params[term]);
293
289
  }
294
290
  else {
@@ -329,9 +325,6 @@ const searchCompiler = (connection, query) => {
329
325
  fail('MissingServerExtension', 'Server does not support X-GM-EXT-1 extension required for label search');
330
326
  }
331
327
  let rawQuery = rawParts.join(' ');
332
- if (isUnicodeString(rawQuery)) {
333
- hasUnicode = true;
334
- }
335
328
  setOpt(attributes, 'X-GM-RAW', rawQuery);
336
329
  break;
337
330
  }
@@ -391,9 +384,6 @@ const searchCompiler = (connection, query) => {
391
384
  if (typeof value !== 'string') {
392
385
  return;
393
386
  }
394
- if (isUnicodeString(value)) {
395
- hasUnicode = true;
396
- }
397
387
  setOpt(attributes, term, [header.toUpperCase().trim(), value]);
398
388
  });
399
389
  }
@@ -473,7 +463,7 @@ const searchCompiler = (connection, query) => {
473
463
  walk(query);
474
464
  // If we encountered Unicode strings and UTF-8 is not already accepted,
475
465
  // prepend CHARSET UTF-8 to the search command
476
- if (hasUnicode && !connection.enabled.has('UTF8=ACCEPT')) {
466
+ if (!connection.enabled.has('UTF8=ACCEPT') && hasLiteral(attributes)) {
477
467
  attributes.unshift({ type: 'ATOM', value: 'UTF-8' });
478
468
  attributes.unshift({ type: 'ATOM', value: 'CHARSET' });
479
469
  }
@@ -34,10 +34,34 @@ export default async function append(connection, destination, content, flags, id
34
34
  let selected = getSelectedMailbox(connection);
35
35
  // The selected mailbox when appending to it, false otherwise
36
36
  const targetMailbox = selected && comparePaths(connection, selected.path, destination) ? selected : false;
37
- // Validate and format flags. Only flags allowed by the mailbox's permanentFlags are included.
37
+ // Flags are only filtered when a read-write selection of the destination itself says what it
38
+ // accepts: the permanentFlags of an unrelated mailbox say nothing about the destination, and a
39
+ // read-only selection reports an empty PERMANENTFLAGS list (Dovecot does), which canUseFlag()
40
+ // reads as deny-all. Otherwise they go to the server unfiltered and it decides (RFC 9051).
41
+ const flagSource = targetMailbox && !targetMailbox.readOnly ? targetMailbox : false;
42
+ const dropped = [];
38
43
  flags = (Array.isArray(flags) ? flags : [].concat(flags || []))
39
- .map(flag => flag && formatFlag(flag.toString()))
40
- .filter((flag) => !!flag && canUseFlag(connection.mailbox, flag));
44
+ .map(flag => {
45
+ const formatted = flag && formatFlag(flag.toString());
46
+ if (!formatted || !canUseFlag(flagSource, formatted)) {
47
+ if (flag) {
48
+ dropped.push(flag.toString());
49
+ }
50
+ return false;
51
+ }
52
+ return formatted;
53
+ })
54
+ .filter((flag) => !!flag);
55
+ // The stored message simply ends up without them, and the caller is told nothing, so leave
56
+ // a trail for the flags that never reached the server.
57
+ if (dropped.length) {
58
+ connection.log.warn({
59
+ msg: 'Dropped flags the mailbox does not accept',
60
+ cid: connection.id,
61
+ path: destination,
62
+ dropped
63
+ });
64
+ }
41
65
  // APPEND command format: APPEND <mailbox> [<flags>] [<date-time>] <literal>
42
66
  let attributes = [{ type: 'ATOM', value: encodePath(connection, destination) }];
43
67
  // Internal date: the date the server should record for this message.
@@ -28,8 +28,11 @@ export default async function store(connection, range, flags, options) {
28
28
  else if (options.silent) {
29
29
  operation = `${operation}.SILENT`;
30
30
  }
31
+ // Normalized once: the raw value is also compared below, and a mixed-case 'SET' must not
32
+ // take the set branch here and the add branch there.
33
+ const operationName = (options.operation || '').toLowerCase();
31
34
  // Prefix determines the operation: none = set (replace), + = add, - = remove
32
- switch ((options.operation || '').toLowerCase()) {
35
+ switch (operationName) {
33
36
  case 'set':
34
37
  break;
35
38
  case 'remove':
@@ -40,20 +43,42 @@ export default async function store(connection, range, flags, options) {
40
43
  operation = `+${operation}`;
41
44
  break;
42
45
  }
43
- // Validate each flag: format it (normalize backslash prefix for system flags),
44
- // then check if the mailbox's permanentFlags allow it. Removal is always allowed
46
+ // Only an explicitly empty array asks for every flag to be cleared. A missing value is not
47
+ // that request, and neither is a 'set' whose flags all get dropped below: either would
48
+ // compile to "FLAGS ()" and wipe the message instead of storing what was asked for. The
49
+ // line is drawn at destroying flags, not at fidelity, so a set that keeps some of the
50
+ // requested flags still runs, as the documented contract for those methods says.
51
+ const clearAll = operationName === 'set' && Array.isArray(flags) && !flags.length;
52
+ // permanentFlags lists the IMAP keywords the mailbox accepts and says nothing about Gmail
53
+ // labels, so it is not consulted for X-GM-LABELS - the same reason an unrelated mailbox's
54
+ // flags are not consulted for APPEND (issue #415).
55
+ const flagSource = options.useLabels ? false : mailbox;
56
+ // Validate each flag: format it (normalize backslash prefix for system flags, reject the
57
+ // server-owned \Recent), then check that the mailbox allows it. Removal is always allowed
45
58
  // since it doesn't require the flag to be in permanentFlags.
59
+ const dropped = [];
46
60
  flags = (Array.isArray(flags) ? flags : [].concat(flags || []))
47
61
  .map(flag => {
48
62
  let formatted = formatFlag(flag);
49
- if (!canUseFlag(mailbox, formatted) && options.operation !== 'remove') {
63
+ if (!formatted || (!canUseFlag(flagSource, formatted) && operationName !== 'remove')) {
64
+ dropped.push(flag);
50
65
  return false;
51
66
  }
52
67
  return formatted;
53
68
  })
54
69
  .filter((flag) => !!flag);
55
- // Allow empty flags only for 'set' operation (which clears all flags)
56
- if (!flags.length && options.operation !== 'set') {
70
+ // The caller is told nothing by the boolean this returns, so leave a trail for the flags
71
+ // that never reached the server.
72
+ if (dropped.length) {
73
+ connection.log.warn({
74
+ msg: 'Dropped flags the mailbox does not accept',
75
+ cid: connection.id,
76
+ path: mailbox.path,
77
+ operation,
78
+ dropped
79
+ });
80
+ }
81
+ if (!flags.length && !clearAll) {
57
82
  return false;
58
83
  }
59
84
  let attributes = [
@@ -356,7 +356,7 @@ export declare class ImapFlow extends EventEmitter {
356
356
  * Sets flags for a message or message range
357
357
  *
358
358
  * @param range Range to filter the messages
359
- * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored
359
+ * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored. An empty array clears every flag, but a non-empty array with nothing usable in it is refused rather than clearing the message
360
360
  * @param options Store options
361
361
  * @returns Did the operation succeed or not
362
362
  *
@@ -2590,7 +2590,7 @@ export class ImapFlow extends EventEmitter {
2590
2590
  * Sets flags for a message or message range
2591
2591
  *
2592
2592
  * @param range Range to filter the messages
2593
- * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored
2593
+ * @param flags Array of flags to set. Only flags that are permitted to set are used, other flags are ignored. An empty array clears every flag, but a non-empty array with nothing usable in it is refused rather than clearing the message
2594
2594
  * @param options Store options
2595
2595
  * @returns Did the operation succeed or not
2596
2596
  *
@@ -1,3 +1,3 @@
1
1
  export declare const name = "imapflow";
2
- export declare const version = "2.2.1";
2
+ export declare const version = "2.2.3";
3
3
  export declare const homepage = "https://imapflow.com/";
@@ -1,4 +1,4 @@
1
1
  // Generated by scripts/build.js from package.json. Do not edit by hand.
2
2
  export const name = "imapflow";
3
- export const version = "2.2.1";
3
+ export const version = "2.2.3";
4
4
  export const homepage = "https://imapflow.com/";
@@ -1,5 +1,7 @@
1
1
  /* eslint no-control-regex:0 */
2
2
  import { formatDate, formatFlag, toValidDate, isRev2Active } from './tools.js';
3
+ // Matches any character outside the ASCII range
4
+ const UNICODE_PATTERN = /[^\x00-\x7F]/;
3
5
  /**
4
6
  * Sets a boolean flag in the IMAP search attributes.
5
7
  * Automatically handles UN- prefixing for falsy values.
@@ -35,6 +37,17 @@ let setBoolOpt = (attributes, term, value) => {
35
37
  * @returns The joined sequence set string
36
38
  */
37
39
  let toSequenceValue = (value) => [].concat(value).join(',');
40
+ /**
41
+ * Builds the token for a search value. A quoted string may only carry 7-bit
42
+ * characters (RFC 3501 section 9), so a non-ASCII value is sent as a literal;
43
+ * strict servers reply BAD to UTF-8 inside a quoted string. A literal can not
44
+ * carry NUL either (CHAR8 is %x01-ff), so such a value stays an ATOM and the
45
+ * compiler rejects it.
46
+ *
47
+ * @param value - The search value
48
+ * @returns An ATOM token (quoted by the compiler when needed), or a LITERAL token
49
+ */
50
+ let toSearchValue = (value) => UNICODE_PATTERN.test(value) && !value.includes('\0') ? { type: 'LITERAL', value: Buffer.from(value) } : { type: 'ATOM', value };
38
51
  /**
39
52
  * Adds a search option with its value(s) to the attributes array.
40
53
  * Handles NOT operations and array values.
@@ -51,10 +64,10 @@ let setOpt = (attributes, term, value) => {
51
64
  attributes.push({ type: 'ATOM', value: term.toUpperCase() });
52
65
  // Handle array values (e.g. HEADER name/value pairs)
53
66
  if (Array.isArray(value)) {
54
- value.forEach(entry => attributes.push({ type: 'ATOM', value: (entry || '').toString() }));
67
+ value.forEach(entry => attributes.push(toSearchValue((entry || '').toString())));
55
68
  }
56
69
  else {
57
- attributes.push({ type: 'ATOM', value: value.toString() });
70
+ attributes.push(toSearchValue(value.toString()));
58
71
  }
59
72
  };
60
73
  /**
@@ -86,8 +99,6 @@ let processDateField = (attributes, term, value) => {
86
99
  }
87
100
  setOpt(attributes, term, formatted);
88
101
  };
89
- // Pre-compiled regex for better performance
90
- const UNICODE_PATTERN = /[^\x00-\x7F]/;
91
102
  /**
92
103
  * Throws a coded search compilation error.
93
104
  *
@@ -100,20 +111,13 @@ let fail = (code, message) => {
100
111
  throw error;
101
112
  };
102
113
  /**
103
- * Checks if a string contains Unicode characters.
104
- * Used to determine if CHARSET UTF-8 needs to be specified.
114
+ * Checks whether any search value was compiled into a literal, which only
115
+ * happens for non-ASCII values and means CHARSET UTF-8 needs to be specified.
105
116
  *
106
- * @param str - String to check
107
- * @returns True if string contains non-ASCII characters
117
+ * @param attributes - Compiled search attributes
118
+ * @returns True if a LITERAL token is present
108
119
  */
109
- let isUnicodeString = (str) => {
110
- if (!str || typeof str !== 'string') {
111
- return false;
112
- }
113
- // Regex test is ~3-5x faster than Buffer.byteLength
114
- // Matches any character outside ASCII range (0x00-0x7F)
115
- return UNICODE_PATTERN.test(str);
116
- };
120
+ let hasLiteral = (attributes) => attributes.some(attr => (Array.isArray(attr) ? hasLiteral(attr) : attr.type === 'LITERAL'));
117
121
  /**
118
122
  * Compiles a JavaScript object query into IMAP search command attributes.
119
123
  * Supports standard IMAP search criteria and extensions like OBJECTID and Gmail extensions.
@@ -142,8 +146,6 @@ let isUnicodeString = (str) => {
142
146
  */
143
147
  export const searchCompiler = (connection, query) => {
144
148
  const attributes = [];
145
- // Track if we need to specify UTF-8 charset
146
- let hasUnicode = false;
147
149
  /**
148
150
  * Recursively walks through the query object and builds IMAP attributes.
149
151
  * @param params - Query parameters to process
@@ -233,9 +235,6 @@ export const searchCompiler = (connection, query) => {
233
235
  case 'SUBJECT':
234
236
  case 'TEXT':
235
237
  case 'TO':
236
- if (isUnicodeString(params[term])) {
237
- hasUnicode = true;
238
- }
239
238
  if (params[term]) {
240
239
  setOpt(attributes, term, params[term]);
241
240
  }
@@ -283,9 +282,6 @@ export const searchCompiler = (connection, query) => {
283
282
  case 'GMRAW':
284
283
  case 'GMAILRAW': // alias for GMRAW
285
284
  if (connection.capabilities.has('X-GM-EXT-1')) {
286
- if (isUnicodeString(params[term])) {
287
- hasUnicode = true;
288
- }
289
285
  setOpt(attributes, 'X-GM-RAW', params[term]);
290
286
  }
291
287
  else {
@@ -326,9 +322,6 @@ export const searchCompiler = (connection, query) => {
326
322
  fail('MissingServerExtension', 'Server does not support X-GM-EXT-1 extension required for label search');
327
323
  }
328
324
  let rawQuery = rawParts.join(' ');
329
- if (isUnicodeString(rawQuery)) {
330
- hasUnicode = true;
331
- }
332
325
  setOpt(attributes, 'X-GM-RAW', rawQuery);
333
326
  break;
334
327
  }
@@ -388,9 +381,6 @@ export const searchCompiler = (connection, query) => {
388
381
  if (typeof value !== 'string') {
389
382
  return;
390
383
  }
391
- if (isUnicodeString(value)) {
392
- hasUnicode = true;
393
- }
394
384
  setOpt(attributes, term, [header.toUpperCase().trim(), value]);
395
385
  });
396
386
  }
@@ -470,7 +460,7 @@ export const searchCompiler = (connection, query) => {
470
460
  walk(query);
471
461
  // If we encountered Unicode strings and UTF-8 is not already accepted,
472
462
  // prepend CHARSET UTF-8 to the search command
473
- if (hasUnicode && !connection.enabled.has('UTF8=ACCEPT')) {
463
+ if (!connection.enabled.has('UTF8=ACCEPT') && hasLiteral(attributes)) {
474
464
  attributes.unshift({ type: 'ATOM', value: 'UTF-8' });
475
465
  attributes.unshift({ type: 'ATOM', value: 'CHARSET' });
476
466
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imapflow",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "IMAP Client for Node",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/imap-flow.js",
@@ -70,7 +70,7 @@
70
70
  "devDependencies": {
71
71
  "@types/node": "20.19.43",
72
72
  "c8": "12.0.0",
73
- "eslint": "10.11.0",
73
+ "eslint": "10.12.0",
74
74
  "eslint-config-prettier": "10.1.8",
75
75
  "globals": "17.13.0",
76
76
  "prettier": "3.9.9",
@@ -78,7 +78,7 @@
78
78
  "types-node-legacy": "npm:@types/node@20.0.0",
79
79
  "typescript": "6.0.3",
80
80
  "typescript-eslint": "8.71.0",
81
- "wrangler": "4.145.0"
81
+ "wrangler": "4.147.0"
82
82
  },
83
83
  "dependencies": {
84
84
  "@zone-eu/mailsplit": "5.4.19",
@@ -87,7 +87,7 @@
87
87
  "libbase64": "1.3.1",
88
88
  "libmime": "5.4.6",
89
89
  "libqp": "2.1.2",
90
- "pino": "10.3.1",
90
+ "pino": "10.4.0",
91
91
  "socks": "2.8.10"
92
92
  },
93
93
  "engines": {