Skip to content

Able to run web server? #360

@TheOrdinaryWow

Description

@TheOrdinaryWow

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions