@statewalker/webrun-files 0.1.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.
- package/CHANGELOG.md +2 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/index-umd.js +881 -0
- package/dist/index-umd.min.js +2 -0
- package/dist/index.js +862 -0
- package/dist/index.min.js +2 -0
- package/index.js +1 -0
- package/package.json +49 -0
- package/src/BrowserFilesApi.js +164 -0
- package/src/FilesApi.js +133 -0
- package/src/MemFilesApi.js +129 -0
- package/src/NodeFilesApi.js +222 -0
- package/src/addFilesApiLogger.js +25 -0
- package/src/getMimeType.js +19 -0
- package/src/index.js +10 -0
- package/src/mimeTypes.js +96 -0
- package/src/normalizePath.js +8 -0
- package/src/notImplemented.js +3 -0
- package/src/openBrowserFilesApi.js +67 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @statewalker/webrun-files v0.1.0 https://github.com/statewalker/webrun-files Copyright 2023 Mikhail Kotelnikov
|
|
2
|
+
function t(t){const e=t.split("/").filter((t=>!!t&&"."!==t));return 0===e.length&&e.push(""),e.unshift(""),e.join("/")}function e(){throw new Error("Not implemented.")}class i{constructor(t){this.options=t||{}}async*list(t,i={}){yield e()}async stats(t){return e()}async remove(t){return e()}async write(t,i){return e()}async*read(t,i){yield e()}async copy(t,e,i={}){const n=async(t,e,i)=>{e=`${e}${i.path.substring(t.length)}`;const n=this.read(i);return await this.write(e,n)},s=void 0===i.recursive||!!i.recursive,o=await this.stats(t);if("directory"===o.kind)for await(const o of this.list(t,{...i,recursive:s}))"directory"!==o.kind&&await n(t,e,o);else await n(t,e,o);return!0}async move(t,e,i={}){return!!await this.copy(t,e,i)&&(await this.remove(t),!0)}normalizePath(e){return t("object"==typeof e?e.path:e+"")}}const n=[{extensions:["aac"],mime:"audio/aac",description:"AAC audio"},{extensions:["abw"],mime:"application/x-abiword",description:"AbiWord document"},{extensions:["arc"],mime:"application/x-freearc",description:"Archive document (multiple files embedded)"},{extensions:["avi"],mime:"video/x-msvideo",description:"AVI: Audio Video Interleave"},{extensions:["azw"],mime:"application/vnd.amazon.ebook",description:"Amazon Kindle eBook format"},{extensions:["bin"],mime:"application/octet-stream",description:"Any kind of binary data"},{extensions:["bmp"],mime:"image/bmp",description:"Windows OS/2 Bitmap Graphics"},{extensions:["bz"],mime:"application/x-bzip",description:"BZip archive"},{extensions:["bz2"],mime:"application/x-bzip2",description:"BZip2 archive"},{extensions:["csh"],mime:"application/x-csh",description:"C-Shell script"},{extensions:["css"],mime:"text/css",description:"Cascading Style Sheets (CSS)"},{extensions:["csv"],mime:"text/csv",description:"Comma-separated values (CSV)"},{extensions:["doc"],mime:"application/msword",description:"Microsoft Word"},{extensions:["docx"],mime:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",description:"Microsoft Word (OpenXML)"},{extensions:["eot"],mime:"application/vnd.ms-fontobject",description:"MS Embedded OpenType fonts"},{extensions:["epub"],mime:"application/epub+zip",description:"Electronic publication (EPUB)"},{extensions:["gz"],mime:"application/gzip",description:"GZip Compressed Archive"},{extensions:["gif"],mime:"image/gif",description:"Graphics Interchange Format (GIF)"},{extensions:["htm","html"],mime:"text/html",description:"HyperText Markup Language (HTML)"},{extensions:["ico"],mime:"image/vnd.microsoft.icon",description:"Icon format"},{extensions:["ics"],mime:"text/calendar",description:"iCalendar format"},{extensions:["jar"],mime:"application/java-archive",description:"Java Archive (JAR)"},{extensions:["jpg","jpeg"],mime:"image/jpeg",description:"JPEG images"},{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/"},{extensions:["json"],mime:"application/json",description:"JSON format"},{extensions:["jsonld"],mime:"application/ld+json",description:"JSON-LD format"},{extensions:["mid","midi"],mime:"application/ld+json",description:"Musical Instrument Digital Interface (MIDI)\taudio/midi audio/x-midi"},{extensions:["mjs"],mime:"text/javascript",description:"JavaScript module"},{extensions:["mp3"],mime:"audio/mpeg",description:"MP3 audio"},{extensions:["md"],mime:"text/markdown",description:"Markdown"},{extensions:["mpeg"],mime:"video/mpeg",description:"MPEG Video"},{extensions:["mpkg"],mime:"application/vnd.apple.installer+xml",description:"Apple Installer Package"},{extensions:["odp"],mime:"application/vnd.oasis.opendocument.presentation",description:"OpenDocument presentation document"},{extensions:["ods"],mime:"application/vnd.oasis.opendocument.spreadsheet",description:"OpenDocument spreadsheet document"},{extensions:["odt"],mime:"application/vnd.oasis.opendocument.text",description:"OpenDocument text document"},{extensions:["oga"],mime:"audio/ogg",description:"OGG audio"},{extensions:["ogv"],mime:"video/ogg",description:"OGG video"},{extensions:["ogx"],mime:"application/ogg",description:"OGG"},{extensions:["opus"],mime:"audio/opus",description:"Opus audio"},{extensions:["otf"],mime:"font/otf",description:"OpenType font"},{extensions:["png"],mime:"image/png",description:"Portable Network Graphics"},{extensions:["pdf"],mime:"application/pdf",description:"Adobe Portable Document Format (PDF)"},{extensions:["php"],mime:"application/x-httpd-php",description:"Hypertext Preprocessor (Personal Home Page)"},{extensions:["ppt"],mime:"application/vnd.ms-powerpoint",description:"Microsoft PowerPoint"},{extensions:["pptx"],mime:"application/vnd.openxmlformats-officedocument.presentationml.presentation",description:"Microsoft PowerPoint (OpenXML)"},{extensions:["rar"],mime:"application/vnd.rar",description:"RAR archive"},{extensions:["rtf"],mime:"application/rtf",description:"Rich Text Format (RTF)"},{extensions:["sh"],mime:"application/x-sh",description:"Bourne shell script"},{extensions:["svg"],mime:"image/svg+xml",description:"Scalable Vector Graphics (SVG)"},{extensions:["swf"],mime:"application/x-shockwave-flash",description:"Small web format (SWF) or Adobe Flash document"},{extensions:["tar"],mime:"application/x-tar",description:"Tape Archive (TAR)"},{extensions:["tif","tiff"],mime:"image/tiff",description:"Tagged Image File Format (TIFF)"},{extensions:["ttf"],mime:"font/ttf",description:"TrueType Font"},{extensions:["txt"],mime:"text/plain",description:"Text, (generally ASCII or ISO 8859-n)"},{extensions:["vsd"],mime:"application/vnd.visio",description:"Microsoft Visio"},{extensions:["wav"],mime:"audio/wav",description:"Waveform Audio Format"},{extensions:["weba"],mime:"audio/webm",description:"WEBM audio"},{extensions:["webm"],mime:"video/webm",description:"WEBM video"},{extensions:["webp"],mime:"image/webp",description:"WEBP image"},{extensions:["woff"],mime:"font/woff",description:"Web Open Font Format (WOFF)"},{extensions:["woff2"],mime:"font/woff2",description:"Web Open Font Format (WOFF)"},{extensions:["xhtml"],mime:"application/xhtml+xml",description:"XHTML"},{extensions:["xls"],mime:"application/vnd.ms-excel",description:"Microsoft Excel"},{extensions:["xlsx"],mime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",description:"Microsoft Excel (OpenXML)"},{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)"},{extensions:["xul"],mime:"application/vnd.mozilla.xul+xml",description:"XUL"},{extensions:["zip"],mime:"application/zip",description:"ZIP archive"},{extensions:["3gp"],mime:"video/3gpp",description:"3GPP audio/video container"},{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"},{extensions:["7z"],mime:"application/x-7z-compressed",description:"7-zip archive"},{extensions:["jsx"],mime:"text/jsx",description:"JavaScript Syntax Extension / JavaScript XML format"},{extensions:["ts"],mime:"text/ts",description:"TypeScript format"},{extensions:["tsx"],mime:"text/tsx",description:"TypeScript Syntax Extension / TypeScript XML format"}],s=n.reduce(((t,e)=>(t[e.mime]=e.description,t)),{}),o=n.reduce(((t,e)=>{for(const i of e.extensions)t[i]=e.mime;return t}),{}),a=n.reduce(((t,e)=>(t[e.mime]=e.extensions,t)),{});function r(t){const e=t=t||"",i=(t=(t=t.split("?").shift()).split("/").pop()).split(".").pop();let n=o[i];return n||(n=e.match(/api\.observablehq\.com/)||e.match(/\.ojs$/)?"application/javascript-observable":"application/binary"),n}class c extends i{constructor(t){super(t),this.index={"/":{kind:"directory",path:"/",name:""}}}async*list(t,{recursive:e=!1}={}){await this._init();const i=this.normalizePath(t),n=this._getPaths();for(const t of n){if(0!==t.indexOf(i)||t===i)continue;const n=t.substring(i.length+1).split("/");(e||1===n.length)&&(yield this._getFileInfo(t))}}async stats(t){await this._init();const e=this.normalizePath(t);return this._getFileInfo(e)}async remove(t){await this._init();const e=await this.stats(t);if(!e)return!1;for await(const{path:t}of this.list(e,{recursive:!0}))"/"!==t&&delete this.index[t];delete this.index[e.path]}async write(t,e){await this._init(),await this._doWrite(t,e)}async*read(t){await this._init();const e=this.normalizePath(t),i=this.index[e];if(!i||"file"!==i.kind)return;const n=i.content;yield*n}async _init(){return this._initPromise=this._initPromise||Promise.resolve().then((()=>this._addEntries(this.options.files)))}async _doWrite(t,e){const i=this.normalizePath(t).split("/").filter((t=>!!t)),n=[""];let s;for(let t=0;t<i.length;t++){const e=i[t];n.push(e);const o=n.join("/");s=this.index[o]=this.index[o]||{path:o,name:e,kind:"directory"}}s.kind="file",s.content=[],s.type=r(s.path),s.size=0,s.lastModified=Date.now(),"function"==typeof e&&(e=e());for await(let t of e)t=new Uint8Array(t),s.content.push(t),s.size+=t.length}async _addEntries(t={}){for(let[e,i]of Object.entries(t))"string"==typeof i?i=[(new TextEncoder).encode(i)]:i instanceof Uint8Array&&(i=[i]),await this._doWrite(e,i)}_getPaths(){return this._getIndexPaths(this.index)}_getIndexPaths(t){return Object.keys(t).sort((function(t,e){return t>e?1:t<e?-1:0}))}_getFileInfo(t){const e=this.index[t];if(!e)return null;const i={...e};return delete i.content,i}}class p extends i{constructor(t){if(super(t),!this.fs)throw new Error("File System is not defined.");if(!this.rootDir)throw new Error("Root directory is not defined.")}get rootDirSegments(){return this._rootDirSegments||(this._rootDirSegments=this._toPathSegments(this.options.rootDir||"./")),this._rootDirSegments}get rootDir(){return this._rootDir||(this._rootDir=this.rootDirSegments.join("/")),this._rootDir}get fs(){return this.options.fs}async _init(){return this._initialized=this._initialized||(async()=>{const t=this.rootDir;return await l(this.fs,t)})()}async*list(t,{recursive:e=!1}={}){await this._init();const i=this._expandPath(t),n=await this._stats(i);if(n&&"directory"===n.kind)for await(let t of f(this.fs,i,e))yield this._getFileInfo(t.path,t.stat)}async stats(t){await this._init();const e=this._expandPath(t);return this._stats(e)}async remove(t){await this._init();const e=this._expandPath(t);return await h(this.fs,e)}async write(t,e){await this._init();const i=this._expandPath(t),n=d(i);let s;await this.fs.mkdir(n,{recursive:!0});try{s=await this.fs.open(i,"w"),"function"==typeof e&&(e=e()),await s.writeFile(e)}finally{s&&await s.close()}}async*read(t,{start:e=0,bufferSize:i=8192}={}){await this._init();const n=this._expandPath(t);let s;try{s=await this.fs.open(n,"r");const t=0;for(let n=e,o=0;;n+=o){const e=new Uint8ClampedArray(i),a=await s.read(e,t,i,n);if(o=a.bytesRead,0===o)break;yield o<i?a.buffer.slice(0,o):a.buffer}}finally{s&&await s.close()}}async _stats(t){try{const e=await this.fs.stat(t);return this._getFileInfo(t,e)}catch(t){if("ENOENT"!==t.code)throw t;return null}}_getFileInfo(t,e){const i=this._toPathSegments(t).slice(this.rootDirSegments.length);i.unshift("");const n=i[i.length-1];let s={path:t=i.join("/"),name:n,kind:""};return e.isFile()?(s.size=e.size||0,s.type=r(t),s.kind="file",s.lastModified=new Date(e.mtime).getTime()):s.kind="directory",s}_toPathSegments(t){return(t=this.normalizePath(t)).split("/")}_expandPath(t){const e="object"==typeof t?t.path:t+"",i=this._toPathSegments(e).filter((t=>!!t));return i.unshift(...this.rootDirSegments),i.join("/")}}function d(t){const e=t.split("/");return e.pop(),e.join("/")}function m(...t){let e=t.map((t=>t.split("/"))).reduce(((t,e)=>(t.push(...e),t)),[]);const i=e.shift();return e=e.filter((t=>!!t&&"."!==t)),e.unshift(i),e.join("/")}async function l(t,e){try{const i=await t.stat(e);if(i&&i.isDirectory())return!0}catch(t){}const i=d(e);return e!==i&&(await l(t,i),await t.mkdir(e,{recursive:!0}),!0)}async function h(t,e){try{if((await t.stat(e)).isDirectory()){for await(let i of f(t,e,!1))await h(t,i.path);await t.rmdir(e)}else await t.unlink(e)}catch(t){if("ENOENT"!==t.code)throw t}}async function*f(t,e,i){try{if((await t.stat(e)).isDirectory()){const n=await t.readdir(e);for(const s of n.sort()){const n=m(e,s);let o=await t.stat(n);yield{path:n,stat:o},i&&o.isDirectory()&&(yield*f(t,n,!0))}}}catch(t){if("ENOENT"!==t.code)throw t}}class u extends i{constructor(t){if(super(t),!this.rootHandler)throw new Error("Root directory handler is not defined")}get rootHandler(){return this.options.rootHandler}async*list(t,{recursive:e=!1}={}){const i=this._getPathSegments(t),n=await this._getHandler(i,!1),s=this;"directory"===n.kind&&(yield*async function*t(e,i,n){for await(const o of i.values())yield await s._getFileInfo([...e,o.name],o),n&&"directory"==o.kind&&(yield*t([...e,o.name],o,n))}(i,n,e))}async stats(t){const e=this._getPathSegments(t),i=await this._getHandler(e,!1);return i?await this._getFileInfo(e,i):null}async remove(t){let e=this._getPathSegments(t);const i=e.pop(),n=await this._getHandler(e,!1),s=[];if(i)s.push(i);else for await(const t of n.values())s.push(t.name);for(const t of s)await n.removeEntry(t,{recursive:!0});return!0}async write(t,e){const i=this._getPathSegments(t),n=await this._getFileHandler(i),s=await n.createWritable();try{"function"==typeof e&&(e=e());for await(let t of e)await s.write(t.buffer?t.buffer:t)}finally{await s.close()}}async*read(t){const e=this._getPathSegments(t),i=await this._getFileHandler(e,!1);if(!i)return;const n=(await i.getFile()).stream().getReader();try{let t;for(;(t=await n.read())&&!t.done;)yield t.value}finally{n.cancel&&n.cancel()}}async _getHandler(t,e=!1){let i;try{i=await this._getFileHandler(t,e)}catch(n){try{i=await this._getDirectoryHandler(t,e)}catch(t){}}return i}async _getDirectoryHandler(t,e=!0){t=[...t];let i=this.rootHandler;for(let n of t)i=await i.getDirectoryHandle(n,{create:e});return i}async _getFileHandler(t,e=!0){const i=(t=[...t]).pop();let n=this.rootHandler;for(let e of t)n=await n.getDirectoryHandle(e,{create:!0});return await n.getFileHandle(i,{create:e})}async _getFileInfo(t,e){const i=(t=[...t])[t.length-1];t.unshift("");const n=t.join("/"),s={kind:"",path:n,name:i};if("directory"===e.kind)s.kind="directory";else{s.kind="file";const t=await e.getFile();s.size=t.size,s.type=r(n),s.lastModified=t.lastModified}return s}_getPathSegments(t){return("object"==typeof t?t.path:t+"").split("/").filter((t=>!!t))}}function x({filesApi:t,log:e=console.log,wrapper:i={}}){return["list","stats","remove","write","read","copy","move"].reduce(((i,n)=>{const s=t[n].bind(t);return i[n]=(...i)=>{let o={method:n,stage:"enter",args:i};try{return e(o),o.result=s.call(t,...i)}catch(t){o.error=t}finally{o.stage="exit",e(o)}},i}),i)}async function w({handlerKey:t="root-dir",readwrite:e=!0,index:i={},get:n=(async t=>i[t]),set:s=(async(t,e)=>i[t]=e),del:o=(async t=>delete i[t])}={}){let a=await n(t);if(a||(a=await window.showDirectoryPicker(),await s(t,a)),!await async function(t,e){const i={};e&&(i.mode="readwrite");let n=!1;"granted"===await t.queryPermission(i)&&(n=!0);n||"granted"!==await t.requestPermission(i)||(n=!0);return n}(a,e))throw new Error("Access was not granted");if(!await async function(t){let e=!1;try{if("file"===t.kind)await t.getFile();else for await(let e of t.values())break;e=!0}catch(i){console.log(i,t),e=!1}return e}(a))throw await o(t),new Error("Can not access to the folder. Please try again.");return new u({rootHandler:a})}export{u as BrowserFilesApi,i as FilesApi,c as MemFilesApi,p as NodeFilesApi,x as addFilesApiLogger,o as extensionToMimeType,r as getMimeType,a as mimeTypeToExtensions,n as mimeTypes,s as mimeTypesDescriptions,t as normalizePath,w as openBrowserFilesApi};
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/index.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statewalker/webrun-files",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Basic files API",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/statewalker/webrun-files",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Mikhail Kotelnikov",
|
|
9
|
+
"email": "mikhail.kotelnikov@gmail.com"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/**/package.json",
|
|
15
|
+
"dist/**/*.js",
|
|
16
|
+
"src/**/*.js",
|
|
17
|
+
"index.js"
|
|
18
|
+
],
|
|
19
|
+
"module": "src/index.js",
|
|
20
|
+
"main": "src/index.js",
|
|
21
|
+
"jsdelivr": "dist/index.js",
|
|
22
|
+
"unpkg": "dist/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
"umd": "./dist/index-umd.min.js",
|
|
25
|
+
"default": "./src/index.js"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@statewalker/rollup": "^0.1.6",
|
|
29
|
+
"@statewalker/webrun-files-tests": "https://github.com/statewalker/webrun-files-test",
|
|
30
|
+
"eslint": "^8",
|
|
31
|
+
"expect.js": "^0.3",
|
|
32
|
+
"mocha": "^10",
|
|
33
|
+
"rollup": "^3"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git@github.com:statewalker/webrun-files.git"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"eslint": "eslint src",
|
|
41
|
+
"rollup": "rollup -c",
|
|
42
|
+
"test": "mocha -R spec ./test/index.js",
|
|
43
|
+
"prepublishOnly": "rm -rf dist && yarn test && yarn rollup"
|
|
44
|
+
},
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import FilesApi from "./FilesApi.js";
|
|
2
|
+
import getMimeType from "./getMimeType.js";
|
|
3
|
+
|
|
4
|
+
export default class BrowserFilesApi extends FilesApi {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super(options);
|
|
7
|
+
if (!this.rootHandler) {
|
|
8
|
+
throw new Error("Root directory handler is not defined");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get rootHandler() {
|
|
13
|
+
return this.options.rootHandler;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async *list(file, { recursive = false } = {}) {
|
|
17
|
+
const segments = this._getPathSegments(file);
|
|
18
|
+
const handle = await this._getHandler(segments, false);
|
|
19
|
+
const that = this;
|
|
20
|
+
if (handle.kind === "directory") {
|
|
21
|
+
yield* listChildren(segments, handle, recursive);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function* listChildren(segments, dirHandle, recursive) {
|
|
25
|
+
for await (const handle of dirHandle.values()) {
|
|
26
|
+
yield await that._getFileInfo([...segments, handle.name], handle);
|
|
27
|
+
if (recursive && handle.kind == "directory") {
|
|
28
|
+
yield* listChildren(
|
|
29
|
+
[...segments, handle.name],
|
|
30
|
+
handle,
|
|
31
|
+
recursive,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async stats(file) {
|
|
39
|
+
const segments = this._getPathSegments(file);
|
|
40
|
+
const handle = await this._getHandler(segments, false);
|
|
41
|
+
return handle ? await this._getFileInfo(segments, handle) : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async remove(file) {
|
|
45
|
+
let segments = this._getPathSegments(file);
|
|
46
|
+
const name = segments.pop();
|
|
47
|
+
const parentHandle = await this._getHandler(segments, false);
|
|
48
|
+
const toRemove = [];
|
|
49
|
+
if (name) {
|
|
50
|
+
toRemove.push(name);
|
|
51
|
+
} else {
|
|
52
|
+
for await (const childHandle of parentHandle.values()) {
|
|
53
|
+
toRemove.push(childHandle.name);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const name of toRemove) {
|
|
57
|
+
await parentHandle.removeEntry(name, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async write(file, content) {
|
|
63
|
+
const segments = this._getPathSegments(file);
|
|
64
|
+
const fileHandle = await this._getFileHandler(segments);
|
|
65
|
+
const writable = await fileHandle.createWritable(); // Write the contents of the file to the stream.
|
|
66
|
+
try {
|
|
67
|
+
if (typeof content === "function") content = content();
|
|
68
|
+
for await (let block of content) {
|
|
69
|
+
await writable.write(block.buffer ? block.buffer : block); // Close the file and write the contents to disk.
|
|
70
|
+
}
|
|
71
|
+
} finally {
|
|
72
|
+
await writable.close();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async *read(file /* { start = 0, bufferSize = 1024 * 8 } = {} */) {
|
|
77
|
+
const segments = this._getPathSegments(file);
|
|
78
|
+
const fileHandle = await this._getFileHandler(segments, false);
|
|
79
|
+
if (!fileHandle) return;
|
|
80
|
+
const fileData = await fileHandle.getFile();
|
|
81
|
+
const stream = fileData.stream();
|
|
82
|
+
const reader = stream.getReader();
|
|
83
|
+
try {
|
|
84
|
+
let chunk;
|
|
85
|
+
while ((chunk = await reader.read()) && !chunk.done) {
|
|
86
|
+
yield chunk.value;
|
|
87
|
+
}
|
|
88
|
+
} finally {
|
|
89
|
+
reader.cancel && reader.cancel();
|
|
90
|
+
// stream.cancel && stream.cancel();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// TODO:
|
|
95
|
+
// async copy(fromPath, toPath, options = {}) { ... }
|
|
96
|
+
|
|
97
|
+
// TODO:
|
|
98
|
+
// async move(fromPath, toPath, options = {}) { ... }
|
|
99
|
+
|
|
100
|
+
async _getHandler(segments, create = false) {
|
|
101
|
+
let handle;
|
|
102
|
+
try {
|
|
103
|
+
handle = await this._getFileHandler(segments, create);
|
|
104
|
+
} catch (e) {
|
|
105
|
+
try {
|
|
106
|
+
handle = await this._getDirectoryHandler(segments, create);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
// Just ignore it
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return handle;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async _getDirectoryHandler(segments, create = true) {
|
|
115
|
+
segments = [...segments];
|
|
116
|
+
let dirHandle = this.rootHandler;
|
|
117
|
+
for (let directoryName of segments) {
|
|
118
|
+
dirHandle = await dirHandle.getDirectoryHandle(directoryName, { create });
|
|
119
|
+
}
|
|
120
|
+
return dirHandle;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async _getFileHandler(segments, create = true) {
|
|
124
|
+
segments = [...segments];
|
|
125
|
+
const filename = segments.pop();
|
|
126
|
+
let dirHandle = this.rootHandler;
|
|
127
|
+
for (let directoryName of segments) {
|
|
128
|
+
dirHandle = await dirHandle.getDirectoryHandle(directoryName, {
|
|
129
|
+
create: true,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const fileHandler = await dirHandle.getFileHandle(filename, { create });
|
|
133
|
+
return fileHandler;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async _getFileInfo(segments, handle) {
|
|
137
|
+
segments = [...segments];
|
|
138
|
+
const name = segments[segments.length - 1];
|
|
139
|
+
segments.unshift("");
|
|
140
|
+
const path = segments.join("/");
|
|
141
|
+
const info = {
|
|
142
|
+
kind: "",
|
|
143
|
+
path,
|
|
144
|
+
name,
|
|
145
|
+
};
|
|
146
|
+
if (handle.kind === "directory") {
|
|
147
|
+
info.kind = "directory";
|
|
148
|
+
} else {
|
|
149
|
+
info.kind = "file";
|
|
150
|
+
const file = await handle.getFile();
|
|
151
|
+
info.size = file.size;
|
|
152
|
+
info.type = getMimeType(path);
|
|
153
|
+
info.lastModified = file.lastModified;
|
|
154
|
+
}
|
|
155
|
+
return info;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
_getPathSegments(file) {
|
|
159
|
+
let filePath = typeof file === "object" ? file.path : file + "";
|
|
160
|
+
// filePath = this.normalizePath(filePath);
|
|
161
|
+
const segments = filePath.split("/").filter((s) => !!s);
|
|
162
|
+
return segments;
|
|
163
|
+
}
|
|
164
|
+
}
|
package/src/FilesApi.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import normalizePath from "./normalizePath.js";
|
|
2
|
+
import notImplemented from "./notImplemented.js";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* FileInfo
|
|
6
|
+
* - `kind` - (`file`|`directory`) kind of the returned entity
|
|
7
|
+
* - `name` - the local name of this file entry (the last segment of the path)
|
|
8
|
+
* - `path` - path to the file from the root of the "file system"
|
|
9
|
+
* - `type` - an optional mime type of the file content; it is not defined for directories
|
|
10
|
+
* - `size` - size of the file (only for files)
|
|
11
|
+
* - `lastModified` - modification time as a long integer
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* This is a common interface providing access to methods dealing with files.
|
|
16
|
+
*/
|
|
17
|
+
export default class FilesApi {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options || {};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns an async generator providing information about files (FileInfo[]).
|
|
24
|
+
* Paths corresponding to directories has the "/" trailing symbol.
|
|
25
|
+
* @param {Object|String} file path to the folder or an object returned by the `list` or `info` methods
|
|
26
|
+
* @param {Object} options optional parameters
|
|
27
|
+
* @param {Boolean} options.recursive this flag defines if the returned generator
|
|
28
|
+
* should give access to all child directories and files or just direct children
|
|
29
|
+
* @return {AsyncGenerator<FileInfo>} generator returning information about
|
|
30
|
+
* individual file entries
|
|
31
|
+
*/
|
|
32
|
+
async *list(file, options = {}) {
|
|
33
|
+
yield notImplemented(file, options);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns information about an individual file.
|
|
38
|
+
* @param {Object|String} file path to the file or an object returned by
|
|
39
|
+
* the `list` or `info` methods
|
|
40
|
+
* @return {FileInfo} information about the file corresponding to the specified
|
|
41
|
+
* path or null if there is no such a file
|
|
42
|
+
*/
|
|
43
|
+
async stats(file) {
|
|
44
|
+
return notImplemented(file);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes file or folder corresponding to the specified path.
|
|
49
|
+
* All children of this entry will be removed as well.
|
|
50
|
+
* @param {Object|String} file path to the file to remove or an object
|
|
51
|
+
* returned by the `list` or `info` methods
|
|
52
|
+
* @return {Boolean} return true if the file was removed
|
|
53
|
+
*/
|
|
54
|
+
async remove(file) {
|
|
55
|
+
return notImplemented(file);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates and returns a write stream for the file with the specified path.
|
|
60
|
+
* @param {Object|String} file path to the file or an object returned by
|
|
61
|
+
* the `list` or `info` methods
|
|
62
|
+
* @param {AsyncGenerator|AsyncIterator} content async generator function providing the binary
|
|
63
|
+
* file content to store
|
|
64
|
+
* @return {any} result of the action execution
|
|
65
|
+
*/
|
|
66
|
+
async write(file, content) {
|
|
67
|
+
return notImplemented(file, content);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Creates an returns an AsyncIterator returning byte blocks with the file content.
|
|
72
|
+
* @param {Object|String} file path to the file or an object returned by
|
|
73
|
+
* the `list` or `info` methods
|
|
74
|
+
* @param {object} options read parameters
|
|
75
|
+
* @param {number} options.start starting read position; default value is 0
|
|
76
|
+
* @param {number} options.bufferSize the optional size of returned chunks buffers
|
|
77
|
+
*/
|
|
78
|
+
async *read(file, options) {
|
|
79
|
+
yield notImplemented(file, options);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Copies a file from one path to another.
|
|
84
|
+
* @param {String} fromPath path to the source file
|
|
85
|
+
* @param {String} toPath path to the target file
|
|
86
|
+
* @param {Object} options additional options
|
|
87
|
+
* @param {Store} options.recursive if this flag is true then subfolders are also copied
|
|
88
|
+
* @param {Store} options.target optional target store where data should be copied
|
|
89
|
+
* @return {Boolean} if the file was successfully copied
|
|
90
|
+
*/
|
|
91
|
+
async copy(source, target, options = {}) {
|
|
92
|
+
const doCopy = async (sourcePath, targetPath, sourceFile) => {
|
|
93
|
+
const suffix = sourceFile.path.substring(sourcePath.length);
|
|
94
|
+
targetPath = `${targetPath}${suffix}`;
|
|
95
|
+
const content = this.read(sourceFile);
|
|
96
|
+
return await this.write(targetPath, content);
|
|
97
|
+
};
|
|
98
|
+
const recursive = options.recursive === undefined || !!options.recursive;
|
|
99
|
+
const sourceFile = await this.stats(source);
|
|
100
|
+
if (sourceFile.kind === "directory") {
|
|
101
|
+
for await (const file of this.list(source, { ...options, recursive })) {
|
|
102
|
+
if (file.kind === "directory") continue;
|
|
103
|
+
await doCopy(source, target, file);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
await doCopy(source, target, sourceFile);
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Moves files from the initial position to the target path.
|
|
113
|
+
* The default implementation creates a new file copy in the new location
|
|
114
|
+
* and removes the old file.
|
|
115
|
+
*
|
|
116
|
+
* @param {String} fromPath path to the source file
|
|
117
|
+
* @param {String} toPath path to the target file
|
|
118
|
+
* @param {Object} options additional options
|
|
119
|
+
* @param {Store} options.target optional target store where data should be moved
|
|
120
|
+
* @return {Boolean} if the file was successfully moved
|
|
121
|
+
*/
|
|
122
|
+
async move(fromPath, toPath, options = {}) {
|
|
123
|
+
if (!await this.copy(fromPath, toPath, options)) return false;
|
|
124
|
+
await this.remove(fromPath);
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
normalizePath(file) {
|
|
129
|
+
let filePath = typeof file === "object" ? file.path : file + "";
|
|
130
|
+
return normalizePath(filePath);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import FilesApi from "./FilesApi.js";
|
|
2
|
+
import getMimeType from "./getMimeType.js";
|
|
3
|
+
|
|
4
|
+
export default class MemFilesApi extends FilesApi {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super(options);
|
|
7
|
+
this.index = {
|
|
8
|
+
"/" : {
|
|
9
|
+
"kind": "directory",
|
|
10
|
+
"path": "/",
|
|
11
|
+
"name": ""
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async *list(file, { recursive = false } = {}) {
|
|
17
|
+
await this._init();
|
|
18
|
+
const filePath = this.normalizePath(file);
|
|
19
|
+
const paths = this._getPaths();
|
|
20
|
+
for (const path of paths) {
|
|
21
|
+
if (path.indexOf(filePath) !== 0 || path === filePath) continue;
|
|
22
|
+
const suffix = path.substring(filePath.length + 1);
|
|
23
|
+
const segments = suffix.split("/");
|
|
24
|
+
if (recursive || segments.length === 1) {
|
|
25
|
+
yield this._getFileInfo(path);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async stats(file) {
|
|
31
|
+
await this._init();
|
|
32
|
+
const filePath = this.normalizePath(file);
|
|
33
|
+
return this._getFileInfo(filePath);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async remove(file) {
|
|
37
|
+
await this._init();
|
|
38
|
+
const fileInfo = await this.stats(file);
|
|
39
|
+
if (!fileInfo) return false;
|
|
40
|
+
for await (const { path } of this.list(fileInfo, { recursive: true })) {
|
|
41
|
+
if (path !== "/") delete this.index[path];
|
|
42
|
+
}
|
|
43
|
+
delete this.index[fileInfo.path];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async write(file, content) {
|
|
47
|
+
await this._init();
|
|
48
|
+
await this._doWrite(file, content);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async *read(file /* { start = 0, bufferSize = 1024 * 8 } = {} */) {
|
|
52
|
+
await this._init();
|
|
53
|
+
const filePath = this.normalizePath(file);
|
|
54
|
+
const fileInfo = this.index[filePath];
|
|
55
|
+
if (!fileInfo || fileInfo.kind !== "file") return ; // throw new Error("Not a file");
|
|
56
|
+
const content = fileInfo.content;
|
|
57
|
+
yield* content;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// TODO:
|
|
61
|
+
// async copy(fromPath, toPath, options = {}) { ... }
|
|
62
|
+
|
|
63
|
+
// TODO:
|
|
64
|
+
// async move(fromPath, toPath, options = {}) { ... }
|
|
65
|
+
|
|
66
|
+
async _init() {
|
|
67
|
+
return this._initPromise = this._initPromise ||
|
|
68
|
+
Promise.resolve().then(() => this._addEntries(this.options.files));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async _doWrite(file, content) {
|
|
72
|
+
const filePath = this.normalizePath(file);
|
|
73
|
+
const segments = filePath.split("/").filter((s) => !!s);
|
|
74
|
+
const pathSegments = [""];
|
|
75
|
+
let info;
|
|
76
|
+
for (let i = 0; i < segments.length; i++) {
|
|
77
|
+
const name = segments[i];
|
|
78
|
+
pathSegments.push(name);
|
|
79
|
+
const path = pathSegments.join("/");
|
|
80
|
+
info = this.index[path] = this.index[path] || {
|
|
81
|
+
path,
|
|
82
|
+
name,
|
|
83
|
+
kind: "directory",
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
info.kind = "file";
|
|
87
|
+
info.content = [];
|
|
88
|
+
info.type = getMimeType(info.path);
|
|
89
|
+
info.size = 0;
|
|
90
|
+
info.lastModified = Date.now();
|
|
91
|
+
if (typeof content === "function") content = content();
|
|
92
|
+
for await (let chunk of content) {
|
|
93
|
+
chunk = new Uint8Array(chunk);
|
|
94
|
+
info.content.push(chunk);
|
|
95
|
+
info.size += chunk.length;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async _addEntries(index = {}) {
|
|
100
|
+
for (let [path, content] of Object.entries(index)) {
|
|
101
|
+
if (typeof content === "string") {
|
|
102
|
+
content = [new TextEncoder().encode(content)];
|
|
103
|
+
} else if (content instanceof Uint8Array) {
|
|
104
|
+
content = [content];
|
|
105
|
+
}
|
|
106
|
+
await this._doWrite(path, content);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
_getPaths() {
|
|
111
|
+
return this._getIndexPaths(this.index);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
_getIndexPaths(index) {
|
|
115
|
+
return Object.keys(index).sort(compare);
|
|
116
|
+
function compare(a, b) {
|
|
117
|
+
return a > b ? 1 : a < b ? -1 : 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_getFileInfo(path) {
|
|
122
|
+
const fileInfo = this.index[path];
|
|
123
|
+
if (!fileInfo) return null;
|
|
124
|
+
const info = { ...fileInfo };
|
|
125
|
+
delete info.content;
|
|
126
|
+
return info;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
}
|