Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sax-wasm

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sax-wasm

An extremely fast JSX, HTML and XML parser written in Rust compiled to WebAssembly for Node and the Web

  • 1.0.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.3K
decreased by-18.24%
Maintainers
1
Weekly downloads
 
Created
Source

SAX (Simple API for XML) for Web Assembly

Build Status Coverage Status

When you absolutely, positively have to have the fastest parser in the room, accept no substitutes.

The first streamable, low memory XML, HTML, and JSX parser for WebAssembly. And yes, it will parse JSX!

Sax Wasm is a sax style parser for XML, HTML and JSX written in Rust, compiled for Web Assembly with the sole motivation to bring near native speeds to xml and JSX parsing for node and the web.

This is a port of sax js to Rust for Web Assembly with optimizations for speed and JSX specific syntax. Since there are no built-ins with WebAssembly, file size is larger but execution time is faster, sometimes much, much faster.

Installation

npm i -s sax-wasm

Usage in Node

const fs = require('fs');
const { SaxEventType, SAXParser } = require('sax-wasm');

// Get the path to the Web Assembly binary and load it
const saxPath = require.resolve('sax-wasm/lib/sax-wasm.wasm');
const saxWasmBuffer = fs.readFileSync(saxPath);

// Instantiate 
const parser = new SAXParser(0, SaxEventType.Attribute | SaxEventType.OpenTag);
parser.eventHandler = (event, data) => {
  if (event === SaxEventType.Attribute ) {
    // process attribute
  } else {
    // process open tag
  }
}

// Instantiate and prepare the wasm for parsing
parser.prepareWasm(saxWasmBuffer).then(ready => {
  if (ready) {
    parser.write('<div class="modal"></div>');
  }
})

Usage for the web

import { SaxEventType, SAXParser } from 'sax-wasm';

async function loadAndPrepareWasm() {
  const saxWasmResponse = await fetch('./path/to/wasm/sax-wasm.wasm');
  const saxWasmbuffer = await saxWasmResponse.arrayBuffer();
  const parser = new SAXParser(0, SaxEventType.Attribute | SaxEventType.OpenTag);
  
  // Instantiate and prepare the wasm for parsing
  const ready = await parser.prepareWasm(new Uint8Array(saxWasmbuffer));
  if (ready) {
    return parser;
  }
}

loadAndPrepareWasm().then(parser => {
  parser.eventHandler = (event, data) => {
      if (event === SaxEventType.Attribute ) {
          // process attribute
        } else {
          // process open tag
        }
    }
    parser.write('<div class="modal"></div>')
});

Differences from sax-js

Besides being incredibly fast, there are some notable differences between sax-wasm and sax-js that may affect some users when migrating:

  1. JSX is supported including JSX fragments. Things like <foo bar={this.bar()}></bar> will parse as expected.
  2. No attempt is made to validate the document. sax-wasm reports what it sees. If you need "strict mode", it could be recreated by applying rules to the events that are reported by the parser.
  3. Namespaces are reported in attributes. No special events dedicated to namespaces
  4. The parser is ready as soon as the promise is handled.

Streaming

Streaming is supported with sax-wasm by writing utf-8 encoded text to the parser instance. Writes can occur safely anywhere except withing the eventHandler function or within the eventTrap (when extending SAXParser class). Doing so anyway risks overwriting memory still in play.

Events

Events are subscribed to using a bitmask composed from flags representing the event type. Bit positions along a 15 bit integer can be masked on to tell the parser to emit the event of that type. For example, passing in the following bitmask to the parser instructs it to emit events for text, open tags and attributes:

import { SaxEventType } from 'sax-wasm';
parser.events = SaxEventType.Text | SaxEventType.OpenTag | SaxEventType.Attribute;

Complete list of event/argument pairs:

EventArgument
SaxEventType.Texttext: string
SaxEventType.ProcessingInstructionprocInst: string
SaxEventType.SGMLDeclarationsgmlDecl: string
SaxEventType.Doctypedoctype: string
SaxEventType.Commentcomment: string
SaxEventType.OpenTagStarttag: Tag
SaxEventType.Attributeattribute: Attribute
SaxEventType.OpenTagtag: Tag
SaxEventType.CloseTagtag: Tag
SaxEventType.OpenCDATAstart: Position
SaxEventType.CDATAcdata: string
SaxEventType.CloseCDATAend: Position
SaxEventType.CloseNamespacens: Namespace
SaxEventType.Scriptscript: string
SaxEventType.OpenNamespacens: Namespace

Building from source

Prerequisites

This project requires rust v1.29+ since it contains the wasm32-unknown-unknown target out of the box. This is currently only available in the nightly build.

Install rust:

curl https://sh.rustup.rs -sSf | sh

Install the nightly compiler and switch to it

rustup install nightly
rustup default nightly

Install node with npm then run the following command from the project root.

npm install

The project can now be built using:

npm run build

Keywords

FAQs

Package last updated on 08 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc