HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago-nodeserver-hotfixes/node_modules/read-chunk/index.js
'use strict';
const fs = require('fs');
const pify = require('pify');
const withOpenFile = require('with-open-file');

const fsReadP = pify(fs.read, {multiArgs: true});

const readChunk = (filePath, startPosition, length) => {
	const buffer = Buffer.alloc(length);

	return withOpenFile(filePath, 'r', fileDescriptor =>
		fsReadP(fileDescriptor, buffer, 0, length, startPosition)
	)
		.then(([bytesRead, buffer]) => {
			if (bytesRead < length) {
				buffer = buffer.slice(0, bytesRead);
			}

			return buffer;
		});
};

module.exports = readChunk;
// TODO: Remove this for the next major release
module.exports.default = readChunk;

module.exports.sync = (filePath, startPosition, length) => {
	let buffer = Buffer.alloc(length);

	const bytesRead = withOpenFile.sync(filePath, 'r', fileDescriptor =>
		fs.readSync(fileDescriptor, buffer, 0, length, startPosition)
	);

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

	return buffer;
};