diff --git a/@vates/read-chunk/index.js b/@vates/read-chunk/index.js index e00ddc484..fcede1035 100644 --- a/@vates/read-chunk/index.js +++ b/@vates/read-chunk/index.js @@ -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() { diff --git a/@vates/read-chunk/index.spec.js b/@vates/read-chunk/index.spec.js index e3d0f6629..a16c552d9 100644 --- a/@vates/read-chunk/index.spec.js +++ b/@vates/read-chunk/index.spec.js @@ -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')) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index b9b119474..bf7f595ad 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -23,6 +23,7 @@ +- @vates/read-chunk patch - xo-remote-parser patch - xo-server-transport-nagios patch - xo-web patch