Skip to content

Commit 118b209

Browse files
committed
address comments.
1 parent 1585d37 commit 118b209

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"keywords": [],
2020
"author": "",
2121
"license": "ISC",
22-
"packageManager": "pnpm@10.20.0",
22+
"packageManager": "bun@1.3.2",
2323
"dependencies": {
2424
"lighthouse": "^12.2.1",
2525
"playwright-core": "^1.52.0",

src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from "./browser.js";
22
export * from "./lighthouse.js";
33

4-
export const port = Number(process.env.PORT) || 3000;
4+
export const port = process.env.PORT ? Number(process.env.PORT) : 3000;

src/routes/reports.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { BrowserContextOptions } from "playwright-core";
1+
import type { BrowserContext, BrowserContextOptions } from "playwright-core";
22
import { playAudit } from "playwright-lighthouse";
33
import { browser, defaultContext, lighthouseConfigs } from "../config";
44
import { lighthouseSchema } from "../schemas";
55

66
export async function handleReportsRequest(req: Request): Promise<Response> {
7+
let context: BrowserContext | undefined;
8+
79
try {
810
const json = await req.json();
911
const body = lighthouseSchema.parse(json);
@@ -19,7 +21,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
1921
if (body.locale) contextOptions.locale = body.locale;
2022
if (body.timezoneId) contextOptions.timezoneId = body.timezoneId;
2123

22-
const context = await browser.newContext(contextOptions);
24+
context = await browser.newContext(contextOptions);
2325

2426
// Grant permissions if specified
2527
if (body.permissions && body.permissions.length > 0) {
@@ -72,7 +74,6 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
7274
thresholds,
7375
});
7476

75-
await context.close();
7677
const report = Array.isArray(results.report)
7778
? results.report.join("")
7879
: results.report;
@@ -85,5 +86,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
8586
status: 400,
8687
headers: { "Content-Type": "application/json" },
8788
});
89+
} finally {
90+
await context?.close();
8891
}
8992
}

src/routes/screenshots.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
BrowserContext,
23
BrowserContextOptions,
34
PageScreenshotOptions,
45
} from "playwright-core";
@@ -8,6 +9,8 @@ import { screenshotSchema } from "../schemas";
89
export async function handleScreenshotsRequest(
910
req: Request,
1011
): Promise<Response> {
12+
let context: BrowserContext | undefined;
13+
1114
try {
1215
const json = await req.json();
1316
const body = screenshotSchema.parse(json);
@@ -28,7 +31,7 @@ export async function handleScreenshotsRequest(
2831
if (body.timezoneId) contextOptions.timezoneId = body.timezoneId;
2932
if (body.geolocation) contextOptions.geolocation = body.geolocation;
3033

31-
const context = await browser.newContext(contextOptions);
34+
context = await browser.newContext(contextOptions);
3235

3336
// Grant permissions if specified
3437
if (body.permissions && body.permissions.length > 0) {
@@ -78,8 +81,6 @@ export async function handleScreenshotsRequest(
7881

7982
const screen = await page.screenshot(screenshotOptions);
8083

81-
await context.close();
82-
8384
return new Response(Buffer.from(screen), {
8485
headers: {
8586
"Content-Type": `image/${body.format}`,
@@ -91,5 +92,7 @@ export async function handleScreenshotsRequest(
9192
status: 400,
9293
headers: { "Content-Type": "application/json" },
9394
});
95+
} finally {
96+
await context?.close();
9497
}
9598
}

src/routes/test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import { generateTestHTML } from "../utils/test-page.js";
33

44
export async function handleTestRequest(_req: Request): Promise<Response> {
55
const context = await browser.newContext(defaultContext);
6-
const page = await context.newPage();
6+
try {
7+
const page = await context.newPage();
78

8-
await page.goto("about:blank");
9+
await page.goto("about:blank");
910

10-
const html = await page.evaluate(() => {
11-
return new Date().toISOString();
12-
});
11+
const html = await page.evaluate(() => {
12+
return new Date().toISOString();
13+
});
1314

14-
await context.close();
15+
await context.close();
1516

16-
return new Response(generateTestHTML(html), {
17-
headers: { "Content-Type": "text/html" },
18-
});
17+
return new Response(generateTestHTML(html), {
18+
headers: { "Content-Type": "text/html" },
19+
});
20+
} finally {
21+
await context.close();
22+
}
1923
}

0 commit comments

Comments
 (0)