D
Deno

help

Embedding all of deno in Rust?

CCherry 🍒2/7/2024
So, I know that you can use deno_core to roll a javascript runtime in Rust and use that, and use it with your own api and whatnot. But what if you want to be able to leverage already created deno libraries, but in your Rust program? Think of it as a sort of plugin system in your Rust app that can leverage all of deno's api (including packages) I guess, after a bit of reading, what I'm asking for is how to embed the deno runtime (not just the deno_core) into a Rust app, such that regular deno packages should work (if this is even possible)
CcrowlKats2/7/2024
the deno cli itself does not have a rust crate, and a bunch of functionality (including typescript typechecking, and other aspects) would need to be implemented manually ontop of the deno_runtime crate
CCherry 🍒2/7/2024
So far I've partially gotten a runtime working (along with a custom module loader which transpiles as well, and can do network requests; one library example I tested worked verbatim) But when it comes to async await it crashes with
thread 'main' panicked at ~\.cargo\registry\src\index.crates.io-6f17d22bba15001f\deno_unsync-0.3.2\src\task.rs:51:3:
assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at ~\.cargo\registry\src\index.crates.io-6f17d22bba15001f\deno_unsync-0.3.2\src\task.rs:51:3:
assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Any ideas how I could solve this?
CcrowlKats2/7/2024
hmm not sure. cc @mmastrac since deno_unsync error
Mmmastrac2/7/2024
@Cherry 🍒 make sure that you are using tokio's current_thread executor -- Deno only supports that particular threading model because of threading limitations in deno_core/v8. Can you share your tokio runtime builder expression?
CCherry 🍒2/7/2024
My code is pretty much verbatim from the examples (other than a custom module loader). I'll try out the current thread executor then!
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

mod module_loader;

use std::path::Path;
use std::rc::Rc;

use deno_runtime::deno_core::error::AnyError;
use deno_runtime::deno_core::op2;
use deno_runtime::deno_core::ModuleSpecifier;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;

deno_runtime::deno_core::extension!(hello_runtime, ops = [op_hello]);

#[op2(fast)]
fn op_hello(#[string] text: &str) {
println!("Hello {}!", text);
}

#[tokio::main]
async fn main() -> Result<(), AnyError> {
let client = reqwest::Client::new();

let js_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(module_loader::TypescriptModuleLoader::new(client)),
extensions: vec![hello_runtime::init_ops()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

mod module_loader;

use std::path::Path;
use std::rc::Rc;

use deno_runtime::deno_core::error::AnyError;
use deno_runtime::deno_core::op2;
use deno_runtime::deno_core::ModuleSpecifier;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;

deno_runtime::deno_core::extension!(hello_runtime, ops = [op_hello]);

#[op2(fast)]
fn op_hello(#[string] text: &str) {
println!("Hello {}!", text);
}

#[tokio::main]
async fn main() -> Result<(), AnyError> {
let client = reqwest::Client::new();

let js_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(module_loader::TypescriptModuleLoader::new(client)),
extensions: vec![hello_runtime::init_ops()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
Mmmastrac2/7/2024
Interesting. We should add tokio::main to those examples -- you'll need to use this:
#[tokio::main(flavor = "current_thread")]
#[tokio::main(flavor = "current_thread")]
That should Just Work
CCherry 🍒2/7/2024
Hey, nice. It worked as you said, thanks a lot!
Jjeff.hykin2/12/2024
@Cherry 🍒 this is something I tried a while ago but ended up stopping short. if you have a repo with an example setup I'd love to check it out!
CCherry 🍒2/12/2024
You can follow the official deno example, but with current_thread runtime https://github.com/denoland/deno/blob/main/runtime/examples/extension_with_ops/main.rs It'll work out of the box
Jjeff.hykin2/13/2024
Oh cool! Thanks!

Looking for more? Join the community!

Recommended Posts
Fresh project next to regular Deno projectHey, I have a question about adding a fresh project next to a regular Deno project. I have the follAstro 4.0 SupportHi there, just wanted to check in if someone from the Deno org could review the PR on the official Dimport { Eta } from "https://deno.land/x/eta@v3.0.3/src/index.ts" results in errorError message: "error trying to connect: invalid peer certificate: NotValidYet." Is there a problem A syantax error which I can not decodeI have attached 2 screenshots, describing the issue, i am using this api: https://docs.abstractapi.cdeno-core adding 40mb overheadWe recently migrated to deno-core from mini_v8 and realized a major increase in the size of the releGetting errors while using puppeteer in deno environmentHere is my code(supabase edge function index.ts): ```import puppeteer from "https://deno.land/x/pupNon-ascii characters not showing in subject field using Deno SMTPWhen using the smtp library non-ascii characters show up fine in the body. But if included in the heDefault Lint RulesIs there a list of the lint rules that are applied by default. When I look at the documentation it mErrors with mocking & testing in supabase edge function running on deno.Hi everyone - I am sincerely hoping you can save me from pulling out all my hair with this. At my Errors Deploying Fresh ProjectI am new to Deno and working through the Fresh tutorial. I have reached the deployment section: httRun all test in sub-folderHey guys - day one deno newby here. Incredibly inspired by what I've been able to achieve on my firsRead source code contentIs there a way to read the current source code of the a given a file URL (using `import.meta.url`) wDanet create is stuck at databaseWhat am I supposed to do to choose postgres?Working around imports without a file extensionI'm facing an apparent contradiction between the requirements for importing modules, and the import deno runtime extensions seems to be not workingthe example provided here https://github.com/denoland/deno/tree/main/runtime/examples/extension_withcant set up vscode language serveri got some error from the initialization of the language server: ```Starting Deno language server...Choose level for std loggerI have some loggers configured using the `setup` function from `std/log`. This way levels are hard-cfolks running into this wiered issue on deploythe deployment is continuously failingfolks running into this wiered issue on deploy the deployment is continuously failing was working fiProblem with the npm package and vite/esbuildhttps://github.com/denoland/denokv/issues/64 Is there a solution to this problem? Has anyone been abHow to send an enum value from Rust to DenoHello there 👋 I am quite a beginner to FFI and I am not sure which resources to read to learn more