@bobfrankston/mailx-store 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/charset.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Charset normalization for incoming email bodies.
3
+ *
4
+ * Many senders (esp. PHPMailer-driven marketing) declare
5
+ * `charset=iso-8859-1` but emit UTF-8 bytes. simpleParser honors the
6
+ * declared charset and produces "â??" garbage for every non-ASCII
7
+ * codepoint (em-dash, smart quotes, …). When the raw body bytes are
8
+ * valid UTF-8, rewrite the charset header before parsing. We only
9
+ * override the obviously-wrong legacy declarations; explicit utf-8 /
10
+ * koi8 / etc. pass through.
11
+ */
12
+ /** Returns either the original buffer (no change needed) or a copy with
13
+ * the leading charset declaration rewritten to utf-8. */
14
+ export declare function sniffAndFixCharset(raw: Buffer): Buffer;
15
+ //# sourceMappingURL=charset.d.ts.map
package/charset.js ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Charset normalization for incoming email bodies.
3
+ *
4
+ * Many senders (esp. PHPMailer-driven marketing) declare
5
+ * `charset=iso-8859-1` but emit UTF-8 bytes. simpleParser honors the
6
+ * declared charset and produces "â??" garbage for every non-ASCII
7
+ * codepoint (em-dash, smart quotes, …). When the raw body bytes are
8
+ * valid UTF-8, rewrite the charset header before parsing. We only
9
+ * override the obviously-wrong legacy declarations; explicit utf-8 /
10
+ * koi8 / etc. pass through.
11
+ */
12
+ /** Returns either the original buffer (no change needed) or a copy with
13
+ * the leading charset declaration rewritten to utf-8. */
14
+ export function sniffAndFixCharset(raw) {
15
+ const HEAD_LIMIT = 16384;
16
+ const head = raw.subarray(0, Math.min(HEAD_LIMIT, raw.length)).toString("latin1");
17
+ const re = /charset\s*=\s*"?(iso-8859-1|us-ascii|windows-1252|latin1)"?/gi;
18
+ if (!re.test(head))
19
+ return raw;
20
+ if (!isValidUtf8(raw))
21
+ return raw;
22
+ const fixed = head.replace(/charset\s*=\s*"?(iso-8859-1|us-ascii|windows-1252|latin1)"?/gi, "charset=utf-8");
23
+ return Buffer.concat([Buffer.from(fixed, "latin1"), raw.subarray(head.length)]);
24
+ }
25
+ /** Strict UTF-8 validity check: rejects overlong forms, invalid start
26
+ * bytes, and dangling continuations. Used to confirm the body is really
27
+ * UTF-8 before overriding a Latin-1 declaration. */
28
+ function isValidUtf8(buf) {
29
+ let i = 0;
30
+ while (i < buf.length) {
31
+ const b = buf[i];
32
+ if (b < 0x80) {
33
+ i++;
34
+ continue;
35
+ }
36
+ let need;
37
+ if ((b & 0xE0) === 0xC0) {
38
+ if (b < 0xC2)
39
+ return false;
40
+ need = 1;
41
+ }
42
+ else if ((b & 0xF0) === 0xE0)
43
+ need = 2;
44
+ else if ((b & 0xF8) === 0xF0) {
45
+ if (b > 0xF4)
46
+ return false;
47
+ need = 3;
48
+ }
49
+ else
50
+ return false;
51
+ if (i + need >= buf.length)
52
+ return false;
53
+ for (let k = 1; k <= need; k++) {
54
+ if ((buf[i + k] & 0xC0) !== 0x80)
55
+ return false;
56
+ }
57
+ i += need + 1;
58
+ }
59
+ return true;
60
+ }
61
+ //# sourceMappingURL=charset.js.map
package/index.d.ts CHANGED
@@ -5,6 +5,8 @@
5
5
  export { MailxDB } from "./db.js";
6
6
  export { FileMessageStore } from "./file-store.js";
7
7
  export { parseSerial, prewarmParseWorker } from "./parse-serial.js";
8
+ export { Store } from "./store.js";
9
+ export type { StoreMessage } from "./store.js";
8
10
  export { StoreBus, storeBus } from "@bobfrankston/mailx-bus";
9
11
  export type { StoreEvent, StoreEventKind, StoreEventHandler } from "@bobfrankston/mailx-bus";
10
12
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -5,6 +5,8 @@
5
5
  export { MailxDB } from "./db.js";
6
6
  export { FileMessageStore } from "./file-store.js";
7
7
  export { parseSerial, prewarmParseWorker } from "./parse-serial.js";
8
+ // Store — the nexus. Owns DB + .eml files + operations + bus.
9
+ export { Store } from "./store.js";
8
10
  // Store-event bus lives in `@bobfrankston/mailx-bus` so the browser-side
9
11
  // store (mailx-store-web) and the desktop-side store (this package) share
10
12
  // the same bus. Re-exported here so existing callers don't have to learn
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-store",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@bobfrankston/mailx-types": "^0.1.13",
13
13
  "@bobfrankston/mailx-settings": "^0.1.17",
14
- "@bobfrankston/mailx-bus": "^0.1.0",
14
+ "@bobfrankston/mailx-bus": "^0.1.2",
15
15
  "mailparser": "^3.7.2"
16
16
  },
