Skip to content

Commit af163dc

Browse files
authoredDec 2, 2024··
fix: accept passing a FileHandle to createReadStream and createWriteStream (#1077)
* fix: let `fd` option be `filehandle` in `fs.createReadStream`, `fs.createWriteStream` * add createReadStream test * prettier
1 parent d68c64d commit af163dc

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed
 

‎src/__tests__/volume.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { promisify } from 'util';
12
import { URL } from 'url';
23
import { Link } from '../node';
34
import Stats from '../Stats';
@@ -1431,4 +1432,41 @@ describe('volume', () => {
14311432
expect(new StatWatcher(vol).vol).toBe(vol);
14321433
});
14331434
});
1435+
describe('.createWriteStream', () => {
1436+
it('accepts filehandle as fd option', async () => {
1437+
const vol = new Volume();
1438+
const fh = await vol.promises.open('/test.txt', 'wx', 0o600);
1439+
const writeStream = vol.createWriteStream('', { fd: fh });
1440+
await promisify(writeStream.write.bind(writeStream))(Buffer.from('Hello'));
1441+
await promisify(writeStream.close.bind(writeStream))();
1442+
expect(vol.toJSON()).toEqual({
1443+
'/test.txt': 'Hello',
1444+
});
1445+
});
1446+
});
1447+
describe('.createReadStream', () => {
1448+
it('accepts filehandle as fd option', done => {
1449+
const vol = Volume.fromJSON({
1450+
'/test.txt': 'Hello',
1451+
});
1452+
vol.promises
1453+
.open('/test.txt', 'r')
1454+
.then(fh => {
1455+
const readStream = vol.createReadStream('/this/should/be/ignored', { fd: fh });
1456+
readStream.setEncoding('utf8');
1457+
let readData = '';
1458+
readStream.on('readable', () => {
1459+
const chunk = readStream.read();
1460+
if (chunk != null) readData += chunk;
1461+
});
1462+
readStream.on('end', () => {
1463+
expect(readData).toEqual('Hello');
1464+
done();
1465+
});
1466+
})
1467+
.catch(err => {
1468+
expect(err).toBeNull();
1469+
});
1470+
});
1471+
});
14341472
});

‎src/volume.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ function FsReadStream(vol, path, options) {
22602260
Readable.call(this, options);
22612261

22622262
this.path = pathToFilename(path);
2263-
this.fd = options.fd === undefined ? null : options.fd;
2263+
this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
22642264
this.flags = options.flags === undefined ? 'r' : options.flags;
22652265
this.mode = options.mode === undefined ? 0o666 : options.mode;
22662266

@@ -2429,7 +2429,7 @@ function FsWriteStream(vol, path, options) {
24292429
Writable.call(this, options);
24302430

24312431
this.path = pathToFilename(path);
2432-
this.fd = options.fd === undefined ? null : options.fd;
2432+
this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
24332433
this.flags = options.flags === undefined ? 'w' : options.flags;
24342434
this.mode = options.mode === undefined ? 0o666 : options.mode;
24352435

0 commit comments

Comments
 (0)
Please sign in to comment.