惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 叶小钗
AWS News Blog
AWS News Blog
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
V
V2EX
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Latest news
Latest news
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
T
Threatpost
L
Lohrmann on Cybersecurity
小众软件
小众软件
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
Security Latest
Security Latest
Recent Announcements
Recent Announcements
C
Check Point Blog
B
Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
I
InfoQ

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno Zero-config Debugging with Deno and OpenTelemetry | Deno Exploring Art with TypeScript, Jupyter, Polars, and Observable Plot | Deno Deno v Oracle Update 3: Fighting the JavaScript Trademark | Deno Build a custom RAG AI agent in TypeScript and Jupyter | Deno How to get deep traces in your Node.js backend with OTel and Deno | Deno toranoana.deno #20 登録受付中(2025年3月14日) | Deno Node just added TypeScript support. What does that mean for Deno? | Deno The Dino 🦕, the Llama 🦙, and the Whale 🐋 | Deno Publish a lint rule, get a prize | Deno Deno 2.2: OpenTelemetry, Lint Plugins, node:sqlite | Deno If you're not using npm specifiers, you're doing it wrong | Deno How Deno's documentation is evolving | Deno Oracle justified its JavaScript trademark with Node.js—now it wants that ignored | Deno Introducing the JSR open governance board | Deno Intro to Wasm in Deno | Deno Announcing OpenAI on JSR | Deno Deno in 2024 | Deno Goodbye WinterCG, welcome WinterTC | Deno Build a SolidJS app with Deno | Deno Run your Next.js SSR app on Deno Deploy | Deno Solve Advent of Code 2024 with Deno and Win Prizes! | Deno Deno v. Oracle: Canceling the JavaScript Trademark | Deno Deno 2.1: Wasm Imports and other enhancements | Deno Build a Typesafe API with tRPC and Deno | Deno Self-contained Executable Programs with Deno Compile | Deno Build a Database App with Drizzle ORM and Deno | Deno Introducing your new JavaScript package manager: Deno | Deno Announcing Growthbook on JSR | Deno Build an Astro site with Deno | Deno How to convert CommonJS to ESM | Deno Announcing Deno 2 | Deno The Final Touches: What’s New In v2.0.0-rc.10 | Deno Announcing Stable V8 Bindings for Rust | Deno Deno 2.0 Release Candidate | Deno Secure, efficient private npm registries with Cloudsmith and Deno | Deno Painting the Plane as We Fly It: Designing JSR | Deno Introducing Web Cache API support on Deno Deploy | Deno Deno 1.46: The Last 1.x Release | Deno Protect your cloud spend with new Deno Deploy spend limits | Deno What we got wrong about HTTP imports | Deno Benchmarking AWS Lambda Cold Starts Across JavaScript Runtimes | Deno Announcing Supabase on JSR | Deno Deno 1.45: Workspace and Monorepo Support | Deno Introducing KV Backup for Deno Subhosting | Deno A Gentle Intro to TypeScript | Deno Announcing Hono on JSR | Deno How We Made the Deno Language Server Ten Times Faster | Deno How the Guardian uses Deno to audit accessibility and performance across their 2.7 million articles | Deno Introducing More Flexible Domain Association for Deno Subhosting | Deno The stabilization process of the Standard Library has begun | Deno Deno 1.44: Private npm registries, improved Node.js compat, and performance boosts | Deno How we built a secure, performant, multi-tenant cloud platform to run untrusted code | Deno The Deno Standard Library is now available on JSR | Deno How to document your JavaScript package | Deno Your Low Code Solution Needs an Escape Hatch | Deno Deno 1.43: Improved Language Server performance | Deno How Slack used Deno to save months of engineering effort in launching their new platform | Deno JSR Is Not Another Package Manager | Deno Announcing the Hookdeck SDK on JSR | Deno Announcing the Neon Serverless Driver on JSR | Deno An intro to TSConfig for JavaScript Developers | Deno How we built JSR | Deno How Netlify used Deno Subhosting to build a successful edge functions product | Deno Introducing Simpler Project Creation in Deno Deploy | Deno Deno 1.42: Better dependency management with JSR | Deno Introducing deployctl, the command line interface for Deno Deploy | Deno Introducing JSR - the JavaScript Registry | Deno How to add Monaco to a Next.js app and securely run untrusted user code | Deno Survey Results and Roadmap | Deno Deno 1.41: smaller deno compile binaries | Deno
Roll your own JavaScript runtime | Deno
Bartek Iwańc · 2022-07-29 · via Deno

