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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
EOF,
Reader,
Writer,
Seeker,
Closer,
SeekMode,
SyncReader,
SyncWriter,
SyncSeeker
} from "./io.ts";
import {
sendSync as sendSyncJson,
sendAsync as sendAsyncJson
} from "./ops/dispatch_json.ts";
import { close } from "./ops/resources.ts";
import { read, readSync, write, writeSync } from "./ops/io.ts";
/** Synchronously open a file and return an instance of the `File` object.
*
* const file = Deno.openSync("/foo/bar.txt", { read: true, write: true });
*
* Requires `allow-read` and `allow-write` permissions depending on mode.
*/
export function openSync(path: string, mode?: OpenOptions): File;
/** Synchronously open a file and return an instance of the `File` object.
*
* const file = Deno.openSync("/foo/bar.txt", "r");
*
* Requires `allow-read` and `allow-write` permissions depending on mode.
*/
export function openSync(path: string, mode?: OpenMode): File;
/**@internal*/
export function openSync(
path: string,
modeOrOptions: OpenOptions | OpenMode = "r"
): File {
let mode = null;
let options = null;
if (typeof modeOrOptions === "string") {
mode = modeOrOptions;
} else {
checkOpenOptions(modeOrOptions);
options = modeOrOptions;
}
const rid = sendSyncJson("op_open", { path, options, mode });
return new File(rid);
}
/** Open a file and resolve to an instance of the `File` object.
*
* const file = await Deno.open("/foo/bar.txt", { read: true, write: true });
*
* Requires `allow-read` and `allow-write` permissions depending on mode.
*/
export async function open(path: string, options?: OpenOptions): Promise<File>;
/** Open a file and resolves to an instance of `Deno.File`.
*
* const file = await Deno.open("/foo/bar.txt, "w+");
*
* Requires `allow-read` and `allow-write` permissions depending on mode.
*/
export async function open(path: string, mode?: OpenMode): Promise<File>;
/**@internal*/
export async function open(
path: string,
modeOrOptions: OpenOptions | OpenMode = "r"
): Promise<File> {
let mode = null;
let options = null;
if (typeof modeOrOptions === "string") {
mode = modeOrOptions;
} else {
checkOpenOptions(modeOrOptions);
options = modeOrOptions;
}
const rid = await sendAsyncJson("op_open", {
path,
options,
mode
});
return new File(rid);
}
/** Creates a file if none exists or truncates an existing file and returns
* an instance of `Deno.File`.
*
* const file = Deno.createSync("/foo/bar.txt");
*
* Requires `allow-read` and `allow-write` permissions.
*/
export function createSync(path: string): File {
return openSync(path, "w+");
}
/** Creates a file if none exists or truncates an existing file and resolves to
* an instance of `Deno.File`.
*
* const file = await Deno.create("/foo/bar.txt");
*
* Requires `allow-read` and `allow-write` permissions.
*/
export function create(path: string): Promise<File> {
return open(path, "w+");
}
/** Synchronously seek a file ID to the given offset under mode given by `whence`.
*
* Returns the number of cursor position.
*
* const file = Deno.openSync("/foo/bar.txt");
* const position = Deno.seekSync(file.rid, 0, 0);
*/
export function seekSync(
rid: number,
offset: number,
whence: SeekMode
): number {
return sendSyncJson("op_seek", { rid, offset, whence });
}
/** Seek a file ID to the given offset under mode given by `whence`.
*
* Resolves with the number of cursor position.
*
* const file = await Deno.open("/foo/bar.txt");
* const position = await Deno.seek(file.rid, 0, 0);
*/
export async function seek(
rid: number,
offset: number,
whence: SeekMode
): Promise<number> {
return await sendAsyncJson("op_seek", { rid, offset, whence });
}
/** The Deno abstraction for reading and writing files. */
export class File
implements
Reader,
SyncReader,
Writer,
SyncWriter,
Seeker,
SyncSeeker,
Closer {
constructor(readonly rid: number) {}
write(p: Uint8Array): Promise<number> {
return write(this.rid, p);
}
writeSync(p: Uint8Array): number {
return writeSync(this.rid, p);
}
read(p: Uint8Array): Promise<number | EOF> {
return read(this.rid, p);
}
readSync(p: Uint8Array): number | EOF {
return readSync(this.rid, p);
}
seek(offset: number, whence: SeekMode): Promise<number> {
return seek(this.rid, offset, whence);
}
seekSync(offset: number, whence: SeekMode): number {
return seekSync(this.rid, offset, whence);
}
close(): void {
close(this.rid);
}
}
/** An instance of `Deno.File` for `stdin`. */
export const stdin = new File(0);
/** An instance of `Deno.File` for `stdout`. */
export const stdout = new File(1);
/** An instance of `Deno.File` for `stderr`. */
export const stderr = new File(2);
export interface OpenOptions {
/** Sets the option for read access. This option, when `true`, means that the
* file should be read-able if opened. */
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. */
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 }`. */
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`
* length if it already exists. The file must be opened with write access
* for truncate to work. */
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. */
create?: boolean;
/** Defaults to `false`. 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. */
createNew?: boolean;
}
/** A set of string literals which specify the open mode of a file.
*
* |Value |Description |
* |------|--------------------------------------------------------------------------------------------------|
* |`"r"` |Read-only. Default. Starts at beginning of file. |
* |`"r+"`|Read-write. Start at beginning of file. |
* |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. |
* |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. |
* |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.|
* |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. |
* |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. |
* |`"x+"`|Read-write. Behaves like `x` and allows reading from file. |
*/
export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+";
/** Check if OpenOptions is set to valid combination of options.
* @returns Tuple representing if openMode is valid and error message if it's not
* @internal
*/
function checkOpenOptions(options: OpenOptions): void {
if (Object.values(options).filter(val => val === true).length === 0) {
throw new Error("OpenOptions requires at least one option to be true");
}
if (options.truncate && !options.write) {
throw new Error("'truncate' option requires 'write' option");
}
const createOrCreateNewWithoutWriteOrAppend =
(options.create || options.createNew) && !(options.write || options.append);
if (createOrCreateNewWithoutWriteOrAppend) {
throw new Error(
"'create' or 'createNew' options require 'write' or 'append' option"
);
}
}
|