@@ -58,6 +58,7 @@ const kMaxLength = require('buffer').kMaxLength;
5858
5959const isWindows = process . platform === 'win32' ;
6060
61+ const DEBUG = process . env . NODE_DEBUG && / f s / . test ( process . env . NODE_DEBUG ) ;
6162const errnoException = util . _errnoException ;
6263
6364function getOptions ( options , defaultOptions ) {
@@ -87,26 +88,48 @@ function copyObject(source) {
8788 return target ;
8889}
8990
90- var internalErrors ;
91- function lazyErrors ( ) {
92- if ( ! internalErrors )
93- internalErrors = require ( 'internal/errors' ) ;
94- return internalErrors ;
91+ function rethrow ( ) {
92+ // TODO(thefourtheye) Throw error instead of warning in major version > 7
93+ process . emitWarning (
94+ 'Calling an asynchronous function without callback is deprecated.' ,
95+ 'DeprecationWarning' , 'DEP0013' , rethrow
96+ ) ;
97+
98+ // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
99+ // is fairly slow to generate.
100+ if ( DEBUG ) {
101+ var backtrace = new Error ( ) ;
102+ return function ( err ) {
103+ if ( err ) {
104+ backtrace . stack = err . name + ': ' + err . message +
105+ backtrace . stack . substr ( backtrace . name . length ) ;
106+ throw backtrace ;
107+ }
108+ } ;
109+ }
110+
111+ return function ( err ) {
112+ if ( err ) {
113+ throw err ; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
114+ }
115+ } ;
95116}
96117
97118function maybeCallback ( cb ) {
98- if ( typeof cb === 'function' )
99- return cb ;
100- else
101- throw new ( lazyErrors ( ) . TypeError ) ( 'ERR_INVALID_CALLBACK' ) ;
119+ return typeof cb === 'function' ? cb : rethrow ( ) ;
102120}
103121
104122// Ensure that callbacks run in the global context. Only use this function
105123// for callbacks that are passed to the binding layer, callbacks that are
106124// invoked from JS already run in the proper scope.
107125function makeCallback ( cb ) {
108- if ( typeof cb !== 'function' )
109- throw new ( lazyErrors ( ) . TypeError ) ( 'ERR_INVALID_CALLBACK' ) ;
126+ if ( cb === undefined ) {
127+ return rethrow ( ) ;
128+ }
129+
130+ if ( typeof cb !== 'function' ) {
131+ throw new TypeError ( '"callback" argument must be a function' ) ;
132+ }
110133
111134 return function ( ) {
112135 return cb . apply ( null , arguments ) ;
@@ -117,8 +140,13 @@ function makeCallback(cb) {
117140// an optimization, since the data passed back to the callback needs to be
118141// transformed anyway.
119142function makeStatsCallback ( cb ) {
120- if ( typeof cb !== 'function' )
121- throw new ( lazyErrors ( ) . TypeError ) ( 'ERR_INVALID_CALLBACK' ) ;
143+ if ( cb === undefined ) {
144+ return rethrow ( ) ;
145+ }
146+
147+ if ( typeof cb !== 'function' ) {
148+ throw new TypeError ( '"callback" argument must be a function' ) ;
149+ }
122150
123151 return function ( err ) {
124152 if ( err ) return cb ( err ) ;
@@ -240,10 +268,10 @@ fs.access = function(path, mode, callback) {
240268 if ( typeof mode === 'function' ) {
241269 callback = mode ;
242270 mode = fs . F_OK ;
271+ } else if ( typeof callback !== 'function' ) {
272+ throw new TypeError ( '"callback" argument must be a function' ) ;
243273 }
244274
245- callback = makeCallback ( callback ) ;
246-
247275 if ( handleError ( ( path = getPathFromURL ( path ) ) , callback ) )
248276 return ;
249277
@@ -252,7 +280,7 @@ fs.access = function(path, mode, callback) {
252280
253281 mode = mode | 0 ;
254282 var req = new FSReqWrap ( ) ;
255- req . oncomplete = callback ;
283+ req . oncomplete = makeCallback ( callback ) ;
256284 binding . access ( pathModule . _makeLong ( path ) , mode , req ) ;
257285} ;
258286
0 commit comments