rustyscript icon indicating copy to clipboard operation
rustyscript copied to clipboard

Able to run web server?

Open TheOrdinaryWow opened this issue 8 months ago • 8 comments

Is it able to run a frontend server, like vinxi or so?

Minimal reproduction (not functional)

rusty.ts

use std::path::Path;

use rust_embed::RustEmbed;
use rustyscript::{Error, Module, Runtime, RuntimeOptions, Undefined, json_args};
use vfs::{EmbeddedFS, VfsPath};

#[derive(RustEmbed, Debug)]
#[folder = ".output"]
pub struct ServerFiles;

pub fn run() -> Result<(), Box<Error>> {
    let mut runtime = match Runtime::new(RuntimeOptions {
        ..Default::default()
    }) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("Error creating runtime: {}", e);
            return Err(Box::new(Error::Runtime(e.to_string())));
        }
    };

    let fs: EmbeddedFS<ServerFiles> = EmbeddedFS::new();
    let root: VfsPath = fs.into();

    let module_path = "server/index.mjs";
    let file = root
        .join(module_path)
        .map_err(|e| Box::new(Error::Runtime(e.to_string())))?;
    let mut content = String::new();

    file.open_file()
        .map_err(|e| Box::new(Error::Runtime(e.to_string())))?
        .read_to_string(&mut content)
        .map_err(|e| Box::new(Error::Runtime(e.to_string())))?;

    runtime.set_current_dir(Path::new(root.as_str())).unwrap();

    let module = Module::new(module_path, content);
    let module_handle = runtime.load_module(&module)?;
    runtime.call_entrypoint::<Undefined>(&module_handle, json_args!())?;

    Ok(())
}

And the embedded dir structure as followed.

.output
├── deno.json
├── nitro.json
├── public
│   ├── _build
│   ├── _server
│   ├── api
│   ├── assets
│   ├── favicon.ico
│   ├── logo192.png
│   ├── logo512.png
│   ├── manifest.json
│   └── robots.txt
└── server
    ├── chunks
    ├── index.mjs
    ├── index.mjs.map
    ├── node_modules
    └── package.json

Cargo.toml

[package]
name = "example"
version = "0.1.0"
edition = "2024"

[dependencies]
rust-embed = { version = "8.6.0", features = ["debug-embed"] }
rustyscript = { version = "0.11", features = ["web", "io", "node_experimental"] }
vfs = { version = "0.12", features = ["async-trait", "embedded-fs"] }
libffi-sys = { version = "*", features = ["system"] }

# https://github.com/rscarson/rustyscript/issues/332
[patch.crates-io]
deno_media_type = { git = 'https://github.com/denoland/deno_media_type', tag = "0.2.6" }

While running, error occurred.

