emailengine-app 2.81.2 → 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 (204) hide show
  1. package/.ncurc.js +12 -11
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +27 -0
  4. package/Dockerfile +2 -0
  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/expected-identity.js +1 -1
  13. package/lib/account-assignment.js +138 -1
  14. package/lib/account.js +254 -859
  15. package/lib/api-reference/index.js +2 -3
  16. package/lib/api-reference/schema-tree.js +53 -3
  17. package/lib/api-routes/account-routes.js +18 -29
  18. package/lib/api-routes/blocklist-routes.js +2 -2
  19. package/lib/api-routes/bull-board-routes.js +1 -6
  20. package/lib/api-routes/export-routes.js +2 -22
  21. package/lib/api-routes/gateway-routes.js +5 -14
  22. package/lib/api-routes/index.js +1 -13
  23. package/lib/api-routes/mailbox-routes.js +2 -7
  24. package/lib/api-routes/mcp-oauth-routes.js +2 -1
  25. package/lib/api-routes/mcp-routes.js +7 -7
  26. package/lib/api-routes/message-routes.js +40 -235
  27. package/lib/api-routes/mint-bounds.js +110 -0
  28. package/lib/api-routes/oauth2-app-routes.js +23 -32
  29. package/lib/api-routes/operation-impact.js +1 -1
  30. package/lib/api-routes/outbox-routes.js +2 -2
  31. package/lib/api-routes/permission-map.js +1 -7
  32. package/lib/api-routes/route-helpers.js +11 -5
  33. package/lib/api-routes/settings-routes.js +19 -19
  34. package/lib/api-routes/stats-routes.js +1 -1
  35. package/lib/api-routes/submit-routes.js +17 -18
  36. package/lib/api-routes/template-routes.js +4 -9
  37. package/lib/api-routes/token-routes.js +27 -5
  38. package/lib/api-token-scheme.js +71 -0
  39. package/lib/arf-detect.js +33 -6
  40. package/lib/auth-token.js +44 -24
  41. package/lib/autodetect-imap-settings.js +163 -123
  42. package/lib/bounce-detect.js +25 -16
  43. package/lib/calendar-event.js +78 -0
  44. package/lib/consts.js +42 -5
  45. package/lib/create-imap-client.js +24 -0
  46. package/lib/db.js +76 -11
  47. package/lib/delivery-error.js +2 -1
  48. package/lib/egress-fetch.js +54 -3
  49. package/lib/egress-filter.js +11 -0
  50. package/lib/email-client/api-retry.js +147 -0
  51. package/lib/email-client/base-client.js +377 -523
  52. package/lib/email-client/gmail/gmail-api.js +38 -125
  53. package/lib/email-client/gmail/send-request.js +1 -1
  54. package/lib/email-client/gmail-client.js +76 -50
  55. package/lib/email-client/imap/mailbox.js +158 -324
  56. package/lib/email-client/imap/stale-view.js +109 -0
  57. package/lib/email-client/imap/subconnection.js +46 -9
  58. package/lib/email-client/imap/sync-operations.js +5 -37
  59. package/lib/email-client/imap-client.js +188 -64
  60. package/lib/email-client/message-builder.js +9 -14
  61. package/lib/email-client/notification-handler.js +5 -219
  62. package/lib/email-client/outlook/graph-api.js +147 -126
  63. package/lib/email-client/outlook/send-request.js +10 -4
  64. package/lib/email-client/outlook-client.js +194 -397
  65. package/lib/email-client/smtp-pool-manager.js +3 -2
  66. package/lib/encrypt.js +184 -19
  67. package/lib/enum-descriptions.js +1 -2
  68. package/lib/export.js +133 -39
  69. package/lib/gateway.js +20 -6
  70. package/lib/get-raw-email.js +26 -44
  71. package/lib/handlebars-helpers.js +31 -10
  72. package/lib/imap-proxy-auth.js +13 -29
  73. package/lib/imapproxy/imap-core/README.md +1 -0
  74. package/lib/imapproxy/imap-core/lib/handler/imap-parser.js +12 -0
  75. package/lib/imapproxy/imap-core/lib/imap-command.js +74 -10
  76. package/lib/imapproxy/imap-core/lib/imap-connection.js +9 -1
  77. package/lib/imapproxy/imap-core/lib/imap-server.js +112 -26
  78. package/lib/imapproxy/imap-core/lib/imap-stream.js +41 -1
  79. package/lib/imapproxy/imap-core/lib/imap-tools.js +10 -2
  80. package/lib/imapproxy/imap-core/lib/indexer/indexer.js +4 -2
  81. package/lib/imapproxy/imap-server.js +17 -177
  82. package/lib/imapproxy/proxy-handoff.js +235 -0
  83. package/lib/imapproxy/response-error.js +33 -0
  84. package/lib/imapproxy/upstream-client.js +2 -2
  85. package/lib/license-beacon.js +3 -9
  86. package/lib/llm-pre-process.js +182 -43
  87. package/lib/logger.js +1 -1
  88. package/lib/lua/ee-export-queue-add.lua +39 -0
  89. package/lib/lua/ee-get-idempotency.lua +15 -3
  90. package/lib/lua/ee-release-attempts.lua +23 -0
  91. package/lib/lua/ee-reserve-attempts.lua +42 -0
  92. package/lib/lua/h-set-bigger.lua +12 -2
  93. package/lib/lua/s-list-accounts.lua +6 -3
  94. package/lib/mcp/index.js +1 -2
  95. package/lib/mcp/listen.js +221 -18
  96. package/lib/mcp/oauth.js +44 -5
  97. package/lib/mcp/resources.js +8 -2
  98. package/lib/mcp/tools.js +13 -0
  99. package/lib/message-port-stream.js +64 -6
  100. package/lib/oauth/external-account-signer.js +16 -13
  101. package/lib/oauth/gmail.js +22 -20
  102. package/lib/oauth/outlook.js +19 -16
  103. package/lib/oauth/pubsub/google.js +38 -2
  104. package/lib/oauth/scope-checker.js +4 -7
  105. package/lib/oauth/verify-app.js +2 -2
  106. package/lib/oauth2-apps.js +139 -44
  107. package/lib/openapi/build-document.js +1 -1
  108. package/lib/openapi/joi-schema.js +78 -9
  109. package/lib/parse-message.js +21 -0
  110. package/lib/payload-examples-webhooks.json +4 -7
  111. package/lib/pbkdf2-phc.js +101 -0
  112. package/lib/privileged-settings.js +0 -14
  113. package/lib/rate-limit.js +54 -15
  114. package/lib/redis-scan-delete.js +56 -29
  115. package/lib/redis-url.js +3 -1
  116. package/lib/respawn-backoff.js +5 -4
  117. package/lib/response-stream.js +25 -4
  118. package/lib/rewrite-text-nodes.js +27 -40
  119. package/lib/routes-ui.js +0 -5
  120. package/lib/schemas.js +122 -242
  121. package/lib/sentry.js +21 -5
  122. package/lib/settings.js +58 -59
  123. package/lib/smtp-auth.js +6 -2
  124. package/lib/smtp-message-processor.js +28 -7
  125. package/lib/sso.js +7 -3
  126. package/lib/stream-encrypt.js +14 -13
  127. package/lib/swagger-options.js +3 -1
  128. package/lib/tls/provision.js +69 -3
  129. package/lib/tls/store.js +14 -1
  130. package/lib/tools.js +113 -52
  131. package/lib/totp.js +16 -1
  132. package/lib/ui-routes/account-routes.js +27 -63
  133. package/lib/ui-routes/admin-config-routes.js +111 -134
  134. package/lib/ui-routes/admin-entities-routes.js +101 -191
  135. package/lib/ui-routes/ai-options.js +104 -0
  136. package/lib/ui-routes/auth-routes.js +98 -18
  137. package/lib/ui-routes/export-routes.js +3 -23
  138. package/lib/ui-routes/internals-routes.js +12 -9
  139. package/lib/ui-routes/network-config-routes.js +8 -18
  140. package/lib/ui-routes/oauth-config-routes.js +80 -214
  141. package/lib/ui-routes/route-helpers.js +40 -44
  142. package/lib/ui-routes/unsubscribe-routes.js +2 -2
  143. package/lib/utils/decode-headers.js +32 -5
  144. package/lib/utils/header-map.js +62 -1
  145. package/lib/utils/network.js +2 -0
  146. package/lib/web-safe-html.js +77 -4
  147. package/lib/webhook-request.js +9 -1
  148. package/lib/webhook-routing.js +27 -1
  149. package/lib/webhooks.js +120 -43
  150. package/lib/worker-rpc-error.js +43 -2
  151. package/list-generate.js +13 -2
  152. package/package.json +33 -41
  153. package/sbom.json +1 -1
  154. package/scripts/refresh-test-tokens.js +42 -14
  155. package/server.js +317 -289
  156. package/static/css/flyonui.css +1 -1
  157. package/static/css/src/app.tailwind.css +7 -8
  158. package/static/js/ee-client.js +145 -70
  159. package/static/js/ui.js +350 -154
  160. package/static/licenses.html +434 -1835
  161. package/translations/messages.pot +97 -97
  162. package/views/accounts/browse.hbs +10 -4
  163. package/views/config/ai.hbs +49 -41
  164. package/views/config/oauth/app.hbs +2 -1
  165. package/views/dashboard.hbs +18 -16
  166. package/views/layout/app.hbs +0 -22
  167. package/views/layout/login.hbs +2 -1
  168. package/views/partials/filter_editor_js.hbs +4 -3
  169. package/views/partials/side_menu.hbs +0 -4
  170. package/views/partials/ui/account-picker.hbs +6 -32
  171. package/views/partials/ui/model-picker.hbs +31 -0
  172. package/views/partials/ui/pagination.hbs +6 -1
  173. package/views/partials/ui/picker.hbs +47 -0
  174. package/views/unsubscribe.hbs +1 -1
  175. package/workers/api.js +20 -20
  176. package/workers/export.js +55 -60
  177. package/workers/imap.js +126 -46
  178. package/workers/submit.js +3 -1
  179. package/workers/webhooks.js +46 -27
  180. package/.githooks/pre-commit +0 -80
  181. package/.github/CODE_OF_CONDUCT.md +0 -76
  182. package/.github/contributing.md +0 -17
  183. package/.github/dependabot.yml +0 -22
  184. package/.github/docker-smoke-test.sh +0 -61
  185. package/.github/workflows/deploy.yml +0 -215
  186. package/.github/workflows/e2e.yml +0 -55
  187. package/.github/workflows/release.yaml +0 -217
  188. package/.github/workflows/test.yml +0 -307
  189. package/lib/api-routes/chat-routes.js +0 -535
  190. package/lib/document-store.js +0 -75
  191. package/lib/es.js +0 -757
  192. package/lib/generate-text-preview.js +0 -61
  193. package/lib/imapproxy/imap-core/memory-notifier.js +0 -129
  194. package/lib/pre-process.js +0 -196
  195. package/lib/threads.js +0 -155
  196. package/lib/ui-routes/document-store-routes.js +0 -806
  197. package/playwright.config.js +0 -47
  198. package/views/config/document-store/chat.hbs +0 -286
  199. package/views/config/document-store/index.hbs +0 -146
  200. package/views/config/document-store/mappings/index.hbs +0 -108
  201. package/views/config/document-store/mappings/new.hbs +0 -68
  202. package/views/config/document-store/pre-processing/index.hbs +0 -103
  203. package/views/partials/document_store_header.hbs +0 -30
  204. package/workers/documents.js +0 -1128
