diff --git a/index.js b/index.js index 1256b78..6047d4b 100644 --- a/index.js +++ b/index.js @@ -4,13 +4,13 @@ var through = require('through2'); var removeBom = require('remove-bom-buffer'); function removeBomStream() { - var completed = false; + var state = 0; // 0:Not removed, -1:In removing, 1:Already removed var buffer = Buffer.alloc(0); return through(onChunk, onFlush); function removeAndCleanup(data) { - completed = true; + state = 1; // Already removed buffer = null; @@ -18,14 +18,16 @@ function removeBomStream() { } function onChunk(data, enc, cb) { - if (completed) { + if (state === 1) { return cb(null, data); } - if (data.length >= 7) { + if (state === 0 /* Not removed */ && data.length >= 7) { return cb(null, removeAndCleanup(data)); } + state = -1; // In removing + var bufferLength = buffer.length; var chunkLength = data.length; var totalLength = bufferLength + chunkLength; @@ -39,7 +41,7 @@ function removeBomStream() { } function onFlush(cb) { - if (completed || !buffer) { + if (state === 2 /* Already removed */ || !buffer) { return cb(); } diff --git a/test/index.js b/test/index.js index fb51e29..cb3c759 100644 --- a/test/index.js +++ b/test/index.js @@ -29,7 +29,23 @@ describe('removeBomStream', function () { ); }); - it('removes the BOM from a UTF8 buffer', function (done) { + it('ignores UTF8 buffer without a BOM even if first chunk is shorter than 7 chars but second and subsequent are larger', function(done) { + var filepath = path.join(__dirname, './fixtures/test.txt'); + var fileContent = fs.readFileSync(filepath, 'utf-8'); + + var rmBom = removeBomStream(); + var output = ''; + rmBom.on('data', function(d) { + output += d.toString(); + }); + rmBom.write(Buffer.from(fileContent.slice(0, 5))); + rmBom.write(Buffer.from(fileContent.slice(5))); + + expect(output).toEqual(fileContent); + done(); + }); + + it('removes the BOM from a UTF8 buffer', function(done) { var filepath = path.join(__dirname, './fixtures/bom-utf8.txt'); var expected = fs.readFileSync(filepath).slice(3); @@ -81,7 +97,23 @@ describe('removeBomStream', function () { ); }); - it('does not remove the BOM from a UTF16BE buffer', function (done) { + it('remove the BOM from a UTF8 buffer even if first chunk is shorter than 7 chars but second and subsequent are larger', function(done) { + var filepath = path.join(__dirname, './fixtures/bom-utf8.txt'); + var fileContent = fs.readFileSync(filepath, 'utf-8'); + + var rmBom = removeBomStream(); + var output = ''; + rmBom.on('data', function(d) { + output += d.toString(); + }); + rmBom.write(Buffer.from(fileContent.slice(0, 5))); + rmBom.write(Buffer.from(fileContent.slice(5))); + + expect(output).toEqual(fileContent.slice(1)); + done(); + }); + + it('does not remove the BOM from a UTF16BE buffer', function(done) { var filepath = path.join(__dirname, './fixtures/bom-utf16be.txt'); var expected = fs.readFileSync(filepath);