Skip to content

Commit 2eac49b

Browse files
Copilotstreamich
andcommitted
feat: handle stream reading past file size in FsaNodeReadStream
Co-authored-by: streamich <[email protected]>
1 parent dc271f0 commit 2eac49b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/fsa-to-node/FsaNodeReadStream.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class FsaNodeReadStream extends Readable implements IReadStream {
4444
const start = this.options.start || 0;
4545
let end = typeof this.options.end === 'number' ? this.options.end + 1 : buffer.byteLength;
4646
if (end > buffer.byteLength) end = buffer.byteLength;
47+
if (start >= buffer.byteLength) return new Uint8Array(0);
4748
const uint8 = new Uint8Array(buffer, start, end - start);
4849
return uint8;
4950
});

src/fsa-to-node/__tests__/FsaNodeFs.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,5 +990,33 @@ onlyOnNode20('FsaNodeFs', () => {
990990
'/mountpoint/f.html': 'test',
991991
});
992992
});
993+
994+
test('can read with start position beyond file size', async () => {
995+
const { fs, vol } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
996+
const readStream = fs.createReadStream('/folder/file', { start: 100 });
997+
const writeStream = fs.createWriteStream('/folder/file2');
998+
readStream.pipe(writeStream);
999+
await new Promise(resolve => writeStream.once('close', resolve));
1000+
expect(vol.toJSON()).toStrictEqual({
1001+
'/mountpoint/folder/file': 'test',
1002+
'/mountpoint/folder/file2': '',
1003+
'/mountpoint/empty-folder': null,
1004+
'/mountpoint/f.html': 'test',
1005+
});
1006+
});
1007+
1008+
test('can read with start position at file size', async () => {
1009+
const { fs, vol } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
1010+
const readStream = fs.createReadStream('/folder/file', { start: 4 });
1011+
const writeStream = fs.createWriteStream('/folder/file3');
1012+
readStream.pipe(writeStream);
1013+
await new Promise(resolve => writeStream.once('close', resolve));
1014+
expect(vol.toJSON()).toStrictEqual({
1015+
'/mountpoint/folder/file': 'test',
1016+
'/mountpoint/folder/file3': '',
1017+
'/mountpoint/empty-folder': null,
1018+
'/mountpoint/f.html': 'test',
1019+
});
1020+
});
9931021
});
9941022
});

0 commit comments

Comments
 (0)