From 773769df60ac4f2448fa88b2ece035de2512928f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 9 Aug 2019 09:01:43 +0200 Subject: fs: add runtime deprecate for file stream open() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WriteStream.open() and ReadStream.open() are undocumented internal APIs that do not make sense to use in userland. File streams should always be opened through their corresponding factory methods (fs.createWriteStream() and fs.createReadStream()) or by passing a file descriptor in options. PR-URL: https://github.com/nodejs/node/pull/29061 Reviewed-By: Ben Noordhuis Reviewed-By: Matteo Collina Reviewed-By: Jeremiah Senkpiel Reviewed-By: João Reis Reviewed-By: Ruben Bridgewater --- lib/internal/fs/streams.js | 65 +++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 21 deletions(-) (limited to 'lib') 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) { -- cgit v1.2.3