In this post we’ll walk through creating a custom JavaScript runtime. Let’s call it runjs. Think of it as building a (much) simplified version of deno itself. A goal of this post is to create a CLI that can execute local JavaScript files, read a file, write a file, remove a file and has simplified console API.

Let’s get started.

Update 2022-12-04: updated the code samples to the latest version of deno_core

Update 2023-02-16: we posted a second part of this tutorial, where we implement fetch-like API and add TypeScript transpilation.

Update 2023-05-04: we posted a third part of this tutorial, where we create snapshots to speed up startup time.

Update 2024-09-26: updated the code samples to the latest version of deno_core

Pre-requisites

This tutorial assumes that the reader has:

  • a basic knowledge of Rust
  • a basic knowledge of JavaScript event loops

Make sure you have Rust installed on your machine (along with cargo) and it should be at least 1.80.0. Visit rust-lang.org to install Rust compiler and cargo.

Make sure we’re ready to go:

$ cargo --version
cargo 1.80.1 (3f5fd8dd4 2024-08-06)

Hello, Rust!

First off, let’s create a new Rust project, which will be a binary crate called runjs:

$ cargo init --bin runjs
     Created binary (application) package

Change your working directory to runjs and open it in your editor. Make sure that everything is set up properly:

$ cd runjs
$ cargo run
   Compiling runjs v0.1.0 (/Users/ib/dev/runjs)
    Finished dev [unoptimized + debuginfo] target(s) in 1.76s
     Running `target/debug/runjs`
Hello, world!

Great! Now let’s begin creating our own JavaScript runtime.

Dependencies

Next, let’s add the deno_core and tokio dependencies to our project:

$ cargo add deno_core
    Updating crates.io index
      Adding deno_core v0.311.0 to dependencies.
$ cargo add tokio --features=full
    Updating crates.io index
      Adding tokio v1.40.0 to dependencies.

Our updated Cargo.toml file should look like this:

[package]
name = "runjs"
version = "0.1.0"
edition = "2021"



[dependencies]
deno_core = "0.311"
tokio = { version = "1.40", features = ["full"] }

deno_core is a crate by the Deno team that abstracts away interactions with the V8 JavaScript engine. V8 is a complex project with thousands of APIs, so to make it simpler to use them, deno_core provides a JsRuntime struct that encapsulates a V8 engine instance (called an Isolate) and allows integration with an event loop.

tokio is an asynchronous Rust runtime that we will use as an event loop. Tokio is responsible for interacting with OS abstractions like net sockets or file system. deno_core together with tokio allow JavaScript’s Promises to be easily mapped onto Rust’s Futures.

Having both a JavaScript engine and an event loop allows us to create a JavaScript runtime.

Hello, runjs!

Let’s start by writing an asynchronous Rust function that will create an instance of JsRuntime, which is responsible for JavaScript execution.


use deno_core::error::AnyError;
use std::rc::Rc;

async fn run_js(file_path: &str) -> Result<(), AnyError> {
  let main_module =
    deno_core::resolve_path(file_path, &std::env::current_dir()?)?;
  let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
    module_loader: Some(Rc::new(deno_core::FsModuleLoader)),
    ..Default::default()
  });

  let mod_id = js_runtime.load_main_es_module(&main_module).await?;
  let result = js_runtime.mod_evaluate(mod_id);
  js_runtime.run_event_loop(Default::default()).await?;
  result.await
}

fn main() {
  println!("Hello, world!");
}

There’s a lot to unpack here. The asynchronous run_js function creates a new instance of JsRuntime, which uses a file-system based module loader. After that, we load a module into js_runtime runtime, evaluate it, and run an event loop to completion.

