From df27ff0715f9d207b03dcb1e8c497411a77012c7 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 13 Jul 2023 17:15:40 -0700 Subject: [PATCH] fix(chunker-rabin): types and errors are aligned (#341) * 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> --- .../ipfs-unixfs-importer/src/chunker/rabin.ts | 10 ++++- .../test/chunker-rabin.spec.ts | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts index 2f0567c6..a64defdb 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts +++ b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts @@ -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 diff --git a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts index f081687e..1e7bcf06 100644 --- a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts +++ b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts @@ -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) @@ -96,12 +100,13 @@ 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) { @@ -109,6 +114,34 @@ describe('chunker: rabin', function () { } }) + 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 = {