@remit/imap-worker 0.0.93 → 0.0.95

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.
@@ -187,13 +187,7 @@ export const handleEmptyTrash = async (event, ctx) => {
187
187
  const swept = sweepable.filter((message) => expunged.has(message.uid));
188
188
  const sweptIds = new Set(swept.map((message) => message.messageId));
189
189
  const deletedCount = swept.length;
190
- for (const message of swept) {
191
- await messageService.delete(message.messageId);
192
- const threadMessage = await threadMessageService.findByMessageId(account.accountConfigId, message.messageId);
193
- if (threadMessage) {
194
- await threadMessageService.delete(threadMessage.accountConfigId, threadMessage.threadMessageId);
195
- }
196
- }
190
+ await messageService.deleteMany([...sweptIds]);
197
191
  // Everything this sweep did not carry through is still marked for a
198
192
  // deletion that will never come: mail another client already
199
193
  // emptied, mail that reached Trash after the SEARCH, a redelivery
@@ -0,0 +1,24 @@
1
+ import type { ConnectionScopeFactory } from "@remit/backend/create-remit-client";
2
+ import type { AccountItem } from "@remit/data-ports";
3
+ import { ConflictError } from "@remit/data-ports/errors";
4
+ import type { Logger } from "@remit/logger";
5
+ import type { ConnectionScope } from "@remit/mailbox-service";
6
+ import type { MailCredentials } from "@remit/mailbox-service/types";
7
+ import type { TokenService } from "./context.js";
8
+ /**
9
+ * The account holds no credential a connection can be opened with: it stores
10
+ * none, or its OAuth consent was withdrawn. The account has already been
11
+ * flipped to the state that says so; the caller has to reconnect it.
12
+ */
13
+ export declare class AccountNeedsReconnectError extends ConflictError {
14
+ name: string;
15
+ constructor(accountId: string);
16
+ }
17
+ export type OpenConnection = (account: AccountItem, credentials: MailCredentials) => ConnectionScope | Promise<ConnectionScope>;
18
+ /**
19
+ * The read path's connection scope, resolving credentials under the same OAuth
20
+ * lifecycle the queue handlers run: a rotated refresh token is persisted, and a
21
+ * revoked token or a missing credential flips the account's connection state
22
+ * and answers 409 instead of surfacing as a 500.
23
+ */
24
+ export declare const lifecycleConnectionScope: (tokenService: TokenService, open: OpenConnection, log: Logger) => ConnectionScopeFactory;
@@ -0,0 +1,35 @@
1
+ import { ConflictError } from "@remit/data-ports/errors";
2
+ import { withOAuthLifecycle } from "./with-oauth-lifecycle.js";
3
+ import { buildLifecycleDeps } from "./with-oauth-lifecycle-deps.js";
4
+ /**
5
+ * The account holds no credential a connection can be opened with: it stores
6
+ * none, or its OAuth consent was withdrawn. The account has already been
7
+ * flipped to the state that says so; the caller has to reconnect it.
8
+ */
9
+ export class AccountNeedsReconnectError extends ConflictError {
10
+ name = "AccountNeedsReconnectError";
11
+ constructor(accountId) {
12
+ super(`Account ${accountId} has to be reconnected before it can be read`);
13
+ this.publicApiError = {
14
+ code: "account_needs_reconnect",
15
+ details: { accountId },
16
+ };
17
+ }
18
+ }
19
+ /**
20
+ * The read path's connection scope, resolving credentials under the same OAuth
21
+ * lifecycle the queue handlers run: a rotated refresh token is persisted, and a
22
+ * revoked token or a missing credential flips the account's connection state
23
+ * and answers 409 instead of surfacing as a 500.
24
+ */
25
+ export const lifecycleConnectionScope = (tokenService, open, log) => (accountService, secrets) => async (accountId) => {
26
+ const account = await accountService.get(accountId);
27
+ const opened = [];
28
+ await withOAuthLifecycle(buildLifecycleDeps(secrets, accountService, tokenService), account, log, async (credentials) => {
29
+ opened.push(await open(account, credentials));
30
+ });
31
+ const [scope] = opened;
32
+ if (scope === undefined)
33
+ throw new AccountNeedsReconnectError(accountId);
34
+ return scope;
35
+ };
package/dist/process.d.ts CHANGED
@@ -2,5 +2,6 @@ export type { ConnectionScope } from "@remit/mailbox-service";
2
2
  export { type BorrowedConnection, type ConnectImap, type ConnectionPool, type ConnectionPoolOptions, createConnectionPool, type PoolAccount, type PoolOverflow, } from "./connection-pool.js";
3
3
  export { type AccountObjectPage, type AccountObjectStore, type AttemptBudgets, DEFAULT_MAILBOX_FRESHNESS_MS, defaultImapWorkerConfig, type ImapFailureKind, type ImapWorkerConfig, type ImapWorkerQueueName, type ProcessContext, type TokenService, type WorkerMetrics, } from "./context.js";
4
4
  export type { ImapEvent, WorkerEvent } from "./events.js";
5
+ export { AccountNeedsReconnectError, lifecycleConnectionScope, type OpenConnection, } from "./lifecycle-connection-scope.js";
5
6
  export { processEvent } from "./processor.js";
6
7
  export type { WorkerQueues } from "./queues.js";
package/dist/process.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { createConnectionPool, } from "./connection-pool.js";
2
2
  export { DEFAULT_MAILBOX_FRESHNESS_MS, defaultImapWorkerConfig, } from "./context.js";
3
+ export { AccountNeedsReconnectError, lifecycleConnectionScope, } from "./lifecycle-connection-scope.js";
3
4
  export { processEvent } from "./processor.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/imap-worker",
3
- "version": "0.0.93",
3
+ "version": "0.0.95",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {