@zenfs/devel 1.0.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/node-compat.js +7 -4
- package/dist/node_compat/state.d.ts +1 -1
- package/dist/node_compat/state.js +2 -2
- 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 +2 -2
|
@@ -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)]);
|
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
1
53
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
54
|
import assert from 'node:assert/strict';
|
|
3
55
|
import { suite, test } from 'node:test';
|
|
@@ -14,6 +66,13 @@ suite('Truncating', config('truncate'), () => {
|
|
|
14
66
|
fs.writeFileSync(path, data);
|
|
15
67
|
assert.equal(fs.statSync(path).size, size);
|
|
16
68
|
});
|
|
69
|
+
test('Path functions reject file descriptors', config('sync'), async () => {
|
|
70
|
+
const fd = fs.openSync(path, 'r+');
|
|
71
|
+
const error = { name: 'TypeError', code: 'ERR_INVALID_ARG_TYPE' };
|
|
72
|
+
assert.throws(() => fs.truncateSync(fd, 1), error);
|
|
73
|
+
await assert.rejects(fs.promises.truncate(fd, 1), error);
|
|
74
|
+
fs.closeSync(fd);
|
|
75
|
+
});
|
|
17
76
|
test('FD functions', config('sync'), () => {
|
|
18
77
|
const fd = fs.openSync(path, 'r+');
|
|
19
78
|
fs.ftruncateSync(fd, 1024);
|
|
@@ -59,4 +118,34 @@ suite('Truncating', config('truncate'), () => {
|
|
|
59
118
|
await fs.promises.truncate(path, 10);
|
|
60
119
|
assert.equal(await fs.promises.readFile(path, 'utf8'), 'SECR\0\0\0\0\0\0');
|
|
61
120
|
});
|
|
121
|
+
test('Negative lengths truncate to 0', async () => {
|
|
122
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
123
|
+
try {
|
|
124
|
+
const path = 'truncate-negative.txt';
|
|
125
|
+
fs.writeFileSync(path, 'abc');
|
|
126
|
+
fs.truncateSync(path, -1);
|
|
127
|
+
assert.equal(fs.statSync(path).size, 0);
|
|
128
|
+
fs.writeFileSync(path, 'abc');
|
|
129
|
+
const fd = fs.openSync(path, 'r+');
|
|
130
|
+
fs.ftruncateSync(fd, -1);
|
|
131
|
+
assert.equal(fs.fstatSync(fd).size, 0);
|
|
132
|
+
fs.closeSync(fd);
|
|
133
|
+
fs.writeFileSync(path, 'abc');
|
|
134
|
+
await fs.promises.truncate(path, -1);
|
|
135
|
+
assert.equal(fs.statSync(path).size, 0);
|
|
136
|
+
fs.writeFileSync(path, 'abc');
|
|
137
|
+
const handle = __addDisposableResource(env_1, await fs.promises.open(path, 'r+'), true);
|
|
138
|
+
await handle.truncate(-1);
|
|
139
|
+
assert.equal((await handle.stat()).size, 0);
|
|
140
|
+
}
|
|
141
|
+
catch (e_1) {
|
|
142
|
+
env_1.error = e_1;
|
|
143
|
+
env_1.hasError = true;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
const result_1 = __disposeResources(env_1);
|
|
147
|
+
if (result_1)
|
|
148
|
+
await result_1;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
62
151
|
});
|