Safe constructors for fs.ReadStream and fs.WriteStream
authorRyan Dahl <ry@tinyclouds.org>
Fri, 17 Sep 2010 02:04:09 +0000 (19:04 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 17 Sep 2010 02:04:09 +0000 (19:04 -0700)
lib/fs.js
test/simple/test-fs-read-stream.js
test/simple/test-fs-write-stream.js

index 4e1d8b9..8f09235 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -598,6 +598,8 @@ fs.createReadStream = function(path, options) {
 };
 
 var ReadStream = fs.ReadStream = function(path, options) {
+  if (!(this instanceof ReadStream)) return new ReadStream(path, options);
+
   events.EventEmitter.call(this);
 
   var self = this;
@@ -794,6 +796,8 @@ fs.createWriteStream = function(path, options) {
 };
 
 var WriteStream = fs.WriteStream = function(path, options) {
+  if (!(this instanceof WriteStream)) return new WriteStream(path, options);
+
   events.EventEmitter.call(this);
 
   this.path = path;
index db1275a..b008e91 100644 (file)
@@ -15,7 +15,7 @@ callbacks = { open: 0, end: 0, close: 0, destroy: 0 };
 
 paused = false;
 
-file = fs.createReadStream(fn);
+file = fs.ReadStream(fn);
 
 file.addListener('open', function(fd) {
   file.length = 0;
index 6679536..14ad62e 100644 (file)
@@ -7,7 +7,7 @@ var path = require('path'),
 var file = path.join(common.fixturesDir, "write.txt");
 
 (function() {
-  var stream = fs.createWriteStream(file),
+  var stream = fs.WriteStream(file),
       _fs_close = fs.close;
       
   fs.close = function(fd) {