Skip to content

Commit

Permalink
Fix: Avoid setting a position in custom write stream (fixes #202) (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb authored and phated committed Nov 28, 2017
1 parent 7a44070 commit 2432b80
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ function WriteStream(path, options, flush) {

this.mode = options.mode || constants.DEFAULT_FILE_MODE;
this.flag = options.flag || 'w';
this.pos = APPEND_MODE_REGEXP.test(this.flag) ? null : 0;;

// Used by node's `fs.WriteStream`
this.fd = null;
Expand Down Expand Up @@ -398,7 +397,7 @@ function worker(data, encoding, callback) {
return this.once('open', onOpen);
}

fs.write(this.fd, data, 0, data.length, this.pos, onWrite);
fs.write(this.fd, data, 0, data.length, null, onWrite);

function onOpen() {
self._write(data, encoding, callback);
Expand Down
25 changes: 25 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var count = testStreams.count;
var rename = testStreams.rename;
var includes = testStreams.includes;
var slowCount = testStreams.slowCount;
var string = testStreams.string;

function noop() {}

Expand Down Expand Up @@ -304,6 +305,30 @@ describe('.dest()', function() {
], done);
});

it('writes large streaming files to the right folder', function(done) {
var size = 40000;

var file = new File({
base: inputBase,
path: inputPath,
contents: string(size),
});

function assert(files) {
var stats = fs.lstatSync(outputPath);

expect(files.length).toEqual(1);
expect(files).toInclude(file);
expect(stats.size).toEqual(size);
};

pipe([
from.obj([file]),
vfs.dest(outputBase),
concat(assert),
], done);
});

it('writes directories to the right folder', function(done) {
var file = new File({
base: inputBase,
Expand Down
19 changes: 19 additions & 0 deletions test/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var statMode = require('./utils/stat-mode');
var mockError = require('./utils/mock-error');
var isWindows = require('./utils/is-windows');
var applyUmask = require('./utils/apply-umask');
var testStreams = require('./utils/test-streams');
var testConstants = require('./utils/test-constants');

var mkdirp = fo.mkdirp;
Expand All @@ -35,6 +36,8 @@ var createWriteStream = fo.createWriteStream;
var pipe = miss.pipe;
var from = miss.from;

var string = testStreams.string;

var outputBase = testConstants.outputBase;
var inputPath = testConstants.inputPath;
var outputPath = testConstants.outputPath;
Expand Down Expand Up @@ -1453,6 +1456,22 @@ describe('createWriteStream', function() {
], assert);
});

it('accepts just a file path and writes a large file to it', function(done) {
var size = 40000;

function assert(err) {
var stats = fs.lstatSync(outputPath);

expect(stats.size).toEqual(size);
done(err);
}

pipe([
string(size),
createWriteStream(outputPath),
], assert);
});

it('accepts flag option', function(done) {
// Write 12 stars then 12345 because the length of expected is 12
fs.writeFileSync(outputPath, '************12345');
Expand Down
22 changes: 22 additions & 0 deletions test/utils/test-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@ var miss = require('mississippi');
var expect = require('expect');

var to = miss.to;
var from = miss.from;
var through = miss.through;

function string(length) {
return from(function(size, next) {
if (length <= 0) {
next(null, null);
return;
}

var chunkSize = size <= length ? size : length;

length -= size;

var chunk = '';
for (var x = 0; x < chunkSize; x++) {
chunk += 'a';
}

next(null, chunk);
});
}

function rename(filepath) {
return through.obj(function(file, enc, cb) {
file.path = filepath;
Expand Down Expand Up @@ -46,6 +67,7 @@ function slowCount(value) {
}

module.exports = {
string: string,
rename: rename,
includes: includes,
count: count,
Expand Down

0 comments on commit 2432b80

Please sign in to comment.