emailengine-app 2.72.1 → 2.72.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.72.2](https://github.com/postalsys/emailengine/compare/v2.72.1...v2.72.2) (2026-06-26)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * reconnect error-state accounts after OAuth re-auth via create() ([77cee1e](https://github.com/postalsys/emailengine/commit/77cee1eb1916fbd6582cfcf931023d53f6cc8abf))
9
+ * resume sync after OAuth re-auth and enable Run sync for Gmail/Outlook ([f3bef82](https://github.com/postalsys/emailengine/commit/f3bef823ee90cd6fcf90b95fadcb6f2730657441))
10
+ * update dependencies (libmime 5.4.0, mailparser 3.9.12, imapflow 1.4.3, email-ai-tools 1.13.9) ([8c999d8](https://github.com/postalsys/emailengine/commit/8c999d8c5e6b550c6d3a28fd4ec118c48d095901))
11
+
3
12
  ## [2.72.1](https://github.com/postalsys/emailengine/compare/v2.72.0...v2.72.1) (2026-06-19)
4
13
 
5
14
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "creationTime": "2026-06-19T14:46:02.000000",
2
+ "creationTime": "2026-06-25T14:46:07.000000",
3
3
  "prefixes": [
4
4
  {
5
5
  "ipv6Prefix": "2001:4860:4801:2008::/64"
@@ -17,6 +17,10 @@ const ACCOUNT_STATES = {
17
17
  // States that indicate the account is operational
18
18
  const VALID_STATES = [ACCOUNT_STATES.CONNECTED, ACCOUNT_STATES.CONNECTING, ACCOUNT_STATES.SYNCING];
19
19
 
20
+ // Non-operational error states. Re-authorizing / re-saving credentials for an account in one of
21
+ // these states should trigger a full reconnect so syncing resumes.
22
+ const ERROR_STATES = [ACCOUNT_STATES.AUTHENTICATION_ERROR, ACCOUNT_STATES.CONNECT_ERROR];
23
+
20
24
  // States that bypass runIndex checking
21
25
  const BYPASS_RUN_INDEX_STATES = [ACCOUNT_STATES.INIT, ACCOUNT_STATES.UNSET];
22
26
 
@@ -225,6 +229,7 @@ module.exports = {
225
229
  // Constants
226
230
  ACCOUNT_STATES,
227
231
  VALID_STATES,
232
+ ERROR_STATES,
228
233
  BYPASS_RUN_INDEX_STATES,
229
234
 
230
235
  // State calculation
package/lib/account.js CHANGED
@@ -29,7 +29,7 @@ const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 16);
29
29
  const { REDIS_PREFIX, ACCOUNT_DELETED_NOTIFY, MAILBOX_HASH } = require('./consts');
30
30
  const { mimeHtml } = require('@postalsys/email-text-tools');
31
31
  const { checkAccountScopes: checkScopes } = require('./oauth/scope-checker');
32
- const { ACCOUNT_STATES, calculateEffectiveState, validateAccountState, getDisplayState, formatLastError } = require('./account/account-state');
32
+ const { ACCOUNT_STATES, ERROR_STATES, calculateEffectiveState, validateAccountState, getDisplayState, formatLastError } = require('./account/account-state');
33
33
 
34
34
  // Shared Lock instance to prevent Redis connection leak (one subscriber per Lock instance)
35
35
  // Previously, each Account created its own Lock, causing ~30k connections with 30k accounts
@@ -848,6 +848,39 @@ class Account {
848
848
  });
849
849
  }
850
850
 
851
+ // When OAuth2 credentials are re-authorized for an account that is currently in a
852
+ // non-operational error state, trigger a full reconnect so syncing resumes without a
853
+ // manual "Reconnect". Gated on the error state and on the credentials actually
854
+ // changing, so routine token renewal - which runs while the account is connected and
855
+ // re-saves oauth2 hourly - never triggers a reconnect. During an actual reconnect the
856
+ // state is set to "connecting" before tokens are fetched, so this cannot loop.
857
+ if (
858
+ !reconnectRequested &&
859
+ 'oauth2' in accountData &&
860
+ accountData.oauth2?.accessToken &&
861
+ (accountData.oauth2.accessToken !== oldAccountData.oauth2?.accessToken ||
862
+ accountData.oauth2.refreshToken !== oldAccountData.oauth2?.refreshToken) &&
863
+ ERROR_STATES.includes(oldAccountData.state)
864
+ ) {
865
+ this.logger.info({
866
+ msg: 'OAuth2 credentials re-authorized for account in error state, requesting reconnect',
867
+ account: this.account,
868
+ state: oldAccountData.state
869
+ });
870
+ try {
871
+ await this.call({
872
+ cmd: 'reconnect',
873
+ account: this.account,
874
+ timeout: this.timeout
875
+ });
876
+ } catch (err) {
877
+ // Credentials are already persisted; a failed reconnect dispatch must not turn a
878
+ // saved credential update into an error response. The next periodic reconnect
879
+ // recovers. Log and continue.
880
+ this.logger.error({ msg: 'Failed to request reconnect after credential update', account: this.account, state: oldAccountData.state, err });
881
+ }
882
+ }
883
+
851
884
  return {
852
885
  account: this.account
853
886
  };
@@ -961,11 +994,42 @@ class Account {
961
994
  // ignore
962
995
  }
963
996
 
964
- await this.call({
965
- cmd: 'update',
966
- account: this.account,
967
- timeout: this.timeout
968
- });
997
+ const oldState = result[0][1].state;
998
+ if (ERROR_STATES.includes(oldState)) {
999
+ // Re-registration of an account that was in a non-operational error state (the
1000
+ // interactive OAuth re-auth redirect and the IMAP re-credentials form both land here).
1001
+ // cmd:'update' only calls reconnect() on the existing client instance, which cannot
1002
+ // switch client type (IMAP to API or back) and is a no-op against a torn-down
1003
+ // connection, so syncing would not resume without a manual "Reconnect". cmd:'reconnect'
1004
+ // closes and re-assigns a fresh client of the correct provider type. assignConnection
1005
+ // sets the state to "connecting" before any network work, so this cannot loop.
1006
+ // Gated on the error state alone (unlike update(), which also requires the oauth2
1007
+ // token to have changed): create() runs only on explicit re-registration, never on
1008
+ // routine token renewal, so there is no healthy-account churn to suppress here.
1009
+ this.logger.info({
1010
+ msg: 'Account re-created while in error state, requesting full reconnect',
1011
+ account: this.account,
1012
+ state: oldState
1013
+ });
1014
+ try {
1015
+ await this.call({
1016
+ cmd: 'reconnect',
1017
+ account: this.account,
1018
+ timeout: this.timeout
1019
+ });
1020
+ } catch (err) {
1021
+ // Credentials are already persisted (the hmset above returned OK). A failed
1022
+ // reconnect dispatch must not fail the re-auth or surface a 500; the next periodic
1023
+ // reconnect or a manual Reconnect will recover. Log and continue.
1024
+ this.logger.error({ msg: 'Failed to request reconnect after re-auth', account: this.account, state: oldState, err });
1025
+ }
1026
+ } else {
1027
+ await this.call({
1028
+ cmd: 'update',
1029
+ account: this.account,
1030
+ timeout: this.timeout
1031
+ });
1032
+ }
969
1033
  } else {
970
1034
  state = 'new';
971
1035
  await this.call({
@@ -777,3 +777,9 @@ async function autodetectImapSettings(email, gt) {
777
777
  }
778
778
 
779
779
  module.exports.autodetectImapSettings = autodetectImapSettings;
780
+
781
+ // Exposed for unit testing of the pure parsing/matching helpers. These have no
782
+ // network side effects, so they can be exercised directly without mocking DNS or HTTP.
783
+ module.exports.processAutoconfigFile = processAutoconfigFile;
784
+ module.exports.getAppPassword = getAppPassword;
785
+ module.exports.escapeXml = escapeXml;
@@ -2,7 +2,7 @@
2
2
 
3
3
  const { Account } = require('../account');
4
4
  const { oauth2Apps } = require('../oauth2-apps');
5
- const { checkAccountScopes } = require('../oauth/scope-checker');
5
+ const { checkAccountScopes, isSendOnlyByScopes } = require('../oauth/scope-checker');
6
6
  const getSecret = require('../get-secret');
7
7
  const msgpack = require('msgpack5')();
8
8
  const addressparser = require('nodemailer/lib/addressparser');
@@ -509,6 +509,46 @@ class GmailClient extends BaseClient {
509
509
  return null;
510
510
  }
511
511
 
512
+ /**
513
+ * Performs an on-demand catch-up sync. Triggered by the "Run sync" action
514
+ * (PUT /v1/account/{account}/sync). Gmail has no folder-walk sync like IMAP; instead it
515
+ * reads the current history checkpoint and processes anything that arrived since the last
516
+ * stored historyId, reusing the same code path as live Pub/Sub notifications.
517
+ */
518
+ async syncMailboxes() {
519
+ if (this.closed) {
520
+ return null;
521
+ }
522
+
523
+ let accountData = await this.accountObject.loadAccountData(this.account, false);
524
+
525
+ // Send-only accounts have no mailbox to sync
526
+ if (isSendOnlyByScopes('gmail', accountData)) {
527
+ return null;
528
+ }
529
+
530
+ let profileRes;
531
+ try {
532
+ profileRes = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/profile`);
533
+ } catch (err) {
534
+ this.logger.error({ msg: 'Failed to fetch profile for manual sync', account: this.account, err });
535
+ return null;
536
+ }
537
+
538
+ let currentHistoryId = Number(profileRes?.historyId) || null;
539
+ // googleHistoryId is already loaded with the account data; reuse it (matches init())
540
+ let storedHistoryId = Number(accountData.googleHistoryId) || null;
541
+
542
+ if (currentHistoryId && storedHistoryId && currentHistoryId > storedHistoryId) {
543
+ // Reuse triggerSync so the processingHistory guard prevents overlap with a
544
+ // concurrent Pub/Sub-driven sync. triggerSync is fire-and-forget but catches its
545
+ // own errors, so it cannot produce an unhandled rejection.
546
+ this.triggerSync(storedHistoryId, currentHistoryId);
547
+ }
548
+
549
+ return true;
550
+ }
551
+
512
552
  async currentState() {
513
553
  return (await this.redis.hget(this.getAccountKey(), 'state')) || 'disconnected';
514
554
  }
@@ -409,6 +409,28 @@ class OutlookClient extends BaseClient {
409
409
  return null;
410
410
  }
411
411
 
412
+ /**
413
+ * Performs an on-demand catch-up sync. Triggered by the "Run sync" action
414
+ * (PUT /v1/account/{account}/sync). Outlook syncs via webhook subscriptions and delta
415
+ * queries rather than a folder walk, so a manual sync refreshes the folder cache and runs
416
+ * the missed-notification recovery (with the cooldown bypassed for this explicit action).
417
+ */
418
+ async syncMailboxes() {
419
+ if (this.closed) {
420
+ return null;
421
+ }
422
+
423
+ try {
424
+ await this.renewMailboxFolderCache();
425
+ } catch (err) {
426
+ this.logger.error({ msg: 'Failed to refresh folder cache for manual sync', account: this.account, err });
427
+ }
428
+
429
+ await this.syncMissedMessages({ force: true });
430
+
431
+ return true;
432
+ }
433
+
412
434
  /**
413
435
  * Get the current connection state from Redis
414
436
  */
@@ -3968,13 +3990,19 @@ class OutlookClient extends BaseClient {
3968
3990
  * Queries recent messages and processes any not already seen via normal webhooks.
3969
3991
  * @returns {boolean} Whether recovery ran (false if debounced)
3970
3992
  */
3971
- async syncMissedMessages() {
3972
- // Debounce: skip if recovery already ran recently
3993
+ async syncMissedMessages(opts) {
3994
+ let { force } = opts || {};
3995
+
3996
+ // Debounce: skip if recovery already ran recently. A manual "Run sync" passes
3997
+ // force:true to bypass the cooldown; duplicate suppression is still handled per
3998
+ // message by rollingBucketLock below, so forcing cannot re-emit seen messages.
3973
3999
  const cooldownKey = `${REDIS_PREFIX}iam:${this.account}:missedRecovery`;
3974
- const acquired = await this.redis.set(cooldownKey, '1', 'EX', 300, 'NX');
3975
- if (!acquired) {
3976
- this.logger.debug({ msg: 'Missed notification recovery skipped (cooldown active)', account: this.account });
3977
- return false;
4000
+ if (!force) {
4001
+ const acquired = await this.redis.set(cooldownKey, '1', 'EX', 300, 'NX');
4002
+ if (!acquired) {
4003
+ this.logger.debug({ msg: 'Missed notification recovery skipped (cooldown active)', account: this.account });
4004
+ return false;
4005
+ }
3978
4006
  }
3979
4007
 
3980
4008
  // Determine lookback window
@@ -190,7 +190,8 @@ module.exports = {
190
190
  getMailTransport,
191
191
  closePooledConnection,
192
192
  getPoolSize,
193
- // Export constants for testing
193
+ // Exported for testing
194
+ generatePoolKey,
194
195
  SMTP_POOL_MAX_IDLE,
195
196
  SMTP_POOL_CLEANUP_INTERVAL
196
197
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emailengine-app",
3
- "version": "2.72.1",
3
+ "version": "2.72.2",
4
4
  "private": false,
5
5
  "productTitle": "EmailEngine",
6
6
  "description": "Email Sync Engine",
@@ -62,19 +62,19 @@
62
62
  "@postalsys/bounce-classifier": "3.0.0",
63
63
  "@postalsys/certs": "1.0.15",
64
64
  "@postalsys/ee-client": "1.3.0",
65
- "@postalsys/email-ai-tools": "1.13.8",
66
- "@postalsys/email-text-tools": "2.4.8",
65
+ "@postalsys/email-ai-tools": "1.13.9",
66
+ "@postalsys/email-text-tools": "2.4.9",
67
67
  "@postalsys/gettext": "4.1.1",
68
68
  "@postalsys/joi-messages": "1.0.5",
69
69
  "@postalsys/templates": "2.0.1",
70
- "@sentry/node": "10.59.0",
70
+ "@sentry/node": "10.61.0",
71
71
  "@simplewebauthn/browser": "13.3.0",
72
- "@simplewebauthn/server": "13.3.1",
73
- "@zone-eu/mailsplit": "5.4.12",
72
+ "@simplewebauthn/server": "13.3.2",
73
+ "@zone-eu/mailsplit": "5.4.13",
74
74
  "@zone-eu/wild-config": "1.7.5",
75
75
  "ace-builds": "1.44.0",
76
76
  "base32.js": "0.1.0",
77
- "bullmq": "5.79.0",
77
+ "bullmq": "5.79.1",
78
78
  "compare-versions": "6.1.1",
79
79
  "dotenv": "17.4.2",
80
80
  "encoding-japanese": "2.2.0",
@@ -88,22 +88,22 @@
88
88
  "html-to-text": "10.0.0",
89
89
  "ical.js": "1.5.0",
90
90
  "iconv-lite": "0.7.2",
91
- "imapflow": "1.4.2",
91
+ "imapflow": "1.4.3",
92
92
  "ioredfour": "1.4.1",
93
93
  "ioredis": "5.11.1",
94
94
  "ipaddr.js": "2.4.0",
95
95
  "joi": "17.13.4",
96
96
  "jquery": "4.0.0",
97
97
  "libbase64": "1.3.0",
98
- "libmime": "5.3.8",
98
+ "libmime": "5.4.0",
99
99
  "libqp": "2.1.1",
100
100
  "license-checker": "25.0.1",
101
- "mailparser": "3.9.11",
101
+ "mailparser": "3.9.12",
102
102
  "marked": "15.0.12",
103
103
  "minimist": "1.2.8",
104
104
  "msgpack5": "6.0.2",
105
105
  "murmurhash": "2.0.1",
106
- "nanoid": "3.3.13",
106
+ "nanoid": "3.3.15",
107
107
  "nodemailer": "9.0.1",
108
108
  "pino": "10.3.1",
109
109
  "popper.js": "1.16.1",
@@ -122,7 +122,7 @@
122
122
  },
123
123
  "devDependencies": {
124
124
  "@eslint/js": "10.0.1",
125
- "@playwright/test": "1.61.0",
125
+ "@playwright/test": "1.61.1",
126
126
  "acorn": "8.17.0",
127
127
  "acorn-walk": "8.3.5",
128
128
  "chai": "4.5.0",