High-performance hashing utility providing xxHash-based functions for strings and buffers
npx @tessl/cli install tessl/npm-parcel--hash@2.9.0@parcel/hash is a high-performance hashing utility designed for the Parcel bundler ecosystem. It provides xxHash (XXH3) implementations through dual execution environments: a JavaScript version using xxhash-wasm for browser compatibility and a native Rust implementation using xxhash-rust with napi-rs bindings for Node.js environments.
npm install @parcel/hashconst { hashString, hashBuffer, Hash, init } = require('@parcel/hash');For ES modules:
import { hashString, hashBuffer, Hash, init } from '@parcel/hash';const { hashString, hashBuffer, Hash } = require('@parcel/hash');
// Hash a string
const stringHash = hashString('hello world');
console.log(stringHash); // '0123456789abcdef' (16-char hex)
// Hash a buffer
const buffer = Buffer.from('hello world', 'utf8');
const bufferHash = hashBuffer(buffer);
console.log(bufferHash); // '0123456789abcdef' (16-char hex)
// Incremental hashing
const hash = new Hash();
hash.writeString('hello ');
hash.writeString('world');
const result = hash.finish();
console.log(result); // '0123456789abcdef' (16-char hex)import { hashString, hashBuffer, Hash, init } from '@parcel/hash';
// Initialize the WASM module first
await init;
// Hash a string
const stringHash = hashString('hello world');
console.log(stringHash); // '0123456789abcdef' (16-char hex)
// Hash a Uint8Array
const buffer = new TextEncoder().encode('hello world');
const bufferHash = hashBuffer(buffer);
console.log(bufferHash); // '0123456789abcdef' (16-char hex)
// Incremental hashing
const hash = new Hash();
hash.writeString('hello ');
hash.writeString('world');
const result = hash.finish();
console.log(result); // '0123456789abcdef' (16-char hex)@parcel/hash implements a dual-target strategy:
Hash string inputs using the xxHash3 algorithm.
/**
* Hash a string using xxHash3 algorithm
* @param s - The input string to hash
* @returns 16-character hexadecimal hash string
*/
function hashString(s: string): string;Hash binary data using the xxHash3 algorithm.
/**
* Hash a buffer using xxHash3 algorithm
* @param b - The input buffer to hash (Buffer in Node.js, Uint8Array in browser)
* @returns 16-character hexadecimal hash string
*/
function hashBuffer(b: Buffer | Uint8Array): string;Build hashes incrementally from multiple inputs using the Hash class.
/**
* Hash class for incremental hashing operations
*/
class Hash {
/**
* Create a new Hash instance
*/
constructor();
/**
* Add a string to the incremental hash
* @param s - String to add to the hash
*/
writeString(s: string): void;
/**
* Add a buffer to the incremental hash
* @param b - Buffer to add to the hash (Buffer in Node.js, Uint8Array in browser)
*/
writeBuffer(b: Buffer | Uint8Array): void;
/**
* Finalize the incremental hash and return the result
* @returns 16-character hexadecimal hash string
*/
finish(): string;
}WebAssembly module initialization for browser environments.
/**
* Promise that resolves when the WASM module is initialized (browser only)
* Must be awaited before using other functions in browser environment
*/
const init: Promise<void>;const { hashString, hashBuffer } = require('@parcel/hash');
// Hash strings
const hash1 = hashString('user123');
const hash2 = hashString('file-content-data');
// Hash buffers
const jsonData = Buffer.from(JSON.stringify({id: 123, name: 'test'}));
const jsonHash = hashBuffer(jsonData);
const binaryData = new Uint8Array([0xFF, 0x00, 0xAB, 0xCD]);
const binaryHash = hashBuffer(binaryData);const { Hash } = require('@parcel/hash');
// Build hash from multiple components
const hash = new Hash();
hash.writeString('module:');
hash.writeString(moduleName);
hash.writeString(':version:');
hash.writeString(moduleVersion);
hash.writeBuffer(moduleContent);
const finalHash = hash.finish();import { init, hashString, Hash } from '@parcel/hash';
async function computeHashes() {
// Wait for WASM initialization
await init;
// Now safe to use hashing functions
const quickHash = hashString('fast-computation');
const incrementalHash = new Hash();
incrementalHash.writeString('browser-');
incrementalHash.writeString('compatible');
const result = incrementalHash.finish();
return { quickHash, result };
}In browser environments, ensure the WASM module initializes successfully:
import { init, hashString } from '@parcel/hash';
try {
await init;
const hash = hashString('data');
} catch (error) {
console.error('Failed to initialize WASM module:', error);
// Fallback to alternative hashing if needed
}Native bindings should load automatically. If native bindings fail to load, the package will fall back to the browser implementation:
const { hashString } = require('@parcel/hash');
try {
const hash = hashString('data');
} catch (error) {
console.error('Hashing failed:', error);
}