Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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