Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
klausenbusk committed Jul 26, 2020
0 parents commit 2169b98
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: build

on: push

jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Install Emscripten
uses: mymindstorm/setup-emsdk@v6
with:
# 1.39.{19,20} hit a bug in clang: https://bugs.llvm.org/show_bug.cgi?id=46786
version: 1.39.18
- name: Verify
run: emcc -v
- uses: actions/checkout@v2
with:
submodules: true
- name: make
run: make
- uses: actions/upload-artifact@v2
with:
name: dist
path: dist/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "zstd"]
path = zstd
url = https://github.com/facebook/zstd.git
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Kristian Klausen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC := emcc
CFLAGS = -O3 \
-s WASM=1 \
-s EXPORTED_FUNCTIONS="['_ZSTD_compress','_ZSTD_compressBound','_ZSTD_getFrameContentSize','_ZSTD_decompress','_ZSTD_isError','_ZSTD_getErrorName','_malloc','_free']" \
-s EXTRA_EXPORTED_RUNTIME_METHODS="['cwrap','stringToUTF8','UTF8ToString','lengthBytesUTF8']" \
-s ALLOW_MEMORY_GROWTH=1 \
-s MODULARIZE=1 \
-s EXPORT_NAME="'ZSTD'" \
--pre-js pre.js

build:
cd zstd && CC=gcc emmake make lib-release
exit
mkdir -p dist
$(CC) $(CFLAGS) "zstd/lib/libzstd.so" -o "dist/zstd.js"

clean:
rm -rf dist/
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*This is mostly a POC and isn't used in production, you have been warned!*

[`zstd`](https://github.com/facebook/zstd/) in the browser, because why not? ;)

## Build
```
$ git clone --recurse-submodules https://github.com/klausenbusk/zstd.js.git
$ cd zstd.js
$ make
```

## Usage
```javascript
const zstd = await ZSTD();

zstd.compress(<Uint8Array>, <compressionLevel>) <Uint8Array>
zstd.decompress(<Uint8Array>) <Uint8Array>
zstd.compressString(<string>) <Uint8Array>
zstd.decompressString(<Uint8Array>) <string>
```
See also: [example.html](./example.html)
14 changes: 14 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script type="text/javascript" src="dist/zstd.js"></script>
<script>
let zstd;
ZSTD().then(instance => {
zstd = instance;
test();
});

function test() {
let buffer = zstd.compressString("Hello World");
console.log(buffer);
console.log(zstd.decompressString(buffer.buffer));
}
</script>
107 changes: 107 additions & 0 deletions pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
let ZSTD_compressBound,ZSTD_compress,ZSTD_getFrameContentSize,ZSTD_decompress,ZSTD_isError,ZSTD_getErrorName;

Module.preRun = function() {
ZSTD_compressBound = cwrap("ZSTD_compressBound", "number", ["number"]);
ZSTD_compress = cwrap("ZSTD_compress", "number", ["number", "number", "number", "number", "number"]);
ZSTD_getFrameContentSize = cwrap("ZSTD_getFrameContentSize", "number", ["number", "number"]);
ZSTD_decompress = cwrap("ZSTD_decompress", "number", ["number", "number", "number", "number"]);
ZSTD_isError = cwrap("ZSTD_isError", "number", ["number"]);
ZSTD_getErrorName = cwrap("ZSTD_getErrorName", "string", ["number"]);
};

function copy(src) {
return new Uint8Array(src);
}

// https://github.com/facebook/zstd/blob/15c5e200235edc520c1bd678ed126a6dd05736e1/examples/simple_compression.c#L17
function compress(iSize, iBuff, compressionLevel) {
let cBuffSize = ZSTD_compressBound(iSize);
let cBuff = _malloc(cBuffSize);

let cSize = ZSTD_compress(cBuff, cBuffSize, iBuff, iSize, compressionLevel);
if (ZSTD_isError(cSize)) {
_free(iBuff);
_free(cBuff);
throw new Error(ZSTD_getErrorName(cSize));
}

let cData = new Uint8Array(HEAPU8.buffer, cBuff, cSize);
cData = copy(cData);

_free(iBuff);
_free(cBuff);

return cData;
};

Module.compress = function(input, compressionLevel = 3) {
if (!(input instanceof Uint8Array)) {
throw new TypeError();
}

let iSize = input.byteLength;
let iBuff = _malloc(iSize);
HEAPU8.set(new Uint8Array(input), iBuff);

return compress(iSize, iBuff, compressionLevel);
};

Module.compressString = function(input, compressionLevel) {
// https://stackoverflow.com/a/203757
if (typeof input !== "string") {
throw new TypeError();
}

let iSize = lengthBytesUTF8(input)+1;
let iBuff = _malloc(iSize);
stringToUTF8(input, iBuff, iSize);

return compress(iSize, iBuff, compressionLevel);
};

// https://github.com/facebook/zstd/blob/15c5e200235edc520c1bd678ed126a6dd05736e1/examples/simple_decompression.c#L16
function decompress(input) {
if (!(input instanceof Uint8Array)) {
throw new TypeError();
}

let cSize = input.byteLength;
let cBuff = _malloc(cSize);
HEAPU8.set(new Uint8Array(input), cBuff);

rSize = ZSTD_getFrameContentSize(cBuff, cSize);
if (rSize < 0) {
_free(cBuff);
throw new Error(rSize);
}

let rBuff = _malloc(rSize);
let dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
if (ZSTD_isError(dSize)) {
_free(cBuff);
_free(rBuff);
throw new Error(ZSTD_getErrorName(cSize));
}
_free(cBuff);

return [rBuff, dSize];
};

Module.decompress = function(input) {
[rBuff, dSize] = decompress(input);

let cData = new Uint8Array(HEAPU8.buffer, rBuff, dSize);
cData = copy(cData);
_free(rBuff);

return cData;
};

Module.decompressString = function(input) {
[rBuff, dSize] = decompress(input);

let s = UTF8ToString(rBuff, dSize);
_free(rBuff);

return s;
};
1 change: 1 addition & 0 deletions zstd
Submodule zstd added at 15c5e2

0 comments on commit 2169b98

Please sign in to comment.