Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(read-chunk/readChunkStrict): attach read chunk to error if small text #6940

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
julien-f marked this conversation as resolved.
Show resolved Hide resolved
`stream has ended with not enough data (actual: ${chunk.length}, expected: ${size}). Received: ${content}`
julien-f marked this conversation as resolved.
Show resolved Hide resolved
)
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