Skip to content

Commit 9a47a81

Browse files
authored
fix(server/root): use http.writeHead after async operation (#555)
1 parent d4fa12d commit 9a47a81

File tree

2 files changed

+4
-40
lines changed

2 files changed

+4
-40
lines changed

workspaces/server/src/endpoints/root.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { context } from "../ALS.js";
77

88
export async function get(_req: Request, res: Response) {
99
try {
10-
res.writeHead(200, {
11-
"Content-Type": "text/html"
12-
});
13-
1410
const { viewBuilder } = context.getStore()!;
1511

1612
const templateStr = await viewBuilder.render();
13+
14+
res.writeHead(200, {
15+
"Content-Type": "text/html"
16+
});
1717
res.end(templateStr);
1818
}
1919
catch (err: any) {

workspaces/server/test/httpServer.test.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { type Polka } from "polka";
1818

1919
// Import Internal Dependencies
2020
import { buildServer } from "../index.js";
21-
import * as rootEndpoint from "../src/endpoints/root.js";
2221
import * as flagsEndpoint from "../src/endpoints/flags.js";
2322

2423
// CONSTANTS
@@ -82,41 +81,6 @@ describe("httpServer", { concurrency: 1 }, () => {
8281
assert.equal(result.headers["content-type"], "text/html");
8382
});
8483

85-
test("'/' should fail", async(ctx) => {
86-
class Response {
87-
body: string;
88-
headers: Record<string, string>;
89-
statusCode: number;
90-
91-
constructor() {
92-
this.body = "";
93-
this.headers = {};
94-
this.statusCode = 200;
95-
}
96-
end(str: string) {
97-
this.body = str;
98-
}
99-
writeHead(int: number) {
100-
this.statusCode = int;
101-
}
102-
getHeader(key: string) {
103-
return this.headers[key];
104-
}
105-
}
106-
107-
const fakeError = "fake error";
108-
function toThrow() {
109-
throw new Error(fakeError);
110-
}
111-
ctx.mock.method(Response.prototype, "writeHead", toThrow, { times: 1 });
112-
113-
const response: any = new Response();
114-
await rootEndpoint.get({} as any, response);
115-
116-
assert.strictEqual(response.body, JSON.stringify({ error: fakeError }));
117-
assert.strictEqual(response.statusCode, 500);
118-
});
119-
12084
test("'/flags' should return the flags list as JSON", async() => {
12185
const result = await get(new URL("/flags", kHttpURL));
12286

0 commit comments

Comments
 (0)