SnappyJS

repository·master·Indexed 41 days ago

https://github.com/zhipeng-jia/snappyjs

A 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.

Tokens
1.2K
Snippets
7
Records
9
Agent score
1%

What's inside SnappyJS

  1. Use SnappyJS in the Browser

    master
    To use SnappyJS in a browser, include dist/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.
  2. Configure webpack to prevent Buffer polyfill

    master

    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,
    }
  3. Use SnappyJS in Node.js

    master

    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)
  4. SnappyJS.compress(input)

    master

    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);
  5. SnappyJS.uncompress(compressed, [maxLength])

    master

    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);
  6. Compress data with SnappyJS.compress()

    master

    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);
  7. Decompress data with SnappyJS.uncompress()

    master

    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);
    }
  8. Supported input types and error handling

    master

    Both compress and uncompress require the input to be one of the following types:

    • Uint8Array
    • ArrayBuffer
    • Buffer (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.