-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
use wasm_bindgen::prelude::*; | ||
use fflate::{inflate, InflateError}; | ||
use fflate; | ||
|
||
#[wasm_bindgen] | ||
pub fn inflate_raw(buf: &[u8]) -> Result<Vec<u8>, JsValue> { | ||
let mut out = Vec::new(); | ||
if let Err(e) = inflate(buf, &mut out) { | ||
return Err(JsValue::from(match e { | ||
InflateError::InvalidBlockType => "invalid block type", | ||
InflateError::InvalidDistance => "invalid distance", | ||
InflateError::InvalidLengthOrLiteral => "invalid length/literal", | ||
InflateError::UnexpectedEOF => "unexpected EOF" | ||
})); | ||
pub struct Inflate { | ||
buf: &'static Vec<u8>, | ||
inflator: fflate::Inflate<'static> | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Inflate { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new() -> Inflate { | ||
unsafe { | ||
static mut buf: Vec<u8> = Vec::new(); | ||
Inflate { | ||
buf: &buf, | ||
inflator: fflate::Inflate::new(&mut buf) | ||
} | ||
} | ||
} | ||
pub fn push(&mut self, dat: &[u8], last: bool) -> Result<> { | ||
self.inflator.write_all(dat); | ||
} | ||
Ok(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
// Mediocre shim | ||
import { Worker } from 'worker_threads'; | ||
|
||
let Worker: typeof import('worker_threads').Worker; | ||
const workerAdd = ";var __w=require('worker_threads');__w.parentPort.on('message',function(m){onmessage({data:m})}),postMessage=function(m,t){__w.parentPort.postMessage(m,t)},close=process.exit;self=global"; | ||
|
||
export default <T>(c: string, _: number, msg: unknown, transfer: ArrayBuffer[], cb: (err: Error, msg: T) => void) => { | ||
try { | ||
Worker = require('worker_threads').Worker; | ||
} catch(e) { | ||
} | ||
export default Worker ? <T>(c: string, _: number, msg: unknown, transfer: ArrayBuffer[], cb: (err: Error, msg: T) => void) => { | ||
let done = false; | ||
const w = new Worker(c + workerAdd, { eval: true }) | ||
.on('error', e => cb(e, null)) | ||
.on('message', m => cb(null, m)) | ||
.on('exit', c => { | ||
if (c && !done) cb(new Error('Exited with code ' + c), null); | ||
if (c && !done) cb(new Error('exited with code ' + c), null); | ||
}); | ||
w.postMessage(msg, transfer); | ||
w.terminate = () => { | ||
done = true; | ||
return Worker.prototype.terminate.call(w); | ||
} | ||
return w; | ||
} : (_: string, __: number, ___: unknown, ____: ArrayBuffer[], cb: (err: Error, msg: null) => void) => { | ||
setImmediate(() => cb(new Error('async operations unsupported - update to Node 12+ (or Node 10-11 with the --experimental-worker CLI flag)'), null)); | ||
const NOP = () => {}; | ||
return { | ||
terminate: NOP, | ||
postMessage: NOP | ||
} as unknown as import('worker_threads').Worker; | ||
} |