Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Require encoding option to avoid inspecting chunks for UTF-8 encoding #8

Merged
merged 7 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var concat = require('concat-stream');
var removeBOM = require('remove-bom-stream');

fs.createReadStream('utf8-file-with-bom.txt')
.pipe(removeBOM())
.pipe(removeBOM('utf-8'))
.pipe(
concat(function (result) {
// result won't have a BOM
Expand All @@ -28,9 +28,9 @@ fs.createReadStream('utf8-file-with-bom.txt')

## API

### `removeBOM()`
### `removeBOM(encoding)`

Returns a `through2` stream that will remove a BOM, given the data is a UTF8 Buffer with a BOM at the beginning. If the data is not UTF8 or does not have a BOM, the data is not changed and this becomes a normal passthrough stream.
Returns a `through2` stream that will remove a BOM, if the argument `encoding` is `'utf-8'` and the given data is a UTF8 Buffer with a BOM at the beginning. If the `encoding` is not `'utf-8'` or does not have a BOM, the data is not changed and this becomes a normal passthrough stream.

## License

Expand Down
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

var through = require('through2');
var removeBom = require('remove-bom-buffer');
var TextDecoder = require('util').TextDecoder;

var removeBom = new TextDecoder('utf-8', { ignoreBOM: false });

function removeBomStream(encoding) {
encoding = (encoding || '').toLowerCase();
var isUtf8 = (encoding === 'utf-8' || encoding === 'utf8');

function removeBomStream() {
var state = 0; // 0:Not removed, -1:In removing, 1:Already removed
var buffer = Buffer.alloc(0);

Expand All @@ -14,7 +19,10 @@ function removeBomStream() {

buffer = null;

return removeBom(data);
if (isUtf8) {
return removeBom.decode(data);
phated marked this conversation as resolved.
Show resolved Hide resolved
}
return data;
}

function onChunk(data, enc, cb) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"test": "nyc mocha --async-only"
},
"dependencies": {
"remove-bom-buffer": "^3.0.0",
"through2": "^4.0.2"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-8'), concat(assert)],
done
);
});
Expand All @@ -33,7 +33,7 @@ describe('removeBomStream', function () {
var filepath = path.join(__dirname, './fixtures/test.txt');
var fileContent = fs.readFileSync(filepath, 'utf-8');

var rmBom = removeBomStream();
var rmBom = removeBomStream('utf8');
var output = '';
rmBom.on('data', function (d) {
output += d.toString();
Expand All @@ -55,7 +55,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)],
done
);
});
Expand All @@ -73,7 +73,7 @@ describe('removeBomStream', function () {
[
fs.createReadStream(filepath),
chunker(1),
removeBomStream(),
removeBomStream('UTF8'),
concat(assert),
],
done
Expand All @@ -92,7 +92,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)],
done
);
});
Expand All @@ -101,7 +101,7 @@ describe('removeBomStream', function () {
var filepath = path.join(__dirname, './fixtures/bom-utf8.txt');
var fileContent = fs.readFileSync(filepath, 'utf-8');

var rmBom = removeBomStream();
var rmBom = removeBomStream('utf-8');
var output = '';
rmBom.on('data', function (d) {
output += d.toString();
Expand All @@ -123,7 +123,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)],
done
);
});
Expand All @@ -138,7 +138,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)],
done
);
});
Expand All @@ -153,7 +153,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16le'), concat(assert)],
done
);
});
Expand Down