{"id":13626478,"url":"https://github.com/cloudflare/html-rewriter-wasm","last_synced_at":"2025-03-21T20:06:57.439Z","repository":{"id":37711047,"uuid":"388216979","full_name":"cloudflare/html-rewriter-wasm","owner":"cloudflare","description":"WebAssembly version of HTMLRewriter","archived":false,"fork":false,"pushed_at":"2024-09-27T17:12:22.000Z","size":112,"stargazers_count":182,"open_issues_count":7,"forks_count":17,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-17T15:46:58.498Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/html-rewriter-wasm","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudflare.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-21T18:56:02.000Z","updated_at":"2025-03-15T15:03:44.000Z","dependencies_parsed_at":"2024-06-11T19:04:55.116Z","dependency_job_id":"166c78c0-0233-4330-b879-26b2c0a1d9b1","html_url":"https://github.com/cloudflare/html-rewriter-wasm","commit_stats":{"total_commits":28,"total_committers":1,"mean_commits":28.0,"dds":0.0,"last_synced_commit":"de05e01f225e6d4c316b19b9aae8b9aa68db9524"},"previous_names":["mrbbot/html-rewriter-wasm"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fhtml-rewriter-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fhtml-rewriter-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fhtml-rewriter-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fhtml-rewriter-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudflare","download_url":"https://codeload.github.com/cloudflare/html-rewriter-wasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244860584,"owners_count":20522464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:02:20.105Z","updated_at":"2025-03-21T20:06:57.402Z","avatar_url":"https://github.com/cloudflare.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Frameworks \u0026 Libraries"],"sub_categories":["Utilities \u0026 Processing"],"readme":"# `html-rewriter-wasm`\n\nAn implementation of\n[HTMLRewriter](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter)\nusing a WebAssembly version of\n[lol-html](https://github.com/cloudflare/lol-html/). This was primarily written\nfor [🔥 Miniflare](https://github.com/mrbbot/miniflare), but may be useful for\nother projects too. Many thanks to [@inikulin](https://github.com/inikulin) for\ntheir work on\n[lol-html's JavaScript API](https://github.com/cloudflare/lol-html/tree/master/js-api)\nwhich this package's Rust code is based on.\n\n## Features\n\n- 🔋 Supports all handler types, properties and methods\n- ⏰ Supports synchronous and asynchronous handlers\n- 📌 Supports class handlers with correctly bound methods\n\n## Usage\n\n```js\nimport { HTMLRewriter } from \"html-rewriter-wasm\";\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\nlet output = \"\";\nconst rewriter = new HTMLRewriter((outputChunk) =\u003e {\n  output += decoder.decode(outputChunk);\n});\n\nrewriter.on(\"p\", {\n  element(element) {\n    element.setInnerContent(\"new\");\n  },\n});\n\ntry {\n  await rewriter.write(encoder.encode(\"\u003cp\u003eold\u003c/p\u003e\"));\n  await rewriter.end();\n  console.log(output); // \u003cp\u003enew\u003c/p\u003e\n} finally {\n  rewriter.free(); // Remember to free memory\n}\n```\n\nSee [test/index.ts](./test/index.ts) for a more traditional `HTMLRewriter`\nimplementation that doesn't have the caveats listed below, but restricts input\nand output to strings.\n\nTo enable the\n[`html_rewriter_treats_esi_include_as_void_tag`](https://developers.cloudflare.com/workers/platform/compatibility-dates#htmlrewriter-handling-of-esiinclude)\ncompatibility flag, set `enableEsiTags` when constructing the `HTMLRewriter`:\n\n```js\nconst rewriter = new HTMLRewriter((outputChunk) =\u003e { ... }, {\n  enableEsiTags: true,\n});\n```\n\n## Caveats\n\n- Once `write` or `end` has been called, you cannot add any more handlers. You\n  must register all handlers before you start transforming:\n\n  ```js\n  const rewriter = new HTMLRewriter(...);\n\n  // ❌\n  rewriter.on(\"h1\", { ... });\n  await rewriter.write(encoder.encode(\"\u003ch1\u003e1\u003c/h1\"));\n  rewriter.on(\"p\", { ... }); // not allowed\n  await rewriter.write(encoder.encode(\"\u003cp\u003e2\u003c/p\u003e\"));\n\n  // ✅\n  rewriter.on(\"h1\", { ... });\n  rewriter.on(\"p\", { ... });\n  await rewriter.write(encoder.encode(\"\u003ch1\u003e1\u003c/h1\"));\n  await rewriter.write(encoder.encode(\"\u003cp\u003e2\u003c/p\u003e\"));\n  ```\n\n- `end` may only be called once per `HTMLRewriter` instance. This means you must\n  create a new `HTMLRewriter` instance for each transformation:\n\n  ```js\n  // ❌\n  const rewriter = new HTMLRewriter(...);\n  await rewriter.end();\n  await rewriter.end(); // not allowed\n\n  // ✅\n  const rewriter1 = new HTMLRewriter(...);\n  await rewriter1.end();\n  const rewriter2 = new HTMLRewriter(...);\n  await rewriter2.end();\n  ```\n\n- When using `async` handlers, you must always `await` calls to `write` and\n  `end` before calling them again. In other words, you cannot have concurrent\n  `write` and `end` calls:\n\n  ```js\n  const rewriter = new HTMLRewriter(...).on(\"p\", {\n    async element(element) {\n      await fetch(...);\n      element.setInnerContent(\"new\");\n    }\n  });\n\n  // ❌\n  rewriter.write(encoder.encode(\"\u003cp\u003e1\u003c/p\u003e\"));\n  rewriter.write(encoder.encode(\"\u003cp\u003e2\u003c/p\u003e\")); // not allowed\n\n  // ❌\n  const promise1 = rewriter.write(encoder.encode(\"\u003cp\u003e1\u003c/p\u003e\"));\n  const promise2 = rewriter.write(encoder.encode(\"\u003cp\u003e2\u003c/p\u003e\"));\n  await Promise.all([promise1, promise2]); // not allowed\n\n  // ✅\n  await rewriter.write(encoder.encode(\"\u003cp\u003e1\u003c/p\u003e\"));\n  await rewriter.write(encoder.encode(\"\u003cp\u003e2\u003c/p\u003e\"));\n  ```\n\n## Internals\n\n`lol-html` doesn't natively support asynchronous handlers. Instead, whenever a\nhandler returns a `Promise`, we have to unwind the WebAssembly stack into\ntemporary storage, wait for the promise to resolve, then rewind the stack and\ncontinue parsing. This temporary storage is per `HTMLRewriter` instance, hence\nwe cannot have concurrent `write` and `end` calls. We use the\n[Asyncify](https://github.com/WebAssembly/binaryen/blob/main/src/passes/Asyncify.cpp)\nfeature of [Binaryen](https://github.com/WebAssembly/binaryen) to implement\nthis. See\n[this article](https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html) for\nmore details.\n\n## Building\n\nYou can build the package by running `npm run build`. You must do this prior to\nrunning tests with `npm test`.\n\nYou **must** have mrbbot's fork of wasm-pack installed. This upgrades binaryen\n(wasm-opt) to version_92 which exports `asyncify_get_state`:\n\n```shell\n$ cargo install --git https://github.com/mrbbot/wasm-pack\n$ npm run build\n$ npm test\n```\n\n## License\n\n`html-rewriter-wasm` uses [lol-html](https://github.com/cloudflare/lol-html/)\nwhich is BSD 3-Clause licensed:\n\n```\nCopyright (C) 2019, Cloudflare, Inc.\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this\nlist of conditions and the following disclaimer.\n\n2. Redistributions in binary form must reproduce the above copyright notice,\nthis list of conditions and the following disclaimer in the documentation and/or\nother materials provided with the distribution.\n\n3. Neither the name of the copyright holder nor the names of its contributors\nmay be used to endorse or promote products derived from this software without\nspecific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudflare%2Fhtml-rewriter-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudflare%2Fhtml-rewriter-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudflare%2Fhtml-rewriter-wasm/lists"}