]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - example/iot-gateway/node_modules/engine.io/lib/transports/polling-jsonp.js
Initial commit
[apps/tidep0084.git] / example / iot-gateway / node_modules / engine.io / lib / transports / polling-jsonp.js
2 /**
3  * Module dependencies.
4  */
6 var Polling = require('./polling');
7 var qs = require('querystring');
8 var rDoubleSlashes = /\\\\n/g;
9 var rSlashes = /(\\)?\\n/g;
11 /**
12  * Module exports.
13  */
15 module.exports = JSONP;
17 /**
18  * JSON-P polling transport.
19  *
20  * @api public
21  */
23 function JSONP (req) {
24   Polling.call(this, req);
26   this.head = '___eio[' + (req._query.j || '').replace(/[^0-9]/g, '') + '](';
27   this.foot = ');';
28 };
30 /**
31  * Inherits from Polling.
32  */
34 JSONP.prototype.__proto__ = Polling.prototype;
36 /**
37  * Handles incoming data.
38  * Due to a bug in \n handling by browsers, we expect a escaped string.
39  *
40  * @api private
41  */
43 JSONP.prototype.onData = function (data) {
44   // we leverage the qs module so that we get built-in DoS protection
45   // and the fast alternative to decodeURIComponent
46   data = qs.parse(data).d;
47   if ('string' == typeof data) {
48     //client will send already escaped newlines as \\\\n and newlines as \\n
49     // \\n must be replaced with \n and \\\\n with \\n
50     data = data.replace(rSlashes, function(match, slashes) {
51       return slashes ? match : '\n';
52     });
53     Polling.prototype.onData.call(this, data.replace(rDoubleSlashes, '\\n'));
54   }
55 };
57 /**
58  * Performs the write.
59  *
60  * @api private
61  */
63 JSONP.prototype.doWrite = function (data, options, callback) {
64   // we must output valid javascript, not valid json
65   // see: http://timelessrepo.com/json-isnt-a-javascript-subset
66   var js = JSON.stringify(data)
67     .replace(/\u2028/g, '\\u2028')
68     .replace(/\u2029/g, '\\u2029');
70   // prepare response
71   data = this.head + js + this.foot;
73   Polling.prototype.doWrite.call(this, data, options, callback);
74 };