@wener/common 2.0.5 → 2.0.6

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 (206) hide show
  1. package/lib/ai/qwen3vl/index.js +1 -1
  2. package/lib/ai/qwen3vl/utils.js +15 -15
  3. package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
  4. package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
  5. package/lib/ai/vision/index.js +2 -2
  6. package/lib/ai/vision/resolveImageAnnotation.js +81 -95
  7. package/lib/cn/ChineseResidentIdNo.js +55 -41
  8. package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
  9. package/lib/cn/ChineseResidentIdNo.test.js +22 -21
  10. package/lib/cn/DivisionCode.js +220 -235
  11. package/lib/cn/DivisionCode.mod.js +6 -1
  12. package/lib/cn/DivisionCode.test.js +92 -121
  13. package/lib/cn/Mod11.js +18 -37
  14. package/lib/cn/Mod31.js +23 -41
  15. package/lib/cn/UnifiedSocialCreditCode.js +143 -137
  16. package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
  17. package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
  18. package/lib/cn/formatChineseAmount.js +46 -71
  19. package/lib/cn/index.js +6 -6
  20. package/lib/cn/mod.js +5 -3
  21. package/lib/cn/parseChineseNumber.js +81 -85
  22. package/lib/cn/parseChineseNumber.test.js +183 -261
  23. package/lib/cn/pinyin/cartesianProduct.js +19 -19
  24. package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
  25. package/lib/cn/pinyin/loader.js +13 -11
  26. package/lib/cn/pinyin/preload.js +2 -1
  27. package/lib/cn/pinyin/toPinyin.test.js +149 -161
  28. package/lib/cn/pinyin/toPinyinPure.js +28 -23
  29. package/lib/cn/pinyin/transform.js +11 -11
  30. package/lib/cn/types.d.js +2 -2
  31. package/lib/consola/createStandardConsolaReporter.js +14 -15
  32. package/lib/consola/formatLogObject.js +149 -133
  33. package/lib/consola/formatLogObject.test.js +167 -178
  34. package/lib/consola/index.js +2 -2
  35. package/lib/data/formatSort.js +14 -12
  36. package/lib/data/formatSort.test.js +33 -33
  37. package/lib/data/index.js +3 -3
  38. package/lib/data/maybeNumber.js +23 -23
  39. package/lib/data/parseSort.js +75 -68
  40. package/lib/data/parseSort.test.js +196 -187
  41. package/lib/data/resolvePagination.js +38 -39
  42. package/lib/data/resolvePagination.test.js +228 -218
  43. package/lib/data/types.d.js +2 -2
  44. package/lib/dayjs/dayjs.js +20 -20
  45. package/lib/dayjs/formatDuration.js +56 -56
  46. package/lib/dayjs/formatDuration.test.js +63 -77
  47. package/lib/dayjs/index.js +4 -4
  48. package/lib/dayjs/parseDuration.js +21 -26
  49. package/lib/dayjs/parseRelativeTime.js +65 -66
  50. package/lib/dayjs/parseRelativeTime.test.js +227 -243
  51. package/lib/dayjs/resolveRelativeTime.js +73 -72
  52. package/lib/dayjs/resolveRelativeTime.test.js +296 -307
  53. package/lib/decimal/index.js +1 -1
  54. package/lib/decimal/parseDecimal.js +12 -12
  55. package/lib/drain3/Drain.js +303 -338
  56. package/lib/drain3/LogCluster.js +25 -25
  57. package/lib/drain3/Node.js +24 -24
  58. package/lib/drain3/TemplateMiner.js +197 -196
  59. package/lib/drain3/index.js +5 -5
  60. package/lib/drain3/persistence/FilePersistence.js +19 -19
  61. package/lib/drain3/persistence/MemoryPersistence.js +8 -8
  62. package/lib/drain3/persistence/PersistenceHandler.js +2 -2
  63. package/lib/drain3/types.js +2 -2
  64. package/lib/emittery/emitter.js +7 -7
  65. package/lib/emittery/index.js +1 -1
  66. package/lib/foundation/schema/SexType.js +15 -12
  67. package/lib/foundation/schema/index.js +1 -1
  68. package/lib/foundation/schema/parseSexType.js +15 -16
  69. package/lib/foundation/schema/types.js +8 -6
  70. package/lib/fs/FileSystemError.js +18 -18
  71. package/lib/fs/IFileSystem.d.js +2 -2
  72. package/lib/fs/MemoryFileSystem.test.js +172 -181
  73. package/lib/fs/createBrowserFileSystem.js +222 -235
  74. package/lib/fs/createMemoryFileSystem.js +472 -510
  75. package/lib/fs/createSandboxFileSystem.js +102 -101
  76. package/lib/fs/createWebDavFileSystem.js +162 -149
  77. package/lib/fs/createWebFileSystem.js +197 -220
  78. package/lib/fs/findMimeType.js +14 -14
  79. package/lib/fs/index.js +7 -7
  80. package/lib/fs/minio/createMinioFileSystem.js +959 -956
  81. package/lib/fs/minio/index.js +1 -1
  82. package/lib/fs/orpc/FileSystemContract.js +57 -57
  83. package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
  84. package/lib/fs/orpc/index.js +2 -2
  85. package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
  86. package/lib/fs/orpc/server/index.js +1 -1
  87. package/lib/fs/s3/createS3MiniFileSystem.js +756 -737
  88. package/lib/fs/s3/index.js +1 -1
  89. package/lib/fs/s3/s3mini.test.js +524 -553
  90. package/lib/fs/scandir.js +56 -56
  91. package/lib/fs/server/createDatabaseFileSystem.js +834 -741
  92. package/lib/fs/server/createNodeFileSystem.js +407 -405
  93. package/lib/fs/server/dbfs.test.js +201 -214
  94. package/lib/fs/server/index.js +1 -1
  95. package/lib/fs/server/loadTestDatabase.js +40 -43
  96. package/lib/fs/tests/runFileSystemTest.js +352 -316
  97. package/lib/fs/types.js +17 -20
  98. package/lib/fs/utils/getFileUrl.js +24 -30
  99. package/lib/fs/utils.js +17 -17
  100. package/lib/fs/webdav/index.js +1 -1
  101. package/lib/index.js +2 -2
  102. package/lib/jsonschema/JsonSchema.js +216 -155
  103. package/lib/jsonschema/JsonSchema.test.js +123 -124
  104. package/lib/jsonschema/forEachJsonSchema.js +41 -41
  105. package/lib/jsonschema/index.js +2 -2
  106. package/lib/jsonschema/types.d.js +2 -2
  107. package/lib/meta/defineFileType.js +32 -38
  108. package/lib/meta/defineInit.js +39 -35
  109. package/lib/meta/defineMetadata.js +37 -34
  110. package/lib/meta/defineMetadata.test.js +13 -12
  111. package/lib/meta/index.js +3 -3
  112. package/lib/orpc/createOpenApiContractClient.js +26 -24
  113. package/lib/orpc/createRpcContractClient.js +37 -31
  114. package/lib/orpc/index.js +2 -2
  115. package/lib/orpc/resolveLinkPlugins.js +25 -25
  116. package/lib/password/PHC.js +187 -189
  117. package/lib/password/PHC.test.js +517 -535
  118. package/lib/password/Password.js +85 -80
  119. package/lib/password/Password.test.js +330 -364
  120. package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
  121. package/lib/password/createBase64PasswordAlgorithm.js +11 -11
  122. package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
  123. package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
  124. package/lib/password/createScryptPasswordAlgorithm.js +74 -63
  125. package/lib/password/index.js +5 -5
  126. package/lib/password/server/index.js +1 -1
  127. package/lib/resource/Identifiable.js +2 -2
  128. package/lib/resource/ListQuery.js +42 -42
  129. package/lib/resource/getTitleOfResource.js +5 -5
  130. package/lib/resource/index.js +2 -2
  131. package/lib/resource/schema/AnyResourceSchema.js +91 -89
  132. package/lib/resource/schema/BaseResourceSchema.js +26 -26
  133. package/lib/resource/schema/ResourceActionType.js +117 -115
  134. package/lib/resource/schema/ResourceStatus.js +94 -92
  135. package/lib/resource/schema/ResourceType.js +25 -23
  136. package/lib/resource/schema/index.js +5 -5
  137. package/lib/resource/schema/types.js +86 -55
  138. package/lib/resource/schema/types.test.js +16 -13
  139. package/lib/s3/formatS3Url.js +60 -60
  140. package/lib/s3/formatS3Url.test.js +238 -261
  141. package/lib/s3/index.js +2 -2
  142. package/lib/s3/parseS3Url.js +61 -60
  143. package/lib/s3/parseS3Url.test.js +270 -269
  144. package/lib/schema/SchemaRegistry.js +41 -42
  145. package/lib/schema/SchemaRegistry.mod.js +1 -1
  146. package/lib/schema/TypeSchema.d.js +2 -2
  147. package/lib/schema/createSchemaData.js +113 -67
  148. package/lib/schema/findJsonSchemaByPath.js +28 -23
  149. package/lib/schema/formatZodError.js +112 -131
  150. package/lib/schema/formatZodError.test.js +192 -195
  151. package/lib/schema/getSchemaCache.js +7 -7
  152. package/lib/schema/getSchemaOptions.js +17 -16
  153. package/lib/schema/index.js +6 -6
  154. package/lib/schema/toJsonSchema.js +195 -189
  155. package/lib/schema/toJsonSchema.test.js +34 -26
  156. package/lib/schema/validate.js +105 -96
  157. package/lib/tools/generateSchema.js +40 -40
  158. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
  159. package/lib/utils/buildBaseUrl.js +8 -8
  160. package/lib/utils/buildRedactorFormSchema.js +54 -53
  161. package/lib/utils/getEstimateProcessTime.js +24 -19
  162. package/lib/utils/index.js +3 -3
  163. package/lib/utils/resolveFeatureOptions.js +9 -9
  164. package/package.json +14 -14
  165. package/src/ai/vision/index.ts +2 -2
  166. package/src/cn/index.ts +1 -2
  167. package/src/consola/index.ts +1 -1
  168. package/src/data/index.ts +3 -4
  169. package/src/data/resolvePagination.ts +2 -2
  170. package/src/dayjs/formatDuration.ts +8 -9
  171. package/src/dayjs/index.ts +1 -1
  172. package/src/dayjs/parseRelativeTime.ts +1 -1
  173. package/src/dayjs/resolveRelativeTime.ts +1 -1
  174. package/src/drain3/Drain.test.ts +2 -2
  175. package/src/drain3/index.ts +2 -4
  176. package/src/fs/createWebDavFileSystem.ts +2 -7
  177. package/src/fs/createWebFileSystem.ts +1 -1
  178. package/src/fs/index.ts +4 -4
  179. package/src/fs/minio/createMinioFileSystem.ts +2 -2
  180. package/src/fs/minio/index.ts +1 -1
  181. package/src/fs/s3/createS3MiniFileSystem.ts +1 -1
  182. package/src/fs/server/createDatabaseFileSystem.ts +84 -120
  183. package/src/fs/server/dbfs.test.ts +14 -10
  184. package/src/fs/server/index.ts +1 -0
  185. package/src/fs/server/loadTestDatabase.ts +8 -119
  186. package/src/jsonschema/index.ts +1 -1
  187. package/src/meta/index.ts +2 -3
  188. package/src/orm/createSqliteDialect.ts +17 -0
  189. package/src/orm/index.ts +1 -0
  190. package/src/orpc/createOpenApiContractClient.ts +1 -1
  191. package/src/orpc/index.ts +1 -1
  192. package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
  193. package/src/password/index.ts +2 -2
  194. package/src/resource/index.ts +3 -3
  195. package/src/resource/schema/index.ts +4 -4
  196. package/src/s3/index.ts +1 -1
  197. package/src/schema/SchemaRegistry.ts +1 -1
  198. package/src/schema/createSchemaData.ts +1 -1
  199. package/src/schema/findJsonSchemaByPath.ts +1 -1
  200. package/src/schema/index.ts +5 -5
  201. package/src/schema/validate.ts +1 -1
  202. package/src/utils/buildRedactorFormSchema.ts +1 -1
  203. package/src/utils/formatNumber.ts +18 -0
  204. package/src/utils/formatPercent.ts +17 -0
  205. package/src/utils/index.ts +3 -3
  206. package/src/utils/resolveFeatureOptions.ts +1 -1
