Skip to content

Commit

Permalink
Update html-rewriter-wasm to 0.3.1 and remove @mrbbot/asyncify-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Jul 22, 2021
1 parent 96de92a commit 9f1d725
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 53 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 🚧 Changelog

## 1.3.1

### Fixes

- Upgraded [`html-rewriter-wasm`](https://github.com/mrbbot/html-rewriter-wasm)
to version `0.3.1`, fixing the return type of `Element.attributes`

## 1.3.0

### Features
Expand Down
8 changes: 8 additions & 0 deletions docs/html-rewriter.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ Miniflare includes `HTMLRewriter` in its sandbox. It's powered by
[`html-rewriter-wasm`](https://github.com/mrbbot/html-rewriter-wasm), which uses
a WebAssembly version of [`lol-html`](https://github.com/cloudflare/lol-html),
the same library Cloudflare Workers use for their `HTMLRewriter`.

<!--prettier-ignore-start-->
::: warning
If you're using `async` handlers, and a testing framework that supports running
tests in parallel, you should run tests that use `HTMLRewriter` in serial (e.g.
`test.serial` with AVA) for improved stability.
:::
<!--prettier-ignore-end-->
29 changes: 9 additions & 20 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "miniflare",
"version": "1.3.0",
"version": "1.3.1",
"description": "Fun, full-featured, fully-local simulator for Cloudflare Workers",
"keywords": [
"cloudflare",
Expand Down Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"@cloudflare/workers-types": "^2.2.2",
"@iarna/toml": "^2.2.5",
"@mrbbot/asyncify-wasm": "^1.3.1",
"@mrbbot/node-fetch": "^4.4.0",
"@peculiar/webcrypto": "^1.1.4",
"chalk": "^4.1.0",
Expand All @@ -45,7 +44,7 @@
"dotenv": "^8.2.0",
"env-paths": "^2.2.1",
"formdata-node": "^2.4.0",
"html-rewriter-wasm": "^0.3.0",
"html-rewriter-wasm": "^0.3.1",
"http-cache-semantics": "^4.1.0",
"ioredis": "^4.27.6",
"micromatch": "^4.0.4",
Expand Down
48 changes: 19 additions & 29 deletions src/modules/rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,16 @@ export function transformToArray(chunk: any): Uint8Array {
}

export class HTMLRewriter {
#elementHandlers: [selector: string, handlers: any][] = [];
#documentHandlers: any[] = [];
#elementHandlers: [selector: string, handlers: ElementHandlers][] = [];
#documentHandlers: DocumentHandlers[] = [];

on(selector: string, handlers: ElementHandlers): this {
// Ensure handlers register returned promises, and `this` is bound correctly
const wrappedHandlers = {
element: handlers.element?.bind(handlers),
comments: handlers.comments?.bind(handlers),
text: handlers.text?.bind(handlers),
};
this.#elementHandlers.push([selector, wrappedHandlers]);
this.#elementHandlers.push([selector, handlers]);
return this;
}

onDocument(handlers: DocumentHandlers): this {
// Ensure handlers register returned promises, and `this` is bound correctly
const wrappedHandlers = {
doctype: handlers.doctype?.bind(handlers),
comments: handlers.comments?.bind(handlers),
text: handlers.text?.bind(handlers),
end: handlers.end?.bind(handlers),
};
this.#documentHandlers.push(wrappedHandlers);
this.#documentHandlers.push(handlers);
return this;
}

Expand All @@ -72,13 +59,8 @@ export class HTMLRewriter {
// Create a rewriter instance for this transformation that writes its
// output to the transformed response's stream
const rewriter = new BaseHTMLRewriter((output: Uint8Array) => {
if (output.length === 0) {
// Free the rewriter once it's finished doing its thing
queueMicrotask(() => rewriter.free());
controller.close();
} else {
controller.enqueue(output);
}
// enqueue will throw on empty chunks
if (output.length !== 0) controller.enqueue(output);
});
// Add all registered handlers
for (const [selector, handlers] of this.#elementHandlers) {
Expand All @@ -88,13 +70,21 @@ export class HTMLRewriter {
rewriter.onDocument(handlers);
}

// Transform the response body (may be null if empty)
if (response.body) {
for await (const chunk of response.body) {
await rewriter.write(transformToArray(chunk));
try {
// Transform the response body (may be null if empty)
if (response.body) {
for await (const chunk of response.body) {
await rewriter.write(transformToArray(chunk));
}
}
await rewriter.end();
} catch (e) {
controller.error(e);
} finally {
// Make sure the rewriter/controller are always freed/closed
rewriter.free();
controller.close();
}
await rewriter.end();
},
});

Expand Down
3 changes: 2 additions & 1 deletion test/modules/rewriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
} from "../../src/modules/rewriter";
import { getObjectProperties, wait } from "../helpers";

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

// region: Uint8ArrayTransformStream
const encoder = new TextEncoder();
Expand Down

0 comments on commit 9f1d725

Please sign in to comment.