Skip to content

Commit 42d36dc

Browse files
addaleaxBridgeAR
authored andcommitted
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: #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>
1 parent 614b074 commit 42d36dc

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
'use strict';
22

33
const credentials = internalBinding('credentials');
4+
const rawMethods = internalBinding('process_methods');
5+
// TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION
6+
const { unavailable } = require('internal/process/worker_thread_only');
47

5-
if (credentials.implementsPosixCredentials) {
6-
// TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION
7-
const { unavailable } = require('internal/process/worker_thread_only');
8+
process.abort = unavailable('process.abort()');
9+
process.chdir = unavailable('process.chdir()');
10+
process.umask = wrappedUmask;
11+
process.cwd = rawMethods.cwd;
812

13+
if (credentials.implementsPosixCredentials) {
914
process.initgroups = unavailable('process.initgroups()');
1015
process.setgroups = unavailable('process.setgroups()');
1116
process.setegid = unavailable('process.setegid()');
@@ -16,3 +21,16 @@ if (credentials.implementsPosixCredentials) {
1621

1722
// ---- keep the attachment of the wrappers above so that it's easier to ----
1823
// ---- compare the setups side-by-side -----
24+
25+
const {
26+
codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
27+
} = require('internal/errors');
28+
29+
function wrappedUmask(mask) {
30+
// process.umask() is a read-only operation in workers.
31+
if (mask !== undefined) {
32+
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
33+
}
34+
35+
return rawMethods.umask(mask);
36+
}

lib/internal/bootstrap/switches/does_own_process_state.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

33
const credentials = internalBinding('credentials');
4+
const rawMethods = internalBinding('process_methods');
5+
6+
process.abort = rawMethods.abort;
7+
process.umask = wrappedUmask;
8+
process.chdir = wrappedChdir;
9+
process.cwd = wrappedCwd;
410

511
if (credentials.implementsPosixCredentials) {
612
const wrapped = wrapPosixCredentialSetters(credentials);
@@ -16,6 +22,11 @@ if (credentials.implementsPosixCredentials) {
1622
// ---- keep the attachment of the wrappers above so that it's easier to ----
1723
// ---- compare the setups side-by-side -----
1824

25+
const {
26+
parseMode,
27+
validateString
28+
} = require('internal/validators');
29+
1930
function wrapPosixCredentialSetters(credentials) {
2031
const {
2132
ArrayIsArray,
@@ -94,3 +105,27 @@ function wrapPosixCredentialSetters(credentials) {
94105
setuid: wrapIdSetter('User', _setuid)
95106
};
96107
}
108+
109+
// Cache the working directory to prevent lots of lookups. If the working
110+
// directory is changed by `chdir`, it'll be updated.
111+
let cachedCwd = '';
112+
113+
function wrappedChdir(directory) {
114+
validateString(directory, 'directory');
115+
rawMethods.chdir(directory);
116+
// Mark cache that it requires an update.
117+
cachedCwd = '';
118+
}
119+
120+
function wrappedUmask(mask) {
121+
if (mask !== undefined) {
122+
mask = parseMode(mask, 'mask');
123+
}
124+
return rawMethods.umask(mask);
125+
}
126+
127+
function wrappedCwd() {
128+
if (cachedCwd === '')
129+
cachedCwd = rawMethods.cwd();
130+
return cachedCwd;
131+
}

lib/internal/bootstrap/switches/is_main_thread.js

-33
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
const { ObjectDefineProperty } = primordials;
44
const rawMethods = internalBinding('process_methods');
55

6-
process.abort = rawMethods.abort;
7-
process.umask = wrappedUmask;
8-
process.chdir = wrappedChdir;
9-
process.cwd = wrappedCwd;
10-
116
// TODO(joyeecheung): deprecate and remove these underscore methods
127
process._debugProcess = rawMethods._debugProcess;
138
process._debugEnd = rawMethods._debugEnd;
@@ -38,34 +33,6 @@ process.on('removeListener', stopListeningIfSignal);
3833
// ---- compare the setups side-by-side -----
3934

4035
const { guessHandleType } = internalBinding('util');
41-
const {
42-
parseMode,
43-
validateString
44-
} = require('internal/validators');
45-
46-
// Cache the working directory to prevent lots of lookups. If the working
47-
// directory is changed by `chdir`, it'll be updated.
48-
let cachedCwd = '';
49-
50-
function wrappedChdir(directory) {
51-
validateString(directory, 'directory');
52-
rawMethods.chdir(directory);
53-
// Mark cache that it requires an update.
54-
cachedCwd = '';
55-
}
56-
57-
function wrappedUmask(mask) {
58-
if (mask !== undefined) {
59-
mask = parseMode(mask, 'mask');
60-
}
61-
return rawMethods.umask(mask);
62-
}
63-
64-
function wrappedCwd() {
65-
if (cachedCwd === '')
66-
cachedCwd = rawMethods.cwd();
67-
return cachedCwd;
68-
}
6936

7037
function createWritableStdioStream(fd) {
7138
let stream;

lib/internal/bootstrap/switches/is_not_main_thread.js

-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
'use strict';
22

33
const { ObjectDefineProperty } = primordials;
4-
const rawMethods = internalBinding('process_methods');
5-
const {
6-
unavailable
7-
} = require('internal/process/worker_thread_only');
8-
9-
process.abort = unavailable('process.abort()');
10-
process.chdir = unavailable('process.chdir()');
11-
process.umask = wrappedUmask;
12-
process.cwd = rawMethods.cwd;
134

145
delete process._debugProcess;
156
delete process._debugEnd;
@@ -42,9 +33,6 @@ process.removeListener('removeListener', stopListeningIfSignal);
4233
const {
4334
createWorkerStdio
4435
} = require('internal/worker/io');
45-
const {
46-
codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
47-
} = require('internal/errors');
4836

4937
let workerStdio;
5038
function lazyWorkerStdio() {
@@ -57,12 +45,3 @@ function getStdout() { return lazyWorkerStdio().stdout; }
5745
function getStderr() { return lazyWorkerStdio().stderr; }
5846

5947
function getStdin() { return lazyWorkerStdio().stdin; }
60-
61-
function wrappedUmask(mask) {
62-
// process.umask() is a read-only operation in workers.
63-
if (mask !== undefined) {
64-
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
65-
}
66-
67-
return rawMethods.umask(mask);
68-
}

0 commit comments

Comments
 (0)