Skip to content

Commit

Permalink
Switch from gzip to deflate for compression
Browse files Browse the repository at this point in the history
Resolves #2796
  • Loading branch information
Gerrit0 committed Dec 6, 2024
1 parent 9dcbd5d commit ed1ac7a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ title: Changelog

### Bug Fixes

- Switch from gzip to deflate for compressing assets to make output consistent across different operating systems, #2796.
- Cascaded modifier tags will no longer be copied into type literals, #2802.

## v0.27.3 (2024-12-04)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Decompresses Base64-encoded Gzip data and parses it into a JSON object.
* Decompresses Base64-encoded deflate compressed data and parses it into a JSON object.
*
* @param base64 - The Base64-encoded string representing the Gzip-compressed JSON string.
* @param base64 - The Base64-encoded string representing the deflate-compressed JSON string.
* @returns A promise that resolves to the parsed JSON object.
*/
export async function decompressJson(base64: string) {
const binaryData = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
const blob = new Blob([binaryData]);
const decompressedStream = blob
.stream()
.pipeThrough(new DecompressionStream("gzip"));
.pipeThrough(new DecompressionStream("deflate"));
const decompressedText = await new Response(decompressedStream).text();
return JSON.parse(decompressedText);
}
10 changes: 5 additions & 5 deletions src/lib/utils/compress.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { gzip } from "zlib";
import { deflate } from "zlib";
import { promisify } from "util";

const gzipP = promisify(gzip);
const deflateP = promisify(deflate);

/**
* Compresses a JSON-serializable object into a Base64-encoded Gzip string.
* Compresses a JSON-serializable object into a Base64-encoded deflate string.
*
* @param data - The JSON-serializable object to compress.
* @returns A promise that resolves to a Base64-encoded string of the Gzip-compressed data.
* @returns A promise that resolves to a Base64-encoded string of the deflate-compressed data.
*/
export async function compressJson(data: any) {
const gz = await gzipP(Buffer.from(JSON.stringify(data)));
const gz = await deflateP(Buffer.from(JSON.stringify(data)));
return gz.toString("base64");
}

0 comments on commit ed1ac7a

Please sign in to comment.