javascriptrustdeno

How to reuse full-featured Deno in my Rust project?


I'm trying to implement a JS plugin system for my project that uses Deno underground (for its comprehensive permission model). I expect it to have:

I tried deno_core, but it only has ECMAScript built-ins. (reproduction) And deno is executable.


Solution

  • You're looking for the deno_runtime crate. It includes everything you're looking for. It notably doesn't include typescript support if you're looking for that.

    You can find an example of bootstrapping the runtime from Rust and two-way communication here, but TLDR; you need to bootstrap a main worker, pass in some options, and then run your JavaScript.

    let mut worker = MainWorker::bootstrap_from_options(
      &main_module,
      WorkerServiceOptions::<
        DenoInNpmPackageChecker,
        NpmResolver<sys_traits::impls::RealSys>,
        sys_traits::impls::RealSys,
      > {
        module_loader: Rc::new(FsModuleLoader),
        permissions: PermissionsContainer::allow_all(permission_desc_parser),
        blob_store: Default::default(),
        broadcast_channel: Default::default(),
        feature_checker: Default::default(),
        node_services: Default::default(),
        npm_process_state_provider: Default::default(),
        root_cert_store_provider: Default::default(),
        fetch_dns_resolver: Default::default(),
        shared_array_buffer_store: Default::default(),
        compiled_wasm_module_store: Default::default(),
        v8_code_cache: Default::default(),
        fs,
      },
      WorkerOptions {
        extensions: vec![hello_runtime::init_ops_and_esm()],
        ..Default::default()
      },
    );
    worker.execute_main_module(&main_module).await?;
    worker.run_event_loop(false).await?;