diff --git a/package.json b/package.json index 6ce09fe5..e76bfda9 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "pull-stream-to-stream": "^1.3.4", "pull-zip": "^2.0.1", "rimraf": "^2.6.2", - "sinon": "^6.1.5" + "sinon": "^7.1.0" }, "dependencies": { "async": "^2.6.1", diff --git a/src/chunker/rabin.js b/src/chunker/rabin.js index 00398303..dfe648eb 100644 --- a/src/chunker/rabin.js +++ b/src/chunker/rabin.js @@ -1,6 +1,7 @@ 'use strict' const toPull = require('stream-to-pull-stream') +const through = require('pull-through') let createRabin @@ -8,10 +9,16 @@ module.exports = (options) => { if (!createRabin) { try { createRabin = require('rabin') - } catch (error) { - error.message = `Rabin chunker not supported, it may have failed to install - ${error.message}` - throw error + if (typeof createRabin !== 'function') { + throw new Error('createRabin was not a function') + } + } catch (err) { + const error = new Error(`Rabin chunker not available, it may have failed to install or not be supported on this platform`) + + return through(function () { + this.emit('error', error) + }) } } diff --git a/test/browser.js b/test/browser.js index 6cf3280f..9aa30e66 100644 --- a/test/browser.js +++ b/test/browser.js @@ -41,6 +41,7 @@ describe('IPFS data importing tests on the Browser', function () { // Chunkers require('./chunker-fixed-size') + require('./chunker-rabin-browser') // Graph Builders require('./builder')(repo) diff --git a/test/chunker-rabin-browser.js b/test/chunker-rabin-browser.js new file mode 100644 index 00000000..08ee9e67 --- /dev/null +++ b/test/chunker-rabin-browser.js @@ -0,0 +1,31 @@ +/* eslint-env mocha */ +'use strict' + +const chunker = require('./../src/chunker/rabin') +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const pull = require('pull-stream') + +describe('chunker: rabin browser', function () { + it('returns an error', (done) => { + const b1 = Buffer.alloc(2 * 256) + const b2 = Buffer.alloc(1 * 256) + const b3 = Buffer.alloc(5 * 256) + + b1.fill('a') + b2.fill('b') + b3.fill('c') + + pull( + pull.values([b1, b2, b3]), + chunker({minChunkSize: 48, avgChunkSize: 96, maxChunkSize: 192}), + pull.collect((err) => { + expect(err).to.exist() + expect(err.message).to.include('Rabin chunker not available') + + done() + }) + ) + }) +})