@open-mercato/channel-imap 0.10.1-develop.8274.1.c8f0850146 → 0.10.1-develop.8278.1.f7f8e5ab1a

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.
@@ -93,6 +93,17 @@ class ImapChannelAdapter {
93
93
  } catch (appendError) {
94
94
  logger.warn("failed to append outbound message to Sent folder", { err: appendError });
95
95
  }
96
+ const rejected = result.rejected ?? [];
97
+ if (rejected.length > 0) {
98
+ const accepted = result.accepted ?? [];
99
+ return {
100
+ externalMessageId: result.messageId,
101
+ conversationId: input.conversationId,
102
+ status: "failed",
103
+ error: `SMTP_SEND_PARTIALLY_REJECTED: relay rejected ${rejected.length} of ${accepted.length + rejected.length} recipients`,
104
+ metadata: { ...result.response ? { response: result.response } : {}, accepted, rejected }
105
+ };
106
+ }
96
107
  return {
97
108
  externalMessageId: result.messageId,
98
109
  conversationId: input.conversationId,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/channel_imap/lib/adapter.ts"],
4
- "sourcesContent": ["import type {\n ChannelAdapter,\n ConvertOutboundInput,\n ChannelNativeContent,\n FetchHistoryInput,\n GetMessageStatusInput,\n HistoryPage,\n ImportHistoryInput,\n ImportHistoryPage,\n InboundMessage,\n MessageStatus,\n NormalizedInboundMessage,\n ResolveContactInput,\n ContactHint,\n SendMessageInput,\n SendMessageResult,\n ValidateCredentialsInput,\n ValidateCredentialsResult,\n VerifyWebhookInput,\n} from '@open-mercato/core/modules/communication_channels/lib/adapter'\nimport { imapCapabilities } from './capabilities'\nimport { imapCredentialsSchema, imapChannelStateSchema, type ImapCredentials, type ImapChannelState } from './credentials'\nimport {\n credentialsToConnection,\n getImapClient,\n} from './imap-client'\nimport {\n credentialsToSmtpConnection,\n getSmtpClient,\n isSmtpMessageTooLarge,\n type SmtpMessage,\n type SmtpSendResult,\n} from './smtp-client'\nimport { convertOutboundForEmail } from './convert-outbound'\nimport { normalizeInboundImapMessage } from './normalize-inbound'\nimport { validateImapCredentials } from './validate-credentials'\nimport { emailResolveContact } from '@open-mercato/core/modules/communication_channels/lib/email-contact'\nimport {\n decodeCursor,\n encodeCursor,\n sanitizeHeaderValue,\n} from '@open-mercato/core/modules/communication_channels/lib/email-mime'\nimport { resolveOutboundAttachmentBytes } from '@open-mercato/core/modules/communication_channels/lib/outbound-attachments'\nimport { getImportHistoryLimits } from '@open-mercato/core/modules/communication_channels/lib/import-history-limits'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('channel_imap')\n\n/**\n * IMAP+SMTP `ChannelAdapter`. Inbound is polling-driven (`realtimePush: false`),\n * outbound is SMTP. Threading is RFC2822 (In-Reply-To / References).\n *\n * Why this adapter omits some methods:\n * - `verifyWebhook` \u2014 IMAP has no webhook; we return a no-op event with\n * `eventType: 'other'` so the hub's webhook route returns 202 if anyone\n * ever POSTs at `/api/communication_channels/webhook/imap`.\n * - `getStatus` \u2014 IMAP has no delivery-status concept beyond `\\Seen`; we\n * return `{ status: 'sent' }` as a best-effort placeholder.\n * - No `sendReaction` / `editMessage` / `deleteMessage` \u2014 email doesn't\n * support these capabilities.\n */\nclass ImapChannelAdapter implements ChannelAdapter {\n readonly providerKey = 'imap'\n readonly channelType = 'email'\n readonly capabilities = imapCapabilities\n\n async sendMessage(input: SendMessageInput): Promise<SendMessageResult> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n\n let native: ChannelNativeContent\n try {\n native = await convertOutboundForEmail({\n body: input.content.html ?? input.content.text ?? '',\n bodyFormat: input.content.bodyFormat ?? (input.content.html ? 'html' : 'text'),\n attachments: input.content.attachments,\n channelMetadata: input.metadata,\n })\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Outbound conversion failed'\n return { externalMessageId: '', status: 'failed', error: message }\n }\n const meta = (native.metadata ?? {}) as Record<string, unknown>\n const to = Array.isArray(meta.to) ? (meta.to as string[]) : []\n if (to.length === 0) {\n return { externalMessageId: '', status: 'failed', error: '[internal] Email send requires at least one recipient' }\n }\n\n // Attachments arrive as bytes the hub read from the attachments module (or\n // as `data:` URLs); a remote URL is refused rather than fetched.\n let attachments: SmtpMessage['attachments']\n try {\n attachments = native.content.attachments?.map((attachment) => ({\n filename: sanitizeHeaderValue(attachment.fileName) || 'attachment',\n content: asBuffer(resolveOutboundAttachmentBytes(attachment)),\n contentType: attachment.mimeType,\n inline: attachment.inline,\n }))\n } catch (error) {\n const message = error instanceof Error ? error.message : '[internal] Outbound attachment is not readable'\n return { externalMessageId: '', status: 'failed', error: message }\n }\n\n const smtp = getSmtpClient()\n let result: SmtpSendResult\n try {\n result = await smtp.send(credentialsToSmtpConnection(credentials), {\n from: credentials.fromAddress,\n to,\n cc: Array.isArray(meta.cc) ? (meta.cc as string[]) : undefined,\n bcc: Array.isArray(meta.bcc) ? (meta.bcc as string[]) : undefined,\n subject: typeof meta.subject === 'string' ? (meta.subject as string) : undefined,\n text: native.content.text,\n html: native.content.html,\n inReplyTo: typeof meta.inReplyTo === 'string' ? (meta.inReplyTo as string) : undefined,\n references: Array.isArray(meta.references) ? (meta.references as string[]) : undefined,\n messageId: typeof meta.messageId === 'string' ? (meta.messageId as string) : undefined,\n attachments,\n })\n } catch (error) {\n if (isSmtpMessageTooLarge(error)) {\n return {\n externalMessageId: '',\n status: 'failed',\n error: '[internal] The mail server rejected the message as too large (message_too_large)',\n }\n }\n throw error\n }\n\n // Best-effort append to Sent \u2014 many servers auto-store via \"Submission\" but not all do.\n const imap = getImapClient()\n try {\n // Skip when the RFC2822 bytes are empty (MailComposer build failed upstream):\n // appending a 0-byte buffer would create a corrupt Sent-folder entry, and the\n // send itself already succeeded.\n if (result.raw.length > 0) {\n await imap.appendSent(credentialsToConnection(credentials), result.raw)\n }\n } catch (appendError) {\n // Best-effort: many servers auto-store sent mail via Submission, and the\n // Sent mailbox name is provider-specific (localized, or \"[Gmail]/Sent Mail\").\n // Log so operators can diagnose missing Sent-folder archival rather than\n // failing the send.\n logger.warn('failed to append outbound message to Sent folder', { err: appendError })\n }\n\n return {\n externalMessageId: result.messageId,\n conversationId: input.conversationId,\n status: 'sent',\n metadata: { response: result.response },\n }\n }\n\n async verifyWebhook(_input: VerifyWebhookInput): Promise<InboundMessage> {\n return { raw: {}, eventType: 'other', metadata: { reason: 'imap-does-not-use-webhooks' } }\n }\n\n async getStatus(_input: GetMessageStatusInput): Promise<MessageStatus> {\n return { status: 'sent' }\n }\n\n async convertOutbound(input: ConvertOutboundInput): Promise<ChannelNativeContent> {\n return convertOutboundForEmail(input)\n }\n\n async normalizeInbound(raw: InboundMessage): Promise<NormalizedInboundMessage> {\n const rawBuffer = pickRawMimeBuffer(raw)\n const accountIdentifier = pickAccountIdentifier(raw)\n const uid = pickUid(raw)\n return normalizeInboundImapMessage({\n rawMessage: rawBuffer,\n uid,\n accountIdentifier,\n })\n }\n\n async validateCredentials(input: ValidateCredentialsInput): Promise<ValidateCredentialsResult> {\n return validateImapCredentials(input.credentials)\n }\n\n async fetchHistory(input: FetchHistoryInput): Promise<HistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const channelState = imapChannelStateSchema.parse(input.channelState ?? {}) satisfies ImapChannelState\n const imap = getImapClient()\n const connection = credentialsToConnection(credentials)\n\n // Spec B \u00A7 Bounded, cursor-driven IMAP inbound:\n // - Bootstrap (no cursor): SELECT INBOX, persist UIDVALIDITY + UIDNEXT,\n // return ZERO messages. Backlog import happens via the explicit\n // `/import-history` endpoint, not via the silent connect flow.\n // - Incremental (cursor exists): UID FETCH `previousUidNext:*`, capped\n // at HARD_CAP = 200. If more available, set `hasMore: true` so the\n // hub re-enqueues immediately and drains the backlog.\n // - UIDVALIDITY mismatch: discard cursor and treat as bootstrap (the\n // mailbox was recreated or renamed; we cannot trust the prior cursor).\n const folderState = await imap.selectInbox(connection)\n const previousUidValidity = toNumberOrUndefined(channelState.uidValidity)\n const previousUidNext = toNumberOrUndefined(channelState.uidNext)\n const serverUidNext = toNumberOrUndefined(folderState.uidNext)\n const HARD_CAP = clampHardCap(input.limit)\n\n const uidValidityMismatch =\n previousUidValidity !== undefined &&\n folderState.uidValidity !== undefined &&\n folderState.uidValidity !== previousUidValidity\n if (uidValidityMismatch) {\n logger.warn('UIDVALIDITY changed for INBOX \u2014 discarding cursor and re-bootstrapping', {\n previousUidValidity,\n uidValidity: folderState.uidValidity,\n })\n }\n const needsBootstrap =\n uidValidityMismatch || previousUidNext === undefined || previousUidNext === null\n\n let fetched: { uid: number; rawBody: Buffer; internalDate?: Date; flags?: string[] }[]\n let hasMore = false\n if (needsBootstrap) {\n // \u2500\u2500 Bootstrap: persist cursor only, fetch zero messages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Spec B \u00A7 Bootstrap. The \"1M inbox\" failure mode is fixed by\n // construction: a fresh user sees zero history until they explicitly\n // request `/import-history`. Set `hasMore: false` so the poll worker\n // does NOT immediately re-enqueue.\n fetched = []\n hasMore = false\n } else if (previousUidNext !== undefined && serverUidNext !== undefined && previousUidNext >= serverUidNext) {\n // \u2500\u2500 Idle: UIDNEXT did not advance, so there is no new mail \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Skip the FETCH entirely. IMAP `<n>:*` always matches at least the\n // highest existing UID, so an idle mailbox would otherwise re-fetch and\n // re-normalize one already-ingested message every tick. The cursor is\n // retained downstream (an empty fetch does not advance it).\n fetched = []\n hasMore = false\n } else {\n // \u2500\u2500 Incremental: UID FETCH previousUidNext:* up to HARD_CAP \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // On a mature mailbox this is typically 0-N UIDs. The HARD_CAP bounds\n // the per-poll wall-clock + DB transaction size; if more remain, the\n // hub re-enqueues us immediately via `hasMore: true`.\n const range = `${previousUidNext}:*`\n // Fetch up to HARD_CAP + 1 so we can detect whether more remain\n // without paying for an extra round-trip later.\n const probeLimit = HARD_CAP + 1\n const raw = await imap.fetchUidRange(connection, range, { limit: probeLimit })\n if (raw.length > HARD_CAP) {\n fetched = raw.slice(0, HARD_CAP)\n hasMore = true\n } else {\n fetched = raw\n hasMore = false\n }\n }\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n // INTERNALDATE is when the server received the message; the MIME Date\n // header is the sender's and must not date the platform message (#6095).\n receivedAt: item.internalDate,\n })\n messages.push(normalized)\n }\n\n // Cursor advancement contract:\n // - Bootstrap: persist the server's current UIDNEXT so the next poll\n // becomes incremental from this point onward (intentionally skips the\n // pre-existing backlog; use `/import-history` to pull it).\n // - Incremental: persist `highestFetchedUid + 1` \u2014 NEVER the server's\n // UIDNEXT. When `fetched` is empty we retain `previousUidNext` so the\n // next poll resumes from the same point.\n const advancedUidNext = (() => {\n if (needsBootstrap) return serverUidNext\n if (fetched.length === 0) return previousUidNext\n const highest = fetched.reduce((max, item) => (item.uid > max ? item.uid : max), 0)\n // Anchor the cursor to the highest UID we ACTUALLY fetched \u2014 never to the\n // server's UIDNEXT. Providers like Gmail report a UIDNEXT that runs ahead\n // of the highest message currently in the folder (UID gaps from\n // labels/threads, or a message materialising into INBOX at a UID below\n // UIDNEXT). Jumping the cursor to `serverUidNext` then steps over any\n // INBOX message that sits below it: that message lands permanently below\n // the cursor and is never fetched again \u2014 the bug that silently dropped\n // inbound replies (UID 61979 skipped while cursor jumped to 61981).\n // `highest + 1` guarantees we never step over an unfetched message; if the\n // server's UIDNEXT is higher, the next poll just re-scans `highest+1:*`\n // (idempotent \u2014 the hub dedups on (channel_id, external_message_id)).\n return highest + 1\n })()\n const nextChannelState: ImapChannelState = {\n uidValidity: folderState.uidValidity ?? previousUidValidity,\n uidNext: advancedUidNext,\n lastFolder: 'INBOX',\n }\n\n // The hub's polling worker re-reads cursor through `fetchHistory`. We embed the\n // next-poll state in `nextCursor` (base64-encoded JSON) so workers can persist\n // it onto `CommunicationChannel.channelState` without depending on a hub-specific\n // contract beyond the existing `HistoryPage` shape.\n const nextCursor = encodeCursor(nextChannelState)\n return { messages, nextCursor, hasMore }\n }\n\n async importHistory(input: ImportHistoryInput): Promise<ImportHistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const connection = credentialsToConnection(credentials)\n const imap = getImapClient()\n\n // Ceiling is provider-specific and lower than the deployment-wide one:\n // this adapter's cursor embeds the full remaining UID list, so it stays\n // capped until it gets a paging cursor of its own. See\n // `import-history-limits.ts` for the shared source of truth.\n const limits = getImportHistoryLimits(this.providerKey)\n\n const sinceDaysRaw = Number.isFinite(input.sinceDays) ? Math.trunc(input.sinceDays) : limits.defaultSinceDays\n const sinceDays = Math.max(1, Math.min(limits.maxSinceDays, sinceDaysRaw))\n const sinceDate = new Date(Date.now() - sinceDays * 24 * 60 * 60 * 1000)\n\n const maxMessagesRaw = Number.isFinite(input.maxMessages)\n ? Math.trunc(input.maxMessages as number)\n : limits.defaultMaxMessages\n const maxMessages = Math.max(1, Math.min(limits.maxMessages, maxMessagesRaw))\n\n const PAGE_SIZE = clampHardCap(undefined)\n\n // Resume previous page or perform initial SEARCH on first call. The cursor\n // encodes the full remaining UID list discovered server-side so subsequent\n // pages don't re-issue SEARCH (which on large mailboxes is expensive).\n let allUids: number[]\n let remainingUids: number[]\n let collectedSoFar: number\n let totalCandidates: number | undefined\n const cursor = decodeImportCursor(input.cursor)\n if (cursor) {\n remainingUids = cursor.remaining\n collectedSoFar = cursor.collected\n totalCandidates = cursor.total\n allUids = cursor.remaining\n } else {\n // FROM-chunking: SEARCH with very long OR chains can blow imapflow's\n // tag-buffer; chunk to \u226430 senders and union the results. When\n // contactEmails is empty we issue a single SINCE-only search.\n const senders = (input.contactEmails ?? []).filter((s): s is string => typeof s === 'string' && s.includes('@'))\n const uidSet = new Set<number>()\n if (senders.length === 0) {\n const uids = await imap.searchUidsByFromAndSince(connection, { sinceDate })\n for (const uid of uids) uidSet.add(uid)\n } else {\n const CHUNK_SIZE = 30\n for (let i = 0; i < senders.length; i += CHUNK_SIZE) {\n const chunk = senders.slice(i, i + CHUNK_SIZE)\n const uids = await imap.searchUidsByFromAndSince(connection, { fromAddresses: chunk, sinceDate })\n for (const uid of uids) uidSet.add(uid)\n }\n }\n // Process newest first (highest UIDs ~= most recent on standard servers).\n allUids = Array.from(uidSet).sort((a, b) => b - a).slice(0, maxMessages)\n remainingUids = allUids\n collectedSoFar = 0\n totalCandidates = allUids.length\n }\n\n if (remainingUids.length === 0) {\n return { messages: [], hasMore: false, totalCandidates }\n }\n\n const batchUids = remainingUids.slice(0, PAGE_SIZE)\n const stillRemaining = remainingUids.slice(PAGE_SIZE)\n const uidSetExpression = batchUids.join(',')\n const fetched = await imap.fetchUidRange(connection, uidSetExpression, { limit: PAGE_SIZE })\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n // INTERNALDATE is when the server received the message; the MIME Date\n // header is the sender's and must not date the platform message (#6095).\n receivedAt: item.internalDate,\n })\n messages.push(normalized)\n }\n\n const newCollected = collectedSoFar + messages.length\n const hasMore = stillRemaining.length > 0 && newCollected < maxMessages\n const nextCursor = hasMore\n ? encodeImportCursor({ remaining: stillRemaining, collected: newCollected, total: totalCandidates })\n : undefined\n\n return { messages, nextCursor, hasMore, totalCandidates }\n }\n\n async resolveContact(input: ResolveContactInput): Promise<ContactHint | null> {\n return emailResolveContact(input)\n }\n}\n\nfunction parseCredentialsOrThrow(value: unknown): ImapCredentials {\n const parsed = imapCredentialsSchema.safeParse(value)\n if (!parsed.success) {\n const first = parsed.error.issues[0]\n throw new Error(`Invalid IMAP credentials: ${first?.message ?? 'unknown validation error'}`)\n }\n return parsed.data\n}\n\nfunction asBuffer(bytes: Uint8Array): Buffer {\n return Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n}\n\nfunction pickRawMimeBuffer(raw: InboundMessage): Buffer {\n const candidate = raw.raw as { rawBody?: unknown; mime?: unknown }\n const value = candidate?.rawBody ?? candidate?.mime ?? raw.raw\n if (Buffer.isBuffer(value)) return value\n if (value instanceof Uint8Array) return Buffer.from(value)\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n throw new Error('[internal] IMAP normalizeInbound requires `raw.rawBody` to be a Buffer or string MIME payload')\n}\n\nfunction pickAccountIdentifier(raw: InboundMessage): string {\n const candidate = raw.raw as { accountIdentifier?: unknown }\n const id = typeof candidate?.accountIdentifier === 'string' ? candidate.accountIdentifier : undefined\n return id ?? 'unknown@imap'\n}\n\nfunction pickUid(raw: InboundMessage): number | undefined {\n const candidate = raw.raw as { uid?: unknown }\n return typeof candidate?.uid === 'number' ? candidate.uid : undefined\n}\n\n/**\n * Spec B \u00A7 HARD_CAP. Bound each poll's wall-clock + DB transaction size.\n * Honor the caller's `limit` hint but never exceed `HARD_CAP_MAX`. A\n * single poll will fetch at most this many UIDs; if more remain we set\n * `hasMore: true` and the hub re-enqueues immediately.\n *\n * Configurable via `OM_CHANNEL_IMAP_HARD_CAP_PER_POLL` (default 200).\n */\nfunction clampHardCap(callerLimit: number | undefined): number {\n const envOverride = Number.parseInt(process.env.OM_CHANNEL_IMAP_HARD_CAP_PER_POLL ?? '', 10)\n const HARD_CAP_MAX = Number.isFinite(envOverride) && envOverride > 0 ? envOverride : 200\n if (typeof callerLimit === 'number' && callerLimit > 0) {\n return Math.min(callerLimit, HARD_CAP_MAX)\n }\n return HARD_CAP_MAX\n}\n\ninterface ImportCursor {\n remaining: number[]\n collected: number\n total?: number\n}\n\nfunction encodeImportCursor(cursor: ImportCursor): string {\n return encodeCursor(cursor)\n}\n\nfunction decodeImportCursor(value: string | undefined): ImportCursor | null {\n const parsed = decodeCursor(value)\n if (!parsed || typeof parsed !== 'object') return null\n const obj = parsed as { remaining?: unknown; collected?: unknown; total?: unknown }\n const remaining = Array.isArray(obj.remaining)\n ? obj.remaining.filter((n): n is number => typeof n === 'number' && Number.isFinite(n))\n : []\n const collected = typeof obj.collected === 'number' ? obj.collected : 0\n const total = typeof obj.total === 'number' ? obj.total : undefined\n return { remaining, collected, total }\n}\n\nfunction toNumberOrUndefined(value: unknown): number | undefined {\n if (typeof value === 'number') return value\n if (typeof value === 'string' && value.length > 0) {\n const n = Number(value)\n return Number.isFinite(n) ? n : undefined\n }\n return undefined\n}\n\nlet cachedAdapter: ImapChannelAdapter | null = null\n\nexport function getImapChannelAdapter(): ImapChannelAdapter {\n if (!cachedAdapter) cachedAdapter = new ImapChannelAdapter()\n return cachedAdapter\n}\n\nexport { ImapChannelAdapter }\n"],
5
- "mappings": "AAoBA,SAAS,wBAAwB;AACjC,SAAS,uBAAuB,8BAA2E;AAC3G;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP,SAAS,+BAA+B;AACxC,SAAS,mCAAmC;AAC5C,SAAS,+BAA+B;AACxC,SAAS,2BAA2B;AACpC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sCAAsC;AAC/C,SAAS,8BAA8B;AACvC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,cAAc;AAe1C,MAAM,mBAA6C;AAAA,EAAnD;AACE,SAAS,cAAc;AACvB,SAAS,cAAc;AACvB,SAAS,eAAe;AAAA;AAAA,EAExB,MAAM,YAAY,OAAqD;AACrE,UAAM,cAAc,wBAAwB,MAAM,WAAW;AAE7D,QAAI;AACJ,QAAI;AACF,eAAS,MAAM,wBAAwB;AAAA,QACrC,MAAM,MAAM,QAAQ,QAAQ,MAAM,QAAQ,QAAQ;AAAA,QAClD,YAAY,MAAM,QAAQ,eAAe,MAAM,QAAQ,OAAO,SAAS;AAAA,QACvE,aAAa,MAAM,QAAQ;AAAA,QAC3B,iBAAiB,MAAM;AAAA,MACzB,CAAC;AAAA,IACH,SAAS,OA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AQ,WAAW,UAAU,MAAM;AAC9D;AAUA,SAAS,aAAa,aAAyC;AAC7D,QAAM,cAAc,OAAO,SAAS,QAAQ,IAAI,qCAAqC,IAAI,EAAE;AAC3F,QAAM,eAAe,OAAO,SAAS,WAAW,KAAK,cAAc,IAAI,cAAc;AACrF,MAAI,OAAO,gBAAgB,YAAY,cAAc,GAAG;AACtD,WAAO,KAAK,IAAI,aAAa,YAAY;AAAA,EAC3C;AACA,SAAO;AACT;AAQA,SAAS,mBAAmB,QAA8B;AACxD,SAAO,aAAa,MAAM;AAC5B;AAEA,SAAS,mBAAmB,OAAgD;AAC1E,QAAM,SAAS,aAAa,KAAK;AACjC,MAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;AAClD,QAAM,MAAM;AACZ,QAAM,YAAY,MAAM,QAAQ,IAAI,SAAS,IACzC,IAAI,UAAU,OAAO,CAAC,MAAmB,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC,CAAC,IACpF,CAAC;AACL,QAAM,YAAY,OAAO,IAAI,cAAc,WAAW,IAAI,YAAY;AACtE,QAAM,QAAQ,OAAO,IAAI,UAAU,WAAW,IAAI,QAAQ;AAC1D,SAAO,EAAE,WAAW,WAAW,MAAM;AACvC;AAEA,SAAS,oBAAoB,OAAoC;AAC/D,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,MAAI,OAAO,UAAU,YAAY,MAAM,SAAS,GAAG;AACjD,UAAM,IAAI,OAAO,KAAK;AACtB,WAAO,OAAO,SAAS,CAAC,IAAI,IAAI;AAAA,EAClC;AACA,SAAO;AACT;AAEA,IAAI,gBAA2C;AAExC,SAAS,wBAA4C;AAC1D,MAAI,CAAC,cAAe,iBAAgB,IAAI,mBAAmB;AAC3D,SAAO;AACT;",
4
+ "sourcesContent": ["import type {\n ChannelAdapter,\n ConvertOutboundInput,\n ChannelNativeContent,\n FetchHistoryInput,\n GetMessageStatusInput,\n HistoryPage,\n ImportHistoryInput,\n ImportHistoryPage,\n InboundMessage,\n MessageStatus,\n NormalizedInboundMessage,\n ResolveContactInput,\n ContactHint,\n SendMessageInput,\n SendMessageResult,\n ValidateCredentialsInput,\n ValidateCredentialsResult,\n VerifyWebhookInput,\n} from '@open-mercato/core/modules/communication_channels/lib/adapter'\nimport { imapCapabilities } from './capabilities'\nimport { imapCredentialsSchema, imapChannelStateSchema, type ImapCredentials, type ImapChannelState } from './credentials'\nimport {\n credentialsToConnection,\n getImapClient,\n} from './imap-client'\nimport {\n credentialsToSmtpConnection,\n getSmtpClient,\n isSmtpMessageTooLarge,\n type SmtpMessage,\n type SmtpSendResult,\n} from './smtp-client'\nimport { convertOutboundForEmail } from './convert-outbound'\nimport { normalizeInboundImapMessage } from './normalize-inbound'\nimport { validateImapCredentials } from './validate-credentials'\nimport { emailResolveContact } from '@open-mercato/core/modules/communication_channels/lib/email-contact'\nimport {\n decodeCursor,\n encodeCursor,\n sanitizeHeaderValue,\n} from '@open-mercato/core/modules/communication_channels/lib/email-mime'\nimport { resolveOutboundAttachmentBytes } from '@open-mercato/core/modules/communication_channels/lib/outbound-attachments'\nimport { getImportHistoryLimits } from '@open-mercato/core/modules/communication_channels/lib/import-history-limits'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('channel_imap')\n\n/**\n * IMAP+SMTP `ChannelAdapter`. Inbound is polling-driven (`realtimePush: false`),\n * outbound is SMTP. Threading is RFC2822 (In-Reply-To / References).\n *\n * Why this adapter omits some methods:\n * - `verifyWebhook` \u2014 IMAP has no webhook; we return a no-op event with\n * `eventType: 'other'` so the hub's webhook route returns 202 if anyone\n * ever POSTs at `/api/communication_channels/webhook/imap`.\n * - `getStatus` \u2014 IMAP has no delivery-status concept beyond `\\Seen`; we\n * return `{ status: 'sent' }` as a best-effort placeholder.\n * - No `sendReaction` / `editMessage` / `deleteMessage` \u2014 email doesn't\n * support these capabilities.\n */\nclass ImapChannelAdapter implements ChannelAdapter {\n readonly providerKey = 'imap'\n readonly channelType = 'email'\n readonly capabilities = imapCapabilities\n\n async sendMessage(input: SendMessageInput): Promise<SendMessageResult> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n\n let native: ChannelNativeContent\n try {\n native = await convertOutboundForEmail({\n body: input.content.html ?? input.content.text ?? '',\n bodyFormat: input.content.bodyFormat ?? (input.content.html ? 'html' : 'text'),\n attachments: input.content.attachments,\n channelMetadata: input.metadata,\n })\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Outbound conversion failed'\n return { externalMessageId: '', status: 'failed', error: message }\n }\n const meta = (native.metadata ?? {}) as Record<string, unknown>\n const to = Array.isArray(meta.to) ? (meta.to as string[]) : []\n if (to.length === 0) {\n return { externalMessageId: '', status: 'failed', error: '[internal] Email send requires at least one recipient' }\n }\n\n // Attachments arrive as bytes the hub read from the attachments module (or\n // as `data:` URLs); a remote URL is refused rather than fetched.\n let attachments: SmtpMessage['attachments']\n try {\n attachments = native.content.attachments?.map((attachment) => ({\n filename: sanitizeHeaderValue(attachment.fileName) || 'attachment',\n content: asBuffer(resolveOutboundAttachmentBytes(attachment)),\n contentType: attachment.mimeType,\n inline: attachment.inline,\n }))\n } catch (error) {\n const message = error instanceof Error ? error.message : '[internal] Outbound attachment is not readable'\n return { externalMessageId: '', status: 'failed', error: message }\n }\n\n const smtp = getSmtpClient()\n let result: SmtpSendResult\n try {\n result = await smtp.send(credentialsToSmtpConnection(credentials), {\n from: credentials.fromAddress,\n to,\n cc: Array.isArray(meta.cc) ? (meta.cc as string[]) : undefined,\n bcc: Array.isArray(meta.bcc) ? (meta.bcc as string[]) : undefined,\n subject: typeof meta.subject === 'string' ? (meta.subject as string) : undefined,\n text: native.content.text,\n html: native.content.html,\n inReplyTo: typeof meta.inReplyTo === 'string' ? (meta.inReplyTo as string) : undefined,\n references: Array.isArray(meta.references) ? (meta.references as string[]) : undefined,\n messageId: typeof meta.messageId === 'string' ? (meta.messageId as string) : undefined,\n attachments,\n })\n } catch (error) {\n if (isSmtpMessageTooLarge(error)) {\n return {\n externalMessageId: '',\n status: 'failed',\n error: '[internal] The mail server rejected the message as too large (message_too_large)',\n }\n }\n throw error\n }\n\n // Best-effort append to Sent \u2014 many servers auto-store via \"Submission\" but not all do.\n const imap = getImapClient()\n try {\n // Skip when the RFC2822 bytes are empty (MailComposer build failed upstream):\n // appending a 0-byte buffer would create a corrupt Sent-folder entry, and the\n // send itself already succeeded.\n if (result.raw.length > 0) {\n await imap.appendSent(credentialsToConnection(credentials), result.raw)\n }\n } catch (appendError) {\n // Best-effort: many servers auto-store sent mail via Submission, and the\n // Sent mailbox name is provider-specific (localized, or \"[Gmail]/Sent Mail\").\n // Log so operators can diagnose missing Sent-folder archival rather than\n // failing the send.\n logger.warn('failed to append outbound message to Sent folder', { err: appendError })\n }\n\n const rejected = result.rejected ?? []\n if (rejected.length > 0) {\n const accepted = result.accepted ?? []\n return {\n externalMessageId: result.messageId,\n conversationId: input.conversationId,\n status: 'failed',\n error: `SMTP_SEND_PARTIALLY_REJECTED: relay rejected ${rejected.length} of ${accepted.length + rejected.length} recipients`,\n metadata: { ...(result.response ? { response: result.response } : {}), accepted, rejected },\n }\n }\n\n return {\n externalMessageId: result.messageId,\n conversationId: input.conversationId,\n status: 'sent',\n metadata: { response: result.response },\n }\n }\n\n async verifyWebhook(_input: VerifyWebhookInput): Promise<InboundMessage> {\n return { raw: {}, eventType: 'other', metadata: { reason: 'imap-does-not-use-webhooks' } }\n }\n\n async getStatus(_input: GetMessageStatusInput): Promise<MessageStatus> {\n return { status: 'sent' }\n }\n\n async convertOutbound(input: ConvertOutboundInput): Promise<ChannelNativeContent> {\n return convertOutboundForEmail(input)\n }\n\n async normalizeInbound(raw: InboundMessage): Promise<NormalizedInboundMessage> {\n const rawBuffer = pickRawMimeBuffer(raw)\n const accountIdentifier = pickAccountIdentifier(raw)\n const uid = pickUid(raw)\n return normalizeInboundImapMessage({\n rawMessage: rawBuffer,\n uid,\n accountIdentifier,\n })\n }\n\n async validateCredentials(input: ValidateCredentialsInput): Promise<ValidateCredentialsResult> {\n return validateImapCredentials(input.credentials)\n }\n\n async fetchHistory(input: FetchHistoryInput): Promise<HistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const channelState = imapChannelStateSchema.parse(input.channelState ?? {}) satisfies ImapChannelState\n const imap = getImapClient()\n const connection = credentialsToConnection(credentials)\n\n // Spec B \u00A7 Bounded, cursor-driven IMAP inbound:\n // - Bootstrap (no cursor): SELECT INBOX, persist UIDVALIDITY + UIDNEXT,\n // return ZERO messages. Backlog import happens via the explicit\n // `/import-history` endpoint, not via the silent connect flow.\n // - Incremental (cursor exists): UID FETCH `previousUidNext:*`, capped\n // at HARD_CAP = 200. If more available, set `hasMore: true` so the\n // hub re-enqueues immediately and drains the backlog.\n // - UIDVALIDITY mismatch: discard cursor and treat as bootstrap (the\n // mailbox was recreated or renamed; we cannot trust the prior cursor).\n const folderState = await imap.selectInbox(connection)\n const previousUidValidity = toNumberOrUndefined(channelState.uidValidity)\n const previousUidNext = toNumberOrUndefined(channelState.uidNext)\n const serverUidNext = toNumberOrUndefined(folderState.uidNext)\n const HARD_CAP = clampHardCap(input.limit)\n\n const uidValidityMismatch =\n previousUidValidity !== undefined &&\n folderState.uidValidity !== undefined &&\n folderState.uidValidity !== previousUidValidity\n if (uidValidityMismatch) {\n logger.warn('UIDVALIDITY changed for INBOX \u2014 discarding cursor and re-bootstrapping', {\n previousUidValidity,\n uidValidity: folderState.uidValidity,\n })\n }\n const needsBootstrap =\n uidValidityMismatch || previousUidNext === undefined || previousUidNext === null\n\n let fetched: { uid: number; rawBody: Buffer; internalDate?: Date; flags?: string[] }[]\n let hasMore = false\n if (needsBootstrap) {\n // \u2500\u2500 Bootstrap: persist cursor only, fetch zero messages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Spec B \u00A7 Bootstrap. The \"1M inbox\" failure mode is fixed by\n // construction: a fresh user sees zero history until they explicitly\n // request `/import-history`. Set `hasMore: false` so the poll worker\n // does NOT immediately re-enqueue.\n fetched = []\n hasMore = false\n } else if (previousUidNext !== undefined && serverUidNext !== undefined && previousUidNext >= serverUidNext) {\n // \u2500\u2500 Idle: UIDNEXT did not advance, so there is no new mail \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Skip the FETCH entirely. IMAP `<n>:*` always matches at least the\n // highest existing UID, so an idle mailbox would otherwise re-fetch and\n // re-normalize one already-ingested message every tick. The cursor is\n // retained downstream (an empty fetch does not advance it).\n fetched = []\n hasMore = false\n } else {\n // \u2500\u2500 Incremental: UID FETCH previousUidNext:* up to HARD_CAP \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // On a mature mailbox this is typically 0-N UIDs. The HARD_CAP bounds\n // the per-poll wall-clock + DB transaction size; if more remain, the\n // hub re-enqueues us immediately via `hasMore: true`.\n const range = `${previousUidNext}:*`\n // Fetch up to HARD_CAP + 1 so we can detect whether more remain\n // without paying for an extra round-trip later.\n const probeLimit = HARD_CAP + 1\n const raw = await imap.fetchUidRange(connection, range, { limit: probeLimit })\n if (raw.length > HARD_CAP) {\n fetched = raw.slice(0, HARD_CAP)\n hasMore = true\n } else {\n fetched = raw\n hasMore = false\n }\n }\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n // INTERNALDATE is when the server received the message; the MIME Date\n // header is the sender's and must not date the platform message (#6095).\n receivedAt: item.internalDate,\n })\n messages.push(normalized)\n }\n\n // Cursor advancement contract:\n // - Bootstrap: persist the server's current UIDNEXT so the next poll\n // becomes incremental from this point onward (intentionally skips the\n // pre-existing backlog; use `/import-history` to pull it).\n // - Incremental: persist `highestFetchedUid + 1` \u2014 NEVER the server's\n // UIDNEXT. When `fetched` is empty we retain `previousUidNext` so the\n // next poll resumes from the same point.\n const advancedUidNext = (() => {\n if (needsBootstrap) return serverUidNext\n if (fetched.length === 0) return previousUidNext\n const highest = fetched.reduce((max, item) => (item.uid > max ? item.uid : max), 0)\n // Anchor the cursor to the highest UID we ACTUALLY fetched \u2014 never to the\n // server's UIDNEXT. Providers like Gmail report a UIDNEXT that runs ahead\n // of the highest message currently in the folder (UID gaps from\n // labels/threads, or a message materialising into INBOX at a UID below\n // UIDNEXT). Jumping the cursor to `serverUidNext` then steps over any\n // INBOX message that sits below it: that message lands permanently below\n // the cursor and is never fetched again \u2014 the bug that silently dropped\n // inbound replies (UID 61979 skipped while cursor jumped to 61981).\n // `highest + 1` guarantees we never step over an unfetched message; if the\n // server's UIDNEXT is higher, the next poll just re-scans `highest+1:*`\n // (idempotent \u2014 the hub dedups on (channel_id, external_message_id)).\n return highest + 1\n })()\n const nextChannelState: ImapChannelState = {\n uidValidity: folderState.uidValidity ?? previousUidValidity,\n uidNext: advancedUidNext,\n lastFolder: 'INBOX',\n }\n\n // The hub's polling worker re-reads cursor through `fetchHistory`. We embed the\n // next-poll state in `nextCursor` (base64-encoded JSON) so workers can persist\n // it onto `CommunicationChannel.channelState` without depending on a hub-specific\n // contract beyond the existing `HistoryPage` shape.\n const nextCursor = encodeCursor(nextChannelState)\n return { messages, nextCursor, hasMore }\n }\n\n async importHistory(input: ImportHistoryInput): Promise<ImportHistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const connection = credentialsToConnection(credentials)\n const imap = getImapClient()\n\n // Ceiling is provider-specific and lower than the deployment-wide one:\n // this adapter's cursor embeds the full remaining UID list, so it stays\n // capped until it gets a paging cursor of its own. See\n // `import-history-limits.ts` for the shared source of truth.\n const limits = getImportHistoryLimits(this.providerKey)\n\n const sinceDaysRaw = Number.isFinite(input.sinceDays) ? Math.trunc(input.sinceDays) : limits.defaultSinceDays\n const sinceDays = Math.max(1, Math.min(limits.maxSinceDays, sinceDaysRaw))\n const sinceDate = new Date(Date.now() - sinceDays * 24 * 60 * 60 * 1000)\n\n const maxMessagesRaw = Number.isFinite(input.maxMessages)\n ? Math.trunc(input.maxMessages as number)\n : limits.defaultMaxMessages\n const maxMessages = Math.max(1, Math.min(limits.maxMessages, maxMessagesRaw))\n\n const PAGE_SIZE = clampHardCap(undefined)\n\n // Resume previous page or perform initial SEARCH on first call. The cursor\n // encodes the full remaining UID list discovered server-side so subsequent\n // pages don't re-issue SEARCH (which on large mailboxes is expensive).\n let allUids: number[]\n let remainingUids: number[]\n let collectedSoFar: number\n let totalCandidates: number | undefined\n const cursor = decodeImportCursor(input.cursor)\n if (cursor) {\n remainingUids = cursor.remaining\n collectedSoFar = cursor.collected\n totalCandidates = cursor.total\n allUids = cursor.remaining\n } else {\n // FROM-chunking: SEARCH with very long OR chains can blow imapflow's\n // tag-buffer; chunk to \u226430 senders and union the results. When\n // contactEmails is empty we issue a single SINCE-only search.\n const senders = (input.contactEmails ?? []).filter((s): s is string => typeof s === 'string' && s.includes('@'))\n const uidSet = new Set<number>()\n if (senders.length === 0) {\n const uids = await imap.searchUidsByFromAndSince(connection, { sinceDate })\n for (const uid of uids) uidSet.add(uid)\n } else {\n const CHUNK_SIZE = 30\n for (let i = 0; i < senders.length; i += CHUNK_SIZE) {\n const chunk = senders.slice(i, i + CHUNK_SIZE)\n const uids = await imap.searchUidsByFromAndSince(connection, { fromAddresses: chunk, sinceDate })\n for (const uid of uids) uidSet.add(uid)\n }\n }\n // Process newest first (highest UIDs ~= most recent on standard servers).\n allUids = Array.from(uidSet).sort((a, b) => b - a).slice(0, maxMessages)\n remainingUids = allUids\n collectedSoFar = 0\n totalCandidates = allUids.length\n }\n\n if (remainingUids.length === 0) {\n return { messages: [], hasMore: false, totalCandidates }\n }\n\n const batchUids = remainingUids.slice(0, PAGE_SIZE)\n const stillRemaining = remainingUids.slice(PAGE_SIZE)\n const uidSetExpression = batchUids.join(',')\n const fetched = await imap.fetchUidRange(connection, uidSetExpression, { limit: PAGE_SIZE })\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n // INTERNALDATE is when the server received the message; the MIME Date\n // header is the sender's and must not date the platform message (#6095).\n receivedAt: item.internalDate,\n })\n messages.push(normalized)\n }\n\n const newCollected = collectedSoFar + messages.length\n const hasMore = stillRemaining.length > 0 && newCollected < maxMessages\n const nextCursor = hasMore\n ? encodeImportCursor({ remaining: stillRemaining, collected: newCollected, total: totalCandidates })\n : undefined\n\n return { messages, nextCursor, hasMore, totalCandidates }\n }\n\n async resolveContact(input: ResolveContactInput): Promise<ContactHint | null> {\n return emailResolveContact(input)\n }\n}\n\nfunction parseCredentialsOrThrow(value: unknown): ImapCredentials {\n const parsed = imapCredentialsSchema.safeParse(value)\n if (!parsed.success) {\n const first = parsed.error.issues[0]\n throw new Error(`Invalid IMAP credentials: ${first?.message ?? 'unknown validation error'}`)\n }\n return parsed.data\n}\n\nfunction asBuffer(bytes: Uint8Array): Buffer {\n return Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n}\n\nfunction pickRawMimeBuffer(raw: InboundMessage): Buffer {\n const candidate = raw.raw as { rawBody?: unknown; mime?: unknown }\n const value = candidate?.rawBody ?? candidate?.mime ?? raw.raw\n if (Buffer.isBuffer(value)) return value\n if (value instanceof Uint8Array) return Buffer.from(value)\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n throw new Error('[internal] IMAP normalizeInbound requires `raw.rawBody` to be a Buffer or string MIME payload')\n}\n\nfunction pickAccountIdentifier(raw: InboundMessage): string {\n const candidate = raw.raw as { accountIdentifier?: unknown }\n const id = typeof candidate?.accountIdentifier === 'string' ? candidate.accountIdentifier : undefined\n return id ?? 'unknown@imap'\n}\n\nfunction pickUid(raw: InboundMessage): number | undefined {\n const candidate = raw.raw as { uid?: unknown }\n return typeof candidate?.uid === 'number' ? candidate.uid : undefined\n}\n\n/**\n * Spec B \u00A7 HARD_CAP. Bound each poll's wall-clock + DB transaction size.\n * Honor the caller's `limit` hint but never exceed `HARD_CAP_MAX`. A\n * single poll will fetch at most this many UIDs; if more remain we set\n * `hasMore: true` and the hub re-enqueues immediately.\n *\n * Configurable via `OM_CHANNEL_IMAP_HARD_CAP_PER_POLL` (default 200).\n */\nfunction clampHardCap(callerLimit: number | undefined): number {\n const envOverride = Number.parseInt(process.env.OM_CHANNEL_IMAP_HARD_CAP_PER_POLL ?? '', 10)\n const HARD_CAP_MAX = Number.isFinite(envOverride) && envOverride > 0 ? envOverride : 200\n if (typeof callerLimit === 'number' && callerLimit > 0) {\n return Math.min(callerLimit, HARD_CAP_MAX)\n }\n return HARD_CAP_MAX\n}\n\ninterface ImportCursor {\n remaining: number[]\n collected: number\n total?: number\n}\n\nfunction encodeImportCursor(cursor: ImportCursor): string {\n return encodeCursor(cursor)\n}\n\nfunction decodeImportCursor(value: string | undefined): ImportCursor | null {\n const parsed = decodeCursor(value)\n if (!parsed || typeof parsed !== 'object') return null\n const obj = parsed as { remaining?: unknown; collected?: unknown; total?: unknown }\n const remaining = Array.isArray(obj.remaining)\n ? obj.remaining.filter((n): n is number => typeof n === 'number' && Number.isFinite(n))\n : []\n const collected = typeof obj.collected === 'number' ? obj.collected : 0\n const total = typeof obj.total === 'number' ? obj.total : undefined\n return { remaining, collected, total }\n}\n\nfunction toNumberOrUndefined(value: unknown): number | undefined {\n if (typeof value === 'number') return value\n if (typeof value === 'string' && value.length > 0) {\n const n = Number(value)\n return Number.isFinite(n) ? n : undefined\n }\n return undefined\n}\n\nlet cachedAdapter: ImapChannelAdapter | null = null\n\nexport function getImapChannelAdapter(): ImapChannelAdapter {\n if (!cachedAdapter) cachedAdapter = new ImapChannelAdapter()\n return cachedAdapter\n}\n\nexport { ImapChannelAdapter }\n"],
5
+ "mappings": "AAoBA,SAAS,wBAAwB;AACjC,SAAS,uBAAuB,8BAA2E;AAC3G;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP,S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,YAAY,cAAc,GAAG;AACtD,WAAO,KAAK,IAAI,aAAa,YAAY;AAAA,EAC3C;AACA,SAAO;AACT;AAQA,SAAS,mBAAmB,QAA8B;AACxD,SAAO,aAAa,MAAM;AAC5B;AAEA,SAAS,mBAAmB,OAAgD;AAC1E,QAAM,SAAS,aAAa,KAAK;AACjC,MAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;AAClD,QAAM,MAAM;AACZ,QAAM,YAAY,MAAM,QAAQ,IAAI,SAAS,IACzC,IAAI,UAAU,OAAO,CAAC,MAAmB,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC,CAAC,IACpF,CAAC;AACL,QAAM,YAAY,OAAO,IAAI,cAAc,WAAW,IAAI,YAAY;AACtE,QAAM,QAAQ,OAAO,IAAI,UAAU,WAAW,IAAI,QAAQ;AAC1D,SAAO,EAAE,WAAW,WAAW,MAAM;AACvC;AAEA,SAAS,oBAAoB,OAAoC;AAC/D,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,MAAI,OAAO,UAAU,YAAY,MAAM,SAAS,GAAG;AACjD,UAAM,IAAI,OAAO,KAAK;AACtB,WAAO,OAAO,SAAS,CAAC,IAAI,IAAI;AAAA,EAClC;AACA,SAAO;AACT;AAEA,IAAI,gBAA2C;AAExC,SAAS,wBAA4C;AAC1D,MAAI,CAAC,cAAe,iBAAgB,IAAI,mBAAmB;AAC3D,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -86,7 +86,7 @@ class NodemailerClient {
86
86
  const info = await transporter.sendMail(mailOptions);
87
87
  const id = info.messageId ?? composedMessageId ?? info.envelope?.messageId;
88
88
  if (!id) throw new Error("[internal] SMTP server did not return a Message-ID");
89
- return { messageId: id, raw, response: info.response };
89
+ return { messageId: id, raw, response: info.response, accepted: info.accepted, rejected: info.rejected };
90
90
  } finally {
91
91
  transporter.close();
92
92
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/channel_imap/lib/smtp-client.ts"],
4
- "sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { ImapCredentials } from './credentials'\nimport { resolveSafeHostAddress } from './host-pinning'\nimport { assertTransportAllowed } from './transport'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('channel_imap')\n\n/**\n * Outbound SMTP client wrapper. Same trade-offs as `imap-client.ts`: we wrap\n * `nodemailer` behind a tiny interface so tests can swap in a mock and the\n * adapter doesn't import SDK types directly.\n */\n\nexport interface SmtpConnectionOptions {\n host: string\n port: number\n user: string\n pass: string\n transport: 'tls' | 'starttls' | 'none'\n timeoutMs?: number\n}\n\nexport interface SmtpMessage {\n from: string\n to: string[]\n cc?: string[]\n bcc?: string[]\n subject?: string\n text?: string\n html?: string\n /** RFC2822 Message-ID; if omitted the client generates one from the sender domain. */\n messageId?: string\n /** RFC2822 In-Reply-To (single value). */\n inReplyTo?: string\n /** RFC2822 References (whitespace-delimited list). */\n references?: string[]\n attachments?: Array<{\n filename: string\n content: Buffer\n contentType?: string\n cid?: string\n inline?: boolean\n }>\n headers?: Record<string, string>\n}\n\nexport interface SmtpSendResult {\n /** Effective Message-ID. */\n messageId: string\n /** Raw RFC2822 message buffer (used for Sent-folder append). */\n raw: Buffer\n /** Provider response string. */\n response?: string\n}\n\nexport interface SmtpClient {\n verify(options: SmtpConnectionOptions): Promise<void>\n send(options: SmtpConnectionOptions, message: SmtpMessage): Promise<SmtpSendResult>\n}\n\ntype MailComposerCtor = new (mail: Record<string, unknown>) => unknown\n\n// nodemailer 10 dropped the root `MailComposer` re-export that v9 exposed; it is\n// still published under the `nodemailer/lib/mail-composer` subpath. Without this\n// fallback the Sent-folder append silently uploads a 0-byte message.\nasync function loadMailComposer(): Promise<MailComposerCtor | undefined> {\n try {\n const mod = (await import('nodemailer/lib/mail-composer')) as unknown as {\n default?: MailComposerCtor\n } & MailComposerCtor\n const ctor = mod.default ?? mod\n return typeof ctor === 'function' ? ctor : undefined\n } catch (err) {\n logger.warn('failed to load nodemailer MailComposer', { err })\n return undefined\n }\n}\n\n/**\n * Whether an SMTP send failed because the message is too large: a local\n * `SIZE` pre-check, or a server answer of 552 / 554 with a size status\n * (RFC 1870 `5.3.4`, `5.2.3`) or wording. A full recipient mailbox (`5.2.2`)\n * is not a message size problem, even when its text mentions a size.\n */\nexport function isSmtpMessageTooLarge(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const { responseCode, response, message } = error as { responseCode?: number; response?: string; message?: string }\n const text = `${response ?? ''} ${message ?? ''}`\n if (/size larger than allowed/i.test(text)) return true\n if (responseCode !== 552 && responseCode !== 554) return false\n if (/\\b5\\.2\\.2\\b/.test(text)) return false\n return /5\\.3\\.4|5\\.2\\.3|too (large|big)|message size|size exceeds/i.test(text)\n}\n\n// send() composes the message twice (MailComposer for the Sent-folder bytes, then\n// transporter.sendMail), and each composition would generate its own Message-ID.\n// Pinning one up front keeps the Sent copy, the delivered message and the\n// returned id identical so replies thread with the Sent copy.\nexport function generateMessageId(from: string): string {\n const domain = /@([^\\s<>@]+)>?\\s*$/.exec(from)?.[1] ?? 'localhost'\n return `<${randomUUID()}@${domain}>`\n}\n\nclass NodemailerClient implements SmtpClient {\n async verify(options: SmtpConnectionOptions): Promise<void> {\n const { transporter } = await this.createTransporter(options)\n try {\n await transporter.verify()\n } finally {\n // Mirror send(): close on every path so a failed verify (wrong password,\n // unreachable host \u2014 the common case) does not leak the socket pool.\n transporter.close()\n }\n }\n\n async send(options: SmtpConnectionOptions, message: SmtpMessage): Promise<SmtpSendResult> {\n const { transporter, MailComposer } = await this.createTransporter(options)\n try {\n const mailOptions: Record<string, unknown> = {\n from: message.from,\n to: message.to,\n cc: message.cc,\n bcc: message.bcc,\n subject: message.subject,\n text: message.text,\n html: message.html,\n messageId: message.messageId ?? generateMessageId(message.from),\n inReplyTo: message.inReplyTo,\n references: message.references,\n attachments: message.attachments?.map((a) => ({\n filename: a.filename,\n content: a.content,\n contentType: a.contentType,\n cid: a.cid,\n contentDisposition: a.inline ? 'inline' : 'attachment',\n })),\n headers: message.headers,\n // Attachments are always bytes; never let a value make nodemailer read\n // the local disk or fetch a URL.\n disableFileAccess: true,\n disableUrlAccess: true,\n }\n\n // Build the RFC2822 bytes ourselves via MailComposer so we can capture\n // them for the Sent-folder append (review H1, 2026-05-26).\n // nodemailer's `transporter.sendMail` info object does NOT contain `raw`\n // unless you configure a streamTransport, so naively reading\n // `info.raw` produces a 0-byte buffer and the Sent-folder append uploads\n // a corrupt message.\n let raw: Buffer = Buffer.alloc(0)\n let composedMessageId = message.messageId\n if (typeof MailComposer === 'function') {\n try {\n const composed = new MailComposer(mailOptions) as unknown as {\n compile: () => {\n build: (callback: (err: Error | null, output: Buffer) => void) => void\n messageId?: () => string | undefined\n }\n }\n const compiled = composed.compile()\n raw = await new Promise<Buffer>((resolve, reject) => {\n compiled.build((err, output) => {\n if (err) reject(err)\n else resolve(output)\n })\n })\n const messageIdFn = compiled.messageId\n if (typeof messageIdFn === 'function') {\n composedMessageId = messageIdFn.call(compiled) ?? composedMessageId\n }\n } catch (composeError) {\n // MailComposer build failed: the send below still delivers the mail, but we\n // cannot capture the RFC2822 bytes, so the caller skips the Sent-folder append.\n // Log so operators can diagnose missing Sent archival.\n raw = Buffer.alloc(0)\n logger.warn('failed to build RFC2822 bytes for Sent-folder append', { err: composeError })\n }\n }\n\n const info = (await transporter.sendMail(mailOptions)) as {\n messageId?: string\n envelope?: { messageId?: string }\n response?: string\n }\n const id = info.messageId ?? composedMessageId ?? info.envelope?.messageId\n if (!id) throw new Error('[internal] SMTP server did not return a Message-ID')\n return { messageId: id, raw, response: info.response }\n } finally {\n transporter.close()\n }\n }\n\n private async createTransporter(\n options: SmtpConnectionOptions,\n ): Promise<{\n transporter: NodemailerTransporter\n MailComposer: (new (mail: Record<string, unknown>) => unknown) | undefined\n }> {\n const mod = (await import('nodemailer')) as unknown as {\n default?: {\n createTransport: (opts: Record<string, unknown>) => NodemailerTransporter\n MailComposer?: new (mail: Record<string, unknown>) => unknown\n }\n createTransport?: (opts: Record<string, unknown>) => NodemailerTransporter\n MailComposer?: new (mail: Record<string, unknown>) => unknown\n }\n const createTransport = mod.createTransport ?? mod.default?.createTransport\n if (typeof createTransport !== 'function') {\n throw new Error('nodemailer.createTransport is unavailable')\n }\n const MailComposer = mod.MailComposer ?? mod.default?.MailComposer ?? await loadMailComposer()\n // Resolve + pin the SMTP host to a validated public IP at connect time\n // (DNS-rebinding-safe), keeping the hostname as the TLS servername for SNI +\n // certificate hostname verification.\n const pinned = await resolveSafeHostAddress(options.host)\n const transporter = createTransport({\n host: pinned.host,\n port: options.port,\n secure: options.transport === 'tls',\n requireTLS: options.transport === 'starttls',\n auth: { user: options.user, pass: options.pass },\n connectionTimeout: options.timeoutMs ?? 10_000,\n // Reject downgrade attacks: only allow cleartext when the operator\n // explicitly opts into `transport: 'none'`. Even then, refuse to skip\n // certificate verification on STARTTLS / TLS.\n tls:\n options.transport === 'none'\n ? undefined\n : { rejectUnauthorized: true, ...(pinned.servername ? { servername: pinned.servername } : {}) },\n })\n return { transporter, MailComposer }\n }\n}\n\ninterface NodemailerTransporter {\n verify(): Promise<true>\n sendMail(options: Record<string, unknown>): Promise<unknown>\n close(): void\n}\n\nlet cachedClient: SmtpClient | null = null\n\nexport function getSmtpClient(): SmtpClient {\n if (!cachedClient) cachedClient = new NodemailerClient()\n return cachedClient\n}\n\nexport function setSmtpClient(client: SmtpClient | null): void {\n cachedClient = client\n}\n\nexport function credentialsToSmtpConnection(credentials: ImapCredentials): SmtpConnectionOptions {\n assertTransportAllowed(credentials)\n return {\n host: credentials.smtpHost,\n port: Number(credentials.smtpPort),\n user: credentials.smtpUser,\n pass: credentials.smtpPassword,\n transport: credentials.smtpTls,\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,kBAAkB;AAE3B,SAAS,8BAA8B;AACvC,SAAS,8BAA8B;AACvC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,cAAc;AA4D1C,eAAe,mBAA0D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EACF;AAAA,EAEA,MAAc,kBACZ,SAIC;AACD,UAAM,MAAO,MAAM,OAAO,YAAY;AAQtC,UAAM,kBAAkB,IAAI,mBAAmB,IAAI,SAAS;AAC5D,QAAI,OAAO,oBAAoB,YAAY;AACzC,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC7D;AACA,UAAM,eAAe,IAAI,gBAAgB,IAAI,SAAS,gBAAgB,MAAM,iBAAiB;AAI7F,UAAM,SAAS,MAAM,uBAAuB,QAAQ,IAAI;AACxD,UAAM,cAAc,gBAAgB;AAAA,MAClC,MAAM,OAAO;AAAA,MACb,MAAM,QAAQ;AAAA,MACd,QAAQ,QAAQ,cAAc;AAAA,MAC9B,YAAY,QAAQ,cAAc;AAAA,MAClC,MAAM,EAAE,MAAM,QAAQ,MAAM,MAAM,QAAQ,KAAK;AAAA,MAC/C,mBAAmB,QAAQ,aAAa;AAAA;AAAA;AAAA;AAAA,MAIxC,KACE,QAAQ,cAAc,SAClB,SACA,EAAE,oBAAoB,MAAM,GAAI,OAAO,aAAa,EAAE,YAAY,OAAO,WAAW,IAAI,CAAC,EAAG;AAAA,IACpG,CAAC;AACD,WAAO,EAAE,aAAa,aAAa;AAAA,EACrC;AACF;AAQA,IAAI,eAAkC;AAE/B,SAAS,gBAA4B;AAC1C,MAAI,CAAC,aAAc,gBAAe,IAAI,iBAAiB;AACvD,SAAO;AACT;AAEO,SAAS,cAAc,QAAiC;AAC7D,iBAAe;AACjB;AAEO,SAAS,4BAA4B,aAAqD;AAC/F,yBAAuB,WAAW;AAClC,SAAO;AAAA,IACL,MAAM,YAAY;AAAA,IAClB,MAAM,OAAO,YAAY,QAAQ;AAAA,IACjC,MAAM,YAAY;AAAA,IAClB,MAAM,YAAY;AAAA,IAClB,WAAW,YAAY;AAAA,EACzB;AACF;",
4
+ "sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { ImapCredentials } from './credentials'\nimport { resolveSafeHostAddress } from './host-pinning'\nimport { assertTransportAllowed } from './transport'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('channel_imap')\n\n/**\n * Outbound SMTP client wrapper. Same trade-offs as `imap-client.ts`: we wrap\n * `nodemailer` behind a tiny interface so tests can swap in a mock and the\n * adapter doesn't import SDK types directly.\n */\n\nexport interface SmtpConnectionOptions {\n host: string\n port: number\n user: string\n pass: string\n transport: 'tls' | 'starttls' | 'none'\n timeoutMs?: number\n}\n\nexport interface SmtpMessage {\n from: string\n to: string[]\n cc?: string[]\n bcc?: string[]\n subject?: string\n text?: string\n html?: string\n /** RFC2822 Message-ID; if omitted the client generates one from the sender domain. */\n messageId?: string\n /** RFC2822 In-Reply-To (single value). */\n inReplyTo?: string\n /** RFC2822 References (whitespace-delimited list). */\n references?: string[]\n attachments?: Array<{\n filename: string\n content: Buffer\n contentType?: string\n cid?: string\n inline?: boolean\n }>\n headers?: Record<string, string>\n}\n\nexport interface SmtpSendResult {\n /** Effective Message-ID. */\n messageId: string\n /** Raw RFC2822 message buffer (used for Sent-folder append). */\n raw: Buffer\n /** Provider response string. */\n response?: string\n /** Recipients the server accepted at RCPT TO. */\n accepted?: string[]\n /** Recipients the server refused at RCPT TO while still delivering to the others. */\n rejected?: string[]\n}\n\nexport interface SmtpClient {\n verify(options: SmtpConnectionOptions): Promise<void>\n send(options: SmtpConnectionOptions, message: SmtpMessage): Promise<SmtpSendResult>\n}\n\ntype MailComposerCtor = new (mail: Record<string, unknown>) => unknown\n\n// nodemailer 10 dropped the root `MailComposer` re-export that v9 exposed; it is\n// still published under the `nodemailer/lib/mail-composer` subpath. Without this\n// fallback the Sent-folder append silently uploads a 0-byte message.\nasync function loadMailComposer(): Promise<MailComposerCtor | undefined> {\n try {\n const mod = (await import('nodemailer/lib/mail-composer')) as unknown as {\n default?: MailComposerCtor\n } & MailComposerCtor\n const ctor = mod.default ?? mod\n return typeof ctor === 'function' ? ctor : undefined\n } catch (err) {\n logger.warn('failed to load nodemailer MailComposer', { err })\n return undefined\n }\n}\n\n/**\n * Whether an SMTP send failed because the message is too large: a local\n * `SIZE` pre-check, or a server answer of 552 / 554 with a size status\n * (RFC 1870 `5.3.4`, `5.2.3`) or wording. A full recipient mailbox (`5.2.2`)\n * is not a message size problem, even when its text mentions a size.\n */\nexport function isSmtpMessageTooLarge(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const { responseCode, response, message } = error as { responseCode?: number; response?: string; message?: string }\n const text = `${response ?? ''} ${message ?? ''}`\n if (/size larger than allowed/i.test(text)) return true\n if (responseCode !== 552 && responseCode !== 554) return false\n if (/\\b5\\.2\\.2\\b/.test(text)) return false\n return /5\\.3\\.4|5\\.2\\.3|too (large|big)|message size|size exceeds/i.test(text)\n}\n\n// send() composes the message twice (MailComposer for the Sent-folder bytes, then\n// transporter.sendMail), and each composition would generate its own Message-ID.\n// Pinning one up front keeps the Sent copy, the delivered message and the\n// returned id identical so replies thread with the Sent copy.\nexport function generateMessageId(from: string): string {\n const domain = /@([^\\s<>@]+)>?\\s*$/.exec(from)?.[1] ?? 'localhost'\n return `<${randomUUID()}@${domain}>`\n}\n\nclass NodemailerClient implements SmtpClient {\n async verify(options: SmtpConnectionOptions): Promise<void> {\n const { transporter } = await this.createTransporter(options)\n try {\n await transporter.verify()\n } finally {\n // Mirror send(): close on every path so a failed verify (wrong password,\n // unreachable host \u2014 the common case) does not leak the socket pool.\n transporter.close()\n }\n }\n\n async send(options: SmtpConnectionOptions, message: SmtpMessage): Promise<SmtpSendResult> {\n const { transporter, MailComposer } = await this.createTransporter(options)\n try {\n const mailOptions: Record<string, unknown> = {\n from: message.from,\n to: message.to,\n cc: message.cc,\n bcc: message.bcc,\n subject: message.subject,\n text: message.text,\n html: message.html,\n messageId: message.messageId ?? generateMessageId(message.from),\n inReplyTo: message.inReplyTo,\n references: message.references,\n attachments: message.attachments?.map((a) => ({\n filename: a.filename,\n content: a.content,\n contentType: a.contentType,\n cid: a.cid,\n contentDisposition: a.inline ? 'inline' : 'attachment',\n })),\n headers: message.headers,\n // Attachments are always bytes; never let a value make nodemailer read\n // the local disk or fetch a URL.\n disableFileAccess: true,\n disableUrlAccess: true,\n }\n\n // Build the RFC2822 bytes ourselves via MailComposer so we can capture\n // them for the Sent-folder append (review H1, 2026-05-26).\n // nodemailer's `transporter.sendMail` info object does NOT contain `raw`\n // unless you configure a streamTransport, so naively reading\n // `info.raw` produces a 0-byte buffer and the Sent-folder append uploads\n // a corrupt message.\n let raw: Buffer = Buffer.alloc(0)\n let composedMessageId = message.messageId\n if (typeof MailComposer === 'function') {\n try {\n const composed = new MailComposer(mailOptions) as unknown as {\n compile: () => {\n build: (callback: (err: Error | null, output: Buffer) => void) => void\n messageId?: () => string | undefined\n }\n }\n const compiled = composed.compile()\n raw = await new Promise<Buffer>((resolve, reject) => {\n compiled.build((err, output) => {\n if (err) reject(err)\n else resolve(output)\n })\n })\n const messageIdFn = compiled.messageId\n if (typeof messageIdFn === 'function') {\n composedMessageId = messageIdFn.call(compiled) ?? composedMessageId\n }\n } catch (composeError) {\n // MailComposer build failed: the send below still delivers the mail, but we\n // cannot capture the RFC2822 bytes, so the caller skips the Sent-folder append.\n // Log so operators can diagnose missing Sent archival.\n raw = Buffer.alloc(0)\n logger.warn('failed to build RFC2822 bytes for Sent-folder append', { err: composeError })\n }\n }\n\n const info = (await transporter.sendMail(mailOptions)) as {\n messageId?: string\n envelope?: { messageId?: string }\n response?: string\n accepted?: string[]\n rejected?: string[]\n }\n const id = info.messageId ?? composedMessageId ?? info.envelope?.messageId\n if (!id) throw new Error('[internal] SMTP server did not return a Message-ID')\n return { messageId: id, raw, response: info.response, accepted: info.accepted, rejected: info.rejected }\n } finally {\n transporter.close()\n }\n }\n\n private async createTransporter(\n options: SmtpConnectionOptions,\n ): Promise<{\n transporter: NodemailerTransporter\n MailComposer: (new (mail: Record<string, unknown>) => unknown) | undefined\n }> {\n const mod = (await import('nodemailer')) as unknown as {\n default?: {\n createTransport: (opts: Record<string, unknown>) => NodemailerTransporter\n MailComposer?: new (mail: Record<string, unknown>) => unknown\n }\n createTransport?: (opts: Record<string, unknown>) => NodemailerTransporter\n MailComposer?: new (mail: Record<string, unknown>) => unknown\n }\n const createTransport = mod.createTransport ?? mod.default?.createTransport\n if (typeof createTransport !== 'function') {\n throw new Error('nodemailer.createTransport is unavailable')\n }\n const MailComposer = mod.MailComposer ?? mod.default?.MailComposer ?? await loadMailComposer()\n // Resolve + pin the SMTP host to a validated public IP at connect time\n // (DNS-rebinding-safe), keeping the hostname as the TLS servername for SNI +\n // certificate hostname verification.\n const pinned = await resolveSafeHostAddress(options.host)\n const transporter = createTransport({\n host: pinned.host,\n port: options.port,\n secure: options.transport === 'tls',\n requireTLS: options.transport === 'starttls',\n auth: { user: options.user, pass: options.pass },\n connectionTimeout: options.timeoutMs ?? 10_000,\n // Reject downgrade attacks: only allow cleartext when the operator\n // explicitly opts into `transport: 'none'`. Even then, refuse to skip\n // certificate verification on STARTTLS / TLS.\n tls:\n options.transport === 'none'\n ? undefined\n : { rejectUnauthorized: true, ...(pinned.servername ? { servername: pinned.servername } : {}) },\n })\n return { transporter, MailComposer }\n }\n}\n\ninterface NodemailerTransporter {\n verify(): Promise<true>\n sendMail(options: Record<string, unknown>): Promise<unknown>\n close(): void\n}\n\nlet cachedClient: SmtpClient | null = null\n\nexport function getSmtpClient(): SmtpClient {\n if (!cachedClient) cachedClient = new NodemailerClient()\n return cachedClient\n}\n\nexport function setSmtpClient(client: SmtpClient | null): void {\n cachedClient = client\n}\n\nexport function credentialsToSmtpConnection(credentials: ImapCredentials): SmtpConnectionOptions {\n assertTransportAllowed(credentials)\n return {\n host: credentials.smtpHost,\n port: Number(credentials.smtpPort),\n user: credentials.smtpUser,\n pass: credentials.smtpPassword,\n transport: credentials.smtpTls,\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,kBAAkB;AAE3B,SAAS,8BAA8B;AACvC,SAAS,8BAA8B;AACvC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,cAAc;AAgE1C,eAAe,mBAA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ato/channel-imap",
3
- "version": "0.10.1-develop.8274.1.c8f0850146",
3
+ "version": "0.10.1-develop.8278.1.f7f8e5ab1a",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -62,9 +62,9 @@
62
62
  }
