Skip to content

Commit 9fbcb61

Browse files
committed
process: make process.config read only
1 parent 49a5e81 commit 9fbcb61

File tree

4 files changed

+22
-93
lines changed

4 files changed

+22
-93
lines changed

doc/api/deprecations.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,21 +2885,6 @@ Type: Documentation-only.
28852885

28862886
Prefer [`message.socket`][] over [`message.connection`][].
28872887

2888-
### DEP0150: Changing the value of `process.config`
2889-
2890-
<!-- YAML
2891-
changes:
2892-
- version: v16.0.0
2893-
pr-url: https://github.com/nodejs/node/pull/36902
2894-
description: Runtime deprecation.
2895-
-->
2896-
2897-
Type: Runtime
2898-
2899-
The `process.config` property provides access to Node.js compile-time settings.
2900-
However, the property is mutable and therefore subject to tampering. The ability
2901-
to change the value will be removed in a future version of Node.js.
2902-
29032888
### DEP0151: Main index lookup and extension searching
29042889

29052890
<!-- YAML

doc/api/process.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,17 +1041,21 @@ This feature is not available in [`Worker`][] threads.
10411041
<!-- YAML
10421042
added: v0.7.7
10431043
changes:
1044+
- version: REPLACEME
1045+
pr-url: https://github.com/nodejs/node/pull/43627
1046+
description: The object returned by `process.config` is read-only. Modifying
1047+
it in strict mode will throw an error.
10441048
- version: v16.0.0
10451049
pr-url: https://github.com/nodejs/node/pull/36902
10461050
description: Modifying process.config has been deprecated.
10471051
-->
10481052

10491053
* {Object}
10501054

1051-
The `process.config` property returns an `Object` containing the JavaScript
1052-
representation of the configure options used to compile the current Node.js
1053-
executable. This is the same as the `config.gypi` file that was produced when
1054-
running the `./configure` script.
1055+
The `process.config` property returns a read-only `Object` containing the
1056+
JavaScript representation of the configure options used to compile the current
1057+
Node.js executable. This is the same as the `config.gypi` file that was produced
1058+
when running the `./configure` script.
10551059

10561060
An example of the possible output looks like:
10571061

@@ -1085,14 +1089,6 @@ An example of the possible output looks like:
10851089
}
10861090
```
10871091

1088-
The `process.config` property is **not** read-only and there are existing
1089-
modules in the ecosystem that are known to extend, modify, or entirely replace
1090-
the value of `process.config`.
1091-
1092-
Modifying the `process.config` property, or any child-property of the
1093-
`process.config` object has been deprecated. The `process.config` will be made
1094-
read-only in a future release.
1095-
10961092
## `process.connected`
10971093

10981094
<!-- YAML

lib/internal/bootstrap/node.js

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ const {
4646
JSONParse,
4747
ObjectDefineProperty,
4848
ObjectGetPrototypeOf,
49-
ObjectPreventExtensions,
5049
ObjectSetPrototypeOf,
51-
ReflectGet,
52-
ReflectSet,
50+
ObjectFreeze,
5351
SymbolToStringTag,
5452
globalThis,
5553
} = primordials;
@@ -72,75 +70,15 @@ process._exiting = false;
7270
// process.config is serialized config.gypi
7371
const nativeModule = internalBinding('native_module');
7472

75-
// TODO(@jasnell): Once this has gone through one full major
76-
// release cycle, remove the Proxy and setter and update the
77-
// getter to either return a read-only object or always return
78-
// a freshly parsed version of nativeModule.config.
79-
80-
const deprecationHandler = {
81-
warned: false,
82-
message: 'Setting process.config is deprecated. ' +
83-
'In the future the property will be read-only.',
84-
code: 'DEP0150',
85-
maybeWarn() {
86-
if (!this.warned) {
87-
process.emitWarning(this.message, {
88-
type: 'DeprecationWarning',
89-
code: this.code
90-
});
91-
this.warned = true;
92-
}
93-
},
94-
95-
defineProperty(target, key, descriptor) {
96-
this.maybeWarn();
97-
return ObjectDefineProperty(target, key, descriptor);
98-
},
99-
100-
deleteProperty(target, key) {
101-
this.maybeWarn();
102-
delete target[key];
103-
},
104-
105-
preventExtensions(target) {
106-
this.maybeWarn();
107-
return ObjectPreventExtensions(target);
108-
},
109-
110-
set(target, key, value) {
111-
this.maybeWarn();
112-
return ReflectSet(target, key, value);
113-
},
114-
115-
get(target, key, receiver) {
116-
const val = ReflectGet(target, key, receiver);
117-
if (val != null && typeof val === 'object') {
118-
// eslint-disable-next-line node-core/prefer-primordials
119-
return new Proxy(val, deprecationHandler);
120-
}
121-
return val;
122-
},
123-
124-
setPrototypeOf(target, proto) {
125-
this.maybeWarn();
126-
return ObjectSetPrototypeOf(target, proto);
127-
}
128-
};
129-
130-
// eslint-disable-next-line node-core/prefer-primordials
131-
let processConfig = new Proxy(
132-
JSONParse(nativeModule.config),
133-
deprecationHandler);
73+
const processConfig = JSONParse(nativeModule.config, (_key, value) => {
74+
return ObjectFreeze(value);
75+
});
13476

13577
ObjectDefineProperty(process, 'config', {
13678
__proto__: null,
13779
enumerable: true,
13880
configurable: true,
13981
get() { return processConfig; },
140-
set(value) {
141-
deprecationHandler.maybeWarn();
142-
processConfig = value;
143-
}
14482
});
14583

14684
require('internal/worker/js_transferable').setup();

test/parallel/test-process-config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ assert(Object.hasOwn(process, 'config'));
3636
// Ensure that `process.config` is an Object.
3737
assert.strictEqual(Object(process.config), process.config);
3838

39+
// Ensure that you can't change config values
40+
try {
41+
process.config.variables = 42;
42+
} catch (e) {
43+
assert.strictEqual(
44+
e.message,
45+
"Cannot assign to read only property 'variables' of object '#<Object>'"
46+
);
47+
}
48+
3949
const configPath = path.resolve(__dirname, '..', '..', 'config.gypi');
4050

4151
if (!fs.existsSync(configPath)) {

0 commit comments

Comments
 (0)