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