diff --git a/pkg/.gitignore b/pkg/.gitignore new file mode 100644 index 0000000..d26296c --- /dev/null +++ b/pkg/.gitignore @@ -0,0 +1 @@ +wasm_* \ No newline at end of file diff --git a/pkg/README.md b/pkg/README.md new file mode 100644 index 0000000..bf1e02c --- /dev/null +++ b/pkg/README.md @@ -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); +}); +``` diff --git a/pkg/package.json b/pkg/package.json new file mode 100644 index 0000000..9a3d411 --- /dev/null +++ b/pkg/package.json @@ -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" +} diff --git a/pkg/tests/browser.html b/pkg/tests/browser.html new file mode 100644 index 0000000..589e645 --- /dev/null +++ b/pkg/tests/browser.html @@ -0,0 +1,11 @@ + + + + + + Tests + + + + + \ No newline at end of file diff --git a/pkg/tests/browser.js b/pkg/tests/browser.js new file mode 100644 index 0000000..513deaf --- /dev/null +++ b/pkg/tests/browser.js @@ -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."); +});