Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are primordials in Node.js?

Tags:

node.js

I've just started to read Node.js source code, and something that I notice quite a lot is the use of an object called primordials. But I can't seem find where its definition is.

E.g: /node/lib/events.js

const {
  Array,
  Boolean,
  Error,
  MathMin,
  NumberIsNaN,
  ObjectCreate,
  ObjectDefineProperty,
  ObjectGetPrototypeOf,
  ObjectSetPrototypeOf,
  ObjectKeys,
  Promise,
  PromiseReject,
  PromiseResolve,
  ReflectApply,
  ReflectOwnKeys,
  Symbol,
  SymbolFor,
  SymbolAsyncIterator
} = primordials;

Can someone explain what primordials are and where it is declared?

like image 982
Ian Guimarães Avatar asked Jan 15 '20 11:01

Ian Guimarães


People also ask

What is Deno node JS?

Deno is a JavaScript and TypeScript runtime similar to Node. js, built on Rust and the V8 JavaScript engine. It was created by Ryan Dahl, the original inventor of Node. js, to counter mistakes he made when he originally designed and released Node.


Video Answer


1 Answers

Some brief research revealed this github issue leading to this node source file with some enlightening comments about the purpose of primordials:

// This file subclasses and stores the JS builtins that come from the VM
// so that Node.js's builtin modules do not need to later look these up from
// the global proxy, which can be mutated by users.

So, as mentioned in the issue, the primordials object is meant to be a way to guarantee that node builtin modules can access the true untampered globals instead of ones which may have been modified by users.

like image 69
Klaycon Avatar answered Oct 22 '22 22:10

Klaycon