diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-07-19 19:49:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-19 19:49:44 +0200 |
commit | fa61956f03491101b6ef64423ea2f1f73af26a73 (patch) | |
tree | c3800702071ca78aa4dd71bdd0a59a9bbe460bdd /cli/js/util.ts | |
parent | 53adde866dd399aa2509d14508642fce37afb8f5 (diff) |
Port internal TS code to JS (#6793)
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/js/util.ts')
-rw-r--r-- | cli/js/util.ts | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/cli/js/util.ts b/cli/js/util.ts deleted file mode 100644 index f1aefb601..000000000 --- a/cli/js/util.ts +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { build } from "./build.ts"; -import { exposeForTest } from "./internals.ts"; - -let logDebug = false; -let logSource = "JS"; - -// @internal -export function setLogDebug(debug: boolean, source?: string): void { - logDebug = debug; - if (source) { - logSource = source; - } -} - -export function log(...args: unknown[]): void { - 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} -`, ...args); - } -} - -// @internal -export class AssertionError extends Error { - constructor(msg?: string) { - super(msg); - this.name = "AssertionError"; - } -} - -// @internal -export function assert(cond: unknown, msg = "Assertion failed."): asserts cond { - if (!cond) { - throw new AssertionError(msg); - } -} - -export type ResolveFunction<T> = (value?: T | PromiseLike<T>) => void; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type RejectFunction = (reason?: any) => void; - -export interface ResolvableMethods<T> { - resolve: ResolveFunction<T>; - reject: RejectFunction; -} - -// @internal -export type Resolvable<T> = Promise<T> & ResolvableMethods<T>; - -// @internal -export function createResolvable<T>(): Resolvable<T> { - 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; -} - -// @internal -export function notImplemented(): never { - throw new Error("not implemented"); -} - -// @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, - writable: false, - }); -} - -function pathFromURLWin32(url: URL): string { - const hostname = url.hostname; - const pathname = decodeURIComponent(url.pathname.replace(/\//g, "\\")); - - if (hostname !== "") { - //TODO(actual-size) Node adds a punycode decoding step, we should consider adding this - return `\\\\${hostname}${pathname}`; - } - - const validPath = /^\\(?<driveLetter>[A-Za-z]):\\/; - const matches = validPath.exec(pathname); - - 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 (pathOrUrl instanceof URL) { - if (pathOrUrl.protocol != "file:") { - throw new TypeError("Must be a file URL."); - } - - return build.os == "windows" - ? pathFromURLWin32(pathOrUrl) - : pathFromURLPosix(pathOrUrl); - } - return pathOrUrl; -} - -exposeForTest("pathFromURL", pathFromURL); |