@@ -5,6 +5,7 @@ const { Math, Object } = primordials;
55const {
66 ERR_OUT_OF_RANGE
77} = require ( 'internal/errors' ) . codes ;
8+ const internalUtil = require ( 'internal/util' ) ;
89const { validateNumber } = require ( 'internal/validators' ) ;
910const fs = require ( 'fs' ) ;
1011const { Buffer } = require ( 'buffer' ) ;
@@ -100,7 +101,7 @@ function ReadStream(path, options) {
100101 }
101102
102103 if ( typeof this . fd !== 'number' )
103- this . open ( ) ;
104+ _openReadFs ( this ) ;
104105
105106 this . on ( 'end' , function ( ) {
106107 if ( this . autoClose ) {
@@ -111,23 +112,34 @@ function ReadStream(path, options) {
111112Object . setPrototypeOf ( ReadStream . prototype , Readable . prototype ) ;
112113Object . setPrototypeOf ( ReadStream , Readable ) ;
113114
114- ReadStream . prototype . open = function ( ) {
115- fs . open ( this . path , this . flags , this . mode , ( er , fd ) => {
115+ const openReadFs = internalUtil . deprecate ( function ( ) {
116+ _openReadFs ( this ) ;
117+ } , 'ReadStream.prototype.open() is deprecated' , 'DEP0XXX' ) ;
118+ ReadStream . prototype . open = openReadFs ;
119+
120+ function _openReadFs ( stream ) {
121+ // Backwards compat for overriden open.
122+ if ( stream . open !== openReadFs ) {
123+ stream . open ( ) ;
124+ return ;
125+ }
126+
127+ fs . open ( stream . path , stream . flags , stream . mode , ( er , fd ) => {
116128 if ( er ) {
117- if ( this . autoClose ) {
118- this . destroy ( ) ;
129+ if ( stream . autoClose ) {
130+ stream . destroy ( ) ;
119131 }
120- this . emit ( 'error' , er ) ;
132+ stream . emit ( 'error' , er ) ;
121133 return ;
122134 }
123135
124- this . fd = fd ;
125- this . emit ( 'open' , fd ) ;
126- this . emit ( 'ready' ) ;
136+ stream . fd = fd ;
137+ stream . emit ( 'open' , fd ) ;
138+ stream . emit ( 'ready' ) ;
127139 // Start the flow of data.
128- this . read ( ) ;
140+ stream . read ( ) ;
129141 } ) ;
130- } ;
142+ }
131143
132144ReadStream . prototype . _read = function ( n ) {
133145 if ( typeof this . fd !== 'number' ) {
@@ -266,7 +278,7 @@ function WriteStream(path, options) {
266278 this . setDefaultEncoding ( options . encoding ) ;
267279
268280 if ( typeof this . fd !== 'number' )
269- this . open ( ) ;
281+ _openWriteFs ( this ) ;
270282}
271283Object . setPrototypeOf ( WriteStream . prototype , Writable . prototype ) ;
272284Object . setPrototypeOf ( WriteStream , Writable ) ;
@@ -279,21 +291,32 @@ WriteStream.prototype._final = function(callback) {
279291 callback ( ) ;
280292} ;
281293
282- WriteStream . prototype . open = function ( ) {
283- fs . open ( this . path , this . flags , this . mode , ( er , fd ) => {
294+ const openWriteFs = internalUtil . deprecate ( function ( ) {
295+ _openWriteFs ( this ) ;
296+ } , 'WriteStream.prototype.open() is deprecated' , 'DEP0XXX' ) ;
297+ WriteStream . prototype . open = openWriteFs ;
298+
299+ function _openWriteFs ( stream ) {
300+ // Backwards compat for overriden open.
301+ if ( stream . open !== openWriteFs ) {
302+ stream . open ( ) ;
303+ return ;
304+ }
305+
306+ fs . open ( stream . path , stream . flags , stream . mode , ( er , fd ) => {
284307 if ( er ) {
285- if ( this . autoClose ) {
286- this . destroy ( ) ;
308+ if ( stream . autoClose ) {
309+ stream . destroy ( ) ;
287310 }
288- this . emit ( 'error' , er ) ;
311+ stream . emit ( 'error' , er ) ;
289312 return ;
290313 }
291314
292- this . fd = fd ;
293- this . emit ( 'open' , fd ) ;
294- this . emit ( 'ready' ) ;
315+ stream . fd = fd ;
316+ stream . emit ( 'open' , fd ) ;
317+ stream . emit ( 'ready' ) ;
295318 } ) ;
296- } ;
319+ }
297320
298321
299322WriteStream . prototype . _write = function ( data , encoding , cb ) {
0 commit comments