Skip to content

Commit 04d7792

Browse files
committed
Add support for async handlers
1 parent c875e85 commit 04d7792

12 files changed

+540
-42
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.DS_Store
22
.idea
3+
dist
4+
node_modules
35
target

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"proseWrap": "always"
3+
}

Cargo.lock

+26-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ js-sys = "0.3.33"
99
lol_html = "0.3.0"
1010
serde = { version = "1.0.104", features = ["derive"] }
1111
serde-wasm-bindgen = "0.1.3"
12-
wasm-bindgen = "0.2"
12+
wasm-bindgen = "0.2.74"
1313
thiserror = "1.0.2"
1414

1515
[lib]
1616
crate-type = ["cdylib", "rlib"]
17+
18+
[package.metadata.wasm-pack.profile.dev]
19+
wasm-opt = ["--asyncify"]
20+
[package.metadata.wasm-pack.profile.release]
21+
wasm-opt = ["-Os", "--asyncify"]

build.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
echo "---> Checking wasm-pack version..."
5+
# We need to make sure the version of wasm-pack uses Binaryen version_92,
6+
# which exports asyncify_get_state
7+
WASM_PACK_VERSION=$(wasm-pack --version)
8+
if [[ ! $WASM_PACK_VERSION =~ -asyncify$ ]]; then
9+
echo "$WASM_PACK_VERSION installed, please install mrbbot's fork:"
10+
echo "cargo install --git https://github.com/mrbbot/wasm-pack"
11+
exit 1
12+
fi
13+
14+
echo "---> Building WebAssembly with wasm-pack..."
15+
wasm-pack build --target nodejs
16+
17+
echo "---> Patching JavaScript glue code..."
18+
# Wraps write/end with asyncify magic and adds this returns for chaining
19+
patch -uN pkg/html_rewriter.js < html_rewriter.js.patch
20+
21+
echo "---> Copying required files to dist..."
22+
mkdir -p dist
23+
cp pkg/html_rewriter.js dist/html_rewriter.js
24+
cp pkg/html_rewriter_bg.wasm dist/html_rewriter_bg.wasm
25+
cp src/asyncify.js dist/asyncify.js
26+
cp src/html_rewriter.d.ts dist/html_rewriter.d.ts

