Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: expose a global node proxy in eval script and REPL #42114

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ parameter usage.
Return the URL of the active inspector, or `undefined` if there is none.

```console
$ node --inspect -p 'inspector.url()'
$ node --inspect -p 'node.inspector.url()'
Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
For help, see: https://nodejs.org/en/docs/inspector
ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34

$ node --inspect=localhost:3000 -p 'inspector.url()'
$ node --inspect=localhost:3000 -p 'node.inspector.url()'
Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
For help, see: https://nodejs.org/en/docs/inspector
ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a

$ node -p 'inspector.url()'
$ node -p 'node.inspector.url()'
undefined
```

Expand Down
2 changes: 1 addition & 1 deletion doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ distributions to provide their own default list.
The following command can be used to show the default cipher suite:

```console
node -p crypto.constants.defaultCoreCipherList | tr ':' '\n'
node -p node.crypto.constants.defaultCoreCipherList | tr ':' '\n'
TLS_AES_256_GCM_SHA384
TLS_CHACHA20_POLY1305_SHA256
TLS_AES_128_GCM_SHA256
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ function evalScript(name, body, breakFirstLine, print) {

// Create wrapper for cache entry
const script = `
globalThis.node = new Proxy(Object.freeze(Object.create(null)), {
get: (_, prop) => require("node:"+prop),
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
globalThis.module = module;
globalThis.exports = exports;
globalThis.__dirname = __dirname;
Expand Down
6 changes: 5 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,17 @@ REPLServer.prototype.createContext = function() {
writable: true,
value: replModule
});
const requireFunction = makeRequireFunction(replModule)
ObjectDefineProperty(context, 'require', {
configurable: true,
writable: true,
value: makeRequireFunction(replModule)
value: requireFunction,
});

addBuiltinLibsToObject(context, '<REPL>');
context.node = new Proxy(Object.freeze(Object.create(null)), {
get: (_, prop) => requireFunction("node:"+prop),
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
})

return context;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function test(main, callSite, expected) {
const { stderr } = child_process.spawnSync(process.execPath, ['-p', `
process.mainModule = { filename: ${JSON.stringify(main)} };

vm.runInNewContext('new Buffer(10)', { Buffer }, {
node.vm.runInNewContext('new Buffer(10)', { Buffer }, {
filename: ${JSON.stringify(callSite)}
});`], { encoding: 'utf8' });
if (expected)
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ child.exec(`${nodejs} --eval "console.error(42)"`,
}

// Check that builtin modules are pre-defined.
child.exec(`${nodejs} --print "os.platform()"`,
child.exec(`${nodejs} --print "node.os.platform()"`,
common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stderr, '');
assert.strictEqual(stdout.trim(), require('os').platform());
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cli-options-precedence.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const { spawnSync } = require('child_process');
assert.strictEqual(spawnSync(process.execPath, [
'--max-http-header-size=1234',
'--max-http-header-size=5678',
'-p', 'http.maxHeaderSize',
'-p', 'node.http.maxHeaderSize',
], {
encoding: 'utf8'
}).stdout.trim(), '5678');

// The command line takes precedence over NODE_OPTIONS:
assert.strictEqual(spawnSync(process.execPath, [
'--max-http-header-size=5678',
'-p', 'http.maxHeaderSize',
'-p', 'node.http.maxHeaderSize',
], {
encoding: 'utf8',
env: { ...process.env, NODE_OPTIONS: '--max-http-header-size=1234' }
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dummy-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function runTest(fd, streamName, testOutputStream, expectedName) {
'-e',
`const { internalBinding } = require('internal/test/binding');
internalBinding('process_methods').resetStdioForTesting();
fs.closeSync(${fd});
node.fs.closeSync(${fd});
const ctorName = process.${streamName}.constructor.name;
process.${testOutputStream}.write(ctorName);
`]);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const http = require('http');

assert.strictEqual(http.maxHeaderSize, 16 * 1024);
const child = spawnSync(process.execPath, ['--max-http-header-size=10', '-p',
'http.maxHeaderSize']);
'node.http.maxHeaderSize']);
assert.strictEqual(+child.stdout.toString().trim(), 10);
2 changes: 1 addition & 1 deletion test/parallel/test-os-userinfo-handles-getter-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;

const script = `os.userInfo({
const script = `node.os.userInfo({
get encoding() {
throw new Error('xyz');
}
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-tls-cipher-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function doCheck(arg, expression, check) {
}

// Test the default unmodified version
doCheck([], 'crypto.constants.defaultCipherList', defaultCoreList);
doCheck([], 'tls.DEFAULT_CIPHERS', defaultCoreList);
doCheck([], 'node.crypto.constants.defaultCipherList', defaultCoreList);
doCheck([], 'node.tls.DEFAULT_CIPHERS', defaultCoreList);

// Test the command line switch by itself
doCheck(['--tls-cipher-list=ABC'], 'crypto.constants.defaultCipherList', 'ABC');
doCheck(['--tls-cipher-list=ABC'], 'tls.DEFAULT_CIPHERS', 'ABC');
doCheck(['--tls-cipher-list=ABC'], 'node.crypto.constants.defaultCipherList', 'ABC');
doCheck(['--tls-cipher-list=ABC'], 'node.tls.DEFAULT_CIPHERS', 'ABC');
184 changes: 92 additions & 92 deletions test/parallel/test-trace-events-fs-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,102 +16,102 @@ if (!common.isWindows) {
uid = process.getuid();
}

tests['fs.sync.access'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.accessSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.chmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.chmodSync("fs.txt",100);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.chown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
`fs.chownSync("fs.txt", ${uid}, ${gid});` +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.close'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.copyfile'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.copyFileSync("fs.txt","a.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fchmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fchmodSync(fd,100);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
`fs.fchownSync(fd, ${uid}, ${gid});` +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fdatasyncSync(fd);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.readFileSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fsync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fsyncSync(fd);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.ftruncateSync(fd, 1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.futimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.futimesSync(fd,1,1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.lchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
`fs.lchownSync("fs.txt", ${uid}, ${gid});` +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.link'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.lstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.lstatSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp");' +
'fs.rmdirSync("fstemp")';
tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstest");' +
'fs.rmdirSync(fp)';
tests['fs.sync.open'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.read'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.readFileSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.readdir'] = 'fs.readdirSync("./")';
tests['fs.sync.realpath'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.realpathSync.native("linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.rename'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.renameSync("fs.txt","xyz.txt"); ' +
'fs.unlinkSync("xyz.txt")';
tests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp");' +
'fs.rmdirSync("fstemp")';
tests['fs.sync.stat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.statSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.unlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.utimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.utimesSync("fs.txt",1,1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.write'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.access'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.accessSync("fs.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.chmod'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.chmodSync("fs.txt",100);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.chown'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
`node.fs.chownSync("fs.txt", ${uid}, ${gid});` +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.close'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.copyfile'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.copyFileSync("fs.txt","a.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.fchmod'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
'node.fs.fchmodSync(fd,100);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.fchown'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
`node.fs.fchownSync(fd, ${uid}, ${gid});` +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.fdatasync'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
'node.fs.fdatasyncSync(fd);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.fstat'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.readFileSync("fs.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.fsync'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
'node.fs.fsyncSync(fd);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.ftruncate'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
'node.fs.ftruncateSync(fd, 1);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.futimes'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = node.fs.openSync("fs.txt", "r+");' +
'node.fs.futimesSync(fd,1,1);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.lchown'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
`node.fs.lchownSync("fs.txt", ${uid}, ${gid});` +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.link'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.linkSync("fs.txt", "linkx");' +
'node.fs.unlinkSync("linkx");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.lstat'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.lstatSync("fs.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.mkdir'] = 'node.fs.mkdirSync("fstemp");' +
'node.fs.rmdirSync("fstemp")';
tests['fs.sync.mkdtemp'] = 'const fp = node.fs.mkdtempSync("fstest");' +
'node.fs.rmdirSync(fp)';
tests['fs.sync.open'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.read'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.readFileSync("fs.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.readdir'] = 'node.fs.readdirSync("./")';
tests['fs.sync.realpath'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.linkSync("fs.txt", "linkx");' +
'node.fs.realpathSync.native("linkx");' +
'node.fs.unlinkSync("linkx");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.rename'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.renameSync("fs.txt","xyz.txt"); ' +
'node.fs.unlinkSync("xyz.txt")';
tests['fs.sync.rmdir'] = 'node.fs.mkdirSync("fstemp");' +
'node.fs.rmdirSync("fstemp")';
tests['fs.sync.stat'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.statSync("fs.txt");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.unlink'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.linkSync("fs.txt", "linkx");' +
'node.fs.unlinkSync("linkx");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.utimes'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.utimesSync("fs.txt",1,1);' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.write'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.unlinkSync("fs.txt")';

// On windows, we need permissions to test symlink and readlink.
// We'll only try to run these tests if we have enough privileges.
if (common.canCreateSymLink()) {
tests['fs.sync.symlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.symlinkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.readlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.symlinkSync("fs.txt", "linkx");' +
'fs.readlinkSync("linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.symlink'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.symlinkSync("fs.txt", "linkx");' +
'node.fs.unlinkSync("linkx");' +
'node.fs.unlinkSync("fs.txt")';
tests['fs.sync.readlink'] = 'node.fs.writeFileSync("fs.txt", "123", "utf8");' +
'node.fs.symlinkSync("fs.txt", "linkx");' +
'node.fs.readlinkSync("linkx");' +
'node.fs.unlinkSync("linkx");' +
'node.fs.unlinkSync("fs.txt")';
}

const tmpdir = require('../common/tmpdir');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-vm-api-handles-getter-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const scripts = [];

['filename', 'cachedData', 'produceCachedData', 'lineOffset', 'columnOffset']
.forEach((prop) => {
scripts.push(`vm.createScript('', {
scripts.push(`node.vm.createScript('', {
get ${prop} () {
throw new Error('xyz');
}
Expand All @@ -20,7 +20,7 @@ const scripts = [];

['breakOnSigint', 'timeout', 'displayErrors']
.forEach((prop) => {
scripts.push(`vm.createScript('').runInThisContext({
scripts.push(`node.vm.createScript('').runInThisContext({
get ${prop} () {
throw new Error('xyz');
}
Expand Down