forked from primus/omega-supreme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupreme.js
196 lines (170 loc) · 5.32 KB
/
supreme.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
'use strict';
var mapLimit = require('async/mapLimit')
, request = require('request')
, url = require('url').resolve;
//
// Expose the Primus plugin.
//
var supreme = module.exports;
/**
* Ensure that all default options have been set.
*
* @param {Object} options Supplied options.
* @returns {Object} options
* @api private
*/
supreme.options = function optional(options) {
//
// Options is provided by default by the Primus middleware but I'll rather be
// save then sorry here.
//
options = options || {};
options.concurrently = 'concurrently' in options ? options.concurrently : 20;
options.method = 'method' in options ? options.method.toUpperCase() : 'PUT';
options.password = 'password' in options ? options.password : 'supreme';
options.username = 'username' in options ? options.username : 'omega';
options.url = 'url' in options ? options.url : '/primus/omega/supreme';
return options;
};
/**
* Extend the Primus with additional methods which will do the actual
* broadcasting.
*
* @param {Primus} primus
* @param {Object} options The options supplied to Primus.
* @api public
*/
supreme.server = function server(primus, options) {
var index = primus.indexOfLayer('authorization');
options = supreme.options(options);
//
// Load the middleware so we can intercept messages.
//
primus.use('omega-supreme', require('./omega'), options, index);
/**
* Forward a message to a given server set.
*
* @param {Array} servers A server address or addresses.
* @param {Mixed} msg The messages to send.
* @param {Mixed} sparks ids to send.
* @param {Function} fn Callback.
* @returns {Primus}
* @api public
*/
primus.forward = function forward(servers, msg, sparks, fn) {
servers = (!Array.isArray(servers) ? [servers] : servers).filter(Boolean);
var type = 'broadcast'
, calls = 0
, spark
, local = false;
if ('function' === typeof sparks) {
fn = sparks;
sparks = '';
}
//
// Start off with local execution to see if we could hopefully save
// a broadcast to the set of servers because the connection is hosted
// locally on the primus server.
//
if (Array.isArray(sparks)) {
sparks = sparks.filter(function (id) {
spark = primus.spark(id);
if (spark) {
spark.write(msg);
calls++;
}
return !spark;
});
//
// if no more sparks are left, then we finished with just local sparks
//
local = !sparks.length;
type = 'sparks';
} else if (sparks) {
spark = primus.spark(sparks);
if (spark) {
spark.write(msg);
sparks = '';
//
// just one local spark was given
//
local = true;
calls++;
}
type = 'spark';
} else {
primus.forEach(function each(spark) {
spark.write(msg);
calls++;
});
//
// since there are no sparks, having no servers means
// *local* broadcast only
//
local = !servers.length;
}
//
// Everything was broad casted locally, we can bail out early, which is
// naaaais <-- say out loud with Borat's voice.
//
if (local) return fn(undefined, {
ok: true,
send: calls,
local: true
});
if (!servers.length) return fn(new Error('No servers provided'), {
ok: false,
send: calls,
local: false
});
mapLimit(servers, options.concurrently, function contact(server, next) {
request({
method: options.method, // Set the correct method.
uri: url(server, options.url), // Compile the correct URL
json: { // The actual JSON payload
msg: msg, // - The message we write
sparks: sparks // - Who the message should receive
}, //
auth: { // Set authentication headers
user: options.username, // With this user name.
pass: options.password, // And password.
sendImmediately: true // Send the header, don't wait for 401.
}
}, function requested(err, response, body) {
response = response || {};
body = body || {};
var status = response.statusCode
, reason = body.reason;
//
// Handle errors that are produced by our own library.
//
if (!err) {
if (200 !== status) {
err = new Error(reason || 'Invalid status code ('+ status +') returned');
} else if (body.ok !== true) {
err = new Error(reason || 'Unable to process the request');
}
}
if (err) {
err.url = url(server, options.url);
err.status = status || 500;
err.body = body;
err.type = type;
err.packet = {
sparks: sparks,
msg: msg
};
return next(err);
}
next(undefined, body);
});
}, function calculate(err, reached) {
if (err) return fn(err, reached);
fn(err, reached.reduce(function reduce(memo, reach) {
memo.send += reach.send || 0;
return memo;
}, { ok: true, send: calls }));
});
return primus;
};
};