Files
reverse-engineering-anything/tests/fixtures/firmwareShortReadFileSystem.ts
T

32 lines
1017 B
TypeScript

import { lstat, open, realpath } from "node:fs/promises";
import type { StableArtifactFileSystem } from "../../src/resources/readStableArtifact.js";
export const shortReadAfterStreamFileSystem = (): StableArtifactFileSystem => ({
lstat: (path) => lstat(path, { bigint: true }),
realpath,
open: async (path, flags, mode) => {
const handle = await open(path, flags, mode);
const originalRead = handle.read.bind(handle);
let streamed = false;
handle.read = (async (
buffer: Buffer,
bufOffset?: number | null,
length?: number | null,
position?: number | bigint | null,
) => {
const res = await originalRead(
buffer,
bufOffset ?? 0,
length ?? buffer.length,
position ?? null,
);
if (res.bytesRead === 0) streamed = true;
else if (streamed && typeof position === "number" && position > 0) {
return { bytesRead: 0, buffer };
}
return res;
}) as typeof handle.read;
return handle;
},
});