@fgv/ts-web-extras 5.0.2 → 5.1.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/.rush/temp/81e0881271ff236956b2f52e8ca99da6574c6e1e.tar.log +223 -0
  2. package/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +35 -25
  3. package/.rush/temp/operation/build/all.log +35 -25
  4. package/.rush/temp/operation/build/log-chunks.jsonl +35 -25
  5. package/.rush/temp/operation/build/state.json +1 -1
  6. package/.rush/temp/shrinkwrap-deps.json +175 -163
  7. package/config/jest.config.json +4 -1
  8. package/config/typedoc.json +6 -0
  9. package/dist/index.js +2 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/packlets/crypto-utils/browserCryptoProvider.js +254 -0
  12. package/dist/packlets/crypto-utils/browserCryptoProvider.js.map +1 -0
  13. package/dist/packlets/crypto-utils/browserHashProvider.js.map +1 -0
  14. package/dist/packlets/{crypto → crypto-utils}/index.js +1 -0
  15. package/dist/packlets/crypto-utils/index.js.map +1 -0
  16. package/dist/packlets/file-api-types/index.js +27 -3
  17. package/dist/packlets/file-api-types/index.js.map +1 -1
  18. package/dist/packlets/file-tree/directoryHandleStore.js +124 -0
  19. package/dist/packlets/file-tree/directoryHandleStore.js.map +1 -0
  20. package/dist/packlets/file-tree/fileApiTreeAccessors.js +76 -0
  21. package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  22. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js +345 -0
  23. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -0
  24. package/dist/packlets/file-tree/index.js +3 -0
  25. package/dist/packlets/file-tree/index.js.map +1 -1
  26. package/dist/packlets/file-tree/localStorageTreeAccessors.js +308 -0
  27. package/dist/packlets/file-tree/localStorageTreeAccessors.js.map +1 -0
  28. package/dist/test/mocks/idb-keyval.js +6 -0
  29. package/dist/test/mocks/idb-keyval.js.map +1 -0
  30. package/dist/test/unit/browserHashProvider.test.js +1 -1
  31. package/dist/test/unit/browserHashProvider.test.js.map +1 -1
  32. package/dist/test/unit/directoryHandleStore.test.js +190 -0
  33. package/dist/test/unit/directoryHandleStore.test.js.map +1 -0
  34. package/dist/test/unit/fileApiTypes.test.js +30 -0
  35. package/dist/test/unit/fileApiTypes.test.js.map +1 -1
  36. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js +517 -0
  37. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js.map +1 -0
  38. package/dist/test/unit/localStorageTreeAccessors.test.js +595 -0
  39. package/dist/test/unit/localStorageTreeAccessors.test.js.map +1 -0
  40. package/dist/test/utils/fileSystemAccessMocks.js +271 -0
  41. package/dist/test/utils/fileSystemAccessMocks.js.map +1 -0
  42. package/dist/ts-web-extras.d.ts +460 -1
  43. package/dist/tsdoc-metadata.json +1 -1
  44. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider._constructor_.md +50 -0
  45. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.decrypt.md +104 -0
  46. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.derivekey.md +88 -0
  47. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.encrypt.md +72 -0
  48. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.frombase64.md +56 -0
  49. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.generatekey.md +19 -0
  50. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.generaterandombytes.md +56 -0
  51. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.md +169 -0
  52. package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.tobase64.md +56 -0
  53. package/docs/{ts-web-extras.browserhashprovider.hashparts.md → ts-web-extras.cryptoutils.browserhashprovider.hashparts.md} +2 -2
  54. package/docs/{ts-web-extras.browserhashprovider.hashstring.md → ts-web-extras.cryptoutils.browserhashprovider.hashstring.md} +2 -2
  55. package/docs/{ts-web-extras.browserhashprovider.md → ts-web-extras.cryptoutils.browserhashprovider.md} +4 -4
  56. package/docs/ts-web-extras.cryptoutils.createbrowsercryptoprovider.md +19 -0
  57. package/docs/ts-web-extras.cryptoutils.md +71 -0
  58. package/docs/ts-web-extras.fileapitreeaccessors.createfromlocalstorage.md +74 -0
  59. package/docs/ts-web-extras.fileapitreeaccessors.createpersistent.md +76 -0
  60. package/docs/ts-web-extras.fileapitreeaccessors.md +32 -0
  61. package/docs/ts-web-extras.filesystemaccesstreeaccessors._constructor_.md +114 -0
  62. package/docs/ts-web-extras.filesystemaccesstreeaccessors.fileismutable.md +52 -0
  63. package/docs/ts-web-extras.filesystemaccesstreeaccessors.fromdirectoryhandle.md +72 -0
  64. package/docs/ts-web-extras.filesystemaccesstreeaccessors.getdirtypaths.md +17 -0
  65. package/docs/ts-web-extras.filesystemaccesstreeaccessors.isdirty.md +17 -0
  66. package/docs/ts-web-extras.filesystemaccesstreeaccessors.md +159 -0
  67. package/docs/ts-web-extras.filesystemaccesstreeaccessors.savefilecontents.md +66 -0
  68. package/docs/ts-web-extras.filesystemaccesstreeaccessors.synctodisk.md +17 -0
  69. package/docs/ts-web-extras.ifilesystemaccesstreeparams.autosync.md +13 -0
  70. package/docs/ts-web-extras.ifilesystemaccesstreeparams.md +78 -0
  71. package/docs/ts-web-extras.ifilesystemaccesstreeparams.requirewritepermission.md +13 -0
  72. package/docs/ts-web-extras.ilocalstoragetreeparams.autosync.md +13 -0
  73. package/docs/ts-web-extras.ilocalstoragetreeparams.md +97 -0
  74. package/docs/ts-web-extras.ilocalstoragetreeparams.pathtokeymap.md +13 -0
  75. package/docs/ts-web-extras.ilocalstoragetreeparams.storage.md +13 -0
  76. package/docs/ts-web-extras.localstoragetreeaccessors.fileismutable.md +56 -0
  77. package/docs/ts-web-extras.localstoragetreeaccessors.fromstorage.md +56 -0
  78. package/docs/ts-web-extras.localstoragetreeaccessors.getdirtypaths.md +19 -0
  79. package/docs/ts-web-extras.localstoragetreeaccessors.isdirty.md +19 -0
  80. package/docs/ts-web-extras.localstoragetreeaccessors.md +131 -0
  81. package/docs/ts-web-extras.localstoragetreeaccessors.savefilecontents.md +72 -0
  82. package/docs/ts-web-extras.localstoragetreeaccessors.synctodisk.md +19 -0
  83. package/docs/ts-web-extras.md +50 -4
  84. package/etc/ts-web-extras.api.md +91 -1
  85. package/lib/index.d.ts +2 -1
  86. package/lib/index.d.ts.map +1 -1
  87. package/lib/index.js +25 -2
  88. package/lib/index.js.map +1 -1
  89. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts +77 -0
  90. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts.map +1 -0
  91. package/lib/packlets/crypto-utils/browserCryptoProvider.js +259 -0
  92. package/lib/packlets/crypto-utils/browserCryptoProvider.js.map +1 -0
  93. package/lib/packlets/crypto-utils/browserHashProvider.d.ts.map +1 -0
  94. package/lib/packlets/crypto-utils/browserHashProvider.js.map +1 -0
  95. package/lib/packlets/{crypto → crypto-utils}/index.d.ts +1 -0
  96. package/lib/packlets/crypto-utils/index.d.ts.map +1 -0
  97. package/lib/packlets/{crypto → crypto-utils}/index.js +1 -0
  98. package/lib/packlets/crypto-utils/index.js.map +1 -0
  99. package/lib/packlets/file-api-types/index.d.ts.map +1 -1
  100. package/lib/packlets/file-api-types/index.js +27 -3
  101. package/lib/packlets/file-api-types/index.js.map +1 -1
  102. package/lib/packlets/file-tree/directoryHandleStore.d.ts +59 -0
  103. package/lib/packlets/file-tree/directoryHandleStore.d.ts.map +1 -0
  104. package/lib/packlets/file-tree/directoryHandleStore.js +128 -0
  105. package/lib/packlets/file-tree/directoryHandleStore.js.map +1 -0
  106. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts +57 -0
  107. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts.map +1 -1
  108. package/lib/packlets/file-tree/fileApiTreeAccessors.js +76 -0
  109. package/lib/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  110. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts +136 -0
  111. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts.map +1 -0
  112. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js +349 -0
  113. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -0
  114. package/lib/packlets/file-tree/index.d.ts +3 -0
  115. package/lib/packlets/file-tree/index.d.ts.map +1 -1
  116. package/lib/packlets/file-tree/index.js +3 -0
  117. package/lib/packlets/file-tree/index.js.map +1 -1
  118. package/lib/packlets/file-tree/localStorageTreeAccessors.d.ts +129 -0
  119. package/lib/packlets/file-tree/localStorageTreeAccessors.d.ts.map +1 -0
  120. package/lib/packlets/file-tree/localStorageTreeAccessors.js +312 -0
  121. package/lib/packlets/file-tree/localStorageTreeAccessors.js.map +1 -0
  122. package/lib/test/mocks/idb-keyval.d.ts +6 -0
  123. package/lib/test/mocks/idb-keyval.d.ts.map +1 -0
  124. package/lib/test/mocks/idb-keyval.js +9 -0
  125. package/lib/test/mocks/idb-keyval.js.map +1 -0
  126. package/lib/test/unit/browserHashProvider.test.js +21 -21
  127. package/lib/test/unit/browserHashProvider.test.js.map +1 -1
  128. package/lib/test/unit/directoryHandleStore.test.d.ts +2 -0
  129. package/lib/test/unit/directoryHandleStore.test.d.ts.map +1 -0
  130. package/lib/test/unit/directoryHandleStore.test.js +192 -0
  131. package/lib/test/unit/directoryHandleStore.test.js.map +1 -0
  132. package/lib/test/unit/fileApiTypes.test.js +30 -0
  133. package/lib/test/unit/fileApiTypes.test.js.map +1 -1
  134. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts +2 -0
  135. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts.map +1 -0
  136. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js +519 -0
  137. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js.map +1 -0
  138. package/lib/test/unit/localStorageTreeAccessors.test.d.ts +2 -0
  139. package/lib/test/unit/localStorageTreeAccessors.test.d.ts.map +1 -0
  140. package/lib/test/unit/localStorageTreeAccessors.test.js +597 -0
  141. package/lib/test/unit/localStorageTreeAccessors.test.js.map +1 -0
  142. package/lib/test/utils/fileSystemAccessMocks.d.ts +53 -0
  143. package/lib/test/utils/fileSystemAccessMocks.d.ts.map +1 -0
  144. package/lib/test/utils/fileSystemAccessMocks.js +277 -0
  145. package/lib/test/utils/fileSystemAccessMocks.js.map +1 -0
  146. package/package.json +27 -20
  147. package/rush-logs/ts-web-extras.build.cache.log +3 -1
  148. package/rush-logs/ts-web-extras.build.log +35 -25
  149. package/src/index.ts +2 -2
  150. package/src/packlets/crypto-utils/browserCryptoProvider.ts +311 -0
  151. package/src/packlets/{crypto → crypto-utils}/index.ts +1 -0
  152. package/src/packlets/file-api-types/index.ts +24 -3
  153. package/src/packlets/file-tree/directoryHandleStore.ts +136 -0
  154. package/src/packlets/file-tree/fileApiTreeAccessors.ts +90 -0
  155. package/src/packlets/file-tree/fileSystemAccessTreeAccessors.ts +427 -0
  156. package/src/packlets/file-tree/index.ts +3 -0
  157. package/src/packlets/file-tree/localStorageTreeAccessors.ts +377 -0
  158. package/src/test/mocks/idb-keyval.ts +5 -0
  159. package/src/test/unit/browserHashProvider.test.ts +1 -1
  160. package/src/test/unit/directoryHandleStore.test.ts +251 -0
  161. package/src/test/unit/fileApiTypes.test.ts +36 -0
  162. package/src/test/unit/fileSystemAccessTreeAccessors.test.ts +732 -0
  163. package/src/test/unit/localStorageTreeAccessors.test.ts +746 -0
  164. package/src/test/utils/fileSystemAccessMocks.ts +353 -0
  165. package/temp/build/typescript/ts_8nwakTlr.json +1 -0
  166. package/temp/coverage/crypto-utils/browserCryptoProvider.ts.html +1018 -0
  167. package/temp/coverage/{crypto → crypto-utils}/browserHashProvider.ts.html +3 -3
  168. package/temp/coverage/{lcov-report/crypto → crypto-utils}/index.html +21 -6
  169. package/temp/coverage/file-tree/directoryHandleStore.ts.html +493 -0
  170. package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +276 -6
  171. package/temp/coverage/file-tree/fileSystemAccessTreeAccessors.ts.html +1366 -0
  172. package/temp/coverage/file-tree/index.html +55 -10
  173. package/temp/coverage/file-tree/localStorageTreeAccessors.ts.html +1216 -0
  174. package/temp/coverage/helpers/fileTreeHelpers.ts.html +1 -1
  175. package/temp/coverage/helpers/index.html +1 -1
  176. package/temp/coverage/index.html +15 -15
  177. package/temp/coverage/lcov-report/crypto-utils/browserCryptoProvider.ts.html +1018 -0
  178. package/temp/coverage/lcov-report/{crypto → crypto-utils}/browserHashProvider.ts.html +3 -3
  179. package/temp/coverage/{crypto → lcov-report/crypto-utils}/index.html +21 -6
  180. package/temp/coverage/lcov-report/file-tree/directoryHandleStore.ts.html +493 -0
  181. package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +276 -6
  182. package/temp/coverage/lcov-report/file-tree/fileSystemAccessTreeAccessors.ts.html +1366 -0
  183. package/temp/coverage/lcov-report/file-tree/index.html +55 -10
  184. package/temp/coverage/lcov-report/file-tree/localStorageTreeAccessors.ts.html +1216 -0
  185. package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +1 -1
  186. package/temp/coverage/lcov-report/helpers/index.html +1 -1
  187. package/temp/coverage/lcov-report/index.html +15 -15
  188. package/temp/coverage/lcov-report/url-utils/index.html +1 -1
  189. package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +1 -1
  190. package/temp/coverage/lcov.info +2128 -451
  191. package/temp/coverage/url-utils/index.html +1 -1
  192. package/temp/coverage/url-utils/urlParams.ts.html +1 -1
  193. package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
  194. package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +1 -1
  195. package/temp/ts-web-extras.api.json +3236 -385
  196. package/temp/ts-web-extras.api.md +91 -1
  197. package/dist/packlets/crypto/browserHashProvider.js.map +0 -1
  198. package/dist/packlets/crypto/index.js.map +0 -1
  199. package/lib/packlets/crypto/browserHashProvider.d.ts.map +0 -1
  200. package/lib/packlets/crypto/browserHashProvider.js.map +0 -1
  201. package/lib/packlets/crypto/index.d.ts.map +0 -1
  202. package/lib/packlets/crypto/index.js.map +0 -1
  203. package/temp/build/typescript/ts_vnCx6LlY.json +0 -1
  204. /package/dist/packlets/{crypto → crypto-utils}/browserHashProvider.js +0 -0
  205. /package/lib/packlets/{crypto → crypto-utils}/browserHashProvider.d.ts +0 -0
  206. /package/lib/packlets/{crypto → crypto-utils}/browserHashProvider.js +0 -0
  207. /package/src/packlets/{crypto → crypto-utils}/browserHashProvider.ts +0 -0
  208. /package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/{0e/package_0eb6535f5987849d93ea51ef33a14cf6 → 8d/package_8dcbedef69e4299f0f51fcda8f4f1c8e} +0 -0
