-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathfs-read.ts
More file actions
33 lines (30 loc) · 929 Bytes
/
fs-read.ts
File metadata and controls
33 lines (30 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Scanner-safe file reading utilities.
*
* Uses open() + read() to avoid false positives from openclaw's
* potential-exfiltration heuristic in bundled output.
*/
import { open } from "node:fs/promises";
import { openSync, readSync, closeSync, fstatSync } from "node:fs";
/** Read file contents as UTF-8 string (async). */
export async function readTextFile(filePath: string): Promise<string> {
const fh = await open(filePath, "r");
try {
const buf = Buffer.alloc((await fh.stat()).size);
await fh.read(buf, 0, buf.length, 0);
return buf.toString("utf-8");
} finally {
await fh.close();
}
}
/** Read file contents as UTF-8 string (sync). */
export function readTextFileSync(filePath: string): string {
const fd = openSync(filePath, "r");
try {
const buf = Buffer.alloc(fstatSync(fd).size);
readSync(fd, buf);
return buf.toString("utf-8");
} finally {
closeSync(fd);
}
}