Skip to content

Commit 8e79ae0

Browse files
doowbphated
authored andcommitted
Fix: Avoid setting a position in custom write stream (fixes #202) (#203)
1 parent d11401a commit 8e79ae0

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

lib/file-operations.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ function WriteStream(path, options, flush) {
349349

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

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

401-
fs.write(this.fd, data, 0, data.length, this.pos, onWrite);
400+
fs.write(this.fd, data, 0, data.length, null, onWrite);
402401

403402
function onOpen() {
404403
self._write(data, encoding, callback);

test/dest.js

+25
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var count = testStreams.count;
2525
var rename = testStreams.rename;
2626
var includes = testStreams.includes;
2727
var slowCount = testStreams.slowCount;
28+
var string = testStreams.string;
2829

2930
function noop() {}
3031

@@ -304,6 +305,30 @@ describe('.dest()', function() {
304305
], done);
305306
});
306307

308+
it('writes large streaming files to the right folder', function(done) {
309+
var size = 40000;
310+
311+
var file = new File({
312+
base: inputBase,
313+
path: inputPath,
314+
contents: string(size),
315+
});
316+
317+
function assert(files) {
318+
var stats = fs.lstatSync(outputPath);
319+
320+
expect(files.length).toEqual(1);
321+
expect(files).toInclude(file);
322+
expect(stats.size).toEqual(size);
323+
};
324+
325+
pipe([
326+
from.obj([file]),
327+
vfs.dest(outputBase),
328+
concat(assert),
329+
], done);
330+
});
331+
307332
it('writes directories to the right folder', function(done) {
308333
var file = new File({
309334
base: inputBase,

test/file-operations.js

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var statMode = require('./utils/stat-mode');
1919
var mockError = require('./utils/mock-error');
2020
var isWindows = require('./utils/is-windows');
2121
var applyUmask = require('./utils/apply-umask');
22+
var testStreams = require('./utils/test-streams');
2223
var testConstants = require('./utils/test-constants');
2324

2425
var mkdirp = fo.mkdirp;
@@ -35,6 +36,8 @@ var createWriteStream = fo.createWriteStream;
3536
var pipe = miss.pipe;
3637
var from = miss.from;
3738

39+
var string = testStreams.string;
40+
3841
var outputBase = testConstants.outputBase;
3942
var inputPath = testConstants.inputPath;
4043
var outputPath = testConstants.outputPath;
@@ -1453,6 +1456,22 @@ describe('createWriteStream', function() {
14531456
], assert);
14541457
});
14551458

1459+
it('accepts just a file path and writes a large file to it', function(done) {
1460+
var size = 40000;
1461+
1462+
function assert(err) {
1463+
var stats = fs.lstatSync(outputPath);
1464+
1465+
expect(stats.size).toEqual(size);
1466+
done(err);
1467+
}
1468+
1469+
pipe([
1470+
string(size),
1471+
createWriteStream(outputPath),
1472+
], assert);
1473+
});
1474+
14561475
it('accepts flag option', function(done) {
14571476
// Write 12 stars then 12345 because the length of expected is 12
14581477
fs.writeFileSync(outputPath, '************12345');

test/utils/test-streams.js

+22
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ var miss = require('mississippi');
44
var expect = require('expect');
55

66
var to = miss.to;
7+
var from = miss.from;
78
var through = miss.through;
89

10+
function string(length) {
11+
return from(function(size, next) {
12+
if (length <= 0) {
13+
next(null, null);
14+
return;
15+
}
16+
17+
var chunkSize = size <= length ? size : length;
18+
19+
length -= size;
20+
21+
var chunk = '';
22+
for (var x = 0; x < chunkSize; x++) {
23+
chunk += 'a';
24+
}
25+
26+
next(null, chunk);
27+
});
28+
}
29+
930
function rename(filepath) {
1031
return through.obj(function(file, enc, cb) {
1132
file.path = filepath;
@@ -46,6 +67,7 @@ function slowCount(value) {
4667
}
4768

4869
module.exports = {
70+
string: string,
4971
rename: rename,
5072
includes: includes,
5173
count: count,

0 commit comments

Comments
 (0)