• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  ObjectDefineProperty,
5  globalThis,
6} = primordials;
7
8const {
9  defineOperation,
10  exposeInterface,
11  lazyDOMExceptionClass,
12  defineLazyProperties,
13  defineReplaceableLazyAttribute,
14  exposeLazyInterfaces,
15} = require('internal/util');
16const config = internalBinding('config');
17
18// https://console.spec.whatwg.org/#console-namespace
19exposeNamespace(globalThis, 'console',
20                createGlobalConsole());
21
22const { URL, URLSearchParams } = require('internal/url');
23// https://url.spec.whatwg.org/#url
24exposeInterface(globalThis, 'URL', URL);
25// https://url.spec.whatwg.org/#urlsearchparams
26exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
27exposeGetterAndSetter(globalThis,
28                      'DOMException',
29                      () => {
30                        const DOMException = lazyDOMExceptionClass();
31                        exposeInterface(globalThis, 'DOMException', DOMException);
32                        return DOMException;
33                      },
34                      (value) => {
35                        exposeInterface(globalThis, 'DOMException', value);
36                      });
37
38// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
39const timers = require('timers');
40defineOperation(globalThis, 'clearInterval', timers.clearInterval);
41defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
42defineOperation(globalThis, 'setInterval', timers.setInterval);
43defineOperation(globalThis, 'setTimeout', timers.setTimeout);
44
45// Lazy ones.
46exposeLazyInterfaces(globalThis, 'internal/worker/io', ['BroadcastChannel']);
47exposeLazyInterfaces(globalThis, 'internal/abort_controller', [
48  'AbortController', 'AbortSignal',
49]);
50const {
51  EventTarget, Event,
52} = require('internal/event_target');
53exposeInterface(globalThis, 'Event', Event);
54exposeInterface(globalThis, 'EventTarget', EventTarget);
55exposeLazyInterfaces(globalThis, 'internal/worker/io', [
56  'MessageChannel', 'MessagePort', 'MessageEvent',
57]);
58defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
59// https://www.w3.org/TR/FileAPI/#dfn-Blob
60exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
61// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
62
63exposeLazyInterfaces(globalThis, 'perf_hooks', [
64  'Performance',
65]);
66
67defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
68
69// https://encoding.spec.whatwg.org/#textencoder
70// https://encoding.spec.whatwg.org/#textdecoder
71exposeLazyInterfaces(globalThis,
72                     'internal/encoding',
73                     ['TextEncoder', 'TextDecoder']);
74
75function createGlobalConsole() {
76  const consoleFromNode =
77    require('internal/console/global');
78  if (config.hasInspector) {
79    const inspector = require('internal/util/inspector');
80    // TODO(joyeecheung): postpone this until the first time inspector
81    // is activated.
82    inspector.wrapConsole(consoleFromNode);
83    const { setConsoleExtensionInstaller } = internalBinding('inspector');
84    // Setup inspector command line API.
85    setConsoleExtensionInstaller(inspector.installConsoleExtensions);
86  }
87  return consoleFromNode;
88}
89
90// https://heycam.github.io/webidl/#es-namespaces
91function exposeNamespace(target, name, namespaceObject) {
92  ObjectDefineProperty(target, name, {
93    __proto__: null,
94    writable: true,
95    enumerable: false,
96    configurable: true,
97    value: namespaceObject,
98  });
99}
100
101function exposeGetterAndSetter(target, name, getter, setter = undefined) {
102  ObjectDefineProperty(target, name, {
103    __proto__: null,
104    enumerable: false,
105    configurable: true,
106    get: getter,
107    set: setter,
108  });
109}
110
111// Web Streams API
112exposeLazyInterfaces(
113  globalThis,
114  'internal/webstreams/transformstream',
115  ['TransformStream', 'TransformStreamDefaultController']);
116
117exposeLazyInterfaces(
118  globalThis,
119  'internal/webstreams/writablestream',
120  ['WritableStream', 'WritableStreamDefaultController', 'WritableStreamDefaultWriter']);
121
122exposeLazyInterfaces(
123  globalThis,
124  'internal/webstreams/readablestream',
125  [
126    'ReadableStream', 'ReadableStreamDefaultReader',
127    'ReadableStreamBYOBReader', 'ReadableStreamBYOBRequest',
128    'ReadableByteStreamController', 'ReadableStreamDefaultController',
129  ]);
130
131exposeLazyInterfaces(
132  globalThis,
133  'internal/webstreams/queuingstrategies',
134  [
135    'ByteLengthQueuingStrategy', 'CountQueuingStrategy',
136  ]);
137
138exposeLazyInterfaces(
139  globalThis,
140  'internal/webstreams/encoding',
141  [
142    'TextEncoderStream', 'TextDecoderStream',
143  ]);
144
145exposeLazyInterfaces(
146  globalThis,
147  'internal/webstreams/compression',
148  [
149    'CompressionStream', 'DecompressionStream',
150  ]);
151