1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.JSONP = void 0;
|
---|
4 | const polling_1 = require("./polling");
|
---|
5 | const qs = require("querystring");
|
---|
6 | const rDoubleSlashes = /\\\\n/g;
|
---|
7 | const rSlashes = /(\\)?\\n/g;
|
---|
8 | class JSONP extends polling_1.Polling {
|
---|
9 | /**
|
---|
10 | * JSON-P polling transport.
|
---|
11 | *
|
---|
12 | * @api public
|
---|
13 | */
|
---|
14 | constructor(req) {
|
---|
15 | super(req);
|
---|
16 | this.head = "___eio[" + (req._query.j || "").replace(/[^0-9]/g, "") + "](";
|
---|
17 | this.foot = ");";
|
---|
18 | }
|
---|
19 | /**
|
---|
20 | * Handles incoming data.
|
---|
21 | * Due to a bug in \n handling by browsers, we expect a escaped string.
|
---|
22 | *
|
---|
23 | * @api private
|
---|
24 | */
|
---|
25 | onData(data) {
|
---|
26 | // we leverage the qs module so that we get built-in DoS protection
|
---|
27 | // and the fast alternative to decodeURIComponent
|
---|
28 | data = qs.parse(data).d;
|
---|
29 | if ("string" === typeof data) {
|
---|
30 | // client will send already escaped newlines as \\\\n and newlines as \\n
|
---|
31 | // \\n must be replaced with \n and \\\\n with \\n
|
---|
32 | data = data.replace(rSlashes, function (match, slashes) {
|
---|
33 | return slashes ? match : "\n";
|
---|
34 | });
|
---|
35 | super.onData(data.replace(rDoubleSlashes, "\\n"));
|
---|
36 | }
|
---|
37 | }
|
---|
38 | /**
|
---|
39 | * Performs the write.
|
---|
40 | *
|
---|
41 | * @api private
|
---|
42 | */
|
---|
43 | doWrite(data, options, callback) {
|
---|
44 | // we must output valid javascript, not valid json
|
---|
45 | // see: http://timelessrepo.com/json-isnt-a-javascript-subset
|
---|
46 | const js = JSON.stringify(data)
|
---|
47 | .replace(/\u2028/g, "\\u2028")
|
---|
48 | .replace(/\u2029/g, "\\u2029");
|
---|
49 | // prepare response
|
---|
50 | data = this.head + js + this.foot;
|
---|
51 | super.doWrite(data, options, callback);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | exports.JSONP = JSONP;
|
---|