Back to packages list

Vals using pako

Description from the NPM package:
zlib port to javascript - fast, modularized, with browser support

Compress Response

Isn't it cool that browsers can natively decompress stuff? There are a couple of supported compression algorithms: gzip (used below), compress, deflate, br.

Learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding

If you don't add the content-encoding header, the gzip result looks like:

���������
���.���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������YDA��"��

Which is incredibly short for a 500kb string! (Which shouldn't be surprising because it's just "hi" 250k times)

1
2
3
4
5
6
7
8
9
import { gzip } from "npm:pako";
export default async function(req: Request): Promise<Response> {
return new Response(await gzip(JSON.stringify("hi".repeat(250_000))), {
headers: {
"Content-Type": "application/json",
},
});
}

Compress Response

Isn't it cool that browsers can natively decompress stuff? There are a couple of supported compression algorithms: gzip (used below), compress, deflate, br.

Learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding

If you don't add the content-encoding header, the gzip result looks like:

���������
���.���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������YDA��"��

Which is incredibly short for a 500kb string! (Which shouldn't be surprising because it's just "hi" 250k times)

1
2
3
4
5
6
7
8
9
10
import { gzip } from "npm:pako";
export default async function(req: Request): Promise<Response> {
return new Response(await gzip("hi".repeat(250_000)), {
headers: {
"Content-Encoding": "gzip",
"Content-Type": "text/plain",
},
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Buffer } from "node:buffer";
export const parsePacoEx = (async () => {
var pako = await import("npm:pako");
function decodeMermaid(encodedString) {
var binaryData = Buffer.from(encodedString, "base64");
var inflatedString = pako.inflate(binaryData, { to: "string" });
return inflatedString;
}
return JSON.parse(
decodeMermaid(
"eNp1kk1rwzAMhv-K8WmFhLEeO9gYW2GDjnVtj76osdIYHDv4o2sJ-e-T42Snzpco0quH15Z6XlmJfMVPDrqGbXaPwjA6-wAu3N2Nn8WiLJ--I_qgrHnoBX_ZfjDrGJ5BRwjIwB1VcOCuLMGeBR8myl9TWTLB_9ELzoi_piJx-_ukYhTdZqwvnfXKnBiYJMq9s2RJ3l6jD7Zl74fDljVgpEZ3w9ByhN3QTmYunUPvs58c37I0UchK9Mh8I
),
).code;
})();
1
Next