Skip to content

Commit a936768

Browse files
committed
stdout and stderr are blocking when referring to regular files
Fixes message tests.
1 parent 4a8088a commit a936768

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

doc/api/process.markdown

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,20 @@ Example: the definition of `console.log`
8080
process.stdout.write(d + '\n');
8181
};
8282

83+
`process.stderr` and `process.stdout` are unlike other streams in Node in
84+
that writes to them are usually blocking. They are blocking in the case
85+
that they refer to regular files or TTY file descriptors. In the case they
86+
refer to pipes, they are non-blocking like other streams.
87+
8388

8489
### process.stderr
8590

86-
A writable stream to stderr. Writes on this stream are blocking.
91+
A writable stream to stderr.
92+
93+
`process.stderr` and `process.stdout` are unlike other streams in Node in
94+
that writes to them are usually blocking. They are blocking in the case
95+
that they refer to regular files or TTY file descriptors. In the case they
96+
refer to pipes, they are non-blocking like other streams.
8797

8898

8999
### process.stdin

lib/fs.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,3 +1337,64 @@ WriteStream.prototype.destroy = function(cb) {
13371337
// There is no shutdown() for files.
13381338
WriteStream.prototype.destroySoon = WriteStream.prototype.end;
13391339

1340+
1341+
// SyncWriteStream is internal. DO NOT USE.
1342+
// Temporary hack for process.stdout and process.stderr when piped to files.
1343+
function SyncWriteStream(fd) {
1344+
this.fd = fd;
1345+
this.writable = true;
1346+
this.readable = false;
1347+
};
1348+
util.inherits(SyncWriteStream, Stream);
1349+
1350+
1351+
// Export
1352+
fs.SyncWriteStream = SyncWriteStream;
1353+
1354+
1355+
SyncWriteStream.prototype.write = function(data, arg1, arg2) {
1356+
var encoding, cb;
1357+
1358+
// parse arguments
1359+
if (arg1) {
1360+
if (typeof arg1 === 'string') {
1361+
encoding = arg1;
1362+
cb = arg2;
1363+
} else if (typeof arg1 === 'function') {
1364+
cb = arg1;
1365+
} else {
1366+
throw new Error("bad arg");
1367+
}
1368+
}
1369+
1370+
// Change strings to buffers. SLOW
1371+
if (typeof data == 'string') {
1372+
data = new Buffer(data, encoding);
1373+
}
1374+
1375+
fs.writeSync(this.fd, data, 0, data.length);
1376+
1377+
if (cb) {
1378+
process.nextTick(cb);
1379+
}
1380+
1381+
return true;
1382+
};
1383+
1384+
1385+
SyncWriteStream.prototype.end = function(data, arg1, arg2) {
1386+
if (data) {
1387+
this.write(data, arg1, arg2);
1388+
}
1389+
this.destroy();
1390+
};
1391+
1392+
1393+
SyncWriteStream.prototype.destroy = function() {
1394+
fs.closeSync(this.fd);
1395+
this.fd = null;
1396+
this.emit('close');
1397+
return true;
1398+
};
1399+
1400+
SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;

src/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239

240240
case 'FILE':
241241
var fs = NativeModule.require('fs');
242-
stream = new fs.WriteStream(null, { fd: fd });
242+
stream = new fs.SyncWriteStream(fd);
243243
stream._type = 'fs';
244244
break;
245245

0 commit comments

Comments
 (0)