简体   繁体   English

NodeJs global.process

[英]NodeJs global.process

Why is the Object.keys function on the global object not returning the process field?为什么全局object上的Object.keys function没有返回process字段?

> Object.keys(global)
[
  'global',
  'clearInterval',
  'clearTimeout',
  'setInterval',
  'setTimeout',
  'queueMicrotask',
  'clearImmediate',
  'setImmediate'
]
> global.process
process {
  version: 'v13.7.0',
  versions: {
    node: '13.7.0',
    v8: '7.9.317.25-node.28',
    uv: '1.34.1',

process is not actually a key on the global object as of node version 12 (Described in the changelog https://nodejs.org/tr/blog/uncategorized/10-lts-to-12-lts/#notable-changes-in-node-js-12-0-0 , implemented through PR https://github.com/nodejs/node/pull/26882 ).从节点版本 12 开始, process实际上不是全局 object 上的键(在变更日志https 中描述://nodejs.org/tr/blog/uncategorized/10-lts-to-12-lts/#notable-changes-in -node-js-12-0-0 ,通过 PR https://github.com/nodejs/node/pull/26882实现)。 Instead it has a defined getter for it.相反,它有一个定义的吸气剂。 Eg try out global.__lookupGetter__('process') and you should see that you get a function back.例如尝试global.__lookupGetter__('process')你应该看到你得到一个 function 回来。

> Object.keys(global)
[
  'global',
  'clearInterval',
  'clearTimeout',
  'setInterval',
  'setTimeout',
  'queueMicrotask',
  'clearImmediate',
  'setImmediate'
]
> global.__lookupGetter__('process')
[Function: get]
> global.__lookupGetter__('process')()
process {
  version: 'v13.7.0',
  versions: {
    node: '13.7.0',
...

The reason for the change as described in the PR: PR中描述的更改原因:

This implements just the semver major breaking changes from #26334 Restrict process and Buffer globals to CommonJS, which is to make global.process and global.Buffer getters / setters over value properties.这仅实现了从 #26334 Restrict process 和 Buffer globals 到 CommonJS 的重大重大更改,即使 global.process 和 global.Buffer getter / setter 覆盖值属性。

... ...

The other referenced PR is https://github.com/nodejs/node/pull/26334 which is described as:另一个引用的 PR 是https://github.com/nodejs/node/pull/26334 ,描述为:

This PR deprecates access to global.process and global.Buffer access in ECMAScript modules only, while ensuring they continue to behave fine for all CommonJS modules and contexts providing comprehensive backwards compatibility.此 PR 仅在 ECMAScript 模块中弃用对 global.process 和 global.Buffer 的访问,同时确保它们在所有 CommonJS 模块和上下文中继续表现良好,提供全面的向后兼容性。

This is done by making them getters which check the context in which they are called and throw when inside an ECMAScript module.这是通过使它们成为 getter 来完成的,这些 getter 检查调用它们的上下文并在 ECMAScript 模块内抛出。 To avoid this getter slowpath in accesses, process and Buffer are also added as a context to the compileFunction wrapper in CJS.为了避免在访问中出现这种 getter 慢路径,进程和缓冲区也作为上下文添加到 CJS 中的 compileFunction 包装器中。 In addition these getters are only defined as soon as there is a load of an ECMAScript module to avoid any slowdowns in these cases.此外,这些 getter 仅在加载 ECMAScript 模块时才定义,以避免在这些情况下出现任何减速。

The benefits of this are that then ECMAScript modules don't by default have to assume access to all the root-level-security functions and properties on process allowing access control to be added in future, as well as helping towards browser compatibility by making process an import.这样做的好处是,ECMAScript 模块默认情况下不必假定访问进程上的所有根级安全功能和属性,从而允许将来添加访问控制,以及通过制作进程来帮助实现浏览器兼容性进口。

ECMAScript modules share the same realm global, so there isn't a way to do this otherwise. ECMAScript 模块共享相同的 realm 全局,因此没有其他方法可以做到这一点。 In addition once users start running and writing and publishing ECMAScript modules in Node.js that assume the process and Buffer globals exist, it becomes very difficult to change this then.此外,一旦用户开始在 Node.js 中运行、编写和发布 ECMAScript 模块,假设进程和 Buffer 全局变量存在,那么就很难改变这一点。

For these reasons I think it is quite important to land this with the ECMAScript modules implementation in Node.js to provide these properties, or risk never being able to provide them at all in Node.js due to ecosystem compatibility constraints.由于这些原因,我认为通过 Node.js 中的 ECMAScript 模块实现来提供这些属性非常重要,否则由于生态系统兼容性限制,可能永远无法在 Node.js 中提供它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM