Registry / serialization / snappyjs

snappyjs

JSON →
library0.7.0jsnpmunverified

SnappyJS is a pure JavaScript implementation of Google's Snappy compression algorithm, designed for both Node.js and browser environments. The current stable version is 0.7.0. While there isn't an explicit, regular release cadence, updates appear to be driven by feature additions and maintenance, as indicated by the recent minor version bumps (e.g., 0.7.0, 0.6.1, 0.5.0). Its primary differentiator is being entirely written in JavaScript, leveraging `ArrayBuffer` for efficient handling of binary data. This allows it to run without native dependencies, making it highly suitable for web applications and environments where native modules might be problematic or undesirable. Benchmarks suggest that it achieves approximately 35%-45% of the performance of native Snappy implementations for larger inputs in Node.js, though it can occasionally outperform native solutions for very small input sizes due to lower FFI (Foreign Function Interface) overhead. It's a reliable choice for client-side compression needs or Node.js projects prioritizing pure JS solutions.

npm install snappyjs
INSTALL
IMPORT
SIG · SNAPPYJS
S
snappyjs
serializationjavascriptv0.7.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SnappyJS
import SnappyJS from 'snappyjs'
import { SnappyJS } from 'snappyjs'
For CommonJS environments, use 'const SnappyJS = require("snappyjs")'. The library primarily exports a single object containing 'compress' and 'uncompress' methods.
compress
import SnappyJS from 'snappyjs'; const compressed = SnappyJS.compress(input);
import { compress } from 'snappyjs'
The 'compress' function is a method of the main SnappyJS object. Ensure you import the default SnappyJS object first.
uncompress
import SnappyJS from 'snappyjs'; const uncompressed = SnappyJS.uncompress(compressed, maxLength);
import { uncompress } from 'snappyjs'
The 'uncompress' function is a method of the main SnappyJS object. It supports an optional 'maxLength' parameter for security since v0.7.0.

Demonstrates how to import SnappyJS, convert a string to an `ArrayBuffer`, then compress and uncompress the data, including verification and using the `maxLength` security feature.

import * as SnappyJS from 'snappyjs'; // Example data: a simple text string converted to a Uint8Array const text = "Hello, SnappyJS! This is a test string to demonstrate compression and decompression."; const encoder = new TextEncoder(); const originalData = encoder.encode(text); // SnappyJS expects ArrayBuffer, Buffer, or Uint8Array. // Uint8Array's .buffer property provides the underlying ArrayBuffer. const inputBuffer = originalData.buffer; console.log(`Original text: "${text}"`); console.log('Original size:', inputBuffer.byteLength, 'bytes'); try { // Compress the data const compressed = SnappyJS.compress(inputBuffer); console.log('Compressed size:', compressed.byteLength, 'bytes'); // Uncompress the data // Using maxLength as a security measure, estimating twice the original size as a safe upper bound. const estimatedMaxLength = inputBuffer.byteLength * 2; const uncompressed = SnappyJS.uncompress(compressed, estimatedMaxLength); console.log('Uncompressed size:', uncompressed.byteLength, 'bytes'); // Convert back to string to verify const decoder = new TextDecoder(); const decompressedText = decoder.decode(uncompressed); console.log('Decompressed text:', decompressedText); if (text === decompressedText) { console.log("Compression and decompression successful and data integrity maintained!"); } else { console.error("Error: Data mismatch after compression/decompression!"); } } catch (error) { console.error("An error occurred during SnappyJS operation:", error.message); }
Debug
Known issues
gotchaWhen bundling `snappyjs` for browser environments using webpack, explicitly disable the Node.js `Buffer` polyfill to prevent unnecessary code from being included in your bundle. This is crucial if you intend to only use `ArrayBuffer` or `Uint8Array` as input parameters.
fix
Add `node: { Buffer: false }` to your webpack configuration object to prevent automatic `Buffer` polyfilling.
affects: >=0.1.0
gotchaWhile SnappyJS is optimized JavaScript, its performance for larger inputs (e.g., >100KB) typically reaches only 35-45% of native Snappy implementations. For extremely performance-critical Node.js applications, consider native C++ bindings if cross-platform pure JS isn't a strict requirement.
fix
Evaluate your application's performance needs; use native Snappy bindings (e.g., `node-snappy`) for maximum speed in Node.js, or accept the pure JavaScript performance for browser compatibility and simpler deployments.
affects: >=0.1.0
gotchaSince v0.7.0, the `uncompress` function accepts an optional `maxLength` parameter. It is highly recommended to provide this parameter as a protection mechanism against malicious or malformed data streams that could attempt to exhaust memory by declaring an excessively large uncompressed size.
fix
Always call `SnappyJS.uncompress(compressedData, expectedMaxLength)` and provide a reasonable upper bound for the uncompressed data size based on your application's expectations.
affects: >=0.7.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'compress')
The SnappyJS module was not correctly imported or required, or the 'compress' method was called on an undefined object.
fix
Ensure you are importing the default export (e.g., `import SnappyJS from "snappyjs"`) or correctly requiring the module (e.g., `const SnappyJS = require("snappyjs")`) before calling its methods.
ReferenceError: Buffer is not defined
When bundling for a browser environment with webpack, the Node.js `Buffer` global is not available, and webpack may implicitly attempt to polyfill it, failing if not configured correctly.
fix
Add `node: { Buffer: false }` to your webpack configuration object to explicitly prevent `Buffer` polyfilling, especially if you only intend to use `ArrayBuffer` or `Uint8Array`.
Error: Input must be type of ArrayBuffer, Buffer, or Uint8Array
The input provided to `SnappyJS.compress` or `SnappyJS.uncompress` is not one of the supported binary data types.
fix
Convert your data into an `ArrayBuffer`, `Buffer` (Node.js only), or `Uint8Array` before passing it to SnappyJS compression/decompression functions.
RangeError: Offset is out of bounds
This error typically occurs during decompression if the input `compressed` data stream is corrupted, malformed, or does not conform to the Snappy format. It can also be triggered if the `maxLength` parameter in `uncompress` is exceeded by the data's internal header.
fix
Verify the integrity of your compressed data. If using `uncompress(data, maxLength)`, ensure `maxLength` is set appropriately to prevent unexpected data sizes from causing an error.
Upgrade
Version history
0.7.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
snappyjs — npm install snappyjs · libregistry