Skip to content

Commit f4b451c

Browse files
aberemia24claude
andcommitted
refactor: use context.saveFile() for file writing
Updates take_snapshot and evaluate_script to use context.saveFile() instead of direct writeFile() calls, following the pattern established in PR #250. Changes: - Removed direct fs/promises imports - Convert string data to Uint8Array using TextEncoder - Use context.saveFile() for consistent file writing - Update response messages to use returned filename 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 50f92b0 commit f4b451c

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/tools/script.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright 2025 Google LLC
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
import {writeFile} from 'node:fs/promises';
76

87
import type {JSHandle} from 'puppeteer-core';
98
import z from 'zod';
@@ -69,9 +68,11 @@ Example with arguments: \`(el) => {
6968
);
7069

7170
if (request.params.filePath) {
72-
await writeFile(request.params.filePath, result);
71+
const encoder = new TextEncoder();
72+
const data = encoder.encode(result);
73+
const file = await context.saveFile(data, request.params.filePath);
7374
response.appendResponseLine(
74-
`Saved script result to ${request.params.filePath}.`,
75+
`Saved script result to ${file.filename}.`,
7576
);
7677
} else {
7778
response.appendResponseLine('Script ran on page and returned:');

src/tools/snapshot.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {writeFile} from 'node:fs/promises';
8-
97
import {Locator} from 'puppeteer-core';
108
import z from 'zod';
119

@@ -41,10 +39,10 @@ identifier (uid). Always use the latest snapshot. Prefer taking a snapshot over
4139
const formattedSnapshot = formatA11ySnapshot(snapshot.root);
4240

4341
if (request.params.filePath) {
44-
await writeFile(request.params.filePath, formattedSnapshot);
45-
response.appendResponseLine(
46-
`Saved snapshot to ${request.params.filePath}.`,
47-
);
42+
const encoder = new TextEncoder();
43+
const data = encoder.encode(formattedSnapshot);
44+
const file = await context.saveFile(data, request.params.filePath);
45+
response.appendResponseLine(`Saved snapshot to ${file.filename}.`);
4846
} else {
4947
response.setIncludeSnapshot(true);
5048
}

0 commit comments

Comments
 (0)