63
63
  },
64
64
  "dependencies": {
65
- "@open-mercato/core": "0.10.1-develop.8274.1.c8f0850146",
66
- "@open-mercato/queue": "0.10.1-develop.8274.1.c8f0850146",
67
- "@open-mercato/ui": "0.10.1-develop.8274.1.c8f0850146",
65
+ "@open-mercato/core": "0.10.1-develop.8278.1.f7f8e5ab1a",
66
+ "@open-mercato/queue": "0.10.1-develop.8278.1.f7f8e5ab1a",
67
+ "@open-mercato/ui": "0.10.1-develop.8278.1.f7f8e5ab1a",
68
68
  "@types/mailparser": "^3.4.5",
69
69
  "@types/nodemailer": "^8.0.1",
70
70
  "imapflow": "^1.7.6",
@@ -73,15 +73,15 @@
73
73
  },
74
74
  "peerDependencies": {
75
75
  "@mikro-orm/postgresql": "^7.0.14",
76
- "@open-mercato/ai-assistant": "0.10.1-develop.8274.1.c8f0850146",
77
- "@open-mercato/shared": "0.10.1-develop.8274.1.c8f0850146",
76
+ "@open-mercato/ai-assistant": "0.10.1-develop.8278.1.f7f8e5ab1a",
77
+ "@open-mercato/shared": "0.10.1-develop.8278.1.f7f8e5ab1a",
78
78
  "react": "^19.0.0",
79
79
  "react-dom": "^19.0.0",
80
80
  "react-is": ">=18.0.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@open-mercato/ai-assistant": "0.10.1-develop.8274.1.c8f0850146",
84
- "@open-mercato/shared": "0.10.1-develop.8274.1.c8f0850146",
83
+ "@open-mercato/ai-assistant": "0.10.1-develop.8278.1.f7f8e5ab1a",
84
+ "@open-mercato/shared": "0.10.1-develop.8278.1.f7f8e5ab1a",
85
85
  "@types/jest": "^30.0.0",
86
86
  "@types/react": "^19.2.18",
87
87
  "@types/react-dom": "^19.2.5",
@@ -143,6 +143,72 @@ describe('ImapChannelAdapter.sendMessage', () => {
143
143
  })
144
144
  expect(result.status).toBe('sent')
145
145
  })
