Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This package doesn't fully work #5

Open
RossComputerGuy opened this issue Jun 16, 2017 · 4 comments
Open

This package doesn't fully work #5

RossComputerGuy opened this issue Jun 16, 2017 · 4 comments

Comments

@RossComputerGuy
Copy link

RossComputerGuy commented Jun 16, 2017

I've been trying to use this inside of Runtime.js, but it doesn't work. Here is the code I created that actually works.

const MemoryFileSystem = require("memory-fs");
var vfs = {};

vfs.mountTable = {};
vfs.mountTable["/"] = new MemoryFileSystem();

function setupPath(path) {
    if(path[0] == "/") path = path.slice(1);
    if(path[path.length-1] != "/") path += "/";
    var mountPath = "";
    for(var i = 0;i < Object.keys(vfs.mountTable).length;i++) {
        var key = Object.keys(vfs.mountTable)[i];
        var value = vfs.mountTable[key];
        if(path.startsWith(key)) {
            mountPath = key;
            break;
        }
    }
    if(!vfs.mountTable[mountPath]) throw new Error(path+" is not mounted!");
    var opts = {};
    opts.mountPath = mountPath;
    opts.path = path;
    opts.vpath = path.replace(opts.mountPath,"/");
    return opts;
}

function appendSync(name,path,options) {
    var opts = setupPath(path);
    path = path.replace(opts.mountPath,"");
    return vfs.mountTable[opts.mountPath][name+"Sync"](opts.vpath,options);
}

function appendAsync(name,path,options,callback) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath][name](opts.vpath,options,callback);
}

vfs.mount = function(path,mount) {
    if(path[0] == "/") path = path.slice(1);
    if(path[path.length-1] != "/") path += "/";
    if(typeof(vfs.mountTable[path]) == "object") throw new Error(path+" is already mounted!");
    vfs.mountTable[path] = mount;
    return vfs;
};

vfs.unmount = function(path) {
    if(path[0] != "/") path = "/"+path;
    if(path[path.length-1] != "/") path += "/";
    if(typeof(vfs.mountTable[path]) != "object") throw new Error(path+" is not mounted!");
    delete vfs.mountTable[path];
    return vfs;
};

vfs.access = function(path,mode,callback) {
    return appendAsync("access",path,mode,callback);
};

vfs.accessSync = function(path,mode) {
    return appendSync("access",path,mode);
};

vfs.appendFile = function(file,data,options,callback) {
    var opts = setupPath(file);
    return vfs.mountTable[opts.mountPath]["appendFile"](opts.vpath,data,options,callback);
};

vfs.appendFileSync = function(file,data,options) {
    var opts = setupPath(file);
    return vfs.mountTable[opts.mountPath]["appendFileSync"](opts.vpath,data,options);
};

vfs.chmod = function(path,mode,callback) {
    return appendAsync("chmod",path,mode,callback);
};

vfs.chmodSync = function(path,mode) {
    return appendSync("chmod",path,mode);
};

vfs.chown = function(path,uid,gid,callback) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["chown"](opts.vpath,uid,gid,callback);
};

vfs.chownSync = function(path,uid,gid) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["chownSync"](opts.vpath,uid,gid);
};

vfs.createReadStream = function(path,options) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["createReadStream"](opts.vpath,options);
};

vfs.createWriteStream = function(path,options) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["createWriteStream"](opts.vpath,options);
};

vfs.exists = function(path,callback) {
    return appendAsync("exists",path,callback);
};

vfs.existsSync = function(path) {
    return appendSync("exists",path);
};

vfs.link = function(existingPath,newPath,callback) {
    console.warn("Linking Files between file systems is not yet supported!");
    return appendAsync("link",existingPath,newPath,callback);
};

vfs.linkSync = function(existingPath,newPath) {
    console.warn("Linking Files between file systems is not yet supported!");
    return appendSync("link",existingPath,newPath);
};

vfs.lstat = function(path,callback) {
    return appendAsync("lstat",path,callback);
};

vfs.lstatSync = function(path) {
    return appendSync("lstat",path);
};

vfs.mkdir = function(path,mode,callback) {
    return appendAsync("mkdir",path,mode,callback);
};

vfs.mkdirSync = function(path,mode) {
    return appendSync("mkdir",path,mode);
};

vfs.mkdtemp = function(prefix,options,callback) {
    return appendAsync("mkdtemp",prefix,options,callback);
};

vfs.mkdtempSync = function(prefix,options) {
    return appendSync("mkdir",prefix,options);
};

vfs.readdir = function(path,options,callback) {
    if(path != "/") return appendAsync("readdir",path,options,callback);
    return appendAsync("readdir",path,options,(err,res) => {
        if(err) return callback(err,res);
        for(var i = 0;i < Object.keys(vfs.mountTable).length;i++) {
            var key = Object.keys(vfs.mountTable)[i];
            key = key.split("/")[0];
            if(key != path) res.push(key);
        }
        return callback(err,res);
    });
};

