Skip to content

Commit

Permalink
compression stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Kero van Gelder committed Mar 7, 2024
1 parent 82ec69b commit bfa2794
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
1 change: 0 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ gleam_json = "~> 0.7 or ~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
simplifile = "~> 1.5"
2 changes: 0 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ packages = [
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "simplifile", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "EB9AA8E65E5C1E3E0FDCFC81BC363FD433CB122D7D062750FFDF24DE4AC40116" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

Expand All @@ -15,4 +14,3 @@ gleam_javascript = { version = "~> 0.4" }
gleam_json = { version = "~> 0.7 or ~> 1.0" }
gleam_stdlib = { version = "~> 0.29" }
gleeunit = { version = "~> 1.0" }
simplifile = { version = "~> 1.5" }
11 changes: 11 additions & 0 deletions src/compression_stream_ffi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { toBitArray } from "../gleam_stdlib/gleam.mjs"

export const compress = async (data, encoding) => {
const ds = new CompressionStream(encoding);
const blob = new Blob([data.buffer])
const compressedStream = blob.stream().pipeThrough(ds)

const compressed = await new Response(compressedStream).blob()
const ab = await compressed.arrayBuffer()
return toBitArray(new Uint8Array(ab))
}
6 changes: 6 additions & 0 deletions src/plinth/javascript/compression_stream.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import gleam/javascript/promise.{type Promise}

/// For encoding, pass "gzip" or "deflate"
@target(javascript)
@external(javascript, "../../compression_stream_ffi.mjs", "compress")
pub fn compress(data: BitArray, encoding: String) -> Promise(BitArray)
28 changes: 22 additions & 6 deletions test/gunzip_test.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import gleam/bit_array
import gleam/javascript/promise
import gleeunit/should
import simplifile
import plinth/javascript/compression_stream.{compress}
import plinth/javascript/decompression_stream.{decompress}

pub fn gunzip_test() {
let assert Ok(gzipped_data) = simplifile.read_bits("test/hello.txt.gz")
pub fn gzip_test() {
use data <- promise.await(compress(hello(), "gzip"))
data
|> should.equal(gzipped_hello())

promise.resolve(Ok(Nil))
}

use data <- promise.await(decompress(gzipped_data, "gzip"))
pub fn gunzip_test() {
use data <- promise.await(decompress(gzipped_hello(), "gzip"))
data
|> bit_array.to_string
|> should.equal(Ok("Hello, world!\n"))
|> should.equal(hello())

promise.resolve(Ok(Nil))
}

fn hello() {
bit_array.from_string("Hello")
}

fn gzipped_hello() {
<<
31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 243, 72, 205, 201, 201, 7, 0, 130, 137, 209,
247, 5, 0, 0, 0,
>>
}
Binary file removed test/hello.txt.gz
Binary file not shown.

0 comments on commit bfa2794

Please sign in to comment.