diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-01-03 08:57:52 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2020-01-07 00:39:46 +0300 |
commit | 42d36dca90fd5ef3972ec55ae7a4a5258423bbf9 (patch) | |
tree | 25c7c02001a938f78b46c83f99554318a5869c4b | |
parent | 614b074f3bc2cb007fce6aa4eac9503c5cc53988 (diff) |
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>
4 files changed, 56 insertions, 57 deletions
diff --git a/lib/internal/bootstrap/switches/does_not_own_process_state.js b/lib/internal/bootstrap/switches/does_not_own_process_state.js index 1ec449b34a9..69946e802e1 100644 --- a/lib/internal/bootstrap/switches/does_not_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_not_own_process_state.js @@ -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'); -if (credentials.implementsPosixCredentials) { - // 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) { 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); +} diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 88f77520720..15023ea3f55 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -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; +} diff --git a/lib/internal/bootstrap/switches/is_main_thread.js b/lib/internal/bootstrap/switches/is_main_thread.js index 4f7ef7b6a69..ac8336fb622 100644 --- a/lib/internal/bootstrap/switches/is_main_thread.js +++ b/lib/internal/bootstrap/switches/is_main_thread.js @@ -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; diff --git a/lib/internal/bootstrap/switches/is_not_main_thread.js b/lib/internal/bootstrap/switches/is_not_main_thread.js index 33e98d387cf..852352eead0 100644 --- a/lib/internal/bootstrap/switches/is_not_main_thread.js +++ b/lib/internal/bootstrap/switches/is_not_main_thread.js @@ -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); -} |