-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
371 lines (321 loc) · 10.7 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var util = require ('util');
var fs = require ('fs');
var path = require ('path');
var EventEmitter = require ('events').EventEmitter;
var async = require ('async');
var filth = require ('filth');
var submergence = require ('submergence');
var Reply = submergence.Reply;
var Remote = require ('./remote');
var Router = require ('./lib/Router');
var Action = require ('./lib/Action');
// pull in extra docs automatically
require ('./info.js');
var standalone =
'<script type="text/javascript">'
+ fs.readFileSync (path.resolve (__dirname, './build/bundle.js')).toString()
+ '</script>'
;
/** @module/class substation
@super submergence
Realtime application gateway and authentication provider. This is the all-purpose module for
writing client applications over the [submergence](submergence) layer. You may serve an
application from a local cluster or respond to events forwarded from a submergence service. To
access the client library, bundle this module with [browserify](http://browserify.org/).
A `substation` always represents a single domain (or subdomain) which must [appear in the
configuration](:Configuration#domain).
@spare details
@load `README.md`
@argument/:Configuration config
Configuration options to use for this instance.
@returns/substation
If the `new` keyword is not used, an instance is created and returned.
*/
function substation (config) {
if (!(this instanceof substation))
return new substation (config);
EventEmitter.call (this);
this.config = filth.clone (DEFAULT_CONFIG);
filth.merge (this.config, config);
this.server = new submergence (this.config);
this.router = new Router (this, this.config);
this.logger = this.server.logger;
}
// util.inherits (substation, EventEmitter);
substation.Remote = Remote;
/** @submodule/class Configuration
@super submergence:Configuration
@member/String databaseName
@default `"substation"`
@member/String applicationName
@default `"substation"`
@member/Object context
Sets default values for html template rendering operations. The render context will be a deep
clone of `context` merged with the `content` context generated by an Action.
@member/Object template
*/
var DEFAULT_CONFIG = {
databaseName: "substation",
applicationName: "substation",
context: {
standalone: standalone
}
};
/** @member/Function listen
Initializes the [Router](:Router), then opens inbound sockets. Every [Action](:Action) will
have its [setup](:Action:Configuration#setup) Function called **before** any requests are
accepted.
@argument/Number port
The port number to listen on.
@callback
The server is now accepting requests. If something prevents this, the process will exit with
status code `1`.
*/
substation.prototype.listen = function (port, callback) {
if (!this.config.domain) {
logger.fatal ('domain not configured');
return process.exit (1);
}
var self = this;
this.router.init (function(){
self.server.listen (port, self.router, callback);
});
}
/** @member/Function addAction
@alias :Router#addAction
@callback
@argument/Error|undefined err
*/
substation.prototype.addAction = function(){
return this.router.addAction.apply (this.router, arguments);
}
/** @member/Function sendEvent
@argument/String user
@argument/String client
@optional
@argument/Object info
@callback
@optional
*/
substation.prototype.sendEvent = function (/* user, client, info, callback */) {
var user, client, info, callback;
switch (arguments.length) {
case 2:
user = arguments[0];
info = arguments[1];
break;
case 3:
user = arguments[0];
info = arguments[1];
callback = arguments[2];
break;
default:
user = arguments[0];
client = arguments[1]
info = arguments[2];
callback = arguments[3];
}
if (typeof info[0] != 'string')
return process.nextTick (function(){ callback (
new Error ('First event argument must be a string')
); });
try {
return this.server.sendEvent (this.config.domain || null, user, client, info, callback);
} catch (err) {
self.logger.error (err, 'failed to send event');
if (callback)
process.nextTick (callback);
}
};
/** @member/Function isActive
@argument/String user
@argument/String client
@optional
@callback
@argument/Error|undefined err
@argument/Boolean isActive
*/
substation.prototype.isActive = function (/* user, client, callback */) {
var user, client, callback;
if (arguments.length == 2) {
user = arguments[0];
callback = arguments[1];
} else {
user = arguments[0];
client = arguments[1];
callback = arguments[2];
}
try {
return this.server.isActive (this.config.domain || null, user, client, callback);
} catch (err) {
this.logger.error (err, 'failed to check if a user/client is active');
if (callback)
process.nextTick (function(){ callback (err); });
}
};
/** @member/Function action
Trigger an [Action](substation:Action) in JSON mode and get the resulting content and events,
or a [stream](stream.Readable) if the Action produces one. Any manually-produced html is
ignored.
There is no method to run an [Action](substation:Action) in HTML mode because an Action's
template should generate a complete page.
@argument/submergence:Agent
@argument/substation:Action
@argument/submergence:Request
@callback
@argument/Error|undefined err
@argument/String status
@argument/Object|stream.Readable content
The 'content' value produced by the selected Action.
@argument/Array<Array> events
@argument/Number length
@optional
If a Stream was returned, its length will be passed.
@argument/String type
@optional
If a Stream was returned and a type was defined, it will be passed.
*/
substation.prototype.action = function (agent, action, request, callback) {
this.router.getActionByName (action, function (err, action) {
if (err) return callback (err);
var reply = new Reply (function (status, events, content, html) {
callback (undefined, status, content, events);
}, function (status, stream, length, type) {
callback (undefined, status, stream, events, length, type);
});
});
};
// proxy events:EventEmitter methods to underlying submergence
substation.prototype.addListener = function(){
this.server.addListener.apply (this.server, arguments);
};
substation.prototype.on = function(){
this.server.on.apply (this.server, arguments);
};
substation.prototype.once = function(){
this.server.once.apply (this.server, arguments);
};
substation.prototype.removeListener = function(){
this.server.removeListener.apply (this.server, arguments);
};
substation.prototype.removeAllListeners = function(){
this.server.removeAllListeners.apply (this.server, arguments);
};
substation.prototype.setMaxListeners = function(){
this.server.setMaxListeners.apply (this.server, arguments);
};
substation.prototype.listeners = function(){
this.server.listeners.apply (this.server, arguments);
};
/** @property/Function configure
Set configuration options for `substation` as a monad. You may call configure multiple times to
assemble configuration options - in case of conflict, the most recent configuration wins.
You may still instantiate `substation` instances after configuring and starting the monad. They
will **not** inherit configuration options or Actions from the monad.
@argument/:Configuration config
*/
var monadConfig, monad;
function configure (config) {
if (monad)
throw new Error ('cannot configure as a monad when already listening as a monad');
if (!monadConfig)
monadConfig = filth.clone (config);
else
filth.merge (monadConfig, config);
}
/** @property/Function listen
@argument/Number port
@callback
@argument/Error|undefined err
*/
function monadListen (port, callback) {
if (!monad)
if (monadConfig.APIKey)
monad = new Remote (monadConfig);
else
monad = new substation (monadConfig);
for (var i=0,j=actionQueue.length; i<j; i++)
monad.addAction.apply (monad, actionQueue[i]);
for (var i=0,j=monadQueue.length; i<j; i++)
monad[monadQueue[i][0]].apply (monad, monadQueue[i][1]);
return monad.listen (port, callback);
}
/** @property/Function addAction
@argument/String method
@optional
@argument/String|RegExp route
@optional
@argument/substation:Action action
*/
var actionQueue = [];
var monadQueue = [];
function addAction (method, route, action) {
if (monad)
return monad.addAction (method, route, action);
actionQueue.push ([ method, route, action ]);
}
/** @property/Function sendEvent
*/
function sendEvent(){
return monad.sendEvent.apply (monad, arguments);
};
/** @property/Function isActive
*/
function isActive(){
return monad.isActive.apply (monad, arguments);
};
// proxy events:EventEmitter methods to underlying submergence
substation.addListener = function(){
if (!monad) {
monadQueue.push ('addListener', arguments);
return;
}
monad.addListener.apply (monad, arguments);
};
substation.on = function(){
if (!monad) {
monadQueue.push ('on', arguments);
return;
}
monad.on.apply (monad, arguments);
};
substation.once = function(){
if (!monad) {
monadQueue.push ('once', arguments);
return;
}
monad.once.apply (monad, arguments);
};
substation.removeListener = function(){
if (!monad) {
monadQueue.push ('removeListener', arguments);
return;
}
monad.removeListener.apply (monad, arguments);
};
substation.removeAllListeners = function(){
if (!monad) {
monadQueue.push ('removeAllListeners', arguments);
return;
}
monad.removeAllListeners.apply (monad, arguments);
};
substation.setMaxListeners = function(){
if (!monad) {
monadQueue.push ('setMaxListeners', arguments);
return;
}
monad.setMaxListeners.apply (monad, arguments);
};
substation.listeners = function(){
if (!monad) {
monadQueue.push ('listeners', arguments);
return;
}
monad.listeners.apply (monad, arguments);
};
module.exports = substation;
substation.configure = configure;
substation.listen = monadListen;
substation.Action = Action;
substation.Router = Router;