-
Notifications
You must be signed in to change notification settings - Fork 0
/
deflate.ts
33 lines (29 loc) · 1.17 KB
/
deflate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {streamEncode, streamDecode} from "./stream.ts";
type CompressCodec = "gzip" | "deflate" | "deflate-raw";
const COMPRESS_CODEC = "deflate-raw";
/**
* Compress binary with DEFLATE format.
* Default codec is DEFLATE with no header (RFC-1951) and name in WebAPI specification is `"deflate-raw"`.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = await deflateEncode(bin);
* const decode = await deflateDecode(encode);
* ```
*/
export async function deflateEncode(data:Uint8Array, codec?:CompressCodec):Promise<Uint8Array>{
return await streamDecode(streamEncode(data).pipeThrough(new CompressionStream(codec ?? COMPRESS_CODEC)));
}
/**
* Decompress DEFLATE format binary.
* Default codec is DEFLATE with no header (RFC-1951) and name in WebAPI specification is `"deflate-raw"`.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = await deflateEncode(bin);
* const decode = await deflateDecode(encode);
* ```
*/
export async function deflateDecode(data:Uint8Array, codec?:CompressCodec):Promise<Uint8Array>{
return await streamDecode(streamEncode(data).pipeThrough(new DecompressionStream(codec ?? COMPRESS_CODEC)));
}