2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const internals = globalThis.__bootstrap.internals;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
decodeURIComponent,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
Promise,
|
|
|
|
SafeArrayIterator,
|
2023-02-28 18:14:16 -05:00
|
|
|
SafeRegExp,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeReplace,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import { build } from "internal:runtime/01_build.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import { URLPrototype } from "internal:deno_url/00_url.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
let logDebug = false;
|
|
|
|
let logSource = "JS";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function setLogDebug(debug, source) {
|
|
|
|
logDebug = debug;
|
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
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 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.
|
|
|
|
globalThis.console.log(
|
|
|
|
`DEBUG ${logSource} -`,
|
|
|
|
...new SafeArrayIterator(args),
|
|
|
|
);
|
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 createResolvable() {
|
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
promise.resolve = resolve;
|
|
|
|
promise.reject = reject;
|
|
|
|
return promise;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`.
|
|
|
|
function pathFromURLWin32(url) {
|
|
|
|
let p = StringPrototypeReplace(
|
|
|
|
url.pathname,
|
2023-02-28 18:14:16 -05:00
|
|
|
new SafeRegExp(/^\/*([A-Za-z]:)(\/|$)/),
|
2023-02-07 14:22:46 -05:00
|
|
|
"$1/",
|
|
|
|
);
|
|
|
|
p = StringPrototypeReplace(
|
|
|
|
p,
|
|
|
|
/\//g,
|
|
|
|
"\\",
|
|
|
|
);
|
|
|
|
p = StringPrototypeReplace(
|
|
|
|
p,
|
2023-02-28 18:14:16 -05:00
|
|
|
new SafeRegExp(/%(?![0-9A-Fa-f]{2})/g),
|
2023-02-07 14:22:46 -05:00
|
|
|
"%25",
|
|
|
|
);
|
|
|
|
let path = decodeURIComponent(p);
|
|
|
|
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 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return path;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Keep in sync with `fromFileUrl()` in `std/path/posix.ts`.
|
|
|
|
function pathFromURLPosix(url) {
|
|
|
|
if (url.hostname !== "") {
|
|
|
|
throw new TypeError(`Host must be empty.`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return decodeURIComponent(
|
2023-02-28 18:14:16 -05:00
|
|
|
StringPrototypeReplace(
|
|
|
|
url.pathname,
|
|
|
|
new SafeRegExp(/%(?![0-9A-Fa-f]{2})/g),
|
|
|
|
"%25",
|
|
|
|
),
|
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 pathFromURL(pathOrUrl) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) {
|
|
|
|
if (pathOrUrl.protocol != "file:") {
|
|
|
|
throw new TypeError("Must be a file URL.");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return build.os == "windows"
|
|
|
|
? pathFromURLWin32(pathOrUrl)
|
|
|
|
: pathFromURLPosix(pathOrUrl);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return pathOrUrl;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(bartlomieju): remove
|
|
|
|
internals.pathFromURL = pathFromURL;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function writable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function nonEnumerable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function readOnly(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getterOnly(getter) {
|
|
|
|
return {
|
|
|
|
get: getter,
|
|
|
|
set() {},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
createResolvable,
|
|
|
|
getterOnly,
|
|
|
|
log,
|
|
|
|
nonEnumerable,
|
|
|
|
pathFromURL,
|
|
|
|
readOnly,
|
|
|
|
setLogDebug,
|
|
|
|
writable,
|
|
|
|
};
|