-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (31 loc) · 1018 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var multiplex = require('multiplex')
var wrapStream = require('./util').wrapStream
module.exports = function createChannel () {
var multiplexStream = multiplex(onConnection)
return multiplexStream
function onConnection (negotiationChannel, channelId) {
var negotiationStream = wrapStream(negotiationChannel)
negotiationStream.once('data', onChannelRequest)
negotiationStream.once('error', warnError)
function onChannelRequest (payload) {
var request = {
payload: payload,
grant: grant,
deny: deny
}
multiplexStream.emit('request', request)
}
function grant () {
negotiationStream.end([null, channelId])
return wrapStream(multiplexStream.createStream(channelId))
}
function deny (cause) {
negotiationStream.end([{message: cause}])
}
}
}
/* istanbul ignore next */
function warnError (err) {
var log = (console.warn || console.error || console.log).bind(console)
log(err.stack || err.message || err)
}