Skip to content

Commit

Permalink
feat: 🎸 add gzip utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jul 27, 2024
1 parent e2f6638 commit d74ed22
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/compression/__tests__/gzip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {utf8} from '../../buffers/strings';
import {gzip, ungzip} from '../gzip';

test('can gzip and ungzip data', async () => {
const data = utf8`Hello, World!`;
const compressed = await gzip(data);
const uncompressed = await ungzip(compressed);
expect(uncompressed).toEqual(data);
});
11 changes: 11 additions & 0 deletions src/compression/gzip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {fromStream} from '../streams/fromStream';
import {toStream} from '../streams/toStream';

const pipeThrough = async (data: Uint8Array, transform: ReadableWritablePair<Uint8Array, Uint8Array>): Promise<Uint8Array> =>
await fromStream(toStream(data).pipeThrough<Uint8Array>(transform));

export const gzip = async (data: Uint8Array): Promise<Uint8Array> =>
await pipeThrough(data, new CompressionStream('gzip'));

export const ungzip = async (data: Uint8Array): Promise<Uint8Array> =>
await pipeThrough(data, new DecompressionStream('gzip'));

0 comments on commit d74ed22

Please sign in to comment.