Skip to content

Commit b1d28bf

Browse files
committed
solid-ws should be self-contained
1 parent f5fa641 commit b1d28bf

File tree

5 files changed

+96
-23
lines changed

5 files changed

+96
-23
lines changed

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
var debug = require('debug')('ldnode:ws-app')
12
var SolidWs = require('./lib/server')
23

34
module.exports = function attachToServer (server, app, opts) {
45
var solidWs = new SolidWs(server, opts)
6+
7+
if (app) {
8+
console.log("setting up the app")
9+
10+
app.post('/*', function (req, res, next) {
11+
debug("pub " + req.originalUrl + ' after post')
12+
solidWs.publish(req.originalUrl)
13+
solidWs.publish(path.basename(req.originalUrl))
14+
next()
15+
})
16+
app.patch('/*', function (req, res, next) {
17+
debug("pub " + req.originalUrl + ' after patch')
18+
solidWs.publish(req.originalUrl)
19+
console.log(solidWs.store)
20+
next()
21+
})
22+
app.put('/*', function (req, res, next) {
23+
debug("pub " + req.originalUrl + ' after put')
24+
solidWs.publish(req.originalUrl)
25+
next()
26+
})
27+
app.delete('/*', function (req, res, next) {
28+
debug("pub " + req.originalUrl + ' after delete')
29+
solidWs.publish(req.originalUrl)
30+
solidWs.publish(path.basename(req.originalUrl))
31+
next()
32+
})
33+
}
34+
535
return solidWs
636
}

lib/in-memory.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@ function InMemory (opts) {
88
this.subscribers = opts.subscribers || {}
99
}
1010

11-
InMemory.prototype.subscribe = function (uri, client, callback) {
11+
InMemory.prototype.subscribe = function (channel, uri, client, callback) {
1212
var self = this
13-
this.uris[uri] = true
1413

15-
if (!this.subscribers[uri]) {
16-
this.subscribers[uri] = {}
14+
if (!this.subscribers[channel]) {
15+
this.subscribers[channel] = {}
1716
}
1817

19-
client.uuid = uuid.v1()
20-
this.subscribers[uri][client.uuid] = client
18+
if (!client.uuid) {
19+
client.uuid = uuid.v1()
20+
}
21+
22+
this.subscribers[channel][client.uuid] = [client, uri]
2123

2224
client.on('close', function () {
23-
delete self.subscribers[uri][client.uuid]
25+
delete self.subscribers[channel][client.uuid]
2426
})
2527

2628
return callback(null, client.uuid)
2729
}
2830

29-
InMemory.prototype.get = function (uri, callback) {
30-
return callback(null, this.subscribers[uri] || {})
31+
InMemory.prototype.get = function (channel, callback) {
32+
return callback(null, this.subscribers[channel] || {})
3133
}

lib/server.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
var WebSocketServer = require('ws').Server
2-
var debug = require('debug')('solid:subscription')
2+
var debug = require('debug')('ldnode:ws')
33
var InMemory = require('./in-memory')
44
var parallel = require('run-parallel')
5+
var url = require('url')
56

67
module.exports = WsServer
78

9+
function defaultToChannel(iri) {
10+
return url.parse(iri).path
11+
}
12+
813
function WsServer (server, opts) {
914
var self = this
1015

1116
opts = opts || {}
1217
this.suffix = opts.suffix || '.changes'
1318
this.store = opts.store || new InMemory(opts)
19+
var toChannel = opts.toChannel || defaultToChannel
1420

1521
// Starting WSS server
1622
var wss = new WebSocketServer({
@@ -33,13 +39,16 @@ function WsServer (server, opts) {
3339
}
3440

3541
var tuple = message.split(' ')
42+
var command = tuple[0]
43+
var iri = tuple[1]
3644

3745
// Only accept 'sub http://example.tld/hello'
38-
if (tuple.length < 2 || tuple[0] !== 'sub') {
46+
if (tuple.length < 2 || command !== 'sub') {
3947
return
4048
}
4149

42-
self.store.subscribe(tuple[1], client, function (err, uuid) {
50+
var channel = toChannel ? toChannel(iri) : iri
51+
self.store.subscribe(channel, iri, client, function (err, uuid) {
4352
if (err) {
4453
// TODO Should return an error
4554
return
@@ -56,8 +65,8 @@ function WsServer (server, opts) {
5665
})
5766
}
5867

59-
WsServer.prototype.publish = function (uri, callback) {
60-
this.store.get(uri, function (err, subscribers) {
68+
WsServer.prototype.publish = function (iri, callback) {
69+
this.store.get(iri, function (err, subscribers) {
6170

6271
if (err) {
6372
if (callback) return callback(err)
@@ -71,8 +80,10 @@ WsServer.prototype.publish = function (uri, callback) {
7180
var tasks = Object.keys(subscribers)
7281
.map(function (uuid) {
7382
return function (cb) {
74-
var client = subscribers[uuid]
75-
client.send('pub ' + uri)
83+
var client = subscribers[uuid][0]
84+
var channel = subscribers[uuid][1]
85+
debug('pub ' + channel + ' to ' + client.uuid)
86+
client.send('pub ' + channel)
7687
}
7788
})
7889

test/utils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ exports.pubAll = pubAll
44

55
var parallel = require('run-parallel')
66

7-
function connectAll(clients, url, done) {
8-
parallel(clients.map(function(client) {
7+
function connectAll(clients, urls, done) {
8+
if (typeof urls === 'string') {
9+
urls = [urls]
10+
}
11+
parallel(clients.map(function(client, i) {
912
return function (cb) {
1013
client.on('open', function() {
11-
client.send('sub ' + url)
14+
client.send('sub ' + (urls[i] || urls[0]))
1215
cb()
1316
})
1417
}

test/websockets.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,40 @@ describe('Solid-ws', function() {
9999
})
100100

101101
describe('pub', function() {
102+
it('should pub to everyone, independently of the host name', function (done) {
103+
var urls = [
104+
'http://example.com/resource.ttl',
105+
'http://domain.com/resource.ttl',
106+
'/resource.ttl' ]
107+
var users = [
108+
'http://nicola.io/#me',
109+
'http://timbl.com/#me',
110+
'http://deiu.io/#me' ]
111+
112+
var clients = users.map(function() {
113+
return new WebSocket('http://localhost:' + port)
114+
})
115+
116+
var pubs = []
117+
118+
utils.connectAll(clients, urls, function() {
119+
utils.ackAll(clients, function() {
120+
utils.pubAll(clients, pubs, function() {
121+
assert.equal(pubs.length, users.length)
122+
done()
123+
})
124+
pubsub.publish('/resource.ttl')
125+
})
126+
})
127+
})
128+
102129
it('should be received by all the clients subscribed to a resource', function(done) {
103130

104131
var url = 'http://example.com/resource.ttl'
105132
var users = [
106-
'http://nicola.io#me',
107-
'http://timbl.com#me',
108-
'http://deiu.io#me' ]
133+
'http://nicola.io/#me',
134+
'http://timbl.com/#me',
135+
'http://deiu.io/#me' ]
109136

110137
var clients = users.map(function() {
111138
return new WebSocket('http://localhost:' + port)
@@ -119,7 +146,7 @@ describe('Solid-ws', function() {
119146
assert.equal(pubs.length, users.length)
120147
done()
121148
})
122-
pubsub.publish(url)
149+
pubsub.publish('/resource.ttl')
123150
})
124151
})
125152
})

0 commit comments

Comments
 (0)