@zenfs/devel 0.1.0 → 1.1.0
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/dist/bin/make-index.js +37 -74
- package/dist/bin/node-compat.d.ts +2 -0
- package/dist/bin/node-compat.js +340 -0
- package/dist/bin/zci.js +33 -44
- package/dist/bin/zenfs-test.js +45 -78
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/make_index.d.ts +20 -0
- package/dist/make_index.js +49 -0
- package/dist/node_compat/analysis.d.ts +53 -0
- package/dist/node_compat/analysis.js +60 -0
- package/dist/node_compat/changelog.d.ts +25 -0
- package/dist/node_compat/changelog.js +60 -0
- package/dist/node_compat/commits.d.ts +26 -0
- package/dist/node_compat/commits.js +66 -0
- package/dist/node_compat/config.d.ts +6 -0
- package/dist/node_compat/config.js +18 -0
- package/dist/node_compat/docs.d.ts +58 -0
- package/dist/node_compat/docs.js +213 -0
- package/dist/node_compat/git.d.ts +22 -0
- package/dist/node_compat/git.js +74 -0
- package/dist/node_compat/index.d.ts +10 -0
- package/dist/node_compat/index.js +11 -0
- package/dist/node_compat/node.d.ts +42 -0
- package/dist/node_compat/node.js +84 -0
- package/dist/node_compat/output.d.ts +29 -0
- package/dist/node_compat/output.js +95 -0
- package/dist/node_compat/pr.d.ts +59 -0
- package/dist/node_compat/pr.js +125 -0
- package/dist/node_compat/state.d.ts +18 -0
- package/dist/node_compat/state.js +12 -0
- package/dist/node_compat/versions.d.ts +5 -0
- package/dist/node_compat/versions.js +13 -0
- package/dist/node_compat/zenfs.d.ts +3 -0
- package/dist/node_compat/zenfs.js +108 -0
- package/dist/tests/fs/callbacks.test.d.ts +1 -0
- package/dist/tests/fs/callbacks.test.js +41 -0
- package/dist/tests/fs/cp.test.d.ts +1 -0
- package/dist/tests/fs/cp.test.js +186 -0
- package/dist/tests/fs/dir.test.js +40 -0
- package/dist/tests/fs/directory.test.js +42 -0
- package/dist/tests/fs/links.test.js +36 -0
- package/dist/tests/fs/open.test.js +15 -0
- package/dist/tests/fs/read.test.js +139 -2
- package/dist/tests/fs/readFile.test.js +159 -0
- package/dist/tests/fs/stat.test.js +67 -0
- package/dist/tests/fs/streams.test.js +178 -0
- package/dist/tests/fs/tempdir.test.js +8 -0
- package/dist/tests/fs/truncate.test.js +89 -0
- package/dist/tests/fs/watch.test.js +131 -6
- package/dist/tests/fs/write.test.js +182 -0
- package/package.json +8 -3
|
@@ -20,6 +20,42 @@ suite('Links', config('symlinks'), () => {
|
|
|
20
20
|
assert.equal(stats.mode & 0o7777, 0o777);
|
|
21
21
|
fs.unlinkSync('symlink-sync.js');
|
|
22
22
|
});
|
|
23
|
+
test('symlink type may be null', async () => {
|
|
24
|
+
fs.symlinkSync(target, 'symlink-null-sync.js', null);
|
|
25
|
+
assert(fs.lstatSync('symlink-null-sync.js').isSymbolicLink());
|
|
26
|
+
fs.unlinkSync('symlink-null-sync.js');
|
|
27
|
+
await fs.promises.symlink(target, 'symlink-null.js', null);
|
|
28
|
+
assert((await fs.promises.lstat('symlink-null.js')).isSymbolicLink());
|
|
29
|
+
await fs.promises.unlink('symlink-null.js');
|
|
30
|
+
});
|
|
31
|
+
test('symlink rejects invalid types', () => {
|
|
32
|
+
assert.throws(() => fs.symlinkSync(target, 'symlink-invalid.js', 'bogus'), TypeError);
|
|
33
|
+
});
|
|
34
|
+
test('realpath.native resolves like realpath', async () => {
|
|
35
|
+
const expected = fs.realpathSync(symlink);
|
|
36
|
+
assert.equal(fs.realpathSync.native(symlink), expected);
|
|
37
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
38
|
+
fs.realpath.native(symlink, (error, resolved) => (error ? reject(error) : resolve(resolved)));
|
|
39
|
+
assert.equal(await promise, expected);
|
|
40
|
+
});
|
|
41
|
+
test('readlink returns a string unless the encoding is buffer', async () => {
|
|
42
|
+
const link = fs.readlinkSync(symlink);
|
|
43
|
+
assert.equal(typeof link, 'string');
|
|
44
|
+
assert(Buffer.isBuffer(fs.readlinkSync(symlink, { encoding: 'buffer' })));
|
|
45
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
46
|
+
fs.readlink(symlink, { encoding: 'buffer' }, (error, result) => (error ? reject(error) : resolve(result)));
|
|
47
|
+
assert(Buffer.isBuffer(await promise));
|
|
48
|
+
});
|
|
49
|
+
test('symlink onto an existing entry throws EEXIST for the link', async () => {
|
|
50
|
+
fs.symlinkSync('/does-not-exist', 'symlink-broken.js');
|
|
51
|
+
for (const existing of [target, symlink, 'symlink-broken.js']) {
|
|
52
|
+
const error = (thrown) => thrown.code == 'EEXIST' && thrown.syscall == 'symlink' && thrown.path == 'other' && thrown.dest.endsWith(existing);
|
|
53
|
+
assert.throws(() => fs.symlinkSync('other', existing), error);
|
|
54
|
+
await assert.rejects(fs.promises.symlink('other', existing), error);
|
|
55
|
+
}
|
|
56
|
+
assert.equal(fs.readlinkSync('symlink-broken.js', 'utf8'), '/does-not-exist');
|
|
57
|
+
fs.unlinkSync('symlink-broken.js');
|
|
58
|
+
});
|
|
23
59
|
test('lstat file inside symlinked directory #241', async () => {
|
|
24
60
|
await fs.promises.mkdir('/a');
|
|
25
61
|
await fs.promises.writeFile('/a/hello.txt', 'hello world');
|
|
@@ -92,4 +92,19 @@ suite('Opening files', () => {
|
|
|
92
92
|
await result_2;
|
|
93
93
|
}
|
|
94
94
|
});
|
|
95
|
+
test('flags default to "r"', async () => {
|
|
96
|
+
fs.closeSync(fs.openSync(filename));
|
|
97
|
+
fs.closeSync(fs.openSync(filename, undefined));
|
|
98
|
+
assert.throws(() => fs.writeSync(fs.openSync(filename), 'x'), { code: 'EBADF' });
|
|
99
|
+
const handle = await fs.promises.open(filename);
|
|
100
|
+
await handle.close();
|
|
101
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
102
|
+
fs.open(filename, (error, fd) => (error ? reject(error) : resolve(fd)));
|
|
103
|
+
fs.closeSync(await promise);
|
|
104
|
+
});
|
|
105
|
+
test('Windows-only open flags are 0', () => {
|
|
106
|
+
for (const name of ['UV_FS_O_FILEMAP', 'UV_FS_O_TEMPORARY', 'UV_FS_O_SHORT_LIVED', 'UV_FS_O_SEQUENTIAL', 'UV_FS_O_RANDOM']) {
|
|
107
|
+
assert.equal(fs.constants[name], 0, name);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
95
110
|
});
|
|
@@ -131,6 +131,95 @@ suite('read', () => {
|
|
|
131
131
|
assert.equal(buffer.subarray(10, buffer.length).toString(), expected);
|
|
132
132
|
assert.equal(bytesRead, expected.length);
|
|
133
133
|
});
|
|
134
|
+
test('fractional read lengths are truncated asynchronously', config('async'), async () => {
|
|
135
|
+
const env_3 = { stack: [], error: void 0, hasError: false };
|
|
136
|
+
try {
|
|
137
|
+
const handle = __addDisposableResource(env_3, await fs.promises.open(filepath, 'r'), true);
|
|
138
|
+
const { bytesRead } = await handle.read(Buffer.alloc(expected.length), 0, 2.7, 0);
|
|
139
|
+
assert.equal(bytesRead, 2);
|
|
140
|
+
}
|
|
141
|
+
catch (e_3) {
|
|
142
|
+
env_3.error = e_3;
|
|
143
|
+
env_3.hasError = true;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
const result_3 = __disposeResources(env_3);
|
|
147
|
+
if (result_3)
|
|
148
|
+
await result_3;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
test('fractional read lengths are truncated synchronously', config('sync'), () => {
|
|
152
|
+
const fd = fs.openSync(filepath, 'r');
|
|
153
|
+
assert.equal(fs.readSync(fd, Buffer.alloc(expected.length), 0, 2.7, 0), 2);
|
|
154
|
+
fs.closeSync(fd);
|
|
155
|
+
});
|
|
156
|
+
test('read with an options object and a callback', config('async'), async () => {
|
|
157
|
+
const fd = fs.openSync(filepath, 'r');
|
|
158
|
+
const read = (...args) => {
|
|
159
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
160
|
+
fs.read(fd, ...args, (error, bytesRead, buffer) => error ? reject(error) : resolve([bytesRead, buffer]));
|
|
161
|
+
return promise;
|
|
162
|
+
};
|
|
163
|
+
const [bytesRead, buffer] = await read(Buffer.alloc(expected.length), { length: 2, position: 0 });
|
|
164
|
+
assert.equal(bytesRead, 2);
|
|
165
|
+
assert.equal(buffer.subarray(0, 2).toString(), expected.slice(0, 2));
|
|
166
|
+
const target = Buffer.alloc(expected.length);
|
|
167
|
+
const [bytesReadWithBuffer, bufferFromOptions] = await read({ buffer: target, position: 0 });
|
|
168
|
+
assert.equal(bytesReadWithBuffer, expected.length);
|
|
169
|
+
assert.equal(bufferFromOptions, target);
|
|
170
|
+
assert.equal(target.toString(), expected);
|
|
171
|
+
fs.closeSync(fd);
|
|
172
|
+
});
|
|
173
|
+
test('positional reads do not move the file position', config('sync'), async () => {
|
|
174
|
+
const env_4 = { stack: [], error: void 0, hasError: false };
|
|
175
|
+
try {
|
|
176
|
+
const fd = fs.openSync(filepath, 'r');
|
|
177
|
+
const buffer = Buffer.alloc(1);
|
|
178
|
+
fs.readSync(fd, buffer, 0, 1, null);
|
|
179
|
+
fs.readSync(fd, buffer, 0, 1, 3);
|
|
180
|
+
fs.readSync(fd, buffer, 0, 1, null);
|
|
181
|
+
assert.equal(buffer.toString(), expected[1]);
|
|
182
|
+
fs.closeSync(fd);
|
|
183
|
+
const handle = __addDisposableResource(env_4, await fs.promises.open(filepath, 'r'), true);
|
|
184
|
+
await handle.read(buffer, 0, 1, null);
|
|
185
|
+
await handle.read(buffer, 0, 1, 3);
|
|
186
|
+
await handle.read(buffer, 0, 1, null);
|
|
187
|
+
assert.equal(buffer.toString(), expected[1]);
|
|
188
|
+
}
|
|
189
|
+
catch (e_4) {
|
|
190
|
+
env_4.error = e_4;
|
|
191
|
+
env_4.hasError = true;
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
const result_4 = __disposeResources(env_4);
|
|
195
|
+
if (result_4)
|
|
196
|
+
await result_4;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
test('readv reads at the current position', config('sync'), async () => {
|
|
200
|
+
const env_5 = { stack: [], error: void 0, hasError: false };
|
|
201
|
+
try {
|
|
202
|
+
const fd = fs.openSync(filepath, 'r');
|
|
203
|
+
const [first, second] = [Buffer.alloc(1), Buffer.alloc(2)];
|
|
204
|
+
assert.equal(fs.readvSync(fd, [first]), 1);
|
|
205
|
+
assert.equal(fs.readvSync(fd, [second]), 2);
|
|
206
|
+
assert.equal(first.toString() + second.toString(), expected.slice(0, 3));
|
|
207
|
+
fs.closeSync(fd);
|
|
208
|
+
const handle = __addDisposableResource(env_5, await fs.promises.open(filepath, 'r'), true);
|
|
209
|
+
await handle.readv([first], 2);
|
|
210
|
+
await handle.readv([second]);
|
|
211
|
+
assert.equal(first.toString() + second.toString(), expected[2] + expected.slice(0, 2));
|
|
212
|
+
}
|
|
213
|
+
catch (e_5) {
|
|
214
|
+
env_5.error = e_5;
|
|
215
|
+
env_5.hasError = true;
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
const result_5 = __disposeResources(env_5);
|
|
219
|
+
if (result_5)
|
|
220
|
+
await result_5;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
134
223
|
test('read using callback API #239', config('sync', 'write'), async () => {
|
|
135
224
|
const path = '/text.txt';
|
|
136
225
|
fs.writeFileSync(path, 'hello world');
|
|
@@ -138,12 +227,60 @@ suite('read', () => {
|
|
|
138
227
|
const fd = (await promisify(fs.open)(path, 0, 0));
|
|
139
228
|
const read = promisify(fs.read);
|
|
140
229
|
const buf = Buffer.alloc(1024);
|
|
141
|
-
const n0 = await read(fd, buf, 0, 1024,
|
|
230
|
+
const { bytesRead: n0 } = await read(fd, buf, 0, 1024, null);
|
|
142
231
|
assert.equal(n0, 11);
|
|
143
232
|
assert.equal(buf.subarray(0, n0).toString('utf8'), 'hello world');
|
|
144
|
-
const n1 = await read(fd, buf, 0, 1024,
|
|
233
|
+
const { bytesRead: n1 } = await read(fd, buf, 0, 1024, null);
|
|
145
234
|
assert.equal(n1, 0);
|
|
146
235
|
assert.equal(buf.subarray(0, n1).toString('utf8'), '');
|
|
147
236
|
await promisify(fs.close)(fd);
|
|
148
237
|
});
|
|
238
|
+
test('util.promisify resolves with named results', config('async'), async () => {
|
|
239
|
+
const path = 'promisify-results.txt';
|
|
240
|
+
fs.writeFileSync(path, 'abc');
|
|
241
|
+
const fd = fs.openSync(path, 'r+');
|
|
242
|
+
const read = await promisify(fs.read)(fd, Buffer.alloc(3), 0, 3, 0);
|
|
243
|
+
assert.equal(read.bytesRead, 3);
|
|
244
|
+
assert.equal(read.buffer.toString(), 'abc');
|
|
245
|
+
const written = await promisify(fs.write)(fd, Buffer.from('xy'), 0, 2, 0);
|
|
246
|
+
assert.equal(written.bytesWritten, 2);
|
|
247
|
+
assert.equal(written.buffer.toString(), 'xy');
|
|
248
|
+
const readv = await promisify(fs.readv)(fd, [Buffer.alloc(3)], 0);
|
|
249
|
+
assert.equal(readv.bytesRead, 3);
|
|
250
|
+
assert.equal(readv.buffers[0].toString(), 'xyc');
|
|
251
|
+
const writev = await promisify(fs.writev)(fd, [Buffer.from('z')], 0);
|
|
252
|
+
assert.equal(writev.bytesWritten, 1);
|
|
253
|
+
assert.equal(writev.buffer.length, 1);
|
|
254
|
+
fs.closeSync(fd);
|
|
255
|
+
assert.equal(await promisify(fs.exists)(path), true);
|
|
256
|
+
assert.equal(await promisify(fs.exists)('promisify-missing.txt'), false);
|
|
257
|
+
});
|
|
258
|
+
test('reads without a buffer allocate 16 KiB', config('async'), async () => {
|
|
259
|
+
const env_6 = { stack: [], error: void 0, hasError: false };
|
|
260
|
+
try {
|
|
261
|
+
const handle = __addDisposableResource(env_6, await fs.promises.open(filepath, 'r'), true);
|
|
262
|
+
for (const result of [await handle.read(), await handle.read({ position: 0 })]) {
|
|
263
|
+
assert(Buffer.isBuffer(result.buffer));
|
|
264
|
+
assert.equal(result.buffer.length, 16384);
|
|
265
|
+
}
|
|
266
|
+
const fd = fs.openSync(filepath, 'r');
|
|
267
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
268
|
+
fs.read(fd, (error, bytesRead, buffer) => (error ? reject(error) : resolve([bytesRead, buffer])));
|
|
269
|
+
const [bytesRead, buffer] = await promise;
|
|
270
|
+
fs.closeSync(fd);
|
|
271
|
+
assert.equal(bytesRead, expected.length);
|
|
272
|
+
assert(Buffer.isBuffer(buffer));
|
|
273
|
+
assert.equal(buffer.byteLength, 16384);
|
|
274
|
+
assert.equal(buffer.subarray(0, bytesRead).toString(), expected);
|
|
275
|
+
}
|
|
276
|
+
catch (e_6) {
|
|
277
|
+
env_6.error = e_6;
|
|
278
|
+
env_6.hasError = true;
|
|
279
|
+
}
|
|
280
|
+
finally {
|
|
281
|
+
const result_6 = __disposeResources(env_6);
|
|
282
|
+
if (result_6)
|
|
283
|
+
await result_6;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
149
286
|
});
|
|
@@ -107,4 +107,163 @@ suite('Reading', () => {
|
|
|
107
107
|
assert.equal(content[i], '…');
|
|
108
108
|
}
|
|
109
109
|
});
|
|
110
|
+
test('File descriptors are read from the current position and left open', config('sync'), () => {
|
|
111
|
+
const file = 'readFile-fd.txt';
|
|
112
|
+
fs.writeFileSync(file, 'abcdef');
|
|
113
|
+
const fd = fs.openSync(file, 'r');
|
|
114
|
+
fs.readSync(fd, Buffer.alloc(2));
|
|
115
|
+
assert.equal(fs.readFileSync(fd, 'utf8'), 'cdef');
|
|
116
|
+
assert.equal(fs.readFileSync(fd, 'utf8'), '');
|
|
117
|
+
fs.fstatSync(fd);
|
|
118
|
+
fs.closeSync(fd);
|
|
119
|
+
fs.unlinkSync(file);
|
|
120
|
+
});
|
|
121
|
+
test('File descriptors are written at the current position and left open', config('sync'), () => {
|
|
122
|
+
const file = 'writeFile-fd.txt';
|
|
123
|
+
const fd = fs.openSync(file, 'w+');
|
|
124
|
+
fs.writeSync(fd, 'abc');
|
|
125
|
+
fs.writeFileSync(fd, 'XY');
|
|
126
|
+
fs.writeFileSync(fd, 'Z');
|
|
127
|
+
fs.closeSync(fd);
|
|
128
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'abcXYZ');
|
|
129
|
+
const appendFd = fs.openSync(file, 'a');
|
|
130
|
+
fs.appendFileSync(appendFd, '1');
|
|
131
|
+
fs.appendFileSync(appendFd, '2');
|
|
132
|
+
fs.closeSync(appendFd);
|
|
133
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'abcXYZ12');
|
|
134
|
+
fs.unlinkSync(file);
|
|
135
|
+
});
|
|
136
|
+
test('File handles use the current position and are left open', config('async'), async () => {
|
|
137
|
+
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
138
|
+
try {
|
|
139
|
+
const file = 'readFile-handle.txt';
|
|
140
|
+
const handle = __addDisposableResource(env_2, await fs.promises.open(file, 'w+'), true);
|
|
141
|
+
await handle.writeFile('abc');
|
|
142
|
+
await handle.writeFile('def');
|
|
143
|
+
assert.equal(await handle.readFile('utf8'), '');
|
|
144
|
+
assert.equal(await fs.promises.readFile(handle, 'utf8'), '');
|
|
145
|
+
await handle.stat();
|
|
146
|
+
assert.equal(await fs.promises.readFile(file, 'utf8'), 'abcdef');
|
|
147
|
+
}
|
|
148
|
+
catch (e_2) {
|
|
149
|
+
env_2.error = e_2;
|
|
150
|
+
env_2.hasError = true;
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
const result_2 = __disposeResources(env_2);
|
|
154
|
+
if (result_2)
|
|
155
|
+
await result_2;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
test('Callback functions accept file descriptors', config('async'), async () => {
|
|
159
|
+
const file = 'callback-fd.txt';
|
|
160
|
+
fs.writeFileSync(file, 'abc');
|
|
161
|
+
const fd = fs.openSync(file, 'r+');
|
|
162
|
+
const read = Promise.withResolvers();
|
|
163
|
+
fs.readFile(fd, 'utf8', (error, data) => (error ? read.reject(error) : read.resolve(data)));
|
|
164
|
+
assert.equal(await read.promise, 'abc');
|
|
165
|
+
const write = Promise.withResolvers();
|
|
166
|
+
fs.writeFile(fd, 'd', error => (error ? write.reject(error) : write.resolve()));
|
|
167
|
+
await write.promise;
|
|
168
|
+
fs.closeSync(fd);
|
|
169
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'abcd');
|
|
170
|
+
});
|
|
171
|
+
test('readFile with the a+ flag reads the whole file', config('sync'), () => {
|
|
172
|
+
const file = 'readFile-append.txt';
|
|
173
|
+
fs.writeFileSync(file, 'xyz');
|
|
174
|
+
assert.equal(fs.readFileSync(file, { encoding: 'utf8', flag: 'a+' }), 'xyz');
|
|
175
|
+
fs.unlinkSync(file);
|
|
176
|
+
});
|
|
177
|
+
test('readFile reads into a caller-supplied buffer', config('async'), async () => {
|
|
178
|
+
const env_3 = { stack: [], error: void 0, hasError: false };
|
|
179
|
+
try {
|
|
180
|
+
const file = 'readFile-buffer.txt';
|
|
181
|
+
fs.writeFileSync(file, 'abcdef');
|
|
182
|
+
const buffer = Buffer.alloc(16);
|
|
183
|
+
const contents = await fs.promises.readFile(file, { buffer });
|
|
184
|
+
assert.equal(contents.buffer, buffer.buffer);
|
|
185
|
+
assert.equal(contents.toString(), 'abcdef');
|
|
186
|
+
assert.equal((await fs.promises.readFile(file, { buffer: size => Buffer.alloc(size) })).toString(), 'abcdef');
|
|
187
|
+
assert.equal(await fs.promises.readFile(file, { buffer, encoding: 'utf8' }), 'abcdef');
|
|
188
|
+
const handle = __addDisposableResource(env_3, await fs.promises.open(file), true);
|
|
189
|
+
assert.equal((await handle.readFile({ buffer })).buffer, buffer.buffer);
|
|
190
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
191
|
+
fs.readFile(file, { buffer }, (error, data) => (error ? reject(error) : resolve(data)));
|
|
192
|
+
assert.equal((await promise).buffer, buffer.buffer);
|
|
193
|
+
await assert.rejects(fs.promises.readFile(file, { buffer: Buffer.alloc(2) }), { code: 'ERR_INVALID_ARG_VALUE' });
|
|
194
|
+
await assert.rejects(fs.promises.readFile(file, { buffer: 'x' }), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
195
|
+
fs.unlinkSync(file);
|
|
196
|
+
}
|
|
197
|
+
catch (e_3) {
|
|
198
|
+
env_3.error = e_3;
|
|
199
|
+
env_3.hasError = true;
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
const result_3 = __disposeResources(env_3);
|
|
203
|
+
if (result_3)
|
|
204
|
+
await result_3;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
test('openAsBlob', config('async'), async () => {
|
|
208
|
+
const file = 'openAsBlob.txt';
|
|
209
|
+
fs.writeFileSync(file, 'blob contents');
|
|
210
|
+
const blob = await fs.openAsBlob(file, { type: 'text/plain' });
|
|
211
|
+
assert(blob instanceof Blob);
|
|
212
|
+
assert.equal(blob.type, 'text/plain');
|
|
213
|
+
assert.equal(await blob.text(), 'blob contents');
|
|
214
|
+
await assert.rejects(async () => await fs.openAsBlob(file, null), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
215
|
+
await assert.rejects(async () => await fs.openAsBlob(file, { type: 1 }), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
216
|
+
await assert.rejects(async () => await fs.openAsBlob('openAsBlob-missing.txt'), { code: 'ENOENT' });
|
|
217
|
+
fs.unlinkSync(file);
|
|
218
|
+
});
|
|
219
|
+
test('openAsBlobSync', config('sync'), async () => {
|
|
220
|
+
const file = 'openAsBlobSync.txt';
|
|
221
|
+
fs.writeFileSync(file, 'blob contents');
|
|
222
|
+
for (const path of [file, Buffer.from(file)]) {
|
|
223
|
+
const blob = fs.openAsBlobSync(path, { type: 'text/plain' });
|
|
224
|
+
assert(blob instanceof Blob);
|
|
225
|
+
assert.equal(blob.size, 13);
|
|
226
|
+
assert.equal(blob.type, 'text/plain');
|
|
227
|
+
assert.equal(await blob.text(), 'blob contents');
|
|
228
|
+
}
|
|
229
|
+
assert.equal(fs.openAsBlobSync(file).type, '');
|
|
230
|
+
assert.throws(() => fs.openAsBlobSync(1), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
231
|
+
assert.throws(() => fs.openAsBlobSync(file, null), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
232
|
+
assert.throws(() => fs.openAsBlobSync(file, { type: 1 }), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
233
|
+
assert.throws(() => fs.openAsBlobSync('openAsBlobSync-missing.txt'), { code: 'ENOENT' });
|
|
234
|
+
fs.unlinkSync(file);
|
|
235
|
+
});
|
|
236
|
+
test('readFile and writeFile reject with an aborted signal', async () => {
|
|
237
|
+
const env_4 = { stack: [], error: void 0, hasError: false };
|
|
238
|
+
try {
|
|
239
|
+
const path = 'abort-signal.txt';
|
|
240
|
+
fs.writeFileSync(path, 'contents');
|
|
241
|
+
const reason = new Error('reason');
|
|
242
|
+
const signal = AbortSignal.abort(reason);
|
|
243
|
+
const expected = { name: 'AbortError', code: 'ABORT_ERR', cause: reason };
|
|
244
|
+
await assert.rejects(fs.promises.readFile(path, { signal }), expected);
|
|
245
|
+
await assert.rejects(fs.promises.writeFile('abort-signal-new.txt', 'x', { signal }), expected);
|
|
246
|
+
assert(!fs.existsSync('abort-signal-new.txt'));
|
|
247
|
+
const handle = __addDisposableResource(env_4, await fs.promises.open(path, 'r+'), true);
|
|
248
|
+
await assert.rejects(handle.readFile({ signal }), expected);
|
|
249
|
+
await assert.rejects(handle.writeFile('x', { signal }), expected);
|
|
250
|
+
assert.equal(fs.readFileSync(path, 'utf8'), 'contents');
|
|
251
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
252
|
+
fs.readFile(path, { signal }, resolve);
|
|
253
|
+
const error = await promise;
|
|
254
|
+
assert.throws(() => {
|
|
255
|
+
throw error;
|
|
256
|
+
}, expected);
|
|
257
|
+
assert.equal(await fs.promises.readFile(path, { signal: new AbortController().signal, encoding: 'utf8' }), 'contents');
|
|
258
|
+
}
|
|
259
|
+
catch (e_4) {
|
|
260
|
+
env_4.error = e_4;
|
|
261
|
+
env_4.hasError = true;
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
const result_4 = __disposeResources(env_4);
|
|
265
|
+
if (result_4)
|
|
266
|
+
await result_4;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
110
269
|
});
|
|
@@ -90,4 +90,71 @@ suite('Stats', () => {
|
|
|
90
90
|
assert(!stats.isSymbolicLink());
|
|
91
91
|
assert(stats instanceof fs.Stats);
|
|
92
92
|
});
|
|
93
|
+
test('Date properties are enumerable and stored when accessed', config('sync'), () => {
|
|
94
|
+
const stats = fs.statSync('/');
|
|
95
|
+
const enumerated = [];
|
|
96
|
+
for (const key in stats)
|
|
97
|
+
enumerated.push(key);
|
|
98
|
+
for (const name of ['atime', 'mtime', 'ctime', 'birthtime'])
|
|
99
|
+
assert(enumerated.includes(name));
|
|
100
|
+
assert(!Object.keys(stats).includes('atime'));
|
|
101
|
+
assert(stats.atime instanceof Date);
|
|
102
|
+
assert(Object.keys(stats).includes('atime'));
|
|
103
|
+
const atimeMs = stats.atimeMs;
|
|
104
|
+
stats.atime = new Date(0);
|
|
105
|
+
assert.equal(stats.atime.getTime(), 0);
|
|
106
|
+
assert.equal(stats.atimeMs, atimeMs);
|
|
107
|
+
});
|
|
108
|
+
test('stat and lstat can be aborted', config('async'), async () => {
|
|
109
|
+
const reason = new Error('reason');
|
|
110
|
+
const error = { name: 'AbortError', code: 'ABORT_ERR', cause: reason };
|
|
111
|
+
await assert.rejects(fs.promises.stat('/', { signal: AbortSignal.abort(reason) }), error);
|
|
112
|
+
await assert.rejects(fs.promises.lstat('/', { signal: AbortSignal.abort(reason) }), error);
|
|
113
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
114
|
+
fs.stat('/', { signal: AbortSignal.abort(reason) }, resolve);
|
|
115
|
+
assert.equal((await promise).name, 'AbortError');
|
|
116
|
+
const controller = new AbortController();
|
|
117
|
+
const pending = fs.promises.stat('/', { signal: controller.signal });
|
|
118
|
+
controller.abort(reason);
|
|
119
|
+
await assert.rejects(pending, error);
|
|
120
|
+
});
|
|
121
|
+
test('bigint stats have bigint fields', async () => {
|
|
122
|
+
const path = 'stat-bigint.txt';
|
|
123
|
+
fs.writeFileSync(path, 'abc');
|
|
124
|
+
const fields = [
|
|
125
|
+
'dev',
|
|
126
|
+
'ino',
|
|
127
|
+
'mode',
|
|
128
|
+
'nlink',
|
|
129
|
+
'uid',
|
|
130
|
+
'gid',
|
|
131
|
+
'rdev',
|
|
132
|
+
'size',
|
|
133
|
+
'blksize',
|
|
134
|
+
'blocks',
|
|
135
|
+
'atimeMs',
|
|
136
|
+
'mtimeMs',
|
|
137
|
+
'ctimeMs',
|
|
138
|
+
'birthtimeMs',
|
|
139
|
+
];
|
|
140
|
+
for (const stats of [
|
|
141
|
+
fs.statSync(path, { bigint: true }),
|
|
142
|
+
fs.lstatSync(path, { bigint: true }),
|
|
143
|
+
await fs.promises.stat(path, { bigint: true }),
|
|
144
|
+
]) {
|
|
145
|
+
for (const field of fields)
|
|
146
|
+
assert.equal(typeof stats[field], 'bigint', field);
|
|
147
|
+
assert.equal(stats.size, 3n);
|
|
148
|
+
assert(stats.isFile());
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
test('watchFile with bigint', async () => {
|
|
152
|
+
const path = 'stat-watch-bigint.txt';
|
|
153
|
+
fs.writeFileSync(path, 'abc');
|
|
154
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
155
|
+
fs.watchFile(path, { bigint: true, interval: 5 }, (current, previous) => resolve([current.size, previous.size]));
|
|
156
|
+
setTimeout(() => fs.writeFileSync(path, 'abcd'), 20);
|
|
157
|
+
assert.deepEqual(await promise, [4n, 3n]);
|
|
158
|
+
fs.unwatchFile(path);
|
|
159
|
+
});
|
|
93
160
|
});
|
|
@@ -158,4 +158,182 @@ suite('Streams', config('streams'), () => {
|
|
|
158
158
|
chunks.push(chunk);
|
|
159
159
|
assert.deepEqual(chunks, []);
|
|
160
160
|
});
|
|
161
|
+
const text = async (stream) => {
|
|
162
|
+
const chunks = [];
|
|
163
|
+
for await (const chunk of stream)
|
|
164
|
+
chunks.push(Buffer.from(chunk));
|
|
165
|
+
return Buffer.concat(chunks).toString();
|
|
166
|
+
};
|
|
167
|
+
const isOpen = (fd) => {
|
|
168
|
+
try {
|
|
169
|
+
fs.fstatSync(fd);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
test('createReadStream uses the fd option and closes it', async () => {
|
|
177
|
+
const fd = fs.openSync(testFilePath, 'r');
|
|
178
|
+
assert.equal(await text(fs.createReadStream(null, { fd })), testData);
|
|
179
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
180
|
+
assert(!isOpen(fd));
|
|
181
|
+
});
|
|
182
|
+
test('createReadStream leaves the fd open with autoClose false', async () => {
|
|
183
|
+
const fd = fs.openSync(testFilePath, 'r');
|
|
184
|
+
assert.equal(await text(fs.createReadStream(null, { fd, autoClose: false, start: 7 })), testData.slice(7));
|
|
185
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
186
|
+
assert(isOpen(fd));
|
|
187
|
+
fs.closeSync(fd);
|
|
188
|
+
});
|
|
189
|
+
test('createWriteStream uses the fd option', async () => {
|
|
190
|
+
const file = 'stream-write-fd.txt';
|
|
191
|
+
const fd = fs.openSync(file, 'w');
|
|
192
|
+
const stream = fs.createWriteStream(null, { fd });
|
|
193
|
+
await new Promise((resolve, reject) => stream.end('written', (error) => (error ? reject(error) : resolve())));
|
|
194
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
195
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'written');
|
|
196
|
+
assert(!isOpen(fd));
|
|
197
|
+
});
|
|
198
|
+
test('createWriteStream uses the flags option', async () => {
|
|
199
|
+
const file = 'stream-append.txt';
|
|
200
|
+
fs.writeFileSync(file, 'x');
|
|
201
|
+
const stream = fs.createWriteStream(file, { flags: 'a' });
|
|
202
|
+
await new Promise(resolve => stream.end('y', resolve));
|
|
203
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
204
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'xy');
|
|
205
|
+
});
|
|
206
|
+
test('readableWebStream closes the handle with autoClose', async () => {
|
|
207
|
+
const handle = await fs.promises.open(testFilePath);
|
|
208
|
+
const chunks = [];
|
|
209
|
+
for await (const chunk of handle.readableWebStream({ autoClose: true }))
|
|
210
|
+
chunks.push(Buffer.from(chunk));
|
|
211
|
+
assert.equal(Buffer.concat(chunks).toString(), testData);
|
|
212
|
+
assert.equal(handle.fd, -1);
|
|
213
|
+
const cancelled = await fs.promises.open(testFilePath);
|
|
214
|
+
await cancelled.readableWebStream({ autoClose: true }).cancel();
|
|
215
|
+
assert.equal(cancelled.fd, -1);
|
|
216
|
+
});
|
|
217
|
+
test('Streams without start read from the current position', async () => {
|
|
218
|
+
const handle = await fs.promises.open(testFilePath);
|
|
219
|
+
await handle.read(Buffer.alloc(7));
|
|
220
|
+
assert.equal(await text(handle.createReadStream({ autoClose: false })), testData.slice(7));
|
|
221
|
+
assert.equal((await handle.read(Buffer.alloc(1))).bytesRead, 0);
|
|
222
|
+
await handle.close();
|
|
223
|
+
const fd = fs.openSync(testFilePath, 'r');
|
|
224
|
+
fs.readSync(fd, Buffer.alloc(7));
|
|
225
|
+
assert.equal(await text(fs.createReadStream(null, { fd, autoClose: false, end: 2 })), testData.slice(7, 10));
|
|
226
|
+
fs.closeSync(fd);
|
|
227
|
+
const webHandle = await fs.promises.open(testFilePath);
|
|
228
|
+
await webHandle.read(Buffer.alloc(7));
|
|
229
|
+
assert.equal(await text(webHandle.readableWebStream()), testData.slice(7));
|
|
230
|
+
await webHandle.close();
|
|
231
|
+
});
|
|
232
|
+
test('Streams with start do not move the current position', async () => {
|
|
233
|
+
const fd = fs.openSync(testFilePath, 'r');
|
|
234
|
+
fs.readSync(fd, Buffer.alloc(7));
|
|
235
|
+
assert.equal(await text(fs.createReadStream(null, { fd, autoClose: false, start: 0, end: 4 })), testData.slice(0, 5));
|
|
236
|
+
const next = Buffer.alloc(1);
|
|
237
|
+
fs.readSync(fd, next);
|
|
238
|
+
assert.equal(next.toString(), testData[7]);
|
|
239
|
+
fs.closeSync(fd);
|
|
240
|
+
});
|
|
241
|
+
test('Streams expose fd, which stays open with autoClose false', async () => {
|
|
242
|
+
const path = 'stream-fd.txt';
|
|
243
|
+
const stream = fs.createWriteStream(path, { autoClose: false });
|
|
244
|
+
assert.equal(stream.fd, null);
|
|
245
|
+
await new Promise(resolve => stream.end('abc', () => resolve(null)));
|
|
246
|
+
assert.equal(typeof stream.fd, 'number');
|
|
247
|
+
fs.writeSync(stream.fd, 'd');
|
|
248
|
+
fs.closeSync(stream.fd);
|
|
249
|
+
assert.equal(fs.readFileSync(path, 'utf8'), 'abcd');
|
|
250
|
+
const closing = fs.createWriteStream(path);
|
|
251
|
+
await new Promise(resolve => closing.end('x', () => closing.on('close', resolve)));
|
|
252
|
+
assert.equal(closing.fd, null);
|
|
253
|
+
});
|
|
254
|
+
test('Write streams without start write at the current position', async () => {
|
|
255
|
+
const path = 'stream-write-position.txt';
|
|
256
|
+
fs.writeFileSync(path, '12345');
|
|
257
|
+
const fd = fs.openSync(path, 'r+');
|
|
258
|
+
fs.readSync(fd, Buffer.alloc(2));
|
|
259
|
+
const stream = fs.createWriteStream(null, { fd, autoClose: false });
|
|
260
|
+
await new Promise(resolve => stream.end('XY', () => resolve(null)));
|
|
261
|
+
fs.writeSync(fd, '!');
|
|
262
|
+
fs.closeSync(fd);
|
|
263
|
+
assert.equal(fs.readFileSync(path, 'utf8'), '12XY!');
|
|
264
|
+
});
|
|
265
|
+
test('Streams emit open and ready only when they open the file', async () => {
|
|
266
|
+
const path = 'stream-events.txt';
|
|
267
|
+
fs.writeFileSync(path, 'abc');
|
|
268
|
+
async function events(stream) {
|
|
269
|
+
const emitted = [];
|
|
270
|
+
for (const event of ['open', 'ready', 'end', 'finish', 'close']) {
|
|
271
|
+
stream.on(event, (fd) => emitted.push(event == 'open' ? `open(${typeof fd})` : event));
|
|
272
|
+
}
|
|
273
|
+
if (stream instanceof fs.ReadStream)
|
|
274
|
+
stream.resume();
|
|
275
|
+
else
|
|
276
|
+
stream.end();
|
|
277
|
+
await new Promise(resolve => stream.on('close', resolve));
|
|
278
|
+
return emitted;
|
|
279
|
+
}
|
|
280
|
+
assert.deepEqual(await events(fs.createReadStream(path)), ['open(number)', 'ready', 'end', 'close']);
|
|
281
|
+
assert.deepEqual(await events(fs.createWriteStream(path)), ['open(number)', 'ready', 'finish', 'close']);
|
|
282
|
+
assert.deepEqual(await events(fs.createReadStream(null, { fd: fs.openSync(path, 'r') })), ['end', 'close']);
|
|
283
|
+
assert.deepEqual(await events(fs.createWriteStream(null, { fd: fs.openSync(path, 'w') })), ['finish', 'close']);
|
|
284
|
+
});
|
|
285
|
+
test('Streams use the fs option', async () => {
|
|
286
|
+
const path = 'stream-fs-option.txt';
|
|
287
|
+
fs.writeFileSync(path, 'hello');
|
|
288
|
+
const calls = [];
|
|
289
|
+
const operations = {
|
|
290
|
+
open: (_, flags, mode, callback) => {
|
|
291
|
+
calls.push('open');
|
|
292
|
+
fs.open(path, flags, mode, callback);
|
|
293
|
+
},
|
|
294
|
+
read: (...args) => {
|
|
295
|
+
calls.push('read');
|
|
296
|
+
fs.read(...args);
|
|
297
|
+
},
|
|
298
|
+
write: (...args) => {
|
|
299
|
+
calls.push('write');
|
|
300
|
+
fs.write(...args);
|
|
301
|
+
},
|
|
302
|
+
close: (fd, callback) => {
|
|
303
|
+
calls.push('close');
|
|
304
|
+
fs.close(fd, callback);
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
let data = '';
|
|
308
|
+
for await (const chunk of fs.createReadStream(path, { fs: operations, encoding: 'utf8' }))
|
|
309
|
+
data += chunk;
|
|
310
|
+
assert.equal(data, 'hello');
|
|
311
|
+
assert.deepEqual([calls[0], calls.at(-1)], ['open', 'close']);
|
|
312
|
+
assert(calls.includes('read'));
|
|
313
|
+
calls.length = 0;
|
|
314
|
+
const writer = fs.createWriteStream(path, { fs: operations });
|
|
315
|
+
await new Promise(resolve => writer.end('written', () => writer.on('close', resolve)));
|
|
316
|
+
assert.equal(fs.readFileSync(path, 'utf8'), 'written');
|
|
317
|
+
assert.deepEqual(calls, ['open', 'write', 'close']);
|
|
318
|
+
// Without opening or closing, `open` and `close` are not needed
|
|
319
|
+
calls.length = 0;
|
|
320
|
+
const fd = fs.openSync(path, 'r');
|
|
321
|
+
data = '';
|
|
322
|
+
for await (const chunk of fs.createReadStream(null, { fd, fs: { read: operations.read }, autoClose: false, encoding: 'utf8' }))
|
|
323
|
+
data += chunk;
|
|
324
|
+
fs.closeSync(fd);
|
|
325
|
+
assert.equal(data, 'written');
|
|
326
|
+
assert.throws(() => fs.createReadStream(path, { fs: { read: operations.read, close: operations.close } }), {
|
|
327
|
+
code: 'ERR_INVALID_ARG_TYPE',
|
|
328
|
+
message: /options\.fs\.open/,
|
|
329
|
+
});
|
|
330
|
+
assert.throws(() => fs.createReadStream(path, { fs: { open: operations.open, read: operations.read } }), {
|
|
331
|
+
code: 'ERR_INVALID_ARG_TYPE',
|
|
332
|
+
message: /options\.fs\.close/,
|
|
333
|
+
});
|
|
334
|
+
assert.throws(() => fs.createWriteStream(path, { fs: { open: operations.open, close: operations.close } }), {
|
|
335
|
+
code: 'ERR_INVALID_ARG_TYPE',
|
|
336
|
+
message: /options\.fs\.write/,
|
|
337
|
+
});
|
|
338
|
+
});
|
|
161
339
|
});
|
|
@@ -62,6 +62,14 @@ suite('Temporary Directories', config('tempdir'), () => {
|
|
|
62
62
|
assert.deepEqual(fs.readdirSync('/tmp'), [basename(path)]);
|
|
63
63
|
fs.rmdirSync(path);
|
|
64
64
|
});
|
|
65
|
+
test('mkdtemp with an empty prefix', async () => {
|
|
66
|
+
const path = fs.mkdtempSync('');
|
|
67
|
+
assert(fs.statSync(path).isDirectory());
|
|
68
|
+
fs.rmdirSync(path);
|
|
69
|
+
const promised = await fs.promises.mkdtemp('');
|
|
70
|
+
assert(fs.statSync(promised).isDirectory());
|
|
71
|
+
fs.rmdirSync(promised);
|
|
72
|
+
});
|
|
65
73
|
test('mkdtemp', config('async'), async () => {
|
|
66
74
|
const path = await fs.promises.mkdtemp('/tmp/test-', { encoding: 'utf8' });
|
|
67
75
|
assert.deepEqual(await fs.promises.readdir('/tmp'), [basename(path)]);
|