emailengine-app 2.81.1 → 2.82.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 (208) hide show
  1. package/.ncurc.js +12 -6
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +52 -0
  4. package/Dockerfile +4 -2
  5. package/bin/emailengine.js +28 -18
  6. package/config/default.toml +0 -5
  7. package/config/test.toml +0 -5
  8. package/data/google-crawlers.json +1 -1
  9. package/encrypt.js +70 -42
  10. package/getswagger.sh +0 -4
  11. package/lib/account/account-state.js +2 -110
  12. package/lib/account/bounce-store-sweep.js +63 -0
  13. package/lib/account/expected-identity.js +1 -1
  14. package/lib/account-assignment.js +207 -0
  15. package/lib/account.js +374 -878
  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-routes.js +18 -29
  19. package/lib/api-routes/blocklist-routes.js +2 -2
  20. package/lib/api-routes/bull-board-routes.js +1 -6
  21. package/lib/api-routes/export-routes.js +2 -22
  22. package/lib/api-routes/gateway-routes.js +5 -14
  23. package/lib/api-routes/index.js +1 -13
  24. package/lib/api-routes/mailbox-routes.js +2 -7
  25. package/lib/api-routes/mcp-oauth-routes.js +2 -1
  26. package/lib/api-routes/mcp-routes.js +7 -7
  27. package/lib/api-routes/message-routes.js +44 -236
  28. package/lib/api-routes/mint-bounds.js +110 -0
  29. package/lib/api-routes/oauth2-app-routes.js +23 -32
  30. package/lib/api-routes/operation-impact.js +1 -1
  31. package/lib/api-routes/outbox-routes.js +2 -2
  32. package/lib/api-routes/permission-map.js +1 -7
  33. package/lib/api-routes/route-helpers.js +11 -5
  34. package/lib/api-routes/settings-routes.js +19 -19
  35. package/lib/api-routes/stats-routes.js +1 -1
  36. package/lib/api-routes/submit-routes.js +17 -18
  37. package/lib/api-routes/template-routes.js +4 -9
  38. package/lib/api-routes/token-routes.js +27 -5
  39. package/lib/api-token-scheme.js +71 -0
  40. package/lib/arf-detect.js +41 -7
  41. package/lib/auth-token.js +44 -24
  42. package/lib/autodetect-imap-settings.js +163 -123
  43. package/lib/bounce-detect.js +25 -16
  44. package/lib/calendar-event.js +78 -0
  45. package/lib/consts.js +51 -5
  46. package/lib/create-imap-client.js +24 -0
  47. package/lib/db.js +76 -17
  48. package/lib/delivery-error.js +2 -1
  49. package/lib/egress-fetch.js +54 -3
  50. package/lib/egress-filter.js +11 -0
  51. package/lib/email-client/api-retry.js +147 -0
  52. package/lib/email-client/base-client.js +628 -615
  53. package/lib/email-client/gmail/gmail-api.js +38 -125
  54. package/lib/email-client/gmail/send-request.js +1 -1
  55. package/lib/email-client/gmail-client.js +170 -84
  56. package/lib/email-client/imap/mailbox.js +244 -535
  57. package/lib/email-client/imap/stale-view.js +109 -0
  58. package/lib/email-client/imap/subconnection.js +46 -9
  59. package/lib/email-client/imap/sync-operations.js +5 -37
  60. package/lib/email-client/imap-client.js +188 -64
  61. package/lib/email-client/message-builder.js +9 -14
  62. package/lib/email-client/notification-handler.js +5 -219
  63. package/lib/email-client/outlook/graph-api.js +147 -126
  64. package/lib/email-client/outlook/send-request.js +10 -4
  65. package/lib/email-client/outlook-client.js +519 -617
  66. package/lib/email-client/smtp-pool-manager.js +3 -2
  67. package/lib/encrypt.js +184 -19
  68. package/lib/enum-descriptions.js +1 -2
  69. package/lib/export.js +144 -41
  70. package/lib/gateway.js +20 -6
  71. package/lib/get-raw-email.js +26 -44
  72. package/lib/handlebars-helpers.js +31 -10
  73. package/lib/imap-proxy-auth.js +13 -29
  74. package/lib/imapproxy/imap-core/README.md +1 -0
  75. package/lib/imapproxy/imap-core/lib/handler/imap-parser.js +12 -0
  76. package/lib/imapproxy/imap-core/lib/imap-command.js +74 -10
  77. package/lib/imapproxy/imap-core/lib/imap-connection.js +9 -1
  78. package/lib/imapproxy/imap-core/lib/imap-server.js +112 -26
  79. package/lib/imapproxy/imap-core/lib/imap-stream.js +41 -1
  80. package/lib/imapproxy/imap-core/lib/imap-tools.js +10 -2
  81. package/lib/imapproxy/imap-core/lib/indexer/indexer.js +4 -2
  82. package/lib/imapproxy/imap-server.js +17 -177
  83. package/lib/imapproxy/proxy-handoff.js +235 -0
  84. package/lib/imapproxy/response-error.js +33 -0
  85. package/lib/imapproxy/upstream-client.js +2 -2
  86. package/lib/license-beacon.js +3 -9
  87. package/lib/llm-pre-process.js +182 -43
  88. package/lib/logger.js +1 -1
  89. package/lib/lua/ee-export-queue-add.lua +39 -0
  90. package/lib/lua/ee-get-idempotency.lua +15 -3
  91. package/lib/lua/ee-release-attempts.lua +23 -0
  92. package/lib/lua/ee-reserve-attempts.lua +42 -0
  93. package/lib/lua/h-set-bigger.lua +12 -2
  94. package/lib/lua/s-list-accounts.lua +6 -3
  95. package/lib/mcp/index.js +1 -2
  96. package/lib/mcp/listen.js +221 -18
  97. package/lib/mcp/oauth.js +44 -5
  98. package/lib/mcp/resources.js +8 -2
  99. package/lib/mcp/tools.js +13 -0
  100. package/lib/message-port-stream.js +64 -6
  101. package/lib/msgpack.js +1 -6
  102. package/lib/oauth/external-account-signer.js +16 -13
  103. package/lib/oauth/gmail.js +22 -20
  104. package/lib/oauth/outlook.js +19 -16
  105. package/lib/oauth/pubsub/google.js +38 -2
  106. package/lib/oauth/scope-checker.js +4 -7
  107. package/lib/oauth/verify-app.js +2 -2
  108. package/lib/oauth2-apps.js +139 -44
  109. package/lib/openapi/build-document.js +1 -1
  110. package/lib/openapi/joi-schema.js +78 -9
  111. package/lib/parse-message.js +21 -0
  112. package/lib/payload-examples-webhooks.json +4 -7
  113. package/lib/pbkdf2-phc.js +101 -0
  114. package/lib/privileged-settings.js +0 -14
  115. package/lib/rate-limit.js +54 -15
  116. package/lib/redis-scan-delete.js +56 -29
  117. package/lib/redis-url.js +3 -1
  118. package/lib/respawn-backoff.js +5 -4
  119. package/lib/response-stream.js +25 -4
  120. package/lib/rewrite-text-nodes.js +27 -40
  121. package/lib/routes-ui.js +0 -5
  122. package/lib/schemas.js +129 -262
  123. package/lib/sentry.js +21 -5
  124. package/lib/settings.js +58 -59
  125. package/lib/smtp-auth.js +6 -2
  126. package/lib/smtp-message-processor.js +28 -7
  127. package/lib/sso.js +7 -3
  128. package/lib/stream-encrypt.js +14 -13
  129. package/lib/swagger-options.js +3 -1
  130. package/lib/tls/provision.js +69 -3
  131. package/lib/tls/store.js +14 -1
  132. package/lib/tools.js +113 -52
  133. package/lib/totp.js +16 -1
  134. package/lib/ui-routes/account-routes.js +27 -63
  135. package/lib/ui-routes/admin-config-routes.js +111 -134
  136. package/lib/ui-routes/admin-entities-routes.js +101 -191
  137. package/lib/ui-routes/ai-options.js +104 -0
  138. package/lib/ui-routes/auth-routes.js +98 -18
  139. package/lib/ui-routes/export-routes.js +3 -23
  140. package/lib/ui-routes/internals-routes.js +12 -9
  141. package/lib/ui-routes/network-config-routes.js +8 -18
  142. package/lib/ui-routes/oauth-config-routes.js +80 -214
  143. package/lib/ui-routes/route-helpers.js +40 -44
  144. package/lib/ui-routes/unsubscribe-routes.js +2 -2
  145. package/lib/utils/decode-headers.js +32 -5
  146. package/lib/utils/header-map.js +62 -1
  147. package/lib/utils/network.js +2 -0
  148. package/lib/web-safe-html.js +77 -4
  149. package/lib/webhook-request.js +9 -1
  150. package/lib/webhook-routing.js +27 -1
  151. package/lib/webhooks.js +120 -43
  152. package/lib/worker-rpc-error.js +43 -2
  153. package/list-generate.js +13 -2
  154. package/package.json +38 -46
  155. package/sbom.json +1 -1
  156. package/scripts/refresh-test-tokens.js +42 -14
  157. package/server.js +340 -336
  158. package/static/css/flyonui.css +1 -1
  159. package/static/css/src/app.tailwind.css +7 -8
  160. package/static/js/ee-client.js +145 -70
  161. package/static/js/ui.js +350 -154
  162. package/static/licenses.html +460 -1861
  163. package/translations/messages.pot +99 -99
  164. package/views/accounts/browse.hbs +10 -4
  165. package/views/config/ai.hbs +49 -41
  166. package/views/config/oauth/app.hbs +2 -1
  167. package/views/dashboard.hbs +18 -16
  168. package/views/layout/app.hbs +0 -22
  169. package/views/layout/login.hbs +2 -1
  170. package/views/partials/filter_editor_js.hbs +4 -3
  171. package/views/partials/side_menu.hbs +0 -4
  172. package/views/partials/ui/account-picker.hbs +6 -32
  173. package/views/partials/ui/model-picker.hbs +31 -0
  174. package/views/partials/ui/pagination.hbs +6 -1
  175. package/views/partials/ui/picker.hbs +47 -0
  176. package/views/unsubscribe.hbs +1 -1
  177. package/workers/api.js +33 -23
  178. package/workers/export.js +55 -60
  179. package/workers/imap.js +134 -47
  180. package/workers/submit.js +3 -1
  181. package/workers/webhooks.js +46 -27
  182. package/.githooks/pre-commit +0 -80
  183. package/.github/CODE_OF_CONDUCT.md +0 -76
  184. package/.github/contributing.md +0 -17
  185. package/.github/dependabot.yml +0 -22
  186. package/.github/docker-smoke-test.sh +0 -61
  187. package/.github/workflows/deploy.yml +0 -215
  188. package/.github/workflows/e2e.yml +0 -55
  189. package/.github/workflows/release.yaml +0 -217
  190. package/.github/workflows/test.yml +0 -307
  191. package/lib/api-routes/chat-routes.js +0 -535
  192. package/lib/append-list.js +0 -63
  193. package/lib/document-store.js +0 -75
  194. package/lib/es.js +0 -757
  195. package/lib/generate-text-preview.js +0 -61
  196. package/lib/imapproxy/imap-core/memory-notifier.js +0 -129
  197. package/lib/lua/h-push.lua +0 -32
  198. package/lib/pre-process.js +0 -196
  199. package/lib/threads.js +0 -155
  200. package/lib/ui-routes/document-store-routes.js +0 -806
  201. package/playwright.config.js +0 -47
  202. package/views/config/document-store/chat.hbs +0 -286
  203. package/views/config/document-store/index.hbs +0 -146
  204. package/views/config/document-store/mappings/index.hbs +0 -108
  205. package/views/config/document-store/mappings/new.hbs +0 -68
  206. package/views/config/document-store/pre-processing/index.hbs +0 -103
  207. package/views/partials/document_store_header.hbs +0 -30
  208. package/workers/documents.js +0 -1128