This run_js function encapsulates the whole life-cycle that our JavaScript code will go through. But before we can do that, we need to create a single-threaded tokio runtime to be able to execute our run_js function:


fn main() {
  let runtime = tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()
    .unwrap();
  if let Err(error) = runtime.block_on(run_js("./example.js")) {
    eprintln!("error: {}", error);
  }
}

Let’s try to execute some JavaScript code! Create an example.js file that will print “Hello runjs!”:


Deno.core.print("Hello runjs!");

Notice that we are using the print function from Deno.core - this is a globally available built-in object that is provided by the deno_core Rust crate.

Now run it:

cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/runjs`
Hello runjs!

Success! In just 33 lines of Rust code we created a simple JavaScript runtime, that can execute local files. Of course this runtime can’t do much at this point (for example, console.log doesn’t work yet - try it!), but we have integrated a V8 JavaScript engine and tokio into our Rust project.

Adding the console API

Let’s work on the console API. First, create the src/runtime.js file that will instantiate and make the console object globally available:


const { core } = Deno;

function argsToMessage(...args) {
  return args.map((arg) => JSON.stringify(arg)).join(" ");
}

globalThis.console = {
  log: (...args) => {
    core.print(`[out]: ${argsToMessage(...args)}\n`, false);
  },
  error: (...args) => {
    core.print(`[err]: ${argsToMessage(...args)}\n`, true);
  },
};

The functions console.log and console.error will accept multiple parameters, stringify them as JSON (so we can inspect non-primitive JS objects) and prefix each message with log or error. This is a “plain old” JavaScript file, like we were writing JavaScript in browsers before ES modules.

Now let’s include this code in our binary and execute on every run:

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
  module_loader: Some(Rc::new(deno_core::FsModuleLoader)),
  ..Default::default()
});
+ let internal_mod_id = js_runtime
+   .load_side_es_module_from_code(
+     &deno_core::ModuleSpecifier::parse("runjs:runtime.js")?,
+     include_str!("./runtime.js"),
+   )
+   .await?;
+ let internal_mod_result = js_runtime.mod_evaluate(internal_mod_id);
let mod_id = js_runtime.load_main_es_module(&main_module).await?;
let result = js_runtime.mod_evaluate(mod_id);
js_runtime.run_event_loop(Default::default()).await?;
+ internal_mod_result.await?;
result.await

Finally, let’s update example.js with our new console API:

- Deno.core.print("Hello runjs!");
+ console.log("Hello", "runjs!");
+ console.error("Boom!");

And run it again:

cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/runjs`
[out]: "Hello" "runjs!"
[err]: "Boom!"

It works! Now let’s add an API that will allow us to interact with the file system.

Adding a basic filesystem API

Let’s start by updating our runtime.js file:

};

+ globalThis.runjs = {
+   readFile: (path) => {
+     return core.ops.op_read_file(path);
+   },
+   writeFile: (path, contents) => {
+     return core.ops.op_write_file(path, contents);
+   },
+   removeFile: (path) => {
+     return core.ops.op_remove_file(path);
+   },
+ };

We just added a new global object, called runjs, which has three methods on it: readFile, writeFile and removeFile. The first two methods are asynchronous, while the third is synchronous.

You might be wondering what these core.ops.[op name] calls are - they’re mechanisms in deno_core crate for binding JavaScript and Rust functions. When you call either of these, deno_core will look for a Rust function that has an #[op2] attribute and a matching name.

Let’s see this in action by updating main.rs:

+ use deno_core::extension;
+ use deno_core::op2;
use deno_core::PollEventLoopOptions;
use std::rc::Rc;

+ #[op2(async)]
+ #[string]
+ async fn op_read_file(#[string] path: String) -> Result<String, AnyError> {
+   let contents = tokio::fs::read_to_string(path).await?;
+   Ok(contents)
+ }
+ 
+ #[op2(async)]
+ async fn op_write_file(#[string] path: String, #[string] contents: String) -> Result<(), AnyError> {
+   tokio::fs::write(path, contents).await?;
+   Ok(())
+ }
+ 
+ #[op2(fast)]
+ fn op_remove_file(#[string] path: String) -> Result<(), AnyError> {
+   std::fs::remove_file(path)?;
+   Ok(())
+ }

