https://github.com/ije/deno-lol-html
The Low Output Latency streaming HTML rewriter/parser with CSS-selector based API, in Webassembly.
https://github.com/ije/deno-lol-html
Last synced: about 1 year ago
JSON representation
The Low Output Latency streaming HTML rewriter/parser with CSS-selector based API, in Webassembly.
- Host: GitHub
- URL: https://github.com/ije/deno-lol-html
- Owner: ije
- License: mit
- Created: 2022-02-10T19:43:22.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-18T03:17:54.000Z (over 2 years ago)
- Last Synced: 2025-03-30T22:32:19.167Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 2.46 MB
- Stars: 23
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# LOL(😂) HTML
Wasm bindings of [cloudflare/lol-html](https://github.com/cloudflare/lol-html) for Deno, the Low Output
Latency streaming HTML rewriter/parser with CSS-selector based API, in
Webassembly.
> The wasm bindings code is based on
> https://github.com/cloudflare/lol-html/pull/100 by @devsnek
## Documentation
See https://developers.cloudflare.com/workers/runtime-apis/html-rewriter
## Example
```ts
import { concat } from "https://deno.land/std@0.200.0/bytes/mod.ts";
import init, { HTMLRewriter } from "https://deno.land/x/lol_html@0.1.0/mod.ts";
await init();
const enc = new TextEncoder();
const dec = new TextDecoder();
const chunks: Uint8Array[] = [];
const parts = [
"
',
"LOL(:lol) HTML",
"a>
",
];
const rewriter = new HTMLRewriter("utf8", (chunk: Uint8Array) => {
chunks.push(chunk);
});
// add handlers before calling `write` or `end`
rewriter.on("a[href]", {
element(el) {
el.setAttribute("class", "this-is-a-link");
},
text(chunk) {
chunk.replace(chunk.text.replaceAll(":lol", "😂"));
},
});
try {
for (const part of parts) {
rewriter.write(enc.encode(part));
}
rewriter.end();
console.log(dec.decode(concat(...chunks)));
} finally {
// don't forget to free the memory
rewriter.free();
}
```
## Using Stream
The wasm binding version doesn't implement the `transform` method in the [docs](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter), you can use
the `ReadableStream` instead.
```ts
import { serve } from "https://deno.land/std@0.170.0/http/server.ts";
import init, { HTMLRewriter } from "https://deno.land/x/lol_html@0.1.0/mod.ts";
await init();
serve(() => {
const readable = new ReadableStream({
async start(controller) {
const rewriter = new HTMLRewriter("utf8", (chunk: Uint8Array) => {
controller.enqueue(chunk);
});
const res = await fetch("https://lol-html.fun");
const pump = async () => {
const { done, value } = await res.body!.read();
if (done) {
controller.close();
try {
rewriter.end();
} finally {
rewriter.free();
}
return;
}
rewriter.write(value!);
pump();
};
pump();
},
});
return new Response(readable, {
headers: { "content-type": "text/html" },
});
});
```