emailengine-app 2.83.1 → 2.83.2

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.
@@ -15,6 +15,11 @@ const { Writable, Readable, pipeline } = require('stream');
15
15
  const DEFAULT_CREDIT_WINDOW = 1024 * 1024;
16
16
  const ACK_BATCH_FRACTION = 4;
17
17
 
18
+ // How long a write may wait for credit before the transfer is abandoned. A consumer that stops
19
+ // reading without disconnecting never cancels, and the IMAP download feeding this writer would
20
+ // hold the account's mailbox lock for as long as its socket lived.
21
+ const DEFAULT_STALL_TIMEOUT = 5 * 60 * 1000;
22
+
18
23
  class MessagePortWritable extends Writable {
19
24
  constructor(messagePort, opts) {
20
25
  super();
@@ -24,6 +29,9 @@ class MessagePortWritable extends Writable {
24
29
  this.credit = (opts && opts.creditWindow) || DEFAULT_CREDIT_WINDOW;
25
30
  this.pendingWrite = null;
26
31
 
32
+ this.stallTimeout = (opts && opts.stallTimeout) || DEFAULT_STALL_TIMEOUT;
33
+ this.stallTimer = null;
34
+
27
35
  // The reader side posts { cancel: true } when it is torn down (e.g. the
28
36
  // HTTP client aborted the download). Stop the transfer so the upstream
29
37
  // source - and the IMAP lock it holds - is released.
@@ -37,7 +45,11 @@ class MessagePortWritable extends Writable {
37
45
  if (this.pendingWrite && this.credit > 0) {
38
46
  let done = this.pendingWrite;
39
47
  this.pendingWrite = null;
48
+ clearTimeout(this.stallTimer);
40
49
  done();
50
+ } else if (this.pendingWrite) {
51
+ // Credit is still short, but the reader did take something: that is progress
52
+ this.stallTimer.refresh();
41
53
  }
42
54
  }
43
55
  };
@@ -110,6 +122,12 @@ class MessagePortWritable extends Writable {
110
122
  }
111
123
  // Out of credit: the next write waits until the reader has taken some of what was sent
112
124
  this.pendingWrite = done;
125
+ this.stallTimer = setTimeout(() => {
126
+ let err = new Error('Message stream consumer stopped reading');
127
+ err.code = 'ConsumerStalled';
128
+ this.destroy(err);
129
+ }, this.stallTimeout);
130
+ this.stallTimer.unref();
113
131
  }
114
132
 
115
133
  _final(done) {
@@ -121,6 +139,7 @@ class MessagePortWritable extends Writable {
121
139
  _destroy(err, done) {
122
140
  // A write waiting for credit is abandoned with the stream; destroy() does not wait for it
123
141
  this.pendingWrite = null;
142
+ clearTimeout(this.stallTimer);
124
143
 
125
144
  // Abnormal termination: tell the reader so a truncated transfer is not
126
145
  // mistaken for a complete message, then release the port.
@@ -278,6 +297,10 @@ function pipeToMessagePort(source, writable, logger) {
278
297
  }
279
298
  return;
280
299
  }
300
+ if (err.code === 'ConsumerStalled') {
301
+ logger.warn?.({ msg: 'Message stream transfer abandoned, the consumer stopped reading', err });
302
+ return;
303
+ }
281
304
  if (typeof logger.error === 'function') {
282
305
  logger.error({ msg: 'Message stream transfer failed', err });
283
306
  }
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ // How a failed outbound HTTP request is told apart from a refused or broken one. undici raises
4
+ // every connection failure as a generic TypeError ("fetch failed", "terminated") with the real
5
+ // DNS/socket/timeout error on err.cause, so a check of err.code alone misses all of them.
6
+ //
7
+ // Dependency-free apart from consts, so lib/logger.js can use it without a require cycle.
8
+
9
+ const { TRANSIENT_NETWORK_CODES } = require('./consts');
10
+
11
+ // How far down a cause chain the transient check looks. A chain is not guaranteed to be finite - an
12
+ // error may cite itself - so the walk is bounded rather than trusting it to end.
13
+ const MAX_CAUSE_DEPTH = 5;
14
+
15
+ /**
16
+ * Failing to reach a remote endpoint is a connection problem, not a rejected credential or a bug.
17
+ *
18
+ * The whole cause chain is searched, not one level: a caller that wraps an undici failure in an
19
+ * error of its own pushes the code one level deeper (lib/oauth/external-account-signer.js does
20
+ * exactly that for its Workload Identity Federation requests).
21
+ *
22
+ * @param {Error} err - The error to check
23
+ * @returns {boolean} True if the error, or anything it was caused by, is a transient network failure
24
+ */
25
+ function isTransientNetworkError(err) {
26
+ for (let cause = err, depth = 0; cause && typeof cause === 'object' && depth < MAX_CAUSE_DEPTH; cause = cause.cause, depth++) {
27
+ if (TRANSIENT_NETWORK_CODES.has(cause.code)) {
28
+ return true;
29
+ }
30
+ }
31
+
32
+ return false;
33
+ }
34
+
35
+ /**
36
+ * Copies the code of an undici failure from err.cause onto the error itself, so callers that read
37
+ * err.code (and the webhook and log payloads built from it) see ECONNRESET or UND_ERR_BODY_TIMEOUT
38
+ * rather than nothing. Leaves an error that already carries a code alone.
39
+ *
40
+ * @param {Error} err - The error thrown by fetch()
41
+ * @returns {Error} The same error
42
+ */
43
+ function promoteCauseCode(err) {
44
+ if (err && !err.code && err.cause && err.cause.code) {
45
+ err.code = err.cause.code;
46
+ }
47
+ return err;
48
+ }
49
+
50
+ module.exports = { isTransientNetworkError, promoteCauseCode };
@@ -5,6 +5,7 @@ const { formatPartialSecretKey, structuredClone, httpAgent, formatTokenError, pa
5
5
  const crypto = require('crypto');
6
6
 
7
7
  const { fetch: fetchCmd } = require('undici');
8
+ const { promoteCauseCode } = require('../network-errors');
8
9
 
9
10
  const { ExternalAccountSigner } = require('./external-account-signer');
10
11
  const { fetchTokenRequest } = require('./token-fetch');
@@ -733,7 +734,10 @@ class GmailOauth {
733
734
 
734
735
  let startTime = Date.now();
735
736
 
736
- let res = await fetchCmd(url, reqData);
737
+ let res = await fetchCmd(url, reqData).catch(err => {
738
+ // undici puts the network code on err.cause; callers read err.code
739
+ throw promoteCauseCode(err);
740
+ });
737
741
 
738
742
  let reqTime = Date.now() - startTime;
739
743
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  const packageData = require('../../package.json');
4
4
  const { fetch: fetchCmd } = require('undici');
5
+ const { promoteCauseCode } = require('../network-errors');
5
6
  const { fetchTokenRequest } = require('./token-fetch');
6
7
  const { formatPartialSecretKey, structuredClone, httpAgent, formatTokenError } = require('../tools');
7
8
 
@@ -304,7 +305,10 @@ class MailRuOauth {
304
305
  const requestUrl = new URL(url);
305
306
  requestUrl.searchParams.set('access_token', accessToken);
306
307
 
307
- let res = await fetchCmd(requestUrl.href, reqData);
308
+ let res = await fetchCmd(requestUrl.href, reqData).catch(err => {
309
+ // undici puts the network code on err.cause; callers read err.code
310
+ throw promoteCauseCode(err);
311
+ });
308
312
 
309
313
  if (!res.ok) {
310
314
  let err = new Error('OAuth2 request failed');
@@ -4,6 +4,7 @@ const packageData = require('../../package.json');
4
4
  const { formatPartialSecretKey, structuredClone, httpAgent, formatTokenError, parseRetryAfter } = require('../tools');
5
5
 
6
6
  const { fetch: fetchCmd } = require('undici');
7
+ const { promoteCauseCode } = require('../network-errors');
7
8
  const { fetchTokenRequest } = require('./token-fetch');
8
9
 
9
10
  // Individual MS Graph API scope names for detection
@@ -594,7 +595,10 @@ class OutlookOauth {
594
595
  }
595
596
 
596
597
  let startTime = Date.now();
597
- let res = await fetchCmd(url, reqData);
598
+ let res = await fetchCmd(url, reqData).catch(err => {
599
+ // undici puts the network code on err.cause; callers read err.code
600
+ throw promoteCauseCode(err);
601
+ });
598
602
  let reqTime = Date.now() - startTime;
599
603
 
600
604
  if (!res.ok) {
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  const { redis } = require('../../db');
4
- const { REDIS_PREFIX, TRANSIENT_NETWORK_CODES } = require('../../consts');
4
+ const { REDIS_PREFIX } = require('../../consts');
5
+ const { isTransientNetworkError } = require('../../network-errors');
5
6
  const logger = require('../../logger').child({
6
7
  component: 'google-subscriber'
7
8
  });
@@ -298,7 +299,7 @@ class PubSubInstance {
298
299
  throw err;
299
300
  }
300
301
  // Transient network errors are expected for long-polling connections
301
- if (TRANSIENT_NETWORK_CODES.has(err.code)) {
302
+ if (isTransientNetworkError(err)) {
302
303
  logger.warn({ msg: 'Transient error pulling subscription messages', app: this.app, code: err.code });
303
304
  err.retryDelay = 5000;
304
305
  err.eeLogged = true;
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { fetch: fetchCmd } = require('undici');
4
4
  const { httpAgent } = require('../tools');
5
+ const { promoteCauseCode } = require('../network-errors');
5
6
 
6
7
  /**
7
8
  * Performs a token-endpoint request, and says which endpoint failed when the request never got there.
@@ -35,9 +36,7 @@ async function fetchTokenRequest(url, options, context, route) {
35
36
  } catch (err) {
36
37
  // The cause carries the actionable code. Promoted onto the error itself so the connectError
37
38
  // webhook has something to report - the transient check consults both, so no verdict changes.
38
- if (!err.code && err.cause && err.cause.code) {
39
- err.code = err.cause.code;
40
- }
39
+ promoteCauseCode(err);
41
40
 
42
41
  err.tokenRequest = Object.assign({ url, method: (options && options.method) || 'get', error: err.message, errorCode: err.code }, context);
43
42
  err.message = `Token request to ${url} failed${err.code ? ` [${err.code}]` : ''}: ${(err.cause && err.cause.message) || err.message}`;
package/lib/sub-script.js CHANGED
@@ -12,13 +12,16 @@ const crypto = require('crypto');
12
12
  const vm = require('vm');
13
13
  const logger = require('./logger');
14
14
  const settings = require('./settings');
15
- const { httpAgent } = require('./tools');
15
+ const { httpAgent, LRUCache } = require('./tools');
16
16
 
17
17
  const { SUBSCRIPT_RUNTIME_TIMEOUT } = require('./consts');
18
18
 
19
19
  const { fetch: fetchCmd } = require('undici');
20
20
 
21
- const fnCache = new Map();
21
+ // Every edit of a filter, map or pre-processing script compiles a new source, in every thread that
22
+ // runs it, so the cache is bounded. An evicted script is only recompiled: a SubScript keeps its own
23
+ // reference to what it compiled
24
+ const fnCache = new LRUCache(256);
22
25
 
23
26
  function getCompiledScript(code) {
24
27
  let fnHash = crypto.createHash('sha256').update(code).digest('hex');
@@ -124,4 +127,8 @@ class SubScript {
124
127
  }
125
128
  }
126
129
 
127
- module.exports = { SubScript };
130
+ module.exports = {
131
+ SubScript,
132
+ // Exported for testing
133
+ fnCache
134
+ };
package/lib/tools.js CHANGED
@@ -464,12 +464,26 @@ class LRUCache extends Map {
464
464
  this.maxSize = maxSize;
465
465
  }
466
466
 
467
+ // A hit moves the entry to the end of the insertion order, so eviction takes the least
468
+ // recently used entry rather than the oldest one
469
+ get(key) {
470
+ if (!super.has(key)) {
471
+ return undefined;
472
+ }
473
+ const value = super.get(key);
474
+ super.delete(key);
475
+ super.set(key, value);
476
+ return value;
477
+ }
478
+
467
479
  set(key, value) {
468
- if (this.size >= this.maxSize) {
480
+ if (super.has(key)) {
481
+ super.delete(key);
482
+ } else if (this.size >= this.maxSize) {
469
483
  const firstKey = this.keys().next().value;
470
484
  this.delete(firstKey);
471
485
  }
472
- super.set(key, value);
486
+ return super.set(key, value);
473
487
  }
474
488
  }
475
489
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emailengine-app",
3
- "version": "2.83.1",
3
+ "version": "2.83.2",
4
4
  "private": false,
5
5
  "productTitle": "EmailEngine",
6
6
  "description": "Email Sync Engine",
@@ -26,7 +26,7 @@
26
26
  "build-dist": "pkg --compress Brotli package.json && npm install && node winconf.js",
27
27
  "build-dist-fast": "pkg --debug package.json && npm install && node winconf.js",
28
28
  "licenses": "node list-generate.js",
29
- "gettext": "find ./views -name \"*.hbs\" -print0 | xargs -0 xgettext-template -L Handlebars --force-po -o - > translations/messages.pot.tmp && mv translations/messages.pot.tmp translations/messages.pot && node gettext-extract.js",
29
+ "gettext": "find ./views -name \"*.hbs\" -print0 | xargs -0 xgettext-template -L Handlebars --force-po -o - > translations/messages.pot.tmp && node gettext-extract.js",
30
30
  "update": "rm -rf node_modules package-lock.json && ncu -u && npm install && ./copy-static-files.sh && npm run licenses && npm run gettext",
31
31
  "test-gmail-api": "node lib/email-client/gmail-client.js --dbs.redis=redis://127.0.0.1/11",
32
32
  "format": "prettier --write 'lib/**/*.js' 'workers/**/*.js' 'test/**/*.js' '*.js'",
@@ -70,13 +70,13 @@
70
70
  "@postalsys/gettext": "4.1.1",
71
71
  "@postalsys/joi-messages": "1.3.1",
72
72
  "@postalsys/templates": "2.0.4",
73
- "@sentry/node-core": "10.76.1",
73
+ "@sentry/node-core": "10.76.2",
74
74
  "@simplewebauthn/server": "14.0.3",
75
75
  "@zone-eu/mailsplit": "5.4.20",
76
76
  "@zone-eu/wild-config": "1.7.7",
77
- "bullmq": "6.3.11",
77
+ "bullmq": "6.3.12",
78
78
  "compare-versions": "6.1.1",
79
- "dotenv": "18.0.6",
79
+ "dotenv": "18.0.7",
80
80
  "encoding-japanese": "2.4.0",
81
81
  "exponential-backoff": "3.1.3",
82
82
  "fast-xml-parser": "5.11.2",
@@ -87,7 +87,7 @@
87
87
  "html-to-text": "10.0.1",
88
88
  "ical.js": "2.2.1",
89
89
  "iconv-lite": "0.7.3",
90
- "imapflow": "2.3.0",
90
+ "imapflow": "2.4.0",
91
91
  "ioredfour": "1.4.4",
92
92
  "ioredis": "6.0.0",
93
93
  "ipaddr.js": "2.5.0",
@@ -95,19 +95,19 @@
95
95
  "libbase64": "1.3.2",
96
96
  "libmime": "5.4.7",
97
97
  "libqp": "2.1.2",
98
- "mailparser": "3.9.37",
98
+ "mailparser": "3.9.38",
99
99
  "marked": "15.0.12",
100
100
  "minimist": "1.2.8",
101
101
  "murmurhash": "2.0.1",
102
102
  "nanoid": "3.3.20",
103
- "nodemailer": "10.0.16",
103
+ "nodemailer": "10.1.0",
104
104
  "pino": "10.3.1",
105
105
  "prom-client": "15.1.3",
106
106
  "psl": "1.15.0",
107
107
  "pubface": "1.1.18",
108
108
  "punycode.js": "2.3.1",
109
109
  "qrcode-generator": "2.0.4",
110
- "smtp-server": "3.19.18",
110
+ "smtp-server": "3.19.21",
111
111
  "socks": "2.8.10",
112
112
  "timezones-list": "3.1.0",
113
113
  "undici": "7.30.0"
@@ -128,7 +128,7 @@
128
128
  "eslint": "10.12.0",
129
129
  "flyonui": "2.4.1",
130
130
  "pino-pretty": "13.2.0",
131
- "prettier": "3.9.9",
131
+ "prettier": "3.9.10",
132
132
  "resedit": "3.1.0",
133
133
  "spdx-satisfies": "6.0.0",
134
134
  "supertest": "7.3.1",