fs: minor corrections from examining stream read positioning
authorThomas Shinnick <tshinnic@gmail.com>
Thu, 8 Sep 2011 23:19:28 +0000 (18:19 -0500)
committerkoichik <koichik@improvement.jp>
Mon, 12 Sep 2011 05:57:43 +0000 (14:57 +0900)
Fix minor typos, one small refactor, and change emit() in a constructor
to a throw

lib/fs.js

index a4dc875..7a0e642 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -931,10 +931,10 @@ var ReadStream = fs.ReadStream = function(path, options) {
     }
 
     if (this.start > this.end) {
-      this.emit('error', new Error('start must be <= end'));
-    } else {
-      this._firstRead = true;
+      throw new Error('start must be <= end');
     }
+
+    this.pos = this.start;
   }
 
   if (this.fd !== null) {
@@ -965,9 +965,9 @@ ReadStream.prototype.setEncoding = function(encoding) {
 
 ReadStream.prototype._read = function() {
   var self = this;
-  if (!self.readable || self.paused || self.reading) return;
+  if (!this.readable || this.paused || this.reading) return;
 
-  self.reading = true;
+  this.reading = true;
 
   if (!pool || pool.length - pool.used < kMinPoolSpace) {
     // discard the old pool. Can't add to the free list because
@@ -976,11 +976,6 @@ ReadStream.prototype._read = function() {
     allocNewPool();
   }
 
-  if (self.start !== undefined && self._firstRead) {
-    self.pos = self.start;
-    self._firstRead = false;
-  }
-
   // Grab another reference to the pool in the case that while we're in the
   // thread pool another read() finishes up the pool, and allocates a new
   // one.
@@ -1025,10 +1020,10 @@ ReadStream.prototype._read = function() {
     self._read();
   }
 
-  fs.read(self.fd, pool, pool.used, toRead, self.pos, afterRead);
+  fs.read(this.fd, pool, pool.used, toRead, this.pos, afterRead);
 
-  if (self.pos !== undefined) {
-    self.pos += toRead;
+  if (this.pos !== undefined) {
+    this.pos += toRead;
   }
   pool.used += toRead;
 };
@@ -1135,7 +1130,7 @@ WriteStream.prototype.flush = function() {
 
   var args = this._queue.shift();
   if (!args) {
-    if (this.drainable) { self.emit('drain'); }
+    if (this.drainable) { this.emit('drain'); }
     return;
   }
 
@@ -1144,8 +1139,6 @@ WriteStream.prototype.flush = function() {
   var method = args.shift(),
       cb = args.pop();
 
-  var self = this;
-
   args.push(function(err) {
     self.busy = false;
 
@@ -1185,7 +1178,7 @@ WriteStream.prototype.flush = function() {
 
   // Inject the file pointer
   if (method !== fs.open) {
-    args.unshift(self.fd);
+    args.unshift(this.fd);
   }
 
   method.apply(this, args);
@@ -1193,7 +1186,7 @@ WriteStream.prototype.flush = function() {
 
 WriteStream.prototype.write = function(data) {
   if (!this.writable) {
-    this.emit("error", new Error('stream not writable'));
+    this.emit('error', new Error('stream not writable'));
     return false;
   }