-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathserver.js
114 lines (95 loc) · 3.45 KB
/
server.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
var EventEmitter = Npm.require('events').EventEmitter;
var util = Npm.require('util');
var Fibers = Npm.require('fibers');
Meteor.Stream = function Stream(name) {
EV.call(this);
var self = this;
var streamName = 'stream-' + name;
var allowFunction;
var allowResultCache = true;
var allowResults = {};
var filters = [];
self.name = name;
var events = new EventEmitter();
events.setMaxListeners(0);
var disconnectEvents = new EV();
self._emit = self.emit;
self.emit = function emit() {
self.emitToSubscriptions(arguments, null, null);
};
var defaultResult = (typeof(Package) == 'object' && Package.insecure)? true: Meteor.Collection.insecure === true;
self.permissions = new Meteor.Stream.Permission(defaultResult, true);
self.addFilter = function addFilter(callback) {
filters.push(callback);
};
self.emitToSubscriptions = function emitToSubscriptions(args, subscriptionId, userId) {
events.emit('item', {args: args, userId: userId, subscriptionId: subscriptionId});
};
Meteor.publish(streamName, function() {
check(arguments, Match.Any);
var subscriptionId = Random.id();
var publication = this;
//send subscription id as the first document
publication.added(streamName, subscriptionId, {type: 'subscriptionId'});
publication.ready();
events.on('item', onItem);
function onItem(item) {
Fibers(function() {
var id = Random.id();
if(self.permissions.checkPermission('read', subscriptionId, publication.userId, item.args)) {
//do not send again this to the sender
if(subscriptionId != item.subscriptionId) {
publication.added(streamName, id, item);
publication.removed(streamName, id);
}
}
}).run();
}
publication.onStop(function() {
//trigger related onDisconnect handlers if exists
Fibers(function() {
disconnectEvents.emit(subscriptionId);
disconnectEvents.removeAllListeners(subscriptionId);
}).run();
events.removeListener('item', onItem);
});
});
var methods = {};
methods[streamName] = function(subscriptionId, args) {
check(arguments, Match.Any);
//in order to send this to the server callback
var userId = this.userId;
Fibers(function() {
var methodContext = {};
methodContext.userId = userId;
methodContext.subscriptionId = subscriptionId;
//in order to send this to the serve callback
methodContext.allowed = self.permissions.checkPermission('write', subscriptionId, methodContext.userId, args);
if(methodContext.allowed) {
//apply filters
args = applyFilters(args, methodContext);
self.emitToSubscriptions(args, subscriptionId, methodContext.userId);
//send to firehose if exists
if(self.firehose) {
self.firehose(args, subscriptionId, methodContext.userId);
}
}
//need to send this to server always
self._emit.apply(methodContext, args);
//register onDisconnect handlers if provided
if(typeof(methodContext.onDisconnect) == 'function') {
disconnectEvents.on(subscriptionId, methodContext.onDisconnect)
}
}).run();
};
Meteor.methods(methods);
function applyFilters(args, context) {
var eventName = args.shift();
filters.forEach(function(filter) {
args = filter.call(context, eventName, args);
});
args.unshift(eventName);
return args;
}
};
util.inherits(Meteor.Stream, EV);