Skip to content

Commit d3b6dd5

Browse files
committed
Add more detail to README.md
1 parent 6f0597b commit d3b6dd5

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
profile: minimal
1919
toolchain: stable
2020
- uses: Swatinem/rust-cache@v1
21+
with:
22+
cache-on-failure: true
2123
- name: Use Node.js LTS
2224
uses: actions/setup-node@v2
2325
with:

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ their work on
1010
[lol-html's JavaScript API](https://github.com/cloudflare/lol-html/tree/master/js-api)
1111
which this package's Rust code is based on.
1212

13+
## Features
14+
15+
- 🔋 Supports all handler types, properties and methods
16+
- ⏰ Supports synchronous and asynchronous handlers
17+
- 📌 Supports class handlers with correctly bound methods
18+
1319
## Usage
1420

1521
```js
@@ -44,6 +50,25 @@ and output to strings.
4450

4551
## Caveats
4652

53+
- Once `write` or `end` has been called, you cannot add any more handlers. You
54+
must register all handlers before you start transforming:
55+
56+
```js
57+
const rewriter = new HTMLRewriter(...);
58+
59+
//
60+
rewriter.on("h1", { ... });
61+
await rewriter.write(encoder.encode("<h1>1</h1"));
62+
rewriter.on("p", { ... }); // not allowed
63+
await rewriter.write(encoder.encode("<p>2</p>"));
64+
65+
//
66+
rewriter.on("h1", { ... });
67+
rewriter.on("p", { ... });
68+
await rewriter.write(encoder.encode("<h1>1</h1"));
69+
await rewriter.write(encoder.encode("<p>2</p>"));
70+
```
71+
4772
- `end` may only be called once per `HTMLRewriter` instance. This means you must
4873
create a new `HTMLRewriter` instance for each transformation:
4974

@@ -86,6 +111,19 @@ and output to strings.
86111
await rewriter.write(encoder.encode("<p>2</p>"));
87112
```
88113

114+
## Internals
115+
116+
`lol-html` doesn't natively support asynchronous handlers. Instead, whenever a
117+
handler returns a `Promise`, we have to unwind the WebAssembly stack into
118+
temporary storage, wait for the promise to resolve, then rewind the stack and
119+
continue parsing. This temporary storage is per `HTMLRewriter` instance, hence
120+
we cannot have concurrent `write` and `end` calls. We use the
121+
[Asyncify](https://github.com/WebAssembly/binaryen/blob/main/src/passes/Asyncify.cpp)
122+
feature of [Binaryen](https://github.com/WebAssembly/binaryen) to implement
123+
this. See
124+
[this article](https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html) for
125+
more details.
126+
89127
## Building
90128

91129
You can build the package by running `npm run build`. You must do this prior to

package-lock.json

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616
"type": "git",
1717
"url": "git+https://github.com/mrbbot/html-rewriter-wasm.git"
1818
},
19-
"keywords": ["cloudflare", "workers", "worker", "html", "rewriter", "lol"],
19+
"keywords": [
20+
"cloudflare",
21+
"workers",
22+
"worker",
23+
"html",
24+
"rewriter",
25+
"lol"
26+
],
2027
"author": "MrBBot",
2128
"license": "BSD-3-Clause",
2229
"bugs": {
2330
"url": "https://github.com/mrbbot/html-rewriter-wasm/issues"
2431
},
2532
"homepage": "https://github.com/mrbbot/html-rewriter-wasm#readme",
2633
"devDependencies": {
34+
"@types/node": "^14.17.5",
2735
"ava": "^3.15.0",
2836
"prettier": "^2.3.2",
2937
"ts-node": "^10.1.0",

0 commit comments

Comments
 (0)