emailengine-app 2.81.2 → 2.82.1

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 (226) hide show
  1. package/.ncurc.js +33 -10
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +71 -0
  4. package/DOCKER_DEPLOYMENT.md +13 -12
  5. package/Dockerfile +2 -0
  6. package/bin/emailengine.js +105 -25
  7. package/config/default.toml +0 -5
  8. package/config/test.toml +0 -5
  9. package/data/google-crawlers.json +1 -1
  10. package/encrypt.js +70 -42
  11. package/getswagger.sh +0 -4
  12. package/lib/account/account-state.js +2 -110
  13. package/lib/account/expected-identity.js +1 -1
  14. package/lib/account-assignment.js +138 -1
  15. package/lib/account.js +289 -869
  16. package/lib/api-reference/index.js +2 -3
  17. package/lib/api-reference/schema-tree.js +53 -3
  18. package/lib/api-routes/account-binding.js +58 -0
  19. package/lib/api-routes/account-routes.js +65 -37
  20. package/lib/api-routes/behavior-notes.js +4 -0
  21. package/lib/api-routes/blocklist-routes.js +2 -2
  22. package/lib/api-routes/bull-board-routes.js +1 -6
  23. package/lib/api-routes/export-routes.js +2 -22
  24. package/lib/api-routes/gateway-routes.js +15 -16
  25. package/lib/api-routes/index.js +1 -13
  26. package/lib/api-routes/mailbox-routes.js +2 -7
  27. package/lib/api-routes/mcp-oauth-routes.js +2 -1
  28. package/lib/api-routes/mcp-routes.js +7 -7
  29. package/lib/api-routes/message-routes.js +50 -238
  30. package/lib/api-routes/mint-bounds.js +110 -0
  31. package/lib/api-routes/oauth2-app-routes.js +23 -32
  32. package/lib/api-routes/operation-impact.js +1 -1
  33. package/lib/api-routes/outbox-routes.js +3 -3
  34. package/lib/api-routes/permission-map.js +1 -7
  35. package/lib/api-routes/route-helpers.js +11 -5
  36. package/lib/api-routes/settings-routes.js +20 -21
  37. package/lib/api-routes/stats-routes.js +1 -1
  38. package/lib/api-routes/submit-routes.js +17 -18
  39. package/lib/api-routes/template-routes.js +4 -9
  40. package/lib/api-routes/token-routes.js +28 -6
  41. package/lib/api-token-scheme.js +71 -0
  42. package/lib/arf-detect.js +33 -6
  43. package/lib/auth-token.js +44 -24
  44. package/lib/autodetect-imap-settings.js +305 -123
  45. package/lib/bounce-detect.js +25 -16
  46. package/lib/calendar-event.js +78 -0
  47. package/lib/consts.js +54 -5
  48. package/lib/create-imap-client.js +24 -0
  49. package/lib/db.js +77 -30
  50. package/lib/delivery-error.js +70 -2
  51. package/lib/egress-fetch.js +54 -3
  52. package/lib/egress-filter.js +11 -0
  53. package/lib/email-client/api-retry.js +147 -0
  54. package/lib/email-client/base-client.js +383 -536
  55. package/lib/email-client/credential-errors.js +21 -5
  56. package/lib/email-client/gmail/gmail-api.js +38 -125
  57. package/lib/email-client/gmail/send-request.js +1 -1
  58. package/lib/email-client/gmail-client.js +77 -51
  59. package/lib/email-client/imap/mailbox.js +158 -324
  60. package/lib/email-client/imap/stale-view.js +109 -0
  61. package/lib/email-client/imap/subconnection.js +46 -9
  62. package/lib/email-client/imap/sync-operations.js +5 -37
  63. package/lib/email-client/imap-client.js +190 -65
  64. package/lib/email-client/message-builder.js +50 -25
  65. package/lib/email-client/notification-handler.js +5 -219
  66. package/lib/email-client/outlook/graph-api.js +147 -126
  67. package/lib/email-client/outlook/send-request.js +10 -4
  68. package/lib/email-client/outlook-client.js +389 -463
  69. package/lib/email-client/smtp-pool-manager.js +3 -2
  70. package/lib/encrypt.js +184 -19
  71. package/lib/enum-descriptions.js +6 -3
  72. package/lib/export.js +141 -41
  73. package/lib/gateway.js +25 -6
  74. package/lib/get-raw-email.js +26 -44
  75. package/lib/get-secret.js +1 -21
  76. package/lib/handlebars-helpers.js +31 -10
  77. package/lib/imap-proxy-auth.js +13 -29
  78. package/lib/imapproxy/imap-core/README.md +1 -0
  79. package/lib/imapproxy/imap-core/lib/handler/imap-parser.js +12 -0
  80. package/lib/imapproxy/imap-core/lib/imap-command.js +74 -10
  81. package/lib/imapproxy/imap-core/lib/imap-connection.js +9 -1
  82. package/lib/imapproxy/imap-core/lib/imap-server.js +112 -26
  83. package/lib/imapproxy/imap-core/lib/imap-stream.js +41 -1
  84. package/lib/imapproxy/imap-core/lib/imap-tools.js +10 -2
  85. package/lib/imapproxy/imap-core/lib/indexer/indexer.js +4 -2
  86. package/lib/imapproxy/imap-server.js +17 -177
  87. package/lib/imapproxy/proxy-handoff.js +235 -0
  88. package/lib/imapproxy/response-error.js +33 -0
  89. package/lib/imapproxy/upstream-client.js +2 -2
  90. package/lib/license-beacon.js +3 -9
  91. package/lib/llm-pre-process.js +182 -43
  92. package/lib/logger.js +1 -1
  93. package/lib/lua/ee-export-queue-add.lua +39 -0
  94. package/lib/lua/ee-get-idempotency.lua +15 -3
  95. package/lib/lua/ee-release-attempts.lua +23 -0
  96. package/lib/lua/ee-reserve-attempts.lua +42 -0
  97. package/lib/lua/h-set-bigger.lua +12 -2
  98. package/lib/lua/s-list-accounts.lua +6 -3
  99. package/lib/mcp/index.js +1 -2
  100. package/lib/mcp/listen.js +221 -18
  101. package/lib/mcp/oauth.js +44 -5
  102. package/lib/mcp/resources.js +8 -2
  103. package/lib/mcp/tools.js +108 -36
  104. package/lib/message-port-stream.js +64 -6
  105. package/lib/oauth/external-account-signer.js +16 -13
  106. package/lib/oauth/gmail.js +64 -32
  107. package/lib/oauth/mail-ru.js +24 -17
  108. package/lib/oauth/outlook.js +53 -40
  109. package/lib/oauth/pubsub/google.js +38 -2
  110. package/lib/oauth/scope-checker.js +39 -8
  111. package/lib/oauth/scope-list.js +55 -0
  112. package/lib/oauth/token-fetch.js +47 -0
  113. package/lib/oauth/verify-app.js +2 -2
  114. package/lib/oauth2-apps.js +154 -90
  115. package/lib/openapi/build-document.js +1 -1
  116. package/lib/openapi/joi-schema.js +78 -9
  117. package/lib/outbox.js +2 -7
  118. package/lib/parse-message.js +21 -0
  119. package/lib/payload-examples-webhooks.json +4 -7
  120. package/lib/pbkdf2-phc.js +101 -0
  121. package/lib/privileged-settings.js +0 -14
  122. package/lib/queue-retention.js +4 -2
  123. package/lib/rate-limit.js +54 -15
  124. package/lib/read-env-value.js +33 -2
  125. package/lib/redis-scan-delete.js +56 -29
  126. package/lib/redis-url.js +3 -1
  127. package/lib/respawn-backoff.js +5 -4
  128. package/lib/response-stream.js +25 -4
  129. package/lib/rewrite-text-nodes.js +27 -40
  130. package/lib/routes-ui.js +0 -5
  131. package/lib/schemas.js +180 -253
  132. package/lib/sentry.js +21 -5
  133. package/lib/settings.js +58 -59
  134. package/lib/smtp-auth.js +6 -2
  135. package/lib/smtp-message-processor.js +28 -7
  136. package/lib/sso.js +7 -3
  137. package/lib/stream-encrypt.js +14 -13
  138. package/lib/swagger-options.js +3 -1
  139. package/lib/tls/provision.js +69 -3
  140. package/lib/tls/store.js +14 -1
  141. package/lib/token-audit-log.js +3 -2
  142. package/lib/tools.js +114 -53
  143. package/lib/totp.js +16 -1
  144. package/lib/ui-routes/account-routes.js +67 -70
  145. package/lib/ui-routes/admin-config-routes.js +111 -134
  146. package/lib/ui-routes/admin-entities-routes.js +101 -191
  147. package/lib/ui-routes/ai-options.js +104 -0
  148. package/lib/ui-routes/auth-routes.js +98 -18
  149. package/lib/ui-routes/export-routes.js +3 -23
  150. package/lib/ui-routes/internals-routes.js +12 -9
  151. package/lib/ui-routes/network-config-routes.js +8 -18
  152. package/lib/ui-routes/oauth-config-routes.js +83 -228
  153. package/lib/ui-routes/route-helpers.js +63 -67
  154. package/lib/ui-routes/special-use-paths.js +33 -0
  155. package/lib/ui-routes/unsubscribe-routes.js +2 -2
  156. package/lib/utils/decode-headers.js +32 -5
  157. package/lib/utils/header-map.js +62 -1
  158. package/lib/utils/network.js +11 -17
  159. package/lib/web-safe-html.js +77 -4
  160. package/lib/webhook-request.js +9 -1
  161. package/lib/webhook-routing.js +27 -1
  162. package/lib/webhooks.js +120 -43
  163. package/lib/worker-rpc-error.js +43 -2
  164. package/list-generate.js +13 -2
  165. package/package.json +38 -46
  166. package/sbom.json +1 -1
  167. package/scripts/refresh-test-tokens.js +42 -14
  168. package/server.js +338 -292
  169. package/setup-production.sh +16 -7
  170. package/static/css/flyonui.css +1 -1
  171. package/static/css/src/app.tailwind.css +7 -8
  172. package/static/js/ee-client.js +145 -70
  173. package/static/js/ui.js +350 -154
  174. package/static/licenses.html +349 -1740
  175. package/translations/messages.pot +129 -111
  176. package/views/account-workspace-error.hbs +30 -0
  177. package/views/accounts/account.hbs +9 -4
  178. package/views/accounts/browse.hbs +10 -4
  179. package/views/accounts/edit.hbs +11 -2
  180. package/views/config/ai.hbs +49 -41
  181. package/views/config/oauth/app.hbs +2 -1
  182. package/views/dashboard.hbs +18 -16
  183. package/views/internals/index.hbs +1 -1
  184. package/views/layout/app.hbs +0 -22
  185. package/views/layout/login.hbs +2 -1
  186. package/views/partials/filter_editor_js.hbs +4 -3
  187. package/views/partials/gmail_api_scopes.hbs +16 -0
  188. package/views/partials/oauth_form.hbs +67 -94
  189. package/views/partials/oauth_pubsub_app_select.hbs +43 -0
  190. package/views/partials/scope_info.hbs +14 -23
  191. package/views/partials/side_menu.hbs +0 -4
  192. package/views/partials/ui/account-picker.hbs +6 -32
  193. package/views/partials/ui/model-picker.hbs +31 -0
  194. package/views/partials/ui/pagination.hbs +6 -1
  195. package/views/partials/ui/picker.hbs +47 -0
  196. package/views/unsubscribe.hbs +1 -1
  197. package/workers/api.js +82 -47
  198. package/workers/export.js +55 -60
  199. package/workers/imap.js +132 -49
  200. package/workers/submit.js +5 -6
  201. package/workers/webhooks.js +46 -27
  202. package/.githooks/pre-commit +0 -80
  203. package/.github/CODE_OF_CONDUCT.md +0 -76
  204. package/.github/contributing.md +0 -17
  205. package/.github/dependabot.yml +0 -22
  206. package/.github/docker-smoke-test.sh +0 -61
  207. package/.github/workflows/deploy.yml +0 -215
  208. package/.github/workflows/e2e.yml +0 -55
  209. package/.github/workflows/release.yaml +0 -217
  210. package/.github/workflows/test.yml +0 -307
  211. package/lib/api-routes/chat-routes.js +0 -535
  212. package/lib/document-store.js +0 -75
  213. package/lib/es.js +0 -757
  214. package/lib/generate-text-preview.js +0 -61
  215. package/lib/imapproxy/imap-core/memory-notifier.js +0 -129
  216. package/lib/pre-process.js +0 -196
  217. package/lib/threads.js +0 -155
  218. package/lib/ui-routes/document-store-routes.js +0 -806
  219. package/playwright.config.js +0 -47
  220. package/views/config/document-store/chat.hbs +0 -286
  221. package/views/config/document-store/index.hbs +0 -146
  222. package/views/config/document-store/mappings/index.hbs +0 -108
  223. package/views/config/document-store/mappings/new.hbs +0 -68
  224. package/views/config/document-store/pre-processing/index.hbs +0 -103
  225. package/views/partials/document_store_header.hbs +0 -30
  226. package/workers/documents.js +0 -1128
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const Boom = require('@hapi/boom');
4
- const { REDIS_PREFIX } = require('../consts');
5
4
 
