2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-02-05 03:48:32 +05:30
|
|
|
"use strict";
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
((window) => {
|
2021-07-26 20:52:59 +09:00
|
|
|
const {
|
2022-01-27 16:27:22 +01:00
|
|
|
decodeURIComponent,
|
2022-02-01 18:06:11 +01:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
Promise,
|
2022-02-07 13:54:32 +01:00
|
|
|
SafeArrayIterator,
|
2022-02-01 18:06:11 +01:00
|
|
|
StringPrototypeReplace,
|
|
|
|
TypeError,
|
2021-07-26 20:52:59 +09:00
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 19:49:44 +02:00
|
|
|
const { build } = window.__bootstrap.build;
|
2022-02-01 18:06:11 +01:00
|
|
|
const { URLPrototype } = window.__bootstrap.url;
|
2020-07-19 19:49:44 +02:00
|
|
|
let logDebug = false;
|
|
|
|
let logSource = "JS";
|
|
|
|
|
|
|
|
function setLogDebug(debug, source) {
|
|
|
|
logDebug = debug;
|
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(...args) {
|
|
|
|
if (logDebug) {
|
|
|
|
// if we destructure `console` off `globalThis` too early, we don't bind to
|
|
|
|
// the right console, therefore we don't log anything out.
|
2022-02-07 13:54:32 +01:00
|
|
|
globalThis.console.log(
|
|
|
|
`DEBUG ${logSource} -`,
|
|
|
|
...new SafeArrayIterator(args),
|
|
|
|
);
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createResolvable() {
|
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
promise.resolve = resolve;
|
|
|
|
promise.reject = reject;
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:37:06 +01:00
|
|
|
// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`.
|
2020-07-19 19:49:44 +02:00
|
|
|
function pathFromURLWin32(url) {
|
2021-07-03 16:58:08 +02:00
|
|
|
let p = StringPrototypeReplace(
|
|
|
|
url.pathname,
|
|
|
|
/^\/*([A-Za-z]:)(\/|$)/,
|
|
|
|
"$1/",
|
2020-07-30 23:37:26 +01:00
|
|
|
);
|
2021-07-03 16:58:08 +02:00
|
|
|
p = StringPrototypeReplace(
|
|
|
|
p,
|
|
|
|
/\//g,
|
|
|
|
"\\",
|
|
|
|
);
|
|
|
|
p = StringPrototypeReplace(
|
|
|
|
p,
|
|
|
|
/%(?![0-9A-Fa-f]{2})/g,
|
|
|
|
"%25",
|
|
|
|
);
|
|
|
|
let path = decodeURIComponent(p);
|
2020-07-30 23:37:26 +01:00
|
|
|
if (url.hostname != "") {
|
|
|
|
// Note: The `URL` implementation guarantees that the drive letter and
|
|
|
|
// hostname are mutually exclusive. Otherwise it would not have been valid
|
|
|
|
// to append the hostname and path like this.
|
|
|
|
path = `\\\\${url.hostname}${path}`;
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
2020-07-30 23:37:26 +01:00
|
|
|
return path;
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 14:37:06 +01:00
|
|
|
// Keep in sync with `fromFileUrl()` in `std/path/posix.ts`.
|
2020-07-19 19:49:44 +02:00
|
|
|
function pathFromURLPosix(url) {
|
|
|
|
if (url.hostname !== "") {
|
|
|
|
throw new TypeError(`Host must be empty.`);
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:37:06 +01:00
|
|
|
return decodeURIComponent(
|
2021-07-03 16:58:08 +02:00
|
|
|
StringPrototypeReplace(url.pathname, /%(?![0-9A-Fa-f]{2})/g, "%25"),
|
2020-08-21 14:37:06 +01:00
|
|
|
);
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function pathFromURL(pathOrUrl) {
|
2022-02-01 18:06:11 +01:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) {
|
2020-07-19 19:49:44 +02:00
|
|
|
if (pathOrUrl.protocol != "file:") {
|
|
|
|
throw new TypeError("Must be a file URL.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return build.os == "windows"
|
|
|
|
? pathFromURLWin32(pathOrUrl)
|
|
|
|
: pathFromURLPosix(pathOrUrl);
|
|
|
|
}
|
|
|
|
return pathOrUrl;
|
|
|
|
}
|
|
|
|
|
2021-03-12 21:23:59 +01:00
|
|
|
window.__bootstrap.internals = {
|
|
|
|
...window.__bootstrap.internals ?? {},
|
|
|
|
pathFromURL,
|
|
|
|
};
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
function writable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function nonEnumerable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
2021-06-22 07:17:35 +10:00
|
|
|
enumerable: false,
|
2020-07-19 19:49:44 +02:00
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readOnly(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
enumerable: true,
|
2021-06-22 07:17:35 +10:00
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
2020-07-19 19:49:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getterOnly(getter) {
|
|
|
|
return {
|
|
|
|
get: getter,
|
|
|
|
enumerable: true,
|
2021-06-22 07:17:35 +10:00
|
|
|
configurable: true,
|
2020-07-19 19:49:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.util = {
|
|
|
|
log,
|
|
|
|
setLogDebug,
|
|
|
|
createResolvable,
|
|
|
|
pathFromURL,
|
|
|
|
writable,
|
|
|
|
nonEnumerable,
|
|
|
|
readOnly,
|
|
|
|
getterOnly,
|
|
|
|
};
|
|
|
|
})(this);
|