@@ -1,250 +1,237 @@
1
- import { dirname, join, normalize } from "pathe";
1
+ import { dirname, join, normalize } from 'pathe';
2
+
2
3
  let BrowserFSError = class BrowserFSError extends Error {
3
- code;
4
- constructor(message, code){
5
- super(message), this.code = code;
6
- this.name = 'BrowserFSError';
7
- }
4
+ code;
5
+ constructor(message, code) {
6
+ super(message), (this.code = code);
7
+ this.name = 'BrowserFSError';
8
+ }
8
9
  };
9
10
  /**
10
11
  * Creates an IFileSystem instance backed by the browser's File System Access API.
11
12
  * @param root A FileSystemDirectoryHandle, usually obtained from `window.showDirectoryPicker()`.
12
13
  */ export function createBrowserFileSystem(root) {
13
- return new BrowserFS(root);
14
+ return new BrowserFS(root);
14
15
  }
15
16
  let BrowserFS = class BrowserFS {
16
- root;
17
- node;
18
- constructor(root){
19
- this.root = root;
20
- this.node = {
21
- path: '/',
22
- name: '',
23
- directory: '',
24
- kind: 'directory',
25
- mtime: 0,
26
- size: 0,
27
- meta: {
28
- handle: root
29
- }
30
- };
31
- }
32
- /**
17
+ root;
18
+ node;
19
+ constructor(root) {
20
+ this.root = root;
21
+ this.node = {
22
+ path: '/',
23
+ name: '',
24
+ directory: '',
25
+ kind: 'directory',
26
+ mtime: 0,
27
+ size: 0,
28
+ meta: {
29
+ handle: root,
30
+ },
31
+ };
32
+ }
33
+ /**
33
34
  * Resolves a path string to a file or directory handle. This is the core navigation logic.
34
35
  * @returns A tuple of [handle, parentHandle, name] or [null, parentHandle, name] if not found.
35
36
  */ async _getHandle(path) {
36
- const normalized = normalize(path);
37
- if (normalized === '/' || normalized === '.') {
38
- return [
39
- this.root,
40
- null,
41
- this.root.name
42
- ];
43
- }
44
- const parts = normalized.split('/').filter((p)=>p);
45
- const name = parts.pop();
46
- let parent = this.root;
47
- for (const part of parts){
48
- try {
49
- parent = await parent.getDirectoryHandle(part);
50
- } catch (e) {
51
- if (e.name === 'NotFoundError' || e.name === 'TypeMismatchError') {
52
- return [
53
- null,
54
- null,
55
- name
56
- ]; // Parent path does not exist or is a file
57
- }
58
- throw e;
59
- }
60
- }
61
- try {
62
- // Check for file first, then directory
63
- const handle = await parent.getFileHandle(name).catch(()=>parent.getDirectoryHandle(name));
64
- return [
65
- handle,
66
- parent,
67
- name
68
- ];
69
- } catch (e) {
70
- if (e.name === 'NotFoundError') {
71
- return [
72
- null,
73
- parent,
74
- name
75
- ]; // Entry not found
76
- }
77
- throw e;
78
- }
79
- }
80
- /**
37
+ const normalized = normalize(path);
38
+ if (normalized === '/' || normalized === '.') {
39
+ return [this.root, null, this.root.name];
40
+ }
41
+ const parts = normalized.split('/').filter((p) => p);
42
+ const name = parts.pop();
43
+ let parent = this.root;
44
+ for (const part of parts) {
45
+ try {
46
+ parent = await parent.getDirectoryHandle(part);
47
+ } catch (e) {
48
+ if (e.name === 'NotFoundError' || e.name === 'TypeMismatchError') {
49
+ return [null, null, name]; // Parent path does not exist or is a file
50
+ }
51
+ throw e;
52
+ }
53
+ }
54
+ try {
55
+ // Check for file first, then directory
56
+ const handle = await parent.getFileHandle(name).catch(() => parent.getDirectoryHandle(name));
57
+ return [handle, parent, name];
58
+ } catch (e) {
59
+ if (e.name === 'NotFoundError') {
60
+ return [null, parent, name]; // Entry not found
61
+ }
62
+ throw e;
63
+ }
64
+ }
65
+ /**
81
66
  * Converts a FileSystemHandle into our standard IFileStat object.
82
67
  */ async _toFileStat(handle, path) {
83
- const isFile = handle.kind === 'file';
84
- const file = isFile ? await handle.getFile() : undefined;
85
- return {
86
- name: handle.name,
87
- kind: handle.kind,
88
- path: path,
89
- directory: dirname(path),
90
- size: file?.size ?? 0,
91
- mtime: file?.lastModified ?? Date.now(),
92
- meta: {
93
- handle,
94
- file
95
- }
96
- };
97
- }
98
- // --- IFileSystem Implementation ---
99
- async stat(path) {
100
- const [handle] = await this._getHandle(path);
101
- if (!handle) {
102
- throw new BrowserFSError(`Path not found: ${path}`, 'ENOENT');
103
- }
104
- return this._toFileStat(handle, path);
105
- }
106
- async exists(path) {
107
- const [handle] = await this._getHandle(path);
108
- return !!handle;
109
- }
110
- async readdir(dir, _options) {
111
- const [handle] = await this._getHandle(dir);
112
- if (!handle) {
113
- throw new BrowserFSError(`Directory not found: ${dir}`, 'ENOENT');
114
- }
115
- if (handle.kind !== 'directory') {
116
- throw new BrowserFSError(`Not a directory: ${dir}`, 'ENOTDIR');
117
- }
118
- const entries = [];
119
- // FileSystemDirectoryHandle.values() is part of the File System Access API
120
- // but not yet in TypeScript's lib.dom.d.ts
121
- for await (const entry of handle.values()){
122
- entries.push(await this._toFileStat(entry, join(dir, entry.name)));
123
- }
124
- return entries;
125
- }
126
- async mkdir(path, options = {}) {
127
- const parts = normalize(path).split('/').filter((p)=>p);
128
- let currentDir = this.root;
129
- for (const part of parts){
130
- try {
131
- currentDir = await currentDir.getDirectoryHandle(part, {
132
- create: options.recursive
133
- });
134
- } catch (e) {
135
- // If recursive is false, getDirectoryHandle throws if a segment is missing.
136
- // We re-throw a more standard error.
137
- if (e.name === 'NotFoundError' && !options.recursive) {
138
- throw new BrowserFSError(`Cannot create directory: Parent does not exist for path ${path}`, 'ENOENT');
139
- }
140
- // If it's another error (like a file with the same name), re-throw it.
141
- throw e;
142
- }
143
- }
144
- }
145
- async readFile(path, options) {
146
- const [handle] = await this._getHandle(path);
147
- if (!handle || handle.kind !== 'file') {
148
- throw new BrowserFSError(`File not found: ${path}`, 'ENOENT');
149
- }
150
- const file = await handle.getFile();
151
- return options?.encoding === 'text' ? file.text() : file.bytes();
152
- }
153
- async writeFile(path, data, options = {}) {
154
- const { overwrite = true } = options;
155
- const [handle, parent, name] = await this._getHandle(path);
156
- if (!parent) {
157
- throw new BrowserFSError(`Parent directory does not exist for path: ${path}`, 'ENOENT');
158
- }
159
- if (handle && !overwrite) {
160
- throw new BrowserFSError(`File already exists: ${path}`, 'EEXIST');
161
- }
162
- const fileHandle = await parent.getFileHandle(name, {
163
- create: true
164
- });
165
- const writable = await fileHandle.createWritable();
166
- await writable.write(data);
167
- await writable.close();
168
- }
169
- async rm(path, options = {}) {
170
- const { recursive = false, force = false } = options;
171
- const [handle, parent, name] = await this._getHandle(path);
172
- if (!handle) {
173
- if (force) return; // If force is true, do not error on not found.
174
- throw new BrowserFSError(`Path not found: ${path}`, 'ENOENT');
175
- }
176
- if (!parent) {
177
- throw new BrowserFSError(`Cannot remove root directory`);
178
- }
179
- await parent.removeEntry(name, {
180
- recursive
181
- });
182
- }
183
- async rename(oldPath, newPath, options) {
184
- // The File System Access API does NOT have a native move/rename.
185
- // The standard workaround is to copy, then delete the original.
186
- await this.copy(oldPath, newPath, options);
187
- await this.rm(oldPath, {
188
- recursive: true
189
- });
190
- }
191
- async copy(srcPath, destPath, options) {
192
- const [srcHandle] = await this._getHandle(srcPath);
193
- if (!srcHandle) {
194
- throw new BrowserFSError(`Source not found: ${srcPath}`, 'ENOENT');
195
- }
196
- const [destHandle, destParent, destName] = await this._getHandle(destPath);
197
- if (!destParent) {
198
- throw new BrowserFSError(`Destination directory does not exist: ${dirname(destPath)}`, 'ENOENT');
199
- }
200
- if (destHandle && !options?.overwrite) {
201
- throw new BrowserFSError(`Destination already exists: ${destPath}`, 'EEXIST');
202
- }
203
- if (destHandle?.kind === 'directory' && srcHandle.kind === 'file') {
204
- throw new BrowserFSError(`Cannot overwrite a directory with a file: ${destPath}`, 'EISDIR');
205
- }
206
- await this._copyEntry(srcHandle, destParent, destName);
207
- }
208
- async _copyEntry(srcHandle, destDirHandle, newName) {
209
- if (srcHandle.kind === 'file') {
210
- const file = await srcHandle.getFile();
211
- const destFileHandle = await destDirHandle.getFileHandle(newName, {
212
- create: true
213
- });
214
- const writable = await destFileHandle.createWritable();
215
- await writable.write(file);
216
- await writable.close();
217
- } else if (srcHandle.kind === 'directory') {
218
- const newDirHandle = await destDirHandle.getDirectoryHandle(newName, {
219
- create: true
220
- });
221
- for await (const entry of srcHandle.values()){
222
- await this._copyEntry(entry, newDirHandle, entry.name);
223
- }
224
- }
225
- }
226
- async createUrl(stat, _options) {
227
- // The handle is stored in the meta property in our _toFileStat method
228
- const handle = stat.meta.handle;
229
- if (handle?.kind !== 'file') {
230
- // Return a placeholder or throw an error for directories
231
- return '';
232
- }
233
- const file = await handle.getFile();
234
- return URL.createObjectURL(file);
235
- }
236
- createReadStream() {
237
- throw new Error('Streaming is not implemented for BrowserFS. Use readFile instead.');
238
- }
239
- createWriteStream() {
240
- throw new Error('Streaming is not implemented for BrowserFS. Use writeFile instead.');
241
- }
242
- createWritableStream() {
243
- throw new Error('Streaming is not implemented for BrowserFS. Use writeFile instead.');
244
- }
245
- createReadableStream() {
246
- throw new Error('Streaming is not implemented for BrowserFS. Use readFile instead.');
247
- }
68
+ const isFile = handle.kind === 'file';
69
+ const file = isFile ? await handle.getFile() : undefined;
70
+ return {
71
+ name: handle.name,
72
+ kind: handle.kind,
73
+ path: path,
74
+ directory: dirname(path),
75
+ size: file?.size ?? 0,
76
+ mtime: file?.lastModified ?? Date.now(),
77
+ meta: {
78
+ handle,
79
+ file,
80
+ },
81
+ };
82
+ }
83
+ // --- IFileSystem Implementation ---
84
+ async stat(path) {
85
+ const [handle] = await this._getHandle(path);
86
+ if (!handle) {
87
+ throw new BrowserFSError(`Path not found: ${path}`, 'ENOENT');
88
+ }
89
+ return this._toFileStat(handle, path);
90
+ }
91
+ async exists(path) {
92
+ const [handle] = await this._getHandle(path);
93
+ return !!handle;
94
+ }
95
+ async readdir(dir, _options) {
96
+ const [handle] = await this._getHandle(dir);
97
+ if (!handle) {
98
+ throw new BrowserFSError(`Directory not found: ${dir}`, 'ENOENT');
99
+ }
100
+ if (handle.kind !== 'directory') {
101
+ throw new BrowserFSError(`Not a directory: ${dir}`, 'ENOTDIR');
102
+ }
103
+ const entries = [];
104
+ // FileSystemDirectoryHandle.values() is part of the File System Access API
105
+ // but not yet in TypeScript's lib.dom.d.ts
106
+ for await (const entry of handle.values()) {
107
+ entries.push(await this._toFileStat(entry, join(dir, entry.name)));
108
+ }
109
+ return entries;
110
+ }
111
+ async mkdir(path, options = {}) {
112
+ const parts = normalize(path)
113
+ .split('/')
114
+ .filter((p) => p);
115
+ let currentDir = this.root;
116
+ for (const part of parts) {
117
+ try {
118
+ currentDir = await currentDir.getDirectoryHandle(part, {
119
+ create: options.recursive,
120
+ });
121
+ } catch (e) {
122
+ // If recursive is false, getDirectoryHandle throws if a segment is missing.
123
+ // We re-throw a more standard error.
124
+ if (e.name === 'NotFoundError' && !options.recursive) {
125
+ throw new BrowserFSError(`Cannot create directory: Parent does not exist for path ${path}`, 'ENOENT');
126
+ }
127
+ // If it's another error (like a file with the same name), re-throw it.
128
+ throw e;
129
+ }
130
+ }
131
+ }
132
+ async readFile(path, options) {
133
+ const [handle] = await this._getHandle(path);
134
+ if (!handle || handle.kind !== 'file') {
135
+ throw new BrowserFSError(`File not found: ${path}`, 'ENOENT');
136
+ }
137
+ const file = await handle.getFile();
138
+ return options?.encoding === 'text' ? file.text() : file.bytes();
139
+ }
140
+ async writeFile(path, data, options = {}) {
141
+ const { overwrite = true } = options;
142
+ const [handle, parent, name] = await this._getHandle(path);
143
+ if (!parent) {
144
+ throw new BrowserFSError(`Parent directory does not exist for path: ${path}`, 'ENOENT');
145
+ }
146
+ if (handle && !overwrite) {
147
+ throw new BrowserFSError(`File already exists: ${path}`, 'EEXIST');
148
+ }
149
+ const fileHandle = await parent.getFileHandle(name, {
150
+ create: true,
151
+ });
152
+ const writable = await fileHandle.createWritable();
153
+ await writable.write(data);
154
+ await writable.close();
155
+ }
156
+ async rm(path, options = {}) {
157
+ const { recursive = false, force = false } = options;
158
+ const [handle, parent, name] = await this._getHandle(path);
159
+ if (!handle) {
160
+ if (force) return; // If force is true, do not error on not found.
161
+ throw new BrowserFSError(`Path not found: ${path}`, 'ENOENT');
162
+ }
163
+ if (!parent) {
164
+ throw new BrowserFSError(`Cannot remove root directory`);
165
+ }
166
+ await parent.removeEntry(name, {
167
+ recursive,
168
+ });
169
+ }
170
+ async rename(oldPath, newPath, options) {
171
+ // The File System Access API does NOT have a native move/rename.
172
+ // The standard workaround is to copy, then delete the original.
173
+ await this.copy(oldPath, newPath, options);
174
+ await this.rm(oldPath, {
175
+ recursive: true,
176
+ });
177
+ }
178
+ async copy(srcPath, destPath, options) {
179
+ const [srcHandle] = await this._getHandle(srcPath);
180
+ if (!srcHandle) {
181
+ throw new BrowserFSError(`Source not found: ${srcPath}`, 'ENOENT');
182
+ }
183
+ const [destHandle, destParent, destName] = await this._getHandle(destPath);
184
+ if (!destParent) {
185
+ throw new BrowserFSError(`Destination directory does not exist: ${dirname(destPath)}`, 'ENOENT');
186
+ }
187
+ if (destHandle && !options?.overwrite) {
188
+ throw new BrowserFSError(`Destination already exists: ${destPath}`, 'EEXIST');
189
+ }
190
+ if (destHandle?.kind === 'directory' && srcHandle.kind === 'file') {
191
+ throw new BrowserFSError(`Cannot overwrite a directory with a file: ${destPath}`, 'EISDIR');
192
+ }
193
+ await this._copyEntry(srcHandle, destParent, destName);
194
+ }
195
+ async _copyEntry(srcHandle, destDirHandle, newName) {
196
+ if (srcHandle.kind === 'file') {
197
+ const file = await srcHandle.getFile();
198
+ const destFileHandle = await destDirHandle.getFileHandle(newName, {
199
+ create: true,
200
+ });
201
+ const writable = await destFileHandle.createWritable();
202
+ await writable.write(file);
203
+ await writable.close();
204
+ } else if (srcHandle.kind === 'directory') {
205
+ const newDirHandle = await destDirHandle.getDirectoryHandle(newName, {
206
+ create: true,
207
+ });
208
+ for await (const entry of srcHandle.values()) {
209
+ await this._copyEntry(entry, newDirHandle, entry.name);
210
+ }
211
+ }
212
+ }
213
+ async createUrl(stat, _options) {
214
+ // The handle is stored in the meta property in our _toFileStat method
215
+ const handle = stat.meta.handle;
216
+ if (handle?.kind !== 'file') {
217
+ // Return a placeholder or throw an error for directories
218
+ return '';
219
+ }
220
+ const file = await handle.getFile();
221
+ return URL.createObjectURL(file);
222
+ }
223
+ createReadStream() {
224
+ throw new Error('Streaming is not implemented for BrowserFS. Use readFile instead.');
225
+ }
226
+ createWriteStream() {
227
+ throw new Error('Streaming is not implemented for BrowserFS. Use writeFile instead.');
228
+ }
229
+ createWritableStream() {
230
+ throw new Error('Streaming is not implemented for BrowserFS. Use writeFile instead.');
231
+ }
232
+ createReadableStream() {
233
+ throw new Error('Streaming is not implemented for BrowserFS. Use readFile instead.');
234
+ }
248
235
  };
249
236
 
250
- //# sourceMappingURL=createBrowserFileSystem.js.map
237
+ //# sourceMappingURL=createBrowserFileSystem.js.map