@@ -58,7 +58,6 @@ const kMaxLength = require('buffer').kMaxLength;
5858
5959const isWindows = process.platform === 'win32';
6060
61- const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
6261const errnoException = util._errnoException;
6362
6463function getOptions(options, defaultOptions) {
@@ -88,48 +87,26 @@ function copyObject(source) {
8887 return target;
8988}
9089
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;
11695}
11796
11897function 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');
120102}
121103
122104// Ensure that callbacks run in the global context. Only use this function
123105// for callbacks that are passed to the binding layer, callbacks that are
124106// invoked from JS already run in the proper scope.
125107function 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');
133110
134111 return function() {
135112 return cb.apply(null, arguments);
@@ -140,13 +117,8 @@ function makeCallback(cb) {
140117// an optimization, since the data passed back to the callback needs to be
141118// transformed anyway.
142119function 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');
150122
151123 return function(err) {
152124 if (err) return cb(err);
@@ -268,10 +240,10 @@ fs.access = function(path, mode, callback) {
268240 if (typeof mode === 'function') {
269241 callback = mode;
270242 mode = fs.F_OK;
271- } else if (typeof callback !== 'function') {
272- throw new TypeError('"callback" argument must be a function');
273243 }
274244
245+ callback = makeCallback(callback);
246+
275247 if (handleError((path = getPathFromURL(path)), callback))
276248 return;
277249
@@ -280,7 +252,7 @@ fs.access = function(path, mode, callback) {
280252
281253 mode = mode | 0;
282254 var req = new FSReqWrap();
283- req.oncomplete = makeCallback( callback) ;
255+ req.oncomplete = callback;
284256 binding.access(pathModule._makeLong(path), mode, req);
285257};
286258
0 commit comments