@@ -0,0 +1,207 @@
1
+ 'use strict';
2
+
3
+ // Bookkeeping shared by assignAccounts() and the IMAP worker exit handler in server.js, kept apart
4
+ // so a worker exiting in the middle of an assignment pass can be tested without the main thread.
5
+ // The exit handler runs while a pass is awaiting a worker, so the pass cannot assume a worker it
6
+ // picked, or that worker's account set, still exists.
7
+
8
+ /**
9
+ * The least loaded running worker below `targetPerWorker`, else the least loaded running one
10
+ * @param {Set<Object>} availableWorkers
11
+ * @param {Map<Object, number>} workerLoadMap - worker -> accounts counted in this pass
12
+ * @param {number} targetPerWorker
13
+ * @returns {Object|undefined}
14
+ */
15
+ function pickLeastLoadedWorker(availableWorkers, workerLoadMap, targetPerWorker) {
16
+ const byLoad = Array.from(availableWorkers).sort((a, b) => (workerLoadMap.get(a) || 0) - (workerLoadMap.get(b) || 0));
17
+ return byLoad.find(worker => (workerLoadMap.get(worker) || 0) < targetPerWorker) || byLoad[0];
18
+ }
19
+
20
+ /**
21
+ * Undoes the bookkeeping for an account whose `assign` call failed. The worker's account set is
22
+ * already gone when the worker exited while the call was in flight
23
+ * @param {Object} state - `workerAssigned`, `assigned` and `workerLoadMap`
24
+ * @param {string} account
25
+ * @param {Object} worker
26
+ * @param {boolean} countedLoad - Whether the account was counted in workerLoadMap
27
+ */
28
+ function rollbackAssignment({ workerAssigned, assigned, workerLoadMap }, account, worker, countedLoad) {
29
+ workerAssigned.get(worker)?.delete(account);
30
+ assigned.delete(account);
31
+ if (countedLoad) {
32
+ workerLoadMap.set(worker, workerLoadMap.get(worker) - 1);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Moves the accounts of an exited worker back to unassigned
38
+ * @param {Object} state - `workerAssigned`, `assigned` and `unassigned`
39
+ * @param {Object} worker
40
+ * @returns {Set<string>|null} The accounts released, null when the worker had none tracked
41
+ */
42
+ function releaseWorkerAccounts({ workerAssigned, assigned, unassigned }, worker) {
43
+ const accounts = workerAssigned.get(worker);
44
+ if (!accounts) {
45
+ return null;
46
+ }
47
+ workerAssigned.delete(worker);
48
+ for (const account of accounts) {
49
+ assigned.delete(account);
50
+ unassigned.add(account);
51
+ }
52
+ return accounts;
53
+ }
54
+
55
+ /**
56
+ * Returns the accounts that failed in a pass to unassigned, except the ones assigned since: the
57
+ * exit handler requeues an account whose worker exited mid-call, and the same pass can pick it up
58
+ * again from there
59
+ * @param {Object} state - `assigned` and `unassigned`
60
+ * @param {string[]} failedAccounts
61
+ */
62
+ function requeueFailedAccounts({ assigned, unassigned }, failedAccounts) {
63
+ for (const account of failedAccounts) {
64
+ if (!assigned.has(account)) {
65
+ unassigned.add(account);
66
+ }
67
+ }
68
+ }
69
+
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
+ };