All files / lib/internal/console global.js

100% Statements 54/54
100% Branches 4/4
100% Functions 0/0
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 5523x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 621x 598x 598x 598x 598x 598x 598x 598x 598x 23x 23x 23x 23x 23x 23x 23x 23x 23x  
'use strict';
 
// See https://console.spec.whatwg.org/#console-namespace
// > For historical web-compatibility reasons, the namespace object
// > for console must have as its [[Prototype]] an empty object,
// > created as if by ObjectCreate(%ObjectPrototype%),
// > instead of %ObjectPrototype%.
 
// Since in Node.js, the Console constructor has been exposed through
// require('console'), we need to keep the Console constructor but
// we cannot actually use `new Console` to construct the global console.
// Therefore, the console.Console.prototype is not
// in the global console prototype chain anymore.
 
const {
  FunctionPrototypeBind,
  ObjectCreate,
  ReflectDefineProperty,
  ReflectGetOwnPropertyDescriptor,
  ReflectOwnKeys,
} = primordials;
 
const {
  Console,
  kBindStreamsLazy,
  kBindProperties
} = require('internal/console/constructor');
 
const globalConsole = ObjectCreate({});
 
// Since Console is not on the prototype chain of the global console,
// the symbol properties on Console.prototype have to be looked up from
// the global console itself. In addition, we need to make the global
// console a namespace by binding the console methods directly onto
// the global console with the receiver fixed.
for (const prop of ReflectOwnKeys(Console.prototype)) {
  if (prop === 'constructor') { continue; }
  const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
  if (typeof desc.value === 'function') { // fix the receiver
    const name = desc.value.name;
    desc.value = FunctionPrototypeBind(desc.value, globalConsole);
    ReflectDefineProperty(desc.value, 'name', { value: name });
  }
  ReflectDefineProperty(globalConsole, prop, desc);
}
 
globalConsole[kBindStreamsLazy](process);
globalConsole[kBindProperties](true, 'auto');
 
// This is a legacy feature - the Console constructor is exposed on
// the global console instance.
globalConsole.Console = Console;
 
module.exports = globalConsole;