Skip to content

Commit

Permalink
remove stripBOM flag, document BOM stripping. #72
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Jun 30, 2015
1 parent c016215 commit f6ead3c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ fs.src(['*.js', '!b*.js'])
- read - `true` or `false` if you want the file to be read or not. Useful for stuff like `rm`ing files.
- Default value is `true`
- `false` will disable writing the file to disk via `.dest()`
- stripBOM - `true` or `false` if you want UTF-8 BOM stripped from files as they are read.
- Default value is `true`
- since - `Date` or `number` if you only want files that have been modified since the time specified.
- passthrough - `true` or `false` if you want a duplex stream which passes items through and emits globbed files.
- Default is `false`.
Expand All @@ -68,6 +70,8 @@ fs.src(['*.js', '!b*.js'])
- Returns a Readable stream by default, or a Duplex stream if the `passthrough` option is set to `true`.
- This stream emits matching [vinyl] File objects

_Note:_ UTF-8 BOM will be stripped from all files read with `.src`

### watch(globs[, opt, cb])

This is just [glob-watcher]
Expand Down
2 changes: 1 addition & 1 deletion lib/dest/writeContents/writeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function writeStream(writePath, file, cb) {
file.contents.pipe(outStream);

function success() {
streamFile(file, complete);
streamFile(file, {}, complete);
}

// cleanup
Expand Down
4 changes: 2 additions & 2 deletions lib/src/getContents/bufferFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
var fs = require('graceful-fs');
var stripBom = require('strip-bom');

function bufferFile(file, cb) {
function bufferFile(file, opt, cb) {
fs.readFile(file.path, function(err, data) {
if (err) {
return cb(err);
}
file.contents = stripBom(data);
file.contents = opt.stripBOM ? stripBom(data) : data;
cb(null, file);
});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/getContents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ function getContents(opt) {
return through2.obj(function(file, enc, cb) {
// don't fail to read a directory
if (file.isDirectory()) {
return readDir(file, cb);
return readDir(file, opt, cb);
}

// read and pass full contents
if (opt.buffer !== false) {
return bufferFile(file, cb);
return bufferFile(file, opt, cb);
}

// dont buffer anything - just pass streams
return streamFile(file, cb);
return streamFile(file, opt, cb);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/getContents/readDir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function readDir(file, cb) {
function readDir(file, opt, cb) {
// do nothing for now
cb(null, file);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/src/getContents/streamFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
var fs = require('graceful-fs');
var stripBom = require('strip-bom');

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

function streamFile(file, opt, cb) {
file.contents = fs.createReadStream(file.path);
if (opt.stripBOM) {
file.contents = file.contents.pipe(stripBom.stream());
}
cb(null, file);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ function createFile(globFile, enc, cb) {

function src(glob, opt) {
var options = assign({
stripBOM: true,
read: true,
buffer: true,
sourcemaps: false,
passthrough: false
}, opt);

var pass, inputPass;

if (!isValidGlob(glob)) {
Expand Down
22 changes: 22 additions & 0 deletions test/src.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ describe('source stream', function() {
stream.pipe(bufferStream);
});

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

var onEnd = function(){
should.exist(buffered[0].stat);
buffered[0].path.should.equal(expectedPath);
buffered[0].isBuffer().should.equal(true);
buffered[0].contents.length.should.equal(expectedContent.length);
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 f6ead3c

Please sign in to comment.