From 3dbf3ba835dd111120a3e2755d442ae23fbb3f64 Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Wed, 27 Nov 2024 16:01:54 -0800 Subject: [PATCH 1/3] fix: let `fd` option be `filehandle` in `fs.createReadStream`, `fs.createWriteStream` --- src/__tests__/volume.test.ts | 13 +++++++++++++ src/volume.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index bc5ba0ff0..5f132e8e1 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -1,3 +1,4 @@ +import { promisify } from 'util'; import { URL } from 'url'; import { Link } from '../node'; import Stats from '../Stats'; @@ -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', + }); + }); + }); }); diff --git a/src/volume.ts b/src/volume.ts index 1baaa0442..f685f6109 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -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; @@ -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; From 5734a60c2bff80f6823a54c56b1de563529e5971 Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Fri, 29 Nov 2024 21:16:03 -0800 Subject: [PATCH 2/3] add createReadStream test --- src/__tests__/volume.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 5f132e8e1..75abb2ae3 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -1444,4 +1444,29 @@ describe('volume', () => { }); }); }); + 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(); + }); + }); + }); }); From 3a44eabc541300b6541ca879549252e441ee26cd Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Sun, 1 Dec 2024 17:24:42 -0800 Subject: [PATCH 3/3] prettier --- src/__tests__/volume.test.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 75abb2ae3..7c7798d12 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -1449,24 +1449,24 @@ describe('volume', () => { 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(); + 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(); }); - }).catch((err) => { - expect(err).toBeNull(); - }); }); }); });