Skip to content

Commit 564f87b

Browse files
authored
fix: Handle first chunk shorter than 7 bytes correctly when next chunks are larger (#7)
1 parent bc47af6 commit 564f87b

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@ var through = require('through2');
44
var removeBom = require('remove-bom-buffer');
55

66
function removeBomStream() {
7-
var completed = false;
7+
var state = 0; // 0:Not removed, -1:In removing, 1:Already removed
88
var buffer = Buffer.alloc(0);
99

1010
return through(onChunk, onFlush);
1111

1212
function removeAndCleanup(data) {
13-
completed = true;
13+
state = 1; // Already removed
1414

1515
buffer = null;
1616

1717
return removeBom(data);
1818
}
1919

2020
function onChunk(data, enc, cb) {
21-
if (completed) {
21+
if (state === 1) {
2222
return cb(null, data);
2323
}
2424

25-
if (data.length >= 7) {
25+
if (state === 0 /* Not removed */ && data.length >= 7) {
2626
return cb(null, removeAndCleanup(data));
2727
}
2828

29+
state = -1; // In removing
30+
2931
var bufferLength = buffer.length;
3032
var chunkLength = data.length;
3133
var totalLength = bufferLength + chunkLength;
@@ -39,7 +41,7 @@ function removeBomStream() {
3941
}
4042

4143
function onFlush(cb) {
42-
if (completed || !buffer) {
44+
if (state === 2 /* Already removed */ || !buffer) {
4345
return cb();
4446
}
4547

test/index.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ describe('removeBomStream', function () {
2929
);
3030
});
3131

32-
it('removes the BOM from a UTF8 buffer', function (done) {
32+
it('ignores UTF8 buffer without a BOM even if first chunk is shorter than 7 chars but second and subsequent are larger', function(done) {
33+
var filepath = path.join(__dirname, './fixtures/test.txt');
34+
var fileContent = fs.readFileSync(filepath, 'utf-8');
35+
36+
var rmBom = removeBomStream();
37+
var output = '';
38+
rmBom.on('data', function(d) {
39+
output += d.toString();
40+
});
41+
rmBom.write(Buffer.from(fileContent.slice(0, 5)));
42+
rmBom.write(Buffer.from(fileContent.slice(5)));
43+
44+
expect(output).toEqual(fileContent);
45+
done();
46+
});
47+
48+
it('removes the BOM from a UTF8 buffer', function(done) {
3349
var filepath = path.join(__dirname, './fixtures/bom-utf8.txt');
3450

3551
var expected = fs.readFileSync(filepath).slice(3);
@@ -81,7 +97,23 @@ describe('removeBomStream', function () {
8197
);
8298
});
8399

84-
it('does not remove the BOM from a UTF16BE buffer', function (done) {
100+
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) {
101+
var filepath = path.join(__dirname, './fixtures/bom-utf8.txt');
102+
var fileContent = fs.readFileSync(filepath, 'utf-8');
103+
104+
var rmBom = removeBomStream();
105+
var output = '';
106+
rmBom.on('data', function(d) {
107+
output += d.toString();
108+
});
109+
rmBom.write(Buffer.from(fileContent.slice(0, 5)));
110+
rmBom.write(Buffer.from(fileContent.slice(5)));
111+
112+
expect(output).toEqual(fileContent.slice(1));
113+
done();
114+
});
115+
116+
it('does not remove the BOM from a UTF16BE buffer', function(done) {
85117
var filepath = path.join(__dirname, './fixtures/bom-utf16be.txt');
86118

87119
var expected = fs.readFileSync(filepath);

0 commit comments

Comments
 (0)