forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.js
More file actions
104 lines (91 loc) · 1.89 KB
/
Copy pathdir.js
File metadata and controls
104 lines (91 loc) · 1.89 KB
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
'use strict';
const {
SymbolAsyncDispose,
SymbolAsyncIterator,
SymbolDispose,
} = primordials;
const {
codes: {
ERR_DIR_CLOSED,
},
} = require('internal/errors');
/**
* Virtual directory handle returned by VFS opendir/opendirSync.
* Mimics the subset of the native Dir interface used by Node.js internals
* (e.g. fs.cp, fs.promises.cp).
*/
class VirtualDir {
#path;
#entries;
#index;
#closed;
constructor(dirPath, entries) {
this.#path = dirPath;
this.#entries = entries;
this.#index = 0;
this.#closed = false;
}
get path() {
return this.#path;
}
readSync() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
if (this.#index >= this.#entries.length) {
return null;
}
return this.#entries[this.#index++];
}
async read(callback) {
if (typeof callback === 'function') {
try {
const result = this.readSync();
process.nextTick(callback, null, result);
} catch (err) {
process.nextTick(callback, err);
}
return;
}
return this.readSync();
}
closeSync() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
this.#closed = true;
}
async close(callback) {
if (typeof callback === 'function') {
this.closeSync();
process.nextTick(callback, null);
return;
}
this.closeSync();
}
async *entries() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
try {
let entry;
while ((entry = this.readSync()) !== null) {
yield entry;
}
} finally {
if (!this.#closed) {
this.closeSync();
}
}
}
[SymbolDispose]() {
if (!this.#closed) {
this.closeSync();
}
}
}
VirtualDir.prototype[SymbolAsyncIterator] = VirtualDir.prototype.entries;
VirtualDir.prototype[SymbolAsyncDispose] = VirtualDir.prototype.close;
module.exports = {
VirtualDir,
};