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

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,448 +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 { DetailedResult, fail, type Result, succeed, succeedWithDetail, Logging } from '@fgv/ts-utils';
24
- import { FileTree } from '@fgv/ts-json-base';
25
-
26
- interface IHttpStorageTreeItem {
27
- readonly path: string;
28
- readonly name: string;
29
- readonly type: 'file' | 'directory';
30
- }
31
-
32
- interface IHttpStorageTreeChildrenResponse {
33
- readonly path: string;
34
- readonly children: ReadonlyArray<IHttpStorageTreeItem>;
35
- }
36
-
37
- interface IHttpStorageFileResponse {
38
- readonly path: string;
39
- readonly contents: string;
40
- readonly contentType?: string;
41
- }
42
-
43
- interface IHttpStorageSyncResponse {
44
- readonly synced: number;
45
- }
46
-
47
- function normalizeFetch(fetchImpl?: typeof fetch): typeof fetch {
48
- const resolved = fetchImpl ?? globalThis.fetch;
49
- return resolved.bind(globalThis) as typeof fetch;
50
- }
51
-
52
- /**
53
- * Configuration for creating HTTP-backed tree accessors.
54
- * @public
55
- */
56
- export interface IHttpTreeParams<TCT extends string = string> extends FileTree.IFileTreeInitParams<TCT> {
57
- readonly baseUrl: string;
58
- readonly namespace?: string;
59
- readonly autoSync?: boolean;
60
- readonly fetchImpl?: typeof fetch;
61
- readonly userId?: string;
62
- readonly logger?: Logging.LogReporter<unknown>;
63
- }
64
-
65
- /**
66
- * HTTP-backed file tree accessors that cache data in memory and persist via REST API.
67
- * @public
68
- */
69
- export class HttpTreeAccessors<TCT extends string = string>
70
- extends FileTree.InMemoryTreeAccessors<TCT>
71
- implements FileTree.IPersistentFileTreeAccessors<TCT>
72
- {
73
- private readonly _baseUrl: string;
74
- private readonly _namespace: string | undefined;
75
- private readonly _fetchImpl: typeof fetch;
76
- private readonly _dirtyFiles: Set<string> = new Set();
77
- private readonly _pendingDeletions: Set<string> = new Set();
78
- private readonly _autoSync: boolean;
79
- private readonly _userId: string | undefined;
80
- private readonly _logger: Logging.LogReporter<unknown>;
81
-
82
- /** Guards against concurrent syncToDisk calls (thundering herd from autoSync). */
83
- private _syncPromise: Promise<Result<void>> | undefined;
84
-
85
- private constructor(files: FileTree.IInMemoryFile<TCT>[], params: IHttpTreeParams<TCT>) {
86
- super(files, params);
87
- this._baseUrl = params.baseUrl.replace(/\/$/, '');
88
- this._namespace = params.namespace;
89
- this._fetchImpl = normalizeFetch(params.fetchImpl);
90
- this._autoSync = params.autoSync ?? false;
91
- this._userId = params.userId;
92
- this._logger = params.logger ?? new Logging.LogReporter<unknown>();
93
- }
94
-
95
- private async _runAutoSyncTask(path: string): Promise<void> {
96
- try {
97
- const result = await this.syncToDisk();
98
- if (result.isFailure()) {
99
- this._logger.error(`Auto-sync failed for ${path}: ${result.message}`);
100
- }
101
- } catch (err) {
102
- this._logger.error(`Auto-sync threw for ${path}: ${String(err)}`);
103
- }
104
- }
105
-
106
- /**
107
- * Creates a new HttpTreeAccessors instance from an HTTP backend.
108
- * @param params - Configuration parameters for the HTTP tree accessors.
109
- * @returns A promise that resolves to a result containing the new HttpTreeAccessors instance or an error message.
110
- */
111
- public static async fromHttp<TCT extends string = string>(
112
- params: IHttpTreeParams<TCT>
113
- ): Promise<Result<HttpTreeAccessors<TCT>>> {
114
- const filesResult = await this._loadFiles<TCT>(params, '/');
115
- if (filesResult.isFailure()) {
116
- return fail(filesResult.message);
117
- }
118
- return succeed(new HttpTreeAccessors<TCT>(filesResult.value, params));
119
- }
120
-
121
- /**
122
- * Synchronizes all dirty files to the HTTP backend.
123
- *
124
- * Uses a concurrency guard: if a sync is already in progress, callers
125
- * await the existing operation rather than starting a parallel one.
126
- * This prevents the thundering herd that occurs when autoSync fires
127
- * for every file written during a bulk operation (e.g. restore).
128
- *
129
- * @returns A promise that resolves to a result indicating success or failure.
130
- */
131
- public async syncToDisk(): Promise<Result<void>> {
132
- if (this._syncPromise) {
133
- return this._syncPromise;
134
- }
135
-
136
- this._syncPromise = this._doSync().finally(() => {
137
- this._syncPromise = undefined;
138
- });
139
- return this._syncPromise;
140
- }
141
-
142
- private async _doSync(): Promise<Result<void>> {
143
- if (this._dirtyFiles.size === 0 && this._pendingDeletions.size === 0) {
144
- return succeed(undefined);
145
- }
146
-
147
- // Snapshot and clear dirty sets so that changes arriving during
148
- // the async sync are not dropped when we finish.
149
- const deletions = new Set(this._pendingDeletions);
150
- const dirty = new Set(this._dirtyFiles);
151
- this._pendingDeletions.clear();
152
- this._dirtyFiles.clear();
153
-
154
- for (const path of deletions) {
155
- const query = new URLSearchParams();
156
- query.set('path', path);
157
- if (this._namespace) {
158
- query.set('namespace', this._namespace);
159
- }
160
-
161
- const deleteResult = await this._requestWithRetry<{ deleted: boolean }>(`/file?${query.toString()}`, {
162
- method: 'DELETE'
163
- });
164
- if (deleteResult.isFailure()) {
165
- return fail(`delete ${path}: ${deleteResult.message}`);
166
- }
167
- }
168
-
169
- for (const path of dirty) {
170
- const contentsResult = this.getFileContents(path);
171
- if (contentsResult.isFailure()) {
172
- return fail(`${path}: ${contentsResult.message}`);
173
- }
174
-
175
- const body: Record<string, unknown> = {
176
- path,
177
- contents: contentsResult.value
178
- };
179
- if (this._namespace) {
180
- body.namespace = this._namespace;
181
- }
182
-
183
- const saveResult = await this._requestWithRetry<IHttpStorageFileResponse>('/file', {
184
- method: 'PUT',
185
- body: JSON.stringify(body)
186
- });
187
- if (saveResult.isFailure()) {
188
- return fail(`sync ${path}: ${saveResult.message}`);
189
- }
190
- }
191
-
192
- const syncBody: Record<string, unknown> = {};
193
- if (this._namespace) {
194
- syncBody.namespace = this._namespace;
195
- }
196
-
197
- const syncResult = await this._requestWithRetry<IHttpStorageSyncResponse>('/sync', {
198
- method: 'POST',
199
- body: JSON.stringify(syncBody)
200
- });
201
-
202
- return syncResult.isFailure() ? fail(syncResult.message) : succeed(undefined);
203
- }
204
-
205
- /**
206
- * Checks if there are any dirty files that need synchronization.
207
- * @returns True if there are dirty files, false otherwise.
208
- */
209
- public isDirty(): boolean {
210
- return this._dirtyFiles.size > 0 || this._pendingDeletions.size > 0;
211
- }
212
-
213
- /**
214
- * Gets the list of paths for all dirty files.
215
- * @returns An array of file paths that have been modified but not yet synchronized.
216
- */
217
- public getDirtyPaths(): string[] {
218
- return [...Array.from(this._dirtyFiles), ...Array.from(this._pendingDeletions)];
219
- }
220
-
221
- public deleteFile(path: string): Result<boolean> {
222
- const result = super.deleteFile(path);
223
- if (result.isFailure()) {
224
- return result;
225
- }
226
-
227
- this._dirtyFiles.delete(path);
228
- this._pendingDeletions.add(path);
229
-
230
- if (!this._autoSync) {
231
- return result;
232
- }
233
-
234
- void this._runAutoSyncTask(path);
235
- return result;
236
- }
237
-
238
- /**
239
- * Saves file contents and marks the file as dirty for synchronization.
240
- * @param path - The path to the file.
241
- * @param contents - The new contents of the file.
242
- * @returns A result indicating success or failure.
243
- */
244
- public saveFileContents(path: string, contents: string): Result<string> {
245
- const result = super.saveFileContents(path, contents);
246
- if (result.isFailure()) {
247
- return result;
248
- }
249
-
250
- this._dirtyFiles.add(path);
251
- if (!this._autoSync) {
252
- return result;
253
- }
254
-
255
- // fire-and-log-on-failure automatic sync for immediate persistence workflow
256
- void this._runAutoSyncTask(path);
257
- return result;
258
- }
259
-
260
- /**
261
- * Checks if a file is mutable (can be modified).
262
- * @param path - The path to the file.
263
- * @returns A detailed result indicating if the file is mutable and the reason.
264
- */
265
- public fileIsMutable(path: string): DetailedResult<boolean, FileTree.SaveDetail> {
266
- const result = super.fileIsMutable(path);
267
- if (result.isSuccess() && result.value === true) {
268
- return succeedWithDetail(true, 'persistent');
269
- }
270
- return result;
271
- }
272
-
273
- /**
274
- * Makes an HTTP request to the specified resource path.
275
- * @param resourcePath - The path to the resource.
276
- * @param init - Optional request initialization options.
277
- * @returns A promise that resolves to a result containing the response data or an error message.
278
- */
279
- private async _request<T>(resourcePath: string, init?: RequestInit): Promise<Result<T>> {
280
- const response = await this._fetchImpl(`${this._baseUrl}${resourcePath}`, {
281
- headers: {
282
- 'Content-Type': 'application/json',
283
- ...(this._userId ? { 'X-User-Id': this._userId } : {}),
284
- /* c8 ignore next 1 - defensive coding: init.headers is never set by current callers */
285
- ...(init?.headers ?? {})
286
- },
287
- ...init
288
- }).catch((err: unknown) => ({ err } as const));
289
-
290
- if ('err' in response) {
291
- const message = response.err instanceof Error ? response.err.message : String(response.err);
292
- return fail(message);
293
- }
294
-
295
- if (!response.ok) {
296
- const body = await response.text().catch(() => '');
297
- const message = body
298
- ? `HTTP ${response.status}: ${body}`
299
- : `HTTP ${response.status} ${response.statusText}`;
300
- return fail(message);
301
- }
302
-
303
- const json = await response.json().catch(() => undefined);
304
- if (json === undefined) {
305
- return fail('invalid JSON response');
306
- }
307
- return succeed(json as T);
308
- }
309
-
310
- /**
311
- * Wraps `_request` with retry logic for transient failures
312
- * (network errors, 503 service unavailable, etc.).
313
- */
314
- private async _requestWithRetry<T>(resourcePath: string, init?: RequestInit): Promise<Result<T>> {
315
- const maxAttempts = 3;
316
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
317
- const result = await this._request<T>(resourcePath, init);
318
- if (result.isSuccess() || attempt === maxAttempts) {
319
- return result;
320
- }
321
- // Retry on transient-looking errors
322
- const msg = result.message;
323
- const lowerMsg = msg.toLowerCase();
324
- const isTransient =
325
- msg.includes('503') ||
326
- msg.includes('502') ||
327
- msg.includes('429') ||
328
- lowerMsg.includes('disconnect') ||
329
- lowerMsg.includes('econnreset') ||
330
- lowerMsg.includes('failed to fetch') ||
331
- lowerMsg.includes('network');
332
- if (!isTransient) {
333
- return result;
334
- }
335
- // Exponential backoff: 500ms, 1000ms
336
- const delayMs = 500 * Math.pow(2, attempt - 1);
337
- this._logger.detail(
338
- `Retrying ${
339
- init?.method ?? 'GET'
340
- } ${resourcePath} after ${delayMs}ms (attempt ${attempt}/${maxAttempts})`
341
- );
342
- await new Promise((resolve) => setTimeout(resolve, delayMs));
343
- }
344
- /* c8 ignore next 1 - defensive coding: loop always returns */
345
- return fail('retry loop exited unexpectedly');
346
- }
347
-
348
- /**
349
- * Loads files from the HTTP backend for the specified directory path.
350
- * @param params - Configuration parameters for the HTTP tree accessors.
351
- * @param directoryPath - The path to the directory to load files from.
352
- * @returns A promise that resolves to a result containing the loaded files or an error message.
353
- */
354
- private static async _loadFiles<TCT extends string = string>(
355
- params: IHttpTreeParams<TCT>,
356
- directoryPath: string
357
- ): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
358
- const childrenResult = await this._requestWithParams<IHttpStorageTreeChildrenResponse>(
359
- params,
360
- '/tree/children',
361
- {
362
- path: directoryPath,
363
- namespace: params.namespace
364
- }
365
- );
366
- if (childrenResult.isFailure()) {
367
- return fail(childrenResult.message);
368
- }
369
-
370
- const allFiles: FileTree.IInMemoryFile<TCT>[] = [];
371
-
372
- for (const item of childrenResult.value.children) {
373
- if (item.type === 'directory') {
374
- const nestedResult = await this._loadFiles(params, item.path);
375
- if (nestedResult.isFailure()) {
376
- return fail(nestedResult.message);
377
- }
378
- allFiles.push(...nestedResult.value);
379
- } else {
380
- const fileResult = await this._requestWithParams<IHttpStorageFileResponse>(params, '/file', {
381
- path: item.path,
382
- namespace: params.namespace
383
- });
384
- if (fileResult.isFailure()) {
385
- return fail(fileResult.message);
386
- }
387
-
388
- const contentType = params.inferContentType?.(item.path, fileResult.value.contentType).orDefault();
389
- allFiles.push({
390
- path: item.path,
391
- contents: fileResult.value.contents,
392
- contentType
393
- });
394
- }
395
- }
396
-
397
- return succeed(allFiles);
398
- }
399
-
400
- /**
401
- * Makes an HTTP request with query parameters to the specified resource path.
402
- * @param params - Configuration parameters for the HTTP tree accessors.
403
- * @param resourcePath - The path to the resource.
404
- * @param query - Query parameters to include in the request.
405
- * @returns A promise that resolves to a result containing the response data or an error message.
406
- */
407
- private static async _requestWithParams<T>(
408
- params: IHttpTreeParams,
409
- resourcePath: string,
410
- query: Record<string, string | undefined>
411
- ): Promise<Result<T>> {
412
- const search = new URLSearchParams();
413
- for (const [key, value] of Object.entries(query)) {
414
- if (value !== undefined) {
415
- search.set(key, value);
416
- }
417
- }
418
-
419
- const fetchImpl = normalizeFetch(params.fetchImpl);
420
- const userIdHeaders: RequestInit | undefined = /* c8 ignore next */ params.userId
421
- ? { headers: { 'X-User-Id': params.userId } }
422
- : undefined;
423
- const response = await fetchImpl(
424
- `${params.baseUrl.replace(/\/$/, '')}${resourcePath}?${search.toString()}`,
425
- userIdHeaders
426
- ).catch((err: unknown) => ({ err } as const));
427
-
428
- if ('err' in response) {
429
- const message = response.err instanceof Error ? response.err.message : String(response.err);
430
- return fail(message);
431
- }
432
-
433
- if (!response.ok) {
434
- const body = await response.text().catch(() => '');
435
- const message = body
436
- ? `HTTP ${response.status}: ${body}`
437
- : `HTTP ${response.status} ${response.statusText}`;
438
- return fail(message);
439
- }
440
-
441
- const json = await response.json().catch(() => undefined);
442
- if (json === undefined) {
443
- return fail('invalid JSON response');
444
- }
445
-
446
- return succeed(json as T);
447
- }
448
- }
@@ -1,32 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 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
- /**
24
- * Browser-compatible FileTree implementations using the File API.
25
- * @packageDocumentation
26
- */
27
-
28
- export * from './directoryHandleStore';
29
- export * from './fileApiTreeAccessors';
30
- export * from './fileSystemAccessTreeAccessors';
31
- export * from './httpTreeAccessors';
32
- export * from './localStorageTreeAccessors';