internxt-crypto 1.0.2 → 1.1.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/README.md +1 -1
- package/dist/derive-key.d.mts +8 -1
- package/dist/derive-key.d.mts.map +1 -1
- package/dist/derive-key.d.ts +8 -1
- package/dist/derive-key.d.ts.map +1 -1
- package/dist/derive-key.js +2 -1
- package/dist/derive-key.mjs +2 -2
- package/dist/{deriveKeysFromKey-DPU_h7wr.mjs → deriveKeysFromKey-CseBjg0n.mjs} +12 -3
- package/dist/deriveKeysFromKey-CseBjg0n.mjs.map +1 -0
- package/dist/{deriveKeysFromKey-C4McqO6_.js → deriveKeysFromKey-GTRMuxDj.js} +16 -1
- package/dist/deriveKeysFromKey-GTRMuxDj.js.map +1 -0
- package/dist/email-crypto.d.mts +65 -2
- package/dist/email-crypto.d.mts.map +1 -1
- package/dist/email-crypto.d.ts +65 -2
- package/dist/email-crypto.d.ts.map +1 -1
- package/dist/email-crypto.js +14 -120
- package/dist/email-crypto.mjs +2 -115
- package/dist/{emailEncryptionKey-C4X1Bwy4.js → emailEncryptionKey-F_YuJu1H.js} +2 -2
- package/dist/{emailEncryptionKey-C4X1Bwy4.js.map → emailEncryptionKey-F_YuJu1H.js.map} +1 -1
- package/dist/{emailEncryptionKey-BtSPYd9M.mjs → emailEncryptionKey-kIsseFo6.mjs} +2 -2
- package/dist/{emailEncryptionKey-BtSPYd9M.mjs.map → emailEncryptionKey-kIsseFo6.mjs.map} +1 -1
- package/dist/{core-Bj26rNdA.js → emailKeys-DWvKANTZ.js} +148 -1
- package/dist/emailKeys-DWvKANTZ.js.map +1 -0
- package/dist/{core-pEGygy6S.mjs → emailKeys-nzn7U7HQ.mjs} +114 -3
- package/dist/emailKeys-nzn7U7HQ.mjs.map +1 -0
- package/dist/index.d.mts +3 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -36
- package/dist/index.mjs +4 -6
- package/dist/keystore-crypto.js +1 -1
- package/dist/keystore-crypto.mjs +1 -1
- package/dist/types.d.mts +1 -34
- package/dist/types.d.mts.map +1 -1
- package/dist/types.d.ts +1 -34
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -3
- package/dist/core-Bj26rNdA.js.map +0 -1
- package/dist/core-pEGygy6S.mjs.map +0 -1
- package/dist/deriveKeysFromKey-C4McqO6_.js.map +0 -1
- package/dist/deriveKeysFromKey-DPU_h7wr.mjs.map +0 -1
- package/dist/email-crypto.js.map +0 -1
- package/dist/email-crypto.mjs.map +0 -1
- package/dist/email-search.d.mts +0 -232
- package/dist/email-search.d.mts.map +0 -1
- package/dist/email-search.d.ts +0 -232
- package/dist/email-search.d.ts.map +0 -1
- package/dist/email-search.js +0 -27
- package/dist/email-search.mjs +0 -3
- package/dist/search-B9YM3XAA.mjs +0 -587
- package/dist/search-B9YM3XAA.mjs.map +0 -1
- package/dist/search-DUTm4-rN.js +0 -731
- package/dist/search-DUTm4-rN.js.map +0 -1
package/dist/search-B9YM3XAA.mjs
DELETED
|
@@ -1,587 +0,0 @@
|
|
|
1
|
-
import { CONTEXT_INDEX, DB_LABEL, DB_VERSION, MAX_CACHE_SIZE, MAX_EMAIL_PER_BATCH } from "./constants.mjs";
|
|
2
|
-
import { s as UTF8ToUint8 } from "./genID-D08K7JPr.mjs";
|
|
3
|
-
import { t as deriveSymmetricKeyFromContext } from "./deriveKeysFromKey-DPU_h7wr.mjs";
|
|
4
|
-
import { i as encryptEmailBodyWithKey, t as decryptEmailBody } from "./core-pEGygy6S.mjs";
|
|
5
|
-
import { deleteDB, openDB } from "idb";
|
|
6
|
-
import { Index } from "flexsearch";
|
|
7
|
-
|
|
8
|
-
//#region src/email-search/indexedDB.ts
|
|
9
|
-
/**
|
|
10
|
-
* Returns IndexedDB database name for the given user
|
|
11
|
-
*
|
|
12
|
-
* @param userID - The user ID
|
|
13
|
-
* @returns The database name
|
|
14
|
-
*/
|
|
15
|
-
const getDatabaseName = (userID) => {
|
|
16
|
-
return `ES:${userID}:DB`;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Opens IndexedDB database for the given user
|
|
20
|
-
*
|
|
21
|
-
* @param userID - The user ID
|
|
22
|
-
* @returns The database
|
|
23
|
-
*/
|
|
24
|
-
const openDatabase = async (userID) => {
|
|
25
|
-
try {
|
|
26
|
-
return openDB(getDatabaseName(userID), DB_VERSION, { upgrade(db) {
|
|
27
|
-
if (!db.objectStoreNames.contains(DB_LABEL)) db.createObjectStore(DB_LABEL, { keyPath: "id" }).createIndex("byTime", "params.createdAt");
|
|
28
|
-
} });
|
|
29
|
-
} catch (error) {
|
|
30
|
-
throw new Error(`Cannot open a database for the user ${userID}`, { cause: error });
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Closes the IndexedDB database
|
|
35
|
-
*
|
|
36
|
-
* @param esDB - The database
|
|
37
|
-
*/
|
|
38
|
-
const closeDatabase = (esDB) => {
|
|
39
|
-
return esDB.close();
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Deletes IndexedDB database for the given user
|
|
43
|
-
*
|
|
44
|
-
* @param userID - The user ID
|
|
45
|
-
* @returns The database
|
|
46
|
-
*/
|
|
47
|
-
const deleteDatabase = async (userID) => {
|
|
48
|
-
return deleteDB(getDatabaseName(userID));
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Derives database encryption key for the given user
|
|
52
|
-
*
|
|
53
|
-
* @param userID - The user ID
|
|
54
|
-
* @returns The symmetric key for protecting database
|
|
55
|
-
*/
|
|
56
|
-
const deriveIndexKey = async (baseKey) => {
|
|
57
|
-
return deriveSymmetricKeyFromContext(CONTEXT_INDEX, baseKey);
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Encrypts the given email and stores it in the IndexedDB database
|
|
61
|
-
*
|
|
62
|
-
* @param newEmailToStore - The email for storing
|
|
63
|
-
* @param indexKey - The symmetric key for protecting database
|
|
64
|
-
* @param esDB - The database
|
|
65
|
-
*/
|
|
66
|
-
const encryptAndStoreEmail = async (newEmailToStore, indexKey, esDB) => {
|
|
67
|
-
try {
|
|
68
|
-
const encryptedEmail = {
|
|
69
|
-
encEmailBody: await encryptEmailBodyWithKey(newEmailToStore.body, indexKey),
|
|
70
|
-
params: newEmailToStore.params,
|
|
71
|
-
id: newEmailToStore.id
|
|
72
|
-
};
|
|
73
|
-
await esDB.put(DB_LABEL, encryptedEmail);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
throw new Error("Cannot encrypt and add the given email to the database", { cause: error });
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Encrypts the given set of emails and stores it in the IndexedDB database
|
|
80
|
-
*
|
|
81
|
-
* @param newEmailsToStore - The set of emails for storing
|
|
82
|
-
* @param indexKey - The symmetric key key for protecting database
|
|
83
|
-
* @param esDB - The database
|
|
84
|
-
*/
|
|
85
|
-
const encryptAndStoreManyEmail = async (newEmailsToStore, indexKey, esDB) => {
|
|
86
|
-
try {
|
|
87
|
-
const encryptedEmails = await Promise.all(newEmailsToStore.map(async (email) => {
|
|
88
|
-
return {
|
|
89
|
-
encEmailBody: await encryptEmailBodyWithKey(email.body, indexKey),
|
|
90
|
-
params: email.params,
|
|
91
|
-
id: email.id
|
|
92
|
-
};
|
|
93
|
-
}));
|
|
94
|
-
const tr = esDB.transaction(DB_LABEL, "readwrite");
|
|
95
|
-
await Promise.all([...encryptedEmails.map((encEmail) => tr.store.put(encEmail)), tr.done]);
|
|
96
|
-
} catch (error) {
|
|
97
|
-
throw new Error("Cannot encrypt and add emails to the database", { cause: error });
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
/**
|
|
101
|
-
* Decrypts the given email
|
|
102
|
-
*
|
|
103
|
-
* @param indexKey - The symmetric key key for protecting database
|
|
104
|
-
* @param encryptedEmail - The encrypted email
|
|
105
|
-
* @returns The decrypted email
|
|
106
|
-
*/
|
|
107
|
-
const decryptEmail = async (indexKey, encryptedEmail) => {
|
|
108
|
-
try {
|
|
109
|
-
return {
|
|
110
|
-
body: await decryptEmailBody(encryptedEmail.encEmailBody, indexKey),
|
|
111
|
-
params: encryptedEmail.params,
|
|
112
|
-
id: encryptedEmail.id
|
|
113
|
-
};
|
|
114
|
-
} catch (error) {
|
|
115
|
-
throw new Error("Cannot decrypt the given email", { cause: error });
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Fetches the email from the database and decrypts it
|
|
120
|
-
*
|
|
121
|
-
* @param emailID - The email identifier
|
|
122
|
-
* @param indexKey - The symmetric key key for protecting database
|
|
123
|
-
* @param encryptedEmail - The encrypted email
|
|
124
|
-
* @returns The decrypted email
|
|
125
|
-
*/
|
|
126
|
-
const getAndDecryptEmail = async (emailID, indexKey, esDB) => {
|
|
127
|
-
try {
|
|
128
|
-
const encryptedEmail = await esDB.get(DB_LABEL, emailID);
|
|
129
|
-
if (!encryptedEmail) throw new Error(`DB cannot find email with id ${emailID}`);
|
|
130
|
-
return decryptEmail(indexKey, encryptedEmail);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
throw new Error(`Cannot fetch the email ${emailID} from the database`, { cause: error });
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
/**
|
|
136
|
-
* Fetches all email from the database and decrypts them
|
|
137
|
-
*
|
|
138
|
-
* @param indexKey - The symmetric key key for protecting database
|
|
139
|
-
* @param esDB - The database
|
|
140
|
-
* @returns The decrypted emails
|
|
141
|
-
*/
|
|
142
|
-
const getAndDecryptAllEmails = async (indexKey, esDB) => {
|
|
143
|
-
try {
|
|
144
|
-
const encryptedEmails = await esDB.getAll(DB_LABEL);
|
|
145
|
-
return (await Promise.all(encryptedEmails.map(async (encEmail) => {
|
|
146
|
-
return {
|
|
147
|
-
body: await decryptEmailBody(encEmail.encEmailBody, indexKey),
|
|
148
|
-
params: encEmail.params,
|
|
149
|
-
id: encEmail.id
|
|
150
|
-
};
|
|
151
|
-
}))).filter((email) => email !== null);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
throw new Error("Cannot fetch and decrypt all emails from the database", { cause: error });
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
/**
|
|
157
|
-
* Deletes the email from the database
|
|
158
|
-
*
|
|
159
|
-
* @param emailID - The email identifier
|
|
160
|
-
* @param esDB - The database
|
|
161
|
-
*/
|
|
162
|
-
const deleteEmail = async (emailID, esDB) => {
|
|
163
|
-
await esDB.delete(DB_LABEL, emailID);
|
|
164
|
-
};
|
|
165
|
-
/**
|
|
166
|
-
* Returns the number of stored email
|
|
167
|
-
*
|
|
168
|
-
* @param esDB - The database
|
|
169
|
-
* @returns The number of stored emails
|
|
170
|
-
*/
|
|
171
|
-
const getEmailCount = async (esDB) => {
|
|
172
|
-
return await esDB.count(DB_LABEL);
|
|
173
|
-
};
|
|
174
|
-
/**
|
|
175
|
-
* Removes the given number of oldests emails from the database
|
|
176
|
-
*
|
|
177
|
-
* @param emailsToDelete - The number of emails to delete
|
|
178
|
-
* @param esDB - The database
|
|
179
|
-
*/
|
|
180
|
-
const deleteOldestEmails = async (emailsToDelete, esDB) => {
|
|
181
|
-
try {
|
|
182
|
-
const tx = esDB.transaction(DB_LABEL, "readwrite");
|
|
183
|
-
let cursor = await tx.store.index("byTime").openCursor();
|
|
184
|
-
let deletedCount = 0;
|
|
185
|
-
while (cursor && deletedCount < emailsToDelete) {
|
|
186
|
-
await cursor.delete();
|
|
187
|
-
deletedCount++;
|
|
188
|
-
cursor = await cursor.continue();
|
|
189
|
-
}
|
|
190
|
-
await tx.done;
|
|
191
|
-
} catch (error) {
|
|
192
|
-
throw new Error(`Cannot delete ${emailsToDelete} oldests emails from the database`, { cause: error });
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
/**
|
|
196
|
-
* Enforces the maximum email number in the database
|
|
197
|
-
*
|
|
198
|
-
* @param esDB - The database
|
|
199
|
-
* @param max - The maximum allowed number of emails
|
|
200
|
-
*/
|
|
201
|
-
const enforceMaxEmailNumber = async (esDB, max) => {
|
|
202
|
-
try {
|
|
203
|
-
const currentCount = await getEmailCount(esDB);
|
|
204
|
-
if (currentCount <= max) return;
|
|
205
|
-
await deleteOldestEmails(currentCount - max, esDB);
|
|
206
|
-
} catch (error) {
|
|
207
|
-
throw new Error(`Cannot enforce the maximum of ${max} emails on the database`, { cause: error });
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
/**
|
|
211
|
-
* Fetches all emails from the database, decrypts them and sortes the results in the specified order
|
|
212
|
-
*
|
|
213
|
-
* @param esDB - The database
|
|
214
|
-
* @param indexKey - The symmetric key key for protecting database
|
|
215
|
-
* @param direction - The order of sorting. If 'next' - oldest first, if 'prev' - newest first
|
|
216
|
-
* @returns Decryped emails in the specified order
|
|
217
|
-
*/
|
|
218
|
-
const fetchEmails = async (esDB, indexKey, direction) => {
|
|
219
|
-
try {
|
|
220
|
-
const index = esDB.transaction(DB_LABEL, "readonly").store.index("byTime");
|
|
221
|
-
const encryptedEmails = [];
|
|
222
|
-
let cursor = await index.openCursor(null, direction);
|
|
223
|
-
while (cursor) {
|
|
224
|
-
encryptedEmails.push(cursor.value);
|
|
225
|
-
cursor = await cursor.continue();
|
|
226
|
-
}
|
|
227
|
-
return await Promise.all(encryptedEmails.map((encryptedEmail) => decryptEmail(indexKey, encryptedEmail)));
|
|
228
|
-
} catch (error) {
|
|
229
|
-
throw new Error("Cannot fetch emails from database", { cause: error });
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
/**
|
|
233
|
-
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
234
|
-
*
|
|
235
|
-
* @param esDB - The database
|
|
236
|
-
* @param indexKey - The symmetric Uint8Array key for protecting database
|
|
237
|
-
* @returns The number of stored emails
|
|
238
|
-
*/
|
|
239
|
-
const getAllEmailsSortedNewestFirst = async (esDB, indexKey) => {
|
|
240
|
-
return fetchEmails(esDB, indexKey, "prev");
|
|
241
|
-
};
|
|
242
|
-
/**
|
|
243
|
-
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)
|
|
244
|
-
*
|
|
245
|
-
* @param esDB - The database
|
|
246
|
-
* @param indexKey - The symmetric key for protecting database
|
|
247
|
-
* @returns The number of stored emails
|
|
248
|
-
*/
|
|
249
|
-
const getAllEmailsSortedOldestFirst = async (esDB, indexKey) => {
|
|
250
|
-
return fetchEmails(esDB, indexKey, "next");
|
|
251
|
-
};
|
|
252
|
-
/**
|
|
253
|
-
* Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
254
|
-
*
|
|
255
|
-
* @param esDB - The database
|
|
256
|
-
* @param indexKey - The symmetric key for protecting database
|
|
257
|
-
* @param batchSize - The size of the batch
|
|
258
|
-
* @param startCursor - The starting point (optional). If not given, starts from the beginning
|
|
259
|
-
* @returns The number of stored emails
|
|
260
|
-
*/
|
|
261
|
-
const getEmailBatch = async (esDB, indexKey, batchSize, startCursor) => {
|
|
262
|
-
try {
|
|
263
|
-
const index = esDB.transaction(DB_LABEL, "readonly").store.index("byTime");
|
|
264
|
-
const encryptedEmails = [];
|
|
265
|
-
let cursor;
|
|
266
|
-
if (startCursor) {
|
|
267
|
-
const range = IDBKeyRange.upperBound(startCursor, true);
|
|
268
|
-
cursor = await index.openCursor(range, "prev");
|
|
269
|
-
} else cursor = await index.openCursor(null, "prev");
|
|
270
|
-
let count = 0;
|
|
271
|
-
let nextCursor;
|
|
272
|
-
while (cursor && count < batchSize) {
|
|
273
|
-
encryptedEmails.push(cursor.value);
|
|
274
|
-
nextCursor = cursor.key;
|
|
275
|
-
count++;
|
|
276
|
-
cursor = await cursor.continue();
|
|
277
|
-
}
|
|
278
|
-
return {
|
|
279
|
-
emails: await Promise.all(encryptedEmails.map((encryptedEmail) => decryptEmail(indexKey, encryptedEmail))),
|
|
280
|
-
nextCursor: count === batchSize ? nextCursor : void 0
|
|
281
|
-
};
|
|
282
|
-
} catch (error) {
|
|
283
|
-
throw new Error(`Cannot fetch email batch of ${batchSize} from the database`, { cause: error });
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
//#endregion
|
|
288
|
-
//#region src/email-search/utils.ts
|
|
289
|
-
/**
|
|
290
|
-
* Converts an Email type into a Uint8Array array.
|
|
291
|
-
*
|
|
292
|
-
* @param email - The email.
|
|
293
|
-
* @returns The Uint8Array array representation of the Email type.
|
|
294
|
-
*/
|
|
295
|
-
function emailToBinary(email) {
|
|
296
|
-
try {
|
|
297
|
-
return UTF8ToUint8(JSON.stringify(email));
|
|
298
|
-
} catch (error) {
|
|
299
|
-
throw new Error("Failed to convert EmailBody to Uint8Array", { cause: error });
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
//#endregion
|
|
304
|
-
//#region src/email-search/mailCache.ts
|
|
305
|
-
/**
|
|
306
|
-
* Estimates the email size in the memory
|
|
307
|
-
*
|
|
308
|
-
* @param email - The email
|
|
309
|
-
* @returns The estimation of the email size
|
|
310
|
-
*/
|
|
311
|
-
function sizeOfEmail(email) {
|
|
312
|
-
return emailToBinary(email).byteLength;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Creates an empty cache variable
|
|
316
|
-
*
|
|
317
|
-
* @returns The empty cache
|
|
318
|
-
*/
|
|
319
|
-
function createEmptyCache() {
|
|
320
|
-
return {
|
|
321
|
-
esCache: /* @__PURE__ */ new Map(),
|
|
322
|
-
cacheSize: 0,
|
|
323
|
-
isCacheLimited: false,
|
|
324
|
-
isCacheReady: true
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Fetches all emails from the database in batches and cahces them
|
|
329
|
-
*
|
|
330
|
-
* @param indexKey - The symmetric key for protecting database
|
|
331
|
-
* @param esCache - The cache to add emails too
|
|
332
|
-
* @param esDB - The database
|
|
333
|
-
*/
|
|
334
|
-
const createCacheFromDB = async (indexKey, esDB) => {
|
|
335
|
-
const esCache = createEmptyCache();
|
|
336
|
-
esCache.isCacheReady = false;
|
|
337
|
-
try {
|
|
338
|
-
const count = await getEmailCount(esDB);
|
|
339
|
-
if (!count) {
|
|
340
|
-
esCache.isCacheReady = true;
|
|
341
|
-
return esCache;
|
|
342
|
-
}
|
|
343
|
-
if (count <= MAX_EMAIL_PER_BATCH) addEmailsToCache(await getAllEmailsSortedNewestFirst(esDB, indexKey), esCache);
|
|
344
|
-
else {
|
|
345
|
-
let nextCursor = void 0;
|
|
346
|
-
let cacheFull = false;
|
|
347
|
-
while (!cacheFull) {
|
|
348
|
-
const { emails, nextCursor: newCursor } = await getEmailBatch(esDB, indexKey, MAX_EMAIL_PER_BATCH, nextCursor);
|
|
349
|
-
if (!newCursor || !emails.length) break;
|
|
350
|
-
nextCursor = newCursor;
|
|
351
|
-
if (!addEmailsToCache(emails, esCache)) cacheFull = true;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return esCache;
|
|
355
|
-
} catch (error) {
|
|
356
|
-
throw new Error(`Email caching failed: ${error}`, { cause: error });
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
/**
|
|
360
|
-
* Gets an email from the cache
|
|
361
|
-
*
|
|
362
|
-
* @param emailID - The email identifier
|
|
363
|
-
* @param esCache - The email cache
|
|
364
|
-
* @returns The found email or throws an error
|
|
365
|
-
*/
|
|
366
|
-
const getEmailFromCache = async (emailID, esCache) => {
|
|
367
|
-
const email = esCache.esCache.get(emailID);
|
|
368
|
-
if (!email) throw new Error(`Email not found in cache for ID: ${emailID}`);
|
|
369
|
-
return email;
|
|
370
|
-
};
|
|
371
|
-
/**
|
|
372
|
-
* Removes the email from cache
|
|
373
|
-
*
|
|
374
|
-
* @param emailID - The email identifier
|
|
375
|
-
* @param esCache - The email cache
|
|
376
|
-
*/
|
|
377
|
-
const deleteEmailFromCache = async (emailID, esCache) => {
|
|
378
|
-
try {
|
|
379
|
-
const size = sizeOfEmail(await getEmailFromCache(emailID, esCache));
|
|
380
|
-
if (esCache.esCache.delete(emailID)) esCache.cacheSize -= size;
|
|
381
|
-
} catch (error) {
|
|
382
|
-
throw new Error(`Failed to delete email with ID ${emailID}`, { cause: error });
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
/**
|
|
386
|
-
* Adds emails to the cache
|
|
387
|
-
*
|
|
388
|
-
* @param emails - The emails to add
|
|
389
|
-
* @param esCache - The email cache
|
|
390
|
-
* @returns TRUE if all emails were added sucessfully, or FALSE and error reason.
|
|
391
|
-
*/
|
|
392
|
-
function addEmailsToCache(emails, esCache) {
|
|
393
|
-
try {
|
|
394
|
-
for (const email of emails) {
|
|
395
|
-
const result = addEmailToCache(email, esCache);
|
|
396
|
-
if (!result.success) return result;
|
|
397
|
-
}
|
|
398
|
-
return { success: true };
|
|
399
|
-
} catch (error) {
|
|
400
|
-
throw new Error("Failed to add emails to the cache", { cause: error });
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* Adds email to the cache
|
|
405
|
-
*
|
|
406
|
-
* @param email - The email to add
|
|
407
|
-
* @param esCache - The email cache
|
|
408
|
-
* @returns TRUE if the email was added sucessfully, or FALSE and error reason.
|
|
409
|
-
*/
|
|
410
|
-
const addEmailToCache = (email, esCache) => {
|
|
411
|
-
try {
|
|
412
|
-
if (esCache.esCache.has(email.id)) return {
|
|
413
|
-
success: false,
|
|
414
|
-
reason: "email already exists in cache"
|
|
415
|
-
};
|
|
416
|
-
const emailSize = sizeOfEmail(email);
|
|
417
|
-
if (esCache.cacheSize + emailSize > MAX_CACHE_SIZE) {
|
|
418
|
-
esCache.isCacheLimited = true;
|
|
419
|
-
return {
|
|
420
|
-
success: false,
|
|
421
|
-
reason: "hit cache limit"
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
esCache.esCache.set(email.id, email);
|
|
425
|
-
esCache.cacheSize += emailSize;
|
|
426
|
-
return { success: true };
|
|
427
|
-
} catch (error) {
|
|
428
|
-
throw new Error("Failed to add email to the cache", { cause: error });
|
|
429
|
-
}
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
//#endregion
|
|
433
|
-
//#region src/email-search/search.ts
|
|
434
|
-
const SUBJECT_OPTIONS = {
|
|
435
|
-
preset: "match",
|
|
436
|
-
tokenize: "forward",
|
|
437
|
-
resolution: 9,
|
|
438
|
-
minlength: 2,
|
|
439
|
-
optimize: true,
|
|
440
|
-
fastupdate: true,
|
|
441
|
-
cache: true
|
|
442
|
-
};
|
|
443
|
-
const BODY_OPTIONS = {
|
|
444
|
-
...SUBJECT_OPTIONS,
|
|
445
|
-
minlength: 3,
|
|
446
|
-
resolution: 6
|
|
447
|
-
};
|
|
448
|
-
const FROM_OPTIONS = {
|
|
449
|
-
...SUBJECT_OPTIONS,
|
|
450
|
-
tokenize: "strict",
|
|
451
|
-
minlength: 1
|
|
452
|
-
};
|
|
453
|
-
const TO_OPTIONS = {
|
|
454
|
-
...SUBJECT_OPTIONS,
|
|
455
|
-
tokenize: "strict",
|
|
456
|
-
minlength: 1
|
|
457
|
-
};
|
|
458
|
-
/**
|
|
459
|
-
* Creates new email search index
|
|
460
|
-
*
|
|
461
|
-
* @returns The email search index
|
|
462
|
-
*/
|
|
463
|
-
const createSearchIndex = () => ({
|
|
464
|
-
subjectIndex: new Index(SUBJECT_OPTIONS),
|
|
465
|
-
bodyIndex: new Index(BODY_OPTIONS),
|
|
466
|
-
fromIndex: new Index(FROM_OPTIONS),
|
|
467
|
-
toIndex: new Index(TO_OPTIONS),
|
|
468
|
-
isReady: false
|
|
469
|
-
});
|
|
470
|
-
/**
|
|
471
|
-
* Adds the given email to the email search index
|
|
472
|
-
*
|
|
473
|
-
* @param email - The email to add
|
|
474
|
-
* @param searchIndex - The email search index
|
|
475
|
-
*/
|
|
476
|
-
const addEmailToSearchIndex = (email, searchIndex) => {
|
|
477
|
-
try {
|
|
478
|
-
const emailId = email.id;
|
|
479
|
-
searchIndex.subjectIndex.add(emailId, email.body.subject);
|
|
480
|
-
searchIndex.bodyIndex.add(emailId, email.body.text);
|
|
481
|
-
const senderText = `${email.params.sender.name || ""} ${email.params.sender.email || ""}`.trim();
|
|
482
|
-
searchIndex.fromIndex.add(emailId, senderText);
|
|
483
|
-
const recipientsText = (email.params.recipients?.length ? email.params.recipients : [email.params.recipient]).map((recipient) => `${recipient.name || ""} ${recipient.email || ""}`.trim()).join(" ");
|
|
484
|
-
searchIndex.toIndex.add(emailId, recipientsText);
|
|
485
|
-
} catch (error) {
|
|
486
|
-
throw new Error("Failed to add email to the search index", { cause: error });
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
/**
|
|
490
|
-
* Removes the email from the email search index
|
|
491
|
-
*
|
|
492
|
-
* @param emailID - The email identifier
|
|
493
|
-
* @param searchIndex - The email search index
|
|
494
|
-
*/
|
|
495
|
-
const removeEmailFromSearchIndex = (emailID, searchIndex) => {
|
|
496
|
-
try {
|
|
497
|
-
searchIndex.subjectIndex.remove(emailID);
|
|
498
|
-
searchIndex.bodyIndex.remove(emailID);
|
|
499
|
-
searchIndex.fromIndex.remove(emailID);
|
|
500
|
-
searchIndex.toIndex.remove(emailID);
|
|
501
|
-
} catch (error) {
|
|
502
|
-
throw new Error(`Failed to remove email with ID ${emailID} from the search index`, { cause: error });
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
|
-
/**
|
|
506
|
-
* Buils the email search index from the email cache
|
|
507
|
-
*
|
|
508
|
-
* @param esCache - The email cache
|
|
509
|
-
* @returns The email search index
|
|
510
|
-
*/
|
|
511
|
-
const buildSearchIndexFromCache = async (esCache) => {
|
|
512
|
-
try {
|
|
513
|
-
const searchIndex = createSearchIndex();
|
|
514
|
-
searchIndex.isReady = false;
|
|
515
|
-
for (const email of esCache.esCache.values()) addEmailToSearchIndex(email, searchIndex);
|
|
516
|
-
searchIndex.isReady = true;
|
|
517
|
-
return searchIndex;
|
|
518
|
-
} catch (error) {
|
|
519
|
-
throw new Error("Failed build an email search index from the cache", { cause: error });
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
/**
|
|
523
|
-
* Searches in teh
|
|
524
|
-
*
|
|
525
|
-
* @param query - The string to search for
|
|
526
|
-
* @param esCache - The email cache
|
|
527
|
-
* @param searchIndex - The email search index
|
|
528
|
-
* @param opetions - The optional search limitations
|
|
529
|
-
* @returns The result of the email search (emails ans their corresponding result weights)
|
|
530
|
-
*/
|
|
531
|
-
const searchEmails = async (query, esCache, searchIndex, options = {}) => {
|
|
532
|
-
try {
|
|
533
|
-
if (!searchIndex.isReady || !query.trim()) return [];
|
|
534
|
-
const { fields = [
|
|
535
|
-
"subject",
|
|
536
|
-
"body",
|
|
537
|
-
"from",
|
|
538
|
-
"to"
|
|
539
|
-
], limit = 50, boost = {
|
|
540
|
-
subject: 3,
|
|
541
|
-
body: 1,
|
|
542
|
-
from: 2,
|
|
543
|
-
to: 2
|
|
544
|
-
} } = options;
|
|
545
|
-
const results = /* @__PURE__ */ new Map();
|
|
546
|
-
const searchPromises = fields.map(async (field) => {
|
|
547
|
-
let fieldResults;
|
|
548
|
-
switch (field) {
|
|
549
|
-
case "subject":
|
|
550
|
-
fieldResults = await searchIndex.subjectIndex.searchAsync(query);
|
|
551
|
-
break;
|
|
552
|
-
case "body":
|
|
553
|
-
fieldResults = await searchIndex.bodyIndex.searchAsync(query);
|
|
554
|
-
break;
|
|
555
|
-
case "from":
|
|
556
|
-
fieldResults = await searchIndex.fromIndex.searchAsync(query);
|
|
557
|
-
break;
|
|
558
|
-
case "to":
|
|
559
|
-
fieldResults = await searchIndex.toIndex.searchAsync(query);
|
|
560
|
-
break;
|
|
561
|
-
default: return;
|
|
562
|
-
}
|
|
563
|
-
const fieldBoost = boost[field] || 1;
|
|
564
|
-
fieldResults.forEach((emailId) => {
|
|
565
|
-
const currentScore = results.get(String(emailId)) || 0;
|
|
566
|
-
results.set(String(emailId), currentScore + fieldBoost);
|
|
567
|
-
});
|
|
568
|
-
});
|
|
569
|
-
await Promise.all(searchPromises);
|
|
570
|
-
const emailResults = [];
|
|
571
|
-
for (const [emailId, score] of results) {
|
|
572
|
-
const email = esCache.esCache.get(emailId);
|
|
573
|
-
if (email) emailResults.push({
|
|
574
|
-
email,
|
|
575
|
-
score
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
emailResults.sort((a, b) => (b.score || 0) - (a.score || 0));
|
|
579
|
-
return emailResults.slice(0, limit);
|
|
580
|
-
} catch (error) {
|
|
581
|
-
throw new Error("Email search failed", { cause: error });
|
|
582
|
-
}
|
|
583
|
-
};
|
|
584
|
-
|
|
585
|
-
//#endregion
|
|
586
|
-
export { getEmailCount as C, getEmailBatch as S, enforceMaxEmailNumber as _, addEmailToCache as a, getAndDecryptAllEmails as b, deleteEmailFromCache as c, deleteDatabase as d, deleteEmail as f, encryptAndStoreManyEmail as g, encryptAndStoreEmail as h, searchEmails as i, getEmailFromCache as l, deriveIndexKey as m, buildSearchIndexFromCache as n, addEmailsToCache as o, deleteOldestEmails as p, removeEmailFromSearchIndex as r, createCacheFromDB as s, addEmailToSearchIndex as t, closeDatabase as u, getAllEmailsSortedNewestFirst as v, openDatabase as w, getAndDecryptEmail as x, getAllEmailsSortedOldestFirst as y };
|
|
587
|
-
//# sourceMappingURL=search-B9YM3XAA.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search-B9YM3XAA.mjs","names":[],"sources":["../src/email-search/indexedDB.ts","../src/email-search/utils.ts","../src/email-search/mailCache.ts","../src/email-search/search.ts"],"sourcesContent":["import { DBSchema, openDB, deleteDB, IDBPDatabase } from 'idb';\nimport { StoredEmail, Email } from '../types';\nimport { decryptEmailBody, encryptEmailBodyWithKey } from '../email-crypto/core';\nimport { deriveSymmetricKeyFromContext } from '../derive-key';\nimport { CONTEXT_INDEX, DB_LABEL, DB_VERSION } from '../constants';\n\nexport type MailDB = IDBPDatabase<EncryptedSearchDB>;\n\nexport interface EncryptedSearchDB extends DBSchema {\n email: {\n key: string;\n value: StoredEmail;\n indexes: { byTime: number[] };\n };\n}\n\n/**\n * Returns IndexedDB database name for the given user\n *\n * @param userID - The user ID\n * @returns The database name\n */\nconst getDatabaseName = (userID: string): string => {\n return `ES:${userID}:DB`;\n};\n\n/**\n * Opens IndexedDB database for the given user\n *\n * @param userID - The user ID\n * @returns The database\n */\nexport const openDatabase = async (userID: string): Promise<MailDB> => {\n try {\n const dbName = getDatabaseName(userID);\n return openDB<EncryptedSearchDB>(dbName, DB_VERSION, {\n upgrade(db) {\n if (!db.objectStoreNames.contains(DB_LABEL)) {\n const store = db.createObjectStore(DB_LABEL, { keyPath: 'id' });\n store.createIndex('byTime', 'params.createdAt');\n }\n },\n });\n } catch (error) {\n throw new Error(`Cannot open a database for the user ${userID}`, { cause: error });\n }\n};\n\n/**\n * Closes the IndexedDB database\n *\n * @param esDB - The database\n */\nexport const closeDatabase = (esDB: MailDB): void => {\n return esDB.close();\n};\n\n/**\n * Deletes IndexedDB database for the given user\n *\n * @param userID - The user ID\n * @returns The database\n */\nexport const deleteDatabase = async (userID: string): Promise<void> => {\n const dbName = getDatabaseName(userID);\n return deleteDB(dbName);\n};\n\n/**\n * Derives database encryption key for the given user\n *\n * @param userID - The user ID\n * @returns The symmetric key for protecting database\n */\nexport const deriveIndexKey = async (baseKey: Uint8Array): Promise<Uint8Array> => {\n return deriveSymmetricKeyFromContext(CONTEXT_INDEX, baseKey);\n};\n\n/**\n * Encrypts the given email and stores it in the IndexedDB database\n *\n * @param newEmailToStore - The email for storing\n * @param indexKey - The symmetric key for protecting database\n * @param esDB - The database\n */\nexport const encryptAndStoreEmail = async (\n newEmailToStore: Email,\n indexKey: Uint8Array,\n esDB: MailDB,\n): Promise<void> => {\n try {\n const enc = await encryptEmailBodyWithKey(newEmailToStore.body, indexKey);\n const encryptedEmail: StoredEmail = { encEmailBody: enc, params: newEmailToStore.params, id: newEmailToStore.id };\n await esDB.put(DB_LABEL, encryptedEmail);\n } catch (error) {\n throw new Error('Cannot encrypt and add the given email to the database', { cause: error });\n }\n};\n\n/**\n * Encrypts the given set of emails and stores it in the IndexedDB database\n *\n * @param newEmailsToStore - The set of emails for storing\n * @param indexKey - The symmetric key key for protecting database\n * @param esDB - The database\n */\nexport const encryptAndStoreManyEmail = async (\n newEmailsToStore: Email[],\n indexKey: Uint8Array,\n esDB: MailDB,\n): Promise<void> => {\n try {\n const encryptedEmails = await Promise.all(\n newEmailsToStore.map(async (email: Email) => {\n const encEmailBody = await encryptEmailBodyWithKey(email.body, indexKey);\n\n return { encEmailBody, params: email.params, id: email.id };\n }),\n );\n\n const tr = esDB.transaction(DB_LABEL, 'readwrite');\n await Promise.all([...encryptedEmails.map((encEmail) => tr.store.put(encEmail)), tr.done]);\n } catch (error) {\n throw new Error('Cannot encrypt and add emails to the database', { cause: error });\n }\n};\n\n/**\n * Decrypts the given email\n *\n * @param indexKey - The symmetric key key for protecting database\n * @param encryptedEmail - The encrypted email\n * @returns The decrypted email\n */\nconst decryptEmail = async (indexKey: Uint8Array, encryptedEmail: StoredEmail): Promise<Email> => {\n try {\n const email = await decryptEmailBody(encryptedEmail.encEmailBody, indexKey);\n return { body: email, params: encryptedEmail.params, id: encryptedEmail.id };\n } catch (error) {\n throw new Error('Cannot decrypt the given email', { cause: error });\n }\n};\n\n/**\n * Fetches the email from the database and decrypts it\n *\n * @param emailID - The email identifier\n * @param indexKey - The symmetric key key for protecting database\n * @param encryptedEmail - The encrypted email\n * @returns The decrypted email\n */\nexport const getAndDecryptEmail = async (emailID: string, indexKey: Uint8Array, esDB: MailDB): Promise<Email> => {\n try {\n const encryptedEmail = await esDB.get(DB_LABEL, emailID);\n if (!encryptedEmail) {\n throw new Error(`DB cannot find email with id ${emailID}`);\n }\n return decryptEmail(indexKey, encryptedEmail);\n } catch (error) {\n throw new Error(`Cannot fetch the email ${emailID} from the database`, { cause: error });\n }\n};\n\n/**\n * Fetches all email from the database and decrypts them\n *\n * @param indexKey - The symmetric key key for protecting database\n * @param esDB - The database\n * @returns The decrypted emails\n */\nexport const getAndDecryptAllEmails = async (indexKey: Uint8Array, esDB: MailDB): Promise<Email[]> => {\n try {\n const encryptedEmails = await esDB.getAll(DB_LABEL);\n\n const decryptedEmails = await Promise.all(\n encryptedEmails.map(async (encEmail) => {\n const body = await decryptEmailBody(encEmail.encEmailBody, indexKey);\n return { body, params: encEmail.params, id: encEmail.id };\n }),\n );\n\n return decryptedEmails.filter((email): email is Email => email !== null);\n } catch (error) {\n throw new Error('Cannot fetch and decrypt all emails from the database', { cause: error });\n }\n};\n\n/**\n * Deletes the email from the database\n *\n * @param emailID - The email identifier\n * @param esDB - The database\n */\nexport const deleteEmail = async (emailID: string, esDB: MailDB): Promise<void> => {\n await esDB.delete(DB_LABEL, emailID);\n};\n\n/**\n * Returns the number of stored email\n *\n * @param esDB - The database\n * @returns The number of stored emails\n */\nexport const getEmailCount = async (esDB: MailDB): Promise<number> => {\n return await esDB.count(DB_LABEL);\n};\n\n/**\n * Removes the given number of oldests emails from the database\n *\n * @param emailsToDelete - The number of emails to delete\n * @param esDB - The database\n */\nexport const deleteOldestEmails = async (emailsToDelete: number, esDB: MailDB): Promise<void> => {\n try {\n const tx = esDB.transaction(DB_LABEL, 'readwrite');\n const index = tx.store.index('byTime');\n\n let cursor = await index.openCursor();\n let deletedCount = 0;\n\n while (cursor && deletedCount < emailsToDelete) {\n await cursor.delete();\n deletedCount++;\n cursor = await cursor.continue();\n }\n\n await tx.done;\n } catch (error) {\n throw new Error(`Cannot delete ${emailsToDelete} oldests emails from the database`, { cause: error });\n }\n};\n\n/**\n * Enforces the maximum email number in the database\n *\n * @param esDB - The database\n * @param max - The maximum allowed number of emails\n */\nexport const enforceMaxEmailNumber = async (esDB: MailDB, max: number): Promise<void> => {\n try {\n const currentCount = await getEmailCount(esDB);\n if (currentCount <= max) {\n return;\n }\n await deleteOldestEmails(currentCount - max, esDB);\n } catch (error) {\n throw new Error(`Cannot enforce the maximum of ${max} emails on the database`, { cause: error });\n }\n};\n\n/**\n * Fetches all emails from the database, decrypts them and sortes the results in the specified order\n *\n * @param esDB - The database\n * @param indexKey - The symmetric key key for protecting database\n * @param direction - The order of sorting. If 'next' - oldest first, if 'prev' - newest first\n * @returns Decryped emails in the specified order\n */\nconst fetchEmails = async (esDB: MailDB, indexKey: Uint8Array, direction: 'next' | 'prev'): Promise<Email[]> => {\n try {\n const tx = esDB.transaction(DB_LABEL, 'readonly');\n const index = tx.store.index('byTime');\n\n const encryptedEmails: StoredEmail[] = [];\n let cursor = await index.openCursor(null, direction);\n\n while (cursor) {\n encryptedEmails.push(cursor.value);\n cursor = await cursor.continue();\n }\n\n const emails = await Promise.all(encryptedEmails.map((encryptedEmail) => decryptEmail(indexKey, encryptedEmail)));\n\n return emails;\n } catch (error) {\n throw new Error('Cannot fetch emails from database', { cause: error });\n }\n};\n\n/**\n * Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)\n *\n * @param esDB - The database\n * @param indexKey - The symmetric Uint8Array key for protecting database\n * @returns The number of stored emails\n */\nexport const getAllEmailsSortedNewestFirst = async (esDB: MailDB, indexKey: Uint8Array): Promise<Email[]> => {\n return fetchEmails(esDB, indexKey, 'prev');\n};\n\n/**\n * Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)\n *\n * @param esDB - The database\n * @param indexKey - The symmetric key for protecting database\n * @returns The number of stored emails\n */\nexport const getAllEmailsSortedOldestFirst = async (esDB: MailDB, indexKey: Uint8Array): Promise<Email[]> => {\n return fetchEmails(esDB, indexKey, 'next');\n};\n\n/**\n * Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)\n *\n * @param esDB - The database\n * @param indexKey - The symmetric key for protecting database\n * @param batchSize - The size of the batch\n * @param startCursor - The starting point (optional). If not given, starts from the beginning\n * @returns The number of stored emails\n */\nexport const getEmailBatch = async (\n esDB: MailDB,\n indexKey: Uint8Array,\n batchSize: number,\n startCursor?: IDBValidKey,\n): Promise<{ emails: Email[]; nextCursor?: IDBValidKey }> => {\n try {\n const tx = esDB.transaction(DB_LABEL, 'readonly');\n const index = tx.store.index('byTime');\n\n const encryptedEmails: StoredEmail[] = [];\n let cursor;\n\n if (startCursor) {\n const range = IDBKeyRange.upperBound(startCursor, true);\n cursor = await index.openCursor(range, 'prev');\n } else {\n cursor = await index.openCursor(null, 'prev');\n }\n\n let count = 0;\n let nextCursor: IDBValidKey | undefined;\n\n while (cursor && count < batchSize) {\n encryptedEmails.push(cursor.value);\n nextCursor = cursor.key;\n count++;\n cursor = await cursor.continue();\n }\n\n const emails = await Promise.all(encryptedEmails.map((encryptedEmail) => decryptEmail(indexKey, encryptedEmail)));\n\n return {\n emails,\n nextCursor: count === batchSize ? nextCursor : undefined,\n };\n } catch (error) {\n throw new Error(`Cannot fetch email batch of ${batchSize} from the database`, { cause: error });\n }\n};\n","import { UTF8ToUint8 } from '../utils';\nimport { Email } from '../types';\n\n/**\n * Converts an Email type into a Uint8Array array.\n *\n * @param email - The email.\n * @returns The Uint8Array array representation of the Email type.\n */\nexport function emailToBinary(email: Email): Uint8Array {\n try {\n const json = JSON.stringify(email);\n return UTF8ToUint8(json);\n } catch (error) {\n throw new Error('Failed to convert EmailBody to Uint8Array', { cause: error });\n }\n}\n","import { MAX_CACHE_SIZE, MAX_EMAIL_PER_BATCH } from '../constants';\nimport { getEmailBatch, getAllEmailsSortedNewestFirst, getEmailCount, MailDB } from './indexedDB';\nimport { Email, MailCache } from '../types';\nimport { emailToBinary } from './utils';\n\n/**\n * Estimates the email size in the memory\n *\n * @param email - The email\n * @returns The estimation of the email size\n */\nfunction sizeOfEmail(email: Email): number {\n return emailToBinary(email).byteLength;\n}\n\n/**\n * Creates an empty cache variable\n *\n * @returns The empty cache\n */\nfunction createEmptyCache(): MailCache<Email> {\n return {\n esCache: new Map(),\n cacheSize: 0,\n isCacheLimited: false,\n isCacheReady: true,\n };\n}\n\n/**\n * Fetches all emails from the database in batches and cahces them\n *\n * @param indexKey - The symmetric key for protecting database\n * @param esCache - The cache to add emails too\n * @param esDB - The database\n */\nexport const createCacheFromDB = async (indexKey: Uint8Array, esDB: MailDB): Promise<MailCache<Email>> => {\n const esCache = createEmptyCache();\n esCache.isCacheReady = false;\n try {\n const count = await getEmailCount(esDB);\n if (!count) {\n esCache.isCacheReady = true;\n return esCache;\n }\n\n if (count <= MAX_EMAIL_PER_BATCH) {\n const emails = await getAllEmailsSortedNewestFirst(esDB, indexKey);\n addEmailsToCache(emails, esCache);\n } else {\n let nextCursor: IDBValidKey | undefined = undefined;\n\n let cacheFull = false;\n while (!cacheFull) {\n const { emails, nextCursor: newCursor }: { emails: Email[]; nextCursor?: IDBValidKey } = await getEmailBatch(\n esDB,\n indexKey,\n MAX_EMAIL_PER_BATCH,\n nextCursor,\n );\n if (!newCursor || !emails.length) break;\n nextCursor = newCursor;\n\n const success = addEmailsToCache(emails, esCache);\n if (!success) cacheFull = true;\n }\n }\n return esCache;\n } catch (error) {\n throw new Error(`Email caching failed: ${error}`, { cause: error });\n }\n};\n\n/**\n * Gets an email from the cache\n *\n * @param emailID - The email identifier\n * @param esCache - The email cache\n * @returns The found email or throws an error\n */\nexport const getEmailFromCache = async (emailID: string, esCache: MailCache<Email>): Promise<Email> => {\n const email = esCache.esCache.get(emailID);\n if (!email) {\n throw new Error(`Email not found in cache for ID: ${emailID}`);\n }\n return email;\n};\n\n/**\n * Removes the email from cache\n *\n * @param emailID - The email identifier\n * @param esCache - The email cache\n */\nexport const deleteEmailFromCache = async (emailID: string, esCache: MailCache<Email>): Promise<void> => {\n try {\n const email = await getEmailFromCache(emailID, esCache);\n const size = sizeOfEmail(email);\n const removed = esCache.esCache.delete(emailID);\n if (removed) esCache.cacheSize -= size;\n } catch (error) {\n throw new Error(`Failed to delete email with ID ${emailID}`, { cause: error });\n }\n};\n\n/**\n * Adds emails to the cache\n *\n * @param emails - The emails to add\n * @param esCache - The email cache\n * @returns TRUE if all emails were added sucessfully, or FALSE and error reason.\n */\nexport function addEmailsToCache(emails: Email[], esCache: MailCache<Email>): { success: boolean; reason?: string } {\n try {\n for (const email of emails) {\n const result = addEmailToCache(email, esCache);\n if (!result.success) return result;\n }\n return { success: true };\n } catch (error) {\n throw new Error('Failed to add emails to the cache', { cause: error });\n }\n}\n\n/**\n * Adds email to the cache\n *\n * @param email - The email to add\n * @param esCache - The email cache\n * @returns TRUE if the email was added sucessfully, or FALSE and error reason.\n */\nexport const addEmailToCache = (email: Email, esCache: MailCache<Email>): { success: boolean; reason?: string } => {\n try {\n if (esCache.esCache.has(email.id)) {\n return { success: false, reason: 'email already exists in cache' };\n }\n\n const emailSize = sizeOfEmail(email);\n\n if (esCache.cacheSize + emailSize > MAX_CACHE_SIZE) {\n esCache.isCacheLimited = true;\n return { success: false, reason: 'hit cache limit' };\n }\n\n esCache.esCache.set(email.id, email);\n esCache.cacheSize += emailSize;\n\n return { success: true };\n } catch (error) {\n throw new Error('Failed to add email to the cache', { cause: error });\n }\n};\n","import { Index, DefaultSearchResults } from 'flexsearch';\nimport type { IndexOptions } from 'flexsearch';\nimport { Email, MailCache, EmailSearchResult } from '../types';\n\ntype ExtendedIndexOptions = IndexOptions & {\n minlength?: number;\n resolution?: number;\n optimize?: boolean;\n fastupdate?: boolean;\n cache?: boolean;\n};\n\nconst SUBJECT_OPTIONS: ExtendedIndexOptions = {\n preset: 'match',\n tokenize: 'forward',\n resolution: 9,\n minlength: 2,\n optimize: true,\n fastupdate: true,\n cache: true,\n};\nconst BODY_OPTIONS: ExtendedIndexOptions = {\n ...SUBJECT_OPTIONS,\n minlength: 3,\n resolution: 6,\n};\n\nconst FROM_OPTIONS: ExtendedIndexOptions = {\n ...SUBJECT_OPTIONS,\n tokenize: 'strict',\n minlength: 1,\n};\n\nconst TO_OPTIONS: ExtendedIndexOptions = {\n ...SUBJECT_OPTIONS,\n tokenize: 'strict',\n minlength: 1,\n};\n\nexport interface EmailSearchIndex {\n subjectIndex: Index;\n bodyIndex: Index;\n fromIndex: Index;\n toIndex: Index;\n isReady: boolean;\n}\n\n/**\n * Creates new email search index\n *\n * @returns The email search index\n */\nconst createSearchIndex = (): EmailSearchIndex => ({\n subjectIndex: new Index(SUBJECT_OPTIONS),\n bodyIndex: new Index(BODY_OPTIONS),\n fromIndex: new Index(FROM_OPTIONS),\n toIndex: new Index(TO_OPTIONS),\n isReady: false,\n});\n\n/**\n * Adds the given email to the email search index\n *\n * @param email - The email to add\n * @param searchIndex - The email search index\n */\nexport const addEmailToSearchIndex = (email: Email, searchIndex: EmailSearchIndex): void => {\n try {\n const emailId = email.id;\n searchIndex.subjectIndex.add(emailId, email.body.subject);\n searchIndex.bodyIndex.add(emailId, email.body.text);\n const senderText = `${email.params.sender.name || ''} ${email.params.sender.email || ''}`.trim();\n searchIndex.fromIndex.add(emailId, senderText);\n\n const recipientsList = email.params.recipients?.length ? email.params.recipients : [email.params.recipient];\n\n const recipientsText = recipientsList\n .map((recipient) => `${recipient.name || ''} ${recipient.email || ''}`.trim())\n .join(' ');\n searchIndex.toIndex.add(emailId, recipientsText);\n } catch (error) {\n throw new Error('Failed to add email to the search index', { cause: error });\n }\n};\n\n/**\n * Removes the email from the email search index\n *\n * @param emailID - The email identifier\n * @param searchIndex - The email search index\n */\nexport const removeEmailFromSearchIndex = (emailID: string, searchIndex: EmailSearchIndex): void => {\n try {\n searchIndex.subjectIndex.remove(emailID);\n searchIndex.bodyIndex.remove(emailID);\n searchIndex.fromIndex.remove(emailID);\n searchIndex.toIndex.remove(emailID);\n } catch (error) {\n throw new Error(`Failed to remove email with ID ${emailID} from the search index`, { cause: error });\n }\n};\n\n/**\n * Buils the email search index from the email cache\n *\n * @param esCache - The email cache\n * @returns The email search index\n */\nexport const buildSearchIndexFromCache = async (esCache: MailCache<Email>): Promise<EmailSearchIndex> => {\n try {\n const searchIndex = createSearchIndex();\n searchIndex.isReady = false;\n\n for (const email of esCache.esCache.values()) {\n addEmailToSearchIndex(email, searchIndex);\n }\n\n searchIndex.isReady = true;\n return searchIndex;\n } catch (error) {\n throw new Error('Failed build an email search index from the cache', { cause: error });\n }\n};\n\nexport interface SearchOptions {\n fields?: ('subject' | 'body' | 'from' | 'to')[];\n limit?: number;\n boost?: {\n subject?: number;\n body?: number;\n from?: number;\n to?: number;\n };\n}\n\n/**\n * Searches in teh\n *\n * @param query - The string to search for\n * @param esCache - The email cache\n * @param searchIndex - The email search index\n * @param opetions - The optional search limitations\n * @returns The result of the email search (emails ans their corresponding result weights)\n */\nexport const searchEmails = async (\n query: string,\n esCache: MailCache<Email>,\n searchIndex: EmailSearchIndex,\n options: SearchOptions = {},\n): Promise<EmailSearchResult[]> => {\n try {\n if (!searchIndex.isReady || !query.trim()) {\n return [];\n }\n\n const {\n fields = ['subject', 'body', 'from', 'to'],\n limit = 50,\n boost = { subject: 3, body: 1, from: 2, to: 2 },\n } = options;\n\n const results = new Map<string, number>();\n\n const searchPromises = fields.map(async (field) => {\n let fieldResults: DefaultSearchResults;\n\n switch (field) {\n case 'subject':\n fieldResults = await searchIndex.subjectIndex.searchAsync(query);\n break;\n case 'body':\n fieldResults = await searchIndex.bodyIndex.searchAsync(query);\n break;\n case 'from':\n fieldResults = await searchIndex.fromIndex.searchAsync(query);\n break;\n case 'to':\n fieldResults = await searchIndex.toIndex.searchAsync(query);\n break;\n default:\n return;\n }\n\n const fieldBoost = boost[field] || 1;\n\n fieldResults.forEach((emailId) => {\n const currentScore = results.get(String(emailId)) || 0;\n results.set(String(emailId), currentScore + fieldBoost);\n });\n });\n\n await Promise.all(searchPromises);\n\n const emailResults: EmailSearchResult[] = [];\n\n for (const [emailId, score] of results) {\n const email = esCache.esCache.get(emailId);\n if (email) {\n emailResults.push({ email, score });\n }\n }\n emailResults.sort((a, b) => (b.score || 0) - (a.score || 0));\n return emailResults.slice(0, limit);\n } catch (error) {\n throw new Error('Email search failed', { cause: error });\n }\n};\n"],"mappings":";;;;;;;;;;;;;;AAsBA,MAAM,mBAAmB,WAA2B;AAClD,QAAO,MAAM,OAAO;;;;;;;;AAStB,M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