Initial commit

This commit is contained in:
LilyRose2798 2024-06-04 18:35:24 +10:00
commit 4652462526
9 changed files with 152 additions and 0 deletions

23
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,23 @@
name: test
on:
push:
branches:
- master
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.2.0"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump

28
README.md Normal file
View File

@ -0,0 +1,28 @@
# gzlib
[![Package Version](https://img.shields.io/hexpm/v/gzlib)](https://hex.pm/packages/gzlib)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gzlib/)
```sh
gleam add gzlib
```
```gleam
import gzlib
pub fn main() {
let data = <<1, 2, 3, 4, 5>>
let compressed_data = gzlib.compress(data)
let more_compressed_data = gzlib.compress_with_level(data, gzlib.max_compression)
let crc = gzlib.crc32(data)
}
```
Further documentation can be found at <https://hexdocs.pm/gzlib>.
## Development
```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```

15
gleam.toml Normal file
View File

@ -0,0 +1,15 @@
name = "gzlib"
version = "1.0.0"
description = "zlib utilities for Gleam"
licences = ["AGPL-3.0-only"]
repository = { type = "github", user = "LilyRose2798", repo = "gzlib" }
links = [
{ title = "7Circles Git", href = "https://git.7cs.dev/lily/gzlib" },
]
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

11
manifest.toml Normal file
View File

@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
]
[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }

42
src/gzlib.gleam Normal file
View File

@ -0,0 +1,42 @@
pub opaque type CompressionLevel {
CompressionLevel(level: Int)
}
pub const no_compression = CompressionLevel(0)
pub const min_compression = CompressionLevel(1)
pub const max_compression = CompressionLevel(9)
pub const default_compression = CompressionLevel(6)
pub fn compression_level(level: Int) -> Result(CompressionLevel, Nil) {
case level >= 0 && level <= 9 {
True -> Ok(CompressionLevel(level))
False -> Error(Nil)
}
}
@external(erlang, "zlib", "compress")
@external(javascript, "./gzlib_js.mjs", "compress")
pub fn compress(data: BitArray) -> BitArray
@external(erlang, "gzlib_erl", "compress")
@external(javascript, "./gzlib_js.mjs", "compress")
fn do_compress_with_level(data: BitArray, level: Int) -> BitArray
pub fn compress_with_level(data: BitArray, level: CompressionLevel) -> BitArray {
do_compress_with_level(data, level.level)
}
@external(erlang, "zlib", "uncompress")
@external(javascript, "./gzlib_js.mjs", "uncompress")
pub fn uncompress(data: BitArray) -> BitArray
@external(erlang, "erlang", "crc32")
@external(javascript, "./gzlib_js.mjs", "crc32")
pub fn crc32(data: BitArray) -> Int
@external(erlang, "erlang", "crc32")
@external(javascript, "./gzlib_js.mjs", "continueCrc32")
pub fn continue_crc32(init: Int, data: BitArray) -> Int

12
src/gzlib_erl.erl Normal file
View File

@ -0,0 +1,12 @@
-module(gleam_zlib_erl).
-export([compress/2]).
compress(Data, Level) ->
Z = zlib:open(),
ok = zlib:deflateInit(Z, Level),
Compressed = zlib:deflate(Z, Data),
Last = zlib:deflate(Z, [], finish),
ok = zlib:deflateEnd(Z),
zlib:close(Z),
list_to_binary([Compressed|Last]).

12
src/gzlib_js.js Normal file
View File

@ -0,0 +1,12 @@
import { deflateSync, inflateSync } from "node:zlib"
import { BitArray } from "./gleam.mjs"
export const compress = (data, level = undefined) => new BitArray(new Uint8Array(deflateSync(data.buffer, { level })))
export const uncompress = data => new BitArray(new Uint8Array(inflateSync(data.buffer)))
const crcTable = [...Array(256)].map((_, i) => [...Array(8)].reduce(c => ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)), i))
export const crc32 = (data, init = 0) => (data.buffer.reduce((crc, x) => (crc >>> 8) ^ crcTable[(crc ^ x) & 0xFF], init ^ (-1)) ^ (-1)) >>> 0
export const continueCrc32 = (init, str) => crc32(str, init)

5
test/gzlib_test.gleam Normal file
View File

@ -0,0 +1,5 @@
import gleeunit
pub fn main() {
gleeunit.main()
}