Skip to content

Commit 2f18244

Browse files
saphidcodex
andcommitted
fix(cli): keep current runtime archives within the download bound
Raise the archive cap to 256 MiB and exercise current-size archives, declared overflow and headerless streamed overflow in the existing HTTP installer test. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 6dab4eb commit 2f18244

2 files changed

Lines changed: 63 additions & 23 deletions

File tree

‎scripts/build-npm-launcher.test.ts‎

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import * as NodeChildProcess from "node:child_process";
1212
import * as NodeCrypto from "node:crypto";
1313
import * as NodeEvents from "node:events";
1414
import * as NodeHttp from "node:http";
15+
import * as NodeStream from "node:stream";
1516

1617
import { npmReleaseDatabaseMigration } from "@t3tools/shared/cliRelease";
1718

1819
import { buildNpmLauncher } from "./build-npm-launcher.ts";
1920

2021
const VERSION = "1.2.3";
22+
const MAX_ARCHIVE_BYTES = 256 * 1024 * 1024;
23+
const CURRENT_ARCHIVE_BYTES = 183 * 1024 * 1024;
2124
const decodeManifest = Schema.decodeEffect(
2225
Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
2326
);
@@ -74,7 +77,14 @@ exit 7
7477
const archive = yield* fs.readFile(path.join(root, "release", archiveName));
7578
let checksum = NodeCrypto.createHash("sha256").update(archive).digest("hex");
7679
const requests: Array<string> = [];
77-
let oversizedArchive = false;
80+
let archiveMode: "normal" | "current-size" | "declared-overflow" | "streamed-overflow" = "normal";
81+
function* archiveChunks(size: number) {
82+
yield archive;
83+
const padding = new Uint8Array(64 * 1024);
84+
for (let remaining = size - archive.length; remaining > 0; remaining -= padding.length) {
85+
yield padding.subarray(0, remaining);
86+
}
87+
}
7888
const failures: Array<number> = [];
7989
const server = NodeHttp.createServer((request, response) => {
8090
requests.push(request.url ?? "");
@@ -84,12 +94,21 @@ exit 7
8494
} else if (request.url === `/v${VERSION}/SHA256SUMS`) {
8595
response.end(`${checksum} ${archiveName}\n`);
8696
} else if (request.url === `/v${VERSION}/${archiveName}`) {
87-
response
88-
.writeHead(200, {
89-
"Content-Length": oversizedArchive ? 128 * 1024 * 1024 + 1 : archive.length,
90-
Connection: "close",
91-
})
92-
.end(archive);
97+
const size =
98+
archiveMode === "current-size"
99+
? CURRENT_ARCHIVE_BYTES
100+
: archiveMode === "normal"
101+
? archive.length
102+
: MAX_ARCHIVE_BYTES + 1;
103+
response.writeHead(200, {
104+
...(archiveMode === "streamed-overflow" ? {} : { "Content-Length": size }),
105+
Connection: "close",
106+
});
107+
const body = NodeStream.Readable.from(
108+
archiveChunks(archiveMode === "declared-overflow" ? archive.length : size),
109+
);
110+
response.once("close", () => body.destroy());
111+
body.pipe(response);
93112
} else {
94113
response.writeHead(404).end();
95114
}
@@ -108,8 +127,15 @@ exit 7
108127
return {
109128
baseUrl: `http://127.0.0.1:${address.port}`,
110129
requests,
111-
setOversizedArchive: (enabled: boolean) => {
112-
oversizedArchive = enabled;
130+
setArchiveMode: (mode: typeof archiveMode) => {
131+
archiveMode = mode;
132+
const hash = NodeCrypto.createHash("sha256");
133+
for (const chunk of archiveChunks(
134+
mode === "current-size" ? CURRENT_ARCHIVE_BYTES : archive.length,
135+
)) {
136+
hash.update(chunk);
137+
}
138+
checksum = hash.digest("hex");
113139
},
114140
corruptChecksum: () => {
115141
checksum = "0".repeat(64);
@@ -184,19 +210,33 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
184210
[VERSION],
185211
);
186212

187-
release.setOversizedArchive(true);
188-
const oversizedHome = path.join(root, "oversized-home");
189-
const oversized = yield* run(process.execPath, [launcher, "--version"], {
213+
release.setArchiveMode("current-size");
214+
const currentSize = yield* run(process.execPath, [launcher, "--version"], {
190215
cwd: root,
191-
env: { ...env, T3CODE_HOME: oversizedHome },
216+
env: {
217+
...env,
218+
T3CODE_HOME: path.join(root, "current-size-home"),
219+
T3CODE_TEST_CALLS: undefined,
220+
},
192221
});
193-
assert.equal(oversized.exitCode, 1);
194-
assert.include(oversized.stderr, "134217728-byte limit");
195-
assert.deepStrictEqual(
196-
yield* fs.readDirectory(path.join(oversizedHome, "runtime/versions")),
197-
[],
198-
);
199-
release.setOversizedArchive(false);
222+
assert.equal(currentSize.exitCode, 0, currentSize.stderr);
223+
assert.equal(currentSize.stdout.trim(), VERSION);
224+
225+
for (const mode of ["declared-overflow", "streamed-overflow"] as const) {
226+
release.setArchiveMode(mode);
227+
const oversizedHome = path.join(root, mode);
228+
const oversized = yield* run(process.execPath, [launcher, "--version"], {
229+
cwd: root,
230+
env: { ...env, T3CODE_HOME: oversizedHome },
231+
});
232+
assert.equal(oversized.exitCode, 1);
233+
assert.include(oversized.stderr, `${MAX_ARCHIVE_BYTES}-byte limit`);
234+
assert.deepStrictEqual(
235+
yield* fs.readDirectory(path.join(oversizedHome, "runtime/versions")),
236+
[],
237+
);
238+
}
239+
release.setArchiveMode("normal");
200240

201241
const requestsAfterFirst = release.requests.length;
202242
const second = yield* run(process.execPath, [launcher, "--version"], { cwd: root, env });

‎scripts/npm-launcher/t3.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const { version } = JSON.parse(
1010
fs.readFileSync(path.join(import.meta.dirname, "../package.json"), "utf8"),
1111
);
1212

13-
// Current CLI archives fit within 128 MiB. A larger release needs an explicit
14-
// limit change; extraction and other installer writes are separate costs.
15-
const MAX_DOWNLOAD_BYTES = 128 * 1024 * 1024;
13+
// Leave room above current releases while bounding the compressed download.
14+
// Extraction and other installer writes are separate costs.
15+
const MAX_DOWNLOAD_BYTES = 256 * 1024 * 1024;
1616

1717
const SUPPORTED = ["darwin-arm64", "linux-arm64", "linux-x64", "win32-arm64", "win32-x64"];
1818

0 commit comments

Comments
 (0)