2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-11 12:36:20 -04:00
|
|
|
import { build } from "./build.ts";
|
|
|
|
import { exposeForTest } from "./internals.ts";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-17 16:34:30 -04:00
|
|
|
let logDebug = false;
|
2019-02-02 18:27:53 -05:00
|
|
|
let logSource = "JS";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2019-02-02 18:27:53 -05:00
|
|
|
export function setLogDebug(debug: boolean, source?: string): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
logDebug = debug;
|
2019-02-02 18:27:53 -05:00
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
|
|
|
}
|
2018-08-17 16:34:30 -04:00
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export function log(...args: unknown[]): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
if (logDebug) {
|
2020-01-20 09:30:30 -05:00
|
|
|
// if we destructure `console` off `globalThis` too early, we don't bind to
|
2019-09-15 11:04:05 -04:00
|
|
|
// the right console, therefore we don't log anything out.
|
2020-01-20 09:30:30 -05:00
|
|
|
globalThis.console.log(`DEBUG ${logSource} -`, ...args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2020-04-30 10:40:10 -04:00
|
|
|
export class AssertionError extends Error {
|
|
|
|
constructor(msg?: string) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function assert(cond: unknown, msg = "Assertion failed."): asserts cond {
|
2018-07-06 11:20:35 -04:00
|
|
|
if (!cond) {
|
2020-04-30 10:40:10 -04:00
|
|
|
throw new AssertionError(msg);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 21:59:46 -05:00
|
|
|
export type ResolveFunction<T> = (value?: T | PromiseLike<T>) => void;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export type RejectFunction = (reason?: any) => void;
|
|
|
|
|
2018-08-15 12:40:30 -04:00
|
|
|
export interface ResolvableMethods<T> {
|
2019-12-24 21:59:46 -05:00
|
|
|
resolve: ResolveFunction<T>;
|
|
|
|
reject: RejectFunction;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-08-15 12:40:30 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-15 20:57:36 -04:00
|
|
|
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
2018-08-15 12:40:30 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function createResolvable<T>(): Resolvable<T> {
|
2019-12-24 21:59:46 -05:00
|
|
|
let resolve: ResolveFunction<T>;
|
|
|
|
let reject: RejectFunction;
|
|
|
|
const promise = new Promise<T>((res, rej): void => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
}) as Resolvable<T>;
|
|
|
|
promise.resolve = resolve!;
|
|
|
|
promise.reject = reject!;
|
|
|
|
return promise;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-08-19 15:04:27 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-19 15:04:27 -04:00
|
|
|
export function notImplemented(): never {
|
2020-03-20 12:10:02 -04:00
|
|
|
throw new Error("not implemented");
|
2018-08-19 15:04:27 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 20:39:54 -04:00
|
|
|
// @internal
|
|
|
|
export function immutableDefine(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
o: any,
|
|
|
|
p: string | number | symbol,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
value: any
|
|
|
|
): void {
|
|
|
|
Object.defineProperty(o, p, {
|
|
|
|
value,
|
|
|
|
configurable: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
writable: false,
|
2019-04-19 20:39:54 -04:00
|
|
|
});
|
|
|
|
}
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
function pathFromURLWin32(url: URL): string {
|
2020-06-18 06:10:07 -04:00
|
|
|
const hostname = url.hostname;
|
|
|
|
const pathname = decodeURIComponent(url.pathname.replace(/\//g, "\\"));
|
|
|
|
|
|
|
|
if (hostname !== "") {
|
2020-06-11 12:36:20 -04:00
|
|
|
//TODO(actual-size) Node adds a punycode decoding step, we should consider adding this
|
2020-06-18 06:10:07 -04:00
|
|
|
return `\\\\${hostname}${pathname}`;
|
2020-06-11 12:36:20 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 06:10:07 -04:00
|
|
|
const validPath = /^\\(?<driveLetter>[A-Za-z]):\\/;
|
|
|
|
const matches = validPath.exec(pathname);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
if (!matches?.groups?.driveLetter) {
|
|
|
|
throw new TypeError("A URL with the file schema must be absolute.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't want a leading slash on an absolute path in Windows
|
|
|
|
return pathname.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function pathFromURLPosix(url: URL): string {
|
|
|
|
if (url.hostname !== "") {
|
|
|
|
throw new TypeError(`Host must be empty.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeURIComponent(url.pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pathFromURL(pathOrUrl: string | URL): string {
|
|
|
|
if (typeof pathOrUrl == "string") {
|
|
|
|
try {
|
|
|
|
pathOrUrl = new URL(pathOrUrl);
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
if (pathOrUrl instanceof URL) {
|
|
|
|
if (pathOrUrl.protocol != "file:") {
|
|
|
|
throw new TypeError("Must be a path string or file URL.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return build.os == "windows"
|
|
|
|
? pathFromURLWin32(pathOrUrl)
|
|
|
|
: pathFromURLPosix(pathOrUrl);
|
|
|
|
}
|
|
|
|
return pathOrUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
exposeForTest("pathFromURL", pathFromURL);
|