-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
265 lines (216 loc) · 6.5 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
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
"use strict";
/*!
* Prism Realtime Server
*
* Copyright(c) 2013 Owen Barnes <[email protected]>
* MIT Licensed
*/
var fs = require('fs');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var Service = require('realtime-service');
var Request = require('./lib/request');
var systemService = require('./lib/system-service');
/**
* Prism Server
*
* Example:
*
* var server = new Server({
* root: '/my/app/dir',
* dir: 'services',
* log: console.log,
* transport: require('rtt-engineio')(),
* events: instanceOfAnEventEmitter,
* sessionStore: new RedisStore({port: 6379}),
* cacheSessions: true
* });
*
* @param {Object} options
* @return {Object} instance of ServiceManager
* @api public
*
*/
function Server(options) {
options = options || {};
this.serviceCount = 0;
this.services = {};
this.api = {}; // server-side api returned from services
this.middleware = []; // request middleware (processed before msg is sent to service)
this.version = require('./package.json').version;
this.root = options.root || process.cwd().replace(/\\/g, '/');
this.transport = options.transport || null;
this.port = options.port || 3001;
this.config = options.config || {}; // sent to the client
this.events = options.events || new EventEmitter();
this.dir = options.dir || 'services'; // dir containing Service data files
this.log = options.log || function(){};
this.sessionStore = options.sessionStore || false; // will use default Connect Memory store if not set
this.cacheSessions = options.cacheSessions || true; // cache sessions in RAM (avoids hitting the store on each req)
this._registerSystemService();
}
/**
* Use a Realtime Service
*
* Examples:
*
* server.service('rpc', require('rts-rpc')())
*
* @param {String} name of service (must be unique and < 12 chars)
* @param {Object} service definition object (see Realtime Service Spec)
* @param {Object} options and overrides (e.g. don't send client libs). not implemeted yet
* @return {Object} instance of Service
* @api public
*
*/
Server.prototype.service = function(name, definition, options) {
options = options || {};
// check name length
if (name.length > 12) throw new Error("Service name '" + name + "' must be 12 chars or less");
// todo: check for spaces and other weird chars (as name must form a directory)
// check for name uniqueness
for (var serviceId in this.services) {
if (this.services[serviceId].assigned.name === name)
throw new Error('Service name ' + name + ' already used. Please choose another name.');
}
var assign = {
id: this.serviceCount++,
name: name,
api: this.api,
events: this.events,
root: options.root || path.join(this.root, this.dir, name),
log: options.log || this.log,
options: options || {},
version: this.version
};
var service = new Service(definition, assign, this);
this.services[assign.id] = service;
return service;
};
/**
* Use pre-request middleware for rate limiting, message sanitizing etc
*
* Works exactly like Express/Connect middleware
*
* Examples:
*
* server.use(require('ss-rate-limiter'));
*
* @param {Function} middleware to execute
* @return {}
* @api public
*
*/
Server.prototype.use = function(fn) {
var middleware = fn(this);
this.middleware.push(middleware);
};
/**
* Returns a list of all files which need to be sent to the browser
*
* @return {Array}
* @api public
*
*/
Server.prototype.browserAssets = function(options) {
options = options || {};
var output = this.transport.browserAssets || [];
for (var id in this.services) {
var service = this.services[id];
if (service.browserAssets && service.browserAssets.length) {
output = output.concat(service.browserAssets);
}
}
return output;
};
/**
* Builds a custom client module which includes the client-side
* transport and service code you need
*
* @return {String}
* @api public
*
*/
Server.prototype.buildClient = function() {
return require('./lib/code-generator')(this);
};
/**
* Returns an array of non-private services which should be sent to the client
*
* @return {Array}
* @api public
*
*/
Server.prototype.publicServices = function() {
var buf = [];
for (var id in this.services) {
var service = this.services[id];
if (service.private) continue;
buf.push(service);
}
return buf;
};
/**
* Process Incoming Request from a Realtime Transport
*
* Examples:
*
* server.process({message: '1|{"method": "callMe"}', socketId: 1234});
*
* @param {Object} an object containing the raw message plus info about the sender (socketId, transport, etc)
* @return {null}
* @api public
*
*/
Server.prototype.process = function(params) {
var request = new Request(params, this);
execute(this.middleware, request, function(msg){
this.api._system.sendError(request.socketId, msg);
this.log('error', msg, 'for socketId', request.socketId);
}.bind(this));
};
/**
* Start Server
*
* Examples:
*
* server.start(function() {
* console.log("Realtime Server started on port %s", server.port);
* });
*
* @param {Function} Callback to execute once server has started
* @return {Object} Server-side API
* @api public
*
*/
Server.prototype.start = function(cb) {
if (!this.transport) throw new Error('The server can only be started once a Realtime Transport has been defined. Set with server.transport()');
var connection = this.transport.server(this);
for (var id in this.services) {
var service = this.services[id];
var serverApi = service.start(connection);
if (serverApi) this.api[service.assigned.name] = serverApi;
}
// Add main request processor
this.middleware.push(function(req) {
req.process();
});
if (cb) cb(); // todo: callback when server really starts
// Return an API object of functions you can call in your server-side code
return this.api;
};
Server.prototype._registerSystemService = function() {
this.service('_system', systemService(this));
};
// Private Helpers
function execute(stack, request, cb) {
function exec(req, res, i){
stack[i].call(stack, req, res, function(){
exec(req, res, ++i);
});
}
exec(request, cb, 0);
}
module.exports = function(options){
return new Server(options);
};