@@ -23,23 +23,38 @@ Description
23
23
  </th></tr></thead>
24
24
  <tbody><tr><td>
25
25
 
26
- [BrowserHashProvider](./ts-web-extras.browserhashprovider.md)
26
+ [FileApiTreeAccessors](./ts-web-extras.fileapitreeaccessors.md)
27
27
 
28
28
 
29
29
  </td><td>
30
30
 
31
- Browser-compatible hash provider using the Web Crypto API. Supports common hash algorithms available in browsers.
31
+ Helper class to create FileTree instances from various file sources. Supports File API (FileList) and File System Access API handles.
32
32
 
33
33
 
34
34
  </td></tr>
35
35
  <tr><td>
36
36
 
37
- [FileApiTreeAccessors](./ts-web-extras.fileapitreeaccessors.md)
37
+ [FileSystemAccessTreeAccessors](./ts-web-extras.filesystemaccesstreeaccessors.md)
38
38
 
39
39
 
40
40
  </td><td>
41
41
 
42
- Helper class to create FileTree instances from various file sources. Supports File API (FileList) and File System Access API handles.
42
+ Implementation of `FileTree.IPersistentFileTreeAccessors` that uses the File System Access API to provide persistent file editing in browsers.
43
+
44
+
45
+ </td></tr>
46
+ <tr><td>
47
+
48
+ [LocalStorageTreeAccessors](./ts-web-extras.localstoragetreeaccessors.md)
49
+
50
+
51
+ </td><td>
52
+
53
+ Browser localStorage-backed file tree accessors with persistence support.
54
+
55
+ Maps filesystem-like directory paths to localStorage keys, where each key stores multiple collections as a JSON object. This provides a general-purpose implementation for browser-based file tree persistence without requiring File System Access API.
56
+
57
+ Storage format per key: `{ "collection-id": { ...collectionData }, ... }` File paths map as: `/data/ingredients/collection-id.json` → stored in key for `/data/ingredients`
43
58
 
