mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
23 lines
507 B
JavaScript
23 lines
507 B
JavaScript
// Original fork from https://github.com/sindresorhus/read-chunk
|
|
import { open } from "node:fs/promises"
|
|
|
|
export default async (filePath, { length, startPosition }) => {
|
|
const fileDescriptor = await open(filePath, "r")
|
|
|
|
try {
|
|
let { bytesRead, buffer } = await fileDescriptor.read({
|
|
buffer: new Uint8Array(length),
|
|
length,
|
|
position: startPosition,
|
|
})
|
|
|
|
if (bytesRead < length) {
|
|
buffer = buffer.subarray(0, bytesRead)
|
|
}
|
|
|
|
return buffer
|
|
} finally {
|
|
await fileDescriptor?.close()
|
|
}
|
|
}
|