Skip to content

Commit

Permalink
Add support for async handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Jul 21, 2021
1 parent c875e85 commit 04d7792
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
.idea
dist
node_modules
target
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"proseWrap": "always"
}
46 changes: 26 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ js-sys = "0.3.33"
lol_html = "0.3.0"
serde = { version = "1.0.104", features = ["derive"] }
serde-wasm-bindgen = "0.1.3"
wasm-bindgen = "0.2"
wasm-bindgen = "0.2.74"
thiserror = "1.0.2"

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wasm-pack.profile.dev]
wasm-opt = ["--asyncify"]
[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Os", "--asyncify"]
26 changes: 26 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -e

echo "---> Checking wasm-pack version..."
# We need to make sure the version of wasm-pack uses Binaryen version_92,
# which exports asyncify_get_state
WASM_PACK_VERSION=$(wasm-pack --version)
if [[ ! $WASM_PACK_VERSION =~ -asyncify$ ]]; then
echo "$WASM_PACK_VERSION installed, please install mrbbot's fork:"
echo "cargo install --git https://github.com/mrbbot/wasm-pack"
exit 1
fi

echo "---> Building WebAssembly with wasm-pack..."
wasm-pack build --target nodejs

echo "---> Patching JavaScript glue code..."
# Wraps write/end with asyncify magic and adds this returns for chaining
patch -uN pkg/html_rewriter.js < html_rewriter.js.patch

echo "---> Copying required files to dist..."
mkdir -p dist
cp pkg/html_rewriter.js dist/html_rewriter.js
cp pkg/html_rewriter_bg.wasm dist/html_rewriter_bg.wasm
cp src/asyncify.js dist/asyncify.js
cp src/html_rewriter.d.ts dist/html_rewriter.d.ts
194 changes: 194 additions & 0 deletions html_rewriter.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
--- pkg/html_rewriter.js 2021-07-21 19:45:46.000000000 +0100
+++ pkg2/html_rewriter.js 2021-07-21 19:44:41.000000000 +0100
@@ -1,7 +1,7 @@
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
let wasm;
-const { awaitPromise } = require(String.raw`./asyncify.js`);
+const { awaitPromise, setWasmExports, wrap } = require(String.raw`./asyncify.js`);
const { TextDecoder, TextEncoder } = require(String.raw`util`);

const heap = new Array(32).fill(undefined);
@@ -233,6 +233,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -242,6 +243,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -251,11 +253,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
*/
remove() {
wasm.comment_remove(this.ptr);
+ return this;
}
/**
* @returns {boolean}
@@ -364,6 +368,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.documentend_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
}
module.exports.DocumentEnd = DocumentEnd;
@@ -397,6 +402,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -406,6 +412,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -415,11 +422,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
*/
remove() {
wasm.element_remove(this.ptr);
+ return this;
}
/**
* @returns {boolean}
@@ -495,6 +504,7 @@
var ptr1 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
wasm.element_setAttribute(this.ptr, ptr0, len0, ptr1, len1);
+ return this;
}
/**
* @param {string} name
@@ -503,6 +513,7 @@
var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_removeAttribute(this.ptr, ptr0, len0);
+ return this;
}
/**
* @param {string} content
@@ -512,6 +523,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_prepend(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -521,6 +533,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -530,11 +543,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_setInnerContent(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
*/
removeAndKeepContent() {
wasm.element_removeAndKeepContent(this.ptr);
+ return this;
}
}
module.exports.Element = Element;
@@ -579,25 +594,27 @@
var ptr0 = passStringToWasm0(selector, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.htmlrewriter_on(this.ptr, ptr0, len0, addHeapObject(handlers));
+ return this;
}
/**
* @param {any} handlers
*/
onDocument(handlers) {
wasm.htmlrewriter_onDocument(this.ptr, addHeapObject(handlers));
+ return this;
}
/**
* @param {Uint8Array} chunk
*/
- write(chunk) {
+ async write(chunk) {
var ptr0 = passArray8ToWasm0(chunk, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
- wasm.htmlrewriter_write(this.ptr, ptr0, len0);
+ await wrap(this, wasm.htmlrewriter_write, this.ptr, ptr0, len0);
}
/**
*/
- end() {
- wasm.htmlrewriter_end(this.ptr);
+ async end() {
+ await wrap(this, wasm.htmlrewriter_end, this.ptr);
}
/**
* @returns {number}
@@ -638,6 +655,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -647,6 +665,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -656,11 +675,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
*/
remove() {
wasm.textchunk_remove(this.ptr);
+ return this;
}
/**
* @returns {boolean}
@@ -847,5 +868,6 @@
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
+setWasmExports(wasm);
module.exports.__wasm = wasm;

Loading

0 comments on commit 04d7792

Please sign in to comment.