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