Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
stdout and stderr are blocking when referring to regular files
Fixes message tests.
  • Loading branch information
ry committed Nov 3, 2011
1 parent 4a8088a commit a936768
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
12 changes: 11 additions & 1 deletion doc/api/process.markdown
Expand Up @@ -80,10 +80,20 @@ Example: the definition of `console.log`
process.stdout.write(d + '\n');
};

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


### process.stderr

A writable stream to stderr. Writes on this stream are blocking.
A writable stream to stderr.

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


### process.stdin
Expand Down
61 changes: 61 additions & 0 deletions lib/fs.js
Expand Up @@ -1337,3 +1337,64 @@ WriteStream.prototype.destroy = function(cb) {
// There is no shutdown() for files.
WriteStream.prototype.destroySoon = WriteStream.prototype.end;


// SyncWriteStream is internal. DO NOT USE.
// Temporary hack for process.stdout and process.stderr when piped to files.
function SyncWriteStream(fd) {
this.fd = fd;
this.writable = true;
this.readable = false;
};
util.inherits(SyncWriteStream, Stream);


// Export
fs.SyncWriteStream = SyncWriteStream;


SyncWriteStream.prototype.write = function(data, arg1, arg2) {
var encoding, cb;

// parse arguments
if (arg1) {
if (typeof arg1 === 'string') {
encoding = arg1;
cb = arg2;
} else if (typeof arg1 === 'function') {
cb = arg1;
} else {
throw new Error("bad arg");
}
}

// Change strings to buffers. SLOW
if (typeof data == 'string') {
data = new Buffer(data, encoding);
}

fs.writeSync(this.fd, data, 0, data.length);

if (cb) {
process.nextTick(cb);
}

return true;
};


SyncWriteStream.prototype.end = function(data, arg1, arg2) {
if (data) {
this.write(data, arg1, arg2);
}
this.destroy();
};


SyncWriteStream.prototype.destroy = function() {
fs.closeSync(this.fd);
this.fd = null;
this.emit('close');
return true;
};

SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;
2 changes: 1 addition & 1 deletion src/node.js
Expand Up @@ -239,7 +239,7 @@

case 'FILE':
var fs = NativeModule.require('fs');
stream = new fs.WriteStream(null, { fd: fd });
stream = new fs.SyncWriteStream(fd);
stream._type = 'fs';
break;

Expand Down

0 comments on commit a936768

Please sign in to comment.