@fgv/ts-web-extras 5.1.0-32 → 5.1.0-34

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.
@@ -0,0 +1,238 @@
1
+ /*
2
+ * Copyright (c) 2026 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import { captureAsyncResult, captureResult, fail, succeed } from '@fgv/ts-utils';
23
+ const DEFAULT_DATABASE_NAME = 'fgv-keystore-private-keys';
24
+ const DEFAULT_STORE_NAME = 'privateKeys';
25
+ /**
26
+ * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}
27
+ * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via
28
+ * IndexedDB's structured-clone serialization — no JWK round-trip — so it works
29
+ * with non-extractable keys.
30
+ *
31
+ * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by
32
+ * reference (structured clone) rather than exported, the keystore may generate
33
+ * `extractable: false` keys for maximum security on browsers that support it.
34
+ *
35
+ * The database is opened lazily on first use and cached. Each operation runs in
36
+ * its own transaction, relying on IndexedDB's default serialization. Multi-tab
37
+ * concurrency is a known limitation: two tabs writing the same id can race; this
38
+ * implementation targets single-tab use.
39
+ *
40
+ * @public
41
+ */
42
+ export class IdbPrivateKeyStorage {
43
+ constructor(factory, databaseName, storeName) {
44
+ /**
45
+ * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable
46
+ * keys are supported.
47
+ */
48
+ this.supportsNonExtractable = true;
49
+ this._factory = factory;
50
+ this._databaseName = databaseName;
51
+ this._storeName = storeName;
52
+ }
53
+ /**
54
+ * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.
55
+ * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.
56
+ * @returns `Success` with the new instance, or `Failure` if no IndexedDB
57
+ * factory is available.
58
+ */
59
+ static create(params = {}) {
60
+ var _a, _b, _c;
61
+ const factory = (_a = params.indexedDB) !== null && _a !== void 0 ? _a : globalThis.indexedDB;
62
+ if (factory === undefined) {
63
+ return fail('IdbPrivateKeyStorage: no IndexedDB factory available (pass params.indexedDB)');
64
+ }
65
+ return succeed(new IdbPrivateKeyStorage(factory, (_b = params.databaseName) !== null && _b !== void 0 ? _b : DEFAULT_DATABASE_NAME, (_c = params.storeName) !== null && _c !== void 0 ? _c : DEFAULT_STORE_NAME));
66
+ }
67
+ /**
68
+ * Stores `key` under `id`.
69
+ * @param id - Storage handle to write under.
70
+ * @param key - The private `CryptoKey` to persist.
71
+ */
72
+ async store(id, key) {
73
+ if (key.type !== 'private') {
74
+ return fail(`failed to store private key '${id}': expected a private key, got '${key.type}'`);
75
+ }
76
+ return (await this._withStore('readwrite', (store) => this._request(store.put(key, id))))
77
+ .withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)
78
+ .onSuccess(() => succeed(id));
79
+ }
80
+ /**
81
+ * Loads the private key stored under `id`.
82
+ * @param id - Storage handle to look up.
83
+ */
84
+ async load(id) {
85
+ return (await this._getRaw(id))
86
+ .withErrorFormat((msg) => `failed to load private key '${id}': ${msg}`)
87
+ .onSuccess((key) => (key === undefined ? fail(`key not found: '${id}'`) : succeed(key)));
88
+ }
89
+ /**
90
+ * Deletes the entry stored under `id`. Missing ids fail, mirroring the
91
+ * encrypted-file backend. The existence check and the delete run in separate
92
+ * transactions (single-tab assumption).
93
+ * @param id - Storage handle to remove.
94
+ */
95
+ async delete(id) {
96
+ const existingResult = await this._getRaw(id);
97
+ /* c8 ignore next 3 - defensive: the existence-check read only fails on an IndexedDB environment error */
98
+ if (existingResult.isFailure()) {
99
+ return fail(`failed to delete private key '${id}': ${existingResult.message}`);
100
+ }
101
+ if (existingResult.value === undefined) {
102
+ return fail(`key not found: '${id}'`);
103
+ }
104
+ return (await this._withStore('readwrite', (store) => this._request(store.delete(id))))
105
+ .withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)
106
+ .onSuccess(() => succeed(id));
107
+ }
108
+ /**
109
+ * Lists every stored id.
110
+ */
111
+ async list() {
112
+ return (await this._withStore('readonly', (store) => this._request(store.getAllKeys())))
113
+ .withErrorFormat((msg) => `failed to list private keys: ${msg}`)
114
+ .onSuccess((keys) => succeed(keys.map((key) => String(key))));
115
+ }
116
+ async _getRaw(id) {
117
+ return this._withStore('readonly', (store) => this._request(store.get(id)));
118
+ }
119
+ async _openDb() {
120
+ if (this._db !== undefined) {
121
+ return succeed(this._db);
122
+ }
123
+ const firstOpen = await this._open(undefined);
124
+ /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */
125
+ if (firstOpen.isFailure()) {
126
+ return firstOpen;
127
+ }
128
+ // The object store is created in `onupgradeneeded`, which only fires when
129
+ // the version changes. If a database at this name already existed at the
130
+ // current version but without our store (e.g. a different `storeName` than
131
+ // a prior instance used), the store is absent. Re-open at the next version
132
+ // to add it via a version-bump upgrade rather than failing later writes
133
+ // with NotFoundError.
134
+ let db = firstOpen.value;
135
+ if (!db.objectStoreNames.contains(this._storeName)) {
136
+ const nextVersion = db.version + 1;
137
+ db.close();
138
+ const reopen = await this._open(nextVersion);
139
+ /* c8 ignore next 3 - defensive: the version-bump re-open only fails on an IndexedDB environment fault */
140
+ if (reopen.isFailure()) {
141
+ return reopen;
142
+ }
143
+ db = reopen.value;
144
+ }
145
+ this._db = db;
146
+ return succeed(db);
147
+ }
148
+ async _open(version) {
149
+ return captureAsyncResult(() => new Promise((resolve, reject) => {
150
+ const request = version === undefined
151
+ ? this._factory.open(this._databaseName)
152
+ : this._factory.open(this._databaseName, version);
153
+ request.onupgradeneeded = () => {
154
+ // Create our object store if absent. This fires on initial creation
155
+ // and on the version bump used to add a store to an existing
156
+ // database; future schema migrations extend this hook additively.
157
+ if (!request.result.objectStoreNames.contains(this._storeName)) {
158
+ request.result.createObjectStore(this._storeName);
159
+ }
160
+ };
161
+ request.onsuccess = () => {
162
+ const db = request.result;
163
+ // If another connection (e.g. a sibling instance adding a store via
164
+ // version bump, or another tab) needs to upgrade, close this one so
165
+ // we don't block it. Clear the cached handle first so the next
166
+ // operation reopens rather than reusing the now-closed connection.
167
+ // Single-tab use is the documented assumption; this just prevents a
168
+ // deadlock when several instances share a db.
169
+ db.onversionchange = () => {
170
+ if (this._db === db) {
171
+ this._db = undefined;
172
+ }
173
+ db.close();
174
+ };
175
+ resolve(db);
176
+ };
177
+ /* c8 ignore next 2 - defensive: open errors require a corrupted/blocked IndexedDB environment */
178
+ request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB open failed')); };
179
+ }));
180
+ }
181
+ async _withStore(mode, op) {
182
+ const dbResult = await this._openDb();
183
+ /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */
184
+ if (dbResult.isFailure()) {
185
+ return fail(dbResult.message);
186
+ }
187
+ const txnResult = captureResult(() => dbResult.value.transaction(this._storeName, mode));
188
+ /* c8 ignore next 3 - transaction creation only throws if the store was deleted out from under us */
189
+ if (txnResult.isFailure()) {
190
+ return fail(txnResult.message);
191
+ }
192
+ const transaction = txnResult.value;
193
+ // For writes, a successful request is not durable until the transaction
194
+ // commits (oncomplete); it can still abort afterwards. Attach the completion
195
+ // listener BEFORE issuing the request so no event is missed, and wait for it
196
+ // before reporting success. Reads need no such wait.
197
+ const completion = mode === 'readwrite' ? this._awaitCompletion(transaction) : undefined;
198
+ // `op` builds and issues the IDB request synchronously (e.g. `store.put`),
199
+ // which can throw before any Promise is created — DataCloneError on a
200
+ // non-cloneable value, or a transaction-state DOMException. Run it inside a
201
+ // capture boundary so those surface as Failure rather than a rejection,
202
+ // preserving the Result contract.
203
+ const opOuter = await captureAsyncResult(() => op(transaction.objectStore(this._storeName)));
204
+ /* c8 ignore next 3 - defensive: synchronous IDB throws (e.g. DataCloneError) are unreachable for the typed public surface (private CryptoKeys clone; reads pass a string id), but the capture preserves the Result contract */
205
+ if (opOuter.isFailure()) {
206
+ return fail(opOuter.message);
207
+ }
208
+ const opResult = opOuter.value;
209
+ /* c8 ignore next 3 - defensive: the request itself only fails on an IndexedDB environment fault */
210
+ if (opResult.isFailure()) {
211
+ return opResult;
212
+ }
213
+ if (completion !== undefined) {
214
+ const commitResult = await completion;
215
+ /* c8 ignore next 3 - defensive: a transaction abort after a successful request requires an IndexedDB environment fault */
216
+ if (commitResult.isFailure()) {
217
+ return fail(commitResult.message);
218
+ }
219
+ }
220
+ return opResult;
221
+ }
222
+ async _request(request) {
223
+ return captureAsyncResult(() => new Promise((resolve, reject) => {
224
+ request.onsuccess = () => resolve(request.result);
225
+ /* c8 ignore next 2 - defensive: per-request errors require a corrupted IndexedDB environment */
226
+ request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB request failed')); };
227
+ }));
228
+ }
229
+ async _awaitCompletion(transaction) {
230
+ return captureAsyncResult(() => new Promise((resolve, reject) => {
231
+ transaction.oncomplete = () => resolve(true);
232
+ /* c8 ignore next 4 - defensive: abort/error after a successful write requires an IndexedDB environment fault */
233
+ transaction.onabort = () => { var _a; return reject((_a = transaction.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB transaction aborted')); };
234
+ transaction.onerror = () => { var _a; return reject((_a = transaction.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB transaction error')); };
235
+ }));
236
+ }
237
+ }
238
+ //# sourceMappingURL=idbPrivateKeyStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idbPrivateKeyStorage.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/idbPrivateKeyStorage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,IAAI,EAAU,OAAO,EAAE,MAAM,eAAe,CAAC;AA0BzF,MAAM,qBAAqB,GAAW,2BAA2B,CAAC;AAClE,MAAM,kBAAkB,GAAW,aAAa,CAAC;AAEjD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,oBAAoB;IAY/B,YAAoB,OAAmB,EAAE,YAAoB,EAAE,SAAiB;QAXhF;;;WAGG;QACa,2BAAsB,GAAS,IAAI,CAAC;QAQlD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,SAA4C,EAAE;;QACjE,MAAM,OAAO,GAA2B,MAAA,MAAM,CAAC,SAAS,mCAAI,UAAU,CAAC,SAAS,CAAC;QACjF,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,8EAA8E,CAAC,CAAC;QAC9F,CAAC;QACD,OAAO,OAAO,CACZ,IAAI,oBAAoB,CACtB,OAAO,EACP,MAAA,MAAM,CAAC,YAAY,mCAAI,qBA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,CAAC,OAAO,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEO,KAAK,CAAC,QAAQ,CAAI,OAAsB;QAC9C,OAAO,kBAAkB,CACvB,GAAG,EAAE,CACH,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACjC,OAAO,CAAC,SAAS,GAAG,GAAS,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACxD,gGAAgG;YAChG,OAAO,CAAC,OAAO,GAAG,GAAS,EAAE,WAAC,OAAA,MAAM,CAAC,MAAA,OAAO,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC,CAAA,EAAA,CAAC;QAC/F,CAAC,CAAC,CACL,CAAC;IACJ,CAAC;IAEO,KAAK,CAAC,gBAAgB,CAAC,WAA2B;QACxD,OAAO,kBAAkB,CACvB,GAAG,EAAE,CACH,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpC,WAAW,CAAC,UAAU,GAAG,GAAS,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACnD,gHAAgH;YAChH,WAAW,CAAC,OAAO,GAAG,GAAS,EAAE,WAC/B,OAAA,MAAM,CAAC,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC,CAAA,EAAA,CAAC;YAC1E,WAAW,CAAC,OAAO,GAAG,GAAS,EAAE,WAC/B,OAAA,MAAM,CAAC,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC,CAAA,EAAA,CAAC;QAC1E,CAAC,CAAC,CACL,CAAC;IACJ,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureAsyncResult, captureResult, fail, Result, succeed } from '@fgv/ts-utils';\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * Parameters for {@link CryptoUtils.IdbPrivateKeyStorage.create}.\n * @public\n */\nexport interface IIdbPrivateKeyStorageCreateParams {\n /**\n * IndexedDB database name. Default: `'fgv-keystore-private-keys'`.\n */\n readonly databaseName?: string;\n\n /**\n * IndexedDB object-store name. Default: `'privateKeys'`.\n */\n readonly storeName?: string;\n\n /**\n * IndexedDB factory to use. Defaults to `globalThis.indexedDB`. Supplied\n * explicitly in tests (e.g. a `fake-indexeddb` factory) or to target a\n * non-default factory.\n */\n readonly indexedDB?: IDBFactory;\n}\n\nconst DEFAULT_DATABASE_NAME: string = 'fgv-keystore-private-keys';\nconst DEFAULT_STORE_NAME: string = 'privateKeys';\n\n/**\n * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}\n * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via\n * IndexedDB's structured-clone serialization — no JWK round-trip — so it works\n * with non-extractable keys.\n *\n * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by\n * reference (structured clone) rather than exported, the keystore may generate\n * `extractable: false` keys for maximum security on browsers that support it.\n *\n * The database is opened lazily on first use and cached. Each operation runs in\n * its own transaction, relying on IndexedDB's default serialization. Multi-tab\n * concurrency is a known limitation: two tabs writing the same id can race; this\n * implementation targets single-tab use.\n *\n * @public\n */\nexport class IdbPrivateKeyStorage implements CryptoUtils.KeyStore.IPrivateKeyStorage {\n /**\n * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable\n * keys are supported.\n */\n public readonly supportsNonExtractable: true = true;\n\n private readonly _factory: IDBFactory;\n private readonly _databaseName: string;\n private readonly _storeName: string;\n private _db: IDBDatabase | undefined;\n\n private constructor(factory: IDBFactory, databaseName: string, storeName: string) {\n this._factory = factory;\n this._databaseName = databaseName;\n this._storeName = storeName;\n }\n\n /**\n * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.\n * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.\n * @returns `Success` with the new instance, or `Failure` if no IndexedDB\n * factory is available.\n */\n public static create(params: IIdbPrivateKeyStorageCreateParams = {}): Result<IdbPrivateKeyStorage> {\n const factory: IDBFactory | undefined = params.indexedDB ?? globalThis.indexedDB;\n if (factory === undefined) {\n return fail('IdbPrivateKeyStorage: no IndexedDB factory available (pass params.indexedDB)');\n }\n return succeed(\n new IdbPrivateKeyStorage(\n factory,\n params.databaseName ?? DEFAULT_DATABASE_NAME,\n params.storeName ?? DEFAULT_STORE_NAME\n )\n );\n }\n\n /**\n * Stores `key` under `id`.\n * @param id - Storage handle to write under.\n * @param key - The private `CryptoKey` to persist.\n */\n public async store(id: string, key: CryptoKey): Promise<Result<string>> {\n if (key.type !== 'private') {\n return fail(`failed to store private key '${id}': expected a private key, got '${key.type}'`);\n }\n return (await this._withStore('readwrite', (store) => this._request(store.put(key, id))))\n .withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)\n .onSuccess(() => succeed(id));\n }\n\n /**\n * Loads the private key stored under `id`.\n * @param id - Storage handle to look up.\n */\n public async load(id: string): Promise<Result<CryptoKey>> {\n return (await this._getRaw(id))\n .withErrorFormat((msg) => `failed to load private key '${id}': ${msg}`)\n .onSuccess((key) => (key === undefined ? fail(`key not found: '${id}'`) : succeed(key)));\n }\n\n /**\n * Deletes the entry stored under `id`. Missing ids fail, mirroring the\n * encrypted-file backend. The existence check and the delete run in separate\n * transactions (single-tab assumption).\n * @param id - Storage handle to remove.\n */\n public async delete(id: string): Promise<Result<string>> {\n const existingResult = await this._getRaw(id);\n /* c8 ignore next 3 - defensive: the existence-check read only fails on an IndexedDB environment error */\n if (existingResult.isFailure()) {\n return fail(`failed to delete private key '${id}': ${existingResult.message}`);\n }\n if (existingResult.value === undefined) {\n return fail(`key not found: '${id}'`);\n }\n return (await this._withStore('readwrite', (store) => this._request(store.delete(id))))\n .withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)\n .onSuccess(() => succeed(id));\n }\n\n /**\n * Lists every stored id.\n */\n public async list(): Promise<Result<readonly string[]>> {\n return (await this._withStore('readonly', (store) => this._request<IDBValidKey[]>(store.getAllKeys())))\n .withErrorFormat((msg) => `failed to list private keys: ${msg}`)\n .onSuccess((keys) => succeed(keys.map((key) => String(key))));\n }\n\n private async _getRaw(id: string): Promise<Result<CryptoKey | undefined>> {\n return this._withStore('readonly', (store) => this._request<CryptoKey | undefined>(store.get(id)));\n }\n\n private async _openDb(): Promise<Result<IDBDatabase>> {\n if (this._db !== undefined) {\n return succeed(this._db);\n }\n const firstOpen = await this._open(undefined);\n /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */\n if (firstOpen.isFailure()) {\n return firstOpen;\n }\n // The object store is created in `onupgradeneeded`, which only fires when\n // the version changes. If a database at this name already existed at the\n // current version but without our store (e.g. a different `storeName` than\n // a prior instance used), the store is absent. Re-open at the next version\n // to add it via a version-bump upgrade rather than failing later writes\n // with NotFoundError.\n let db = firstOpen.value;\n if (!db.objectStoreNames.contains(this._storeName)) {\n const nextVersion = db.version + 1;\n db.close();\n const reopen = await this._open(nextVersion);\n /* c8 ignore next 3 - defensive: the version-bump re-open only fails on an IndexedDB environment fault */\n if (reopen.isFailure()) {\n return reopen;\n }\n db = reopen.value;\n }\n this._db = db;\n return succeed(db);\n }\n\n private async _open(version: number | undefined): Promise<Result<IDBDatabase>> {\n return captureAsyncResult<IDBDatabase>(\n () =>\n new Promise<IDBDatabase>((resolve, reject) => {\n const request =\n version === undefined\n ? this._factory.open(this._databaseName)\n : this._factory.open(this._databaseName, version);\n request.onupgradeneeded = (): void => {\n // Create our object store if absent. This fires on initial creation\n // and on the version bump used to add a store to an existing\n // database; future schema migrations extend this hook additively.\n if (!request.result.objectStoreNames.contains(this._storeName)) {\n request.result.createObjectStore(this._storeName);\n }\n };\n request.onsuccess = (): void => {\n const db = request.result;\n // If another connection (e.g. a sibling instance adding a store via\n // version bump, or another tab) needs to upgrade, close this one so\n // we don't block it. Clear the cached handle first so the next\n // operation reopens rather than reusing the now-closed connection.\n // Single-tab use is the documented assumption; this just prevents a\n // deadlock when several instances share a db.\n db.onversionchange = (): void => {\n if (this._db === db) {\n this._db = undefined;\n }\n db.close();\n };\n resolve(db);\n };\n /* c8 ignore next 2 - defensive: open errors require a corrupted/blocked IndexedDB environment */\n request.onerror = (): void => reject(request.error ?? new Error('IndexedDB open failed'));\n })\n );\n }\n\n private async _withStore<T>(\n mode: IDBTransactionMode,\n op: (store: IDBObjectStore) => Promise<Result<T>>\n ): Promise<Result<T>> {\n const dbResult = await this._openDb();\n /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */\n if (dbResult.isFailure()) {\n return fail(dbResult.message);\n }\n const txnResult = captureResult(() => dbResult.value.transaction(this._storeName, mode));\n /* c8 ignore next 3 - transaction creation only throws if the store was deleted out from under us */\n if (txnResult.isFailure()) {\n return fail(txnResult.message);\n }\n const transaction = txnResult.value;\n\n // For writes, a successful request is not durable until the transaction\n // commits (oncomplete); it can still abort afterwards. Attach the completion\n // listener BEFORE issuing the request so no event is missed, and wait for it\n // before reporting success. Reads need no such wait.\n const completion = mode === 'readwrite' ? this._awaitCompletion(transaction) : undefined;\n\n // `op` builds and issues the IDB request synchronously (e.g. `store.put`),\n // which can throw before any Promise is created — DataCloneError on a\n // non-cloneable value, or a transaction-state DOMException. Run it inside a\n // capture boundary so those surface as Failure rather than a rejection,\n // preserving the Result contract.\n const opOuter = await captureAsyncResult(() => op(transaction.objectStore(this._storeName)));\n /* c8 ignore next 3 - defensive: synchronous IDB throws (e.g. DataCloneError) are unreachable for the typed public surface (private CryptoKeys clone; reads pass a string id), but the capture preserves the Result contract */\n if (opOuter.isFailure()) {\n return fail(opOuter.message);\n }\n const opResult = opOuter.value;\n /* c8 ignore next 3 - defensive: the request itself only fails on an IndexedDB environment fault */\n if (opResult.isFailure()) {\n return opResult;\n }\n if (completion !== undefined) {\n const commitResult = await completion;\n /* c8 ignore next 3 - defensive: a transaction abort after a successful request requires an IndexedDB environment fault */\n if (commitResult.isFailure()) {\n return fail(commitResult.message);\n }\n }\n return opResult;\n }\n\n private async _request<T>(request: IDBRequest<T>): Promise<Result<T>> {\n return captureAsyncResult<T>(\n () =>\n new Promise<T>((resolve, reject) => {\n request.onsuccess = (): void => resolve(request.result);\n /* c8 ignore next 2 - defensive: per-request errors require a corrupted IndexedDB environment */\n request.onerror = (): void => reject(request.error ?? new Error('IndexedDB request failed'));\n })\n );\n }\n\n private async _awaitCompletion(transaction: IDBTransaction): Promise<Result<true>> {\n return captureAsyncResult<true>(\n () =>\n new Promise<true>((resolve, reject) => {\n transaction.oncomplete = (): void => resolve(true);\n /* c8 ignore next 4 - defensive: abort/error after a successful write requires an IndexedDB environment fault */\n transaction.onabort = (): void =>\n reject(transaction.error ?? new Error('IndexedDB transaction aborted'));\n transaction.onerror = (): void =>\n reject(transaction.error ?? new Error('IndexedDB transaction error'));\n })\n );\n }\n}\n"]}
@@ -25,6 +25,7 @@
25
25
  */
26
26
  export * from './browserHashProvider';
27
27
  export * from './browserCryptoProvider';
28
+ export * from './idbPrivateKeyStorage';
28
29
  // HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;
29
30
  // re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.
30
31
  // We import from the top-level namespace to avoid relying on package.json exports path
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;GAGG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AAExC,sFAAsF;AACtF,6FAA6F;AAC7F,uFAAuF;AACvF,mEAAmE;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAoC,WAAW,CAAC,YAAY,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible cryptographic utilities using the Web Crypto API.\n * @packageDocumentation\n */\n\nexport * from './browserHashProvider';\nexport * from './browserCryptoProvider';\n\n// HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;\n// re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.\n// We import from the top-level namespace to avoid relying on package.json exports path\n// resolution (which requires moduleResolution: node16 or bundler).\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * HPKE base mode (RFC 9180) — `DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM`.\n * Re-exported from `@fgv/ts-extras` for browser consumers.\n * @see {@link CryptoUtils.HpkeProvider}\n * @public\n */\nexport const HpkeProvider: typeof CryptoUtils.HpkeProvider = CryptoUtils.HpkeProvider;\n\n/**\n * Output of `HpkeProvider.sealBase`. Re-exported from `@fgv/ts-extras`.\n * @public\n */\nexport type IHpkeSealResult = CryptoUtils.IHpkeSealResult;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;GAGG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AAEvC,sFAAsF;AACtF,6FAA6F;AAC7F,uFAAuF;AACvF,mEAAmE;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAoC,WAAW,CAAC,YAAY,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible cryptographic utilities using the Web Crypto API.\n * @packageDocumentation\n */\n\nexport * from './browserHashProvider';\nexport * from './browserCryptoProvider';\nexport * from './idbPrivateKeyStorage';\n\n// HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;\n// re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.\n// We import from the top-level namespace to avoid relying on package.json exports path\n// resolution (which requires moduleResolution: node16 or bundler).\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * HPKE base mode (RFC 9180) — `DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM`.\n * Re-exported from `@fgv/ts-extras` for browser consumers.\n * @see {@link CryptoUtils.HpkeProvider}\n * @public\n */\nexport const HpkeProvider: typeof CryptoUtils.HpkeProvider = CryptoUtils.HpkeProvider;\n\n/**\n * Output of `HpkeProvider.sealBase`. Re-exported from `@fgv/ts-extras`.\n * @public\n */\nexport type IHpkeSealResult = CryptoUtils.IHpkeSealResult;\n"]}
@@ -230,7 +230,9 @@ declare namespace CryptoUtils {
230
230
  IHpkeSealResult,
231
231
  BrowserHashProvider,
232
232
  createBrowserCryptoProvider,
233
- BrowserCryptoProvider
233
+ BrowserCryptoProvider,
234
+ IIdbPrivateKeyStorageCreateParams,
235
+ IdbPrivateKeyStorage
234
236
  }
235
237
  }
236
238
  export { CryptoUtils }
@@ -868,6 +870,71 @@ declare function extractFileListMetadata(fileList: FileList): Array<IFileMetadat
868
870
  private static _requestWithParams;
869
871
  }
870
872
 
873
+ /**
874
+ * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}
875
+ * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via
876
+ * IndexedDB's structured-clone serialization — no JWK round-trip — so it works
877
+ * with non-extractable keys.
878
+ *
879
+ * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by
880
+ * reference (structured clone) rather than exported, the keystore may generate
881
+ * `extractable: false` keys for maximum security on browsers that support it.
882
+ *
883
+ * The database is opened lazily on first use and cached. Each operation runs in
884
+ * its own transaction, relying on IndexedDB's default serialization. Multi-tab
885
+ * concurrency is a known limitation: two tabs writing the same id can race; this
886
+ * implementation targets single-tab use.
887
+ *
888
+ * @public
889
+ */
890
+ declare class IdbPrivateKeyStorage implements CryptoUtils_2.KeyStore.IPrivateKeyStorage {
891
+ /**
892
+ * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable
893
+ * keys are supported.
894
+ */
895
+ readonly supportsNonExtractable: true;
896
+ private readonly _factory;
897
+ private readonly _databaseName;
898
+ private readonly _storeName;
899
+ private _db;
900
+ private constructor();
901
+ /**
902
+ * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.
903
+ * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.
904
+ * @returns `Success` with the new instance, or `Failure` if no IndexedDB
905
+ * factory is available.
906
+ */
907
+ static create(params?: IIdbPrivateKeyStorageCreateParams): Result<IdbPrivateKeyStorage>;
908
+ /**
909
+ * Stores `key` under `id`.
910
+ * @param id - Storage handle to write under.
911
+ * @param key - The private `CryptoKey` to persist.
912
+ */
913
+ store(id: string, key: CryptoKey): Promise<Result<string>>;
914
+ /**
915
+ * Loads the private key stored under `id`.
916
+ * @param id - Storage handle to look up.
917
+ */
918
+ load(id: string): Promise<Result<CryptoKey>>;
919
+ /**
920
+ * Deletes the entry stored under `id`. Missing ids fail, mirroring the
921
+ * encrypted-file backend. The existence check and the delete run in separate
922
+ * transactions (single-tab assumption).
923
+ * @param id - Storage handle to remove.
924
+ */
925
+ delete(id: string): Promise<Result<string>>;
926
+ /**
927
+ * Lists every stored id.
928
+ */
929
+ list(): Promise<Result<readonly string[]>>;
930
+ private _getRaw;
931
+ private _openDb;
932
+ private _open;
933
+ private _withStore;
934
+ private _request;
935
+ private _awaitCompletion;
936
+ }
937
+
871
938
  /**
872
939
  * Tree initializer for File System Access API directory handles.
873
940
  * @public
@@ -964,6 +1031,27 @@ declare function extractFileListMetadata(fileList: FileList): Array<IFileMetadat
964
1031
  readonly logger?: Logging.LogReporter<unknown>;
965
1032
  }
966
1033
 
1034
+ /**
1035
+ * Parameters for {@link CryptoUtils.IdbPrivateKeyStorage.create}.
1036
+ * @public
1037
+ */
1038
+ declare interface IIdbPrivateKeyStorageCreateParams {
1039
+ /**
1040
+ * IndexedDB database name. Default: `'fgv-keystore-private-keys'`.
1041
+ */
1042
+ readonly databaseName?: string;
1043
+ /**
1044
+ * IndexedDB object-store name. Default: `'privateKeys'`.
1045
+ */
1046
+ readonly storeName?: string;
1047
+ /**
1048
+ * IndexedDB factory to use. Defaults to `globalThis.indexedDB`. Supplied
1049
+ * explicitly in tests (e.g. a `fake-indexeddb` factory) or to target a
1050
+ * non-default factory.
1051
+ */
1052
+ readonly indexedDB?: IDBFactory;
1053
+ }
1054
+
967
1055
  /**
968
1056
  * Configuration for LocalStorageTreeAccessors.
969
1057
  * @public
@@ -0,0 +1,87 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { CryptoUtils } from '@fgv/ts-extras';
3
+ /**
4
+ * Parameters for {@link CryptoUtils.IdbPrivateKeyStorage.create}.
5
+ * @public
6
+ */
7
+ export interface IIdbPrivateKeyStorageCreateParams {
8
+ /**
9
+ * IndexedDB database name. Default: `'fgv-keystore-private-keys'`.
10
+ */
11
+ readonly databaseName?: string;
12
+ /**
13
+ * IndexedDB object-store name. Default: `'privateKeys'`.
14
+ */
15
+ readonly storeName?: string;
16
+ /**
17
+ * IndexedDB factory to use. Defaults to `globalThis.indexedDB`. Supplied
18
+ * explicitly in tests (e.g. a `fake-indexeddb` factory) or to target a
19
+ * non-default factory.
20
+ */
21
+ readonly indexedDB?: IDBFactory;
22
+ }
23
+ /**
24
+ * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}
25
+ * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via
26
+ * IndexedDB's structured-clone serialization — no JWK round-trip — so it works
27
+ * with non-extractable keys.
28
+ *
29
+ * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by
30
+ * reference (structured clone) rather than exported, the keystore may generate
31
+ * `extractable: false` keys for maximum security on browsers that support it.
32
+ *
33
+ * The database is opened lazily on first use and cached. Each operation runs in
34
+ * its own transaction, relying on IndexedDB's default serialization. Multi-tab
35
+ * concurrency is a known limitation: two tabs writing the same id can race; this
36
+ * implementation targets single-tab use.
37
+ *
38
+ * @public
39
+ */
40
+ export declare class IdbPrivateKeyStorage implements CryptoUtils.KeyStore.IPrivateKeyStorage {
41
+ /**
42
+ * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable
43
+ * keys are supported.
44
+ */
45
+ readonly supportsNonExtractable: true;
46
+ private readonly _factory;
47
+ private readonly _databaseName;
48
+ private readonly _storeName;
49
+ private _db;
50
+ private constructor();
51
+ /**
52
+ * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.
53
+ * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.
54
+ * @returns `Success` with the new instance, or `Failure` if no IndexedDB
55
+ * factory is available.
56
+ */
57
+ static create(params?: IIdbPrivateKeyStorageCreateParams): Result<IdbPrivateKeyStorage>;
58
+ /**
59
+ * Stores `key` under `id`.
60
+ * @param id - Storage handle to write under.
61
+ * @param key - The private `CryptoKey` to persist.
62
+ */
63
+ store(id: string, key: CryptoKey): Promise<Result<string>>;
64
+ /**
65
+ * Loads the private key stored under `id`.
66
+ * @param id - Storage handle to look up.
67
+ */
68
+ load(id: string): Promise<Result<CryptoKey>>;
69
+ /**
70
+ * Deletes the entry stored under `id`. Missing ids fail, mirroring the
71
+ * encrypted-file backend. The existence check and the delete run in separate
72
+ * transactions (single-tab assumption).
73
+ * @param id - Storage handle to remove.
74
+ */
75
+ delete(id: string): Promise<Result<string>>;
76
+ /**
77
+ * Lists every stored id.
78
+ */
79
+ list(): Promise<Result<readonly string[]>>;
80
+ private _getRaw;
81
+ private _openDb;
82
+ private _open;
83
+ private _withStore;
84
+ private _request;
85
+ private _awaitCompletion;
86
+ }
87
+ //# sourceMappingURL=idbPrivateKeyStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idbPrivateKeyStorage.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/idbPrivateKeyStorage.ts"],"names":[],"mappings":"AAsBA,OAAO,EAA2C,MAAM,EAAW,MAAM,eAAe,CAAC;AACzF,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;GAGG;AACH,MAAM,WAAW,iCAAiC;IAChD;;OAEG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAE/B;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,UAAU,CAAC;CACjC;AAKD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,oBAAqB,YAAW,WAAW,CAAC,QAAQ,CAAC,kBAAkB;IAClF;;;OAGG;IACH,SAAgB,sBAAsB,EAAE,IAAI,CAAQ;IAEpD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAa;IACtC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,GAAG,CAA0B;IAErC,OAAO;IAMP;;;;;OAKG;WACW,MAAM,CAAC,MAAM,GAAE,iCAAsC,GAAG,MAAM,CAAC,oBAAoB,CAAC;IAclG;;;;OAIG;IACU,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IASvE;;;OAGG;IACU,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAMzD;;;;;OAKG;IACU,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAcxD;;OAEG;IACU,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;YAMzC,OAAO;YAIP,OAAO;YA8BP,KAAK;YAsCL,UAAU;YA+CV,QAAQ;YAWR,gBAAgB;CAa/B"}
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2026 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.IdbPrivateKeyStorage = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ const DEFAULT_DATABASE_NAME = 'fgv-keystore-private-keys';
27
+ const DEFAULT_STORE_NAME = 'privateKeys';
28
+ /**
29
+ * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}
30
+ * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via
31
+ * IndexedDB's structured-clone serialization — no JWK round-trip — so it works
32
+ * with non-extractable keys.
33
+ *
34
+ * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by
35
+ * reference (structured clone) rather than exported, the keystore may generate
36
+ * `extractable: false` keys for maximum security on browsers that support it.
37
+ *
38
+ * The database is opened lazily on first use and cached. Each operation runs in
39
+ * its own transaction, relying on IndexedDB's default serialization. Multi-tab
40
+ * concurrency is a known limitation: two tabs writing the same id can race; this
41
+ * implementation targets single-tab use.
42
+ *
43
+ * @public
44
+ */
45
+ class IdbPrivateKeyStorage {
46
+ constructor(factory, databaseName, storeName) {
47
+ /**
48
+ * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable
49
+ * keys are supported.
50
+ */
51
+ this.supportsNonExtractable = true;
52
+ this._factory = factory;
53
+ this._databaseName = databaseName;
54
+ this._storeName = storeName;
55
+ }
56
+ /**
57
+ * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.
58
+ * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.
59
+ * @returns `Success` with the new instance, or `Failure` if no IndexedDB
60
+ * factory is available.
61
+ */
62
+ static create(params = {}) {
63
+ var _a, _b, _c;
64
+ const factory = (_a = params.indexedDB) !== null && _a !== void 0 ? _a : globalThis.indexedDB;
65
+ if (factory === undefined) {
66
+ return (0, ts_utils_1.fail)('IdbPrivateKeyStorage: no IndexedDB factory available (pass params.indexedDB)');
67
+ }
68
+ return (0, ts_utils_1.succeed)(new IdbPrivateKeyStorage(factory, (_b = params.databaseName) !== null && _b !== void 0 ? _b : DEFAULT_DATABASE_NAME, (_c = params.storeName) !== null && _c !== void 0 ? _c : DEFAULT_STORE_NAME));
69
+ }
70
+ /**
71
+ * Stores `key` under `id`.
72
+ * @param id - Storage handle to write under.
73
+ * @param key - The private `CryptoKey` to persist.
74
+ */
75
+ async store(id, key) {
76
+ if (key.type !== 'private') {
77
+ return (0, ts_utils_1.fail)(`failed to store private key '${id}': expected a private key, got '${key.type}'`);
78
+ }
79
+ return (await this._withStore('readwrite', (store) => this._request(store.put(key, id))))
80
+ .withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)
81
+ .onSuccess(() => (0, ts_utils_1.succeed)(id));
82
+ }
83
+ /**
84
+ * Loads the private key stored under `id`.
85
+ * @param id - Storage handle to look up.
86
+ */
87
+ async load(id) {
88
+ return (await this._getRaw(id))
89
+ .withErrorFormat((msg) => `failed to load private key '${id}': ${msg}`)
90
+ .onSuccess((key) => (key === undefined ? (0, ts_utils_1.fail)(`key not found: '${id}'`) : (0, ts_utils_1.succeed)(key)));
91
+ }
92
+ /**
93
+ * Deletes the entry stored under `id`. Missing ids fail, mirroring the
94
+ * encrypted-file backend. The existence check and the delete run in separate
95
+ * transactions (single-tab assumption).
96
+ * @param id - Storage handle to remove.
97
+ */
98
+ async delete(id) {
99
+ const existingResult = await this._getRaw(id);
100
+ /* c8 ignore next 3 - defensive: the existence-check read only fails on an IndexedDB environment error */
101
+ if (existingResult.isFailure()) {
102
+ return (0, ts_utils_1.fail)(`failed to delete private key '${id}': ${existingResult.message}`);
103
+ }
104
+ if (existingResult.value === undefined) {
105
+ return (0, ts_utils_1.fail)(`key not found: '${id}'`);
106
+ }
107
+ return (await this._withStore('readwrite', (store) => this._request(store.delete(id))))
108
+ .withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)
109
+ .onSuccess(() => (0, ts_utils_1.succeed)(id));
110
+ }
111
+ /**
112
+ * Lists every stored id.
113
+ */
114
+ async list() {
115
+ return (await this._withStore('readonly', (store) => this._request(store.getAllKeys())))
116
+ .withErrorFormat((msg) => `failed to list private keys: ${msg}`)
117
+ .onSuccess((keys) => (0, ts_utils_1.succeed)(keys.map((key) => String(key))));
118
+ }
119
+ async _getRaw(id) {
120
+ return this._withStore('readonly', (store) => this._request(store.get(id)));
121
+ }
122
+ async _openDb() {
123
+ if (this._db !== undefined) {
124
+ return (0, ts_utils_1.succeed)(this._db);
125
+ }
126
+ const firstOpen = await this._open(undefined);
127
+ /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */
128
+ if (firstOpen.isFailure()) {
129
+ return firstOpen;
130
+ }
131
+ // The object store is created in `onupgradeneeded`, which only fires when
132
+ // the version changes. If a database at this name already existed at the
133
+ // current version but without our store (e.g. a different `storeName` than
134
+ // a prior instance used), the store is absent. Re-open at the next version
135
+ // to add it via a version-bump upgrade rather than failing later writes
136
+ // with NotFoundError.
137
+ let db = firstOpen.value;
138
+ if (!db.objectStoreNames.contains(this._storeName)) {
139
+ const nextVersion = db.version + 1;
140
+ db.close();
141
+ const reopen = await this._open(nextVersion);
142
+ /* c8 ignore next 3 - defensive: the version-bump re-open only fails on an IndexedDB environment fault */
143
+ if (reopen.isFailure()) {
144
+ return reopen;
145
+ }
146
+ db = reopen.value;
147
+ }
148
+ this._db = db;
149
+ return (0, ts_utils_1.succeed)(db);
150
+ }
151
+ async _open(version) {
152
+ return (0, ts_utils_1.captureAsyncResult)(() => new Promise((resolve, reject) => {
153
+ const request = version === undefined
154
+ ? this._factory.open(this._databaseName)
155
+ : this._factory.open(this._databaseName, version);
156
+ request.onupgradeneeded = () => {
157
+ // Create our object store if absent. This fires on initial creation
158
+ // and on the version bump used to add a store to an existing
159
+ // database; future schema migrations extend this hook additively.
160
+ if (!request.result.objectStoreNames.contains(this._storeName)) {
161
+ request.result.createObjectStore(this._storeName);
162
+ }
163
+ };
164
+ request.onsuccess = () => {
165
+ const db = request.result;
166
+ // If another connection (e.g. a sibling instance adding a store via
167
+ // version bump, or another tab) needs to upgrade, close this one so
168
+ // we don't block it. Clear the cached handle first so the next
169
+ // operation reopens rather than reusing the now-closed connection.
170
+ // Single-tab use is the documented assumption; this just prevents a
171
+ // deadlock when several instances share a db.
172
+ db.onversionchange = () => {
173
+ if (this._db === db) {
174
+ this._db = undefined;
175
+ }
176
+ db.close();
177
+ };
178
+ resolve(db);
179
+ };
180
+ /* c8 ignore next 2 - defensive: open errors require a corrupted/blocked IndexedDB environment */
181
+ request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB open failed')); };
182
+ }));
183
+ }
184
+ async _withStore(mode, op) {
185
+ const dbResult = await this._openDb();
186
+ /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */
187
+ if (dbResult.isFailure()) {
188
+ return (0, ts_utils_1.fail)(dbResult.message);
189
+ }
190
+ const txnResult = (0, ts_utils_1.captureResult)(() => dbResult.value.transaction(this._storeName, mode));
191
+ /* c8 ignore next 3 - transaction creation only throws if the store was deleted out from under us */
192
+ if (txnResult.isFailure()) {
193
+ return (0, ts_utils_1.fail)(txnResult.message);
194
+ }
195
+ const transaction = txnResult.value;
196
+ // For writes, a successful request is not durable until the transaction
197
+ // commits (oncomplete); it can still abort afterwards. Attach the completion
198
+ // listener BEFORE issuing the request so no event is missed, and wait for it
199
+ // before reporting success. Reads need no such wait.
200
+ const completion = mode === 'readwrite' ? this._awaitCompletion(transaction) : undefined;
201
+ // `op` builds and issues the IDB request synchronously (e.g. `store.put`),
202
+ // which can throw before any Promise is created — DataCloneError on a
203
+ // non-cloneable value, or a transaction-state DOMException. Run it inside a
204
+ // capture boundary so those surface as Failure rather than a rejection,
205
+ // preserving the Result contract.
206
+ const opOuter = await (0, ts_utils_1.captureAsyncResult)(() => op(transaction.objectStore(this._storeName)));
207
+ /* c8 ignore next 3 - defensive: synchronous IDB throws (e.g. DataCloneError) are unreachable for the typed public surface (private CryptoKeys clone; reads pass a string id), but the capture preserves the Result contract */
208
+ if (opOuter.isFailure()) {
209
+ return (0, ts_utils_1.fail)(opOuter.message);
210
+ }
211
+ const opResult = opOuter.value;
212
+ /* c8 ignore next 3 - defensive: the request itself only fails on an IndexedDB environment fault */
213
+ if (opResult.isFailure()) {
214
+ return opResult;
215
+ }
216
+ if (completion !== undefined) {
217
+ const commitResult = await completion;
218
+ /* c8 ignore next 3 - defensive: a transaction abort after a successful request requires an IndexedDB environment fault */
219
+ if (commitResult.isFailure()) {
220
+ return (0, ts_utils_1.fail)(commitResult.message);
221
+ }
222
+ }
223
+ return opResult;
224
+ }
225
+ async _request(request) {
226
+ return (0, ts_utils_1.captureAsyncResult)(() => new Promise((resolve, reject) => {
227
+ request.onsuccess = () => resolve(request.result);
228
+ /* c8 ignore next 2 - defensive: per-request errors require a corrupted IndexedDB environment */
229
+ request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB request failed')); };
230
+ }));
231
+ }
232
+ async _awaitCompletion(transaction) {
233
+ return (0, ts_utils_1.captureAsyncResult)(() => new Promise((resolve, reject) => {
234
+ transaction.oncomplete = () => resolve(true);
235
+ /* c8 ignore next 4 - defensive: abort/error after a successful write requires an IndexedDB environment fault */
236
+ transaction.onabort = () => { var _a; return reject((_a = transaction.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB transaction aborted')); };
237
+ transaction.onerror = () => { var _a; return reject((_a = transaction.error) !== null && _a !== void 0 ? _a : new Error('IndexedDB transaction error')); };
238
+ }));
239
+ }
240
+ }
241
+ exports.IdbPrivateKeyStorage = IdbPrivateKeyStorage;
242
+ //# sourceMappingURL=idbPrivateKeyStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idbPrivateKeyStorage.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/idbPrivateKeyStorage.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyF;AA0BzF,MAAM,qBAAqB,GAAW,2BAA2B,CAAC;AAClE,MAAM,kBAAkB,GAAW,aAAa,CAAC;AAEjD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAa,oBAAoB;IAY/B,YAAoB,OAAmB,EAAE,YAAoB,EAAE,SAAiB;QAXhF;;;WAGG;QACa,2BAAsB,GAAS,IAAI,CAAC;QAQlD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,SAA4C,EAAE;;QACjE,MAAM,OAAO,GAA2B,MAAA,MAAM,CAAC,SAAS,mCAAI,UAAU,CAAC,SAAS,CAAC;QACjF,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAA,eAAI,EAAC,8EAA8E,CAAC,CAAC;QAC9F,CAAC;QACD,OAAO,IAAA,kBAAO,EACZ,IAAI,oBAAoB,CACtB,OAAO,EACP,MAAA,MAAM,CAAC,YAAY,mCAAI,qBAAqB,EAC5C,MAAA,MAAM,CAAC,SAAS,mCAAI,kBAAkB,CACvC,CACF,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,KAAK,CAAC,EAAU,EAAE,GAAc;QAC3C,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC3B,OAAO,IAAA,eAAI,EAAC,gCAAgC,EAAE,mCAAmC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC;QAChG,CAAC;QACD,OAAO,CAAC,MAAM,IAA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EAAE,EAAE;YACjC,OAAO,CAAC,SAAS,GAAG,GAAS,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACxD,gGAAgG;YAChG,OAAO,CAAC,OAAO,GAAG,GAAS,EAAE,WAAC,OAAA,MAAM,CAAC,MAAA,OAAO,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC,CAAA,EAAA,CAAC;QAC/F,CAAC,CAAC,CACL,CAAC;IACJ,CAAC;IAEO,KAAK,CAAC,gBAAgB,CAAC,WAA2B;QACxD,OAAO,IAAA,6BAAkB,EACvB,GAAG,EAAE,CACH,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpC,WAAW,CAAC,UAAU,GAAG,GAAS,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACnD,gHAAgH;YAChH,WAAW,CAAC,OAAO,GAAG,GAAS,EAAE,WAC/B,OAAA,MAAM,CAAC,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC,CAAA,EAAA,CAAC;YAC1E,WAAW,CAAC,OAAO,GAAG,GAAS,EAAE,WAC/B,OAAA,MAAM,CAAC,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC,CAAA,EAAA,CAAC;QAC1E,CAAC,CAAC,CACL,CAAC;IACJ,CAAC;CACF;AA1OD,oDA0OC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureAsyncResult, captureResult, fail, Result, succeed } from '@fgv/ts-utils';\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * Parameters for {@link CryptoUtils.IdbPrivateKeyStorage.create}.\n * @public\n */\nexport interface IIdbPrivateKeyStorageCreateParams {\n /**\n * IndexedDB database name. Default: `'fgv-keystore-private-keys'`.\n */\n readonly databaseName?: string;\n\n /**\n * IndexedDB object-store name. Default: `'privateKeys'`.\n */\n readonly storeName?: string;\n\n /**\n * IndexedDB factory to use. Defaults to `globalThis.indexedDB`. Supplied\n * explicitly in tests (e.g. a `fake-indexeddb` factory) or to target a\n * non-default factory.\n */\n readonly indexedDB?: IDBFactory;\n}\n\nconst DEFAULT_DATABASE_NAME: string = 'fgv-keystore-private-keys';\nconst DEFAULT_STORE_NAME: string = 'privateKeys';\n\n/**\n * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}\n * implementation backed by IndexedDB. Stores `CryptoKey` objects directly via\n * IndexedDB's structured-clone serialization — no JWK round-trip — so it works\n * with non-extractable keys.\n *\n * `supportsNonExtractable` is `true`: because the `CryptoKey` is stored by\n * reference (structured clone) rather than exported, the keystore may generate\n * `extractable: false` keys for maximum security on browsers that support it.\n *\n * The database is opened lazily on first use and cached. Each operation runs in\n * its own transaction, relying on IndexedDB's default serialization. Multi-tab\n * concurrency is a known limitation: two tabs writing the same id can race; this\n * implementation targets single-tab use.\n *\n * @public\n */\nexport class IdbPrivateKeyStorage implements CryptoUtils.KeyStore.IPrivateKeyStorage {\n /**\n * `true` — IndexedDB stores `CryptoKey` objects directly, so non-extractable\n * keys are supported.\n */\n public readonly supportsNonExtractable: true = true;\n\n private readonly _factory: IDBFactory;\n private readonly _databaseName: string;\n private readonly _storeName: string;\n private _db: IDBDatabase | undefined;\n\n private constructor(factory: IDBFactory, databaseName: string, storeName: string) {\n this._factory = factory;\n this._databaseName = databaseName;\n this._storeName = storeName;\n }\n\n /**\n * Creates a new {@link CryptoUtils.IdbPrivateKeyStorage}.\n * @param params - Optional {@link CryptoUtils.IIdbPrivateKeyStorageCreateParams}.\n * @returns `Success` with the new instance, or `Failure` if no IndexedDB\n * factory is available.\n */\n public static create(params: IIdbPrivateKeyStorageCreateParams = {}): Result<IdbPrivateKeyStorage> {\n const factory: IDBFactory | undefined = params.indexedDB ?? globalThis.indexedDB;\n if (factory === undefined) {\n return fail('IdbPrivateKeyStorage: no IndexedDB factory available (pass params.indexedDB)');\n }\n return succeed(\n new IdbPrivateKeyStorage(\n factory,\n params.databaseName ?? DEFAULT_DATABASE_NAME,\n params.storeName ?? DEFAULT_STORE_NAME\n )\n );\n }\n\n /**\n * Stores `key` under `id`.\n * @param id - Storage handle to write under.\n * @param key - The private `CryptoKey` to persist.\n */\n public async store(id: string, key: CryptoKey): Promise<Result<string>> {\n if (key.type !== 'private') {\n return fail(`failed to store private key '${id}': expected a private key, got '${key.type}'`);\n }\n return (await this._withStore('readwrite', (store) => this._request(store.put(key, id))))\n .withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)\n .onSuccess(() => succeed(id));\n }\n\n /**\n * Loads the private key stored under `id`.\n * @param id - Storage handle to look up.\n */\n public async load(id: string): Promise<Result<CryptoKey>> {\n return (await this._getRaw(id))\n .withErrorFormat((msg) => `failed to load private key '${id}': ${msg}`)\n .onSuccess((key) => (key === undefined ? fail(`key not found: '${id}'`) : succeed(key)));\n }\n\n /**\n * Deletes the entry stored under `id`. Missing ids fail, mirroring the\n * encrypted-file backend. The existence check and the delete run in separate\n * transactions (single-tab assumption).\n * @param id - Storage handle to remove.\n */\n public async delete(id: string): Promise<Result<string>> {\n const existingResult = await this._getRaw(id);\n /* c8 ignore next 3 - defensive: the existence-check read only fails on an IndexedDB environment error */\n if (existingResult.isFailure()) {\n return fail(`failed to delete private key '${id}': ${existingResult.message}`);\n }\n if (existingResult.value === undefined) {\n return fail(`key not found: '${id}'`);\n }\n return (await this._withStore('readwrite', (store) => this._request(store.delete(id))))\n .withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)\n .onSuccess(() => succeed(id));\n }\n\n /**\n * Lists every stored id.\n */\n public async list(): Promise<Result<readonly string[]>> {\n return (await this._withStore('readonly', (store) => this._request<IDBValidKey[]>(store.getAllKeys())))\n .withErrorFormat((msg) => `failed to list private keys: ${msg}`)\n .onSuccess((keys) => succeed(keys.map((key) => String(key))));\n }\n\n private async _getRaw(id: string): Promise<Result<CryptoKey | undefined>> {\n return this._withStore('readonly', (store) => this._request<CryptoKey | undefined>(store.get(id)));\n }\n\n private async _openDb(): Promise<Result<IDBDatabase>> {\n if (this._db !== undefined) {\n return succeed(this._db);\n }\n const firstOpen = await this._open(undefined);\n /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */\n if (firstOpen.isFailure()) {\n return firstOpen;\n }\n // The object store is created in `onupgradeneeded`, which only fires when\n // the version changes. If a database at this name already existed at the\n // current version but without our store (e.g. a different `storeName` than\n // a prior instance used), the store is absent. Re-open at the next version\n // to add it via a version-bump upgrade rather than failing later writes\n // with NotFoundError.\n let db = firstOpen.value;\n if (!db.objectStoreNames.contains(this._storeName)) {\n const nextVersion = db.version + 1;\n db.close();\n const reopen = await this._open(nextVersion);\n /* c8 ignore next 3 - defensive: the version-bump re-open only fails on an IndexedDB environment fault */\n if (reopen.isFailure()) {\n return reopen;\n }\n db = reopen.value;\n }\n this._db = db;\n return succeed(db);\n }\n\n private async _open(version: number | undefined): Promise<Result<IDBDatabase>> {\n return captureAsyncResult<IDBDatabase>(\n () =>\n new Promise<IDBDatabase>((resolve, reject) => {\n const request =\n version === undefined\n ? this._factory.open(this._databaseName)\n : this._factory.open(this._databaseName, version);\n request.onupgradeneeded = (): void => {\n // Create our object store if absent. This fires on initial creation\n // and on the version bump used to add a store to an existing\n // database; future schema migrations extend this hook additively.\n if (!request.result.objectStoreNames.contains(this._storeName)) {\n request.result.createObjectStore(this._storeName);\n }\n };\n request.onsuccess = (): void => {\n const db = request.result;\n // If another connection (e.g. a sibling instance adding a store via\n // version bump, or another tab) needs to upgrade, close this one so\n // we don't block it. Clear the cached handle first so the next\n // operation reopens rather than reusing the now-closed connection.\n // Single-tab use is the documented assumption; this just prevents a\n // deadlock when several instances share a db.\n db.onversionchange = (): void => {\n if (this._db === db) {\n this._db = undefined;\n }\n db.close();\n };\n resolve(db);\n };\n /* c8 ignore next 2 - defensive: open errors require a corrupted/blocked IndexedDB environment */\n request.onerror = (): void => reject(request.error ?? new Error('IndexedDB open failed'));\n })\n );\n }\n\n private async _withStore<T>(\n mode: IDBTransactionMode,\n op: (store: IDBObjectStore) => Promise<Result<T>>\n ): Promise<Result<T>> {\n const dbResult = await this._openDb();\n /* c8 ignore next 3 - defensive: database open only fails on a corrupted/blocked IndexedDB environment */\n if (dbResult.isFailure()) {\n return fail(dbResult.message);\n }\n const txnResult = captureResult(() => dbResult.value.transaction(this._storeName, mode));\n /* c8 ignore next 3 - transaction creation only throws if the store was deleted out from under us */\n if (txnResult.isFailure()) {\n return fail(txnResult.message);\n }\n const transaction = txnResult.value;\n\n // For writes, a successful request is not durable until the transaction\n // commits (oncomplete); it can still abort afterwards. Attach the completion\n // listener BEFORE issuing the request so no event is missed, and wait for it\n // before reporting success. Reads need no such wait.\n const completion = mode === 'readwrite' ? this._awaitCompletion(transaction) : undefined;\n\n // `op` builds and issues the IDB request synchronously (e.g. `store.put`),\n // which can throw before any Promise is created — DataCloneError on a\n // non-cloneable value, or a transaction-state DOMException. Run it inside a\n // capture boundary so those surface as Failure rather than a rejection,\n // preserving the Result contract.\n const opOuter = await captureAsyncResult(() => op(transaction.objectStore(this._storeName)));\n /* c8 ignore next 3 - defensive: synchronous IDB throws (e.g. DataCloneError) are unreachable for the typed public surface (private CryptoKeys clone; reads pass a string id), but the capture preserves the Result contract */\n if (opOuter.isFailure()) {\n return fail(opOuter.message);\n }\n const opResult = opOuter.value;\n /* c8 ignore next 3 - defensive: the request itself only fails on an IndexedDB environment fault */\n if (opResult.isFailure()) {\n return opResult;\n }\n if (completion !== undefined) {\n const commitResult = await completion;\n /* c8 ignore next 3 - defensive: a transaction abort after a successful request requires an IndexedDB environment fault */\n if (commitResult.isFailure()) {\n return fail(commitResult.message);\n }\n }\n return opResult;\n }\n\n private async _request<T>(request: IDBRequest<T>): Promise<Result<T>> {\n return captureAsyncResult<T>(\n () =>\n new Promise<T>((resolve, reject) => {\n request.onsuccess = (): void => resolve(request.result);\n /* c8 ignore next 2 - defensive: per-request errors require a corrupted IndexedDB environment */\n request.onerror = (): void => reject(request.error ?? new Error('IndexedDB request failed'));\n })\n );\n }\n\n private async _awaitCompletion(transaction: IDBTransaction): Promise<Result<true>> {\n return captureAsyncResult<true>(\n () =>\n new Promise<true>((resolve, reject) => {\n transaction.oncomplete = (): void => resolve(true);\n /* c8 ignore next 4 - defensive: abort/error after a successful write requires an IndexedDB environment fault */\n transaction.onabort = (): void =>\n reject(transaction.error ?? new Error('IndexedDB transaction aborted'));\n transaction.onerror = (): void =>\n reject(transaction.error ?? new Error('IndexedDB transaction error'));\n })\n );\n }\n}\n"]}
@@ -4,6 +4,7 @@
4
4
  */
5
5
  export * from './browserHashProvider';
6
6
  export * from './browserCryptoProvider';
7
+ export * from './idbPrivateKeyStorage';
7
8
  import { CryptoUtils } from '@fgv/ts-extras';
8
9
  /**
9
10
  * HPKE base mode (RFC 9180) — `DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM`.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":"AAsBA;;;GAGG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AAMxC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,OAAO,WAAW,CAAC,YAAuC,CAAC;AAEtF;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":"AAsBA;;;GAGG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AAMvC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,OAAO,WAAW,CAAC,YAAuC,CAAC;AAEtF;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,eAAe,CAAC"}
@@ -42,6 +42,7 @@ exports.HpkeProvider = void 0;
42
42
  */
43
43
  __exportStar(require("./browserHashProvider"), exports);
44
44
  __exportStar(require("./browserCryptoProvider"), exports);
45
+ __exportStar(require("./idbPrivateKeyStorage"), exports);
45
46
  // HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;
46
47
  // re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.
47
48
  // We import from the top-level namespace to avoid relying on package.json exports path
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;AAEH;;;GAGG;AAEH,wDAAsC;AACtC,0DAAwC;AAExC,sFAAsF;AACtF,6FAA6F;AAC7F,uFAAuF;AACvF,mEAAmE;AACnE,8CAA6C;AAE7C;;;;;GAKG;AACU,QAAA,YAAY,GAAoC,uBAAW,CAAC,YAAY,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible cryptographic utilities using the Web Crypto API.\n * @packageDocumentation\n */\n\nexport * from './browserHashProvider';\nexport * from './browserCryptoProvider';\n\n// HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;\n// re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.\n// We import from the top-level namespace to avoid relying on package.json exports path\n// resolution (which requires moduleResolution: node16 or bundler).\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * HPKE base mode (RFC 9180) — `DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM`.\n * Re-exported from `@fgv/ts-extras` for browser consumers.\n * @see {@link CryptoUtils.HpkeProvider}\n * @public\n */\nexport const HpkeProvider: typeof CryptoUtils.HpkeProvider = CryptoUtils.HpkeProvider;\n\n/**\n * Output of `HpkeProvider.sealBase`. Re-exported from `@fgv/ts-extras`.\n * @public\n */\nexport type IHpkeSealResult = CryptoUtils.IHpkeSealResult;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;AAEH;;;GAGG;AAEH,wDAAsC;AACtC,0DAAwC;AACxC,yDAAuC;AAEvC,sFAAsF;AACtF,6FAA6F;AAC7F,uFAAuF;AACvF,mEAAmE;AACnE,8CAA6C;AAE7C;;;;;GAKG;AACU,QAAA,YAAY,GAAoC,uBAAW,CAAC,YAAY,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible cryptographic utilities using the Web Crypto API.\n * @packageDocumentation\n */\n\nexport * from './browserHashProvider';\nexport * from './browserCryptoProvider';\nexport * from './idbPrivateKeyStorage';\n\n// HpkeProvider re-export: implementation lives in @fgv/ts-extras CryptoUtils packlet;\n// re-exported here so browser callers can import from @fgv/ts-web-extras for this primitive.\n// We import from the top-level namespace to avoid relying on package.json exports path\n// resolution (which requires moduleResolution: node16 or bundler).\nimport { CryptoUtils } from '@fgv/ts-extras';\n\n/**\n * HPKE base mode (RFC 9180) — `DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM`.\n * Re-exported from `@fgv/ts-extras` for browser consumers.\n * @see {@link CryptoUtils.HpkeProvider}\n * @public\n */\nexport const HpkeProvider: typeof CryptoUtils.HpkeProvider = CryptoUtils.HpkeProvider;\n\n/**\n * Output of `HpkeProvider.sealBase`. Re-exported from `@fgv/ts-extras`.\n * @public\n */\nexport type IHpkeSealResult = CryptoUtils.IHpkeSealResult;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-web-extras",
3
- "version": "5.1.0-32",
3
+ "version": "5.1.0-34",
4
4
  "description": "Browser-compatible utilities and FileTree implementations",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-web-extras.d.ts",
@@ -46,19 +46,20 @@
46
46
  "@types/react": "~19.2.8",
47
47
  "typedoc": "~0.28.16",
48
48
  "typedoc-plugin-markdown": "~4.9.0",
49
- "@fgv/heft-dual-rig": "5.1.0-32",
50
- "@fgv/ts-extras": "5.1.0-32",
51
- "@fgv/typedoc-compact-theme": "5.1.0-32",
52
- "@fgv/ts-utils": "5.1.0-32",
53
- "@fgv/ts-json-base": "5.1.0-32",
54
- "@fgv/ts-utils-jest": "5.1.0-32"
49
+ "fake-indexeddb": "~6.2.5",
50
+ "@fgv/heft-dual-rig": "5.1.0-34",
51
+ "@fgv/ts-extras": "5.1.0-34",
52
+ "@fgv/ts-utils": "5.1.0-34",
53
+ "@fgv/ts-json-base": "5.1.0-34",
54
+ "@fgv/typedoc-compact-theme": "5.1.0-34",
55
+ "@fgv/ts-utils-jest": "5.1.0-34"
55
56
  },
56
57
  "peerDependencies": {
57
58
  "react": ">=18 <20",
58
59
  "react-dom": ">=18 <20",
59
- "@fgv/ts-extras": "5.1.0-32",
60
- "@fgv/ts-utils": "5.1.0-32",
61
- "@fgv/ts-json-base": "5.1.0-32"
60
+ "@fgv/ts-extras": "5.1.0-34",
61
+ "@fgv/ts-utils": "5.1.0-34",
62
+ "@fgv/ts-json-base": "5.1.0-34"
62
63
  },
63
64
  "exports": {
64
65
  ".": {