diff --git a/@vates/read-chunk/index.js b/@vates/read-chunk/index.js index ba87261adc1..cb35a57fc56 100644 --- a/@vates/read-chunk/index.js +++ b/@vates/read-chunk/index.js @@ -1,6 +1,7 @@ 'use strict' const assert = require('assert') +const isUtf8 = require('isutf8') /** * Read a chunk of data from a stream. @@ -80,7 +81,15 @@ exports.readChunkStrict = async function readChunkStrict(stream, size) { } if (size !== undefined && chunk.length !== size) { - const error = new Error(`stream has ended with not enough data (actual: ${chunk.length}, expected: ${size})`) + // Buffer.isUtf8 is too recent for now + // @todo : replace external package by Buffer.isUtf8 when the supported version of node reach 18 + + // cut the message to a sane length and make it usable + const content = chunk.subarray(0, 128).toString(isUtf8(chunk) ? 'utf-8' : 'base64') + + const error = new Error( + `stream has ended with not enough data (actual: ${chunk.length}, expected: ${size}). Received: ${content}` + ) Object.defineProperties(error, { chunk: { value: chunk, diff --git a/@vates/read-chunk/index.test.js b/@vates/read-chunk/index.test.js index 4cfdaa94a58..4f9e528bd7a 100644 --- a/@vates/read-chunk/index.test.js +++ b/@vates/read-chunk/index.test.js @@ -102,12 +102,39 @@ describe('readChunkStrict', function () { assert.strictEqual(error.chunk, undefined) }) - it('throws if stream ends with not enough data', async () => { + it('throws if stream ends with not enough data, utf8', async () => { const error = await rejectionOf(readChunkStrict(makeStream(['foo', 'bar']), 10)) assert(error instanceof Error) - assert.strictEqual(error.message, 'stream has ended with not enough data (actual: 6, expected: 10)') + assert.strictEqual( + error.message, + 'stream has ended with not enough data (actual: 6, expected: 10). Received: foobar' + ) assert.deepEqual(error.chunk, Buffer.from('foobar')) }) + + it('throws if stream ends with not enough data, non utf8 ', async () => { + const source = [Buffer.alloc(10, 128), Buffer.alloc(10, 128)] + const error = await rejectionOf(readChunkStrict(makeStream(source), 30)) + assert(error instanceof Error) + assert.strictEqual( + error.message, + 'stream has ended with not enough data (actual: 20, expected: 30). Received: gICAgICAgICAgICAgICAgICAgIA=' + ) + assert.deepEqual(error.chunk, Buffer.concat(source)) + }) + + it('throws if stream ends with not enough data, non utf8 , long data', async () => { + const source = Buffer.alloc(256, 128) + const error = await rejectionOf(readChunkStrict(makeStream([source]), 512)) + assert(error instanceof Error) + assert.strictEqual( + error.message, + `stream has ended with not enough data (actual: 256, expected: 512). Received: ${source + .subarray(128) + .toString('base64')}` + ) + assert.deepEqual(error.chunk, source) + }) }) describe('skip', function () { diff --git a/@vates/read-chunk/package.json b/@vates/read-chunk/package.json index 5e246b6ac1c..86e27ad70a4 100644 --- a/@vates/read-chunk/package.json +++ b/@vates/read-chunk/package.json @@ -33,5 +33,8 @@ }, "devDependencies": { "test": "^3.2.1" + }, + "dependencies": { + "isutf8": "^4.0.0" } }