Skip to content

Commit

Permalink
lib: move initialization of APIs for changing process state
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
addaleax authored and Trott committed Jan 6, 2020
1 parent 714eb91 commit 20fd123
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 57 deletions.
24 changes: 21 additions & 3 deletions lib/internal/bootstrap/switches/does_not_own_process_state.js
Original file line number Diff line number Diff line change
@@ -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()');
Expand All @@ -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);
}
35 changes: 35 additions & 0 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
33 changes: 0 additions & 33 deletions lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 0 additions & 21 deletions lib/internal/bootstrap/switches/is_not_main_thread.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}

0 comments on commit 20fd123

Please sign in to comment.