-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kero van Gelder
committed
Mar 7, 2024
1 parent
82ec69b
commit bfa2794
Showing
6 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,3 @@ gleam_json = "~> 0.7 or ~> 1.0" | |
|
||
[dev-dependencies] | ||
gleeunit = "~> 1.0" | ||
simplifile = "~> 1.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.