Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Start of a thing to do fs.readStream that can start at an index, but …

…without knowing the end
  • Loading branch information...
commit 69c2fa1f143e53a3cefe5d1b5640397dcebb74f3 1 parent e3ffad1
Isaac Z. Schlueter authored
18  lib/fs.js
@@ -629,12 +629,12 @@ var ReadStream = fs.ReadStream = function(path, options) {
629 629
   }
630 630
 
631 631
   if(this.encoding) this.setEncoding(this.encoding);
632  
-
633  
-  if (this.start !== undefined || this.end !== undefined) {
634  
-    if (this.start === undefined || this.end === undefined) {
635  
-      this.emit('error',
636  
-        new Error('Both start and end are needed for range streaming.'));
637  
-    } else if (this.start > this.end) {
  632
+  
  633
+  if (this.start !== undefined) {
  634
+    if (this.end === undefined) {
  635
+      this.end = Infinity;
  636
+    }
  637
+    if (this.start > this.end) {
638 638
       this.emit('error', new Error('start must be <= end'));
639 639
     } else {
640 640
       this.firstRead = true;
@@ -691,6 +691,7 @@ ReadStream.prototype._read = function () {
691 691
 
692 692
   if (this.pos !== undefined) {
693 693
     toRead = Math.min(this.end - this.pos + 1, toRead);
  694
+    console.error([this.pos, this.end, toRead], "pos, end, toRead")
694 695
   }
695 696
 
696 697
   function afterRead (err, bytesRead) {
@@ -699,7 +700,7 @@ ReadStream.prototype._read = function () {
699 700
       self.readable = false;
700 701
       return;
701 702
     }
702  
-
  703
+    
703 704
     if (bytesRead === 0) {
704 705
       self.emit('end');
705 706
       self.destroy();
@@ -707,6 +708,7 @@ ReadStream.prototype._read = function () {
707 708
     }
708 709
 
709 710
     var b = thisPool.slice(start, start+bytesRead);
  711
+    console.error([err, bytesRead, start, b+"", self.pos],  "er, bytesRead, start, b, pos")
710 712
 
711 713
     // Possible optimizition here?
712 714
     // Reclaim some bytes if bytesRead < toRead?
@@ -727,6 +729,8 @@ ReadStream.prototype._read = function () {
727 729
 
728 730
   // pass null for position after we've seeked to the start of a range read
729 731
   // always pass null on a non-range read
  732
+  Buffer.prototype.inspect = function () { return "BUFFER["+this.length+"]" }
  733
+  console.error([self.fd, pool, pool.used, toRead, self.firstRead, (self.firstRead ? self.pos : null)], "fd, buffer, offset, length, position")
730 734
   fs.read(self.fd, pool, pool.used, toRead, (self.firstRead ? self.pos : null), afterRead);
731 735
   self.firstRead = false;
732 736
 
19  test/simple/test-fs-read-stream.js
@@ -11,6 +11,19 @@ fs = require('fs');
11 11
 fn = path.join(common.fixturesDir, 'elipses.txt');
12 12
 rangeFile = path.join(common.fixturesDir, 'x.txt');
13 13
 
  14
+
  15
+var file5 = fs.createReadStream(rangeFile, {start: 1});
  16
+var contentRead = '';
  17
+file5.addListener('data', function(data) {
  18
+	contentRead += data.toString('utf-8');
  19
+});
  20
+file5.addListener('end', function(data) {
  21
+	assert.equal(contentRead, 'yz\n');
  22
+});
  23
+
  24
+return
  25
+
  26
+
14 27
 callbacks = { open: 0, end: 0, close: 0, destroy: 0 };
15 28
 
16 29
 paused = false;
@@ -103,12 +116,6 @@ try {
103 116
   assert.equal(e.message, 'start must be <= end');
104 117
 }
105 118
 
106  
-try {
107  
-  fs.createReadStream(rangeFile, {start: 2});
108  
-  assert.fail('Creating a ReadStream with a only one range limits must throw.');
109  
-} catch(e) {
110  
-  assert.equal(e.message, 'Both start and end are needed for range streaming.');
111  
-}
112 119
 
113 120
 var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
114 121
 stream.data = '';

0 notes on commit 69c2fa1

Please sign in to comment.
Something went wrong with that request. Please try again.