Access JavaScript from Rust
Use JavaScript modules to access the file system, network, databases, and other system resources

This page is outdated. Please visit here to see how to access JavaScript from Rust.

In this tutorial, we will show you how to use the nodejs-helper crate to call Node.js functions from Rust code. Rust functions can now access the file system, network, database, and other system resources from within the WebAssembly container.
It is important to note that a better way for Rust programs to access system resources is through the WebAssembly WASI extension, as well as numerous host extensions provided by the SSVM.
The source code of the tutorial is here.
Let's see an example Rust function that gets the system time and prints to the standard output console, all from within a WebAssembly container.
1
#[wasm_bindgen]
2
pub fn utc_now() {
3
let now: String = nodejs_helper::date::utc_string();
4
nodejs_helper::console::log("UTC time: ");
5
nodejs_helper::console::log(&now);
6
}
Copied!

Prerequisite

You must have Node.js installed with the following packages.
1
$ npm i ssvm sync-request better-sqlite3
2
$ npm i -g wasm-pack
Copied!
In your Rust application, add the following dependency.
1
[dependencies]
2
wasm-bindgen = "=0.2.61"
3
nodejs-helper = "0.0.3"
Copied!

Building and running

Use the following command to build your Rust application for WebAssembly.
1
$ wasm-pack build --target nodejs
Copied!
Now, let's look some concrete examples from the example project.

Example: system time and console

The Rust functions to access the system time and console resources are as follows.
1
#[wasm_bindgen]
2
pub fn show_now() {
3
nodejs_helper::console::log("Timestamp now: ");
4
nodejs_helper::console::log(&nodejs_helper::date::timestamp());
5
}
6
7
#[wasm_bindgen]
8
pub fn utc_now() {
9
nodejs_helper::console::log("UTC time: ");
10
nodejs_helper::console::log(&nodejs_helper::date::utc_string());
11
}
12
13
#[wasm_bindgen]
14
pub fn my_time(tz: &str) {
15
nodejs_helper::console::log(tz);
16
nodejs_helper::console::log(&nodejs_helper::date::format_date("en-US", "long", "numeric", "long", "numeric", tz, "short"));
17
}
Copied!
The JavaScript code that loads the WebAssembly container and runs the above Rust functions is as follows.
1
const { show_now, utc_now, my_time } = require('../pkg/nodejs_example.js');
2
3
show_now();
4
utc_now();
5
my_time("America/Chicago");
Copied!
Running the Javascript in Node.js shows the following.
1
$ node date.js
2
Timestamp now:
3
1588013800826
4
UTC time:
5
Mon, 27 Apr 2020 18:56:40 GMT
6
America/Chicago
7
Monday, April 27, 2020, CDT
Copied!

Example: Sqlite database access

The Rust functions to create, update, and query a Sqlite database on the local file system are as follows.
1
#[derive(Serialize, Deserialize)]
2
pub struct User {
3
pub id: u32,
4
pub full_name: String,
5
pub created: String,
6
}
7
8
#[wasm_bindgen]
9
pub fn create_sqlite(path: &str) {
10
let sql_create = "
11
CREATE TABLE users (
12
id INTEGER PRIMARY KEY NOT NULL,
13
full_name TEXT NOT NULL,
14
created DATE NOT NULL
15
);";
16
let sql_insert = "
17
INSERT INTO users
18
VALUES
19
(1, 'Bob McFett', '32-01-01'),
20
(2, 'Angus Vader', '02-03-04'),
21
(3, 'Imperator Colin', '01-01-01');";
22
23
nodejs_helper::sqlite3::create(path);
24
nodejs_helper::sqlite3::update(path, sql_create);
25
nodejs_helper::sqlite3::update(path, sql_insert);
26
}
27
28
#[wasm_bindgen]
29
pub fn query_sqlite(path: &str) {
30
let sql_query = "SELECT * FROM users;";
31
let rows: String = nodejs_helper::sqlite3::query(path, sql_query);
32
let users: Vec<User> = serde_json::from_str(&rows).unwrap();
33
for user in users.into_iter() {
34
nodejs_helper::console::log(&(user.id.to_string() + " : " + &user.full_name));
35
}
36
}
Copied!
The JavaScript code that loads the WebAssembly container and runs the above Rust functions is as follows.
1
const { create_sqlite, query_sqlite } = require('../pkg/nodejs_example.js');
2
3
create_sqlite("test.sqlite");
4
query_sqlite("test.sqlite");
Copied!
Running the Javascript in Node.js shows the following.
1
$ node db.js
2
1 : Bob McFett
3
2 : Angus Vader
4
3 : Imperator Colin
Copied!

