Skip to content

Commit 0ae90ec

Browse files
bnoordhuisMyles Borins
authored and
Myles Borins
committed
module,repl: remove repl require() hack
Remove a hack that was introduced in commit bb6d468 from November 2010. This is groundwork for a follow-up commit that makes it possible to use internal modules in lib/repl.js. PR-URL: #4026 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Conflicts: lib/module.js
1 parent 93afc39 commit 0ae90ec

File tree

6 files changed

+56
-49
lines changed

6 files changed

+56
-49
lines changed

lib/_debugger.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path');
55
const net = require('net');
66
const vm = require('vm');
77
const Module = require('module');
8-
const repl = Module.requireRepl();
8+
const repl = require('repl');
99
const inherits = util.inherits;
1010
const assert = require('assert');
1111
const spawn = require('child_process').spawn;

lib/internal/module.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
'use strict';
22

3-
module.exports.stripBOM = stripBOM;
3+
module.exports = { makeRequireFunction, stripBOM };
4+
5+
// Invoke with makeRequireFunction.call(module) where |module| is the
6+
// Module object to use as the context for the require() function.
7+
function makeRequireFunction() {
8+
const Module = this.constructor;
9+
const self = this;
10+
11+
function require(path) {
12+
return self.require(path);
13+
}
14+
15+
require.resolve = function(request) {
16+
return Module._resolveFilename(request, self);
17+
};
18+
19+
require.main = process.mainModule;
20+
21+
// Enable support to add extra extension types.
22+
require.extensions = Module._extensions;
23+
24+
require.cache = Module._cache;
25+
26+
return require;
27+
}
428

529
/**
630
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)

lib/module.js

+20-45
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,6 @@ Module._load = function(request, parent, isMain) {
274274
debug('Module._load REQUEST %s parent: %s', request, parent.id);
275275
}
276276

277-
// REPL is a special case, because it needs the real require.
278-
if (request === 'internal/repl' || request === 'repl') {
279-
if (Module._cache[request]) {
280-
return Module._cache[request];
281-
}
282-
var replModule = new Module(request);
283-
replModule._compile(NativeModule.getSource(request), `${request}.js`);
284-
NativeModule._cache[request] = replModule;
285-
return replModule.exports;
286-
}
287-
288277
var filename = Module._resolveFilename(request, parent);
289278

290279
var cachedModule = Module._cache[filename];
@@ -376,37 +365,9 @@ var resolvedArgv;
376365
// the file.
377366
// Returns exception, if any.
378367
Module.prototype._compile = function(content, filename) {
379-
var self = this;
380368
// remove shebang
381369
content = content.replace(shebangRe, '');
382370

383-
function require(path) {
384-
return self.require(path);
385-
}
386-
387-
require.resolve = function(request) {
388-
return Module._resolveFilename(request, self);
389-
};
390-
391-
Object.defineProperty(require, 'paths', { get: function() {
392-
throw new Error('require.paths is removed. Use ' +
393-
'node_modules folders, or the NODE_PATH ' +
394-
'environment variable instead.');
395-
}});
396-
397-
require.main = process.mainModule;
398-
399-
// Enable support to add extra extension types
400-
require.extensions = Module._extensions;
401-
require.registerExtension = function() {
402-
throw new Error('require.registerExtension() removed. Use ' +
403-
'require.extensions instead.');
404-
};
405-
406-
require.cache = Module._cache;
407-
408-
var dirname = path.dirname(filename);
409-
410371
// create wrapper function
411372
var wrapper = Module.wrap(content);
412373

@@ -431,8 +392,22 @@ Module.prototype._compile = function(content, filename) {
431392
global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0);
432393
}
433394
}
434-
var args = [self.exports, require, self, filename, dirname];
435-
return compiledWrapper.apply(self.exports, args);
395+
const dirname = path.dirname(filename);
396+
const require = internalModule.makeRequireFunction.call(this);
397+
398+
Object.defineProperty(require, 'paths', { get: function() {
399+
throw new Error('require.paths is removed. Use ' +
400+
'node_modules folders, or the NODE_PATH ' +
401+
'environment variable instead.');
402+
}});
403+
404+
require.registerExtension = function() {
405+
throw new Error('require.registerExtension() removed. Use ' +
406+
'require.extensions instead.');
407+
};
408+
409+
const args = [this.exports, require, this, filename, dirname];
410+
return compiledWrapper.apply(this.exports, args);
436411
};
437412

438413

@@ -498,10 +473,10 @@ Module._initPaths = function() {
498473
Module.globalPaths = modulePaths.slice(0);
499474
};
500475

501-
// bootstrap repl
502-
Module.requireRepl = function() {
503-
return Module._load('internal/repl', '.');
504-
};
476+
// TODO(bnoordhuis) Unused, remove in the future.
477+
Module.requireRepl = internalUtil.deprecate(function() {
478+
return NativeModule.require('internal/repl');
479+
}, 'Module.requireRepl is deprecated.');
505480

506481
Module._preloadModules = function(requests) {
507482
if (!Array.isArray(requests))

lib/repl.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'use strict';
2323

24+
const internalModule = require('internal/module');
2425
const util = require('util');
2526
const inherits = util.inherits;
2627
const Stream = require('stream');
@@ -29,6 +30,7 @@ const path = require('path');
2930
const fs = require('fs');
3031
const rl = require('readline');
3132
const Console = require('console').Console;
33+
const Module = require('module');
3234
const domain = require('domain');
3335
const debug = util.debuglog('repl');
3436

@@ -508,6 +510,8 @@ REPLServer.prototype.createContext = function() {
508510
context.global.global = context;
509511
}
510512

513+
const module = new Module('<repl>');
514+
const require = internalModule.makeRequireFunction.call(module);
511515
context.module = module;
512516
context.require = require;
513517

@@ -647,7 +651,7 @@ REPLServer.prototype.complete = function(line, callback) {
647651
completionGroupsLoaded();
648652
} else if (match = line.match(requireRE)) {
649653
// require('...<Tab>')
650-
var exts = Object.keys(require.extensions);
654+
const exts = Object.keys(this.context.require.extensions);
651655
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
652656
')$');
653657

src/node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
// If -i or --interactive were passed, or stdin is a TTY.
143143
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
144144
// REPL
145-
var cliRepl = Module.requireRepl();
145+
var cliRepl = NativeModule.require('internal/repl');
146146
cliRepl.createInternalRepl(process.env, function(err, repl) {
147147
if (err) {
148148
throw err;

test/parallel/test-repl.js

+4
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ function error_test() {
278278
expect: 'undefined\n' + prompt_unix },
279279
{ client: client_unix, send: '/* \'\n"\n\'"\'\n*/',
280280
expect: 'undefined\n' + prompt_unix },
281+
// REPL should get a normal require() function, not one that allows
282+
// access to internal modules without the --expose_internals flag.
283+
{ client: client_unix, send: 'require("internal/repl")',
284+
expect: /^Error: Cannot find module 'internal\/repl'/ },
281285
]);
282286
}
283287

0 commit comments

Comments
 (0)