action-debian-package/node_modules/firstline/index.js

31 lines
724 B
JavaScript
Raw Normal View History

2020-03-26 09:34:31 +00:00
'use strict';
const fs = require('fs');
module.exports = (path, usrOpts) => {
const opts = {
encoding: 'utf8',
lineEnding: '\n'
};
Object.assign(opts, usrOpts);
return new Promise((resolve, reject) => {
const rs = fs.createReadStream(path, {encoding: opts.encoding});
let acc = '';
let pos = 0;
let index;
rs
.on('data', chunk => {
index = chunk.indexOf(opts.lineEnding);
acc += chunk;
if (index === -1) {
pos += chunk.length;
} else {
pos += index;
rs.close();
}
})
.on('close', () => resolve(acc.slice(acc.charCodeAt(0) === 0xFEFF ? 1 : 0, pos)))
.on('error', err => reject(err));
});
};