Skip to content

Commit

Permalink
note about browser compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
stackdump committed Mar 4, 2024
1 parent 02161dc commit 93c75c9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
49 changes: 49 additions & 0 deletions compression.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
import {Model, ModelDeclaration, newModel} from "./model";
import {compress, decompress} from "brotli-wasm";

/*
*/

/**
* Compress a string using brotli
*
* NOTE: does not work in the browser use brotliPromise instead
* ```
* import brotliPromise from 'brotli-wasm';
*
* async function compressBrotliEncode(data: string): Promise<string> {
* const brotli = await brotliPromise;
* const encoder = new TextEncoder();
* const encodedData = encoder.encode(data);
* const compressedData = brotli.compress(encodedData);
* const byteNumbers = new Array(compressedData.length);
* for (let i = 0; i < compressedData.length; i++) {
* byteNumbers[i] = compressedData[i];
* }
* const byteArray = new Uint8Array(byteNumbers);
* // @ts-ignore
* const byteCharacters = String.fromCharCode.apply(null, byteArray);
* return btoa(byteCharacters);
* }
* ```
*/
export function compressBrotliEncode(data: string): string {
return Buffer.from(
compress(Buffer.from(data))
).toString("base64");
}

/**
* Decompress a string using brotli
*
* NOTE: does not work in the browser use brotliPromise instead
* ```
* import brotliPromise from 'brotli-wasm';
*
* async function decompressBrotliDecode(data: string): Promise<string> {
* const brotli = await brotliPromise;
* const byteCharacters = atob(data);
* const byteNumbers = new Array(byteCharacters.length);
* for (let i = 0; i < byteCharacters.length; i++) {
* byteNumbers[i] = byteCharacters.charCodeAt(i);
* }
* const byteArray = new Uint8Array(byteNumbers);
* const decompressedData = brotli.decompress(byteArray);
* const textDecoder = new TextDecoder();
* return textDecoder.decode(decompressedData);
* }
* ```
*
* @param data
*/
export async function decompressBrotliDecode(data: string): Promise<string> {
return Buffer.from(
decompress(Buffer.from(data, "base64"))
Expand Down
9 changes: 8 additions & 1 deletion test/compression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ describe("compression", () => {
expect(decompressed).toEqual(data);
});

});
it("should decompress data from browser", async () => {
const compressedData = "GzkCIBwHdqMPWUYyo7XgaT/B09w+1fHywu1u31IMRQwiCxaRsTAxQRT6UodF4e9vcmthITygLrPfojnB4nxsskw21O/iE3GRG82+n/aPgzT++TW8fY5765PjEAvRHLk1fa0Atw8uCVzrgniE9AOCxwJt0eNbZxX3GlCwKSXlDBVIj2qWMSpoWCuQ0SZF4WJKQu7IYz8DzVzPNGg5hqbWWqtzXBixNz9qkiODzShUClkETwDocbjtBJp9Wh5QW8T8PXrgq9nCDI3qaA==";
const decompressed = await decompressBrotliDecode(compressedData);
console.log(decompressed);

});

});

0 comments on commit 93c75c9

Please sign in to comment.