17
17
  "repository": {
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "@bobfrankston/mailx-types": "^0.1.13",
33
33
  "@bobfrankston/mailx-settings": "^0.1.17",
34
- "@bobfrankston/mailx-bus": "^0.1.0",
34
+ "@bobfrankston/mailx-bus": "^0.1.2",
35
35
  "mailparser": "^3.7.2"
36
36
  }
37
37
  }
package/store.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Store — the nexus. Owns the local database, the .eml file store, the
3
+ * operations API, and the event bus. Single source of truth for everything
4
+ * about a mailx account that lives on this device.
5
+ *
6
+ * UI ──┐ ┌── Sync clients (IMAP, Gmail API, …)
7
+ * │ reads / writes / subscribes │ read pending actions, commit
8
+ * ↓ ↓ server-discovered changes
9
+ * [ Store ]
10
+ *
11
+ * Contract: every method here mutates local-only state. No IMAP, no Gmail,
12
+ * no SMTP, no DNS, no Drive API. Mutations emit on the bus; subscribers
13
+ * (UI in another process, in-process reconciler, etc.) react. Sync clients
14
+ * (mailx-imap, mailx-sync) hold a Store reference and never touch the
15
+ * underlying MailxDB / FileMessageStore directly.
16
+ *
17
+ * This was previously named `Store` and lived in mailx-service. It
18
+ * moved to mailx-store because mailx-imap (and other sync clients) must
19
+ * be able to consume it without depending on mailx-service — that's the
20
+ * "arrow points the right way" property of the architecture.
21
+ */
22
+ import { MailxDB } from "./db.js";
23
+ import { FileMessageStore } from "./file-store.js";
24
+ import type { StoreBus } from "./bus.js";
25
+ import type { MessageEnvelope, MessageQuery, PagedResult } from "@bobfrankston/mailx-types";
26
+ /** What the UI gets back from a body read. Mirrors the historical
27
+ * `getMessage` shape so call-site migration is mechanical. `cached: false`
28
+ * means the body isn't on disk yet — the UI shows a "downloading…"
29
+ * placeholder and listens for `bodyAvailable` to re-render. The reconciler
30
+ * is responsible for actually fetching and emitting the event. */
31
+ export interface StoreMessage extends MessageEnvelope {
32
+ bodyHtml: string;
33
+ bodyText: string;
34
+ hasRemoteContent: boolean;
35
+ remoteAllowed: boolean;
36
+ attachments: Array<{
37
+ id: number;
38
+ filename: string;
39
+ mimeType: string;
40
+ size: number;
41
+ contentId: string;
42
+ }>;
43
+ cached: boolean;
44
+ deliveredTo: string;
45
+ returnPath: string;
46
+ listUnsubscribe: string;
47
+ listUnsubscribeMail: string;
48
+ listUnsubscribeHttp: string;
49
+ listUnsubscribeOneClick: boolean;
50
+ emlPath: string;
51
+ isFlagged: boolean;
52
+ }
53
+ export declare class Store {
54
+ /** SQLite metadata index. Exposed as a public field — sync clients
55
+ * (mailx-imap, mailx-sync) read/write through it during the
56
+ * refactor. Future state: every external mutation routes through
57
+ * a Store method that emits a bus event; raw `store.db.X()` calls
58
+ * shrink to zero. Today, this is read mostly. */
59
+ readonly db: MailxDB;
60
+ /** .eml file backend. Same migration story as `db` — sync clients
61
+ * read/write directly today, will route through Store methods. */
62
+ readonly bodyStore: FileMessageStore;
63
+ /** Event bus for Store mutations. Defaults to the process-singleton
64
+ * so cross-package subscribers (bin/mailx.ts forwarder → WebView,
65
+ * in-process reconciler triggers) see all writes without explicit
66
+ * wiring. Tests can pass a fresh StoreBus for isolation. */
67
+ readonly bus: StoreBus;
68
+ private static readonly PARSED_LRU_CAPACITY;
69
+ private parsedLru;
70
+ private parsedLruGet;
71
+ private parsedLruPut;
72
+ private _allowlistCache;
73
+ private _settingsCache;
74
+ private getCachedAllowlist;
75
+ private getCachedSettings;
76
+ invalidateConfigCaches(): void;
77
+ constructor(
78
+ /** SQLite metadata index. Exposed as a public field — sync clients
79
+ * (mailx-imap, mailx-sync) read/write through it during the
80
+ * refactor. Future state: every external mutation routes through
81
+ * a Store method that emits a bus event; raw `store.db.X()` calls
82
+ * shrink to zero. Today, this is read mostly. */
83
+ db: MailxDB,
84
+ /** .eml file backend. Same migration story as `db` — sync clients
85
+ * read/write directly today, will route through Store methods. */
86
+ bodyStore: FileMessageStore,
87
+ /** Event bus for Store mutations. Defaults to the process-singleton
88
+ * so cross-package subscribers (bin/mailx.ts forwarder → WebView,
89
+ * in-process reconciler triggers) see all writes without explicit
90
+ * wiring. Tests can pass a fresh StoreBus for isolation. */
91
+ bus?: StoreBus);
92
+ /** DB-shape account list (id/name/email/lastSync). The richer
93
+ * AccountConfig (with imap/smtp/etc.) lives in accounts.jsonc and is
94
+ * loaded by mailx-settings, not the DB — that path stays in
95
+ * MailxService until step 3 of the local-first plan. */
96
+ getAccounts(): {
97
+ id: string;
98
+ name: string;
99
+ email: string;
100
+ lastSync: number;
101
+ }[];
102
+ getFolders(accountId: string): any[];
103
+ /** Look up a folder by RFC 6154 specialUse tag (`trash`, `drafts`, `sent`,
104
+ * `junk`, etc.) for the given account. Falls back to a case-insensitive
105
+ * path match for legacy rows where specialUse never got tagged.
106
+ * Returns null when the account has no such folder configured. */
107
+ findSpecialFolder(accountId: string, specialUse: string): {
108
+ id: number;
109
+ path: string;
110
+ } | null;
111
+ /** Single envelope by (account, uid, folder). Null when the row isn't
112
+ * in the DB — caller decides whether to show "deleted" or queue a
113
+ * server lookup via the reconciler. */
114
+ getMessageEnvelope(accountId: string, uid: number, folderId?: number): MessageEnvelope | null;
115
+ /** Paginated message list for a (account, folder, ...) query. */
116
+ getMessages(query: MessageQuery): PagedResult<MessageEnvelope>;
117
+ /** All-Inboxes view: union of every account's INBOX, paginated. */
118
+ getUnifiedInbox(page?: number, pageSize?: number): PagedResult<MessageEnvelope>;
119
+ /** Local FTS5 search. Server-scope search is the reconciler's job. */
120
+ searchMessages(query: string, page?: number, pageSize?: number, accountId?: string, folderId?: number, includeTrashSpam?: boolean): PagedResult<MessageEnvelope>;
121
+ /** Read a fully-parsed message (envelope + body + attachments) entirely
122
+ * from local state. Returns null when the envelope isn't known.
123
+ * Returns `{ ...envelope, cached: false }` when the envelope is known
124
+ * but the body file isn't on disk — UI shows a placeholder and the
125
+ * reconciler queues the fetch.
126
+ *
127
+ * `allowRemote=true` skips HTML sanitization. Used when the user has
128
+ * explicitly allowed remote content for this sender / domain. */
129
+ getMessage(accountId: string, uid: number, allowRemote: boolean, folderId?: number): Promise<StoreMessage | null>;
130
+ getCalendarEvents(accountId: string, fromMs: number, toMs: number): any[];
131
+ getTasks(accountId: string, includeCompleted?: boolean): any[];
132
+ searchContacts(query: string, limit?: number): any[];
133
+ listContacts(query: string, page?: number, pageSize?: number): any;
134
+ /** Update a message's flag set. Local DB write completes synchronously;
135
+ * the server-mirror enqueue is the caller's responsibility (typically
136
+ * via SyncQueue.enqueueFlag) so callers that don't want a server push
137
+ * — pure-local UI state like "pin in pane" — can skip it.
138
+ *
139
+ * Publishes:
140
+ * `message:<uuid>` { kind: "flagsChanged" }
141
+ * `folder:<id>` (auto fan-out)
142
+ */
143
+ updateFlags(accountId: string, uid: number, folderId: number, flags: string[]): void;
144
+ /** Move a message between folders in the same account. Adds a tombstone
145
+ * on the Message-ID so the next sync doesn't re-import the pre-move row
146
+ * in the source folder before the server-side MOVE completes; tombstone
147
+ * is cleared on terminal IMAP failure (see processSyncActions).
148
+ *
149
+ * Returns true if a local row existed and was moved, false otherwise.
150
+ *
151
+ * Publishes:
152
+ * `message:<uuid>` { kind: "messageMoved", folderId: source, targetFolderId }
153
+ * `folder:<source>` and `folder:<target>` (auto fan-out + explicit count)
154
+ */
155
+ moveMessage(accountId: string, uid: number, fromFolderId: number, targetFolderId: number): boolean;
156
+ /** Trash a message. If a trash folder is configured and the message is
157
+ * not already in it, this is a move-to-trash. If the message is already
158
+ * in trash (or no trash exists), it's a hard delete + body unlink.
159
+ *
160
+ * Returns "moved-to-trash" or "expunged" so the caller knows whether
161
+ * to enqueue an IMAP MOVE or a DELETE+EXPUNGE on the queue.
162
+ */
163
+ trashMessage(accountId: string, uid: number, folderId: number, trashFolderId: number | null): "moved-to-trash" | "expunged";
164
+ /** Restore a message from trash back to its original folder. Local-only;
165
+ * caller handles the queue (cancel-pending-MOVE vs queue-counter-MOVE).
166
+ * Returns true if a local row was moved. */
167
+ undeleteMessage(accountId: string, uid: number, trashFolderId: number, originalFolderId: number): boolean;
168
+ }
169
+ //# sourceMappingURL=store.d.ts.map
package/store.js ADDED
@@ -0,0 +1,528 @@
1
+ /**
2
+ * Store — the nexus. Owns the local database, the .eml file store, the
3
+ * operations API, and the event bus. Single source of truth for everything
4
+ * about a mailx account that lives on this device.
5
+ *
6
+ * UI ──┐ ┌── Sync clients (IMAP, Gmail API, …)
7
+ * │ reads / writes / subscribes │ read pending actions, commit
8
+ * ↓ ↓ server-discovered changes
9
+ * [ Store ]
10
+ *
11
+ * Contract: every method here mutates local-only state. No IMAP, no Gmail,
12
+ * no SMTP, no DNS, no Drive API. Mutations emit on the bus; subscribers
13
+ * (UI in another process, in-process reconciler, etc.) react. Sync clients
14
+ * (mailx-imap, mailx-sync) hold a Store reference and never touch the
15
+ * underlying MailxDB / FileMessageStore directly.
16
+ *
17
+ * This was previously named `Store` and lived in mailx-service. It
18
+ * moved to mailx-store because mailx-imap (and other sync clients) must
19
+ * be able to consume it without depending on mailx-service — that's the
20
+ * "arrow points the right way" property of the architecture.
21
+ */
22
+ import * as fs from "node:fs";
23
+ import { parseSerial } from "./parse-serial.js";
24
+ import { storeBus } from "./bus.js";
25
+ import { sanitizeHtml } from "@bobfrankston/mailx-types";
26
+ import { loadSettings, loadAllowlist } from "@bobfrankston/mailx-settings";
27
+ import { sniffAndFixCharset } from "./charset.js";
28
+ /** Parse `List-Unsubscribe` (RFC 2369) and `List-Unsubscribe-Post` (RFC 8058).
29
+ * mailparser only exposes ONE of mail/url even when both are present, so we
30
+ * also scan the raw header text for the full set of angle-bracketed URIs. */
31
+ function parseListUnsubscribe(headers) {
32
+ let mail = "";
33
+ let http = "";
34
+ let oneClick = false;
35
+ const raw = headers.get("list-unsubscribe");
36
+ const rawStr = typeof raw === "string" ? raw : (raw && typeof raw.text === "string" ? raw.text : "");
37
+ if (rawStr) {
38
+ const matches = rawStr.match(/<([^>]+)>/g) || [];
39
+ for (const m of matches) {
40
+ const url = m.slice(1, -1).trim();
41
+ if (!mail && /^mailto:/i.test(url))
42
+ mail = url;
43
+ else if (!http && /^https?:/i.test(url))
44
+ http = url;
45
+ }
46
+ }
47
+ if (!mail && !http) {
48
+ const listHeaders = headers.get("list");
49
+ if (listHeaders?.unsubscribe) {
50
+ const unsub = listHeaders.unsubscribe;
51
+ if (unsub.url)
52
+ http = Array.isArray(unsub.url) ? unsub.url[0] : unsub.url;
53
+ if (unsub.mail)
54
+ mail = `mailto:${Array.isArray(unsub.mail) ? unsub.mail[0] : unsub.mail}`;
55
+ }
56
+ }
57
+ const post = headers.get("list-unsubscribe-post");
58
+ const postStr = typeof post === "string" ? post : (post && typeof post.text === "string" ? post.text : "");
59
+ if (postStr && /one-?click/i.test(postStr))
60
+ oneClick = true;
61
+ return { listUnsubscribeMail: mail, listUnsubscribeHttp: http, listUnsubscribeOneClick: oneClick };
62
+ }
63
+ export class Store {
64
+ db;
65
+ bodyStore;
66
+ bus;
67
+ // Parsed-body LRU. `simpleParser` is the dominant cost of getMessage on
68
+ // a body that's already on disk — 100-500 ms for a typical Gmail message
69
+ // (heavier multipart/alternative + embedded-image swizzling than plainer
70
+ // IMAP mail). Stash the parsed result keyed by .eml path + mtime so
71
+ // repeat views of the same message return in microseconds.
72
+ //
73
+ // Invalidation is implicit: when the underlying .eml is overwritten
74
+ // (rare — bodies are immutable once cached, but reconcile can re-fetch
75
+ // on UIDVALIDITY change), the new mtime won't match the cached key.
76
+ // Capacity 200 entries × ~50 kB parsed envelope = ~10 MB ceiling.
77
+ static PARSED_LRU_CAPACITY = 200;
78
+ parsedLru = new Map();
79
+ parsedLruGet(key) {
80
+ const v = this.parsedLru.get(key);
81
+ if (!v)
82
+ return undefined;
83
+ // Bump recency.
84
+ this.parsedLru.delete(key);
85
+ this.parsedLru.set(key, v);
86
+ return v;
87
+ }
88
+ parsedLruPut(key, msg) {
89
+ this.parsedLru.delete(key);
90
+ this.parsedLru.set(key, msg);
91
+ while (this.parsedLru.size > Store.PARSED_LRU_CAPACITY) {
92
+ const oldest = this.parsedLru.keys().next().value;
93
+ if (oldest === undefined)
94
+ break;
95
+ this.parsedLru.delete(oldest);
96
+ }
97
+ }
98
+ // Allowlist + settings caches. Both files live on the GDrive-mounted
99
+ // shared dir; their sync `readFileSync` calls in `loadAllowlist()` and
100
+ // `loadSettings()` can stall for seconds per call. getMessage runs on
101
+ // every preview click, so paying that twice each click made .eml
102
+ // display "go to GDrive" even though the body itself is local. Cached
103
+ // here; invalidated externally via invalidateConfigCaches() when the
104
+ // parent service receives a `configChanged` event.
105
+ _allowlistCache = null;
106
+ _settingsCache = null;
107
+ getCachedAllowlist() {
108
+ if (!this._allowlistCache)
109
+ this._allowlistCache = loadAllowlist();
110
+ return this._allowlistCache;
111
+ }
112
+ getCachedSettings() {
113
+ if (!this._settingsCache)
114
+ this._settingsCache = loadSettings();
115
+ return this._settingsCache;
116
+ }
117
+ invalidateConfigCaches() {
118
+ this._allowlistCache = null;
119
+ this._settingsCache = null;
120
+ }
121
+ constructor(
122
+ /** SQLite metadata index. Exposed as a public field — sync clients
123
+ * (mailx-imap, mailx-sync) read/write through it during the
124
+ * refactor. Future state: every external mutation routes through
125
+ * a Store method that emits a bus event; raw `store.db.X()` calls
126
+ * shrink to zero. Today, this is read mostly. */
127
+ db,
128
+ /** .eml file backend. Same migration story as `db` — sync clients
129
+ * read/write directly today, will route through Store methods. */
130
+ bodyStore,
131
+ /** Event bus for Store mutations. Defaults to the process-singleton
132
+ * so cross-package subscribers (bin/mailx.ts forwarder → WebView,
133
+ * in-process reconciler triggers) see all writes without explicit
134
+ * wiring. Tests can pass a fresh StoreBus for isolation. */
135
+ bus = storeBus) {
136
+ this.db = db;
137
+ this.bodyStore = bodyStore;
138
+ this.bus = bus;
139
+ }
140
+ // ── Account list (read-only here; mutations go through MailxService
141
+ // until the cloud-write path is part of the queue too) ──
142
+ /** DB-shape account list (id/name/email/lastSync). The richer
143
+ * AccountConfig (with imap/smtp/etc.) lives in accounts.jsonc and is
144
+ * loaded by mailx-settings, not the DB — that path stays in
145
+ * MailxService until step 3 of the local-first plan. */
146
+ getAccounts() {
147
+ return this.db.getAccounts();
148
+ }
149
+ // ── Folders ──
150
+ getFolders(accountId) {
151
+ return this.db.getFolders(accountId);
152
+ }
153
+ /** Look up a folder by RFC 6154 specialUse tag (`trash`, `drafts`, `sent`,
154
+ * `junk`, etc.) for the given account. Falls back to a case-insensitive
155
+ * path match for legacy rows where specialUse never got tagged.
156
+ * Returns null when the account has no such folder configured. */
157
+ findSpecialFolder(accountId, specialUse) {
158
+ const folders = this.db.getFolders(accountId);
159
+ const f = folders.find(x => x.specialUse === specialUse ||
160
+ x.path.toLowerCase() === specialUse.toLowerCase());
161
+ return f ? { id: f.id, path: f.path } : null;
162
+ }
163
+ // ── Message envelopes ──
164
+ /** Single envelope by (account, uid, folder). Null when the row isn't
165
+ * in the DB — caller decides whether to show "deleted" or queue a
166
+ * server lookup via the reconciler. */
167
+ getMessageEnvelope(accountId, uid, folderId) {
168
+ const env = this.db.getMessageByUid(accountId, uid, folderId);
169
+ return env || null;
170
+ }
171
+ /** Paginated message list for a (account, folder, ...) query. */
172
+ getMessages(query) {
173
+ return this.db.getMessages(query);
174
+ }
175
+ /** All-Inboxes view: union of every account's INBOX, paginated. */
176
+ getUnifiedInbox(page = 1, pageSize = 50) {
177
+ return this.db.getUnifiedInbox(page, pageSize);
178
+ }
179
+ /** Local FTS5 search. Server-scope search is the reconciler's job. */
180
+ searchMessages(query, page = 1, pageSize = 50, accountId, folderId, includeTrashSpam = false) {
181
+ return this.db.searchMessages(query, page, pageSize, accountId, folderId, includeTrashSpam);
182
+ }
183
+ // ── Message body (read-from-disk only) ──
184
+ /** Read a fully-parsed message (envelope + body + attachments) entirely
185
+ * from local state. Returns null when the envelope isn't known.
186
+ * Returns `{ ...envelope, cached: false }` when the envelope is known
187
+ * but the body file isn't on disk — UI shows a placeholder and the
188
+ * reconciler queues the fetch.
189
+ *
190
+ * `allowRemote=true` skips HTML sanitization. Used when the user has
191
+ * explicitly allowed remote content for this sender / domain. */
192
+ async getMessage(accountId, uid, allowRemote, folderId) {
193
+ const envelope = this.db.getMessageByUid(accountId, uid, folderId);
194
+ if (!envelope)
195
+ return null;
196
+ const allowList = this.getCachedAllowlist();
197
+ const senderAddr = (envelope.from?.address || "").toLowerCase();
198
+ const senderDomain = senderAddr.split("@")[1] || "";
199
+ const toAddrs = (envelope.to || []).map((a) => (a.address || "").toLowerCase());
200
+ // Allowlist auto-allow: trusted sender / domain / recipient skips
201
+ // sanitization. Same rule as the legacy service implementation.
202
+ if (!allowRemote) {
203
+ const senders = (allowList.senders || []).map((s) => (s || "").toLowerCase());
204
+ const domains = (allowList.domains || []).map((d) => (d || "").toLowerCase());
205
+ const recipients = (allowList.recipients || []).map((r) => (r || "").toLowerCase());
206
+ if (senders.includes(senderAddr) ||
207
+ domains.includes(senderDomain) ||
208
+ toAddrs.some((a) => recipients.includes(a))) {
209
+ allowRemote = true;
210
+ }
211
+ }
212
+ const isFlagged = !!((allowList.flaggedSenders || []).some((s) => (s || "").toLowerCase() === senderAddr) ||
213
+ (allowList.flaggedDomains || []).some((d) => (d || "").toLowerCase() === senderDomain));
214
+ // Resolve body path: prefer the row's own bodyPath, fall back to
215
+ // the historical lookup. Either may be empty (body never fetched).
216
+ let storedPath = envelope.bodyPath || "";
217
+ if (!storedPath)
218
+ storedPath = this.db.getMessageBodyPath(accountId, uid) || "";
219
+ const empty = {
220
+ ...envelope,
221
+ bodyHtml: "", bodyText: "",
222
+ hasRemoteContent: false, remoteAllowed: allowRemote,
223
+ attachments: [],
224
+ cached: false,
225
+ deliveredTo: "", returnPath: "",
226
+ listUnsubscribe: "", listUnsubscribeMail: "", listUnsubscribeHttp: "", listUnsubscribeOneClick: false,
227
+ emlPath: "",
228
+ isFlagged,
229
+ };
230
+ if (!storedPath)
231
+ return empty;
232
+ if (!await this.bodyStore.hasByPath(storedPath))
233
+ return empty;
234
+ // Parse-cache lookup. Key = path + mtime so a re-fetched body
235
+ // (mtime changes) invalidates automatically. mtime stat is cheap
236
+ // (sub-ms) compared to the parse it avoids (~100-500 ms).
237
+ let mtimeMs = 0;
238
+ try {
239
+ mtimeMs = (await fs.promises.stat(storedPath)).mtimeMs;
240
+ }
241
+ catch { /* will fall through and fail at readByPath */ }
242
+ const cacheKey = `${storedPath}|${mtimeMs}`;
243
+ const cached = this.parsedLruGet(cacheKey);
244
+ if (cached) {
245
+ // Allowlist state can change between views even with the same
246
+ // body cached; recompute the volatile fields and overlay.
247
+ return { ...cached, remoteAllowed: allowRemote, isFlagged };
248
+ }
249
+ let raw;
250
+ try {
251
+ raw = await this.bodyStore.readByPath(storedPath);
252
+ }
253
+ catch {
254
+ // File path is in DB but the file is missing — the prefetch
255
+ // dot is lying. Caller (UI) should mark the row as broken and
256
+ // the reconciler should re-fetch. Don't crash; return as if
257
+ // not cached.
258
+ return empty;
259
+ }
260
+ // Parse + sanitize. Same logic as today's MailxService.getMessage,
261
+ // but executed only when the body is local — never on a fresh
262
+ // server fetch.
263
+ const adjusted = sniffAndFixCharset(raw);
264
+ // simpleParser blocks the event loop while it parses — visible cost
265
+ // on >100 KB messages. Time it so the log makes the cost concrete:
266
+ // when this number is high, that's why other IPC calls (the next
267
+ // message click, the folder-count update tick) stall.
268
+ const _parseT0 = Date.now();
269
+ const parsed = await parseSerial(adjusted);
270
+ const _parseMs = Date.now() - _parseT0;
271
+ if (_parseMs > 50) {
272
+ console.log(` [parse] simpleParser ${_parseMs}ms for ${(raw.length / 1024).toFixed(0)} KB (${storedPath})`);
273
+ }
274
+ let bodyHtml = parsed.html || "";
275
+ const bodyText = parsed.text || "";
276
+ // Backfill FTS body_text now that we've parsed the body. upsertMessage
277
+ // only had the short `preview` snippet at index time; without this
278
+ // backfill, searches miss any word that only appears deeper in the
279
+ // body. Fire-and-forget — failures are non-fatal, never block the
280
+ // user's preview render.
281
+ if (bodyText) {
282
+ try {
283
+ this.db.updateFtsBodyByUid(accountId, envelope.folderId, uid, bodyText);
284
+ }
285
+ catch { /* */ }
286
+ }
287
+ let hasRemoteContent = false;
288
+ // Filter out "spurious" attachments: mailing-list footers and signature
289
+ // blocks frequently arrive as a separate text/plain MIME part with
290
+ // Content-Disposition: inline and no filename. mailparser dutifully
291
+ // surfaces them as attachments. Showing them as attachment chips
292
+ // confuses the user (Bob 2026-05-13: spurious image-shaped chip on
293
+ // a list message that had no real attachment). Rule: if the part is
294
+ // text/* AND has no filename AND is dispositionally inline (or no
295
+ // disposition at all), it's body content, not a real attachment.
296
+ // Keep the original index even for filtered rows so getAttachment's
297
+ // index lookup still works for the surviving ones.
298
+ const attachments = (parsed.attachments || [])
299
+ .map((a, i) => {
300
+ const mime = (a.contentType || "application/octet-stream").toLowerCase();
301
+ const disposition = (a.contentDisposition || "").toLowerCase();
302
+ const isSpuriousTextPart = mime.startsWith("text/")
303
+ && !a.filename
304
+ && (disposition === "inline" || disposition === "");
305
+ return { a, i, isSpuriousTextPart };
306
+ })
307
+ .filter(x => !x.isSpuriousTextPart)
308
+ .map(x => ({
309
+ id: x.i,
310
+ filename: x.a.filename || `attachment-${x.i}`,
311
+ mimeType: x.a.contentType || "application/octet-stream",
312
+ size: x.a.size || 0,
313
+ contentId: x.a.contentId || "",
314
+ }));
315
+ if (bodyHtml && !allowRemote) {
316
+ const result = sanitizeHtml(bodyHtml);
317
+ bodyHtml = result.html;
318
+ hasRemoteContent = result.hasRemoteContent;
319
+ }
320
+ // Header extraction — Delivered-To, Return-Path, List-Unsubscribe.
321
+ // mlproc preprocesses Delivered-To server-side, so the inner
322
+ // address is already clean by the time mailx sees it. We take the
323
+ // last entry of the chain (the final-delivery hop). The
324
+ // `relayDomains` filtering layer was retired 2026-05-13: nothing
325
+ // configures it in practice and the conditional made the code
326
+ // harder to reason about than the one-liner that replaces it.
327
+ let deliveredTo = "";
328
+ const rawDelivered = parsed.headers.get("delivered-to");
329
+ if (rawDelivered) {
330
+ const deliveredList = Array.isArray(rawDelivered) ? rawDelivered : [rawDelivered];
331
+ const d = deliveredList[deliveredList.length - 1];
332
+ deliveredTo = typeof d === "string" ? d : d?.text || d?.address || String(d);
333
+ }
334
+ const hdr = (key) => {
335
+ let v = parsed.headers.get(key);
336
+ if (!v)
337
+ return "";
338
+ if (Array.isArray(v))
339
+ v = v[0];
340
+ if (typeof v === "string")
341
+ return v;
342
+ if (typeof v === "object" && v !== null) {
343
+ if ("text" in v)
344
+ return v.text || "";
345
+ if ("value" in v)
346
+ return String(v.value);
347
+ if ("address" in v)
348
+ return v.address || "";
349
+ }
350
+ return String(v);
351
+ };
352
+ const returnPath = hdr("return-path").replace(/[<>]/g, "");
353
+ const { listUnsubscribeMail, listUnsubscribeHttp, listUnsubscribeOneClick } = parseListUnsubscribe(parsed.headers);
354
+ const listUnsubscribe = listUnsubscribeHttp || listUnsubscribeMail;
355
+ const result = {
356
+ ...envelope,
357
+ bodyHtml, bodyText,
358
+ hasRemoteContent, remoteAllowed: allowRemote,
359
+ attachments,
360
+ cached: true,
361
+ deliveredTo, returnPath,
362
+ listUnsubscribe, listUnsubscribeMail, listUnsubscribeHttp, listUnsubscribeOneClick,
363
+ // body_path in the DB is stored relative to the body-store
364
+ // basePath; resolve to absolute so the UI's "Source" button
365
+ // can hand the path to an OS file open without re-deriving
366
+ // basePath every time.
367
+ emlPath: this.bodyStore.absolutePath(storedPath),
368
+ isFlagged,
369
+ };
370
+ // Memoize the parsed result for subsequent views of the same UID.
371
+ // `remoteAllowed` and `isFlagged` get re-overlaid on read so a flag
372
+ // toggle or allowlist edit doesn't require a re-parse to take
373
+ // effect (see the parsedLruGet path above).
374
+ //
375
+ // BUT don't poison the cache with an empty parse — a malformed .eml
376
+ // or a fetch that left a stub file gives bodyHtml === "" AND
377
+ // bodyText === ""; caching that means future views serve emptiness
378
+ // until the daemon restarts. Let the next view try again; the
379
+ // parse cost on a 9 kB .eml is ~20 ms, so re-parsing an oddity
380
+ // is cheap.
381
+ const hasContent = (bodyHtml && bodyHtml.length > 0) || (bodyText && bodyText.length > 0);
382
+ if (mtimeMs > 0 && hasContent)
383
+ this.parsedLruPut(cacheKey, result);
384
+ return result;
385
+ }
386
+ // ── Calendar / tasks / contacts (read paths) ──
387
+ getCalendarEvents(accountId, fromMs, toMs) {
388
+ return this.db.getCalendarEvents(accountId, fromMs, toMs);
389
+ }
390
+ getTasks(accountId, includeCompleted = false) {
391
+ return this.db.getTasks(accountId, includeCompleted);
392
+ }
393
+ searchContacts(query, limit = 10) {
394
+ return this.db.searchContacts(query, limit);
395
+ }
396
+ listContacts(query, page = 1, pageSize = 100) {
397
+ return this.db.listContacts(query, page, pageSize);
398
+ }
399
+ // ── Write paths (local-only; mirror to server is queued separately) ──
400
+ /** Update a message's flag set. Local DB write completes synchronously;
401
+ * the server-mirror enqueue is the caller's responsibility (typically
402
+ * via SyncQueue.enqueueFlag) so callers that don't want a server push
403
+ * — pure-local UI state like "pin in pane" — can skip it.
404
+ *
405
+ * Publishes:
406
+ * `message:<uuid>` { kind: "flagsChanged" }
407
+ * `folder:<id>` (auto fan-out)
408
+ */
409
+ updateFlags(accountId, uid, folderId, flags) {
410
+ this.db.updateMessageFlags(accountId, uid, flags);
411
+ const env = this.db.getMessageByUid(accountId, uid, folderId);
412
+ const msgUuid = env?.uuid;
413
+ if (msgUuid) {
414
+ this.bus.publish({
415
+ topic: `message:${msgUuid}`,
416
+ kind: "flagsChanged",
417
+ accountId, folderId, uid, msgUuid, flags,
418
+ });
419
+ }
420
+ }
421
+ /** Move a message between folders in the same account. Adds a tombstone
422
+ * on the Message-ID so the next sync doesn't re-import the pre-move row
423
+ * in the source folder before the server-side MOVE completes; tombstone
424
+ * is cleared on terminal IMAP failure (see processSyncActions).
425
+ *
426
+ * Returns true if a local row existed and was moved, false otherwise.
427
+ *
428
+ * Publishes:
429
+ * `message:<uuid>` { kind: "messageMoved", folderId: source, targetFolderId }
430
+ * `folder:<source>` and `folder:<target>` (auto fan-out + explicit count)
431
+ */
432
+ moveMessage(accountId, uid, fromFolderId, targetFolderId) {
433
+ const env = this.db.getMessageByUid(accountId, uid, fromFolderId);
434
+ if (!env)
435
+ return false;
436
+ if (env.messageId)
437
+ this.db.addTombstone(accountId, env.messageId, env.subject || "");
438
+ const moved = this.db.moveMessageLocal(accountId, uid, fromFolderId, targetFolderId);
439
+ if (!moved)
440
+ return false;
441
+ this.db.recalcFolderCounts(fromFolderId);
442
+ this.db.recalcFolderCounts(targetFolderId);
443
+ const msgUuid = env?.uuid;
444
+ if (msgUuid) {
445
+ this.bus.publish({
446
+ topic: `message:${msgUuid}`,
447
+ kind: "messageMoved",
448
+ accountId, folderId: fromFolderId, targetFolderId, uid, msgUuid,
449
+ });
450
+ }
451
+ // Folder count change isn't tied to a specific message uuid — publish
452
+ // to both folder topics directly. (The fan-out only covers the source
453
+ // via the message event's folderId; target needs its own publish.)
454
+ this.bus.publish({
455
+ topic: `folder:${targetFolderId}`,
456
+ kind: "folderCountsChanged",
457
+ accountId, folderId: targetFolderId,
458
+ });
459
+ return true;
460
+ }
461
+ /** Trash a message. If a trash folder is configured and the message is
462
+ * not already in it, this is a move-to-trash. If the message is already
463
+ * in trash (or no trash exists), it's a hard delete + body unlink.
464
+ *
465
+ * Returns "moved-to-trash" or "expunged" so the caller knows whether
466
+ * to enqueue an IMAP MOVE or a DELETE+EXPUNGE on the queue.
467
+ */
468
+ trashMessage(accountId, uid, folderId, trashFolderId) {
469
+ const env = this.db.getMessageByUid(accountId, uid, folderId);
470
+ if (env?.messageId)
471
+ this.db.addTombstone(accountId, env.messageId, env.subject || "");
472
+ const msgUuid = env?.uuid;
473
+ if (trashFolderId != null && trashFolderId !== folderId) {
474
+ this.db.moveMessageLocal(accountId, uid, folderId, trashFolderId);
475
+ this.db.recalcFolderCounts(folderId);
476
+ this.db.recalcFolderCounts(trashFolderId);
477
+ if (msgUuid) {
478
+ this.bus.publish({
479
+ topic: `message:${msgUuid}`,
480
+ kind: "messageMoved",
481
+ accountId, folderId, targetFolderId: trashFolderId, uid, msgUuid,
482
+ });
483
+ }
484
+ this.bus.publish({
485
+ topic: `folder:${trashFolderId}`,
486
+ kind: "folderCountsChanged",
487
+ accountId, folderId: trashFolderId,
488
+ });
489
+ return "moved-to-trash";
490
+ }
491
+ this.db.deleteMessage(accountId, uid, "user-initiated trash (already in trash → expunge)", "Store.trashMessage");
492
+ this.db.recalcFolderCounts(folderId);
493
+ if (msgUuid) {
494
+ this.bus.publish({
495
+ topic: `message:${msgUuid}`,
496
+ kind: "messageRemoved",
497
+ accountId, folderId, uid, msgUuid,
498
+ });
499
+ }
500
+ return "expunged";
501
+ }
502
+ /** Restore a message from trash back to its original folder. Local-only;
503
+ * caller handles the queue (cancel-pending-MOVE vs queue-counter-MOVE).
504
+ * Returns true if a local row was moved. */
505
+ undeleteMessage(accountId, uid, trashFolderId, originalFolderId) {
506
+ const moved = this.db.moveMessageLocal(accountId, uid, trashFolderId, originalFolderId);
507
+ if (!moved)
508
+ return false;
509
+ this.db.recalcFolderCounts(trashFolderId);
510
+ this.db.recalcFolderCounts(originalFolderId);
511
+ const env = this.db.getMessageByUid(accountId, uid, originalFolderId);
512
+ const msgUuid = env?.uuid;
513
+ if (msgUuid) {
514
+ this.bus.publish({
515
+ topic: `message:${msgUuid}`,
516
+ kind: "messageMoved",
517
+ accountId, folderId: trashFolderId, targetFolderId: originalFolderId, uid, msgUuid,
518
+ });
519
+ }
520
+ this.bus.publish({
521
+ topic: `folder:${originalFolderId}`,
522
+ kind: "folderCountsChanged",
523
+ accountId, folderId: originalFolderId,
524
+ });
525
+ return true;
526
+ }
527
+ }
528
+ //# sourceMappingURL=store.js.map