From 6e1907bd45d80c5f607f75c80b193c0bfd1f87a0 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Tue, 4 Jul 2023 11:15:01 -0700 Subject: [PATCH 1/4] test(ipfs-unixfs-importer): chunker-rabin constructor empty args --- packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts index f081687e..4211e58c 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) From 9f68f1fa5a92e60d470a7b87f0921faef050c817 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Tue, 4 Jul 2023 11:49:27 -0700 Subject: [PATCH 2/4] fix(chunker-rabin): types and errors are aligned fixes #339 --- .../ipfs-unixfs-importer/src/chunker/rabin.ts | 10 ++++-- .../test/chunker-rabin.spec.ts | 33 +++++++++++++++++-- 2 files changed, 39 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..a743466f 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 (options.avgChunkSize != null && isInvalidChunkSizes) { + throw errcode(new Error('please specify a valid average chunk size number'), 'ERR_INVALID_AVG_CHUNK_SIZE') + } + + if (isInvalidChunkSizes) { + 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 4211e58c..1e7bcf06 100644 --- a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts +++ b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts @@ -100,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) { @@ -113,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 = { From 1e3fe8a677056d81cf850b68436b0c71ce0faae5 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 6 Jul 2023 13:34:33 -0700 Subject: [PATCH 3/4] chore: pr-nit fix Co-authored-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- packages/ipfs-unixfs-importer/src/chunker/rabin.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts index a743466f..bba910ff 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts +++ b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts @@ -52,11 +52,11 @@ export const rabin = (options: RabinOptions = {}): Chunker => { const isInvalidChunkSizes = [min, avg, max].some((size) => size == null || isNaN(size)) - if (options.avgChunkSize != null && isInvalidChunkSizes) { - throw errcode(new Error('please specify a valid average chunk size number'), 'ERR_INVALID_AVG_CHUNK_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') } From ce619800ff30eba0b43677bc86083f3b88eaa93d Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:48:21 -0700 Subject: [PATCH 4/4] chore: fix extra space from PR suggestion --- packages/ipfs-unixfs-importer/src/chunker/rabin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts index bba910ff..a64defdb 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/rabin.ts +++ b/packages/ipfs-unixfs-importer/src/chunker/rabin.ts @@ -56,7 +56,7 @@ export const rabin = (options: RabinOptions = {}): Chunker => { 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') }