Files
reverse-engineering-anything/src/filesystem/fileHandleMatchesBytes.ts
T
morluto 21ff4911bf refactor(setup): share publication byte verification
Use one bounded positional byte comparator for client configuration and skill
publication readback. Keep pathname identity, rollback, mode restoration,
cleanup and retry authority in the existing publication owners.

Preserve short-read progress, EOF, empty-file equality and original I/O errors.
Extend existing byte-exact skill recovery coverage to empty originals.

Validation: client configuration transaction, skill publication recovery and
process-global rollback suites passed; static, docs and discovery checks passed.
2026-10-24 02:57:11 +08:00

28 lines
846 B
TypeScript

import type { FileHandle } from "node:fs/promises";
/** Compare descriptor bytes without taking ownership or validating pathname identity. */
export const fileHandleMatchesBytes = async (
handle: Pick<FileHandle, "stat" | "read">,
expected: Buffer,
): Promise<boolean> => {
if ((await handle.stat()).size !== expected.length) return false;
const chunk = Buffer.allocUnsafe(Math.min(64 * 1024, expected.length));
for (let position = 0; position < expected.length;) {
const { bytesRead } = await handle.read(
chunk,
0,
Math.min(chunk.length, expected.length - position),
position,
);
if (
bytesRead === 0 ||
!chunk
.subarray(0, bytesRead)
.equals(expected.subarray(position, position + bytesRead))
)
return false;
position += bytesRead;
}
return true;
};