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
|
'use strict';
const { Object } = primordials;
const pathModule = require('path');
const binding = internalBinding('fs');
const dirBinding = internalBinding('fs_dir');
const {
codes: {
ERR_DIR_CLOSED,
ERR_INVALID_CALLBACK,
ERR_MISSING_ARGS
}
} = require('internal/errors');
const { FSReqCallback } = binding;
const internalUtil = require('internal/util');
const {
getDirent,
getOptions,
getValidatedPath,
handleErrorFromBinding
} = require('internal/fs/utils');
const kDirHandle = Symbol('kDirHandle');
const kDirPath = Symbol('kDirPath');
const kDirClosed = Symbol('kDirClosed');
const kDirOptions = Symbol('kDirOptions');
const kDirReadPromisified = Symbol('kDirReadPromisified');
const kDirClosePromisified = Symbol('kDirClosePromisified');
class Dir {
constructor(handle, path, options) {
if (handle == null) throw new ERR_MISSING_ARGS('handle');
this[kDirHandle] = handle;
this[kDirPath] = path;
this[kDirClosed] = false;
this[kDirOptions] = getOptions(options, {
encoding: 'utf8'
});
this[kDirReadPromisified] = internalUtil.promisify(this.read).bind(this);
this[kDirClosePromisified] = internalUtil.promisify(this.close).bind(this);
}
get path() {
return this[kDirPath];
}
read(options, callback) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
callback = typeof options === 'function' ? options : callback;
if (callback === undefined) {
return this[kDirReadPromisified](options);
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
options = getOptions(options, this[kDirOptions]);
const req = new FSReqCallback();
req.oncomplete = (err, result) => {
if (err || result === null) {
return callback(err, result);
}
getDirent(this[kDirPath], result[0], result[1], callback);
};
this[kDirHandle].read(
options.encoding,
req
);
}
readSync(options) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
options = getOptions(options, this[kDirOptions]);
const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].read(
options.encoding,
undefined,
ctx
);
handleErrorFromBinding(ctx);
if (result === null) {
return result;
}
return getDirent(this[kDirPath], result[0], result[1]);
}
close(callback) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
if (callback === undefined) {
return this[kDirClosePromisified]();
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
this[kDirClosed] = true;
const req = new FSReqCallback();
req.oncomplete = callback;
this[kDirHandle].close(req);
}
closeSync() {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
this[kDirClosed] = true;
const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].close(undefined, ctx);
handleErrorFromBinding(ctx);
return result;
}
async* entries() {
try {
while (true) {
const result = await this[kDirReadPromisified]();
if (result === null) {
break;
}
yield result;
}
} finally {
await this[kDirClosePromisified]();
}
}
}
Object.defineProperty(Dir.prototype, Symbol.asyncIterator, {
value: Dir.prototype.entries,
enumerable: false,
writable: true,
configurable: true,
});
function opendir(path, options, callback) {
callback = typeof options === 'function' ? options : callback;
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
path = getValidatedPath(path);
options = getOptions(options, {
encoding: 'utf8'
});
function opendirCallback(error, handle) {
if (error) {
callback(error);
} else {
callback(null, new Dir(handle, path, options));
}
}
const req = new FSReqCallback();
req.oncomplete = opendirCallback;
dirBinding.opendir(
pathModule.toNamespacedPath(path),
options.encoding,
req
);
}
function opendirSync(path, options) {
path = getValidatedPath(path);
options = getOptions(options, {
encoding: 'utf8'
});
const ctx = { path };
const handle = dirBinding.opendir(
pathModule.toNamespacedPath(path),
options.encoding,
undefined,
ctx
);
handleErrorFromBinding(ctx);
return new Dir(handle, path, options);
}
module.exports = {
Dir,
opendir,
opendirSync
};
|