Skip to content

Commit

Permalink
Ensure the first chunk is at least 3 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 25, 2014
1 parent 6f8d64d commit 8a55585
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ var stripBom = module.exports = function (arg) {
};

stripBom.stream = function () {
var through = require('through2');
var first = true;
var firstChunk = require('first-chunk-stream');

return through(function (chunk, enc, cb) {
if (first) {
first = false;
chunk = stripBom(chunk);
}

this.push(chunk);
return firstChunk({minSize: 3}, function (chunk, enc, cb) {
this.push(stripBom(chunk));
cb();
});
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"streams"
],
"dependencies": {
"is-utf8": "^0.2.0",
"through2": "^0.4.1"
"first-chunk-stream": "^0.1.0",
"is-utf8": "^0.2.0"
},
"devDependencies": {
"concat-stream": "^1.4.5",
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ it('should support streams', function (cb) {
cb();
}));
});

it('should support streams with low `highWaterMark`', function (cb) {
fs.createReadStream('fixture-utf8', {highWaterMark: 1})
.pipe(stripBom.stream())
.pipe(concat(function (data) {
assert.strictEqual(data.toString(), 'Unicorn\n');
cb();
}));
});

0 comments on commit 8a55585

Please sign in to comment.