@fgv/ts-web-extras 5.1.0-3 → 5.1.0-30

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 (275) hide show
  1. package/dist/packlets/crypto-utils/browserCryptoProvider.js +330 -18
  2. package/dist/packlets/crypto-utils/browserCryptoProvider.js.map +1 -1
  3. package/dist/packlets/crypto-utils/index.js +12 -0
  4. package/dist/packlets/crypto-utils/index.js.map +1 -1
  5. package/dist/packlets/file-tree/directoryHandleStore.js.map +1 -1
  6. package/dist/packlets/file-tree/fileApiTreeAccessors.js +1 -1
  7. package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  8. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js +2 -2
  9. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -1
  10. package/dist/packlets/file-tree/httpTreeAccessors.js +74 -44
  11. package/dist/packlets/file-tree/httpTreeAccessors.js.map +1 -1
  12. package/dist/packlets/file-tree/localStorageTreeAccessors.js.map +1 -1
  13. package/dist/packlets/helpers/fileTreeHelpers.js +1 -1
  14. package/dist/packlets/helpers/fileTreeHelpers.js.map +1 -1
  15. package/dist/ts-web-extras.d.ts +141 -10
  16. package/dist/tsdoc-metadata.json +1 -1
  17. package/eslint.config.js +32 -0
  18. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts +115 -6
  19. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts.map +1 -1
  20. package/lib/packlets/crypto-utils/browserCryptoProvider.js +329 -17
  21. package/lib/packlets/crypto-utils/browserCryptoProvider.js.map +1 -1
  22. package/lib/packlets/crypto-utils/index.d.ts +13 -0
  23. package/lib/packlets/crypto-utils/index.d.ts.map +1 -1
  24. package/lib/packlets/crypto-utils/index.js +13 -0
  25. package/lib/packlets/crypto-utils/index.js.map +1 -1
  26. package/lib/packlets/file-tree/directoryHandleStore.d.ts +2 -2
  27. package/lib/packlets/file-tree/directoryHandleStore.d.ts.map +1 -1
  28. package/lib/packlets/file-tree/directoryHandleStore.js.map +1 -1
  29. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts +2 -2
  30. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts.map +1 -1
  31. package/lib/packlets/file-tree/fileApiTreeAccessors.js +1 -1
  32. package/lib/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  33. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts.map +1 -1
  34. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js +2 -2
  35. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -1
  36. package/lib/packlets/file-tree/httpTreeAccessors.d.ts +6 -0
  37. package/lib/packlets/file-tree/httpTreeAccessors.d.ts.map +1 -1
  38. package/lib/packlets/file-tree/httpTreeAccessors.js +74 -44
  39. package/lib/packlets/file-tree/httpTreeAccessors.js.map +1 -1
  40. package/lib/packlets/file-tree/localStorageTreeAccessors.js.map +1 -1
  41. package/lib/packlets/helpers/fileTreeHelpers.d.ts +1 -1
  42. package/lib/packlets/helpers/fileTreeHelpers.d.ts.map +1 -1
  43. package/lib/packlets/helpers/fileTreeHelpers.js +6 -6
  44. package/lib/packlets/helpers/fileTreeHelpers.js.map +1 -1
  45. package/package.json +16 -15
  46. package/.rush/temp/61c1d4a91d98f048b475a5bb3e6541c5d27819de.tar.log +0 -237
  47. package/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +0 -55
  48. package/.rush/temp/operation/build/all.log +0 -55
  49. package/.rush/temp/operation/build/log-chunks.jsonl +0 -55
  50. package/.rush/temp/operation/build/state.json +0 -3
  51. package/.rush/temp/shrinkwrap-deps.json +0 -635
  52. package/CHANGELOG.md +0 -23
  53. package/config/api-extractor.json +0 -343
  54. package/config/jest.config.json +0 -19
  55. package/config/rig.json +0 -16
  56. package/config/typedoc.json +0 -6
  57. package/dist/test/mocks/idb-keyval.js +0 -6
  58. package/dist/test/mocks/idb-keyval.js.map +0 -1
  59. package/dist/test/setupTests.js +0 -74
  60. package/dist/test/setupTests.js.map +0 -1
  61. package/dist/test/unit/browserHashProvider.test.js +0 -140
  62. package/dist/test/unit/browserHashProvider.test.js.map +0 -1
  63. package/dist/test/unit/directoryHandleStore.test.js +0 -190
  64. package/dist/test/unit/directoryHandleStore.test.js.map +0 -1
  65. package/dist/test/unit/fileApiTreeAccessors.test.js +0 -1188
  66. package/dist/test/unit/fileApiTreeAccessors.test.js.map +0 -1
  67. package/dist/test/unit/fileApiTypes.test.js +0 -472
  68. package/dist/test/unit/fileApiTypes.test.js.map +0 -1
  69. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js +0 -622
  70. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js.map +0 -1
  71. package/dist/test/unit/fileTreeHelpers.test.js +0 -590
  72. package/dist/test/unit/fileTreeHelpers.test.js.map +0 -1
  73. package/dist/test/unit/httpTreeAccessors.test.js +0 -1229
  74. package/dist/test/unit/httpTreeAccessors.test.js.map +0 -1
  75. package/dist/test/unit/localStorageTreeAccessors.test.js +0 -812
  76. package/dist/test/unit/localStorageTreeAccessors.test.js.map +0 -1
  77. package/dist/test/unit/urlParams.test.js +0 -393
  78. package/dist/test/unit/urlParams.test.js.map +0 -1
  79. package/dist/test/utils/fileSystemAccessMocks.js +0 -271
  80. package/dist/test/utils/fileSystemAccessMocks.js.map +0 -1
  81. package/dist/test/utils/testHelpers.js +0 -124
  82. package/dist/test/utils/testHelpers.js.map +0 -1
  83. package/docs/@fgv/namespaces/CryptoUtils/README.md +0 -18
  84. package/docs/@fgv/namespaces/CryptoUtils/classes/BrowserCryptoProvider.md +0 -203
  85. package/docs/@fgv/namespaces/CryptoUtils/classes/BrowserHashProvider.md +0 -63
  86. package/docs/@fgv/namespaces/CryptoUtils/functions/createBrowserCryptoProvider.md +0 -18
  87. package/docs/@fgv/namespaces/FileTreeHelpers/README.md +0 -19
  88. package/docs/@fgv/namespaces/FileTreeHelpers/functions/extractFileListMetadata.md +0 -23
  89. package/docs/@fgv/namespaces/FileTreeHelpers/functions/extractFileMetadata.md +0 -23
  90. package/docs/@fgv/namespaces/FileTreeHelpers/functions/fromDirectoryUpload.md +0 -33
  91. package/docs/@fgv/namespaces/FileTreeHelpers/functions/fromFileList.md +0 -33
  92. package/docs/@fgv/namespaces/FileTreeHelpers/functions/getOriginalFile.md +0 -25
  93. package/docs/@fgv/namespaces/FileTreeHelpers/variables/defaultFileApiTreeInitParams.md +0 -11
  94. package/docs/README.md +0 -78
  95. package/docs/classes/DirectoryHandleStore.md +0 -116
  96. package/docs/classes/FileApiTreeAccessors.md +0 -286
  97. package/docs/classes/FileSystemAccessTreeAccessors.md +0 -557
  98. package/docs/classes/HttpTreeAccessors.md +0 -508
  99. package/docs/classes/LocalStorageTreeAccessors.md +0 -520
  100. package/docs/functions/exportAsJson.md +0 -23
  101. package/docs/functions/exportUsingFileSystemAPI.md +0 -26
  102. package/docs/functions/extractDirectoryPath.md +0 -23
  103. package/docs/functions/isDirectoryHandle.md +0 -23
  104. package/docs/functions/isFileHandle.md +0 -23
  105. package/docs/functions/isFilePath.md +0 -21
  106. package/docs/functions/parseContextFilter.md +0 -22
  107. package/docs/functions/parseQualifierDefaults.md +0 -22
  108. package/docs/functions/parseResourceTypes.md +0 -22
  109. package/docs/functions/parseUrlParameters.md +0 -15
  110. package/docs/functions/safeShowDirectoryPicker.md +0 -24
  111. package/docs/functions/safeShowOpenFilePicker.md +0 -24
  112. package/docs/functions/safeShowSaveFilePicker.md +0 -24
  113. package/docs/functions/supportsFileSystemAccess.md +0 -23
  114. package/docs/interfaces/FilePickerAcceptType.md +0 -16
  115. package/docs/interfaces/FileSystemCreateWritableOptions.md +0 -15
  116. package/docs/interfaces/FileSystemDirectoryHandle.md +0 -187
  117. package/docs/interfaces/FileSystemFileHandle.md +0 -106
  118. package/docs/interfaces/FileSystemGetDirectoryOptions.md +0 -15
  119. package/docs/interfaces/FileSystemGetFileOptions.md +0 -15
  120. package/docs/interfaces/FileSystemHandle.md +0 -69
  121. package/docs/interfaces/FileSystemHandlePermissionDescriptor.md +0 -15
  122. package/docs/interfaces/FileSystemRemoveOptions.md +0 -15
  123. package/docs/interfaces/FileSystemWritableFileStream.md +0 -127
  124. package/docs/interfaces/IDirectoryHandleTreeInitializer.md +0 -17
  125. package/docs/interfaces/IFileHandleTreeInitializer.md +0 -16
  126. package/docs/interfaces/IFileListTreeInitializer.md +0 -15
  127. package/docs/interfaces/IFileMetadata.md +0 -19
  128. package/docs/interfaces/IFileSystemAccessTreeParams.md +0 -30
  129. package/docs/interfaces/IFsAccessApis.md +0 -57
  130. package/docs/interfaces/IHttpTreeParams.md +0 -32
  131. package/docs/interfaces/ILocalStorageTreeParams.md +0 -30
  132. package/docs/interfaces/IUrlConfigOptions.md +0 -27
  133. package/docs/interfaces/ShowDirectoryPickerOptions.md +0 -17
  134. package/docs/interfaces/ShowOpenFilePickerOptions.md +0 -19
  135. package/docs/interfaces/ShowSaveFilePickerOptions.md +0 -19
  136. package/docs/type-aliases/TreeInitializer.md +0 -11
  137. package/docs/type-aliases/WellKnownDirectory.md +0 -11
  138. package/docs/type-aliases/WindowWithFsAccess.md +0 -11
  139. package/docs/variables/DEFAULT_DIRECTORY_HANDLE_DB.md +0 -11
  140. package/docs/variables/DEFAULT_DIRECTORY_HANDLE_STORE.md +0 -11
  141. package/etc/ts-web-extras.api.md +0 -433
  142. package/lib/test/mocks/idb-keyval.d.ts +0 -6
  143. package/lib/test/mocks/idb-keyval.d.ts.map +0 -1
  144. package/lib/test/mocks/idb-keyval.js +0 -9
  145. package/lib/test/mocks/idb-keyval.js.map +0 -1
  146. package/lib/test/setupTests.d.ts +0 -2
  147. package/lib/test/setupTests.d.ts.map +0 -1
  148. package/lib/test/setupTests.js +0 -76
  149. package/lib/test/setupTests.js.map +0 -1
  150. package/lib/test/unit/browserHashProvider.test.d.ts +0 -2
  151. package/lib/test/unit/browserHashProvider.test.d.ts.map +0 -1
  152. package/lib/test/unit/browserHashProvider.test.js +0 -142
  153. package/lib/test/unit/browserHashProvider.test.js.map +0 -1
  154. package/lib/test/unit/directoryHandleStore.test.d.ts +0 -2
  155. package/lib/test/unit/directoryHandleStore.test.d.ts.map +0 -1
  156. package/lib/test/unit/directoryHandleStore.test.js +0 -192
  157. package/lib/test/unit/directoryHandleStore.test.js.map +0 -1
  158. package/lib/test/unit/fileApiTreeAccessors.test.d.ts +0 -2
  159. package/lib/test/unit/fileApiTreeAccessors.test.d.ts.map +0 -1
  160. package/lib/test/unit/fileApiTreeAccessors.test.js +0 -1190
  161. package/lib/test/unit/fileApiTreeAccessors.test.js.map +0 -1
  162. package/lib/test/unit/fileApiTypes.test.d.ts +0 -2
  163. package/lib/test/unit/fileApiTypes.test.d.ts.map +0 -1
  164. package/lib/test/unit/fileApiTypes.test.js +0 -474
  165. package/lib/test/unit/fileApiTypes.test.js.map +0 -1
  166. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts +0 -2
  167. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts.map +0 -1
  168. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js +0 -624
  169. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js.map +0 -1
  170. package/lib/test/unit/fileTreeHelpers.test.d.ts +0 -2
  171. package/lib/test/unit/fileTreeHelpers.test.d.ts.map +0 -1
  172. package/lib/test/unit/fileTreeHelpers.test.js +0 -592
  173. package/lib/test/unit/fileTreeHelpers.test.js.map +0 -1
  174. package/lib/test/unit/httpTreeAccessors.test.d.ts +0 -2
  175. package/lib/test/unit/httpTreeAccessors.test.d.ts.map +0 -1
  176. package/lib/test/unit/httpTreeAccessors.test.js +0 -1231
  177. package/lib/test/unit/httpTreeAccessors.test.js.map +0 -1
  178. package/lib/test/unit/localStorageTreeAccessors.test.d.ts +0 -2
  179. package/lib/test/unit/localStorageTreeAccessors.test.d.ts.map +0 -1
  180. package/lib/test/unit/localStorageTreeAccessors.test.js +0 -814
  181. package/lib/test/unit/localStorageTreeAccessors.test.js.map +0 -1
  182. package/lib/test/unit/urlParams.test.d.ts +0 -2
  183. package/lib/test/unit/urlParams.test.d.ts.map +0 -1
  184. package/lib/test/unit/urlParams.test.js +0 -395
  185. package/lib/test/unit/urlParams.test.js.map +0 -1
  186. package/lib/test/utils/fileSystemAccessMocks.d.ts +0 -53
  187. package/lib/test/utils/fileSystemAccessMocks.d.ts.map +0 -1
  188. package/lib/test/utils/fileSystemAccessMocks.js +0 -277
  189. package/lib/test/utils/fileSystemAccessMocks.js.map +0 -1
  190. package/lib/test/utils/testHelpers.d.ts +0 -51
  191. package/lib/test/utils/testHelpers.d.ts.map +0 -1
  192. package/lib/test/utils/testHelpers.js +0 -133
  193. package/lib/test/utils/testHelpers.js.map +0 -1
  194. package/rush-logs/ts-web-extras.build.cache.log +0 -3
  195. package/rush-logs/ts-web-extras.build.log +0 -55
  196. package/src/index.browser.ts +0 -24
  197. package/src/index.ts +0 -47
  198. package/src/packlets/crypto-utils/browserCryptoProvider.ts +0 -311
  199. package/src/packlets/crypto-utils/browserHashProvider.ts +0 -73
  200. package/src/packlets/crypto-utils/index.ts +0 -29
  201. package/src/packlets/file-api-types/index.ts +0 -366
  202. package/src/packlets/file-tree/directoryHandleStore.ts +0 -136
  203. package/src/packlets/file-tree/fileApiTreeAccessors.ts +0 -528
  204. package/src/packlets/file-tree/fileSystemAccessTreeAccessors.ts +0 -519
  205. package/src/packlets/file-tree/httpTreeAccessors.ts +0 -448
  206. package/src/packlets/file-tree/index.ts +0 -32
  207. package/src/packlets/file-tree/localStorageTreeAccessors.ts +0 -430
  208. package/src/packlets/helpers/fileTreeHelpers.ts +0 -107
  209. package/src/packlets/helpers/index.ts +0 -28
  210. package/src/packlets/url-utils/index.ts +0 -28
  211. package/src/packlets/url-utils/urlParams.ts +0 -245
  212. package/src/test/mocks/idb-keyval.ts +0 -5
  213. package/src/test/setupTests.ts +0 -87
  214. package/src/test/unit/browserHashProvider.test.ts +0 -155
  215. package/src/test/unit/browserHashProvider.test.ts.bak +0 -376
  216. package/src/test/unit/directoryHandleStore.test.ts +0 -251
  217. package/src/test/unit/fileApiTreeAccessors.test.ts +0 -1387
  218. package/src/test/unit/fileApiTypes.test.ts +0 -587
  219. package/src/test/unit/fileSystemAccessTreeAccessors.test.ts +0 -885
  220. package/src/test/unit/fileTreeHelpers.test.ts +0 -694
  221. package/src/test/unit/httpTreeAccessors.test.ts +0 -1571
  222. package/src/test/unit/localStorageTreeAccessors.test.ts +0 -1014
  223. package/src/test/unit/urlParams.test.ts +0 -464
  224. package/src/test/utils/fileSystemAccessMocks.ts +0 -353
  225. package/src/test/utils/testHelpers.ts +0 -155
  226. package/temp/build/typescript/ts_8nwakTlr.json +0 -1
  227. package/temp/coverage/base.css +0 -224
  228. package/temp/coverage/block-navigation.js +0 -87
  229. package/temp/coverage/crypto-utils/browserCryptoProvider.ts.html +0 -1018
  230. package/temp/coverage/crypto-utils/browserHashProvider.ts.html +0 -304
  231. package/temp/coverage/crypto-utils/index.html +0 -131
  232. package/temp/coverage/favicon.png +0 -0
  233. package/temp/coverage/file-tree/directoryHandleStore.ts.html +0 -493
  234. package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +0 -1669
  235. package/temp/coverage/file-tree/fileSystemAccessTreeAccessors.ts.html +0 -1642
  236. package/temp/coverage/file-tree/httpTreeAccessors.ts.html +0 -1429
  237. package/temp/coverage/file-tree/index.html +0 -176
  238. package/temp/coverage/file-tree/localStorageTreeAccessors.ts.html +0 -1375
  239. package/temp/coverage/helpers/fileTreeHelpers.ts.html +0 -406
  240. package/temp/coverage/helpers/index.html +0 -116
  241. package/temp/coverage/index.html +0 -161
  242. package/temp/coverage/lcov-report/base.css +0 -224
  243. package/temp/coverage/lcov-report/block-navigation.js +0 -87
  244. package/temp/coverage/lcov-report/crypto-utils/browserCryptoProvider.ts.html +0 -1018
  245. package/temp/coverage/lcov-report/crypto-utils/browserHashProvider.ts.html +0 -304
  246. package/temp/coverage/lcov-report/crypto-utils/index.html +0 -131
  247. package/temp/coverage/lcov-report/favicon.png +0 -0
  248. package/temp/coverage/lcov-report/file-tree/directoryHandleStore.ts.html +0 -493
  249. package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +0 -1669
  250. package/temp/coverage/lcov-report/file-tree/fileSystemAccessTreeAccessors.ts.html +0 -1642
  251. package/temp/coverage/lcov-report/file-tree/httpTreeAccessors.ts.html +0 -1429
  252. package/temp/coverage/lcov-report/file-tree/index.html +0 -176
  253. package/temp/coverage/lcov-report/file-tree/localStorageTreeAccessors.ts.html +0 -1375
  254. package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +0 -406
  255. package/temp/coverage/lcov-report/helpers/index.html +0 -116
  256. package/temp/coverage/lcov-report/index.html +0 -161
  257. package/temp/coverage/lcov-report/prettify.css +0 -1
  258. package/temp/coverage/lcov-report/prettify.js +0 -2
  259. package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  260. package/temp/coverage/lcov-report/sorter.js +0 -210
  261. package/temp/coverage/lcov-report/url-utils/index.html +0 -116
  262. package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +0 -820
  263. package/temp/coverage/lcov.info +0 -3597
  264. package/temp/coverage/prettify.css +0 -1
  265. package/temp/coverage/prettify.js +0 -2
  266. package/temp/coverage/sort-arrow-sprite.png +0 -0
  267. package/temp/coverage/sorter.js +0 -210
  268. package/temp/coverage/url-utils/index.html +0 -116
  269. package/temp/coverage/url-utils/urlParams.ts.html +0 -820
  270. package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
  271. package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/d6/package_d6e3b0fa94752e16b0b2a2777739b973 +0 -53
  272. package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +0 -1
  273. package/temp/ts-web-extras.api.json +0 -8850
  274. package/temp/ts-web-extras.api.md +0 -433
  275. package/tsconfig.json +0 -7
