Skip to content

feat: try reference types #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ jobs:
- run: cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
- run: cargo check --no-default-features --features "console_error_panic_hook wee_alloc"
- run: cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc"

# install experimental wasm bindgen cli
- uses: actions/setup-node@v2
with:
node-version: "15"
- run: cargo install wasm-bindgen-cli --git https://github.com/rustwasm/wasm-bindgen
- run: yarn build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
Cargo.lock
dist
pkg
node_modules
vscode-profile*
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ wasm-opt = false

[dependencies]
arrow = { git = "https://github.com/apache/arrow" }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen = { git = "https://github.com/rustwasm/wasm-bindgen", features = ["serde-serialize"] }
paste = "1.0"
js-sys = "*"

Expand All @@ -43,6 +43,9 @@ wee_alloc = { version = "*", optional = true }
version = "0.3"
features = ["console"]

[patch.crates-io]
wasm-bindgen = { git = 'https://github.com/rustwasm/wasm-bindgen' }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

Expand Down
25 changes: 25 additions & 0 deletions examples/index_classic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<script src="../dist/arrow.js"></script>
<script>
async function run() {
await arrow("../dist/arrow_wasm_bg.wasm");

const url =
"https://gist.githubusercontent.com/domoritz/0f53a5abde95564c36dfaac623a7a922/raw/cce3719b853e25d5dfff97a270283ba83af3c0e6/flights-10k.arrow";

const response = await fetch(url);
const data = await response.arrayBuffer();
const contents = new Uint8Array(data);

const table = arrow.Table.from(contents);
console.log(table.schema.toJSON());
}

run();
</script>
</body>
</html>
File renamed without changes.
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import init from "./Cargo.toml";
import init, * as arrow from "./pkg/arrow_wasm";

export default init;
export default Object.assign(init, arrow);
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
],
"main": "dist/arrow.js",
"scripts": {
"prebuild": "yarn clean",
"prebuild": "yarn clean && yarn build:cargo && yarn build:wasm-bindgen",
"build": "rollup --config",
"build:cargo": "cargo build --release --target wasm32-unknown-unknown",
"build:wasm-bindgen": "wasm-bindgen target/wasm32-unknown-unknown/release/arrow_wasm.wasm --target web --reference-types --out-dir pkg",
"build:wasm-opt": "wasm-opt pkg/arrow_wasm_bg.wasm -O -g --output pkg/arrow_wasm_bg.opt.wasm && mv pkg/arrow_wasm_bg.opt.wasm pkg/arrow_wasm_bg.wasm || yarn build:wasm-opt-note",
"build:wasm-opt-note": "echo 'NOTE: Since wasm-opt could not be found (or it failed), the resulting module might not perform that well, but it should still work.'",
"watch": "rollup --config --watch",
"build:web": "wasm-pack build --target web",
"build:node": "wasm-pack build --target nodejs",
Expand All @@ -27,11 +31,11 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^8.1.1",
"@wasm-tool/rollup-plugin-rust": "^1.0.5",
"apache-arrow": "^3.0.0",
"light-server": "^2.9.1",
"rimraf": "^3.0.2",
"rollup": "^2.38.5",
"rollup-plugin-copy": "^3.3.0",
"typescript": "^4.1.3"
},
"sideEffects": false
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typescript from "@rollup/plugin-typescript";
import rust from "@wasm-tool/rollup-plugin-rust";
import copy from "rollup-plugin-copy";

export default {
input: "index.ts",
Expand All @@ -10,8 +10,8 @@ export default {
name: "arrow",
},
plugins: [
rust({
inlineWasm: true,
copy({
targets: [{ src: "pkg/arrow_wasm_bg.wasm", dest: "dist/" }],
}),
typescript(),
],
Expand Down
21 changes: 21 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Cursor;

use arrow::ipc;
use arrow::{datatypes, error::ArrowError};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -30,6 +32,25 @@ impl Table {
self.record_batches.len()
}

#[wasm_bindgen(js_name = fromUint8Array)]
pub fn from_uint8_array(contents: js_sys::Uint8Array) -> Result<Table, JsValue> {
let vec = contents.to_vec();
let cursor: Cursor<&[u8]> = std::io::Cursor::new(vec.as_slice());
let reader = match arrow::ipc::reader::FileReader::try_new(cursor) {
Ok(reader) => reader,
Err(error) => return Err(format!("{}", error).into()),
};

let schema = reader.schema();
match reader.collect() {
Ok(record_batches) => Ok(Table {
schema,
record_batches,
}),
Err(error) => Err(format!("{}", error).into()),
}
}

pub fn from(contents: &[u8]) -> Result<Table, JsValue> {
let cursor = std::io::Cursor::new(contents);
let reader = match arrow::ipc::reader::FileReader::try_new(cursor) {
Expand Down
Loading