HEX
Server: nginx/1.24.0
System: Linux nowruzgan 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64
User: babak (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/dev/farhangmoaser/web/node_modules/read-chunk/index.js
import {closeSync, openSync, readSync} from 'node:fs';
import {open} from 'node:fs/promises';

export async function readChunk(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();
	}
}

export function readChunkSync(filePath, {length, startPosition}) {
	let buffer = new Uint8Array(length);
	const fileDescriptor = openSync(filePath, 'r');

	try {
		const bytesRead = readSync(fileDescriptor, buffer, {
			length,
			position: startPosition,
		});

		if (bytesRead < length) {
			buffer = buffer.subarray(0, bytesRead);
		}

		return buffer;
	} finally {
		closeSync(fileDescriptor);
	}
}