Socket
Book a DemoInstallSign in
Socket

libxml2-wasm

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libxml2-wasm

WebAssembly-based libxml2 javascript binding

Source
npmnpm
Version
0.4.1
Version published
Weekly downloads
5.2K
-26.09%
Maintainers
1
Weekly downloads
 
Created
Source

libxml2-wasm

CI Build on master npm

Why another xml lib?

Comparing to the previous two main approaches, pure javascript implementation as well as traditional C implementation binding, using WebAssembly combines the pros from both sides, providing good performance while keeping best compatibility with modern Javascript runtime.

Javascript ImplementationTraditional C BindingWebAssembly
Parsing Speed1AverageFastFast
C/C++ ToolchainNot requiredRequired2Not Required
Prebuilt BinariesN/AOne for each OS/Runtime versionUniversal for all OS/Runtime versions
Prebuilt Binary CompatibilityN/AMay broke across libc versionsVery Good
Browser CompatibilityYesNoYes

Documentation

https://jameslan.github.io/libxml2-wasm/index.html

Supported Environments

Due to the usage of WebAssembly, ES module and top level await etc, it requires the minimum version of the following environments,

EnvironmentVersion
Node.jsv16+
ChromeV89+
EdgeV89+
Safariv15+

Features

  • Parsing
  • Validating
  • XInclude and XSD include/import (experimental)

Getting started

Install libxml2-wasm package:

npm i libxml2-wasm

libxml2-wasm is an ES module, importing it are different between ES module and commonJS module.

ESM

Import it directly.

import fs from 'node:fs';
import { XmlDocument } from 'libxml2-wasm';
const doc1 = XmlDocument.fromString('<note><to>Tove</to></note>');
const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
doc1.dispose();
doc2.dispose();

CommonJS

Dynamic import is needed:

const fs = require('node:fs');
import('libxml2-wasm').then(({ XmlDocument }) => {
    const doc1 = XmlDocument.fromString('<note><to>Tove</to></note>');
    const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
    doc1.dispose();
    doc2.dispose();
});

IMPORTANT: dispose() is required to avoid memory leak.

For more detail, see the Doc.

Footnotes

  • The speed of different libraries varies a lot, see benchmark.

  • The requirement of C/C++ toolchain may be waived if prebuilt binary is available.

Keywords

xml

FAQs

Package last updated on 17 Oct 2024

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