diff --git a/lib/fasta-parser.js b/lib/fasta-parser.js index 945a90f..296e35f 100644 --- a/lib/fasta-parser.js +++ b/lib/fasta-parser.js @@ -37,6 +37,7 @@ var through = require('through2') var split = require('split') var pumpify = require('pumpify') +var BufferList = require('bl') module.exports = function() { return pumpify(split(), parser()) @@ -44,7 +45,6 @@ module.exports = function() { function parser() { var cacheBuf - var cacheBufLen = 8 var openID = new Buffer('{"id":"') var closeIDOpenSeq = new Buffer('","seq":"') var closeSeq = new Buffer('"}\n') @@ -55,23 +55,24 @@ function parser() { function transform(buf, enc, next) { if (buf[0] === 62) { // If line starts with '>', this is an ID if (cacheBuf) { // If a previous object is in cache, push it - cacheBuf = Buffer.concat([cacheBuf, closeSeq], cacheBufLen+3) - this.push(cacheBuf) + cacheBuf.append(closeSeq) + this.push(cacheBuf.slice()) } var id = buf.toString().slice(1).trim().replace(/"/g, '\\"') - cacheBufLen = id.length + 16 - cacheBuf = Buffer.concat([openID, new Buffer(id), closeIDOpenSeq], cacheBufLen) + cacheBuf = new BufferList() + cacheBuf.append(openID) + cacheBuf.append(id) + cacheBuf.append(closeIDOpenSeq) } else { - cacheBufLen += buf.length - cacheBuf = Buffer.concat([cacheBuf, buf], cacheBufLen) + cacheBuf.append(buf) } next() } function flush() { - cacheBuf = Buffer.concat([cacheBuf, closeSeq], cacheBufLen+3) - this.push(cacheBuf) + cacheBuf.append(closeSeq) + this.push(cacheBuf.slice()) this.push(null) } } diff --git a/package.json b/package.json index e9035f3..e5a2a6a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "license": "MIT", "dependencies": { + "bl": "^0.9.0", "pumpify": "^1.3.0", "split": "^0.3.0", "through2": "~0.6.0"