cycpt
cycpt9mo ago

Rust run JS/TS

How can I embed this project into a rust application so that rust can run js ts and has runtime api For example fetch
4 Replies
cycpt
cycpt9mo ago
use deno_core can run js but can't run ts, and no runtime api
Deno AI Helper
Deno AI Helper9mo ago
To embed a JavaScript/TypeScript runtime into a Rust application, you can use the deno_core crate. This crate allows you to create a JavaScript runtime in your Rust application. Here's a step-by-step guide based on the documentation: 1. Add Dependencies: First, you need to add the deno_core and tokio dependencies to your project. You can do this by running the following commands:
$ cargo add deno_core
$ cargo add tokio --features=full
$ cargo add deno_core
$ cargo add tokio --features=full
2. Create a JavaScript Runtime: Next, you need to create an instance of JsRuntime, which is responsible for JavaScript execution. You can do this in an asynchronous Rust function. Here's an example:
async fn run_js(file_path: &str) -> Result<(), AnyError> {
let main_module = deno_core::resolve_path(file_path)?;
let runjs_extension = Extension::builder("runjs")
.ops(vec![
op_read_file::decl(),
op_write_file::decl(),
op_remove_file::decl(),
op_fetch::decl(),
])
.build();

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
extensions: vec![runjs_extension],
..Default::default()
});

js_runtime.execute_script(
&deno_core::resolve_url_or_path("./runtime.js").unwrap(),
include_str!("./runtime.js"),
)?;

js_runtime.execute_module(&main_module).await?;
js_runtime.run_event_loop().await?;
Ok(())
}
async fn run_js(file_path: &str) -> Result<(), AnyError> {
let main_module = deno_core::resolve_path(file_path)?;
let runjs_extension = Extension::builder("runjs")
.ops(vec![
op_read_file::decl(),
op_write_file::decl(),
op_remove_file::decl(),
op_fetch::decl(),
])
.build();

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
extensions: vec![runjs_extension],
..Default::default()
});

js_runtime.execute_script(
&deno_core::resolve_url_or_path("./runtime.js").unwrap(),
include_str!("./runtime.js"),
)?;

js_runtime.execute_module(&main_module).await?;
js_runtime.run_event_loop().await?;
Ok(())
}
3. Add a Fetch API: To add a fetch API, you can create a runtime.js file that will instantiate and make the fetch object globally available. Here's an example:
((globalThis) => {
const core = Deno.core;

globalThis.runjs = {
fetch: (url) => {
return core.ops.op_fetch(url);
},
};
})(globalThis);
((globalThis) => {
const core = Deno.core;

globalThis.runjs = {
fetch: (url) => {
return core.ops.op_fetch(url);
},
};
})(globalThis);
4. **Register the Fetch
Deno AI Helper
Deno AI Helper9mo ago
API**: You need to register the fetch API in your run_js function. Here's an example: ```rust let runjs_extension = Extension::builder("runjs") .ops(vec![ op_read_file::decl(), op_write_file::decl
cycpt
cycpt9mo ago
how like deno cli The same supports all runtime api Instead of doing it manually
More Posts
Set-Cookie - Header shows as "deleted"I am trying to use deno to script/scrape a website. I can successfully log in to the site (using fetI keep running into python download failed while trying to run 'cargo test' commandI am currently following the steps I. Deno's Contributing Guide and I am running cargo test but I kehow I can create an restful api with deno and firebase as database?how I can create an restful api with deno and firebase as database?How can I replace writeAllSync with the Streams API?How would one best refactor the following code to use the Web Streams API as suggested by the deprecIs possible to run nodejs (typescript) project with esm modules?I'm trying to run small nodejs project with esm modules. Simple example ```ts // foo.ts import { bAny tutorial links to help me understand the Schedule string in Deno.cron()I am currently working on an issue on the deno repo and I am trying to reconstruct the deno scheduleDeno Deploy failed:Uncaught Exception /Permission Denied : Deno.exit() is not allowedI have a college assignments that needs to be deployed using deno deploy but i get this message whenConnect to a WebSocket through a HTTP(S)/SOCKS5 proxyTitle - I am trying to connect to a `wss://` websocket through a proxy of choice at runtime. Is therExternal package code throws error: window is undefinedHello, I have imported an npm package to my project. And in this package's code, there is a conditiError with importsI have this fancy peace of code that import a bunch of files from a directory: ```ts const utils =