Skip to content

Commit

Permalink
Fixed NPM Package
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinTimBarndt committed Jan 9, 2021
1 parent d1c8149 commit a0b6d86
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wasm_*
74 changes: 74 additions & 0 deletions pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Wasm-Gzip

Wasm-Gzip was built for the web and does not work with NodeJS.

This small library allows compression and decompression with Gzip
using the [libflate](https://crates.io/crates/libflate) Rust library.
It also comes with functions to convert a string into UTF-8 bytes and
back. The binary WASM is very lightweight (~125 KiB) which may be useful
to compress network traffic or for web applications that let a user save
and load files.

You can see the Rust source code on [GitHub](https://github.com/ColinTimBarndt/wasm-gzip).

## Examples

### Compress binary data

In this example, binary data in a Uint8Array is compressed and decompressed.

```js
import init, { compressGzip, decompressGzip } from "./wasm_gzip.js";

let inputData = new Uint8Array([
0x0a,
0x83,
0xd8,
0x4a,
0x84,
0x0d,
0x44,
0x02,
0xbb,
0x1f,
0xcd,
0x33,
0x24,
0x8e,
0x49,
0x99,
]);

// Load the WASM
init().then(() => {
let compressed = compressGzip(inputData);
console.log(compressed);

// If decompressing fails, undefined is returned.
let decompressed = decompressGzip(compressed);
console.log(decompressed, inputData);
});
```

### Compress text

```js
import init, { compressStringGzip, decompressStringGzip } from "./wasm_gzip.js";

let text = `\
Exercitationem placeat consequatur cumque sint qui et tenetur. Quaerat vel ratione
vitae a perspiciatis consectetur numquam. Dicta illo ducimus explicabo molestiae
assumenda. Quisquam reiciendis aspernatur in ullam. Quia magnam in itaque quaerat
quisquam ipsa voluptatem.
`.repeat(10);

// Load the WASM
init().then(() => {
let compressed = compressStringGzip(text);
console.log(text.length, compressed.length);

// If decompressing fails, undefined is returned.
let reverse = decompressStringGzip(compressed);
console.log(reverse == text);
});
```
29 changes: 29 additions & 0 deletions pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "wasm-gzip",
"version": "0.1.2",
"description": "Compress data on the web using Webassembly and Rust.",
"main": "wasm_gzip.js",
"directories": {
"test": "tests"
},
"files": [
"tests",
"README.md",
"wasm_gzip_bg.js",
"wasm_gzip_bg.wasm",
"wasm_gzip_bg.wasm.d.ts",
"wasm_gzip.d.ts",
"wasm_gzip.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"wasm",
"gzip",
"compression"
],
"author": "Colin Tim Barndt",
"license": "Apache-2.0",
"repository": "github:ColinTimBarndt/wasm-gzip"
}
11 changes: 11 additions & 0 deletions pkg/tests/browser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tests</title>
</head>
<body>
<script src="./browser.js" type="module"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions pkg/tests/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import init, * as Zip from "../wasm_gzip.js";

Object.defineProperty(window, "Zip", { value: Zip, writable: false });

init().then(() => {
const inputText = `\
Modi est dolor saepe nobis. Blanditiis mollitia ut et corporis voluptatem. Numquam repudiandae quas reiciendis sit vel sed est. Explicabo rerum minima et et aut magni. Voluptatum assumenda doloribus sint.
Libero voluptate ut sit sit. Tempora possimus accusamus ut numquam quo in fugit deserunt. Voluptas nihil dolores excepturi.
Qui velit et dolores. Nemo et corrupti voluptas culpa omnis quo ducimus aut. Molestiae distinctio ut ullam accusamus.
In eum saepe atque doloribus qui fugit ut quaerat. Veritatis sint praesentium natus enim voluptate recusandae. Ratione nemo ipsum deserunt. Eligendi velit eaque aut at non ut placeat. Qui nemo quo quidem nisi quibusdam ut. Dolorum eveniet odit quisquam.
Quaerat officia rem voluptatibus corrupti vel aut corporis. Dolor earum hic quibusdam et omnis totam. Voluptas dolores consequatur soluta aut tenetur. Odit quidem consequatur provident.
Sed eum cum nam dicta dolor. Velit consectetur et sapiente animi quo est adipisci sed. Repudiandae et hic neque sit nemo accusantium quas. Ipsum vero nesciunt assumenda.
Commodi sint dolores molestias sed repudiandae repellat qui. Quo labore minus nulla. Reiciendis reiciendis vel fuga ullam placeat. Ea cumque distinctio ut soluta ipsum cumque quia ea.
Magnam est est autem laborum non. Similique cupiditate fuga eveniet placeat voluptatem odio. Impedit saepe consequatur blanditiis ad velit qui culpa asperiores. Exercitationem qui impedit dolores ut dolorem voluptatem culpa et. Laudantium aut quibusdam iusto. Cumque exercitationem debitis exercitationem molestias vel ex expedita eos.
Qui ut cupiditate exercitationem quia. Enim aut repellat ad. Possimus et numquam quia.
Atque iste aperiam adipisci quia eos. Perferendis sunt dignissimos omnis repellat commodi sit qui quia. Magnam beatae nostrum temporibus. Sint fugit ab ullam.`;
const uncompressed = Zip.stringToUtf8(inputText);
const compressed = Zip.compressGzip(uncompressed).slice(0);
const result = Zip.decompressGzip(compressed).slice(0);
const outputText = Zip.utf8ToString(result);
console.table({ inputText, outputText });
console.log("Uncompressed size:", uncompressed.length);
console.log("Compressed size:", compressed.length);

if (outputText != inputText) throw new Error("Test failed.");
});

0 comments on commit a0b6d86

Please sign in to comment.