@statewalker/webrun-files 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +188 -6
  2. package/dist/cjs/index.cjs +599 -0
  3. package/dist/esm/index.js +584 -0
  4. package/dist/files-api.d.ts +44 -0
  5. package/dist/files-api.d.ts.map +1 -0
  6. package/dist/impl/index.d.ts +6 -0
  7. package/dist/impl/index.d.ts.map +1 -0
  8. package/dist/impl/mem-files-api.d.ts +13 -0
  9. package/dist/impl/mem-files-api.d.ts.map +1 -0
  10. package/dist/impl/node-files-api.d.ts +24 -0
  11. package/dist/impl/node-files-api.d.ts.map +1 -0
  12. package/dist/index.d.ts +12 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/types.d.ts +67 -0
  15. package/dist/types.d.ts.map +1 -0
  16. package/dist/utils/collect-stream.d.ts +16 -0
  17. package/dist/utils/collect-stream.d.ts.map +1 -0
  18. package/dist/utils/file-info-utils.d.ts +10 -0
  19. package/dist/utils/file-info-utils.d.ts.map +1 -0
  20. package/dist/utils/index.d.ts +8 -0
  21. package/dist/utils/index.d.ts.map +1 -0
  22. package/dist/utils/normalize-path.d.ts +21 -0
  23. package/dist/utils/normalize-path.d.ts.map +1 -0
  24. package/dist/utils/path-utils.d.ts +20 -0
  25. package/dist/utils/path-utils.d.ts.map +1 -0
  26. package/package.json +30 -31
  27. package/src/files-api.ts +168 -0
  28. package/src/impl/index.ts +6 -0
  29. package/src/impl/mem-files-api.ts +323 -0
  30. package/src/impl/node-files-api.ts +271 -0
  31. package/src/index.ts +36 -0
  32. package/src/types.ts +79 -0
  33. package/src/utils/collect-stream.ts +35 -0
  34. package/src/utils/file-info-utils.ts +15 -0
  35. package/src/utils/index.ts +8 -0
  36. package/src/utils/normalize-path.ts +34 -0
  37. package/src/utils/path-utils.ts +46 -0
  38. package/CHANGELOG.md +0 -2
  39. package/dist/index-umd.js +0 -1054
  40. package/dist/index-umd.min.js +0 -2
  41. package/dist/index.js +0 -1033
  42. package/dist/index.min.js +0 -2
  43. package/index.js +0 -1
  44. package/src/BrowserFilesApi.js +0 -335
  45. package/src/FilesApi.js +0 -133
  46. package/src/MemFilesApi.js +0 -129
  47. package/src/NodeFilesApi.js +0 -222
  48. package/src/addFilesApiLogger.js +0 -25
  49. package/src/getMimeType.js +0 -19
  50. package/src/index.js +0 -10
  51. package/src/mimeTypes.js +0 -96
  52. package/src/normalizePath.js +0 -8
  53. package/src/notImplemented.js +0 -3
  54. package/src/openBrowserFilesApi.js +0 -67
