Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept passing a FileHandle to createReadStream and createWriteStream #1077

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 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,41 @@ describe('volume', () => {
expect(new StatWatcher(vol).vol).toBe(vol);
});
});
describe('.createWriteStream', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why you've added a test for just createWriteStream, but not createReadStream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just because they're very similar but sure I can write one for createReadStream

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',
});
});
});
describe('.createReadStream', () => {
it('accepts filehandle as fd option', done => {
const vol = Volume.fromJSON({
'/test.txt': 'Hello',
});
vol.promises
.open('/test.txt', 'r')
.then(fh => {
const readStream = vol.createReadStream('/this/should/be/ignored', { fd: fh });
readStream.setEncoding('utf8');
let readData = '';
readStream.on('readable', () => {
const chunk = readStream.read();
if (chunk != null) readData += chunk;
});
readStream.on('end', () => {
expect(readData).toEqual('Hello');
done();
});
})
.catch(err => {
expect(err).toBeNull();
});
});
});
});
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
Loading