Skip to content

Commit

Permalink
fix(chunker-rabin): types and errors are aligned (#341)
Browse files Browse the repository at this point in the history
* test(ipfs-unixfs-importer): chunker-rabin constructor empty args

* fix(chunker-rabin): types and errors are aligned

fixes #339

* chore: pr-nit fix

Co-authored-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>

* chore: fix extra space from PR suggestion

---------

Co-authored-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
SgtPooki and whizzzkid authored Jul 14, 2023
1 parent 3a9f97a commit df27ff0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/ipfs-unixfs-importer/src/chunker/rabin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ export const rabin = (options: RabinOptions = {}): Chunker => {
max = avg + (avg / 2)
}

if (options.avgChunkSize == null && options.minChunkSize == null && options.maxChunkSize == null) {
throw errcode(new Error('please specify an average chunk size'), 'ERR_INVALID_AVG_CHUNK_SIZE')
const isInvalidChunkSizes = [min, avg, max].some((size) => size == null || isNaN(size))

if (isInvalidChunkSizes) {
if (options.avgChunkSize != null) {
throw errcode(new Error('please specify a valid average chunk size number'), 'ERR_INVALID_AVG_CHUNK_SIZE')
}

throw errcode(new Error('please specify valid numbers for (min|max|avg)ChunkSize'), 'ERR_INVALID_CHUNK_SIZE')
}

// validate min/max/avg in the same way as go
Expand Down
37 changes: 35 additions & 2 deletions packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('chunker: rabin', function () {
return
}

it('Allows constructing without any options', () => {
expect(() => rabin()).to.not.throw()
})

it('chunks non flat buffers', async () => {
const b1 = new Uint8Array(2 * 256)
const b2 = new Uint8Array(1 * 256)
Expand Down Expand Up @@ -96,19 +100,48 @@ describe('chunker: rabin', function () {
}
})

it('throws when avg chunk size is not specified', async () => {
it('throws when invalid avg chunk size is specified', async () => {
const opts = {
avgChunkSize: undefined
avgChunkSize: 'fortytwo'
}

try {
// @ts-expect-error invalid input
await all(rabin(opts)(asAsyncIterable([])))
throw new Error('Should have thrown')
} catch (err: any) {
expect(err.code).to.equal('ERR_INVALID_AVG_CHUNK_SIZE')
}
})

it('throws when invalid min chunk size is specified', async () => {
const opts = {
minChunkSize: 'fortytwo'
}

try {
// @ts-expect-error invalid input
await all(rabin(opts)(asAsyncIterable([])))
throw new Error('Should have thrown')
} catch (err: any) {
expect(err.code).to.equal('ERR_INVALID_CHUNK_SIZE')
}
})

it('throws when invalid max chunk size is specified', async () => {
const opts = {
maxChunkSize: 'fortytwo'
}

try {
// @ts-expect-error invalid input
await all(rabin(opts)(asAsyncIterable([])))
throw new Error('Should have thrown')
} catch (err: any) {
expect(err.code).to.equal('ERR_INVALID_CHUNK_SIZE')
}
})

it('uses the min chunk size when max and avg are too small', async () => {
const file = uint8ArrayConcat([rawFile, uint8ArrayFromString('hello')])
const opts = {
Expand Down

0 comments on commit df27ff0

Please sign in to comment.