Example: HTTP network access

The Rust functions to access web services via HTTP/HTTPS and then save content on the local file system are as follows.
1
#[wasm_bindgen]
2
pub fn fetch(url: &str) {
3
let content = nodejs_helper::request::fetch_as_string(url);
4
nodejs_helper::console::log(url);
5
nodejs_helper::console::log(&content);
6
}
7
8
#[wasm_bindgen]
9
pub fn download(url: &str, path: &str) {
10
let content = nodejs_helper::request::fetch(url);
11
nodejs_helper::fs::write_file_sync(path, &content);
12
}
Copied!
The JavaScript code that loads the WebAssembly container and runs the above Rust functions is as follows.
1
const { fetch, download } = require('../pkg/nodejs_example.js');
2
3
fetch("https://raw.githubusercontent.com/second-state/nodejs-helper/master/LICENSE");
4
download("https://www.secondstate.io/", "test.html");
Copied!
Running the Javascript in Node.js shows the following.
1
$ node http.js
2
https://raw.githubusercontent.com/second-state/nodejs-helper/master/LICENSE
3
MIT License
4
5
Copyright (c) 2020 Second State
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
... ...
Copied!

Example: File system access and performance profiler

The Rust functions in this section read an image file from the local file system, resize it, and write back to the file system. It also uses the Javascript console tool to measure the time spent on each task.
1
#[derive(Serialize, Deserialize)]
2
#[derive(Copy, Clone, Debug)]
3
pub struct Dimension {
4
pub width: u32,
5
pub height: u32,
6
}
7
8
#[derive(Serialize, Deserialize)]
9
pub struct Picture {
10
pub dim: Dimension,
11
pub raw: Vec<u8>,
12
}
13
14
#[wasm_bindgen]
15
pub fn resize_file(input: &str) {
16
// Use JSON to pass multiple call arguments
17
let p: (Dimension, String, String) = serde_json::from_str(input).unwrap();
18
19
nodejs_helper::console::time("Resize file");
20
let raw = nodejs_helper::fs::read_file_sync(&p.1);
21
nodejs_helper::console::time_log("Resize file", "Done reading");
22
let src = Picture {
23
dim: p.0,
24
raw: raw,
25
};
26
let target = resize_impl(&src);
27
nodejs_helper::console::time_log("Resize file", "Done resizing");
28
29
nodejs_helper::fs::write_file_sync(&p.2, &target.raw);
30
nodejs_helper::console::time_log("Resize file", "Done writing");
31
nodejs_helper::console::time_end("Resize file");
32
}
33
34
pub fn resize_impl(src: &Picture) -> Picture {
35
// ... use the img crate to resize ...
36
}
Copied!
The JavaScript code that loads the WebAssembly container and runs the above Rust functions is as follows.
1
const { resize_file } = require('../pkg/nodejs_example.js');
2
3
const dim = {
4
width: 100,
5
height: 100
6
};
7
8
resize_file(JSON.stringify([dim, 'cat.png', `test.png`]));
Copied!
Running the Javascript in Node.js shows the following.
1
$ node image.js
2
Resize file: 5.603ms Done reading
3
Resize file: 1506.694ms Done resizing
4
Resize file: 1507.634ms Done writing
5
Resize file: 1507.977ms
Copied!
That's it for now. The nodejs-helper crate is still a work-in-progress. We aim to eventually provide Rust APIs for all common system functions here. You are welcome to fork and add to it.
Last modified 1yr ago