-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
223 lines (186 loc) · 5.13 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
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
/*global Primus*/
'use strict';
var substream = require('./substream');
/**
* SubStream for the Primus server.
*
* @param {Primus} primus The Primus real-time server.
* @api public
*/
exports.server = function server(primus) {
var Stream = require('stream')
, SubStream = substream(Stream)
, emit = Stream.prototype.emit
, Spark = primus.Spark;
/**
* Return a preconfigured listener.
*
* @param {String} event Name of the event.
* @returns {Function} listener
* @api private
*/
function listen(event, spark) {
if ('end' === event) return function end() {
if (!spark.streams) return;
for (var stream in spark.streams) {
stream = spark.streams[stream];
if (stream.end) stream.end();
}
};
if ('readyStateChange' === event) return function change(reason) {
if (!spark.streams) return;
for (var stream in spark.streams) {
stream = spark.streams[stream];
stream.readyState = spark.readyState;
if (stream.emit) emit.call(stream, event, reason);
}
};
return function proxy() {
if (!spark.streams) return;
var args = Array.prototype.slice.call(arguments, 0);
for (var stream in spark.streams) {
if (stream.emit) emit.call(stream, [event].concat(args));
}
};
}
/**
* Setup the Primus instance so we can start creating substreams.
*
* @param {Spark} spark Incoming connection
* @api private
*/
function setup(spark) {
spark.streams = {};
Object.keys(spark.reserved.events).filter(function filter(event) {
return 'data' !== event;
}).forEach(function each(event) {
spark.on(event, listen(event, spark));
});
}
/**
* Create a new namespace.
*
* @param {String} name Namespace id
* @returns {Namespace}
* @api private
*/
Spark.prototype.substream = function substream(name) {
if (!this.streams) setup(this);
if (!this.streams[name]) this.streams[name] = new SubStream(this, name, {
primus: this.primus
});
return this.streams[name];
};
/**
* Intercept the incoming messages to see if they belong to a given substream.
*
* @param {Object} packet The message packet.
* @api private
*/
primus.transform('incoming', function incoming(packet) {
var next;
if (!this.streams) return;
for (var stream in this.streams) {
stream = this.streams[stream];
if (stream.mine && stream.mine(packet.data)) {
next = false;
break;
}
}
return next;
});
};
/**
* SubStream for the Primus client API.
*
* @param {Primus} primus The Primus client.
* @api public
*/
exports.client = function client(primus) {
var SubStream = Primus._substream(Primus.Stream)
, emit = Primus.Stream.prototype.emit;
/**
* Return a preconfigured listener.
*
* @param {String} event Name of the event.
* @returns {Function} listener
* @api private
*/
function listen(event) {
if ('end' === event) return function end() {
if (!primus.streams) return;
for (var stream in primus.streams) {
stream = primus.streams[stream];
if (stream.end) stream.end();
}
};
if ('readyStateChange' === event) return function change(reason) {
if (!primus.streams) return;
for (var stream in primus.streams) {
stream = primus.streams[stream];
stream.readyState = primus.readyState;
if (stream.emit) emit.call(stream, event, reason);
}
};
return function proxy() {
if (!primus.streams) return;
var args = Array.prototype.slice.call(arguments, 0);
for (var stream in primus.streams) {
if (stream.emit) emit.call(stream, [event].concat(args));
}
};
}
/**
* Setup the Primus instance so we can start creating substreams.
*
* @api private
*/
function setup() {
primus.streams = {};
for (var event in primus.reserved.events) {
if ('data' === event) continue;
primus.on(event, listen(event));
}
}
/**
* Create a new namespace.
*
* @param {String} name Namespace id
* @returns {Namespace}
* @api private
*/
primus.substream = function substream(name) {
//
// First time that we've been called, setup the additional data structure
// for this connection and make sure we set all our listeners once to reduce
// memory when using a large portion of listeners.
//
if (!primus.streams) setup();
if (!primus.streams[name]) primus.streams[name] = new SubStream(primus, name, {
primus: primus
});
return primus.streams[name];
};
/**
* Intercept the incoming messages to see if they belong to a given substream.
*
* @param {Object} packet The message packet.
* @api private
*/
primus.transform('incoming', function incoming(packet) {
var next;
if (!this.streams) return;
for (var stream in this.streams) {
stream = this.streams[stream];
if (stream.mine && stream.mine(packet.data)) {
next = false;
break;
}
}
return next;
});
};
//
// Expose the library.
//
exports.library = 'Primus._substream = '+ substream.toString();