Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 9f1d725

Browse files
committed
Update html-rewriter-wasm to 0.3.1 and remove @mrbbot/asyncify-wasm
1 parent 96de92a commit 9f1d725

File tree

6 files changed

+47
-53
lines changed

6 files changed

+47
-53
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# 🚧 Changelog
22

3+
## 1.3.1
4+
5+
### Fixes
6+
7+
- Upgraded [`html-rewriter-wasm`](https://github.com/mrbbot/html-rewriter-wasm)
8+
to version `0.3.1`, fixing the return type of `Element.attributes`
9+
310
## 1.3.0
411

512
### Features

docs/html-rewriter.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ Miniflare includes `HTMLRewriter` in its sandbox. It's powered by
66
[`html-rewriter-wasm`](https://github.com/mrbbot/html-rewriter-wasm), which uses
77
a WebAssembly version of [`lol-html`](https://github.com/cloudflare/lol-html),
88
the same library Cloudflare Workers use for their `HTMLRewriter`.
9+
10+
<!--prettier-ignore-start-->
11+
::: warning
12+
If you're using `async` handlers, and a testing framework that supports running
13+
tests in parallel, you should run tests that use `HTMLRewriter` in serial (e.g.
14+
`test.serial` with AVA) for improved stability.
15+
:::
16+
<!--prettier-ignore-end-->

package-lock.json

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

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "miniflare",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Fun, full-featured, fully-local simulator for Cloudflare Workers",
55
"keywords": [
66
"cloudflare",
@@ -36,7 +36,6 @@
3636
"dependencies": {
3737
"@cloudflare/workers-types": "^2.2.2",
3838
"@iarna/toml": "^2.2.5",
39-
"@mrbbot/asyncify-wasm": "^1.3.1",
4039
"@mrbbot/node-fetch": "^4.4.0",
4140
"@peculiar/webcrypto": "^1.1.4",
4241
"chalk": "^4.1.0",
@@ -45,7 +44,7 @@
4544
"dotenv": "^8.2.0",
4645
"env-paths": "^2.2.1",
4746
"formdata-node": "^2.4.0",
48-
"html-rewriter-wasm": "^0.3.0",
47+
"html-rewriter-wasm": "^0.3.1",
4948
"http-cache-semantics": "^4.1.0",
5049
"ioredis": "^4.27.6",
5150
"micromatch": "^4.0.4",

src/modules/rewriter.ts

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,16 @@ export function transformToArray(chunk: any): Uint8Array {
3939
}
4040

4141
export class HTMLRewriter {
42-
#elementHandlers: [selector: string, handlers: any][] = [];
43-
#documentHandlers: any[] = [];
42+
#elementHandlers: [selector: string, handlers: ElementHandlers][] = [];
43+
#documentHandlers: DocumentHandlers[] = [];
4444

4545
on(selector: string, handlers: ElementHandlers): this {
46-
// Ensure handlers register returned promises, and `this` is bound correctly
47-
const wrappedHandlers = {
48-
element: handlers.element?.bind(handlers),
49-
comments: handlers.comments?.bind(handlers),
50-
text: handlers.text?.bind(handlers),
51-
};
52-
this.#elementHandlers.push([selector, wrappedHandlers]);
46+
this.#elementHandlers.push([selector, handlers]);
5347
return this;
5448
}
5549

5650
onDocument(handlers: DocumentHandlers): this {
57-
// Ensure handlers register returned promises, and `this` is bound correctly
58-
const wrappedHandlers = {
59-
doctype: handlers.doctype?.bind(handlers),
60-
comments: handlers.comments?.bind(handlers),
61-
text: handlers.text?.bind(handlers),
62-
end: handlers.end?.bind(handlers),
63-
};
64-
this.#documentHandlers.push(wrappedHandlers);
51+
this.#documentHandlers.push(handlers);
6552
return this;
6653
}
6754

@@ -72,13 +59,8 @@ export class HTMLRewriter {
7259
// Create a rewriter instance for this transformation that writes its
7360
// output to the transformed response's stream
7461
const rewriter = new BaseHTMLRewriter((output: Uint8Array) => {
75-
if (output.length === 0) {
76-
// Free the rewriter once it's finished doing its thing
77-
queueMicrotask(() => rewriter.free());
78-
controller.close();
79-
} else {
80-
controller.enqueue(output);
81-
}
62+
// enqueue will throw on empty chunks
63+
if (output.length !== 0) controller.enqueue(output);
8264
});
8365
// Add all registered handlers
8466
for (const [selector, handlers] of this.#elementHandlers) {
@@ -88,13 +70,21 @@ export class HTMLRewriter {
8870
rewriter.onDocument(handlers);
8971
}
9072

91-
// Transform the response body (may be null if empty)
92-
if (response.body) {
93-
for await (const chunk of response.body) {
94-
await rewriter.write(transformToArray(chunk));
73+
try {
74+
// Transform the response body (may be null if empty)
75+
if (response.body) {
76+
for await (const chunk of response.body) {
77+
await rewriter.write(transformToArray(chunk));
78+
}
9579
}
80+
await rewriter.end();
81+
} catch (e) {
82+
controller.error(e);
83+
} finally {
84+
// Make sure the rewriter/controller are always freed/closed
85+
rewriter.free();
86+
controller.close();
9687
}
97-
await rewriter.end();
9888
},
9989
});
10090

test/modules/rewriter.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
} from "../../src/modules/rewriter";
1919
import { getObjectProperties, wait } from "../helpers";
2020

21-
// TODO: remove most of these tests, they're now in html-rewriter-wasm
21+
// TODO: (low priority) remove most of these tests, they're now in html-rewriter-wasm
22+
// TODO: (low priority) debug why removing .serial breaks some of these tests
2223

2324
// region: Uint8ArrayTransformStream
2425
const encoder = new TextEncoder();

0 commit comments

Comments
 (0)