@@ -1,519 +0,0 @@
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
-
23
- import {
24
- Result,
25
- succeed,
26
- fail,
27
- captureResult,
28
- DetailedResult,
29
- succeedWithDetail,
30
- Logging
31
- } from '@fgv/ts-utils';
32
- import { FileTree } from '@fgv/ts-json-base';
33
- import { FileSystemDirectoryHandle, FileSystemFileHandle } from '../file-api-types';
34
-
35
- /**
36
- * Options for creating persistent file trees.
37
- * @public
38
- */
39
- export interface IFileSystemAccessTreeParams<TCT extends string = string>
40
- extends FileTree.IFileTreeInitParams<TCT> {
41
- /**
42
- * Automatically sync changes to disk immediately after each save.
43
- * If false, changes are batched and written on explicit syncToDisk() call.
44
- * @defaultValue false
45
- */
46
- autoSync?: boolean;
47
-
48
- /**
49
- * Require write permission on the directory handle.
50
- * If true, fails if write permission cannot be obtained.
51
- * If false, falls back to read-only mode.
52
- * @defaultValue true
53
- */
54
- requireWritePermission?: boolean;
55
-
56
- /**
57
- * Override the path at which the file is stored in the tree (for fromFileHandle).
58
- * Must be an absolute path (e.g., '/data/confections/common.yaml').
59
- * If omitted, defaults to `/<filename>`.
60
- */
61
- filePath?: string;
62
-
63
- /** Optional logger for auto-sync and persistence failures. */
64
- logger?: Logging.LogReporter<unknown>;
65
- }
66
-
67
- /**
68
- * Implementation of `FileTree.IPersistentFileTreeAccessors` that uses the File System Access API
69
- * to provide persistent file editing in browsers.
70
- * @public
71
- */
72
- export class FileSystemAccessTreeAccessors<TCT extends string = string>
73
- extends FileTree.InMemoryTreeAccessors<TCT>
74
- implements FileTree.IPersistentFileTreeAccessors<TCT>
75
- {
76
- private readonly _handles: Map<string, FileSystemFileHandle>;
77
- private readonly _rootDir: FileSystemDirectoryHandle;
78
- private readonly _dirtyFiles: Set<string>;
79
- private readonly _pendingDeletions: Set<string>;
80
- private readonly _autoSync: boolean;
81
- private readonly _hasWritePermission: boolean;
82
- private readonly _logger: Logging.LogReporter<unknown>;
83
-
84
- /**
85
- * Protected constructor for FileSystemAccessTreeAccessors.
86
- * @param files - An array of in-memory files to include in the tree.
87
- * @param rootDir - The root directory handle.
88
- * @param handles - Map of file paths to their handles.
89
- * @param params - Optional params for the tree.
90
- * @param hasWritePermission - Whether write permission was granted.
91
- */
92
- protected constructor(
93
- files: FileTree.IInMemoryFile<TCT>[],
94
- rootDir: FileSystemDirectoryHandle,
95
- handles: Map<string, FileSystemFileHandle>,
96
- params: IFileSystemAccessTreeParams<TCT> | undefined,
97
- hasWritePermission: boolean
98
- ) {
99
- super(files, params);
100
- this._rootDir = rootDir;
101
- this._handles = handles;
102
- this._dirtyFiles = new Set();
103
- this._pendingDeletions = new Set();
104
- /* c8 ignore next 3 - intermittent branch coverage: ?? fallback branches in constructor */
105
- this._autoSync = params?.autoSync ?? false;
106
- this._hasWritePermission = hasWritePermission;
107
- this._logger = params?.logger ?? new Logging.LogReporter<unknown>();
108
- }
109
-
110
- private async _runAutoSyncTask(
111
- path: string,
112
- operation: 'save' | 'delete',
113
- action: () => Promise<Result<void>>
114
- ): Promise<void> {
115
- try {
116
- const result = await action();
117
- if (result.isFailure()) {
118
- this._logger.error(`Auto-${operation} failed for ${path}: ${result.message}`);
119
- }
120
- } catch (err) {
121
- this._logger.error(`Auto-${operation} threw for ${path}: ${String(err)}`);
122
- }
123
- }
124
-
125
- /**
126
- * Creates a new FileSystemAccessTreeAccessors instance from a directory handle.
127
- * @param dirHandle - The FileSystemDirectoryHandle to load files from.
128
- * @param params - Optional parameters including autoSync and permission settings.
129
- * @returns Promise resolving to a FileSystemAccessTreeAccessors instance.
130
- * @public
131
- */
132
- public static async fromDirectoryHandle<TCT extends string = string>(
133
- dirHandle: FileSystemDirectoryHandle,
134
- params?: IFileSystemAccessTreeParams<TCT>
135
- ): Promise<Result<FileSystemAccessTreeAccessors<TCT>>> {
136
- try {
137
- // Check write permission
138
- const hasWritePermission = await this._checkWritePermission(dirHandle);
139
-
140
- /* c8 ignore next 3 - coverage intermittently missed in full suite */
141
- if (!hasWritePermission && (params?.requireWritePermission ?? true)) {
142
- return fail('Write permission required but not granted');
143
- }
144
-
145
- // Load all files from the directory (always use '/' as base, prefix is handled by parent)
146
- const { files, handles } = await this._loadDirectory<TCT>(dirHandle, '/', params);
147
-
148
- // Enable tree mutability when write permission is granted and caller didn't explicitly set mutable
149
- const effectiveParams: IFileSystemAccessTreeParams<TCT> = {
150
- ...params,
151
- mutable: params?.mutable ?? (hasWritePermission ? true : false)
152
- };
153
-
154
- return succeed(
155
- new FileSystemAccessTreeAccessors<TCT>(files, dirHandle, handles, effectiveParams, hasWritePermission)
156
- );
157
- /* c8 ignore next 4 - defensive: outer catch for unexpected errors */
158
- } catch (error) {
159
- const message = error instanceof Error ? error.message : String(error);
160
- return fail(`Failed to create FileSystemAccessTreeAccessors: ${message}`);
161
- }
162
- }
163
-
164
- /**
165
- * Creates a new FileSystemAccessTreeAccessors instance from a single file handle.
166
- *
167
- * The resulting tree contains exactly one file at `/<filename>`.
168
- * `syncToDisk()` writes changes back to the original file via the File System Access API.
169
- * New file creation is not supported on this tree (no parent directory handle).
170
- *
171
- * @param fileHandle - The FileSystemFileHandle to load.
172
- * @param params - Optional parameters including autoSync and permission settings.
173
- * @returns Promise resolving to a FileSystemAccessTreeAccessors instance.
174
- * @public
175
- */
176
- public static async fromFileHandle<TCT extends string = string>(
177
- fileHandle: FileSystemFileHandle,
178
- params?: IFileSystemAccessTreeParams<TCT>
179
- ): Promise<Result<FileSystemAccessTreeAccessors<TCT>>> {
180
- try {
181
- const hasWritePermission = await this._checkFileWritePermission(fileHandle);
182
-
183
- /* c8 ignore next 1 - intermittent branch coverage: ?? true fallback */
184
- if (!hasWritePermission && (params?.requireWritePermission ?? true)) {
185
- return fail('Write permission required but not granted');
186
- }
187
-
188
- const file = await fileHandle.getFile();
189
- const contents = await file.text();
190
- const path = params?.filePath ?? `/${fileHandle.name}`;
191
- /* c8 ignore next 3 - intermittent branch coverage: ternary for inferContentType */
192
- const contentType = params?.inferContentType
193
- ? params.inferContentType(path, file.type).orDefault()
194
- : undefined;
195
-
196
- const files: FileTree.IInMemoryFile<TCT>[] = [{ path, contents, contentType }];
197
- const handles = new Map<string, FileSystemFileHandle>([[path, fileHandle]]);
198
-
199
- const dummyRoot = {} as FileSystemDirectoryHandle;
200
- const effectiveParams: IFileSystemAccessTreeParams<TCT> = {
201
- ...params,
202
- /* c8 ignore next 1 - intermittent branch coverage: ?? false fallback */
203
- mutable: hasWritePermission ? true : params?.mutable ?? false
204
- };
205
-
206
- return succeed(
207
- new FileSystemAccessTreeAccessors<TCT>(files, dummyRoot, handles, effectiveParams, hasWritePermission)
208
- );
209
- } catch (error) {
210
- /* c8 ignore next 1 - intermittent branch coverage: error instanceof Error false branch */
211
- const message = error instanceof Error ? error.message : String(error);
212
- return fail(`Failed to create FileSystemAccessTreeAccessors from file: ${message}`);
213
- }
214
- }
215
-
216
- /**
217
- * Check if the directory handle has write permission.
218
- * @param handle - The directory handle to check.
219
- * @returns Promise resolving to true if write permission is granted.
220
- * @internal
221
- */
222
- private static async _checkWritePermission(handle: FileSystemDirectoryHandle): Promise<boolean> {
223
- try {
224
- const permission = await handle.queryPermission({ mode: 'readwrite' });
225
-
226
- if (permission === 'granted') {
227
- return true;
228
- }
229
-
230
- if (permission === 'prompt') {
231
- const requested = await handle.requestPermission({ mode: 'readwrite' });
232
- return requested === 'granted';
233
- }
234
-
235
- return false;
236
- } catch (error) {
237
- // Permission API might not be available or might throw
238
- return false;
239
- }
240
- }
241
-
242
- /**
243
- * Check if the file handle has write permission.
244
- * @param handle - The file handle to check.
245
- * @returns Promise resolving to true if write permission is granted.
246
- * @internal
247
- */
248
- private static async _checkFileWritePermission(handle: FileSystemFileHandle): Promise<boolean> {
249
- try {
250
- const permission = await handle.queryPermission({ mode: 'readwrite' });
251
-
252
- if (permission === 'granted') {
253
- return true;
254
- }
255
-
256
- if (permission === 'prompt') {
257
- const requested = await handle.requestPermission({ mode: 'readwrite' });
258
- return requested === 'granted';
259
- }
260
-
261
- return false;
262
- } catch (error) {
263
- return false;
264
- }
265
- }
266
-
267
- /**
268
- * Load all files from a directory handle recursively.
269
- * @param dirHandle - The directory handle to load from.
270
- * @param basePath - The base path for files.
271
- * @param params - Optional parameters.
272
- * @returns Promise resolving to files and handles.
273
- * @internal
274
- */
275
- private static async _loadDirectory<TCT extends string = string>(
276
- dirHandle: FileSystemDirectoryHandle,
277
- basePath: string,
278
- params?: IFileSystemAccessTreeParams<TCT>
279
- ): Promise<{ files: FileTree.IInMemoryFile<TCT>[]; handles: Map<string, FileSystemFileHandle> }> {
280
- const files: FileTree.IInMemoryFile<TCT>[] = [];
281
- const handles = new Map<string, FileSystemFileHandle>();
282
-
283
- for await (const [name, handle] of dirHandle.entries()) {
284
- if (handle.kind === 'file') {
285
- const fileHandle = handle as FileSystemFileHandle;
286
- const file = await fileHandle.getFile();
287
- const contents = await file.text();
288
- const path = this._joinPath(basePath, name);
289
- /* c8 ignore next 2 - coverage intermittently missed in full suite */
290
- const contentType = params?.inferContentType
291
- ? params.inferContentType(path, file.type).orDefault()
292
- : undefined;
293
-
294
- files.push({ path, contents, contentType });
295
- handles.set(path, fileHandle);
296
- } else if (handle.kind === 'directory') {
297
- const subDirHandle = handle as FileSystemDirectoryHandle;
298
- const subPath = this._joinPath(basePath, name);
299
- const subResult = await this._loadDirectory<TCT>(subDirHandle, subPath, params);
300
- files.push(...subResult.files);
301
- for (const [path, fileHandle] of subResult.handles) {
302
- handles.set(path, fileHandle);
303
- }
304
- }
305
- }
306
-
307
- return { files, handles };
308
- }
309
-
310
- /**
311
- * Join path segments.
312
- * @param base - The base path.
313
- * @param name - The name to append.
314
- * @returns The joined path.
315
- * @internal
316
- */
317
- private static _joinPath(base: string, name: string): string {
318
- const normalized = base.endsWith('/') ? base.slice(0, -1) : base;
319
- return `${normalized}/${name}`;
320
- }
321
-
322
- /**
323
- * Implements `FileTree.IPersistentFileTreeAccessors.syncToDisk`
324
- */
325
- public async syncToDisk(): Promise<Result<void>> {
326
- if (!this._hasWritePermission) {
327
- return fail('Write permission not granted - cannot sync to disk');
328
- }
329
-
330
- const errors: string[] = [];
331
-
332
- // Process pending deletions from disk
333
- for (const path of this._pendingDeletions) {
334
- const deleteResult = await this._deleteFileFromDisk(path);
335
- if (deleteResult.isFailure()) {
336
- errors.push(`delete ${path}: ${deleteResult.message}`);
337
- }
338
- }
339
-
340
- for (const path of this._dirtyFiles) {
341
- const syncResult = await this._syncFile(path);
342
- if (syncResult.isFailure()) {
343
- errors.push(`${path}: ${syncResult.message}`);
344
- }
345
- }
346
-
347
- if (errors.length > 0) {
348
- return fail(`Failed to sync ${errors.length} file(s):\n${errors.join('\n')}`);
349
- }
350
-
351
- this._pendingDeletions.clear();
352
- this._dirtyFiles.clear();
353
- return succeed(undefined);
354
- }
355
-
356
- /**
357
- * Implements `FileTree.IPersistentFileTreeAccessors.isDirty`
358
- */
359
- public isDirty(): boolean {
360
- return this._dirtyFiles.size > 0 || this._pendingDeletions.size > 0;
361
- }
362
-
363
- /**
364
- * Implements `FileTree.IPersistentFileTreeAccessors.getDirtyPaths`
365
- */
366
- public getDirtyPaths(): string[] {
367
- return [...Array.from(this._dirtyFiles), ...Array.from(this._pendingDeletions)];
368
- }
369
-
370
- /**
371
- * Override deleteFile to track pending deletions for syncToDisk.
372
- */
373
- public deleteFile(path: string): Result<boolean> {
374
- const result = super.deleteFile(path);
375
- if (result.isSuccess()) {
376
- this._dirtyFiles.delete(path);
377
- this._handles.delete(path);
378
-
379
- if (this._hasWritePermission) {
380
- this._pendingDeletions.add(path);
381
-
382
- if (this._autoSync) {
383
- void this._runAutoSyncTask(path, 'delete', () => this._deleteFileFromDisk(path));
384
- }
385
- }
386
- }
387
- return result;
388
- }
389
-
390
- /**
391
- * Implements `FileTree.IMutableFileTreeAccessors.saveFileContents`
392
- */
393
- public saveFileContents(path: string, contents: string): Result<string> {
394
- // Call parent to update in-memory state
395
- const result = super.saveFileContents(path, contents);
396
-
397
- if (result.isSuccess() && this._hasWritePermission) {
398
- this._dirtyFiles.add(path);
399
-
400
- // Auto-sync if enabled
401
- if (this._autoSync) {
402
- // Fire and log-on-failure; don't block the save path.
403
- void this._runAutoSyncTask(path, 'save', () => this._syncFile(path));
404
- }
405
- }
406
-
407
- return result;
408
- }
409
-
410
- /**
411
- * Implements `FileTree.IMutableFileTreeAccessors.fileIsMutable`
412
- */
413
- public fileIsMutable(path: string): DetailedResult<boolean, FileTree.SaveDetail> {
414
- const baseResult = super.fileIsMutable(path);
415
-
416
- if (baseResult.isSuccess() && baseResult.detail === 'transient' && this._hasWritePermission) {
417
- // Upgrade to 'persistent' if we have write permission
418
- return succeedWithDetail(true, 'persistent');
419
- }
420
-
421
- return baseResult;
422
- }
423
-
424
- /**
425
- * Sync a single file to disk.
426
- * @param path - The path of the file to sync.
427
- * @returns Promise resolving to success or failure.
428
- * @internal
429
- */
430
- private async _syncFile(path: string): Promise<Result<void>> {
431
- const handle = this._handles.get(path);
432
- if (!handle) {
433
- return this._createAndWriteFile(path);
434
- }
435
-
436
- const contents = this.getFileContents(path);
437
- if (contents.isFailure()) {
438
- return fail(contents.message);
439
- }
440
-
441
- try {
442
- const writable = await handle.createWritable();
443
- await writable.write(contents.value);
444
- await writable.close();
445
- return succeed(undefined);
446
- /* c8 ignore next 4 - coverage intermittently missed in full suite */
447
- } catch (error) {
448
- const message = error instanceof Error ? error.message : String(error);
449
- return fail(`Failed to write file: ${message}`);
450
- }
451
- }
452
-
453
- /**
454
- * Delete a file from disk using the File System Access API.
455
- * @param path - The path of the file to delete.
456
- * @returns Promise resolving to success or failure.
457
- * @internal
458
- */
459
- private async _deleteFileFromDisk(path: string): Promise<Result<void>> {
460
- try {
461
- const absolutePath = this.resolveAbsolutePath(path);
462
- const parts = absolutePath.split('/').filter((p) => p.length > 0);
463
- const filename = parts.pop();
464
-
465
- /* c8 ignore next 3 - defensive: invalid path */
466
- if (!filename) {
467
- return fail(`Invalid file path: ${path}`);
468
- }
469
-
470
- // Navigate to the parent directory
471
- let currentDir = this._rootDir;
472
- for (const part of parts) {
473
- currentDir = await currentDir.getDirectoryHandle(part);
474
- }
475
-
476
- await currentDir.removeEntry(filename);
477
- return succeed(undefined);
478
- } catch (error) {
479
- const message = error instanceof Error ? error.message : String(error);
480
- return fail(`Failed to delete file ${path}: ${message}`);
481
- }
482
- }
483
-
484
- /**
485
- * Create a new file and write its contents.
486
- * @param path - The path of the file to create.
487
- * @returns Promise resolving to success or failure.
488
- * @internal
489
- */
490
- private async _createAndWriteFile(path: string): Promise<Result<void>> {
491
- try {
492
- // Parse path to get directory and filename
493
- const absolutePath = this.resolveAbsolutePath(path);
494
- const parts = absolutePath.split('/').filter((p) => p.length > 0);
495
- const filename = parts.pop();
496
-
497
- /* c8 ignore next 3 - coverage intermittently missed in full suite */
498
- if (!filename) {
499
- return fail(`Invalid file path: ${path}`);
500
- }
501
-
502
- // Navigate/create directory structure
503
- let currentDir = this._rootDir;
504
- for (const part of parts) {
505
- currentDir = await currentDir.getDirectoryHandle(part, { create: true });
506
- }
507
-
508
- // Create file and write contents
509
- const fileHandle = await currentDir.getFileHandle(filename, { create: true });
510
- this._handles.set(path, fileHandle);
511
-
512
- return this._syncFile(path);
513
- /* c8 ignore next 4 - coverage intermittently missed in full suite */
514
- } catch (error) {
515
- const message = error instanceof Error ? error.message : String(error);
516
- return fail(`Failed to create file ${path}: ${message}`);
517
- }
518
- }
519
- }