From 76511ddca04d9ec24194327593ec4f627c7eaba2 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 27 Sep 2015 14:22:30 -0700 Subject: [PATCH] add option to strip BOM, default to true - closes #83 --- README.md | 4 ++++ lib/src/getContents/bufferFile.js | 8 +++++++- lib/src/getContents/streamFile.js | 8 ++++++-- lib/src/index.js | 1 + test/src.js | 22 +++++++++++++++++++++- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dd1412aa..510589dd 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/lib/src/getContents/bufferFile.js b/lib/src/getContents/bufferFile.js index ebd225e0..3c0cfa06 100644 --- a/lib/src/getContents/bufferFile.js +++ b/lib/src/getContents/bufferFile.js @@ -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); }); } diff --git a/lib/src/getContents/streamFile.js b/lib/src/getContents/streamFile.js index 23504d8c..4ad5984f 100644 --- a/lib/src/getContents/streamFile.js +++ b/lib/src/getContents/streamFile.js @@ -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); } diff --git a/lib/src/index.js b/lib/src/index.js index 136b8ced..a1341b91 100644 --- a/lib/src/index.js +++ b/lib/src/index.js @@ -21,6 +21,7 @@ function src(glob, opt) { var options = assign({ read: true, buffer: true, + stripBOM: true, sourcemaps: false, passthrough: false, followSymlinks: true diff --git a/test/src.js b/test/src.js index 2dc3c606..f9f7d781 100644 --- a/test/src.js +++ b/test/src.js @@ -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 @@ -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.