Install SnappyJS
masterYou can install SnappyJS using npm for Node.js projects or Bower for client-side projects.
npm install snappyjs
# or
bower install snappyjsrepository·master·Indexed 41 days ago
https://github.com/zhipeng-jia/snappyjsA pure JavaScript implementation of Google's Snappy compression library version 0.7.0. It supports ArrayBuffer, Buffer, and Uint8Array for efficient data handling across Node.js 10.x+ and modern browser environments. Provides SnappyJS.compress() and SnappyJS.uncompress() methods for data compression and decompression.
You can install SnappyJS using npm for Node.js projects or Bower for client-side projects.
npm install snappyjs
# or
bower install snappyjsdist/snappyjs.js or dist/snappyjs.min.js in your project. This will expose the SnappyJS object in the global scope. The library relies on ArrayBuffer, which is supported by all major modern browsers.If you are using webpack and only plan to use ArrayBuffer or Uint8Array as input parameters, you should disable the automatic Buffer polyfill in your webpack configuration to keep your bundle clean.
node: {
Buffer: false,
}SnappyJS is compatible with Node.js 10.x or later. It works with ArrayBuffer, Buffer, or Uint8Array types.
var SnappyJS = require('snappyjs')
var buffer = new ArrayBuffer(100)
// fill data in buffer
var compressed = SnappyJS.compress(buffer)
var uncompressed = SnappyJS.uncompress(compressed)Compresses the provided input.
Parameters:
input: Must be of type ArrayBuffer, Buffer, or Uint8Array.Returns:
A compressed byte stream of the same type as the input.
// Example usage
const compressed = SnappyJS.compress(inputBuffer);Decompresses the provided compressed data.
Parameters:
compressed: Must be of type ArrayBuffer, Buffer, or Uint8Array.maxLength (optional): An integer representing the maximum allowed length. If the data length encoded in the header exceeds this value, the function will throw an exception. This serves as a protection mechanism against malicious data streams.Returns:
The uncompressed byte stream of the same type as the compressed input.
// Example usage
const uncompressed = SnappyJS.uncompress(compressedBuffer, 1024 * 1024);Use SnappyJS.compress(uncompressed) to compress data. The input must be a Uint8Array, ArrayBuffer, or a Node.js Buffer. The function returns a new instance of the same type as the input, sliced to the actual compressed length.
const SnappyJS = require('snappyjs');
// Example with Uint8Array
const input = new Uint8Array([1, 2, 3, 4, 5]);
const compressed = SnappyJS.compress(input);Use SnappyJS.uncompress(compressed, [maxLength]) to decompress data.
compressed: Must be a Uint8Array, ArrayBuffer, or a Node.js Buffer.maxLength (optional): An integer representing the maximum expected uncompressed size. If the actual uncompressed length exceeds this value, an error is thrown. This is useful for preventing memory exhaustion attacks.The function returns a new instance of the same type as the input (e.g., if you pass a Buffer, you get a Buffer back).
const SnappyJS = require('snappyjs');
// Example with Uint8Array
const compressed = new Uint8Array([...]);
const uncompressed = SnappyJS.uncompress(compressed);
// Example with maxLength protection
try {
const uncompressed = SnappyJS.uncompress(compressed, 1024);
} catch (e) {
console.error(e.message);
}Both compress and uncompress require the input to be one of the following types:
Uint8ArrayArrayBufferBuffer (Node.js only)If the input does not match these types, a TypeError is thrown with the message: Argument compressed must be type of ArrayBuffer, Buffer, or Uint8Array (Note: the error message uses the word 'compressed' even for the compress function).
Common errors during decompression include:
Invalid Snappy bitstream: Thrown if the bitstream is corrupted or invalid.The uncompressed length of {length} is too big, expect at most {maxLength}: Thrown if the maxLength constraint is violated.