From 42d36dca90fd5ef3972ec55ae7a4a5258423bbf9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 3 Jan 2020 06:57:52 +0100 Subject: [PATCH] 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 Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: Joyee Cheung Reviewed-By: David Carlier --- .../switches/does_not_own_process_state.js | 24 +++++++++++-- .../switches/does_own_process_state.js | 35 +++++++++++++++++++ .../bootstrap/switches/is_main_thread.js | 33 ----------------- .../bootstrap/switches/is_not_main_thread.js | 21 ----------- 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 1ec449b34a9f71..69946e802e1115 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 88f7752072015a..15023ea3f55fed 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 4f7ef7b6a69a68..ac8336fb6229e7 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 33e98d387cfd15..852352eead0d73 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); -}