All files / fs / _utils.ts

0.00% Branches 0/2
17.14% Lines 6/35
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
 
 
 
 
 
 
x37
 
 
x37
 
 
 
 
x37
 
 
 
x74
x74
 
x74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 














I



I




























































// Copyright 2018-2025 the Deno authors. MIT license.
// deno-lint-ignore-file no-explicit-any

/**
 * True if the runtime is Deno, false otherwise.
 */
export const isDeno = navigator.userAgent?.includes("Deno");

/** True if the platform is windows, false otherwise */
export const isWindows = checkWindows();

/**
 * @returns true if the platform is Windows, false otherwise.
 */
function checkWindows(): boolean {
  if (typeof navigator !== "undefined" && (navigator as any).platform) {
    return (navigator as any).platform.startsWith("Win");
  } else if (typeof (globalThis as any).process !== "undefined") {
    return (globalThis as any).platform === "win32";
  }
  return false;
}

/**
 * @returns The Node.js `fs` module.
 */
export function getNodeFs() {
  return (globalThis as any).process.getBuiltinModule("node:fs");
}

/**
 * @returns The Node.js `os` module.
 */
export function getNodeOs() {
  return (globalThis as any).process.getBuiltinModule("node:os");
}

/**
 * @returns The Node.js `path` module.
 */
export function getNodePath() {
  return (globalThis as any).process.getBuiltinModule("node:path");
}

/**
 * @returns The Node.js `process` module.
 */
export function getNodeProcess() {
  return (globalThis as any).process.getBuiltinModule("node:process");
}

/**
 * @returns The Node.js `stream` module.
 */
export function getNodeStream() {
  return (globalThis as any).process.getBuiltinModule("node:stream");
}

/**
 * @returns The Node.js `tty` module.
 */
export function getNodeTty() {
  return (globalThis as any).process.getBuiltinModule("node:tty");
}

/**
 * @returns The Node.js `util` module.
 */
export function getNodeUtil() {
  return (globalThis as any).process.getBuiltinModule("node:util");
}

/**
 * Used for naming temporary files. See {@linkcode makeTempFile} and
 * {@linkcode makeTempFileSync}.
 * @returns A randomized 6-digit hexadecimal string.
 */
export function randomId(): string {
  const n = (Math.random() * 0xfffff * 1_000_000).toString(16);
  return "".concat(n.slice(0, 6));
}