summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-11-03 13:27:26 -0700
committerRyan Dahl <ry@tinyclouds.org>2011-11-03 13:27:33 -0700
commita936768890db2e4b14f8994a84edb7121e6c82d0 (patch)
treed825ee4bbb07d925fd76d0bfc6a3ae69ee516684
parent4a8088a603387697f24c65653e279f9d841d74e5 (diff)
downloadnodejs-a936768890db2e4b14f8994a84edb7121e6c82d0.tar.gz
nodejs-a936768890db2e4b14f8994a84edb7121e6c82d0.tar.bz2
nodejs-a936768890db2e4b14f8994a84edb7121e6c82d0.zip
stdout and stderr are blocking when referring to regular files
Fixes message tests.
-rw-r--r--doc/api/process.markdown12
-rw-r--r--lib/fs.js61
-rw-r--r--src/node.js2
3 files changed, 73 insertions, 2 deletions
diff --git a/doc/api/process.markdown b/doc/api/process.markdown
index 98b095cae..365e97573 100644
--- a/doc/api/process.markdown
+++ b/doc/api/process.markdown
@@ -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
diff --git a/lib/fs.js b/lib/fs.js
index 678e24aa3..6f1f29872 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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;
diff --git a/src/node.js b/src/node.js
index c3f7f9f93..2e2fc1b33 100644
--- a/src/node.js
+++ b/src/node.js
@@ -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;