146
+
147
+ it('reports a send the server refused for some recipients as failed', async () => {
148
+ const appendCalls: Buffer[] = []
149
+ setSmtpClient({
150
+ verify: async () => undefined,
151
+ send: async () => ({
152
+ messageId: '<partial@example.com>',
153
+ raw: Buffer.from('RAW'),
154
+ response: '250 2.0.0 Ok: queued',
155
+ accepted: ['bob@example.com'],
156
+ rejected: ['timeout@example.com'],
157
+ }),
158
+ })
159
+ setImapClient({
160
+ connectAndValidate: async () => ({ capabilities: [] }),
161
+ selectInbox: async () => ({}),
162
+ fetchUidRange: async () => [],
163
+ appendSent: async (_options, raw) => {
164
+ appendCalls.push(raw)
165
+ },
166
+ })
167
+ const adapter = getImapChannelAdapter()
168
+ const result = await adapter.sendMessage({
169
+ content: { text: 'hi', bodyFormat: 'text' },
170
+ credentials,
171
+ scope: { tenantId: 't', organizationId: 'o' },
172
+ metadata: { to: ['bob@example.com', 'timeout@example.com'] },
173
+ })
174
+ expect(result.status).toBe('failed')
175
+ expect(result.error).toBe('SMTP_SEND_PARTIALLY_REJECTED: relay rejected 1 of 2 recipients')
176
+ expect(result.externalMessageId).toBe('<partial@example.com>')
177
+ expect(result.metadata).toEqual({
178
+ response: '250 2.0.0 Ok: queued',
179
+ accepted: ['bob@example.com'],
180
+ rejected: ['timeout@example.com'],
181
+ })
182
+ expect(appendCalls).toHaveLength(1)
183
+ })
184
+
185
+ it('keeps reporting sent when the server rejected no recipients', async () => {
186
+ setSmtpClient({
187
+ verify: async () => undefined,
188
+ send: async () => ({
189
+ messageId: '<clean@example.com>',
190
+ raw: Buffer.from('RAW'),
191
+ response: '250 OK',
192
+ accepted: ['bob@example.com'],
193
+ rejected: [],
194
+ }),
195
+ })
196
+ setImapClient({
197
+ connectAndValidate: async () => ({ capabilities: [] }),
198
+ selectInbox: async () => ({}),
199
+ fetchUidRange: async () => [],
200
+ appendSent: async () => undefined,
201
+ })
202
+ const adapter = getImapChannelAdapter()
203
+ const result = await adapter.sendMessage({
204
+ content: { text: 'hi', bodyFormat: 'text' },
205
+ credentials,
206
+ scope: { tenantId: 't', organizationId: 'o' },
207
+ metadata: { to: ['bob@example.com'] },
208
+ })
209
+ expect(result.status).toBe('sent')
210
+ expect(result.metadata).toEqual({ response: '250 OK' })
211
+ })
146
212
  })
