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

Use RAWNODEFS with MEMFS on the same module #23566

Open
ceifa opened this issue Feb 1, 2025 · 9 comments
Open

Use RAWNODEFS with MEMFS on the same module #23566

ceifa opened this issue Feb 1, 2025 · 9 comments

Comments

@ceifa
Copy link

ceifa commented Feb 1, 2025

I have a library that uses emscripten, and I would like to add an option for the consumer to use MEMFS or RAWNODEFS.
I tried adding to the compilation -lnoderawfs.js -lnodefs.js, but the RAWNODEFS replaces the MEMFS automatically, on the following lines:

// Wrap the whole in-memory filesystem API with
// our Node.js based functions
for (var _key in NODERAWFS) {
  FS[_key] = _wrapNodeError(NODERAWFS[_key]);
}

Is it possible to import the MEMFS and NODERAWFS, but choose at runtime which one to use?

@hoodmane
Copy link
Collaborator

hoodmane commented Feb 1, 2025

What about using NODEFS and mounting all directories in a prerun hook? You'll also want to copy the environment and set the home directory and working directory. See here:
https://github.com/python/cpython/blob/d89a5f6a6e65511a5f6e0618c4c30a7aa5aba56a/Tools/wasm/emscripten/node_entry.mjs#L34

@sbc100
Copy link
Collaborator

sbc100 commented Feb 1, 2025

The point of NODERAWFS is to subsume / replace the whole FS, so you cannot mix with other filesystem times.

However, that is exactly what NODEFS is for. Which NODEFS you can mount whatever node directories you want inside of your VFS. You could even mount your entire root directory under /root or /native for example

@ceifa
Copy link
Author

ceifa commented Feb 2, 2025

What about using NODEFS and mounting all directories in a prerun hook? You'll also want to copy the environment and set the home directory and working directory. See here: https://github.com/python/cpython/blob/d89a5f6a6e65511a5f6e0618c4c30a7aa5aba56a/Tools/wasm/emscripten/node_entry.mjs#L34

Thanks for this example, very helpful. But it doesn't do the entire job. Because it traverses the "/" and mount each directory, I cannot do something like create a new directory on the root /, because it would create on MEMFS.

The point of NODERAWFS is to subsume / replace the whole FS, so you cannot mix with other filesystem times.

I don't want to mix, I want to replace it, but at runtime. Mounting the root directory in a /root doesn't work because it would create different directory calls and I want it to be transparent -> /var would be /root/var, but I would like it to be /var.

@sbc100
Copy link
Collaborator

sbc100 commented Feb 2, 2025

Can you explain a little more. Under what circumstances to you want to use the real node FS and under what circumstances do you want to use the emscripten VFS?

What if we allowed NODEFS to be mounted at the root /? Would that work for you?

@sbc100
Copy link
Collaborator

sbc100 commented Feb 2, 2025

I cannot do something like create a new directory on the root /, because it would create on MEMFS.

Does your application create new directories in the root directory? That seems like very unusual thing to want to do (or be able to do).

@sbc100
Copy link
Collaborator

sbc100 commented Feb 2, 2025

I cannot do something like create a new directory on the root /, because it would create on MEMFS.

Does your application create new directories in the root directory? That seems like very unusual thing to want to do (or be able to do).

Actually, if you tried to do this (create directories in the root) with NODERAWFS wouldn't it fail with permission errors? Or are you running node as root on your system?

@hoodmane
Copy link
Collaborator

hoodmane commented Feb 2, 2025

Under what circumstances to you want to use the real node FS and under what circumstances do you want to use the emscripten VFS?

I think a potentially useful feature would be to be able to make a single binary that uses noderawfs in node and uses memfs in browsers. But using nodefs is a good enough approximation that it might not be worth the effort to implement this.

@sbc100
Copy link
Collaborator

sbc100 commented Feb 2, 2025

I think a potentially useful feature would be to be able to make a single binary that uses noderawfs in node and uses memfs in browsers. But using nodefs is a good enough approximation that it might not be worth the effort to implement this.

Yes, I would hope that nodefs would be good enough in this case. But I'm trying to understand better the OP's exact use case to be sure.

@ceifa
Copy link
Author

ceifa commented Feb 2, 2025

Can you explain a little more. Under what circumstances to you want to use the real node FS and under what circumstances do you want to use the emscripten VFS?

I want to be able to choose between both. This is my project: https://github.com/ceifa/wasmoon
Because it is a library, I want to have an option on API which the consumer choose if the filesystem will be virtual or the real fs.

What if we allowed NODEFS to be mounted at the root /? Would that work for you?

Yes, I think it would fit. That is exactly what I tried at first, but because MEMFS is the default, it didn't work.

Does your application create new directories in the root directory? That seems like very unusual thing to want to do (or be able to do).

My library is a platform for running code in Lua, people can use it in many ways. I just would like to have it work as same as the real Lua C binary, to enable more use cases.

But actually I think you are quite right, I don't see a use-case for that, the python repo solution may fit great for now. I will make more tests on it.

Thank you very much guys.

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

3 participants