html-rewriter-wasm
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -18,3 +18,3 @@ export interface ContentTypeOptions { | ||
removeAndKeepContent(): this; | ||
readonly attributes: [string, string][]; | ||
readonly attributes: IterableIterator<[string, string]>; | ||
readonly namespaceURI: string; | ||
@@ -51,3 +51,3 @@ readonly removed: boolean; | ||
export class DocumentEnd { | ||
append(content: string, options?: ContentTypeOptions): DocumentEnd; | ||
append(content: string, options?: ContentTypeOptions): this; | ||
} | ||
@@ -54,0 +54,0 @@ |
@@ -475,3 +475,3 @@ let imports = {}; | ||
var ret = wasm.element_attributes(this.ptr); | ||
return takeObject(ret); | ||
return takeObject(ret)[Symbol.iterator](); | ||
} | ||
@@ -742,2 +742,7 @@ /** | ||
module.exports.__wbindgen_object_clone_ref = function(arg0) { | ||
var ret = getObject(arg0); | ||
return addHeapObject(ret); | ||
}; | ||
module.exports.__wbg_element_c38470ed972aea27 = function(arg0) { | ||
@@ -798,7 +803,2 @@ var ret = getObject(arg0).element; | ||
module.exports.__wbindgen_object_clone_ref = function(arg0) { | ||
var ret = getObject(arg0); | ||
return addHeapObject(ret); | ||
}; | ||
module.exports.__wbg_String_60c4ba333b5ca1c6 = function(arg0, arg1) { | ||
@@ -805,0 +805,0 @@ var ret = String(getObject(arg1)); |
{ | ||
"name": "html-rewriter-wasm", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "WebAssembly version of HTMLRewriter", | ||
@@ -19,3 +19,10 @@ "main": "dist/html_rewriter.js", | ||
}, | ||
"keywords": ["cloudflare", "workers", "worker", "html", "rewriter", "lol"], | ||
"keywords": [ | ||
"cloudflare", | ||
"workers", | ||
"worker", | ||
"html", | ||
"rewriter", | ||
"lol" | ||
], | ||
"author": "MrBBot", | ||
@@ -28,2 +35,3 @@ "license": "BSD-3-Clause", | ||
"devDependencies": { | ||
"@types/node": "^14.17.5", | ||
"ava": "^3.15.0", | ||
@@ -30,0 +38,0 @@ "prettier": "^2.3.2", |
@@ -13,2 +13,8 @@ # `html-rewriter-wasm` | ||
## Features | ||
- 🔋 Supports all handler types, properties and methods | ||
- ⏰ Supports synchronous and asynchronous handlers | ||
- 📌 Supports class handlers with correctly bound methods | ||
## Usage | ||
@@ -22,9 +28,5 @@ | ||
let output = ""; | ||
const rewriter = new HTMLRewriter((outputChunk) => { | ||
if (outputChunk.length === 0) { | ||
// Remember to free memory on last chunk | ||
queueMicrotask(() => rewriter.free()); | ||
} else { | ||
console.log(decoder.decode(outputChunk)); // <p>new</p> | ||
} | ||
output += decoder.decode(outputChunk); | ||
}); | ||
@@ -38,4 +40,9 @@ | ||
await rewriter.write(encoder.encode("<p>old</p>")); | ||
await rewriter.end(); | ||
try { | ||
await rewriter.write(encoder.encode("<p>old</p>")); | ||
await rewriter.end(); | ||
console.log(output); // <p>new</p> | ||
} finally { | ||
rewriter.free(); // Remember to free memory | ||
} | ||
``` | ||
@@ -49,2 +56,21 @@ | ||
- Once `write` or `end` has been called, you cannot add any more handlers. You | ||
must register all handlers before you start transforming: | ||
```js | ||
const rewriter = new HTMLRewriter(...); | ||
// ❌ | ||
rewriter.on("h1", { ... }); | ||
await rewriter.write(encoder.encode("<h1>1</h1")); | ||
rewriter.on("p", { ... }); // not allowed | ||
await rewriter.write(encoder.encode("<p>2</p>")); | ||
// ✅ | ||
rewriter.on("h1", { ... }); | ||
rewriter.on("p", { ... }); | ||
await rewriter.write(encoder.encode("<h1>1</h1")); | ||
await rewriter.write(encoder.encode("<p>2</p>")); | ||
``` | ||
- `end` may only be called once per `HTMLRewriter` instance. This means you must | ||
@@ -92,26 +118,15 @@ create a new `HTMLRewriter` instance for each transformation: | ||
- If using handler classes, you must bind their methods to the class first: | ||
## Internals | ||
```js | ||
class Handler { | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
`lol-html` doesn't natively support asynchronous handlers. Instead, whenever a | ||
handler returns a `Promise`, we have to unwind the WebAssembly stack into | ||
temporary storage, wait for the promise to resolve, then rewind the stack and | ||
continue parsing. This temporary storage is per `HTMLRewriter` instance, hence | ||
we cannot have concurrent `write` and `end` calls. We use the | ||
[Asyncify](https://github.com/WebAssembly/binaryen/blob/main/src/passes/Asyncify.cpp) | ||
feature of [Binaryen](https://github.com/WebAssembly/binaryen) to implement | ||
this. See | ||
[this article](https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html) for | ||
more details. | ||
element(element) { | ||
element.setInnerContent(this.value); | ||
} | ||
} | ||
const rewriter = new HTMLRewriter(...); | ||
const handler = new Handler("new"); | ||
// ❌ | ||
rewriter.on("p", handler); | ||
// ✅ | ||
rewriter.on("p", { | ||
element: handler.element.bind(handler) | ||
}) | ||
``` | ||
## Building | ||
@@ -118,0 +133,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
989892
175
5