/
transformer.js
228 lines (191 loc) · 6.5 KB
/
transformer.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
'use strict';
var querystring = require('querystring').parse
, url = require('url').parse;
//
// Used to fake middleware's as we don't have a next callback.
//
function noop() {}
/**
* Transformer skeletons
*
* @constructor
* @param {Primus} primus Reference to the Primus.
* @api public
*/
function Transformer(primus) {
this.Spark = primus.Spark; // Used by the Server to create a new connection.
this.primus = primus; // Reference to the Primus instance.
this.primusjs = null; // Path to the client library.
this.specfile = null; // Path to the Primus specification.
this.service = null; // Stores the real-time service.
this.buffer = null; // Buffer of the library.
this.initialise();
}
Transformer.prototype.__proto__ = require('events').EventEmitter.prototype;
//
// Simple logger shortcut.
//
Object.defineProperty(Transformer.prototype, 'logger', {
get: function logger() {
return {
error: this.log.bind(this.primus, 'log', 'error'), // Log error <line>.
warn: this.log.bind(this.primus, 'log', 'warn'), // Log warn <line>.
info: this.log.bind(this.primus, 'log', 'info'), // Log info <line>.
debug: this.log.bind(this.primus, 'log', 'debug'), // Log debug <line>.
plain: this.log.bind(this.primus, 'log') // Log x <line>.
};
}
});
/**
* Simple log handler that will emit log messages under the given `type`.
*
* @api private
*/
Transformer.prototype.log = function log(type) {
this.emit.apply(this, arguments);
};
/**
* Create the server and attach the appropriate event listeners.
*
* @api private
*/
Transformer.prototype.initialise = function initialise() {
if (this.server) this.server();
var server = this.primus.server
, transformer = this;
server.listeners('request').map(this.on.bind(this, 'previous::request'));
server.listeners('upgrade').map(this.on.bind(this, 'previous::upgrade'));
//
// Remove the old listeners as we want to be the first request handler for all
// events.
//
server.removeAllListeners('request');
server.removeAllListeners('upgrade');
//
// Emit a close event.
//
server.on('close', function close() {
transformer.emit('close');
});
//
// Start listening for incoming requests if we have a listener assigned to us.
//
if (this.listeners('request').length || this.listeners('previous::request').length) {
server.on('request', this.request.bind(this));
}
if (this.listeners('upgrade').length || this.listeners('previous::upgrade').length) {
server.on('upgrade', this.upgrade.bind(this));
}
//
// Create a client URL, this where we respond with our library. The path to
// the server specification which can be used to retrieve the transformer that
// was used.
//
var pathname = this.primus.pathname.split('/').filter(Boolean)
, client = pathname.slice(0)
, spec = pathname.slice(0);
client.push('primus.js');
spec.push('spec');
this.primusjs = '/'+ client.join('/');
this.specfile = '/'+ spec.join('/');
//
// Listen for static requests.
//
this.on('static', function serve(req, res) {
this.buffer = this.buffer || new Buffer(this.primus.library());
res.statusCode = 200;
res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
res.end(this.buffer);
});
this.on('spec', function spec(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(this.primus.spec));
});
};
/**
* Start listening for incoming requests and check if we need to forward them to
* the transformers.
*
* @param {Request} req HTTP request.
* @param {Response} res HTTP response.
* @api private
*/
Transformer.prototype.request = function request(req, res) {
if (!this.test(req)) return this.emit('previous::request', req, res);
if (req.uri.pathname === this.primusjs) return this.emit('static', req, res);
if (req.uri.pathname === this.specfile) return this.emit('spec', req, res);
if (!this.primus.auth) return this.emit('request', req, res, noop);
var transformer = this;
this.primus.auth(req, function authorized(err) {
if (!err) return transformer.emit('request', req, res, noop);
res.statusCode = err.statusCode || 401;
res.setHeader('Content-Type', 'application/json');
if ((res.statusCode === 401) && err.authenticate) {
res.setHeader('WWW-Authenticate', err.authenticate);
}
res.end(JSON.stringify({ error: err.message || err }));
});
};
/**
* Starting listening for incoming upgrade requests and check if we need to
* forward them to the transformers.
*
* @param {Request} req HTTP request.
* @param {Socket} socket Socket.
* @param {Buffer} head Buffered data.
* @api private
*/
Transformer.prototype.upgrade = function upgrade(req, socket, head) {
//
// Copy buffer to prevent large buffer retention in Node core.
// @see jmatthewsr-ms/node-slab-memory-issues
//
var buffy = new Buffer(head.length);
head.copy(buffy);
if (!this.test(req)) return this.emit('previous::upgrade', req, socket, buffy);
if (!this.primus.auth) return this.emit('upgrade', req, socket, buffy, noop);
var transformer = this;
this.primus.auth(req, function authorized(err) {
if (!err) return transformer.emit('upgrade', req, socket, buffy, noop);
var message = JSON.stringify({ error: err.message || err });
var code = err.statusCode || 401;
socket.write('HTTP/' + req.httpVersion + ' ');
socket.write(code + ' ' + require('http').STATUS_CODES[code] + '\r\n');
socket.write('Connection: close\r\n');
socket.write('Content-Type: application/json\r\n');
socket.write('Content-Length: ' + message.length + '\r\n');
if ((code === 401) && err.authenticate) {
socket.write('WWW-Authenticate: ' + err.authenticate + '\r\n');
}
socket.write('\r\n');
socket.write(message);
socket.destroy();
});
};
/**
* Check if we should accept this request.
*
* @param {Request} req HTTP Request.
* @returns {Boolean} Do we need to accept this request.
* @api private
*/
Transformer.prototype.test = function test(req) {
req.uri = url(req.url);
var pathname = req.uri.pathname || '/'
, route = this.primus.pathname
, accepted = pathname.slice(0, route.length) === route;
if (!accepted) this.emit('unknown', req);
//
// Make sure that the first part of the path matches.
//
return accepted;
};
//
// Make the transporter extendable.
//
Transformer.extend = require('extendable');
//
// Expose the transformer's skeleton.
//
module.exports = Transformer;