core: Writing to specific folders not working?

Hi I’ve been using BrowserFS and recently translated to ZenFS and I am not clear at all on how this mounting system works.

So I want to do something with IndexedDB like

import {configure} from '@zenfs/core'
import * as fs from '@zenfs/core/promises'
import { IndexedDB } from '@zenfs/dom';

await configure({
    mounts:{
        '/data':IndexedDB
    }
});

await fs.writeFile('/data/list.csv','A,B,C');

And I get

Error: No such file or directory

???

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

No the issue there was I didn’t see I had pasted in the script tag twice and it was causing overrides. I still can’t seem to work with the directory/subdirectory system in a way that makes any sense yet though. It seems like if I call mkdir it just creates a new StoreFS endpoint (which is still IDB in browser?) and I can’t do subdirectories e.g. /data/dir, which in the case of IDB would just be a key in a flat mapped dictionary anyway but would tell us to send it there vs e.g. /localstorage/dir

I also get different behavior if I use the fs import versus importing the @zenfs/core/promises, where I just get undefineds when checking directories and stuff with fs while the async calls actually error and enforce things for me.

I’ve ended up just converting my whole wrapper file (which basically just documents the API) to native indexedDB calls and I get the exact behavior I want.

This is the hacky zenfs stuff I’ve tried but like createFolder errors in this usage. Other issue is I’m not accessing the same DB version each session so the application memory increases but I dont get the previous state on refresh.

//@ts-ignore
import { fs, configure, InMemory } from '@zenfs/core';
//@ts-ignore
import * as asyncFs from '@zenfs/core/promises' //has promise based calls 
//@ts-ignore
import { IndexedDB } from '@zenfs/dom';

const defaultDB = IndexedDB;

let mounted = {};

// Initialize file systems
export const initFS = async (
    mounts: { [key: string]: any } = { '/tmp': InMemory, '/data': defaultDB },
    oninit = (asyncFs) => { },
    onerror = (e) => { }
) => {

    let toMount = {};
    if(!mounted['/tmp']) mounts['/tmp'] = InMemory; 
    for(const key in mounts) {
        if(!mounted[key]) {
            toMount[key] = mounts[key];
        }
    }

    if(Object.keys(toMount).length === 0) {
        return;
    } 

    Object.assign(mounted, mounts);

    console.log(mounted, asyncFs);
    try {
        await configure({
            mounts: toMount
        });
        oninit(asyncFs);
        
        return true;
    } catch (e) {
        onerror(e);
        return false;
    }
};

export const createFolder = async (foldername: string) => {
    await asyncFs.mkdir(foldername);
    return true;
}
export const deleteFolder = async (foldername: string) => {
    await asyncFs.rmdir(foldername);
    return true;
}

//make sure it starts with the /
export const formatPath = (path: string, dir?: string) => {
    if (!path.startsWith('/')) path = '/' + path;
    if (dir) {
        if (!dir.startsWith('/')) dir = '/' + dir;
        path = dir + path;
    }
    return path;
}

//asyncFs.stat(path).then((stats)=>{ if(stats.isDirectory()) console.log("directory") }); //etc

export const checkDirInit = async (path: string, dbType = defaultDB as any) => {
    path = formatPath(path);
    let splt = path.split('/').filter(Boolean);

    if (splt.length < 1) throw `Bad Path: ${path}`; // no directory or invalid path

    const [dbName, folderName, subFolderName] = splt;

    // Check if the DB (first part) exists
    if (!await dirExists('/' + dbName)) {
        //console.trace(dbName, 'DNE, initDB', path);
        await initFS({ ['/' + dbName]: dbType });
        console.log(await asyncFs.readdir('/'+dbName));
        // createFolder('/'+dbName)
    } else {
        //console.log(dbName, 'DB exists', path);
    }

    // If there is a third part (sub-folder), check if the second part (folder) exists and create it if not
    if (subFolderName) {
        const folderPath = '/' + dbName + '/' + folderName;
        if (!await asyncFs.exists(folderName)) {
            //console.log(folderName, 'DNE, mkdir', folderPath);
            await createFolder(folderName);
        } else {
            //console.log(folderName, 'dir exists', folderPath);
        }
    }

    return path;
}

//for zenfs paths, start with the folder name, e.g. 'data/' like so, as it will make a (default indexedDB) type 
// Check if a path exists
export const exists = async (path = ''):Promise<boolean> => {
    path = await checkDirInit(path);
    return await new Promise(async (res,rej) => {
        try{
            let ex = await asyncFs.exists(path);
            res(ex);
        } catch(er) {
            res(false);
        }
    });
};

I had this all working before in BrowserFS but there seem to be different caveats and it’s not obvious to me. wrapper.zip

Actually in isolation it works, that zip I linked had some other issues though. I’m gonna assume it’s on my end taskmanager.zip

I am bundling it with esbuild but same behavior with or without minification, here is my test application. Here is my test application. You can run it if you install the bundler system I use via npm i -g tinybuild which just wraps esbuild with a dev server.

taskmanager.zip