Skip to content

Commit

Permalink
add option to strip BOM, default to true - closes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 28, 2017
1 parent 925bedf commit d568dd1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fs.src(['*.js', '!b*.js'])
- `false` will disable writing the file to disk via `.dest()`.

- since - `Date` or `number` if you only want files that have been modified since the time specified.

- stripBOM - `true` or `false` if you want the BOM to be stripped on UTF-8 encoded files.
- Default value is `true`.

- passthrough - `true` or `false` if you want a duplex stream which passes items through and emits globbed files.
- Default is `false`.

Expand Down
8 changes: 7 additions & 1 deletion lib/src/getContents/bufferFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ function bufferFile(file, opt, cb) {
if (err) {
return cb(err);
}
file.contents = stripBom(data);

if (opt.stripBOM){
file.contents = stripBom(data);
} else {
file.contents = data;
}

cb(null, file);
});
}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/getContents/streamFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ var fs = require('graceful-fs');
var stripBom = require('strip-bom-stream');

function streamFile(file, opt, cb) {
file.contents = fs.createReadStream(file.path)
.pipe(stripBom());
file.contents = fs.createReadStream(file.path);

if (opt.stripBOM) {
file.contents = file.contents.pipe(stripBom());
}

cb(null, file);
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function src(glob, opt) {
var options = assign({
read: true,
buffer: true,
stripBOM: true,
sourcemaps: false,
passthrough: false,
followSymlinks: true
Expand Down
22 changes: 21 additions & 1 deletion test/src.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('source stream', function() {
stream.write(expectedFile);
});

it('should strip BOM from UTF-8-encoded files', function(done) {
it('should strip BOM from UTF-8-encoded files by default', function(done) {
var expectedPath = path.join(__dirname, './fixtures/bom-utf8.txt');
var expectedContent = fs.readFileSync(expectedPath)
// U+FEFF takes up 3 bytes in UTF-8: http://mothereff.in/utf-8#%EF%BB%BF
Expand All @@ -143,6 +143,26 @@ describe('source stream', function() {
stream.pipe(bufferStream);
});

it('should not strip BOM from UTF-8-encoded files if option is false', function(done) {
var expectedPath = path.join(__dirname, './fixtures/bom-utf8.txt');
var expectedContent = fs.readFileSync(expectedPath);

var onEnd = function(){
buffered.length.should.equal(1);
should.exist(buffered[0].stat);
buffered[0].path.should.equal(expectedPath);
buffered[0].isBuffer().should.equal(true);
bufEqual(buffered[0].contents, expectedContent).should.equal(true);
done();
};

var stream = vfs.src('./fixtures/bom-utf8.txt', {cwd: __dirname, stripBOM: false});

var buffered = [];
bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);
stream.pipe(bufferStream);
});

it('should not strip anything that looks like a UTF-8-encoded BOM from UTF-16-BE-encoded files', function(done) {
// Note: this goes for any non-UTF-8 encoding, but testing for UTF-16-BE
// and UTF-16-LE is enough to demonstrate this is done properly.
Expand Down

0 comments on commit d568dd1

Please sign in to comment.