All files / fs / _utils.ts

100.00% Branches 0/0
6.25% Lines 2/32
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
 
 
 
 
 
 
x37
x37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 






































































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

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

/**
 * @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 bytes = new Uint8Array(3);
  crypto.getRandomValues(bytes);
  let result = "";
  for (const byte of bytes) {
    result += byte.toString(16).padStart(2, "0");
  }
  return result;
}