JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1386526
  • Score
    100M100P100Q198778F
  • License MIT

A WebAssembly implementation of xxHash

Package Exports

  • xxhash-wasm
  • xxhash-wasm/cjs/xxhash-wasm

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xxhash-wasm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

xxhash-wasm

Build Status npm

A WebAssembly implementation of xxHash.

Installation

From npm

npm install --save xxhash-wasm

Or with Yarn:

yarn add xxhash-wasm

From Unpkg

ES Modules

<script type="module">
  import Xxhash from "https://unpkg.com/xxhash-wasm/esm/xxhash-wasm.js";
</script>

UMD build

<script src="https://unpkg.com/xxhash-wasm/umd/xxhash-wasm.js"></script>

The global Xxhash will be available.

Usage

The WebAssembly is contained in the JavaScript bundle, so you don't need to manually fetch it and create a new WebAssembly instance.

import Xxhash from "xxhash-wasm";

// Creates the WebAssembly instance.
const xxhash = new Xxhash();

const input = "The string that is being hashed";
// 32-bit version
xxhash.h32(input).then(h32 => console.log(h32)); // ee563564
// 64-bit version
xxhash.h64(input).then(h64 => console.log(h64)); // 502b0c5fc4a5704c

Node

This was initially meant for the browser, but Node 8 also added support for WebAssembly, so it can be run in Node as well. The implementation uses the browser API TextEncoder, which is has been added recently to Node as util.TextEncoder, but it is not a global. To compensate for that, a CommonJS bundle is created which automatically imports util.TextEncoder.

Note: You will see a warning that it's experimental, but it should work just fine.

The main field in package.json points to the CommonJS bundle, so you can require it as usual.

const Xxhash = require("xxhash-wasm");

// Or explicitly use the cjs bundle
const Xxhash = require("xxhash-wasm/cjs/xxhash-wasm");

If you want to bundle your application for Node with a module bundler that uses the module field in package.json, such as webpack or Rollup, you will need to explicitly import xxhash-wasm/cjs/xxhash-wasm otherwise the browser version is used.

API

const xxhash = new Xxhash()

Create a WebAssembly instance.

xxhash.h32(input: string, [seed: u32]): Promise<string>

Generate a 32-bit hash of input. The optional seed is a u32 and any number greater than the maximum (0xffffffff) is wrapped, which means that 0xffffffff + 1 = 0.

The returned promise resolves with the string of the hash in hexadecimal.

xxhash.h64(input: string, [seedHigh: u32, seedLow: u32]): Promise<string>

Generate a 64-bit hash of input. Because JavaScript doesn't support u64 the seed is split into two u32, where seedHigh represents the first 32-bits of the u64 and seedLow the remaining 32-bits. For example:

// Hex
seed64:   ffffffff22222222
seedhigh: ffffffff
seedLow:          22222222

// Binary
seed64:   1111111111111111111111111111111100100010001000100010001000100010
seedhigh: 11111111111111111111111111111111
seedLow:                                  00100010001000100010001000100010

Each individual part of the seed is a u32 and they are also wrapped individually for numbers greater than the maximum.

The returned promise resolves with the string of the hash in hexadecimal.