/
index.js
87 lines (73 loc) · 2.36 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
'use strict';
/**
* The client-side plugin for Primus which adds EventEmitter functionality.
*
* @param {Primus} primus The initialised Primus connection.
* @api public
*/
exports.client = function client(primus) {
var toString = Object.prototype.toString
, emit = primus.emit;
primus.transform('incoming', function incoming(packet) {
var data = packet.data;
if (
this !== primus // Incorrect context.
|| 'object' !== typeof data // Events are objects.
|| !~toString.call(data.emit).indexOf(' Array]') // Not an emit object.
) {
return;
}
//
// Check if we've received an event that is already used internally.
// We use our previously saved `emit` function to emit the event so we
// prevent recursion and message flood.
//
if (!this.reserved(data.emit[0])) emit.apply(primus, data.emit);
return false;
});
primus.emit = function emitter(event) {
if (
primus.reserved(event)
|| 'newListener' === event
|| 'removeListener' === event
) return emit.apply(this, arguments);
primus.write({ emit: Array.prototype.slice.call(arguments, 0) });
return true;
};
};
/**
* The server-side plugin for Primus which adds EventEmitter functionality.
*
* @param {Primus} primus The initialised Primus server.
* @api public
*/
exports.server = function server(primus) {
var Spark = primus.Spark
, emit = Spark.prototype.emit;
primus.transform('incoming', function incoming(packet) {
var data = packet.data;
if (
!(this instanceof Spark) // Incorrect context.
|| 'object' !== typeof data // Events are objects.
|| !Array.isArray(data.emit) // Not an emit object.
) {
return;
}
//
// Check if we've received an event that is already used internally.
// We use our previously saved `emit` function to emit the event so we
// prevent recursion and message flood.
//
if (!this.reserved(data.emit[0])) emit.apply(this, data.emit);
return false;
});
Spark.prototype.emit = function emitter(event) {
if (
this.reserved(event)
|| 'newListener' === event
|| 'removeListener' === event
) return emit.apply(this, arguments);
this.write({ emit: Array.prototype.slice.call(arguments, 0) });
return true;
};
};