summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_open.ts
blob: 55ecbdc1eb29e627fce43c3be3deebbc1c7453aa (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
import { existsSync } from "../../fs/mod.ts";
import { fromFileUrl } from "../path.ts";
import { getOpenOptions } from "./_fs_common.ts";

type openFlags =
  | "a"
  | "ax"
  | "a+"
  | "ax+"
  | "as"
  | "as+"
  | "r"
  | "r+"
  | "rs+"
  | "w"
  | "wx"
  | "w+"
  | "wx+";

type openCallback = (err: Error | null, fd: number) => void;

function convertFlagAndModeToOptions(
  flag?: openFlags,
  mode?: number,
): Deno.OpenOptions | undefined {
  if (!flag && !mode) return undefined;
  if (!flag && mode) return { mode };
  return { ...getOpenOptions(flag), mode };
}

export function open(path: string | URL, callback: openCallback): void;
export function open(
  path: string | URL,
  flags: openFlags,
  callback: openCallback,
): void;
export function open(
  path: string | URL,
  flags: openFlags,
  mode: number,
  callback: openCallback,
): void;
export function open(
  path: string | URL,
  flagsOrCallback: openCallback | openFlags,
  callbackOrMode?: openCallback | number,
  maybeCallback?: openCallback,
) {
  const flags = typeof flagsOrCallback === "string"
    ? flagsOrCallback
    : undefined;
  const callback = typeof flagsOrCallback === "function"
    ? flagsOrCallback
    : typeof callbackOrMode === "function"
    ? callbackOrMode
    : maybeCallback;
  const mode = typeof callbackOrMode === "number" ? callbackOrMode : undefined;
  path = path instanceof URL ? fromFileUrl(path) : path;

  if (!callback) throw new Error("No callback function supplied");

  if (["ax", "ax+", "wx", "wx+"].includes(flags || "") && existsSync(path)) {
    const err = new Error(`EEXIST: file already exists, open '${path}'`);
    (callback as (err: Error) => void)(err);
  } else {
    if (flags === "as" || flags === "as+") {
      let err: Error | null = null, res: number;
      try {
        res = openSync(path, flags, mode);
      } catch (error) {
        err = error;
      }
      if (err) {
        (callback as (err: Error) => void)(err);
      } else {
        callback(null, res!);
      }
      return;
    }
    Deno.open(path, convertFlagAndModeToOptions(flags, mode)).then(
      (file) => callback(null, file.rid),
      (err) => (callback as (err: Error) => void)(err),
    );
  }
}

export function openSync(path: string | URL): number;
export function openSync(path: string | URL, flags?: openFlags): number;
export function openSync(path: string | URL, mode?: number): number;
export function openSync(
  path: string | URL,
  flags?: openFlags,
  mode?: number,
): number;
export function openSync(
  path: string | URL,
  flagsOrMode?: openFlags | number,
  maybeMode?: number,
) {
  const flags = typeof flagsOrMode === "string" ? flagsOrMode : undefined;
  const mode = typeof flagsOrMode === "number" ? flagsOrMode : maybeMode;
  path = path instanceof URL ? fromFileUrl(path) : path;

  if (["ax", "ax+", "wx", "wx+"].includes(flags || "") && existsSync(path)) {
    throw new Error(`EEXIST: file already exists, open '${path}'`);
  }

  return Deno.openSync(path, convertFlagAndModeToOptions(flags, mode)).rid;
}