We just added three ops that could be called from JavaScript. But before these ops will be available to our JavaScript code, we need to tell deno_core about them by registering an “extension”:

+ extension!(
+   runjs,
+   ops = [
+     op_read_file,
+     op_write_file,
+     op_remove_file,
+   ]
+ );

async fn run_js(file_path: &str) -> Result<(), AnyError> {
  let main_module = deno_core::resolve_path(file_path)?;
  let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
    module_loader: Some(Rc::new(deno_core::FsModuleLoader)),
+    extensions: vec![runjs::init_ops()],
    ..Default::default()
  });

Extensions allow you to configure your instance of JsRuntime and expose different Rust functions to JavaScript, as well as perform more advanced things like loading additional JavaScript code.

Due to that, we can now also clean up our code a bit by changing our declaration of out runtime.js:

extension!(
  runjs,
  ops = [
    op_read_file,
    op_write_file,
    op_remove_file,
  ],
+  esm_entry_point = "ext:runjs/runtime.js",
+  esm = [dir "src", "runtime.js"],
);

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
  module_loader: Some(Rc::new(deno_core::FsModuleLoader)),
-  extensions: vec![runjs::init_ops()],
+  extensions: vec![runjs::init_ops_and_esm()],
  ..Default::default()
});
- let internal_mod_id = js_runtime
-   .load_side_es_module_from_code(
-     &deno_core::ModuleSpecifier::parse("runjs:runtime.js")?,
-     include_str!("./runtime.js"),
-   )
-   .await?;
- let internal_mod_result = js_runtime.mod_evaluate(internal_mod_id);
let mod_id = js_runtime.load_main_es_module(&main_module).await?;
let result = js_runtime.mod_evaluate(mod_id);
js_runtime.run_event_loop(Default::default()).await?;
- internal_mod_result.await?;
result.await

The esm directive in the macro defines the JavaScript files we want to include into the extension. the dir "src" part specifies that our JavaScript files for this extension are located in the src directory. Then we add runtime.js to it as a file we want to include.

The esm_entry_point directive declares the file we want to use as an entrypoint. Let’s break down the string we have specified for it:

  • "ext:": a special schema used by deno_core to refer to extensions
  • runjs: the name of the extension we are trying to access the files of
  • runtime.js: the JavaScript file we want to use as the entrypoint

Now, let’s update our example.js again:

console.log("Hello", "runjs!");
console.error("Boom!");
+
+ const path = "./log.txt";
+ try {
+   const contents = await runjs.readFile(path);
+   console.log("Read from a file", contents);
+ } catch (err) {
+   console.error("Unable to read file", path, err);
+ }
+
+ await runjs.writeFile(path, "I can write to a file.");
+ const contents = await runjs.readFile(path);
+ console.log("Read from a file", path, "contents:", contents);
+ console.log("Removing file", path);
+ runjs.removeFile(path);
+ console.log("File removed");
+

And run it:

$ cargo run
   Compiling runjs v0.1.0 (/Users/ib/dev/runjs)
    Finished dev [unoptimized + debuginfo] target(s) in 0.97s
     Running `target/debug/runjs`
[out]: "Hello" "runjs!"
[err]: "Boom!"
[err]: "Unable to read file" "./log.txt" {"code":"ENOENT"}
[out]: "Read from a file" "./log.txt" "contents:" "I can write to a file."
[out]: "Removing file" "./log.txt"
[out]: "File removed"

Congratulations, our runjs runtime now works with the file system! Notice how little code was required to call from JavaScript to Rust - deno_core takes care of marshalling data between JavaScript and Rust so we didn’t need to do any of the conversions ourselves.

Summary

In this short example, we have started a Rust project that integrates a powerful JavaScript engine (V8) with an efficient implementation of an event loop (tokio).

A full working example can be found on denoland’s GitHub.

Update 2023-02-16: we posted a second part of this tutorial, where we implement fetch-like API and add TypeScript transpilation.