Skip to content

Commit

Permalink
fix: let fd option be filehandle in fs.createReadStream, `fs.cr…
Browse files Browse the repository at this point in the history
…eateWriteStream`
  • Loading branch information
graue committed Nov 28, 2024
1 parent 20476f3 commit 3dbf3ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { promisify } from 'util';
import { URL } from 'url';
import { Link } from '../node';
import Stats from '../Stats';
Expand Down Expand Up @@ -1431,4 +1432,16 @@ describe('volume', () => {
expect(new StatWatcher(vol).vol).toBe(vol);
});
});
describe('.createWriteStream', () => {
it('accepts filehandle as fd option', async () => {
const vol = new Volume();
const fh = await vol.promises.open('/test.txt', 'wx', 0o600);
const writeStream = vol.createWriteStream('', { fd: fh });
await promisify(writeStream.write.bind(writeStream))(Buffer.from('Hello'));
await promisify(writeStream.close.bind(writeStream))();
expect(vol.toJSON()).toEqual({
'/test.txt': 'Hello',
});
});
});
});
4 changes: 2 additions & 2 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ function FsReadStream(vol, path, options) {
Readable.call(this, options);

this.path = pathToFilename(path);
this.fd = options.fd === undefined ? null : options.fd;
this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
this.flags = options.flags === undefined ? 'r' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;

Expand Down Expand Up @@ -2429,7 +2429,7 @@ function FsWriteStream(vol, path, options) {
Writable.call(this, options);

this.path = pathToFilename(path);
this.fd = options.fd === undefined ? null : options.fd;
this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
this.flags = options.flags === undefined ? 'w' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;

Expand Down

0 comments on commit 3dbf3ba

Please sign in to comment.