Files
webmail/lib/mail-import/pst/worker.ts
T
Linus Rath 8d48f98261 fix: keep failed messages for the next import run and stop an import that can't go on
A message that failed for a passing reason (a dropped connection, a
server error, an expired token) used to count as done, so continuing
the import skipped it for good, and a slow upload followed by a cancel
imported the messages after it twice.

- The checkpoint keeps every message that is done, also past a gap,
  and the next run of the same import tries only the rest again.
- Ten failures in a row, a full mailbox (overQuota) or a refusal
  (forbidden) stop the import with a message instead of going through
  the whole file.
- An upload without an answer pauses the import instead of failing the
  message: 30 seconds, then until the upload window ends.
- Email/import, the export's Blob/get and folder listing pass the
  client's own 429 pause by, and a 429 on them doesn't pause the rest
  of the app.
- Signing out stops the job of that account only, writes no checkpoint
  and imports nothing more afterwards; another account's sign-out
  leaves the job alone.
- One tab imports a file at a time (Web Locks), and Import waits until
  the dialog knows whether an earlier run can be continued.

The import dialog tests now wait for the Import button to be enabled
before clicking it, since it stays off during that lookup.
2026-11-01 06:37:08 +01:00

81 lines
3.2 KiB
TypeScript

/**
* The PST worker (built by scripts/build-pst-worker.mjs into public/). It
* opens an Outlook .pst/.ost File without loading it whole, walks its mail
* folders and hands over one message at a time as MIME, only as many as the
* page asked for, so a large file never piles up in memory.
*
* page → worker { type: "open", file, skipBefore, skipAlso, maxMessageBytes }
* { type: "pull", count }
* worker → page { type: "opened", total }
* { type: "item", item, fraction } (item.mime is transferred)
* { type: "done" } | { type: "error", code, message }
*/
import { PSTFile } from "pst-extractor";
import { Buffer } from "buffer";
import { registerPstFile } from "./shims/fs";
import { walkPst, type PstItem } from "./items";
interface WorkerScope {
onmessage: ((event: MessageEvent) => void) | null;
postMessage(message: unknown, transfer?: Transferable[]): void;
}
const scope = globalThis as unknown as WorkerScope;
// Attachment bytes are read into a Buffer over our own memory (extract.ts).
(globalThis as { Buffer?: unknown }).Buffer ??= Buffer;
let items: Generator<PstItem> | null = null;
let total = 0;
let done = 0;
let pst: { close(): void } | null = null;
function errorCode(error: unknown): string {
const message = error instanceof Error ? error.message : String(error);
if (/encrypt/i.test(message)) return "encrypted";
if (/not a pst|invalid|unrecognised|unrecognized|magic|!BDN/i.test(message)) return "not_pst";
return "unreadable";
}
scope.onmessage = (event: MessageEvent) => {
const data = event.data as { type: string; file?: File; skipBefore?: number; skipAlso?: number[]; maxMessageBytes?: number; count?: number };
try {
if (data.type === "open" && data.file) {
registerPstFile("import.pst", data.file);
pst = new PSTFile("import.pst") as unknown as { close(): void };
const walk = walkPst(pst, { skipBefore: data.skipBefore ?? 0, skipAlso: new Set(data.skipAlso ?? []), maxMessageBytes: data.maxMessageBytes ?? Number.POSITIVE_INFINITY });
items = walk.items;
total = walk.total;
scope.postMessage({ type: "opened", total });
return;
}
if (data.type === "pull" && items) {
for (let n = 0; n < (data.count ?? 1); n++) {
const next = items.next();
if (next.done) {
items = null;
pst?.close();
scope.postMessage({ type: "done" });
return;
}
done++;
const item = next.value;
const fraction = total > 0 ? Math.min(1, done / total) : 0;
if (item.kind === "message") {
const buffer = item.mime.buffer.byteLength === item.mime.byteLength ? item.mime.buffer : item.mime.slice().buffer;
scope.postMessage({ type: "item", item: { ...item, mime: buffer }, fraction }, [buffer as ArrayBuffer]);
} else {
scope.postMessage({ type: "item", item, fraction });
}
}
return;
}
if (data.type === "close") {
items = null;
pst?.close();
pst = null;
}
} catch (error) {
scope.postMessage({ type: "error", code: errorCode(error), message: error instanceof Error ? error.message : String(error) });
}
};