Skip to content

Commit

Permalink
Use builtin brotli functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
timdawborn committed Dec 8, 2023
1 parent a9efac5 commit 538ab65
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"dependencies": {
"assert-never": "^1.2.1",
"brotli": "^1.3.3",
"chalk": "^4.1.0",
"commander": "^10.0.1",
"content-type": "^1.0.5",
Expand Down
13 changes: 10 additions & 3 deletions src/compression.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from "os";
import {
compressBuffer,
CompressionAlgorithm,
Expand Down Expand Up @@ -69,15 +70,21 @@ const favicon = Buffer.from([
68, 174, 66, 96, 130,
]);

const GZIP_OS_MAP: Record<string, number> = {
"linux": 3,
"darwin": 19,
}
const GZIP_OS = GZIP_OS_MAP[os.type().toLowerCase()];

const emptyGzip = Buffer.from([
31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, GZIP_OS, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
const smallGzip = Buffer.from([
31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 99, 100, 98, 102, 97, 5, 0, 244, 153, 11,
31, 139, 8, 0, 0, 0, 0, 0, 0, GZIP_OS, 99, 100, 98, 102, 97, 5, 0, 244, 153, 11,
71, 5, 0, 0, 0,
]);
const faviconGzip = Buffer.from([
31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 1, 239, 3, 16, 252, 137, 80, 78, 71, 13, 10,
31, 139, 8, 0, 0, 0, 0, 0, 0, GZIP_OS, 1, 239, 3, 16, 252, 137, 80, 78, 71, 13, 10,
26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 57, 0, 0, 0, 57, 8, 6, 0, 0, 0,
140, 24, 131, 133, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19,
1, 0, 154, 156, 24, 0, 0, 0, 1, 115, 82, 71, 66, 0, 174, 206, 28, 233, 0, 0,
Expand Down
10 changes: 2 additions & 8 deletions src/compression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import brotli from "brotli";
import zlib from "zlib";

export type CompressionAlgorithm = "br" | "gzip" | "none";
Expand All @@ -11,12 +10,7 @@ export function compressBuffer(
case "none":
return buffer;
case "br":
const compressed = brotli.compress(buffer);
if (compressed !== null) {
return Buffer.from(compressed);
} else {
throw new Error(`Brotli compression failed!`);
}
return zlib.brotliCompressSync(buffer);
case "gzip":
return zlib.gzipSync(buffer);
default:
Expand All @@ -32,7 +26,7 @@ export function decompressBuffer(
case "none":
return buffer;
case "br":
return Buffer.from(brotli.decompress(buffer));
return zlib.brotliDecompressSync(buffer);
case "gzip":
return zlib.gunzipSync(buffer);
default:
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1015,11 +1015,6 @@ balanced-match@^1.0.0:
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

base64-js@^1.1.2:
version "1.5.1"
resolved "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz"
Expand Down Expand Up @@ -1065,13 +1060,6 @@ braces@^3.0.2, braces@~3.0.2:
dependencies:
fill-range "^7.0.1"

brotli@^1.3.3:
version "1.3.3"
resolved "https://registry.npmjs.org/brotli/-/brotli-1.3.3.tgz"
integrity sha512-oTKjJdShmDuGW94SyyaoQvAjf30dZaHnjJ8uAF+u2/vGJkJbJPJAT1gDiOJP5v1Zb6f9KEyW/1HpuaWIXtGHPg==
dependencies:
base64-js "^1.1.2"

browserslist@^4.21.9:
version "4.22.1"
resolved "https://registry.npmjs.org/browserslist/-/browserslist-4.22.1.tgz"
Expand Down

0 comments on commit 538ab65

Please sign in to comment.