wasmerio / winterjs

Winter is coming... ❄️
https://winterjs.org/
MIT License
3.05k stars 53 forks source link

Serving assets from filesystem #28

Closed pi0 closed 1 year ago

pi0 commented 1 year ago

Hi. While implementing a Nitro preset for winterjs support, i have hitted a limitation where there is no API to read assets from provided filesystem mounts.

Is there any API currently available in order to read assets?

Arshia001 commented 1 year ago

While there is no FS API in WinterCG, Spiderfire has an fs module with the following shape which is enabled in WinterJS by default:

{
  "readBinary": function readBinary() {
      [native code]
  },
  "readString": function readString() {
      [native code]
  },
  "readDir": function readDir() {
      [native code]
  },
  "write": function write() {
      [native code]
  },
  "createDir": function createDir() {
      [native code]
  },
  "createDirRecursive": function createDirRecursive() {
      [native code]
  },
  "removeFile": function removeFile() {
      [native code]
  },
  "removeDir": function removeDir() {
      [native code]
  },
  "removeDirRecursive": function removeDirRecursive() {
      [native code]
  },
  "copy": function copy() {
      [native code]
  },
  "rename": function rename() {
      [native code]
  },
  "softLink": function softLink() {
      [native code]
  },
  "hardLink": function hardLink() {
      [native code]
  },
  "sync": {
    "readBinary": function readBinary() {
        [native code]
    },
    "readString": function readString() {
        [native code]
    },
    "readDir": function readDir() {
        [native code]
    },
    "write": function write() {
        [native code]
    },
    "createDir": function createDir() {
        [native code]
    },
    "createDirRecursive": function createDirRecursive() {
        [native code]
    },
    "removeFile": function removeFile() {
        [native code]
    },
    "removeDir": function removeDir() {
        [native code]
    },
    "removeDirRecursive": function removeDirRecursive() {
        [native code]
    },
    "copy": function copy() {
        [native code]
    },
    "rename": function rename() {
        [native code]
    },
    "softLink": function softLink() {
        [native code]
    },
    "hardLink": function hardLink() {
        [native code]
    }
  }
} 
jurijzahn8019 commented 8 months ago

Hi there,

do you know what is needed to be able to read file system?

I am running on mac m2 and

doing this:

import * as spiderfireFs from "fs";
const { readBinary, readDir } = spiderfireFs;

console.log("Here:", readDir("/"));
console.log("Here:", readDir("./"));
console.log("Here:", readDir("/dist"));
console.log("Here:", readDir("./dist/"));

starting like this:

wasmer run wasmer/winterjs --net --mapdir=dist:dist -vv dist/test.js 

having this in debug output

2024-03-15T15:56:08.764463Z DEBUG ThreadId(15) wasmer_run:execute_webc:run_command: wasmer_wasix::bin_factory::exec: current_dir=/ next_fd=6 Backing(OverlayFileSystem { primary: TmpFileSystem { fs: 
inode       type    name
0           dir     /
1           dir       .app
2           dir       .private
3           dir       bin
7           custom-file        wasmer
16          file        wasmer-winter
17          file        winterjs
4           dir       dev
8           custom-file        null
9           custom-file        zero
10          custom-file        urandom
11          custom-file        stdin
12          custom-file        stdout
13          custom-file        stderr
14          custom-file        tty
5           dir       etc
6           dir       tmp
15          arc-dir      dist
 }, secondaries: [RelativeOrAbsolutePathHack(OverlayFileSystem { primary: EmptyFileSystem, secondaries: [MappedPathFileSystem { inner: WebcVolumeFileSystem { volume: Volume { imp: VolumeSection { name: "atom", header: "\u{1e}\0\0\0\0\0\0\0\0", data: "", data_offset: 46 } } }, map: "wasmer_wasix::runtime::package_loader::load_package_tree::filesystem::{{closure}}" }] })] })

I only get something like this

Unhandled Promise Rejection: Error - Path /dist/ does not exist
guest271314 commented 4 months ago

If you can import you can use import attributes with type set to "json".

guest271314 commented 2 months ago

@Arshia001

#!/usr/bin/env -S /home/user/bin/winterjs exec
const f = await globalThis.fs.readBinary(new URL("nm_host.js", import.meta.url));
console.log(f);
 ./winter.js
Unhandled Promise Rejection: Error - Could not read file: file:///home/user/bin/nm_host.js
mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed: Device or resource busy
Segmentation fault (core dumped)