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

Commit

Permalink
fs: minor corrections from examining stream read positioning
Browse files Browse the repository at this point in the history
Fix minor typos, one small refactor, and change emit() in a constructor
to a throw
  • Loading branch information
tshinnic authored and koichik committed Sep 12, 2011
1 parent 389e2a0 commit e4ebeb6
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down Expand Up @@ -1185,15 +1178,15 @@ WriteStream.prototype.flush = function() {

// Inject the file pointer
if (method !== fs.open) {
args.unshift(self.fd);
args.unshift(this.fd);
}

method.apply(this, args);
};

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;
}

Expand Down

0 comments on commit e4ebeb6

Please sign in to comment.