Slice by read size, not buffer size

master
Sven Slootweg 3 years ago
parent f4004c035d
commit b22539a109

@ -34,13 +34,15 @@ function doRead(handle, length) {
return buffer; return buffer;
} else if (result.bytesRead < length) { } else if (result.bytesRead < length) {
// TODO: For possible future performance optimization, consider reusing the remaining Buffer allocation for a next read, if possible. Need more data on how often this case occurs first, though, to justify the added complexity. // TODO: For possible future performance optimization, consider reusing the remaining Buffer allocation for a next read, if possible. Need more data on how often this case occurs first, though, to justify the added complexity.
return buffer.slice(0, length); return buffer.slice(0, result.bytesRead);
} else { } else {
throw new Error(`Read more bytes (${result.bytesRead}) than the specified 'length' (${length}); this should never happen!`); throw new Error(`Read more bytes (${result.bytesRead}) than the specified 'length' (${length}); this should never happen!`);
} }
}); });
} }
// FIXME: This should probably *only* allow reading mode flags
module.exports = function createReadFileStream(_path, _options) { module.exports = function createReadFileStream(_path, _options) {
let [ path, options ] = validateArguments(arguments, [ let [ path, options ] = validateArguments(arguments, [
[ "path", required, isString ], [ "path", required, isString ],
@ -54,6 +56,9 @@ module.exports = function createReadFileStream(_path, _options) {
let handlePromise = fs.open(path, options.flag, options.mode); let handlePromise = fs.open(path, options.flag, options.mode);
// Silence unhandled rejection warnings until later
handlePromise.catch(() => {});
// TODO: Metadata, including stream label and file size/type/path // TODO: Metadata, including stream label and file size/type/path
return pipe([ return pipe([
simpleSource({ simpleSource({

Loading…
Cancel
Save