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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// This is a "special" module, in that it define the global runtime scope of
// Deno, and therefore it defines a lot of the runtime environment that code
// is evaluated in.
// By convention we import those items that are globally exposed as namespaces
import * as blob from "./blob.ts";
import * as consoleTypes from "./console.ts";
import * as csprng from "./get_random_values.ts";
import * as customEvent from "./custom_event.ts";
import * as Deno from "./deno.ts";
import * as domTypes from "./dom_types.ts";
import * as domFile from "./dom_file.ts";
import * as event from "./event.ts";
import * as eventTarget from "./event_target.ts";
import * as formData from "./form_data.ts";
import * as fetchTypes from "./fetch.ts";
import * as headers from "./headers.ts";
import * as textEncoding from "./text_encoding.ts";
import * as timers from "./timers.ts";
import * as url from "./url.ts";
import * as urlSearchParams from "./url_search_params.ts";
import * as workerRuntime from "./worker_main.ts";
import * as workers from "./workers.ts";
import * as performanceUtil from "./performance.ts";
import * as request from "./request.ts";
// These imports are not exposed and therefore are fine to just import the
// symbols required.
import { core } from "./core.ts";
import { internalObject } from "./internals.ts";
// This global augmentation is just enough types to be able to build Deno,
// the runtime types are fully defined in `lib.deno_runtime.d.ts`.
declare global {
interface CallSite {
getThis(): unknown;
getTypeName(): string;
getFunction(): Function;
getFunctionName(): string;
getMethodName(): string;
getFileName(): string;
getLineNumber(): number | null;
getColumnNumber(): number | null;
getEvalOrigin(): string | null;
isToplevel(): boolean;
isEval(): boolean;
isNative(): boolean;
isConstructor(): boolean;
isAsync(): boolean;
isPromiseAll(): boolean;
getPromiseIndex(): number | null;
}
interface ErrorConstructor {
prepareStackTrace(error: Error, structuredStackTrace: CallSite[]): string;
}
interface Object {
[consoleTypes.customInspect]?(): string;
}
interface EvalErrorInfo {
isNativeError: boolean;
isCompileError: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
thrown: any;
}
interface DenoCore {
print(s: string, isErr?: boolean): void;
dispatch(
opId: number,
control: Uint8Array,
zeroCopy?: ArrayBufferView | null
): Uint8Array | null;
setAsyncHandler(opId: number, cb: (msg: Uint8Array) => void): void;
sharedQueue: {
head(): number;
numRecords(): number;
size(): number;
push(buf: Uint8Array): boolean;
reset(): void;
shift(): Uint8Array | null;
};
ops(): Record<string, number>;
recv(cb: (opId: number, msg: Uint8Array) => void): void;
send(
opId: number,
control: null | ArrayBufferView,
data?: ArrayBufferView
): null | Uint8Array;
shared: SharedArrayBuffer;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evalContext(code: string): [any, EvalErrorInfo | null];
errorToJSON: (e: Error) => string;
}
// Only `var` variables show up in the `globalThis` type when doing a global
// scope augmentation.
/* eslint-disable no-var */
var addEventListener: (
type: string,
callback: (event: domTypes.Event) => void | null,
options?: boolean | domTypes.AddEventListenerOptions | undefined
) => void;
var bootstrapTsCompiler: (() => void) | undefined;
var console: consoleTypes.Console;
var Deno: {
core: DenoCore;
};
var bootstrapCompilerRuntime: ((compilerType: string) => void) | undefined;
var bootstrapMainRuntime: (() => void) | undefined;
var location: domTypes.Location;
var onerror:
| ((
msg: string,
source: string,
lineno: number,
colno: number,
e: domTypes.Event
) => boolean | void)
| undefined;
var onload: ((e: domTypes.Event) => void) | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var onmessage: ((e: { data: any }) => Promise<void> | void) | undefined;
var onunload: ((e: domTypes.Event) => void) | undefined;
var queueMicrotask: (callback: () => void) => void;
var bootstrapWasmCompiler: (() => void) | undefined;
var bootstrapWorkerRuntime: (() => Promise<void> | void) | undefined;
/* eslint-enable */
}
// Add internal object to Deno object.
// This is not exposed as part of the Deno types.
// @ts-ignore
Deno[Deno.symbols.internal] = internalObject;
function writable(value: unknown): PropertyDescriptor {
return {
value,
writable: true,
enumerable: true,
configurable: true
};
}
function nonEnumerable(value: unknown): PropertyDescriptor {
return {
value,
writable: true,
configurable: true
};
}
function readOnly(value: unknown): PropertyDescriptor {
return {
value,
enumerable: true
};
}
const globalProperties = {
window: readOnly(globalThis),
Deno: readOnly(Deno),
atob: writable(textEncoding.atob),
btoa: writable(textEncoding.btoa),
fetch: writable(fetchTypes.fetch),
clearTimeout: writable(timers.clearTimeout),
clearInterval: writable(timers.clearInterval),
console: writable(new consoleTypes.Console(core.print)),
setTimeout: writable(timers.setTimeout),
setInterval: writable(timers.setInterval),
onload: writable(undefined),
onunload: writable(undefined),
crypto: readOnly(csprng),
Blob: nonEnumerable(blob.DenoBlob),
File: nonEnumerable(domFile.DomFileImpl),
CustomEvent: nonEnumerable(customEvent.CustomEvent),
Event: nonEnumerable(event.Event),
EventTarget: nonEnumerable(eventTarget.EventTarget),
URL: nonEnumerable(url.URL),
URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParams),
Headers: nonEnumerable(headers.Headers),
FormData: nonEnumerable(formData.FormData),
TextEncoder: nonEnumerable(textEncoding.TextEncoder),
TextDecoder: nonEnumerable(textEncoding.TextDecoder),
Request: nonEnumerable(request.Request),
Response: nonEnumerable(fetchTypes.Response),
performance: writable(new performanceUtil.Performance()),
onmessage: writable(workerRuntime.onmessage),
onerror: writable(workerRuntime.onerror),
bootstrapWorkerRuntime: nonEnumerable(workerRuntime.bootstrapWorkerRuntime),
workerClose: nonEnumerable(workerRuntime.workerClose),
postMessage: writable(workerRuntime.postMessage),
Worker: nonEnumerable(workers.WorkerImpl),
[domTypes.eventTargetHost]: nonEnumerable(null),
[domTypes.eventTargetListeners]: nonEnumerable({}),
[domTypes.eventTargetMode]: nonEnumerable(""),
[domTypes.eventTargetNodeType]: nonEnumerable(0),
[eventTarget.eventTargetAssignedSlot]: nonEnumerable(false),
[eventTarget.eventTargetHasActivationBehavior]: nonEnumerable(false),
addEventListener: readOnly(
eventTarget.EventTarget.prototype.addEventListener
),
dispatchEvent: readOnly(eventTarget.EventTarget.prototype.dispatchEvent),
removeEventListener: readOnly(
eventTarget.EventTarget.prototype.removeEventListener
)
};
Object.defineProperties(globalThis, globalProperties);
// Registers the handler for window.onload function.
globalThis.addEventListener("load", (e: domTypes.Event): void => {
const { onload } = globalThis;
if (typeof onload === "function") {
onload(e);
}
});
// Registers the handler for window.onunload function.
globalThis.addEventListener("unload", (e: domTypes.Event): void => {
const { onunload } = globalThis;
if (typeof onunload === "function") {
onunload(e);
}
});
|