help icon indicating copy to clipboard operation
help copied to clipboard

Extends from fs.ReadStream

Open lzszone opened this issue 7 years ago • 6 comments

  • Node.js Version: 12.20
  • OS: windows 10 with latest updates
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): no

My purpose:

I want to write something that can both emit custom events and handle file system operations(e.g. write or read a file), and I want to call the _read method of the super class inside a child class. I think I could just extend from fs.ReadStream or fs.WriteStream, but I got an error.


Example:

const {ReadStream} = require('fs');
const path = require('path');

class RS extends ReadStream {
    constructor(path, options) {
        super(path, options);
    }

    _read(n) {
        // doSomething()
        return this.constructor.prototype._read(n)
    }
}

const rs = new RS(path.resolve(__dirname, './a.file'), {});
rs.on('ready', function() => {
    rs.read(0)
})

Error:

 _read(n) {
         ^

RangeError: Maximum call stack size exceeded
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:9:10)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)
    at ReadStream._read (C:\Users\q\Documents\file_transfer\client\view\dist\test.js:10:43)

What should I do?

lzszone avatar Jun 10 '19 02:06 lzszone

Change this.constructor.prototype._read(n) to super._read(n)

Hakerh400 avatar Jun 10 '19 11:06 Hakerh400

Thank you for answering, and it works...:D @Hakerh400 Could you please spend a little more time to explain what's the difference between those two?

I also try to use a non-class way to solve this, like:

const {ReadStream} = require('fs');
const path = require('path');

function RS(url, options) {
    ReadStream.call(this, url, options);
    // ....
}
Object.setPrototypeOf(ReadStream.prototype, RS.prototype);
Object.setProtoTypeOf(ReadStream, RS);

RS.prototype._read(n) {
    this.constructor.prototype._read(n);
    //this.constructor.prototype._read.call(this, n);  //maybe I call like this
}

The above does not work neither, throws a same error, and there is no super to call, what's the problem with my code?

lzszone avatar Jun 11 '19 03:06 lzszone

try this

class RS extends ReadStream {
  constructor(path, options) {
    super(path, options);
  }

  _read(n) {
    // doSomething()
    return super._read(n)
  }
}

devsnek avatar Jun 11 '19 03:06 devsnek

@devsnek Yes, it works. Could you take some time to explain my question above, please?

lzszone avatar Jun 11 '19 03:06 lzszone

this.constructor.prototype._read is just this._read, so you just call your _read function recursively.

devsnek avatar Jun 11 '19 03:06 devsnek

@devsnek Seems that I have something misunderstood about the prototype...horrible thing... changing this.constructor.prototype._read(n) to ReadStream.prototype._read.call(this, n) will work. @Hakerh400 @devsnek Thank you for the help.😂

lzszone avatar Jun 11 '19 05:06 lzszone