Skip to content

Commit df27ff0

Browse files
SgtPookiwhizzzkid
andauthored
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 <[email protected]> * chore: fix extra space from PR suggestion --------- Co-authored-by: Nishant Arora <[email protected]>
1 parent 3a9f97a commit df27ff0

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/ipfs-unixfs-importer/src/chunker/rabin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ export const rabin = (options: RabinOptions = {}): Chunker => {
5050
max = avg + (avg / 2)
5151
}
5252

53-
if (options.avgChunkSize == null && options.minChunkSize == null && options.maxChunkSize == null) {
54-
throw errcode(new Error('please specify an average chunk size'), 'ERR_INVALID_AVG_CHUNK_SIZE')
53+
const isInvalidChunkSizes = [min, avg, max].some((size) => size == null || isNaN(size))
54+
55+
if (isInvalidChunkSizes) {
56+
if (options.avgChunkSize != null) {
57+
throw errcode(new Error('please specify a valid average chunk size number'), 'ERR_INVALID_AVG_CHUNK_SIZE')
58+
}
59+
60+
throw errcode(new Error('please specify valid numbers for (min|max|avg)ChunkSize'), 'ERR_INVALID_CHUNK_SIZE')
5561
}
5662

5763
// validate min/max/avg in the same way as go

packages/ipfs-unixfs-importer/test/chunker-rabin.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe('chunker: rabin', function () {
2121
return
2222
}
2323

24+
it('Allows constructing without any options', () => {
25+
expect(() => rabin()).to.not.throw()
26+
})
27+
2428
it('chunks non flat buffers', async () => {
2529
const b1 = new Uint8Array(2 * 256)
2630
const b2 = new Uint8Array(1 * 256)
@@ -96,19 +100,48 @@ describe('chunker: rabin', function () {
96100
}
97101
})
98102

99-
it('throws when avg chunk size is not specified', async () => {
103+
it('throws when invalid avg chunk size is specified', async () => {
100104
const opts = {
101-
avgChunkSize: undefined
105+
avgChunkSize: 'fortytwo'
102106
}
103107

104108
try {
109+
// @ts-expect-error invalid input
105110
await all(rabin(opts)(asAsyncIterable([])))
106111
throw new Error('Should have thrown')
107112
} catch (err: any) {
108113
expect(err.code).to.equal('ERR_INVALID_AVG_CHUNK_SIZE')
109114
}
110115
})
111116

117+
it('throws when invalid min chunk size is specified', async () => {
118+
const opts = {
119+
minChunkSize: 'fortytwo'
120+
}
121+
122+
try {
123+
// @ts-expect-error invalid input
124+
await all(rabin(opts)(asAsyncIterable([])))
125+
throw new Error('Should have thrown')
126+
} catch (err: any) {
127+
expect(err.code).to.equal('ERR_INVALID_CHUNK_SIZE')
128+
}
129+
})
130+
131+
it('throws when invalid max chunk size is specified', async () => {
132+
const opts = {
133+
maxChunkSize: 'fortytwo'
134+
}
135+
136+
try {
137+
// @ts-expect-error invalid input
138+
await all(rabin(opts)(asAsyncIterable([])))
139+
throw new Error('Should have thrown')
140+
} catch (err: any) {
141+
expect(err.code).to.equal('ERR_INVALID_CHUNK_SIZE')
142+
}
143+
})
144+
112145
it('uses the min chunk size when max and avg are too small', async () => {
113146
const file = uint8ArrayConcat([rawFile, uint8ArrayFromString('hello')])
114147
const opts = {

0 commit comments

Comments
 (0)