> RUST_BACKTRACE=1 cargo run
   Compiling rust-js-launcher v0.1.0 (.../example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.78s
     Running `target/debug/rust-js-launcher`

thread 'main' panicked at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/v8-130.0.7/src/template.rs:1020:32:
null pointer dereference occurred
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/panicking.rs:117:22
   2: core::panicking::panic_nounwind_fmt
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/intrinsics/mod.rs:3241:9
   3: core::panicking::panic_null_pointer_dereference
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/panicking.rs:304:5
   4: v8::template::<impl v8::data::ObjectTemplate>::set_accessor_property
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/v8-130.0.7/src/template.rs:1020:32
   5: deno_core::runtime::bindings::op_ctx_template_or_accessor
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/bindings.rs:463:5
   6: deno_core::runtime::bindings::initialize_deno_core_ops_bindings
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/bindings.rs:395:7
   7: deno_core::runtime::jsruntime::JsRuntime::new_inner
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/jsruntime.rs:1047:7
   8: deno_core::runtime::jsruntime::JsRuntime::try_new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/jsruntime.rs:759:5
   9: <deno_core::runtime::jsruntime::JsRuntime as rustyscript::inner_runtime::RuntimeTrait>::try_new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/inner_runtime.rs:35:18
  10: rustyscript::inner_runtime::InnerRuntime<RT>::new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/inner_runtime.rs:252:32
  11: rustyscript::runtime::Runtime::new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/runtime.rs:79:21
  12: rust_js_launcher::rusty::run
             at ./src/rusty.rs:16:29
  13: rust_js_launcher::main
             at ./src/main.rs:4:11
  14: core::ops::function::FnOnce::call_once
             at .../.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
zsh: abort      RUST_BACKTRACE=1 cargo run

rust-js-launcher via 🦀 v1.88.0-nightly
× RUST_BACKTRACE=1 cargo run
   Compiling rust-js-launcher v0.1.0 (/.../example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.59s
     Running `target/debug/rust-js-launcher`

thread 'main' panicked at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/v8-130.0.7/src/template.rs:1020:32:
null pointer dereference occurred
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/panicking.rs:117:22
   2: core::panicking::panic_nounwind_fmt
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/intrinsics/mod.rs:3241:9
   3: core::panicking::panic_null_pointer_dereference
             at /rustc/2fa8b11f0933dae9b4e5d287cc10c989218e8b36/library/core/src/panicking.rs:304:5
   4: v8::template::<impl v8::data::ObjectTemplate>::set_accessor_property
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/v8-130.0.7/src/template.rs:1020:32
   5: deno_core::runtime::bindings::op_ctx_template_or_accessor
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/bindings.rs:463:5
   6: deno_core::runtime::bindings::initialize_deno_core_ops_bindings
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/bindings.rs:395:7
   7: deno_core::runtime::jsruntime::JsRuntime::new_inner
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/jsruntime.rs:1047:7
   8: deno_core::runtime::jsruntime::JsRuntime::try_new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/deno_core-0.323.0/runtime/jsruntime.rs:759:5
   9: <deno_core::runtime::jsruntime::JsRuntime as rustyscript::inner_runtime::RuntimeTrait>::try_new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/inner_runtime.rs:35:18
  10: rustyscript::inner_runtime::InnerRuntime<RT>::new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/inner_runtime.rs:252:32
  11: rustyscript::runtime::Runtime::new
             at .../.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustyscript-0.11.0/src/runtime.rs:79:21
  12: rust_js_launcher::rusty::run
             at ./src/rusty.rs:12:29
  13: rust_js_launcher::main
             at ./src/main.rs:4:11
  14: core::ops::function::FnOnce::call_once
             at .../.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
zsh: abort      RUST_BACKTRACE=1 cargo run

No idea where went wrong.

TheOrdinaryWow avatar Apr 07 '25 13:04 TheOrdinaryWow

Its possible but require a lot of work.

https://github.com/rscarson/rustyscript/issues/354#issuecomment-2770861762:

The runtime extension required every single extension to be configured to work, including a bunch of node specific APIs

Would not make sense to activate it without node support

utyfua avatar Apr 07 '25 17:04 utyfua

Its possible but require a lot of work.

#354 (comment):

The runtime extension required every single extension to be configured to work, including a bunch of node specific APIs Would not make sense to activate it without node support

Got it. In this case, since nitro can specify build presets, if I insist to work on it, should I choose preset deno-server or node-server? The API between them is quite different, so I don't want to mess this up.

TheOrdinaryWow avatar Apr 07 '25 18:04 TheOrdinaryWow

Very odd - it's choking on one of the extension Ops somehow

Are you adding any extensions?

rscarson avatar Apr 07 '25 18:04 rscarson

Very odd - it's choking on one of the extension Ops somehow

Are you adding any extensions?

Nope. Full code is above. Main file is simply running rusty::run().

If you want to I can make a minimal reproduction.

TheOrdinaryWow avatar Apr 08 '25 01:04 TheOrdinaryWow

Yes please

rscarson avatar Apr 08 '25 01:04 rscarson

Yes please

Here you go. https://github.com/TheOrdinaryWow/unknown-arm-still

TheOrdinaryWow avatar Apr 08 '25 02:04 TheOrdinaryWow

Yes please

Any update on this?

TheOrdinaryWow avatar Apr 13 '25 12:04 TheOrdinaryWow

Sorry I have not had much time to work on this recently

You have not been forgotten!

rscarson avatar Apr 17 '25 15:04 rscarson