In the end, I decided to use this solution, which seems to be the most perfect I've seen so far:
var fs = require('fs'); var Q = require('q'); function readFirstLine (path) { return Q.promise(function (resolve, reject) { var rs = fs.createReadStream(path, {encoding: 'utf8'}); var acc = ''; var pos = 0; var index; rs .on('data', function (chunk) { index = chunk.indexOf('\n'); acc += chunk; index !== -1 ? rs.close() : pos += chunk.length; }) .on('close', function () { resolve(acc.slice(0, pos + index)); }) .on('error', function (err) { reject(err); }) }); }
I created an npm module for convenience called " firstline ."
Thanks to @dandavis for suggesting using String.prototype.slice() !
Pensierinmusica
source share