diff --git a/package.json b/package.json index d080a1ed..d48208a8 100644 --- a/package.json +++ b/package.json @@ -72,10 +72,12 @@ "pull-through": "^1.0.18", "pull-traverse": "^1.0.3", "pull-write": "^1.1.4", - "rabin": "^1.6.0", "sparse-array": "^1.3.1", "stream-to-pull-stream": "^1.7.2" }, + "optionalDependencies": { + "rabin": "^1.6.0" + }, "contributors": [ "Alan Shaw ", "Arpit Agarwal ", diff --git a/src/builder/builder.js b/src/builder/builder.js index a94763dd..7b1d7eb6 100644 --- a/src/builder/builder.js +++ b/src/builder/builder.js @@ -105,13 +105,20 @@ module.exports = function builder (createChunker, ipld, createReducer, _options) } const reducer = createReducer(reduce(file, ipld, options), options) + let chunker + + try { + chunker = createChunker(options.chunkerOptions) + } catch (error) { + return callback(error) + } let previous let count = 0 pull( file.content, - createChunker(options.chunkerOptions), + chunker, pull.map(chunk => { if (options.progress && typeof options.progress === 'function') { options.progress(chunk.byteLength) diff --git a/src/chunker/rabin.js b/src/chunker/rabin.js index 8fe506e4..00398303 100644 --- a/src/chunker/rabin.js +++ b/src/chunker/rabin.js @@ -1,9 +1,20 @@ 'use strict' -const createRabin = require('rabin') const toPull = require('stream-to-pull-stream') +let createRabin + 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 + } + } + let min, max, avg if (options.minChunkSize && options.maxChunkSize && options.avgChunkSize) { avg = options.avgChunkSize