deno.land / std@0.166.0 / node / _fs / _fs_common.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY,} from "./_fs_constants.ts";import { validateFunction } from "../internal/validators.mjs";import type { ErrnoException } from "../_global.d.ts";import { BinaryEncodings, Encodings, notImplemented, TextEncodings,} from "../_utils.ts";
export type CallbackWithError = (err: ErrnoException | null) => void;
export interface FileOptions { encoding?: Encodings; flag?: string; signal?: AbortSignal;}
export type TextOptionsArgument = | TextEncodings | ({ encoding: TextEncodings } & FileOptions);export type BinaryOptionsArgument = | BinaryEncodings | ({ encoding: BinaryEncodings } & FileOptions);export type FileOptionsArgument = Encodings | FileOptions;
export interface WriteFileOptions extends FileOptions { mode?: number;}
export function isFileOptions( fileOptions: string | WriteFileOptions | undefined,): fileOptions is FileOptions { if (!fileOptions) return false;
return ( (fileOptions as FileOptions).encoding != undefined || (fileOptions as FileOptions).flag != undefined || (fileOptions as FileOptions).signal != undefined || (fileOptions as WriteFileOptions).mode != undefined );}
export function getEncoding( optOrCallback?: | FileOptions | WriteFileOptions // deno-lint-ignore no-explicit-any | ((...args: any[]) => any) | Encodings | null,): Encodings | null { if (!optOrCallback || typeof optOrCallback === "function") { return null; }
const encoding = typeof optOrCallback === "string" ? optOrCallback : optOrCallback.encoding; if (!encoding) return null; return encoding;}
export function checkEncoding(encoding: Encodings | null): Encodings | null { if (!encoding) return null;
encoding = encoding.toLowerCase() as Encodings; if (["utf8", "hex", "base64"].includes(encoding)) return encoding;
if (encoding === "utf-8") { return "utf8"; } if (encoding === "binary") { return "binary"; // before this was buffer, however buffer is not used in Node // node -e "require('fs').readFile('../world.txt', 'buffer', console.log)" }
const notImplementedEncodings = ["utf16le", "latin1", "ascii", "ucs2"];
if (notImplementedEncodings.includes(encoding as string)) { notImplemented(`"${encoding}" encoding`); }
throw new Error(`The value "${encoding}" is invalid for option "encoding"`);}
export function getOpenOptions( flag: string | number | undefined,): Deno.OpenOptions { if (!flag) { return { create: true, append: true }; }
let openOptions: Deno.OpenOptions = {};
if (typeof flag === "string") { switch (flag) { case "a": { // 'a': Open file for appending. The file is created if it does not exist. openOptions = { create: true, append: true }; break; } case "ax": case "xa": { // 'ax', 'xa': Like 'a' but fails if the path exists. openOptions = { createNew: true, write: true, append: true }; break; } case "a+": { // 'a+': Open file for reading and appending. The file is created if it does not exist. openOptions = { read: true, create: true, append: true }; break; } case "ax+": case "xa+": { // 'ax+', 'xa+': Like 'a+' but fails if the path exists. openOptions = { read: true, createNew: true, append: true }; break; } case "r": { // 'r': Open file for reading. An exception occurs if the file does not exist. openOptions = { read: true }; break; } case "r+": { // 'r+': Open file for reading and writing. An exception occurs if the file does not exist. openOptions = { read: true, write: true }; break; } case "w": { // 'w': Open file for writing. The file is created (if it does not exist) or truncated (if it exists). openOptions = { create: true, write: true, truncate: true }; break; } case "wx": case "xw": { // 'wx', 'xw': Like 'w' but fails if the path exists. openOptions = { createNew: true, write: true }; break; } case "w+": { // 'w+': Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). openOptions = { create: true, write: true, truncate: true, read: true }; break; } case "wx+": case "xw+": { // 'wx+', 'xw+': Like 'w+' but fails if the path exists. openOptions = { createNew: true, write: true, read: true }; break; } case "as": case "sa": { // 'as', 'sa': Open file for appending in synchronous mode. The file is created if it does not exist. openOptions = { create: true, append: true }; break; } case "as+": case "sa+": { // 'as+', 'sa+': Open file for reading and appending in synchronous mode. The file is created if it does not exist. openOptions = { create: true, read: true, append: true }; break; } case "rs+": case "sr+": { // 'rs+', 'sr+': Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache. openOptions = { create: true, read: true, write: true }; break; } default: { throw new Error(`Unrecognized file system flag: ${flag}`); } } } else if (typeof flag === "number") { if ((flag & O_APPEND) === O_APPEND) { openOptions.append = true; } if ((flag & O_CREAT) === O_CREAT) { openOptions.create = true; openOptions.write = true; } if ((flag & O_EXCL) === O_EXCL) { openOptions.createNew = true; openOptions.read = true; openOptions.write = true; } if ((flag & O_TRUNC) === O_TRUNC) { openOptions.truncate = true; } if ((flag & O_RDONLY) === O_RDONLY) { openOptions.read = true; } if ((flag & O_WRONLY) === O_WRONLY) { openOptions.write = true; } if ((flag & O_RDWR) === O_RDWR) { openOptions.read = true; openOptions.write = true; } }
return openOptions;}
export { isUint32 as isFd } from "../internal/validators.mjs";
export function maybeCallback(cb: unknown) { validateFunction(cb, "cb");
return cb as CallbackWithError;}
// Ensure that callbacks run in the global context. Only use this function// for callbacks that are passed to the binding layer, callbacks that are// invoked from JS already run in the proper scope.export function makeCallback( this: unknown, cb?: (err: Error | null, result?: unknown) => void,) { validateFunction(cb, "cb");
return (...args: unknown[]) => Reflect.apply(cb!, this, args);}
std

Version Info

Tagged at
a year ago