@@ -0,0 +1,147 @@
1
+ 'use strict';
2
+
3
+ // The one retry policy of the Gmail and Graph provider API requests. The OAuth2 clients send
4
+ // every provider request exactly once (lib/oauth/gmail.js, lib/oauth/outlook.js dispatch through
5
+ // httpAgent.fetch, never the retrying dispatcher), so this is the only place a failed request is
6
+ // repeated from. Two layers used to retry independently, and one throttled call turned into up to
7
+ // 24 requests.
8
+
9
+ const timers = require('timers/promises');
10
+ const { isTransientNetworkError } = require('./credential-errors');
11
+ const { metricsMeta } = require('./base-client');
12
+
13
+ const MAX_RETRY_ATTEMPTS = 3;
14
+ const RETRY_BASE_DELAY = 1000; // first backoff wait, doubled on every retry
15
+ const RETRY_JITTER = 500; // random 0..500 ms added to every wait, so a fleet does not retry in step
16
+
17
+ // Waits are bounded so a request keeps inside the API worker's RPC timeout (10 s by default):
18
+ // a caller that has already given up gains nothing from a request that finishes minutes later
19
+ const MAX_RETRY_DELAY = 5000; // one wait, also caps a server-requested Retry-After
20
+ const MAX_TOTAL_RETRY_DELAY = 8000; // all waits of one request together
21
+
22
+ // A read that hit a server error is repeated once; a write that failed with a 5xx may still have
23
+ // been applied, so it is not
24
+ const SERVER_ERROR_RETRIES = 1;
25
+
26
+ // The only methods a request is re-sent for after the socket failed: the response was lost, and
27
+ // re-sending anything else (a send, a move, a delete) may apply the operation a second time
28
+ const IDEMPOTENT_METHODS = new Set(['get', 'head']);
29
+
30
+ /**
31
+ * How long to wait before the next attempt: the server's Retry-After when it sent one, otherwise
32
+ * exponential backoff, plus jitter, never above MAX_RETRY_DELAY
33
+ * @param {Error} err - The error (the transport stores the parsed Retry-After in seconds as err.retryAfter)
34
+ * @param {number} attempt - 0-based attempt index
35
+ * @returns {number} Milliseconds
36
+ */
37
+ function retryDelay(err, attempt) {
38
+ const retryAfter = Number(err?.retryAfter ?? err?.oauthRequest?.retryAfter);
39
+ let delay = retryAfter > 0 ? retryAfter * 1000 : RETRY_BASE_DELAY * Math.pow(2, attempt);
40
+ delay += Math.random() * RETRY_JITTER;
41
+ return Math.min(delay, MAX_RETRY_DELAY);
42
+ }
43
+
44
+ /**
45
+ * Decides whether a failed request is repeated: a throttled request (the provider says so, and
46
+ * a 429 means nothing was processed), a network error of an idempotent request, and one retry of
47
+ * a read that hit a server error the provider asks to retry
48
+ * @param {Error} err - The error
49
+ * @param {Object} policy
50
+ * @param {string} policy.method - HTTP method of the request
51
+ * @param {Object} policy.options - Request options (`noRetry` sends exactly once)
52
+ * @param {Object} policy.state - Per-request retry counters
53
+ * @param {Function} policy.isRateLimited - Provider predicate for a throttled response
54
+ * @param {Set<number>} policy.serverErrorStatuses - Statuses the provider asks to retry a read on
55
+ * @returns {string|false} The retry reason, or false
56
+ */
57
+ function retryReason(err, { method, options, state, isRateLimited, serverErrorStatuses }) {
58
+ if (options.noRetry) {
59
+ return false;
60
+ }
61
+
62
+ const idempotent = IDEMPOTENT_METHODS.has((method || 'get').toLowerCase());
63
+
64
+ if (isTransientNetworkError(err)) {
65
+ return idempotent ? 'network' : false;
66
+ }
67
+
68
+ if (isRateLimited(err)) {
69
+ return 'rate_limited';
70
+ }
71
+
72
+ if (idempotent && serverErrorStatuses.has(err.oauthRequest?.status) && state.serverErrorRetries < SERVER_ERROR_RETRIES) {
73
+ state.serverErrorRetries++;
74
+ return 'server_error';
75
+ }
76
+
77
+ return false;
78
+ }
79
+
80
+ /**
81
+ * Runs one provider API request with retries
82
+ *
83
+ * @param {Function} sendOnce - Performs one attempt; resolves with the response or throws
84
+ * @param {Object} policy
85
+ * @param {Object} policy.context - The client (`account` and `logger` are read for the log lines)
86
+ * @param {string} policy.provider - Provider name for the log lines and metrics
87
+ * @param {string} policy.url - Request URL for the log lines
88
+ * @param {string} policy.method - HTTP method
89
+ * @param {Object} [policy.options] - Request options: `maxRetries` overrides the default, `noRetry` sends exactly once
90
+ * @param {Function} policy.isRateLimited - Provider predicate for a throttled response
91
+ * @param {Set<number>} policy.serverErrorStatuses - Statuses the provider asks to retry a read on
92
+ * @returns {Promise<*>} The response
93
+ */
94
+ async function requestWithRetry(sendOnce, { context, provider, url, method, options = {}, isRateLimited, serverErrorStatuses }) {
95
+ const maxRetries = options.maxRetries ?? MAX_RETRY_ATTEMPTS;
96
+ const state = { serverErrorRetries: 0 };
97
+ let totalDelay = 0;
98
+
99
+ for (let attempt = 0; ; attempt++) {
100
+ try {
101
+ return await sendOnce(attempt);
102
+ } catch (err) {
103
+ const reason = attempt < maxRetries && retryReason(err, { method, options, state, isRateLimited, serverErrorStatuses });
104
+ const delay = reason ? retryDelay(err, attempt) : 0;
105
+
106
+ if (!reason || totalDelay + delay > MAX_TOTAL_RETRY_DELAY) {
107
+ throw err;
108
+ }
109
+ totalDelay += delay;
110
+
111
+ context.logger.warn({
112
+ msg: 'API request failed, retrying',
113
+ account: context.account,
114
+ provider,
115
+ reason,
116
+ attempt: attempt + 1,
117
+ maxRetries,
118
+ delay: Math.round(delay),
119
+ code: err.code,
120
+ status: err.oauthRequest?.status,
121
+ errorReason: err.oauthRequest?.response?.error?.errors?.[0]?.reason,
122
+ url
123
+ });
124
+
125
+ metricsMeta({ account: context.account }, context.logger, 'oauth2ApiRequest', 'inc', {
126
+ status: reason,
127
+ provider,
128
+ statusCode: String(err.oauthRequest?.status || 0)
129
+ });
130
+
131
+ // Looked up on the module at call time, so a test can stand in for the wait
132
+ await timers.setTimeout(delay);
133
+ }
134
+ }
135
+ }
136
+
137
+ module.exports = {
138
+ MAX_RETRY_ATTEMPTS,
139
+ RETRY_BASE_DELAY,
140
+ RETRY_JITTER,
141
+ MAX_RETRY_DELAY,
142
+ MAX_TOTAL_RETRY_DELAY,
143
+ SERVER_ERROR_RETRIES,
144
+ retryDelay,
145
+ retryReason,
146
+ requestWithRetry
147
+ };