-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (131 loc) · 3.88 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
const WebSocket = require('ws')
const WebSocketServer = require('ws').Server
const fs = require('fs')
const uuid = require('uuid')
const handler = require('serve-handler')
const https = require('https')
const INotifyWait = require('inotifywait')
const parallel = require('run-parallel')
const url = require('url')
const port = 3001
const server = https.createServer({
cert: fs.readFileSync('./fullchain.pem'),
key: fs.readFileSync('./privkey.pem')
},
(request, response) => {
// You pass two more arguments for config and middleware
// More details here: https://github.com/zeit/serve-handler#options
return handler(request, response, {
public: 'public',
'renderSingle': true
})
})
WsServer(server)
server.listen(port, () => {
console.log('Running at https://localhost:' + port)
})
function defaultToChannel (iri) {
return url.parse(iri).path
}
function WsServer (server, opts) {
var self = this
opts = opts || {}
this.suffix = opts.suffix || '.changes'
this.store = opts.store || new InMemory(opts)
var toChannel = opts.toChannel || defaultToChannel
publish = function (iri, callback) {
this.store.get(iri, function (err, subscribers) {
if (err) {
if (callback) return callback(err)
else return
}
if (!subscribers) {
subscribers = {}
}
console.log('publish', 'subscribers', subscribers, 'iri', iri)
var tasks = Object.keys(subscribers)
.map(function (uuid) {
return function (cb) {
var client = subscribers[uuid][0]
var channel = subscribers[uuid][1]
console.log('pub ' + channel + ' to ' + client.uuid)
client.send('pub ' + channel)
}
})
parallel(tasks, callback)
})
}
// Starting WSS server
var wss = new WebSocketServer({
server: server,
clientTracking: false,
path: opts.path
})
var watch1 = new INotifyWait('./public/', { recursive: false })
watch1.on('ready', function (filename) {
console.log('watcher is watching')
})
watch1.on('change', function (filename) {
console.log(filename + ' change')
self.publish(filename)
})
watch1.on('add', function (filename) {
console.log(filename + ' added')
})
// Handling a single connection
wss.on('connection', function (client) {
console.log('New connection')
// var location = url.parse(client.upgradeReq.url, true)
// Handling messages
client.on('message', function (message) {
console.log('New message: ' + message)
if (!message || typeof message !== 'string') {
return
}
var tuple = message.split(' ')
var command = tuple[0]
var iri = tuple[1]
// Only accept 'sub http://example.tld/hello'
if (tuple.length < 2 || command !== 'sub') {
return
}
var channel = toChannel ? toChannel(iri) : iri
self.store.subscribe(channel, iri, client, function (err, uuid) {
if (err) {
// TODO Should return an error
return
}
console.log('ack ' + tuple[1])
client.send('ack ' + tuple[1])
})
})
// Respond to ping
client.on('ping', function () {
client.pong()
})
})
}
function InMemory (opts) {
opts = opts || {}
this.uris = opts.uris || {}
this.subscribers = opts.subscribers || {}
}
InMemory.prototype.subscribe = function (channel, uri, client, callback) {
var self = this
if (!this.subscribers[channel]) {
this.subscribers[channel] = {}
}
if (!client.uuid) {
client.uuid = uuid.v1()
}
this.subscribers[channel][client.uuid] = [client, uri]
console.log('subscribers', this.subscribers)
client.on('close', function () {
delete self.subscribers[channel][client.uuid]
})
return callback(null, client.uuid)
}
InMemory.prototype.get = function (channel, callback) {
return callback(null, this.subscribers[channel] || {})
}