Skip to content

Commit

Permalink
fix: config validation (#405)
Browse files Browse the repository at this point in the history
* test: backport content provide test update

* fix: avoid using superstruct interface
  • Loading branch information
jacobheun committed Aug 20, 2019
1 parent b4a70ea commit 2f6fea9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
32 changes: 18 additions & 14 deletions src/config.js
Expand Up @@ -4,29 +4,33 @@ const { struct, superstruct } = require('superstruct')
const { optional, list } = struct

// Define custom types
const s = superstruct()
const transport = s.union([
s.interface({
createListener: 'function',
dial: 'function'
}),
'function'
])
const s = superstruct({
types: {
transport: value => {
if (value.length === 0) return 'ERROR_EMPTY'
value.forEach(i => {
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
})
return true
},
protector: value => {
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
return true
}
}
})

const modulesSchema = s({
connEncryption: optional(list([s('object|function')])),
// this is hacky to simulate optional because interface doesnt work correctly with it
// change to optional when fixed upstream
connProtector: s.union(['undefined', s.interface({ protect: 'function' })]),
connProtector: s('undefined|protector'),
contentRouting: optional(list(['object'])),
dht: optional(s('null|function|object')),
peerDiscovery: optional(list([s('object|function')])),
peerRouting: optional(list(['object'])),
streamMuxer: optional(list([s('object|function')])),
transport: s.intersection([[transport], s.interface({
length (v) {
return v > 0 ? true : 'ERROR_EMPTY'
}
})])
transport: 'transport'
})

const configSchema = s({
Expand Down
23 changes: 8 additions & 15 deletions test/content-routing.node.js
Expand Up @@ -121,8 +121,9 @@ describe('.contentRouting', () => {
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')

nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
expect(err).to.not.exist()
expect(providers).to.have.length(0)
expect(err).to.exist()
expect(err.code).to.eql('ERR_NOT_FOUND')
expect(providers).to.not.exist()
done()
})
})
Expand Down Expand Up @@ -185,19 +186,10 @@ describe('.contentRouting', () => {
it('should be able to register as a provider', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
// mock the swarm connect
.post('/api/v0/swarm/connect')
.query({
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
'stream-channels': true
})
.reply(200, {
Strings: [`connect ${nodeA.peerInfo.id.toB58String()} success`]
}, ['Content-Type', 'application/json'])
// mock the refs call
.post('/api/v0/refs')
.query({
recursive: true,
recursive: false,
arg: cid.toBaseEncodedString(),
'stream-channels': true
})
Expand All @@ -216,10 +208,11 @@ describe('.contentRouting', () => {
it('should handle errors when registering as a provider', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
// mock the swarm connect
.post('/api/v0/swarm/connect')
// mock the refs call
.post('/api/v0/refs')
.query({
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
recursive: false,
arg: cid.toBaseEncodedString(),
'stream-channels': true
})
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
Expand Down

0 comments on commit 2f6fea9

Please sign in to comment.