Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: config validation #405

Merged
merged 2 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/config.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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