Skip to content

Commit

Permalink
feat(@vates/readchunk): show the content received when stream is too …
Browse files Browse the repository at this point in the history
…short
  • Loading branch information
fbeauchamp committed Jul 12, 2023
1 parent ff432e0 commit 38c9aa8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
11 changes: 10 additions & 1 deletion @vates/read-chunk/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const assert = require('assert')
const isUtf8 = require('isutf8')

/**
* Read a chunk of data from a stream.
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 29 additions & 2 deletions @vates/read-chunk/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 3 additions & 0 deletions @vates/read-chunk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
},
"devDependencies": {
"test": "^3.2.1"
},
"dependencies": {
"isutf8": "^4.0.0"
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Incremental Backup & Replication] Attempt to work around HVM multiplier issues when creating VMs on older XAPIs (PR [#6866](https://github.com/vatesfr/xen-orchestra/pull/6866))
- [REST API] Fix VDI export when NBD is enabled
- [Backup/exports] Show more information on error ` stream has ended with not enough data (actual: xxx, expected: 512)` (PR [#6940](https://github.com/vatesfr/xen-orchestra/pull/6940))

### Packages to release

Expand All @@ -34,6 +35,7 @@

- @xen-orchestra/backups minor
- @xen-orchestra/xapi major
- @vates/read-chunk minor
- complex-matcher patch
- xo-server patch
- xo-web minor
Expand Down

0 comments on commit 38c9aa8

Please sign in to comment.