From 69e639c3cd5d1ba3603222b834abe67c07c99041 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 5 Jun 2019 12:09:46 +0100 Subject: [PATCH 1/3] fix: validate rabin args go-ipfs requires a min chunk size of 16 and will use that for the max/avg if they are set smaller than the min, so perform the same sort of validation. --- src/chunker/rabin.js | 14 ++++++++++++++ test/chunker-rabin.spec.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/chunker/rabin.js b/src/chunker/rabin.js index 7d76a44..a6cd046 100644 --- a/src/chunker/rabin.js +++ b/src/chunker/rabin.js @@ -2,6 +2,7 @@ const BufferList = require('bl') const { create } = require('rabin-wasm') +const errcode = require('err-code') module.exports = async function * rabinChunker (source, options) { const rabin = jsRabin() @@ -18,6 +19,19 @@ module.exports = async function * rabinChunker (source, options) { max = avg + (avg / 2) } + // validate min/max/avg in the same way as go + if (min < 16) { + throw errcode(new Error('rabin min must be greater than 16'), 'ERR_INVALID_MIN_CHUNK_SIZE') + } + + if (max < min) { + max = min + } + + if (avg < min) { + avg = min + } + const sizepow = Math.floor(Math.log2(avg)) for await (const chunk of rabin(source, { diff --git a/test/chunker-rabin.spec.js b/test/chunker-rabin.spec.js index 4ac92bf..495d5ff 100644 --- a/test/chunker-rabin.spec.js +++ b/test/chunker-rabin.spec.js @@ -56,7 +56,7 @@ describe('chunker: rabin', function () { const chunks = await all(chunker([b1], { ...defaultOptions, maxChunkSize: 262144, - minChunkSize: 1, + minChunkSize: 18, avgChunkSize: 256 })) @@ -83,4 +83,39 @@ describe('chunker: rabin', function () { expect(chunk).to.have.length.lte(opts.maxChunkSize) }) }) + + it('throws when min chunk size is too small', async () => { + const opts = { + ...defaultOptions, + minChunkSize: 1, + maxChunkSize: 100 + } + + try { + await all(chunker([], opts)) + throw new Error('Should have thrown') + } catch (err) { + expect(err.code).to.equal('ERR_INVALID_MIN_CHUNK_SIZE') + } + }) + + it('uses the min chunk size when max and avg are too small', async () => { + let file = Buffer.concat([rawFile, Buffer.from('hello')]) + const opts = { + ...defaultOptions, + minChunkSize: 100, + maxChunkSize: 5, + avgChunkSize: 5 + } + + const chunks = await all(chunker([file], opts)) + + chunks.forEach((chunk, index) => { + if (index === chunks.length - 1) { + expect(chunk.length).to.equal(81) + } else { + expect(chunk.length).to.equal(100) + } + }) + }) }) From 24cae81577baab9bba494df33e85a8f6cb11304e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 6 Jun 2019 17:02:08 +0100 Subject: [PATCH 2/3] chore: address PR comments --- src/chunker/rabin.js | 2 ++ test/chunker-rabin.spec.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/chunker/rabin.js b/src/chunker/rabin.js index a6cd046..cb4a84b 100644 --- a/src/chunker/rabin.js +++ b/src/chunker/rabin.js @@ -13,6 +13,8 @@ module.exports = async function * rabinChunker (source, options) { avg = options.avgChunkSize min = options.minChunkSize max = options.maxChunkSize + } else if (!options.avgChunkSize) { + throw errcode(new Error('please specify an average chunk size'), 'ERR_INVALID_AVG_CHUNK_SIZE') } else { avg = options.avgChunkSize min = avg / 3 diff --git a/test/chunker-rabin.spec.js b/test/chunker-rabin.spec.js index 495d5ff..143f75a 100644 --- a/test/chunker-rabin.spec.js +++ b/test/chunker-rabin.spec.js @@ -99,6 +99,21 @@ describe('chunker: rabin', function () { } }) + it('throws when avg chunk size is not specified', async () => { + const opts = { + ...defaultOptions, + avgChunkSize: undefined + } + + try { + await all(chunker([], opts)) + throw new Error('Should have thrown') + } catch (err) { + expect(err.code).to.equal('ERR_INVALID_AVG_CHUNK_SIZE') + } + }) + + it('uses the min chunk size when max and avg are too small', async () => { let file = Buffer.concat([rawFile, Buffer.from('hello')]) const opts = { From eb1eacddd34a44591672514f6f9ad645b28e23e8 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 6 Jun 2019 17:25:55 +0100 Subject: [PATCH 3/3] chore: fix linting --- test/chunker-rabin.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/chunker-rabin.spec.js b/test/chunker-rabin.spec.js index 143f75a..4b31b54 100644 --- a/test/chunker-rabin.spec.js +++ b/test/chunker-rabin.spec.js @@ -113,7 +113,6 @@ describe('chunker: rabin', function () { } }) - it('uses the min chunk size when max and avg are too small', async () => { let file = Buffer.concat([rawFile, Buffer.from('hello')]) const opts = {