WASM-based implementation of Cloudflare's HTML Rewriter for use in Deno, browsers, etc.

Overview

HTML Rewriter

WASM-based implementation of Cloudflare's HTML Rewriter for use in Deno, browsers, etc.

It uses lol-html under the hood, the same implementation used by Cloudflare Workers. It is based on Miniflare's WASM build.

Installation

This package includes 2 versions of HTML Rewriter.

index.ts loads the WASM that is co-located with this module via fetch and (streaming-) instantiates the module that way. In Deno, this works via file system (if you've downloaded the module) and web (when loading from deno.land/x or even githubusercontent.com). However, if you are using this version with other tooling, depending on the bundler and configuration the WASM source may or may not be included...

base64.ts has the required WASM inlined as compressed base64. The total size is 447K (345K gzipped). This ensures that HTML Rewriter is working properly when bundled, offline, etc. The "hackyness" of ~400K of inlined WASM and relying on DecompressionStream is significant (without compression, the file size would be 1.2MB), but its simplicity makes it easier to get it to work with various bundlers (including Deno's own, as of this writing).

Use in Browser

For use in the browser it's best to install the "node-ified" package from npm and use it with a bundler.

npm install @worker-tools/html-rewriter

Which version of HTML Rewriter to pick depends on which bundler you are using:

For Webpack 4, it's best to use the non-ESM base64 version via its full path: @worker-tools/html-rewriter/script/base64.

Webpack 5 treats the regular version correctly by default, which can be imported as @worker-tools/html-rewriter.

For esbuild it's best to use the base64-version via @worker-tools/html-rewriter/base64.

You can explore the full contents of the npm package here.

Usage

import { 
  HTMLRewriter 
} from 'https://ghuc.cc/worker-tools/html-rewriter/index.ts'

new HTMLRewriter()
  .on("p", {
    element(element) { 
      element.tagName = "h1" 
    },
  })
  .transform(new Response('<p class="red">test</p>'))
  .text().then(x => console.log(x))

For more on how to use HTMLRewriter, see the Cloudflare Workers Docs.

Building

Make sure you've initialized all git submodules. It is 2 levels deep.

git submodule update --init --recursive

Make sure you have rustup installed. Then run

make dist

This will build a custom version of wasm-pack first, then use it to compile lol-html to WASM. Please see the submodules for details on why this is necessary.

You might also like...

XML/HTML parser and processing library for JavaScript and TypeScript

XML/HTML parser and processing library for JavaScript and TypeScript

[VIEW DOCUMENTATION] Robin is an XML parser and processing library that supports a sane version of HTML. It features a set of DOM utilities, including

Oct 5, 2022

An easy-to-use multi SQL dialect ORM tool for Node.js

Sequelize Sequelize is a promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction s

Jan 4, 2023

A query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.

knex.js A SQL query builder that is flexible, portable, and fun to use! A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle

Jan 4, 2023

Lovefield is a relational database for web apps. Written in JavaScript, works cross-browser. Provides SQL-like APIs that are fast, safe, and easy to use.

Lovefield Lovefield is a relational database written in pure JavaScript. It provides SQL-like syntax and works cross-browser (currently supporting Chr

Jan 3, 2023

Workshop to illustrate how to use GraphQL

Workshop to illustrate how to use GraphQL

🎓 Netflix Clone using Astra DB and GraphQL 50 minutes, Intermediate, Start Building A simple ReactJS Netflix homepage clone running on Astra DB that

Jan 4, 2023

A proposal to add modern, easy to use binary encoders to the web platform.

proposal-binary-encoding A proposal to add modern, easy to use binary encoders to the web platform. This is proposed as an addition to the HTML spec,

Nov 27, 2022

This API can be use to all developers to get location details of Sri Lanka 🇱🇰 including major cities, sub areas, districts and Provinces. ⛳️

This API can be use to all developers to get location details of Sri Lanka  🇱🇰 including major cities, sub areas, districts and Provinces. ⛳️

Location Data API - Sri Lanka Table of Contents Introduction Technologies Implemantations Hosting Use Cases Getting Started Project Setup Contributing

Jun 29, 2022

An easy-to-use discord bot including database, slash commands and context menus !

Discord Bot Template An easy-to-use discord bot using Discord.JS V13. Wiki Includes: Slash commands Database User commands and more in future Requirem

Dec 28, 2022

Illustration of issues around use of top-level await in Vite apps

vite-top-level-await-repro Illustration of issues around use of top-level await in Vite apps: https://github.com/vitejs/vite/issues/5013 Top-level awa

Apr 25, 2022
Comments
  • Small licensing snafu

    Small licensing snafu

    Cool project. I noticed it looks like the vendor folder checked into the source code was adapted from html-rewriter-wasm with some adaptations to support ESM though it appears the original license is missing. Here it is for reference. It would be a good idea to add that to the source code as the BSD-3-Clause requires the license is carried around with the work.

    opened by vhscom 0
  • Cannot find module './vendor/html_rewriter.d.d.js' or its corresponding type declarations.

    Cannot find module './vendor/html_rewriter.d.d.js' or its corresponding type declarations.

    Seems to be an error in the type import statement here:

    @worker-tools/html-rewriter/types/index.d.ts:

    import type { ContentTypeOptions, Element, EndTag, Comment, TextChunk, Doctype, DocumentEnd, ElementHandlers, DocumentHandlers } from "./vendor/html_rewriter.d.d.js";
    

    I think html_rewriter.d.d.js should be html_rewriter.d.ts, no?

    image

    opened by Deester4x4jr 0
  • VSCode Debugger

    VSCode Debugger

    When running some code that includes html-rewriter in a vscode debug session I receive the following error.

    arg0: TypeError: WebAssembly.compile(): Argument 0 must be a buffer source {stack: 'TypeError: WebAssembly.compile(): Argument 0 must be a buffer source', message: 'WebAssembly.compile(): Argument 0 must be a buffer source', __callSiteEvals: Array(0)}
    <anonymous> @ /workspaces/ultra/workspace/x/[email protected]/https:/deno.land/x/[email protected]/index.ts:63:19
    
    opened by deckchairlabs 1
  • Export an `init` function

    Export an `init` function

    In order to allow pre-emptive initialisation of the wasm module to prevent an unwanted lag on the first request.

    Current workaround is calling new HtmlRewriter() and a delay() function, but this functionality should ideally be exported from the library.

    opened by ghost 5
Owner
Worker Tools
Utilities for working with worker backends such as Cloudflare Workers and Deno Deploy
Worker Tools
A web client port-scanner written in GO, that supports the WASM/WASI interface for Browser WebAssembly runtime execution.

WebAssembly Port Scanner Written in Go with target WASM/WASI. The WASM main function scans all the open ports in the specified range (see main.go), vi

Avi Lumelsky 74 Dec 27, 2022
A wrapper for abstract-leveldown compliant stores, for Node.js and browsers.

levelup Table of Contents Click to expand levelup Table of Contents Introduction Supported Platforms Usage API Special Notes levelup(db[, options[, ca

Level 4k Jan 9, 2023
Securely collect browsing history over browsers.

Visited-CLI Securely collect browsing history over browsers. Getting started Here is the getting started guide. Firstly, clone the git, and change to

Fumiya A 62 Dec 30, 2022
Anonymify - Outils TypeScript pour l'anonymisation des données en langue Française, compatible Node.js et dans les browsers.

@socialgouv/anonymify Outils TypeScript pour l'anonymisation des données en langue Française. Compatible Node.js et dans les navigateurs Démo : https:

Fabrique numérique des Ministères Sociaux 4 Nov 16, 2022
DolphinDB JavaScript API is a JavaScript library that encapsulates the ability to operate the DolphinDB database, such as: connecting to the database, executing scripts, calling functions, uploading variables, etc.

DolphinDB JavaScript API English | 中文 Overview DolphinDB JavaScript API is a JavaScript library that encapsulates the ability to operate the DolphinDB

DolphinDB 6 Dec 12, 2022
An HTML5 saveAs() FileSaver implementation

If you need to save really large files bigger than the blob's size limitation or don't have enough RAM, then have a look at the more advanced StreamSa

Eli Grey 19.9k Jan 9, 2023
GraphQL Fastify Server is an implementation of GraphQL.

GraphQL Fastify Server Installation Usage Using cache Middlewares Liveness & Readiness Contributing License Installation npm install --save graphql-fa

Rui Silva 33 Dec 19, 2022
An in-depth implementation of Clean Architecture using NestJS and type-script

Clean Architecture With NestJS Description It's been a while since my last article on how to implement clean architecture on Node.js applications, git

null 297 Dec 28, 2022
Fast and advanced, document based and key-value based NoSQL database that able to work as it is installed.

About Fast and advanced, document based and key-value based NoSQL database that able to work as it is installed. Features NoSQL database Can be run as

null 6 Dec 7, 2022
Fast and advanced, document-based and key-value-based NoSQL database.

Contents About Features Installation Links About Fast and advanced, document-based and key-value-based NoSQL database. Features NoSQL database Can be

null 6 Dec 7, 2022