147
213
 
148
214
  describe('ImapChannelAdapter.fetchHistory', () => {
@@ -1,7 +1,14 @@
1
1
  import { generateMessageId, getSmtpClient, setSmtpClient, type SmtpConnectionOptions } from '../smtp-client'
2
2
 
3
- const sendMail = jest.fn(async (_options: { messageId?: string }) => ({
4
- messageId: '<server@example.com>' as string | undefined,
3
+ type SendMailInfo = {
4
+ messageId?: string
5
+ response?: string
6
+ accepted?: string[]
7
+ rejected?: string[]
8
+ }
9
+
10
+ const sendMail = jest.fn(async (_options: { messageId?: string }): Promise<SendMailInfo> => ({
11
+ messageId: '<server@example.com>',
5
12
  response: '250 OK',
6
13
  }))
7
14
  const close = jest.fn()
@@ -100,6 +107,67 @@ describe('NodemailerClient.send — RFC2822 capture for the Sent-folder append',
100
107
  })
101
108
  })
102
109
 
110
+ describe('NodemailerClient.send — recipients refused by the server', () => {
111
+ afterEach(() => {
112
+ setSmtpClient(null)
113
+ jest.clearAllMocks()
114
+ })
115
+
116
+ it('returns the accepted and rejected recipients nodemailer reports', async () => {
117
+ sendMail.mockImplementationOnce(async () => ({
118
+ messageId: '<server@example.com>',
119
+ response: '250 2.0.0 Ok: queued',
120
+ accepted: ['good@example.com'],
121
+ rejected: ['bad@example.com'],
122
+ }))
123
+
124
+ const result = await getSmtpClient().send(connection, {
125
+ from: 'sender@example.com',
126
+ to: ['good@example.com', 'bad@example.com'],
127
+ subject: 'Partial rejection',
128
+ text: 'body',
129
+ })
130
+
131
+ expect(result.accepted).toEqual(['good@example.com'])
132
+ expect(result.rejected).toEqual(['bad@example.com'])
133
+ })
134
+
135
+ it('reports no rejected recipients when the server accepts everyone', async () => {
136
+ sendMail.mockImplementationOnce(async () => ({
137
+ messageId: '<server@example.com>',
138
+ response: '250 OK',
139
+ accepted: ['good@example.com'],
140
+ rejected: [],
141
+ }))
142
+
143
+ const result = await getSmtpClient().send(connection, {
144
+ from: 'sender@example.com',
145
+ to: ['good@example.com'],
146
+ subject: 'Full success',
147
+ text: 'body',
148
+ })
149
+
150
+ expect(result.accepted).toEqual(['good@example.com'])
151
+ expect(result.rejected).toEqual([])
152
+ })
153
+
154
+ it('still fails the send when the server rejects every recipient', async () => {
155
+ sendMail.mockImplementationOnce(async () => {
156
+ throw new Error("Can't send mail - all recipients were rejected")
157
+ })
158
+
159
+ await expect(
160
+ getSmtpClient().send(connection, {
161
+ from: 'sender@example.com',
162
+ to: ['bad@example.com'],
163
+ subject: 'Full rejection',
164
+ text: 'body',
165
+ }),
166
+ ).rejects.toThrow('all recipients were rejected')
167
+ expect(close).toHaveBeenCalledTimes(1)
168
+ })
169
+ })
170
+
103
171
  describe('generateMessageId', () => {
104
172
  it('derives the domain from a bare or display-name sender address', () => {
105
173
  expect(generateMessageId('a@example.org')).toMatch(/^<[0-9a-f-]{36}@example\.org>$/)
@@ -144,6 +144,18 @@ class ImapChannelAdapter implements ChannelAdapter {
144
144
  logger.warn('failed to append outbound message to Sent folder', { err: appendError })
145
145
  }
146
146
 
147
+ const rejected = result.rejected ?? []
148
+ if (rejected.length > 0) {
149
+ const accepted = result.accepted ?? []
150
+ return {
151
+ externalMessageId: result.messageId,
152
+ conversationId: input.conversationId,
153
+ status: 'failed',
154
+ error: `SMTP_SEND_PARTIALLY_REJECTED: relay rejected ${rejected.length} of ${accepted.length + rejected.length} recipients`,
155
+ metadata: { ...(result.response ? { response: result.response } : {}), accepted, rejected },
156
+ }
157
+ }
158
+
147
159
  return {
148
160
  externalMessageId: result.messageId,
149
161
  conversationId: input.conversationId,
@@ -52,6 +52,10 @@ export interface SmtpSendResult {
52
52
  raw: Buffer
53
53
  /** Provider response string. */
54
54
  response?: string
55
+ /** Recipients the server accepted at RCPT TO. */
56
+ accepted?: string[]
57
+ /** Recipients the server refused at RCPT TO while still delivering to the others. */
58
+ rejected?: string[]
55
59
  }
56
60
 
57
61
  export interface SmtpClient {
@@ -182,10 +186,12 @@ class NodemailerClient implements SmtpClient {
182
186
  messageId?: string
183
187
  envelope?: { messageId?: string }
184
188
  response?: string
189
+ accepted?: string[]
190
+ rejected?: string[]
185
191
  }
186
192
  const id = info.messageId ?? composedMessageId ?? info.envelope?.messageId
187
193
  if (!id) throw new Error('[internal] SMTP server did not return a Message-ID')
188
- return { messageId: id, raw, response: info.response }
194
+ return { messageId: id, raw, response: info.response, accepted: info.accepted, rejected: info.rejected }
189
195
  } finally {
190
196
  transporter.close()
191
197
  }