This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
701 lines (613 loc) · 16.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/**
* @module File System
*
* This module enables interacting with the file system in a way modeled on
* standard POSIX functions.
*
* The Application Sandbox restricts access to the file system.
* Please see the Application Sandbox documentation for more information:
* https://sockets.sh/guides/#working-with-the-file-system-on-ios
*
* To use the promise-based APIs:
*
* ```js
* import * as fs from '@socketsupply/socket-api/fs/promises.js';
* ```
*
* To use the callback and async APIs:
*
* ```js
* import * as fs from '@socketsupply/socket-api/fs/index.js';
* ```
*/
import { Dir, Dirent, sortDirectoryEntries } from './dir.js'
import { DirectoryHandle, FileHandle } from './handle.js'
import { ReadStream, WriteStream } from './stream.js'
import { isBufferLike, isFunction, noop } from '../util.js'
import * as constants from './constants.js'
import * as promises from './promises.js'
import { Stats } from './stats.js'
import console from '../console.js'
import ipc from '../ipc.js'
import fds from './fds.js'
import gc from '../gc.js'
import * as exports from './index.js'
// export * from './stream.js'
export { default as binding } from './binding.js'
function defaultCallback (err) {
if (err) throw err
}
async function visit (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
const { flags, flag, mode } = options || {}
let handle = null
try {
handle = await FileHandle.open(path, flags || flag, mode, options)
} catch (err) {
return callback(err)
}
if (handle) {
await callback(null, handle)
try {
await handle.close(options)
} catch (err) {
console.warn(err.message || err)
}
}
}
/**
* Asynchronously check access a file for a given mode calling `callback`
* upon success or error.
* @see {@link https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsopenpath-flags-mode-callback}
* @param {string | Buffer | URL} path
* @param {string=} [mode = F_OK(0)]
* @param {function(err, fd)} callback
*/
export function access (path, mode, callback) {
if (typeof mode === 'function') {
callback = mode
mode = FileHandle.DEFAULT_ACCESS_MODE
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
FileHandle
.access(path, mode)
.then((mode) => callback(null, mode))
.catch((err) => callback(err))
}
/**
* @ignore
*/
export function appendFile (path, data, options, callback) {
}
/**
*
* Asynchronously changes the permissions of a file.
* No arguments other than a possible exception are given to the completion callback
*
* @see {@link https://nodejs.org/api/fs.html#fschmodpath-mode-callback}
*
* @param {string | Buffer | URL} path
* @param {number} mode
* @param {function(err)} callback
*/
export function chmod (path, mode, callback) {
if (typeof mode !== 'number') {
throw new TypeError(`The argument 'mode' must be a 32-bit unsigned integer or an octal string. Received ${mode}`)
}
if (mode < 0 || !Number.isInteger(mode)) {
throw new RangeError(`The value of "mode" is out of range. It must be an integer. Received ${mode}`)
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
ipc.send('fs.chmod', { mode, path }).then((result) => {
result?.err ? callback(result.err) : callback(null)
})
}
/**
* @ignore
*/
export function chown (path, uid, gid, callback) {
}
/**
* Asynchronously close a file descriptor calling `callback` upon success or error.
* @see {https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsclosefd-callback}
* @param {number} fd
* @param {function(err)=} callback
*/
export function close (fd, callback) {
if (typeof callback !== 'function') {
callback = defaultCallback
}
try {
FileHandle
.from(fd)
.close()
.then(() => callback(null))
.catch((err) => callback(err))
} catch (err) {
callback(err)
}
}
export function copyFile (src, dst, mode, callback) {
}
/**
* @see {@link https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fscreatewritestreampath-options}
* @param {string | Buffer | URL} path
* @param {object=} options
* @returns {fs.ReadStream}
*/
export function createReadStream (path, options) {
if (path?.fd) {
options = path
path = options?.path || null
}
let handle = null
const stream = new ReadStream({
autoClose: typeof options?.fd !== 'number',
...options
})
if (options?.fd) {
handle = FileHandle.from(options.fd)
} else {
handle = new FileHandle({ flags: 'r', path, ...options })
handle.open(options).catch((err) => stream.emit('error', err))
}
stream.once('end', async () => {
if (options?.autoClose !== false) {
try {
await handle.close(options)
} catch (err) {
stream.emit('error', err)
}
}
})
stream.setHandle(handle)
return stream
}
/**
* @see {@link https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fscreatewritestreampath-options}
* @param {string | Buffer | URL} path
* @param {object=} options
* @returns {fs.WriteStream}
*/
export function createWriteStream (path, options) {
if (path?.fd) {
options = path
path = options?.path || null
}
let handle = null
const stream = new WriteStream({
autoClose: typeof options?.fd !== 'number',
...options
})
if (typeof options?.fd === 'number') {
handle = FileHandle.from(options.fd)
} else {
handle = new FileHandle({ flags: 'w', path, ...options })
handle.open(options).catch((err) => stream.emit('error', err))
}
stream.once('finish', async () => {
if (options?.autoClose !== false) {
try {
await handle.close(options)
} catch (err) {
stream.emit('error', err)
}
}
})
stream.setHandle(handle)
return stream
}
/**
* Invokes the callback with the <fs.Stats> for the file descriptor. See
* the POSIX fstat(2) documentation for more detail.
*
* @see {@link https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsfstatfd-options-callback}
*
* @param {number} fd - A file descriptor.
* @param {Object=} options - An options object.
* @param {function} callback - The function to call after completion.
*/
export function fstat (fd, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
try {
FileHandle
.from(fd)
.stat(options)
.then(() => callback(null))
.catch((err) => callback(err))
} catch (err) {
callback(err)
}
}
/**
* @ignore
*/
export function lchmod (path, mode, callback) {
}
/**
* @ignore
*/
export function lchown (path, uid, gid, callback) {
}
/**
* @ignore
*/
export function lutimes (path, atime, mtime, callback) {
}
/**
* @ignore
*/
export function link (existingPath, newPath, callback) {
}
/**
* @ignore
*/
export function lstat (path, options, callback) {
}
/**
* @ignore
*/
export function mkdir (path, options, callback) {
if ((typeof options === 'undefined') || (typeof options === 'function')) {
throw new TypeError('options must be an object.')
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
const mode = options.mode || 0o777
const recursive = options.recurisve === true
if (typeof mode !== 'number') {
throw new TypeError('mode must be a number.')
}
if (mode < 0 || !Number.isInteger(mode)) {
throw new RangeError('mode must be a positive finite number.')
}
ipc
.request('fs.mkdir', { mode, path, recursive })
.then(result => result?.err ? callback(result.err) : callback(null))
.catch(err => callback(err))
}
/**
* Asynchronously open a file calling `callback` upon success or error.
* @see {https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsopenpath-flags-mode-callback}
* @param {string | Buffer | URL} path
* @param {string=} [flags = 'r']
* @param {string=} [mode = 0o666]
* @param {function(err, fd)} callback
*/
export function open (path, flags, mode, options, callback) {
if (typeof flags === 'object') {
callback = mode
options = flags
flags = FileHandle.DEFAULT_OPEN_FLAGS
mode = FileHandle.DEFAULT_OPEN_MODE
}
if (typeof mode === 'object') {
callback = options
options = mode
flags = FileHandle.DEFAULT_OPEN_FLAGS
mode = FileHandle.DEFAULT_OPEN_MODE
}
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof flags === 'function') {
callback = flags
flags = FileHandle.DEFAULT_OPEN_FLAGS
mode = FileHandle.DEFAULT_OPEN_MODE
}
if (typeof mode === 'function') {
callback = mode
mode = FileHandle.DEFAULT_OPEN_MODE
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
FileHandle
.open(path, flags, mode, options)
.then((handle) => {
gc.unref(handle)
callback(null, handle.fd)
})
.catch((err) => callback(err))
}
/**
* Asynchronously open a directory calling `callback` upon success or error.
* @see {https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsreaddirpath-options-callback}
* @param {string | Buffer | URL} path
* @param {Object=} options
* @param {string=} [options.encoding = 'utf8']
* @param {boolean=} [options.withFileTypes = false]
* @param {function(err, fd)} callback
*/
export function opendir (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
DirectoryHandle
.open(path, options)
.then((handle) => callback(null, new Dir(handle, options)))
.catch((err) => callback(err))
}
/**
* Asynchronously read from an open file descriptor.
* @see {https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsreadfd-buffer-offset-length-position-callback}
* @param {number} fd
* @param {object | Buffer | TypedArray} buffer - The buffer that the data will be written to.
* @param {number} offset - The position in buffer to write the data to.
* @param {number} length - The number of bytes to read.
* @param {number | BigInt | null} position - Specifies where to begin reading from in the file. If position is null or -1 , data will be read from the current file position, and the file position will be updated. If position is an integer, the file position will be unchanged.
* @param {function(err, bytesRead, buffer)} callback
*/
export function read (fd, buffer, offset, length, position, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof buffer === 'object' && !isBufferLike(buffer)) {
options = buffer
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
try {
FileHandle
.from(fd)
.read({ ...options, buffer, offset, length, position })
.then(({ bytesRead, buffer }) => callback(null, bytesRead, buffer))
.catch((err) => callback(err))
} catch (err) {
callback(err)
}
}
/**
* Asynchronously read all entries in a directory.
* @see {https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsreaddirpath-options-callback}
* @param {string | Buffer | URL } path
* @param {object=} [options]
* @param {string=} [options.encoding = 'utf8']
* @param {boolean=} [options.withFileTypes = false]
* @param {function(err, buffer)} callback
*/
export function readdir (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options || typeof options !== 'object') {
options = {}
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
options = {
entries: DirectoryHandle.MAX_ENTRIES,
withFileTypes: false,
...options
}
DirectoryHandle
.open(path, options)
.then(async (handle) => {
const entries = []
const dir = new Dir(handle, options)
try {
for await (const entry of dir.entries(options)) {
entries.push(entry)
}
} catch (err) {
return callback(err)
} finally {
if (!dir.closing && !dir.closed) {
dir.close().catch(noop)
}
}
callback(null, entries.sort(sortDirectoryEntries))
})
.catch((err) => callback(err))
}
/**
* @param {string | Buffer | URL | number } path
* @param {object=} [options]
* @param {string=} [options.encoding = 'utf8']
* @param {string=} [options.flag = 'r']
* @param {AbortSignal=} [options.signal]
* @param {function(err, buffer)} callback
*/
export function readFile (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof options === 'string') {
options = { encoding: options }
}
options = {
flags: 'r',
...options
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
visit(path, options, async (err, handle) => {
let buffer = null
if (err) {
callback(err)
return
}
try {
buffer = await handle.readFile(options)
} catch (err) {
callback(err)
return
}
callback(null, buffer)
})
}
/**
* @ignore
*/
export function readlink (path, options, callback) {
}
/**
* @ignore
*/
export function realpath (path, options, callback) {
}
/**
* @ignore
*/
export function rename (oldPath, newPath, callback) {
}
/**
* @ignore
*/
export function rmdir (path, options, callback) {
}
/**
* @ignore
*/
export function rm (path, options, callback) {
}
/**
*
* @param {string | Buffer | URL | number } path - filename or file descriptor
* @param {object=} options
* @param {string=} [options.encoding = 'utf8']
* @param {string=} [options.flag = 'r']
* @param {AbortSignal=} [options.signal]
* @param {function(err, data)} callback
*/
export function stat (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
visit(path, async (err, handle) => {
let stats = null
if (err) {
callback(err)
return
}
try {
stats = await handle.stat(options)
} catch (err) {
callback(err)
return
}
callback(null, stats)
})
}
/**
* @ignore
*/
export function symlink (target, path, type, callback) {
}
/**
* @ignore
*/
export function truncate (path, length, callback) {
}
/**
* @ignore
*/
export function unlink (path, callback) {
}
/**
* @ignore
*/
export function utimes (path, atime, mtime, callback) {
}
/**
* @ignore
*/
export function watch (path, options, callback) {
}
/**
* @ignore
*/
export function write (fd, buffer, offset, length, position, callback) {
}
/**
* @see {@url https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fswritefilefile-data-options-callback}
* @param {string | Buffer | URL | number } path - filename or file descriptor
* @param {string | Buffer | TypedArray | DataView | object } data
* @param {object=} options
* @param {string=} [options.encoding = 'utf8']
* @param {string=} [options.mode = 0o666]
* @param {string=} [options.flag = 'w']
* @param {AbortSignal=} [options.signal]
* @param {function(err)} callback
*/
export function writeFile (path, data, options, callback) {
if (typeof options === 'function') {
callback = options
options = { encoding: 'utf8' }
}
if (typeof options === 'string') {
options = { encoding: options }
}
options = {
mode: 0o666,
flag: 'w',
...options
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.')
}
visit(path, options, async (err, handle) => {
if (err) {
callback(err)
return
}
try {
await handle.writeFile(data, options)
} catch (err) {
callback(err)
return
}
callback(null)
})
}
export function writev (fd, buffers, position, callback) {
}
// re-exports
export {
constants,
Dir,
DirectoryHandle,
Dirent,
fds,
FileHandle,
promises,
ReadStream,
Stats,
WriteStream
}
export default exports
for (const key in exports) {
const value = exports[key]
if (key in promises && isFunction(value) && isFunction(promises[key])) {
value[Symbol.for('nodejs.util.promisify.custom')] = promises[key]
}
}