You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support mounting local directories in the browser via Filesystem API #196
The File System Access API can read local files and directories, and even exposes a createSyncAccessHandle method for files – super convenient for building Emscripten FS adapter:
// const dirHandle = await window.showDirectoryPicker();(asyncfunction(){constfileHandle=awaitwindow.showOpenFilePicker();constdirHandle=awaitwindow.showDirectoryPicker();varblob=newBlob([` self.onmessage = function(e) { console.log(e.data); }; // Rest of your worker code goes here. `],{type: "text/javascript"})// Sync methods can only be used in web workers on https sitesvarworker=newWorker(window.URL.createObjectURL(blob));worker.onmessage=function(e){console.log("Received: "+e.data);}worker.postMessage(fileHandle);worker.postMessage(dirHandle);})()
Until we can support local filesystem as Emscripten FS, there could be a “refresh” button that brings all the files from local directory into Playground. It could stat them and load just the updated ones. There could be a “update every 1s” checkbox.
Here’s some code from ChatGPT that could make a good starting point:
—-
Write a TypeScript function that uses the Filesystem API in the browser to read a file tree from the local filesystem into a JavaScript object. Each file name should be a key, its contents should be the value, and directories should be represented as nested objects. Parallelize up to 20 filesystem api operations.
typeFileTree={[fileName: string]: string|FileTree;};asyncfunctionreadFile(file: File): Promise<string>{consttext=awaitfile.text();returntext;}constMAX_CONCURRENT_OPERATIONS=20;asyncfunctionreadDirectory(directory: any): Promise<FileTree>{letresult: FileTree={};letoperations: Promise<any>[]=[];forawait(constentryofdirectory.values()){constoperation=(async()=>{if(entry.kind==='file'){constfile=awaitentry.getFile();consttext=awaitreadFile(file);result[entry.name]=text;}elseif(entry.kind==='directory'){constnestedDirectory=awaitentry.getDirectory();result[entry.name]=awaitreadDirectory(nestedDirectory);}})();operations.push(operation);// If we've hit the limit, wait for one to complete before starting a new oneif(operations.length>=MAX_CONCURRENT_OPERATIONS){awaitPromise.race(operations).then(()=>{operations=operations.filter(op=>op!==operation);});}}// Wait for all operations to completeawaitPromise.all(operations);returnresult;}asyncfunctionreadFileTree(rootDirectory: any): Promise<FileTree>{constfileTree=awaitreadDirectory(rootDirectory);returnfileTree;}
Usage:
asyncfunctionmain(){try{// Request a handle to the directory from the userconstrootDirectory=awaitwindow.showDirectoryPicker();// Read the file treeconstfileTree=awaitreadFileTree(rootDirectory);// Do something with the file treeconsole.log(fileTree);}catch(error){console.error('Failed to read file tree:',error);}}// Call the main function when the user clicks a button, for exampledocument.getElementById('readFilesButton').addEventListener('click',main);
Activity
adamziel commentedon Apr 22, 2023
Related:
https://github.com/jvilk/BrowserFS
http://jvilk.com/browserfs/1.4.1/
adamziel commentedon May 13, 2023
The File System Access API can read local files and directories, and even exposes a
createSyncAccessHandlemethod for files – super convenient for building Emscripten FS adapter:More about synchronous methods. Also, WHATWG Spec.
I also found an Emscripten PR that proposes a
EMSCRIPTEN_NATIVE_FSimplementation, although it seems to have a few issues.IDBFS Test
adamziel commentedon May 15, 2023
Exploratory PR.
A mapping of Emscripten concepts to FS API concepts:
adamziel commentedon May 24, 2023
Until we can support local filesystem as Emscripten FS, there could be a “refresh” button that brings all the files from local directory into Playground. It could stat them and load just the updated ones. There could be a “update every 1s” checkbox.
it’s similar to the explorations in #19
adamziel commentedon Jun 3, 2023
Here’s some code from ChatGPT that could make a good starting point:
—-
Write a TypeScript function that uses the Filesystem API in the browser to read a file tree from the local filesystem into a JavaScript object. Each file name should be a key, its contents should be the value, and directories should be represented as nested objects. Parallelize up to 20 filesystem api operations.
Usage:
It could fit the
websiteproject in this repo.adamziel commentedon Jun 12, 2023
Related Emscripten PR: Add support for read/seek on files in the native file system
adamziel commentedon Jun 22, 2023
Basic support with manual "sync" button was added in #548