Skip to content

Commit

Permalink
feat: 🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed May 29, 2020
0 parents commit b91c5b2
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ide
.idea

# cargo build
target/

# wasm-pack
pkg/
187 changes: 187 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "deno_lz4"
description = "lz4 wasm module for deno"
repository = "https://github.com/denosaurs/deno_lz4"
license = "MIT"
version = "0.1.0"
authors = ["Elias SJögreen"]
edition = "2018"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
lz4-compression = "0.6.1"
wasm-bindgen = "0.2.63"
wee_alloc = { version = "0.4.5", optional = true }

[profile.release]
lto = true
opt-level = "z"
codegen-units = 1

[features]
default = ["wee_alloc"]
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) 2020-present the denosaurs team

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.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# deno_lz4

[![stars](https://img.shields.io/github/stars/denosaurs/deno_lz4)](https://github.com/denosaurs/deno_lz4/stargazers)
[![workflow](https://img.shields.io/github/workflow/status/denosaurs/deno_lz4/ci)](https://github.com/denosaurs/deno_lz4/actions)
[![releases](https://img.shields.io/github/v/release/denosaurs/deno_lz4)](https://github.com/denosaurs/deno_lz4/releases/latest/)
[![deno version](https://img.shields.io/badge/deno-^1.0.2-informational)](https://github.com/denoland/deno)
[![deno doc](https://img.shields.io/badge/deno-doc-informational)](https://doc.deno.land/https/deno.land/x/deno_lz4/mod.ts)
[![Discord](https://img.shields.io/discord/713043818806509608)](https://discord.gg/shHG8vg)
[![license](https://img.shields.io/github/license/denosaurs/deno_lz4)](https://github.com/denosaurs/deno_lz4/blob/master/LICENSE)
[![issues](https://img.shields.io/github/issues/denosaurs/deno_lz4)](https://github.com/denosaurs/deno_lz4/issues)

This module provides [lz4](https://en.wikipedia.org/wiki/LZ4_(compression_algorithm)) support for deno and the web by providing [simple bindings](src/lib.rs) using [lz4-compression](https://github.com/johannesvollmer/lz4-compression-rs) compiled to webassembly.

## Usage

### Compression

```ts
import { compress } from "https://deno.land/x/lz4/mod.ts";
const text = new TextEncoder().encode("X".repeat(64));
console.log(text.length); // 64 Bytes
console.log(compress(text).length); // 6 Bytes
```

### Decompression

```ts
import { decompress } from "https://deno.land/x/lz4/mod.ts";
const compressed = Uint8Array.from([ 31, 88, 1, 0, 44, 0 ]);
console.log(compressed.length); // 6 Bytes
console.log(decompress(compressed).length); // 64 Bytes
```

## Other

### Contribution

Pull request, issues and feedback are very welcome. Code style is formatted with `deno fmt` and commit messages are done following [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec.

### Licence

Copyright 2020-present, the denosaurs team. All rights reserved. MIT license.
42 changes: 42 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.

import init, {
source,
lz4_compress,
lz4_decompress,
} from "./wasm.js";

await init(source);

/**
* Compress a byte array using lz4.
*
* ```typescript
* import { compress } from "https://deno.land/x/lz4/mod.ts";
* const text = new TextEncoder().encode("X".repeat(64));
* console.log(text.length); // 64 Bytes
* console.log(compress(text).length); // 6 Bytes
* ```
*
* @param input Input data.
*/
export function compress(input: Uint8Array): Uint8Array {
return lz4_compress(input);
}

/**
* Decompress a byte array using lz4.
*
* ```typescript
* import { decompress } from "https://deno.land/x/lz4/mod.ts";
* const compressed = Uint8Array.from([ 31, 88, 1, 0, 44, 0 ]);
* console.log(compressed.length); // 6 Bytes
* console.log(decompress(compressed).length); // 64 Bytes
* ```
*
* @param input Input data.
* @param bufferSize Read buffer size
*/
export function decompress(input: Uint8Array): Uint8Array {
return lz4_decompress(input);
}
53 changes: 53 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.

import { encode } from "https://deno.land/std/encoding/base64.ts";
import Terser from "https://cdn.pika.dev/terser@^4.7.0";

const encoder = new TextEncoder();

const toml = await Deno.stat("Cargo.toml");
if (!toml.isFile) {
console.log(
`[!] Error: the build script should be executed in the "deno_lz4" root`,
);
Deno.exit(1);
}

console.log("[!] building using wasm-pack");
const pack = Deno.run(
{ cmd: ["wasm-pack", "build", "--target", "web", "--release"] },
);
await pack.status();

console.log("[!] inlining wasm in js");
const wasm = await Deno.readFile("pkg/deno_lz4_bg.wasm");

const source =
`export const source = Uint8Array.from(atob("${
encode(wasm)
}"), c => c.charCodeAt(0));`;

const init = await Deno.readTextFile("pkg/deno_lz4.js");

const output = Terser.minify(`${source}\n${init}`, {
mangle: {
module: true,
reserved: ["decode"]
},
output: {
preamble: "//deno-fmt-ignore-file"
}
});
console.log(
`[!] minified js, size reduction: ${new Blob([(`${source}\n${init}`)]).size -
new Blob([output.code]).size} bytes`,
);

console.log(`[!] writing output to file ("wasm.js")`);
await Deno.writeFile(
"wasm.js",
encoder.encode(output.code),
);

const outputFile = await Deno.stat("wasm.js");
console.log(`[!] output file ("wasm.js") size is: ${outputFile.size} bytes`);

0 comments on commit b91c5b2

Please sign in to comment.