bare-fs 1.5.5 → 1.6.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/README.md +16 -10
- package/binding.c +905 -376
- package/index.js +646 -236
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
const { Readable, Writable } = require('streamx')
|
|
2
2
|
const binding = require('./binding')
|
|
3
3
|
|
|
4
|
-
const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff
|
|
5
|
-
|
|
6
4
|
const sep = process.platform === 'win32' ? '\\' : '/'
|
|
7
5
|
|
|
8
6
|
const constants = exports.constants = {
|
|
@@ -12,6 +10,7 @@ const constants = exports.constants = {
|
|
|
12
10
|
O_CREAT: binding.O_CREAT,
|
|
13
11
|
O_TRUNC: binding.O_TRUNC,
|
|
14
12
|
O_APPEND: binding.O_APPEND,
|
|
13
|
+
|
|
15
14
|
S_IFMT: binding.S_IFMT,
|
|
16
15
|
S_IFREG: binding.S_IFREG,
|
|
17
16
|
S_IFDIR: binding.S_IFDIR,
|
|
@@ -19,7 +18,16 @@ const constants = exports.constants = {
|
|
|
19
18
|
S_IFLNK: binding.S_IFLNK,
|
|
20
19
|
S_IFBLK: binding.S_IFBLK || 0,
|
|
21
20
|
S_IFIFO: binding.S_IFIFO || 0,
|
|
22
|
-
S_IFSOCK: binding.S_IFSOCK || 0
|
|
21
|
+
S_IFSOCK: binding.S_IFSOCK || 0,
|
|
22
|
+
|
|
23
|
+
UV_DIRENT_UNKNOWN: binding.UV_DIRENT_UNKNOWN,
|
|
24
|
+
UV_DIRENT_FILE: binding.UV_DIRENT_FILE,
|
|
25
|
+
UV_DIRENT_DIR: binding.UV_DIRENT_DIR,
|
|
26
|
+
UV_DIRENT_LINK: binding.UV_DIRENT_LINK,
|
|
27
|
+
UV_DIRENT_FIFO: binding.UV_DIRENT_FIFO,
|
|
28
|
+
UV_DIRENT_SOCKET: binding.UV_DIRENT_SOCKET,
|
|
29
|
+
UV_DIRENT_CHAR: binding.UV_DIRENT_CHAR,
|
|
30
|
+
UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
const reqs = []
|
|
@@ -29,10 +37,14 @@ const fs = {
|
|
|
29
37
|
handle: Buffer.allocUnsafe(binding.sizeofFS)
|
|
30
38
|
}
|
|
31
39
|
|
|
32
|
-
binding.init(fs.handle, fs,
|
|
40
|
+
binding.init(fs.handle, fs, onresponse)
|
|
33
41
|
|
|
34
42
|
process.on('exit', () => binding.destroy(fs.handle))
|
|
35
43
|
|
|
44
|
+
if (process.thread) {
|
|
45
|
+
process.thread.on('exit', () => binding.destroy(fs.handle))
|
|
46
|
+
}
|
|
47
|
+
|
|
36
48
|
// Lightly-modified from the Node FS internal utils.
|
|
37
49
|
function flagsToNumber (flags) {
|
|
38
50
|
switch (flags) {
|
|
@@ -86,8 +98,6 @@ function alloc () {
|
|
|
86
98
|
handle,
|
|
87
99
|
view,
|
|
88
100
|
type: 0,
|
|
89
|
-
buffer: null,
|
|
90
|
-
buffers: null,
|
|
91
101
|
callback: null
|
|
92
102
|
}
|
|
93
103
|
|
|
@@ -100,7 +110,7 @@ function getReq () {
|
|
|
100
110
|
return used === reqs.length ? alloc() : reqs[used++]
|
|
101
111
|
}
|
|
102
112
|
|
|
103
|
-
function
|
|
113
|
+
function onresponse (id, result) {
|
|
104
114
|
const req = reqs[id]
|
|
105
115
|
used--
|
|
106
116
|
|
|
@@ -111,17 +121,13 @@ function onfsresponse (id, result) {
|
|
|
111
121
|
}
|
|
112
122
|
|
|
113
123
|
const callback = req.callback
|
|
114
|
-
const buffer = req.buffer
|
|
115
|
-
const buffers = req.buffers
|
|
116
124
|
|
|
117
125
|
req.callback = null
|
|
118
|
-
req.buffer = null
|
|
119
|
-
req.buffers = null
|
|
120
126
|
|
|
121
127
|
if (result < 0) {
|
|
122
128
|
callback(createError(result), result, null)
|
|
123
129
|
} else {
|
|
124
|
-
callback(null, result
|
|
130
|
+
callback(null, result)
|
|
125
131
|
}
|
|
126
132
|
}
|
|
127
133
|
|
|
@@ -135,249 +141,362 @@ function createError (errno) {
|
|
|
135
141
|
return err
|
|
136
142
|
}
|
|
137
143
|
|
|
138
|
-
function
|
|
139
|
-
if (typeof
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
req.buffer = buf
|
|
143
|
-
req.callback = cb
|
|
144
|
-
|
|
145
|
-
binding.write(req.handle, fd, buf, offset, len, pos)
|
|
146
|
-
return
|
|
144
|
+
function open (path, flags, mode, cb) {
|
|
145
|
+
if (typeof path !== 'string') {
|
|
146
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
if (typeof
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
if (typeof cb !== 'function') {
|
|
150
|
+
if (typeof flags === 'function') {
|
|
151
|
+
cb = flags
|
|
152
|
+
flags = 'r'
|
|
153
|
+
mode = 0o666
|
|
154
|
+
} else if (typeof mode === 'function') {
|
|
155
|
+
cb = mode
|
|
156
|
+
mode = 0o666
|
|
157
|
+
} else {
|
|
158
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
159
|
+
}
|
|
160
|
+
}
|
|
152
161
|
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
if (typeof flags === 'string') flags = flagsToNumber(flags)
|
|
163
|
+
if (typeof mode === 'string') mode = modeToNumber(mode)
|
|
155
164
|
|
|
156
|
-
|
|
157
|
-
|
|
165
|
+
const req = getReq()
|
|
166
|
+
req.callback = cb
|
|
167
|
+
binding.open(req.handle, path, flags, mode)
|
|
158
168
|
}
|
|
159
169
|
|
|
160
|
-
function
|
|
161
|
-
if (typeof
|
|
162
|
-
|
|
163
|
-
pos = 0
|
|
170
|
+
function openSync (path, flags = 'r', mode = 0o666) {
|
|
171
|
+
if (typeof path !== 'string') {
|
|
172
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
164
173
|
}
|
|
165
174
|
|
|
166
|
-
|
|
175
|
+
if (typeof flags === 'string') flags = flagsToNumber(flags)
|
|
176
|
+
if (typeof mode === 'string') mode = modeToNumber(mode)
|
|
167
177
|
|
|
168
|
-
|
|
169
|
-
|
|
178
|
+
const res = binding.openSync(path, flags, mode)
|
|
179
|
+
if (res < 0) throw createError(res)
|
|
170
180
|
|
|
171
|
-
|
|
181
|
+
return res
|
|
172
182
|
}
|
|
173
183
|
|
|
174
|
-
function
|
|
175
|
-
if (typeof
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
req.buffer = buf
|
|
179
|
-
req.callback = cb
|
|
180
|
-
|
|
181
|
-
binding.read(req.handle, fd, buf, offset, len, pos)
|
|
182
|
-
return
|
|
184
|
+
function close (fd, cb = noop) {
|
|
185
|
+
if (typeof fd !== 'number') {
|
|
186
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
183
187
|
}
|
|
184
188
|
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
189
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
190
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
191
|
+
}
|
|
188
192
|
|
|
189
|
-
|
|
190
|
-
|
|
193
|
+
if (typeof cb !== 'function') {
|
|
194
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
195
|
+
}
|
|
191
196
|
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
const req = getReq()
|
|
198
|
+
req.callback = cb
|
|
199
|
+
binding.close(req.handle, fd)
|
|
194
200
|
}
|
|
195
201
|
|
|
196
|
-
function
|
|
197
|
-
if (typeof
|
|
198
|
-
|
|
199
|
-
pos = 0
|
|
202
|
+
function closeSync (fd) {
|
|
203
|
+
if (typeof fd !== 'number') {
|
|
204
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
200
205
|
}
|
|
201
206
|
|
|
202
|
-
|
|
207
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
208
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
209
|
+
}
|
|
203
210
|
|
|
204
|
-
|
|
205
|
-
|
|
211
|
+
const res = binding.closeSync(fd)
|
|
212
|
+
if (res < 0) throw createError(res)
|
|
206
213
|
|
|
207
|
-
|
|
214
|
+
return res
|
|
208
215
|
}
|
|
209
216
|
|
|
210
|
-
function
|
|
211
|
-
if (typeof
|
|
217
|
+
function read (fd, buffer, offset, len, pos, cb) {
|
|
218
|
+
if (typeof fd !== 'number') {
|
|
219
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
220
|
+
}
|
|
212
221
|
|
|
213
|
-
if (
|
|
214
|
-
|
|
222
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
223
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
224
|
+
}
|
|
215
225
|
|
|
216
|
-
if (
|
|
226
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
227
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
|
|
228
|
+
}
|
|
217
229
|
|
|
218
|
-
if (typeof
|
|
219
|
-
|
|
230
|
+
if (typeof cb !== 'function') {
|
|
231
|
+
if (typeof offset === 'function') {
|
|
232
|
+
cb = offset
|
|
233
|
+
offset = 0
|
|
234
|
+
len = buffer.byteLength
|
|
235
|
+
pos = -1
|
|
236
|
+
} else if (typeof len === 'function') {
|
|
237
|
+
cb = len
|
|
238
|
+
len = buffer.byteLength - offset
|
|
239
|
+
pos = -1
|
|
240
|
+
} else if (typeof pos === 'function') {
|
|
241
|
+
cb = pos
|
|
242
|
+
pos = -1
|
|
243
|
+
} else {
|
|
244
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
245
|
+
}
|
|
246
|
+
}
|
|
220
247
|
|
|
221
|
-
|
|
248
|
+
if (typeof position !== 'number') pos = -1
|
|
222
249
|
|
|
250
|
+
const req = getReq()
|
|
223
251
|
req.callback = cb
|
|
224
|
-
binding.
|
|
252
|
+
binding.read(req.handle, fd, buffer, offset, len, pos)
|
|
225
253
|
}
|
|
226
254
|
|
|
227
|
-
function
|
|
228
|
-
if (typeof
|
|
255
|
+
function readSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
|
|
256
|
+
if (typeof fd !== 'number') {
|
|
257
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
258
|
+
}
|
|
229
259
|
|
|
230
|
-
if (
|
|
231
|
-
|
|
260
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
261
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
262
|
+
}
|
|
232
263
|
|
|
233
|
-
|
|
264
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
265
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
|
|
266
|
+
}
|
|
234
267
|
|
|
235
|
-
|
|
236
|
-
return res
|
|
268
|
+
return binding.readSync(fd, buffer, offset, len, pos)
|
|
237
269
|
}
|
|
238
270
|
|
|
239
|
-
function
|
|
240
|
-
if (typeof fd !== 'number')
|
|
241
|
-
|
|
242
|
-
|
|
271
|
+
function readv (fd, buffers, pos, cb) {
|
|
272
|
+
if (typeof fd !== 'number') {
|
|
273
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
274
|
+
}
|
|
243
275
|
|
|
244
|
-
|
|
276
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
277
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (typeof pos === 'function') {
|
|
281
|
+
cb = pos
|
|
282
|
+
pos = -1
|
|
283
|
+
} else if (typeof cb !== 'function') {
|
|
284
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
285
|
+
}
|
|
245
286
|
|
|
287
|
+
if (typeof position !== 'number') pos = -1
|
|
288
|
+
|
|
289
|
+
const req = getReq()
|
|
246
290
|
req.callback = cb
|
|
247
|
-
binding.
|
|
291
|
+
binding.readv(req.handle, fd, buffers, pos)
|
|
248
292
|
}
|
|
249
293
|
|
|
250
|
-
function
|
|
251
|
-
|
|
294
|
+
function write (fd, buffer, offset, len, pos, cb) {
|
|
295
|
+
if (typeof fd !== 'number') {
|
|
296
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
297
|
+
}
|
|
252
298
|
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
}
|
|
299
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
300
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
301
|
+
}
|
|
256
302
|
|
|
257
|
-
|
|
258
|
-
|
|
303
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
304
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (typeof cb !== 'function') {
|
|
308
|
+
if (typeof offset === 'function') {
|
|
309
|
+
cb = offset
|
|
310
|
+
offset = 0
|
|
311
|
+
len = buffer.byteLength
|
|
312
|
+
pos = -1
|
|
313
|
+
} else if (typeof len === 'function') {
|
|
314
|
+
cb = len
|
|
315
|
+
len = buffer.byteLength - offset
|
|
316
|
+
pos = -1
|
|
317
|
+
} else if (typeof pos === 'function') {
|
|
318
|
+
cb = pos
|
|
319
|
+
pos = -1
|
|
320
|
+
} else {
|
|
321
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (typeof position !== 'number') pos = -1
|
|
259
326
|
|
|
327
|
+
const req = getReq()
|
|
260
328
|
req.callback = cb
|
|
261
|
-
binding.
|
|
329
|
+
binding.write(req.handle, fd, buffer, offset, len, pos)
|
|
262
330
|
}
|
|
263
331
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
this.dev = toNumber(view, 0)
|
|
269
|
-
this.mode = toNumber(view, 2)
|
|
270
|
-
this.nlink = toNumber(view, 4)
|
|
271
|
-
this.uid = toNumber(view, 6)
|
|
272
|
-
this.gid = toNumber(view, 8)
|
|
273
|
-
this.rdev = toNumber(view, 10)
|
|
274
|
-
this.ino = toNumber(view, 12)
|
|
275
|
-
this.size = toNumber(view, 14)
|
|
276
|
-
this.blksize = toNumber(view, 16)
|
|
277
|
-
this.blocks = toNumber(view, 18)
|
|
278
|
-
this.flags = toNumber(view, 20)
|
|
279
|
-
this.gen = toNumber(view, 22)
|
|
280
|
-
this.atimeMs = toNumber(view, 24)
|
|
281
|
-
this.mtimeMs = toNumber(view, 26)
|
|
282
|
-
this.ctimeMs = toNumber(view, 28)
|
|
283
|
-
this.birthtimeMs = toNumber(view, 30)
|
|
284
|
-
this.atime = new Date(this.atimeMs)
|
|
285
|
-
this.mtime = new Date(this.mtimeMs)
|
|
286
|
-
this.ctime = new Date(this.ctimeMs)
|
|
287
|
-
this.birthtime = new Date(this.birthtimeMs)
|
|
332
|
+
function writeSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
|
|
333
|
+
if (typeof fd !== 'number') {
|
|
334
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
288
335
|
}
|
|
289
336
|
|
|
290
|
-
|
|
291
|
-
|
|
337
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
338
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
292
339
|
}
|
|
293
340
|
|
|
294
|
-
|
|
295
|
-
|
|
341
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
342
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
|
|
296
343
|
}
|
|
297
344
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
345
|
+
return binding.writeSync(fd, buffer, offset, len, pos)
|
|
346
|
+
}
|
|
301
347
|
|
|
302
|
-
|
|
303
|
-
|
|
348
|
+
function writev (fd, buffers, pos, cb) {
|
|
349
|
+
if (typeof fd !== 'number') {
|
|
350
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
304
351
|
}
|
|
305
352
|
|
|
306
|
-
|
|
307
|
-
|
|
353
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
354
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
308
355
|
}
|
|
309
356
|
|
|
310
|
-
|
|
311
|
-
|
|
357
|
+
if (typeof pos === 'function') {
|
|
358
|
+
cb = pos
|
|
359
|
+
pos = -1
|
|
360
|
+
} else if (typeof cb !== 'function') {
|
|
361
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
312
362
|
}
|
|
313
363
|
|
|
314
|
-
|
|
315
|
-
return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
|
|
316
|
-
}
|
|
317
|
-
}
|
|
364
|
+
if (typeof position !== 'number') pos = -1
|
|
318
365
|
|
|
319
|
-
|
|
320
|
-
|
|
366
|
+
const req = getReq()
|
|
367
|
+
req.callback = cb
|
|
368
|
+
binding.writev(req.handle, fd, buffers, pos)
|
|
321
369
|
}
|
|
322
370
|
|
|
323
371
|
function stat (path, cb) {
|
|
324
|
-
|
|
372
|
+
if (typeof path !== 'string') {
|
|
373
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (typeof cb !== 'function') {
|
|
377
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const data = new Array(Stats.length)
|
|
325
381
|
|
|
326
|
-
req
|
|
382
|
+
const req = getReq()
|
|
327
383
|
|
|
328
|
-
req.callback = function (err, _
|
|
384
|
+
req.callback = function (err, _) {
|
|
329
385
|
if (err) cb(err, null)
|
|
330
|
-
else cb(null, new Stats(
|
|
386
|
+
else cb(null, new Stats(...data))
|
|
331
387
|
}
|
|
332
388
|
|
|
333
|
-
binding.stat(req.handle, path,
|
|
389
|
+
binding.stat(req.handle, path, data)
|
|
334
390
|
}
|
|
335
391
|
|
|
336
392
|
function statSync (path) {
|
|
337
|
-
|
|
338
|
-
|
|
393
|
+
if (typeof path !== 'string') {
|
|
394
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const data = new Array(Stats.length)
|
|
398
|
+
|
|
399
|
+
const res = binding.statSync(path, data)
|
|
339
400
|
if (res < 0) throw createError(res)
|
|
340
|
-
|
|
401
|
+
|
|
402
|
+
return new Stats(...data)
|
|
341
403
|
}
|
|
342
404
|
|
|
343
405
|
function lstat (path, cb) {
|
|
344
|
-
|
|
406
|
+
if (typeof path !== 'string') {
|
|
407
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (typeof cb !== 'function') {
|
|
411
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
412
|
+
}
|
|
345
413
|
|
|
346
|
-
|
|
414
|
+
const data = new Array(Stats.length)
|
|
347
415
|
|
|
348
|
-
req
|
|
416
|
+
const req = getReq()
|
|
417
|
+
|
|
418
|
+
req.callback = function (err, _) {
|
|
349
419
|
if (err) cb(err, null)
|
|
350
|
-
else cb(null, new Stats(
|
|
420
|
+
else cb(null, new Stats(...data))
|
|
351
421
|
}
|
|
352
422
|
|
|
353
|
-
binding.lstat(req.handle, path,
|
|
423
|
+
binding.lstat(req.handle, path, data)
|
|
354
424
|
}
|
|
355
425
|
|
|
356
426
|
function lstatSync (path) {
|
|
357
|
-
|
|
358
|
-
|
|
427
|
+
if (typeof path !== 'string') {
|
|
428
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const data = new Array(Stats.length)
|
|
432
|
+
|
|
433
|
+
const res = binding.lstatSync(path, data)
|
|
359
434
|
if (res < 0) throw createError(res)
|
|
360
|
-
|
|
435
|
+
|
|
436
|
+
return new Stats(...data)
|
|
361
437
|
}
|
|
362
438
|
|
|
363
439
|
function fstat (fd, cb) {
|
|
364
|
-
|
|
440
|
+
if (typeof fd !== 'number') {
|
|
441
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
442
|
+
}
|
|
365
443
|
|
|
366
|
-
|
|
444
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
445
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
446
|
+
}
|
|
367
447
|
|
|
368
|
-
|
|
448
|
+
if (typeof cb !== 'function') {
|
|
449
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const data = new Array(Stats.length)
|
|
453
|
+
|
|
454
|
+
const req = getReq()
|
|
455
|
+
|
|
456
|
+
req.callback = function (err, _) {
|
|
369
457
|
if (err) cb(err, null)
|
|
370
|
-
else cb(null, new Stats(
|
|
458
|
+
else cb(null, new Stats(...data))
|
|
371
459
|
}
|
|
372
460
|
|
|
373
|
-
binding.fstat(req.handle, fd,
|
|
461
|
+
binding.fstat(req.handle, fd, data)
|
|
374
462
|
}
|
|
375
463
|
|
|
376
464
|
function fstatSync (fd) {
|
|
377
|
-
|
|
378
|
-
|
|
465
|
+
if (typeof fd !== 'number') {
|
|
466
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
470
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const data = new Array(Stats.length)
|
|
474
|
+
|
|
475
|
+
const res = binding.fstatSync(fd, data)
|
|
379
476
|
if (res < 0) throw createError(res)
|
|
380
|
-
|
|
477
|
+
|
|
478
|
+
return new Stats(...data)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function ftruncate (fd, len, cb) {
|
|
482
|
+
if (typeof fd !== 'number') {
|
|
483
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (fd < 0 || fd > 0x7fffffff) {
|
|
487
|
+
throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (typeof len === 'function') {
|
|
491
|
+
cb = len
|
|
492
|
+
len = 0
|
|
493
|
+
} else if (typeof cb !== 'function') {
|
|
494
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const req = getReq()
|
|
498
|
+
req.callback = cb
|
|
499
|
+
binding.ftruncate(req.handle, fd, len)
|
|
381
500
|
}
|
|
382
501
|
|
|
383
502
|
function mkdirp (path, mode, cb) {
|
|
@@ -404,101 +523,208 @@ function mkdirp (path, mode, cb) {
|
|
|
404
523
|
})
|
|
405
524
|
}
|
|
406
525
|
|
|
407
|
-
function rename (src, dst, cb) {
|
|
408
|
-
const req = getReq()
|
|
409
|
-
|
|
410
|
-
req.callback = cb
|
|
411
|
-
binding.rename(req.handle, src, dst)
|
|
412
|
-
}
|
|
413
|
-
|
|
414
526
|
function mkdir (path, opts, cb) {
|
|
527
|
+
if (typeof path !== 'string') {
|
|
528
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
529
|
+
}
|
|
530
|
+
|
|
415
531
|
if (typeof opts === 'function') {
|
|
416
532
|
cb = opts
|
|
417
533
|
opts = { mode: 0o777 }
|
|
534
|
+
} else if (typeof cb !== 'function') {
|
|
535
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
418
536
|
}
|
|
419
537
|
|
|
420
|
-
if (
|
|
538
|
+
if (typeof opts === 'number') opts = { mode: opts }
|
|
539
|
+
else if (!opts) opts = {}
|
|
421
540
|
|
|
422
541
|
const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
|
|
423
542
|
|
|
424
|
-
if (opts.recursive)
|
|
425
|
-
return mkdirp(path, mode, cb)
|
|
426
|
-
}
|
|
543
|
+
if (opts.recursive) return mkdirp(path, mode, cb)
|
|
427
544
|
|
|
428
545
|
const req = getReq()
|
|
429
|
-
|
|
430
546
|
req.callback = cb
|
|
431
547
|
binding.mkdir(req.handle, path, mode)
|
|
432
548
|
}
|
|
433
549
|
|
|
434
550
|
function rmdir (path, cb) {
|
|
435
|
-
|
|
551
|
+
if (typeof path !== 'string') {
|
|
552
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (typeof cb !== 'function') {
|
|
556
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
557
|
+
}
|
|
436
558
|
|
|
559
|
+
const req = getReq()
|
|
437
560
|
req.callback = cb
|
|
438
561
|
binding.rmdir(req.handle, path)
|
|
439
562
|
}
|
|
440
563
|
|
|
441
564
|
function unlink (path, cb) {
|
|
442
|
-
|
|
565
|
+
if (typeof path !== 'string') {
|
|
566
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
567
|
+
}
|
|
443
568
|
|
|
569
|
+
if (typeof cb !== 'function') {
|
|
570
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const req = getReq()
|
|
444
574
|
req.callback = cb
|
|
445
575
|
binding.unlink(req.handle, path)
|
|
446
576
|
}
|
|
447
577
|
|
|
578
|
+
function rename (src, dst, cb) {
|
|
579
|
+
if (typeof src !== 'string') {
|
|
580
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (typeof dst !== 'string') {
|
|
584
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
if (typeof cb !== 'function') {
|
|
588
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const req = getReq()
|
|
592
|
+
req.callback = cb
|
|
593
|
+
binding.rename(req.handle, src, dst)
|
|
594
|
+
}
|
|
595
|
+
|
|
448
596
|
function readlink (path, opts, cb) {
|
|
449
|
-
if (typeof
|
|
450
|
-
|
|
597
|
+
if (typeof path !== 'string') {
|
|
598
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (typeof opts === 'function') {
|
|
602
|
+
cb = opts
|
|
603
|
+
opts = {}
|
|
604
|
+
} else if (typeof cb !== 'function') {
|
|
605
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
606
|
+
}
|
|
607
|
+
|
|
451
608
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
452
|
-
if (!opts) opts = {}
|
|
609
|
+
else if (!opts) opts = {}
|
|
453
610
|
|
|
454
611
|
const {
|
|
455
612
|
encoding = 'utf8'
|
|
456
613
|
} = opts
|
|
457
614
|
|
|
615
|
+
const data = Buffer.allocUnsafe(binding.sizeofFSPath)
|
|
616
|
+
|
|
458
617
|
const req = getReq()
|
|
459
618
|
|
|
460
|
-
req.
|
|
619
|
+
req.callback = function (err, _) {
|
|
620
|
+
if (err) return cb(err, null)
|
|
621
|
+
let path = data.subarray(0, data.indexOf(0))
|
|
622
|
+
if (encoding !== 'buffer') path = path.toString(encoding)
|
|
623
|
+
cb(null, path)
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
binding.readlink(req.handle, path, data)
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function opendir (path, opts, cb) {
|
|
630
|
+
if (typeof path !== 'string') {
|
|
631
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (typeof opts === 'function') {
|
|
635
|
+
cb = opts
|
|
636
|
+
opts = {}
|
|
637
|
+
} else if (typeof cb !== 'function') {
|
|
638
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
if (typeof opts === 'string') opts = { encoding: opts }
|
|
642
|
+
else if (!opts) opts = {}
|
|
643
|
+
|
|
644
|
+
const data = Buffer.allocUnsafe(binding.sizeofFSDir)
|
|
645
|
+
|
|
646
|
+
const req = getReq()
|
|
461
647
|
|
|
462
|
-
req.callback = function (err, _
|
|
648
|
+
req.callback = function (err, _) {
|
|
463
649
|
if (err) return cb(err, null)
|
|
464
|
-
|
|
465
|
-
if (encoding !== 'buffer') return cb(null, Buffer.coerce(buf).toString(encoding))
|
|
466
|
-
cb(null, buf)
|
|
650
|
+
cb(null, new Dir(path, data, opts))
|
|
467
651
|
}
|
|
468
652
|
|
|
469
|
-
binding.
|
|
653
|
+
binding.opendir(req.handle, path, data)
|
|
470
654
|
}
|
|
471
655
|
|
|
472
|
-
function
|
|
473
|
-
if (typeof
|
|
474
|
-
|
|
656
|
+
function readdir (path, opts, cb) {
|
|
657
|
+
if (typeof path !== 'string') {
|
|
658
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (typeof opts === 'function') {
|
|
662
|
+
cb = opts
|
|
663
|
+
opts = {}
|
|
664
|
+
} else if (typeof cb !== 'function') {
|
|
665
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
666
|
+
}
|
|
667
|
+
|
|
475
668
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
476
669
|
if (!opts) opts = {}
|
|
477
670
|
|
|
671
|
+
const {
|
|
672
|
+
withFileTypes = false
|
|
673
|
+
} = opts
|
|
674
|
+
|
|
675
|
+
opendir(path, opts, async (err, dir) => {
|
|
676
|
+
if (err) return cb(err, null)
|
|
677
|
+
const result = []
|
|
678
|
+
dir
|
|
679
|
+
.on('data', (entry) => result.push(withFileTypes ? entry : entry.name))
|
|
680
|
+
.on('error', (err) => cb(err, null))
|
|
681
|
+
.on('end', () => cb(null, result))
|
|
682
|
+
})
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function readFile (path, opts, cb) {
|
|
686
|
+
if (typeof path !== 'string') {
|
|
687
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
if (typeof opts === 'function') {
|
|
691
|
+
cb = opts
|
|
692
|
+
opts = {}
|
|
693
|
+
} else if (typeof cb !== 'function') {
|
|
694
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (typeof opts === 'string') opts = { encoding: opts }
|
|
698
|
+
else if (!opts) opts = {}
|
|
699
|
+
|
|
700
|
+
const {
|
|
701
|
+
encoding = 'buffer'
|
|
702
|
+
} = opts
|
|
703
|
+
|
|
478
704
|
open(path, opts.flag || 'r', function (err, fd) {
|
|
479
705
|
if (err) return cb(err)
|
|
480
706
|
|
|
481
707
|
fstat(fd, function (err, st) {
|
|
482
708
|
if (err) return closeAndError(err)
|
|
483
709
|
|
|
484
|
-
let
|
|
710
|
+
let buffer = Buffer.allocUnsafe(st.size)
|
|
485
711
|
let len = 0
|
|
486
712
|
|
|
487
|
-
read(fd,
|
|
713
|
+
read(fd, buffer, loop)
|
|
488
714
|
|
|
489
715
|
function loop (err, r) {
|
|
490
716
|
if (err) return closeAndError(err)
|
|
491
717
|
len += r
|
|
492
|
-
if (r === 0 || len ===
|
|
493
|
-
read(fd,
|
|
718
|
+
if (r === 0 || len === buffer.byteLength) return done()
|
|
719
|
+
read(fd, buffer.subarray(len), loop)
|
|
494
720
|
}
|
|
495
721
|
|
|
496
722
|
function done () {
|
|
497
|
-
if (len !==
|
|
723
|
+
if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
|
|
498
724
|
close(fd, function (err) {
|
|
499
725
|
if (err) return cb(err)
|
|
500
|
-
if (
|
|
501
|
-
|
|
726
|
+
if (encoding !== 'buffer') buffer = buffer.toString(encoding)
|
|
727
|
+
cb(null, buffer)
|
|
502
728
|
})
|
|
503
729
|
}
|
|
504
730
|
})
|
|
@@ -512,26 +738,34 @@ function readFile (path, opts, cb) {
|
|
|
512
738
|
}
|
|
513
739
|
|
|
514
740
|
function readFileSync (path, opts) {
|
|
741
|
+
if (typeof path !== 'string') {
|
|
742
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
743
|
+
}
|
|
744
|
+
|
|
515
745
|
if (typeof opts === 'string') opts = { encoding: opts }
|
|
516
|
-
if (!opts) opts = {}
|
|
746
|
+
else if (!opts) opts = {}
|
|
747
|
+
|
|
748
|
+
const {
|
|
749
|
+
encoding = 'buffer'
|
|
750
|
+
} = opts
|
|
517
751
|
|
|
518
752
|
const fd = openSync(path, opts.flag || 'r')
|
|
519
753
|
|
|
520
754
|
try {
|
|
521
755
|
const st = fstatSync(fd)
|
|
522
756
|
|
|
523
|
-
let
|
|
757
|
+
let buffer = Buffer.allocUnsafe(st.size)
|
|
524
758
|
let len = 0
|
|
525
759
|
|
|
526
760
|
while (true) {
|
|
527
|
-
const r = readSync(fd, len ?
|
|
761
|
+
const r = readSync(fd, len ? buffer.subarray(len) : buffer)
|
|
528
762
|
len += r
|
|
529
|
-
if (r === 0 || len ===
|
|
763
|
+
if (r === 0 || len === buffer.byteLength) break
|
|
530
764
|
}
|
|
531
|
-
if (len !== buf.byteLength) buf = buf.subarray(0, len)
|
|
532
765
|
|
|
533
|
-
if (
|
|
534
|
-
|
|
766
|
+
if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
|
|
767
|
+
if (encoding !== 'buffer') buffer = buffer.toString(encoding)
|
|
768
|
+
return buffer
|
|
535
769
|
} finally {
|
|
536
770
|
try {
|
|
537
771
|
closeSync(fd)
|
|
@@ -540,16 +774,26 @@ function readFileSync (path, opts) {
|
|
|
540
774
|
}
|
|
541
775
|
|
|
542
776
|
function writeFile (path, data, opts, cb) {
|
|
543
|
-
if (typeof
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
if (typeof opts === 'string') opts = { encoding: opts }
|
|
547
|
-
if (!opts) opts = {}
|
|
777
|
+
if (typeof path !== 'string') {
|
|
778
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
779
|
+
}
|
|
548
780
|
|
|
549
|
-
if (
|
|
550
|
-
|
|
781
|
+
if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
|
|
782
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
|
|
551
783
|
}
|
|
552
784
|
|
|
785
|
+
if (typeof opts === 'function') {
|
|
786
|
+
cb = opts
|
|
787
|
+
opts = {}
|
|
788
|
+
} else if (typeof cb !== 'function') {
|
|
789
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (typeof opts === 'string') opts = { encoding: opts }
|
|
793
|
+
else if (!opts) opts = {}
|
|
794
|
+
|
|
795
|
+
if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
|
|
796
|
+
|
|
553
797
|
open(path, opts.flag || 'w', opts.mode, function (err, fd) {
|
|
554
798
|
if (err) return cb(err)
|
|
555
799
|
|
|
@@ -576,21 +820,28 @@ function writeFile (path, data, opts, cb) {
|
|
|
576
820
|
})
|
|
577
821
|
}
|
|
578
822
|
|
|
579
|
-
function writeFileSync (path,
|
|
580
|
-
if (typeof
|
|
581
|
-
|
|
823
|
+
function writeFileSync (path, data, opts) {
|
|
824
|
+
if (typeof path !== 'string') {
|
|
825
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
|
|
826
|
+
}
|
|
582
827
|
|
|
583
|
-
if (
|
|
584
|
-
|
|
828
|
+
if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
|
|
829
|
+
throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
|
|
585
830
|
}
|
|
586
831
|
|
|
832
|
+
if (typeof opts === 'string') opts = { encoding: opts }
|
|
833
|
+
else if (!opts) opts = {}
|
|
834
|
+
|
|
835
|
+
if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
|
|
836
|
+
|
|
587
837
|
const fd = openSync(path, opts.flag || 'w', opts.mode)
|
|
588
838
|
|
|
589
839
|
try {
|
|
590
840
|
let len = 0
|
|
841
|
+
|
|
591
842
|
while (true) {
|
|
592
|
-
len += writeSync(fd, len ?
|
|
593
|
-
if (len ===
|
|
843
|
+
len += writeSync(fd, len ? data.subarray(len) : data)
|
|
844
|
+
if (len === data.byteLength) break
|
|
594
845
|
}
|
|
595
846
|
} finally {
|
|
596
847
|
try {
|
|
@@ -599,6 +850,57 @@ function writeFileSync (path, buf, opts) {
|
|
|
599
850
|
}
|
|
600
851
|
}
|
|
601
852
|
|
|
853
|
+
class Stats {
|
|
854
|
+
constructor (dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeMs, mtimeMs, ctimeMs, birthtimeMs) {
|
|
855
|
+
this.dev = dev
|
|
856
|
+
this.mode = mode
|
|
857
|
+
this.nlink = nlink
|
|
858
|
+
this.uid = uid
|
|
859
|
+
this.gid = gid
|
|
860
|
+
this.rdev = rdev
|
|
861
|
+
this.blksize = blksize
|
|
862
|
+
this.ino = ino
|
|
863
|
+
this.size = size
|
|
864
|
+
this.blocks = blocks
|
|
865
|
+
this.atimeMs = atimeMs
|
|
866
|
+
this.mtimeMs = mtimeMs
|
|
867
|
+
this.ctimeMs = ctimeMs
|
|
868
|
+
this.birthtimeMs = birthtimeMs
|
|
869
|
+
this.atime = new Date(atimeMs)
|
|
870
|
+
this.mtime = new Date(mtimeMs)
|
|
871
|
+
this.ctime = new Date(ctimeMs)
|
|
872
|
+
this.birthtime = new Date(birthtimeMs)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
isDirectory () {
|
|
876
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFDIR
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
isFile () {
|
|
880
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFREG
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
isBlockDevice () {
|
|
884
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFBLK
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
isCharacterDevice () {
|
|
888
|
+
return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
isFIFO () {
|
|
892
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFIFO
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
isSymbolicLink () {
|
|
896
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFLNK
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
isSocket () {
|
|
900
|
+
return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
602
904
|
class FileWriteStream extends Writable {
|
|
603
905
|
constructor (path, opts = {}) {
|
|
604
906
|
super({ map })
|
|
@@ -702,6 +1004,104 @@ class FileReadStream extends Readable {
|
|
|
702
1004
|
}
|
|
703
1005
|
}
|
|
704
1006
|
|
|
1007
|
+
class Dir extends Readable {
|
|
1008
|
+
constructor (path, handle, opts = {}) {
|
|
1009
|
+
const {
|
|
1010
|
+
encoding = 'utf8',
|
|
1011
|
+
bufferSize = 32
|
|
1012
|
+
} = opts
|
|
1013
|
+
|
|
1014
|
+
super()
|
|
1015
|
+
|
|
1016
|
+
this._handle = handle
|
|
1017
|
+
this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
|
|
1018
|
+
this._encoding = encoding
|
|
1019
|
+
this._closed = false
|
|
1020
|
+
|
|
1021
|
+
this.path = path
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
_read (cb) {
|
|
1025
|
+
const self = this
|
|
1026
|
+
const data = []
|
|
1027
|
+
|
|
1028
|
+
const req = getReq()
|
|
1029
|
+
|
|
1030
|
+
req.callback = function (err, _) {
|
|
1031
|
+
if (err) return cb(err)
|
|
1032
|
+
if (data.length === 0) self.push(null)
|
|
1033
|
+
else {
|
|
1034
|
+
for (const entry of data) {
|
|
1035
|
+
let name = Buffer.from(entry.name)
|
|
1036
|
+
if (self._encoding !== 'buffer') name = name.toString(self._encoding)
|
|
1037
|
+
self.push(new Dirent(self.path, name, entry.type))
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
cb(null)
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
binding.readdir(req.handle, this._handle, this._dirents, data)
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
_destroy (cb) {
|
|
1047
|
+
if (this._closed) cb(null)
|
|
1048
|
+
else this.close(cb)
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
close (cb = noop) {
|
|
1052
|
+
if (this._closed) return
|
|
1053
|
+
|
|
1054
|
+
const self = this
|
|
1055
|
+
|
|
1056
|
+
const req = getReq()
|
|
1057
|
+
|
|
1058
|
+
req.callback = function (err, _) {
|
|
1059
|
+
if (err) return cb(err)
|
|
1060
|
+
self._closed = true
|
|
1061
|
+
cb(null)
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
binding.closedir(req.handle, this._handle)
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
class Dirent {
|
|
1069
|
+
constructor (path, name, type) {
|
|
1070
|
+
this._type = type
|
|
1071
|
+
|
|
1072
|
+
this.path = path
|
|
1073
|
+
this.name = name
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
isFile () {
|
|
1077
|
+
return this._type === constants.UV_DIRENT_FILE
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
isDirectory () {
|
|
1081
|
+
return this._type === constants.UV_DIRENT_DIR
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
isSymbolicLink () {
|
|
1085
|
+
return this._type === constants.UV_DIRENT_LINK
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
isFIFO () {
|
|
1089
|
+
return this._type === constants.UV_DIRENT_FIFO
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
isSocket () {
|
|
1093
|
+
return this._type === constants.UV_DIRENT_SOCKET
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
isCharacterDevice () {
|
|
1097
|
+
return this._type === constants.UV_DIRENT_CHAR
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
isBlockDevice () {
|
|
1101
|
+
return this._type === constants.UV_DIRENT_BLOCK
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
705
1105
|
exports.promises = {}
|
|
706
1106
|
|
|
707
1107
|
function typeError (code, message) {
|
|
@@ -718,31 +1118,22 @@ exports.read = read
|
|
|
718
1118
|
exports.readv = readv
|
|
719
1119
|
exports.write = write
|
|
720
1120
|
exports.writev = writev
|
|
721
|
-
exports.ftruncate = ftruncate
|
|
722
1121
|
exports.fstat = fstat
|
|
1122
|
+
exports.ftruncate = ftruncate
|
|
723
1123
|
|
|
724
1124
|
exports.openSync = openSync
|
|
725
1125
|
exports.closeSync = closeSync
|
|
726
1126
|
exports.readSync = readSync
|
|
727
1127
|
exports.writeSync = writeSync
|
|
728
|
-
exports.fstatSync = fstatSync
|
|
729
1128
|
exports.statSync = statSync
|
|
730
1129
|
exports.lstatSync = lstatSync
|
|
1130
|
+
exports.fstatSync = fstatSync
|
|
731
1131
|
|
|
732
|
-
exports.
|
|
733
|
-
exports.
|
|
734
|
-
|
|
735
|
-
exports.unlink = unlink
|
|
736
|
-
exports.promises.unlink = promisify(unlink)
|
|
737
|
-
|
|
738
|
-
exports.readFile = readFile
|
|
739
|
-
exports.promises.readFile = promisify(readFile)
|
|
740
|
-
|
|
741
|
-
exports.writeFile = writeFile
|
|
742
|
-
exports.promises.writeFile = promisify(writeFile)
|
|
1132
|
+
exports.stat = stat
|
|
1133
|
+
exports.promises.stat = promisify(stat)
|
|
743
1134
|
|
|
744
|
-
exports.
|
|
745
|
-
exports.promises.
|
|
1135
|
+
exports.lstat = lstat
|
|
1136
|
+
exports.promises.lstat = promisify(lstat)
|
|
746
1137
|
|
|
747
1138
|
exports.mkdir = mkdir
|
|
748
1139
|
exports.promises.mkdir = promisify(mkdir)
|
|
@@ -750,22 +1141,41 @@ exports.promises.mkdir = promisify(mkdir)
|
|
|
750
1141
|
exports.rmdir = rmdir
|
|
751
1142
|
exports.promises.rmdir = promisify(rmdir)
|
|
752
1143
|
|
|
753
|
-
exports.
|
|
754
|
-
|
|
755
|
-
exports.stat = stat
|
|
756
|
-
exports.promises.stat = promisify(stat)
|
|
1144
|
+
exports.unlink = unlink
|
|
1145
|
+
exports.promises.unlink = promisify(unlink)
|
|
757
1146
|
|
|
758
|
-
exports.
|
|
759
|
-
exports.promises.
|
|
1147
|
+
exports.rename = rename
|
|
1148
|
+
exports.promises.rename = promisify(rename)
|
|
760
1149
|
|
|
761
1150
|
exports.readlink = readlink
|
|
762
1151
|
exports.promises.readlink = promisify(readlink)
|
|
763
1152
|
|
|
1153
|
+
exports.opendir = opendir
|
|
1154
|
+
exports.promises.opendir = promisify(opendir)
|
|
1155
|
+
|
|
1156
|
+
exports.readdir = readdir
|
|
1157
|
+
exports.promises.readdir = promisify(readdir)
|
|
1158
|
+
|
|
1159
|
+
exports.readFile = readFile
|
|
1160
|
+
exports.promises.readFile = promisify(readFile)
|
|
1161
|
+
|
|
1162
|
+
exports.writeFile = writeFile
|
|
1163
|
+
exports.promises.writeFile = promisify(writeFile)
|
|
1164
|
+
|
|
1165
|
+
exports.readFileSync = readFileSync
|
|
1166
|
+
exports.writeFileSync = writeFileSync
|
|
1167
|
+
|
|
1168
|
+
exports.Stats = Stats
|
|
1169
|
+
|
|
764
1170
|
exports.ReadStream = FileReadStream
|
|
765
|
-
exports.createReadStream =
|
|
1171
|
+
exports.createReadStream = function createReadStream (path, opts) {
|
|
1172
|
+
return new FileReadStream(path, opts)
|
|
1173
|
+
}
|
|
766
1174
|
|
|
767
1175
|
exports.WriteStream = FileWriteStream
|
|
768
|
-
exports.createWriteStream =
|
|
1176
|
+
exports.createWriteStream = function createWriteStream (path, opts) {
|
|
1177
|
+
return new FileWriteStream(path, opts)
|
|
1178
|
+
}
|
|
769
1179
|
|
|
770
1180
|
function promisify (fn) {
|
|
771
1181
|
return function (...args) {
|
|
@@ -778,6 +1188,6 @@ function promisify (fn) {
|
|
|
778
1188
|
}
|
|
779
1189
|
}
|
|
780
1190
|
|
|
781
|
-
function map (
|
|
782
|
-
return typeof
|
|
1191
|
+
function map (data) {
|
|
1192
|
+
return typeof data === 'string' ? Buffer.from(data) : data
|
|
783
1193
|
}
|