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

Commit fdd8429

Browse files
committed
fix: fixes #230 by returning a through stream that emits the error instead of throwing it
1 parent e26a868 commit fdd8429

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"pull-stream-to-stream": "^1.3.4",
5252
"pull-zip": "^2.0.1",
5353
"rimraf": "^2.6.2",
54-
"sinon": "^6.1.5"
54+
"sinon": "^7.1.0"
5555
},
5656
"dependencies": {
5757
"async": "^2.6.1",

src/chunker/rabin.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
'use strict'
22

33
const toPull = require('stream-to-pull-stream')
4+
const through = require('pull-through')
45

56
let createRabin
67

78
module.exports = (options) => {
89
if (!createRabin) {
910
try {
1011
createRabin = require('rabin')
11-
} catch (error) {
12-
error.message = `Rabin chunker not supported, it may have failed to install - ${error.message}`
1312

14-
throw error
13+
if (typeof createRabin !== 'function') {
14+
throw new Error('createRabin was not a function')
15+
}
16+
} catch (err) {
17+
const error = new Error(`Rabin chunker not available, it may have failed to install or not be supported on this platform`)
18+
19+
return through(function () {
20+
this.emit('error', error)
21+
})
1522
}
1623
}
1724

test/browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('IPFS data importing tests on the Browser', function () {
4141

4242
// Chunkers
4343
require('./chunker-fixed-size')
44+
require('./chunker-rabin-browser')
4445

4546
// Graph Builders
4647
require('./builder')(repo)

test/chunker-rabin-browser.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chunker = require('./../src/chunker/rabin')
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
8+
const pull = require('pull-stream')
9+
10+
describe('chunker: rabin browser', function () {
11+
it('returns an error', (done) => {
12+
const b1 = Buffer.alloc(2 * 256)
13+
const b2 = Buffer.alloc(1 * 256)
14+
const b3 = Buffer.alloc(5 * 256)
15+
16+
b1.fill('a')
17+
b2.fill('b')
18+
b3.fill('c')
19+
20+
pull(
21+
pull.values([b1, b2, b3]),
22+
chunker({minChunkSize: 48, avgChunkSize: 96, maxChunkSize: 192}),
23+
pull.collect((err) => {
24+
expect(err).to.exist()
25+
expect(err.message).to.include('Rabin chunker not available')
26+
27+
done()
28+
})
29+
)
30+
})
31+
})

0 commit comments

Comments
 (0)