From 5f8ce8ff5fbe9eef35e1467c4221f8b080ca18a1 Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Sun, 4 Aug 2024 21:01:28 -0400 Subject: [PATCH] refactor: Remove brotli encoding of contracts It takes entire seconds to compress, and the memory overhead is huge when decompressing. --- components/controller.ts | 8 ++------ components/index.ts | 2 +- packaging/buildTasks.mjs | 7 ++----- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/components/controller.ts b/components/controller.ts index c9df226b..68ef70dc 100644 --- a/components/controller.ts +++ b/components/controller.ts @@ -65,8 +65,6 @@ import { ChallengeService } from "./candle/challengeService" import { getFlag } from "./flags" import { unpack } from "msgpackr" import { ChallengePackage, SavedChallengeGroup } from "./types/challenges" -import { promisify } from "util" -import { brotliDecompress } from "zlib" import assert from "assert" import { Response } from "express" import { ChallengeFilterType } from "./candle/challengeHelpers" @@ -1115,17 +1113,15 @@ export class Controller { } private async _loadInternalContracts(): Promise { - const decompress = promisify(brotliDecompress) - const buf = await readFile( join( PEACOCK_DEV ? process.cwd() : __dirname, "resources", - "contracts.br", + "contracts.prp", ), ) - const decompressed = JSON.parse((await decompress(buf)).toString()) as { + const decompressed = unpack(buf) as { b: MissionManifest[] el: MissionManifest[] } diff --git a/components/index.ts b/components/index.ts index 0b2953b5..282d19f2 100644 --- a/components/index.ts +++ b/components/index.ts @@ -586,6 +586,6 @@ export async function startServer(options: { return } catch (e) { log(LogLevel.ERROR, "Critical error during bootstrap!") - log(LogLevel.ERROR, e) + log(LogLevel.ERROR, (e as Error)?.stack ?? e) } } diff --git a/packaging/buildTasks.mjs b/packaging/buildTasks.mjs index 9c5218f1..4a30bbfa 100644 --- a/packaging/buildTasks.mjs +++ b/packaging/buildTasks.mjs @@ -22,8 +22,6 @@ import millis from "ms" import { mkdir, readdir, readFile, unlink, writeFile } from "fs/promises" import { createHash } from "crypto" import { Packr } from "msgpackr" -import { brotliCompress } from "zlib" -import { promisify } from "util" import glob from "fast-glob" import prettier from "prettier" @@ -197,9 +195,8 @@ export async function packResources() { } } - const d = JSON.stringify({ b, el }) - const compressed = await promisify(brotliCompress)(d) - await writeFile("resources/contracts.br", compressed) + const compressed = packer.pack({ b, el }) + await writeFile("resources/contracts.prp", compressed) console.log( `Gathered built-in contracts and challenges in ${millis(