html_rewriter.js.patch

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
--- pkg/html_rewriter.js 2021-07-21 19:45:46.000000000 +0100
2+
+++ pkg2/html_rewriter.js 2021-07-21 19:44:41.000000000 +0100
3+
@@ -1,7 +1,7 @@
4+
let imports = {};
5+
imports['__wbindgen_placeholder__'] = module.exports;
6+
let wasm;
7+
-const { awaitPromise } = require(String.raw`./asyncify.js`);
8+
+const { awaitPromise, setWasmExports, wrap } = require(String.raw`./asyncify.js`);
9+
const { TextDecoder, TextEncoder } = require(String.raw`util`);
10+
11+
const heap = new Array(32).fill(undefined);
12+
@@ -233,6 +233,7 @@
13+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
14+
var len0 = WASM_VECTOR_LEN;
15+
wasm.comment_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
16+
+ return this;
17+
}
18+
/**
19+
* @param {string} content
20+
@@ -242,6 +243,7 @@
21+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
22+
var len0 = WASM_VECTOR_LEN;
23+
wasm.comment_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
24+
+ return this;
25+
}
26+
/**
27+
* @param {string} content
28+
@@ -251,11 +253,13 @@
29+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
30+
var len0 = WASM_VECTOR_LEN;
31+
wasm.comment_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
32+
+ return this;
33+
}
34+
/**
35+
*/
36+
remove() {
37+
wasm.comment_remove(this.ptr);
38+
+ return this;
39+
}
40+
/**
41+
* @returns {boolean}
42+
@@ -364,6 +368,7 @@
43+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
44+
var len0 = WASM_VECTOR_LEN;
45+
wasm.documentend_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
46+
+ return this;
47+
}
48+
}
49+
module.exports.DocumentEnd = DocumentEnd;
50+
@@ -397,6 +402,7 @@
51+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
52+
var len0 = WASM_VECTOR_LEN;
53+
wasm.element_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
54+
+ return this;
55+
}
56+
/**
57+
* @param {string} content
58+
@@ -406,6 +412,7 @@
59+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
60+
var len0 = WASM_VECTOR_LEN;
61+
wasm.element_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
62+
+ return this;
63+
}
64+
/**
65+
* @param {string} content
66+
@@ -415,11 +422,13 @@
67+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
68+
var len0 = WASM_VECTOR_LEN;
69+
wasm.element_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
70+
+ return this;
71+
}
72+
/**
73+
*/
74+
remove() {
75+
wasm.element_remove(this.ptr);
76+
+ return this;
77+
}
78+
/**
79+
* @returns {boolean}
80+
@@ -495,6 +504,7 @@
81+
var ptr1 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
82+
var len1 = WASM_VECTOR_LEN;
83+
wasm.element_setAttribute(this.ptr, ptr0, len0, ptr1, len1);
84+
+ return this;
85+
}
86+
/**
87+
* @param {string} name
88+
@@ -503,6 +513,7 @@
89+
var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
90+
var len0 = WASM_VECTOR_LEN;
91+
wasm.element_removeAttribute(this.ptr, ptr0, len0);
92+
+ return this;
93+
}
94+
/**
95+
* @param {string} content
96+
@@ -512,6 +523,7 @@
97+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
98+
var len0 = WASM_VECTOR_LEN;
99+
wasm.element_prepend(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
100+
+ return this;
101+
}
102+
/**
103+
* @param {string} content
104+
@@ -521,6 +533,7 @@
105+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
106+
var len0 = WASM_VECTOR_LEN;
107+
wasm.element_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
108+
+ return this;
109+
}
110+
/**
111+
* @param {string} content
112+
@@ -530,11 +543,13 @@
113+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
114+
var len0 = WASM_VECTOR_LEN;
115+
wasm.element_setInnerContent(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
116+
+ return this;
117+
}
118+
/**
119+
*/
120+
removeAndKeepContent() {
121+
wasm.element_removeAndKeepContent(this.ptr);
122+
+ return this;
123+
}
124+
}
125+
module.exports.Element = Element;
126+
@@ -579,25 +594,27 @@
127+
var ptr0 = passStringToWasm0(selector, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
128+
var len0 = WASM_VECTOR_LEN;
129+
wasm.htmlrewriter_on(this.ptr, ptr0, len0, addHeapObject(handlers));
130+
+ return this;
131+
}
132+
/**
133+
* @param {any} handlers
134+
*/
135+
onDocument(handlers) {
136+
wasm.htmlrewriter_onDocument(this.ptr, addHeapObject(handlers));
137+
+ return this;
138+
}
139+
/**
140+
* @param {Uint8Array} chunk
141+
*/
142+
- write(chunk) {
143+
+ async write(chunk) {
144+
var ptr0 = passArray8ToWasm0(chunk, wasm.__wbindgen_malloc);
145+
var len0 = WASM_VECTOR_LEN;
146+
- wasm.htmlrewriter_write(this.ptr, ptr0, len0);
147+
+ await wrap(this, wasm.htmlrewriter_write, this.ptr, ptr0, len0);
148+
}
149+
/**
150+
*/
151+
- end() {
152+
- wasm.htmlrewriter_end(this.ptr);
153+
+ async end() {
154+
+ await wrap(this, wasm.htmlrewriter_end, this.ptr);
155+
}
156+
/**
157+
* @returns {number}
158+
@@ -638,6 +655,7 @@
159+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
160+
var len0 = WASM_VECTOR_LEN;
161+
wasm.textchunk_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
162+
+ return this;
163+
}
164+
/**
165+
* @param {string} content
166+
@@ -647,6 +665,7 @@
167+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
168+
var len0 = WASM_VECTOR_LEN;
169+
wasm.textchunk_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
170+
+ return this;
171+
}
172+
/**
173+
* @param {string} content
174+
@@ -656,11 +675,13 @@
175+
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
176+
var len0 = WASM_VECTOR_LEN;
177+
wasm.textchunk_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
178+
+ return this;
179+
}
180+
/**
181+
*/
182+
remove() {
183+
wasm.textchunk_remove(this.ptr);
184+
+ return this;
185+
}
186+
/**
187+
* @returns {boolean}
188+
@@ -847,5 +868,6 @@
189+
const wasmModule = new WebAssembly.Module(bytes);
190+
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
191+
wasm = wasmInstance.exports;
192+
+setWasmExports(wasm);
193+
module.exports.__wasm = wasm;
194+

0 commit comments

Comments
 (0)