All Projects → cruise-automation → wasm-lz4

cruise-automation / wasm-lz4

Licence: Apache-2.0 license
https://github.com/lz4/lz4 compiled to WebAssembly.

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects
c
50402 projects - #5 most used programming language
Dockerfile
14818 projects

Labels

Projects that are alternatives of or similar to wasm-lz4

imagecodecs
Image transformation, compression, and decompression codecs. Forked from https://pypi.org/project/imagecodecs
Stars: ✭ 56 (+166.67%)
Mutual labels:  lz4
blz4
Example of LZ4 compression with optimal parsing using BriefLZ algorithms
Stars: ✭ 24 (+14.29%)
Mutual labels:  lz4
py-lz4framed
LZ4-frame library for Python (via C bindings)
Stars: ✭ 42 (+100%)
Mutual labels:  lz4
lz4ultra
Optimal LZ4 compressor, that produces files that decompress faster while keeping the best compression ratio
Stars: ✭ 49 (+133.33%)
Mutual labels:  lz4
7 Zip Zstd
7-Zip with support for Brotli, Fast-LZMA2, Lizard, LZ4, LZ5 and Zstandard
Stars: ✭ 2,150 (+10138.1%)
Mutual labels:  lz4
Lz4
Extremely Fast Compression algorithm
Stars: ✭ 6,623 (+31438.1%)
Mutual labels:  lz4
Messagepack Csharp
Extremely Fast MessagePack Serializer for C#(.NET, .NET Core, Unity, Xamarin). / msgpack.org[C#]
Stars: ✭ 3,668 (+17366.67%)
Mutual labels:  lz4
Archiver
Easily create & extract archives, and compress & decompress files of various formats
Stars: ✭ 3,373 (+15961.9%)
Mutual labels:  lz4
EasyCompressor
⚡ A compression library that implements many compression algorithms such as LZ4, Zstd, LZMA, Snappy, Brotli, GZip, and Deflate. It helps you to improve performance by reducing Memory Usage and Network Traffic for caching.
Stars: ✭ 167 (+695.24%)
Mutual labels:  lz4
smallz4
Optimal LZ4 compression
Stars: ✭ 24 (+14.29%)
Mutual labels:  lz4
lz4-napi
Fastest lz4 compression library in Node.js, powered by napi-rs and lz4-flex.
Stars: ✭ 29 (+38.1%)
Mutual labels:  lz4
pyrus-cramjam
Thin Python wrapper to de/compression algorithms in Rust - lightweight & no dependencies
Stars: ✭ 40 (+90.48%)
Mutual labels:  lz4
ccxx
This is a cross-platform library software library about c, c ++, unix4, posix. Include gtest, benchmark, cmake, process lock, daemon, libuv, lua, cpython, re2, json, yaml, mysql, redis, opencv, qt, lz4, oci ... https://hub.docker.com/u/oudream
Stars: ✭ 31 (+47.62%)
Mutual labels:  lz4
zstdmt
Multithreading Library for Brotli, Lizard, LZ4, LZ5, Snappy and Zstandard
Stars: ✭ 107 (+409.52%)
Mutual labels:  lz4
tiny
compress data for better performance
Stars: ✭ 21 (+0%)
Mutual labels:  lz4
wasm
fast wasm modules
Stars: ✭ 37 (+76.19%)
Mutual labels:  lz4

wasm-lz4

https://github.com/lz4/lz4 compiled to WebAssembly. For now only decompression is supported. PRs welcome!

WASM compilation has been tested with Emscripten SDK 2.0.23.

API

wasm-lz4 exports a single function:

export default (buffer: Buffer, destinationByteSize: number) => Buffer

Here is an example of using the module:

import fs from 'fs'
import decompress from 'wasm-lz4';

const compressedBytes = fs.readFileSync('compressed.lz4');

// currently you need to know the exact expected size of the output buffer
// so the wasm runtime can allocate enough bytes to decompress into
// TODO: this should not be necessary...it's exposed in the lz4 C code
const outputBytes = compressedBytes * 10;
const decompressedBytes = decompress(compressedBytes, outputBytes);

Using the module in a browser

Emscripten compiled WebAssembly modules are built in 2 parts: a .js side and a .wasm side. In the browser the .js side needs to download the .wasm side from the server so it can compile it. There is more information available in the emscripten documentation.

Usage with Webpack

We require the wasm-lz4.wasm module in our locateFile definition, so that module bundling with dynamic paths is possible. So if you are using Webpack, you can add the following entry:

...
rules : [
  ...
  {
    test: /\.wasm$/,
    type: "javascript/auto",
    use: {
      loader: "file-loader",
      options: {
        name: "[name]-[hash].[ext]",
      },
    },
  },
  ...
]
...

The javascript/auto type setting tells Webpack to bypass its default importing logic, and just import the file as-is. Hopefully this hack will go away with improved WebAssembly support in webpack.

Asynchronous loading & compiling

After the .wasm binary is loaded (via fetch in the browser or require in node) it must be compiled by the WebAssembly runtime. If you call decompress before it is finished compiling it will throw an error indicating it isn't ready yet. To wait for the module to be loaded you can do something like the following:

import decompress from 'wasm-lz4'

async function doWork() {
  await decompress.isLoaded;
  decompress(buffer, outputByteLength);
}

Developing locally

Building

Step 1

First, and most importantly, you need to install emscripten and activate it into your terminal environment.

IMPORTANT: For now we only support emscripten 2.0.23.

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install sdk-2.0.23-64bit
./emsdk activate sdk-2.0.23-64bit
source ./emsdk_env.sh

Step 2

Next, clone the module recursively as it includes lz4 as a git submodule:

$ git clone [email protected]:cruise-automation/wasm-lz4.git --recursive

Step 3

Run npm run build to invoke emcc and compile the code in wasm-lz4.c as well as the required lz4 source files from the lz4 git submodule.

Step 4

To run the tests, run npm install followed by npm test.

To run the tests in Docker, first make sure Docker is installed, and then run npm run docker:test.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].