fix(read-chunk): handle already ended stream

This commit is contained in:
Julien Fontanet 2022-10-06 15:13:15 +02:00
parent 13f19de1a0
commit e2521b6688
3 changed files with 11 additions and 1 deletions

View File

@ -1,7 +1,9 @@
'use strict'
const readChunk = (stream, size) =>
size === 0
stream.closed || stream.readableEnded
? Promise.resolve(null)
: size === 0
? Promise.resolve(Buffer.alloc(0))
: new Promise((resolve, reject) => {
function onEnd() {

View File

@ -14,6 +14,13 @@ describe('readChunk', () => {
expect(await readChunk(makeStream([]))).toBe(null)
})
it('returns null if the stream is already ended', async () => {
const stream = await makeStream([])
await readChunk(stream)
expect(await readChunk(stream)).toBe(null)
})
describe('with binary stream', () => {
it('returns the first chunk of data', async () => {
expect(await readChunk(makeStream(['foo', 'bar']))).toEqual(Buffer.from('foo'))

View File

@ -23,6 +23,7 @@
<!--packages-start-->
- @vates/read-chunk patch
- xo-remote-parser patch
- xo-server-transport-nagios patch
- xo-web patch