-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinject.js
49 lines (40 loc) · 1.13 KB
/
inject.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
42
43
44
45
46
47
48
49
var WS = require('multiserver/plugins/ws')
function no_handler (req, res, next) {
next(new Error('ssb-ws:web sockets only'))
}
function getPort (config) {
if (config.ws && config.ws.port) return config.ws.port
return 1024+(~~(Math.random()*(65536-1024)))
}
module.exports = function (createHandlers) {
var exports = {}
exports.name = 'ws'
exports.version = require('./package.json').version
exports.manifest = {}
exports.init = function (sbot, config) {
var port = getPort(config)
var handlers
function no_handler (req, res, next) {
next(new Error('ssb-ws:web sockets only'))
}
var handlers = createHandlers(sbot)
sbot.multiserver.transport({
name: 'ws',
create: function (config) {
var _host = config.host || 'localhost'
var _port = config.port || port
return WS(Object.assign({
host: _host,
port: _port,
handler: config.http !== false ? handlers : no_handler
}, config))
}
})
return {
use: function (handler) {
if (handlers.layers) handlers.layers.push(handler)
}
}
}
return exports
}