Skip to content

Commit a6d5be6

Browse files
add build-rust and readme for rust repos
1 parent f8afa94 commit a6d5be6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

rust/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Prepare for compiling
2+
3+
The rust repos require rust toolchains(cargo, rustup, rustc, etc.). You can find the latest rust toolchains [here](https://www.rust-lang.org/tools/install).
4+
Besides the rust toolchains, we also need the [wasm-pack](https://github.com/rustwasm/wasm-pack) to compile our rust code into wasm.
5+
6+
# How to compile
7+
8+
With all gear up, you can compile the rust repos like this(take md5 as an example):
9+
```shell
10+
cd md5
11+
wasm-pack build --release
12+
```
13+
The compiled wasm binary(_bg.wasm) and javascript glue code(_bg.js) will be generated in the `pkg` directory if nothing goes wrong.
14+
15+
# Some extra work
16+
17+
In `crypto-js-wasm`, we use base64-encoded wasm binary(_bg.wasm).
18+
To get a smaller size, we use `pako` to compress the base64-encoded binary.
19+
You can check the `build_rust.js` for more details.

rust/build-rust.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// the file is supposed to be run in node.js
2+
const fs = require('fs');
3+
const pako = require('pako');
4+
5+
const contents = fs.readFileSync('./md5/pkg/md5Rust_bg.wasm', null);
6+
const compressedBytes = pako.deflate(contents);
7+
const base64Encoded = Buffer.from((compressedBytes)).toString('base64');
8+
console.log(base64Encoded);

rust/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "crypto-js-wasm-rust",
3+
"version": "1.0.0",
4+
"description": "The rust repos require rust toolchains(cargo, rustup, rustc, etc.). You can find the latest rust toolchains [here](https://www.rust-lang.org/tools/install). Besides the rust toolchains, we also need the [wasm-pack](https://github.com/rustwasm/wasm-pack) to compile our rust code into wasm.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"dependencies": {
11+
"pako": "^2.0.4"
12+
}
13+
}

0 commit comments

Comments
 (0)