In a previous question it seemed that the only way to do random-access reads from a file in node.js is to use fs.createReadStream()
with its optional fd
, start
, and end
fields.
This worked fine in my simplest tests. But in my project I need to repeatedly read from different offsets of a binary file. This failed in a strange way so I came up with a minimal test case:
var fs = require('fs');
fs.open('test.txt', 'r', function (err, fd) {
if (err) {
console.error('error opening file: ' + err);
} else {
fs.createReadStream(null, {fd: fd, start: 2, end: 5}).on('error', function (err) {
throw e;
}).on('close', function () {
console.log('outer close');
}).on('data', function (data) {
console.log('outer data', data);
}).on('end', function () {
console.log('outer end');
fs.createReadStream(null, {fd: fd, start: 0, end: 3}).on('error', function (err) {
throw e;
}).on('close', function () {
console.log('inner close');
}).on('data', function (data) {
console.log('inner data', data);
}).on('end', function () {
console.log('inner end');
// more code to execute after both reads
});
});
}
});
The inner end
event is never received. (The outer close
is received inconsistently, but I don't need to attach code to it.)
I've implemented this project before in Perl and even in JavaScript as a Firefox extension, but it's proving difficult under node. This is also a test for whether I can start using node.js as a general purpose scripting language.