-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·93 lines (73 loc) · 2.01 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
var sockets = require('json-sockets');
var common = require('common');
if (typeof JSON === 'undefined') {
JSON = require('JSON')
}
var noop = function() {};
var parse = function(host) {
if (host && typeof host === 'object') {
return host;
}
var result = ((host || '').match(/([^:\/]*)(?::(\d+))?(?:\/(.*))?/) || []).slice(1);
return {
host: result[0] || 'localhost',
port: parseInt(result[1] || (module.browser ? 80 : 10547), 10),
sub: result[2] || '/'
};
};
var normalize = function(query) {
for (var i in query) {
if (Object.prototype.toString.call(query[i]) === '[object RegExp]') {
query[i] = {$regex:''+query[i]};
continue;
}
if (typeof query === 'object') {
query[i] = normalize(query[i]);
continue;
}
}
return query;
};
exports.connect = function(host) {
host = parse(host);
var socket = sockets.connect(host.host + ':' + host.port);
var pubsub = {};
var subscriptions = {};
socket.send({sub:host.sub});
socket.on('message', function(message) {
if (message.name === 'publish') {
(subscriptions[message.id] || noop)(message.doc);
}
});
pubsub.signer = exports.signer;
pubsub.subscribe = function(query, selection, callback) {
if (!callback) {
callback = selection;
selection = undefined;
}
var id = common.gensym();
subscriptions[id] = callback;
socket.send({name:'subscribe', id:id, query:normalize(query), selection:selection});
return function() {
delete subscriptions[id];
socket.send({name:'unsubscribe', id:id});
};
};
pubsub.publish = function(doc, challenge) {
socket.send({name:'publish', doc:doc, challenge:challenge});
};
return pubsub;
};
if (!module.browser) {
var signer = require("signer");
exports.signer = function(secret) {
var sign = signer.create(typeof secret === 'string' ? new Buffer(secret, 'base64') : secret);
return function(doc) {
var signed = {};
for (var i in doc) {
signed[i] = {$signature:sign.sign(i.replace(/\//g, '-') + '/' + doc[i]), value:doc[i]};
}
return signed;
};
};
}