@@ -17,7 +17,10 @@ const {
1717 ERR_METHOD_NOT_IMPLEMENTED ,
1818} = require ( 'internal/errors' ) . codes ;
1919const { deprecate } = require ( 'internal/util' ) ;
20- const { validateInteger } = require ( 'internal/validators' ) ;
20+ const {
21+ validateFunction,
22+ validateInteger,
23+ } = require ( 'internal/validators' ) ;
2124const { errorOrDestroy } = require ( 'internal/streams/destroy' ) ;
2225const fs = require ( 'fs' ) ;
2326const { kRef, kUnref, FileHandle } = require ( 'internal/fs/promises' ) ;
@@ -155,20 +158,9 @@ function ReadStream(path, options) {
155158
156159 this [ kFs ] = options . fs || fs ;
157160
158- if ( typeof this [ kFs ] . open !== 'function' ) {
159- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.open' , 'function' ,
160- this [ kFs ] . open ) ;
161- }
162-
163- if ( typeof this [ kFs ] . read !== 'function' ) {
164- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.read' , 'function' ,
165- this [ kFs ] . read ) ;
166- }
167-
168- if ( typeof this [ kFs ] . close !== 'function' ) {
169- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.close' , 'function' ,
170- this [ kFs ] . close ) ;
171- }
161+ validateFunction ( this [ kFs ] . open , 'options.fs.open' ) ;
162+ validateFunction ( this [ kFs ] . read , 'options.fs.read' ) ;
163+ validateFunction ( this [ kFs ] . close , 'options.fs.close' ) ;
172164
173165 options . autoDestroy = options . autoClose === undefined ?
174166 true : options . autoClose ;
@@ -310,30 +302,23 @@ function WriteStream(path, options) {
310302 options . decodeStrings = true ;
311303
312304 this [ kFs ] = options . fs || fs ;
313- if ( typeof this [ kFs ] . open !== 'function' ) {
314- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.open' , 'function' ,
315- this [ kFs ] . open ) ;
316- }
305+
306+ validateFunction ( this [ kFs ] . open , 'options.fs.open' ) ;
317307
318308 if ( ! this [ kFs ] . write && ! this [ kFs ] . writev ) {
319309 throw new ERR_INVALID_ARG_TYPE ( 'options.fs.write' , 'function' ,
320310 this [ kFs ] . write ) ;
321311 }
322312
323- if ( this [ kFs ] . write && typeof this [ kFs ] . write !== 'function' ) {
324- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.write' , 'function' ,
325- this [ kFs ] . write ) ;
313+ if ( this [ kFs ] . write ) {
314+ validateFunction ( this [ kFs ] . write , 'options.fs.write' ) ;
326315 }
327316
328- if ( this [ kFs ] . writev && typeof this [ kFs ] . writev !== 'function' ) {
329- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.writev' , 'function' ,
330- this [ kFs ] . writev ) ;
317+ if ( this [ kFs ] . writev ) {
318+ validateFunction ( this [ kFs ] . writev , 'options.fs.writev' ) ;
331319 }
332320
333- if ( typeof this [ kFs ] . close !== 'function' ) {
334- throw new ERR_INVALID_ARG_TYPE ( 'options.fs.close' , 'function' ,
335- this [ kFs ] . close ) ;
336- }
321+ validateFunction ( this [ kFs ] . close , 'options.fs.close' ) ;
337322
338323 // It's enough to override either, in which case only one will be used.
339324 if ( ! this [ kFs ] . write ) {
0 commit comments