summaryrefslogtreecommitdiff
path: root/cli/js/util.ts
blob: a67dec59584557e9cf456f87fe2516ac1cff7828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 (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);