Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | 18x 18x 15x 15x 15x 15x 12x 14x 14x 14x 14x 14x 14x 10x 10x 1x 1x 1x 1x 3x 3x 3x 2x 2x 1x 5x 5x 5x 5x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 5x 5x 2x 1x 1x 1x 1x 3x 2x 2x 2x 16x 16x 16x 16x 1x 18x 20x 20x 3x 3x 2x 3x | import { ImapFlow } from "imapflow";
import { simpleParser } from "mailparser";
import type { FolderInfo, IngestConnector, RawMessage } from "./types.js";
export interface ImapConnectorConfig {
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
}
const MAX_RETRIES = 3;
const RETRY_DELAY_MS = 1000;
/**
* IngestConnector implementation backed by IMAP via ImapFlow.
*
* This is a thin transport adapter — it handles connecting to the IMAP server,
* listing folders, and streaming messages. Storage, label management, and sync
* state tracking remain in the sync engine (ImapSync), which orchestrates this
* connector alongside the database layer.
*/
export class ImapIngestConnector implements IngestConnector {
readonly name = "imap";
private client: ImapFlow;
private config: ImapConnectorConfig;
constructor(config: ImapConnectorConfig) {
this.config = config;
this.client = new ImapFlow({
...config,
logger: false,
});
// Attach error handler immediately to prevent unhandled 'error' events
// between construction and first connect() call.
this.client.on("error", () => {});
}
async connect(): Promise<void> {
await withRetry(async () => {
// ImapFlow instances cannot be reconnected once closed — create fresh
suppressImapFlowErrors(this.client);
try {
this.client.close();
} catch {
// May not be connected yet
}
this.client = new ImapFlow({
...this.config,
logger: false,
});
this.client.on("error", () => {});
return this.client.connect();
}, "IMAP connect");
}
async disconnect(): Promise<void> {
try {
await this.client.logout();
} catch {
// Connection may already be closed
}
}
async listFolders(): Promise<FolderInfo[]> {
const mailboxes = await withRetry(() => this.client.list(), "list mailboxes");
const folders: FolderInfo[] = [];
const seen = new Set<string>();
for (const mailbox of mailboxes) {
Iif (seen.has(mailbox.path)) continue;
const flags = mailbox.flags ? Array.from(mailbox.flags) : [];
if (flags.some((f) => f.toLowerCase() === "\\noselect")) continue;
seen.add(mailbox.path);
folders.push({
path: mailbox.path,
name: mailbox.name,
delimiter: mailbox.delimiter || "/",
flags,
});
}
return folders;
}
async *fetchMessages(folder: string, sinceUid: number): AsyncIterable<RawMessage> {
const lock = await withRetry(() => this.client.getMailboxLock(folder), `lock ${folder}`);
try {
const range = sinceUid > 0 ? `${sinceUid + 1}:*` : "1:*";
for await (const message of this.client.fetch(range, {
uid: true,
envelope: true,
flags: true,
size: true,
source: true,
})) {
const source = message.source;
Iif (!source) continue;
const parsed = await simpleParser(source);
const envelope = message.envelope;
Iif (!envelope) continue;
const fromAddr = envelope.from?.[0];
const toAddrs = envelope.to
?.map((a) => ({
address: a.address ?? "",
name: a.name,
}))
.filter((a) => a.address);
const ccAddrs = envelope.cc
?.map((a) => ({
address: a.address ?? "",
name: a.name,
}))
.filter((a) => a.address);
yield {
uid: message.uid,
messageId: envelope.messageId ?? undefined,
inReplyTo: envelope.inReplyTo ?? undefined,
subject: envelope.subject ?? undefined,
from: fromAddr
? {
address: fromAddr.address ?? "",
name: fromAddr.name ?? undefined,
}
: undefined,
to: toAddrs,
cc: ccAddrs,
date: envelope.date ?? undefined,
textBody: parsed.text ?? undefined,
htmlBody: typeof parsed.html === "string" ? parsed.html : undefined,
flags: Array.from(message.flags ?? new Set()),
size: message.size ?? undefined,
attachments: parsed.attachments
.filter((att) => att.content != null)
.map((att) => ({
filename: att.filename ?? undefined,
contentType:
typeof att.contentType === "string" ? att.contentType : "application/octet-stream",
size: typeof att.size === "number" ? att.size : att.content.length,
contentId: att.contentId ?? undefined,
content: att.content,
})),
};
}
} finally {
try {
lock.release();
} catch {
// Lock release may fail if connection was force-closed
}
}
}
async deleteMessages(folder: string, uids: number[]): Promise<void> {
if (uids.length === 0) return;
const lock = await this.client.getMailboxLock(folder);
try {
await this.client.messageDelete(uids, { uid: true });
} finally {
lock.release();
}
}
/**
* Returns the underlying ImapFlow client for advanced operations.
* Used by the sync engine for operations not covered by IngestConnector
* (e.g., flag sync, UIDVALIDITY checks).
*/
getClient(): ImapFlow {
return this.client;
}
/**
* Force-closes the IMAP connection by destroying the underlying socket.
*/
forceClose(): void {
suppressImapFlowErrors(this.client);
try {
this.client.close();
} catch {
// Already closed
}
}
}
function suppressImapFlowErrors(client: ImapFlow): void {
const noop = () => {};
Iif (!client.listenerCount("error")) {
client.on("error", noop);
}
const socket = (
client as unknown as {
socket?: { on?: (event: string, fn: () => void) => void };
}
).socket;
if (socket && typeof socket.on === "function") {
socket.on("error", noop);
}
}
async function withRetry<T>(
fn: () => Promise<T>,
label: string,
retries = MAX_RETRIES,
): Promise<T> {
let lastError: unknown;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await fn();
} catch (err) {
lastError = err;
if (attempt < retries) {
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS * attempt));
}
}
}
throw new Error(`${label} failed after ${retries} attempts: ${lastError}`);
}
|