bare-fs 4.2.2 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +1 -1
- package/README.md +9 -78
- package/binding.c +916 -1508
- package/index.d.ts +447 -177
- package/index.js +1381 -1761
- package/lib/errors.d.ts +11 -0
- package/lib/errors.js +61 -0
- package/package.json +9 -4
- package/prebuilds/android-arm/bare-fs.bare +0 -0
- package/prebuilds/android-arm64/bare-fs.bare +0 -0
- package/prebuilds/android-ia32/bare-fs.bare +0 -0
- package/prebuilds/android-x64/bare-fs.bare +0 -0
- package/prebuilds/darwin-arm64/bare-fs.bare +0 -0
- package/prebuilds/darwin-x64/bare-fs.bare +0 -0
- package/prebuilds/ios-arm64/bare-fs.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-fs.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-fs.bare +0 -0
- package/prebuilds/linux-arm64/bare-fs.bare +0 -0
- package/prebuilds/linux-x64/bare-fs.bare +0 -0
- package/prebuilds/win32-arm64/bare-fs.bare +0 -0
- package/prebuilds/win32-x64/bare-fs.bare +0 -0
- package/promises.d.ts +67 -69
- package/promises.js +22 -1
package/index.js
CHANGED
|
@@ -1,393 +1,278 @@
|
|
|
1
1
|
const EventEmitter = require('bare-events')
|
|
2
2
|
const path = require('bare-path')
|
|
3
|
+
const { fileURLToPath } = require('bare-url')
|
|
3
4
|
const { Readable, Writable } = require('bare-stream')
|
|
4
5
|
const binding = require('./binding')
|
|
5
6
|
const constants = require('./lib/constants')
|
|
7
|
+
const FileError = require('./lib/errors')
|
|
6
8
|
|
|
7
9
|
const isWindows = Bare.platform === 'win32'
|
|
8
10
|
|
|
9
11
|
exports.constants = constants
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
case 'r':
|
|
15
|
-
return constants.O_RDONLY
|
|
16
|
-
case 'rs': // Fall through.
|
|
17
|
-
case 'sr':
|
|
18
|
-
return constants.O_RDONLY | constants.O_SYNC
|
|
19
|
-
case 'r+':
|
|
20
|
-
return constants.O_RDWR
|
|
21
|
-
case 'rs+': // Fall through.
|
|
22
|
-
case 'sr+':
|
|
23
|
-
return constants.O_RDWR | constants.O_SYNC
|
|
13
|
+
class FileRequest {
|
|
14
|
+
constructor() {
|
|
15
|
+
this._reset()
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
case 'wx': // Fall through.
|
|
28
|
-
case 'xw':
|
|
29
|
-
return (
|
|
30
|
-
constants.O_TRUNC |
|
|
31
|
-
constants.O_CREAT |
|
|
32
|
-
constants.O_WRONLY |
|
|
33
|
-
constants.O_EXCL
|
|
34
|
-
)
|
|
17
|
+
this._handle = binding.requestInit(this, this._onresult)
|
|
18
|
+
}
|
|
35
19
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
case 'xw+':
|
|
40
|
-
return (
|
|
41
|
-
constants.O_TRUNC |
|
|
42
|
-
constants.O_CREAT |
|
|
43
|
-
constants.O_RDWR |
|
|
44
|
-
constants.O_EXCL
|
|
45
|
-
)
|
|
20
|
+
get handle() {
|
|
21
|
+
return this._handle
|
|
22
|
+
}
|
|
46
23
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
case 'xa':
|
|
51
|
-
return (
|
|
52
|
-
constants.O_APPEND |
|
|
53
|
-
constants.O_CREAT |
|
|
54
|
-
constants.O_WRONLY |
|
|
55
|
-
constants.O_EXCL
|
|
56
|
-
)
|
|
57
|
-
case 'as': // Fall through.
|
|
58
|
-
case 'sa':
|
|
59
|
-
return (
|
|
60
|
-
constants.O_APPEND |
|
|
61
|
-
constants.O_CREAT |
|
|
62
|
-
constants.O_WRONLY |
|
|
63
|
-
constants.O_SYNC
|
|
64
|
-
)
|
|
24
|
+
retain(value) {
|
|
25
|
+
this._retain = value // Tie the lifetime of `value` to the lifetime of `this`
|
|
26
|
+
}
|
|
65
27
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
constants.O_APPEND |
|
|
72
|
-
constants.O_CREAT |
|
|
73
|
-
constants.O_RDWR |
|
|
74
|
-
constants.O_EXCL
|
|
75
|
-
)
|
|
76
|
-
case 'as+': // Fall through.
|
|
77
|
-
case 'sa+':
|
|
78
|
-
return (
|
|
79
|
-
constants.O_APPEND |
|
|
80
|
-
constants.O_CREAT |
|
|
81
|
-
constants.O_RDWR |
|
|
82
|
-
constants.O_SYNC
|
|
83
|
-
)
|
|
28
|
+
reset() {
|
|
29
|
+
this._reset()
|
|
30
|
+
|
|
31
|
+
binding.requestReset(this._handle)
|
|
84
32
|
}
|
|
85
33
|
|
|
86
|
-
|
|
87
|
-
|
|
34
|
+
destroy() {
|
|
35
|
+
this._reset()
|
|
88
36
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
96
|
-
return mode
|
|
97
|
-
}
|
|
37
|
+
binding.requestDestroy(this._handle)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
then(resolve, reject) {
|
|
41
|
+
this._promise.then(resolve, reject)
|
|
42
|
+
}
|
|
98
43
|
|
|
99
|
-
|
|
44
|
+
_reset() {
|
|
45
|
+
const { promise, resolve, reject } = Promise.withResolvers()
|
|
100
46
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
47
|
+
this._promise = promise
|
|
48
|
+
this._resolve = resolve
|
|
49
|
+
this._reject = reject
|
|
50
|
+
this._retain = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_onresult(err, status) {
|
|
54
|
+
if (err) this._reject(err)
|
|
55
|
+
else this._resolve(status)
|
|
56
|
+
}
|
|
105
57
|
}
|
|
106
58
|
|
|
107
|
-
function
|
|
108
|
-
|
|
59
|
+
function ok(result, cb) {
|
|
60
|
+
if (typeof result === 'function') {
|
|
61
|
+
cb = result
|
|
62
|
+
result = undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (cb) cb(null, result)
|
|
66
|
+
else return result
|
|
109
67
|
}
|
|
110
68
|
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
req.callback = null
|
|
115
|
-
free.push(req)
|
|
116
|
-
cb(err, result)
|
|
69
|
+
function fail(err, cb) {
|
|
70
|
+
if (cb) cb(err)
|
|
71
|
+
else throw err
|
|
117
72
|
}
|
|
118
73
|
|
|
119
|
-
function
|
|
120
|
-
if (typeof
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
'Path must be a string. Received type ' +
|
|
124
|
-
typeof filepath +
|
|
125
|
-
' (' +
|
|
126
|
-
filepath +
|
|
127
|
-
')'
|
|
128
|
-
)
|
|
74
|
+
function done(err, result, cb) {
|
|
75
|
+
if (typeof result === 'function') {
|
|
76
|
+
cb = result
|
|
77
|
+
result = undefined
|
|
129
78
|
}
|
|
130
79
|
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
typeof cb +
|
|
144
|
-
' (' +
|
|
145
|
-
cb +
|
|
146
|
-
')'
|
|
147
|
-
)
|
|
148
|
-
}
|
|
80
|
+
if (err) fail(err, cb)
|
|
81
|
+
else return ok(result, cb)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function open(filepath, flags = 'r', mode = 0o666, cb) {
|
|
85
|
+
if (typeof flags === 'function') {
|
|
86
|
+
cb = flags
|
|
87
|
+
flags = 'r'
|
|
88
|
+
mode = 0o666
|
|
89
|
+
} else if (typeof mode === 'function') {
|
|
90
|
+
cb = mode
|
|
91
|
+
mode = 0o666
|
|
149
92
|
}
|
|
150
93
|
|
|
151
|
-
if (typeof flags === 'string') flags =
|
|
152
|
-
if (typeof mode === 'string') mode =
|
|
94
|
+
if (typeof flags === 'string') flags = toFlags(flags)
|
|
95
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
153
96
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
97
|
+
filepath = toNamespacedPath(filepath)
|
|
98
|
+
|
|
99
|
+
const req = new FileRequest()
|
|
100
|
+
|
|
101
|
+
let fd
|
|
102
|
+
let err = null
|
|
103
|
+
try {
|
|
104
|
+
binding.open(req.handle, filepath, flags, mode)
|
|
105
|
+
|
|
106
|
+
fd = await req
|
|
107
|
+
} catch (e) {
|
|
108
|
+
err = new FileError(e.message, {
|
|
109
|
+
operation: 'open',
|
|
110
|
+
code: e.code,
|
|
111
|
+
path: filepath
|
|
112
|
+
})
|
|
113
|
+
} finally {
|
|
114
|
+
req.destroy()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return done(err, fd, cb)
|
|
157
118
|
}
|
|
158
119
|
|
|
159
120
|
function openSync(filepath, flags = 'r', mode = 0o666) {
|
|
160
|
-
if (typeof
|
|
161
|
-
|
|
162
|
-
'ERR_INVALID_ARG_TYPE',
|
|
163
|
-
'Path must be a string. Received type ' +
|
|
164
|
-
typeof filepath +
|
|
165
|
-
' (' +
|
|
166
|
-
filepath +
|
|
167
|
-
')'
|
|
168
|
-
)
|
|
169
|
-
}
|
|
121
|
+
if (typeof flags === 'string') flags = toFlags(flags)
|
|
122
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
170
123
|
|
|
171
|
-
|
|
172
|
-
if (typeof mode === 'string') mode = modeToNumber(mode)
|
|
124
|
+
filepath = toNamespacedPath(filepath)
|
|
173
125
|
|
|
174
|
-
|
|
175
|
-
}
|
|
126
|
+
const req = new FileRequest()
|
|
176
127
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)
|
|
128
|
+
try {
|
|
129
|
+
return binding.openSync(req.handle, filepath, flags, mode)
|
|
130
|
+
} catch (e) {
|
|
131
|
+
throw new FileError(e.message, {
|
|
132
|
+
operation: 'open',
|
|
133
|
+
code: e.code,
|
|
134
|
+
path: filepath
|
|
135
|
+
})
|
|
136
|
+
} finally {
|
|
137
|
+
req.destroy()
|
|
187
138
|
}
|
|
139
|
+
}
|
|
188
140
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
'ERR_OUT_OF_RANGE',
|
|
192
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
193
|
-
fd
|
|
194
|
-
)
|
|
195
|
-
}
|
|
141
|
+
async function close(fd, cb) {
|
|
142
|
+
const req = new FileRequest()
|
|
196
143
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
)
|
|
144
|
+
let err = null
|
|
145
|
+
try {
|
|
146
|
+
binding.close(req.handle, fd)
|
|
147
|
+
|
|
148
|
+
await req
|
|
149
|
+
} catch (e) {
|
|
150
|
+
err = new FileError(e.message, { operation: 'close', code: e.code, fd })
|
|
151
|
+
} finally {
|
|
152
|
+
req.destroy()
|
|
206
153
|
}
|
|
207
154
|
|
|
208
|
-
|
|
209
|
-
req.callback = cb
|
|
210
|
-
binding.close(req.handle, fd)
|
|
155
|
+
return done(err, cb)
|
|
211
156
|
}
|
|
212
157
|
|
|
213
158
|
function closeSync(fd) {
|
|
214
|
-
|
|
215
|
-
throw typeError(
|
|
216
|
-
'ERR_INVALID_ARG_TYPE',
|
|
217
|
-
'File descriptor must be a number. Received type ' +
|
|
218
|
-
typeof fd +
|
|
219
|
-
' (' +
|
|
220
|
-
fd +
|
|
221
|
-
')'
|
|
222
|
-
)
|
|
223
|
-
}
|
|
159
|
+
const req = new FileRequest()
|
|
224
160
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
)
|
|
161
|
+
try {
|
|
162
|
+
binding.closeSync(req.handle, fd)
|
|
163
|
+
} catch (e) {
|
|
164
|
+
throw new FileError(e.message, { operation: 'close', code: e.code, fd })
|
|
165
|
+
} finally {
|
|
166
|
+
req.destroy()
|
|
231
167
|
}
|
|
232
|
-
|
|
233
|
-
return binding.closeSync(fd)
|
|
234
168
|
}
|
|
235
169
|
|
|
236
|
-
function access(filepath, mode, cb) {
|
|
237
|
-
if (typeof
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
'Path must be a string. Received type ' +
|
|
241
|
-
typeof filepath +
|
|
242
|
-
' (' +
|
|
243
|
-
filepath +
|
|
244
|
-
')'
|
|
245
|
-
)
|
|
170
|
+
async function access(filepath, mode = constants.F_OK, cb) {
|
|
171
|
+
if (typeof mode === 'function') {
|
|
172
|
+
cb = mode
|
|
173
|
+
mode = constants.F_OK
|
|
246
174
|
}
|
|
247
175
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
176
|
+
filepath = toNamespacedPath(filepath)
|
|
177
|
+
|
|
178
|
+
const req = new FileRequest()
|
|
179
|
+
|
|
180
|
+
let err = null
|
|
181
|
+
try {
|
|
182
|
+
binding.access(req.handle, filepath, mode)
|
|
183
|
+
|
|
184
|
+
await req
|
|
185
|
+
} catch (e) {
|
|
186
|
+
err = new FileError(e.message, {
|
|
187
|
+
operation: 'access',
|
|
188
|
+
code: e.code,
|
|
189
|
+
path: filepath
|
|
190
|
+
})
|
|
191
|
+
} finally {
|
|
192
|
+
req.destroy()
|
|
262
193
|
}
|
|
263
194
|
|
|
264
|
-
|
|
265
|
-
req.callback = cb
|
|
266
|
-
binding.access(req.handle, toNamespacedPath(filepath), mode)
|
|
195
|
+
return done(err, cb)
|
|
267
196
|
}
|
|
268
197
|
|
|
269
198
|
function accessSync(filepath, mode = constants.F_OK) {
|
|
270
|
-
|
|
271
|
-
throw typeError(
|
|
272
|
-
'ERR_INVALID_ARG_TYPE',
|
|
273
|
-
'Path must be a string. Received type ' +
|
|
274
|
-
typeof filepath +
|
|
275
|
-
' (' +
|
|
276
|
-
filepath +
|
|
277
|
-
')'
|
|
278
|
-
)
|
|
279
|
-
}
|
|
199
|
+
const req = new FileRequest()
|
|
280
200
|
|
|
281
|
-
|
|
282
|
-
}
|
|
201
|
+
filepath = toNamespacedPath(filepath)
|
|
283
202
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
)
|
|
203
|
+
try {
|
|
204
|
+
binding.accessSync(req.handle, filepath, mode)
|
|
205
|
+
} catch (e) {
|
|
206
|
+
throw new FileError(e.message, {
|
|
207
|
+
operation: 'access',
|
|
208
|
+
code: e.code,
|
|
209
|
+
path: filepath
|
|
210
|
+
})
|
|
211
|
+
} finally {
|
|
212
|
+
req.destroy()
|
|
294
213
|
}
|
|
214
|
+
}
|
|
295
215
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
cb +
|
|
303
|
-
')'
|
|
304
|
-
)
|
|
216
|
+
async function exists(filepath, cb) {
|
|
217
|
+
let ok = true
|
|
218
|
+
try {
|
|
219
|
+
await access(filepath)
|
|
220
|
+
} catch {
|
|
221
|
+
ok = false
|
|
305
222
|
}
|
|
306
223
|
|
|
307
|
-
return
|
|
224
|
+
return done(null, ok, cb)
|
|
308
225
|
}
|
|
309
226
|
|
|
310
227
|
function existsSync(filepath) {
|
|
311
|
-
if (typeof filepath !== 'string') {
|
|
312
|
-
throw typeError(
|
|
313
|
-
'ERR_INVALID_ARG_TYPE',
|
|
314
|
-
'Path must be a string. Received type ' +
|
|
315
|
-
typeof filepath +
|
|
316
|
-
' (' +
|
|
317
|
-
filepath +
|
|
318
|
-
')'
|
|
319
|
-
)
|
|
320
|
-
}
|
|
321
|
-
|
|
322
228
|
try {
|
|
323
|
-
accessSync(
|
|
324
|
-
return true
|
|
229
|
+
accessSync(filepath)
|
|
325
230
|
} catch {
|
|
326
231
|
return false
|
|
327
232
|
}
|
|
233
|
+
|
|
234
|
+
return true
|
|
328
235
|
}
|
|
329
236
|
|
|
330
|
-
function read(
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
237
|
+
async function read(
|
|
238
|
+
fd,
|
|
239
|
+
buffer,
|
|
240
|
+
offset = 0,
|
|
241
|
+
len = buffer.byteLength - offset,
|
|
242
|
+
pos = -1,
|
|
243
|
+
cb
|
|
244
|
+
) {
|
|
245
|
+
if (typeof offset === 'function') {
|
|
246
|
+
cb = offset
|
|
247
|
+
offset = 0
|
|
248
|
+
len = buffer.byteLength
|
|
249
|
+
pos = -1
|
|
250
|
+
} else if (typeof len === 'function') {
|
|
251
|
+
cb = len
|
|
252
|
+
len = buffer.byteLength - offset
|
|
253
|
+
pos = -1
|
|
254
|
+
} else if (typeof pos === 'function') {
|
|
255
|
+
cb = pos
|
|
256
|
+
pos = -1
|
|
340
257
|
}
|
|
341
258
|
|
|
342
|
-
if (
|
|
343
|
-
throw typeError(
|
|
344
|
-
'ERR_OUT_OF_RANGE',
|
|
345
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
346
|
-
fd
|
|
347
|
-
)
|
|
348
|
-
}
|
|
259
|
+
if (typeof pos !== 'number') pos = -1
|
|
349
260
|
|
|
350
|
-
|
|
351
|
-
throw typeError(
|
|
352
|
-
'ERR_INVALID_ARG_TYPE',
|
|
353
|
-
'Buffer must be a buffer. Received type ' +
|
|
354
|
-
typeof buffer +
|
|
355
|
-
' (' +
|
|
356
|
-
buffer +
|
|
357
|
-
')'
|
|
358
|
-
)
|
|
359
|
-
}
|
|
261
|
+
const req = new FileRequest()
|
|
360
262
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
len = buffer.byteLength
|
|
366
|
-
pos = -1
|
|
367
|
-
} else if (typeof len === 'function') {
|
|
368
|
-
cb = len
|
|
369
|
-
len = buffer.byteLength - offset
|
|
370
|
-
pos = -1
|
|
371
|
-
} else if (typeof pos === 'function') {
|
|
372
|
-
cb = pos
|
|
373
|
-
pos = -1
|
|
374
|
-
} else {
|
|
375
|
-
throw typeError(
|
|
376
|
-
'ERR_INVALID_ARG_TYPE',
|
|
377
|
-
'Callback must be a function. Received type ' +
|
|
378
|
-
typeof cb +
|
|
379
|
-
' (' +
|
|
380
|
-
cb +
|
|
381
|
-
')'
|
|
382
|
-
)
|
|
383
|
-
}
|
|
384
|
-
}
|
|
263
|
+
let bytes
|
|
264
|
+
let err = null
|
|
265
|
+
try {
|
|
266
|
+
binding.read(req.handle, fd, buffer, offset, len, pos)
|
|
385
267
|
|
|
386
|
-
|
|
268
|
+
bytes = await req
|
|
269
|
+
} catch (e) {
|
|
270
|
+
err = new FileError(e.message, { operation: 'read', code: e.code, fd })
|
|
271
|
+
} finally {
|
|
272
|
+
req.destroy()
|
|
273
|
+
}
|
|
387
274
|
|
|
388
|
-
|
|
389
|
-
req.callback = cb
|
|
390
|
-
binding.read(req.handle, fd, buffer, offset, len, pos)
|
|
275
|
+
return done(err, bytes, cb)
|
|
391
276
|
}
|
|
392
277
|
|
|
393
278
|
function readSync(
|
|
@@ -397,134 +282,69 @@ function readSync(
|
|
|
397
282
|
len = buffer.byteLength - offset,
|
|
398
283
|
pos = -1
|
|
399
284
|
) {
|
|
400
|
-
|
|
401
|
-
throw typeError(
|
|
402
|
-
'ERR_INVALID_ARG_TYPE',
|
|
403
|
-
'File descriptor must be a number. Received type ' +
|
|
404
|
-
typeof fd +
|
|
405
|
-
' (' +
|
|
406
|
-
fd +
|
|
407
|
-
')'
|
|
408
|
-
)
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (fd < 0 || fd > 0x7fffffff) {
|
|
412
|
-
throw typeError(
|
|
413
|
-
'ERR_OUT_OF_RANGE',
|
|
414
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
415
|
-
fd
|
|
416
|
-
)
|
|
417
|
-
}
|
|
285
|
+
const req = new FileRequest()
|
|
418
286
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
buffer +
|
|
426
|
-
')'
|
|
427
|
-
)
|
|
287
|
+
try {
|
|
288
|
+
return binding.readSync(req.handle, fd, buffer, offset, len, pos)
|
|
289
|
+
} catch (e) {
|
|
290
|
+
throw new FileError(e.message, { operation: 'read', code: e.code, fd })
|
|
291
|
+
} finally {
|
|
292
|
+
req.destroy()
|
|
428
293
|
}
|
|
429
|
-
|
|
430
|
-
return binding.readSync(fd, buffer, offset, len, pos)
|
|
431
294
|
}
|
|
432
295
|
|
|
433
|
-
function readv(fd, buffers, pos, cb) {
|
|
434
|
-
if (typeof fd !== 'number') {
|
|
435
|
-
throw typeError(
|
|
436
|
-
'ERR_INVALID_ARG_TYPE',
|
|
437
|
-
'File descriptor must be a number. Received type ' +
|
|
438
|
-
typeof fd +
|
|
439
|
-
' (' +
|
|
440
|
-
fd +
|
|
441
|
-
')'
|
|
442
|
-
)
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
if (fd < 0 || fd > 0x7fffffff) {
|
|
446
|
-
throw typeError(
|
|
447
|
-
'ERR_OUT_OF_RANGE',
|
|
448
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
449
|
-
fd
|
|
450
|
-
)
|
|
451
|
-
}
|
|
452
|
-
|
|
296
|
+
async function readv(fd, buffers, pos = -1, cb) {
|
|
453
297
|
if (typeof pos === 'function') {
|
|
454
298
|
cb = pos
|
|
455
299
|
pos = -1
|
|
456
|
-
} else if (typeof cb !== 'function') {
|
|
457
|
-
throw typeError(
|
|
458
|
-
'ERR_INVALID_ARG_TYPE',
|
|
459
|
-
'Callback must be a function. Received type ' +
|
|
460
|
-
typeof cb +
|
|
461
|
-
' (' +
|
|
462
|
-
cb +
|
|
463
|
-
')'
|
|
464
|
-
)
|
|
465
300
|
}
|
|
466
301
|
|
|
467
302
|
if (typeof pos !== 'number') pos = -1
|
|
468
303
|
|
|
469
|
-
const req =
|
|
470
|
-
req.callback = cb
|
|
471
|
-
binding.readv(req.handle, fd, buffers, pos)
|
|
472
|
-
}
|
|
304
|
+
const req = new FileRequest()
|
|
473
305
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
'File descriptor must be a number. Received type ' +
|
|
479
|
-
typeof fd +
|
|
480
|
-
' (' +
|
|
481
|
-
fd +
|
|
482
|
-
')'
|
|
483
|
-
)
|
|
484
|
-
}
|
|
306
|
+
let bytes
|
|
307
|
+
let err = null
|
|
308
|
+
try {
|
|
309
|
+
binding.readv(req.handle, fd, buffers, pos)
|
|
485
310
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
)
|
|
311
|
+
bytes = await req
|
|
312
|
+
} catch (e) {
|
|
313
|
+
err = new FileError(e.message, { operation: 'readv', code: e.code, fd })
|
|
314
|
+
} finally {
|
|
315
|
+
req.destroy()
|
|
492
316
|
}
|
|
493
317
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
)
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
318
|
+
return done(err, bytes, cb)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function readvSync(fd, buffers, pos = -1) {
|
|
322
|
+
if (typeof pos !== 'number') pos = -1
|
|
323
|
+
|
|
324
|
+
const req = new FileRequest()
|
|
325
|
+
|
|
326
|
+
try {
|
|
327
|
+
return binding.readvSync(req.handle, fd, buffers, pos)
|
|
328
|
+
} catch (e) {
|
|
329
|
+
throw new FileError(e.message, { operation: 'readv', code: e.code, fd })
|
|
330
|
+
} finally {
|
|
331
|
+
req.destroy()
|
|
503
332
|
}
|
|
333
|
+
}
|
|
504
334
|
|
|
335
|
+
async function write(fd, data, offset = 0, len, pos = -1, cb) {
|
|
505
336
|
if (typeof data === 'string') {
|
|
506
337
|
let encoding = len
|
|
507
338
|
cb = pos
|
|
508
339
|
pos = offset
|
|
509
340
|
|
|
510
|
-
if (typeof
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
encoding = 'utf8'
|
|
518
|
-
} else {
|
|
519
|
-
throw typeError(
|
|
520
|
-
'ERR_INVALID_ARG_TYPE',
|
|
521
|
-
'Callback must be a function. Received type ' +
|
|
522
|
-
typeof cb +
|
|
523
|
-
' (' +
|
|
524
|
-
cb +
|
|
525
|
-
')'
|
|
526
|
-
)
|
|
527
|
-
}
|
|
341
|
+
if (typeof pos === 'function') {
|
|
342
|
+
cb = pos
|
|
343
|
+
pos = -1
|
|
344
|
+
encoding = 'utf8'
|
|
345
|
+
} else if (typeof encoding === 'function') {
|
|
346
|
+
cb = encoding
|
|
347
|
+
encoding = 'utf8'
|
|
528
348
|
}
|
|
529
349
|
|
|
530
350
|
if (typeof pos === 'string') {
|
|
@@ -535,536 +355,388 @@ function write(fd, data, offset, len, pos, cb) {
|
|
|
535
355
|
data = Buffer.from(data, encoding)
|
|
536
356
|
offset = 0
|
|
537
357
|
len = data.byteLength
|
|
538
|
-
} else if (typeof
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
pos = -1
|
|
551
|
-
} else {
|
|
552
|
-
throw typeError(
|
|
553
|
-
'ERR_INVALID_ARG_TYPE',
|
|
554
|
-
'Callback must be a function. Received type ' +
|
|
555
|
-
typeof cb +
|
|
556
|
-
' (' +
|
|
557
|
-
cb +
|
|
558
|
-
')'
|
|
559
|
-
)
|
|
560
|
-
}
|
|
358
|
+
} else if (typeof offset === 'function') {
|
|
359
|
+
cb = offset
|
|
360
|
+
offset = 0
|
|
361
|
+
len = data.byteLength
|
|
362
|
+
pos = -1
|
|
363
|
+
} else if (typeof len === 'function') {
|
|
364
|
+
cb = len
|
|
365
|
+
len = data.byteLength - offset
|
|
366
|
+
pos = -1
|
|
367
|
+
} else if (typeof pos === 'function') {
|
|
368
|
+
cb = pos
|
|
369
|
+
pos = -1
|
|
561
370
|
}
|
|
562
371
|
|
|
372
|
+
if (typeof len !== 'number') len = data.byteLength - offset
|
|
563
373
|
if (typeof pos !== 'number') pos = -1
|
|
564
374
|
|
|
565
|
-
const req =
|
|
566
|
-
req.callback = cb
|
|
567
|
-
binding.write(req.handle, fd, data, offset, len, pos)
|
|
568
|
-
}
|
|
375
|
+
const req = new FileRequest()
|
|
569
376
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
'File descriptor must be a number. Received type ' +
|
|
575
|
-
typeof fd +
|
|
576
|
-
' (' +
|
|
577
|
-
fd +
|
|
578
|
-
')'
|
|
579
|
-
)
|
|
580
|
-
}
|
|
377
|
+
let bytes
|
|
378
|
+
let err = null
|
|
379
|
+
try {
|
|
380
|
+
binding.write(req.handle, fd, data, offset, len, pos)
|
|
581
381
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
)
|
|
382
|
+
bytes = await req
|
|
383
|
+
} catch (e) {
|
|
384
|
+
err = new FileError(e.message, { operation: 'write', code: e.code, fd })
|
|
385
|
+
} finally {
|
|
386
|
+
req.destroy()
|
|
588
387
|
}
|
|
589
388
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
!Buffer.isBuffer(data) &&
|
|
593
|
-
!ArrayBuffer.isView(data)
|
|
594
|
-
) {
|
|
595
|
-
throw typeError(
|
|
596
|
-
'ERR_INVALID_ARG_TYPE',
|
|
597
|
-
'Data must be a string or buffer. Received type ' + typeof data
|
|
598
|
-
)
|
|
599
|
-
}
|
|
389
|
+
return done(err, bytes, cb)
|
|
390
|
+
}
|
|
600
391
|
|
|
392
|
+
function writeSync(fd, data, offset = 0, len, pos = -1) {
|
|
601
393
|
if (typeof data === 'string') data = Buffer.from(data)
|
|
602
|
-
|
|
603
394
|
if (typeof len !== 'number') len = data.byteLength - offset
|
|
604
|
-
|
|
605
395
|
if (typeof pos !== 'number') pos = -1
|
|
606
396
|
|
|
607
|
-
|
|
608
|
-
}
|
|
397
|
+
const req = new FileRequest()
|
|
609
398
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
' (' +
|
|
617
|
-
fd +
|
|
618
|
-
')'
|
|
619
|
-
)
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
if (fd < 0 || fd > 0x7fffffff) {
|
|
623
|
-
throw typeError(
|
|
624
|
-
'ERR_OUT_OF_RANGE',
|
|
625
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
626
|
-
fd
|
|
627
|
-
)
|
|
399
|
+
try {
|
|
400
|
+
return binding.writeSync(req.handle, fd, data, offset, len, pos)
|
|
401
|
+
} catch (e) {
|
|
402
|
+
throw new FileError(e.message, { operation: 'write', code: e.code, fd })
|
|
403
|
+
} finally {
|
|
404
|
+
req.destroy()
|
|
628
405
|
}
|
|
406
|
+
}
|
|
629
407
|
|
|
408
|
+
async function writev(fd, buffers, pos = -1, cb) {
|
|
630
409
|
if (typeof pos === 'function') {
|
|
631
410
|
cb = pos
|
|
632
411
|
pos = -1
|
|
633
|
-
} else if (typeof cb !== 'function') {
|
|
634
|
-
throw typeError(
|
|
635
|
-
'ERR_INVALID_ARG_TYPE',
|
|
636
|
-
'Callback must be a function. Received type ' +
|
|
637
|
-
typeof cb +
|
|
638
|
-
' (' +
|
|
639
|
-
cb +
|
|
640
|
-
')'
|
|
641
|
-
)
|
|
642
412
|
}
|
|
643
413
|
|
|
644
414
|
if (typeof pos !== 'number') pos = -1
|
|
645
415
|
|
|
646
|
-
const req =
|
|
647
|
-
|
|
648
|
-
|
|
416
|
+
const req = new FileRequest()
|
|
417
|
+
|
|
418
|
+
let bytes
|
|
419
|
+
let err = null
|
|
420
|
+
try {
|
|
421
|
+
binding.writev(req.handle, fd, buffers, pos)
|
|
422
|
+
|
|
423
|
+
bytes = await req
|
|
424
|
+
} catch (e) {
|
|
425
|
+
err = new FileError(e.message, { operation: 'writev', code: e.code, fd })
|
|
426
|
+
} finally {
|
|
427
|
+
req.destroy()
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return done(err, bytes, cb)
|
|
649
431
|
}
|
|
650
432
|
|
|
651
|
-
function
|
|
652
|
-
if (typeof
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
433
|
+
function writevSync(fd, buffers, pos = -1) {
|
|
434
|
+
if (typeof pos !== 'number') pos = -1
|
|
435
|
+
|
|
436
|
+
const req = new FileRequest()
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
return binding.writevSync(req.handle, fd, buffers, pos)
|
|
440
|
+
} catch (e) {
|
|
441
|
+
throw new FileError(e.message, { operation: 'writev', code: e.code, fd })
|
|
442
|
+
} finally {
|
|
443
|
+
req.destroy()
|
|
661
444
|
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
async function stat(filepath, cb) {
|
|
448
|
+
filepath = toNamespacedPath(filepath)
|
|
662
449
|
|
|
663
|
-
|
|
664
|
-
throw typeError(
|
|
665
|
-
'ERR_INVALID_ARG_TYPE',
|
|
666
|
-
'Callback must be a function. Received type ' +
|
|
667
|
-
typeof cb +
|
|
668
|
-
' (' +
|
|
669
|
-
cb +
|
|
670
|
-
')'
|
|
671
|
-
)
|
|
672
|
-
}
|
|
450
|
+
const req = new FileRequest()
|
|
673
451
|
|
|
674
|
-
|
|
452
|
+
let st
|
|
453
|
+
let err = null
|
|
454
|
+
try {
|
|
455
|
+
binding.stat(req.handle, filepath)
|
|
675
456
|
|
|
676
|
-
|
|
457
|
+
await req
|
|
677
458
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
459
|
+
st = new Stats(...binding.requestResultStat(req.handle))
|
|
460
|
+
} catch (e) {
|
|
461
|
+
err = new FileError(e.message, {
|
|
462
|
+
operation: 'stat',
|
|
463
|
+
code: e.code,
|
|
464
|
+
path: filepath
|
|
465
|
+
})
|
|
466
|
+
} finally {
|
|
467
|
+
req.destroy()
|
|
681
468
|
}
|
|
682
469
|
|
|
683
|
-
|
|
470
|
+
return done(err, st, cb)
|
|
684
471
|
}
|
|
685
472
|
|
|
686
473
|
function statSync(filepath) {
|
|
687
|
-
|
|
688
|
-
throw typeError(
|
|
689
|
-
'ERR_INVALID_ARG_TYPE',
|
|
690
|
-
'Path must be a string. Received type ' +
|
|
691
|
-
typeof filepath +
|
|
692
|
-
' (' +
|
|
693
|
-
filepath +
|
|
694
|
-
')'
|
|
695
|
-
)
|
|
696
|
-
}
|
|
474
|
+
filepath = toNamespacedPath(filepath)
|
|
697
475
|
|
|
698
|
-
|
|
699
|
-
}
|
|
476
|
+
const req = new FileRequest()
|
|
700
477
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
)
|
|
478
|
+
try {
|
|
479
|
+
binding.statSync(req.handle, filepath)
|
|
480
|
+
|
|
481
|
+
return new Stats(...binding.requestResultStat(req.handle))
|
|
482
|
+
} catch (e) {
|
|
483
|
+
throw new FileError(e.message, {
|
|
484
|
+
operation: 'stat',
|
|
485
|
+
code: e.code,
|
|
486
|
+
path: filepath
|
|
487
|
+
})
|
|
488
|
+
} finally {
|
|
489
|
+
req.destroy()
|
|
711
490
|
}
|
|
491
|
+
}
|
|
712
492
|
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
typeof cb +
|
|
718
|
-
' (' +
|
|
719
|
-
cb +
|
|
720
|
-
')'
|
|
721
|
-
)
|
|
722
|
-
}
|
|
493
|
+
async function lstat(filepath, cb) {
|
|
494
|
+
filepath = toNamespacedPath(filepath)
|
|
495
|
+
|
|
496
|
+
const req = new FileRequest()
|
|
723
497
|
|
|
724
|
-
|
|
498
|
+
let st
|
|
499
|
+
let err = null
|
|
500
|
+
try {
|
|
501
|
+
binding.lstat(req.handle, filepath)
|
|
725
502
|
|
|
726
|
-
|
|
503
|
+
await req
|
|
727
504
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
505
|
+
st = new Stats(...binding.requestResultStat(req.handle))
|
|
506
|
+
} catch (e) {
|
|
507
|
+
err = new FileError(e.message, {
|
|
508
|
+
operation: 'lstat',
|
|
509
|
+
code: e.code,
|
|
510
|
+
path: filepath
|
|
511
|
+
})
|
|
512
|
+
} finally {
|
|
513
|
+
req.destroy()
|
|
731
514
|
}
|
|
732
515
|
|
|
733
|
-
|
|
516
|
+
return done(err, st, cb)
|
|
734
517
|
}
|
|
735
518
|
|
|
736
519
|
function lstatSync(filepath) {
|
|
737
|
-
|
|
738
|
-
throw typeError(
|
|
739
|
-
'ERR_INVALID_ARG_TYPE',
|
|
740
|
-
'Path must be a string. Received type ' +
|
|
741
|
-
typeof filepath +
|
|
742
|
-
' (' +
|
|
743
|
-
filepath +
|
|
744
|
-
')'
|
|
745
|
-
)
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
return new Stats(...binding.lstatSync(toNamespacedPath(filepath)))
|
|
749
|
-
}
|
|
520
|
+
filepath = toNamespacedPath(filepath)
|
|
750
521
|
|
|
751
|
-
|
|
752
|
-
if (typeof fd !== 'number') {
|
|
753
|
-
throw typeError(
|
|
754
|
-
'ERR_INVALID_ARG_TYPE',
|
|
755
|
-
'File descriptor must be a number. Received type ' +
|
|
756
|
-
typeof fd +
|
|
757
|
-
' (' +
|
|
758
|
-
fd +
|
|
759
|
-
')'
|
|
760
|
-
)
|
|
761
|
-
}
|
|
522
|
+
const req = new FileRequest()
|
|
762
523
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
524
|
+
try {
|
|
525
|
+
binding.lstatSync(req.handle, filepath)
|
|
526
|
+
|
|
527
|
+
return new Stats(...binding.requestResultStat(req.handle))
|
|
528
|
+
} catch (e) {
|
|
529
|
+
throw new FileError(e.message, {
|
|
530
|
+
operation: 'lstat',
|
|
531
|
+
code: e.code,
|
|
532
|
+
path: filepath
|
|
533
|
+
})
|
|
534
|
+
} finally {
|
|
535
|
+
req.destroy()
|
|
769
536
|
}
|
|
537
|
+
}
|
|
770
538
|
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
'ERR_INVALID_ARG_TYPE',
|
|
774
|
-
'Callback must be a function. Received type ' +
|
|
775
|
-
typeof cb +
|
|
776
|
-
' (' +
|
|
777
|
-
cb +
|
|
778
|
-
')'
|
|
779
|
-
)
|
|
780
|
-
}
|
|
539
|
+
async function fstat(fd, cb) {
|
|
540
|
+
const req = new FileRequest()
|
|
781
541
|
|
|
782
|
-
|
|
542
|
+
let st
|
|
543
|
+
let err = null
|
|
544
|
+
try {
|
|
545
|
+
binding.fstat(req.handle, fd)
|
|
783
546
|
|
|
784
|
-
|
|
547
|
+
await req
|
|
785
548
|
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
549
|
+
st = new Stats(...binding.requestResultStat(req.handle))
|
|
550
|
+
} catch (e) {
|
|
551
|
+
err = new FileError(e.message, { operation: 'fstat', code: e.code, fd })
|
|
552
|
+
} finally {
|
|
553
|
+
req.destroy()
|
|
789
554
|
}
|
|
790
555
|
|
|
791
|
-
|
|
556
|
+
return done(err, st, cb)
|
|
792
557
|
}
|
|
793
558
|
|
|
794
559
|
function fstatSync(fd) {
|
|
795
|
-
|
|
796
|
-
throw typeError(
|
|
797
|
-
'ERR_INVALID_ARG_TYPE',
|
|
798
|
-
'File descriptor must be a number. Received type ' +
|
|
799
|
-
typeof fd +
|
|
800
|
-
' (' +
|
|
801
|
-
fd +
|
|
802
|
-
')'
|
|
803
|
-
)
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
if (fd < 0 || fd > 0x7fffffff) {
|
|
807
|
-
throw typeError(
|
|
808
|
-
'ERR_OUT_OF_RANGE',
|
|
809
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
810
|
-
fd
|
|
811
|
-
)
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
return new Stats(...binding.fstatSync(fd))
|
|
815
|
-
}
|
|
560
|
+
const req = new FileRequest()
|
|
816
561
|
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
throw typeError(
|
|
820
|
-
'ERR_INVALID_ARG_TYPE',
|
|
821
|
-
'File descriptor must be a number. Received type ' +
|
|
822
|
-
typeof fd +
|
|
823
|
-
' (' +
|
|
824
|
-
fd +
|
|
825
|
-
')'
|
|
826
|
-
)
|
|
827
|
-
}
|
|
562
|
+
try {
|
|
563
|
+
binding.fstatSync(req.handle, fd)
|
|
828
564
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
)
|
|
565
|
+
return new Stats(...binding.requestResultStat(req.handle))
|
|
566
|
+
} catch (e) {
|
|
567
|
+
throw new FileError(e.message, { operation: 'fstat', code: e.code, fd })
|
|
568
|
+
} finally {
|
|
569
|
+
req.destroy()
|
|
835
570
|
}
|
|
571
|
+
}
|
|
836
572
|
|
|
573
|
+
async function ftruncate(fd, len = 0, cb) {
|
|
837
574
|
if (typeof len === 'function') {
|
|
838
575
|
cb = len
|
|
839
576
|
len = 0
|
|
840
|
-
} else if (typeof cb !== 'function') {
|
|
841
|
-
throw typeError(
|
|
842
|
-
'ERR_INVALID_ARG_TYPE',
|
|
843
|
-
'Callback must be a function. Received type ' +
|
|
844
|
-
typeof cb +
|
|
845
|
-
' (' +
|
|
846
|
-
cb +
|
|
847
|
-
')'
|
|
848
|
-
)
|
|
849
577
|
}
|
|
850
578
|
|
|
851
579
|
if (typeof len !== 'number') len = 0
|
|
852
580
|
|
|
853
|
-
const req =
|
|
854
|
-
req.callback = cb
|
|
855
|
-
binding.ftruncate(req.handle, fd, len)
|
|
856
|
-
}
|
|
581
|
+
const req = new FileRequest()
|
|
857
582
|
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
'ERR_INVALID_ARG_TYPE',
|
|
862
|
-
'File descriptor must be a number. Received type ' +
|
|
863
|
-
typeof fd +
|
|
864
|
-
' (' +
|
|
865
|
-
fd +
|
|
866
|
-
')'
|
|
867
|
-
)
|
|
868
|
-
}
|
|
583
|
+
let err = null
|
|
584
|
+
try {
|
|
585
|
+
binding.ftruncate(req.handle, fd, len)
|
|
869
586
|
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
)
|
|
587
|
+
await req
|
|
588
|
+
} catch (e) {
|
|
589
|
+
err = new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
|
|
590
|
+
} finally {
|
|
591
|
+
req.destroy()
|
|
876
592
|
}
|
|
877
593
|
|
|
594
|
+
return done(err, cb)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function ftruncateSync(fd, len = 0) {
|
|
878
598
|
if (typeof len !== 'number') len = 0
|
|
879
599
|
|
|
880
|
-
|
|
881
|
-
}
|
|
600
|
+
const req = new FileRequest()
|
|
882
601
|
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
' (' +
|
|
890
|
-
filepath +
|
|
891
|
-
')'
|
|
892
|
-
)
|
|
602
|
+
try {
|
|
603
|
+
binding.ftruncateSync(req.handle, fd, len)
|
|
604
|
+
} catch (e) {
|
|
605
|
+
throw new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
|
|
606
|
+
} finally {
|
|
607
|
+
req.destroy()
|
|
893
608
|
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
async function chmod(filepath, mode, cb) {
|
|
612
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
613
|
+
|
|
614
|
+
filepath = toNamespacedPath(filepath)
|
|
615
|
+
|
|
616
|
+
const req = new FileRequest()
|
|
894
617
|
|
|
895
|
-
|
|
618
|
+
let err = null
|
|
619
|
+
try {
|
|
620
|
+
binding.chmod(req.handle, filepath, mode)
|
|
621
|
+
|
|
622
|
+
await req
|
|
623
|
+
} catch (e) {
|
|
624
|
+
err = new FileError(e.message, {
|
|
625
|
+
operation: 'chmod',
|
|
626
|
+
code: e.code,
|
|
627
|
+
path: filepath
|
|
628
|
+
})
|
|
629
|
+
} finally {
|
|
630
|
+
req.destroy()
|
|
631
|
+
}
|
|
896
632
|
|
|
897
|
-
|
|
898
|
-
req.callback = cb
|
|
899
|
-
binding.chmod(req.handle, toNamespacedPath(filepath), mode)
|
|
633
|
+
return done(err, cb)
|
|
900
634
|
}
|
|
901
635
|
|
|
902
636
|
function chmodSync(filepath, mode) {
|
|
903
|
-
if (typeof
|
|
904
|
-
throw typeError(
|
|
905
|
-
'ERR_INVALID_ARG_TYPE',
|
|
906
|
-
'Path must be a string. Received type ' +
|
|
907
|
-
typeof filepath +
|
|
908
|
-
' (' +
|
|
909
|
-
filepath +
|
|
910
|
-
')'
|
|
911
|
-
)
|
|
912
|
-
}
|
|
637
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
913
638
|
|
|
914
|
-
|
|
639
|
+
filepath = toNamespacedPath(filepath)
|
|
915
640
|
|
|
916
|
-
|
|
917
|
-
}
|
|
641
|
+
const req = new FileRequest()
|
|
918
642
|
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
)
|
|
643
|
+
try {
|
|
644
|
+
binding.chmodSync(req.handle, filepath, mode)
|
|
645
|
+
} catch (e) {
|
|
646
|
+
throw new FileError(e.message, {
|
|
647
|
+
operation: 'chmod',
|
|
648
|
+
code: e.code,
|
|
649
|
+
path: filepath
|
|
650
|
+
})
|
|
651
|
+
} finally {
|
|
652
|
+
req.destroy()
|
|
929
653
|
}
|
|
654
|
+
}
|
|
930
655
|
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
'ERR_OUT_OF_RANGE',
|
|
934
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
935
|
-
fd
|
|
936
|
-
)
|
|
937
|
-
}
|
|
656
|
+
async function fchmod(fd, mode, cb) {
|
|
657
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
938
658
|
|
|
939
|
-
|
|
659
|
+
const req = new FileRequest()
|
|
940
660
|
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
}
|
|
661
|
+
let err = null
|
|
662
|
+
try {
|
|
663
|
+
binding.fchmod(req.handle, fd, mode)
|
|
945
664
|
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
typeof fd +
|
|
952
|
-
' (' +
|
|
953
|
-
fd +
|
|
954
|
-
')'
|
|
955
|
-
)
|
|
665
|
+
await req
|
|
666
|
+
} catch (e) {
|
|
667
|
+
err = new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
|
|
668
|
+
} finally {
|
|
669
|
+
req.destroy()
|
|
956
670
|
}
|
|
957
671
|
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
'ERR_OUT_OF_RANGE',
|
|
961
|
-
'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
|
|
962
|
-
fd
|
|
963
|
-
)
|
|
964
|
-
}
|
|
672
|
+
return done(err, cb)
|
|
673
|
+
}
|
|
965
674
|
|
|
966
|
-
|
|
675
|
+
function fchmodSync(fd, mode) {
|
|
676
|
+
if (typeof mode === 'string') mode = toMode(mode)
|
|
967
677
|
|
|
968
|
-
|
|
969
|
-
}
|
|
678
|
+
const req = new FileRequest()
|
|
970
679
|
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
' (' +
|
|
978
|
-
filepath +
|
|
979
|
-
')'
|
|
980
|
-
)
|
|
680
|
+
try {
|
|
681
|
+
binding.fchmodSync(req.handle, fd, mode)
|
|
682
|
+
} catch (e) {
|
|
683
|
+
throw new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
|
|
684
|
+
} finally {
|
|
685
|
+
req.destroy()
|
|
981
686
|
}
|
|
687
|
+
}
|
|
982
688
|
|
|
689
|
+
async function utimes(filepath, atime, mtime, cb) {
|
|
983
690
|
if (typeof atime !== 'number') atime = atime.getTime() / 1000
|
|
984
691
|
if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
|
|
985
692
|
|
|
986
|
-
|
|
987
|
-
req.callback = cb
|
|
988
|
-
binding.utimes(req.handle, toNamespacedPath(filepath), atime, mtime)
|
|
989
|
-
}
|
|
693
|
+
filepath = toNamespacedPath(filepath)
|
|
990
694
|
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
695
|
+
const req = new FileRequest()
|
|
696
|
+
|
|
697
|
+
let err = null
|
|
698
|
+
try {
|
|
699
|
+
binding.utimes(req.handle, filepath, atime, mtime)
|
|
700
|
+
|
|
701
|
+
await req
|
|
702
|
+
} catch (e) {
|
|
703
|
+
err = new FileError(e.message, {
|
|
704
|
+
operation: 'utimes',
|
|
705
|
+
code: e.code,
|
|
706
|
+
path: filepath
|
|
707
|
+
})
|
|
708
|
+
} finally {
|
|
709
|
+
req.destroy()
|
|
1001
710
|
}
|
|
1002
711
|
|
|
712
|
+
return done(err, cb)
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function utimesSync(filepath, atime, mtime) {
|
|
1003
716
|
if (typeof atime !== 'number') atime = atime.getTime() / 1000
|
|
1004
717
|
if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
|
|
1005
718
|
|
|
1006
|
-
binding.utimesSync(toNamespacedPath(filepath), atime, mtime)
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
function mkdirRecursive(filepath, mode, cb) {
|
|
1010
719
|
filepath = toNamespacedPath(filepath)
|
|
1011
720
|
|
|
1012
|
-
|
|
1013
|
-
if (err === null) return cb(null, 0, null)
|
|
1014
|
-
|
|
1015
|
-
if (err.code !== 'ENOENT') {
|
|
1016
|
-
stat(filepath, function (e, st) {
|
|
1017
|
-
if (e) return cb(e, e.errno, null)
|
|
1018
|
-
if (st.isDirectory()) return cb(null, 0, null)
|
|
1019
|
-
cb(err, err.errno, null)
|
|
1020
|
-
})
|
|
1021
|
-
return
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
|
|
1025
|
-
const i = filepath.lastIndexOf(path.sep)
|
|
1026
|
-
if (i <= 0) return cb(err, err.errno, null)
|
|
1027
|
-
|
|
1028
|
-
mkdirRecursive(filepath.slice(0, i), mode, function (err) {
|
|
1029
|
-
if (err) return cb(err, err.errno, null)
|
|
721
|
+
const req = new FileRequest()
|
|
1030
722
|
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
})
|
|
1039
|
-
})
|
|
723
|
+
try {
|
|
724
|
+
binding.utimesSync(req.handle, filepath, atime, mtime)
|
|
725
|
+
} catch (e) {
|
|
726
|
+
throw new FileError(e.message, {
|
|
727
|
+
operation: 'utimes',
|
|
728
|
+
code: e.code,
|
|
729
|
+
path: filepath
|
|
1040
730
|
})
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
function mkdir(filepath, opts, cb) {
|
|
1045
|
-
if (typeof filepath !== 'string') {
|
|
1046
|
-
throw typeError(
|
|
1047
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1048
|
-
'Path must be a string. Received type ' +
|
|
1049
|
-
typeof filepath +
|
|
1050
|
-
' (' +
|
|
1051
|
-
filepath +
|
|
1052
|
-
')'
|
|
1053
|
-
)
|
|
731
|
+
} finally {
|
|
732
|
+
req.destroy()
|
|
1054
733
|
}
|
|
734
|
+
}
|
|
1055
735
|
|
|
736
|
+
async function mkdir(filepath, opts, cb) {
|
|
1056
737
|
if (typeof opts === 'function') {
|
|
1057
738
|
cb = opts
|
|
1058
739
|
opts = { mode: 0o777 }
|
|
1059
|
-
} else if (typeof cb !== 'function') {
|
|
1060
|
-
throw typeError(
|
|
1061
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1062
|
-
'Callback must be a function. Received type ' +
|
|
1063
|
-
typeof cb +
|
|
1064
|
-
' (' +
|
|
1065
|
-
cb +
|
|
1066
|
-
')'
|
|
1067
|
-
)
|
|
1068
740
|
}
|
|
1069
741
|
|
|
1070
742
|
if (typeof opts === 'number') opts = { mode: opts }
|
|
@@ -1074,53 +746,55 @@ function mkdir(filepath, opts, cb) {
|
|
|
1074
746
|
|
|
1075
747
|
filepath = toNamespacedPath(filepath)
|
|
1076
748
|
|
|
1077
|
-
if (opts.recursive)
|
|
749
|
+
if (opts.recursive) {
|
|
750
|
+
try {
|
|
751
|
+
await mkdir(filepath, { mode })
|
|
752
|
+
} catch (err) {
|
|
753
|
+
if (err.code !== 'ENOENT' && (await stat(filepath)).isDirectory()) {
|
|
754
|
+
return ok(cb)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
|
|
758
|
+
const i = filepath.lastIndexOf(path.sep)
|
|
759
|
+
if (i <= 0) return fail(err, cb)
|
|
1078
760
|
|
|
1079
|
-
|
|
1080
|
-
req.callback = cb
|
|
1081
|
-
binding.mkdir(req.handle, filepath, mode)
|
|
1082
|
-
}
|
|
761
|
+
await mkdir(filepath.slice(0, i), { mode, recursive: true })
|
|
1083
762
|
|
|
1084
|
-
|
|
1085
|
-
|
|
763
|
+
try {
|
|
764
|
+
await mkdir(filepath, { mode })
|
|
765
|
+
} catch (err) {
|
|
766
|
+
if ((await stat(filepath)).isDirectory()) {
|
|
767
|
+
return ok(cb)
|
|
768
|
+
}
|
|
1086
769
|
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
} catch (err) {
|
|
1090
|
-
if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
|
|
1091
|
-
return
|
|
770
|
+
return fail(err, cb)
|
|
771
|
+
}
|
|
1092
772
|
}
|
|
1093
773
|
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
if (i <= 0) throw err
|
|
1097
|
-
|
|
1098
|
-
mkdirRecursiveSync(filepath.slice(0, i), { mode })
|
|
774
|
+
return ok(cb)
|
|
775
|
+
}
|
|
1099
776
|
|
|
1100
|
-
|
|
1101
|
-
mkdirSync(filepath, { mode })
|
|
1102
|
-
} catch (err) {
|
|
1103
|
-
if (statSync(filepath).isDirectory()) {
|
|
1104
|
-
return
|
|
1105
|
-
}
|
|
777
|
+
const req = new FileRequest()
|
|
1106
778
|
|
|
1107
|
-
|
|
1108
|
-
|
|
779
|
+
let err = null
|
|
780
|
+
try {
|
|
781
|
+
binding.mkdir(req.handle, filepath, mode)
|
|
782
|
+
|
|
783
|
+
await req
|
|
784
|
+
} catch (e) {
|
|
785
|
+
err = new FileError(e.message, {
|
|
786
|
+
operation: 'mkdir',
|
|
787
|
+
code: e.code,
|
|
788
|
+
path: filepath
|
|
789
|
+
})
|
|
790
|
+
} finally {
|
|
791
|
+
req.destroy()
|
|
1109
792
|
}
|
|
793
|
+
|
|
794
|
+
return done(err, cb)
|
|
1110
795
|
}
|
|
1111
796
|
|
|
1112
797
|
function mkdirSync(filepath, opts) {
|
|
1113
|
-
if (typeof filepath !== 'string') {
|
|
1114
|
-
throw typeError(
|
|
1115
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1116
|
-
'Path must be a string. Received type ' +
|
|
1117
|
-
typeof filepath +
|
|
1118
|
-
' (' +
|
|
1119
|
-
filepath +
|
|
1120
|
-
')'
|
|
1121
|
-
)
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
798
|
if (typeof opts === 'number') opts = { mode: opts }
|
|
1125
799
|
else if (!opts) opts = {}
|
|
1126
800
|
|
|
@@ -1128,163 +802,137 @@ function mkdirSync(filepath, opts) {
|
|
|
1128
802
|
|
|
1129
803
|
filepath = toNamespacedPath(filepath)
|
|
1130
804
|
|
|
1131
|
-
if (opts.recursive)
|
|
805
|
+
if (opts.recursive) {
|
|
806
|
+
try {
|
|
807
|
+
mkdirSync(filepath, { mode })
|
|
808
|
+
} catch (err) {
|
|
809
|
+
if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
|
|
810
|
+
return
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
|
|
814
|
+
const i = filepath.lastIndexOf(path.sep)
|
|
815
|
+
if (i <= 0) throw err
|
|
1132
816
|
|
|
1133
|
-
|
|
1134
|
-
}
|
|
817
|
+
mkdirSync(filepath.slice(0, i), { mode, recursive: true })
|
|
1135
818
|
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
' (' +
|
|
1143
|
-
filepath +
|
|
1144
|
-
')'
|
|
1145
|
-
)
|
|
1146
|
-
}
|
|
819
|
+
try {
|
|
820
|
+
mkdirSync(filepath, { mode })
|
|
821
|
+
} catch (err) {
|
|
822
|
+
if (statSync(filepath).isDirectory()) {
|
|
823
|
+
return
|
|
824
|
+
}
|
|
1147
825
|
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
' (' +
|
|
1154
|
-
cb +
|
|
1155
|
-
')'
|
|
1156
|
-
)
|
|
826
|
+
throw err
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
return
|
|
1157
831
|
}
|
|
1158
832
|
|
|
1159
|
-
const req =
|
|
1160
|
-
req.callback = cb
|
|
1161
|
-
binding.rmdir(req.handle, toNamespacedPath(filepath))
|
|
1162
|
-
}
|
|
833
|
+
const req = new FileRequest()
|
|
1163
834
|
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
)
|
|
835
|
+
try {
|
|
836
|
+
binding.mkdirSync(req.handle, filepath, mode)
|
|
837
|
+
} catch (e) {
|
|
838
|
+
throw new FileError(e.message, {
|
|
839
|
+
operation: 'mkdir',
|
|
840
|
+
code: e.code,
|
|
841
|
+
path: filepath
|
|
842
|
+
})
|
|
843
|
+
} finally {
|
|
844
|
+
req.destroy()
|
|
1174
845
|
}
|
|
1175
|
-
|
|
1176
|
-
binding.rmdirSync(toNamespacedPath(filepath))
|
|
1177
846
|
}
|
|
1178
847
|
|
|
1179
|
-
function
|
|
848
|
+
async function rmdir(filepath, cb) {
|
|
1180
849
|
filepath = toNamespacedPath(filepath)
|
|
1181
850
|
|
|
1182
|
-
|
|
1183
|
-
if (err === null) return cb(null)
|
|
1184
|
-
|
|
1185
|
-
if (err.code !== 'ENOTEMPTY') return cb(err)
|
|
1186
|
-
|
|
1187
|
-
readdir(filepath, function (err, files) {
|
|
1188
|
-
if (err) return cb(err)
|
|
851
|
+
const req = new FileRequest()
|
|
1189
852
|
|
|
1190
|
-
|
|
853
|
+
let err = null
|
|
854
|
+
try {
|
|
855
|
+
binding.rmdir(req.handle, filepath)
|
|
856
|
+
|
|
857
|
+
await req
|
|
858
|
+
} catch (e) {
|
|
859
|
+
err = new FileError(e.message, {
|
|
860
|
+
operation: 'rmdir',
|
|
861
|
+
code: e.code,
|
|
862
|
+
path: filepath
|
|
863
|
+
})
|
|
864
|
+
} finally {
|
|
865
|
+
req.destroy()
|
|
866
|
+
}
|
|
1191
867
|
|
|
1192
|
-
|
|
1193
|
-
|
|
868
|
+
return done(err, cb)
|
|
869
|
+
}
|
|
1194
870
|
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
if (done) return
|
|
871
|
+
function rmdirSync(filepath) {
|
|
872
|
+
filepath = toNamespacedPath(filepath)
|
|
1198
873
|
|
|
1199
|
-
|
|
1200
|
-
done = true
|
|
1201
|
-
return cb(err)
|
|
1202
|
-
}
|
|
874
|
+
const req = new FileRequest()
|
|
1203
875
|
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
876
|
+
try {
|
|
877
|
+
binding.rmdirSync(req.handle, filepath)
|
|
878
|
+
} catch (e) {
|
|
879
|
+
throw new FileError(e.message, {
|
|
880
|
+
operation: 'rmdir',
|
|
881
|
+
code: e.code,
|
|
882
|
+
path: filepath
|
|
1207
883
|
})
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
function rm(filepath, opts, cb) {
|
|
1212
|
-
if (typeof filepath !== 'string') {
|
|
1213
|
-
throw typeError(
|
|
1214
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1215
|
-
'Path must be a string. Received type ' +
|
|
1216
|
-
typeof filepath +
|
|
1217
|
-
' (' +
|
|
1218
|
-
filepath +
|
|
1219
|
-
')'
|
|
1220
|
-
)
|
|
884
|
+
} finally {
|
|
885
|
+
req.destroy()
|
|
1221
886
|
}
|
|
887
|
+
}
|
|
1222
888
|
|
|
889
|
+
async function rm(filepath, opts, cb) {
|
|
1223
890
|
if (typeof opts === 'function') {
|
|
1224
891
|
cb = opts
|
|
1225
892
|
opts = {}
|
|
1226
|
-
} else if (typeof cb !== 'function') {
|
|
1227
|
-
throw typeError(
|
|
1228
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1229
|
-
'Callback must be a function. Received type ' +
|
|
1230
|
-
typeof cb +
|
|
1231
|
-
' (' +
|
|
1232
|
-
cb +
|
|
1233
|
-
')'
|
|
1234
|
-
)
|
|
1235
893
|
}
|
|
1236
894
|
|
|
1237
895
|
if (!opts) opts = {}
|
|
1238
896
|
|
|
1239
897
|
filepath = toNamespacedPath(filepath)
|
|
1240
898
|
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
return cb(err.code === 'ENOENT' && opts.force ? null : err)
|
|
1244
|
-
}
|
|
899
|
+
try {
|
|
900
|
+
const st = await lstat(filepath)
|
|
1245
901
|
|
|
1246
902
|
if (st.isDirectory()) {
|
|
1247
|
-
if (opts.recursive)
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
}
|
|
903
|
+
if (opts.recursive) {
|
|
904
|
+
try {
|
|
905
|
+
await rmdir(filepath)
|
|
906
|
+
} catch (err) {
|
|
907
|
+
if (err.code !== 'ENOTEMPTY') throw err
|
|
1253
908
|
|
|
1254
|
-
|
|
1255
|
-
})
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
function rmRecursiveSync(filepath, opts) {
|
|
1259
|
-
filepath = toNamespacedPath(filepath)
|
|
1260
|
-
|
|
1261
|
-
try {
|
|
1262
|
-
rmdirSync(filepath)
|
|
1263
|
-
} catch (err) {
|
|
1264
|
-
if (err.code !== 'ENOTEMPTY') throw err
|
|
909
|
+
const files = await readdir(filepath)
|
|
1265
910
|
|
|
1266
|
-
|
|
911
|
+
for (const file of files) {
|
|
912
|
+
await rm(filepath + path.sep + file, opts)
|
|
913
|
+
}
|
|
1267
914
|
|
|
1268
|
-
|
|
1269
|
-
|
|
915
|
+
await rmdir(filepath)
|
|
916
|
+
}
|
|
917
|
+
} else {
|
|
918
|
+
fail(
|
|
919
|
+
new FileError('is a directory', {
|
|
920
|
+
operation: 'rm',
|
|
921
|
+
code: 'EISDIR',
|
|
922
|
+
path: filepath
|
|
923
|
+
}),
|
|
924
|
+
cb
|
|
925
|
+
)
|
|
926
|
+
}
|
|
927
|
+
} else {
|
|
928
|
+
await unlink(filepath)
|
|
1270
929
|
}
|
|
1271
|
-
|
|
1272
|
-
|
|
930
|
+
} catch (err) {
|
|
931
|
+
if (err.code !== 'ENOENT' || !opts.force) fail(err, cb)
|
|
1273
932
|
}
|
|
1274
933
|
}
|
|
1275
934
|
|
|
1276
935
|
function rmSync(filepath, opts) {
|
|
1277
|
-
if (typeof filepath !== 'string') {
|
|
1278
|
-
throw typeError(
|
|
1279
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1280
|
-
'Path must be a string. Received type ' +
|
|
1281
|
-
typeof filepath +
|
|
1282
|
-
' (' +
|
|
1283
|
-
filepath +
|
|
1284
|
-
')'
|
|
1285
|
-
)
|
|
1286
|
-
}
|
|
1287
|
-
|
|
1288
936
|
if (!opts) opts = {}
|
|
1289
937
|
|
|
1290
938
|
filepath = toNamespacedPath(filepath)
|
|
@@ -1293,178 +941,175 @@ function rmSync(filepath, opts) {
|
|
|
1293
941
|
const st = lstatSync(filepath)
|
|
1294
942
|
|
|
1295
943
|
if (st.isDirectory()) {
|
|
1296
|
-
if (opts.recursive)
|
|
944
|
+
if (opts.recursive) {
|
|
945
|
+
try {
|
|
946
|
+
rmdirSync(filepath)
|
|
947
|
+
} catch (err) {
|
|
948
|
+
if (err.code !== 'ENOTEMPTY') throw err
|
|
1297
949
|
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
950
|
+
const files = readdirSync(filepath)
|
|
951
|
+
|
|
952
|
+
for (const file of files) {
|
|
953
|
+
rmSync(filepath + path.sep + file, opts)
|
|
954
|
+
}
|
|
1302
955
|
|
|
1303
|
-
|
|
956
|
+
rmdirSync(filepath)
|
|
957
|
+
}
|
|
958
|
+
} else {
|
|
959
|
+
throw new FileError('is a directory', {
|
|
960
|
+
operation: 'rm',
|
|
961
|
+
code: 'EISDIR',
|
|
962
|
+
path: filepath
|
|
963
|
+
})
|
|
964
|
+
}
|
|
965
|
+
} else {
|
|
966
|
+
unlinkSync(filepath)
|
|
967
|
+
}
|
|
1304
968
|
} catch (err) {
|
|
1305
969
|
if (err.code !== 'ENOENT' || !opts.force) throw err
|
|
1306
970
|
}
|
|
1307
971
|
}
|
|
1308
972
|
|
|
1309
|
-
function unlink(filepath, cb) {
|
|
1310
|
-
|
|
1311
|
-
throw typeError(
|
|
1312
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1313
|
-
'Path must be a string. Received type ' +
|
|
1314
|
-
typeof filepath +
|
|
1315
|
-
' (' +
|
|
1316
|
-
filepath +
|
|
1317
|
-
')'
|
|
1318
|
-
)
|
|
1319
|
-
}
|
|
973
|
+
async function unlink(filepath, cb) {
|
|
974
|
+
filepath = toNamespacedPath(filepath)
|
|
1320
975
|
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
976
|
+
const req = new FileRequest()
|
|
977
|
+
|
|
978
|
+
let err = null
|
|
979
|
+
try {
|
|
980
|
+
binding.unlink(req.handle, filepath)
|
|
981
|
+
|
|
982
|
+
await req
|
|
983
|
+
} catch (e) {
|
|
984
|
+
err = new FileError(e.message, {
|
|
985
|
+
operation: 'unlink',
|
|
986
|
+
code: e.code,
|
|
987
|
+
path: filepath
|
|
988
|
+
})
|
|
989
|
+
} finally {
|
|
990
|
+
req.destroy()
|
|
1330
991
|
}
|
|
1331
992
|
|
|
1332
|
-
|
|
1333
|
-
req.callback = cb
|
|
1334
|
-
binding.unlink(req.handle, toNamespacedPath(filepath))
|
|
993
|
+
return done(err, cb)
|
|
1335
994
|
}
|
|
1336
995
|
|
|
1337
996
|
function unlinkSync(filepath) {
|
|
1338
|
-
|
|
1339
|
-
throw typeError(
|
|
1340
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1341
|
-
'Path must be a string. Received type ' +
|
|
1342
|
-
typeof filepath +
|
|
1343
|
-
' (' +
|
|
1344
|
-
filepath +
|
|
1345
|
-
')'
|
|
1346
|
-
)
|
|
1347
|
-
}
|
|
997
|
+
filepath = toNamespacedPath(filepath)
|
|
1348
998
|
|
|
1349
|
-
|
|
1350
|
-
}
|
|
999
|
+
const req = new FileRequest()
|
|
1351
1000
|
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1001
|
+
try {
|
|
1002
|
+
binding.unlinkSync(req.handle, filepath)
|
|
1003
|
+
} catch (e) {
|
|
1004
|
+
throw new FileError(e.message, {
|
|
1005
|
+
operation: 'unlink',
|
|
1006
|
+
code: e.code,
|
|
1007
|
+
path: filepath
|
|
1008
|
+
})
|
|
1009
|
+
} finally {
|
|
1010
|
+
req.destroy()
|
|
1358
1011
|
}
|
|
1012
|
+
}
|
|
1359
1013
|
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
|
|
1364
|
-
)
|
|
1365
|
-
}
|
|
1014
|
+
async function rename(src, dst, cb) {
|
|
1015
|
+
src = toNamespacedPath(src)
|
|
1016
|
+
dst = toNamespacedPath(dst)
|
|
1366
1017
|
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1018
|
+
const req = new FileRequest()
|
|
1019
|
+
|
|
1020
|
+
let err = null
|
|
1021
|
+
try {
|
|
1022
|
+
binding.rename(req.handle, src, dst)
|
|
1023
|
+
|
|
1024
|
+
await req
|
|
1025
|
+
} catch (e) {
|
|
1026
|
+
err = new FileError(e.message, {
|
|
1027
|
+
operation: 'rename',
|
|
1028
|
+
code: e.code,
|
|
1029
|
+
path: src,
|
|
1030
|
+
destination: dst
|
|
1031
|
+
})
|
|
1032
|
+
} finally {
|
|
1033
|
+
req.destroy()
|
|
1376
1034
|
}
|
|
1377
1035
|
|
|
1378
|
-
|
|
1379
|
-
req.callback = cb
|
|
1380
|
-
binding.rename(req.handle, src, dst)
|
|
1036
|
+
return done(err, cb)
|
|
1381
1037
|
}
|
|
1382
1038
|
|
|
1383
1039
|
function renameSync(src, dst) {
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1387
|
-
'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
|
|
1388
|
-
)
|
|
1389
|
-
}
|
|
1040
|
+
src = toNamespacedPath(src)
|
|
1041
|
+
dst = toNamespacedPath(dst)
|
|
1390
1042
|
|
|
1391
|
-
|
|
1392
|
-
throw typeError(
|
|
1393
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1394
|
-
'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
|
|
1395
|
-
)
|
|
1396
|
-
}
|
|
1043
|
+
const req = new FileRequest()
|
|
1397
1044
|
|
|
1398
|
-
|
|
1045
|
+
try {
|
|
1046
|
+
binding.renameSync(req.handle, src, dst)
|
|
1047
|
+
} catch (e) {
|
|
1048
|
+
throw new FileError(e.message, {
|
|
1049
|
+
operation: 'rename',
|
|
1050
|
+
code: e.code,
|
|
1051
|
+
path: src,
|
|
1052
|
+
destination: dst
|
|
1053
|
+
})
|
|
1054
|
+
} finally {
|
|
1055
|
+
req.destroy()
|
|
1056
|
+
}
|
|
1399
1057
|
}
|
|
1400
1058
|
|
|
1401
|
-
function copyFile(src, dst, mode, cb) {
|
|
1059
|
+
async function copyFile(src, dst, mode = 0, cb) {
|
|
1402
1060
|
if (typeof mode === 'function') {
|
|
1403
1061
|
cb = mode
|
|
1404
1062
|
mode = 0
|
|
1405
1063
|
}
|
|
1406
1064
|
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1410
|
-
'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
|
|
1411
|
-
)
|
|
1412
|
-
}
|
|
1065
|
+
src = toNamespacedPath(src)
|
|
1066
|
+
dst = toNamespacedPath(dst)
|
|
1413
1067
|
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
)
|
|
1068
|
+
const req = new FileRequest()
|
|
1069
|
+
|
|
1070
|
+
let err = null
|
|
1071
|
+
try {
|
|
1072
|
+
binding.copyfile(req.handle, src, dst, mode)
|
|
1073
|
+
|
|
1074
|
+
await req
|
|
1075
|
+
} catch (e) {
|
|
1076
|
+
err = new FileError(e.message, {
|
|
1077
|
+
operation: 'copyfile',
|
|
1078
|
+
code: e.code,
|
|
1079
|
+
path: src,
|
|
1080
|
+
destination: dst
|
|
1081
|
+
})
|
|
1082
|
+
} finally {
|
|
1083
|
+
req.destroy()
|
|
1419
1084
|
}
|
|
1420
1085
|
|
|
1421
|
-
|
|
1422
|
-
req.callback = cb
|
|
1423
|
-
binding.copyfile(req.handle, src, dst, mode)
|
|
1086
|
+
return done(err, cb)
|
|
1424
1087
|
}
|
|
1425
1088
|
|
|
1426
1089
|
function copyFileSync(src, dst, mode = 0) {
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1430
|
-
'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
|
|
1431
|
-
)
|
|
1432
|
-
}
|
|
1433
|
-
|
|
1434
|
-
if (typeof dst !== 'string') {
|
|
1435
|
-
throw typeError(
|
|
1436
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1437
|
-
'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
|
|
1438
|
-
)
|
|
1439
|
-
}
|
|
1090
|
+
src = toNamespacedPath(src)
|
|
1091
|
+
dst = toNamespacedPath(dst)
|
|
1440
1092
|
|
|
1441
|
-
|
|
1442
|
-
}
|
|
1093
|
+
const req = new FileRequest()
|
|
1443
1094
|
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1095
|
+
try {
|
|
1096
|
+
binding.copyfileSync(req.handle, src, dst, mode)
|
|
1097
|
+
} catch (e) {
|
|
1098
|
+
throw new FileError(e.message, {
|
|
1099
|
+
operation: 'copyfile',
|
|
1100
|
+
code: e.code,
|
|
1101
|
+
path: src,
|
|
1102
|
+
destination: dst
|
|
1103
|
+
})
|
|
1104
|
+
} finally {
|
|
1105
|
+
req.destroy()
|
|
1454
1106
|
}
|
|
1107
|
+
}
|
|
1455
1108
|
|
|
1109
|
+
async function realpath(filepath, opts, cb) {
|
|
1456
1110
|
if (typeof opts === 'function') {
|
|
1457
1111
|
cb = opts
|
|
1458
1112
|
opts = {}
|
|
1459
|
-
} else if (typeof cb !== 'function') {
|
|
1460
|
-
throw typeError(
|
|
1461
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1462
|
-
'Callback must be a function. Received type ' +
|
|
1463
|
-
typeof cb +
|
|
1464
|
-
' (' +
|
|
1465
|
-
cb +
|
|
1466
|
-
')'
|
|
1467
|
-
)
|
|
1468
1113
|
}
|
|
1469
1114
|
|
|
1470
1115
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -1472,70 +1117,66 @@ function realpath(filepath, opts, cb) {
|
|
|
1472
1117
|
|
|
1473
1118
|
const { encoding = 'utf8' } = opts
|
|
1474
1119
|
|
|
1475
|
-
|
|
1120
|
+
filepath = toNamespacedPath(filepath)
|
|
1121
|
+
|
|
1122
|
+
const req = new FileRequest()
|
|
1123
|
+
|
|
1124
|
+
let res
|
|
1125
|
+
let err = null
|
|
1126
|
+
try {
|
|
1127
|
+
binding.realpath(req.handle, filepath)
|
|
1128
|
+
|
|
1129
|
+
await req
|
|
1476
1130
|
|
|
1477
|
-
|
|
1131
|
+
res = Buffer.from(binding.requestResultString(req.handle))
|
|
1478
1132
|
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1133
|
+
if (encoding !== 'buffer') res = res.toString(encoding)
|
|
1134
|
+
} catch (e) {
|
|
1135
|
+
err = new FileError(e.message, {
|
|
1136
|
+
operation: 'realpath',
|
|
1137
|
+
code: e.code,
|
|
1138
|
+
path: filepath
|
|
1139
|
+
})
|
|
1140
|
+
} finally {
|
|
1141
|
+
req.destroy()
|
|
1484
1142
|
}
|
|
1485
1143
|
|
|
1486
|
-
|
|
1144
|
+
return done(err, res, cb)
|
|
1487
1145
|
}
|
|
1488
1146
|
|
|
1489
1147
|
function realpathSync(filepath, opts) {
|
|
1490
|
-
if (typeof filepath !== 'string') {
|
|
1491
|
-
throw typeError(
|
|
1492
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1493
|
-
'Path must be a string. Received type ' +
|
|
1494
|
-
typeof filepath +
|
|
1495
|
-
' (' +
|
|
1496
|
-
filepath +
|
|
1497
|
-
')'
|
|
1498
|
-
)
|
|
1499
|
-
}
|
|
1500
|
-
|
|
1501
1148
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
1502
1149
|
else if (!opts) opts = {}
|
|
1503
1150
|
|
|
1504
1151
|
const { encoding = 'utf8' } = opts
|
|
1505
1152
|
|
|
1506
|
-
|
|
1153
|
+
filepath = toNamespacedPath(filepath)
|
|
1507
1154
|
|
|
1508
|
-
|
|
1155
|
+
const req = new FileRequest()
|
|
1509
1156
|
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
return filepath
|
|
1513
|
-
}
|
|
1157
|
+
try {
|
|
1158
|
+
binding.realpathSync(req.handle, filepath)
|
|
1514
1159
|
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1160
|
+
let res = Buffer.from(binding.requestResultString(req.handle))
|
|
1161
|
+
|
|
1162
|
+
if (encoding !== 'buffer') res = res.toString(encoding)
|
|
1163
|
+
|
|
1164
|
+
return res
|
|
1165
|
+
} catch (e) {
|
|
1166
|
+
throw new FileError(e.message, {
|
|
1167
|
+
operation: 'realpath',
|
|
1168
|
+
code: e.code,
|
|
1169
|
+
path: filepath
|
|
1170
|
+
})
|
|
1171
|
+
} finally {
|
|
1172
|
+
req.destroy()
|
|
1525
1173
|
}
|
|
1174
|
+
}
|
|
1526
1175
|
|
|
1176
|
+
async function readlink(filepath, opts, cb) {
|
|
1527
1177
|
if (typeof opts === 'function') {
|
|
1528
1178
|
cb = opts
|
|
1529
1179
|
opts = {}
|
|
1530
|
-
} else if (typeof cb !== 'function') {
|
|
1531
|
-
throw typeError(
|
|
1532
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1533
|
-
'Callback must be a function. Received type ' +
|
|
1534
|
-
typeof cb +
|
|
1535
|
-
' (' +
|
|
1536
|
-
cb +
|
|
1537
|
-
')'
|
|
1538
|
-
)
|
|
1539
1180
|
}
|
|
1540
1181
|
|
|
1541
1182
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -1543,44 +1184,60 @@ function readlink(filepath, opts, cb) {
|
|
|
1543
1184
|
|
|
1544
1185
|
const { encoding = 'utf8' } = opts
|
|
1545
1186
|
|
|
1546
|
-
|
|
1187
|
+
filepath = toNamespacedPath(filepath)
|
|
1188
|
+
|
|
1189
|
+
const req = new FileRequest()
|
|
1190
|
+
|
|
1191
|
+
let res
|
|
1192
|
+
let err = null
|
|
1193
|
+
try {
|
|
1194
|
+
binding.readlink(req.handle, filepath)
|
|
1195
|
+
|
|
1196
|
+
await req
|
|
1547
1197
|
|
|
1548
|
-
|
|
1198
|
+
res = Buffer.from(binding.requestResultString(req.handle))
|
|
1549
1199
|
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1200
|
+
if (encoding !== 'buffer') res = res.toString(encoding)
|
|
1201
|
+
} catch (e) {
|
|
1202
|
+
err = new FileError(e.message, {
|
|
1203
|
+
operation: 'readlink',
|
|
1204
|
+
code: e.code,
|
|
1205
|
+
path: filepath
|
|
1206
|
+
})
|
|
1207
|
+
} finally {
|
|
1208
|
+
req.destroy()
|
|
1555
1209
|
}
|
|
1556
1210
|
|
|
1557
|
-
|
|
1211
|
+
return done(err, res, cb)
|
|
1558
1212
|
}
|
|
1559
1213
|
|
|
1560
1214
|
function readlinkSync(filepath, opts) {
|
|
1561
|
-
if (typeof filepath !== 'string') {
|
|
1562
|
-
throw typeError(
|
|
1563
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1564
|
-
'Path must be a string. Received type ' +
|
|
1565
|
-
typeof filepath +
|
|
1566
|
-
' (' +
|
|
1567
|
-
filepath +
|
|
1568
|
-
')'
|
|
1569
|
-
)
|
|
1570
|
-
}
|
|
1571
|
-
|
|
1572
1215
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
1573
1216
|
else if (!opts) opts = {}
|
|
1574
1217
|
|
|
1575
1218
|
const { encoding = 'utf8' } = opts
|
|
1576
1219
|
|
|
1577
|
-
|
|
1220
|
+
filepath = toNamespacedPath(filepath)
|
|
1221
|
+
|
|
1222
|
+
const req = new FileRequest()
|
|
1578
1223
|
|
|
1579
|
-
|
|
1224
|
+
try {
|
|
1225
|
+
binding.readlinkSync(req.handle, filepath)
|
|
1226
|
+
|
|
1227
|
+
let res = Buffer.from(binding.requestResultString(req.handle))
|
|
1580
1228
|
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1229
|
+
if (encoding !== 'buffer') res = res.toString(encoding)
|
|
1230
|
+
|
|
1231
|
+
return res
|
|
1232
|
+
} catch (e) {
|
|
1233
|
+
throw new FileError(e.message, {
|
|
1234
|
+
operation: 'readlink',
|
|
1235
|
+
code: e.code,
|
|
1236
|
+
path: filepath
|
|
1237
|
+
})
|
|
1238
|
+
} finally {
|
|
1239
|
+
req.destroy()
|
|
1240
|
+
}
|
|
1584
1241
|
}
|
|
1585
1242
|
|
|
1586
1243
|
function normalizeSymlinkTarget(target, type, filepath) {
|
|
@@ -1595,41 +1252,10 @@ function normalizeSymlinkTarget(target, type, filepath) {
|
|
|
1595
1252
|
return target
|
|
1596
1253
|
}
|
|
1597
1254
|
|
|
1598
|
-
function symlink(target, filepath, type, cb) {
|
|
1599
|
-
if (typeof target !== 'string') {
|
|
1600
|
-
throw typeError(
|
|
1601
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1602
|
-
'Target must be a string. Received type ' +
|
|
1603
|
-
typeof filepath +
|
|
1604
|
-
' (' +
|
|
1605
|
-
filepath +
|
|
1606
|
-
')'
|
|
1607
|
-
)
|
|
1608
|
-
}
|
|
1609
|
-
|
|
1610
|
-
if (typeof filepath !== 'string') {
|
|
1611
|
-
throw typeError(
|
|
1612
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1613
|
-
'Path must be a string. Received type ' +
|
|
1614
|
-
typeof filepath +
|
|
1615
|
-
' (' +
|
|
1616
|
-
filepath +
|
|
1617
|
-
')'
|
|
1618
|
-
)
|
|
1619
|
-
}
|
|
1620
|
-
|
|
1255
|
+
async function symlink(target, filepath, type, cb) {
|
|
1621
1256
|
if (typeof type === 'function') {
|
|
1622
1257
|
cb = type
|
|
1623
1258
|
type = null
|
|
1624
|
-
} else if (typeof cb !== 'function') {
|
|
1625
|
-
throw typeError(
|
|
1626
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1627
|
-
'Callback must be a function. Received type ' +
|
|
1628
|
-
typeof cb +
|
|
1629
|
-
' (' +
|
|
1630
|
-
cb +
|
|
1631
|
-
')'
|
|
1632
|
-
)
|
|
1633
1259
|
}
|
|
1634
1260
|
|
|
1635
1261
|
filepath = toNamespacedPath(filepath)
|
|
@@ -1637,6 +1263,7 @@ function symlink(target, filepath, type, cb) {
|
|
|
1637
1263
|
if (typeof type === 'string') {
|
|
1638
1264
|
switch (type) {
|
|
1639
1265
|
case 'file':
|
|
1266
|
+
default:
|
|
1640
1267
|
type = 0
|
|
1641
1268
|
break
|
|
1642
1269
|
case 'dir':
|
|
@@ -1645,66 +1272,53 @@ function symlink(target, filepath, type, cb) {
|
|
|
1645
1272
|
case 'junction':
|
|
1646
1273
|
type = constants.UV_FS_SYMLINK_JUNCTION
|
|
1647
1274
|
break
|
|
1648
|
-
default:
|
|
1649
|
-
throw typeError(
|
|
1650
|
-
'ERR_FS_INVALID_SYMLINK_TYPE',
|
|
1651
|
-
'Symlink type must be one of "dir", "file", or "junction". Received "' +
|
|
1652
|
-
type +
|
|
1653
|
-
'"'
|
|
1654
|
-
)
|
|
1655
1275
|
}
|
|
1656
1276
|
} else if (typeof type !== 'number') {
|
|
1657
1277
|
if (isWindows) {
|
|
1658
1278
|
target = path.resolve(filepath, '..', target)
|
|
1659
1279
|
|
|
1660
|
-
|
|
1661
|
-
type =
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1280
|
+
try {
|
|
1281
|
+
type = (await stat(target)).isDirectory()
|
|
1282
|
+
? constants.UV_FS_SYMLINK_DIR
|
|
1283
|
+
: constants.UV_FS_SYMLINK_JUNCTION
|
|
1284
|
+
} catch {
|
|
1285
|
+
type = 0
|
|
1286
|
+
}
|
|
1287
|
+
} else {
|
|
1288
|
+
type = 0
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1665
1291
|
|
|
1666
|
-
|
|
1667
|
-
})
|
|
1292
|
+
target = normalizeSymlinkTarget(target)
|
|
1668
1293
|
|
|
1669
|
-
|
|
1670
|
-
}
|
|
1294
|
+
const req = new FileRequest()
|
|
1671
1295
|
|
|
1672
|
-
|
|
1296
|
+
let err = null
|
|
1297
|
+
try {
|
|
1298
|
+
binding.symlink(req.handle, target, filepath, type)
|
|
1299
|
+
|
|
1300
|
+
await req
|
|
1301
|
+
} catch (e) {
|
|
1302
|
+
err = new FileError(e.message, {
|
|
1303
|
+
operation: 'symlink',
|
|
1304
|
+
code: e.code,
|
|
1305
|
+
path: target,
|
|
1306
|
+
destination: filepath
|
|
1307
|
+
})
|
|
1308
|
+
} finally {
|
|
1309
|
+
req.destroy()
|
|
1673
1310
|
}
|
|
1674
1311
|
|
|
1675
|
-
|
|
1676
|
-
req.callback = cb
|
|
1677
|
-
binding.symlink(req.handle, normalizeSymlinkTarget(target), filepath, type)
|
|
1312
|
+
return done(err, cb)
|
|
1678
1313
|
}
|
|
1679
1314
|
|
|
1680
1315
|
function symlinkSync(target, filepath, type) {
|
|
1681
|
-
if (typeof target !== 'string') {
|
|
1682
|
-
throw typeError(
|
|
1683
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1684
|
-
'Target must be a string. Received type ' +
|
|
1685
|
-
typeof filepath +
|
|
1686
|
-
' (' +
|
|
1687
|
-
filepath +
|
|
1688
|
-
')'
|
|
1689
|
-
)
|
|
1690
|
-
}
|
|
1691
|
-
|
|
1692
|
-
if (typeof filepath !== 'string') {
|
|
1693
|
-
throw typeError(
|
|
1694
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1695
|
-
'Path must be a string. Received type ' +
|
|
1696
|
-
typeof filepath +
|
|
1697
|
-
' (' +
|
|
1698
|
-
filepath +
|
|
1699
|
-
')'
|
|
1700
|
-
)
|
|
1701
|
-
}
|
|
1702
|
-
|
|
1703
1316
|
filepath = toNamespacedPath(filepath)
|
|
1704
1317
|
|
|
1705
1318
|
if (typeof type === 'string') {
|
|
1706
1319
|
switch (type) {
|
|
1707
1320
|
case 'file':
|
|
1321
|
+
default:
|
|
1708
1322
|
type = 0
|
|
1709
1323
|
break
|
|
1710
1324
|
case 'dir':
|
|
@@ -1713,53 +1327,45 @@ function symlinkSync(target, filepath, type) {
|
|
|
1713
1327
|
case 'junction':
|
|
1714
1328
|
type = constants.UV_FS_SYMLINK_JUNCTION
|
|
1715
1329
|
break
|
|
1716
|
-
default:
|
|
1717
|
-
throw typeError(
|
|
1718
|
-
'ERR_FS_INVALID_SYMLINK_TYPE',
|
|
1719
|
-
'Symlink type must be one of "dir", "file", or "junction". Received "' +
|
|
1720
|
-
type +
|
|
1721
|
-
'"'
|
|
1722
|
-
)
|
|
1723
1330
|
}
|
|
1724
1331
|
} else if (typeof type !== 'number') {
|
|
1725
1332
|
if (isWindows) {
|
|
1726
1333
|
target = path.resolve(filepath, '..', target)
|
|
1727
1334
|
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1335
|
+
try {
|
|
1336
|
+
type = statSync(target).isDirectory()
|
|
1337
|
+
? constants.UV_FS_SYMLINK_DIR
|
|
1338
|
+
: constants.UV_FS_SYMLINK_JUNCTION
|
|
1339
|
+
} catch {
|
|
1340
|
+
type = 0
|
|
1341
|
+
}
|
|
1731
1342
|
} else {
|
|
1732
1343
|
type = 0
|
|
1733
1344
|
}
|
|
1734
1345
|
}
|
|
1735
1346
|
|
|
1736
|
-
|
|
1737
|
-
}
|
|
1347
|
+
target = normalizeSymlinkTarget(target)
|
|
1738
1348
|
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1349
|
+
const req = new FileRequest()
|
|
1350
|
+
|
|
1351
|
+
try {
|
|
1352
|
+
binding.symlinkSync(req.handle, target, filepath, type)
|
|
1353
|
+
} catch (e) {
|
|
1354
|
+
throw new FileError(e.message, {
|
|
1355
|
+
operation: 'symlink',
|
|
1356
|
+
code: e.code,
|
|
1357
|
+
path: target,
|
|
1358
|
+
destination: filepath
|
|
1359
|
+
})
|
|
1360
|
+
} finally {
|
|
1361
|
+
req.destroy()
|
|
1749
1362
|
}
|
|
1363
|
+
}
|
|
1750
1364
|
|
|
1365
|
+
async function opendir(filepath, opts, cb) {
|
|
1751
1366
|
if (typeof opts === 'function') {
|
|
1752
1367
|
cb = opts
|
|
1753
1368
|
opts = {}
|
|
1754
|
-
} else if (typeof cb !== 'function') {
|
|
1755
|
-
throw typeError(
|
|
1756
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1757
|
-
'Callback must be a function. Received type ' +
|
|
1758
|
-
typeof cb +
|
|
1759
|
-
' (' +
|
|
1760
|
-
cb +
|
|
1761
|
-
')'
|
|
1762
|
-
)
|
|
1763
1369
|
}
|
|
1764
1370
|
|
|
1765
1371
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -1767,64 +1373,56 @@ function opendir(filepath, opts, cb) {
|
|
|
1767
1373
|
|
|
1768
1374
|
filepath = toNamespacedPath(filepath)
|
|
1769
1375
|
|
|
1770
|
-
const
|
|
1376
|
+
const req = new FileRequest()
|
|
1377
|
+
|
|
1378
|
+
let dir
|
|
1379
|
+
let err = null
|
|
1380
|
+
try {
|
|
1381
|
+
binding.opendir(req.handle, filepath)
|
|
1771
1382
|
|
|
1772
|
-
|
|
1383
|
+
await req
|
|
1773
1384
|
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1385
|
+
dir = new Dir(filepath, binding.requestResultDir(req.handle), opts)
|
|
1386
|
+
} catch (e) {
|
|
1387
|
+
err = new FileError(e.message, {
|
|
1388
|
+
operation: 'opendir',
|
|
1389
|
+
code: e.code,
|
|
1390
|
+
path: filepath
|
|
1391
|
+
})
|
|
1392
|
+
} finally {
|
|
1393
|
+
req.destroy()
|
|
1777
1394
|
}
|
|
1778
1395
|
|
|
1779
|
-
|
|
1396
|
+
return done(err, dir, cb)
|
|
1780
1397
|
}
|
|
1781
1398
|
|
|
1782
1399
|
function opendirSync(filepath, opts) {
|
|
1783
|
-
if (typeof filepath !== 'string') {
|
|
1784
|
-
throw typeError(
|
|
1785
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1786
|
-
'Path must be a string. Received type ' +
|
|
1787
|
-
typeof filepath +
|
|
1788
|
-
' (' +
|
|
1789
|
-
filepath +
|
|
1790
|
-
')'
|
|
1791
|
-
)
|
|
1792
|
-
}
|
|
1793
|
-
|
|
1794
1400
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
1795
1401
|
else if (!opts) opts = {}
|
|
1796
1402
|
|
|
1797
1403
|
filepath = toNamespacedPath(filepath)
|
|
1798
1404
|
|
|
1799
|
-
const
|
|
1800
|
-
binding.opendirSync(filepath, data)
|
|
1801
|
-
return new Dir(filepath, data, opts)
|
|
1802
|
-
}
|
|
1405
|
+
const req = new FileRequest()
|
|
1803
1406
|
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
)
|
|
1407
|
+
try {
|
|
1408
|
+
binding.opendirSync(req.handle, filepath)
|
|
1409
|
+
|
|
1410
|
+
return new Dir(filepath, binding.requestResultDir(req.handle), opts)
|
|
1411
|
+
} catch (e) {
|
|
1412
|
+
throw new FileError(e.message, {
|
|
1413
|
+
operation: 'opendir',
|
|
1414
|
+
code: e.code,
|
|
1415
|
+
path: filepath
|
|
1416
|
+
})
|
|
1417
|
+
} finally {
|
|
1418
|
+
req.destroy()
|
|
1814
1419
|
}
|
|
1420
|
+
}
|
|
1815
1421
|
|
|
1422
|
+
async function readdir(filepath, opts, cb) {
|
|
1816
1423
|
if (typeof opts === 'function') {
|
|
1817
1424
|
cb = opts
|
|
1818
1425
|
opts = {}
|
|
1819
|
-
} else if (typeof cb !== 'function') {
|
|
1820
|
-
throw typeError(
|
|
1821
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1822
|
-
'Callback must be a function. Received type ' +
|
|
1823
|
-
typeof cb +
|
|
1824
|
-
' (' +
|
|
1825
|
-
cb +
|
|
1826
|
-
')'
|
|
1827
|
-
)
|
|
1828
1426
|
}
|
|
1829
1427
|
|
|
1830
1428
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -1832,69 +1430,46 @@ function readdir(filepath, opts, cb) {
|
|
|
1832
1430
|
|
|
1833
1431
|
const { withFileTypes = false } = opts
|
|
1834
1432
|
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1433
|
+
filepath = toNamespacedPath(filepath)
|
|
1434
|
+
|
|
1435
|
+
let result = []
|
|
1436
|
+
let err = null
|
|
1437
|
+
try {
|
|
1438
|
+
const dir = await opendir(filepath)
|
|
1439
|
+
|
|
1838
1440
|
for await (const entry of dir) {
|
|
1839
1441
|
result.push(withFileTypes ? entry : entry.name)
|
|
1840
1442
|
}
|
|
1841
|
-
|
|
1842
|
-
|
|
1443
|
+
} catch (e) {
|
|
1444
|
+
result = []
|
|
1445
|
+
err = e
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
return done(err, result, cb)
|
|
1843
1449
|
}
|
|
1844
1450
|
|
|
1845
1451
|
function readdirSync(filepath, opts) {
|
|
1846
|
-
if (typeof filepath !== 'string') {
|
|
1847
|
-
throw typeError(
|
|
1848
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1849
|
-
'Path must be a string. Received type ' +
|
|
1850
|
-
typeof filepath +
|
|
1851
|
-
' (' +
|
|
1852
|
-
filepath +
|
|
1853
|
-
')'
|
|
1854
|
-
)
|
|
1855
|
-
}
|
|
1856
|
-
|
|
1857
1452
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
1858
1453
|
else if (!opts) opts = {}
|
|
1859
1454
|
|
|
1860
1455
|
const { withFileTypes = false } = opts
|
|
1861
1456
|
|
|
1862
|
-
|
|
1457
|
+
filepath = toNamespacedPath(filepath)
|
|
1458
|
+
|
|
1459
|
+
const dir = opendirSync(filepath, opts)
|
|
1863
1460
|
const result = []
|
|
1864
1461
|
|
|
1865
|
-
|
|
1866
|
-
const entry = dir.readSync()
|
|
1867
|
-
if (entry === null) break
|
|
1462
|
+
for (const entry of dir) {
|
|
1868
1463
|
result.push(withFileTypes ? entry : entry.name)
|
|
1869
1464
|
}
|
|
1870
1465
|
|
|
1871
1466
|
return result
|
|
1872
1467
|
}
|
|
1873
1468
|
|
|
1874
|
-
function readFile(filepath, opts, cb) {
|
|
1875
|
-
if (typeof filepath !== 'string') {
|
|
1876
|
-
throw typeError(
|
|
1877
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1878
|
-
'Path must be a string. Received type ' +
|
|
1879
|
-
typeof filepath +
|
|
1880
|
-
' (' +
|
|
1881
|
-
filepath +
|
|
1882
|
-
')'
|
|
1883
|
-
)
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1469
|
+
async function readFile(filepath, opts, cb) {
|
|
1886
1470
|
if (typeof opts === 'function') {
|
|
1887
1471
|
cb = opts
|
|
1888
1472
|
opts = {}
|
|
1889
|
-
} else if (typeof cb !== 'function') {
|
|
1890
|
-
throw typeError(
|
|
1891
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1892
|
-
'Callback must be a function. Received type ' +
|
|
1893
|
-
typeof cb +
|
|
1894
|
-
' (' +
|
|
1895
|
-
cb +
|
|
1896
|
-
')'
|
|
1897
|
-
)
|
|
1898
1473
|
}
|
|
1899
1474
|
|
|
1900
1475
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -1902,118 +1477,106 @@ function readFile(filepath, opts, cb) {
|
|
|
1902
1477
|
|
|
1903
1478
|
const { encoding = 'buffer' } = opts
|
|
1904
1479
|
|
|
1905
|
-
|
|
1906
|
-
|
|
1480
|
+
let fd = -1
|
|
1481
|
+
try {
|
|
1482
|
+
fd = await open(filepath, opts.flag || 'r')
|
|
1907
1483
|
|
|
1908
|
-
fstat(fd
|
|
1909
|
-
if (err) return closeAndError(err)
|
|
1484
|
+
const st = await fstat(fd)
|
|
1910
1485
|
|
|
1911
|
-
|
|
1912
|
-
|
|
1486
|
+
let buffer
|
|
1487
|
+
let len = 0
|
|
1913
1488
|
|
|
1914
|
-
|
|
1489
|
+
if (st.size === 0) {
|
|
1490
|
+
const buffers = []
|
|
1915
1491
|
|
|
1916
|
-
|
|
1917
|
-
|
|
1492
|
+
while (true) {
|
|
1493
|
+
buffer = Buffer.allocUnsafe(8192)
|
|
1494
|
+
const r = await read(fd, buffer, 0, 8192)
|
|
1918
1495
|
len += r
|
|
1919
|
-
if (r === 0
|
|
1920
|
-
|
|
1496
|
+
if (r === 0) break
|
|
1497
|
+
buffers.push(buffer.subarray(0, r))
|
|
1921
1498
|
}
|
|
1922
1499
|
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1500
|
+
buffer = Buffer.concat(buffers)
|
|
1501
|
+
} else {
|
|
1502
|
+
buffer = Buffer.allocUnsafe(st.size)
|
|
1503
|
+
|
|
1504
|
+
while (true) {
|
|
1505
|
+
const r = await read(fd, len ? buffer.subarray(len) : buffer)
|
|
1506
|
+
len += r
|
|
1507
|
+
if (r === 0 || len === buffer.byteLength) break
|
|
1930
1508
|
}
|
|
1931
|
-
})
|
|
1932
1509
|
|
|
1933
|
-
|
|
1934
|
-
close(fd, function () {
|
|
1935
|
-
cb(err)
|
|
1936
|
-
})
|
|
1510
|
+
if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
|
|
1937
1511
|
}
|
|
1938
|
-
})
|
|
1939
|
-
}
|
|
1940
1512
|
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1513
|
+
if (encoding !== 'buffer') buffer = buffer.toString(encoding)
|
|
1514
|
+
|
|
1515
|
+
return ok(buffer, cb)
|
|
1516
|
+
} catch (err) {
|
|
1517
|
+
fail(err, cb)
|
|
1518
|
+
} finally {
|
|
1519
|
+
if (fd === -1) return
|
|
1520
|
+
try {
|
|
1521
|
+
await close(fd)
|
|
1522
|
+
} catch {}
|
|
1951
1523
|
}
|
|
1524
|
+
}
|
|
1952
1525
|
|
|
1526
|
+
function readFileSync(filepath, opts) {
|
|
1953
1527
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
1954
1528
|
else if (!opts) opts = {}
|
|
1955
1529
|
|
|
1956
1530
|
const { encoding = 'buffer' } = opts
|
|
1957
1531
|
|
|
1958
|
-
|
|
1959
|
-
|
|
1532
|
+
let fd = -1
|
|
1960
1533
|
try {
|
|
1534
|
+
fd = openSync(filepath, opts.flag || 'r')
|
|
1535
|
+
|
|
1961
1536
|
const st = fstatSync(fd)
|
|
1962
1537
|
|
|
1963
|
-
let buffer
|
|
1538
|
+
let buffer
|
|
1964
1539
|
let len = 0
|
|
1965
1540
|
|
|
1966
|
-
|
|
1967
|
-
const
|
|
1968
|
-
|
|
1969
|
-
|
|
1541
|
+
if (st.size === 0) {
|
|
1542
|
+
const buffers = []
|
|
1543
|
+
|
|
1544
|
+
while (true) {
|
|
1545
|
+
buffer = Buffer.allocUnsafe(8192)
|
|
1546
|
+
const r = readSync(fd, buffer, 0, 8192)
|
|
1547
|
+
len += r
|
|
1548
|
+
if (r === 0) break
|
|
1549
|
+
buffers.push(buffer.subarray(0, r))
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
buffer = Buffer.concat(buffers)
|
|
1553
|
+
} else {
|
|
1554
|
+
buffer = Buffer.allocUnsafe(st.size)
|
|
1555
|
+
|
|
1556
|
+
while (true) {
|
|
1557
|
+
const r = readSync(fd, len ? buffer.subarray(len) : buffer)
|
|
1558
|
+
len += r
|
|
1559
|
+
if (r === 0 || len === buffer.byteLength) break
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
|
|
1970
1563
|
}
|
|
1971
1564
|
|
|
1972
|
-
if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
|
|
1973
1565
|
if (encoding !== 'buffer') buffer = buffer.toString(encoding)
|
|
1566
|
+
|
|
1974
1567
|
return buffer
|
|
1975
1568
|
} finally {
|
|
1569
|
+
if (fd === -1) return
|
|
1976
1570
|
try {
|
|
1977
1571
|
closeSync(fd)
|
|
1978
1572
|
} catch {}
|
|
1979
1573
|
}
|
|
1980
1574
|
}
|
|
1981
1575
|
|
|
1982
|
-
function writeFile(filepath, data, opts, cb) {
|
|
1983
|
-
if (typeof filepath !== 'string') {
|
|
1984
|
-
throw typeError(
|
|
1985
|
-
'ERR_INVALID_ARG_TYPE',
|
|
1986
|
-
'Path must be a string. Received type ' +
|
|
1987
|
-
typeof filepath +
|
|
1988
|
-
' (' +
|
|
1989
|
-
filepath +
|
|
1990
|
-
')'
|
|
1991
|
-
)
|
|
1992
|
-
}
|
|
1993
|
-
|
|
1994
|
-
if (
|
|
1995
|
-
typeof data !== 'string' &&
|
|
1996
|
-
!Buffer.isBuffer(data) &&
|
|
1997
|
-
!ArrayBuffer.isView(data)
|
|
1998
|
-
) {
|
|
1999
|
-
throw typeError(
|
|
2000
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2001
|
-
'Data must be a string or buffer. Received type ' + typeof data
|
|
2002
|
-
)
|
|
2003
|
-
}
|
|
2004
|
-
|
|
1576
|
+
async function writeFile(filepath, data, opts, cb) {
|
|
2005
1577
|
if (typeof opts === 'function') {
|
|
2006
1578
|
cb = opts
|
|
2007
1579
|
opts = {}
|
|
2008
|
-
} else if (typeof cb !== 'function') {
|
|
2009
|
-
throw typeError(
|
|
2010
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2011
|
-
'Callback must be a function. Received type ' +
|
|
2012
|
-
typeof cb +
|
|
2013
|
-
' (' +
|
|
2014
|
-
cb +
|
|
2015
|
-
')'
|
|
2016
|
-
)
|
|
2017
1580
|
}
|
|
2018
1581
|
|
|
2019
1582
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
@@ -2021,63 +1584,38 @@ function writeFile(filepath, data, opts, cb) {
|
|
|
2021
1584
|
|
|
2022
1585
|
if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
|
|
2023
1586
|
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
write(fd, data, loop)
|
|
1587
|
+
let fd = -1
|
|
1588
|
+
try {
|
|
1589
|
+
fd = await open(filepath, opts.flag || 'w', opts.mode || 0o666)
|
|
2028
1590
|
|
|
2029
|
-
|
|
2030
|
-
if (err) return closeAndError(err)
|
|
2031
|
-
if (w === data.byteLength) return done()
|
|
2032
|
-
write(fd, data.subarray(w), loop)
|
|
2033
|
-
}
|
|
1591
|
+
let len = 0
|
|
2034
1592
|
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
return cb(null)
|
|
2039
|
-
})
|
|
1593
|
+
while (true) {
|
|
1594
|
+
len += await write(fd, len ? data.subarray(len) : data)
|
|
1595
|
+
if (len === data.byteLength) break
|
|
2040
1596
|
}
|
|
2041
1597
|
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
1598
|
+
return ok(cb)
|
|
1599
|
+
} catch (err) {
|
|
1600
|
+
fail(err, cb)
|
|
1601
|
+
} finally {
|
|
1602
|
+
if (fd === -1) return
|
|
1603
|
+
try {
|
|
1604
|
+
await close(fd)
|
|
1605
|
+
} catch {}
|
|
1606
|
+
}
|
|
2048
1607
|
}
|
|
2049
1608
|
|
|
2050
1609
|
function writeFileSync(filepath, data, opts) {
|
|
2051
|
-
if (typeof filepath !== 'string') {
|
|
2052
|
-
throw typeError(
|
|
2053
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2054
|
-
'Path must be a string. Received type ' +
|
|
2055
|
-
typeof filepath +
|
|
2056
|
-
' (' +
|
|
2057
|
-
filepath +
|
|
2058
|
-
')'
|
|
2059
|
-
)
|
|
2060
|
-
}
|
|
2061
|
-
|
|
2062
|
-
if (
|
|
2063
|
-
typeof data !== 'string' &&
|
|
2064
|
-
!Buffer.isBuffer(data) &&
|
|
2065
|
-
!ArrayBuffer.isView(data)
|
|
2066
|
-
) {
|
|
2067
|
-
throw typeError(
|
|
2068
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2069
|
-
'Data must be a string or buffer. Received type ' + typeof data
|
|
2070
|
-
)
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
1610
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
2074
1611
|
else if (!opts) opts = {}
|
|
2075
1612
|
|
|
2076
1613
|
if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
|
|
2077
1614
|
|
|
2078
|
-
|
|
2079
|
-
|
|
1615
|
+
let fd = -1
|
|
2080
1616
|
try {
|
|
1617
|
+
fd = openSync(filepath, opts.flag || 'w', opts.mode || 0o666)
|
|
1618
|
+
|
|
2081
1619
|
let len = 0
|
|
2082
1620
|
|
|
2083
1621
|
while (true) {
|
|
@@ -2085,6 +1623,7 @@ function writeFileSync(filepath, data, opts) {
|
|
|
2085
1623
|
if (len === data.byteLength) break
|
|
2086
1624
|
}
|
|
2087
1625
|
} finally {
|
|
1626
|
+
if (fd === -1) return
|
|
2088
1627
|
try {
|
|
2089
1628
|
closeSync(fd)
|
|
2090
1629
|
} catch {}
|
|
@@ -2092,97 +1631,29 @@ function writeFileSync(filepath, data, opts) {
|
|
|
2092
1631
|
}
|
|
2093
1632
|
|
|
2094
1633
|
function appendFile(filepath, data, opts, cb) {
|
|
2095
|
-
if (typeof filepath !== 'string') {
|
|
2096
|
-
throw typeError(
|
|
2097
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2098
|
-
'Path must be a string. Received type ' +
|
|
2099
|
-
typeof filepath +
|
|
2100
|
-
' (' +
|
|
2101
|
-
filepath +
|
|
2102
|
-
')'
|
|
2103
|
-
)
|
|
2104
|
-
}
|
|
2105
|
-
|
|
2106
|
-
if (
|
|
2107
|
-
typeof data !== 'string' &&
|
|
2108
|
-
!Buffer.isBuffer(data) &&
|
|
2109
|
-
!ArrayBuffer.isView(data)
|
|
2110
|
-
) {
|
|
2111
|
-
throw typeError(
|
|
2112
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2113
|
-
'Data must be a string or buffer. Received type ' + typeof data
|
|
2114
|
-
)
|
|
2115
|
-
}
|
|
2116
|
-
|
|
2117
1634
|
if (typeof opts === 'function') {
|
|
2118
1635
|
cb = opts
|
|
2119
1636
|
opts = {}
|
|
2120
|
-
} else if (typeof cb !== 'function') {
|
|
2121
|
-
throw typeError(
|
|
2122
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2123
|
-
'Callback must be a function. Received type ' +
|
|
2124
|
-
typeof cb +
|
|
2125
|
-
' (' +
|
|
2126
|
-
cb +
|
|
2127
|
-
')'
|
|
2128
|
-
)
|
|
2129
1637
|
}
|
|
2130
1638
|
|
|
2131
1639
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
2132
1640
|
else if (!opts) opts = {}
|
|
2133
1641
|
|
|
2134
|
-
opts = { ...opts }
|
|
2135
|
-
|
|
2136
|
-
if (!opts.flags) opts.flag = 'a'
|
|
1642
|
+
if (!opts.flag) opts = { ...opts, flag: 'a' }
|
|
2137
1643
|
|
|
2138
1644
|
return writeFile(filepath, data, opts, cb)
|
|
2139
1645
|
}
|
|
2140
1646
|
|
|
2141
1647
|
function appendFileSync(filepath, data, opts) {
|
|
2142
|
-
if (typeof filepath !== 'string') {
|
|
2143
|
-
throw typeError(
|
|
2144
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2145
|
-
'Path must be a string. Received type ' +
|
|
2146
|
-
typeof filepath +
|
|
2147
|
-
' (' +
|
|
2148
|
-
filepath +
|
|
2149
|
-
')'
|
|
2150
|
-
)
|
|
2151
|
-
}
|
|
2152
|
-
|
|
2153
|
-
if (
|
|
2154
|
-
typeof data !== 'string' &&
|
|
2155
|
-
!Buffer.isBuffer(data) &&
|
|
2156
|
-
!ArrayBuffer.isView(data)
|
|
2157
|
-
) {
|
|
2158
|
-
throw typeError(
|
|
2159
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2160
|
-
'Data must be a string or buffer. Received type ' + typeof data
|
|
2161
|
-
)
|
|
2162
|
-
}
|
|
2163
|
-
|
|
2164
1648
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
2165
1649
|
else if (!opts) opts = {}
|
|
2166
1650
|
|
|
2167
|
-
opts = { ...opts }
|
|
2168
|
-
|
|
2169
|
-
if (!opts.flags) opts.flag = 'a'
|
|
1651
|
+
if (!opts.flag) opts = { ...opts, flag: 'a' }
|
|
2170
1652
|
|
|
2171
1653
|
return writeFileSync(filepath, data, opts)
|
|
2172
1654
|
}
|
|
2173
1655
|
|
|
2174
1656
|
function watch(filepath, opts, cb) {
|
|
2175
|
-
if (typeof filepath !== 'string') {
|
|
2176
|
-
throw typeError(
|
|
2177
|
-
'ERR_INVALID_ARG_TYPE',
|
|
2178
|
-
'Path must be a string. Received type ' +
|
|
2179
|
-
typeof filepath +
|
|
2180
|
-
' (' +
|
|
2181
|
-
filepath +
|
|
2182
|
-
')'
|
|
2183
|
-
)
|
|
2184
|
-
}
|
|
2185
|
-
|
|
2186
1657
|
if (typeof opts === 'function') {
|
|
2187
1658
|
cb = opts
|
|
2188
1659
|
opts = {}
|
|
@@ -2191,9 +1662,9 @@ function watch(filepath, opts, cb) {
|
|
|
2191
1662
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
2192
1663
|
else if (!opts) opts = {}
|
|
2193
1664
|
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
return
|
|
1665
|
+
filepath = toNamespacedPath(filepath)
|
|
1666
|
+
|
|
1667
|
+
return new Watcher(filepath, opts, cb)
|
|
2197
1668
|
}
|
|
2198
1669
|
|
|
2199
1670
|
class Stats {
|
|
@@ -2266,79 +1737,134 @@ class Dir {
|
|
|
2266
1737
|
constructor(path, handle, opts = {}) {
|
|
2267
1738
|
const { encoding = 'utf8', bufferSize = 32 } = opts
|
|
2268
1739
|
|
|
2269
|
-
this.
|
|
2270
|
-
|
|
1740
|
+
this.path = path
|
|
1741
|
+
|
|
2271
1742
|
this._encoding = encoding
|
|
1743
|
+
this._capacity = bufferSize
|
|
2272
1744
|
this._buffer = []
|
|
2273
1745
|
this._ended = false
|
|
1746
|
+
this._handle = handle
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
async read(cb) {
|
|
1750
|
+
if (this._buffer.length) return ok(this._buffer.shift(), cb)
|
|
1751
|
+
if (this._ended) return ok(null, cb)
|
|
1752
|
+
|
|
1753
|
+
const req = new FileRequest()
|
|
1754
|
+
|
|
1755
|
+
let entries
|
|
1756
|
+
let err = null
|
|
1757
|
+
try {
|
|
1758
|
+
req.retain(binding.readdir(req.handle, this._handle, this._capacity))
|
|
1759
|
+
|
|
1760
|
+
await req
|
|
1761
|
+
|
|
1762
|
+
entries = binding.requestResultDirents(req.handle)
|
|
1763
|
+
} catch (e) {
|
|
1764
|
+
err = new FileError(e.message, {
|
|
1765
|
+
operation: 'readdir',
|
|
1766
|
+
code: e.code,
|
|
1767
|
+
path: this.path
|
|
1768
|
+
})
|
|
1769
|
+
} finally {
|
|
1770
|
+
req.destroy()
|
|
1771
|
+
}
|
|
2274
1772
|
|
|
2275
|
-
|
|
2276
|
-
}
|
|
1773
|
+
if (err) return fail(err, cb)
|
|
2277
1774
|
|
|
2278
|
-
|
|
2279
|
-
|
|
1775
|
+
if (entries.length === 0) {
|
|
1776
|
+
this._ended = true
|
|
2280
1777
|
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
if (this._ended) return queueMicrotask(() => cb(null, null))
|
|
1778
|
+
return ok(null, cb)
|
|
1779
|
+
}
|
|
2284
1780
|
|
|
2285
|
-
const
|
|
2286
|
-
|
|
1781
|
+
for (const entry of entries) {
|
|
1782
|
+
let name = Buffer.from(entry.name)
|
|
2287
1783
|
|
|
2288
|
-
|
|
2289
|
-
if (err) return cb(err, null)
|
|
2290
|
-
if (data.length === 0) this._ended = true
|
|
2291
|
-
else {
|
|
2292
|
-
for (const entry of data) {
|
|
2293
|
-
let name = Buffer.from(entry.name)
|
|
2294
|
-
if (this._encoding !== 'buffer') name = name.toString(this._encoding)
|
|
2295
|
-
this._buffer.push(new Dirent(this.path, name, entry.type))
|
|
2296
|
-
}
|
|
2297
|
-
}
|
|
1784
|
+
if (this._encoding !== 'buffer') name = name.toString(this._encoding)
|
|
2298
1785
|
|
|
2299
|
-
|
|
2300
|
-
cb(null, this._buffer.shift())
|
|
1786
|
+
this._buffer.push(new Dirent(this.path, name, entry.type))
|
|
2301
1787
|
}
|
|
2302
1788
|
|
|
2303
|
-
|
|
1789
|
+
return ok(this._buffer.shift(), cb)
|
|
2304
1790
|
}
|
|
2305
1791
|
|
|
2306
1792
|
readSync() {
|
|
2307
1793
|
if (this._buffer.length) return this._buffer.shift()
|
|
2308
1794
|
if (this._ended) return null
|
|
2309
1795
|
|
|
2310
|
-
const
|
|
1796
|
+
const req = new FileRequest()
|
|
1797
|
+
|
|
1798
|
+
let entries
|
|
1799
|
+
try {
|
|
1800
|
+
req.retain(binding.readdirSync(req.handle, this._handle, this._capacity))
|
|
1801
|
+
|
|
1802
|
+
entries = binding.requestResultDirents(req.handle)
|
|
1803
|
+
} catch (e) {
|
|
1804
|
+
throw new FileError(e.message, {
|
|
1805
|
+
operation: 'readdir',
|
|
1806
|
+
code: e.code,
|
|
1807
|
+
path: this.path
|
|
1808
|
+
})
|
|
1809
|
+
} finally {
|
|
1810
|
+
req.destroy()
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
if (entries.length === 0) {
|
|
1814
|
+
this._ended = true
|
|
2311
1815
|
|
|
2312
|
-
|
|
1816
|
+
return null
|
|
1817
|
+
}
|
|
2313
1818
|
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
}
|
|
1819
|
+
for (const entry of entries) {
|
|
1820
|
+
let name = Buffer.from(entry.name)
|
|
1821
|
+
|
|
1822
|
+
if (this._encoding !== 'buffer') name = name.toString(this._encoding)
|
|
1823
|
+
|
|
1824
|
+
this._buffer.push(new Dirent(this.path, name, entry.type))
|
|
2321
1825
|
}
|
|
2322
1826
|
|
|
2323
|
-
if (this._ended) return null
|
|
2324
1827
|
return this._buffer.shift()
|
|
2325
1828
|
}
|
|
2326
1829
|
|
|
2327
|
-
close(cb) {
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
const req = getReq()
|
|
1830
|
+
async close(cb) {
|
|
1831
|
+
const req = new FileRequest()
|
|
2331
1832
|
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
1833
|
+
let err = null
|
|
1834
|
+
try {
|
|
1835
|
+
binding.closedir(req.handle, this._handle)
|
|
1836
|
+
|
|
1837
|
+
await req
|
|
1838
|
+
} catch (e) {
|
|
1839
|
+
err = new FileError(e.message, {
|
|
1840
|
+
operation: 'closedir',
|
|
1841
|
+
code: e.code,
|
|
1842
|
+
path: this.path
|
|
1843
|
+
})
|
|
1844
|
+
} finally {
|
|
1845
|
+
req.destroy()
|
|
2335
1846
|
}
|
|
2336
1847
|
|
|
2337
|
-
|
|
1848
|
+
this._handle = null
|
|
1849
|
+
|
|
1850
|
+
return done(err, cb)
|
|
2338
1851
|
}
|
|
2339
1852
|
|
|
2340
1853
|
closeSync() {
|
|
2341
|
-
|
|
1854
|
+
const req = new FileRequest()
|
|
1855
|
+
|
|
1856
|
+
try {
|
|
1857
|
+
binding.closedirSync(req.handle, this._handle)
|
|
1858
|
+
} catch (e) {
|
|
1859
|
+
throw new FileError(e.message, {
|
|
1860
|
+
operation: 'closedir',
|
|
1861
|
+
code: e.code,
|
|
1862
|
+
path: this.path
|
|
1863
|
+
})
|
|
1864
|
+
} finally {
|
|
1865
|
+
req.destroy()
|
|
1866
|
+
}
|
|
1867
|
+
|
|
2342
1868
|
this._handle = null
|
|
2343
1869
|
}
|
|
2344
1870
|
|
|
@@ -2368,26 +1894,25 @@ class Dir {
|
|
|
2368
1894
|
|
|
2369
1895
|
[Symbol.asyncIterator]() {
|
|
2370
1896
|
return {
|
|
2371
|
-
next: () =>
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
}
|
|
1897
|
+
next: async () => {
|
|
1898
|
+
if (this._buffer.length) {
|
|
1899
|
+
return { done: false, value: this._buffer.shift() }
|
|
1900
|
+
}
|
|
2376
1901
|
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
1902
|
+
if (this._ended) {
|
|
1903
|
+
return { done: true }
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
const entry = await this.read()
|
|
2380
1907
|
|
|
2381
|
-
|
|
2382
|
-
|
|
1908
|
+
if (entry) {
|
|
1909
|
+
return { done: false, value: entry }
|
|
1910
|
+
}
|
|
2383
1911
|
|
|
2384
|
-
|
|
2385
|
-
return resolve({ done: false, value: entry })
|
|
2386
|
-
}
|
|
1912
|
+
await this.close()
|
|
2387
1913
|
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
})
|
|
1914
|
+
return { done: true }
|
|
1915
|
+
}
|
|
2391
1916
|
}
|
|
2392
1917
|
}
|
|
2393
1918
|
}
|
|
@@ -2428,44 +1953,14 @@ class Dirent {
|
|
|
2428
1953
|
}
|
|
2429
1954
|
}
|
|
2430
1955
|
|
|
2431
|
-
class FileWriteStream extends Writable {
|
|
2432
|
-
constructor(path, opts = {}) {
|
|
2433
|
-
super({ map })
|
|
2434
|
-
|
|
2435
|
-
this.path = path
|
|
2436
|
-
this.fd = 0
|
|
2437
|
-
this.flags = opts.flags || 'w'
|
|
2438
|
-
this.mode = opts.mode || 0o666
|
|
2439
|
-
}
|
|
2440
|
-
|
|
2441
|
-
_open(cb) {
|
|
2442
|
-
open(this.path, this.flags, this.mode, (err, fd) => {
|
|
2443
|
-
if (err) return cb(err)
|
|
2444
|
-
this.fd = fd
|
|
2445
|
-
cb(null)
|
|
2446
|
-
})
|
|
2447
|
-
}
|
|
2448
|
-
|
|
2449
|
-
_writev(batch, cb) {
|
|
2450
|
-
writev(
|
|
2451
|
-
this.fd,
|
|
2452
|
-
batch.map(({ chunk }) => chunk),
|
|
2453
|
-
cb
|
|
2454
|
-
)
|
|
2455
|
-
}
|
|
2456
|
-
|
|
2457
|
-
_destroy(err, cb) {
|
|
2458
|
-
if (!this.fd) return cb(err)
|
|
2459
|
-
close(this.fd, () => cb(err))
|
|
2460
|
-
}
|
|
2461
|
-
}
|
|
2462
|
-
|
|
2463
1956
|
class FileReadStream extends Readable {
|
|
2464
1957
|
constructor(path, opts = {}) {
|
|
2465
1958
|
super()
|
|
2466
1959
|
|
|
2467
1960
|
this.path = path
|
|
2468
|
-
this.fd =
|
|
1961
|
+
this.fd = typeof opts.fd === 'number' ? opts.fd : -1
|
|
1962
|
+
this.flags = opts.flags || 'r'
|
|
1963
|
+
this.mode = opts.mode || 0o666
|
|
2469
1964
|
|
|
2470
1965
|
this._offset = opts.start || 0
|
|
2471
1966
|
this._missing = 0
|
|
@@ -2479,67 +1974,141 @@ class FileReadStream extends Readable {
|
|
|
2479
1974
|
}
|
|
2480
1975
|
}
|
|
2481
1976
|
|
|
2482
|
-
_open(cb) {
|
|
2483
|
-
|
|
1977
|
+
async _open(cb) {
|
|
1978
|
+
let err
|
|
1979
|
+
|
|
1980
|
+
if (this.fd === -1) {
|
|
1981
|
+
err = null
|
|
1982
|
+
try {
|
|
1983
|
+
this.fd = await open(this.path, this.flags, this.mode)
|
|
1984
|
+
} catch (e) {
|
|
1985
|
+
err = e
|
|
1986
|
+
}
|
|
1987
|
+
|
|
2484
1988
|
if (err) return cb(err)
|
|
1989
|
+
}
|
|
2485
1990
|
|
|
2486
|
-
|
|
1991
|
+
let st
|
|
1992
|
+
err = null
|
|
1993
|
+
try {
|
|
1994
|
+
st = await fstat(this.fd)
|
|
1995
|
+
} catch (e) {
|
|
1996
|
+
err = e
|
|
1997
|
+
}
|
|
2487
1998
|
|
|
2488
|
-
|
|
2489
|
-
if (err) return onerror(err)
|
|
2490
|
-
if (!st.isFile())
|
|
2491
|
-
return onerror(new Error(this.path + ' is not a file'))
|
|
1999
|
+
if (err) return cb(err)
|
|
2492
2000
|
|
|
2493
|
-
|
|
2494
|
-
if (this._missing === -1) this._missing = st.size
|
|
2001
|
+
if (this._missing === -1) this._missing = st.size
|
|
2495
2002
|
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
this._missing = st.size - this._offset
|
|
2503
|
-
return cb(null)
|
|
2504
|
-
}
|
|
2003
|
+
if (st.size < this._offset) {
|
|
2004
|
+
this._offset = st.size
|
|
2005
|
+
this._missing = 0
|
|
2006
|
+
} else if (st.size < this._offset + this._missing) {
|
|
2007
|
+
this._missing = st.size - this._offset
|
|
2008
|
+
}
|
|
2505
2009
|
|
|
2506
|
-
|
|
2507
|
-
})
|
|
2508
|
-
})
|
|
2010
|
+
cb(null)
|
|
2509
2011
|
}
|
|
2510
2012
|
|
|
2511
|
-
_read(size) {
|
|
2512
|
-
if (
|
|
2513
|
-
this.push(null)
|
|
2514
|
-
return
|
|
2515
|
-
}
|
|
2013
|
+
async _read(size) {
|
|
2014
|
+
if (this._missing <= 0) return this.push(null)
|
|
2516
2015
|
|
|
2517
2016
|
const data = Buffer.allocUnsafe(Math.min(this._missing, size))
|
|
2518
2017
|
|
|
2519
|
-
|
|
2520
|
-
|
|
2018
|
+
let len
|
|
2019
|
+
let err = null
|
|
2020
|
+
try {
|
|
2021
|
+
len = await read(this.fd, data, 0, data.byteLength, this._offset)
|
|
2022
|
+
} catch (e) {
|
|
2023
|
+
err = e
|
|
2024
|
+
}
|
|
2521
2025
|
|
|
2522
|
-
|
|
2523
|
-
this.push(null)
|
|
2524
|
-
return
|
|
2525
|
-
}
|
|
2026
|
+
if (err) return cb(err)
|
|
2526
2027
|
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2028
|
+
if (len === 0) return this.push(null)
|
|
2029
|
+
|
|
2030
|
+
if (this._missing < len) len = this._missing
|
|
2031
|
+
|
|
2032
|
+
this.push(data.subarray(0, len))
|
|
2033
|
+
|
|
2034
|
+
this._missing -= len
|
|
2035
|
+
this._offset += read
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
async _destroy(err, cb) {
|
|
2039
|
+
if (this.fd === -1) return cb(err)
|
|
2040
|
+
|
|
2041
|
+
err = null
|
|
2042
|
+
try {
|
|
2043
|
+
await close(this.fd)
|
|
2044
|
+
} catch (e) {
|
|
2045
|
+
err = e
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
cb(err)
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
class FileWriteStream extends Writable {
|
|
2053
|
+
constructor(path, opts = {}) {
|
|
2054
|
+
super({ map })
|
|
2055
|
+
|
|
2056
|
+
this.path = path
|
|
2057
|
+
this.fd = typeof opts.fd === 'number' ? opts.fd : -1
|
|
2058
|
+
this.flags = opts.flags || 'w'
|
|
2059
|
+
this.mode = opts.mode || 0o666
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
async _open(cb) {
|
|
2063
|
+
if (this.fd !== -1) return cb(null)
|
|
2064
|
+
|
|
2065
|
+
let err = null
|
|
2066
|
+
try {
|
|
2067
|
+
this.fd = await open(this.path, this.flags, this.mode)
|
|
2068
|
+
} catch (e) {
|
|
2069
|
+
err = e
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
cb(err)
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
async _writev(batch, cb) {
|
|
2076
|
+
let err = null
|
|
2077
|
+
try {
|
|
2078
|
+
await writev(
|
|
2079
|
+
this.fd,
|
|
2080
|
+
batch.map(({ chunk }) => chunk)
|
|
2081
|
+
)
|
|
2082
|
+
} catch (e) {
|
|
2083
|
+
err = e
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
cb(err)
|
|
2533
2087
|
}
|
|
2534
2088
|
|
|
2535
|
-
_destroy(err, cb) {
|
|
2536
|
-
if (
|
|
2537
|
-
|
|
2089
|
+
async _destroy(err, cb) {
|
|
2090
|
+
if (this.fd === -1) return cb(err)
|
|
2091
|
+
|
|
2092
|
+
err = null
|
|
2093
|
+
try {
|
|
2094
|
+
await close(this.fd)
|
|
2095
|
+
} catch (e) {
|
|
2096
|
+
err = e
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
cb(err)
|
|
2538
2100
|
}
|
|
2539
2101
|
}
|
|
2540
2102
|
|
|
2541
2103
|
class Watcher extends EventEmitter {
|
|
2542
|
-
constructor(path, opts) {
|
|
2104
|
+
constructor(path, opts, onchange) {
|
|
2105
|
+
if (typeof opts === 'function') {
|
|
2106
|
+
onchange = opts
|
|
2107
|
+
opts = {}
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
if (!opts) opts = {}
|
|
2111
|
+
|
|
2543
2112
|
const { persistent = true, recursive = false, encoding = 'utf8' } = opts
|
|
2544
2113
|
|
|
2545
2114
|
super()
|
|
@@ -2555,32 +2124,8 @@ class Watcher extends EventEmitter {
|
|
|
2555
2124
|
)
|
|
2556
2125
|
|
|
2557
2126
|
if (!persistent) this.unref()
|
|
2558
|
-
}
|
|
2559
|
-
|
|
2560
|
-
_onevent(err, events, filename) {
|
|
2561
|
-
if (err) {
|
|
2562
|
-
this.close()
|
|
2563
|
-
this.emit('error', err)
|
|
2564
|
-
} else {
|
|
2565
|
-
const path =
|
|
2566
|
-
this._encoding === 'buffer'
|
|
2567
|
-
? Buffer.from(filename)
|
|
2568
|
-
: Buffer.from(filename).toString(this._encoding)
|
|
2569
|
-
|
|
2570
|
-
if (events & binding.UV_RENAME) {
|
|
2571
|
-
this.emit('change', 'rename', path)
|
|
2572
|
-
}
|
|
2573
|
-
|
|
2574
|
-
if (events & binding.UV_CHANGE) {
|
|
2575
|
-
this.emit('change', 'change', path)
|
|
2576
|
-
}
|
|
2577
|
-
}
|
|
2578
|
-
}
|
|
2579
2127
|
|
|
2580
|
-
|
|
2581
|
-
this._handle = null
|
|
2582
|
-
|
|
2583
|
-
this.emit('close')
|
|
2128
|
+
if (onchange) this.on('change', onchange)
|
|
2584
2129
|
}
|
|
2585
2130
|
|
|
2586
2131
|
close() {
|
|
@@ -2646,17 +2191,33 @@ class Watcher extends EventEmitter {
|
|
|
2646
2191
|
})
|
|
2647
2192
|
}
|
|
2648
2193
|
}
|
|
2649
|
-
}
|
|
2650
2194
|
|
|
2651
|
-
|
|
2195
|
+
_onevent(err, events, filename) {
|
|
2196
|
+
if (err) {
|
|
2197
|
+
this.close()
|
|
2198
|
+
this.emit('error', err)
|
|
2199
|
+
} else {
|
|
2200
|
+
const path =
|
|
2201
|
+
this._encoding === 'buffer'
|
|
2202
|
+
? Buffer.from(filename)
|
|
2203
|
+
: Buffer.from(filename).toString(this._encoding)
|
|
2204
|
+
|
|
2205
|
+
if (events & binding.UV_RENAME) {
|
|
2206
|
+
this.emit('change', 'rename', path)
|
|
2207
|
+
}
|
|
2652
2208
|
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
}
|
|
2209
|
+
if (events & binding.UV_CHANGE) {
|
|
2210
|
+
this.emit('change', 'change', path)
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2658
2214
|
|
|
2659
|
-
|
|
2215
|
+
_onclose() {
|
|
2216
|
+
this._handle = null
|
|
2217
|
+
|
|
2218
|
+
this.emit('close')
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2660
2221
|
|
|
2661
2222
|
exports.access = access
|
|
2662
2223
|
exports.appendFile = appendFile
|
|
@@ -2706,6 +2267,7 @@ exports.readFileSync = readFileSync
|
|
|
2706
2267
|
exports.readSync = readSync
|
|
2707
2268
|
exports.readdirSync = readdirSync
|
|
2708
2269
|
exports.readlinkSync = readlinkSync
|
|
2270
|
+
exports.readvSync = readvSync
|
|
2709
2271
|
exports.realpathSync = realpathSync
|
|
2710
2272
|
exports.renameSync = renameSync
|
|
2711
2273
|
exports.rmSync = rmSync
|
|
@@ -2716,28 +2278,9 @@ exports.unlinkSync = unlinkSync
|
|
|
2716
2278
|
exports.utimesSync = utimesSync
|
|
2717
2279
|
exports.writeFileSync = writeFileSync
|
|
2718
2280
|
exports.writeSync = writeSync
|
|
2281
|
+
exports.writevSync = writevSync
|
|
2719
2282
|
|
|
2720
|
-
exports.promises
|
|
2721
|
-
exports.promises.appendFile = promisify(appendFile)
|
|
2722
|
-
exports.promises.chmod = promisify(chmod)
|
|
2723
|
-
exports.promises.copyFile = promisify(copyFile)
|
|
2724
|
-
exports.promises.lstat = promisify(lstat)
|
|
2725
|
-
exports.promises.mkdir = promisify(mkdir)
|
|
2726
|
-
exports.promises.opendir = promisify(opendir)
|
|
2727
|
-
exports.promises.readFile = promisify(readFile)
|
|
2728
|
-
exports.promises.readdir = promisify(readdir)
|
|
2729
|
-
exports.promises.readlink = promisify(readlink)
|
|
2730
|
-
exports.promises.realpath = promisify(realpath)
|
|
2731
|
-
exports.promises.rename = promisify(rename)
|
|
2732
|
-
exports.promises.rm = promisify(rm)
|
|
2733
|
-
exports.promises.rmdir = promisify(rmdir)
|
|
2734
|
-
exports.promises.stat = promisify(stat)
|
|
2735
|
-
exports.promises.symlink = promisify(symlink)
|
|
2736
|
-
exports.promises.unlink = promisify(unlink)
|
|
2737
|
-
exports.promises.utimes = promisify(utimes)
|
|
2738
|
-
exports.promises.writeFile = promisify(writeFile)
|
|
2739
|
-
|
|
2740
|
-
exports.promises.watch = watch // Already async iterable
|
|
2283
|
+
exports.promises = require('./promises')
|
|
2741
2284
|
|
|
2742
2285
|
exports.Stats = Stats
|
|
2743
2286
|
exports.Dir = Dir
|
|
@@ -2745,30 +2288,107 @@ exports.Dirent = Dirent
|
|
|
2745
2288
|
exports.Watcher = Watcher
|
|
2746
2289
|
|
|
2747
2290
|
exports.ReadStream = FileReadStream
|
|
2291
|
+
|
|
2748
2292
|
exports.createReadStream = function createReadStream(path, opts) {
|
|
2749
2293
|
return new FileReadStream(path, opts)
|
|
2750
2294
|
}
|
|
2751
2295
|
|
|
2752
2296
|
exports.WriteStream = FileWriteStream
|
|
2297
|
+
|
|
2753
2298
|
exports.createWriteStream = function createWriteStream(path, opts) {
|
|
2754
2299
|
return new FileWriteStream(path, opts)
|
|
2755
2300
|
}
|
|
2756
2301
|
|
|
2757
|
-
function promisify(fn) {
|
|
2758
|
-
return function (...args) {
|
|
2759
|
-
return new Promise((resolve, reject) => {
|
|
2760
|
-
fn(...args, function (err, res) {
|
|
2761
|
-
if (err) return reject(err)
|
|
2762
|
-
resolve(res)
|
|
2763
|
-
})
|
|
2764
|
-
})
|
|
2765
|
-
}
|
|
2766
|
-
}
|
|
2767
|
-
|
|
2768
2302
|
function map(data) {
|
|
2769
2303
|
return typeof data === 'string' ? Buffer.from(data) : data
|
|
2770
2304
|
}
|
|
2771
2305
|
|
|
2772
2306
|
function toNamespacedPath(filepath) {
|
|
2307
|
+
if (typeof filepath !== 'string') {
|
|
2308
|
+
if (URL.isURL(filepath)) filepath = fileURLToPath(filepath)
|
|
2309
|
+
else filepath = filepath.toString()
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2773
2312
|
return path.toNamespacedPath(filepath)
|
|
2774
2313
|
}
|
|
2314
|
+
|
|
2315
|
+
function toFlags(flags) {
|
|
2316
|
+
switch (flags) {
|
|
2317
|
+
case 'r':
|
|
2318
|
+
return constants.O_RDONLY
|
|
2319
|
+
case 'rs': // Fall through.
|
|
2320
|
+
case 'sr':
|
|
2321
|
+
return constants.O_RDONLY | constants.O_SYNC
|
|
2322
|
+
case 'r+':
|
|
2323
|
+
return constants.O_RDWR
|
|
2324
|
+
case 'rs+': // Fall through.
|
|
2325
|
+
case 'sr+':
|
|
2326
|
+
return constants.O_RDWR | constants.O_SYNC
|
|
2327
|
+
|
|
2328
|
+
case 'w':
|
|
2329
|
+
return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
|
|
2330
|
+
case 'wx': // Fall through.
|
|
2331
|
+
case 'xw':
|
|
2332
|
+
return (
|
|
2333
|
+
constants.O_TRUNC |
|
|
2334
|
+
constants.O_CREAT |
|
|
2335
|
+
constants.O_WRONLY |
|
|
2336
|
+
constants.O_EXCL
|
|
2337
|
+
)
|
|
2338
|
+
|
|
2339
|
+
case 'w+':
|
|
2340
|
+
return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
|
|
2341
|
+
case 'wx+': // Fall through.
|
|
2342
|
+
case 'xw+':
|
|
2343
|
+
return (
|
|
2344
|
+
constants.O_TRUNC |
|
|
2345
|
+
constants.O_CREAT |
|
|
2346
|
+
constants.O_RDWR |
|
|
2347
|
+
constants.O_EXCL
|
|
2348
|
+
)
|
|
2349
|
+
|
|
2350
|
+
case 'a':
|
|
2351
|
+
return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
|
|
2352
|
+
case 'ax': // Fall through.
|
|
2353
|
+
case 'xa':
|
|
2354
|
+
return (
|
|
2355
|
+
constants.O_APPEND |
|
|
2356
|
+
constants.O_CREAT |
|
|
2357
|
+
constants.O_WRONLY |
|
|
2358
|
+
constants.O_EXCL
|
|
2359
|
+
)
|
|
2360
|
+
case 'as': // Fall through.
|
|
2361
|
+
case 'sa':
|
|
2362
|
+
return (
|
|
2363
|
+
constants.O_APPEND |
|
|
2364
|
+
constants.O_CREAT |
|
|
2365
|
+
constants.O_WRONLY |
|
|
2366
|
+
constants.O_SYNC
|
|
2367
|
+
)
|
|
2368
|
+
|
|
2369
|
+
case 'a+':
|
|
2370
|
+
return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
|
|
2371
|
+
case 'ax+': // Fall through.
|
|
2372
|
+
case 'xa+':
|
|
2373
|
+
return (
|
|
2374
|
+
constants.O_APPEND |
|
|
2375
|
+
constants.O_CREAT |
|
|
2376
|
+
constants.O_RDWR |
|
|
2377
|
+
constants.O_EXCL
|
|
2378
|
+
)
|
|
2379
|
+
case 'as+': // Fall through.
|
|
2380
|
+
case 'sa+':
|
|
2381
|
+
return (
|
|
2382
|
+
constants.O_APPEND |
|
|
2383
|
+
constants.O_CREAT |
|
|
2384
|
+
constants.O_RDWR |
|
|
2385
|
+
constants.O_SYNC
|
|
2386
|
+
)
|
|
2387
|
+
default:
|
|
2388
|
+
return 0
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
function toMode(mode) {
|
|
2393
|
+
return parseInt(mode, 8)
|
|
2394
|
+
}
|