44
59
 
45
60
  </td></tr>
@@ -380,6 +395,17 @@ Tree initializer for FileList objects (from File API).
380
395
  Interface for file metadata.
381
396
 
382
397
 
398
+ </td></tr>
399
+ <tr><td>
400
+
401
+ [IFileSystemAccessTreeParams](./ts-web-extras.ifilesystemaccesstreeparams.md)
402
+
403
+
404
+ </td><td>
405
+
406
+ Options for creating persistent file trees.
407
+
408
+
383
409
  </td></tr>
384
410
  <tr><td>
385
411
 
@@ -391,6 +417,17 @@ Interface for file metadata.
391
417
  File System Access API methods available on Window
392
418
 
393
419
 
420
+ </td></tr>
421
+ <tr><td>
422
+
423
+ [ILocalStorageTreeParams](./ts-web-extras.ilocalstoragetreeparams.md)
424
+
425
+
426
+ </td><td>
427
+
428
+ Configuration for LocalStorageTreeAccessors.
429
+
430
+
394
431
  </td></tr>
395
432
  <tr><td>
396
433
 
@@ -453,6 +490,15 @@ Description
453
490
  </th></tr></thead>
454
491
  <tbody><tr><td>
455
492
 
493
+ [CryptoUtils](./ts-web-extras.cryptoutils.md)
494
+
495
+
496
+ </td><td>
497
+
498
+
499
+ </td></tr>
500
+ <tr><td>
501
+
456
502
  [FileTreeHelpers](./ts-web-extras.filetreehelpers.md)
