lib: move initialization of APIs for changing process state

Whether these APIs should be available for Node.js instances
semantically depends on whether the current Node.js instance
was assigned ownership of process-wide state, and not whether
it refers to the main thread or not.

PR-URL: https://github.com/nodejs/node/pull/31172
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
This commit is contained in:
Anna Henningsen
2020-01-03 06:57:52 +01:00
committed by Rich Trott
parent 714eb919cc
commit 20fd12310f
4 changed files with 56 additions and 57 deletions

View File

@@ -1,11 +1,16 @@
'use strict';
const credentials = internalBinding('credentials');
const rawMethods = internalBinding('process_methods');
// TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION
const { unavailable } = require('internal/process/worker_thread_only');
process.abort = unavailable('process.abort()');
process.chdir = unavailable('process.chdir()');
process.umask = wrappedUmask;
process.cwd = rawMethods.cwd;
if (credentials.implementsPosixCredentials) {
// TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION
const { unavailable } = require('internal/process/worker_thread_only');
process.initgroups = unavailable('process.initgroups()');
process.setgroups = unavailable('process.setgroups()');
process.setegid = unavailable('process.setegid()');
@@ -16,3 +21,16 @@ if (credentials.implementsPosixCredentials) {
// ---- keep the attachment of the wrappers above so that it's easier to ----
// ---- compare the setups side-by-side -----
const {
codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
} = require('internal/errors');
function wrappedUmask(mask) {
// process.umask() is a read-only operation in workers.
if (mask !== undefined) {
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
}
return rawMethods.umask(mask);
}

View File

@@ -1,6 +1,12 @@
'use strict';
const credentials = internalBinding('credentials');
const rawMethods = internalBinding('process_methods');
process.abort = rawMethods.abort;
process.umask = wrappedUmask;
process.chdir = wrappedChdir;
process.cwd = wrappedCwd;
if (credentials.implementsPosixCredentials) {
const wrapped = wrapPosixCredentialSetters(credentials);
@@ -16,6 +22,11 @@ if (credentials.implementsPosixCredentials) {
// ---- keep the attachment of the wrappers above so that it's easier to ----
// ---- compare the setups side-by-side -----
const {
parseMode,
validateString
} = require('internal/validators');
function wrapPosixCredentialSetters(credentials) {
const {
ArrayIsArray,
@@ -94,3 +105,27 @@ function wrapPosixCredentialSetters(credentials) {
setuid: wrapIdSetter('User', _setuid)
};
}
// Cache the working directory to prevent lots of lookups. If the working
// directory is changed by `chdir`, it'll be updated.
let cachedCwd = '';
function wrappedChdir(directory) {
validateString(directory, 'directory');
rawMethods.chdir(directory);
// Mark cache that it requires an update.
cachedCwd = '';
}
function wrappedUmask(mask) {
if (mask !== undefined) {
mask = parseMode(mask, 'mask');
}
return rawMethods.umask(mask);
}
function wrappedCwd() {
if (cachedCwd === '')
cachedCwd = rawMethods.cwd();
return cachedCwd;
}

View File

@@ -3,11 +3,6 @@
const { ObjectDefineProperty } = primordials;
const rawMethods = internalBinding('process_methods');
process.abort = rawMethods.abort;
process.umask = wrappedUmask;
process.chdir = wrappedChdir;
process.cwd = wrappedCwd;
// TODO(joyeecheung): deprecate and remove these underscore methods
process._debugProcess = rawMethods._debugProcess;
process._debugEnd = rawMethods._debugEnd;
@@ -38,34 +33,6 @@ process.on('removeListener', stopListeningIfSignal);
// ---- compare the setups side-by-side -----
const { guessHandleType } = internalBinding('util');
const {
parseMode,
validateString
} = require('internal/validators');
// Cache the working directory to prevent lots of lookups. If the working
// directory is changed by `chdir`, it'll be updated.
let cachedCwd = '';
function wrappedChdir(directory) {
validateString(directory, 'directory');
rawMethods.chdir(directory);
// Mark cache that it requires an update.
cachedCwd = '';
}
function wrappedUmask(mask) {
if (mask !== undefined) {
mask = parseMode(mask, 'mask');
}
return rawMethods.umask(mask);
}
function wrappedCwd() {
if (cachedCwd === '')
cachedCwd = rawMethods.cwd();
return cachedCwd;
}
function createWritableStdioStream(fd) {
let stream;

View File

@@ -1,15 +1,6 @@
'use strict';
const { ObjectDefineProperty } = primordials;
const rawMethods = internalBinding('process_methods');
const {
unavailable
} = require('internal/process/worker_thread_only');
process.abort = unavailable('process.abort()');
process.chdir = unavailable('process.chdir()');
process.umask = wrappedUmask;
process.cwd = rawMethods.cwd;
delete process._debugProcess;
delete process._debugEnd;
@@ -42,9 +33,6 @@ process.removeListener('removeListener', stopListeningIfSignal);
const {
createWorkerStdio
} = require('internal/worker/io');
const {
codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
} = require('internal/errors');
let workerStdio;
function lazyWorkerStdio() {
@@ -57,12 +45,3 @@ function getStdout() { return lazyWorkerStdio().stdout; }
function getStderr() { return lazyWorkerStdio().stderr; }
function getStdin() { return lazyWorkerStdio().stdin; }
function wrappedUmask(mask) {
// process.umask() is a read-only operation in workers.
if (mask !== undefined) {
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
}
return rawMethods.umask(mask);
}