/
polling.js
229 lines (188 loc) · 4.32 KB
/
polling.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
/**
* Module requirements.
*/
var Transport = require('../transport')
, parser = require('../parser')
, debug = require('debug')('engine:polling')
/**
* Exports the constructor.
*/
module.exports = Polling;
/**
* HTTP polling constructor.
*
* @api public.
*/
function Polling (req) {
Transport.call(this, req);
};
/**
* Inherits from Transport.
*
* @api public.
*/
Polling.prototype.__proto__ = Transport.prototype;
/**
* Transport name
*
* @api public
*/
Polling.prototype.name = 'polling';
/**
* Overrides onRequest.
*
* @param {http.ServerRequest}
* @api private
*/
Polling.prototype.onRequest = function (req) {
var res = req.res;
if ('GET' == req.method) {
this.onPollRequest(req, res);
} else if ('POST' == req.method) {
this.onDataRequest(req, res);
} else {
res.writeHead(500);
res.end();
}
};
/**
* The client sends a request awaiting for us to send data.
*
* @api private
*/
Polling.prototype.onPollRequest = function (req, res) {
if (this.req) {
debug('request overlap');
// assert: this.res, '.req and .res should be (un)set together'
this.onError('overlap from client');
res.writeHead(500);
} else {
debug('setting request');
this.req = req;
this.res = res;
var self = this;
function onClose () {
self.onError('poll connection closed prematurely');
}
function cleanup () {
req.removeListener('close', onClose);
self.req = self.res = null;
}
req.cleanup = cleanup;
req.on('close', onClose);
this.writable = true;
this.emit('drain');
// if we're still writable but had a pending close, trigger an empty send
if (this.writable && this.shouldClose) {
debug('triggering empty send to append close packet');
this.send([]);
}
}
};
/**
* The client sends a request with data.
*
* @api private
*/
Polling.prototype.onDataRequest = function (req, res) {
if (this.dataReq) {
// assert: this.dataRes, '.dataReq and .dataRes should be (un)set together'
this.onError('data request overlap from client');
res.writeHead(500);
} else {
this.dataReq = req;
this.dataRes = res;
var chunks = ''
, self = this
function cleanup () {
chunks = '';
req.removeListener('data', onData);
req.removeListener('end', onEnd);
req.removeListener('close', onClose);
self.dataReq = self.dataRes = null;
};
function onClose () {
cleanup();
self.onError('data request connection closed prematurely');
};
function onData (data) {
chunks += data;
};
function onEnd () {
self.onData(chunks);
res.writeHead(200, self.headers(req, { 'Content-Length': 2 }));
res.end('ok');
cleanup();
};
req.abort = cleanup;
req.on('close', onClose);
req.on('data', onData);
req.on('end', onEnd);
req.setEncoding('utf8');
}
};
/**
* Processes the incoming data payload.
*
* @param {String} encoded payload
* @api private
*/
Polling.prototype.onData = function (data) {
debug('data', data);
var packets = parser.decodePayload(data);
for (var i = 0, l = packets.length; i < l; i++) {
if ('close' == packets[i].type) {
debug('got xhr close packet');
return this.onClose();
}
this.onPacket(packets[i]);
}
};
/**
* Writes a packet payload.
*
* @param {Object} packet
* @api private
*/
Polling.prototype.send = function (packets) {
if (this.shouldClose) {
debug('appending close packet to payload');
packets.push({ type: 'close' });
this.shouldClose();
this.shouldClose = null;
}
this.write(parser.encodePayload(packets));
};
/**
* Writes data as response to poll request.
*
* @param {String} data
* @api private
*/
Polling.prototype.write = function (data) {
debug('writing', data);
this.doWrite(data);
this.req.cleanup();
this.writable = false;
};
/**
* Closes the transport.
*
* @api private
*/
Polling.prototype.doClose = function (fn) {
debug('closing');
if (this.dataReq) {
// FIXME: should we do this?
debug('aborting ongoing data request');
this.dataReq.abort();
}
if (this.writable) {
debug('transport writable - closing right away');
this.send([{ type: 'close' }]);
fn();
} else {
debug('transport not writable - buffering orderly close');
this.shouldClose = fn;
}
};