457
503
 
458
504
 
@@ -4,18 +4,69 @@
4
4
 
5
5
  ```ts
6
6
 
7
+ import { CryptoUtils as CryptoUtils_2 } from '@fgv/ts-extras';
8
+ import { DetailedResult } from '@fgv/ts-utils';
7
9
  import { FileTree } from '@fgv/ts-json-base';
8
10
  import { Result } from '@fgv/ts-utils';
9
11
 
12
+ // Warning: (ae-forgotten-export) The symbol "ICryptoProvider" needs to be exported by the entry point index.d.ts
13
+ //
14
+ // @public
15
+ class BrowserCryptoProvider implements ICryptoProvider {
16
+ // Warning: (ae-unresolved-link) The @link reference could not be resolved: This type of declaration is not supported yet by the resolver
17
+ constructor(cryptoApi?: Crypto);
18
+ decrypt(encryptedData: Uint8Array, key: Uint8Array, iv: Uint8Array, authTag: Uint8Array): Promise<Result<string>>;
19
+ deriveKey(password: string, salt: Uint8Array, iterations: number): Promise<Result<Uint8Array>>;
20
+ // Warning: (ae-forgotten-export) The symbol "IEncryptionResult" needs to be exported by the entry point index.d.ts
21
+ encrypt(plaintext: string, key: Uint8Array): Promise<Result<IEncryptionResult>>;
22
+ fromBase64(base64: string): Result<Uint8Array>;
23
+ generateKey(): Promise<Result<Uint8Array>>;
24
+ generateRandomBytes(length: number): Result<Uint8Array>;
25
+ toBase64(data: Uint8Array): string;
26
+ }
27
+
10
28
  // @public
11
- export class BrowserHashProvider {
29
+ class BrowserHashProvider {
12
30
  static hashParts(parts: string[], algorithm?: string, separator?: string): Promise<Result<string>>;
13
31
  static hashString(data: string, algorithm?: string): Promise<Result<string>>;
14
32
  }
15
33
 
34
+ // Warning: (ae-unresolved-link) The @link reference could not be resolved: This type of declaration is not supported yet by the resolver
35
+ //
36
+ // @public
37
+ function createBrowserCryptoProvider(): Result<BrowserCryptoProvider>;
38
+
39
+ declare namespace CryptoUtils {
40
+ export {
41
+ BrowserHashProvider,
42
+ createBrowserCryptoProvider,
43
+ BrowserCryptoProvider
44
+ }
45
+ }
46
+ export { CryptoUtils }
47
+
48
+ // @public
49
+ export const DEFAULT_DIRECTORY_HANDLE_DB = "chocolate-lab-storage";
50
+
51
+ // @public
52
+ export const DEFAULT_DIRECTORY_HANDLE_STORE = "directory-handles";
53
+
16
54
  // @public
17
55
  const defaultFileApiTreeInitParams: FileTree.IFileTreeInitParams<string>;
18
56
 
57
+ // @public
58
+ export class DirectoryHandleStore {
59
+ constructor(dbName?: string, storeName?: string);
60
+ getAll(): Promise<Result<Array<{
61
+ label: string;
62
+ handle: FileSystemDirectoryHandle_2;
63
+ }>>>;
64
+ getAllLabels(): Promise<Result<string[]>>;
65
+ load(label: string): Promise<Result<FileSystemDirectoryHandle_2 | undefined>>;
66
+ remove(label: string): Promise<Result<void>>;
67
+ save(label: string, handle: FileSystemDirectoryHandle_2): Promise<Result<void>>;
68
+ }
69
+
19
70
  // @public
20
71
  export function exportAsJson(data: unknown, filename: string): void;
21
72
 
@@ -34,6 +85,9 @@ function extractFileMetadata(file: File): IFileMetadata;
34
85
  // @public
35
86
  export class FileApiTreeAccessors<TCT extends string = string> {
36
87
  static create<TCT extends string = string>(initializers: TreeInitializer[], params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<FileTree.FileTree<TCT>>>;
88
+ static createFromLocalStorage<TCT extends string = string>(params: ILocalStorageTreeParams<TCT>): Result<FileTree.FileTree<TCT>>;
89
+ static createPersistent<TCT extends string = string>(dirHandle: FileSystemDirectoryHandle_2, params?: IFileSystemAccessTreeParams<TCT>): Promise<Result<FileTree.FileTree<TCT>>>;
90
+ static createPersistentFromFile<TCT extends string = string>(fileHandle: FileSystemFileHandle_2, params?: IFileSystemAccessTreeParams<TCT>): Promise<Result<FileTree.FileTree<TCT>>>;
37
91
  static extractFileMetadata(file: File): IFileMetadata;
38
92
  static fromDirectoryUpload<TCT extends string = string>(fileList: FileList, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<FileTree.FileTree<TCT>>>;
39
93
  static fromFileList<TCT extends string = string>(fileList: FileList, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<FileTree.FileTree<TCT>>>;
@@ -48,6 +102,18 @@ export interface FilePickerAcceptType {
48
102
  description?: string;
49
103
  }
50
104
 
105
+ // @public
106
+ export class FileSystemAccessTreeAccessors<TCT extends string = string> extends FileTree.InMemoryTreeAccessors<TCT> implements FileTree.IPersistentFileTreeAccessors<TCT> {
107
+ protected constructor(files: FileTree.IInMemoryFile<TCT>[], rootDir: FileSystemDirectoryHandle_2, handles: Map<string, FileSystemFileHandle_2>, params: IFileSystemAccessTreeParams<TCT> | undefined, hasWritePermission: boolean);
108
+ fileIsMutable(path: string): DetailedResult<boolean, FileTree.SaveDetail>;
109
+ static fromDirectoryHandle<TCT extends string = string>(dirHandle: FileSystemDirectoryHandle_2, params?: IFileSystemAccessTreeParams<TCT>): Promise<Result<FileSystemAccessTreeAccessors<TCT>>>;
110
+ static fromFileHandle<TCT extends string = string>(fileHandle: FileSystemFileHandle_2, params?: IFileSystemAccessTreeParams<TCT>): Promise<Result<FileSystemAccessTreeAccessors<TCT>>>;
111
+ getDirtyPaths(): string[];
112
+ isDirty(): boolean;
113
+ saveFileContents(path: string, contents: string): Result<string>;
114
+ syncToDisk(): Promise<Result<void>>;
115
+ }
116
+
51
117
  // @public
52
118
  interface FileSystemCreateWritableOptions_2 {
53
119
  // (undocumented)
@@ -200,6 +266,13 @@ export interface IFileMetadata {
200
266
  type: string;
201
267
  }
202
268
 
269
+ // @public
270
+ export interface IFileSystemAccessTreeParams<TCT extends string = string> extends FileTree.IFileTreeInitParams<TCT> {
271
+ autoSync?: boolean;
272
+ filePath?: string;
273
+ requireWritePermission?: boolean;
274
+ }
275
+
203
276
  // @public
204
277
  export interface IFsAccessApis {
205
278
  // (undocumented)
@@ -210,6 +283,13 @@ export interface IFsAccessApis {
210
283
  showSaveFilePicker(options?: ShowSaveFilePickerOptions): Promise<FileSystemFileHandle_2>;
211
284
  }
212
285
 
286
+ // @public
287
+ export interface ILocalStorageTreeParams<TCT extends string = string> extends FileTree.IFileTreeInitParams<TCT> {
288
+ autoSync?: boolean;
289
+ pathToKeyMap: Record<string, string>;
290
+ storage?: Storage;
291
+ }
292
+
213
293
  // @public
214
294
  export function isDirectoryHandle(handle: FileSystemHandle_2): handle is FileSystemDirectoryHandle_2;
215
295
 
@@ -236,6 +316,16 @@ export interface IUrlConfigOptions {
236
316
  zipPath?: string;
237
317
  }
238
318
 
319
+ // @public
320
+ export class LocalStorageTreeAccessors<TCT extends string = string> extends FileTree.InMemoryTreeAccessors<TCT> implements FileTree.IPersistentFileTreeAccessors<TCT> {
321
+ fileIsMutable(path: string): DetailedResult<boolean, FileTree.SaveDetail>;
322
+ static fromStorage<TCT extends string = string>(params: ILocalStorageTreeParams<TCT>): Result<LocalStorageTreeAccessors<TCT>>;
323
+ getDirtyPaths(): string[];
324
+ isDirty(): boolean;
325
+ saveFileContents(path: string, contents: string): Result<string>;
326
+ syncToDisk(): Promise<Result<void>>;
327
+ }
328
+
239
329
  // @public
240
330
  export function parseContextFilter(contextFilter: string): Record<string, string>;
241
331
 
package/lib/index.d.ts CHANGED
@@ -8,7 +8,8 @@
8
8
  *
9
9
  * @packageDocumentation
10
10
  */
11
- export * from './packlets/crypto';
11
+ import * as CryptoUtils from './packlets/crypto-utils';
12
+ export { CryptoUtils };
12
13
  export * from './packlets/file-tree';
13
14
  export * from './packlets/helpers';
14
15
  export * from './packlets/file-api-types';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;GASG;AAGH,cAAc,mBAAmB,CAAC;AAGlC,cAAc,sBAAsB,CAAC;AAGrC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,2BAA2B,CAAC;AAG1C,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;GASG;AAEH,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,CAAC;AAGvB,cAAc,sBAAsB,CAAC;AAGrC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,2BAA2B,CAAC;AAG1C,cAAc,sBAAsB,CAAC"}
package/lib/index.js CHANGED
@@ -31,10 +31,33 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
31
31
  if (k2 === undefined) k2 = k;
32
32
  o[k2] = m[k];
33
33
  }));
34
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
36
+ }) : function(o, v) {
37
+ o["default"] = v;
38
+ });
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
34
56
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
35
57
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
36
58
  };
37
59
  Object.defineProperty(exports, "__esModule", { value: true });
60
+ exports.CryptoUtils = void 0;
38
61
  /**
39
62
  * Browser-compatible utilities and FileTree implementations.
40
63
  *
@@ -45,8 +68,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
45
68
  *
46
69
  * @packageDocumentation
47
70
  */
48
- // Export crypto functionality
49
- __exportStar(require("./packlets/crypto"), exports);
71
+ const CryptoUtils = __importStar(require("./packlets/crypto-utils"));
72
+ exports.CryptoUtils = CryptoUtils;
50
73
  // Export file tree functionality
51
74
  __exportStar(require("./packlets/file-tree"), exports);
52
75
  // Export helper functions
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH;;;;;;;;;GASG;AAEH,8BAA8B;AAC9B,oDAAkC;AAElC,iCAAiC;AACjC,uDAAqC;AAErC,0BAA0B;AAC1B,qDAAmC;AAEnC,sCAAsC;AACtC,4DAA0C;AAE1C,uBAAuB;AACvB,uDAAqC","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 utilities and FileTree implementations.\n *\n * This library provides browser-compatible alternatives to Node.js-specific functionality,\n * including Web Crypto API-based hashing, File API-based file tree implementations,\n * and URL parameter parsing utilities.\n * All exports are designed to be tree-shakeable for optimal bundle size.\n *\n * @packageDocumentation\n */\n\n// Export crypto functionality\nexport * from './packlets/crypto';\n\n// Export file tree functionality\nexport * from './packlets/file-tree';\n\n// Export helper functions\nexport * from './packlets/helpers';\n\n// Export File System Access API types\nexport * from './packlets/file-api-types';\n\n// Export URL utilities\nexport * from './packlets/url-utils';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH;;;;;;;;;GASG;AAEH,qEAAuD;AAC9C,kCAAW;AAEpB,iCAAiC;AACjC,uDAAqC;AAErC,0BAA0B;AAC1B,qDAAmC;AAEnC,sCAAsC;AACtC,4DAA0C;AAE1C,uBAAuB;AACvB,uDAAqC","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 utilities and FileTree implementations.\n *\n * This library provides browser-compatible alternatives to Node.js-specific functionality,\n * including Web Crypto API-based hashing, File API-based file tree implementations,\n * and URL parameter parsing utilities.\n * All exports are designed to be tree-shakeable for optimal bundle size.\n *\n * @packageDocumentation\n */\n\nimport * as CryptoUtils from './packlets/crypto-utils';\nexport { CryptoUtils };\n\n// Export file tree functionality\nexport * from './packlets/file-tree';\n\n// Export helper functions\nexport * from './packlets/helpers';\n\n// Export File System Access API types\nexport * from './packlets/file-api-types';\n\n// Export URL utilities\nexport * from './packlets/url-utils';\n"]}
@@ -0,0 +1,77 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { CryptoUtils } from '@fgv/ts-extras';
3
+ type ICryptoProvider = CryptoUtils.ICryptoProvider;
4
+ type IEncryptionResult = CryptoUtils.IEncryptionResult;
5
+ /**
6
+ * Browser implementation of `ICryptoProvider` using the Web Crypto API.
7
+ * Uses AES-256-GCM for authenticated encryption.
8
+ *
9
+ * Note: This provider requires a browser environment with Web Crypto API support.
10
+ * In Node.js 15+, Web Crypto is available via globalThis.crypto or require('crypto').webcrypto.
11
+ *
12
+ * @public
13
+ */
14
+ export declare class BrowserCryptoProvider implements ICryptoProvider {
15
+ private readonly _crypto;
16
+ /**
17
+ * Creates a new {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider}.
18
+ * @param cryptoApi - Optional Crypto instance (defaults to globalThis.crypto)
19
+ */
20
+ constructor(cryptoApi?: Crypto);
21
+ /**
22
+ * Encrypts plaintext using AES-256-GCM.
23
+ * @param plaintext - UTF-8 string to encrypt
24
+ * @param key - 32-byte encryption key
25
+ * @returns `Success` with encryption result, or `Failure` with an error.
26
+ */
27
+ encrypt(plaintext: string, key: Uint8Array): Promise<Result<IEncryptionResult>>;
28
+ /**
29
+ * Decrypts ciphertext using AES-256-GCM.
30
+ * @param encryptedData - Encrypted bytes
31
+ * @param key - 32-byte decryption key
32
+ * @param iv - Initialization vector (12 bytes)
33
+ * @param authTag - GCM authentication tag (16 bytes)
34
+ * @returns `Success` with decrypted UTF-8 string, or `Failure` with an error.
35
+ */
36
+ decrypt(encryptedData: Uint8Array, key: Uint8Array, iv: Uint8Array, authTag: Uint8Array): Promise<Result<string>>;
37
+ /**
38
+ * Generates a random 32-byte key suitable for AES-256.
39
+ * @returns Success with generated key, or Failure with error
40
+ */
41
+ generateKey(): Promise<Result<Uint8Array>>;
42
+ /**
43
+ * Derives a key from a password using PBKDF2.
44
+ * @param password - Password string
45
+ * @param salt - Salt bytes (should be at least 16 bytes)
46
+ * @param iterations - Number of iterations (recommend 100000+)
47
+ * @returns Success with derived 32-byte key, or Failure with error
48
+ */
49
+ deriveKey(password: string, salt: Uint8Array, iterations: number): Promise<Result<Uint8Array>>;
50
+ /**
51
+ * Generates cryptographically secure random bytes.
52
+ * @param length - Number of bytes to generate
53
+ * @returns Success with random bytes, or Failure with error
54
+ */
55
+ generateRandomBytes(length: number): Result<Uint8Array>;
56
+ /**
57
+ * Encodes binary data to base64 string.
58
+ * @param data - Binary data to encode
59
+ * @returns Base64-encoded string
60
+ */
61
+ toBase64(data: Uint8Array): string;
62
+ /**
63
+ * Decodes base64 string to binary data.
64
+ * @param base64 - Base64-encoded string
65
+ * @returns Success with decoded bytes, or Failure if invalid base64
66
+ */
67
+ fromBase64(base64: string): Result<Uint8Array>;
68
+ }
69
+ /**
70
+ * Creates a {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider} if Web
71
+ * Crypto API is available.
72
+ * @returns `Success` with provider, or `Failure` if not available
73
+ * @public
74
+ */
75
+ export declare function createBrowserCryptoProvider(): Result<BrowserCryptoProvider>;
76
+ export {};
77
+ //# sourceMappingURL=browserCryptoProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browserCryptoProvider.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/browserCryptoProvider.ts"],"names":[],"mappings":"AAqBA,OAAO,EAA0B,MAAM,EAAW,MAAM,eAAe,CAAC;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,KAAK,eAAe,GAAG,WAAW,CAAC,eAAe,CAAC;AACnD,KAAK,iBAAiB,GAAG,WAAW,CAAC,iBAAiB,CAAC;AAevD;;;;;;;;GAQG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC;;;OAGG;gBACgB,SAAS,CAAC,EAAE,MAAM;IAYrC;;;;;OAKG;IACU,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAmD5F;;;;;;;OAOG;IACU,OAAO,CAClB,aAAa,EAAE,UAAU,EACzB,GAAG,EAAE,UAAU,EACf,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAgD1B;;;OAGG;IACU,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IASvD;;;;;;OAMG;IACU,SAAS,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAyC9B;;;;OAIG;IACI,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC;IAY9D;;;;OAIG;IACI,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IASzC;;;;OAIG;IACI,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC;CAYtD;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAE3E"}
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ // Copyright (c) 2024 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
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.BrowserCryptoProvider = void 0;
23
+ exports.createBrowserCryptoProvider = createBrowserCryptoProvider;
24
+ /* c8 ignore start - Browser-only implementation cannot be tested in Node.js environment */
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ const ts_extras_1 = require("@fgv/ts-extras");
27
+ const CryptoConstants = ts_extras_1.CryptoUtils.Constants;
28
+ /**
29
+ * Extracts an `ArrayBuffer` from a Uint8Array, handling the potential SharedArrayBuffer case.
30
+ * @param arr - The Uint8Array to extract from
31
+ * @returns An `ArrayBuffer` containing a copy of the data.
32
+ */
33
+ function toArrayBuffer(arr) {
34
+ // Create a new ArrayBuffer and copy the data - this handles both ArrayBuffer and SharedArrayBuffer
35
+ const buffer = new ArrayBuffer(arr.byteLength);
36
+ new Uint8Array(buffer).set(arr);
37
+ return buffer;
38
+ }
39
+ /**
40
+ * Browser implementation of `ICryptoProvider` using the Web Crypto API.
41
+ * Uses AES-256-GCM for authenticated encryption.
42
+ *
43
+ * Note: This provider requires a browser environment with Web Crypto API support.
44
+ * In Node.js 15+, Web Crypto is available via globalThis.crypto or require('crypto').webcrypto.
45
+ *
46
+ * @public
47
+ */
48
+ class BrowserCryptoProvider {
49
+ /**
50
+ * Creates a new {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider}.
51
+ * @param cryptoApi - Optional Crypto instance (defaults to globalThis.crypto)
52
+ */
53
+ constructor(cryptoApi) {
54
+ if (cryptoApi) {
55
+ this._crypto = cryptoApi;
56
+ }
57
+ else if (typeof globalThis !== 'undefined' && globalThis.crypto) {
58
+ this._crypto = globalThis.crypto;
59
+ }
60
+ else if (typeof window !== 'undefined' && window.crypto) {
61
+ this._crypto = window.crypto;
62
+ }
63
+ else {
64
+ throw new Error('Web Crypto API not available');
65
+ }
66
+ }
67
+ /**
68
+ * Encrypts plaintext using AES-256-GCM.
69
+ * @param plaintext - UTF-8 string to encrypt
70
+ * @param key - 32-byte encryption key
71
+ * @returns `Success` with encryption result, or `Failure` with an error.
72
+ */
73
+ async encrypt(plaintext, key) {
74
+ if (key.length !== CryptoConstants.AES_256_KEY_SIZE) {
75
+ return ts_utils_1.Failure.with(`Key must be ${CryptoConstants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
76
+ }
77
+ try {
78
+ // Generate random IV
79
+ const iv = this._crypto.getRandomValues(new Uint8Array(CryptoConstants.GCM_IV_SIZE));
80
+ // Import the key
81
+ const cryptoKey = await this._crypto.subtle.importKey('raw', toArrayBuffer(key), { name: 'AES-GCM' }, false, ['encrypt']);
82
+ // Encode plaintext to bytes
83
+ const encoder = new TextEncoder();
84
+ const plaintextBytes = encoder.encode(plaintext);
85
+ // Encrypt (Web Crypto appends auth tag to ciphertext)
86
+ const encryptedWithTag = await this._crypto.subtle.encrypt({
87
+ name: 'AES-GCM',
88
+ iv: iv,
89
+ tagLength: CryptoConstants.GCM_AUTH_TAG_SIZE * 8 // bits
90
+ }, cryptoKey, plaintextBytes);
91
+ // Split ciphertext and auth tag (auth tag is last 16 bytes)
92
+ const encryptedArray = new Uint8Array(encryptedWithTag);
93
+ const encryptedData = encryptedArray.slice(0, encryptedArray.length - CryptoConstants.GCM_AUTH_TAG_SIZE);
94
+ const authTag = encryptedArray.slice(encryptedArray.length - CryptoConstants.GCM_AUTH_TAG_SIZE);
95
+ return ts_utils_1.Success.with({
96
+ iv,
97
+ authTag,
98
+ encryptedData
99
+ });
100
+ }
101
+ catch (e) {
102
+ const message = e instanceof Error ? e.message : String(e);
103
+ return ts_utils_1.Failure.with(`Encryption failed: ${message}`);
104
+ }
105
+ }
106
+ /**
107
+ * Decrypts ciphertext using AES-256-GCM.
108
+ * @param encryptedData - Encrypted bytes
109
+ * @param key - 32-byte decryption key
110
+ * @param iv - Initialization vector (12 bytes)
111
+ * @param authTag - GCM authentication tag (16 bytes)
112
+ * @returns `Success` with decrypted UTF-8 string, or `Failure` with an error.
113
+ */
114
+ async decrypt(encryptedData, key, iv, authTag) {
115
+ if (key.length !== CryptoConstants.AES_256_KEY_SIZE) {
116
+ return ts_utils_1.Failure.with(`Key must be ${CryptoConstants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
117
+ }
118
+ if (iv.length !== CryptoConstants.GCM_IV_SIZE) {
119
+ return ts_utils_1.Failure.with(`IV must be ${CryptoConstants.GCM_IV_SIZE} bytes, got ${iv.length}`);
120
+ }
121
+ if (authTag.length !== CryptoConstants.GCM_AUTH_TAG_SIZE) {
122
+ return ts_utils_1.Failure.with(`Auth tag must be ${CryptoConstants.GCM_AUTH_TAG_SIZE} bytes, got ${authTag.length}`);
123
+ }
124
+ try {
125
+ // Import the key
126
+ const cryptoKey = await this._crypto.subtle.importKey('raw', toArrayBuffer(key), { name: 'AES-GCM' }, false, ['decrypt']);
127
+ // Web Crypto expects ciphertext + auth tag concatenated
128
+ const encryptedWithTag = new Uint8Array(encryptedData.length + authTag.length);
129
+ encryptedWithTag.set(encryptedData);
130
+ encryptedWithTag.set(authTag, encryptedData.length);
131
+ // Decrypt
132
+ const decrypted = await this._crypto.subtle.decrypt({
133
+ name: 'AES-GCM',
134
+ iv: toArrayBuffer(iv),
135
+ tagLength: CryptoConstants.GCM_AUTH_TAG_SIZE * 8 // bits
136
+ }, cryptoKey, encryptedWithTag);
137
+ // Decode to string
138
+ const decoder = new TextDecoder();
139
+ return ts_utils_1.Success.with(decoder.decode(decrypted));
140
+ }
141
+ catch (e) {
142
+ const message = e instanceof Error ? e.message : String(e);
143
+ return ts_utils_1.Failure.with(`Decryption failed: ${message}`);
144
+ }
145
+ }
146
+ /**
147
+ * Generates a random 32-byte key suitable for AES-256.
148
+ * @returns Success with generated key, or Failure with error
149
+ */
150
+ async generateKey() {
151
+ try {
152
+ return ts_utils_1.Success.with(this._crypto.getRandomValues(new Uint8Array(CryptoConstants.AES_256_KEY_SIZE)));
153
+ }
154
+ catch (e) {
155
+ const message = e instanceof Error ? e.message : String(e);
156
+ return ts_utils_1.Failure.with(`Key generation failed: ${message}`);
157
+ }
158
+ }
159
+ /**
160
+ * Derives a key from a password using PBKDF2.
161
+ * @param password - Password string
162
+ * @param salt - Salt bytes (should be at least 16 bytes)
163
+ * @param iterations - Number of iterations (recommend 100000+)
164
+ * @returns Success with derived 32-byte key, or Failure with error
165
+ */
166
+ async deriveKey(password, salt, iterations) {
167
+ if (iterations < 1) {
168
+ return ts_utils_1.Failure.with('Iterations must be at least 1');
169
+ }
170
+ if (salt.length < 8) {
171
+ return ts_utils_1.Failure.with('Salt should be at least 8 bytes');
172
+ }
173
+ try {
174
+ // Encode password
175
+ const encoder = new TextEncoder();
176
+ const passwordBytes = encoder.encode(password);
177
+ // Import password as key material
178
+ const keyMaterial = await this._crypto.subtle.importKey('raw', passwordBytes, 'PBKDF2', false, [
179
+ 'deriveBits'
180
+ ]);
181
+ // Derive key bits
182
+ const derivedBits = await this._crypto.subtle.deriveBits({
183
+ name: 'PBKDF2',
184
+ salt: toArrayBuffer(salt),
185
+ iterations: iterations,
186
+ hash: 'SHA-256'
187
+ }, keyMaterial, CryptoConstants.AES_256_KEY_SIZE * 8 // bits
188
+ );
189
+ return ts_utils_1.Success.with(new Uint8Array(derivedBits));
190
+ }
191
+ catch (e) {
192
+ const message = e instanceof Error ? e.message : String(e);
193
+ return ts_utils_1.Failure.with(`Key derivation failed: ${message}`);
194
+ }
195
+ }
196
+ // ============================================================================
197
+ // Platform Utility Methods
198
+ // ============================================================================
199
+ /**
200
+ * Generates cryptographically secure random bytes.
201
+ * @param length - Number of bytes to generate
202
+ * @returns Success with random bytes, or Failure with error
203
+ */
204
+ generateRandomBytes(length) {
205
+ if (length < 1) {
206
+ return ts_utils_1.Failure.with('Length must be at least 1');
207
+ }
208
+ try {
209
+ return ts_utils_1.Success.with(this._crypto.getRandomValues(new Uint8Array(length)));
210
+ }
211
+ catch (e) {
212
+ const message = e instanceof Error ? e.message : String(e);
213
+ return ts_utils_1.Failure.with(`Random bytes generation failed: ${message}`);
214
+ }
215
+ }
216
+ /**
217
+ * Encodes binary data to base64 string.
218
+ * @param data - Binary data to encode
219
+ * @returns Base64-encoded string
220
+ */
221
+ toBase64(data) {
222
+ // Convert Uint8Array to binary string, then to base64
223
+ let binary = '';
224
+ for (let i = 0; i < data.length; i++) {
225
+ binary += String.fromCharCode(data[i]);
226
+ }
227
+ return btoa(binary);
228
+ }
229
+ /**
230
+ * Decodes base64 string to binary data.
231
+ * @param base64 - Base64-encoded string
232
+ * @returns Success with decoded bytes, or Failure if invalid base64
233
+ */
234
+ fromBase64(base64) {
235
+ try {
236
+ const binary = atob(base64);
237
+ const bytes = new Uint8Array(binary.length);
238
+ for (let i = 0; i < binary.length; i++) {
239
+ bytes[i] = binary.charCodeAt(i);
240
+ }
241
+ return ts_utils_1.Success.with(bytes);
242
+ }
243
+ catch (e) {
244
+ return ts_utils_1.Failure.with('Invalid base64 string');
245
+ }
246
+ }
247
+ }
248
+ exports.BrowserCryptoProvider = BrowserCryptoProvider;
249
+ /**
250
+ * Creates a {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider} if Web
251
+ * Crypto API is available.
252
+ * @returns `Success` with provider, or `Failure` if not available
253
+ * @public
254
+ */
255
+ function createBrowserCryptoProvider() {
256
+ return (0, ts_utils_1.captureResult)(() => new BrowserCryptoProvider());
257
+ }
258
+ /* c8 ignore stop */
259
+ //# sourceMappingURL=browserCryptoProvider.js.map