@@ -58,6 +58,7 @@ const kMaxLength = require('buffer').kMaxLength;
58
58
59
59
const isWindows = process . platform === 'win32' ;
60
60
61
+ const DEBUG = process . env . NODE_DEBUG && / f s / . test ( process . env . NODE_DEBUG ) ;
61
62
const errnoException = util . _errnoException ;
62
63
63
64
function getOptions ( options , defaultOptions ) {
@@ -87,26 +88,48 @@ function copyObject(source) {
87
88
return target ;
88
89
}
89
90
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
+ } ;
95
116
}
96
117
97
118
function 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 ( ) ;
102
120
}
103
121
104
122
// Ensure that callbacks run in the global context. Only use this function
105
123
// for callbacks that are passed to the binding layer, callbacks that are
106
124
// invoked from JS already run in the proper scope.
107
125
function 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
+ }
110
133
111
134
return function ( ) {
112
135
return cb . apply ( null , arguments ) ;
@@ -117,8 +140,13 @@ function makeCallback(cb) {
117
140
// an optimization, since the data passed back to the callback needs to be
118
141
// transformed anyway.
119
142
function 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
+ }
122
150
123
151
return function ( err ) {
124
152
if ( err ) return cb ( err ) ;
@@ -240,10 +268,10 @@ fs.access = function(path, mode, callback) {
240
268
if ( typeof mode === 'function' ) {
241
269
callback = mode ;
242
270
mode = fs . F_OK ;
271
+ } else if ( typeof callback !== 'function' ) {
272
+ throw new TypeError ( '"callback" argument must be a function' ) ;
243
273
}
244
274
245
- callback = makeCallback ( callback ) ;
246
-
247
275
if ( handleError ( ( path = getPathFromURL ( path ) ) , callback ) )
248
276
return ;
249
277
@@ -252,7 +280,7 @@ fs.access = function(path, mode, callback) {
252
280
253
281
mode = mode | 0 ;
254
282
var req = new FSReqWrap ( ) ;
255
- req . oncomplete = callback ;
283
+ req . oncomplete = makeCallback ( callback ) ;
256
284
binding . access ( pathModule . _makeLong ( path ) , mode , req ) ;
257
285
} ;
258
286
0 commit comments