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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 |
 
 
x3
x3
x3
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x3
x3
 
x42
x42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x40
x40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x40 |
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
import { getNodeFs, getNodeUtil, isDeno } from "./_utils.ts";
import { getOpenFsFlag } from "./_get_fs_flag.ts";
import { mapError } from "./_map_error.ts";
import type { FsFile } from "./unstable_types.ts";
import { NodeFsFile } from "./_node_fs_file.ts";
/**
* Options which can be set when using {@linkcode open} and
* {@linkcode openSync}.
*/
export interface OpenOptions {
/**
* Sets the option for read access. This option, when `true`, means that the
* file should be read-able if opened.
*
* @default {true}
*/
read?: boolean;
/**
* Sets the option for write access. This option, when `true`, means that the
* file should be write-able if opened. If the file already exists, any write
* calls on it will overwrite its contents, by default without truncating it.
*
* @default {false}
*/
write?: boolean;
/**
* Sets the option for the append mode. This option, when `true`, means that
* writes will append to a file instead of overwriting previous contents.
*
* Note that setting `{ write: true, append: true }` has the same effect as
* setting only `{ append: true }`.
*
* @default {false}
*/
append?: boolean;
/**
* Sets the option for truncating a previous file. If a file is successfully
* opened with this option set it will truncate the file to `0` size if it
* already exists. The file must be opened with write access for truncate to
* work.
*
* @default {false}
*/
truncate?: boolean;
/**
* Sets the option to allow creating a new file, if one doesn't already exist
* at the specified path. Requires write or append access to be used.
*
* @default {false}
*/
create?: boolean;
/**
* If set to `true`, no file, directory, or symlink is allowed to exist at
* the target location. Requires write or append access to be used. When
* createNew is set to `true`, create and truncate are ignored.
*
* @default {false}
*/
createNew?: boolean;
/**
* Permissions to use if creating the file (defaults to `0o666`, before the
* process's umask).
*
* Ignored on Windows.
*/
mode?: number;
}
/**
* Open a file and resolve to an instance of {@linkcode FsFile}. The file
* does not need to previously exist if using the `create` or `createNew` open
* options. The caller may have the resulting file automatically closed by the
* runtime once it's out of scope by declaring the file variable with the
* `using` keyword.
*
* Requires `allow-read` and/or `allow-write` permissions depending on
* options.
*
* @example Automatic closing a file with `using` **TypeScript Only**
* ```ts ignore
* import { open } from "@std/fs/unstable-open";
* using file = await open("/foo/bar.txt", { read: true, write: true });
* // Do work with file.
* ```
*
* @example Manually closing a file
* ```ts ignore
* import { open } from "@std/fs/unstable-open";
* const file = await open("/foo/bar.txt", { read: true, write: true });
* // Do work with file.
* file.close();
* ```
*
* @tags allow-read, allow-write
*
* @param path The path to the opened file.
* @param options Options to open a file. See {@linkcode OpenOptions}.
* @returns A Promise that resolves to a {@linkcode FsFile} instance.
*/
export async function open(
path: string | URL,
options?: OpenOptions,
): Promise<FsFile> {
if (isDeno) {
return Deno.open(path, options);
} else {
const {
read = true,
write = false,
append = false,
truncate = false,
create = false,
createNew = false,
mode = 0o666,
} = options ?? {};
try {
const flag = getOpenFsFlag({
read,
write,
append,
truncate,
create,
createNew,
});
const { open } = getNodeFs();
const { promisify } = getNodeUtil();
const nodeOpenFd = promisify(open);
const fd = await nodeOpenFd(path, flag, mode);
return new NodeFsFile(fd) as FsFile;
} catch (error) {
throw mapError(error);
}
}
}
/**
* Synchronously open a file and return an instance of {@linkcode FsFile}.
* The file does not need to previously exist if using the `create` or
* `createNew` open options. The caller may have the resulting file
* automatically closed by the runtime once it's out of scope by declaring the
* file variable with the `using` keyword.
*
* Requires `allow-read` and/or `allow-write` permissions depending on
* options.
*
* @example Automatic closing a file with `using` **TypeScript Only**
* ```ts ignore
* import { openSync } from "@std/fs/unstable-open";
* using file = openSync("/foo/bar.txt", { read: true, write: true });
* // Do work with file.
* ```
*
* @example Manually closing an opened file
* ```ts ignore
* import { openSync } from "@std/fs/unstable-open";
* const file = openSync("/foo/bar.txt", { read: true, write: true });
* // Do work with file.
* file.close();
* ```
*
* @tags allow-read, allow-write
*
* @param path The path to the opened file.
* @param options Options to open a file. See {@linkcode OpenOptions}.
* @returns A {@linkcode FsFile} instance.
*/
export function openSync(path: string | URL, options?: OpenOptions): FsFile {
if (isDeno) {
return Deno.openSync(path, options);
} else {
const {
read = true,
write = false,
append = false,
truncate = false,
create = false,
createNew = false,
mode = 0o666,
} = options ?? {};
try {
const flag = getOpenFsFlag({
read,
write,
append,
truncate,
create,
createNew,
});
const fd = getNodeFs().openSync(path, flag, mode);
return new NodeFsFile(fd) as FsFile;
} catch (error) {
throw mapError(error);
}
}
}
|