vfs.readdirSync = function(path,options) {
    if(path != "/") return appendSync("readdir",path,options);
    var res = appendSync("readdir",path,options);
    for(var i = 0;i < Object.keys(vfs.mountTable).length;i++) {
        var key = Object.keys(vfs.mountTable)[i];
        key = key.split("/")[0];
        if(key != path) res.push(key);
    }
    for(var i = 0;i < res.length;i++) {
        if(res[i].length == 0) res.splice(i,1);
    }
    return res;
};

vfs.readFile = function(path,options,callback) {
    return appendAsync("readFile",path,options,callback);
};

vfs.readFileSync = function(path,options) {
    return appendSync("readFile",path,options);
};

vfs.readlink = function(path,options,callback) {
    return appendAsync("readlink",path,options,callback);
};

vfs.readlinkSync = function(path,options) {
    return appendSync("readlink",path,options);
};

vfs.realpath = function(path,options,callback) {
    return appendAsync("realpath",path,options,callback);
};

vfs.realpathSync = function(path,options) {
    return appendSync("realpath",path,options);
};

vfs.rename = function(oldPath,newPath,callback) {
    console.warn("renameing files and directories across file systems is not yet supported!");
    return appendAsync("rename",oldPath,newPath,callback);
};

vfs.renameSync = function(oldPath,newPath) {
    console.warn("renameing files and directories across file systems is not yet supported!");
    return appendSync("rename",oldPath,newPath);
};

vfs.rmdir = function(path,callback) {
    return appendAsync("rmdir",path,callback);
};

vfs.rmdirSync = function(path) {
    return appendSync("rmdir",path);
};

vfs.stat = function(path,callback) {
    return appendAsync("stat",path,callback);
};

vfs.statSync = function(path) {
    return appendSync("stat",path);
};

vfs.symlink = function(target,path,type,options,callback) {
    console.warn("symlinking files and directories across file systems is not yet supported!");
    var opts = setupPath(target);
    return vfs.mountTable[opts.mountPath]["symlink"](opts.vpath,path,type,options,callback);
};

vfs.symlinkSync = function(target,path,type,options) {
    console.warn("symlinking files and directories across file systems is not yet supported!");
    var opts = setupPath(target);
    return vfs.mountTable[opts.mountPath]["symlinkSync"](opts.vpath,path,type,options);
};

vfs.truncate = function(path,len,callback) {
    return appendAsync("truncate",path,len,callback);
};

vfs.truncateSync = function(path,len) {
    return appendSync("truncate",path,len);
};

vfs.unlink = function(path,callback) {
    return appendAsync("unlink",path,callback);
};

vfs.unlinkSync = function(path) {
    return appendSync("unlink",path);
};

vfs.unwatchFile = function(filename,listener) {
    return appendAsync("unwatchFile",filename,listener);
};

vfs.utimes = function(path,atime,mtime,callback) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["utimes"](opts.vpath,atime,mtime,callback);
};

vfs.utimesSync = function(path,atime,mtime) {
    var opts = setupPath(path);
    return vfs.mountTable[opts.mountPath]["utimesSync"](opts.vpath,atime,mtime);
};

vfs.watch = function(filename,options,listener) {
    return appendAsync("watch",filename,options,listener);
};

vfs.watchFile = function(filename,options,listener) {
    return appendAsync("watchFile",filename,options,listener);
};

vfs.writeFile = function(file,data,options,callback) {
    var opts = setupPath(file);
    return vfs.mountTable[opts.mountPath]["writeFile"](opts.vpath,data,options,callback);
};

vfs.writeFileSync = function(file,data,options) {
    var opts = setupPath(file);
    return vfs.mountTable[opts.mountPath]["writeFileSync"](opts.vpath,data,options);
};

module.exports = vfs;
@papandreou
Copy link
Owner

Hmm, could you share a bit about the circumstances where this module doesn't work? What are you trying to accomplish? Which version of node.js? Can you share a piece of code that uses mountfs and does the wrong thing?

@RossComputerGuy
Copy link
Author

It doesn't work on any version of node.js. I've tried to use it with memory-fs, but I always had an error with calling. My code was:

const MemoryFileSystem = require("memory-fs");
const MountFS = require("mountfs");
module.exports = new MountFS(new MemoryFileSystem());

@RossComputerGuy
Copy link
Author

What I'm trying to do is run a mountable VFS with the ability to mount other node.js compatible file systems.

@papandreou
Copy link
Owner

@SpaceboyRoss01 You can't pass another fs implementation to the MountFs constructor. The syntax is for mounting another file system is:

const MountFs = require("mountfs");
const mountFs = new MountFs();
mountFs.mount('/path/to/desired/mount/point', new MemoryFileSystem());
module.exports = mountFs;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants