Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Commit

Permalink
fix: fixes #230 by returning a through stream that emits the error in…
Browse files Browse the repository at this point in the history
…stead of throwing it
  • Loading branch information
achingbrain committed Oct 25, 2018
1 parent e26a868 commit fdd8429
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 10 additions & 3 deletions src/chunker/rabin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
'use strict'

const toPull = require('stream-to-pull-stream')
const through = require('pull-through')

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
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)
})
}
}

Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions test/chunker-rabin-browser.js
Original file line number Diff line number Diff line change
@@ -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()
})
)
})
})

0 comments on commit fdd8429

Please sign in to comment.