UNPKG

@thi.ng/file-io

Version:

Assorted file I/O utils (w/ logging support) for NodeJS/Bun

34 lines (33 loc) 882 B
import { U32 } from "@thi.ng/hex"; import { open } from "node:fs/promises"; import { maskedPath } from "./mask.js"; async function* fileChunks(path, opts = {}) { let { size = 1024, start = 0, end = Infinity, logger } = opts; const mpath = maskedPath(path); logger?.debug(`start reading file chunks (size: 0x${size}):`, mpath); let fd = void 0; try { fd = await open(path, "r"); while (start < end) { logger?.debug( `reading chunk: 0x${U32(start)} - 0x${U32( start + size - 1 )} (${mpath})` ); const { buffer, bytesRead } = await fd.read({ buffer: Buffer.alloc(size), length: size, position: start }); if (bytesRead === 0) break; yield buffer; if (bytesRead < size) break; start += bytesRead; } } finally { await fd?.close(); } } export { fileChunks };