2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-06-14 23:04:30 -04:00
|
|
|
|
|
|
|
// Remove Intl.v8BreakIterator because it is a non-standard API.
|
|
|
|
delete Intl.v8BreakIterator;
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const ops = core.ops;
|
2024-01-31 17:17:49 -05:00
|
|
|
import {
|
|
|
|
op_bootstrap_args,
|
|
|
|
op_bootstrap_is_tty,
|
|
|
|
op_bootstrap_no_color,
|
|
|
|
op_bootstrap_pid,
|
|
|
|
op_main_module,
|
|
|
|
op_ppid,
|
|
|
|
op_set_format_exception_callback,
|
|
|
|
op_snapshot_options,
|
2024-02-05 18:58:06 -05:00
|
|
|
op_worker_close,
|
|
|
|
op_worker_get_type,
|
|
|
|
op_worker_post_message,
|
|
|
|
op_worker_recv_message,
|
|
|
|
op_worker_sync_fetch,
|
2024-01-31 17:17:49 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeFilter,
|
2023-11-25 11:41:21 -05:00
|
|
|
ArrayPrototypeIncludes,
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeMap,
|
2024-01-18 18:30:49 -05:00
|
|
|
ArrayPrototypePop,
|
|
|
|
ArrayPrototypeShift,
|
2023-02-07 14:22:46 -05:00
|
|
|
DateNow,
|
|
|
|
Error,
|
|
|
|
ErrorPrototype,
|
|
|
|
FunctionPrototypeBind,
|
2023-05-01 09:30:02 -04:00
|
|
|
FunctionPrototypeCall,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign,
|
|
|
|
ObjectDefineProperties,
|
2023-05-01 09:30:02 -04:00
|
|
|
ObjectDefineProperty,
|
2023-12-08 08:24:49 -05:00
|
|
|
ObjectKeys,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ObjectSetPrototypeOf,
|
2023-12-08 08:24:49 -05:00
|
|
|
ObjectValues,
|
2023-05-01 09:30:02 -04:00
|
|
|
PromisePrototypeThen,
|
2023-02-07 14:22:46 -05:00
|
|
|
PromiseResolve,
|
2024-01-18 18:30:49 -05:00
|
|
|
SafeSet,
|
|
|
|
StringPrototypeIncludes,
|
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeTrim,
|
2023-02-07 14:22:46 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolIterator,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
2024-01-03 23:12:38 -05:00
|
|
|
const {
|
|
|
|
isNativeError,
|
|
|
|
} = core;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as event from "ext:deno_web/02_event.js";
|
|
|
|
import * as location from "ext:deno_web/12_location.js";
|
|
|
|
import * as version from "ext:runtime/01_version.ts";
|
|
|
|
import * as os from "ext:runtime/30_os.js";
|
|
|
|
import * as timers from "ext:deno_web/02_timers.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
2024-01-23 16:46:59 -05:00
|
|
|
customInspect,
|
2023-04-30 03:24:13 -04:00
|
|
|
getDefaultInspectOptions,
|
2023-04-30 05:11:37 -04:00
|
|
|
getNoColor,
|
2023-02-07 14:22:46 -05:00
|
|
|
inspectArgs,
|
|
|
|
quoteString,
|
2023-11-12 23:52:59 -05:00
|
|
|
setNoColorFn,
|
2023-04-30 05:11:37 -04:00
|
|
|
} from "ext:deno_console/01_console.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as performance from "ext:deno_web/15_performance.js";
|
|
|
|
import * as url from "ext:deno_url/00_url.js";
|
|
|
|
import * as fetch from "ext:deno_fetch/26_fetch.js";
|
|
|
|
import * as messagePort from "ext:deno_web/13_message_port.js";
|
2023-11-01 18:15:08 -04:00
|
|
|
import {
|
|
|
|
denoNs,
|
|
|
|
denoNsUnstable,
|
|
|
|
denoNsUnstableById,
|
2023-12-08 08:24:49 -05:00
|
|
|
unstableIds,
|
2023-11-01 18:15:08 -04:00
|
|
|
} from "ext:runtime/90_deno_ns.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { errors } from "ext:runtime/01_errors.js";
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2024-01-03 23:12:38 -05:00
|
|
|
import { DOMException } from "ext:deno_web/01_dom_exception.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
2023-12-08 08:24:49 -05:00
|
|
|
unstableForWindowOrWorkerGlobalScope,
|
2023-02-07 14:22:46 -05:00
|
|
|
windowOrWorkerGlobalScope,
|
2024-01-06 08:40:26 -05:00
|
|
|
} from "ext:runtime/98_global_scope_shared.js";
|
|
|
|
import {
|
|
|
|
mainRuntimeGlobalProperties,
|
|
|
|
memoizeLazy,
|
|
|
|
} from "ext:runtime/98_global_scope_window.js";
|
|
|
|
import {
|
2023-02-07 14:22:46 -05:00
|
|
|
workerRuntimeGlobalProperties,
|
2024-01-06 08:40:26 -05:00
|
|
|
} from "ext:runtime/98_global_scope_worker.js";
|
2024-02-06 08:57:25 -05:00
|
|
|
import {
|
|
|
|
SymbolAsyncDispose,
|
|
|
|
SymbolDispose,
|
|
|
|
SymbolMetadata,
|
|
|
|
} from "ext:deno_web/00_infra.js";
|
2023-09-14 14:08:59 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2023-11-01 15:26:12 -04:00
|
|
|
if (Symbol.dispose) throw "V8 supports Symbol.dispose now, no need to shim it!";
|
2024-02-06 08:57:25 -05:00
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
if (Symbol.asyncDispose) {
|
|
|
|
throw "V8 supports Symbol.asyncDispose now, no need to shim it!";
|
|
|
|
}
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
if (Symbol.metadata) {
|
|
|
|
throw "V8 supports Symbol.metadata now, no need to shim it!";
|
|
|
|
}
|
2023-11-01 15:26:12 -04:00
|
|
|
ObjectDefineProperties(Symbol, {
|
|
|
|
dispose: {
|
|
|
|
value: SymbolDispose,
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: false,
|
|
|
|
},
|
|
|
|
asyncDispose: {
|
|
|
|
value: SymbolAsyncDispose,
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: false,
|
|
|
|
},
|
2024-02-06 08:57:25 -05:00
|
|
|
metadata: {
|
|
|
|
value: SymbolMetadata,
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: false,
|
|
|
|
},
|
2023-11-01 15:26:12 -04:00
|
|
|
});
|
2023-09-14 14:08:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let windowIsClosing = false;
|
|
|
|
let globalThis_;
|
|
|
|
|
2024-01-26 10:41:16 -05:00
|
|
|
let verboseDeprecatedApiWarning = false;
|
2024-01-18 18:30:49 -05:00
|
|
|
let deprecatedApiWarningDisabled = false;
|
|
|
|
const ALREADY_WARNED_DEPRECATED = new SafeSet();
|
|
|
|
|
2024-01-24 08:20:51 -05:00
|
|
|
function warnOnDeprecatedApi(apiName, stack, ...suggestions) {
|
2024-01-18 18:30:49 -05:00
|
|
|
if (deprecatedApiWarningDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-26 10:41:16 -05:00
|
|
|
if (!verboseDeprecatedApiWarning) {
|
|
|
|
if (ALREADY_WARNED_DEPRECATED.has(apiName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ALREADY_WARNED_DEPRECATED.add(apiName);
|
|
|
|
console.error(
|
|
|
|
`%cwarning: %cUse of deprecated "${apiName}" API. This API will be removed in Deno 2. Run again with DENO_VERBOSE_WARNINGS=1 to get more details.`,
|
|
|
|
"color: yellow;",
|
|
|
|
"font-weight: bold;",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:30:49 -05:00
|
|
|
if (ALREADY_WARNED_DEPRECATED.has(apiName + stack)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't warned yet, let's do some processing of the stack trace
|
|
|
|
// to make it more useful.
|
|
|
|
const stackLines = StringPrototypeSplit(stack, "\n");
|
|
|
|
ArrayPrototypeShift(stackLines);
|
2024-01-23 16:46:59 -05:00
|
|
|
while (stackLines.length > 0) {
|
2024-01-18 18:30:49 -05:00
|
|
|
// Filter out internal frames at the top of the stack - they are not useful
|
|
|
|
// to the user.
|
|
|
|
if (
|
|
|
|
StringPrototypeIncludes(stackLines[0], "(ext:") ||
|
2024-01-23 16:46:59 -05:00
|
|
|
StringPrototypeIncludes(stackLines[0], "(node:") ||
|
|
|
|
StringPrototypeIncludes(stackLines[0], "<anonymous>")
|
2024-01-18 18:30:49 -05:00
|
|
|
) {
|
|
|
|
ArrayPrototypeShift(stackLines);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now remove the last frame if it's coming from "ext:core" - this is most likely
|
|
|
|
// event loop tick or promise handler calling a user function - again not
|
|
|
|
// useful to the user.
|
|
|
|
if (
|
2024-01-23 16:46:59 -05:00
|
|
|
stackLines.length > 0 &&
|
2024-01-18 18:30:49 -05:00
|
|
|
StringPrototypeIncludes(stackLines[stackLines.length - 1], "(ext:core/")
|
|
|
|
) {
|
|
|
|
ArrayPrototypePop(stackLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
let isFromRemoteDependency = false;
|
|
|
|
const firstStackLine = stackLines[0];
|
|
|
|
if (firstStackLine && !StringPrototypeIncludes(firstStackLine, "file:")) {
|
|
|
|
isFromRemoteDependency = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALREADY_WARNED_DEPRECATED.add(apiName + stack);
|
|
|
|
console.error(
|
2024-01-24 10:35:47 -05:00
|
|
|
`%cwarning: %cUse of deprecated "${apiName}" API. This API will be removed in Deno 2.`,
|
2024-01-18 18:30:49 -05:00
|
|
|
"color: yellow;",
|
2024-01-24 10:35:47 -05:00
|
|
|
"font-weight: bold;",
|
2024-01-18 18:30:49 -05:00
|
|
|
);
|
2024-01-24 08:57:54 -05:00
|
|
|
|
2024-02-01 20:32:46 -05:00
|
|
|
console.error();
|
|
|
|
console.error(
|
|
|
|
"See the Deno 1 to 2 Migration Guide for more information at https://docs.deno.com/runtime/manual/advanced/migrate_deprecations",
|
|
|
|
);
|
2024-01-24 10:35:47 -05:00
|
|
|
console.error();
|
|
|
|
if (stackLines.length > 0) {
|
|
|
|
console.error("Stack trace:");
|
|
|
|
for (let i = 0; i < stackLines.length; i++) {
|
|
|
|
console.error(` ${StringPrototypeTrim(stackLines[i])}`);
|
|
|
|
}
|
|
|
|
console.error();
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:20:51 -05:00
|
|
|
for (let i = 0; i < suggestions.length; i++) {
|
|
|
|
const suggestion = suggestions[i];
|
|
|
|
console.error(
|
2024-01-24 10:35:47 -05:00
|
|
|
`%chint: ${suggestion}`,
|
|
|
|
"font-weight: bold;",
|
2024-01-24 08:20:51 -05:00
|
|
|
);
|
|
|
|
}
|
2024-01-24 14:43:31 -05:00
|
|
|
|
2024-01-18 18:30:49 -05:00
|
|
|
if (isFromRemoteDependency) {
|
|
|
|
console.error(
|
2024-01-24 10:35:47 -05:00
|
|
|
`%chint: It appears this API is used by a remote dependency. Try upgrading to the latest version of that dependency.`,
|
|
|
|
"font-weight: bold;",
|
2024-01-18 18:30:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
console.error();
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function windowClose() {
|
|
|
|
if (!windowIsClosing) {
|
|
|
|
windowIsClosing = true;
|
|
|
|
// Push a macrotask to exit after a promise resolve.
|
|
|
|
// This is not perfect, but should be fine for first pass.
|
|
|
|
PromisePrototypeThen(
|
|
|
|
PromiseResolve(),
|
|
|
|
() =>
|
|
|
|
FunctionPrototypeCall(timers.setTimeout, null, () => {
|
|
|
|
// This should be fine, since only Window/MainWorker has .close()
|
|
|
|
os.exit(0);
|
|
|
|
}, 0),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function workerClose() {
|
|
|
|
if (isClosing) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
isClosing = true;
|
2024-02-05 18:58:06 -05:00
|
|
|
op_worker_close();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function postMessage(message, transferOrOptions = {}) {
|
|
|
|
const prefix =
|
|
|
|
"Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope'";
|
2023-05-01 06:47:13 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { transfer } = options;
|
|
|
|
const data = messagePort.serializeJsMessageData(message, transfer);
|
2024-02-05 18:58:06 -05:00
|
|
|
op_worker_post_message(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let isClosing = false;
|
|
|
|
let globalDispatchEvent;
|
|
|
|
|
|
|
|
async function pollForMessages() {
|
|
|
|
if (!globalDispatchEvent) {
|
|
|
|
globalDispatchEvent = FunctionPrototypeBind(
|
|
|
|
globalThis.dispatchEvent,
|
|
|
|
globalThis,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
while (!isClosing) {
|
2023-12-26 20:30:26 -05:00
|
|
|
const data = await op_worker_recv_message();
|
2023-02-07 14:22:46 -05:00
|
|
|
if (data === null) break;
|
|
|
|
const v = messagePort.deserializeJsMessageData(data);
|
|
|
|
const message = v[0];
|
|
|
|
const transferables = v[1];
|
|
|
|
|
|
|
|
const msgEvent = new event.MessageEvent("message", {
|
|
|
|
cancelable: false,
|
|
|
|
data: message,
|
2023-05-01 09:30:02 -04:00
|
|
|
ports: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
|
|
|
(t) =>
|
|
|
|
ObjectPrototypeIsPrototypeOf(messagePort.MessagePortPrototype, t),
|
2023-02-07 14:22:46 -05:00
|
|
|
),
|
|
|
|
});
|
2023-07-31 17:22:07 -04:00
|
|
|
event.setIsTrusted(msgEvent, true);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
globalDispatchEvent(msgEvent);
|
|
|
|
} catch (e) {
|
|
|
|
const errorEvent = new event.ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber + 1 : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber + 1 : undefined,
|
|
|
|
filename: e.fileName,
|
|
|
|
error: e,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
|
2023-07-31 17:22:07 -04:00
|
|
|
event.setIsTrusted(errorEvent, true);
|
2023-02-07 14:22:46 -05:00
|
|
|
globalDispatchEvent(errorEvent);
|
|
|
|
if (!errorEvent.defaultPrevented) {
|
|
|
|
throw e;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let loadedMainWorkerScript = false;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function importScripts(...urls) {
|
2024-02-05 18:58:06 -05:00
|
|
|
if (op_worker_get_type() === "module") {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("Can't import scripts in a module worker.");
|
|
|
|
}
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const baseUrl = location.getLocationHref();
|
|
|
|
const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => {
|
|
|
|
try {
|
|
|
|
return new url.URL(scriptUrl, baseUrl ?? undefined).href;
|
|
|
|
} catch {
|
|
|
|
throw new DOMException(
|
|
|
|
"Failed to parse URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// A classic worker's main script has looser MIME type checks than any
|
|
|
|
// imported scripts, so we use `loadedMainWorkerScript` to distinguish them.
|
|
|
|
// TODO(andreubotella) Refactor worker creation so the main script isn't
|
|
|
|
// loaded with `importScripts()`.
|
2024-02-05 18:58:06 -05:00
|
|
|
const scripts = op_worker_sync_fetch(
|
2023-02-07 14:22:46 -05:00
|
|
|
parsedUrls,
|
|
|
|
!loadedMainWorkerScript,
|
|
|
|
);
|
|
|
|
loadedMainWorkerScript = true;
|
|
|
|
|
|
|
|
for (let i = 0; i < scripts.length; ++i) {
|
|
|
|
const { url, script } = scripts[i];
|
|
|
|
const err = core.evalContext(script, url)[1];
|
|
|
|
if (err !== null) {
|
|
|
|
throw err.thrown;
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2024-01-31 17:17:49 -05:00
|
|
|
const opArgs = memoizeLazy(() => op_bootstrap_args());
|
|
|
|
const opPid = memoizeLazy(() => op_bootstrap_pid());
|
|
|
|
const opPpid = memoizeLazy(() => op_ppid());
|
|
|
|
setNoColorFn(() => op_bootstrap_no_color() || !op_bootstrap_is_tty());
|
2023-11-12 23:52:59 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function formatException(error) {
|
2024-01-03 23:12:38 -05:00
|
|
|
if (
|
|
|
|
isNativeError(error) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return null;
|
|
|
|
} else if (typeof error == "string") {
|
|
|
|
return `Uncaught ${
|
2023-04-30 03:24:13 -04:00
|
|
|
inspectArgs([quoteString(error, getDefaultInspectOptions())], {
|
2023-04-30 05:11:37 -04:00
|
|
|
colors: !getNoColor(),
|
2023-02-07 14:22:46 -05:00
|
|
|
})
|
|
|
|
}`;
|
|
|
|
} else {
|
2023-04-30 05:11:37 -04:00
|
|
|
return `Uncaught ${inspectArgs([error], { colors: !getNoColor() })}`;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 19:55:31 -05:00
|
|
|
core.registerErrorClass("NotFound", errors.NotFound);
|
|
|
|
core.registerErrorClass("PermissionDenied", errors.PermissionDenied);
|
|
|
|
core.registerErrorClass("ConnectionRefused", errors.ConnectionRefused);
|
|
|
|
core.registerErrorClass("ConnectionReset", errors.ConnectionReset);
|
|
|
|
core.registerErrorClass("ConnectionAborted", errors.ConnectionAborted);
|
|
|
|
core.registerErrorClass("NotConnected", errors.NotConnected);
|
|
|
|
core.registerErrorClass("AddrInUse", errors.AddrInUse);
|
|
|
|
core.registerErrorClass("AddrNotAvailable", errors.AddrNotAvailable);
|
|
|
|
core.registerErrorClass("BrokenPipe", errors.BrokenPipe);
|
|
|
|
core.registerErrorClass("AlreadyExists", errors.AlreadyExists);
|
|
|
|
core.registerErrorClass("InvalidData", errors.InvalidData);
|
|
|
|
core.registerErrorClass("TimedOut", errors.TimedOut);
|
|
|
|
core.registerErrorClass("WouldBlock", errors.WouldBlock);
|
|
|
|
core.registerErrorClass("WriteZero", errors.WriteZero);
|
|
|
|
core.registerErrorClass("UnexpectedEof", errors.UnexpectedEof);
|
|
|
|
core.registerErrorClass("Http", errors.Http);
|
|
|
|
core.registerErrorClass("Busy", errors.Busy);
|
|
|
|
core.registerErrorClass("NotSupported", errors.NotSupported);
|
2023-06-28 19:46:16 -04:00
|
|
|
core.registerErrorClass("FilesystemLoop", errors.FilesystemLoop);
|
|
|
|
core.registerErrorClass("IsADirectory", errors.IsADirectory);
|
|
|
|
core.registerErrorClass("NetworkUnreachable", errors.NetworkUnreachable);
|
|
|
|
core.registerErrorClass("NotADirectory", errors.NotADirectory);
|
2023-02-21 19:55:31 -05:00
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
function DOMExceptionOperationError(msg) {
|
|
|
|
return new DOMException(msg, "OperationError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionQuotaExceededError",
|
|
|
|
function DOMExceptionQuotaExceededError(msg) {
|
|
|
|
return new DOMException(msg, "QuotaExceededError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNotSupportedError",
|
|
|
|
function DOMExceptionNotSupportedError(msg) {
|
|
|
|
return new DOMException(msg, "NotSupported");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNetworkError",
|
|
|
|
function DOMExceptionNetworkError(msg) {
|
|
|
|
return new DOMException(msg, "NetworkError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionAbortError",
|
|
|
|
function DOMExceptionAbortError(msg) {
|
|
|
|
return new DOMException(msg, "AbortError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionInvalidCharacterError",
|
|
|
|
function DOMExceptionInvalidCharacterError(msg) {
|
|
|
|
return new DOMException(msg, "InvalidCharacterError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionDataError",
|
|
|
|
function DOMExceptionDataError(msg) {
|
|
|
|
return new DOMException(msg, "DataError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-03-28 04:27:17 -04:00
|
|
|
function runtimeStart(
|
|
|
|
denoVersion,
|
|
|
|
v8Version,
|
|
|
|
tsVersion,
|
|
|
|
target,
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
core.setWasmStreamingCallback(fetch.handleWasmStreaming);
|
|
|
|
core.setReportExceptionCallback(event.reportException);
|
2024-01-31 17:17:49 -05:00
|
|
|
op_set_format_exception_callback(formatException);
|
2023-02-07 14:22:46 -05:00
|
|
|
version.setVersions(
|
2023-03-28 04:27:17 -04:00
|
|
|
denoVersion,
|
|
|
|
v8Version,
|
|
|
|
tsVersion,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2023-03-28 04:27:17 -04:00
|
|
|
core.setBuildInfo(target);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
core.setUnhandledPromiseRejectionHandler(processUnhandledPromiseRejection);
|
2024-01-12 17:10:42 -05:00
|
|
|
core.setHandledPromiseRejectionHandler(processRejectionHandled);
|
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
// Notification that the core received an unhandled promise rejection that is about to
|
|
|
|
// terminate the runtime. If we can handle it, attempt to do so.
|
|
|
|
function processUnhandledPromiseRejection(promise, reason) {
|
|
|
|
const rejectionEvent = new event.PromiseRejectionEvent(
|
|
|
|
"unhandledrejection",
|
|
|
|
{
|
|
|
|
cancelable: true,
|
2023-02-07 14:22:46 -05:00
|
|
|
promise,
|
2023-12-06 19:02:52 -05:00
|
|
|
reason,
|
|
|
|
},
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
// Note that the handler may throw, causing a recursive "error" event
|
|
|
|
globalThis_.dispatchEvent(rejectionEvent);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
// If event was not yet prevented, try handing it off to Node compat layer
|
|
|
|
// (if it was initialized)
|
|
|
|
if (
|
|
|
|
!rejectionEvent.defaultPrevented &&
|
|
|
|
typeof internals.nodeProcessUnhandledRejectionCallback !== "undefined"
|
|
|
|
) {
|
|
|
|
internals.nodeProcessUnhandledRejectionCallback(rejectionEvent);
|
|
|
|
}
|
2023-05-24 09:40:41 -04:00
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
// If event was not prevented (or "unhandledrejection" listeners didn't
|
|
|
|
// throw) we will let Rust side handle it.
|
|
|
|
if (rejectionEvent.defaultPrevented) {
|
|
|
|
return true;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-12-06 19:02:52 -05:00
|
|
|
|
|
|
|
return false;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2024-01-12 17:10:42 -05:00
|
|
|
function processRejectionHandled(promise, reason) {
|
|
|
|
const rejectionHandledEvent = new event.PromiseRejectionEvent(
|
|
|
|
"rejectionhandled",
|
|
|
|
{ promise, reason },
|
|
|
|
);
|
|
|
|
|
|
|
|
// Note that the handler may throw, causing a recursive "error" event
|
|
|
|
globalThis_.dispatchEvent(rejectionHandledEvent);
|
|
|
|
|
|
|
|
if (typeof internals.nodeProcessRejectionHandledCallback !== "undefined") {
|
|
|
|
internals.nodeProcessRejectionHandledCallback(rejectionHandledEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let hasBootstrapped = false;
|
2023-07-19 04:30:04 -04:00
|
|
|
// Delete the `console` object that V8 automaticaly adds onto the global wrapper
|
|
|
|
// object on context creation. We don't want this console object to shadow the
|
|
|
|
// `console` object exposed by the ext/node globalThis proxy.
|
|
|
|
delete globalThis.console;
|
2023-03-07 10:11:54 -05:00
|
|
|
// Set up global properties shared by main and worker runtime.
|
|
|
|
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
|
2023-12-08 08:24:49 -05:00
|
|
|
|
|
|
|
// Set up global properties shared by main and worker runtime that are exposed
|
|
|
|
// by unstable features if those are enabled.
|
|
|
|
function exposeUnstableFeaturesForWindowOrWorkerGlobalScope(options) {
|
|
|
|
const { unstableFlag, unstableFeatures } = options;
|
|
|
|
if (unstableFlag) {
|
|
|
|
const all = ObjectValues(unstableForWindowOrWorkerGlobalScope);
|
|
|
|
for (let i = 0; i <= all.length; i++) {
|
|
|
|
const props = all[i];
|
|
|
|
ObjectDefineProperties(globalThis, { ...props });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const featureIds = ArrayPrototypeMap(
|
|
|
|
ObjectKeys(
|
|
|
|
unstableForWindowOrWorkerGlobalScope,
|
|
|
|
),
|
|
|
|
(k) => k | 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0; i <= featureIds.length; i++) {
|
|
|
|
const featureId = featureIds[i];
|
|
|
|
if (ArrayPrototypeIncludes(unstableFeatures, featureId)) {
|
|
|
|
const props = unstableForWindowOrWorkerGlobalScope[featureId];
|
|
|
|
ObjectDefineProperties(globalThis, { ...props });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 16:39:56 -05:00
|
|
|
// NOTE(bartlomieju): remove all the ops that have already been imported using
|
|
|
|
// "virtual op module" (`ext:core/ops`).
|
|
|
|
const NOT_IMPORTED_OPS = [
|
2024-02-05 20:47:10 -05:00
|
|
|
// Related to `Deno.bench()` API
|
2024-01-31 16:39:56 -05:00
|
|
|
"op_bench_now",
|
|
|
|
"op_dispatch_bench_event",
|
2024-02-05 20:47:10 -05:00
|
|
|
"op_register_bench",
|
|
|
|
|
|
|
|
// Related to `Deno.jupyter` API
|
|
|
|
"op_jupyter_broadcast",
|
|
|
|
|
|
|
|
// Related to `Deno.test()` API
|
|
|
|
"op_test_event_step_result_failed",
|
|
|
|
"op_test_event_step_result_ignored",
|
|
|
|
"op_test_event_step_result_ok",
|
|
|
|
"op_test_event_step_wait",
|
|
|
|
"op_test_op_sanitizer_collect",
|
|
|
|
"op_test_op_sanitizer_finish",
|
|
|
|
"op_test_op_sanitizer_get_async_message",
|
|
|
|
"op_test_op_sanitizer_report",
|
|
|
|
"op_restore_test_permissions",
|
|
|
|
"op_register_test_step",
|
|
|
|
"op_register_test",
|
2024-02-27 22:30:17 -05:00
|
|
|
"op_test_get_origin",
|
2024-02-05 20:47:10 -05:00
|
|
|
"op_pledge_test_permissions",
|
|
|
|
|
|
|
|
// TODO(bartlomieju): used in various integration tests - figure out a way
|
|
|
|
// to not depend on them.
|
|
|
|
"op_set_exit_code",
|
|
|
|
"op_napi_open",
|
|
|
|
"op_npm_process_state",
|
2024-01-31 16:39:56 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
function removeImportedOps() {
|
|
|
|
const allOpNames = ObjectKeys(ops);
|
|
|
|
for (let i = 0; i < allOpNames.length; i++) {
|
|
|
|
const opName = allOpNames[i];
|
|
|
|
if (!ArrayPrototypeIncludes(NOT_IMPORTED_OPS, opName)) {
|
|
|
|
delete ops[opName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 18:37:03 -04:00
|
|
|
// FIXME(bartlomieju): temporarily add whole `Deno.core` to
|
|
|
|
// `Deno[Deno.internal]` namespace. It should be removed and only necessary
|
|
|
|
// methods should be left there.
|
2024-01-18 18:30:49 -05:00
|
|
|
ObjectAssign(internals, { core, warnOnDeprecatedApi });
|
2023-03-07 23:20:54 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
const finalDenoNs = {
|
|
|
|
internal: internalSymbol,
|
|
|
|
[internalSymbol]: internals,
|
2024-01-23 18:27:29 -05:00
|
|
|
resources() {
|
|
|
|
internals.warnOnDeprecatedApi("Deno.resources()", new Error().stack);
|
|
|
|
return core.resources();
|
|
|
|
},
|
2024-01-24 09:59:55 -05:00
|
|
|
close(rid) {
|
|
|
|
internals.warnOnDeprecatedApi(
|
|
|
|
"Deno.close()",
|
|
|
|
new Error().stack,
|
|
|
|
"Use `closer.close()` instead.",
|
|
|
|
);
|
|
|
|
core.close(rid);
|
|
|
|
},
|
2023-03-07 23:20:54 -05:00
|
|
|
...denoNs,
|
2023-11-24 22:46:16 -05:00
|
|
|
// Deno.test and Deno.bench are noops here, but kept for compatibility; so
|
|
|
|
// that they don't cause errors when used outside of `deno test`/`deno bench`
|
|
|
|
// contexts.
|
|
|
|
test: () => {},
|
|
|
|
bench: () => {},
|
2023-03-07 23:20:54 -05:00
|
|
|
};
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-11-15 07:25:55 -05:00
|
|
|
const {
|
|
|
|
denoVersion,
|
|
|
|
tsVersion,
|
|
|
|
v8Version,
|
|
|
|
target,
|
2024-01-31 17:17:49 -05:00
|
|
|
} = op_snapshot_options();
|
2023-11-15 07:25:55 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bootstrapMainRuntime(runtimeOptions) {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
2023-08-15 15:36:36 -04:00
|
|
|
const nodeBootstrap = globalThis.nodeBootstrap;
|
2023-03-28 04:27:17 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
const {
|
2023-11-15 07:25:55 -05:00
|
|
|
0: location_,
|
|
|
|
1: unstableFlag,
|
|
|
|
2: unstableFeatures,
|
|
|
|
3: inspectFlag,
|
|
|
|
5: hasNodeModulesDir,
|
2024-02-23 11:30:29 -05:00
|
|
|
6: argv0,
|
2024-01-18 18:30:49 -05:00
|
|
|
7: shouldDisableDeprecatedApiWarning,
|
2024-01-26 10:41:16 -05:00
|
|
|
8: shouldUseVerboseDeprecatedApiWarning,
|
2024-02-14 23:50:17 -05:00
|
|
|
9: future,
|
2023-04-02 13:41:41 -04:00
|
|
|
} = runtimeOptions;
|
2023-03-28 04:27:17 -04:00
|
|
|
|
2024-01-31 16:39:56 -05:00
|
|
|
removeImportedOps();
|
|
|
|
|
2024-01-18 18:30:49 -05:00
|
|
|
deprecatedApiWarningDisabled = shouldDisableDeprecatedApiWarning;
|
2024-01-26 10:41:16 -05:00
|
|
|
verboseDeprecatedApiWarning = shouldUseVerboseDeprecatedApiWarning;
|
2023-02-07 14:22:46 -05:00
|
|
|
performance.setTimeOrigin(DateNow());
|
|
|
|
globalThis_ = globalThis;
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2023-08-15 15:36:36 -04:00
|
|
|
delete globalThis.nodeBootstrap;
|
2023-02-07 14:22:46 -05:00
|
|
|
hasBootstrapped = true;
|
2022-07-22 18:40:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// If the `--location` flag isn't set, make `globalThis.location` `undefined` and
|
|
|
|
// writable, so that they can mock it themselves if they like. If the flag was
|
|
|
|
// set, define `globalThis.location`, using the provided value.
|
2023-08-15 15:36:36 -04:00
|
|
|
if (location_ == null) {
|
2023-02-07 14:22:46 -05:00
|
|
|
mainRuntimeGlobalProperties.location = {
|
|
|
|
writable: true,
|
|
|
|
};
|
|
|
|
} else {
|
2023-03-28 04:27:17 -04:00
|
|
|
location.setLocationHref(location_);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
|
2023-12-08 08:24:49 -05:00
|
|
|
exposeUnstableFeaturesForWindowOrWorkerGlobalScope({
|
|
|
|
unstableFlag,
|
|
|
|
unstableFeatures,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties);
|
|
|
|
ObjectDefineProperties(globalThis, {
|
2023-10-08 18:12:59 -04:00
|
|
|
// TODO(bartlomieju): in the future we might want to change the
|
|
|
|
// behavior of setting `name` to actually update the process name.
|
|
|
|
// Empty string matches what browsers do.
|
2024-02-06 16:28:32 -05:00
|
|
|
name: core.propWritable(""),
|
|
|
|
close: core.propWritable(windowClose),
|
|
|
|
closed: core.propGetterOnly(() => windowIsClosing),
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
ObjectSetPrototypeOf(globalThis, Window.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-03-28 04:27:17 -04:00
|
|
|
if (inspectFlag) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const consoleFromDeno = globalThis.console;
|
2023-12-28 14:30:07 -05:00
|
|
|
core.wrapConsole(consoleFromDeno, core.v8Console);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.setEventTargetData(globalThis);
|
|
|
|
event.saveGlobalThisReference(globalThis);
|
2022-08-10 14:57:30 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.defineEventHandler(globalThis, "error");
|
|
|
|
event.defineEventHandler(globalThis, "load");
|
|
|
|
event.defineEventHandler(globalThis, "beforeunload");
|
|
|
|
event.defineEventHandler(globalThis, "unload");
|
|
|
|
event.defineEventHandler(globalThis, "unhandledrejection");
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-03-28 04:27:17 -04:00
|
|
|
runtimeStart(
|
|
|
|
denoVersion,
|
|
|
|
v8Version,
|
|
|
|
tsVersion,
|
|
|
|
target,
|
|
|
|
);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
2024-02-06 16:28:32 -05:00
|
|
|
pid: core.propGetterOnly(opPid),
|
|
|
|
ppid: core.propGetterOnly(opPpid),
|
|
|
|
noColor: core.propGetterOnly(() => op_bootstrap_no_color()),
|
|
|
|
args: core.propGetterOnly(opArgs),
|
|
|
|
mainModule: core.propGetterOnly(() => op_main_module()),
|
2024-01-23 16:46:59 -05:00
|
|
|
// TODO(kt3k): Remove this export at v2
|
|
|
|
// See https://github.com/denoland/deno/issues/9294
|
|
|
|
customInspect: {
|
|
|
|
get() {
|
|
|
|
warnOnDeprecatedApi(
|
|
|
|
"Deno.customInspect",
|
|
|
|
new Error().stack,
|
|
|
|
'Use `Symbol.for("Deno.customInspect")` instead.',
|
|
|
|
);
|
|
|
|
return customInspect;
|
|
|
|
},
|
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
2021-03-12 15:23:59 -05:00
|
|
|
|
2023-11-01 18:15:08 -04:00
|
|
|
// TODO(bartlomieju): deprecate --unstable
|
2023-03-28 04:27:17 -04:00
|
|
|
if (unstableFlag) {
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstable);
|
2023-09-26 20:21:06 -04:00
|
|
|
// TODO(bartlomieju): this is not ideal, but because we use `ObjectAssign`
|
|
|
|
// above any properties that are defined elsewhere using `Object.defineProperty`
|
|
|
|
// are lost.
|
2023-09-28 10:51:44 -04:00
|
|
|
let jupyterNs = undefined;
|
2023-09-26 20:21:06 -04:00
|
|
|
ObjectDefineProperty(finalDenoNs, "jupyter", {
|
|
|
|
get() {
|
2023-09-28 10:51:44 -04:00
|
|
|
if (jupyterNs) {
|
|
|
|
return jupyterNs;
|
|
|
|
}
|
2023-09-26 20:21:06 -04:00
|
|
|
throw new Error(
|
|
|
|
"Deno.jupyter is only available in `deno jupyter` subcommand.",
|
|
|
|
);
|
|
|
|
},
|
2023-09-28 10:51:44 -04:00
|
|
|
set(val) {
|
|
|
|
jupyterNs = val;
|
|
|
|
},
|
2023-09-26 20:21:06 -04:00
|
|
|
});
|
2023-11-01 18:15:08 -04:00
|
|
|
} else {
|
|
|
|
for (let i = 0; i <= unstableFeatures.length; i++) {
|
|
|
|
const id = unstableFeatures[i];
|
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstableById[id]);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-11-10 16:03:28 -05:00
|
|
|
|
2023-12-08 08:24:49 -05:00
|
|
|
if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.unsafeProto)) {
|
2023-11-25 11:41:21 -05:00
|
|
|
// Removes the `__proto__` for security reasons.
|
|
|
|
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
|
|
|
|
delete Object.prototype.__proto__;
|
|
|
|
}
|
|
|
|
|
2024-03-07 18:06:28 -05:00
|
|
|
if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.temporal)) {
|
|
|
|
// Removes the `Temporal` API.
|
|
|
|
delete globalThis.Temporal;
|
|
|
|
delete globalThis.Date.prototype.toTemporalInstant;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Setup `Deno` global - we're actually overriding already existing global
|
|
|
|
// `Deno` with `Deno` namespace from "./deno.ts".
|
2024-02-06 16:28:32 -05:00
|
|
|
ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs));
|
2023-01-24 12:54:10 -05:00
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
if (nodeBootstrap) {
|
2024-03-08 11:31:11 -05:00
|
|
|
nodeBootstrap(hasNodeModulesDir, argv0, /* runningOnMainThread */ true);
|
2023-08-15 15:36:36 -04:00
|
|
|
}
|
2024-02-14 23:50:17 -05:00
|
|
|
|
|
|
|
if (future) {
|
|
|
|
delete globalThis.window;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bootstrapWorkerRuntime(
|
|
|
|
runtimeOptions,
|
|
|
|
name,
|
|
|
|
internalName,
|
2024-03-11 18:18:03 -04:00
|
|
|
maybeWorkerMetadata,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
const nodeBootstrap = globalThis.nodeBootstrap;
|
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
const {
|
2023-11-15 07:25:55 -05:00
|
|
|
0: location_,
|
|
|
|
1: unstableFlag,
|
|
|
|
2: unstableFeatures,
|
|
|
|
4: enableTestingFeaturesFlag,
|
|
|
|
5: hasNodeModulesDir,
|
2024-02-23 11:30:29 -05:00
|
|
|
6: argv0,
|
2024-01-18 18:30:49 -05:00
|
|
|
7: shouldDisableDeprecatedApiWarning,
|
2024-01-26 10:41:16 -05:00
|
|
|
8: shouldUseVerboseDeprecatedApiWarning,
|
2023-04-02 13:41:41 -04:00
|
|
|
} = runtimeOptions;
|
2023-03-28 04:27:17 -04:00
|
|
|
|
2024-01-18 18:30:49 -05:00
|
|
|
deprecatedApiWarningDisabled = shouldDisableDeprecatedApiWarning;
|
2024-01-26 10:41:16 -05:00
|
|
|
verboseDeprecatedApiWarning = shouldUseVerboseDeprecatedApiWarning;
|
2023-02-07 14:22:46 -05:00
|
|
|
performance.setTimeOrigin(DateNow());
|
|
|
|
globalThis_ = globalThis;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2024-01-31 16:39:56 -05:00
|
|
|
removeImportedOps();
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2023-08-15 15:36:36 -04:00
|
|
|
delete globalThis.nodeBootstrap;
|
2023-02-07 14:22:46 -05:00
|
|
|
hasBootstrapped = true;
|
2023-03-07 10:11:54 -05:00
|
|
|
|
2023-12-08 08:24:49 -05:00
|
|
|
exposeUnstableFeaturesForWindowOrWorkerGlobalScope({
|
|
|
|
unstableFlag,
|
|
|
|
unstableFeatures,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties);
|
|
|
|
ObjectDefineProperties(globalThis, {
|
2024-02-06 16:28:32 -05:00
|
|
|
name: core.propWritable(name),
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(bartlomieju): should be readonly?
|
2024-02-06 16:28:32 -05:00
|
|
|
close: core.propNonEnumerable(workerClose),
|
|
|
|
postMessage: core.propWritable(postMessage),
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
2023-03-28 04:27:17 -04:00
|
|
|
if (enableTestingFeaturesFlag) {
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperty(
|
|
|
|
globalThis,
|
|
|
|
"importScripts",
|
2024-02-06 16:28:32 -05:00
|
|
|
core.propWritable(importScripts),
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectSetPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const consoleFromDeno = globalThis.console;
|
2023-12-28 14:30:07 -05:00
|
|
|
core.wrapConsole(consoleFromDeno, core.v8Console);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.setEventTargetData(globalThis);
|
|
|
|
event.saveGlobalThisReference(globalThis);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.defineEventHandler(self, "message");
|
|
|
|
event.defineEventHandler(self, "error", undefined, true);
|
|
|
|
event.defineEventHandler(self, "unhandledrejection");
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// `Deno.exit()` is an alias to `self.close()`. Setting and exit
|
|
|
|
// code using an op in worker context is a no-op.
|
|
|
|
os.setExitHandler((_exitCode) => {
|
|
|
|
workerClose();
|
|
|
|
});
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
runtimeStart(
|
2023-03-28 04:27:17 -04:00
|
|
|
denoVersion,
|
|
|
|
v8Version,
|
|
|
|
tsVersion,
|
|
|
|
target,
|
2023-02-07 14:22:46 -05:00
|
|
|
internalName ?? name,
|
|
|
|
);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-03-28 04:27:17 -04:00
|
|
|
location.setLocationHref(location_);
|
2021-07-10 17:32:10 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
globalThis.pollForMessages = pollForMessages;
|
2022-11-21 08:36:26 -05:00
|
|
|
|
2023-11-01 18:15:08 -04:00
|
|
|
// TODO(bartlomieju): deprecate --unstable
|
2023-03-28 04:27:17 -04:00
|
|
|
if (unstableFlag) {
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstable);
|
2023-11-01 18:15:08 -04:00
|
|
|
} else {
|
|
|
|
for (let i = 0; i <= unstableFeatures.length; i++) {
|
|
|
|
const id = unstableFeatures[i];
|
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstableById[id]);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-11-25 11:41:21 -05:00
|
|
|
|
2023-12-08 08:24:49 -05:00
|
|
|
if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.unsafeProto)) {
|
2023-11-25 11:41:21 -05:00
|
|
|
// Removes the `__proto__` for security reasons.
|
|
|
|
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
|
|
|
|
delete Object.prototype.__proto__;
|
|
|
|
}
|
|
|
|
|
2024-03-07 18:06:28 -05:00
|
|
|
if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.temporal)) {
|
|
|
|
// Removes the `Temporal` API.
|
|
|
|
delete globalThis.Temporal;
|
|
|
|
delete globalThis.Date.prototype.toTemporalInstant;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
2024-02-06 16:28:32 -05:00
|
|
|
pid: core.propGetterOnly(opPid),
|
|
|
|
noColor: core.propGetterOnly(() => op_bootstrap_no_color()),
|
|
|
|
args: core.propGetterOnly(opArgs),
|
2024-01-23 16:46:59 -05:00
|
|
|
// TODO(kt3k): Remove this export at v2
|
|
|
|
// See https://github.com/denoland/deno/issues/9294
|
|
|
|
customInspect: {
|
|
|
|
get() {
|
|
|
|
warnOnDeprecatedApi(
|
|
|
|
"Deno.customInspect",
|
|
|
|
new Error().stack,
|
|
|
|
'Use `Symbol.for("Deno.customInspect")` instead.',
|
|
|
|
);
|
|
|
|
return customInspect;
|
|
|
|
},
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
2024-02-06 16:28:32 -05:00
|
|
|
ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs));
|
2023-08-15 15:36:36 -04:00
|
|
|
|
2024-03-11 18:18:03 -04:00
|
|
|
const workerMetadata = maybeWorkerMetadata
|
|
|
|
? messagePort.deserializeJsMessageData(maybeWorkerMetadata)
|
|
|
|
: undefined;
|
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
if (nodeBootstrap) {
|
2024-03-11 18:18:03 -04:00
|
|
|
nodeBootstrap(
|
|
|
|
hasNodeModulesDir,
|
|
|
|
argv0,
|
|
|
|
/* runningOnMainThread */ false,
|
|
|
|
workerMetadata,
|
|
|
|
);
|
2023-08-15 15:36:36 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:11:54 -05:00
|
|
|
globalThis.bootstrap = {
|
|
|
|
mainRuntime: bootstrapMainRuntime,
|
|
|
|
workerRuntime: bootstrapWorkerRuntime,
|
|
|
|
};
|