Extends from fs.ReadStream
- 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?
Change this.constructor.prototype._read(n) to super._read(n)
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?
try this
class RS extends ReadStream {
constructor(path, options) {
super(path, options);
}
_read(n) {
// doSomething()
return super._read(n)
}
}
@devsnek Yes, it works. Could you take some time to explain my question above, please?
this.constructor.prototype._read is just this._read, so you just call your _read function recursively.
@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.😂