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.
- package/.ncurc.js +12 -6
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +52 -0
- package/Dockerfile +4 -2
- package/bin/emailengine.js +28 -18
- package/config/default.toml +0 -5
- package/config/test.toml +0 -5
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +70 -42
- package/getswagger.sh +0 -4
- package/lib/account/account-state.js +2 -110
- package/lib/account/bounce-store-sweep.js +63 -0
- package/lib/account/expected-identity.js +1 -1
- package/lib/account-assignment.js +207 -0
- package/lib/account.js +374 -878
- package/lib/api-reference/index.js +2 -3
- package/lib/api-reference/schema-tree.js +53 -3
- package/lib/api-routes/account-routes.js +18 -29
- package/lib/api-routes/blocklist-routes.js +2 -2
- package/lib/api-routes/bull-board-routes.js +1 -6
- package/lib/api-routes/export-routes.js +2 -22
- package/lib/api-routes/gateway-routes.js +5 -14
- package/lib/api-routes/index.js +1 -13
- package/lib/api-routes/mailbox-routes.js +2 -7
- package/lib/api-routes/mcp-oauth-routes.js +2 -1
- package/lib/api-routes/mcp-routes.js +7 -7
- package/lib/api-routes/message-routes.js +44 -236
- package/lib/api-routes/mint-bounds.js +110 -0
- package/lib/api-routes/oauth2-app-routes.js +23 -32
- package/lib/api-routes/operation-impact.js +1 -1
- package/lib/api-routes/outbox-routes.js +2 -2
- package/lib/api-routes/permission-map.js +1 -7
- package/lib/api-routes/route-helpers.js +11 -5
- package/lib/api-routes/settings-routes.js +19 -19
- package/lib/api-routes/stats-routes.js +1 -1
- package/lib/api-routes/submit-routes.js +17 -18
- package/lib/api-routes/template-routes.js +4 -9
- package/lib/api-routes/token-routes.js +27 -5
- package/lib/api-token-scheme.js +71 -0
- package/lib/arf-detect.js +41 -7
- package/lib/auth-token.js +44 -24
- package/lib/autodetect-imap-settings.js +163 -123
- package/lib/bounce-detect.js +25 -16
- package/lib/calendar-event.js +78 -0
- package/lib/consts.js +51 -5
- package/lib/create-imap-client.js +24 -0
- package/lib/db.js +76 -17
- package/lib/delivery-error.js +2 -1
- package/lib/egress-fetch.js +54 -3
- package/lib/egress-filter.js +11 -0
- package/lib/email-client/api-retry.js +147 -0
- package/lib/email-client/base-client.js +628 -615
- package/lib/email-client/gmail/gmail-api.js +38 -125
- package/lib/email-client/gmail/send-request.js +1 -1
- package/lib/email-client/gmail-client.js +170 -84
- package/lib/email-client/imap/mailbox.js +244 -535
- package/lib/email-client/imap/stale-view.js +109 -0
- package/lib/email-client/imap/subconnection.js +46 -9
- package/lib/email-client/imap/sync-operations.js +5 -37
- package/lib/email-client/imap-client.js +188 -64
- package/lib/email-client/message-builder.js +9 -14
- package/lib/email-client/notification-handler.js +5 -219
- package/lib/email-client/outlook/graph-api.js +147 -126
- package/lib/email-client/outlook/send-request.js +10 -4
- package/lib/email-client/outlook-client.js +519 -617
- package/lib/email-client/smtp-pool-manager.js +3 -2
- package/lib/encrypt.js +184 -19
- package/lib/enum-descriptions.js +1 -2
- package/lib/export.js +144 -41
- package/lib/gateway.js +20 -6
- package/lib/get-raw-email.js +26 -44
- package/lib/handlebars-helpers.js +31 -10
- package/lib/imap-proxy-auth.js +13 -29
- package/lib/imapproxy/imap-core/README.md +1 -0
- package/lib/imapproxy/imap-core/lib/handler/imap-parser.js +12 -0
- package/lib/imapproxy/imap-core/lib/imap-command.js +74 -10
- package/lib/imapproxy/imap-core/lib/imap-connection.js +9 -1
- package/lib/imapproxy/imap-core/lib/imap-server.js +112 -26
- package/lib/imapproxy/imap-core/lib/imap-stream.js +41 -1
- package/lib/imapproxy/imap-core/lib/imap-tools.js +10 -2
- package/lib/imapproxy/imap-core/lib/indexer/indexer.js +4 -2
- package/lib/imapproxy/imap-server.js +17 -177
- package/lib/imapproxy/proxy-handoff.js +235 -0
- package/lib/imapproxy/response-error.js +33 -0
- package/lib/imapproxy/upstream-client.js +2 -2
- package/lib/license-beacon.js +3 -9
- package/lib/llm-pre-process.js +182 -43
- package/lib/logger.js +1 -1
- package/lib/lua/ee-export-queue-add.lua +39 -0
- package/lib/lua/ee-get-idempotency.lua +15 -3
- package/lib/lua/ee-release-attempts.lua +23 -0
- package/lib/lua/ee-reserve-attempts.lua +42 -0
- package/lib/lua/h-set-bigger.lua +12 -2
- package/lib/lua/s-list-accounts.lua +6 -3
- package/lib/mcp/index.js +1 -2
- package/lib/mcp/listen.js +221 -18
- package/lib/mcp/oauth.js +44 -5
- package/lib/mcp/resources.js +8 -2
- package/lib/mcp/tools.js +13 -0
- package/lib/message-port-stream.js +64 -6
- package/lib/msgpack.js +1 -6
- package/lib/oauth/external-account-signer.js +16 -13
- package/lib/oauth/gmail.js +22 -20
- package/lib/oauth/outlook.js +19 -16
- package/lib/oauth/pubsub/google.js +38 -2
- package/lib/oauth/scope-checker.js +4 -7
- package/lib/oauth/verify-app.js +2 -2
- package/lib/oauth2-apps.js +139 -44
- package/lib/openapi/build-document.js +1 -1
- package/lib/openapi/joi-schema.js +78 -9
- package/lib/parse-message.js +21 -0
- package/lib/payload-examples-webhooks.json +4 -7
- package/lib/pbkdf2-phc.js +101 -0
- package/lib/privileged-settings.js +0 -14
- package/lib/rate-limit.js +54 -15
- package/lib/redis-scan-delete.js +56 -29
- package/lib/redis-url.js +3 -1
- package/lib/respawn-backoff.js +5 -4
- package/lib/response-stream.js +25 -4
- package/lib/rewrite-text-nodes.js +27 -40
- package/lib/routes-ui.js +0 -5
- package/lib/schemas.js +129 -262
- package/lib/sentry.js +21 -5
- package/lib/settings.js +58 -59
- package/lib/smtp-auth.js +6 -2
- package/lib/smtp-message-processor.js +28 -7
- package/lib/sso.js +7 -3
- package/lib/stream-encrypt.js +14 -13
- package/lib/swagger-options.js +3 -1
- package/lib/tls/provision.js +69 -3
- package/lib/tls/store.js +14 -1
- package/lib/tools.js +113 -52
- package/lib/totp.js +16 -1
- package/lib/ui-routes/account-routes.js +27 -63
- package/lib/ui-routes/admin-config-routes.js +111 -134
- package/lib/ui-routes/admin-entities-routes.js +101 -191
- package/lib/ui-routes/ai-options.js +104 -0
- package/lib/ui-routes/auth-routes.js +98 -18
- package/lib/ui-routes/export-routes.js +3 -23
- package/lib/ui-routes/internals-routes.js +12 -9
- package/lib/ui-routes/network-config-routes.js +8 -18
- package/lib/ui-routes/oauth-config-routes.js +80 -214
- package/lib/ui-routes/route-helpers.js +40 -44
- package/lib/ui-routes/unsubscribe-routes.js +2 -2
- package/lib/utils/decode-headers.js +32 -5
- package/lib/utils/header-map.js +62 -1
- package/lib/utils/network.js +2 -0
- package/lib/web-safe-html.js +77 -4
- package/lib/webhook-request.js +9 -1
- package/lib/webhook-routing.js +27 -1
- package/lib/webhooks.js +120 -43
- package/lib/worker-rpc-error.js +43 -2
- package/list-generate.js +13 -2
- package/package.json +38 -46
- package/sbom.json +1 -1
- package/scripts/refresh-test-tokens.js +42 -14
- package/server.js +340 -336
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +7 -8
- package/static/js/ee-client.js +145 -70
- package/static/js/ui.js +350 -154
- package/static/licenses.html +460 -1861
- package/translations/messages.pot +99 -99
- package/views/accounts/browse.hbs +10 -4
- package/views/config/ai.hbs +49 -41
- package/views/config/oauth/app.hbs +2 -1
- package/views/dashboard.hbs +18 -16
- package/views/layout/app.hbs +0 -22
- package/views/layout/login.hbs +2 -1
- package/views/partials/filter_editor_js.hbs +4 -3
- package/views/partials/side_menu.hbs +0 -4
- package/views/partials/ui/account-picker.hbs +6 -32
- package/views/partials/ui/model-picker.hbs +31 -0
- package/views/partials/ui/pagination.hbs +6 -1
- package/views/partials/ui/picker.hbs +47 -0
- package/views/unsubscribe.hbs +1 -1
- package/workers/api.js +33 -23
- package/workers/export.js +55 -60
- package/workers/imap.js +134 -47
- package/workers/submit.js +3 -1
- package/workers/webhooks.js +46 -27
- package/.githooks/pre-commit +0 -80
- package/.github/CODE_OF_CONDUCT.md +0 -76
- package/.github/contributing.md +0 -17
- package/.github/dependabot.yml +0 -22
- package/.github/docker-smoke-test.sh +0 -61
- package/.github/workflows/deploy.yml +0 -215
- package/.github/workflows/e2e.yml +0 -55
- package/.github/workflows/release.yaml +0 -217
- package/.github/workflows/test.yml +0 -307
- package/lib/api-routes/chat-routes.js +0 -535
- package/lib/append-list.js +0 -63
- package/lib/document-store.js +0 -75
- package/lib/es.js +0 -757
- package/lib/generate-text-preview.js +0 -61
- package/lib/imapproxy/imap-core/memory-notifier.js +0 -129
- package/lib/lua/h-push.lua +0 -32
- package/lib/pre-process.js +0 -196
- package/lib/threads.js +0 -155
- package/lib/ui-routes/document-store-routes.js +0 -806
- package/playwright.config.js +0 -47
- package/views/config/document-store/chat.hbs +0 -286
- package/views/config/document-store/index.hbs +0 -146
- package/views/config/document-store/mappings/index.hbs +0 -108
- package/views/config/document-store/mappings/new.hbs +0 -68
- package/views/config/document-store/pre-processing/index.hbs +0 -103
- package/views/partials/document_store_header.hbs +0 -30
- package/workers/documents.js +0 -1128
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { metricsMeta, markRejectedAccessToken, renewRejectedAccessToken } = require('../base-client');
|
|
4
|
-
const
|
|
4
|
+
const apiRetry = require('../api-retry');
|
|
5
5
|
|
|
6
6
|
// Gmail API configuration
|
|
7
7
|
const GMAIL_API_BASE = 'https://gmail.googleapis.com';
|
|
@@ -9,44 +9,12 @@ const GMAIL_API_BASE = 'https://gmail.googleapis.com';
|
|
|
9
9
|
// Maximum concurrent listing requests
|
|
10
10
|
const LIST_BATCH_SIZE = 10;
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
const
|
|
14
|
-
const RETRY_BASE_DELAY = 1000; // 1 second base delay
|
|
15
|
-
|
|
16
|
-
// Gmail API error code mapping to internal error codes
|
|
17
|
-
// https://developers.google.com/gmail/api/reference/rest#error-codes
|
|
18
|
-
const GMAIL_ERROR_MAP = {
|
|
19
|
-
INVALID_ARGUMENT: { code: 'InvalidArgument', status: 400 },
|
|
20
|
-
FAILED_PRECONDITION: { code: 'FailedPrecondition', status: 400 },
|
|
21
|
-
NOT_FOUND: { code: 'NotFound', status: 404 },
|
|
22
|
-
PERMISSION_DENIED: { code: 'PermissionDenied', status: 403 },
|
|
23
|
-
RESOURCE_EXHAUSTED: { code: 'RateLimitExceeded', status: 429 },
|
|
24
|
-
UNAUTHENTICATED: { code: 'Unauthenticated', status: 401 },
|
|
25
|
-
INTERNAL: { code: 'InternalError', status: 500 },
|
|
26
|
-
UNAVAILABLE: { code: 'ServiceUnavailable', status: 503 }
|
|
27
|
-
};
|
|
12
|
+
// Gmail answers a busy backend with these, and its documentation asks for a retry
|
|
13
|
+
const RETRYABLE_SERVER_STATUSES = new Set([500, 502, 503, 504]);
|
|
28
14
|
|
|
29
15
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @param {string} gmailErrorStatus - The error status from Gmail API
|
|
33
|
-
* @returns {Error|null} A formatted error object or null if not mappable
|
|
34
|
-
*/
|
|
35
|
-
function createGmailError(gmailError, gmailErrorStatus) {
|
|
36
|
-
const mappedError = GMAIL_ERROR_MAP[gmailErrorStatus];
|
|
37
|
-
if (!mappedError) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const error = new Error(gmailError?.message || gmailErrorStatus);
|
|
42
|
-
error.code = mappedError.code;
|
|
43
|
-
error.statusCode = mappedError.status;
|
|
44
|
-
error.gmailErrorStatus = gmailErrorStatus;
|
|
45
|
-
return error;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Checks if an error indicates rate limiting
|
|
16
|
+
* Checks if an error indicates rate limiting. Gmail's usual throttling answer is a 403 with a
|
|
17
|
+
* rate-limit reason, not a 429
|
|
50
18
|
* @param {Object} err - The error object
|
|
51
19
|
* @returns {boolean} True if rate limited
|
|
52
20
|
*/
|
|
@@ -58,41 +26,21 @@ function isRateLimitError(err) {
|
|
|
58
26
|
}
|
|
59
27
|
|
|
60
28
|
/**
|
|
61
|
-
*
|
|
62
|
-
* @param {Object} err - The error object (may contain Retry-After header)
|
|
63
|
-
* @param {number} attempt - Current attempt number (0-indexed)
|
|
64
|
-
* @returns {number} Delay in milliseconds
|
|
65
|
-
*/
|
|
66
|
-
function calculateRetryDelay(err, attempt) {
|
|
67
|
-
const retryAfter = err.oauthRequest?.headers?.['retry-after'];
|
|
68
|
-
|
|
69
|
-
// Use Retry-After header if available, otherwise exponential backoff
|
|
70
|
-
let delay = retryAfter ? parseInt(retryAfter, 10) * 1000 : RETRY_BASE_DELAY * Math.pow(2, attempt);
|
|
71
|
-
|
|
72
|
-
// Add jitter (0-500ms) to prevent synchronized retries
|
|
73
|
-
delay += Math.random() * 500;
|
|
74
|
-
|
|
75
|
-
return delay;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Makes authenticated requests to Gmail API with automatic retry on rate limiting
|
|
80
|
-
* Implements exponential backoff with jitter
|
|
29
|
+
* Makes one authenticated request to the Gmail API, repeated only after a rejected cached token
|
|
81
30
|
*
|
|
82
31
|
* @param {Object} context - The client context (GmailClient instance)
|
|
83
32
|
* @param {string} url - API endpoint URL
|
|
84
|
-
* @param {string}
|
|
85
|
-
* @param {*}
|
|
86
|
-
* @param {Object}
|
|
33
|
+
* @param {string} method - HTTP method
|
|
34
|
+
* @param {*} payload - Request payload
|
|
35
|
+
* @param {Object} options - Request options
|
|
87
36
|
* @returns {Promise<*>} API response
|
|
88
37
|
*/
|
|
89
|
-
async function
|
|
90
|
-
const maxRetries = options.maxRetries ?? MAX_RETRY_ATTEMPTS;
|
|
91
|
-
let lastError;
|
|
38
|
+
async function requestOnce(context, url, method, payload, options) {
|
|
92
39
|
let tokenRenewed = false;
|
|
93
40
|
|
|
94
|
-
for
|
|
95
|
-
|
|
41
|
+
// Loops only for the single repeat after a rejected cached token
|
|
42
|
+
for (;;) {
|
|
43
|
+
let tokenData;
|
|
96
44
|
|
|
97
45
|
try {
|
|
98
46
|
tokenData = await context.getTokenData();
|
|
@@ -106,7 +54,7 @@ async function request(context, url, method, payload, options = {}) {
|
|
|
106
54
|
await context.getClient();
|
|
107
55
|
}
|
|
108
56
|
|
|
109
|
-
result = await context.oAuth2Client.request(tokenData.accessToken, url, method, payload, options);
|
|
57
|
+
const result = await context.oAuth2Client.request(tokenData.accessToken, url, method, payload, options);
|
|
110
58
|
|
|
111
59
|
// Track successful API request
|
|
112
60
|
metricsMeta({ account: context.account }, context.logger, 'oauth2ApiRequest', 'inc', {
|
|
@@ -117,59 +65,10 @@ async function request(context, url, method, payload, options = {}) {
|
|
|
117
65
|
|
|
118
66
|
return result;
|
|
119
67
|
} catch (err) {
|
|
120
|
-
lastError = err;
|
|
121
|
-
|
|
122
68
|
// A rejected cached token is renewed and the request repeated once, see
|
|
123
69
|
// renewRejectedAccessToken()
|
|
124
70
|
if (await renewRejectedAccessToken(context, err, tokenData, tokenRenewed)) {
|
|
125
71
|
tokenRenewed = true;
|
|
126
|
-
attempt -= 1;
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Check if this is a transient network error
|
|
131
|
-
if (isTransientNetworkError(err) && attempt < maxRetries) {
|
|
132
|
-
const delay = RETRY_BASE_DELAY * Math.pow(2, attempt) + Math.random() * 500;
|
|
133
|
-
|
|
134
|
-
context.logger.warn({
|
|
135
|
-
msg: 'Transient network error during Gmail API request, retrying',
|
|
136
|
-
account: context.account,
|
|
137
|
-
attempt: attempt + 1,
|
|
138
|
-
maxRetries,
|
|
139
|
-
delay,
|
|
140
|
-
code: err.code
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
metricsMeta({ account: context.account }, context.logger, 'oauth2ApiRequest', 'inc', {
|
|
144
|
-
status: 'network_error',
|
|
145
|
-
provider: 'gmail',
|
|
146
|
-
statusCode: '0'
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
await new Promise(resolve => setTimeout(resolve, delay));
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Check if this is a rate limit error
|
|
154
|
-
if (isRateLimitError(err) && attempt < maxRetries) {
|
|
155
|
-
const delay = calculateRetryDelay(err, attempt);
|
|
156
|
-
|
|
157
|
-
context.logger.warn({
|
|
158
|
-
msg: 'Rate limited by Gmail API, retrying',
|
|
159
|
-
account: context.account,
|
|
160
|
-
attempt: attempt + 1,
|
|
161
|
-
maxRetries,
|
|
162
|
-
delay,
|
|
163
|
-
errorReason: err.oauthRequest?.response?.error?.errors?.[0]?.reason
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
metricsMeta({ account: context.account }, context.logger, 'oauth2ApiRequest', 'inc', {
|
|
167
|
-
status: 'rate_limited',
|
|
168
|
-
provider: 'gmail',
|
|
169
|
-
statusCode: '429'
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
await new Promise(resolve => setTimeout(resolve, delay));
|
|
173
72
|
continue;
|
|
174
73
|
}
|
|
175
74
|
|
|
@@ -185,19 +84,37 @@ async function request(context, url, method, payload, options = {}) {
|
|
|
185
84
|
}
|
|
186
85
|
|
|
187
86
|
// Track failed API request
|
|
188
|
-
const statusCode = String(err.oauthRequest?.status || 0);
|
|
189
87
|
metricsMeta({ account: context.account }, context.logger, 'oauth2ApiRequest', 'inc', {
|
|
190
88
|
status: 'failure',
|
|
191
89
|
provider: 'gmail',
|
|
192
|
-
statusCode
|
|
90
|
+
statusCode: String(status || 0)
|
|
193
91
|
});
|
|
194
92
|
|
|
195
93
|
throw markRejectedAccessToken(err);
|
|
196
94
|
}
|
|
197
95
|
}
|
|
96
|
+
}
|
|
198
97
|
|
|
199
|
-
|
|
200
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Makes authenticated requests to Gmail API, retried under the policy of lib/email-client/api-retry.js
|
|
100
|
+
*
|
|
101
|
+
* @param {Object} context - The client context (GmailClient instance)
|
|
102
|
+
* @param {string} url - API endpoint URL
|
|
103
|
+
* @param {string} [method='get'] - HTTP method
|
|
104
|
+
* @param {*} [payload] - Request payload
|
|
105
|
+
* @param {Object} [options={}] - Request options. `noRetry` sends the request exactly once
|
|
106
|
+
* @returns {Promise<*>} API response
|
|
107
|
+
*/
|
|
108
|
+
async function request(context, url, method, payload, options = {}) {
|
|
109
|
+
return await apiRetry.requestWithRetry(() => requestOnce(context, url, method, payload, options), {
|
|
110
|
+
context,
|
|
111
|
+
provider: 'gmail',
|
|
112
|
+
url,
|
|
113
|
+
method,
|
|
114
|
+
options,
|
|
115
|
+
isRateLimited: isRateLimitError,
|
|
116
|
+
serverErrorStatuses: RETRYABLE_SERVER_STATUSES
|
|
117
|
+
});
|
|
201
118
|
}
|
|
202
119
|
|
|
203
120
|
/**
|
|
@@ -260,9 +177,7 @@ module.exports = {
|
|
|
260
177
|
// Constants
|
|
261
178
|
GMAIL_API_BASE,
|
|
262
179
|
LIST_BATCH_SIZE,
|
|
263
|
-
|
|
264
|
-
RETRY_BASE_DELAY,
|
|
265
|
-
GMAIL_ERROR_MAP,
|
|
180
|
+
RETRYABLE_SERVER_STATUSES,
|
|
266
181
|
|
|
267
182
|
// Request functions
|
|
268
183
|
request,
|
|
@@ -270,7 +185,5 @@ module.exports = {
|
|
|
270
185
|
executeBatchRequests,
|
|
271
186
|
|
|
272
187
|
// Error handling
|
|
273
|
-
|
|
274
|
-
isRateLimitError,
|
|
275
|
-
calculateRetryDelay
|
|
188
|
+
isRateLimitError
|
|
276
189
|
};
|
|
@@ -23,7 +23,7 @@ const JSON_SEND_LIMIT = 3.5 * 1024 * 1024;
|
|
|
23
23
|
*/
|
|
24
24
|
function buildSendRequest(raw, threadId) {
|
|
25
25
|
if (raw.length <= JSON_SEND_LIMIT) {
|
|
26
|
-
// JSON endpoint with base64url encoding
|
|
26
|
+
// JSON endpoint with base64url encoding
|
|
27
27
|
const payload = { raw: raw.toString('base64url') };
|
|
28
28
|
if (threadId) {
|
|
29
29
|
payload.threadId = threadId;
|