Skip to content

Commit e58b186

Browse files
author
Peter Steinberger
committed
fix(gateway): preserve static asset HEAD representation metadata
1 parent e4568f6 commit e58b186

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎src/gateway/control-ui-static.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,16 @@ export function resolveControlUiHtmlEncoding(req: IncomingMessage): ControlUiEnc
159159
}
160160

161161
type OpenedControlUiRepresentation = {
162-
bodyFile: { path: string; fd: number };
162+
bodyFile: { path: string; fd: number; size: number };
163163
contentPath: string;
164164
encoding?: ControlUiContentEncoding;
165165
};
166166

167167
export function resolveOpenedControlUiRepresentation(params: {
168168
req: IncomingMessage;
169-
sourceFile: { path: string; fd: number };
169+
sourceFile: { path: string; fd: number; size: number };
170170
precompressed: boolean;
171-
openPrecompressedFile: (filePath: string) => { path: string; fd: number } | null;
171+
openPrecompressedFile: (filePath: string) => { path: string; fd: number; size: number } | null;
172172
}): OpenedControlUiRepresentation | null {
173173
const { req, sourceFile, precompressed, openPrecompressedFile } = params;
174174
const extension = path.extname(sourceFile.path).toLowerCase();
@@ -187,7 +187,7 @@ export function resolveOpenedControlUiRepresentation(params: {
187187
}
188188

189189
const suffix = selected === "br" ? ".br" : ".gz";
190-
let compressedFile: { path: string; fd: number } | null;
190+
let compressedFile: { path: string; fd: number; size: number } | null;
191191
try {
192192
compressedFile = openPrecompressedFile(`${sourceFile.path}${suffix}`);
193193
} catch (error) {
@@ -236,10 +236,13 @@ function setControlUiFileHeaders(
236236
export function respondHeadForControlUiFile(
237237
res: ServerResponse,
238238
filePath: string,
239-
options?: { immutable?: boolean; encoding?: ControlUiContentEncoding },
239+
options?: { immutable?: boolean; encoding?: ControlUiContentEncoding; contentLength?: number },
240240
) {
241241
res.statusCode = 200;
242242
setControlUiFileHeaders(res, filePath, options);
243+
if (options?.contentLength !== undefined) {
244+
res.setHeader("Content-Length", String(options.contentLength));
245+
}
243246
res.end();
244247
}
245248

‎src/gateway/control-ui.http.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2567,7 +2567,7 @@ describe("handleControlUiHttpRequest", () => {
25672567
req: {
25682568
headers: { "accept-encoding": "br, identity;q=0" },
25692569
} as IncomingMessage,
2570-
sourceFile: { path: filePath, fd },
2570+
sourceFile: { path: filePath, fd, size: fsSync.fstatSync(fd).size },
25712571
precompressed: true,
25722572
openPrecompressedFile: () => {
25732573
throw openError;
@@ -2750,6 +2750,38 @@ describe("handleControlUiHttpRequest", () => {
27502750
});
27512751
});
27522752

2753+
it.each(["identity", "gzip", "br"] as const)(
2754+
"preserves the selected %s static-asset Content-Length for HEAD",
2755+
async (encoding) => {
2756+
await withControlUiRoot({
2757+
fn: async (tmp) => {
2758+
const source = "console.log('static asset metadata');\n".repeat(12);
2759+
const { filePath } = await writeAssetFile(tmp, "app-HeAd1234.js", source);
2760+
await fs.writeFile(`${filePath}.gz`, gzipSync(source));
2761+
await fs.writeFile(`${filePath}.br`, brotliCompressSync(source));
2762+
const request = {
2763+
url: "/assets/app-HeAd1234.js",
2764+
rootPath: tmp,
2765+
rootKind: "bundled" as const,
2766+
headers: { "accept-encoding": encoding },
2767+
};
2768+
const get = await runControlUiRequest({ ...request, method: "GET" });
2769+
const head = await runControlUiRequest({ ...request, method: "HEAD" });
2770+
const body = get.end.mock.calls[0]?.[0];
2771+
2772+
expect(Buffer.isBuffer(body)).toBe(true);
2773+
expect(head.setHeader).toHaveBeenCalledWith("Content-Length", String(body.byteLength));
2774+
expect(firstEndCallLength(head.end)).toBe(0);
2775+
if (encoding === "identity") {
2776+
expect(head.setHeader).not.toHaveBeenCalledWith("Content-Encoding", expect.anything());
2777+
} else {
2778+
expect(head.setHeader).toHaveBeenCalledWith("Content-Encoding", encoding);
2779+
}
2780+
},
2781+
});
2782+
},
2783+
);
2784+
27532785
it.each([
27542786
{
27552787
name: "root-mounted",

‎src/gateway/control-ui.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ function resolveSafeControlUiFile(
885885
rootReal: string,
886886
filePath: string,
887887
rejectHardlinks: boolean,
888-
): { path: string; fd: number } | null {
888+
): { path: string; fd: number; size: number } | null {
889889
const opened = openRootFileSync({
890890
absolutePath: filePath,
891891
rootPath: rootReal,
@@ -902,7 +902,7 @@ function resolveSafeControlUiFile(
902902
fallback: () => null,
903903
});
904904
}
905-
return { path: opened.path, fd: opened.fd };
905+
return { path: opened.path, fd: opened.fd, size: opened.stat.size };
906906
}
907907

908908
function isSafeRelativePath(relPath: string) {
@@ -1214,6 +1214,7 @@ export async function handleControlUiHttpRequest(
12141214
respondHeadForControlUiFile(res, representation.contentPath, {
12151215
immutable: immutableAsset,
12161216
encoding: representation.encoding,
1217+
contentLength: representation.bodyFile.size,
12171218
});
12181219
return true;
12191220
} finally {

0 commit comments

Comments
 (0)