Skip to content

Commit

Permalink
fix: esbuild warning about ?? operator in lte-reader (#71)
Browse files Browse the repository at this point in the history
Restores the [pre-typescript](https://github.com/alanshaw/it-tar/blob/f6e492a44070816c4989af89e1602e656a27ecd4/lte-reader.js#L20)
behaviour of `lte-reader` to just checking that the `done` value
is true.

Adds tests to prevent regressions.

Closes #67
  • Loading branch information
achingbrain committed Feb 22, 2024
1 parent 1aa5266 commit e12843f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lte-reader.ts
Expand Up @@ -23,7 +23,7 @@ export function lteReader (source: Source<Uint8Array>): LteReader {
overflow = overflow.sublist(bytes)
} else if (overflow.length < bytes) {
const { value: nextValue, done } = await input.next(bytes - overflow.length)
if (done === true ?? nextValue == null) {
if (done === true) {
throw Object.assign(
new Error(`stream ended before ${bytes - overflow.length} bytes became available`),
{ code: 'ERR_UNDER_READ' }
Expand Down
29 changes: 29 additions & 0 deletions test/lte-reader.spec.ts
@@ -0,0 +1,29 @@
import { expect } from 'aegir/chai'
import { lteReader } from '../src/lte-reader.js'

describe('lte-reader', () => {
it('should read', async () => {
const reader = lteReader([
Uint8Array.from([0, 1, 2, 3, 4]),
Uint8Array.from([5, 6, 7, 8, 9])
])

const { value } = await reader.next(6)

if (value == null) {
throw new Error('No value received')
}

expect(value.subarray()).to.equalBytes(Uint8Array.from([0, 1, 2, 3, 4, 5]))
})

it('should reject on under-read', async () => {
const reader = lteReader([
Uint8Array.from([0, 1, 2, 3, 4]),
Uint8Array.from([5, 6, 7, 8, 9])
])

await expect(reader.next(100)).to.eventually.be.rejected
.with.property('code', 'ERR_UNDER_READ')
})
})

0 comments on commit e12843f

Please sign in to comment.