-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
xhr.js
42 lines (38 loc) · 877 Bytes
/
xhr.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
'use strict';
const Session = require('../session');
module.exports = {
xhr_send(req, res, _head, next) {
if (!req.body) {
return next({
status: 500,
message: 'Payload expected.'
});
}
let d;
try {
d = JSON.parse(req.body);
} catch (x) {
return next({
status: 500,
message: 'Broken JSON encoding.'
});
}
if (!d || d.__proto__.constructor !== Array) {
return next({
status: 500,
message: 'Payload expected.'
});
}
const jsonp = Session.bySessionId(req.session);
if (!jsonp) {
return next({ status: 404 });
}
for (const message of d) {
jsonp.didMessage(message);
}
// FF assumes that the response is XML.
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.writeHead(204);
res.end();
}
};