internxt-crypto 0.0.13 → 0.0.14
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/dist/{aes-DSDxP8uF.mjs → aes-DCb9dJqb.mjs} +7 -30
- package/dist/aes-DCb9dJqb.mjs.map +1 -0
- package/dist/{aes-bOrKZuUC.js → aes-DQo1MyCz.js} +7 -30
- package/dist/aes-DQo1MyCz.js.map +1 -0
- package/dist/constants.d.mts +5 -5
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.d.ts +5 -5
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +8 -8
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +5 -5
- package/dist/constants.mjs.map +1 -1
- package/dist/derive-key.js +1 -1
- package/dist/derive-key.mjs +1 -1
- package/dist/{deriveKeysFromPassword-Ko7NxkHM.js → deriveKeysFromPassword-BYaiSGKL.js} +4 -9
- package/dist/{deriveKeysFromPassword-Ko7NxkHM.js.map → deriveKeysFromPassword-BYaiSGKL.js.map} +1 -1
- package/dist/{deriveKeysFromPassword-DJRWZ8Rw.mjs → deriveKeysFromPassword-XIwzErlL.mjs} +4 -9
- package/dist/{deriveKeysFromPassword-DJRWZ8Rw.mjs.map → deriveKeysFromPassword-XIwzErlL.mjs.map} +1 -1
- package/dist/email-crypto.js +1 -1
- package/dist/email-crypto.mjs +1 -1
- package/dist/email-search.d.mts +18 -18
- package/dist/email-search.d.mts.map +1 -1
- package/dist/email-search.d.ts +18 -18
- package/dist/email-search.d.ts.map +1 -1
- package/dist/email-search.js +14 -14
- package/dist/email-search.js.map +1 -1
- package/dist/email-search.mjs +14 -14
- package/dist/email-search.mjs.map +1 -1
- package/dist/{emailEncryptionKey-27mIHh18.mjs → emailEncryptionKey-B9xHw1h_.mjs} +6 -6
- package/dist/emailEncryptionKey-B9xHw1h_.mjs.map +1 -0
- package/dist/{emailEncryptionKey-DbaDAdoW.js → emailEncryptionKey-CQNe5W_T.js} +6 -6
- package/dist/emailEncryptionKey-CQNe5W_T.js.map +1 -0
- package/dist/{emailKeys-BJGdkLrg.js → emailKeys-C0KhtfNP.js} +28 -49
- package/dist/emailKeys-C0KhtfNP.js.map +1 -0
- package/dist/{emailKeys-Fp9WR3wl.mjs → emailKeys-D4lp9NRv.mjs} +26 -47
- package/dist/emailKeys-D4lp9NRv.mjs.map +1 -0
- package/dist/index.d.mts +11 -11
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -12
- package/dist/index.mjs +7 -7
- package/dist/keystore-crypto.js +1 -1
- package/dist/keystore-crypto.mjs +1 -1
- package/dist/kyber768-SXnIo-Nw.mjs +47 -0
- package/dist/kyber768-SXnIo-Nw.mjs.map +1 -0
- package/dist/kyber768-WUVc2fIR.js +65 -0
- package/dist/kyber768-WUVc2fIR.js.map +1 -0
- package/dist/post-quantum-crypto.d.mts +5 -41
- package/dist/post-quantum-crypto.d.mts.map +1 -1
- package/dist/post-quantum-crypto.d.ts +5 -41
- package/dist/post-quantum-crypto.d.ts.map +1 -1
- package/dist/post-quantum-crypto.js +4 -16
- package/dist/post-quantum-crypto.mjs +2 -2
- package/dist/symmetric-crypto.d.mts +4 -4
- package/dist/symmetric-crypto.d.mts.map +1 -1
- package/dist/symmetric-crypto.d.ts +4 -4
- package/dist/symmetric-crypto.d.ts.map +1 -1
- package/dist/symmetric-crypto.js +1 -1
- package/dist/symmetric-crypto.mjs +1 -1
- package/dist/types.d.mts +5 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.d.ts +5 -1
- 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 +2 -1
- package/dist/aes-DSDxP8uF.mjs.map +0 -1
- package/dist/aes-bOrKZuUC.js.map +0 -1
- package/dist/emailEncryptionKey-27mIHh18.mjs.map +0 -1
- package/dist/emailEncryptionKey-DbaDAdoW.js.map +0 -1
- package/dist/emailKeys-BJGdkLrg.js.map +0 -1
- package/dist/emailKeys-Fp9WR3wl.mjs.map +0 -1
- package/dist/kyber1024-PpwE1rki.mjs +0 -144
- package/dist/kyber1024-PpwE1rki.mjs.map +0 -1
- package/dist/kyber1024-jeKS8CXf.js +0 -173
- package/dist/kyber1024-jeKS8CXf.js.map +0 -1
- package/dist/kyber768-DGBB_RS7.d.ts +0 -32
- package/dist/kyber768-DGBB_RS7.d.ts.map +0 -1
- package/dist/kyber768-DzIcGg1i.d.mts +0 -32
- package/dist/kyber768-DzIcGg1i.d.mts.map +0 -1
package/dist/email-crypto.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_emailKeys = require('./emailKeys-
|
|
2
|
+
const require_emailKeys = require('./emailKeys-C0KhtfNP.js');
|
|
3
3
|
|
|
4
4
|
exports.createPwdProtectedEmail = require_emailKeys.createPwdProtectedEmail;
|
|
5
5
|
exports.decryptEmailHybrid = require_emailKeys.decryptEmailHybrid;
|
package/dist/email-crypto.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as encryptEmailHybrid, d as recipientsToBytes, f as userToBytes, i as decryptEmailHybrid, l as getAux, n as createPwdProtectedEmail, o as encryptEmailHybridForMultipleRecipients, r as decryptPwdProtectedEmail, t as generateEmailKeys, u as emailToBinary } from "./emailKeys-
|
|
1
|
+
import { a as encryptEmailHybrid, d as recipientsToBytes, f as userToBytes, i as decryptEmailHybrid, l as getAux, n as createPwdProtectedEmail, o as encryptEmailHybridForMultipleRecipients, r as decryptPwdProtectedEmail, t as generateEmailKeys, u as emailToBinary } from "./emailKeys-D4lp9NRv.mjs";
|
|
2
2
|
|
|
3
3
|
export { createPwdProtectedEmail, decryptEmailHybrid, decryptPwdProtectedEmail, emailToBinary, encryptEmailHybrid, encryptEmailHybridForMultipleRecipients, generateEmailKeys, getAux, recipientsToBytes, userToBytes };
|
package/dist/email-search.d.mts
CHANGED
|
@@ -37,42 +37,42 @@ declare const deleteDatabase: (userID: string) => Promise<void>;
|
|
|
37
37
|
* Derives database encryption key for the given user
|
|
38
38
|
*
|
|
39
39
|
* @param userID - The user ID
|
|
40
|
-
* @returns The symmetric
|
|
40
|
+
* @returns The symmetric key for protecting database
|
|
41
41
|
*/
|
|
42
|
-
declare const deriveIndexKey: (baseKey: Uint8Array) => Promise<
|
|
42
|
+
declare const deriveIndexKey: (baseKey: Uint8Array) => Promise<Uint8Array>;
|
|
43
43
|
/**
|
|
44
44
|
* Encrypts the given email and stores it in the IndexedDB database
|
|
45
45
|
*
|
|
46
46
|
* @param newEmailToStore - The email for storing
|
|
47
|
-
* @param indexKey - The symmetric
|
|
47
|
+
* @param indexKey - The symmetric key for protecting database
|
|
48
48
|
* @param esDB - The database
|
|
49
49
|
*/
|
|
50
|
-
declare const encryptAndStoreEmail: (newEmailToStore: Email, indexKey:
|
|
50
|
+
declare const encryptAndStoreEmail: (newEmailToStore: Email, indexKey: Uint8Array, esDB: MailDB) => Promise<void>;
|
|
51
51
|
/**
|
|
52
52
|
* Encrypts the given set of emails and stores it in the IndexedDB database
|
|
53
53
|
*
|
|
54
54
|
* @param newEmailsToStore - The set of emails for storing
|
|
55
|
-
* @param indexKey - The symmetric
|
|
55
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
56
56
|
* @param esDB - The database
|
|
57
57
|
*/
|
|
58
|
-
declare const encryptAndStoreManyEmail: (newEmailsToStore: Email[], indexKey:
|
|
58
|
+
declare const encryptAndStoreManyEmail: (newEmailsToStore: Email[], indexKey: Uint8Array, esDB: MailDB) => Promise<void>;
|
|
59
59
|
/**
|
|
60
60
|
* Fetches the email from the database and decrypts it
|
|
61
61
|
*
|
|
62
62
|
* @param emailID - The email identifier
|
|
63
|
-
* @param indexKey - The symmetric
|
|
63
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
64
64
|
* @param encryptedEmail - The encrypted email
|
|
65
65
|
* @returns The decrypted email
|
|
66
66
|
*/
|
|
67
|
-
declare const getAndDecryptEmail: (emailID: string, indexKey:
|
|
67
|
+
declare const getAndDecryptEmail: (emailID: string, indexKey: Uint8Array, esDB: MailDB) => Promise<Email>;
|
|
68
68
|
/**
|
|
69
69
|
* Fetches all email from the database and decrypts them
|
|
70
70
|
*
|
|
71
|
-
* @param indexKey - The symmetric
|
|
71
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
72
72
|
* @param esDB - The database
|
|
73
73
|
* @returns The decrypted emails
|
|
74
74
|
*/
|
|
75
|
-
declare const getAndDecryptAllEmails: (indexKey:
|
|
75
|
+
declare const getAndDecryptAllEmails: (indexKey: Uint8Array, esDB: MailDB) => Promise<Email[]>;
|
|
76
76
|
/**
|
|
77
77
|
* Deletes the email from the database
|
|
78
78
|
*
|
|
@@ -105,28 +105,28 @@ declare const enforceMaxEmailNumber: (esDB: MailDB, max: number) => Promise<void
|
|
|
105
105
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
106
106
|
*
|
|
107
107
|
* @param esDB - The database
|
|
108
|
-
* @param indexKey - The symmetric
|
|
108
|
+
* @param indexKey - The symmetric Uint8Array key for protecting database
|
|
109
109
|
* @returns The number of stored emails
|
|
110
110
|
*/
|
|
111
|
-
declare const getAllEmailsSortedNewestFirst: (esDB: MailDB, indexKey:
|
|
111
|
+
declare const getAllEmailsSortedNewestFirst: (esDB: MailDB, indexKey: Uint8Array) => Promise<Email[]>;
|
|
112
112
|
/**
|
|
113
113
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)
|
|
114
114
|
*
|
|
115
115
|
* @param esDB - The database
|
|
116
|
-
* @param indexKey - The symmetric
|
|
116
|
+
* @param indexKey - The symmetric key for protecting database
|
|
117
117
|
* @returns The number of stored emails
|
|
118
118
|
*/
|
|
119
|
-
declare const getAllEmailsSortedOldestFirst: (esDB: MailDB, indexKey:
|
|
119
|
+
declare const getAllEmailsSortedOldestFirst: (esDB: MailDB, indexKey: Uint8Array) => Promise<Email[]>;
|
|
120
120
|
/**
|
|
121
121
|
* Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
122
122
|
*
|
|
123
123
|
* @param esDB - The database
|
|
124
|
-
* @param indexKey - The symmetric
|
|
124
|
+
* @param indexKey - The symmetric key for protecting database
|
|
125
125
|
* @param batchSize - The size of the batch
|
|
126
126
|
* @param startCursor - The starting point (optional). If not given, starts from the beginning
|
|
127
127
|
* @returns The number of stored emails
|
|
128
128
|
*/
|
|
129
|
-
declare const getEmailBatch: (esDB: MailDB, indexKey:
|
|
129
|
+
declare const getEmailBatch: (esDB: MailDB, indexKey: Uint8Array, batchSize: number, startCursor?: IDBValidKey) => Promise<{
|
|
130
130
|
emails: Email[];
|
|
131
131
|
nextCursor?: IDBValidKey;
|
|
132
132
|
}>;
|
|
@@ -135,11 +135,11 @@ declare const getEmailBatch: (esDB: MailDB, indexKey: CryptoKey, batchSize: numb
|
|
|
135
135
|
/**
|
|
136
136
|
* Fetches all emails from the database in batches and cahces them
|
|
137
137
|
*
|
|
138
|
-
* @param indexKey - The symmetric
|
|
138
|
+
* @param indexKey - The symmetric key for protecting database
|
|
139
139
|
* @param esCache - The cache to add emails too
|
|
140
140
|
* @param esDB - The database
|
|
141
141
|
*/
|
|
142
|
-
declare const createCacheFromDB: (indexKey:
|
|
142
|
+
declare const createCacheFromDB: (indexKey: Uint8Array, esDB: MailDB) => Promise<MailCache<Email>>;
|
|
143
143
|
/**
|
|
144
144
|
* Gets an email from the cache
|
|
145
145
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"email-search.d.mts","names":[],"sources":["../src/email-search/indexedDB.ts","../src/email-search/mailCache.ts","../src/email-search/search.ts"],"mappings":";;;;;KAOY,MAAA,GAAS,YAAA,CAAa,iBAAA;AAAA,UAEjB,iBAAA,SAA0B,QAAA;EACzC,KAAA;IACE,GAAA;IACA,KAAA,EAAO,WAAA;IACP,OAAA;MAAW,MAAA;IAAA;EAAA;AAAA;;;;;;;cAoBF,YAAA,GAAsB,MAAA,aAAiB,OAAA,CAAQ,MAAA;;;;;AAA5D;cAqBa,aAAA,GAAiB,IAAA,EAAM,MAAA;;;;;;;cAUvB,cAAA,GAAwB,MAAA,aAAiB,OAAA;AAVtD;;;;;AAUA;AAVA,cAqBa,cAAA,GAAwB,OAAA,EAAS,UAAA,KAAa,OAAA,CAAQ,
|
|
1
|
+
{"version":3,"file":"email-search.d.mts","names":[],"sources":["../src/email-search/indexedDB.ts","../src/email-search/mailCache.ts","../src/email-search/search.ts"],"mappings":";;;;;KAOY,MAAA,GAAS,YAAA,CAAa,iBAAA;AAAA,UAEjB,iBAAA,SAA0B,QAAA;EACzC,KAAA;IACE,GAAA;IACA,KAAA,EAAO,WAAA;IACP,OAAA;MAAW,MAAA;IAAA;EAAA;AAAA;;;;;;;cAoBF,YAAA,GAAsB,MAAA,aAAiB,OAAA,CAAQ,MAAA;;;;;AAA5D;cAqBa,aAAA,GAAiB,IAAA,EAAM,MAAA;;;;;;;cAUvB,cAAA,GAAwB,MAAA,aAAiB,OAAA;AAVtD;;;;;AAUA;AAVA,cAqBa,cAAA,GAAwB,OAAA,EAAS,UAAA,KAAa,OAAA,CAAQ,UAAA;;;;AAAnE;;;;cAWa,oBAAA,GACX,eAAA,EAAiB,KAAA,EACjB,QAAA,EAAU,UAAA,EACV,IAAA,EAAM,MAAA,KACL,OAAA;;;;;;;;cAkBU,wBAAA,GACX,gBAAA,EAAkB,KAAA,IAClB,QAAA,EAAU,UAAA,EACV,IAAA,EAAM,MAAA,KACL,OAAA;;AA1BH;;;;;;;cAqEa,kBAAA,GAA4B,OAAA,UAAiB,QAAA,EAAU,UAAA,EAAY,IAAA,EAAM,MAAA,KAAS,OAAA,CAAQ,KAAA;;;;;;;;cAmB1F,sBAAA,GAAgC,QAAA,EAAU,UAAA,EAAY,IAAA,EAAM,MAAA,KAAS,OAAA,CAAQ,KAAA;;;AAlE1F;;;;cA0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AA,EAAc,KAAA;EACd,SAAA,EAAW,KAAA;EACX,SAAA,EAAW,KAAA;EACX,OAAA,EAAS,KAAA;EACT,OAAA;AAAA;;AFnCF;;;;;cEyDa,qBAAA,GAAyB,KAAA,EAAO,KAAA,EAAO,WAAA,EAAa,gBAAA;;;;;;;cAkCpD,0BAAA,GAA8B,OAAA,UAAiB,WAAA,EAAa,gBAAA;AFnEzE;;;;;;AAAA,cEoFa,yBAAA,GAAmC,OAAA,EAAS,SAAA,CAAU,KAAA,MAAS,OAAA,CAAQ,gBAAA;AAAA,UAgBnE,aAAA;EACf,MAAA;EACA,KAAA;EACA,KAAA;IACE,OAAA;IACA,IAAA;IACA,IAAA;IACA,EAAA;EAAA;AAAA;;;AFjEJ;;;;;;;cE8Ea,YAAA,GACX,KAAA,UACA,OAAA,EAAS,SAAA,CAAU,KAAA,GACnB,WAAA,EAAa,gBAAA,EACb,OAAA,GAAS,aAAA,KACR,OAAA,CAAQ,iBAAA"}
|
package/dist/email-search.d.ts
CHANGED
|
@@ -37,42 +37,42 @@ declare const deleteDatabase: (userID: string) => Promise<void>;
|
|
|
37
37
|
* Derives database encryption key for the given user
|
|
38
38
|
*
|
|
39
39
|
* @param userID - The user ID
|
|
40
|
-
* @returns The symmetric
|
|
40
|
+
* @returns The symmetric key for protecting database
|
|
41
41
|
*/
|
|
42
|
-
declare const deriveIndexKey: (baseKey: Uint8Array) => Promise<
|
|
42
|
+
declare const deriveIndexKey: (baseKey: Uint8Array) => Promise<Uint8Array>;
|
|
43
43
|
/**
|
|
44
44
|
* Encrypts the given email and stores it in the IndexedDB database
|
|
45
45
|
*
|
|
46
46
|
* @param newEmailToStore - The email for storing
|
|
47
|
-
* @param indexKey - The symmetric
|
|
47
|
+
* @param indexKey - The symmetric key for protecting database
|
|
48
48
|
* @param esDB - The database
|
|
49
49
|
*/
|
|
50
|
-
declare const encryptAndStoreEmail: (newEmailToStore: Email, indexKey:
|
|
50
|
+
declare const encryptAndStoreEmail: (newEmailToStore: Email, indexKey: Uint8Array, esDB: MailDB) => Promise<void>;
|
|
51
51
|
/**
|
|
52
52
|
* Encrypts the given set of emails and stores it in the IndexedDB database
|
|
53
53
|
*
|
|
54
54
|
* @param newEmailsToStore - The set of emails for storing
|
|
55
|
-
* @param indexKey - The symmetric
|
|
55
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
56
56
|
* @param esDB - The database
|
|
57
57
|
*/
|
|
58
|
-
declare const encryptAndStoreManyEmail: (newEmailsToStore: Email[], indexKey:
|
|
58
|
+
declare const encryptAndStoreManyEmail: (newEmailsToStore: Email[], indexKey: Uint8Array, esDB: MailDB) => Promise<void>;
|
|
59
59
|
/**
|
|
60
60
|
* Fetches the email from the database and decrypts it
|
|
61
61
|
*
|
|
62
62
|
* @param emailID - The email identifier
|
|
63
|
-
* @param indexKey - The symmetric
|
|
63
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
64
64
|
* @param encryptedEmail - The encrypted email
|
|
65
65
|
* @returns The decrypted email
|
|
66
66
|
*/
|
|
67
|
-
declare const getAndDecryptEmail: (emailID: string, indexKey:
|
|
67
|
+
declare const getAndDecryptEmail: (emailID: string, indexKey: Uint8Array, esDB: MailDB) => Promise<Email>;
|
|
68
68
|
/**
|
|
69
69
|
* Fetches all email from the database and decrypts them
|
|
70
70
|
*
|
|
71
|
-
* @param indexKey - The symmetric
|
|
71
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
72
72
|
* @param esDB - The database
|
|
73
73
|
* @returns The decrypted emails
|
|
74
74
|
*/
|
|
75
|
-
declare const getAndDecryptAllEmails: (indexKey:
|
|
75
|
+
declare const getAndDecryptAllEmails: (indexKey: Uint8Array, esDB: MailDB) => Promise<Email[]>;
|
|
76
76
|
/**
|
|
77
77
|
* Deletes the email from the database
|
|
78
78
|
*
|
|
@@ -105,28 +105,28 @@ declare const enforceMaxEmailNumber: (esDB: MailDB, max: number) => Promise<void
|
|
|
105
105
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
106
106
|
*
|
|
107
107
|
* @param esDB - The database
|
|
108
|
-
* @param indexKey - The symmetric
|
|
108
|
+
* @param indexKey - The symmetric Uint8Array key for protecting database
|
|
109
109
|
* @returns The number of stored emails
|
|
110
110
|
*/
|
|
111
|
-
declare const getAllEmailsSortedNewestFirst: (esDB: MailDB, indexKey:
|
|
111
|
+
declare const getAllEmailsSortedNewestFirst: (esDB: MailDB, indexKey: Uint8Array) => Promise<Email[]>;
|
|
112
112
|
/**
|
|
113
113
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)
|
|
114
114
|
*
|
|
115
115
|
* @param esDB - The database
|
|
116
|
-
* @param indexKey - The symmetric
|
|
116
|
+
* @param indexKey - The symmetric key for protecting database
|
|
117
117
|
* @returns The number of stored emails
|
|
118
118
|
*/
|
|
119
|
-
declare const getAllEmailsSortedOldestFirst: (esDB: MailDB, indexKey:
|
|
119
|
+
declare const getAllEmailsSortedOldestFirst: (esDB: MailDB, indexKey: Uint8Array) => Promise<Email[]>;
|
|
120
120
|
/**
|
|
121
121
|
* Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
122
122
|
*
|
|
123
123
|
* @param esDB - The database
|
|
124
|
-
* @param indexKey - The symmetric
|
|
124
|
+
* @param indexKey - The symmetric key for protecting database
|
|
125
125
|
* @param batchSize - The size of the batch
|
|
126
126
|
* @param startCursor - The starting point (optional). If not given, starts from the beginning
|
|
127
127
|
* @returns The number of stored emails
|
|
128
128
|
*/
|
|
129
|
-
declare const getEmailBatch: (esDB: MailDB, indexKey:
|
|
129
|
+
declare const getEmailBatch: (esDB: MailDB, indexKey: Uint8Array, batchSize: number, startCursor?: IDBValidKey) => Promise<{
|
|
130
130
|
emails: Email[];
|
|
131
131
|
nextCursor?: IDBValidKey;
|
|
132
132
|
}>;
|
|
@@ -135,11 +135,11 @@ declare const getEmailBatch: (esDB: MailDB, indexKey: CryptoKey, batchSize: numb
|
|
|
135
135
|
/**
|
|
136
136
|
* Fetches all emails from the database in batches and cahces them
|
|
137
137
|
*
|
|
138
|
-
* @param indexKey - The symmetric
|
|
138
|
+
* @param indexKey - The symmetric key for protecting database
|
|
139
139
|
* @param esCache - The cache to add emails too
|
|
140
140
|
* @param esDB - The database
|
|
141
141
|
*/
|
|
142
|
-
declare const createCacheFromDB: (indexKey:
|
|
142
|
+
declare const createCacheFromDB: (indexKey: Uint8Array, esDB: MailDB) => Promise<MailCache<Email>>;
|
|
143
143
|
/**
|
|
144
144
|
* Gets an email from the cache
|
|
145
145
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"email-search.d.ts","names":[],"sources":["../src/email-search/indexedDB.ts","../src/email-search/mailCache.ts","../src/email-search/search.ts"],"mappings":";;;;;KAOY,MAAA,GAAS,YAAA,CAAa,iBAAA;AAAA,UAEjB,iBAAA,SAA0B,QAAA;EACzC,KAAA;IACE,GAAA;IACA,KAAA,EAAO,WAAA;IACP,OAAA;MAAW,MAAA;IAAA;EAAA;AAAA;;;;;;;cAoBF,YAAA,GAAsB,MAAA,aAAiB,OAAA,CAAQ,MAAA;;;;;AAA5D;cAqBa,aAAA,GAAiB,IAAA,EAAM,MAAA;;;;;;;cAUvB,cAAA,GAAwB,MAAA,aAAiB,OAAA;AAVtD;;;;;AAUA;AAVA,cAqBa,cAAA,GAAwB,OAAA,EAAS,UAAA,KAAa,OAAA,CAAQ,
|
|
1
|
+
{"version":3,"file":"email-search.d.ts","names":[],"sources":["../src/email-search/indexedDB.ts","../src/email-search/mailCache.ts","../src/email-search/search.ts"],"mappings":";;;;;KAOY,MAAA,GAAS,YAAA,CAAa,iBAAA;AAAA,UAEjB,iBAAA,SAA0B,QAAA;EACzC,KAAA;IACE,GAAA;IACA,KAAA,EAAO,WAAA;IACP,OAAA;MAAW,MAAA;IAAA;EAAA;AAAA;;;;;;;cAoBF,YAAA,GAAsB,MAAA,aAAiB,OAAA,CAAQ,MAAA;;;;;AAA5D;cAqBa,aAAA,GAAiB,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@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_chunk = require('./chunk-EbLFQX5m.js');
|
|
3
3
|
const require_constants = require('./constants.js');
|
|
4
|
-
const require_deriveKeysFromPassword = require('./deriveKeysFromPassword-
|
|
5
|
-
const require_emailKeys = require('./emailKeys-
|
|
4
|
+
const require_deriveKeysFromPassword = require('./deriveKeysFromPassword-BYaiSGKL.js');
|
|
5
|
+
const require_emailKeys = require('./emailKeys-C0KhtfNP.js');
|
|
6
6
|
let idb = require("idb");
|
|
7
7
|
let flexsearch = require("flexsearch");
|
|
8
8
|
|
|
@@ -52,16 +52,16 @@ const deleteDatabase = async (userID) => {
|
|
|
52
52
|
* Derives database encryption key for the given user
|
|
53
53
|
*
|
|
54
54
|
* @param userID - The user ID
|
|
55
|
-
* @returns The symmetric
|
|
55
|
+
* @returns The symmetric key for protecting database
|
|
56
56
|
*/
|
|
57
57
|
const deriveIndexKey = async (baseKey) => {
|
|
58
|
-
return require_deriveKeysFromPassword.
|
|
58
|
+
return require_deriveKeysFromPassword.deriveSymmetricKeyFromContext(require_constants.CONTEXT_INDEX, baseKey);
|
|
59
59
|
};
|
|
60
60
|
/**
|
|
61
61
|
* Encrypts the given email and stores it in the IndexedDB database
|
|
62
62
|
*
|
|
63
63
|
* @param newEmailToStore - The email for storing
|
|
64
|
-
* @param indexKey - The symmetric
|
|
64
|
+
* @param indexKey - The symmetric key for protecting database
|
|
65
65
|
* @param esDB - The database
|
|
66
66
|
*/
|
|
67
67
|
const encryptAndStoreEmail = async (newEmailToStore, indexKey, esDB) => {
|
|
@@ -81,7 +81,7 @@ const encryptAndStoreEmail = async (newEmailToStore, indexKey, esDB) => {
|
|
|
81
81
|
* Encrypts the given set of emails and stores it in the IndexedDB database
|
|
82
82
|
*
|
|
83
83
|
* @param newEmailsToStore - The set of emails for storing
|
|
84
|
-
* @param indexKey - The symmetric
|
|
84
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
85
85
|
* @param esDB - The database
|
|
86
86
|
*/
|
|
87
87
|
const encryptAndStoreManyEmail = async (newEmailsToStore, indexKey, esDB) => {
|
|
@@ -103,7 +103,7 @@ const encryptAndStoreManyEmail = async (newEmailsToStore, indexKey, esDB) => {
|
|
|
103
103
|
/**
|
|
104
104
|
* Decrypts the given email
|
|
105
105
|
*
|
|
106
|
-
* @param indexKey - The symmetric
|
|
106
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
107
107
|
* @param encryptedEmail - The encrypted email
|
|
108
108
|
* @returns The decrypted email
|
|
109
109
|
*/
|
|
@@ -122,7 +122,7 @@ const decryptEmail = async (indexKey, encryptedEmail) => {
|
|
|
122
122
|
* Fetches the email from the database and decrypts it
|
|
123
123
|
*
|
|
124
124
|
* @param emailID - The email identifier
|
|
125
|
-
* @param indexKey - The symmetric
|
|
125
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
126
126
|
* @param encryptedEmail - The encrypted email
|
|
127
127
|
* @returns The decrypted email
|
|
128
128
|
*/
|
|
@@ -138,7 +138,7 @@ const getAndDecryptEmail = async (emailID, indexKey, esDB) => {
|
|
|
138
138
|
/**
|
|
139
139
|
* Fetches all email from the database and decrypts them
|
|
140
140
|
*
|
|
141
|
-
* @param indexKey - The symmetric
|
|
141
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
142
142
|
* @param esDB - The database
|
|
143
143
|
* @returns The decrypted emails
|
|
144
144
|
*/
|
|
@@ -214,7 +214,7 @@ const enforceMaxEmailNumber = async (esDB, max) => {
|
|
|
214
214
|
* Fetches all emails from the database, decrypts them and sortes the results in the specified order
|
|
215
215
|
*
|
|
216
216
|
* @param esDB - The database
|
|
217
|
-
* @param indexKey - The symmetric
|
|
217
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
218
218
|
* @param direction - The order of sorting. If 'next' - oldest first, if 'prev' - newest first
|
|
219
219
|
* @returns Decryped emails in the specified order
|
|
220
220
|
*/
|
|
@@ -236,7 +236,7 @@ const fetchEmails = async (esDB, indexKey, direction) => {
|
|
|
236
236
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
237
237
|
*
|
|
238
238
|
* @param esDB - The database
|
|
239
|
-
* @param indexKey - The symmetric
|
|
239
|
+
* @param indexKey - The symmetric Uint8Array key for protecting database
|
|
240
240
|
* @returns The number of stored emails
|
|
241
241
|
*/
|
|
242
242
|
const getAllEmailsSortedNewestFirst = async (esDB, indexKey) => {
|
|
@@ -246,7 +246,7 @@ const getAllEmailsSortedNewestFirst = async (esDB, indexKey) => {
|
|
|
246
246
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)
|
|
247
247
|
*
|
|
248
248
|
* @param esDB - The database
|
|
249
|
-
* @param indexKey - The symmetric
|
|
249
|
+
* @param indexKey - The symmetric key for protecting database
|
|
250
250
|
* @returns The number of stored emails
|
|
251
251
|
*/
|
|
252
252
|
const getAllEmailsSortedOldestFirst = async (esDB, indexKey) => {
|
|
@@ -256,7 +256,7 @@ const getAllEmailsSortedOldestFirst = async (esDB, indexKey) => {
|
|
|
256
256
|
* Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
257
257
|
*
|
|
258
258
|
* @param esDB - The database
|
|
259
|
-
* @param indexKey - The symmetric
|
|
259
|
+
* @param indexKey - The symmetric key for protecting database
|
|
260
260
|
* @param batchSize - The size of the batch
|
|
261
261
|
* @param startCursor - The starting point (optional). If not given, starts from the beginning
|
|
262
262
|
* @returns The number of stored emails
|
|
@@ -314,7 +314,7 @@ function createEmptyCache() {
|
|
|
314
314
|
/**
|
|
315
315
|
* Fetches all emails from the database in batches and cahces them
|
|
316
316
|
*
|
|
317
|
-
* @param indexKey - The symmetric
|
|
317
|
+
* @param indexKey - The symmetric key for protecting database
|
|
318
318
|
* @param esCache - The cache to add emails too
|
|
319
319
|
* @param esDB - The database
|
|
320
320
|
*/
|
package/dist/email-search.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"email-search.js","names":["DB_VERSION","DB_LABEL","deriveSymmetricCryptoKeyFromContext","CONTEXT_INDEX","getAux","encryptEmailContentSymmetricallyWithKey","decryptEmailSymmetrically","emailToBinary","MAX_EMAIL_PER_BATCH","MAX_CACHE_SIZE","Index"],"sources":["../src/email-search/indexedDB.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 { decryptEmailSymmetrically, encryptEmailContentSymmetricallyWithKey } from '../email-crypto/core';\nimport { deriveSymmetricCryptoKeyFromContext } from '../derive-key';\nimport { CONTEXT_INDEX, DB_LABEL, DB_VERSION } from '../constants';\nimport { getAux } from '../email-crypto';\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 CryptoKey for protecting database\n */\nexport const deriveIndexKey = async (baseKey: Uint8Array): Promise<CryptoKey> => {\n return deriveSymmetricCryptoKeyFromContext(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 CryptoKey key for protecting database\n * @param esDB - The database\n */\nexport const encryptAndStoreEmail = async (\n newEmailToStore: Email,\n indexKey: CryptoKey,\n esDB: MailDB,\n): Promise<void> => {\n try {\n const aux = getAux(newEmailToStore.params, false);\n const enc = await encryptEmailContentSymmetricallyWithKey(newEmailToStore.body, indexKey, aux, newEmailToStore.id);\n const encryptedEmail: StoredEmail = { 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 CryptoKey key for protecting database\n * @param esDB - The database\n */\nexport const encryptAndStoreManyEmail = async (\n newEmailsToStore: Email[],\n indexKey: CryptoKey,\n esDB: MailDB,\n): Promise<void> => {\n try {\n const encryptedEmails = await Promise.all(\n newEmailsToStore.map(async (email: Email) => {\n const aux = getAux(email.params, false);\n const enc = await encryptEmailContentSymmetricallyWithKey(email.body, indexKey, aux, email.id);\n\n return { enc, 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 CryptoKey key for protecting database\n * @param encryptedEmail - The encrypted email\n * @returns The decrypted email\n */\nconst decryptEmail = async (indexKey: CryptoKey, encryptedEmail: StoredEmail): Promise<Email> => {\n try {\n const aux = getAux(encryptedEmail.params, false);\n const email = await decryptEmailSymmetrically(indexKey, aux, encryptedEmail.enc);\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 CryptoKey key for protecting database\n * @param encryptedEmail - The encrypted email\n * @returns The decrypted email\n */\nexport const getAndDecryptEmail = async (emailID: string, indexKey: CryptoKey, 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 CryptoKey key for protecting database\n * @param esDB - The database\n * @returns The decrypted emails\n */\nexport const getAndDecryptAllEmails = async (indexKey: CryptoKey, 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 aux = getAux(encEmail.params, false);\n const body = await decryptEmailSymmetrically(indexKey, aux, encEmail.enc);\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 CryptoKey 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: CryptoKey, 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 CryptoKey key for protecting database\n * @returns The number of stored emails\n */\nexport const getAllEmailsSortedNewestFirst = async (esDB: MailDB, indexKey: CryptoKey): 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 CryptoKey key for protecting database\n * @returns The number of stored emails\n */\nexport const getAllEmailsSortedOldestFirst = async (esDB: MailDB, indexKey: CryptoKey): 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 CryptoKey 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: CryptoKey,\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 { MAX_CACHE_SIZE, MAX_EMAIL_PER_BATCH } from '../constants';\nimport { getEmailBatch, getAllEmailsSortedNewestFirst, getEmailCount, MailDB } from './indexedDB';\nimport { Email, MailCache } from '../types';\nimport { emailToBinary } from '../email-crypto';\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 CryptoKey key for protecting database\n * @param esCache - The cache to add emails too\n * @param esDB - The database\n */\nexport const createCacheFromDB = async (indexKey: CryptoKey, 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\n if (email.params.subject) {\n searchIndex.subjectIndex.add(emailId, email.params.subject);\n }\n\n if (email.body?.text) {\n searchIndex.bodyIndex.add(emailId, email.body.text);\n }\n\n if (email.params.sender) {\n const senderText = `${email.params.sender.name || ''} ${email.params.sender.email || ''}`.trim();\n searchIndex.fromIndex.add(emailId, senderText);\n }\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":";;;;;;;;;;;;;;;AAuBA,MAAM,mBAAmB,WAA2B;AAClD,QAAO,MAAM,OAAO;;;;;;;;AAStB,MAAa,eAAe,OAAO,WAAoC;AACrE,KAAI;AAEF,yBADe,gBAAgB,OAAO,EACGA,8BAAY,EACnD,QAAQ,IAAI;AACV,OAAI,CAAC,GAAG,iBAAiB,SAASC,2BAAS,CAEzC,CADc,GAAG,kBAAkBA,4BAAU,EAAE,SAAS,MAAM,CAAC,CACzD,YAAY,UAAU,mBAAmB;KAGpD,CAAC;UACK,OAAO;AACd,QAAM,IAAI,MAAM,uCAAuC,UAAU,EAAE,OAAO,OAAO,CAAC;;;;;;;;AAStF,MAAa,iBAAiB,SAAuB;AACnD,QAAO,KAAK,OAAO;;;;;;;;AASrB,MAAa,iBAAiB,OAAO,WAAkC;AAErE,0BADe,gBAAgB,OAAO,CACf;;;;;;;;AASzB,MAAa,iBAAiB,OAAO,YAA4C;AAC/E,QAAOC,mEAAoCC,iCAAe,QAAQ;;;;;;;;;AAUpE,MAAa,uBAAuB,OAClC,iBACA,UACA,SACkB;AAClB,KAAI;EACF,MAAM,MAAMC,yBAAO,gBAAgB,QAAQ,MAAM;EAEjD,MAAM,iBAA8B;GAAE,KAD1B,MAAMC,0DAAwC,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M,IAAI,MAAM,uBAAuB,EAAE,OAAO,OAAO,CAAC"}
|
|
1
|
+
{"version":3,"file":"email-search.js","names":["DB_VERSION","DB_LABEL","deriveSymmetricKeyFromContext","CONTEXT_INDEX","getAux","encryptEmailContentSymmetricallyWithKey","decryptEmailSymmetrically","emailToBinary","MAX_EMAIL_PER_BATCH","MAX_CACHE_SIZE","Index"],"sources":["../src/email-search/indexedDB.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 { decryptEmailSymmetrically, encryptEmailContentSymmetricallyWithKey } from '../email-crypto/core';\nimport { deriveSymmetricKeyFromContext } from '../derive-key';\nimport { CONTEXT_INDEX, DB_LABEL, DB_VERSION } from '../constants';\nimport { getAux } from '../email-crypto';\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 aux = getAux(newEmailToStore.params, false);\n const enc = await encryptEmailContentSymmetricallyWithKey(newEmailToStore.body, indexKey, aux, newEmailToStore.id);\n const encryptedEmail: StoredEmail = { 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 aux = getAux(email.params, false);\n const enc = await encryptEmailContentSymmetricallyWithKey(email.body, indexKey, aux, email.id);\n\n return { enc, 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 aux = getAux(encryptedEmail.params, false);\n const email = await decryptEmailSymmetrically(indexKey, aux, encryptedEmail.enc);\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 aux = getAux(encEmail.params, false);\n const body = await decryptEmailSymmetrically(indexKey, aux, encEmail.enc);\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 { MAX_CACHE_SIZE, MAX_EMAIL_PER_BATCH } from '../constants';\nimport { getEmailBatch, getAllEmailsSortedNewestFirst, getEmailCount, MailDB } from './indexedDB';\nimport { Email, MailCache } from '../types';\nimport { emailToBinary } from '../email-crypto';\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\n if (email.params.subject) {\n searchIndex.subjectIndex.add(emailId, email.params.subject);\n }\n\n if (email.body?.text) {\n searchIndex.bodyIndex.add(emailId, email.body.text);\n }\n\n if (email.params.sender) {\n const senderText = `${email.params.sender.name || ''} ${email.params.sender.email || ''}`.trim();\n searchIndex.fromIndex.add(emailId, senderText);\n }\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":";;;;;;;;;;;;;;;AAuBA,MAAM,mBAAmB,WAA2B;AAClD,QAAO,MAAM,OAAO;;;;;;;;AAStB,MAAa,eAAe,OAAO,WAAoC;AACrE,KAAI;AAEF,yBADe,gBAAgB,OAAO,EACGA,8BAAY,EACnD,QAAQ,IAAI;AACV,OAAI,CAAC,GAAG,iBAAiB,SAASC,2BAAS,CAEzC,CADc,GAAG,kBAAkBA,4BAAU,EAAE,SAAS,MAAM,CAAC,CACzD,YAAY,UAAU,mBAAmB;KAGpD,CAAC;UACK,OAAO;AACd,QAAM,IAAI,MAAM,uCAAuC,UAAU,EAAE,OAAO,OAAO,CAAC;;;;;;;;AAStF,MAAa,iBAAiB,SAAuB;AACnD,QAAO,KAAK,OAAO;;;;;;;;AASrB,MAAa,iBAAiB,OAAO,WAAkC;AAErE,0BADe,gBAAgB,OAAO,CACf;;;;;;;;AASzB,MAAa,iBAAiB,OAAO,YAA6C;AAChF,QAAOC,6DAA8BC,iCAAe,QAAQ;;;;;;;;;AAU9D,MAAa,uBAAuB,OAClC,iBACA,UACA,SACkB;AAClB,KAAI;EACF,MAAM,MAAMC,yBAAO,gBAAgB,QAAQ,MAAM;EAEjD,MAAM,iBAA8B;GAAE,KAD1B,MAAMC,0DAAwC,gBAAgB,MAAM,UAAU,KAAK,gBA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AO,OAAO,CAAC"}
|
package/dist/email-search.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CONTEXT_INDEX, DB_LABEL, DB_VERSION, MAX_CACHE_SIZE, MAX_EMAIL_PER_BATCH } from "./constants.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { c as encryptEmailContentSymmetricallyWithKey, l as getAux, s as decryptEmailSymmetrically, u as emailToBinary } from "./emailKeys-
|
|
2
|
+
import { c as deriveSymmetricKeyFromContext } from "./deriveKeysFromPassword-XIwzErlL.mjs";
|
|
3
|
+
import { c as encryptEmailContentSymmetricallyWithKey, l as getAux, s as decryptEmailSymmetrically, u as emailToBinary } from "./emailKeys-D4lp9NRv.mjs";
|
|
4
4
|
import { deleteDB, openDB } from "idb";
|
|
5
5
|
import { Index } from "flexsearch";
|
|
6
6
|
|
|
@@ -50,16 +50,16 @@ const deleteDatabase = async (userID) => {
|
|
|
50
50
|
* Derives database encryption key for the given user
|
|
51
51
|
*
|
|
52
52
|
* @param userID - The user ID
|
|
53
|
-
* @returns The symmetric
|
|
53
|
+
* @returns The symmetric key for protecting database
|
|
54
54
|
*/
|
|
55
55
|
const deriveIndexKey = async (baseKey) => {
|
|
56
|
-
return
|
|
56
|
+
return deriveSymmetricKeyFromContext(CONTEXT_INDEX, baseKey);
|
|
57
57
|
};
|
|
58
58
|
/**
|
|
59
59
|
* Encrypts the given email and stores it in the IndexedDB database
|
|
60
60
|
*
|
|
61
61
|
* @param newEmailToStore - The email for storing
|
|
62
|
-
* @param indexKey - The symmetric
|
|
62
|
+
* @param indexKey - The symmetric key for protecting database
|
|
63
63
|
* @param esDB - The database
|
|
64
64
|
*/
|
|
65
65
|
const encryptAndStoreEmail = async (newEmailToStore, indexKey, esDB) => {
|
|
@@ -79,7 +79,7 @@ const encryptAndStoreEmail = async (newEmailToStore, indexKey, esDB) => {
|
|
|
79
79
|
* Encrypts the given set of emails and stores it in the IndexedDB database
|
|
80
80
|
*
|
|
81
81
|
* @param newEmailsToStore - The set of emails for storing
|
|
82
|
-
* @param indexKey - The symmetric
|
|
82
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
83
83
|
* @param esDB - The database
|
|
84
84
|
*/
|
|
85
85
|
const encryptAndStoreManyEmail = async (newEmailsToStore, indexKey, esDB) => {
|
|
@@ -101,7 +101,7 @@ const encryptAndStoreManyEmail = async (newEmailsToStore, indexKey, esDB) => {
|
|
|
101
101
|
/**
|
|
102
102
|
* Decrypts the given email
|
|
103
103
|
*
|
|
104
|
-
* @param indexKey - The symmetric
|
|
104
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
105
105
|
* @param encryptedEmail - The encrypted email
|
|
106
106
|
* @returns The decrypted email
|
|
107
107
|
*/
|
|
@@ -120,7 +120,7 @@ const decryptEmail = async (indexKey, encryptedEmail) => {
|
|
|
120
120
|
* Fetches the email from the database and decrypts it
|
|
121
121
|
*
|
|
122
122
|
* @param emailID - The email identifier
|
|
123
|
-
* @param indexKey - The symmetric
|
|
123
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
124
124
|
* @param encryptedEmail - The encrypted email
|
|
125
125
|
* @returns The decrypted email
|
|
126
126
|
*/
|
|
@@ -136,7 +136,7 @@ const getAndDecryptEmail = async (emailID, indexKey, esDB) => {
|
|
|
136
136
|
/**
|
|
137
137
|
* Fetches all email from the database and decrypts them
|
|
138
138
|
*
|
|
139
|
-
* @param indexKey - The symmetric
|
|
139
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
140
140
|
* @param esDB - The database
|
|
141
141
|
* @returns The decrypted emails
|
|
142
142
|
*/
|
|
@@ -212,7 +212,7 @@ const enforceMaxEmailNumber = async (esDB, max) => {
|
|
|
212
212
|
* Fetches all emails from the database, decrypts them and sortes the results in the specified order
|
|
213
213
|
*
|
|
214
214
|
* @param esDB - The database
|
|
215
|
-
* @param indexKey - The symmetric
|
|
215
|
+
* @param indexKey - The symmetric key key for protecting database
|
|
216
216
|
* @param direction - The order of sorting. If 'next' - oldest first, if 'prev' - newest first
|
|
217
217
|
* @returns Decryped emails in the specified order
|
|
218
218
|
*/
|
|
@@ -234,7 +234,7 @@ const fetchEmails = async (esDB, indexKey, direction) => {
|
|
|
234
234
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
235
235
|
*
|
|
236
236
|
* @param esDB - The database
|
|
237
|
-
* @param indexKey - The symmetric
|
|
237
|
+
* @param indexKey - The symmetric Uint8Array key for protecting database
|
|
238
238
|
* @returns The number of stored emails
|
|
239
239
|
*/
|
|
240
240
|
const getAllEmailsSortedNewestFirst = async (esDB, indexKey) => {
|
|
@@ -244,7 +244,7 @@ const getAllEmailsSortedNewestFirst = async (esDB, indexKey) => {
|
|
|
244
244
|
* Fetches all emails from the database, decrypts them and sortes the results based on the creation time (oldest first)
|
|
245
245
|
*
|
|
246
246
|
* @param esDB - The database
|
|
247
|
-
* @param indexKey - The symmetric
|
|
247
|
+
* @param indexKey - The symmetric key for protecting database
|
|
248
248
|
* @returns The number of stored emails
|
|
249
249
|
*/
|
|
250
250
|
const getAllEmailsSortedOldestFirst = async (esDB, indexKey) => {
|
|
@@ -254,7 +254,7 @@ const getAllEmailsSortedOldestFirst = async (esDB, indexKey) => {
|
|
|
254
254
|
* Fetches a batch of emails from the database, decrypts them and sortes the results based on the creation time (newest first)
|
|
255
255
|
*
|
|
256
256
|
* @param esDB - The database
|
|
257
|
-
* @param indexKey - The symmetric
|
|
257
|
+
* @param indexKey - The symmetric key for protecting database
|
|
258
258
|
* @param batchSize - The size of the batch
|
|
259
259
|
* @param startCursor - The starting point (optional). If not given, starts from the beginning
|
|
260
260
|
* @returns The number of stored emails
|
|
@@ -312,7 +312,7 @@ function createEmptyCache() {
|
|
|
312
312
|
/**
|
|
313
313
|
* Fetches all emails from the database in batches and cahces them
|
|
314
314
|
*
|
|
315
|
-
* @param indexKey - The symmetric
|
|
315
|
+
* @param indexKey - The symmetric key for protecting database
|
|
316
316
|
* @param esCache - The cache to add emails too
|
|
317
317
|
* @param esDB - The database
|
|
318
318
|
*/
|