@rapidmx/web-client 0.3.1 → 0.5.0
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/apps/admin/audit-log/index.tsx +35 -8
- package/apps/admin/branding/index.tsx +39 -404
- package/apps/admin/data-requests/index.tsx +480 -485
- package/apps/admin/distribution-lists/[uid].tsx +19 -1
- package/apps/admin/domains/[uid].tsx +92 -259
- package/apps/admin/encryption-policy/index.tsx +19 -0
- package/apps/admin/escrow-scopes/[uid].tsx +348 -237
- package/apps/admin/escrow-scopes/new/index.tsx +8 -2
- package/apps/admin/index.tsx +118 -82
- package/apps/admin/ingest-queue/index.tsx +44 -5
- package/apps/admin/mailbox-policy/index.tsx +19 -0
- package/apps/admin/mailboxes/[uid].tsx +211 -184
- package/apps/admin/mailboxes/new/index.tsx +28 -291
- package/apps/admin/plugins/index.tsx +15 -0
- package/apps/admin/quarantine/index.tsx +46 -14
- package/apps/admin/retention-policy/index.tsx +39 -132
- package/apps/admin/setup/index.tsx +15 -0
- package/apps/admin/transport-rules/[uid].tsx +30 -1
- package/apps/admin/transport-rules/_transportRuleConfig.tsx +38 -2
- package/apps/admin/transport-rules/index.tsx +24 -6
- package/apps/admin/transport-rules/new/index.tsx +30 -1
- package/apps/escrow/_layout.tsx +2 -3
- package/apps/escrow/audit-log/index.tsx +196 -168
- package/apps/escrow/matters/[uid].tsx +621 -521
- package/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.tsx +22 -1
- package/apps/shared/components/admin/layout/AdminShell.tsx +251 -210
- package/apps/shared/components/admin/mailboxes/EscrowScopeCard.tsx +152 -0
- package/apps/shared/components/admin/mailboxes/ResourceSettingsCard.tsx +3 -3
- package/apps/shared/components/admin/mailboxes/ShareAccessCard.tsx +55 -11
- package/apps/shared/components/admin/settings/BrandingForm.tsx +385 -0
- package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +187 -0
- package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +112 -0
- package/apps/shared/components/admin/settings/LoadedSettingsForm.tsx +34 -0
- package/apps/shared/components/admin/settings/MailboxCreateForm.tsx +308 -0
- package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +152 -0
- package/apps/shared/components/admin/settings/PluginsManager.tsx +1235 -0
- package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +174 -0
- package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +290 -0
- package/apps/shared/components/admin/setup/SetupWizard.tsx +416 -0
- package/apps/shared/components/admin/signOut.ts +79 -0
- package/apps/shared/components/admin/usePagedList.tsx +129 -0
- package/apps/shared/components/calendar/CalendarListSidebar.tsx +120 -76
- package/apps/shared/components/calendar/EventModal.tsx +717 -513
- package/apps/shared/components/calendar/MonthView.tsx +173 -139
- package/apps/shared/components/calendar/RecurrenceEditor.tsx +222 -180
- package/apps/shared/components/calendar/SplitDayView.tsx +143 -137
- package/apps/shared/components/calendar/TimeGridView.tsx +244 -198
- package/apps/shared/components/calendar/allDay.ts +124 -0
- package/apps/shared/components/calendar/layout/CalendarShell.tsx +80 -99
- package/apps/shared/components/contacts/ContactDetailPane.tsx +100 -21
- package/apps/shared/components/contacts/ContactForm.tsx +377 -314
- package/apps/shared/components/contacts/ContactsSidebar.tsx +6 -2
- package/apps/shared/components/contacts/KeyChangeReview.tsx +192 -0
- package/apps/shared/components/contacts/contactKeys.ts +78 -0
- package/apps/shared/components/escrow/layout/EscrowShell.tsx +148 -134
- package/apps/shared/components/layout/AppShell.tsx +270 -169
- package/apps/shared/components/layout/BrandingChrome.tsx +48 -10
- package/apps/shared/components/layout/KeyEnrollmentGate.tsx +150 -26
- package/apps/shared/components/layout/MailboxProvisioning.tsx +19 -3
- package/apps/shared/components/layout/RecoveryCodeUnlock.tsx +350 -0
- package/apps/shared/components/layout/UnlockPromptProvider.tsx +257 -0
- package/apps/shared/components/mail/ConversationThreadPane.tsx +216 -181
- package/apps/shared/components/mail/MessageDetailPane.tsx +799 -76
- package/apps/shared/components/mail/compose/ComposeContext.tsx +26 -16
- package/apps/shared/components/mail/compose/ComposeWindow.tsx +1652 -765
- package/apps/shared/components/mail/compose/composeFlushRegistry.ts +60 -0
- package/apps/shared/components/mail/layout/MailShell.tsx +418 -302
- package/apps/shared/components/mail/pinnedSigners.ts +124 -0
- package/apps/shared/components/mail/verificationSeals.ts +125 -0
- package/apps/shared/components/mail/writableMailboxes.ts +101 -0
- package/apps/shared/components/rules/RuleBuilder.tsx +311 -276
- package/apps/shared/components/settings/layout/SettingsShell.tsx +1 -0
- package/apps/shared/mail/findWellKnownFolderUid.ts +13 -0
- package/apps/shared/mail/listAllPages.ts +39 -0
- package/apps/shared/search/LocalIndexLifecycle.tsx +98 -0
- package/apps/shared/search/localIndexBlockCipher.ts +83 -0
- package/apps/shared/search/localIndexBuilder.ts +481 -0
- package/apps/shared/search/localIndexKey.ts +27 -0
- package/apps/shared/search/localIndexRpcClient.ts +316 -0
- package/apps/shared/search/localIndexSchema.ts +236 -0
- package/apps/shared/search/localIndexSizePreference.ts +88 -0
- package/apps/shared/search/localIndexStorage.ts +98 -0
- package/apps/shared/search/localIndexVFS.ts +266 -0
- package/apps/shared/search/localIndexWorker.ts +926 -0
- package/apps/shared/search/searchTier2.ts +79 -0
- package/apps/shared/search/wa-sqlite-shims.d.ts +44 -0
- package/apps/www/calendar/index.tsx +438 -407
- package/apps/www/contacts/[uid].tsx +21 -2
- package/apps/www/contacts/index.tsx +148 -17
- package/apps/www/index.tsx +1314 -486
- package/apps/www/messages/[uid].tsx +23 -7
- package/apps/www/settings/auto-reply/index.tsx +134 -129
- package/apps/www/settings/booking-types/[uid].tsx +306 -300
- package/apps/www/settings/encryption/index.tsx +800 -215
- package/apps/www/settings/filters/[uid].tsx +171 -155
- package/apps/www/settings/filters/new/index.tsx +8 -1
- package/apps/www/settings/focused-inbox/index.tsx +150 -148
- package/apps/www/settings/privacy/index.tsx +494 -436
- package/apps/www/settings/read-receipts/index.tsx +148 -125
- package/apps/www/settings/sharing/index.tsx +274 -0
- package/apps/www/tasks/index.tsx +638 -530
- package/dist/apps/admin/audit-log/index.js +38 -8
- package/dist/apps/admin/branding/index.js +4 -98
- package/dist/apps/admin/data-requests/index.js +52 -72
- package/dist/apps/admin/distribution-lists/[uid].js +1 -1
- package/dist/apps/admin/domains/[uid].js +5 -68
- package/dist/apps/admin/encryption-policy/index.js +8 -0
- package/dist/apps/admin/escrow-scopes/[uid].js +91 -12
- package/dist/apps/admin/escrow-scopes/new/index.js +8 -3
- package/dist/apps/admin/index.js +17 -1
- package/dist/apps/admin/ingest-queue/index.js +24 -6
- package/dist/apps/admin/mailbox-policy/index.js +8 -0
- package/dist/apps/admin/mailboxes/[uid].js +20 -4
- package/dist/apps/admin/mailboxes/new/index.js +4 -89
- package/dist/apps/admin/plugins/index.js +6 -0
- package/dist/apps/admin/quarantine/index.js +20 -11
- package/dist/apps/admin/retention-policy/index.js +3 -36
- package/dist/apps/admin/setup/index.js +6 -0
- package/dist/apps/admin/transport-rules/[uid].js +18 -2
- package/dist/apps/admin/transport-rules/_transportRuleConfig.js +19 -0
- package/dist/apps/admin/transport-rules/index.js +22 -6
- package/dist/apps/admin/transport-rules/new/index.js +18 -2
- package/dist/apps/escrow/_layout.js +3 -3
- package/dist/apps/escrow/audit-log/index.js +26 -1
- package/dist/apps/escrow/matters/[uid].js +77 -42
- package/dist/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.js +11 -2
- package/dist/apps/shared/components/admin/layout/AdminShell.js +37 -4
- package/dist/apps/shared/components/admin/mailboxes/EscrowScopeCard.js +72 -0
- package/dist/apps/shared/components/admin/mailboxes/ResourceSettingsCard.js +3 -3
- package/dist/apps/shared/components/admin/mailboxes/ShareAccessCard.js +22 -4
- package/dist/apps/shared/components/admin/settings/BrandingForm.js +116 -0
- package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +83 -0
- package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.js +62 -0
- package/dist/apps/shared/components/admin/settings/LoadedSettingsForm.js +25 -0
- package/dist/apps/shared/components/admin/settings/MailboxCreateForm.js +112 -0
- package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.js +74 -0
- package/dist/apps/shared/components/admin/settings/PluginsManager.js +572 -0
- package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.js +73 -0
- package/dist/apps/shared/components/admin/setup/EscrowSetupStep.js +147 -0
- package/dist/apps/shared/components/admin/setup/SetupWizard.js +208 -0
- package/dist/apps/shared/components/admin/signOut.js +75 -0
- package/dist/apps/shared/components/admin/usePagedList.js +99 -0
- package/dist/apps/shared/components/calendar/CalendarListSidebar.js +24 -13
- package/dist/apps/shared/components/calendar/EventModal.js +154 -21
- package/dist/apps/shared/components/calendar/MonthView.js +12 -5
- package/dist/apps/shared/components/calendar/RecurrenceEditor.js +40 -5
- package/dist/apps/shared/components/calendar/SplitDayView.js +10 -4
- package/dist/apps/shared/components/calendar/TimeGridView.js +23 -7
- package/dist/apps/shared/components/calendar/allDay.js +111 -0
- package/dist/apps/shared/components/calendar/layout/CalendarShell.js +47 -39
- package/dist/apps/shared/components/contacts/ContactDetailPane.js +43 -5
- package/dist/apps/shared/components/contacts/ContactForm.js +45 -12
- package/dist/apps/shared/components/contacts/ContactsSidebar.js +2 -2
- package/dist/apps/shared/components/contacts/KeyChangeReview.js +63 -0
- package/dist/apps/shared/components/contacts/contactKeys.js +64 -0
- package/dist/apps/shared/components/escrow/layout/EscrowShell.js +24 -5
- package/dist/apps/shared/components/layout/AppShell.js +100 -8
- package/dist/apps/shared/components/layout/BrandingChrome.js +45 -10
- package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +63 -14
- package/dist/apps/shared/components/layout/MailboxProvisioning.js +10 -2
- package/dist/apps/shared/components/layout/RecoveryCodeUnlock.js +174 -0
- package/dist/apps/shared/components/layout/UnlockPromptProvider.js +136 -0
- package/dist/apps/shared/components/mail/ConversationThreadPane.js +50 -12
- package/dist/apps/shared/components/mail/MessageDetailPane.js +464 -34
- package/dist/apps/shared/components/mail/compose/ComposeContext.js +9 -6
- package/dist/apps/shared/components/mail/compose/ComposeWindow.js +807 -116
- package/dist/apps/shared/components/mail/compose/composeFlushRegistry.js +50 -0
- package/dist/apps/shared/components/mail/layout/MailShell.js +116 -41
- package/dist/apps/shared/components/mail/pinnedSigners.js +98 -0
- package/dist/apps/shared/components/mail/verificationSeals.js +112 -0
- package/dist/apps/shared/components/mail/writableMailboxes.js +87 -0
- package/dist/apps/shared/components/rules/RuleBuilder.js +34 -6
- package/dist/apps/shared/components/settings/layout/SettingsShell.js +1 -0
- package/dist/apps/shared/mail/findWellKnownFolderUid.js +12 -0
- package/dist/apps/shared/mail/listAllPages.js +25 -0
- package/dist/apps/shared/search/LocalIndexLifecycle.js +80 -0
- package/dist/apps/shared/search/localIndexBlockCipher.js +63 -0
- package/dist/apps/shared/search/localIndexBuilder.js +408 -0
- package/dist/apps/shared/search/localIndexKey.js +25 -0
- package/dist/apps/shared/search/localIndexRpcClient.js +241 -0
- package/dist/apps/shared/search/localIndexSchema.js +185 -0
- package/dist/apps/shared/search/localIndexSizePreference.js +78 -0
- package/dist/apps/shared/search/localIndexStorage.js +86 -0
- package/dist/apps/shared/search/localIndexVFS.js +258 -0
- package/dist/apps/shared/search/localIndexWorker.js +687 -0
- package/dist/apps/shared/search/searchTier2.js +38 -0
- package/dist/apps/www/calendar/index.js +38 -18
- package/dist/apps/www/contacts/[uid].js +13 -2
- package/dist/apps/www/contacts/index.js +94 -15
- package/dist/apps/www/index.js +770 -137
- package/dist/apps/www/messages/[uid].js +23 -7
- package/dist/apps/www/settings/auto-reply/index.js +9 -4
- package/dist/apps/www/settings/booking-types/[uid].js +17 -12
- package/dist/apps/www/settings/encryption/index.js +541 -96
- package/dist/apps/www/settings/filters/[uid].js +22 -7
- package/dist/apps/www/settings/filters/new/index.js +8 -2
- package/dist/apps/www/settings/focused-inbox/index.js +3 -1
- package/dist/apps/www/settings/privacy/index.js +57 -22
- package/dist/apps/www/settings/read-receipts/index.js +10 -3
- package/dist/apps/www/settings/sharing/index.js +124 -0
- package/dist/apps/www/tasks/index.js +76 -14
- package/package.json +3 -2
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
/**
|
|
6
|
+
* The Tier 2 local index's background builder (`specs/search.md` §11 "Initial build... incrementally,
|
|
7
|
+
* newest-first"). Runs on the main thread - the actual storage I/O and encryption happen in
|
|
8
|
+
* `localIndexWorker.ts` (off the UI thread, per that file's own doc comment); this module's own work is
|
|
9
|
+
* orchestration (deciding what to fetch) plus already-async fetch/decrypt calls, not CPU-heavy work that
|
|
10
|
+
* would itself need to move off-thread.
|
|
11
|
+
*
|
|
12
|
+
* **Known scoping simplification**: walks the mailbox's real mail folders one at a time (a fixed,
|
|
13
|
+
* reasonable priority order - Inbox and Sent first), each already newest-first via `listMessages()`'s own
|
|
14
|
+
* default sort, rather than a true interleaved k-way merge producing one single globally-newest-first
|
|
15
|
+
* stream across folders. A message in, say, Sent slightly older than the *oldest-processed-so-far*
|
|
16
|
+
* message in Inbox can therefore be indexed slightly out of true global date order relative to it. Given
|
|
17
|
+
* the byte-budget eviction below is itself date-based (oldest `date_for_sort` first, enforced by
|
|
18
|
+
* `localIndexWorker.ts`'s own `applyEviction()` after every insert - see that file), a small amount of
|
|
19
|
+
* cross-folder interleaving imprecision here does not affect *what* ultimately survives the window, only
|
|
20
|
+
* the exact order entities are inserted (and therefore briefly evicted-and-reinserted) in.
|
|
21
|
+
*
|
|
22
|
+
* Only encrypted messages (`subject === "[...]"`) are indexed - see `ENCRYPTED_SUBJECT_PLACEHOLDER`'s
|
|
23
|
+
* own precedent in `apps/www/index.tsx`'s inbox-list decrypt work. An unencrypted message is already
|
|
24
|
+
* fully searchable via Tier 1; indexing it here too would spend this index's bounded byte budget on
|
|
25
|
+
* content that didn't need it.
|
|
26
|
+
*
|
|
27
|
+
* **Verification seals.** A message this pass decrypts that has no seal for the vault's current `masterKeyGeneration` is
|
|
28
|
+
* evaluated with `evaluateMessageSecurityWithSeal()`, against the sender's pinned signing keys from the reader's
|
|
29
|
+
* contacts (`pinnedSigners.ts`, the message pane's own source, looked up once per sender per pass), so a verified
|
|
30
|
+
* message gets its seal without being opened (see `apps/shared/components/mail/verificationSeals.ts`). Pins only change
|
|
31
|
+
* the security state, never the recovered subject or body this index stores. Seal writes are best effort and bounded:
|
|
32
|
+
* at most `SEAL_WRITE_CONCURRENCY` in flight, at most `MAX_SEAL_WRITES_PER_PASS` per pass (past it, messages are
|
|
33
|
+
* evaluated without seals), failures ignored, and nothing new starts once the pass is aborted or its keys are destroyed.
|
|
34
|
+
* A seal carries only a hash, fingerprint, state and time, never plaintext. With no readable vault generation the pass
|
|
35
|
+
* seals nothing. Signed-only (unencrypted) messages aren't sealed here: this pass never fetches their raw MIME (see
|
|
36
|
+
* above), and fetching it only to seal would cost a download per message; they are sealed when opened.
|
|
37
|
+
*/
|
|
38
|
+
import { getMessageRawContent, listFolders, listMessages } from "@rapidmx/react-shared/mail/mailApi.js";
|
|
39
|
+
import { evaluateMessageSecurity, evaluateMessageSecurityWithSeal } from "@rapidmx/react-shared/crypto/messageSecurity.js";
|
|
40
|
+
import { getPinnedSignerFingerprints } from "../components/mail/pinnedSigners.js";
|
|
41
|
+
import { currentVerificationSeal, readVaultGeneration, sendVerificationSeal, verificationSealPending } from "../components/mail/verificationSeals.js";
|
|
42
|
+
import { getIndexedVersions, indexLocalEntities, initLocalIndex, nextLocalIndexGeneration, pruneLocalEntities, setLocalIndexBuilding, setLocalIndexWindow, } from "./localIndexRpcClient.js";
|
|
43
|
+
import { deriveLocalIndexKey } from "./localIndexKey.js";
|
|
44
|
+
import { getLocalIndexByteBudget } from "./localIndexSizePreference.js";
|
|
45
|
+
/** The RFC 9788 placeholder subject every encrypted message's outer envelope carries server-side - see
|
|
46
|
+
* `apps/www/index.tsx`'s identical constant and its own doc comment for the full citation. */
|
|
47
|
+
const ENCRYPTED_SUBJECT_PLACEHOLDER = "[...]";
|
|
48
|
+
/** §11's Window Sizing table's time-floor column - explicitly unvalidated starting-point default per the
|
|
49
|
+
* spec's own §16 "Measurement Task" ("cannot be supplied by design work... MUST NOT be treated as
|
|
50
|
+
* validated"). Used as-is rather than invented/adjusted here. Not user-adjustable today (unlike the byte
|
|
51
|
+
* budget - see `localIndexSizePreference.ts`) - nothing in this codebase has asked for that yet. */
|
|
52
|
+
export const WEB_TIME_FLOOR_MONTHS = 12;
|
|
53
|
+
/** Folder types that actually hold messages, including `user` (custom folders). Inbox and Sent first: the two
|
|
54
|
+
* folders a "did I find that email" search is overwhelmingly likely to land in, so they're covered soonest if
|
|
55
|
+
* the build is interrupted (tab closed, idle timeout) partway through. */
|
|
56
|
+
const MESSAGE_FOLDER_TYPES = new Set(["inbox", "sent_items", "drafts", "deleted_items", "outbox", "junk", "archive", "user"]);
|
|
57
|
+
/** Folder types known not to hold mail. Any folder in neither set is skipped, and the pass is then not
|
|
58
|
+
* complete - a type this module doesn't know about may well hold messages. */
|
|
59
|
+
const NON_MESSAGE_FOLDER_TYPES = new Set(["calendar", "contacts", "tasks", "notes"]);
|
|
60
|
+
const FOLDER_PRIORITY = { inbox: 0, sent_items: 1 };
|
|
61
|
+
/** How many times a pass walks a folder whose total count changed during the walk. `listMessages()` only
|
|
62
|
+
* pages by offset, so a message removed from an already-walked page shifts a later one onto it unseen;
|
|
63
|
+
* only a folder whose count held steady across a whole walk is trusted for completeness and pruning. */
|
|
64
|
+
export const MAX_WALK_ATTEMPTS = 3;
|
|
65
|
+
/** Subtracted from the pass's start time before it's recorded as the end of guaranteed coverage - a
|
|
66
|
+
* message's server-assigned `receivedDate` can be ahead of this device's clock. */
|
|
67
|
+
export const CLOCK_SKEW_MARGIN_MS = 10 * 60 * 1000;
|
|
68
|
+
const PAGE_SIZE = 100;
|
|
69
|
+
const MAX_APPROX_BYTES_PER_MESSAGE_PADDING = 512; // subject/participants/flags overhead beyond raw text length
|
|
70
|
+
const FETCH_FAILED = Symbol("fetch-failed");
|
|
71
|
+
/** How many raw-MIME fetch+decrypt calls run at once - a page of 100 encrypted messages used to fire all
|
|
72
|
+
* 100 requests simultaneously. */
|
|
73
|
+
export const FETCH_CONCURRENCY = 6;
|
|
74
|
+
/** How many verification seal writes run at once during a pass. */
|
|
75
|
+
export const SEAL_WRITE_CONCURRENCY = 2;
|
|
76
|
+
/** The most verification seals one pass writes. */
|
|
77
|
+
export const MAX_SEAL_WRITES_PER_PASS = 200;
|
|
78
|
+
function createPassSealer(mailboxUid, unlocked, signal) {
|
|
79
|
+
let generation;
|
|
80
|
+
const pins = new Map();
|
|
81
|
+
const queue = [];
|
|
82
|
+
let queued = 0;
|
|
83
|
+
let active = 0;
|
|
84
|
+
let idle;
|
|
85
|
+
const stopped = () => signal.aborted || !!unlocked.destroyed;
|
|
86
|
+
const vaultGeneration = () => (generation ?? (generation = readVaultGeneration(mailboxUid)));
|
|
87
|
+
const pinsFor = (address) => {
|
|
88
|
+
const key = address.toLowerCase();
|
|
89
|
+
let entry = pins.get(key);
|
|
90
|
+
if (!entry) {
|
|
91
|
+
entry = getPinnedSignerFingerprints(mailboxUid, address).then((fingerprints) => (fingerprints.length > 0 ? fingerprints : undefined), () => undefined);
|
|
92
|
+
pins.set(key, entry);
|
|
93
|
+
}
|
|
94
|
+
return entry;
|
|
95
|
+
};
|
|
96
|
+
function pump() {
|
|
97
|
+
if (stopped()) {
|
|
98
|
+
queue.length = 0;
|
|
99
|
+
}
|
|
100
|
+
while (active < SEAL_WRITE_CONCURRENCY && queue.length > 0) {
|
|
101
|
+
const write = queue.shift();
|
|
102
|
+
active++;
|
|
103
|
+
void write().finally(() => {
|
|
104
|
+
active--;
|
|
105
|
+
pump();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (active === 0 && queue.length === 0) {
|
|
109
|
+
idle?.();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
async evaluate(message, rawMime) {
|
|
114
|
+
const current = await vaultGeneration();
|
|
115
|
+
const { seal, sealGeneration } = currentVerificationSeal(message);
|
|
116
|
+
if (current === undefined || stopped() || queued >= MAX_SEAL_WRITES_PER_PASS || !verificationSealPending(message.uid, current) || (seal && sealGeneration === current)) {
|
|
117
|
+
return evaluateMessageSecurity(rawMime, unlocked);
|
|
118
|
+
}
|
|
119
|
+
const result = await evaluateMessageSecurityWithSeal(rawMime, unlocked, await pinsFor(message.from.address), undefined, {
|
|
120
|
+
mailboxUid,
|
|
121
|
+
messageUid: message.uid,
|
|
122
|
+
seal,
|
|
123
|
+
sealGeneration,
|
|
124
|
+
masterKeyGeneration: current,
|
|
125
|
+
});
|
|
126
|
+
const sealToWrite = result.sealToWrite;
|
|
127
|
+
if (sealToWrite && queued < MAX_SEAL_WRITES_PER_PASS) {
|
|
128
|
+
queued++;
|
|
129
|
+
queue.push(() => sendVerificationSeal(message.uid, sealToWrite));
|
|
130
|
+
pump();
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
},
|
|
134
|
+
drain() {
|
|
135
|
+
return new Promise((resolve) => {
|
|
136
|
+
idle = resolve;
|
|
137
|
+
pump();
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** `cutoff === undefined` means no time floor at all (an unbounded `windowConfig`) - every message is
|
|
143
|
+
* "within" it, so the caller's own end-of-folder check (`messages.length < PAGE_SIZE`) becomes the only
|
|
144
|
+
* stopping condition. */
|
|
145
|
+
function isWithinTimeFloor(receivedDate, cutoff) {
|
|
146
|
+
return !cutoff || new Date(receivedDate).getTime() >= cutoff.getTime();
|
|
147
|
+
}
|
|
148
|
+
/** A relative weight only - the worker enforces the budget against the database's real measured size and
|
|
149
|
+
* uses these per-row values just to decide how much of the oldest range to evict at once. UTF-8 bytes,
|
|
150
|
+
* not UTF-16 code units. */
|
|
151
|
+
function estimateByteSize(entity) {
|
|
152
|
+
const encoder = new TextEncoder();
|
|
153
|
+
const textBytes = [entity.subject, entity.body, entity.attachmentText, entity.participants].reduce((total, value) => total + (value ? encoder.encode(value).length : 0), 0);
|
|
154
|
+
return textBytes + MAX_APPROX_BYTES_PER_MESSAGE_PADDING;
|
|
155
|
+
}
|
|
156
|
+
/** The change marker stored per row: a new server `version` (flags, content) or a folder move both
|
|
157
|
+
* invalidate the indexed copy. */
|
|
158
|
+
function versionOf(message) {
|
|
159
|
+
return `${message.version}:${message.folderUid}`;
|
|
160
|
+
}
|
|
161
|
+
/** `Promise.all(items.map(fn))`, but with at most `limit` calls in flight. Preserves input order. */
|
|
162
|
+
async function mapWithConcurrency(items, limit, fn) {
|
|
163
|
+
const results = new Array(items.length);
|
|
164
|
+
let next = 0;
|
|
165
|
+
async function worker() {
|
|
166
|
+
while (next < items.length) {
|
|
167
|
+
const index = next++;
|
|
168
|
+
results[index] = await fn(items[index]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
172
|
+
return results;
|
|
173
|
+
}
|
|
174
|
+
/** Decrypts one message and shapes it into a `LocalIndexEntity`, or `undefined` when nothing usable was
|
|
175
|
+
* recovered (a decrypt failure, or a message that turns out not to actually be encrypted despite the
|
|
176
|
+
* placeholder subject) - mirrors `searchTier3.ts`'s own `!security.html && !security.subject` discard
|
|
177
|
+
* rule exactly, for the same reason. */
|
|
178
|
+
async function buildEntity(message, sealer) {
|
|
179
|
+
let rawMime;
|
|
180
|
+
try {
|
|
181
|
+
rawMime = await getMessageRawContent(message.uid);
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Distinct from "undecryptable": a message that couldn't even be fetched may well be searchable,
|
|
185
|
+
// so the pass can't claim complete coverage.
|
|
186
|
+
return FETCH_FAILED;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const security = await sealer.evaluate(message, rawMime);
|
|
190
|
+
if (!security.subject && !security.html) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
const participants = [message.from.address, message.from.displayName, ...message.recipients.map((r) => r.address)]
|
|
194
|
+
.filter(Boolean)
|
|
195
|
+
.join(" ");
|
|
196
|
+
const setFlags = Object.entries(message.flags)
|
|
197
|
+
.filter(([, value]) => value)
|
|
198
|
+
.map(([key]) => key);
|
|
199
|
+
// Leading/trailing comma so `flags LIKE '%,x,%'` (localIndexSchema.ts's buildSearchPredicates())
|
|
200
|
+
// matches correctly even for the first/last flag in the list.
|
|
201
|
+
const flags = `,${setFlags.join(",")},`;
|
|
202
|
+
const entity = {
|
|
203
|
+
entityType: "message",
|
|
204
|
+
entityUid: message.uid,
|
|
205
|
+
mailboxUid: message.mailboxUid,
|
|
206
|
+
folderUid: message.folderUid,
|
|
207
|
+
dateForSort: message.receivedDate,
|
|
208
|
+
participants,
|
|
209
|
+
flags,
|
|
210
|
+
hasAttachments: message.hasAttachments,
|
|
211
|
+
subject: security.subject,
|
|
212
|
+
body: security.html,
|
|
213
|
+
byteSize: 0, // filled in below, after the fields above are known
|
|
214
|
+
entityVersion: versionOf(message),
|
|
215
|
+
};
|
|
216
|
+
entity.byteSize = estimateByteSize(entity);
|
|
217
|
+
return entity;
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// Best-effort, matching searchTier3.ts's own Promise.allSettled-per-candidate posture - one
|
|
221
|
+
// message's fetch/decrypt failure never aborts the rest of the build.
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/** Runs one full incremental build pass for `mailboxUid`: sets the window, walks mail folders newest-first
|
|
226
|
+
* (per this module's own doc comment on the folder-order simplification), decrypts and indexes only
|
|
227
|
+
* `"[...]"`-subject messages until each folder's own coverage passes the time floor, then clears the
|
|
228
|
+
* `building` flag. Messages already indexed at the same version are skipped, fetches run at most
|
|
229
|
+
* `FETCH_CONCURRENCY` at a time, and messages older than the eviction watermark (`WindowState.evictedBefore`)
|
|
230
|
+
* are neither fetched nor walked past. A failure partway through leaves whatever was indexed so far in place
|
|
231
|
+
* and records the pass as incomplete (spec §11's "incomplete-index UX... MUST indicate that coverage is
|
|
232
|
+
* partial" - see `Coverage.complete`). Rows the server no longer lists (deleted or moved elsewhere by any
|
|
233
|
+
* client) are pruned only within folders whose count held steady for a whole walk (see `MAX_WALK_ATTEMPTS`).
|
|
234
|
+
*
|
|
235
|
+
* Only one pass runs per mailbox (a newer call cancels the older), and every Worker call carries this pass's
|
|
236
|
+
* generation, so a pass outlived by a destroy can't write anything (see `cancelLocalIndexBuild()`).
|
|
237
|
+
*
|
|
238
|
+
* `windowConfig` defaults to this device's own configured byte budget (`getLocalIndexByteBudget()` -
|
|
239
|
+
* 500 MB in a browser tab, 1 GB in Electron, or whatever the user has since set in Settings > Encryption)
|
|
240
|
+
* alongside the fixed time floor above. Evaluated fresh on every call with no explicit override, so a
|
|
241
|
+
* preference change in Settings takes effect starting with this mailbox's next build pass (its next
|
|
242
|
+
* unlock), without requiring a reload.
|
|
243
|
+
*/
|
|
244
|
+
export async function buildLocalIndex(mailboxUid, unlocked, folders, windowConfig = { timeFloorMonths: WEB_TIME_FLOOR_MONTHS, byteBudgetBytes: getLocalIndexByteBudget() }) {
|
|
245
|
+
// At most one pass per mailbox: a newer one cancels the older and waits for it to wind down first.
|
|
246
|
+
const previous = activeBuilds.get(mailboxUid);
|
|
247
|
+
const controller = new AbortController();
|
|
248
|
+
const run = (async () => {
|
|
249
|
+
if (previous) {
|
|
250
|
+
previous.controller.abort();
|
|
251
|
+
await previous.settled;
|
|
252
|
+
}
|
|
253
|
+
await runBuildPass(mailboxUid, unlocked, folders, windowConfig, controller.signal);
|
|
254
|
+
})();
|
|
255
|
+
const entry = { controller, settled: run.catch(() => undefined) };
|
|
256
|
+
activeBuilds.set(mailboxUid, entry);
|
|
257
|
+
try {
|
|
258
|
+
await run;
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
if (activeBuilds.get(mailboxUid) === entry) {
|
|
262
|
+
activeBuilds.delete(mailboxUid);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const activeBuilds = new Map();
|
|
267
|
+
/** Stops a mailbox's running build pass, if any (e.g. its keys were just destroyed), resolving once it has
|
|
268
|
+
* wound down. The Worker independently rejects the pass's later calls once the index is destroyed (see
|
|
269
|
+
* `GenerationParams`); this just stops the fetching and decrypting too. Never rejects. */
|
|
270
|
+
export async function cancelLocalIndexBuild(mailboxUid) {
|
|
271
|
+
const active = activeBuilds.get(mailboxUid);
|
|
272
|
+
if (active) {
|
|
273
|
+
active.controller.abort();
|
|
274
|
+
await active.settled;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function isOlderThan(date, watermark) {
|
|
278
|
+
return !!watermark && new Date(date).getTime() < new Date(watermark).getTime();
|
|
279
|
+
}
|
|
280
|
+
async function runBuildPass(mailboxUid, unlocked, folders, windowConfig, signal) {
|
|
281
|
+
const generation = nextLocalIndexGeneration();
|
|
282
|
+
const indexKey = await deriveLocalIndexKey(unlocked.masterKey, mailboxUid);
|
|
283
|
+
signal.throwIfAborted();
|
|
284
|
+
await initLocalIndex({ mailboxUid, indexKey, generation });
|
|
285
|
+
let { evictedBefore } = await setLocalIndexWindow(mailboxUid, windowConfig.timeFloorMonths, windowConfig.byteBudgetBytes, generation);
|
|
286
|
+
await setLocalIndexBuilding(mailboxUid, true, { complete: false, generation });
|
|
287
|
+
// Mail arriving after this moment isn't guaranteed to be picked up by this pass.
|
|
288
|
+
const coveredUntil = new Date(Date.now() - CLOCK_SKEW_MARGIN_MS).toISOString();
|
|
289
|
+
let cutoff;
|
|
290
|
+
if (windowConfig.timeFloorMonths > 0) {
|
|
291
|
+
cutoff = new Date();
|
|
292
|
+
cutoff.setMonth(cutoff.getMonth() - windowConfig.timeFloorMonths);
|
|
293
|
+
}
|
|
294
|
+
// A pass is "complete" only if every folder was walked back to the time floor (or its end) with a stable
|
|
295
|
+
// count, no listing/fetch failure and no budget cut-off - the only case where coverage can be used to
|
|
296
|
+
// narrow Tier 3.
|
|
297
|
+
let complete = true;
|
|
298
|
+
const sealer = createPassSealer(mailboxUid, unlocked, signal);
|
|
299
|
+
try {
|
|
300
|
+
if (folders.some((f) => !MESSAGE_FOLDER_TYPES.has(f.type) && !NON_MESSAGE_FOLDER_TYPES.has(f.type))) {
|
|
301
|
+
complete = false;
|
|
302
|
+
}
|
|
303
|
+
const mailFolders = folders
|
|
304
|
+
.filter((f) => MESSAGE_FOLDER_TYPES.has(f.type))
|
|
305
|
+
.sort((a, b) => (FOLDER_PRIORITY[a.type] ?? 99) - (FOLDER_PRIORITY[b.type] ?? 99));
|
|
306
|
+
const seenEncryptedUids = new Set();
|
|
307
|
+
async function walkFolder(folder) {
|
|
308
|
+
for (let page = 0;; page++) {
|
|
309
|
+
signal.throwIfAborted();
|
|
310
|
+
let messages;
|
|
311
|
+
try {
|
|
312
|
+
messages = await listMessages(folder.uid, { page, limit: PAGE_SIZE });
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
return "failed";
|
|
316
|
+
}
|
|
317
|
+
if (messages.length === 0) {
|
|
318
|
+
return "done";
|
|
319
|
+
}
|
|
320
|
+
const encrypted = messages.filter((m) => m.subject === ENCRYPTED_SUBJECT_PLACEHOLDER);
|
|
321
|
+
for (const m of encrypted) {
|
|
322
|
+
seenEncryptedUids.add(m.uid);
|
|
323
|
+
}
|
|
324
|
+
// Anything older than the eviction watermark would only be inserted and evicted again.
|
|
325
|
+
const candidates = encrypted.filter((m) => !isOlderThan(m.receivedDate, evictedBefore));
|
|
326
|
+
if (candidates.length > 0) {
|
|
327
|
+
// Skip anything already indexed at the same version - a re-unlock used to re-fetch and
|
|
328
|
+
// re-decrypt the entire window every time.
|
|
329
|
+
const indexed = await getIndexedVersions(mailboxUid, candidates.map((m) => m.uid));
|
|
330
|
+
const changed = candidates.filter((m) => indexed[m.uid] !== versionOf(m));
|
|
331
|
+
const built = await mapWithConcurrency(changed, FETCH_CONCURRENCY, (m) => buildEntity(m, sealer));
|
|
332
|
+
signal.throwIfAborted();
|
|
333
|
+
if (built.includes(FETCH_FAILED)) {
|
|
334
|
+
complete = false;
|
|
335
|
+
}
|
|
336
|
+
const entities = built.filter((e) => e !== undefined && e !== FETCH_FAILED);
|
|
337
|
+
if (entities.length > 0) {
|
|
338
|
+
({ evictedBefore } = await indexLocalEntities(mailboxUid, entities, generation));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const oldestOnPage = messages[messages.length - 1].receivedDate;
|
|
342
|
+
if (evictedBefore && isOlderThan(oldestOnPage, evictedBefore) && isWithinTimeFloor(evictedBefore, cutoff)) {
|
|
343
|
+
return "budget";
|
|
344
|
+
}
|
|
345
|
+
if (!isWithinTimeFloor(oldestOnPage, cutoff) || messages.length < PAGE_SIZE) {
|
|
346
|
+
return "done";
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/** Each folder's current total, or `undefined` if the listing failed. */
|
|
351
|
+
async function folderTotals() {
|
|
352
|
+
try {
|
|
353
|
+
return new Map((await listFolders(mailboxUid)).map((f) => [f.uid, f.totalCount]));
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// Folders walked with a steady count and no listing failure - the only ones whose unseen rows can be
|
|
360
|
+
// pruned (deleted, or moved elsewhere by any client).
|
|
361
|
+
const reliable = [];
|
|
362
|
+
let totalsBefore = await folderTotals();
|
|
363
|
+
let toWalk = mailFolders;
|
|
364
|
+
for (let attempt = 0; attempt < MAX_WALK_ATTEMPTS && toWalk.length > 0; attempt++) {
|
|
365
|
+
const outcomes = new Map();
|
|
366
|
+
for (const folder of toWalk) {
|
|
367
|
+
outcomes.set(folder.uid, await walkFolder(folder));
|
|
368
|
+
}
|
|
369
|
+
const totalsAfter = await folderTotals();
|
|
370
|
+
const changed = [];
|
|
371
|
+
for (const folder of toWalk) {
|
|
372
|
+
const outcome = outcomes.get(folder.uid);
|
|
373
|
+
if (outcome === "failed" || !totalsBefore || !totalsAfter) {
|
|
374
|
+
// A failed listing, or no counts to vouch for the walk with: indexed as far as it got, but
|
|
375
|
+
// neither complete nor prunable (see the `reliable` check below).
|
|
376
|
+
}
|
|
377
|
+
else if (totalsAfter.get(folder.uid) === totalsBefore.get(folder.uid)) {
|
|
378
|
+
reliable.push(folder.uid);
|
|
379
|
+
complete && (complete = outcome === "done");
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
changed.push(folder);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
totalsBefore = totalsAfter;
|
|
386
|
+
toWalk = changed;
|
|
387
|
+
}
|
|
388
|
+
// Failed, count-less, or still changing after every attempt.
|
|
389
|
+
if (reliable.length < mailFolders.length) {
|
|
390
|
+
complete = false;
|
|
391
|
+
}
|
|
392
|
+
if (reliable.length > 0) {
|
|
393
|
+
// Every reliable folder was walked back to at least the watermark (or the floor).
|
|
394
|
+
const since = evictedBefore && isWithinTimeFloor(evictedBefore, cutoff) ? evictedBefore : cutoff?.toISOString();
|
|
395
|
+
await pruneLocalEntities(mailboxUid, [...seenEncryptedUids], since, { folderUids: reliable, generation });
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
catch (err) {
|
|
399
|
+
complete = false;
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
402
|
+
finally {
|
|
403
|
+
// Rejected (harmlessly) when the index was destroyed mid-pass.
|
|
404
|
+
await setLocalIndexBuilding(mailboxUid, false, { complete, coveredFrom: cutoff?.toISOString(), coveredUntil, generation }).catch(() => undefined);
|
|
405
|
+
// At most `SEAL_WRITE_CONCURRENCY` writes are still in flight; queued ones are dropped if the pass was aborted.
|
|
406
|
+
await sealer.drain();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
/**
|
|
6
|
+
* Derives the page-encryption key for one mailbox's Tier 2 local index (`specs/search.md` §11
|
|
7
|
+
* "Persistence and protection... MUST be encrypted at rest under the mailbox master key (MK)... using
|
|
8
|
+
* the same AEAD construction"). Reuses `crypto/masterKey.ts`'s existing `hkdfDerive()` - the same HKDF
|
|
9
|
+
* primitive every other MK-derived key in this codebase already goes through.
|
|
10
|
+
*
|
|
11
|
+
* No persisted per-install salt: MK is already 32 bytes of uniform random key material (see
|
|
12
|
+
* `masterKey.ts`'s own `generateMasterKey()`), so a fixed, non-secret salt is standard HKDF practice
|
|
13
|
+
* here (RFC 5869 - a salt matters for stretching low-entropy input, not for re-randomizing input that's
|
|
14
|
+
* already uniformly random) and avoids needing to persist, transmit, or ever lose a random salt just to
|
|
15
|
+
* re-derive the same key on the next unlock.
|
|
16
|
+
*/
|
|
17
|
+
import { hkdfDerive } from "@rapidmx/react-shared/crypto/masterKey.js";
|
|
18
|
+
/** Fixed, non-secret HKDF salt - see this module's own doc comment for why a fixed salt is fine here. */
|
|
19
|
+
const FIXED_SALT = new TextEncoder().encode("rapidmx-local-search-index-v1");
|
|
20
|
+
/** Derives this mailbox's local-index page-encryption key from its already-unlocked master key. Every
|
|
21
|
+
* call with the same `(masterKey, mailboxUid)` pair returns the identical key - callers never need to
|
|
22
|
+
* persist it themselves. */
|
|
23
|
+
export async function deriveLocalIndexKey(masterKey, mailboxUid) {
|
|
24
|
+
return hkdfDerive(masterKey, FIXED_SALT, `local-search-index:${mailboxUid}`);
|
|
25
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { removeLocalIndexDirectories, removeLocalIndexDirectory } from "./localIndexStorage.js";
|
|
2
|
+
let worker;
|
|
3
|
+
let nextRequestId = 1;
|
|
4
|
+
const pending = new Map();
|
|
5
|
+
/** Every mailboxUid `initLocalIndex()` has opened this session, not yet `destroyLocalIndex()`-ed. Gates
|
|
6
|
+
* the in-session mutation helpers (`removeLocalEntity()`/`moveLocalEntity()`) so a delete/move in a
|
|
7
|
+
* mailbox this tab never indexed doesn't spin up the Worker just to no-op. */
|
|
8
|
+
const initializedMailboxes = new Set();
|
|
9
|
+
let generationCounter = 0;
|
|
10
|
+
/** A fresh generation - see this module's doc comment. */
|
|
11
|
+
export function nextLocalIndexGeneration() {
|
|
12
|
+
generationCounter += 1;
|
|
13
|
+
return generationCounter;
|
|
14
|
+
}
|
|
15
|
+
/** Set once this tab (or another one) signed out - no index may be opened again in this page load. */
|
|
16
|
+
let signedOut = false;
|
|
17
|
+
export const SIGN_OUT_CHANNEL = "rapidmx-localsearch";
|
|
18
|
+
let channel;
|
|
19
|
+
function getWorker() {
|
|
20
|
+
if (!worker) {
|
|
21
|
+
worker = new Worker(new URL("./localIndexWorker.ts", import.meta.url), { type: "module" });
|
|
22
|
+
worker.addEventListener("message", (event) => {
|
|
23
|
+
const entry = pending.get(event.data.id);
|
|
24
|
+
if (!entry) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
pending.delete(event.data.id);
|
|
28
|
+
if (event.data.ok) {
|
|
29
|
+
entry.resolve(event.data.result);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
entry.reject(new Error(event.data.error));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
// Only a tab with a Worker has connections to close when another tab signs out.
|
|
36
|
+
listenForSignOut();
|
|
37
|
+
}
|
|
38
|
+
return worker;
|
|
39
|
+
}
|
|
40
|
+
function getChannel() {
|
|
41
|
+
if (!channel && typeof BroadcastChannel !== "undefined") {
|
|
42
|
+
channel = new BroadcastChannel(SIGN_OUT_CHANNEL);
|
|
43
|
+
}
|
|
44
|
+
return channel;
|
|
45
|
+
}
|
|
46
|
+
function listenForSignOut() {
|
|
47
|
+
getChannel()?.addEventListener("message", (event) => {
|
|
48
|
+
if (event.data?.type === "sign-out") {
|
|
49
|
+
void closeEverythingInThisTab();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Another tab signed out: stop every build here and close this tab's connections (which also removes their
|
|
54
|
+
* directories, now that nothing holds them open). Never rejects. */
|
|
55
|
+
async function closeEverythingInThisTab() {
|
|
56
|
+
signedOut = true;
|
|
57
|
+
initializedMailboxes.clear();
|
|
58
|
+
await call("destroyAll", { generation: nextLocalIndexGeneration() }).catch(() => undefined);
|
|
59
|
+
}
|
|
60
|
+
function call(method, params) {
|
|
61
|
+
const id = nextRequestId++;
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
pending.set(id, { resolve: resolve, reject });
|
|
64
|
+
try {
|
|
65
|
+
getWorker().postMessage({ id, method, params });
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
pending.delete(id);
|
|
69
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export const PENDING_DELETIONS_KEY = "rapidmx-localsearch-pending-deletions";
|
|
74
|
+
/** Stands for "every index on this device" in the pending-deletions list - recorded before a sign-out starts,
|
|
75
|
+
* so a navigation that kills it midway still gets finished on the next load. */
|
|
76
|
+
const ALL_INDEXES = "*";
|
|
77
|
+
function readPendingDeletions() {
|
|
78
|
+
try {
|
|
79
|
+
const parsed = JSON.parse(localStorage.getItem(PENDING_DELETIONS_KEY) ?? "[]");
|
|
80
|
+
return Array.isArray(parsed) ? parsed.filter((uid) => typeof uid === "string") : [];
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function writePendingDeletions(uids) {
|
|
87
|
+
try {
|
|
88
|
+
const list = [...new Set(uids)];
|
|
89
|
+
if (list.length === 0) {
|
|
90
|
+
localStorage.removeItem(PENDING_DELETIONS_KEY);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
localStorage.setItem(PENDING_DELETIONS_KEY, JSON.stringify(list));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Storage blocked - nothing to retry from, the same as before this existed.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function updatePendingDeletions(update) {
|
|
101
|
+
const current = new Set(readPendingDeletions());
|
|
102
|
+
update(current);
|
|
103
|
+
writePendingDeletions(current);
|
|
104
|
+
}
|
|
105
|
+
let pendingRetry;
|
|
106
|
+
/** Retries every deletion an earlier page load couldn't finish. Runs once per page load (memoized), before
|
|
107
|
+
* this tab's first `init` - so it never races an index this tab itself opens. Never rejects. */
|
|
108
|
+
export function retryPendingLocalIndexDeletions() {
|
|
109
|
+
pendingRetry ?? (pendingRetry = (async () => {
|
|
110
|
+
const uids = readPendingDeletions();
|
|
111
|
+
if (uids.length === 0) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const failed = [];
|
|
115
|
+
if (uids.includes(ALL_INDEXES)) {
|
|
116
|
+
const result = await removeLocalIndexDirectories().catch(() => ({ failed: [ALL_INDEXES] }));
|
|
117
|
+
failed.push(...result.failed);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
for (const uid of uids) {
|
|
121
|
+
await removeLocalIndexDirectory(uid).catch(() => failed.push(uid));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
writePendingDeletions(failed);
|
|
125
|
+
})());
|
|
126
|
+
return pendingRetry;
|
|
127
|
+
}
|
|
128
|
+
export async function initLocalIndex(params) {
|
|
129
|
+
await retryPendingLocalIndexDeletions();
|
|
130
|
+
if (signedOut) {
|
|
131
|
+
throw new Error("Signed out - the local search index is unavailable in this tab.");
|
|
132
|
+
}
|
|
133
|
+
await call("init", params);
|
|
134
|
+
initializedMailboxes.add(params.mailboxUid);
|
|
135
|
+
}
|
|
136
|
+
export function indexLocalEntities(mailboxUid, entities, generation) {
|
|
137
|
+
return call("indexEntities", { mailboxUid, entities, generation });
|
|
138
|
+
}
|
|
139
|
+
/** Drops one message from this session's local index (a delete). A no-op for a mailbox not indexed in
|
|
140
|
+
* this tab - a later build pass prunes it instead. Never rejects: best-effort housekeeping. */
|
|
141
|
+
export async function removeLocalEntity(mailboxUid, entityUid) {
|
|
142
|
+
if (!initializedMailboxes.has(mailboxUid)) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
await call("removeEntity", { mailboxUid, entityUid }).catch(() => undefined);
|
|
146
|
+
}
|
|
147
|
+
/** Re-points one indexed message at its new folder (archive, cancel-scheduled-send) so `folder:`-scoped
|
|
148
|
+
* local searches stay correct. Same no-op/never-rejects rules as `removeLocalEntity()`. */
|
|
149
|
+
export async function moveLocalEntity(mailboxUid, entityUid, folderUid) {
|
|
150
|
+
if (!initializedMailboxes.has(mailboxUid)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
await call("moveEntity", { mailboxUid, entityUid, folderUid }).catch(() => undefined);
|
|
154
|
+
}
|
|
155
|
+
export function getIndexedVersions(mailboxUid, entityUids) {
|
|
156
|
+
return call("indexedVersions", { mailboxUid, entityUids });
|
|
157
|
+
}
|
|
158
|
+
export function pruneLocalEntities(mailboxUid, keepEntityUids, since, options = {}) {
|
|
159
|
+
return call("pruneEntities", { mailboxUid, keepEntityUids, since, ...options });
|
|
160
|
+
}
|
|
161
|
+
export function searchLocal(mailboxUid, parsed, limit, offset = 0) {
|
|
162
|
+
return call("search", { mailboxUid, parsed, limit, offset });
|
|
163
|
+
}
|
|
164
|
+
export function getLocalCoverage(mailboxUid) {
|
|
165
|
+
return call("coverage", mailboxUid);
|
|
166
|
+
}
|
|
167
|
+
export function setLocalIndexWindow(mailboxUid, timeFloorMonths, byteBudgetBytes, generation) {
|
|
168
|
+
return call("setWindow", { mailboxUid, timeFloorMonths, byteBudgetBytes, generation });
|
|
169
|
+
}
|
|
170
|
+
export function setLocalIndexBuilding(mailboxUid, building, completion = { complete: false }) {
|
|
171
|
+
return call("setBuilding", { mailboxUid, building, ...completion });
|
|
172
|
+
}
|
|
173
|
+
/** Destroys one mailbox's local index (both the SQLite connection and its on-disk OPFS storage) - spec
|
|
174
|
+
* §11 "MUST be destroyed on the same events that destroy private keys." Never rejects (it's called from
|
|
175
|
+
* lifecycle hooks where a failure mustn't block key destruction) but resolves `false` - and logs why -
|
|
176
|
+
* when the index could not be removed, e.g. because another tab still has it open; the deletion is then
|
|
177
|
+
* retried on the next page load. */
|
|
178
|
+
export async function destroyLocalIndex(mailboxUid) {
|
|
179
|
+
updatePendingDeletions((current) => current.add(mailboxUid));
|
|
180
|
+
try {
|
|
181
|
+
await call("destroy", { mailboxUid, generation: nextLocalIndexGeneration() });
|
|
182
|
+
updatePendingDeletions((current) => current.delete(mailboxUid));
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
// `call()` only ever rejects with an Error.
|
|
187
|
+
console.warn(`Could not destroy the local search index for mailbox ${mailboxUid}: ${err.message}`);
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
initializedMailboxes.delete(mailboxUid);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** How long sign-out waits for local index destruction before navigating anyway. */
|
|
195
|
+
export const DESTROY_ALL_TIMEOUT_MS = 3000;
|
|
196
|
+
/**
|
|
197
|
+
* Destroys **every** local index on this device - not just the mailboxes this page load opened (sign-out
|
|
198
|
+
* from Calendar, say, never opened any), by enumerating OPFS for the index directory prefix - and tells every
|
|
199
|
+
* other tab to close its own connections. Closes this tab's own open connections through the Worker first
|
|
200
|
+
* (only if one was ever spawned - there's nothing to close otherwise, and booting one just to delete files
|
|
201
|
+
* would be waste), then removes the directories. Anything left behind is retried on the next load. Resolves
|
|
202
|
+
* `true` when everything was removed within `timeoutMs`; never rejects.
|
|
203
|
+
*/
|
|
204
|
+
export async function destroyAllLocalIndexes(timeoutMs = DESTROY_ALL_TIMEOUT_MS) {
|
|
205
|
+
signedOut = true;
|
|
206
|
+
writePendingDeletions([ALL_INDEXES]);
|
|
207
|
+
try {
|
|
208
|
+
getChannel()?.postMessage({ type: "sign-out" });
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// A closed/unsupported channel only loses the cross-tab notification.
|
|
212
|
+
}
|
|
213
|
+
const work = (async () => {
|
|
214
|
+
let ok = true;
|
|
215
|
+
if (worker) {
|
|
216
|
+
const { failed } = await call("destroyAll", { generation: nextLocalIndexGeneration() }).catch(() => ({
|
|
217
|
+
failed: ["(worker)"],
|
|
218
|
+
}));
|
|
219
|
+
ok = failed.length === 0;
|
|
220
|
+
}
|
|
221
|
+
initializedMailboxes.clear();
|
|
222
|
+
const { failed } = await removeLocalIndexDirectories();
|
|
223
|
+
writePendingDeletions(failed);
|
|
224
|
+
return ok && failed.length === 0;
|
|
225
|
+
})().catch(() => false);
|
|
226
|
+
let timer;
|
|
227
|
+
const timeout = new Promise((resolve) => {
|
|
228
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
229
|
+
});
|
|
230
|
+
const result = await Promise.race([work, timeout]);
|
|
231
|
+
clearTimeout(timer);
|
|
232
|
+
if (!result) {
|
|
233
|
+
console.warn("Could not destroy every local search index before signing out.");
|
|
234
|
+
}
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
/** Removes local indexes for mailboxes outside `accessibleMailboxUids` - e.g. left behind by a different
|
|
238
|
+
* user who signed in on this device without signing out. Never rejects. */
|
|
239
|
+
export async function pruneInaccessibleLocalIndexes(accessibleMailboxUids) {
|
|
240
|
+
await removeLocalIndexDirectories(new Set(accessibleMailboxUids)).catch(() => undefined);
|
|
241
|
+
}
|