Skip to content

Commit 7b2a84c

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

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

lib/file-operations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function worker(data, encoding, callback) {
398398
return this.once('open', onOpen);
399399
}
400400

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

403403
function onOpen() {
404404
self._write(data, encoding, callback);

test/dest.js

+28
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

@@ -33,10 +34,13 @@ var inputBase = testConstants.inputBase;
3334
var outputBase = testConstants.outputBase;
3435
var inputPath = testConstants.inputPath;
3536
var outputPath = testConstants.outputPath;
37+
var inputPathLarge = testConstants.inputPathLarge;
38+
var outputPathLarge = testConstants.outputPathLarge;
3639
var outputRenamePath = testConstants.outputRenamePath;
3740
var inputDirpath = testConstants.inputDirpath;
3841
var outputDirpath = testConstants.outputDirpath;
3942
var contents = testConstants.contents;
43+
var contentsLarge = testConstants.contentsLarge;
4044

4145
var clean = cleanup([outputBase]);
4246

@@ -304,6 +308,30 @@ describe('.dest()', function() {
304308
], done);
305309
});
306310

311+
it('writes large streaming files to the right folder', function(done) {
312+
var file = new File({
313+
base: inputBase,
314+
path: inputPathLarge,
315+
contents: string(contentsLarge),
316+
});
317+
318+
function assert(files) {
319+
var outputContents = fs.readFileSync(outputPathLarge, 'utf8');
320+
321+
expect(files.length).toEqual(1);
322+
expect(files).toInclude(file);
323+
expect(files[0].base).toEqual(outputBase, 'base should have changed');
324+
expect(files[0].path).toEqual(outputPathLarge, 'path should have changed');
325+
expect(outputContents).toEqual(contentsLarge);
326+
};
327+
328+
pipe([
329+
from.obj([file]),
330+
vfs.dest(outputBase),
331+
concat(assert),
332+
], done);
333+
});
334+
307335
it('writes directories to the right folder', function(done) {
308336
var file = new File({
309337
base: inputBase,

test/file-operations.js

+18
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,13 +36,17 @@ 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;
44+
var outputPathLarge = testConstants.outputPathLarge;
4145
var outputDirpath = testConstants.outputDirpath;
4246
var outputNestedPath = testConstants.outputNestedPath;
4347
var outputNestedDirpath = testConstants.outputNestedDirpath;
4448
var contents = testConstants.contents;
49+
var contentsLarge = testConstants.contentsLarge;
4550

4651
var clean = cleanup([outputBase]);
4752

@@ -1453,6 +1458,19 @@ describe('createWriteStream', function() {
14531458
], assert);
14541459
});
14551460

1461+
it('accepts just a file path and writes a large file to it', function(done) {
1462+
function assert(err) {
1463+
var outputContents = fs.readFileSync(outputPathLarge, 'utf8');
1464+
expect(outputContents).toEqual(contentsLarge);
1465+
done(err);
1466+
}
1467+
1468+
pipe([
1469+
string(contentsLarge),
1470+
createWriteStream(outputPathLarge),
1471+
], assert);
1472+
});
1473+
14561474
it('accepts flag option', function(done) {
14571475
// Write 12 stars then 12345 because the length of expected is 12
14581476
fs.writeFileSync(outputPath, '************12345');

test/utils/test-constants.js

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var outputBase = path.join(__dirname, '..', outputRelative);
1111
// Used for file tests
1212
var inputPath = path.join(inputBase, './test.txt');
1313
var outputPath = path.join(outputBase, './test.txt');
14+
var inputPathLarge = path.join(inputBase, './test-large.txt');
15+
var outputPathLarge = path.join(outputBase, './test-large.txt');
1416
// Used for directory tests
1517
var inputDirpath = path.join(inputBase, './foo');
1618
var outputDirpath = path.join(outputBase, './foo');
@@ -37,6 +39,12 @@ var symlinkNestedFirst = path.join(outputBase, './test-multi-layer-symlink');
3739
var symlinkNestedSecond = path.join(outputBase, './foo/baz-link.txt');
3840
// Used for contents of files
3941
var contents = 'Hello World!';
42+
var contentsLarge = '';
43+
var idx = 0;
44+
while (idx++ < 2000) {
45+
contentsLarge += '[' + idx + '] Hello World!\n';
46+
}
47+
4048

4149
module.exports = {
4250
inputRelative: inputRelative,
@@ -45,6 +53,8 @@ module.exports = {
4553
outputBase: outputBase,
4654
inputPath: inputPath,
4755
outputPath: outputPath,
56+
inputPathLarge: inputPathLarge,
57+
outputPathLarge: outputPathLarge,
4858
inputDirpath: inputDirpath,
4959
outputDirpath: outputDirpath,
5060
inputNestedPath: inputNestedPath,
@@ -63,4 +73,5 @@ module.exports = {
6373
symlinkNestedFirst: symlinkNestedFirst,
6474
symlinkNestedSecond: symlinkNestedSecond,
6575
contents: contents,
76+
contentsLarge: contentsLarge,
6677
};

test/utils/test-streams.js

+15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@ 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(str) {
11+
return from(function(size, next) {
12+
if (str.length <= 0) {
13+
next(null, null);
14+
return;
15+
}
16+
17+
var chunk = str.slice(0, size);
18+
str = str.slice(size);
19+
next(null, chunk);
20+
});
21+
}
22+
923
function rename(filepath) {
1024
return through.obj(function(file, enc, cb) {
1125
file.path = filepath;
@@ -46,6 +60,7 @@ function slowCount(value) {
4660
}
4761

4862
module.exports = {
63+
string: string,
4964
rename: rename,
5065
includes: includes,
5166
count: count,

0 commit comments

Comments
 (0)