mirror of
https://github.com/cberner/raptorq.git
synced 2024-06-26 00:38:53 +00:00
Added wasm build configuration (#136)
Co-authored-by: Christopher Berner <christopherberner@gmail.com>
This commit is contained in:
parent
f39a192bc7
commit
02c80b595a
1
.gitignore
vendored
1
.gitignore
vendored
@ -11,3 +11,4 @@ Cargo.lock
|
||||
**/__pycache__
|
||||
|
||||
.idea/
|
||||
/pkg
|
||||
|
@ -16,6 +16,8 @@ crate-type = ["cdylib", "rlib"]
|
||||
[dependencies]
|
||||
serde = {version = "1.0.102", features=["std", "derive"], optional = true}
|
||||
pyo3 = {version = "0.16", features=["extension-module", "abi3-py37"], optional = true }
|
||||
wasm-bindgen = {version = "0.2", optional = true}
|
||||
js-sys = {version = "0.3.60", optional = true}
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
@ -47,6 +49,7 @@ lto = false
|
||||
benchmarking = []
|
||||
python = ["pyo3"]
|
||||
serde_support = ["serde"]
|
||||
wasm = ["wasm-bindgen", "js-sys"]
|
||||
|
||||
[package.metadata.maturin]
|
||||
requires-python = ">= 3.7"
|
||||
|
3
Makefile
3
Makefile
@ -37,3 +37,6 @@ install_py: pre
|
||||
|
||||
test_py: install_py
|
||||
python3 -m unittest discover
|
||||
|
||||
build_wasm: pre
|
||||
wasm-pack build --target web --features wasm
|
||||
|
37
examples/index.html
Normal file
37
examples/index.html
Normal file
@ -0,0 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>hello-wasm example</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import init, { Decoder, Encoder } from "../pkg/raptorq.js";
|
||||
init().then(() => {
|
||||
let test = Array.from({length: 10000}, () => Math.floor(Math.random() * 100));
|
||||
|
||||
let data = Uint8Array.from(test);
|
||||
let encoder = Encoder.with_defaults(data, 512);
|
||||
let packets = encoder.encode(42);
|
||||
console.log(`data ${data} encoded packets are ${packets}`);
|
||||
let shuffledPackets = packets.sort((a, b) => 0.5 - Math.random());
|
||||
console.log(`data ${data} encoded packets are ${packets}`);
|
||||
|
||||
|
||||
let decoder = Decoder.with_defaults(BigInt(data.length), 512);
|
||||
|
||||
let decodedData;
|
||||
for (let i = 0; i < shuffledPackets.length; i++) {
|
||||
console.log(i);
|
||||
decodedData = decoder.decode(packets[i]);
|
||||
if (decodedData) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
console.log(data);
|
||||
console.log(decodedData);
|
||||
console.log(JSON.stringify(data) === JSON.stringify(decodedData));
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -22,16 +22,20 @@ mod sparse_vec;
|
||||
mod symbol;
|
||||
mod systematic_constants;
|
||||
mod util;
|
||||
#[cfg(feature = "wasm")]
|
||||
mod wasm;
|
||||
|
||||
pub use crate::base::partition;
|
||||
pub use crate::base::EncodingPacket;
|
||||
pub use crate::base::ObjectTransmissionInformation;
|
||||
pub use crate::base::PayloadId;
|
||||
#[cfg(not(feature = "python"))]
|
||||
#[cfg(not(feature = "wasm"))]
|
||||
pub use crate::decoder::Decoder;
|
||||
pub use crate::decoder::SourceBlockDecoder;
|
||||
pub use crate::encoder::calculate_block_offsets;
|
||||
#[cfg(not(feature = "python"))]
|
||||
#[cfg(not(feature = "wasm"))]
|
||||
pub use crate::encoder::Encoder;
|
||||
pub use crate::encoder::EncoderBuilder;
|
||||
pub use crate::encoder::SourceBlockEncoder;
|
||||
@ -43,6 +47,10 @@ pub use crate::python::Decoder;
|
||||
#[cfg(feature = "python")]
|
||||
pub use crate::python::Encoder;
|
||||
pub use crate::systematic_constants::extended_source_block_symbols;
|
||||
#[cfg(feature = "wasm")]
|
||||
pub use crate::wasm::Decoder as WasmDecoder;
|
||||
#[cfg(feature = "wasm")]
|
||||
pub use crate::wasm::Encoder as WasmEncoder;
|
||||
|
||||
#[cfg(feature = "benchmarking")]
|
||||
pub use crate::constraint_matrix::generate_constraint_matrix;
|
||||
|
53
src/wasm.rs
Normal file
53
src/wasm.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use crate::base::{EncodingPacket, ObjectTransmissionInformation};
|
||||
use crate::decoder::Decoder as DecoderNative;
|
||||
use crate::encoder::Encoder as EncoderNative;
|
||||
use js_sys::Uint8Array;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Decoder {
|
||||
decoder: DecoderNative,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Decoder {
|
||||
#[wasm_bindgen]
|
||||
pub fn with_defaults(transfer_length: u64, maximum_transmission_unit: u16) -> Decoder {
|
||||
let config = ObjectTransmissionInformation::with_defaults(
|
||||
transfer_length,
|
||||
maximum_transmission_unit,
|
||||
);
|
||||
Decoder {
|
||||
decoder: DecoderNative::new(config),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn decode(&mut self, packet: &[u8]) -> Option<Vec<u8>> {
|
||||
self.decoder.decode(EncodingPacket::deserialize(packet))
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Encoder {
|
||||
encoder: EncoderNative,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Encoder {
|
||||
#[wasm_bindgen]
|
||||
pub fn with_defaults(data: &[u8], maximum_transmission_unit: u16) -> Encoder {
|
||||
Encoder {
|
||||
encoder: EncoderNative::with_defaults(data, maximum_transmission_unit),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn encode(&mut self, repair_packets_per_block: u32) -> Vec<Uint8Array> {
|
||||
self.encoder
|
||||
.get_encoded_packets(repair_packets_per_block)
|
||||
.iter()
|
||||
.map(|packet| Uint8Array::from(packet.serialize().as_slice()))
|
||||
.collect()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user