package/dist/index.js DELETED
@@ -1,1033 +0,0 @@
1
- // @statewalker/webrun-files v0.3.1 https://github.com/statewalker/webrun-files Copyright 2024 Mikhail Kotelnikov
2
- function normalizePath(filePath) {
3
- const segments = filePath.split("/").filter((s) => !!s && s !== ".");
4
- if (segments.length === 0) {
5
- segments.push("");
6
- }
7
- segments.unshift("");
8
- return segments.join("/");
9
- }
10
-
11
- function notImplemented(){
12
- throw new Error('Not implemented.');
13
- }
14
-
15
- /*
16
- * FileInfo
17
- * - `kind` - (`file`|`directory`) kind of the returned entity
18
- * - `name` - the local name of this file entry (the last segment of the path)
19
- * - `path` - path to the file from the root of the "file system"
20
- * - `type` - an optional mime type of the file content; it is not defined for directories
21
- * - `size` - size of the file (only for files)
22
- * - `lastModified` - modification time as a long integer
23
- */
24
-
25
- /**
26
- * This is a common interface providing access to methods dealing with files.
27
- */
28
- class FilesApi {
29
- constructor(options) {
30
- this.options = options || {};
31
- }
32
-
33
- /**
34
- * Returns an async generator providing information about files (FileInfo[]).
35
- * Paths corresponding to directories has the "/" trailing symbol.
36
- * @param {Object|String} file path to the folder or an object returned by the `list` or `info` methods
37
- * @param {Object} options optional parameters
38
- * @param {Boolean} options.recursive this flag defines if the returned generator
39
- * should give access to all child directories and files or just direct children
40
- * @return {AsyncGenerator<FileInfo>} generator returning information about
41
- * individual file entries
42
- */
43
- async *list(file, options = {}) {
44
- yield notImplemented();
45
- }
46
-
47
- /**
48
- * Returns information about an individual file.
49
- * @param {Object|String} file path to the file or an object returned by
50
- * the `list` or `info` methods
51
- * @return {FileInfo} information about the file corresponding to the specified
52
- * path or null if there is no such a file
53
- */
54
- async stats(file) {
55
- return notImplemented();
56
- }
57
-
58
- /**
59
- * Removes file or folder corresponding to the specified path.
60
- * All children of this entry will be removed as well.
61
- * @param {Object|String} file path to the file to remove or an object
62
- * returned by the `list` or `info` methods
63
- * @return {Boolean} return true if the file was removed
64
- */
65
- async remove(file) {
66
- return notImplemented();
67
- }
68
-
69
- /**
70
- * Creates and returns a write stream for the file with the specified path.
71
- * @param {Object|String} file path to the file or an object returned by
72
- * the `list` or `info` methods
73
- * @param {AsyncGenerator|AsyncIterator} content async generator function providing the binary
74
- * file content to store
75
- * @return {any} result of the action execution
76
- */
77
- async write(file, content) {
78
- return notImplemented();
79
- }
80
-
81
- /**
82
- * Creates an returns an AsyncIterator returning byte blocks with the file content.
83
- * @param {Object|String} file path to the file or an object returned by
84
- * the `list` or `info` methods
85
- * @param {object} options read parameters
86
- * @param {number} options.start starting read position; default value is 0
87
- * @param {number} options.bufferSize the optional size of returned chunks buffers
88
- */
89
- async *read(file, options) {
90
- yield notImplemented();
91
- }
92
-
93
- /**
94
- * Copies a file from one path to another.
95
- * @param {String} fromPath path to the source file
96
- * @param {String} toPath path to the target file
97
- * @param {Object} options additional options
98
- * @param {Store} options.recursive if this flag is true then subfolders are also copied
99
- * @param {Store} options.target optional target store where data should be copied
100
- * @return {Boolean} if the file was successfully copied
101
- */
102
- async copy(source, target, options = {}) {
103
- const doCopy = async (sourcePath, targetPath, sourceFile) => {
104
- const suffix = sourceFile.path.substring(sourcePath.length);
105
- targetPath = `${targetPath}${suffix}`;
106
- const content = this.read(sourceFile);
107
- return await this.write(targetPath, content);
108
- };
109
- const recursive = options.recursive === undefined || !!options.recursive;
110
- const sourceFile = await this.stats(source);
111
- if (sourceFile.kind === "directory") {
112
- for await (const file of this.list(source, { ...options, recursive })) {
113
- if (file.kind === "directory") continue;
114
- await doCopy(source, target, file);
115
- }
116
- } else {
117
- await doCopy(source, target, sourceFile);
118
- }
119
- return true;
120
- }
121
-
122
- /**
123
- * Moves files from the initial position to the target path.
124
- * The default implementation creates a new file copy in the new location
125
- * and removes the old file.
126
- *
127
- * @param {String} fromPath path to the source file
128
- * @param {String} toPath path to the target file
129
- * @param {Object} options additional options
130
- * @param {Store} options.target optional target store where data should be moved
131
- * @return {Boolean} if the file was successfully moved
132
- */
133
- async move(fromPath, toPath, options = {}) {
134
- if (!await this.copy(fromPath, toPath, options)) return false;
135
- await this.remove(fromPath);
136
- return true;
137
- }
138
-
139
- normalizePath(file) {
140
- let filePath = typeof file === "object" ? file.path : file + "";
141
- return normalizePath(filePath);
142
- }
143
- }
144
-
145
- // https://docs.w3cub.com/http/basics_of_http/mime_types/complete_list_of_mime_types
146
- const mimeTypes = [
147
- { "extensions": ["aac"], "mime": "audio/aac", "description": "AAC audio" },
148
- { "extensions": ["abw"], "mime": "application/x-abiword", "description": "AbiWord document" },
149
- { "extensions": ["arc"], "mime": "application/x-freearc", "description": "Archive document (multiple files embedded)" },
150
- { "extensions": ["avi"], "mime": "video/x-msvideo", "description": "AVI: Audio Video Interleave" },
151
- { "extensions": ["azw"], "mime": "application/vnd.amazon.ebook", "description": "Amazon Kindle eBook format" },
152
- { "extensions": ["bin"], "mime": "application/octet-stream", "description": "Any kind of binary data" },
153
- { "extensions": ["bmp"], "mime": "image/bmp", "description": "Windows OS/2 Bitmap Graphics" },
154
- { "extensions": ["bz"], "mime": "application/x-bzip", "description": "BZip archive" },
155
- { "extensions": ["bz2"], "mime": "application/x-bzip2", "description": "BZip2 archive" },
156
- { "extensions": ["csh"], "mime": "application/x-csh", "description": "C-Shell script" },
157
- { "extensions": ["css"], "mime": "text/css", "description": "Cascading Style Sheets (CSS)" },
158
- { "extensions": ["csv"], "mime": "text/csv", "description": "Comma-separated values (CSV)" },
159
- { "extensions": ["doc"], "mime": "application/msword", "description": "Microsoft Word" },
160
- { "extensions": ["docx"], "mime": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "description": "Microsoft Word (OpenXML)" },
161
- { "extensions": ["eot"], "mime": "application/vnd.ms-fontobject", "description": "MS Embedded OpenType fonts" },
162
- { "extensions": ["epub"], "mime": "application/epub+zip", "description": "Electronic publication (EPUB)" },
163
- { "extensions": ["gz"], "mime": "application/gzip", "description": "GZip Compressed Archive" },
164
- { "extensions": ["gif"], "mime": "image/gif", "description": "Graphics Interchange Format (GIF)" },
165
- { "extensions": ["htm", "html"], "mime": "text/html", "description": "HyperText Markup Language (HTML)" },
166
- { "extensions": ["ico"], "mime": "image/vnd.microsoft.icon", "description": "Icon format" },
167
- { "extensions": ["ics"], "mime": "text/calendar", "description": "iCalendar format" },
168
- { "extensions": ["jar"], "mime": "application/java-archive", "description": "Java Archive (JAR)" },
169
- { "extensions": ["jpg", "jpeg"], "mime": "image/jpeg", "description": "JPEG images" },
170
- { "extensions": ["js"], "mime": "text/javascript", "description": "JavaScript per the following specifications: https://html.spec.whatwg.org/multipage/#scriptingLanguages, https://html.spec.whatwg.org/multipage/#dependencies:willful-violation, https://datatracker.ietf.org/doc/draft-ietf-dispatch-javascript-mjs/" },
171
- { "extensions": ["json"], "mime": "application/json", "description": "JSON format" },
172
- { "extensions": ["jsonld"], "mime": "application/ld+json", "description": "JSON-LD format" },
173
- { "extensions": ["mid", "midi"], "mime": "application/ld+json", "description": "Musical Instrument Digital Interface (MIDI) audio/midi audio/x-midi" },
174
- { "extensions": ["mjs"], "mime": "text/javascript", "description": "JavaScript module" },
175
- { "extensions": ["mp3"], "mime": "audio/mpeg", "description": "MP3 audio" },
176
- { "extensions": ["md"], "mime": "text/markdown", "description": "Markdown" },
177
- { "extensions": ["mpeg"], "mime": "video/mpeg", "description": "MPEG Video" },
178
- { "extensions": ["mpkg"], "mime": "application/vnd.apple.installer+xml", "description": "Apple Installer Package" },
179
- { "extensions": ["odp"], "mime": "application/vnd.oasis.opendocument.presentation", "description": "OpenDocument presentation document" },
180
- { "extensions": ["ods"], "mime": "application/vnd.oasis.opendocument.spreadsheet", "description": "OpenDocument spreadsheet document" },
181
- { "extensions": ["odt"], "mime": "application/vnd.oasis.opendocument.text", "description": "OpenDocument text document" },
182
- { "extensions": ["oga"], "mime": "audio/ogg", "description": "OGG audio" },
183
- { "extensions": ["ogv"], "mime": "video/ogg", "description": "OGG video" },
184
- { "extensions": ["ogx"], "mime": "application/ogg", "description": "OGG" },
185
- { "extensions": ["opus"], "mime": "audio/opus", "description": "Opus audio" },
186
- { "extensions": ["otf"], "mime": "font/otf", "description": "OpenType font" },
187
- { "extensions": ["png"], "mime": "image/png", "description": "Portable Network Graphics" },
188
- { "extensions": ["pdf"], "mime": "application/pdf", "description": "Adobe Portable Document Format (PDF)" },
189
- { "extensions": ["php"], "mime": "application/x-httpd-php", "description": "Hypertext Preprocessor (Personal Home Page)" },
190
- { "extensions": ["ppt"], "mime": "application/vnd.ms-powerpoint", "description": "Microsoft PowerPoint" },
191
- { "extensions": ["pptx"], "mime": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "description": "Microsoft PowerPoint (OpenXML)" },
192
- { "extensions": ["rar"], "mime": "application/vnd.rar", "description": "RAR archive" },
193
- { "extensions": ["rtf"], "mime": "application/rtf", "description": "Rich Text Format (RTF)" },
194
- { "extensions": ["sh"], "mime": "application/x-sh", "description": "Bourne shell script" },
195
- { "extensions": ["svg"], "mime": "image/svg+xml", "description": "Scalable Vector Graphics (SVG)" },
196
- { "extensions": ["swf"], "mime": "application/x-shockwave-flash", "description": "Small web format (SWF) or Adobe Flash document" },
197
- { "extensions": ["tar"], "mime": "application/x-tar", "description": "Tape Archive (TAR)" },
198
- { "extensions": ["tif", "tiff"], "mime": "image/tiff", "description": "Tagged Image File Format (TIFF)" },
199
- // { "extensions": ["ts"], "mime": "video/mp2t", "description": "MPEG transport stream" },
200
- { "extensions": ["ttf"], "mime": "font/ttf", "description": "TrueType Font" },
201
- { "extensions": ["txt"], "mime": "text/plain", "description": "Text, (generally ASCII or ISO 8859-n)" },
202
- { "extensions": ["vsd"], "mime": "application/vnd.visio", "description": "Microsoft Visio" },
203
- { "extensions": ["wav"], "mime": "audio/wav", "description": "Waveform Audio Format" },
204
- { "extensions": ["weba"], "mime": "audio/webm", "description": "WEBM audio" },
205
- { "extensions": ["webm"], "mime": "video/webm", "description": "WEBM video" },
206
- { "extensions": ["webp"], "mime": "image/webp", "description": "WEBP image" },
207
- { "extensions": ["woff"], "mime": "font/woff", "description": "Web Open Font Format (WOFF)" },
208
- { "extensions": ["woff2"], "mime": "font/woff2", "description": "Web Open Font Format (WOFF)" },
209
- { "extensions": ["xhtml"], "mime": "application/xhtml+xml", "description": "XHTML" },
210
- { "extensions": ["xls"], "mime": "application/vnd.ms-excel", "description": "Microsoft Excel" },
211
- { "extensions": ["xlsx"], "mime": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "description": "Microsoft Excel (OpenXML)" },
212
- { "extensions": ["xml"], "mime": "application/xml", "description": "XML; application/xml - if not readable from casual users (RFC 3023, section 3); text/xml - if readable from casual users (RFC 3023, section 3)" },
213
- { "extensions": ["xul"], "mime": "application/vnd.mozilla.xul+xml", "description": "XUL" },
214
- { "extensions": ["zip"], "mime": "application/zip", "description": "ZIP archive" },
215
- { "extensions": ["3gp"], "mime": "video/3gpp", "description": "3GPP audio/video container" },
216
- { "extensions": ["3g2"], "mime": "video/3gpp2", "description": "3GPP2 audio/video container; audio/3gpp if it doesn't contain video; audio/3gpp2 if it doesn't contain video" },
217
- { "extensions": ["7z"], "mime": "application/x-7z-compressed", "description": "7-zip archive" },
218
-
219
- { "extensions": ["jsx"], "mime": "text/jsx", "description": "JavaScript Syntax Extension / JavaScript XML format" },
220
- { "extensions": ["ts"], "mime": "text/ts", "description": "TypeScript format" },
221
- { "extensions": ["tsx"], "mime": "text/tsx", "description": "TypeScript Syntax Extension / TypeScript XML format" },
222
-
223
- ];
224
-
225
- const mimeTypesDescriptions = mimeTypes.reduce((index, t) => {
226
- index[t.mime] = t.description;
227
- return index;
228
- }, {});
229
-
230
- const extensionToMimeType = mimeTypes.reduce((index, t) => {
231
- for (const ext of t.extensions) {
232
- index[ext] = t.mime;
233
- }
234
- return index;
235
- }, {});
236
-
237
- const mimeTypeToExtensions = mimeTypes.reduce((index, t) => {
238
- index[t.mime] = t.extensions;
239
- return index;
240
- }, {});
241
-
242
- // import mimeDb from 'mime-db';
243
- // import mimeTypes from 'mime-types';
244
-
245
- function getMimeType(url) {
246
- url = url || '';
247
- const originalUrl = url;
248
- url = url.split('?').shift(); // Remove queries (if any)
249
- url = url.split('/').pop(); // Keep only the last segment of the path
250
- const extension = url.split('.').pop(); // Get extension
251
- let mimeType = extensionToMimeType[extension];
252
- if (!mimeType) {
253
- mimeType =
254
- originalUrl.match(/api\.observablehq\.com/) || originalUrl.match(/\.ojs$/)
255
- ? 'application/javascript-observable'
256
- : 'application/binary';
257
- }
258
- return mimeType;
259
- }
260
-
261
- class MemFilesApi extends FilesApi {
262
- constructor(options) {
263
- super(options);
264
- this.index = {
265
- "/" : {
266
- "kind": "directory",
267
- "path": "/",
268
- "name": ""
269
- }
270
- };
271
- }
272
-
273
- async *list(file, { recursive = false } = {}) {
274
- await this._init();
275
- const filePath = this.normalizePath(file);
276
- const paths = this._getPaths();
277
- for (const path of paths) {
278
- if (path.indexOf(filePath) !== 0 || path === filePath) continue;
279
- const suffix = path.substring(filePath.length + 1);
280
- const segments = suffix.split("/");
281
- if (recursive || segments.length === 1) {
282
- yield this._getFileInfo(path);
283
- }
284
- }
285
- }
286
-
287
- async stats(file) {
288
- await this._init();
289
- const filePath = this.normalizePath(file);
290
- return this._getFileInfo(filePath);
291
- }
292
-
293
- async remove(file) {
294
- await this._init();
295
- const fileInfo = await this.stats(file);
296
- if (!fileInfo) return false;
297
- for await (const { path } of this.list(fileInfo, { recursive: true })) {
298
- if (path !== "/") delete this.index[path];
299
- }
300
- delete this.index[fileInfo.path];
301
- }
302
-
303
- async write(file, content) {
304
- await this._init();
305
- await this._doWrite(file, content);
306
- }
307
-
308
- async *read(file /* { start = 0, bufferSize = 1024 * 8 } = {} */) {
309
- await this._init();
310
- const filePath = this.normalizePath(file);
311
- const fileInfo = this.index[filePath];
312
- if (!fileInfo || fileInfo.kind !== "file") return ; // throw new Error("Not a file");
313
- const content = fileInfo.content;
314
- yield* content;
315
- }
316
-
317
- // TODO:
318
- // async copy(fromPath, toPath, options = {}) { ... }
319
-
320
- // TODO:
321
- // async move(fromPath, toPath, options = {}) { ... }
322
-
323
- async _init() {
324
- return this._initPromise = this._initPromise ||
325
- Promise.resolve().then(() => this._addEntries(this.options.files));
326
- }
327
-
328
- async _doWrite(file, content) {
329
- const filePath = this.normalizePath(file);
330
- const segments = filePath.split("/").filter((s) => !!s);
331
- const pathSegments = [""];
332
- let info;
333
- for (let i = 0; i < segments.length; i++) {
334
- const name = segments[i];
335
- pathSegments.push(name);
336
- const path = pathSegments.join("/");
337
- info = this.index[path] = this.index[path] || {
338
- path,
339
- name,
340
- kind: "directory",
341
- };
342
- }
343
- info.kind = "file";
344
- info.content = [];
345
- info.type = getMimeType(info.path);
346
- info.size = 0;
347
- info.lastModified = Date.now();
348
- if (typeof content === "function") content = content();
349
- for await (let chunk of content) {
350
- chunk = new Uint8Array(chunk);
351
- info.content.push(chunk);
352
- info.size += chunk.length;
353
- }
354
- }
355
-
356
- async _addEntries(index = {}) {
357
- for (let [path, content] of Object.entries(index)) {
358
- if (typeof content === "string") {
359
- content = [new TextEncoder().encode(content)];
360
- } else if (content instanceof Uint8Array) {
361
- content = [content];
362
- }
363
- await this._doWrite(path, content);
364
- }
365
- }
366
-
367
- _getPaths() {
368
- return this._getIndexPaths(this.index);
369
- }
370
-
371
- _getIndexPaths(index) {
372
- return Object.keys(index).sort(compare);
373
- function compare(a, b) {
374
- return a > b ? 1 : a < b ? -1 : 0;
375
- }
376
- }
377
-
378
- _getFileInfo(path) {
379
- const fileInfo = this.index[path];
380
- if (!fileInfo) return null;
381
- const info = { ...fileInfo };
382
- delete info.content;
383
- return info;
384
- }
385
-
386
- }
387
-
388
- class NodeFilesApi extends FilesApi {
389
- constructor(options) {
390
- super(options);
391
- if (!this.fs) throw new Error("File System is not defined.");
392
- if (!this.rootDir) throw new Error("Root directory is not defined.");
393
- }
394
-
395
- get rootDirSegments() {
396
- if (!this._rootDirSegments) {
397
- this._rootDirSegments = this._toPathSegments(
398
- this.options.rootDir || "./",
399
- );
400
- }
401
- return this._rootDirSegments;
402
- }
403
-
404
- get rootDir() {
405
- if (!this._rootDir) {
406
- this._rootDir = this.rootDirSegments.join("/");
407
- }
408
- return this._rootDir;
409
- }
410
-
411
- get fs() {
412
- return this.options.fs;
413
- }
414
-
415
- async _init() {
416
- return this._initialized = this._initialized ||
417
- (async () => {
418
- const dirPath = this.rootDir;
419
- return await mkdir(this.fs, dirPath);
420
- })();
421
- }
422
-
423
- async *list(file, { recursive = false } = {}) {
424
- await this._init();
425
- const filePath = this._expandPath(file);
426
- const fileInfo = await this._stats(filePath);
427
- if (!fileInfo || fileInfo.kind !== "directory") return;
428
- for await (let f of list(this.fs, filePath, recursive)) {
429
- yield this._getFileInfo(f.path, f.stat);
430
- }
431
- }
432
-
433
- async stats(file) {
434
- await this._init();
435
- const filePath = this._expandPath(file);
436
- return this._stats(filePath);
437
- }
438
-
439
- async remove(file) {
440
- await this._init();
441
- const filePath = this._expandPath(file);
442
- return await remove(this.fs, filePath);
443
- }
444
-
445
- async write(file, content) {
446
- await this._init();
447
- const filePath = this._expandPath(file);
448
- const dir = pathDirname(filePath);
449
- await this.fs.mkdir(dir, { recursive: true });
450
- let handle;
451
- try {
452
- handle = await this.fs.open(filePath, "w");
453
- if (typeof content === "function") content = content();
454
- await handle.writeFile(content);
455
- } finally {
456
- handle && await handle.close();
457
- }
458
- }
459
-
460
- async *read(file, { start = 0, bufferSize = 1024 * 8 } = {}) {
461
- await this._init();
462
- const filePath = this._expandPath(file);
463
- let handle;
464
- try {
465
- handle = await this.fs.open(filePath, "r");
466
- const offset = 0;
467
- for (let position = start, bytesRead = 0; true; position += bytesRead) {
468
- const buffer = new Uint8ClampedArray(bufferSize);
469
- const result = await handle.read(
470
- buffer,
471
- offset,
472
- bufferSize,
473
- position,
474
- );
475
- bytesRead = result.bytesRead;
476
- if (bytesRead === 0) break;
477
- yield bytesRead < bufferSize
478
- ? result.buffer.slice(0, bytesRead)
479
- : result.buffer;
480
- }
481
- } finally {
482
- handle && await handle.close();
483
- }
484
- }
485
-
486
- // TODO:
487
- // async copy(fromPath, toPath, options = {}) { ... }
488
-
489
- // TODO:
490
- // async move(fromPath, toPath, options = {}) { ... }
491
-
492
- async _stats(filePath) {
493
- try {
494
- const stat = await this.fs.stat(filePath);
495
- return this._getFileInfo(filePath, stat);
496
- } catch (err) {
497
- if (err.code !== "ENOENT") throw err;
498
- return null;
499
- }
500
- }
501
-
502
- _getFileInfo(path, stat) {
503
- const segments = this._toPathSegments(path).slice(
504
- this.rootDirSegments.length,
505
- );
506
- segments.unshift("");
507
- const name = segments[segments.length - 1];
508
- path = segments.join("/");
509
- // const name = path.split("/").filter((s) => !!s).pop() || "";
510
- let info = {
511
- path,
512
- name,
513
- kind: "",
514
- };
515
- if (!stat.isFile()) {
516
- info.kind = "directory";
517
- } else {
518
- info.size = stat.size || 0;
519
- info.type = getMimeType(path);
520
- info.kind = "file";
521
- info.lastModified = new Date(stat.mtime).getTime();
522
- }
523
- return info;
524
- }
525
-
526
- _toPathSegments(filePath) {
527
- filePath = this.normalizePath(filePath);
528
- const segments = filePath.split("/"); // .filter((s) => !!s);
529
- // if (segments.length === 0) segments.push("");
530
- // segments.unshift("");
531
- return segments;
532
- }
533
-
534
- _expandPath(file) {
535
- const filePath = typeof file === "object" ? file.path : file + "";
536
- const segments = this._toPathSegments(filePath).filter((s) => !!s);
537
- // segments.shift(); // The first segment is always empty
538
- segments.unshift(...this.rootDirSegments);
539
- return segments.join("/");
540
- }
541
- }
542
-
543
- function pathDirname(path) {
544
- const segments = path.split("/");
545
- segments.pop();
546
- return segments.join("/");
547
- }
548
-
549
- function pathJoin(...paths) {
550
- let segments = paths
551
- .map((path) => path.split("/"))
552
- .reduce((result, segments) => (result.push(...segments), result), []);
553
- const firstSegment = segments.shift();
554
- segments = segments.filter((s) => !!s && s !== ".");
555
- segments.unshift(firstSegment);
556
- return segments.join("/");
557
- }
558
-
559
- async function mkdir(fs, dir) {
560
- try {
561
- const stat = await fs.stat(dir);
562
- if (stat && stat.isDirectory()) return true;
563
- } catch (error) {
564
- // Just ignore
565
- }
566
- const parent = pathDirname(dir);
567
- if (dir === parent) return false;
568
- await mkdir(fs, parent);
569
- await fs.mkdir(dir, { recursive: true });
570
- return true;
571
- }
572
-
573
- async function remove(fs, path) {
574
- try {
575
- let stat = await fs.stat(path);
576
- if (stat.isDirectory()) {
577
- for await (let file of list(fs, path, false)) {
578
- await remove(fs, file.path);
579
- }
580
- await fs.rmdir(path);
581
- } else {
582
- await fs.unlink(path);
583
- }
584
- } catch (err) {
585
- if (err.code !== "ENOENT") throw err;
586
- }
587
- }
588
-
589
- async function* list(fs, filePath, recursive) {
590
- try {
591
- const stat = await fs.stat(filePath);
592
- if (stat.isDirectory()) {
593
- const array = await fs.readdir(filePath);
594
- for (const name of array.sort()) {
595
- const f = pathJoin(filePath, name);
596
- let stat = await fs.stat(f);
597
- yield { path: f, stat };
598
- if (recursive && stat.isDirectory()) {
599
- yield* list(fs, f, true);
600
- }
601
- }
602
- }
603
- } catch (err) {
604
- if (err.code !== "ENOENT") throw err;
605
- }
606
- }
607
-
608
- /**
609
- * Stateless adapter for the "File and Directory Entries API".
610
- * See https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API
611
- *
612
- * ## File and Directory Entries API
613
- * The File and Directory Entries API simulates a local file system
614
- * this web apps can navigate within and access files in. You can
615
- * develop apps which read, write, and create files and/or directories
616
- * in a virtual, sandboxed file system.
617
- */
618
- class AdapterForFilesAndDirectoryEntriesApi {
619
- static canApply(handle) {
620
- return ((handle?.isDirectory || handle?.isFile) &&
621
- (typeof handle?.name === "string"));
622
- }
623
-
624
- async *listDirectoryChildren(handle) {
625
- let dirReader = handle.createReader();
626
- while (true) {
627
- const list = await new Promise((resolve, reject) =>
628
- dirReader.readEntries(resolve, reject)
629
- );
630
- if (!list.length) break;
631
- yield* list;
632
- }
633
- }
634
-
635
- isDirectoryHandle(handle) {
636
- return handle?.isDirectory;
637
- }
638
- isFileHandle(handle) {
639
- return handle?.isFile;
640
- }
641
-
642
- async getSubdirectoryHandle(dirHandle, directoryName, create) {
643
- return new Promise((resolve, reject) =>
644
- dirHandle.getDirectory(
645
- directoryName,
646
- { create },
647
- resolve,
648
- reject,
649
- )
650
- );
651
- }
652
-
653
- async getFileHandle(dirHandle, fileName, create = true) {
654
- return new Promise((resolve, reject) =>
655
- dirHandle.getFile(
656
- fileName,
657
- { create },
658
- resolve,
659
- reject,
660
- )
661
- );
662
- }
663
-
664
- async removeDirectoryEntry(dirHandle, handle) {
665
- return new Promise((resolve, reject) => handle.remove(resolve, reject));
666
- }
667
-
668
- async getFile(handle) {
669
- const file = await new Promise((resolve, reject) =>
670
- handle.file(resolve, reject)
671
- );
672
- return file;
673
- }
674
-
675
- async getFileWriteStream(handle) {
676
- throw new Error("Not implemented");
677
- }
678
- }
679
-
680
- /**
681
- * Stateless adapter for the "File System API" and "File System Access API".
682
- * See:
683
- * - https://developer.mozilla.org/en-US/docs/Web/API/File_System_API
684
- * - https://wicg.github.io/file-system-access/
685
- */
686
- class AdapterForFileSystemApi {
687
- static canApply(handle) {
688
- return (handle?.kind === "file" || handle?.kind === "directory");
689
- }
690
-
691
- async *listDirectoryChildren(handle) {
692
- yield* handle.values();
693
- }
694
-
695
- isDirectoryHandle(handle) {
696
- return handle?.kind === "directory";
697
- }
698
- isFileHandle(handle) {
699
- return handle?.kind === "file";
700
- }
701
-
702
- async getSubdirectoryHandle(dirHandle, directoryName, create) {
703
- return await dirHandle.getDirectoryHandle(directoryName, { create });
704
- }
705
-
706
- async getFileHandle(dirHandle, fileName, create = true) {
707
- return await dirHandle.getFileHandle(fileName, { create });
708
- }
709
-
710
- async removeDirectoryEntry(dirHandle, handle) {
711
- return await dirHandle.removeEntry(handle.name, { recursive: true });
712
- }
713
-
714
- async getFile(handle) {
715
- return await handle.getFile();
716
- }
717
-
718
- async getFileWriteStream(handle) {
719
- return await handle.createWritable(); // Write the contents of the file to the stream.
720
- }
721
- }
722
-
723
- class BrowserFilesApi extends FilesApi {
724
- constructor(options) {
725
- super(options);
726
- if (!this.rootHandle) {
727
- throw new Error("Root directory handle is not defined");
728
- }
729
- if (!this.adapter) {
730
- throw new Error(
731
- "Can not define a browser adapter for the specified root handle",
732
- );
733
- }
734
- }
735
-
736
- get rootHandle() {
737
- return this.options.rootHandle || this.options.rootHandler;
738
- }
739
-
740
- get adapter() {
741
- if (!this._adapter) {
742
- const handle = this.rootHandle;
743
- let adapter = AdapterForFilesAndDirectoryEntriesApi.canApply(handle)
744
- ? new AdapterForFilesAndDirectoryEntriesApi()
745
- : AdapterForFileSystemApi.canApply(handle)
746
- ? new AdapterForFileSystemApi()
747
- : null;
748
- this._adapter = adapter;
749
- }
750
- return this._adapter;
751
- }
752
-
753
- async *list(file, { recursive = false } = {}) {
754
- const segments = this._getPathSegments(file);
755
- const handle = await this._getHandle(segments);
756
- if (!handle) return;
757
- const that = this;
758
- if (this.adapter.isDirectoryHandle(handle)) {
759
- // yield await this._getFileInfo(segments, handle);
760
- yield* listChildren(segments, handle, recursive);
761
- }
762
-
763
- async function* listChildren(segments, dirHandle, recursive) {
764
- for await (const handle of that._listDirectoryContent(dirHandle)) {
765
- yield await that._getFileInfo([...segments, handle.name], handle);
766
- if (recursive && that.adapter.isDirectoryHandle(handle)) {
767
- yield* listChildren(
768
- [...segments, handle.name],
769
- handle,
770
- recursive,
771
- );
772
- }
773
- }
774
- }
775
- }
776
-
777
- async stats(file) {
778
- const segments = this._getPathSegments(file);
779
- const handle = await this._getHandle(segments);
780
- return handle ? await this._getFileInfo(segments, handle) : null;
781
- }
782
-
783
- async remove(file) {
784
- const doRemove = async (dirHandle, handle) => {
785
- let result = true;
786
- if (this.adapter.isDirectoryHandle(handle)) {
787
- const children = [];
788
- for await (let child of this.adapter.listDirectoryChildren(handle)) {
789
- children.push(child);
790
- }
791
- for (let child of children) {
792
- result = result && await doRemove(handle, child);
793
- }
794
- }
795
- if (dirHandle) {
796
- result &= await this.adapter.removeDirectoryEntry(dirHandle, handle);
797
- }
798
- return result;
799
- };
800
-
801
- const segments = this._getPathSegments(file);
802
- const handle = await this._getHandle(segments);
803
- segments.pop();
804
- const dirHandle = await this._getHandle(segments);
805
- return doRemove(dirHandle !== handle ? dirHandle : null, handle);
806
- }
807
-
808
- async write(file, content) {
809
- const segments = this._getPathSegments(file);
810
- const fileName = segments.pop();
811
- const dirHandle = await this._getDirectoryHandle(segments, true);
812
- const fileHandle = await this.adapter.getFileHandle(
813
- dirHandle,
814
- fileName,
815
- true,
816
- );
817
- const writable = await this.adapter.getFileWriteStream(fileHandle);
818
- try {
819
- if (typeof content === "function") content = content();
820
- for await (let block of content) {
821
- await writable.write(block.buffer ? block.buffer : block); // Close the file and write the contents to disk.
822
- }
823
- } finally {
824
- await writable.close();
825
- }
826
- }
827
-
828
- async *read(file /* { start = 0, bufferSize = 1024 * 8 } = {} */) {
829
- const segments = this._getPathSegments(file);
830
-
831
- const fileName = segments.pop();
832
- const dirHandle = await this._getDirectoryHandle(segments, false);
833
- const fileHandle = await this.adapter.getFileHandle(
834
- dirHandle,
835
- fileName,
836
- false,
837
- );
838
- if (!fileHandle) return;
839
- const fileData = await this.adapter.getFile(fileHandle);
840
- const stream = fileData.stream();
841
- const reader = stream.getReader();
842
- try {
843
- let chunk;
844
- while (chunk = await reader.read()) {
845
- if (chunk.done) break;
846
- yield await chunk.value;
847
- }
848
- } finally {
849
- await reader.cancel && reader.cancel();
850
- // stream.cancel && stream.cancel();
851
- }
852
- }
853
-
854
- // TODO:
855
- // async copy(fromPath, toPath, options = {}) { ... }
856
-
857
- // TODO:
858
- // async move(fromPath, toPath, options = {}) { ... }
859
-
860
- // ---------------------------------------------------------
861
- // Private methods
862
-
863
- async _getFileInfo(segments, handle) {
864
- segments = [...segments];
865
- const name = segments[segments.length - 1];
866
- segments.unshift("");
867
- const path = segments.join("/");
868
- const info = {
869
- kind: "",
870
- path,
871
- name,
872
- };
873
- if (this.adapter.isDirectoryHandle(handle)) {
874
- info.kind = "directory";
875
- } else {
876
- info.kind = "file";
877
-
878
- const file = await this.adapter.getFile(handle);
879
- info.size = file.size;
880
- info.type = getMimeType(path);
881
- info.lastModified = file.lastModified;
882
- }
883
- return info;
884
- }
885
-
886
- _getPathSegments(file) {
887
- let filePath = typeof file === "object" ? file.path : file + "";
888
- // filePath = this.normalizePath(filePath);
889
- const segments = filePath.split("/").filter((s) => !!s);
890
- return segments;
891
- }
892
-
893
- async _getHandle(segments) {
894
- const parentDirSegments = [...segments];
895
- const name = parentDirSegments.pop();
896
- const create = false;
897
- try {
898
- const dirHandle = await this._getDirectoryHandle(
899
- parentDirSegments,
900
- create,
901
- );
902
- try {
903
- if (!name) return dirHandle;
904
- return await this.adapter.getSubdirectoryHandle(
905
- dirHandle,
906
- name,
907
- create,
908
- );
909
- } catch (error) {
910
- return await this.adapter.getFileHandle(dirHandle, name, create);
911
- }
912
- } catch (error) {
913
- return undefined;
914
- }
915
- }
916
-
917
- async _getDirectoryHandle(segments, create = true) {
918
- segments = [...segments];
919
- let dirHandle = this.rootHandle;
920
- for (let directoryName of segments) {
921
- dirHandle = await this.adapter.getSubdirectoryHandle(
922
- dirHandle,
923
- directoryName,
924
- create,
925
- );
926
- if (!dirHandle) break;
927
- }
928
- return dirHandle;
929
- }
930
-
931
- async *_listDirectoryContent(handle) {
932
- let entries = [];
933
- for await (const entry of this.adapter.listDirectoryChildren(handle)) {
934
- entries.push(entry);
935
- }
936
- entries = entries.sort((a, b) => b.name > a.name ? 1 : -1);
937
- yield* entries;
938
- }
939
- }
940
-
941
- function addFilesApiLogger({
942
- filesApi,
943
- log = console.log,
944
- wrapper = {},
945
- }) {
946
- return ["list", "stats", "remove", "write", "read", "copy", "move"].reduce(
947
- (wrapper, methodName) => {
948
- const method = filesApi[methodName].bind(filesApi);
949
- wrapper[methodName] = (...args) => {
950
- let message = { method: methodName, stage : "enter", args };
951
- try {
952
- log(message);
953
- return message.result = method.call(filesApi, ...args);
954
- } catch (e) {
955
- message.error = e;
956
- } finally {
957
- message.stage = "exit";
958
- log(message);
959
- }
960
- };
961
- return wrapper;
962
- },
963
- wrapper,
964
- );
965
- }
966
-
967
- async function openBrowserFilesApi({
968
- handlerKey = "root-dir",
969
- readwrite = true,
970
- // Methods below could be replaced by persistent implementation
971
- // using the "idb-keyval" package (methods "get", "set", "del").
972
- index = {},
973
- get = async (key) => index[key],
974
- set = async (key, handler) => index[key] = handler,
975
- del = async (key) => delete index[key],
976
- } = {}) {
977
- let rootHandler = await get(handlerKey);
978
- if (!rootHandler) {
979
- rootHandler = await window.showDirectoryPicker();
980
- await set(handlerKey, rootHandler);
981
- }
982
- if (!(await verifyPermission(rootHandler, readwrite))) {
983
- throw new Error("Access was not granted");
984
- }
985
- if (!(await isHandlerAccessible(rootHandler))) {
986
- // await set(handlerKey, null);
987
- await del(handlerKey);
988
- throw new Error("Can not access to the folder. Please try again.");
989
- }
990
- return new BrowserFilesApi({ rootHandler });
991
- }
992
-
993
- async function isHandlerAccessible(fileHandle) {
994
- // We need to perform a real read operation with the handler
995
- // to check if the file/directory still exists.
996
- let exists = false;
997
- try {
998
- if (fileHandle.kind === "file") {
999
- await fileHandle.getFile();
1000
- } else {
1001
- for await (let item of fileHandle.values()) {
1002
- break;
1003
- }
1004
- }
1005
- exists = true;
1006
- } catch (error) {
1007
- console.log(error, fileHandle);
1008
- exists = false;
1009
- }
1010
- return exists;
1011
- }
1012
-
1013
- async function verifyPermission(fileHandle, readWrite) {
1014
- const options = {};
1015
- if (readWrite) {
1016
- options.mode = "readwrite";
1017
- }
1018
- let granted = false;
1019
- // Check if permission was already granted. If so, return true.
1020
- if ((await fileHandle.queryPermission(options)) === "granted") {
1021
- granted = true;
1022
- }
1023
- // Request permission. If the user grants permission, return true.
1024
- if (
1025
- !granted && (await fileHandle.requestPermission(options)) === "granted"
1026
- ) {
1027
- granted = true;
1028
- }
1029
- // The user didn't grant permission, so return false.
1030
- return granted;
1031
- }
1032
-
1033
- export { BrowserFilesApi, FilesApi, MemFilesApi, NodeFilesApi, addFilesApiLogger, extensionToMimeType, getMimeType, isHandlerAccessible, mimeTypeToExtensions, mimeTypes, mimeTypesDescriptions, normalizePath, openBrowserFilesApi, verifyPermission };