6
5
  // Account state constants
7
6
  const ACCOUNT_STATES = {
@@ -92,102 +91,6 @@ function validateAccountState(accountData) {
92
91
  throw error;
93
92
  }
94
93
 
95
- /**
96
- * Gets the account state from Redis
97
- * @param {Object} redis - Redis client
98
- * @param {string} account - Account ID
99
- * @returns {Promise<string|null>} The current state value
100
- */
101
- async function getAccountState(redis, account) {
102
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
103
- return await redis.hget(accountKey, 'state');
104
- }
105
-
106
- /**
107
- * Sets the account state in Redis
108
- * Only updates if the account exists
109
- * @param {Object} redis - Redis client
110
- * @param {string} account - Account ID
111
- * @param {string} state - New state value
112
- * @returns {Promise<boolean>} True if state was set
113
- */
114
- async function setAccountState(redis, account, state) {
115
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
116
- const result = await redis.hSetExists(accountKey, 'state', state);
117
- return result === 1;
118
- }
119
-
120
- /**
121
- * Gets the last error state for an account
122
- * @param {Object} redis - Redis client
123
- * @param {string} account - Account ID
124
- * @returns {Promise<Object|null>} Parsed error state object or null
125
- */
126
- async function getLastErrorState(redis, account) {
127
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
128
- const errorState = await redis.hget(accountKey, 'lastErrorState');
129
-
130
- if (!errorState) {
131
- return null;
132
- }
133
-
134
- try {
135
- return JSON.parse(errorState);
136
- } catch (err) {
137
- return null;
138
- }
139
- }
140
-
141
- /**
142
- * Sets the last error state for an account
143
- * @param {Object} redis - Redis client
144
- * @param {string} account - Account ID
145
- * @param {Object} errorData - Error state data
146
- * @returns {Promise<boolean>} True if error state was set
147
- */
148
- async function setLastErrorState(redis, account, errorData) {
149
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
150
- const result = await redis.hSetExists(accountKey, 'lastErrorState', JSON.stringify(errorData));
151
- return result === 1;
152
- }
153
-
154
- /**
155
- * Clears the last error state for an account
156
- * @param {Object} redis - Redis client
157
- * @param {string} account - Account ID
158
- * @returns {Promise<number>} Number of fields removed
159
- */
160
- async function clearLastErrorState(redis, account) {
161
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
162
- return await redis.hdel(accountKey, 'lastErrorState');
163
- }
164
-
165
- /**
166
- * Gets the connection count for a specific state
167
- * @param {Object} redis - Redis client
168
- * @param {string} account - Account ID
169
- * @param {string} state - State to get count for
170
- * @returns {Promise<number>} Connection count
171
- */
172
- async function getStateCount(redis, account, state) {
173
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
174
- const count = await redis.hget(accountKey, `state:count:${state}`);
175
- return parseInt(count, 10) || 0;
176
- }
177
-
178
- /**
179
- * Resets the connection count for a specific state
180
- * @param {Object} redis - Redis client
181
- * @param {string} account - Account ID
182
- * @param {string} state - State to reset count for
183
- * @returns {Promise<boolean>} True if count was reset
184
- */
185
- async function resetStateCount(redis, account, state) {
186
- const accountKey = `${REDIS_PREFIX}iad:${account}`;
187
- const result = await redis.hset(accountKey, `state:count:${state}`, '0');
188
- return result >= 0;
189
- }
190
-
191
94
  // A stored timestamp is only as good as whatever wrote it: a corrupt or hand-edited record must
192
95
  // degrade to null rather than fail the response schema and 500 the endpoint
193
96
  function isoOrNull(date) {
@@ -298,18 +201,7 @@ module.exports = {
298
201
  validateAccountState,
299
202
  getDisplayState,
300
203
 
301
- // Redis operations
302
- getAccountState,
303
- setAccountState,
304
-
305
- // Error state management
306
- getLastErrorState,
307
- setLastErrorState,
308
- clearLastErrorState,
204
+ // Error state formatting
309
205
  formatLastError,
310
- formatGmailWatch,
311
-
312
- // State counters
313
- getStateCount,
314
- resetStateCount
206
+ formatGmailWatch
315
207
  };
@@ -54,7 +54,7 @@ function matchesExpectedIdentity(expectedEmail, identities) {
54
54
  //
55
55
  // Redis is read lazily, only on that fallback: a pinned link would otherwise pay a round trip for a value
56
56
  // it always discards. Reads the field directly rather than through Account.loadAccountData(), which
57
- // hgetalls and decrypts the whole record - same single-field pattern as getAccountState().
57
+ // hgetalls and decrypts the whole record.
58
58
  // The expectation carried by a pending OAuth2 setup, whichever of the two shapes it arrives in.
59
59
  //
60
60
  // Both are written by the server for an authenticated caller and stored under a random nonce, so they are
@@ -67,4 +67,141 @@ function requeueFailedAccounts({ assigned, unassigned }, failedAccounts) {
67
67
  }
68
68
  }
69
69
 
70
- module.exports = { pickLeastLoadedWorker, rollbackAssignment, releaseWorkerAccounts, requeueFailedAccounts };
70
+ /**
71
+ * Drops every record of a deleted account: from unassigned, from `assigned`, and from the account
72
+ * set of the worker that held it. Leaving it in `assigned` kept a deleted account routed to a
73
+ * worker (possibly an exited one) for the life of the process and inflated the per-worker target
74
+ * of every later assignment pass
75
+ * @param {Object} state - `workerAssigned`, `assigned` and `unassigned` (false before the first load)
76
+ * @param {string} account
77
+ * @returns {Object|null} The worker that held the account, for the cleanup call, or null
78
+ */
79
+ function forgetAccount({ workerAssigned, assigned, unassigned }, account) {
80
+ if (unassigned) {
81
+ unassigned.delete(account);
82
+ }
83
+ if (!assigned.has(account)) {
84
+ return null;
85
+ }
86
+ const worker = assigned.get(account);
87
+ assigned.delete(account);
88
+ const accounts = workerAssigned.get(worker);
89
+ if (accounts) {
90
+ accounts.delete(account);
91
+ if (!accounts.size) {
92
+ workerAssigned.delete(worker);
93
+ }
94
+ }
95
+ return worker;
96
+ }
97
+
98
+ /**
99
+ * Whether an account whose `assign` call failed goes back to unassigned. An account that no longer
100
+ * exists fails deterministically on every pass, so it is dropped instead: either the worker said so
101
+ * (404), or the account is no longer registered, which also covers a delete that raced the pass.
102
+ * A failed membership check keeps the account, a lost retry costs more than a wasted one
103
+ * @param {Error} err - The error the assign call failed with
104
+ * @param {string} account
105
+ * @param {Function} isRegistered - async account -> boolean
106
+ * @returns {Promise<boolean>}
107
+ */
108
+ async function shouldRequeueFailedAssignment(err, account, isRegistered) {
109
+ if (err && err.statusCode === 404) {
110
+ return false;
111
+ }
112
+ try {
113
+ return !!(await isRegistered(account));
114
+ } catch {
115
+ return true;
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Picks accounts to move onto a worker that joined after the fleet was already serving them (a
121
+ * worker that restarted after the failsafe reassignment gave its accounts to the survivors).
122
+ * Takes from the most loaded workers until the newcomer holds its fair share
123
+ * @param {Object} state - `workerAssigned` and `availableWorkers`
124
+ * @param {Object} targetWorker - The worker that joined
125
+ * @returns {Array<{account: string, worker: Object}>} Accounts to move, with their current worker
126
+ */
127
+ function planRebalance({ workerAssigned, availableWorkers }, targetWorker) {
128
+ const loads = new Map();
129
+ let total = 0;
130
+ for (const worker of availableWorkers) {
131
+ const size = workerAssigned.get(worker)?.size || 0;
132
+ loads.set(worker, size);
133
+ total += size;
134
+ }
135
+ if (!loads.has(targetWorker) || loads.size < 2) {
136
+ return [];
137
+ }
138
+
139
+ const share = Math.floor(total / loads.size);
140
+ const taken = new Map();
141
+ const moves = [];
142
+ while (loads.get(targetWorker) < share) {
143
+ let donor = null;
144
+ for (const [worker, size] of loads) {
145
+ if (worker !== targetWorker && (!donor || size > loads.get(donor))) {
146
+ donor = worker;
147
+ }
148
+ }
149
+ // Stop once moving an account would only shift the imbalance to the donor
150
+ if (!donor || loads.get(donor) - 1 < loads.get(targetWorker) + 1) {
151
+ break;
152
+ }
153
+ if (!taken.has(donor)) {
154
+ taken.set(donor, workerAssigned.get(donor).values());
155
+ }
156
+ const next = taken.get(donor).next();
157
+ if (next.done) {
158
+ break;
159
+ }
160
+ moves.push({ account: next.value, worker: donor });
161
+ loads.set(donor, loads.get(donor) - 1);
162
+ loads.set(targetWorker, loads.get(targetWorker) + 1);
163
+ }
164
+ return moves;
165
+ }
166
+
167
+ /**
168
+ * Runs `fn` over `items` with at most `limit` calls in flight and resolves with the results in
169
+ * item order once every call has settled. Rejects with the first failure, like Promise.all, but
170
+ * only after the calls already in flight have settled, so a caller never has calls outstanding
171
+ * it does not know about
172
+ * @param {Array} items
173
+ * @param {number} limit - Calls in flight at once
174
+ * @param {Function} fn - async (item, index) -> result
175
+ * @returns {Promise<Array>}
176
+ */
177
+ async function mapConcurrent(items, limit, fn) {
178
+ const results = new Array(items.length);
179
+ let next = 0;
180
+ let failure = null;
181
+ const runner = async () => {
182
+ while (next < items.length && !failure) {
183
+ const index = next++;
184
+ try {
185
+ results[index] = await fn(items[index], index);
186
+ } catch (err) {
187
+ failure = failure || err;
188
+ }
189
+ }
190
+ };
191
+ await Promise.all(Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, runner));
192
+ if (failure) {
193
+ throw failure;
194
+ }
195
+ return results;
196
+ }
197
+
198
+ module.exports = {
199
+ pickLeastLoadedWorker,
200
+ rollbackAssignment,
201
+ releaseWorkerAccounts,
202
+ requeueFailedAccounts,
203
+ forgetAccount,
204
+ shouldRequeueFailedAssignment,
205
+ planRebalance,
206
+ mapConcurrent
207
+ };