Skip to content
This repository
Browse code

Remove require.async

This patch removes require.async from nodejs.

1. It complicated the code unnecessarily.
2. Everyone uses sync require anyway.
3. It's got a lot of weird edge cases when mixed with sync require.
4. It is many months behind the commonjs spec anyhow.
  • Loading branch information...
commit bb08f0c219bf4ca99b1129f54ef415ce46b01f22 1 parent 2d348bb
Isaac Z. Schlueter authored September 19, 2010 ry committed September 19, 2010
181  src/node.js
@@ -106,7 +106,6 @@ var module = (function () {
106 106
     internalModuleCache[id] = m;
107 107
     var e = m._compile(natives[id], id);
108 108
     if (e) throw e;
109  
-    m.loaded = true;
110 109
     return m;
111 110
   }
112 111
 
@@ -172,44 +171,19 @@ var module = (function () {
172 171
     };
173 172
   }
174 173
 
175  
-  /* Sync unless callback given */
176  
-  function findModulePath (id, dirs, callback) {
  174
+  function findModulePath (id, dirs) {
177 175
     process.assert(Array.isArray(dirs));
178 176
 
179  
-    if (/^https?:\/\//.exec(id)) {
180  
-      if (callback) {
181  
-        callback(id);
182  
-      } else {
183  
-        throw new Error("Sync http require not allowed.");
184  
-      }
185  
-      return;
186  
-    }
187  
-
188 177
     var nextLoc = traverser(id, id.charAt(0) === '/' ? [''] : dirs);
189 178
 
190 179
     var fs = requireNative('fs');
191 180
 
192  
-    function searchLocations () {
193  
-      var location, stats;
194  
-      while (location = nextLoc()) {
195  
-        try { stats = fs.statSync(location); } catch(e) { continue; }
196  
-        if (stats && !stats.isDirectory()) return location;
197  
-      }
198  
-      return false;
  181
+    var location, stats;
  182
+    while (location = nextLoc()) {
  183
+      try { stats = fs.statSync(location); } catch(e) { continue; }
  184
+      if (stats && !stats.isDirectory()) return location;
199 185
     }
200  
-
201  
-    function searchLocationsAsync (cb) {
202  
-      var location = nextLoc();
203  
-
204  
-      if (!location) { cb(false); return; }
205  
-
206  
-      fs.stat(location, function (err, stats) {
207  
-        if (stats && !stats.isDirectory()) { cb(location); }
208  
-        else { searchLocationsAsync(cb); }
209  
-      });
210  
-    }
211  
-
212  
-    return callback ? searchLocationsAsync(callback) : searchLocations();
  186
+    return false;
213 187
   }
214 188
 
215 189
 
@@ -235,7 +209,7 @@ var module = (function () {
235 209
   }
236 210
 
237 211
 
238  
-  function loadModule (request, parent, callback) {
  212
+  function loadModule (request, parent) {
239 213
     var resolvedModule = resolveModulePath(request, parent),
240 214
         id = resolvedModule[0],
241 215
         paths = resolvedModule[1];
@@ -245,45 +219,27 @@ var module = (function () {
245 219
     // native modules always take precedence.
246 220
     var cachedNative = internalModuleCache[id];
247 221
     if (cachedNative) {
248  
-      return callback ? callback(null, cachedNative.exports) : cachedNative.exports;
  222
+      return cachedNative.exports;
249 223
     }
250 224
     if (natives[id]) {
251 225
       debug('load native module ' + id);
252  
-      var nativeMod = loadNative(id);
253  
-      return callback ? callback(null, nativeMod.exports) : nativeMod.exports;
  226
+      return loadNative(id).exports;
254 227
     }
255 228
 
256 229
     // look up the filename first, since that's the cache key.
257 230
     debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
258  
-    if (!callback) {
259  
-      // sync
260  
-      var filename = findModulePath(request, paths);
261  
-      if (!filename) {
262  
-        throw new Error("Cannot find module '" + request + "'");
263  
-      }
264  
-
265  
-      var cachedModule = parent.moduleCache[filename];
266  
-      if (cachedModule) return cachedModule.exports;
267  
-
268  
-      var module = new Module(id, parent);
269  
-      module.moduleCache[filename] = module;
270  
-      module.loadSync(filename);
271  
-      return module.exports;
  231
+    var filename = findModulePath(request, paths);
  232
+    if (!filename) {
  233
+      throw new Error("Cannot find module '" + request + "'");
272 234
     }
273  
-    // async
274  
-    findModulePath(request, paths, function (filename) {
275  
-      if (!filename) {
276  
-        var err = new Error("Cannot find module '" + request + "'");
277  
-        return callback(err);
278  
-      }
279 235
 
280  
-      var cachedModule = parent.moduleCache[filename];
281  
-      if (cachedModule) return callback(null, cachedModule.exports);
  236
+    var cachedModule = parent.moduleCache[filename];
  237
+    if (cachedModule) return cachedModule.exports;
282 238
 
283  
-      var module = new Module(id, parent);
284  
-      module.moduleCache[filename] = module;
285  
-      module.load(filename, callback);
286  
-    });
  239
+    var module = new Module(id, parent);
  240
+    module.moduleCache[filename] = module;
  241
+    module.load(filename);
  242
+    return module.exports;
287 243
   };
288 244
 
289 245
 
@@ -313,63 +269,27 @@ var module = (function () {
313 269
   }
314 270
 
315 271
 
316  
-  Module.prototype.loadSync = function (filename) {
317  
-    debug("loadSync " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
318  
-
319  
-    process.assert(!this.loaded);
320  
-    this.filename = filename;
321  
-
322  
-    if (filename.match(/\.node$/)) {
323  
-      this._loadObjectSync(filename);
324  
-    } else {
325  
-      this._loadScriptSync(filename);
326  
-    }
327  
-  };
328  
-
329  
-
330  
-  Module.prototype.load = function (filename, callback) {
  272
+  Module.prototype.load = function (filename) {
331 273
     debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
332 274
 
333 275
     process.assert(!this.loaded);
334  
-
335 276
     this.filename = filename;
336 277
 
337 278
     if (filename.match(/\.node$/)) {
338  
-      this._loadObject(filename, callback);
  279
+      this._loadObject(filename);
339 280
     } else {
340  
-      this._loadScript(filename, callback);
  281
+      this._loadScript(filename);
341 282
     }
342 283
   };
343 284
 
344 285
 
345  
-  Module.prototype._loadObjectSync = function (filename) {
346  
-    this.loaded = true;
  286
+  Module.prototype._loadObject = function (filename) {
347 287
     process.dlopen(filename, this.exports);
348 288
   };
349 289
 
350 290
 
351  
-  Module.prototype._loadObject = function (filename, callback) {
352  
-    var self = this;
353  
-    // XXX Not yet supporting loading from HTTP. would need to download the
354  
-    // file, store it to tmp then run dlopen on it.
355  
-    self.loaded = true;
356  
-    process.dlopen(filename, self.exports); // FIXME synchronus
357  
-    if (callback) callback(null, self.exports);
358  
-  };
359  
-
360  
-
361  
-  function cat (id, callback) {
362  
-    if (id.match(/^http:\/\//)) {
363  
-      loadModule('http', process.mainModule, function (err, http) {
364  
-        if (err) {
365  
-          if (callback) callback(err);
366  
-        } else {
367  
-          http.cat(id, callback);
368  
-        }
369  
-      });
370  
-    } else {
371  
-      requireNative('fs').readFile(id, 'utf8', callback);
372  
-    }
  291
+  function cat (id) {
  292
+    requireNative('fs').readFile(id, 'utf8');
373 293
   }
374 294
 
375 295
 
@@ -390,16 +310,11 @@ var module = (function () {
390 310
       return;
391 311
     }
392 312
 
393  
-    function requireAsync (url, cb) {
394  
-      loadModule(url, self, cb);
395  
-    }
396  
-
397 313
     function require (path) {
398 314
       return loadModule(path, self);
399 315
     }
400 316
 
401 317
     require.paths = modulePaths;
402  
-    require.async = requireAsync;
403 318
     require.main = process.mainModule;
404 319
     require.registerExtension = registerExtension;
405 320
 
@@ -451,62 +366,18 @@ var module = (function () {
451 366
   };
452 367
 
453 368
 
454  
-  Module.prototype._loadScriptSync = function (filename) {
  369
+  Module.prototype._loadScript = function (filename) {
455 370
     var content = requireNative('fs').readFileSync(filename, 'utf8');
456 371
     this._compile(content, filename);
457 372
     this.loaded = true;
458 373
   };
459 374
 
460 375
 
461  
-  Module.prototype._loadScript = function (filename, callback) {
462  
-    var self = this;
463  
-    cat(filename, function (err, content) {
464  
-      debug('cat done');
465  
-      if (err) {
466  
-        if (callback) callback(err);
467  
-      } else {
468  
-        try {
469  
-          self._compile(content, filename);
470  
-        } catch (err) {
471  
-          if (callback) callback(err);
472  
-          return;
473  
-        }
474  
-
475  
-        self._waitChildrenLoad(function () {
476  
-          self.loaded = true;
477  
-          if (self.onload) self.onload();
478  
-          if (callback) callback(null, self.exports);
479  
-        });
480  
-      }
481  
-    });
482  
-  };
483  
-
484  
-
485  
-  Module.prototype._waitChildrenLoad = function (callback) {
486  
-    var nloaded = 0;
487  
-    var children = this.children;
488  
-    for (var i = 0; i < children.length; i++) {
489  
-      var child = children[i];
490  
-      if (child.loaded) {
491  
-        nloaded++;
492  
-      } else {
493  
-        child.onload = function () {
494  
-          child.onload = null;
495  
-          nloaded++;
496  
-          if (children.length == nloaded && callback) callback();
497  
-        };
498  
-      }
499  
-    }
500  
-    if (children.length == nloaded && callback) callback();
501  
-  };
502  
-
503  
-
504  
-
505 376
   // bootstrap main module.
506 377
   exports.runMain = function () {
507 378
     // Load the main module--the command line argument.
508 379
     process.mainModule = new Module(".");
509  
-    process.mainModule.loadSync(process.argv[1]);
  380
+    process.mainModule.load(process.argv[1]);
510 381
   }
511 382
 
512 383
   return exports;
4  test/message/undefined_reference_in_new_context.out
@@ -7,8 +7,8 @@ ReferenceError: foo is not defined
7 7
     at evalmachine.<anonymous>:*
8 8
     at *test/message/undefined_reference_in_new_context.js:*
9 9
     at Module._compile (node.js:*)
10  
-    at Module._loadScriptSync (node.js:*)
11  
-    at Module.loadSync (node.js:*)
  10
+    at Module._loadScript (node.js:*)
  11
+    at Module.load (node.js:*)
12 12
     at Object.runMain (node.js:*)
13 13
     at Array.<anonymous> (node.js:*)
14 14
     at EventEmitter._tickCallback (node.js:*)
31  test/simple/test-module-loading.js
@@ -64,23 +64,8 @@ try {
64 64
   assert.equal("blah", e.message);
65 65
 }
66 66
 
67  
-var errorThrownAsync = false;
68  
-require.async("../fixtures/throws_error1", function(err, a) {
69  
-  if (err) {
70  
-    errorThrownAsync = true;
71  
-    assert.equal("blah", err.message);
72  
-  }
73  
-});
74  
-
75 67
 assert.equal(require('path').dirname(__filename), __dirname);
76 68
 
77  
-var asyncRun = false;
78  
-require.async('../fixtures/a1', function (err, a) {
79  
-  if (err) throw err;
80  
-  assert.equal("A", a.A());
81  
-  asyncRun = true;
82  
-});
83  
-
84 69
 common.debug('load custom file types with registerExtension');
85 70
 require.registerExtension('.test', function(content) {
86 71
   assert.equal("this is custom source\n", content);
@@ -113,16 +98,6 @@ try {
113 98
   assert.equal(err.message, "Cannot find module '../fixtures/empty'");
114 99
 }
115 100
 
116  
-var asyncRequireDir = false;
117  
-require.async("../fixtures/empty", function (err, a) {
118  
-  assert.ok(err);
119  
-
120  
-  if (err) {
121  
-    asyncRequireDir = true;
122  
-    assert.equal(err.message, "Cannot find module '../fixtures/empty'");
123  
-  }
124  
-});
125  
-
126 101
 // Check load order is as expected
127 102
 common.debug('load order');
128 103
 
@@ -169,11 +144,5 @@ process.addListener("exit", function () {
169 144
 
170 145
   assert.equal(true, errorThrown);
171 146
 
172  
-  assert.equal(true, asyncRun);
173  
-
174  
-  assert.equal(true, errorThrownAsync);
175  
-
176  
-  assert.equal(true, asyncRequireDir);
177  
-
178 147
   console.log("exit");
179 148
 });

1 note on commit bb08f0c

JiyinYiyong 题叶

:(
Sad that it was removed. Related discussion here: Bodule/bodule#3
If Node is compatible with the syntax require "name", ->, we can do some hacks to reuse browser code in Node, and per se.
The door was closed long time ago.

Please sign in to comment.
Something went wrong with that request. Please try again.