summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/fs/streams.js65
1 files changed, 44 insertions, 21 deletions
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js
index 110ad11493..3674a71f92 100644
--- a/lib/internal/fs/streams.js
+++ b/lib/internal/fs/streams.js
@@ -5,6 +5,7 @@ const { Math, Object } = primordials;
const {
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
+const internalUtil = require('internal/util');
const { validateNumber } = require('internal/validators');
const fs = require('fs');
const { Buffer } = require('buffer');
@@ -100,7 +101,7 @@ function ReadStream(path, options) {
}
if (typeof this.fd !== 'number')
- this.open();
+ _openReadFs(this);
this.on('end', function() {
if (this.autoClose) {
@@ -111,23 +112,34 @@ function ReadStream(path, options) {
Object.setPrototypeOf(ReadStream.prototype, Readable.prototype);
Object.setPrototypeOf(ReadStream, Readable);
-ReadStream.prototype.open = function() {
- fs.open(this.path, this.flags, this.mode, (er, fd) => {
+const openReadFs = internalUtil.deprecate(function() {
+ _openReadFs(this);
+}, 'ReadStream.prototype.open() is deprecated', 'DEP0XXX');
+ReadStream.prototype.open = openReadFs;
+
+function _openReadFs(stream) {
+ // Backwards compat for overriden open.
+ if (stream.open !== openReadFs) {
+ stream.open();
+ return;
+ }
+
+ fs.open(stream.path, stream.flags, stream.mode, (er, fd) => {
if (er) {
- if (this.autoClose) {
- this.destroy();
+ if (stream.autoClose) {
+ stream.destroy();
}
- this.emit('error', er);
+ stream.emit('error', er);
return;
}
- this.fd = fd;
- this.emit('open', fd);
- this.emit('ready');
+ stream.fd = fd;
+ stream.emit('open', fd);
+ stream.emit('ready');
// Start the flow of data.
- this.read();
+ stream.read();
});
-};
+}
ReadStream.prototype._read = function(n) {
if (typeof this.fd !== 'number') {
@@ -266,7 +278,7 @@ function WriteStream(path, options) {
this.setDefaultEncoding(options.encoding);
if (typeof this.fd !== 'number')
- this.open();
+ _openWriteFs(this);
}
Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);
Object.setPrototypeOf(WriteStream, Writable);
@@ -279,21 +291,32 @@ WriteStream.prototype._final = function(callback) {
callback();
};
-WriteStream.prototype.open = function() {
- fs.open(this.path, this.flags, this.mode, (er, fd) => {
+const openWriteFs = internalUtil.deprecate(function() {
+ _openWriteFs(this);
+}, 'WriteStream.prototype.open() is deprecated', 'DEP0XXX');
+WriteStream.prototype.open = openWriteFs;
+
+function _openWriteFs(stream) {
+ // Backwards compat for overriden open.
+ if (stream.open !== openWriteFs) {
+ stream.open();
+ return;
+ }
+
+ fs.open(stream.path, stream.flags, stream.mode, (er, fd) => {
if (er) {
- if (this.autoClose) {
- this.destroy();
+ if (stream.autoClose) {
+ stream.destroy();
}
- this.emit('error', er);
+ stream.emit('error', er);
return;
}
- this.fd = fd;
- this.emit('open', fd);
- this.emit('ready');
+ stream.fd = fd;
+ stream.emit('open', fd);
+ stream.emit('ready');
});
-};
+}
WriteStream.prototype._write = function(data, encoding, cb) {