Skip to content

Commit 69eaff1

Browse files
addaleaxtargos
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 ff47915 commit 69eaff1

4 files changed

Lines changed: 55 additions & 56 deletions

File tree

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
'use strict';
22

33
constcredentials=internalBinding('credentials');
4+
constrawMethods=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+
functionwrappedUmask(mask){
30+
// process.umask() is a read-only operation in workers.
31+
if(mask!==undefined){
32+
thrownewERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
33+
}
34+
35+
returnrawMethods.umask(mask);
36+
}

‎lib/internal/bootstrap/switches/does_own_process_state.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

33
constcredentials=internalBinding('credentials');
4+
constrawMethods=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
constwrapped=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
functionwrapPosixCredentialSetters(credentials){
2031
const{
2132
ArrayIsArray,
@@ -94,3 +105,26 @@ 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+
letcachedCwd='';
112+
113+
functionwrappedChdir(directory){
114+
validateString(directory,'directory');
115+
rawMethods.chdir(directory);
116+
// Mark cache that it requires an update.
117+
cachedCwd='';
118+
}
119+
120+
functionwrappedUmask(mask){
121+
if(mask!==undefined){
122+
mask=parseMode(mask,'mask');
123+
}
124+
returnrawMethods.umask(mask);
125+
}
126+
127+
functionwrappedCwd(){
128+
cachedCwd=rawMethods.cwd();
129+
returncachedCwd;
130+
}

‎lib/internal/bootstrap/switches/is_main_thread.js‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
const{ ObjectDefineProperty }=primordials;
44
constrawMethods=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,33 +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-
letcachedCwd='';
49-
50-
functionwrappedChdir(directory){
51-
validateString(directory,'directory');
52-
rawMethods.chdir(directory);
53-
// Mark cache that it requires an update.
54-
cachedCwd='';
55-
}
56-
57-
functionwrappedUmask(mask){
58-
if(mask!==undefined){
59-
mask=parseMode(mask,'mask');
60-
}
61-
returnrawMethods.umask(mask);
62-
}
63-
64-
functionwrappedCwd(){
65-
cachedCwd=rawMethods.cwd();
66-
returncachedCwd;
67-
}
6836

6937
functioncreateWritableStdioStream(fd){
7038
letstream;

‎lib/internal/bootstrap/switches/is_not_main_thread.js‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
'use strict';
22

33
const{ ObjectDefineProperty }=primordials;
4-
constrawMethods=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
deleteprocess._debugProcess;
156
deleteprocess._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
letworkerStdio;
5038
functionlazyWorkerStdio(){
@@ -57,12 +45,3 @@ function getStdout() { return lazyWorkerStdio().stdout; }
5745
functiongetStderr(){returnlazyWorkerStdio().stderr;}
5846

5947
functiongetStdin(){returnlazyWorkerStdio().stdin;}
60-
61-
functionwrappedUmask(mask){
62-
// process.umask() is a read-only operation in workers.
63-
if(mask!==undefined){
64-
thrownewERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
65-
}
66-
67-
returnrawMethods.umask(mask);
68-
}

0 commit comments

Comments
 (0)