forked from Tarang/DDP-Server
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.js
231 lines (194 loc) · 5.58 KB
/
lib.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
require('harmony-reflect');
var DDPServer = function(opts) {
opts = opts || {};
var WebSocket = require('faye-websocket'),
EJSON = require('ejson'),
http = require('http'),
server = opts.httpServer,
methods = opts.methods || {},
collections = {},
subscriptions = {},
self = this;
if (!server) {
server = http.createServer()
server.listen(opts.port || 3000);
}
server.on('upgrade', function (request, socket, body) {
if (WebSocket.isWebSocket(request)) {
var ws = new WebSocket(request, socket, body);
var session_id = "" + new Date().getTime();
subscriptions[session_id] = {};
function sendMessage(data) {
ws.send(EJSON.stringify(data));
}
ws.on('message', function(event) {
var data = JSON.parse(event.data);
switch (data.msg) {
case "connect":
sendMessage({
msg: "connected",
session: session_id
});
break;
case "method":
if (data.method in methods) {
try {
var result = methods[data.method].apply(this, data.params)
sendMessage({
msg: "result",
id: data.id,
result: result
});
sendMessage({
msg: "updated",
id: data.id
})
} catch (e) {
console.log("error calling method", data.method, e)
sendMessage({
id: data.id,
error: {
error: 500,
reason: "Internal Server Error",
errorType: "Meteor.Error"
}
});
}
} else {
console.log("Error method " + data.method + " not found");
sendMessage({
id: data.id,
error: {
error: 404,
reason: "Method not found",
errorType: "Meteor.Error"
}
});
}
break;
case "sub":
subscriptions[session_id][data.name] = {
added: function(id, doc) {
sendMessage({
msg: "added",
collection: data.name,
id: id,
fields: doc
})
},
changed: function(id, fields, cleared) {
sendMessage({
msg: "changed",
collection: data.name,
id: id,
fields: fields,
cleared: cleared
})
},
removed: function(id) {
sendMessage({
msg: "removed",
collection: data.name,
id: id
})
}
};
var docs = collections[data.name];
for (var id in docs)
subscriptions[session_id][data.name].added(id, docs[id]);
sendMessage({
msg: "ready",
subs: [data.id]
});
break;
case "ping":
sendMessage({
msg: "pong",
id: data.id
});
break;
default:
}
});
ws.on('close', function(event) {
delete subscriptions[session_id];
ws = null;
session_id = null;
});
}
});
this.methods = function(newMethods) {
for (var key in newMethods) {
if (key in methods)
throw new Error(500, "A method named " + key + " already exists");
methods[key] = newMethods[key];
}
}
this.publish = function(name) {
if (name in collections)
throw new Error(500, "A collection named " + name + " already exists");
var documents = {};
var proxiedDocuments = {};
function add(id, doc) {
documents[id] = doc;
proxiedDocuments[id] = new Proxy(doc, {
set: function(_, field, value) {
var changed = {};
doc[field] = changed[field] = value;
sendChanged(id, changed, []);
return value;
},
deleteProperty: function(_, field) {
delete doc[field];
sendChanged(id, {}, [field]);
return true;
}
});
for (var client in subscriptions)
if (subscriptions[client][name])
subscriptions[client][name].added(id, doc);
}
function change(id, doc) {
var cleared = [];
for (var field in documents[id]) {
if (!(field in doc)) {
cleared.push(field)
delete documents[id][field];
}
}
var changed = {};
for (var field in doc)
if (doc[field] != documents[id][field])
documents[id][field] = changed[field] = doc[field];
sendChanged(id, changed, cleared);
}
function sendChanged(id, changed, cleared) {
for (var client in subscriptions)
if (subscriptions[client][name])
subscriptions[client][name].changed(id, changed, cleared);
}
function remove(id) {
delete documents[id];
for (var client in subscriptions)
if (subscriptions[client][name])
subscriptions[client][name].removed(id);
}
return collections[name] = new Proxy(documents, {
get: function(_, id) {
return proxiedDocuments[id];
},
set: function(_, id, doc) {
if (documents[id])
change(id, doc);
else
add(id, doc);
return proxiedDocuments[id];
},
deleteProperty: function(_, id) {
remove(id);
return true;
}
});
}
}
module.exports = DDPServer