2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-20 09:30:30 -05:00
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
import "./lib.deno.shared_globals.d.ts";
|
|
|
|
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as blob from "./web/blob.ts";
|
2020-03-11 05:53:06 -04:00
|
|
|
import * as consoleTypes from "./web/console.ts";
|
2020-03-30 19:01:19 -04:00
|
|
|
import * as promiseTypes from "./web/promise.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as customEvent from "./web/custom_event.ts";
|
2020-04-11 11:42:02 -04:00
|
|
|
import * as domException from "./web/dom_exception.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as domFile from "./web/dom_file.ts";
|
|
|
|
import * as event from "./web/event.ts";
|
|
|
|
import * as eventTarget from "./web/event_target.ts";
|
|
|
|
import * as formData from "./web/form_data.ts";
|
|
|
|
import * as fetchTypes from "./web/fetch.ts";
|
|
|
|
import * as headers from "./web/headers.ts";
|
|
|
|
import * as textEncoding from "./web/text_encoding.ts";
|
2020-03-11 05:53:06 -04:00
|
|
|
import * as timers from "./web/timers.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as url from "./web/url.ts";
|
|
|
|
import * as urlSearchParams from "./web/url_search_params.ts";
|
2020-03-11 05:53:06 -04:00
|
|
|
import * as workers from "./web/workers.ts";
|
|
|
|
import * as performanceUtil from "./web/performance.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as request from "./web/request.ts";
|
2020-04-03 14:55:23 -04:00
|
|
|
import * as streams from "./web/streams/mod.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2018-11-08 19:09:18 -05:00
|
|
|
// These imports are not exposed and therefore are fine to just import the
|
|
|
|
// symbols required.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { core } from "./core.ts";
|
2020-01-16 19:42:58 -05:00
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
// This global augmentation is just enough types to be able to build Deno,
|
2020-04-09 08:34:24 -04:00
|
|
|
// the runtime types are fully defined in `lib.deno.*.d.ts`.
|
2018-10-11 17:23:13 -04:00
|
|
|
declare global {
|
2019-07-29 05:11:08 -04:00
|
|
|
interface CallSite {
|
|
|
|
getThis(): unknown;
|
2020-04-13 10:54:16 -04:00
|
|
|
getTypeName(): string | null;
|
|
|
|
getFunction(): Function | null;
|
|
|
|
getFunctionName(): string | null;
|
|
|
|
getMethodName(): string | null;
|
|
|
|
getFileName(): string | null;
|
2019-07-29 05:11:08 -04:00
|
|
|
getLineNumber(): number | null;
|
|
|
|
getColumnNumber(): number | null;
|
|
|
|
getEvalOrigin(): string | null;
|
2020-04-13 10:54:16 -04:00
|
|
|
isToplevel(): boolean | null;
|
2019-07-29 05:11:08 -04:00
|
|
|
isEval(): boolean;
|
|
|
|
isNative(): boolean;
|
|
|
|
isConstructor(): boolean;
|
|
|
|
isAsync(): boolean;
|
|
|
|
isPromiseAll(): boolean;
|
|
|
|
getPromiseIndex(): number | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ErrorConstructor {
|
|
|
|
prepareStackTrace(error: Error, structuredStackTrace: CallSite[]): string;
|
|
|
|
}
|
2019-08-19 11:35:43 -04:00
|
|
|
|
|
|
|
interface Object {
|
|
|
|
[consoleTypes.customInspect]?(): string;
|
|
|
|
}
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
interface EvalErrorInfo {
|
|
|
|
isNativeError: boolean;
|
|
|
|
isCompileError: boolean;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
thrown: any;
|
|
|
|
}
|
2018-08-07 16:27:31 -04:00
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
interface ImportMeta {
|
|
|
|
url: string;
|
|
|
|
main: boolean;
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
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;
|
|
|
|
|
2020-03-19 10:45:28 -04:00
|
|
|
setMacrotaskCallback(cb: () => boolean): void;
|
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
shared: SharedArrayBuffer;
|
|
|
|
|
2020-03-19 10:42:07 -04:00
|
|
|
evalContext(
|
|
|
|
code: string,
|
|
|
|
scriptName?: string
|
|
|
|
): [unknown, EvalErrorInfo | null];
|
2020-01-20 09:30:30 -05:00
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
formatError: (e: Error) => string;
|
2020-03-15 10:31:55 -04:00
|
|
|
|
2020-03-30 19:01:19 -04:00
|
|
|
/**
|
|
|
|
* Get promise details as two elements array.
|
|
|
|
*
|
|
|
|
* First element is the `PromiseState`.
|
|
|
|
* If promise isn't pending, second element would be the result of the promise.
|
|
|
|
* Otherwise, second element would be undefined.
|
|
|
|
*
|
|
|
|
* Throws `TypeError` if argument isn't a promise
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getPromiseDetails<T>(promise: Promise<T>): promiseTypes.PromiseDetails<T>;
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
decode(bytes: Uint8Array): string;
|
|
|
|
encode(text: string): Uint8Array;
|
2020-01-20 09:30:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only `var` variables show up in the `globalThis` type when doing a global
|
|
|
|
// scope augmentation.
|
|
|
|
/* eslint-disable no-var */
|
2020-01-27 21:12:25 -05:00
|
|
|
|
|
|
|
// Assigned to `window` global - main runtime
|
2020-01-20 09:30:30 -05:00
|
|
|
var Deno: {
|
|
|
|
core: DenoCore;
|
|
|
|
};
|
2020-04-11 11:42:02 -04:00
|
|
|
var onload: ((e: Event) => void) | undefined;
|
|
|
|
var onunload: ((e: Event) => void) | undefined;
|
2020-01-21 09:53:29 -05:00
|
|
|
var bootstrapMainRuntime: (() => void) | undefined;
|
2020-01-27 21:12:25 -05:00
|
|
|
|
|
|
|
// Assigned to `self` global - worker runtime and compiler
|
|
|
|
var bootstrapWorkerRuntime:
|
|
|
|
| ((name: string) => Promise<void> | void)
|
|
|
|
| undefined;
|
2020-01-20 09:30:30 -05:00
|
|
|
var onerror:
|
|
|
|
| ((
|
|
|
|
msg: string,
|
|
|
|
source: string,
|
|
|
|
lineno: number,
|
|
|
|
colno: number,
|
2020-04-11 11:42:02 -04:00
|
|
|
e: Event
|
2020-01-20 09:30:30 -05:00
|
|
|
) => boolean | void)
|
|
|
|
| undefined;
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
var onmessage: ((e: { data: any }) => Promise<void> | void) | undefined;
|
2020-01-27 21:12:25 -05:00
|
|
|
// Called in compiler
|
2020-01-29 12:54:23 -05:00
|
|
|
var close: () => void;
|
2020-01-27 21:12:25 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
var postMessage: (msg: any) => void;
|
|
|
|
// Assigned to `self` global - compiler
|
|
|
|
var bootstrapTsCompilerRuntime: (() => void) | undefined;
|
|
|
|
var bootstrapWasmCompilerRuntime: (() => void) | undefined;
|
2020-01-20 09:30:30 -05:00
|
|
|
/* eslint-enable */
|
|
|
|
}
|
2018-08-06 18:37:32 -04:00
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
export function writable(value: unknown): PropertyDescriptor {
|
2020-01-20 09:30:30 -05:00
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
configurable: true,
|
2020-01-20 09:30:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
export function nonEnumerable(value: unknown): PropertyDescriptor {
|
2020-01-20 09:30:30 -05:00
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
configurable: true,
|
2020-01-20 09:30:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
export function readOnly(value: unknown): PropertyDescriptor {
|
2020-01-20 09:30:30 -05:00
|
|
|
return {
|
|
|
|
value,
|
2020-03-28 13:03:49 -04:00
|
|
|
enumerable: true,
|
2020-01-20 09:30:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:56:40 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export function getterOnly(getter: () => any): PropertyDescriptor {
|
|
|
|
return {
|
|
|
|
get: getter,
|
2020-03-28 13:03:49 -04:00
|
|
|
enumerable: true,
|
2020-03-24 23:56:40 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope
|
|
|
|
export const windowOrWorkerGlobalScopeMethods = {
|
2020-01-20 09:30:30 -05:00
|
|
|
atob: writable(textEncoding.atob),
|
|
|
|
btoa: writable(textEncoding.btoa),
|
|
|
|
clearInterval: writable(timers.clearInterval),
|
2020-01-27 21:12:25 -05:00
|
|
|
clearTimeout: writable(timers.clearTimeout),
|
|
|
|
fetch: writable(fetchTypes.fetch),
|
|
|
|
// queueMicrotask is bound in Rust
|
2020-01-20 09:30:30 -05:00
|
|
|
setInterval: writable(timers.setInterval),
|
2020-03-28 13:03:49 -04:00
|
|
|
setTimeout: writable(timers.setTimeout),
|
2020-01-27 21:12:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Other properties shared between WindowScope and WorkerGlobalScope
|
|
|
|
export const windowOrWorkerGlobalScopeProperties = {
|
|
|
|
console: writable(new consoleTypes.Console(core.print)),
|
2020-01-20 09:30:30 -05:00
|
|
|
Blob: nonEnumerable(blob.DenoBlob),
|
|
|
|
File: nonEnumerable(domFile.DomFileImpl),
|
2020-04-11 11:42:02 -04:00
|
|
|
CustomEvent: nonEnumerable(customEvent.CustomEventImpl),
|
|
|
|
DOMException: nonEnumerable(domException.DOMExceptionImpl),
|
|
|
|
Event: nonEnumerable(event.EventImpl),
|
|
|
|
EventTarget: nonEnumerable(eventTarget.EventTargetImpl),
|
2020-04-11 17:19:36 -04:00
|
|
|
URL: nonEnumerable(url.URLImpl),
|
|
|
|
URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParamsImpl),
|
2020-01-20 09:30:30 -05:00
|
|
|
Headers: nonEnumerable(headers.Headers),
|
|
|
|
FormData: nonEnumerable(formData.FormData),
|
|
|
|
TextEncoder: nonEnumerable(textEncoding.TextEncoder),
|
|
|
|
TextDecoder: nonEnumerable(textEncoding.TextDecoder),
|
2020-04-03 14:55:23 -04:00
|
|
|
ReadableStream: nonEnumerable(streams.ReadableStream),
|
2020-01-20 09:30:30 -05:00
|
|
|
Request: nonEnumerable(request.Request),
|
|
|
|
Response: nonEnumerable(fetchTypes.Response),
|
|
|
|
performance: writable(new performanceUtil.Performance()),
|
2020-03-28 13:03:49 -04:00
|
|
|
Worker: nonEnumerable(workers.WorkerImpl),
|
2020-01-27 21:12:25 -05:00
|
|
|
};
|
2020-01-20 09:30:30 -05:00
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export function setEventTargetData(value: any): void {
|
|
|
|
eventTarget.eventTargetData.set(value, eventTarget.getDefaultTargetData());
|
|
|
|
}
|
2020-01-20 09:30:30 -05:00
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
export const eventTargetProperties = {
|
2020-01-20 09:30:30 -05:00
|
|
|
addEventListener: readOnly(
|
2020-04-11 11:42:02 -04:00
|
|
|
eventTarget.EventTargetImpl.prototype.addEventListener
|
2020-01-20 09:30:30 -05:00
|
|
|
),
|
2020-04-11 11:42:02 -04:00
|
|
|
dispatchEvent: readOnly(eventTarget.EventTargetImpl.prototype.dispatchEvent),
|
2020-01-20 09:30:30 -05:00
|
|
|
removeEventListener: readOnly(
|
2020-04-11 11:42:02 -04:00
|
|
|
eventTarget.EventTargetImpl.prototype.removeEventListener
|
2020-03-28 13:03:49 -04:00
|
|
|
),
|
2020-01-20 09:30:30 -05:00
|
|
|
};
|