All files / fs / unstable_read_file.ts

0.00% Branches 0/2
41.38% Lines 12/29
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
 
 
x5
 
x5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x5
x5
 
x276
x828
 
 
 
 
 
 
 
 
 
x276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x271
x271
 
 
 
 
 
 
 
 
x271































I

































I







// Copyright 2018-2025 the Deno authors. MIT license.

import { getNodeFs, isDeno } from "./_utils.ts";
import type { ReadFileOptions } from "./unstable_types.ts";
import { mapError } from "./_map_error.ts";

/**
 * Reads and resolves to the entire contents of a file as an array of bytes.
 * `TextDecoder` can be used to transform the bytes to string if required.
 *
 * Requires `allow-read` permission.
 *
 * @example Usage
 * ```ts no-assert
 * import { readFile } from "@std/fs/unstable-read-file";
 * const decoder = new TextDecoder("utf-8");
 * const data = await readFile("README.md");
 * console.log(decoder.decode(data));
 * ```
 *
 * @tags allow-read
 *
 * @param path The path to the file.
 * @param options Options when reading a file. See {@linkcode ReadFileOptions}.
 * @returns A promise that resolves to a `Uint8Array` of the file contents.
 */
export async function readFile(
  path: string | URL,
  options?: ReadFileOptions,
): Promise<Uint8Array> {
  if (isDeno) {
    return Deno.readFile(path, { ...options });
  } else {
    const { signal } = options ?? {};
    try {
      const buf = await getNodeFs().promises.readFile(path, { signal });
      return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
    } catch (error) {
      throw mapError(error);
    }
  }
}

/**
 * Synchronously reads and returns the entire contents of a file as an array
 * of bytes. `TextDecoder` can be used to transform the bytes to string if
 * required.
 *
 * Requires `allow-read` permission.
 *
 * @example Usage
 * ```ts no-assert
 * import { readFileSync } from "@std/fs/unstable-read-file";
 * const decoder = new TextDecoder("utf-8");
 * const data = readFileSync("README.md");
 * console.log(decoder.decode(data));
 * ```
 *
 * @tags allow-read
 *
 * @param path The path to the file.
 * @returns A `Uint8Array` of bytes representing the file contents.
 */
export function readFileSync(path: string | URL): Uint8Array {
  if (isDeno) {
    return Deno.readFileSync(path);
  } else {
    try {
      const buf = getNodeFs().readFileSync(path);
      return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
    } catch (error) {
      throw mapError(error);
    }
  }
}