mbed connector webapp quickstart

Committer:
group-Make-NTU-Hackathon
Date:
Fri Feb 10 08:36:15 2017 +0000
Revision:
0:4233b7b10255
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-Make-NTU-Hackathon 0:4233b7b10255 1 !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.io=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 2
group-Make-NTU-Hackathon 0:4233b7b10255 3 module.exports = _dereq_('./lib/');
group-Make-NTU-Hackathon 0:4233b7b10255 4
group-Make-NTU-Hackathon 0:4233b7b10255 5 },{"./lib/":2}],2:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 6
group-Make-NTU-Hackathon 0:4233b7b10255 7 /**
group-Make-NTU-Hackathon 0:4233b7b10255 8 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 9 */
group-Make-NTU-Hackathon 0:4233b7b10255 10
group-Make-NTU-Hackathon 0:4233b7b10255 11 var url = _dereq_('./url');
group-Make-NTU-Hackathon 0:4233b7b10255 12 var parser = _dereq_('socket.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 13 var Manager = _dereq_('./manager');
group-Make-NTU-Hackathon 0:4233b7b10255 14 var debug = _dereq_('debug')('socket.io-client');
group-Make-NTU-Hackathon 0:4233b7b10255 15
group-Make-NTU-Hackathon 0:4233b7b10255 16 /**
group-Make-NTU-Hackathon 0:4233b7b10255 17 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 18 */
group-Make-NTU-Hackathon 0:4233b7b10255 19
group-Make-NTU-Hackathon 0:4233b7b10255 20 module.exports = exports = lookup;
group-Make-NTU-Hackathon 0:4233b7b10255 21
group-Make-NTU-Hackathon 0:4233b7b10255 22 /**
group-Make-NTU-Hackathon 0:4233b7b10255 23 * Managers cache.
group-Make-NTU-Hackathon 0:4233b7b10255 24 */
group-Make-NTU-Hackathon 0:4233b7b10255 25
group-Make-NTU-Hackathon 0:4233b7b10255 26 var cache = exports.managers = {};
group-Make-NTU-Hackathon 0:4233b7b10255 27
group-Make-NTU-Hackathon 0:4233b7b10255 28 /**
group-Make-NTU-Hackathon 0:4233b7b10255 29 * Looks up an existing `Manager` for multiplexing.
group-Make-NTU-Hackathon 0:4233b7b10255 30 * If the user summons:
group-Make-NTU-Hackathon 0:4233b7b10255 31 *
group-Make-NTU-Hackathon 0:4233b7b10255 32 * `io('http://localhost/a');`
group-Make-NTU-Hackathon 0:4233b7b10255 33 * `io('http://localhost/b');`
group-Make-NTU-Hackathon 0:4233b7b10255 34 *
group-Make-NTU-Hackathon 0:4233b7b10255 35 * We reuse the existing instance based on same scheme/port/host,
group-Make-NTU-Hackathon 0:4233b7b10255 36 * and we initialize sockets for each namespace.
group-Make-NTU-Hackathon 0:4233b7b10255 37 *
group-Make-NTU-Hackathon 0:4233b7b10255 38 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 39 */
group-Make-NTU-Hackathon 0:4233b7b10255 40
group-Make-NTU-Hackathon 0:4233b7b10255 41 function lookup(uri, opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 42 if (typeof uri == 'object') {
group-Make-NTU-Hackathon 0:4233b7b10255 43 opts = uri;
group-Make-NTU-Hackathon 0:4233b7b10255 44 uri = undefined;
group-Make-NTU-Hackathon 0:4233b7b10255 45 }
group-Make-NTU-Hackathon 0:4233b7b10255 46
group-Make-NTU-Hackathon 0:4233b7b10255 47 opts = opts || {};
group-Make-NTU-Hackathon 0:4233b7b10255 48
group-Make-NTU-Hackathon 0:4233b7b10255 49 var parsed = url(uri);
group-Make-NTU-Hackathon 0:4233b7b10255 50 var source = parsed.source;
group-Make-NTU-Hackathon 0:4233b7b10255 51 var id = parsed.id;
group-Make-NTU-Hackathon 0:4233b7b10255 52 var path = parsed.path;
group-Make-NTU-Hackathon 0:4233b7b10255 53 var sameNamespace = (cache[id] && cache[id].nsps[path] &&
group-Make-NTU-Hackathon 0:4233b7b10255 54 path == cache[id].nsps[path].nsp);
group-Make-NTU-Hackathon 0:4233b7b10255 55 var newConnection = opts.forceNew || opts['force new connection'] ||
group-Make-NTU-Hackathon 0:4233b7b10255 56 false === opts.multiplex || sameNamespace;
group-Make-NTU-Hackathon 0:4233b7b10255 57
group-Make-NTU-Hackathon 0:4233b7b10255 58 var io;
group-Make-NTU-Hackathon 0:4233b7b10255 59
group-Make-NTU-Hackathon 0:4233b7b10255 60 if (newConnection) {
group-Make-NTU-Hackathon 0:4233b7b10255 61 debug('ignoring socket cache for %s', source);
group-Make-NTU-Hackathon 0:4233b7b10255 62 io = Manager(source, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 63 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 64 if (!cache[id]) {
group-Make-NTU-Hackathon 0:4233b7b10255 65 debug('new io instance for %s', source);
group-Make-NTU-Hackathon 0:4233b7b10255 66 cache[id] = Manager(source, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 67 }
group-Make-NTU-Hackathon 0:4233b7b10255 68 io = cache[id];
group-Make-NTU-Hackathon 0:4233b7b10255 69 }
group-Make-NTU-Hackathon 0:4233b7b10255 70
group-Make-NTU-Hackathon 0:4233b7b10255 71 return io.socket(parsed.path);
group-Make-NTU-Hackathon 0:4233b7b10255 72 }
group-Make-NTU-Hackathon 0:4233b7b10255 73
group-Make-NTU-Hackathon 0:4233b7b10255 74 /**
group-Make-NTU-Hackathon 0:4233b7b10255 75 * Protocol version.
group-Make-NTU-Hackathon 0:4233b7b10255 76 *
group-Make-NTU-Hackathon 0:4233b7b10255 77 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 78 */
group-Make-NTU-Hackathon 0:4233b7b10255 79
group-Make-NTU-Hackathon 0:4233b7b10255 80 exports.protocol = parser.protocol;
group-Make-NTU-Hackathon 0:4233b7b10255 81
group-Make-NTU-Hackathon 0:4233b7b10255 82 /**
group-Make-NTU-Hackathon 0:4233b7b10255 83 * `connect`.
group-Make-NTU-Hackathon 0:4233b7b10255 84 *
group-Make-NTU-Hackathon 0:4233b7b10255 85 * @param {String} uri
group-Make-NTU-Hackathon 0:4233b7b10255 86 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 87 */
group-Make-NTU-Hackathon 0:4233b7b10255 88
group-Make-NTU-Hackathon 0:4233b7b10255 89 exports.connect = lookup;
group-Make-NTU-Hackathon 0:4233b7b10255 90
group-Make-NTU-Hackathon 0:4233b7b10255 91 /**
group-Make-NTU-Hackathon 0:4233b7b10255 92 * Expose constructors for standalone build.
group-Make-NTU-Hackathon 0:4233b7b10255 93 *
group-Make-NTU-Hackathon 0:4233b7b10255 94 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 95 */
group-Make-NTU-Hackathon 0:4233b7b10255 96
group-Make-NTU-Hackathon 0:4233b7b10255 97 exports.Manager = _dereq_('./manager');
group-Make-NTU-Hackathon 0:4233b7b10255 98 exports.Socket = _dereq_('./socket');
group-Make-NTU-Hackathon 0:4233b7b10255 99
group-Make-NTU-Hackathon 0:4233b7b10255 100 },{"./manager":3,"./socket":5,"./url":6,"debug":10,"socket.io-parser":46}],3:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 101
group-Make-NTU-Hackathon 0:4233b7b10255 102 /**
group-Make-NTU-Hackathon 0:4233b7b10255 103 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 104 */
group-Make-NTU-Hackathon 0:4233b7b10255 105
group-Make-NTU-Hackathon 0:4233b7b10255 106 var url = _dereq_('./url');
group-Make-NTU-Hackathon 0:4233b7b10255 107 var eio = _dereq_('engine.io-client');
group-Make-NTU-Hackathon 0:4233b7b10255 108 var Socket = _dereq_('./socket');
group-Make-NTU-Hackathon 0:4233b7b10255 109 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 110 var parser = _dereq_('socket.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 111 var on = _dereq_('./on');
group-Make-NTU-Hackathon 0:4233b7b10255 112 var bind = _dereq_('component-bind');
group-Make-NTU-Hackathon 0:4233b7b10255 113 var object = _dereq_('object-component');
group-Make-NTU-Hackathon 0:4233b7b10255 114 var debug = _dereq_('debug')('socket.io-client:manager');
group-Make-NTU-Hackathon 0:4233b7b10255 115 var indexOf = _dereq_('indexof');
group-Make-NTU-Hackathon 0:4233b7b10255 116 var Backoff = _dereq_('backo2');
group-Make-NTU-Hackathon 0:4233b7b10255 117
group-Make-NTU-Hackathon 0:4233b7b10255 118 /**
group-Make-NTU-Hackathon 0:4233b7b10255 119 * Module exports
group-Make-NTU-Hackathon 0:4233b7b10255 120 */
group-Make-NTU-Hackathon 0:4233b7b10255 121
group-Make-NTU-Hackathon 0:4233b7b10255 122 module.exports = Manager;
group-Make-NTU-Hackathon 0:4233b7b10255 123
group-Make-NTU-Hackathon 0:4233b7b10255 124 /**
group-Make-NTU-Hackathon 0:4233b7b10255 125 * `Manager` constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 126 *
group-Make-NTU-Hackathon 0:4233b7b10255 127 * @param {String} engine instance or engine uri/opts
group-Make-NTU-Hackathon 0:4233b7b10255 128 * @param {Object} options
group-Make-NTU-Hackathon 0:4233b7b10255 129 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 130 */
group-Make-NTU-Hackathon 0:4233b7b10255 131
group-Make-NTU-Hackathon 0:4233b7b10255 132 function Manager(uri, opts){
group-Make-NTU-Hackathon 0:4233b7b10255 133 if (!(this instanceof Manager)) return new Manager(uri, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 134 if (uri && ('object' == typeof uri)) {
group-Make-NTU-Hackathon 0:4233b7b10255 135 opts = uri;
group-Make-NTU-Hackathon 0:4233b7b10255 136 uri = undefined;
group-Make-NTU-Hackathon 0:4233b7b10255 137 }
group-Make-NTU-Hackathon 0:4233b7b10255 138 opts = opts || {};
group-Make-NTU-Hackathon 0:4233b7b10255 139
group-Make-NTU-Hackathon 0:4233b7b10255 140 opts.path = opts.path || '/socket.io';
group-Make-NTU-Hackathon 0:4233b7b10255 141 this.nsps = {};
group-Make-NTU-Hackathon 0:4233b7b10255 142 this.subs = [];
group-Make-NTU-Hackathon 0:4233b7b10255 143 this.opts = opts;
group-Make-NTU-Hackathon 0:4233b7b10255 144 this.reconnection(opts.reconnection !== false);
group-Make-NTU-Hackathon 0:4233b7b10255 145 this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
group-Make-NTU-Hackathon 0:4233b7b10255 146 this.reconnectionDelay(opts.reconnectionDelay || 1000);
group-Make-NTU-Hackathon 0:4233b7b10255 147 this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
group-Make-NTU-Hackathon 0:4233b7b10255 148 this.randomizationFactor(opts.randomizationFactor || 0.5);
group-Make-NTU-Hackathon 0:4233b7b10255 149 this.backoff = new Backoff({
group-Make-NTU-Hackathon 0:4233b7b10255 150 min: this.reconnectionDelay(),
group-Make-NTU-Hackathon 0:4233b7b10255 151 max: this.reconnectionDelayMax(),
group-Make-NTU-Hackathon 0:4233b7b10255 152 jitter: this.randomizationFactor()
group-Make-NTU-Hackathon 0:4233b7b10255 153 });
group-Make-NTU-Hackathon 0:4233b7b10255 154 this.timeout(null == opts.timeout ? 20000 : opts.timeout);
group-Make-NTU-Hackathon 0:4233b7b10255 155 this.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 156 this.uri = uri;
group-Make-NTU-Hackathon 0:4233b7b10255 157 this.connected = [];
group-Make-NTU-Hackathon 0:4233b7b10255 158 this.encoding = false;
group-Make-NTU-Hackathon 0:4233b7b10255 159 this.packetBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 160 this.encoder = new parser.Encoder();
group-Make-NTU-Hackathon 0:4233b7b10255 161 this.decoder = new parser.Decoder();
group-Make-NTU-Hackathon 0:4233b7b10255 162 this.autoConnect = opts.autoConnect !== false;
group-Make-NTU-Hackathon 0:4233b7b10255 163 if (this.autoConnect) this.open();
group-Make-NTU-Hackathon 0:4233b7b10255 164 }
group-Make-NTU-Hackathon 0:4233b7b10255 165
group-Make-NTU-Hackathon 0:4233b7b10255 166 /**
group-Make-NTU-Hackathon 0:4233b7b10255 167 * Propagate given event to sockets and emit on `this`
group-Make-NTU-Hackathon 0:4233b7b10255 168 *
group-Make-NTU-Hackathon 0:4233b7b10255 169 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 170 */
group-Make-NTU-Hackathon 0:4233b7b10255 171
group-Make-NTU-Hackathon 0:4233b7b10255 172 Manager.prototype.emitAll = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 173 this.emit.apply(this, arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 174 for (var nsp in this.nsps) {
group-Make-NTU-Hackathon 0:4233b7b10255 175 this.nsps[nsp].emit.apply(this.nsps[nsp], arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 176 }
group-Make-NTU-Hackathon 0:4233b7b10255 177 };
group-Make-NTU-Hackathon 0:4233b7b10255 178
group-Make-NTU-Hackathon 0:4233b7b10255 179 /**
group-Make-NTU-Hackathon 0:4233b7b10255 180 * Update `socket.id` of all sockets
group-Make-NTU-Hackathon 0:4233b7b10255 181 *
group-Make-NTU-Hackathon 0:4233b7b10255 182 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 183 */
group-Make-NTU-Hackathon 0:4233b7b10255 184
group-Make-NTU-Hackathon 0:4233b7b10255 185 Manager.prototype.updateSocketIds = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 186 for (var nsp in this.nsps) {
group-Make-NTU-Hackathon 0:4233b7b10255 187 this.nsps[nsp].id = this.engine.id;
group-Make-NTU-Hackathon 0:4233b7b10255 188 }
group-Make-NTU-Hackathon 0:4233b7b10255 189 };
group-Make-NTU-Hackathon 0:4233b7b10255 190
group-Make-NTU-Hackathon 0:4233b7b10255 191 /**
group-Make-NTU-Hackathon 0:4233b7b10255 192 * Mix in `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 193 */
group-Make-NTU-Hackathon 0:4233b7b10255 194
group-Make-NTU-Hackathon 0:4233b7b10255 195 Emitter(Manager.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 196
group-Make-NTU-Hackathon 0:4233b7b10255 197 /**
group-Make-NTU-Hackathon 0:4233b7b10255 198 * Sets the `reconnection` config.
group-Make-NTU-Hackathon 0:4233b7b10255 199 *
group-Make-NTU-Hackathon 0:4233b7b10255 200 * @param {Boolean} true/false if it should automatically reconnect
group-Make-NTU-Hackathon 0:4233b7b10255 201 * @return {Manager} self or value
group-Make-NTU-Hackathon 0:4233b7b10255 202 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 203 */
group-Make-NTU-Hackathon 0:4233b7b10255 204
group-Make-NTU-Hackathon 0:4233b7b10255 205 Manager.prototype.reconnection = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 206 if (!arguments.length) return this._reconnection;
group-Make-NTU-Hackathon 0:4233b7b10255 207 this._reconnection = !!v;
group-Make-NTU-Hackathon 0:4233b7b10255 208 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 209 };
group-Make-NTU-Hackathon 0:4233b7b10255 210
group-Make-NTU-Hackathon 0:4233b7b10255 211 /**
group-Make-NTU-Hackathon 0:4233b7b10255 212 * Sets the reconnection attempts config.
group-Make-NTU-Hackathon 0:4233b7b10255 213 *
group-Make-NTU-Hackathon 0:4233b7b10255 214 * @param {Number} max reconnection attempts before giving up
group-Make-NTU-Hackathon 0:4233b7b10255 215 * @return {Manager} self or value
group-Make-NTU-Hackathon 0:4233b7b10255 216 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 217 */
group-Make-NTU-Hackathon 0:4233b7b10255 218
group-Make-NTU-Hackathon 0:4233b7b10255 219 Manager.prototype.reconnectionAttempts = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 220 if (!arguments.length) return this._reconnectionAttempts;
group-Make-NTU-Hackathon 0:4233b7b10255 221 this._reconnectionAttempts = v;
group-Make-NTU-Hackathon 0:4233b7b10255 222 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 223 };
group-Make-NTU-Hackathon 0:4233b7b10255 224
group-Make-NTU-Hackathon 0:4233b7b10255 225 /**
group-Make-NTU-Hackathon 0:4233b7b10255 226 * Sets the delay between reconnections.
group-Make-NTU-Hackathon 0:4233b7b10255 227 *
group-Make-NTU-Hackathon 0:4233b7b10255 228 * @param {Number} delay
group-Make-NTU-Hackathon 0:4233b7b10255 229 * @return {Manager} self or value
group-Make-NTU-Hackathon 0:4233b7b10255 230 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 231 */
group-Make-NTU-Hackathon 0:4233b7b10255 232
group-Make-NTU-Hackathon 0:4233b7b10255 233 Manager.prototype.reconnectionDelay = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 234 if (!arguments.length) return this._reconnectionDelay;
group-Make-NTU-Hackathon 0:4233b7b10255 235 this._reconnectionDelay = v;
group-Make-NTU-Hackathon 0:4233b7b10255 236 this.backoff && this.backoff.setMin(v);
group-Make-NTU-Hackathon 0:4233b7b10255 237 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 238 };
group-Make-NTU-Hackathon 0:4233b7b10255 239
group-Make-NTU-Hackathon 0:4233b7b10255 240 Manager.prototype.randomizationFactor = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 241 if (!arguments.length) return this._randomizationFactor;
group-Make-NTU-Hackathon 0:4233b7b10255 242 this._randomizationFactor = v;
group-Make-NTU-Hackathon 0:4233b7b10255 243 this.backoff && this.backoff.setJitter(v);
group-Make-NTU-Hackathon 0:4233b7b10255 244 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 245 };
group-Make-NTU-Hackathon 0:4233b7b10255 246
group-Make-NTU-Hackathon 0:4233b7b10255 247 /**
group-Make-NTU-Hackathon 0:4233b7b10255 248 * Sets the maximum delay between reconnections.
group-Make-NTU-Hackathon 0:4233b7b10255 249 *
group-Make-NTU-Hackathon 0:4233b7b10255 250 * @param {Number} delay
group-Make-NTU-Hackathon 0:4233b7b10255 251 * @return {Manager} self or value
group-Make-NTU-Hackathon 0:4233b7b10255 252 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 253 */
group-Make-NTU-Hackathon 0:4233b7b10255 254
group-Make-NTU-Hackathon 0:4233b7b10255 255 Manager.prototype.reconnectionDelayMax = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 256 if (!arguments.length) return this._reconnectionDelayMax;
group-Make-NTU-Hackathon 0:4233b7b10255 257 this._reconnectionDelayMax = v;
group-Make-NTU-Hackathon 0:4233b7b10255 258 this.backoff && this.backoff.setMax(v);
group-Make-NTU-Hackathon 0:4233b7b10255 259 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 260 };
group-Make-NTU-Hackathon 0:4233b7b10255 261
group-Make-NTU-Hackathon 0:4233b7b10255 262 /**
group-Make-NTU-Hackathon 0:4233b7b10255 263 * Sets the connection timeout. `false` to disable
group-Make-NTU-Hackathon 0:4233b7b10255 264 *
group-Make-NTU-Hackathon 0:4233b7b10255 265 * @return {Manager} self or value
group-Make-NTU-Hackathon 0:4233b7b10255 266 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 267 */
group-Make-NTU-Hackathon 0:4233b7b10255 268
group-Make-NTU-Hackathon 0:4233b7b10255 269 Manager.prototype.timeout = function(v){
group-Make-NTU-Hackathon 0:4233b7b10255 270 if (!arguments.length) return this._timeout;
group-Make-NTU-Hackathon 0:4233b7b10255 271 this._timeout = v;
group-Make-NTU-Hackathon 0:4233b7b10255 272 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 273 };
group-Make-NTU-Hackathon 0:4233b7b10255 274
group-Make-NTU-Hackathon 0:4233b7b10255 275 /**
group-Make-NTU-Hackathon 0:4233b7b10255 276 * Starts trying to reconnect if reconnection is enabled and we have not
group-Make-NTU-Hackathon 0:4233b7b10255 277 * started reconnecting yet
group-Make-NTU-Hackathon 0:4233b7b10255 278 *
group-Make-NTU-Hackathon 0:4233b7b10255 279 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 280 */
group-Make-NTU-Hackathon 0:4233b7b10255 281
group-Make-NTU-Hackathon 0:4233b7b10255 282 Manager.prototype.maybeReconnectOnOpen = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 283 // Only try to reconnect if it's the first time we're connecting
group-Make-NTU-Hackathon 0:4233b7b10255 284 if (!this.reconnecting && this._reconnection && this.backoff.attempts === 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 285 // keeps reconnection from firing twice for the same reconnection loop
group-Make-NTU-Hackathon 0:4233b7b10255 286 this.reconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 287 }
group-Make-NTU-Hackathon 0:4233b7b10255 288 };
group-Make-NTU-Hackathon 0:4233b7b10255 289
group-Make-NTU-Hackathon 0:4233b7b10255 290
group-Make-NTU-Hackathon 0:4233b7b10255 291 /**
group-Make-NTU-Hackathon 0:4233b7b10255 292 * Sets the current transport `socket`.
group-Make-NTU-Hackathon 0:4233b7b10255 293 *
group-Make-NTU-Hackathon 0:4233b7b10255 294 * @param {Function} optional, callback
group-Make-NTU-Hackathon 0:4233b7b10255 295 * @return {Manager} self
group-Make-NTU-Hackathon 0:4233b7b10255 296 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 297 */
group-Make-NTU-Hackathon 0:4233b7b10255 298
group-Make-NTU-Hackathon 0:4233b7b10255 299 Manager.prototype.open =
group-Make-NTU-Hackathon 0:4233b7b10255 300 Manager.prototype.connect = function(fn){
group-Make-NTU-Hackathon 0:4233b7b10255 301 debug('readyState %s', this.readyState);
group-Make-NTU-Hackathon 0:4233b7b10255 302 if (~this.readyState.indexOf('open')) return this;
group-Make-NTU-Hackathon 0:4233b7b10255 303
group-Make-NTU-Hackathon 0:4233b7b10255 304 debug('opening %s', this.uri);
group-Make-NTU-Hackathon 0:4233b7b10255 305 this.engine = eio(this.uri, this.opts);
group-Make-NTU-Hackathon 0:4233b7b10255 306 var socket = this.engine;
group-Make-NTU-Hackathon 0:4233b7b10255 307 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 308 this.readyState = 'opening';
group-Make-NTU-Hackathon 0:4233b7b10255 309 this.skipReconnect = false;
group-Make-NTU-Hackathon 0:4233b7b10255 310
group-Make-NTU-Hackathon 0:4233b7b10255 311 // emit `open`
group-Make-NTU-Hackathon 0:4233b7b10255 312 var openSub = on(socket, 'open', function() {
group-Make-NTU-Hackathon 0:4233b7b10255 313 self.onopen();
group-Make-NTU-Hackathon 0:4233b7b10255 314 fn && fn();
group-Make-NTU-Hackathon 0:4233b7b10255 315 });
group-Make-NTU-Hackathon 0:4233b7b10255 316
group-Make-NTU-Hackathon 0:4233b7b10255 317 // emit `connect_error`
group-Make-NTU-Hackathon 0:4233b7b10255 318 var errorSub = on(socket, 'error', function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 319 debug('connect_error');
group-Make-NTU-Hackathon 0:4233b7b10255 320 self.cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 321 self.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 322 self.emitAll('connect_error', data);
group-Make-NTU-Hackathon 0:4233b7b10255 323 if (fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 324 var err = new Error('Connection error');
group-Make-NTU-Hackathon 0:4233b7b10255 325 err.data = data;
group-Make-NTU-Hackathon 0:4233b7b10255 326 fn(err);
group-Make-NTU-Hackathon 0:4233b7b10255 327 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 328 // Only do this if there is no fn to handle the error
group-Make-NTU-Hackathon 0:4233b7b10255 329 self.maybeReconnectOnOpen();
group-Make-NTU-Hackathon 0:4233b7b10255 330 }
group-Make-NTU-Hackathon 0:4233b7b10255 331 });
group-Make-NTU-Hackathon 0:4233b7b10255 332
group-Make-NTU-Hackathon 0:4233b7b10255 333 // emit `connect_timeout`
group-Make-NTU-Hackathon 0:4233b7b10255 334 if (false !== this._timeout) {
group-Make-NTU-Hackathon 0:4233b7b10255 335 var timeout = this._timeout;
group-Make-NTU-Hackathon 0:4233b7b10255 336 debug('connect attempt will timeout after %d', timeout);
group-Make-NTU-Hackathon 0:4233b7b10255 337
group-Make-NTU-Hackathon 0:4233b7b10255 338 // set timer
group-Make-NTU-Hackathon 0:4233b7b10255 339 var timer = setTimeout(function(){
group-Make-NTU-Hackathon 0:4233b7b10255 340 debug('connect attempt timed out after %d', timeout);
group-Make-NTU-Hackathon 0:4233b7b10255 341 openSub.destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 342 socket.close();
group-Make-NTU-Hackathon 0:4233b7b10255 343 socket.emit('error', 'timeout');
group-Make-NTU-Hackathon 0:4233b7b10255 344 self.emitAll('connect_timeout', timeout);
group-Make-NTU-Hackathon 0:4233b7b10255 345 }, timeout);
group-Make-NTU-Hackathon 0:4233b7b10255 346
group-Make-NTU-Hackathon 0:4233b7b10255 347 this.subs.push({
group-Make-NTU-Hackathon 0:4233b7b10255 348 destroy: function(){
group-Make-NTU-Hackathon 0:4233b7b10255 349 clearTimeout(timer);
group-Make-NTU-Hackathon 0:4233b7b10255 350 }
group-Make-NTU-Hackathon 0:4233b7b10255 351 });
group-Make-NTU-Hackathon 0:4233b7b10255 352 }
group-Make-NTU-Hackathon 0:4233b7b10255 353
group-Make-NTU-Hackathon 0:4233b7b10255 354 this.subs.push(openSub);
group-Make-NTU-Hackathon 0:4233b7b10255 355 this.subs.push(errorSub);
group-Make-NTU-Hackathon 0:4233b7b10255 356
group-Make-NTU-Hackathon 0:4233b7b10255 357 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 358 };
group-Make-NTU-Hackathon 0:4233b7b10255 359
group-Make-NTU-Hackathon 0:4233b7b10255 360 /**
group-Make-NTU-Hackathon 0:4233b7b10255 361 * Called upon transport open.
group-Make-NTU-Hackathon 0:4233b7b10255 362 *
group-Make-NTU-Hackathon 0:4233b7b10255 363 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 364 */
group-Make-NTU-Hackathon 0:4233b7b10255 365
group-Make-NTU-Hackathon 0:4233b7b10255 366 Manager.prototype.onopen = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 367 debug('open');
group-Make-NTU-Hackathon 0:4233b7b10255 368
group-Make-NTU-Hackathon 0:4233b7b10255 369 // clear old subs
group-Make-NTU-Hackathon 0:4233b7b10255 370 this.cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 371
group-Make-NTU-Hackathon 0:4233b7b10255 372 // mark as open
group-Make-NTU-Hackathon 0:4233b7b10255 373 this.readyState = 'open';
group-Make-NTU-Hackathon 0:4233b7b10255 374 this.emit('open');
group-Make-NTU-Hackathon 0:4233b7b10255 375
group-Make-NTU-Hackathon 0:4233b7b10255 376 // add new subs
group-Make-NTU-Hackathon 0:4233b7b10255 377 var socket = this.engine;
group-Make-NTU-Hackathon 0:4233b7b10255 378 this.subs.push(on(socket, 'data', bind(this, 'ondata')));
group-Make-NTU-Hackathon 0:4233b7b10255 379 this.subs.push(on(this.decoder, 'decoded', bind(this, 'ondecoded')));
group-Make-NTU-Hackathon 0:4233b7b10255 380 this.subs.push(on(socket, 'error', bind(this, 'onerror')));
group-Make-NTU-Hackathon 0:4233b7b10255 381 this.subs.push(on(socket, 'close', bind(this, 'onclose')));
group-Make-NTU-Hackathon 0:4233b7b10255 382 };
group-Make-NTU-Hackathon 0:4233b7b10255 383
group-Make-NTU-Hackathon 0:4233b7b10255 384 /**
group-Make-NTU-Hackathon 0:4233b7b10255 385 * Called with data.
group-Make-NTU-Hackathon 0:4233b7b10255 386 *
group-Make-NTU-Hackathon 0:4233b7b10255 387 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 388 */
group-Make-NTU-Hackathon 0:4233b7b10255 389
group-Make-NTU-Hackathon 0:4233b7b10255 390 Manager.prototype.ondata = function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 391 this.decoder.add(data);
group-Make-NTU-Hackathon 0:4233b7b10255 392 };
group-Make-NTU-Hackathon 0:4233b7b10255 393
group-Make-NTU-Hackathon 0:4233b7b10255 394 /**
group-Make-NTU-Hackathon 0:4233b7b10255 395 * Called when parser fully decodes a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 396 *
group-Make-NTU-Hackathon 0:4233b7b10255 397 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 398 */
group-Make-NTU-Hackathon 0:4233b7b10255 399
group-Make-NTU-Hackathon 0:4233b7b10255 400 Manager.prototype.ondecoded = function(packet) {
group-Make-NTU-Hackathon 0:4233b7b10255 401 this.emit('packet', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 402 };
group-Make-NTU-Hackathon 0:4233b7b10255 403
group-Make-NTU-Hackathon 0:4233b7b10255 404 /**
group-Make-NTU-Hackathon 0:4233b7b10255 405 * Called upon socket error.
group-Make-NTU-Hackathon 0:4233b7b10255 406 *
group-Make-NTU-Hackathon 0:4233b7b10255 407 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 408 */
group-Make-NTU-Hackathon 0:4233b7b10255 409
group-Make-NTU-Hackathon 0:4233b7b10255 410 Manager.prototype.onerror = function(err){
group-Make-NTU-Hackathon 0:4233b7b10255 411 debug('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 412 this.emitAll('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 413 };
group-Make-NTU-Hackathon 0:4233b7b10255 414
group-Make-NTU-Hackathon 0:4233b7b10255 415 /**
group-Make-NTU-Hackathon 0:4233b7b10255 416 * Creates a new socket for the given `nsp`.
group-Make-NTU-Hackathon 0:4233b7b10255 417 *
group-Make-NTU-Hackathon 0:4233b7b10255 418 * @return {Socket}
group-Make-NTU-Hackathon 0:4233b7b10255 419 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 420 */
group-Make-NTU-Hackathon 0:4233b7b10255 421
group-Make-NTU-Hackathon 0:4233b7b10255 422 Manager.prototype.socket = function(nsp){
group-Make-NTU-Hackathon 0:4233b7b10255 423 var socket = this.nsps[nsp];
group-Make-NTU-Hackathon 0:4233b7b10255 424 if (!socket) {
group-Make-NTU-Hackathon 0:4233b7b10255 425 socket = new Socket(this, nsp);
group-Make-NTU-Hackathon 0:4233b7b10255 426 this.nsps[nsp] = socket;
group-Make-NTU-Hackathon 0:4233b7b10255 427 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 428 socket.on('connect', function(){
group-Make-NTU-Hackathon 0:4233b7b10255 429 socket.id = self.engine.id;
group-Make-NTU-Hackathon 0:4233b7b10255 430 if (!~indexOf(self.connected, socket)) {
group-Make-NTU-Hackathon 0:4233b7b10255 431 self.connected.push(socket);
group-Make-NTU-Hackathon 0:4233b7b10255 432 }
group-Make-NTU-Hackathon 0:4233b7b10255 433 });
group-Make-NTU-Hackathon 0:4233b7b10255 434 }
group-Make-NTU-Hackathon 0:4233b7b10255 435 return socket;
group-Make-NTU-Hackathon 0:4233b7b10255 436 };
group-Make-NTU-Hackathon 0:4233b7b10255 437
group-Make-NTU-Hackathon 0:4233b7b10255 438 /**
group-Make-NTU-Hackathon 0:4233b7b10255 439 * Called upon a socket close.
group-Make-NTU-Hackathon 0:4233b7b10255 440 *
group-Make-NTU-Hackathon 0:4233b7b10255 441 * @param {Socket} socket
group-Make-NTU-Hackathon 0:4233b7b10255 442 */
group-Make-NTU-Hackathon 0:4233b7b10255 443
group-Make-NTU-Hackathon 0:4233b7b10255 444 Manager.prototype.destroy = function(socket){
group-Make-NTU-Hackathon 0:4233b7b10255 445 var index = indexOf(this.connected, socket);
group-Make-NTU-Hackathon 0:4233b7b10255 446 if (~index) this.connected.splice(index, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 447 if (this.connected.length) return;
group-Make-NTU-Hackathon 0:4233b7b10255 448
group-Make-NTU-Hackathon 0:4233b7b10255 449 this.close();
group-Make-NTU-Hackathon 0:4233b7b10255 450 };
group-Make-NTU-Hackathon 0:4233b7b10255 451
group-Make-NTU-Hackathon 0:4233b7b10255 452 /**
group-Make-NTU-Hackathon 0:4233b7b10255 453 * Writes a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 454 *
group-Make-NTU-Hackathon 0:4233b7b10255 455 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 456 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 457 */
group-Make-NTU-Hackathon 0:4233b7b10255 458
group-Make-NTU-Hackathon 0:4233b7b10255 459 Manager.prototype.packet = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 460 debug('writing packet %j', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 461 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 462
group-Make-NTU-Hackathon 0:4233b7b10255 463 if (!self.encoding) {
group-Make-NTU-Hackathon 0:4233b7b10255 464 // encode, then write to engine with result
group-Make-NTU-Hackathon 0:4233b7b10255 465 self.encoding = true;
group-Make-NTU-Hackathon 0:4233b7b10255 466 this.encoder.encode(packet, function(encodedPackets) {
group-Make-NTU-Hackathon 0:4233b7b10255 467 for (var i = 0; i < encodedPackets.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 468 self.engine.write(encodedPackets[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 469 }
group-Make-NTU-Hackathon 0:4233b7b10255 470 self.encoding = false;
group-Make-NTU-Hackathon 0:4233b7b10255 471 self.processPacketQueue();
group-Make-NTU-Hackathon 0:4233b7b10255 472 });
group-Make-NTU-Hackathon 0:4233b7b10255 473 } else { // add packet to the queue
group-Make-NTU-Hackathon 0:4233b7b10255 474 self.packetBuffer.push(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 475 }
group-Make-NTU-Hackathon 0:4233b7b10255 476 };
group-Make-NTU-Hackathon 0:4233b7b10255 477
group-Make-NTU-Hackathon 0:4233b7b10255 478 /**
group-Make-NTU-Hackathon 0:4233b7b10255 479 * If packet buffer is non-empty, begins encoding the
group-Make-NTU-Hackathon 0:4233b7b10255 480 * next packet in line.
group-Make-NTU-Hackathon 0:4233b7b10255 481 *
group-Make-NTU-Hackathon 0:4233b7b10255 482 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 483 */
group-Make-NTU-Hackathon 0:4233b7b10255 484
group-Make-NTU-Hackathon 0:4233b7b10255 485 Manager.prototype.processPacketQueue = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 486 if (this.packetBuffer.length > 0 && !this.encoding) {
group-Make-NTU-Hackathon 0:4233b7b10255 487 var pack = this.packetBuffer.shift();
group-Make-NTU-Hackathon 0:4233b7b10255 488 this.packet(pack);
group-Make-NTU-Hackathon 0:4233b7b10255 489 }
group-Make-NTU-Hackathon 0:4233b7b10255 490 };
group-Make-NTU-Hackathon 0:4233b7b10255 491
group-Make-NTU-Hackathon 0:4233b7b10255 492 /**
group-Make-NTU-Hackathon 0:4233b7b10255 493 * Clean up transport subscriptions and packet buffer.
group-Make-NTU-Hackathon 0:4233b7b10255 494 *
group-Make-NTU-Hackathon 0:4233b7b10255 495 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 496 */
group-Make-NTU-Hackathon 0:4233b7b10255 497
group-Make-NTU-Hackathon 0:4233b7b10255 498 Manager.prototype.cleanup = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 499 var sub;
group-Make-NTU-Hackathon 0:4233b7b10255 500 while (sub = this.subs.shift()) sub.destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 501
group-Make-NTU-Hackathon 0:4233b7b10255 502 this.packetBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 503 this.encoding = false;
group-Make-NTU-Hackathon 0:4233b7b10255 504
group-Make-NTU-Hackathon 0:4233b7b10255 505 this.decoder.destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 506 };
group-Make-NTU-Hackathon 0:4233b7b10255 507
group-Make-NTU-Hackathon 0:4233b7b10255 508 /**
group-Make-NTU-Hackathon 0:4233b7b10255 509 * Close the current socket.
group-Make-NTU-Hackathon 0:4233b7b10255 510 *
group-Make-NTU-Hackathon 0:4233b7b10255 511 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 512 */
group-Make-NTU-Hackathon 0:4233b7b10255 513
group-Make-NTU-Hackathon 0:4233b7b10255 514 Manager.prototype.close =
group-Make-NTU-Hackathon 0:4233b7b10255 515 Manager.prototype.disconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 516 this.skipReconnect = true;
group-Make-NTU-Hackathon 0:4233b7b10255 517 this.backoff.reset();
group-Make-NTU-Hackathon 0:4233b7b10255 518 this.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 519 this.engine && this.engine.close();
group-Make-NTU-Hackathon 0:4233b7b10255 520 };
group-Make-NTU-Hackathon 0:4233b7b10255 521
group-Make-NTU-Hackathon 0:4233b7b10255 522 /**
group-Make-NTU-Hackathon 0:4233b7b10255 523 * Called upon engine close.
group-Make-NTU-Hackathon 0:4233b7b10255 524 *
group-Make-NTU-Hackathon 0:4233b7b10255 525 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 526 */
group-Make-NTU-Hackathon 0:4233b7b10255 527
group-Make-NTU-Hackathon 0:4233b7b10255 528 Manager.prototype.onclose = function(reason){
group-Make-NTU-Hackathon 0:4233b7b10255 529 debug('close');
group-Make-NTU-Hackathon 0:4233b7b10255 530 this.cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 531 this.backoff.reset();
group-Make-NTU-Hackathon 0:4233b7b10255 532 this.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 533 this.emit('close', reason);
group-Make-NTU-Hackathon 0:4233b7b10255 534 if (this._reconnection && !this.skipReconnect) {
group-Make-NTU-Hackathon 0:4233b7b10255 535 this.reconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 536 }
group-Make-NTU-Hackathon 0:4233b7b10255 537 };
group-Make-NTU-Hackathon 0:4233b7b10255 538
group-Make-NTU-Hackathon 0:4233b7b10255 539 /**
group-Make-NTU-Hackathon 0:4233b7b10255 540 * Attempt a reconnection.
group-Make-NTU-Hackathon 0:4233b7b10255 541 *
group-Make-NTU-Hackathon 0:4233b7b10255 542 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 543 */
group-Make-NTU-Hackathon 0:4233b7b10255 544
group-Make-NTU-Hackathon 0:4233b7b10255 545 Manager.prototype.reconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 546 if (this.reconnecting || this.skipReconnect) return this;
group-Make-NTU-Hackathon 0:4233b7b10255 547
group-Make-NTU-Hackathon 0:4233b7b10255 548 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 549
group-Make-NTU-Hackathon 0:4233b7b10255 550 if (this.backoff.attempts >= this._reconnectionAttempts) {
group-Make-NTU-Hackathon 0:4233b7b10255 551 debug('reconnect failed');
group-Make-NTU-Hackathon 0:4233b7b10255 552 this.backoff.reset();
group-Make-NTU-Hackathon 0:4233b7b10255 553 this.emitAll('reconnect_failed');
group-Make-NTU-Hackathon 0:4233b7b10255 554 this.reconnecting = false;
group-Make-NTU-Hackathon 0:4233b7b10255 555 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 556 var delay = this.backoff.duration();
group-Make-NTU-Hackathon 0:4233b7b10255 557 debug('will wait %dms before reconnect attempt', delay);
group-Make-NTU-Hackathon 0:4233b7b10255 558
group-Make-NTU-Hackathon 0:4233b7b10255 559 this.reconnecting = true;
group-Make-NTU-Hackathon 0:4233b7b10255 560 var timer = setTimeout(function(){
group-Make-NTU-Hackathon 0:4233b7b10255 561 if (self.skipReconnect) return;
group-Make-NTU-Hackathon 0:4233b7b10255 562
group-Make-NTU-Hackathon 0:4233b7b10255 563 debug('attempting reconnect');
group-Make-NTU-Hackathon 0:4233b7b10255 564 self.emitAll('reconnect_attempt', self.backoff.attempts);
group-Make-NTU-Hackathon 0:4233b7b10255 565 self.emitAll('reconnecting', self.backoff.attempts);
group-Make-NTU-Hackathon 0:4233b7b10255 566
group-Make-NTU-Hackathon 0:4233b7b10255 567 // check again for the case socket closed in above events
group-Make-NTU-Hackathon 0:4233b7b10255 568 if (self.skipReconnect) return;
group-Make-NTU-Hackathon 0:4233b7b10255 569
group-Make-NTU-Hackathon 0:4233b7b10255 570 self.open(function(err){
group-Make-NTU-Hackathon 0:4233b7b10255 571 if (err) {
group-Make-NTU-Hackathon 0:4233b7b10255 572 debug('reconnect attempt error');
group-Make-NTU-Hackathon 0:4233b7b10255 573 self.reconnecting = false;
group-Make-NTU-Hackathon 0:4233b7b10255 574 self.reconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 575 self.emitAll('reconnect_error', err.data);
group-Make-NTU-Hackathon 0:4233b7b10255 576 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 577 debug('reconnect success');
group-Make-NTU-Hackathon 0:4233b7b10255 578 self.onreconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 579 }
group-Make-NTU-Hackathon 0:4233b7b10255 580 });
group-Make-NTU-Hackathon 0:4233b7b10255 581 }, delay);
group-Make-NTU-Hackathon 0:4233b7b10255 582
group-Make-NTU-Hackathon 0:4233b7b10255 583 this.subs.push({
group-Make-NTU-Hackathon 0:4233b7b10255 584 destroy: function(){
group-Make-NTU-Hackathon 0:4233b7b10255 585 clearTimeout(timer);
group-Make-NTU-Hackathon 0:4233b7b10255 586 }
group-Make-NTU-Hackathon 0:4233b7b10255 587 });
group-Make-NTU-Hackathon 0:4233b7b10255 588 }
group-Make-NTU-Hackathon 0:4233b7b10255 589 };
group-Make-NTU-Hackathon 0:4233b7b10255 590
group-Make-NTU-Hackathon 0:4233b7b10255 591 /**
group-Make-NTU-Hackathon 0:4233b7b10255 592 * Called upon successful reconnect.
group-Make-NTU-Hackathon 0:4233b7b10255 593 *
group-Make-NTU-Hackathon 0:4233b7b10255 594 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 595 */
group-Make-NTU-Hackathon 0:4233b7b10255 596
group-Make-NTU-Hackathon 0:4233b7b10255 597 Manager.prototype.onreconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 598 var attempt = this.backoff.attempts;
group-Make-NTU-Hackathon 0:4233b7b10255 599 this.reconnecting = false;
group-Make-NTU-Hackathon 0:4233b7b10255 600 this.backoff.reset();
group-Make-NTU-Hackathon 0:4233b7b10255 601 this.updateSocketIds();
group-Make-NTU-Hackathon 0:4233b7b10255 602 this.emitAll('reconnect', attempt);
group-Make-NTU-Hackathon 0:4233b7b10255 603 };
group-Make-NTU-Hackathon 0:4233b7b10255 604
group-Make-NTU-Hackathon 0:4233b7b10255 605 },{"./on":4,"./socket":5,"./url":6,"backo2":7,"component-bind":8,"component-emitter":9,"debug":10,"engine.io-client":11,"indexof":42,"object-component":43,"socket.io-parser":46}],4:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 606
group-Make-NTU-Hackathon 0:4233b7b10255 607 /**
group-Make-NTU-Hackathon 0:4233b7b10255 608 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 609 */
group-Make-NTU-Hackathon 0:4233b7b10255 610
group-Make-NTU-Hackathon 0:4233b7b10255 611 module.exports = on;
group-Make-NTU-Hackathon 0:4233b7b10255 612
group-Make-NTU-Hackathon 0:4233b7b10255 613 /**
group-Make-NTU-Hackathon 0:4233b7b10255 614 * Helper for subscriptions.
group-Make-NTU-Hackathon 0:4233b7b10255 615 *
group-Make-NTU-Hackathon 0:4233b7b10255 616 * @param {Object|EventEmitter} obj with `Emitter` mixin or `EventEmitter`
group-Make-NTU-Hackathon 0:4233b7b10255 617 * @param {String} event name
group-Make-NTU-Hackathon 0:4233b7b10255 618 * @param {Function} callback
group-Make-NTU-Hackathon 0:4233b7b10255 619 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 620 */
group-Make-NTU-Hackathon 0:4233b7b10255 621
group-Make-NTU-Hackathon 0:4233b7b10255 622 function on(obj, ev, fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 623 obj.on(ev, fn);
group-Make-NTU-Hackathon 0:4233b7b10255 624 return {
group-Make-NTU-Hackathon 0:4233b7b10255 625 destroy: function(){
group-Make-NTU-Hackathon 0:4233b7b10255 626 obj.removeListener(ev, fn);
group-Make-NTU-Hackathon 0:4233b7b10255 627 }
group-Make-NTU-Hackathon 0:4233b7b10255 628 };
group-Make-NTU-Hackathon 0:4233b7b10255 629 }
group-Make-NTU-Hackathon 0:4233b7b10255 630
group-Make-NTU-Hackathon 0:4233b7b10255 631 },{}],5:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 632
group-Make-NTU-Hackathon 0:4233b7b10255 633 /**
group-Make-NTU-Hackathon 0:4233b7b10255 634 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 635 */
group-Make-NTU-Hackathon 0:4233b7b10255 636
group-Make-NTU-Hackathon 0:4233b7b10255 637 var parser = _dereq_('socket.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 638 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 639 var toArray = _dereq_('to-array');
group-Make-NTU-Hackathon 0:4233b7b10255 640 var on = _dereq_('./on');
group-Make-NTU-Hackathon 0:4233b7b10255 641 var bind = _dereq_('component-bind');
group-Make-NTU-Hackathon 0:4233b7b10255 642 var debug = _dereq_('debug')('socket.io-client:socket');
group-Make-NTU-Hackathon 0:4233b7b10255 643 var hasBin = _dereq_('has-binary');
group-Make-NTU-Hackathon 0:4233b7b10255 644
group-Make-NTU-Hackathon 0:4233b7b10255 645 /**
group-Make-NTU-Hackathon 0:4233b7b10255 646 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 647 */
group-Make-NTU-Hackathon 0:4233b7b10255 648
group-Make-NTU-Hackathon 0:4233b7b10255 649 module.exports = exports = Socket;
group-Make-NTU-Hackathon 0:4233b7b10255 650
group-Make-NTU-Hackathon 0:4233b7b10255 651 /**
group-Make-NTU-Hackathon 0:4233b7b10255 652 * Internal events (blacklisted).
group-Make-NTU-Hackathon 0:4233b7b10255 653 * These events can't be emitted by the user.
group-Make-NTU-Hackathon 0:4233b7b10255 654 *
group-Make-NTU-Hackathon 0:4233b7b10255 655 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 656 */
group-Make-NTU-Hackathon 0:4233b7b10255 657
group-Make-NTU-Hackathon 0:4233b7b10255 658 var events = {
group-Make-NTU-Hackathon 0:4233b7b10255 659 connect: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 660 connect_error: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 661 connect_timeout: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 662 disconnect: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 663 error: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 664 reconnect: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 665 reconnect_attempt: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 666 reconnect_failed: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 667 reconnect_error: 1,
group-Make-NTU-Hackathon 0:4233b7b10255 668 reconnecting: 1
group-Make-NTU-Hackathon 0:4233b7b10255 669 };
group-Make-NTU-Hackathon 0:4233b7b10255 670
group-Make-NTU-Hackathon 0:4233b7b10255 671 /**
group-Make-NTU-Hackathon 0:4233b7b10255 672 * Shortcut to `Emitter#emit`.
group-Make-NTU-Hackathon 0:4233b7b10255 673 */
group-Make-NTU-Hackathon 0:4233b7b10255 674
group-Make-NTU-Hackathon 0:4233b7b10255 675 var emit = Emitter.prototype.emit;
group-Make-NTU-Hackathon 0:4233b7b10255 676
group-Make-NTU-Hackathon 0:4233b7b10255 677 /**
group-Make-NTU-Hackathon 0:4233b7b10255 678 * `Socket` constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 679 *
group-Make-NTU-Hackathon 0:4233b7b10255 680 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 681 */
group-Make-NTU-Hackathon 0:4233b7b10255 682
group-Make-NTU-Hackathon 0:4233b7b10255 683 function Socket(io, nsp){
group-Make-NTU-Hackathon 0:4233b7b10255 684 this.io = io;
group-Make-NTU-Hackathon 0:4233b7b10255 685 this.nsp = nsp;
group-Make-NTU-Hackathon 0:4233b7b10255 686 this.json = this; // compat
group-Make-NTU-Hackathon 0:4233b7b10255 687 this.ids = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 688 this.acks = {};
group-Make-NTU-Hackathon 0:4233b7b10255 689 if (this.io.autoConnect) this.open();
group-Make-NTU-Hackathon 0:4233b7b10255 690 this.receiveBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 691 this.sendBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 692 this.connected = false;
group-Make-NTU-Hackathon 0:4233b7b10255 693 this.disconnected = true;
group-Make-NTU-Hackathon 0:4233b7b10255 694 }
group-Make-NTU-Hackathon 0:4233b7b10255 695
group-Make-NTU-Hackathon 0:4233b7b10255 696 /**
group-Make-NTU-Hackathon 0:4233b7b10255 697 * Mix in `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 698 */
group-Make-NTU-Hackathon 0:4233b7b10255 699
group-Make-NTU-Hackathon 0:4233b7b10255 700 Emitter(Socket.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 701
group-Make-NTU-Hackathon 0:4233b7b10255 702 /**
group-Make-NTU-Hackathon 0:4233b7b10255 703 * Subscribe to open, close and packet events
group-Make-NTU-Hackathon 0:4233b7b10255 704 *
group-Make-NTU-Hackathon 0:4233b7b10255 705 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 706 */
group-Make-NTU-Hackathon 0:4233b7b10255 707
group-Make-NTU-Hackathon 0:4233b7b10255 708 Socket.prototype.subEvents = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 709 if (this.subs) return;
group-Make-NTU-Hackathon 0:4233b7b10255 710
group-Make-NTU-Hackathon 0:4233b7b10255 711 var io = this.io;
group-Make-NTU-Hackathon 0:4233b7b10255 712 this.subs = [
group-Make-NTU-Hackathon 0:4233b7b10255 713 on(io, 'open', bind(this, 'onopen')),
group-Make-NTU-Hackathon 0:4233b7b10255 714 on(io, 'packet', bind(this, 'onpacket')),
group-Make-NTU-Hackathon 0:4233b7b10255 715 on(io, 'close', bind(this, 'onclose'))
group-Make-NTU-Hackathon 0:4233b7b10255 716 ];
group-Make-NTU-Hackathon 0:4233b7b10255 717 };
group-Make-NTU-Hackathon 0:4233b7b10255 718
group-Make-NTU-Hackathon 0:4233b7b10255 719 /**
group-Make-NTU-Hackathon 0:4233b7b10255 720 * "Opens" the socket.
group-Make-NTU-Hackathon 0:4233b7b10255 721 *
group-Make-NTU-Hackathon 0:4233b7b10255 722 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 723 */
group-Make-NTU-Hackathon 0:4233b7b10255 724
group-Make-NTU-Hackathon 0:4233b7b10255 725 Socket.prototype.open =
group-Make-NTU-Hackathon 0:4233b7b10255 726 Socket.prototype.connect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 727 if (this.connected) return this;
group-Make-NTU-Hackathon 0:4233b7b10255 728
group-Make-NTU-Hackathon 0:4233b7b10255 729 this.subEvents();
group-Make-NTU-Hackathon 0:4233b7b10255 730 this.io.open(); // ensure open
group-Make-NTU-Hackathon 0:4233b7b10255 731 if ('open' == this.io.readyState) this.onopen();
group-Make-NTU-Hackathon 0:4233b7b10255 732 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 733 };
group-Make-NTU-Hackathon 0:4233b7b10255 734
group-Make-NTU-Hackathon 0:4233b7b10255 735 /**
group-Make-NTU-Hackathon 0:4233b7b10255 736 * Sends a `message` event.
group-Make-NTU-Hackathon 0:4233b7b10255 737 *
group-Make-NTU-Hackathon 0:4233b7b10255 738 * @return {Socket} self
group-Make-NTU-Hackathon 0:4233b7b10255 739 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 740 */
group-Make-NTU-Hackathon 0:4233b7b10255 741
group-Make-NTU-Hackathon 0:4233b7b10255 742 Socket.prototype.send = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 743 var args = toArray(arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 744 args.unshift('message');
group-Make-NTU-Hackathon 0:4233b7b10255 745 this.emit.apply(this, args);
group-Make-NTU-Hackathon 0:4233b7b10255 746 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 747 };
group-Make-NTU-Hackathon 0:4233b7b10255 748
group-Make-NTU-Hackathon 0:4233b7b10255 749 /**
group-Make-NTU-Hackathon 0:4233b7b10255 750 * Override `emit`.
group-Make-NTU-Hackathon 0:4233b7b10255 751 * If the event is in `events`, it's emitted normally.
group-Make-NTU-Hackathon 0:4233b7b10255 752 *
group-Make-NTU-Hackathon 0:4233b7b10255 753 * @param {String} event name
group-Make-NTU-Hackathon 0:4233b7b10255 754 * @return {Socket} self
group-Make-NTU-Hackathon 0:4233b7b10255 755 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 756 */
group-Make-NTU-Hackathon 0:4233b7b10255 757
group-Make-NTU-Hackathon 0:4233b7b10255 758 Socket.prototype.emit = function(ev){
group-Make-NTU-Hackathon 0:4233b7b10255 759 if (events.hasOwnProperty(ev)) {
group-Make-NTU-Hackathon 0:4233b7b10255 760 emit.apply(this, arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 761 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 762 }
group-Make-NTU-Hackathon 0:4233b7b10255 763
group-Make-NTU-Hackathon 0:4233b7b10255 764 var args = toArray(arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 765 var parserType = parser.EVENT; // default
group-Make-NTU-Hackathon 0:4233b7b10255 766 if (hasBin(args)) { parserType = parser.BINARY_EVENT; } // binary
group-Make-NTU-Hackathon 0:4233b7b10255 767 var packet = { type: parserType, data: args };
group-Make-NTU-Hackathon 0:4233b7b10255 768
group-Make-NTU-Hackathon 0:4233b7b10255 769 // event ack callback
group-Make-NTU-Hackathon 0:4233b7b10255 770 if ('function' == typeof args[args.length - 1]) {
group-Make-NTU-Hackathon 0:4233b7b10255 771 debug('emitting packet with ack id %d', this.ids);
group-Make-NTU-Hackathon 0:4233b7b10255 772 this.acks[this.ids] = args.pop();
group-Make-NTU-Hackathon 0:4233b7b10255 773 packet.id = this.ids++;
group-Make-NTU-Hackathon 0:4233b7b10255 774 }
group-Make-NTU-Hackathon 0:4233b7b10255 775
group-Make-NTU-Hackathon 0:4233b7b10255 776 if (this.connected) {
group-Make-NTU-Hackathon 0:4233b7b10255 777 this.packet(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 778 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 779 this.sendBuffer.push(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 780 }
group-Make-NTU-Hackathon 0:4233b7b10255 781
group-Make-NTU-Hackathon 0:4233b7b10255 782 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 783 };
group-Make-NTU-Hackathon 0:4233b7b10255 784
group-Make-NTU-Hackathon 0:4233b7b10255 785 /**
group-Make-NTU-Hackathon 0:4233b7b10255 786 * Sends a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 787 *
group-Make-NTU-Hackathon 0:4233b7b10255 788 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 789 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 790 */
group-Make-NTU-Hackathon 0:4233b7b10255 791
group-Make-NTU-Hackathon 0:4233b7b10255 792 Socket.prototype.packet = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 793 packet.nsp = this.nsp;
group-Make-NTU-Hackathon 0:4233b7b10255 794 this.io.packet(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 795 };
group-Make-NTU-Hackathon 0:4233b7b10255 796
group-Make-NTU-Hackathon 0:4233b7b10255 797 /**
group-Make-NTU-Hackathon 0:4233b7b10255 798 * Called upon engine `open`.
group-Make-NTU-Hackathon 0:4233b7b10255 799 *
group-Make-NTU-Hackathon 0:4233b7b10255 800 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 801 */
group-Make-NTU-Hackathon 0:4233b7b10255 802
group-Make-NTU-Hackathon 0:4233b7b10255 803 Socket.prototype.onopen = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 804 debug('transport is open - connecting');
group-Make-NTU-Hackathon 0:4233b7b10255 805
group-Make-NTU-Hackathon 0:4233b7b10255 806 // write connect packet if necessary
group-Make-NTU-Hackathon 0:4233b7b10255 807 if ('/' != this.nsp) {
group-Make-NTU-Hackathon 0:4233b7b10255 808 this.packet({ type: parser.CONNECT });
group-Make-NTU-Hackathon 0:4233b7b10255 809 }
group-Make-NTU-Hackathon 0:4233b7b10255 810 };
group-Make-NTU-Hackathon 0:4233b7b10255 811
group-Make-NTU-Hackathon 0:4233b7b10255 812 /**
group-Make-NTU-Hackathon 0:4233b7b10255 813 * Called upon engine `close`.
group-Make-NTU-Hackathon 0:4233b7b10255 814 *
group-Make-NTU-Hackathon 0:4233b7b10255 815 * @param {String} reason
group-Make-NTU-Hackathon 0:4233b7b10255 816 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 817 */
group-Make-NTU-Hackathon 0:4233b7b10255 818
group-Make-NTU-Hackathon 0:4233b7b10255 819 Socket.prototype.onclose = function(reason){
group-Make-NTU-Hackathon 0:4233b7b10255 820 debug('close (%s)', reason);
group-Make-NTU-Hackathon 0:4233b7b10255 821 this.connected = false;
group-Make-NTU-Hackathon 0:4233b7b10255 822 this.disconnected = true;
group-Make-NTU-Hackathon 0:4233b7b10255 823 delete this.id;
group-Make-NTU-Hackathon 0:4233b7b10255 824 this.emit('disconnect', reason);
group-Make-NTU-Hackathon 0:4233b7b10255 825 };
group-Make-NTU-Hackathon 0:4233b7b10255 826
group-Make-NTU-Hackathon 0:4233b7b10255 827 /**
group-Make-NTU-Hackathon 0:4233b7b10255 828 * Called with socket packet.
group-Make-NTU-Hackathon 0:4233b7b10255 829 *
group-Make-NTU-Hackathon 0:4233b7b10255 830 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 831 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 832 */
group-Make-NTU-Hackathon 0:4233b7b10255 833
group-Make-NTU-Hackathon 0:4233b7b10255 834 Socket.prototype.onpacket = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 835 if (packet.nsp != this.nsp) return;
group-Make-NTU-Hackathon 0:4233b7b10255 836
group-Make-NTU-Hackathon 0:4233b7b10255 837 switch (packet.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 838 case parser.CONNECT:
group-Make-NTU-Hackathon 0:4233b7b10255 839 this.onconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 840 break;
group-Make-NTU-Hackathon 0:4233b7b10255 841
group-Make-NTU-Hackathon 0:4233b7b10255 842 case parser.EVENT:
group-Make-NTU-Hackathon 0:4233b7b10255 843 this.onevent(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 844 break;
group-Make-NTU-Hackathon 0:4233b7b10255 845
group-Make-NTU-Hackathon 0:4233b7b10255 846 case parser.BINARY_EVENT:
group-Make-NTU-Hackathon 0:4233b7b10255 847 this.onevent(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 848 break;
group-Make-NTU-Hackathon 0:4233b7b10255 849
group-Make-NTU-Hackathon 0:4233b7b10255 850 case parser.ACK:
group-Make-NTU-Hackathon 0:4233b7b10255 851 this.onack(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 852 break;
group-Make-NTU-Hackathon 0:4233b7b10255 853
group-Make-NTU-Hackathon 0:4233b7b10255 854 case parser.BINARY_ACK:
group-Make-NTU-Hackathon 0:4233b7b10255 855 this.onack(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 856 break;
group-Make-NTU-Hackathon 0:4233b7b10255 857
group-Make-NTU-Hackathon 0:4233b7b10255 858 case parser.DISCONNECT:
group-Make-NTU-Hackathon 0:4233b7b10255 859 this.ondisconnect();
group-Make-NTU-Hackathon 0:4233b7b10255 860 break;
group-Make-NTU-Hackathon 0:4233b7b10255 861
group-Make-NTU-Hackathon 0:4233b7b10255 862 case parser.ERROR:
group-Make-NTU-Hackathon 0:4233b7b10255 863 this.emit('error', packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 864 break;
group-Make-NTU-Hackathon 0:4233b7b10255 865 }
group-Make-NTU-Hackathon 0:4233b7b10255 866 };
group-Make-NTU-Hackathon 0:4233b7b10255 867
group-Make-NTU-Hackathon 0:4233b7b10255 868 /**
group-Make-NTU-Hackathon 0:4233b7b10255 869 * Called upon a server event.
group-Make-NTU-Hackathon 0:4233b7b10255 870 *
group-Make-NTU-Hackathon 0:4233b7b10255 871 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 872 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 873 */
group-Make-NTU-Hackathon 0:4233b7b10255 874
group-Make-NTU-Hackathon 0:4233b7b10255 875 Socket.prototype.onevent = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 876 var args = packet.data || [];
group-Make-NTU-Hackathon 0:4233b7b10255 877 debug('emitting event %j', args);
group-Make-NTU-Hackathon 0:4233b7b10255 878
group-Make-NTU-Hackathon 0:4233b7b10255 879 if (null != packet.id) {
group-Make-NTU-Hackathon 0:4233b7b10255 880 debug('attaching ack callback to event');
group-Make-NTU-Hackathon 0:4233b7b10255 881 args.push(this.ack(packet.id));
group-Make-NTU-Hackathon 0:4233b7b10255 882 }
group-Make-NTU-Hackathon 0:4233b7b10255 883
group-Make-NTU-Hackathon 0:4233b7b10255 884 if (this.connected) {
group-Make-NTU-Hackathon 0:4233b7b10255 885 emit.apply(this, args);
group-Make-NTU-Hackathon 0:4233b7b10255 886 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 887 this.receiveBuffer.push(args);
group-Make-NTU-Hackathon 0:4233b7b10255 888 }
group-Make-NTU-Hackathon 0:4233b7b10255 889 };
group-Make-NTU-Hackathon 0:4233b7b10255 890
group-Make-NTU-Hackathon 0:4233b7b10255 891 /**
group-Make-NTU-Hackathon 0:4233b7b10255 892 * Produces an ack callback to emit with an event.
group-Make-NTU-Hackathon 0:4233b7b10255 893 *
group-Make-NTU-Hackathon 0:4233b7b10255 894 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 895 */
group-Make-NTU-Hackathon 0:4233b7b10255 896
group-Make-NTU-Hackathon 0:4233b7b10255 897 Socket.prototype.ack = function(id){
group-Make-NTU-Hackathon 0:4233b7b10255 898 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 899 var sent = false;
group-Make-NTU-Hackathon 0:4233b7b10255 900 return function(){
group-Make-NTU-Hackathon 0:4233b7b10255 901 // prevent double callbacks
group-Make-NTU-Hackathon 0:4233b7b10255 902 if (sent) return;
group-Make-NTU-Hackathon 0:4233b7b10255 903 sent = true;
group-Make-NTU-Hackathon 0:4233b7b10255 904 var args = toArray(arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 905 debug('sending ack %j', args);
group-Make-NTU-Hackathon 0:4233b7b10255 906
group-Make-NTU-Hackathon 0:4233b7b10255 907 var type = hasBin(args) ? parser.BINARY_ACK : parser.ACK;
group-Make-NTU-Hackathon 0:4233b7b10255 908 self.packet({
group-Make-NTU-Hackathon 0:4233b7b10255 909 type: type,
group-Make-NTU-Hackathon 0:4233b7b10255 910 id: id,
group-Make-NTU-Hackathon 0:4233b7b10255 911 data: args
group-Make-NTU-Hackathon 0:4233b7b10255 912 });
group-Make-NTU-Hackathon 0:4233b7b10255 913 };
group-Make-NTU-Hackathon 0:4233b7b10255 914 };
group-Make-NTU-Hackathon 0:4233b7b10255 915
group-Make-NTU-Hackathon 0:4233b7b10255 916 /**
group-Make-NTU-Hackathon 0:4233b7b10255 917 * Called upon a server acknowlegement.
group-Make-NTU-Hackathon 0:4233b7b10255 918 *
group-Make-NTU-Hackathon 0:4233b7b10255 919 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 920 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 921 */
group-Make-NTU-Hackathon 0:4233b7b10255 922
group-Make-NTU-Hackathon 0:4233b7b10255 923 Socket.prototype.onack = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 924 debug('calling ack %s with %j', packet.id, packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 925 var fn = this.acks[packet.id];
group-Make-NTU-Hackathon 0:4233b7b10255 926 fn.apply(this, packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 927 delete this.acks[packet.id];
group-Make-NTU-Hackathon 0:4233b7b10255 928 };
group-Make-NTU-Hackathon 0:4233b7b10255 929
group-Make-NTU-Hackathon 0:4233b7b10255 930 /**
group-Make-NTU-Hackathon 0:4233b7b10255 931 * Called upon server connect.
group-Make-NTU-Hackathon 0:4233b7b10255 932 *
group-Make-NTU-Hackathon 0:4233b7b10255 933 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 934 */
group-Make-NTU-Hackathon 0:4233b7b10255 935
group-Make-NTU-Hackathon 0:4233b7b10255 936 Socket.prototype.onconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 937 this.connected = true;
group-Make-NTU-Hackathon 0:4233b7b10255 938 this.disconnected = false;
group-Make-NTU-Hackathon 0:4233b7b10255 939 this.emit('connect');
group-Make-NTU-Hackathon 0:4233b7b10255 940 this.emitBuffered();
group-Make-NTU-Hackathon 0:4233b7b10255 941 };
group-Make-NTU-Hackathon 0:4233b7b10255 942
group-Make-NTU-Hackathon 0:4233b7b10255 943 /**
group-Make-NTU-Hackathon 0:4233b7b10255 944 * Emit buffered events (received and emitted).
group-Make-NTU-Hackathon 0:4233b7b10255 945 *
group-Make-NTU-Hackathon 0:4233b7b10255 946 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 947 */
group-Make-NTU-Hackathon 0:4233b7b10255 948
group-Make-NTU-Hackathon 0:4233b7b10255 949 Socket.prototype.emitBuffered = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 950 var i;
group-Make-NTU-Hackathon 0:4233b7b10255 951 for (i = 0; i < this.receiveBuffer.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 952 emit.apply(this, this.receiveBuffer[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 953 }
group-Make-NTU-Hackathon 0:4233b7b10255 954 this.receiveBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 955
group-Make-NTU-Hackathon 0:4233b7b10255 956 for (i = 0; i < this.sendBuffer.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 957 this.packet(this.sendBuffer[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 958 }
group-Make-NTU-Hackathon 0:4233b7b10255 959 this.sendBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 960 };
group-Make-NTU-Hackathon 0:4233b7b10255 961
group-Make-NTU-Hackathon 0:4233b7b10255 962 /**
group-Make-NTU-Hackathon 0:4233b7b10255 963 * Called upon server disconnect.
group-Make-NTU-Hackathon 0:4233b7b10255 964 *
group-Make-NTU-Hackathon 0:4233b7b10255 965 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 966 */
group-Make-NTU-Hackathon 0:4233b7b10255 967
group-Make-NTU-Hackathon 0:4233b7b10255 968 Socket.prototype.ondisconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 969 debug('server disconnect (%s)', this.nsp);
group-Make-NTU-Hackathon 0:4233b7b10255 970 this.destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 971 this.onclose('io server disconnect');
group-Make-NTU-Hackathon 0:4233b7b10255 972 };
group-Make-NTU-Hackathon 0:4233b7b10255 973
group-Make-NTU-Hackathon 0:4233b7b10255 974 /**
group-Make-NTU-Hackathon 0:4233b7b10255 975 * Called upon forced client/server side disconnections,
group-Make-NTU-Hackathon 0:4233b7b10255 976 * this method ensures the manager stops tracking us and
group-Make-NTU-Hackathon 0:4233b7b10255 977 * that reconnections don't get triggered for this.
group-Make-NTU-Hackathon 0:4233b7b10255 978 *
group-Make-NTU-Hackathon 0:4233b7b10255 979 * @api private.
group-Make-NTU-Hackathon 0:4233b7b10255 980 */
group-Make-NTU-Hackathon 0:4233b7b10255 981
group-Make-NTU-Hackathon 0:4233b7b10255 982 Socket.prototype.destroy = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 983 if (this.subs) {
group-Make-NTU-Hackathon 0:4233b7b10255 984 // clean subscriptions to avoid reconnections
group-Make-NTU-Hackathon 0:4233b7b10255 985 for (var i = 0; i < this.subs.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 986 this.subs[i].destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 987 }
group-Make-NTU-Hackathon 0:4233b7b10255 988 this.subs = null;
group-Make-NTU-Hackathon 0:4233b7b10255 989 }
group-Make-NTU-Hackathon 0:4233b7b10255 990
group-Make-NTU-Hackathon 0:4233b7b10255 991 this.io.destroy(this);
group-Make-NTU-Hackathon 0:4233b7b10255 992 };
group-Make-NTU-Hackathon 0:4233b7b10255 993
group-Make-NTU-Hackathon 0:4233b7b10255 994 /**
group-Make-NTU-Hackathon 0:4233b7b10255 995 * Disconnects the socket manually.
group-Make-NTU-Hackathon 0:4233b7b10255 996 *
group-Make-NTU-Hackathon 0:4233b7b10255 997 * @return {Socket} self
group-Make-NTU-Hackathon 0:4233b7b10255 998 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 999 */
group-Make-NTU-Hackathon 0:4233b7b10255 1000
group-Make-NTU-Hackathon 0:4233b7b10255 1001 Socket.prototype.close =
group-Make-NTU-Hackathon 0:4233b7b10255 1002 Socket.prototype.disconnect = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1003 if (this.connected) {
group-Make-NTU-Hackathon 0:4233b7b10255 1004 debug('performing disconnect (%s)', this.nsp);
group-Make-NTU-Hackathon 0:4233b7b10255 1005 this.packet({ type: parser.DISCONNECT });
group-Make-NTU-Hackathon 0:4233b7b10255 1006 }
group-Make-NTU-Hackathon 0:4233b7b10255 1007
group-Make-NTU-Hackathon 0:4233b7b10255 1008 // remove socket from pool
group-Make-NTU-Hackathon 0:4233b7b10255 1009 this.destroy();
group-Make-NTU-Hackathon 0:4233b7b10255 1010
group-Make-NTU-Hackathon 0:4233b7b10255 1011 if (this.connected) {
group-Make-NTU-Hackathon 0:4233b7b10255 1012 // fire events
group-Make-NTU-Hackathon 0:4233b7b10255 1013 this.onclose('io client disconnect');
group-Make-NTU-Hackathon 0:4233b7b10255 1014 }
group-Make-NTU-Hackathon 0:4233b7b10255 1015 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1016 };
group-Make-NTU-Hackathon 0:4233b7b10255 1017
group-Make-NTU-Hackathon 0:4233b7b10255 1018 },{"./on":4,"component-bind":8,"component-emitter":9,"debug":10,"has-binary":38,"socket.io-parser":46,"to-array":50}],6:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1019 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 1020
group-Make-NTU-Hackathon 0:4233b7b10255 1021 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1022 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 1023 */
group-Make-NTU-Hackathon 0:4233b7b10255 1024
group-Make-NTU-Hackathon 0:4233b7b10255 1025 var parseuri = _dereq_('parseuri');
group-Make-NTU-Hackathon 0:4233b7b10255 1026 var debug = _dereq_('debug')('socket.io-client:url');
group-Make-NTU-Hackathon 0:4233b7b10255 1027
group-Make-NTU-Hackathon 0:4233b7b10255 1028 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1029 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 1030 */
group-Make-NTU-Hackathon 0:4233b7b10255 1031
group-Make-NTU-Hackathon 0:4233b7b10255 1032 module.exports = url;
group-Make-NTU-Hackathon 0:4233b7b10255 1033
group-Make-NTU-Hackathon 0:4233b7b10255 1034 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1035 * URL parser.
group-Make-NTU-Hackathon 0:4233b7b10255 1036 *
group-Make-NTU-Hackathon 0:4233b7b10255 1037 * @param {String} url
group-Make-NTU-Hackathon 0:4233b7b10255 1038 * @param {Object} An object meant to mimic window.location.
group-Make-NTU-Hackathon 0:4233b7b10255 1039 * Defaults to window.location.
group-Make-NTU-Hackathon 0:4233b7b10255 1040 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1041 */
group-Make-NTU-Hackathon 0:4233b7b10255 1042
group-Make-NTU-Hackathon 0:4233b7b10255 1043 function url(uri, loc){
group-Make-NTU-Hackathon 0:4233b7b10255 1044 var obj = uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1045
group-Make-NTU-Hackathon 0:4233b7b10255 1046 // default to window.location
group-Make-NTU-Hackathon 0:4233b7b10255 1047 var loc = loc || global.location;
group-Make-NTU-Hackathon 0:4233b7b10255 1048 if (null == uri) uri = loc.protocol + '//' + loc.host;
group-Make-NTU-Hackathon 0:4233b7b10255 1049
group-Make-NTU-Hackathon 0:4233b7b10255 1050 // relative path support
group-Make-NTU-Hackathon 0:4233b7b10255 1051 if ('string' == typeof uri) {
group-Make-NTU-Hackathon 0:4233b7b10255 1052 if ('/' == uri.charAt(0)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1053 if ('/' == uri.charAt(1)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1054 uri = loc.protocol + uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1055 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 1056 uri = loc.hostname + uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1057 }
group-Make-NTU-Hackathon 0:4233b7b10255 1058 }
group-Make-NTU-Hackathon 0:4233b7b10255 1059
group-Make-NTU-Hackathon 0:4233b7b10255 1060 if (!/^(https?|wss?):\/\//.test(uri)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1061 debug('protocol-less url %s', uri);
group-Make-NTU-Hackathon 0:4233b7b10255 1062 if ('undefined' != typeof loc) {
group-Make-NTU-Hackathon 0:4233b7b10255 1063 uri = loc.protocol + '//' + uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1064 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 1065 uri = 'https://' + uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1066 }
group-Make-NTU-Hackathon 0:4233b7b10255 1067 }
group-Make-NTU-Hackathon 0:4233b7b10255 1068
group-Make-NTU-Hackathon 0:4233b7b10255 1069 // parse
group-Make-NTU-Hackathon 0:4233b7b10255 1070 debug('parse %s', uri);
group-Make-NTU-Hackathon 0:4233b7b10255 1071 obj = parseuri(uri);
group-Make-NTU-Hackathon 0:4233b7b10255 1072 }
group-Make-NTU-Hackathon 0:4233b7b10255 1073
group-Make-NTU-Hackathon 0:4233b7b10255 1074 // make sure we treat `localhost:80` and `localhost` equally
group-Make-NTU-Hackathon 0:4233b7b10255 1075 if (!obj.port) {
group-Make-NTU-Hackathon 0:4233b7b10255 1076 if (/^(http|ws)$/.test(obj.protocol)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1077 obj.port = '80';
group-Make-NTU-Hackathon 0:4233b7b10255 1078 }
group-Make-NTU-Hackathon 0:4233b7b10255 1079 else if (/^(http|ws)s$/.test(obj.protocol)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1080 obj.port = '443';
group-Make-NTU-Hackathon 0:4233b7b10255 1081 }
group-Make-NTU-Hackathon 0:4233b7b10255 1082 }
group-Make-NTU-Hackathon 0:4233b7b10255 1083
group-Make-NTU-Hackathon 0:4233b7b10255 1084 obj.path = obj.path || '/';
group-Make-NTU-Hackathon 0:4233b7b10255 1085
group-Make-NTU-Hackathon 0:4233b7b10255 1086 // define unique id
group-Make-NTU-Hackathon 0:4233b7b10255 1087 obj.id = obj.protocol + '://' + obj.host + ':' + obj.port;
group-Make-NTU-Hackathon 0:4233b7b10255 1088 // define href
group-Make-NTU-Hackathon 0:4233b7b10255 1089 obj.href = obj.protocol + '://' + obj.host + (loc && loc.port == obj.port ? '' : (':' + obj.port));
group-Make-NTU-Hackathon 0:4233b7b10255 1090
group-Make-NTU-Hackathon 0:4233b7b10255 1091 return obj;
group-Make-NTU-Hackathon 0:4233b7b10255 1092 }
group-Make-NTU-Hackathon 0:4233b7b10255 1093
group-Make-NTU-Hackathon 0:4233b7b10255 1094 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 1095 },{"debug":10,"parseuri":44}],7:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1096
group-Make-NTU-Hackathon 0:4233b7b10255 1097 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1098 * Expose `Backoff`.
group-Make-NTU-Hackathon 0:4233b7b10255 1099 */
group-Make-NTU-Hackathon 0:4233b7b10255 1100
group-Make-NTU-Hackathon 0:4233b7b10255 1101 module.exports = Backoff;
group-Make-NTU-Hackathon 0:4233b7b10255 1102
group-Make-NTU-Hackathon 0:4233b7b10255 1103 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1104 * Initialize backoff timer with `opts`.
group-Make-NTU-Hackathon 0:4233b7b10255 1105 *
group-Make-NTU-Hackathon 0:4233b7b10255 1106 * - `min` initial timeout in milliseconds [100]
group-Make-NTU-Hackathon 0:4233b7b10255 1107 * - `max` max timeout [10000]
group-Make-NTU-Hackathon 0:4233b7b10255 1108 * - `jitter` [0]
group-Make-NTU-Hackathon 0:4233b7b10255 1109 * - `factor` [2]
group-Make-NTU-Hackathon 0:4233b7b10255 1110 *
group-Make-NTU-Hackathon 0:4233b7b10255 1111 * @param {Object} opts
group-Make-NTU-Hackathon 0:4233b7b10255 1112 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1113 */
group-Make-NTU-Hackathon 0:4233b7b10255 1114
group-Make-NTU-Hackathon 0:4233b7b10255 1115 function Backoff(opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 1116 opts = opts || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1117 this.ms = opts.min || 100;
group-Make-NTU-Hackathon 0:4233b7b10255 1118 this.max = opts.max || 10000;
group-Make-NTU-Hackathon 0:4233b7b10255 1119 this.factor = opts.factor || 2;
group-Make-NTU-Hackathon 0:4233b7b10255 1120 this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
group-Make-NTU-Hackathon 0:4233b7b10255 1121 this.attempts = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 1122 }
group-Make-NTU-Hackathon 0:4233b7b10255 1123
group-Make-NTU-Hackathon 0:4233b7b10255 1124 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1125 * Return the backoff duration.
group-Make-NTU-Hackathon 0:4233b7b10255 1126 *
group-Make-NTU-Hackathon 0:4233b7b10255 1127 * @return {Number}
group-Make-NTU-Hackathon 0:4233b7b10255 1128 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1129 */
group-Make-NTU-Hackathon 0:4233b7b10255 1130
group-Make-NTU-Hackathon 0:4233b7b10255 1131 Backoff.prototype.duration = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1132 var ms = this.ms * Math.pow(this.factor, this.attempts++);
group-Make-NTU-Hackathon 0:4233b7b10255 1133 if (this.jitter) {
group-Make-NTU-Hackathon 0:4233b7b10255 1134 var rand = Math.random();
group-Make-NTU-Hackathon 0:4233b7b10255 1135 var deviation = Math.floor(rand * this.jitter * ms);
group-Make-NTU-Hackathon 0:4233b7b10255 1136 ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
group-Make-NTU-Hackathon 0:4233b7b10255 1137 }
group-Make-NTU-Hackathon 0:4233b7b10255 1138 return Math.min(ms, this.max) | 0;
group-Make-NTU-Hackathon 0:4233b7b10255 1139 };
group-Make-NTU-Hackathon 0:4233b7b10255 1140
group-Make-NTU-Hackathon 0:4233b7b10255 1141 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1142 * Reset the number of attempts.
group-Make-NTU-Hackathon 0:4233b7b10255 1143 *
group-Make-NTU-Hackathon 0:4233b7b10255 1144 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1145 */
group-Make-NTU-Hackathon 0:4233b7b10255 1146
group-Make-NTU-Hackathon 0:4233b7b10255 1147 Backoff.prototype.reset = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1148 this.attempts = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 1149 };
group-Make-NTU-Hackathon 0:4233b7b10255 1150
group-Make-NTU-Hackathon 0:4233b7b10255 1151 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1152 * Set the minimum duration
group-Make-NTU-Hackathon 0:4233b7b10255 1153 *
group-Make-NTU-Hackathon 0:4233b7b10255 1154 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1155 */
group-Make-NTU-Hackathon 0:4233b7b10255 1156
group-Make-NTU-Hackathon 0:4233b7b10255 1157 Backoff.prototype.setMin = function(min){
group-Make-NTU-Hackathon 0:4233b7b10255 1158 this.ms = min;
group-Make-NTU-Hackathon 0:4233b7b10255 1159 };
group-Make-NTU-Hackathon 0:4233b7b10255 1160
group-Make-NTU-Hackathon 0:4233b7b10255 1161 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1162 * Set the maximum duration
group-Make-NTU-Hackathon 0:4233b7b10255 1163 *
group-Make-NTU-Hackathon 0:4233b7b10255 1164 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1165 */
group-Make-NTU-Hackathon 0:4233b7b10255 1166
group-Make-NTU-Hackathon 0:4233b7b10255 1167 Backoff.prototype.setMax = function(max){
group-Make-NTU-Hackathon 0:4233b7b10255 1168 this.max = max;
group-Make-NTU-Hackathon 0:4233b7b10255 1169 };
group-Make-NTU-Hackathon 0:4233b7b10255 1170
group-Make-NTU-Hackathon 0:4233b7b10255 1171 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1172 * Set the jitter
group-Make-NTU-Hackathon 0:4233b7b10255 1173 *
group-Make-NTU-Hackathon 0:4233b7b10255 1174 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1175 */
group-Make-NTU-Hackathon 0:4233b7b10255 1176
group-Make-NTU-Hackathon 0:4233b7b10255 1177 Backoff.prototype.setJitter = function(jitter){
group-Make-NTU-Hackathon 0:4233b7b10255 1178 this.jitter = jitter;
group-Make-NTU-Hackathon 0:4233b7b10255 1179 };
group-Make-NTU-Hackathon 0:4233b7b10255 1180
group-Make-NTU-Hackathon 0:4233b7b10255 1181
group-Make-NTU-Hackathon 0:4233b7b10255 1182 },{}],8:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1183 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1184 * Slice reference.
group-Make-NTU-Hackathon 0:4233b7b10255 1185 */
group-Make-NTU-Hackathon 0:4233b7b10255 1186
group-Make-NTU-Hackathon 0:4233b7b10255 1187 var slice = [].slice;
group-Make-NTU-Hackathon 0:4233b7b10255 1188
group-Make-NTU-Hackathon 0:4233b7b10255 1189 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1190 * Bind `obj` to `fn`.
group-Make-NTU-Hackathon 0:4233b7b10255 1191 *
group-Make-NTU-Hackathon 0:4233b7b10255 1192 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 1193 * @param {Function|String} fn or string
group-Make-NTU-Hackathon 0:4233b7b10255 1194 * @return {Function}
group-Make-NTU-Hackathon 0:4233b7b10255 1195 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1196 */
group-Make-NTU-Hackathon 0:4233b7b10255 1197
group-Make-NTU-Hackathon 0:4233b7b10255 1198 module.exports = function(obj, fn){
group-Make-NTU-Hackathon 0:4233b7b10255 1199 if ('string' == typeof fn) fn = obj[fn];
group-Make-NTU-Hackathon 0:4233b7b10255 1200 if ('function' != typeof fn) throw new Error('bind() requires a function');
group-Make-NTU-Hackathon 0:4233b7b10255 1201 var args = slice.call(arguments, 2);
group-Make-NTU-Hackathon 0:4233b7b10255 1202 return function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1203 return fn.apply(obj, args.concat(slice.call(arguments)));
group-Make-NTU-Hackathon 0:4233b7b10255 1204 }
group-Make-NTU-Hackathon 0:4233b7b10255 1205 };
group-Make-NTU-Hackathon 0:4233b7b10255 1206
group-Make-NTU-Hackathon 0:4233b7b10255 1207 },{}],9:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1208
group-Make-NTU-Hackathon 0:4233b7b10255 1209 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1210 * Expose `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 1211 */
group-Make-NTU-Hackathon 0:4233b7b10255 1212
group-Make-NTU-Hackathon 0:4233b7b10255 1213 module.exports = Emitter;
group-Make-NTU-Hackathon 0:4233b7b10255 1214
group-Make-NTU-Hackathon 0:4233b7b10255 1215 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1216 * Initialize a new `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 1217 *
group-Make-NTU-Hackathon 0:4233b7b10255 1218 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1219 */
group-Make-NTU-Hackathon 0:4233b7b10255 1220
group-Make-NTU-Hackathon 0:4233b7b10255 1221 function Emitter(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 1222 if (obj) return mixin(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 1223 };
group-Make-NTU-Hackathon 0:4233b7b10255 1224
group-Make-NTU-Hackathon 0:4233b7b10255 1225 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1226 * Mixin the emitter properties.
group-Make-NTU-Hackathon 0:4233b7b10255 1227 *
group-Make-NTU-Hackathon 0:4233b7b10255 1228 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 1229 * @return {Object}
group-Make-NTU-Hackathon 0:4233b7b10255 1230 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1231 */
group-Make-NTU-Hackathon 0:4233b7b10255 1232
group-Make-NTU-Hackathon 0:4233b7b10255 1233 function mixin(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 1234 for (var key in Emitter.prototype) {
group-Make-NTU-Hackathon 0:4233b7b10255 1235 obj[key] = Emitter.prototype[key];
group-Make-NTU-Hackathon 0:4233b7b10255 1236 }
group-Make-NTU-Hackathon 0:4233b7b10255 1237 return obj;
group-Make-NTU-Hackathon 0:4233b7b10255 1238 }
group-Make-NTU-Hackathon 0:4233b7b10255 1239
group-Make-NTU-Hackathon 0:4233b7b10255 1240 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1241 * Listen on the given `event` with `fn`.
group-Make-NTU-Hackathon 0:4233b7b10255 1242 *
group-Make-NTU-Hackathon 0:4233b7b10255 1243 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1244 * @param {Function} fn
group-Make-NTU-Hackathon 0:4233b7b10255 1245 * @return {Emitter}
group-Make-NTU-Hackathon 0:4233b7b10255 1246 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1247 */
group-Make-NTU-Hackathon 0:4233b7b10255 1248
group-Make-NTU-Hackathon 0:4233b7b10255 1249 Emitter.prototype.on =
group-Make-NTU-Hackathon 0:4233b7b10255 1250 Emitter.prototype.addEventListener = function(event, fn){
group-Make-NTU-Hackathon 0:4233b7b10255 1251 this._callbacks = this._callbacks || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1252 (this._callbacks[event] = this._callbacks[event] || [])
group-Make-NTU-Hackathon 0:4233b7b10255 1253 .push(fn);
group-Make-NTU-Hackathon 0:4233b7b10255 1254 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1255 };
group-Make-NTU-Hackathon 0:4233b7b10255 1256
group-Make-NTU-Hackathon 0:4233b7b10255 1257 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1258 * Adds an `event` listener that will be invoked a single
group-Make-NTU-Hackathon 0:4233b7b10255 1259 * time then automatically removed.
group-Make-NTU-Hackathon 0:4233b7b10255 1260 *
group-Make-NTU-Hackathon 0:4233b7b10255 1261 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1262 * @param {Function} fn
group-Make-NTU-Hackathon 0:4233b7b10255 1263 * @return {Emitter}
group-Make-NTU-Hackathon 0:4233b7b10255 1264 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1265 */
group-Make-NTU-Hackathon 0:4233b7b10255 1266
group-Make-NTU-Hackathon 0:4233b7b10255 1267 Emitter.prototype.once = function(event, fn){
group-Make-NTU-Hackathon 0:4233b7b10255 1268 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 1269 this._callbacks = this._callbacks || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1270
group-Make-NTU-Hackathon 0:4233b7b10255 1271 function on() {
group-Make-NTU-Hackathon 0:4233b7b10255 1272 self.off(event, on);
group-Make-NTU-Hackathon 0:4233b7b10255 1273 fn.apply(this, arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 1274 }
group-Make-NTU-Hackathon 0:4233b7b10255 1275
group-Make-NTU-Hackathon 0:4233b7b10255 1276 on.fn = fn;
group-Make-NTU-Hackathon 0:4233b7b10255 1277 this.on(event, on);
group-Make-NTU-Hackathon 0:4233b7b10255 1278 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1279 };
group-Make-NTU-Hackathon 0:4233b7b10255 1280
group-Make-NTU-Hackathon 0:4233b7b10255 1281 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1282 * Remove the given callback for `event` or all
group-Make-NTU-Hackathon 0:4233b7b10255 1283 * registered callbacks.
group-Make-NTU-Hackathon 0:4233b7b10255 1284 *
group-Make-NTU-Hackathon 0:4233b7b10255 1285 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1286 * @param {Function} fn
group-Make-NTU-Hackathon 0:4233b7b10255 1287 * @return {Emitter}
group-Make-NTU-Hackathon 0:4233b7b10255 1288 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1289 */
group-Make-NTU-Hackathon 0:4233b7b10255 1290
group-Make-NTU-Hackathon 0:4233b7b10255 1291 Emitter.prototype.off =
group-Make-NTU-Hackathon 0:4233b7b10255 1292 Emitter.prototype.removeListener =
group-Make-NTU-Hackathon 0:4233b7b10255 1293 Emitter.prototype.removeAllListeners =
group-Make-NTU-Hackathon 0:4233b7b10255 1294 Emitter.prototype.removeEventListener = function(event, fn){
group-Make-NTU-Hackathon 0:4233b7b10255 1295 this._callbacks = this._callbacks || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1296
group-Make-NTU-Hackathon 0:4233b7b10255 1297 // all
group-Make-NTU-Hackathon 0:4233b7b10255 1298 if (0 == arguments.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 1299 this._callbacks = {};
group-Make-NTU-Hackathon 0:4233b7b10255 1300 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1301 }
group-Make-NTU-Hackathon 0:4233b7b10255 1302
group-Make-NTU-Hackathon 0:4233b7b10255 1303 // specific event
group-Make-NTU-Hackathon 0:4233b7b10255 1304 var callbacks = this._callbacks[event];
group-Make-NTU-Hackathon 0:4233b7b10255 1305 if (!callbacks) return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1306
group-Make-NTU-Hackathon 0:4233b7b10255 1307 // remove all handlers
group-Make-NTU-Hackathon 0:4233b7b10255 1308 if (1 == arguments.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 1309 delete this._callbacks[event];
group-Make-NTU-Hackathon 0:4233b7b10255 1310 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1311 }
group-Make-NTU-Hackathon 0:4233b7b10255 1312
group-Make-NTU-Hackathon 0:4233b7b10255 1313 // remove specific handler
group-Make-NTU-Hackathon 0:4233b7b10255 1314 var cb;
group-Make-NTU-Hackathon 0:4233b7b10255 1315 for (var i = 0; i < callbacks.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 1316 cb = callbacks[i];
group-Make-NTU-Hackathon 0:4233b7b10255 1317 if (cb === fn || cb.fn === fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 1318 callbacks.splice(i, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 1319 break;
group-Make-NTU-Hackathon 0:4233b7b10255 1320 }
group-Make-NTU-Hackathon 0:4233b7b10255 1321 }
group-Make-NTU-Hackathon 0:4233b7b10255 1322 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1323 };
group-Make-NTU-Hackathon 0:4233b7b10255 1324
group-Make-NTU-Hackathon 0:4233b7b10255 1325 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1326 * Emit `event` with the given args.
group-Make-NTU-Hackathon 0:4233b7b10255 1327 *
group-Make-NTU-Hackathon 0:4233b7b10255 1328 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1329 * @param {Mixed} ...
group-Make-NTU-Hackathon 0:4233b7b10255 1330 * @return {Emitter}
group-Make-NTU-Hackathon 0:4233b7b10255 1331 */
group-Make-NTU-Hackathon 0:4233b7b10255 1332
group-Make-NTU-Hackathon 0:4233b7b10255 1333 Emitter.prototype.emit = function(event){
group-Make-NTU-Hackathon 0:4233b7b10255 1334 this._callbacks = this._callbacks || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1335 var args = [].slice.call(arguments, 1)
group-Make-NTU-Hackathon 0:4233b7b10255 1336 , callbacks = this._callbacks[event];
group-Make-NTU-Hackathon 0:4233b7b10255 1337
group-Make-NTU-Hackathon 0:4233b7b10255 1338 if (callbacks) {
group-Make-NTU-Hackathon 0:4233b7b10255 1339 callbacks = callbacks.slice(0);
group-Make-NTU-Hackathon 0:4233b7b10255 1340 for (var i = 0, len = callbacks.length; i < len; ++i) {
group-Make-NTU-Hackathon 0:4233b7b10255 1341 callbacks[i].apply(this, args);
group-Make-NTU-Hackathon 0:4233b7b10255 1342 }
group-Make-NTU-Hackathon 0:4233b7b10255 1343 }
group-Make-NTU-Hackathon 0:4233b7b10255 1344
group-Make-NTU-Hackathon 0:4233b7b10255 1345 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 1346 };
group-Make-NTU-Hackathon 0:4233b7b10255 1347
group-Make-NTU-Hackathon 0:4233b7b10255 1348 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1349 * Return array of callbacks for `event`.
group-Make-NTU-Hackathon 0:4233b7b10255 1350 *
group-Make-NTU-Hackathon 0:4233b7b10255 1351 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1352 * @return {Array}
group-Make-NTU-Hackathon 0:4233b7b10255 1353 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1354 */
group-Make-NTU-Hackathon 0:4233b7b10255 1355
group-Make-NTU-Hackathon 0:4233b7b10255 1356 Emitter.prototype.listeners = function(event){
group-Make-NTU-Hackathon 0:4233b7b10255 1357 this._callbacks = this._callbacks || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1358 return this._callbacks[event] || [];
group-Make-NTU-Hackathon 0:4233b7b10255 1359 };
group-Make-NTU-Hackathon 0:4233b7b10255 1360
group-Make-NTU-Hackathon 0:4233b7b10255 1361 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1362 * Check if this emitter has `event` handlers.
group-Make-NTU-Hackathon 0:4233b7b10255 1363 *
group-Make-NTU-Hackathon 0:4233b7b10255 1364 * @param {String} event
group-Make-NTU-Hackathon 0:4233b7b10255 1365 * @return {Boolean}
group-Make-NTU-Hackathon 0:4233b7b10255 1366 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1367 */
group-Make-NTU-Hackathon 0:4233b7b10255 1368
group-Make-NTU-Hackathon 0:4233b7b10255 1369 Emitter.prototype.hasListeners = function(event){
group-Make-NTU-Hackathon 0:4233b7b10255 1370 return !! this.listeners(event).length;
group-Make-NTU-Hackathon 0:4233b7b10255 1371 };
group-Make-NTU-Hackathon 0:4233b7b10255 1372
group-Make-NTU-Hackathon 0:4233b7b10255 1373 },{}],10:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1374
group-Make-NTU-Hackathon 0:4233b7b10255 1375 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1376 * Expose `debug()` as the module.
group-Make-NTU-Hackathon 0:4233b7b10255 1377 */
group-Make-NTU-Hackathon 0:4233b7b10255 1378
group-Make-NTU-Hackathon 0:4233b7b10255 1379 module.exports = debug;
group-Make-NTU-Hackathon 0:4233b7b10255 1380
group-Make-NTU-Hackathon 0:4233b7b10255 1381 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1382 * Create a debugger with the given `name`.
group-Make-NTU-Hackathon 0:4233b7b10255 1383 *
group-Make-NTU-Hackathon 0:4233b7b10255 1384 * @param {String} name
group-Make-NTU-Hackathon 0:4233b7b10255 1385 * @return {Type}
group-Make-NTU-Hackathon 0:4233b7b10255 1386 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1387 */
group-Make-NTU-Hackathon 0:4233b7b10255 1388
group-Make-NTU-Hackathon 0:4233b7b10255 1389 function debug(name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1390 if (!debug.enabled(name)) return function(){};
group-Make-NTU-Hackathon 0:4233b7b10255 1391
group-Make-NTU-Hackathon 0:4233b7b10255 1392 return function(fmt){
group-Make-NTU-Hackathon 0:4233b7b10255 1393 fmt = coerce(fmt);
group-Make-NTU-Hackathon 0:4233b7b10255 1394
group-Make-NTU-Hackathon 0:4233b7b10255 1395 var curr = new Date;
group-Make-NTU-Hackathon 0:4233b7b10255 1396 var ms = curr - (debug[name] || curr);
group-Make-NTU-Hackathon 0:4233b7b10255 1397 debug[name] = curr;
group-Make-NTU-Hackathon 0:4233b7b10255 1398
group-Make-NTU-Hackathon 0:4233b7b10255 1399 fmt = name
group-Make-NTU-Hackathon 0:4233b7b10255 1400 + ' '
group-Make-NTU-Hackathon 0:4233b7b10255 1401 + fmt
group-Make-NTU-Hackathon 0:4233b7b10255 1402 + ' +' + debug.humanize(ms);
group-Make-NTU-Hackathon 0:4233b7b10255 1403
group-Make-NTU-Hackathon 0:4233b7b10255 1404 // This hackery is required for IE8
group-Make-NTU-Hackathon 0:4233b7b10255 1405 // where `console.log` doesn't have 'apply'
group-Make-NTU-Hackathon 0:4233b7b10255 1406 window.console
group-Make-NTU-Hackathon 0:4233b7b10255 1407 && console.log
group-Make-NTU-Hackathon 0:4233b7b10255 1408 && Function.prototype.apply.call(console.log, console, arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 1409 }
group-Make-NTU-Hackathon 0:4233b7b10255 1410 }
group-Make-NTU-Hackathon 0:4233b7b10255 1411
group-Make-NTU-Hackathon 0:4233b7b10255 1412 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1413 * The currently active debug mode names.
group-Make-NTU-Hackathon 0:4233b7b10255 1414 */
group-Make-NTU-Hackathon 0:4233b7b10255 1415
group-Make-NTU-Hackathon 0:4233b7b10255 1416 debug.names = [];
group-Make-NTU-Hackathon 0:4233b7b10255 1417 debug.skips = [];
group-Make-NTU-Hackathon 0:4233b7b10255 1418
group-Make-NTU-Hackathon 0:4233b7b10255 1419 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1420 * Enables a debug mode by name. This can include modes
group-Make-NTU-Hackathon 0:4233b7b10255 1421 * separated by a colon and wildcards.
group-Make-NTU-Hackathon 0:4233b7b10255 1422 *
group-Make-NTU-Hackathon 0:4233b7b10255 1423 * @param {String} name
group-Make-NTU-Hackathon 0:4233b7b10255 1424 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1425 */
group-Make-NTU-Hackathon 0:4233b7b10255 1426
group-Make-NTU-Hackathon 0:4233b7b10255 1427 debug.enable = function(name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1428 try {
group-Make-NTU-Hackathon 0:4233b7b10255 1429 localStorage.debug = name;
group-Make-NTU-Hackathon 0:4233b7b10255 1430 } catch(e){}
group-Make-NTU-Hackathon 0:4233b7b10255 1431
group-Make-NTU-Hackathon 0:4233b7b10255 1432 var split = (name || '').split(/[\s,]+/)
group-Make-NTU-Hackathon 0:4233b7b10255 1433 , len = split.length;
group-Make-NTU-Hackathon 0:4233b7b10255 1434
group-Make-NTU-Hackathon 0:4233b7b10255 1435 for (var i = 0; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 1436 name = split[i].replace('*', '.*?');
group-Make-NTU-Hackathon 0:4233b7b10255 1437 if (name[0] === '-') {
group-Make-NTU-Hackathon 0:4233b7b10255 1438 debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
group-Make-NTU-Hackathon 0:4233b7b10255 1439 }
group-Make-NTU-Hackathon 0:4233b7b10255 1440 else {
group-Make-NTU-Hackathon 0:4233b7b10255 1441 debug.names.push(new RegExp('^' + name + '$'));
group-Make-NTU-Hackathon 0:4233b7b10255 1442 }
group-Make-NTU-Hackathon 0:4233b7b10255 1443 }
group-Make-NTU-Hackathon 0:4233b7b10255 1444 };
group-Make-NTU-Hackathon 0:4233b7b10255 1445
group-Make-NTU-Hackathon 0:4233b7b10255 1446 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1447 * Disable debug output.
group-Make-NTU-Hackathon 0:4233b7b10255 1448 *
group-Make-NTU-Hackathon 0:4233b7b10255 1449 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1450 */
group-Make-NTU-Hackathon 0:4233b7b10255 1451
group-Make-NTU-Hackathon 0:4233b7b10255 1452 debug.disable = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1453 debug.enable('');
group-Make-NTU-Hackathon 0:4233b7b10255 1454 };
group-Make-NTU-Hackathon 0:4233b7b10255 1455
group-Make-NTU-Hackathon 0:4233b7b10255 1456 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1457 * Humanize the given `ms`.
group-Make-NTU-Hackathon 0:4233b7b10255 1458 *
group-Make-NTU-Hackathon 0:4233b7b10255 1459 * @param {Number} m
group-Make-NTU-Hackathon 0:4233b7b10255 1460 * @return {String}
group-Make-NTU-Hackathon 0:4233b7b10255 1461 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1462 */
group-Make-NTU-Hackathon 0:4233b7b10255 1463
group-Make-NTU-Hackathon 0:4233b7b10255 1464 debug.humanize = function(ms) {
group-Make-NTU-Hackathon 0:4233b7b10255 1465 var sec = 1000
group-Make-NTU-Hackathon 0:4233b7b10255 1466 , min = 60 * 1000
group-Make-NTU-Hackathon 0:4233b7b10255 1467 , hour = 60 * min;
group-Make-NTU-Hackathon 0:4233b7b10255 1468
group-Make-NTU-Hackathon 0:4233b7b10255 1469 if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
group-Make-NTU-Hackathon 0:4233b7b10255 1470 if (ms >= min) return (ms / min).toFixed(1) + 'm';
group-Make-NTU-Hackathon 0:4233b7b10255 1471 if (ms >= sec) return (ms / sec | 0) + 's';
group-Make-NTU-Hackathon 0:4233b7b10255 1472 return ms + 'ms';
group-Make-NTU-Hackathon 0:4233b7b10255 1473 };
group-Make-NTU-Hackathon 0:4233b7b10255 1474
group-Make-NTU-Hackathon 0:4233b7b10255 1475 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1476 * Returns true if the given mode name is enabled, false otherwise.
group-Make-NTU-Hackathon 0:4233b7b10255 1477 *
group-Make-NTU-Hackathon 0:4233b7b10255 1478 * @param {String} name
group-Make-NTU-Hackathon 0:4233b7b10255 1479 * @return {Boolean}
group-Make-NTU-Hackathon 0:4233b7b10255 1480 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1481 */
group-Make-NTU-Hackathon 0:4233b7b10255 1482
group-Make-NTU-Hackathon 0:4233b7b10255 1483 debug.enabled = function(name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1484 for (var i = 0, len = debug.skips.length; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 1485 if (debug.skips[i].test(name)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1486 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 1487 }
group-Make-NTU-Hackathon 0:4233b7b10255 1488 }
group-Make-NTU-Hackathon 0:4233b7b10255 1489 for (var i = 0, len = debug.names.length; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 1490 if (debug.names[i].test(name)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1491 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 1492 }
group-Make-NTU-Hackathon 0:4233b7b10255 1493 }
group-Make-NTU-Hackathon 0:4233b7b10255 1494 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 1495 };
group-Make-NTU-Hackathon 0:4233b7b10255 1496
group-Make-NTU-Hackathon 0:4233b7b10255 1497 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1498 * Coerce `val`.
group-Make-NTU-Hackathon 0:4233b7b10255 1499 */
group-Make-NTU-Hackathon 0:4233b7b10255 1500
group-Make-NTU-Hackathon 0:4233b7b10255 1501 function coerce(val) {
group-Make-NTU-Hackathon 0:4233b7b10255 1502 if (val instanceof Error) return val.stack || val.message;
group-Make-NTU-Hackathon 0:4233b7b10255 1503 return val;
group-Make-NTU-Hackathon 0:4233b7b10255 1504 }
group-Make-NTU-Hackathon 0:4233b7b10255 1505
group-Make-NTU-Hackathon 0:4233b7b10255 1506 // persist
group-Make-NTU-Hackathon 0:4233b7b10255 1507
group-Make-NTU-Hackathon 0:4233b7b10255 1508 try {
group-Make-NTU-Hackathon 0:4233b7b10255 1509 if (window.localStorage) debug.enable(localStorage.debug);
group-Make-NTU-Hackathon 0:4233b7b10255 1510 } catch(e){}
group-Make-NTU-Hackathon 0:4233b7b10255 1511
group-Make-NTU-Hackathon 0:4233b7b10255 1512 },{}],11:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1513
group-Make-NTU-Hackathon 0:4233b7b10255 1514 module.exports = _dereq_('./lib/');
group-Make-NTU-Hackathon 0:4233b7b10255 1515
group-Make-NTU-Hackathon 0:4233b7b10255 1516 },{"./lib/":12}],12:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1517
group-Make-NTU-Hackathon 0:4233b7b10255 1518 module.exports = _dereq_('./socket');
group-Make-NTU-Hackathon 0:4233b7b10255 1519
group-Make-NTU-Hackathon 0:4233b7b10255 1520 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1521 * Exports parser
group-Make-NTU-Hackathon 0:4233b7b10255 1522 *
group-Make-NTU-Hackathon 0:4233b7b10255 1523 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1524 *
group-Make-NTU-Hackathon 0:4233b7b10255 1525 */
group-Make-NTU-Hackathon 0:4233b7b10255 1526 module.exports.parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 1527
group-Make-NTU-Hackathon 0:4233b7b10255 1528 },{"./socket":13,"engine.io-parser":25}],13:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 1529 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 1530 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1531 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 1532 */
group-Make-NTU-Hackathon 0:4233b7b10255 1533
group-Make-NTU-Hackathon 0:4233b7b10255 1534 var transports = _dereq_('./transports');
group-Make-NTU-Hackathon 0:4233b7b10255 1535 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 1536 var debug = _dereq_('debug')('engine.io-client:socket');
group-Make-NTU-Hackathon 0:4233b7b10255 1537 var index = _dereq_('indexof');
group-Make-NTU-Hackathon 0:4233b7b10255 1538 var parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 1539 var parseuri = _dereq_('parseuri');
group-Make-NTU-Hackathon 0:4233b7b10255 1540 var parsejson = _dereq_('parsejson');
group-Make-NTU-Hackathon 0:4233b7b10255 1541 var parseqs = _dereq_('parseqs');
group-Make-NTU-Hackathon 0:4233b7b10255 1542
group-Make-NTU-Hackathon 0:4233b7b10255 1543 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1544 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 1545 */
group-Make-NTU-Hackathon 0:4233b7b10255 1546
group-Make-NTU-Hackathon 0:4233b7b10255 1547 module.exports = Socket;
group-Make-NTU-Hackathon 0:4233b7b10255 1548
group-Make-NTU-Hackathon 0:4233b7b10255 1549 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1550 * Noop function.
group-Make-NTU-Hackathon 0:4233b7b10255 1551 *
group-Make-NTU-Hackathon 0:4233b7b10255 1552 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1553 */
group-Make-NTU-Hackathon 0:4233b7b10255 1554
group-Make-NTU-Hackathon 0:4233b7b10255 1555 function noop(){}
group-Make-NTU-Hackathon 0:4233b7b10255 1556
group-Make-NTU-Hackathon 0:4233b7b10255 1557 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1558 * Socket constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 1559 *
group-Make-NTU-Hackathon 0:4233b7b10255 1560 * @param {String|Object} uri or options
group-Make-NTU-Hackathon 0:4233b7b10255 1561 * @param {Object} options
group-Make-NTU-Hackathon 0:4233b7b10255 1562 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1563 */
group-Make-NTU-Hackathon 0:4233b7b10255 1564
group-Make-NTU-Hackathon 0:4233b7b10255 1565 function Socket(uri, opts){
group-Make-NTU-Hackathon 0:4233b7b10255 1566 if (!(this instanceof Socket)) return new Socket(uri, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 1567
group-Make-NTU-Hackathon 0:4233b7b10255 1568 opts = opts || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1569
group-Make-NTU-Hackathon 0:4233b7b10255 1570 if (uri && 'object' == typeof uri) {
group-Make-NTU-Hackathon 0:4233b7b10255 1571 opts = uri;
group-Make-NTU-Hackathon 0:4233b7b10255 1572 uri = null;
group-Make-NTU-Hackathon 0:4233b7b10255 1573 }
group-Make-NTU-Hackathon 0:4233b7b10255 1574
group-Make-NTU-Hackathon 0:4233b7b10255 1575 if (uri) {
group-Make-NTU-Hackathon 0:4233b7b10255 1576 uri = parseuri(uri);
group-Make-NTU-Hackathon 0:4233b7b10255 1577 opts.host = uri.host;
group-Make-NTU-Hackathon 0:4233b7b10255 1578 opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
group-Make-NTU-Hackathon 0:4233b7b10255 1579 opts.port = uri.port;
group-Make-NTU-Hackathon 0:4233b7b10255 1580 if (uri.query) opts.query = uri.query;
group-Make-NTU-Hackathon 0:4233b7b10255 1581 }
group-Make-NTU-Hackathon 0:4233b7b10255 1582
group-Make-NTU-Hackathon 0:4233b7b10255 1583 this.secure = null != opts.secure ? opts.secure :
group-Make-NTU-Hackathon 0:4233b7b10255 1584 (global.location && 'https:' == location.protocol);
group-Make-NTU-Hackathon 0:4233b7b10255 1585
group-Make-NTU-Hackathon 0:4233b7b10255 1586 if (opts.host) {
group-Make-NTU-Hackathon 0:4233b7b10255 1587 var pieces = opts.host.split(':');
group-Make-NTU-Hackathon 0:4233b7b10255 1588 opts.hostname = pieces.shift();
group-Make-NTU-Hackathon 0:4233b7b10255 1589 if (pieces.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 1590 opts.port = pieces.pop();
group-Make-NTU-Hackathon 0:4233b7b10255 1591 } else if (!opts.port) {
group-Make-NTU-Hackathon 0:4233b7b10255 1592 // if no port is specified manually, use the protocol default
group-Make-NTU-Hackathon 0:4233b7b10255 1593 opts.port = this.secure ? '443' : '80';
group-Make-NTU-Hackathon 0:4233b7b10255 1594 }
group-Make-NTU-Hackathon 0:4233b7b10255 1595 }
group-Make-NTU-Hackathon 0:4233b7b10255 1596
group-Make-NTU-Hackathon 0:4233b7b10255 1597 this.agent = opts.agent || false;
group-Make-NTU-Hackathon 0:4233b7b10255 1598 this.hostname = opts.hostname ||
group-Make-NTU-Hackathon 0:4233b7b10255 1599 (global.location ? location.hostname : 'localhost');
group-Make-NTU-Hackathon 0:4233b7b10255 1600 this.port = opts.port || (global.location && location.port ?
group-Make-NTU-Hackathon 0:4233b7b10255 1601 location.port :
group-Make-NTU-Hackathon 0:4233b7b10255 1602 (this.secure ? 443 : 80));
group-Make-NTU-Hackathon 0:4233b7b10255 1603 this.query = opts.query || {};
group-Make-NTU-Hackathon 0:4233b7b10255 1604 if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
group-Make-NTU-Hackathon 0:4233b7b10255 1605 this.upgrade = false !== opts.upgrade;
group-Make-NTU-Hackathon 0:4233b7b10255 1606 this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
group-Make-NTU-Hackathon 0:4233b7b10255 1607 this.forceJSONP = !!opts.forceJSONP;
group-Make-NTU-Hackathon 0:4233b7b10255 1608 this.jsonp = false !== opts.jsonp;
group-Make-NTU-Hackathon 0:4233b7b10255 1609 this.forceBase64 = !!opts.forceBase64;
group-Make-NTU-Hackathon 0:4233b7b10255 1610 this.enablesXDR = !!opts.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 1611 this.timestampParam = opts.timestampParam || 't';
group-Make-NTU-Hackathon 0:4233b7b10255 1612 this.timestampRequests = opts.timestampRequests;
group-Make-NTU-Hackathon 0:4233b7b10255 1613 this.transports = opts.transports || ['polling', 'websocket'];
group-Make-NTU-Hackathon 0:4233b7b10255 1614 this.readyState = '';
group-Make-NTU-Hackathon 0:4233b7b10255 1615 this.writeBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 1616 this.callbackBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 1617 this.policyPort = opts.policyPort || 843;
group-Make-NTU-Hackathon 0:4233b7b10255 1618 this.rememberUpgrade = opts.rememberUpgrade || false;
group-Make-NTU-Hackathon 0:4233b7b10255 1619 this.binaryType = null;
group-Make-NTU-Hackathon 0:4233b7b10255 1620 this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
group-Make-NTU-Hackathon 0:4233b7b10255 1621
group-Make-NTU-Hackathon 0:4233b7b10255 1622 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 1623 this.pfx = opts.pfx || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1624 this.key = opts.key || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1625 this.passphrase = opts.passphrase || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1626 this.cert = opts.cert || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1627 this.ca = opts.ca || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1628 this.ciphers = opts.ciphers || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1629 this.rejectUnauthorized = opts.rejectUnauthorized || null;
group-Make-NTU-Hackathon 0:4233b7b10255 1630
group-Make-NTU-Hackathon 0:4233b7b10255 1631 this.open();
group-Make-NTU-Hackathon 0:4233b7b10255 1632 }
group-Make-NTU-Hackathon 0:4233b7b10255 1633
group-Make-NTU-Hackathon 0:4233b7b10255 1634 Socket.priorWebsocketSuccess = false;
group-Make-NTU-Hackathon 0:4233b7b10255 1635
group-Make-NTU-Hackathon 0:4233b7b10255 1636 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1637 * Mix in `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 1638 */
group-Make-NTU-Hackathon 0:4233b7b10255 1639
group-Make-NTU-Hackathon 0:4233b7b10255 1640 Emitter(Socket.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 1641
group-Make-NTU-Hackathon 0:4233b7b10255 1642 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1643 * Protocol version.
group-Make-NTU-Hackathon 0:4233b7b10255 1644 *
group-Make-NTU-Hackathon 0:4233b7b10255 1645 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1646 */
group-Make-NTU-Hackathon 0:4233b7b10255 1647
group-Make-NTU-Hackathon 0:4233b7b10255 1648 Socket.protocol = parser.protocol; // this is an int
group-Make-NTU-Hackathon 0:4233b7b10255 1649
group-Make-NTU-Hackathon 0:4233b7b10255 1650 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1651 * Expose deps for legacy compatibility
group-Make-NTU-Hackathon 0:4233b7b10255 1652 * and standalone browser access.
group-Make-NTU-Hackathon 0:4233b7b10255 1653 */
group-Make-NTU-Hackathon 0:4233b7b10255 1654
group-Make-NTU-Hackathon 0:4233b7b10255 1655 Socket.Socket = Socket;
group-Make-NTU-Hackathon 0:4233b7b10255 1656 Socket.Transport = _dereq_('./transport');
group-Make-NTU-Hackathon 0:4233b7b10255 1657 Socket.transports = _dereq_('./transports');
group-Make-NTU-Hackathon 0:4233b7b10255 1658 Socket.parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 1659
group-Make-NTU-Hackathon 0:4233b7b10255 1660 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1661 * Creates transport of the given type.
group-Make-NTU-Hackathon 0:4233b7b10255 1662 *
group-Make-NTU-Hackathon 0:4233b7b10255 1663 * @param {String} transport name
group-Make-NTU-Hackathon 0:4233b7b10255 1664 * @return {Transport}
group-Make-NTU-Hackathon 0:4233b7b10255 1665 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1666 */
group-Make-NTU-Hackathon 0:4233b7b10255 1667
group-Make-NTU-Hackathon 0:4233b7b10255 1668 Socket.prototype.createTransport = function (name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1669 debug('creating transport "%s"', name);
group-Make-NTU-Hackathon 0:4233b7b10255 1670 var query = clone(this.query);
group-Make-NTU-Hackathon 0:4233b7b10255 1671
group-Make-NTU-Hackathon 0:4233b7b10255 1672 // append engine.io protocol identifier
group-Make-NTU-Hackathon 0:4233b7b10255 1673 query.EIO = parser.protocol;
group-Make-NTU-Hackathon 0:4233b7b10255 1674
group-Make-NTU-Hackathon 0:4233b7b10255 1675 // transport name
group-Make-NTU-Hackathon 0:4233b7b10255 1676 query.transport = name;
group-Make-NTU-Hackathon 0:4233b7b10255 1677
group-Make-NTU-Hackathon 0:4233b7b10255 1678 // session id if we already have one
group-Make-NTU-Hackathon 0:4233b7b10255 1679 if (this.id) query.sid = this.id;
group-Make-NTU-Hackathon 0:4233b7b10255 1680
group-Make-NTU-Hackathon 0:4233b7b10255 1681 var transport = new transports[name]({
group-Make-NTU-Hackathon 0:4233b7b10255 1682 agent: this.agent,
group-Make-NTU-Hackathon 0:4233b7b10255 1683 hostname: this.hostname,
group-Make-NTU-Hackathon 0:4233b7b10255 1684 port: this.port,
group-Make-NTU-Hackathon 0:4233b7b10255 1685 secure: this.secure,
group-Make-NTU-Hackathon 0:4233b7b10255 1686 path: this.path,
group-Make-NTU-Hackathon 0:4233b7b10255 1687 query: query,
group-Make-NTU-Hackathon 0:4233b7b10255 1688 forceJSONP: this.forceJSONP,
group-Make-NTU-Hackathon 0:4233b7b10255 1689 jsonp: this.jsonp,
group-Make-NTU-Hackathon 0:4233b7b10255 1690 forceBase64: this.forceBase64,
group-Make-NTU-Hackathon 0:4233b7b10255 1691 enablesXDR: this.enablesXDR,
group-Make-NTU-Hackathon 0:4233b7b10255 1692 timestampRequests: this.timestampRequests,
group-Make-NTU-Hackathon 0:4233b7b10255 1693 timestampParam: this.timestampParam,
group-Make-NTU-Hackathon 0:4233b7b10255 1694 policyPort: this.policyPort,
group-Make-NTU-Hackathon 0:4233b7b10255 1695 socket: this,
group-Make-NTU-Hackathon 0:4233b7b10255 1696 pfx: this.pfx,
group-Make-NTU-Hackathon 0:4233b7b10255 1697 key: this.key,
group-Make-NTU-Hackathon 0:4233b7b10255 1698 passphrase: this.passphrase,
group-Make-NTU-Hackathon 0:4233b7b10255 1699 cert: this.cert,
group-Make-NTU-Hackathon 0:4233b7b10255 1700 ca: this.ca,
group-Make-NTU-Hackathon 0:4233b7b10255 1701 ciphers: this.ciphers,
group-Make-NTU-Hackathon 0:4233b7b10255 1702 rejectUnauthorized: this.rejectUnauthorized
group-Make-NTU-Hackathon 0:4233b7b10255 1703 });
group-Make-NTU-Hackathon 0:4233b7b10255 1704
group-Make-NTU-Hackathon 0:4233b7b10255 1705 return transport;
group-Make-NTU-Hackathon 0:4233b7b10255 1706 };
group-Make-NTU-Hackathon 0:4233b7b10255 1707
group-Make-NTU-Hackathon 0:4233b7b10255 1708 function clone (obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 1709 var o = {};
group-Make-NTU-Hackathon 0:4233b7b10255 1710 for (var i in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 1711 if (obj.hasOwnProperty(i)) {
group-Make-NTU-Hackathon 0:4233b7b10255 1712 o[i] = obj[i];
group-Make-NTU-Hackathon 0:4233b7b10255 1713 }
group-Make-NTU-Hackathon 0:4233b7b10255 1714 }
group-Make-NTU-Hackathon 0:4233b7b10255 1715 return o;
group-Make-NTU-Hackathon 0:4233b7b10255 1716 }
group-Make-NTU-Hackathon 0:4233b7b10255 1717
group-Make-NTU-Hackathon 0:4233b7b10255 1718 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1719 * Initializes transport to use and starts probe.
group-Make-NTU-Hackathon 0:4233b7b10255 1720 *
group-Make-NTU-Hackathon 0:4233b7b10255 1721 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1722 */
group-Make-NTU-Hackathon 0:4233b7b10255 1723 Socket.prototype.open = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 1724 var transport;
group-Make-NTU-Hackathon 0:4233b7b10255 1725 if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) {
group-Make-NTU-Hackathon 0:4233b7b10255 1726 transport = 'websocket';
group-Make-NTU-Hackathon 0:4233b7b10255 1727 } else if (0 == this.transports.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 1728 // Emit error on next tick so it can be listened to
group-Make-NTU-Hackathon 0:4233b7b10255 1729 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 1730 setTimeout(function() {
group-Make-NTU-Hackathon 0:4233b7b10255 1731 self.emit('error', 'No transports available');
group-Make-NTU-Hackathon 0:4233b7b10255 1732 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 1733 return;
group-Make-NTU-Hackathon 0:4233b7b10255 1734 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 1735 transport = this.transports[0];
group-Make-NTU-Hackathon 0:4233b7b10255 1736 }
group-Make-NTU-Hackathon 0:4233b7b10255 1737 this.readyState = 'opening';
group-Make-NTU-Hackathon 0:4233b7b10255 1738
group-Make-NTU-Hackathon 0:4233b7b10255 1739 // Retry with the next transport if the transport is disabled (jsonp: false)
group-Make-NTU-Hackathon 0:4233b7b10255 1740 var transport;
group-Make-NTU-Hackathon 0:4233b7b10255 1741 try {
group-Make-NTU-Hackathon 0:4233b7b10255 1742 transport = this.createTransport(transport);
group-Make-NTU-Hackathon 0:4233b7b10255 1743 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 1744 this.transports.shift();
group-Make-NTU-Hackathon 0:4233b7b10255 1745 this.open();
group-Make-NTU-Hackathon 0:4233b7b10255 1746 return;
group-Make-NTU-Hackathon 0:4233b7b10255 1747 }
group-Make-NTU-Hackathon 0:4233b7b10255 1748
group-Make-NTU-Hackathon 0:4233b7b10255 1749 transport.open();
group-Make-NTU-Hackathon 0:4233b7b10255 1750 this.setTransport(transport);
group-Make-NTU-Hackathon 0:4233b7b10255 1751 };
group-Make-NTU-Hackathon 0:4233b7b10255 1752
group-Make-NTU-Hackathon 0:4233b7b10255 1753 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1754 * Sets the current transport. Disables the existing one (if any).
group-Make-NTU-Hackathon 0:4233b7b10255 1755 *
group-Make-NTU-Hackathon 0:4233b7b10255 1756 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1757 */
group-Make-NTU-Hackathon 0:4233b7b10255 1758
group-Make-NTU-Hackathon 0:4233b7b10255 1759 Socket.prototype.setTransport = function(transport){
group-Make-NTU-Hackathon 0:4233b7b10255 1760 debug('setting transport %s', transport.name);
group-Make-NTU-Hackathon 0:4233b7b10255 1761 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 1762
group-Make-NTU-Hackathon 0:4233b7b10255 1763 if (this.transport) {
group-Make-NTU-Hackathon 0:4233b7b10255 1764 debug('clearing existing transport %s', this.transport.name);
group-Make-NTU-Hackathon 0:4233b7b10255 1765 this.transport.removeAllListeners();
group-Make-NTU-Hackathon 0:4233b7b10255 1766 }
group-Make-NTU-Hackathon 0:4233b7b10255 1767
group-Make-NTU-Hackathon 0:4233b7b10255 1768 // set up transport
group-Make-NTU-Hackathon 0:4233b7b10255 1769 this.transport = transport;
group-Make-NTU-Hackathon 0:4233b7b10255 1770
group-Make-NTU-Hackathon 0:4233b7b10255 1771 // set up transport listeners
group-Make-NTU-Hackathon 0:4233b7b10255 1772 transport
group-Make-NTU-Hackathon 0:4233b7b10255 1773 .on('drain', function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1774 self.onDrain();
group-Make-NTU-Hackathon 0:4233b7b10255 1775 })
group-Make-NTU-Hackathon 0:4233b7b10255 1776 .on('packet', function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 1777 self.onPacket(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 1778 })
group-Make-NTU-Hackathon 0:4233b7b10255 1779 .on('error', function(e){
group-Make-NTU-Hackathon 0:4233b7b10255 1780 self.onError(e);
group-Make-NTU-Hackathon 0:4233b7b10255 1781 })
group-Make-NTU-Hackathon 0:4233b7b10255 1782 .on('close', function(){
group-Make-NTU-Hackathon 0:4233b7b10255 1783 self.onClose('transport close');
group-Make-NTU-Hackathon 0:4233b7b10255 1784 });
group-Make-NTU-Hackathon 0:4233b7b10255 1785 };
group-Make-NTU-Hackathon 0:4233b7b10255 1786
group-Make-NTU-Hackathon 0:4233b7b10255 1787 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1788 * Probes a transport.
group-Make-NTU-Hackathon 0:4233b7b10255 1789 *
group-Make-NTU-Hackathon 0:4233b7b10255 1790 * @param {String} transport name
group-Make-NTU-Hackathon 0:4233b7b10255 1791 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1792 */
group-Make-NTU-Hackathon 0:4233b7b10255 1793
group-Make-NTU-Hackathon 0:4233b7b10255 1794 Socket.prototype.probe = function (name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1795 debug('probing transport "%s"', name);
group-Make-NTU-Hackathon 0:4233b7b10255 1796 var transport = this.createTransport(name, { probe: 1 })
group-Make-NTU-Hackathon 0:4233b7b10255 1797 , failed = false
group-Make-NTU-Hackathon 0:4233b7b10255 1798 , self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 1799
group-Make-NTU-Hackathon 0:4233b7b10255 1800 Socket.priorWebsocketSuccess = false;
group-Make-NTU-Hackathon 0:4233b7b10255 1801
group-Make-NTU-Hackathon 0:4233b7b10255 1802 function onTransportOpen(){
group-Make-NTU-Hackathon 0:4233b7b10255 1803 if (self.onlyBinaryUpgrades) {
group-Make-NTU-Hackathon 0:4233b7b10255 1804 var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 1805 failed = failed || upgradeLosesBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 1806 }
group-Make-NTU-Hackathon 0:4233b7b10255 1807 if (failed) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1808
group-Make-NTU-Hackathon 0:4233b7b10255 1809 debug('probe transport "%s" opened', name);
group-Make-NTU-Hackathon 0:4233b7b10255 1810 transport.send([{ type: 'ping', data: 'probe' }]);
group-Make-NTU-Hackathon 0:4233b7b10255 1811 transport.once('packet', function (msg) {
group-Make-NTU-Hackathon 0:4233b7b10255 1812 if (failed) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1813 if ('pong' == msg.type && 'probe' == msg.data) {
group-Make-NTU-Hackathon 0:4233b7b10255 1814 debug('probe transport "%s" pong', name);
group-Make-NTU-Hackathon 0:4233b7b10255 1815 self.upgrading = true;
group-Make-NTU-Hackathon 0:4233b7b10255 1816 self.emit('upgrading', transport);
group-Make-NTU-Hackathon 0:4233b7b10255 1817 if (!transport) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1818 Socket.priorWebsocketSuccess = 'websocket' == transport.name;
group-Make-NTU-Hackathon 0:4233b7b10255 1819
group-Make-NTU-Hackathon 0:4233b7b10255 1820 debug('pausing current transport "%s"', self.transport.name);
group-Make-NTU-Hackathon 0:4233b7b10255 1821 self.transport.pause(function () {
group-Make-NTU-Hackathon 0:4233b7b10255 1822 if (failed) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1823 if ('closed' == self.readyState) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1824 debug('changing transport and sending upgrade packet');
group-Make-NTU-Hackathon 0:4233b7b10255 1825
group-Make-NTU-Hackathon 0:4233b7b10255 1826 cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 1827
group-Make-NTU-Hackathon 0:4233b7b10255 1828 self.setTransport(transport);
group-Make-NTU-Hackathon 0:4233b7b10255 1829 transport.send([{ type: 'upgrade' }]);
group-Make-NTU-Hackathon 0:4233b7b10255 1830 self.emit('upgrade', transport);
group-Make-NTU-Hackathon 0:4233b7b10255 1831 transport = null;
group-Make-NTU-Hackathon 0:4233b7b10255 1832 self.upgrading = false;
group-Make-NTU-Hackathon 0:4233b7b10255 1833 self.flush();
group-Make-NTU-Hackathon 0:4233b7b10255 1834 });
group-Make-NTU-Hackathon 0:4233b7b10255 1835 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 1836 debug('probe transport "%s" failed', name);
group-Make-NTU-Hackathon 0:4233b7b10255 1837 var err = new Error('probe error');
group-Make-NTU-Hackathon 0:4233b7b10255 1838 err.transport = transport.name;
group-Make-NTU-Hackathon 0:4233b7b10255 1839 self.emit('upgradeError', err);
group-Make-NTU-Hackathon 0:4233b7b10255 1840 }
group-Make-NTU-Hackathon 0:4233b7b10255 1841 });
group-Make-NTU-Hackathon 0:4233b7b10255 1842 }
group-Make-NTU-Hackathon 0:4233b7b10255 1843
group-Make-NTU-Hackathon 0:4233b7b10255 1844 function freezeTransport() {
group-Make-NTU-Hackathon 0:4233b7b10255 1845 if (failed) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1846
group-Make-NTU-Hackathon 0:4233b7b10255 1847 // Any callback called by transport should be ignored since now
group-Make-NTU-Hackathon 0:4233b7b10255 1848 failed = true;
group-Make-NTU-Hackathon 0:4233b7b10255 1849
group-Make-NTU-Hackathon 0:4233b7b10255 1850 cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 1851
group-Make-NTU-Hackathon 0:4233b7b10255 1852 transport.close();
group-Make-NTU-Hackathon 0:4233b7b10255 1853 transport = null;
group-Make-NTU-Hackathon 0:4233b7b10255 1854 }
group-Make-NTU-Hackathon 0:4233b7b10255 1855
group-Make-NTU-Hackathon 0:4233b7b10255 1856 //Handle any error that happens while probing
group-Make-NTU-Hackathon 0:4233b7b10255 1857 function onerror(err) {
group-Make-NTU-Hackathon 0:4233b7b10255 1858 var error = new Error('probe error: ' + err);
group-Make-NTU-Hackathon 0:4233b7b10255 1859 error.transport = transport.name;
group-Make-NTU-Hackathon 0:4233b7b10255 1860
group-Make-NTU-Hackathon 0:4233b7b10255 1861 freezeTransport();
group-Make-NTU-Hackathon 0:4233b7b10255 1862
group-Make-NTU-Hackathon 0:4233b7b10255 1863 debug('probe transport "%s" failed because of error: %s', name, err);
group-Make-NTU-Hackathon 0:4233b7b10255 1864
group-Make-NTU-Hackathon 0:4233b7b10255 1865 self.emit('upgradeError', error);
group-Make-NTU-Hackathon 0:4233b7b10255 1866 }
group-Make-NTU-Hackathon 0:4233b7b10255 1867
group-Make-NTU-Hackathon 0:4233b7b10255 1868 function onTransportClose(){
group-Make-NTU-Hackathon 0:4233b7b10255 1869 onerror("transport closed");
group-Make-NTU-Hackathon 0:4233b7b10255 1870 }
group-Make-NTU-Hackathon 0:4233b7b10255 1871
group-Make-NTU-Hackathon 0:4233b7b10255 1872 //When the socket is closed while we're probing
group-Make-NTU-Hackathon 0:4233b7b10255 1873 function onclose(){
group-Make-NTU-Hackathon 0:4233b7b10255 1874 onerror("socket closed");
group-Make-NTU-Hackathon 0:4233b7b10255 1875 }
group-Make-NTU-Hackathon 0:4233b7b10255 1876
group-Make-NTU-Hackathon 0:4233b7b10255 1877 //When the socket is upgraded while we're probing
group-Make-NTU-Hackathon 0:4233b7b10255 1878 function onupgrade(to){
group-Make-NTU-Hackathon 0:4233b7b10255 1879 if (transport && to.name != transport.name) {
group-Make-NTU-Hackathon 0:4233b7b10255 1880 debug('"%s" works - aborting "%s"', to.name, transport.name);
group-Make-NTU-Hackathon 0:4233b7b10255 1881 freezeTransport();
group-Make-NTU-Hackathon 0:4233b7b10255 1882 }
group-Make-NTU-Hackathon 0:4233b7b10255 1883 }
group-Make-NTU-Hackathon 0:4233b7b10255 1884
group-Make-NTU-Hackathon 0:4233b7b10255 1885 //Remove all listeners on the transport and on self
group-Make-NTU-Hackathon 0:4233b7b10255 1886 function cleanup(){
group-Make-NTU-Hackathon 0:4233b7b10255 1887 transport.removeListener('open', onTransportOpen);
group-Make-NTU-Hackathon 0:4233b7b10255 1888 transport.removeListener('error', onerror);
group-Make-NTU-Hackathon 0:4233b7b10255 1889 transport.removeListener('close', onTransportClose);
group-Make-NTU-Hackathon 0:4233b7b10255 1890 self.removeListener('close', onclose);
group-Make-NTU-Hackathon 0:4233b7b10255 1891 self.removeListener('upgrading', onupgrade);
group-Make-NTU-Hackathon 0:4233b7b10255 1892 }
group-Make-NTU-Hackathon 0:4233b7b10255 1893
group-Make-NTU-Hackathon 0:4233b7b10255 1894 transport.once('open', onTransportOpen);
group-Make-NTU-Hackathon 0:4233b7b10255 1895 transport.once('error', onerror);
group-Make-NTU-Hackathon 0:4233b7b10255 1896 transport.once('close', onTransportClose);
group-Make-NTU-Hackathon 0:4233b7b10255 1897
group-Make-NTU-Hackathon 0:4233b7b10255 1898 this.once('close', onclose);
group-Make-NTU-Hackathon 0:4233b7b10255 1899 this.once('upgrading', onupgrade);
group-Make-NTU-Hackathon 0:4233b7b10255 1900
group-Make-NTU-Hackathon 0:4233b7b10255 1901 transport.open();
group-Make-NTU-Hackathon 0:4233b7b10255 1902
group-Make-NTU-Hackathon 0:4233b7b10255 1903 };
group-Make-NTU-Hackathon 0:4233b7b10255 1904
group-Make-NTU-Hackathon 0:4233b7b10255 1905 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1906 * Called when connection is deemed open.
group-Make-NTU-Hackathon 0:4233b7b10255 1907 *
group-Make-NTU-Hackathon 0:4233b7b10255 1908 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 1909 */
group-Make-NTU-Hackathon 0:4233b7b10255 1910
group-Make-NTU-Hackathon 0:4233b7b10255 1911 Socket.prototype.onOpen = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 1912 debug('socket open');
group-Make-NTU-Hackathon 0:4233b7b10255 1913 this.readyState = 'open';
group-Make-NTU-Hackathon 0:4233b7b10255 1914 Socket.priorWebsocketSuccess = 'websocket' == this.transport.name;
group-Make-NTU-Hackathon 0:4233b7b10255 1915 this.emit('open');
group-Make-NTU-Hackathon 0:4233b7b10255 1916 this.flush();
group-Make-NTU-Hackathon 0:4233b7b10255 1917
group-Make-NTU-Hackathon 0:4233b7b10255 1918 // we check for `readyState` in case an `open`
group-Make-NTU-Hackathon 0:4233b7b10255 1919 // listener already closed the socket
group-Make-NTU-Hackathon 0:4233b7b10255 1920 if ('open' == this.readyState && this.upgrade && this.transport.pause) {
group-Make-NTU-Hackathon 0:4233b7b10255 1921 debug('starting upgrade probes');
group-Make-NTU-Hackathon 0:4233b7b10255 1922 for (var i = 0, l = this.upgrades.length; i < l; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 1923 this.probe(this.upgrades[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 1924 }
group-Make-NTU-Hackathon 0:4233b7b10255 1925 }
group-Make-NTU-Hackathon 0:4233b7b10255 1926 };
group-Make-NTU-Hackathon 0:4233b7b10255 1927
group-Make-NTU-Hackathon 0:4233b7b10255 1928 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1929 * Handles a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 1930 *
group-Make-NTU-Hackathon 0:4233b7b10255 1931 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1932 */
group-Make-NTU-Hackathon 0:4233b7b10255 1933
group-Make-NTU-Hackathon 0:4233b7b10255 1934 Socket.prototype.onPacket = function (packet) {
group-Make-NTU-Hackathon 0:4233b7b10255 1935 if ('opening' == this.readyState || 'open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 1936 debug('socket receive: type "%s", data "%s"', packet.type, packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 1937
group-Make-NTU-Hackathon 0:4233b7b10255 1938 this.emit('packet', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 1939
group-Make-NTU-Hackathon 0:4233b7b10255 1940 // Socket is live - any packet counts
group-Make-NTU-Hackathon 0:4233b7b10255 1941 this.emit('heartbeat');
group-Make-NTU-Hackathon 0:4233b7b10255 1942
group-Make-NTU-Hackathon 0:4233b7b10255 1943 switch (packet.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 1944 case 'open':
group-Make-NTU-Hackathon 0:4233b7b10255 1945 this.onHandshake(parsejson(packet.data));
group-Make-NTU-Hackathon 0:4233b7b10255 1946 break;
group-Make-NTU-Hackathon 0:4233b7b10255 1947
group-Make-NTU-Hackathon 0:4233b7b10255 1948 case 'pong':
group-Make-NTU-Hackathon 0:4233b7b10255 1949 this.setPing();
group-Make-NTU-Hackathon 0:4233b7b10255 1950 break;
group-Make-NTU-Hackathon 0:4233b7b10255 1951
group-Make-NTU-Hackathon 0:4233b7b10255 1952 case 'error':
group-Make-NTU-Hackathon 0:4233b7b10255 1953 var err = new Error('server error');
group-Make-NTU-Hackathon 0:4233b7b10255 1954 err.code = packet.data;
group-Make-NTU-Hackathon 0:4233b7b10255 1955 this.emit('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 1956 break;
group-Make-NTU-Hackathon 0:4233b7b10255 1957
group-Make-NTU-Hackathon 0:4233b7b10255 1958 case 'message':
group-Make-NTU-Hackathon 0:4233b7b10255 1959 this.emit('data', packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 1960 this.emit('message', packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 1961 break;
group-Make-NTU-Hackathon 0:4233b7b10255 1962 }
group-Make-NTU-Hackathon 0:4233b7b10255 1963 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 1964 debug('packet received with socket readyState "%s"', this.readyState);
group-Make-NTU-Hackathon 0:4233b7b10255 1965 }
group-Make-NTU-Hackathon 0:4233b7b10255 1966 };
group-Make-NTU-Hackathon 0:4233b7b10255 1967
group-Make-NTU-Hackathon 0:4233b7b10255 1968 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1969 * Called upon handshake completion.
group-Make-NTU-Hackathon 0:4233b7b10255 1970 *
group-Make-NTU-Hackathon 0:4233b7b10255 1971 * @param {Object} handshake obj
group-Make-NTU-Hackathon 0:4233b7b10255 1972 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1973 */
group-Make-NTU-Hackathon 0:4233b7b10255 1974
group-Make-NTU-Hackathon 0:4233b7b10255 1975 Socket.prototype.onHandshake = function (data) {
group-Make-NTU-Hackathon 0:4233b7b10255 1976 this.emit('handshake', data);
group-Make-NTU-Hackathon 0:4233b7b10255 1977 this.id = data.sid;
group-Make-NTU-Hackathon 0:4233b7b10255 1978 this.transport.query.sid = data.sid;
group-Make-NTU-Hackathon 0:4233b7b10255 1979 this.upgrades = this.filterUpgrades(data.upgrades);
group-Make-NTU-Hackathon 0:4233b7b10255 1980 this.pingInterval = data.pingInterval;
group-Make-NTU-Hackathon 0:4233b7b10255 1981 this.pingTimeout = data.pingTimeout;
group-Make-NTU-Hackathon 0:4233b7b10255 1982 this.onOpen();
group-Make-NTU-Hackathon 0:4233b7b10255 1983 // In case open handler closes socket
group-Make-NTU-Hackathon 0:4233b7b10255 1984 if ('closed' == this.readyState) return;
group-Make-NTU-Hackathon 0:4233b7b10255 1985 this.setPing();
group-Make-NTU-Hackathon 0:4233b7b10255 1986
group-Make-NTU-Hackathon 0:4233b7b10255 1987 // Prolong liveness of socket on heartbeat
group-Make-NTU-Hackathon 0:4233b7b10255 1988 this.removeListener('heartbeat', this.onHeartbeat);
group-Make-NTU-Hackathon 0:4233b7b10255 1989 this.on('heartbeat', this.onHeartbeat);
group-Make-NTU-Hackathon 0:4233b7b10255 1990 };
group-Make-NTU-Hackathon 0:4233b7b10255 1991
group-Make-NTU-Hackathon 0:4233b7b10255 1992 /**
group-Make-NTU-Hackathon 0:4233b7b10255 1993 * Resets ping timeout.
group-Make-NTU-Hackathon 0:4233b7b10255 1994 *
group-Make-NTU-Hackathon 0:4233b7b10255 1995 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 1996 */
group-Make-NTU-Hackathon 0:4233b7b10255 1997
group-Make-NTU-Hackathon 0:4233b7b10255 1998 Socket.prototype.onHeartbeat = function (timeout) {
group-Make-NTU-Hackathon 0:4233b7b10255 1999 clearTimeout(this.pingTimeoutTimer);
group-Make-NTU-Hackathon 0:4233b7b10255 2000 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2001 self.pingTimeoutTimer = setTimeout(function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2002 if ('closed' == self.readyState) return;
group-Make-NTU-Hackathon 0:4233b7b10255 2003 self.onClose('ping timeout');
group-Make-NTU-Hackathon 0:4233b7b10255 2004 }, timeout || (self.pingInterval + self.pingTimeout));
group-Make-NTU-Hackathon 0:4233b7b10255 2005 };
group-Make-NTU-Hackathon 0:4233b7b10255 2006
group-Make-NTU-Hackathon 0:4233b7b10255 2007 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2008 * Pings server every `this.pingInterval` and expects response
group-Make-NTU-Hackathon 0:4233b7b10255 2009 * within `this.pingTimeout` or closes connection.
group-Make-NTU-Hackathon 0:4233b7b10255 2010 *
group-Make-NTU-Hackathon 0:4233b7b10255 2011 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2012 */
group-Make-NTU-Hackathon 0:4233b7b10255 2013
group-Make-NTU-Hackathon 0:4233b7b10255 2014 Socket.prototype.setPing = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2015 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2016 clearTimeout(self.pingIntervalTimer);
group-Make-NTU-Hackathon 0:4233b7b10255 2017 self.pingIntervalTimer = setTimeout(function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2018 debug('writing ping packet - expecting pong within %sms', self.pingTimeout);
group-Make-NTU-Hackathon 0:4233b7b10255 2019 self.ping();
group-Make-NTU-Hackathon 0:4233b7b10255 2020 self.onHeartbeat(self.pingTimeout);
group-Make-NTU-Hackathon 0:4233b7b10255 2021 }, self.pingInterval);
group-Make-NTU-Hackathon 0:4233b7b10255 2022 };
group-Make-NTU-Hackathon 0:4233b7b10255 2023
group-Make-NTU-Hackathon 0:4233b7b10255 2024 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2025 * Sends a ping packet.
group-Make-NTU-Hackathon 0:4233b7b10255 2026 *
group-Make-NTU-Hackathon 0:4233b7b10255 2027 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2028 */
group-Make-NTU-Hackathon 0:4233b7b10255 2029
group-Make-NTU-Hackathon 0:4233b7b10255 2030 Socket.prototype.ping = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2031 this.sendPacket('ping');
group-Make-NTU-Hackathon 0:4233b7b10255 2032 };
group-Make-NTU-Hackathon 0:4233b7b10255 2033
group-Make-NTU-Hackathon 0:4233b7b10255 2034 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2035 * Called on `drain` event
group-Make-NTU-Hackathon 0:4233b7b10255 2036 *
group-Make-NTU-Hackathon 0:4233b7b10255 2037 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2038 */
group-Make-NTU-Hackathon 0:4233b7b10255 2039
group-Make-NTU-Hackathon 0:4233b7b10255 2040 Socket.prototype.onDrain = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 2041 for (var i = 0; i < this.prevBufferLen; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 2042 if (this.callbackBuffer[i]) {
group-Make-NTU-Hackathon 0:4233b7b10255 2043 this.callbackBuffer[i]();
group-Make-NTU-Hackathon 0:4233b7b10255 2044 }
group-Make-NTU-Hackathon 0:4233b7b10255 2045 }
group-Make-NTU-Hackathon 0:4233b7b10255 2046
group-Make-NTU-Hackathon 0:4233b7b10255 2047 this.writeBuffer.splice(0, this.prevBufferLen);
group-Make-NTU-Hackathon 0:4233b7b10255 2048 this.callbackBuffer.splice(0, this.prevBufferLen);
group-Make-NTU-Hackathon 0:4233b7b10255 2049
group-Make-NTU-Hackathon 0:4233b7b10255 2050 // setting prevBufferLen = 0 is very important
group-Make-NTU-Hackathon 0:4233b7b10255 2051 // for example, when upgrading, upgrade packet is sent over,
group-Make-NTU-Hackathon 0:4233b7b10255 2052 // and a nonzero prevBufferLen could cause problems on `drain`
group-Make-NTU-Hackathon 0:4233b7b10255 2053 this.prevBufferLen = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 2054
group-Make-NTU-Hackathon 0:4233b7b10255 2055 if (this.writeBuffer.length == 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 2056 this.emit('drain');
group-Make-NTU-Hackathon 0:4233b7b10255 2057 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2058 this.flush();
group-Make-NTU-Hackathon 0:4233b7b10255 2059 }
group-Make-NTU-Hackathon 0:4233b7b10255 2060 };
group-Make-NTU-Hackathon 0:4233b7b10255 2061
group-Make-NTU-Hackathon 0:4233b7b10255 2062 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2063 * Flush write buffers.
group-Make-NTU-Hackathon 0:4233b7b10255 2064 *
group-Make-NTU-Hackathon 0:4233b7b10255 2065 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2066 */
group-Make-NTU-Hackathon 0:4233b7b10255 2067
group-Make-NTU-Hackathon 0:4233b7b10255 2068 Socket.prototype.flush = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2069 if ('closed' != this.readyState && this.transport.writable &&
group-Make-NTU-Hackathon 0:4233b7b10255 2070 !this.upgrading && this.writeBuffer.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 2071 debug('flushing %d packets in socket', this.writeBuffer.length);
group-Make-NTU-Hackathon 0:4233b7b10255 2072 this.transport.send(this.writeBuffer);
group-Make-NTU-Hackathon 0:4233b7b10255 2073 // keep track of current length of writeBuffer
group-Make-NTU-Hackathon 0:4233b7b10255 2074 // splice writeBuffer and callbackBuffer on `drain`
group-Make-NTU-Hackathon 0:4233b7b10255 2075 this.prevBufferLen = this.writeBuffer.length;
group-Make-NTU-Hackathon 0:4233b7b10255 2076 this.emit('flush');
group-Make-NTU-Hackathon 0:4233b7b10255 2077 }
group-Make-NTU-Hackathon 0:4233b7b10255 2078 };
group-Make-NTU-Hackathon 0:4233b7b10255 2079
group-Make-NTU-Hackathon 0:4233b7b10255 2080 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2081 * Sends a message.
group-Make-NTU-Hackathon 0:4233b7b10255 2082 *
group-Make-NTU-Hackathon 0:4233b7b10255 2083 * @param {String} message.
group-Make-NTU-Hackathon 0:4233b7b10255 2084 * @param {Function} callback function.
group-Make-NTU-Hackathon 0:4233b7b10255 2085 * @return {Socket} for chaining.
group-Make-NTU-Hackathon 0:4233b7b10255 2086 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2087 */
group-Make-NTU-Hackathon 0:4233b7b10255 2088
group-Make-NTU-Hackathon 0:4233b7b10255 2089 Socket.prototype.write =
group-Make-NTU-Hackathon 0:4233b7b10255 2090 Socket.prototype.send = function (msg, fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 2091 this.sendPacket('message', msg, fn);
group-Make-NTU-Hackathon 0:4233b7b10255 2092 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 2093 };
group-Make-NTU-Hackathon 0:4233b7b10255 2094
group-Make-NTU-Hackathon 0:4233b7b10255 2095 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2096 * Sends a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 2097 *
group-Make-NTU-Hackathon 0:4233b7b10255 2098 * @param {String} packet type.
group-Make-NTU-Hackathon 0:4233b7b10255 2099 * @param {String} data.
group-Make-NTU-Hackathon 0:4233b7b10255 2100 * @param {Function} callback function.
group-Make-NTU-Hackathon 0:4233b7b10255 2101 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2102 */
group-Make-NTU-Hackathon 0:4233b7b10255 2103
group-Make-NTU-Hackathon 0:4233b7b10255 2104 Socket.prototype.sendPacket = function (type, data, fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 2105 if ('closing' == this.readyState || 'closed' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2106 return;
group-Make-NTU-Hackathon 0:4233b7b10255 2107 }
group-Make-NTU-Hackathon 0:4233b7b10255 2108
group-Make-NTU-Hackathon 0:4233b7b10255 2109 var packet = { type: type, data: data };
group-Make-NTU-Hackathon 0:4233b7b10255 2110 this.emit('packetCreate', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 2111 this.writeBuffer.push(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 2112 this.callbackBuffer.push(fn);
group-Make-NTU-Hackathon 0:4233b7b10255 2113 this.flush();
group-Make-NTU-Hackathon 0:4233b7b10255 2114 };
group-Make-NTU-Hackathon 0:4233b7b10255 2115
group-Make-NTU-Hackathon 0:4233b7b10255 2116 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2117 * Closes the connection.
group-Make-NTU-Hackathon 0:4233b7b10255 2118 *
group-Make-NTU-Hackathon 0:4233b7b10255 2119 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2120 */
group-Make-NTU-Hackathon 0:4233b7b10255 2121
group-Make-NTU-Hackathon 0:4233b7b10255 2122 Socket.prototype.close = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2123 if ('opening' == this.readyState || 'open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2124 this.readyState = 'closing';
group-Make-NTU-Hackathon 0:4233b7b10255 2125
group-Make-NTU-Hackathon 0:4233b7b10255 2126 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2127
group-Make-NTU-Hackathon 0:4233b7b10255 2128 function close() {
group-Make-NTU-Hackathon 0:4233b7b10255 2129 self.onClose('forced close');
group-Make-NTU-Hackathon 0:4233b7b10255 2130 debug('socket closing - telling transport to close');
group-Make-NTU-Hackathon 0:4233b7b10255 2131 self.transport.close();
group-Make-NTU-Hackathon 0:4233b7b10255 2132 }
group-Make-NTU-Hackathon 0:4233b7b10255 2133
group-Make-NTU-Hackathon 0:4233b7b10255 2134 function cleanupAndClose() {
group-Make-NTU-Hackathon 0:4233b7b10255 2135 self.removeListener('upgrade', cleanupAndClose);
group-Make-NTU-Hackathon 0:4233b7b10255 2136 self.removeListener('upgradeError', cleanupAndClose);
group-Make-NTU-Hackathon 0:4233b7b10255 2137 close();
group-Make-NTU-Hackathon 0:4233b7b10255 2138 }
group-Make-NTU-Hackathon 0:4233b7b10255 2139
group-Make-NTU-Hackathon 0:4233b7b10255 2140 function waitForUpgrade() {
group-Make-NTU-Hackathon 0:4233b7b10255 2141 // wait for upgrade to finish since we can't send packets while pausing a transport
group-Make-NTU-Hackathon 0:4233b7b10255 2142 self.once('upgrade', cleanupAndClose);
group-Make-NTU-Hackathon 0:4233b7b10255 2143 self.once('upgradeError', cleanupAndClose);
group-Make-NTU-Hackathon 0:4233b7b10255 2144 }
group-Make-NTU-Hackathon 0:4233b7b10255 2145
group-Make-NTU-Hackathon 0:4233b7b10255 2146 if (this.writeBuffer.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 2147 this.once('drain', function() {
group-Make-NTU-Hackathon 0:4233b7b10255 2148 if (this.upgrading) {
group-Make-NTU-Hackathon 0:4233b7b10255 2149 waitForUpgrade();
group-Make-NTU-Hackathon 0:4233b7b10255 2150 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2151 close();
group-Make-NTU-Hackathon 0:4233b7b10255 2152 }
group-Make-NTU-Hackathon 0:4233b7b10255 2153 });
group-Make-NTU-Hackathon 0:4233b7b10255 2154 } else if (this.upgrading) {
group-Make-NTU-Hackathon 0:4233b7b10255 2155 waitForUpgrade();
group-Make-NTU-Hackathon 0:4233b7b10255 2156 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2157 close();
group-Make-NTU-Hackathon 0:4233b7b10255 2158 }
group-Make-NTU-Hackathon 0:4233b7b10255 2159 }
group-Make-NTU-Hackathon 0:4233b7b10255 2160
group-Make-NTU-Hackathon 0:4233b7b10255 2161 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 2162 };
group-Make-NTU-Hackathon 0:4233b7b10255 2163
group-Make-NTU-Hackathon 0:4233b7b10255 2164 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2165 * Called upon transport error
group-Make-NTU-Hackathon 0:4233b7b10255 2166 *
group-Make-NTU-Hackathon 0:4233b7b10255 2167 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2168 */
group-Make-NTU-Hackathon 0:4233b7b10255 2169
group-Make-NTU-Hackathon 0:4233b7b10255 2170 Socket.prototype.onError = function (err) {
group-Make-NTU-Hackathon 0:4233b7b10255 2171 debug('socket error %j', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2172 Socket.priorWebsocketSuccess = false;
group-Make-NTU-Hackathon 0:4233b7b10255 2173 this.emit('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2174 this.onClose('transport error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2175 };
group-Make-NTU-Hackathon 0:4233b7b10255 2176
group-Make-NTU-Hackathon 0:4233b7b10255 2177 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2178 * Called upon transport close.
group-Make-NTU-Hackathon 0:4233b7b10255 2179 *
group-Make-NTU-Hackathon 0:4233b7b10255 2180 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2181 */
group-Make-NTU-Hackathon 0:4233b7b10255 2182
group-Make-NTU-Hackathon 0:4233b7b10255 2183 Socket.prototype.onClose = function (reason, desc) {
group-Make-NTU-Hackathon 0:4233b7b10255 2184 if ('opening' == this.readyState || 'open' == this.readyState || 'closing' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2185 debug('socket close with reason: "%s"', reason);
group-Make-NTU-Hackathon 0:4233b7b10255 2186 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2187
group-Make-NTU-Hackathon 0:4233b7b10255 2188 // clear timers
group-Make-NTU-Hackathon 0:4233b7b10255 2189 clearTimeout(this.pingIntervalTimer);
group-Make-NTU-Hackathon 0:4233b7b10255 2190 clearTimeout(this.pingTimeoutTimer);
group-Make-NTU-Hackathon 0:4233b7b10255 2191
group-Make-NTU-Hackathon 0:4233b7b10255 2192 // clean buffers in next tick, so developers can still
group-Make-NTU-Hackathon 0:4233b7b10255 2193 // grab the buffers on `close` event
group-Make-NTU-Hackathon 0:4233b7b10255 2194 setTimeout(function() {
group-Make-NTU-Hackathon 0:4233b7b10255 2195 self.writeBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 2196 self.callbackBuffer = [];
group-Make-NTU-Hackathon 0:4233b7b10255 2197 self.prevBufferLen = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 2198 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 2199
group-Make-NTU-Hackathon 0:4233b7b10255 2200 // stop event from firing again for transport
group-Make-NTU-Hackathon 0:4233b7b10255 2201 this.transport.removeAllListeners('close');
group-Make-NTU-Hackathon 0:4233b7b10255 2202
group-Make-NTU-Hackathon 0:4233b7b10255 2203 // ensure transport won't stay open
group-Make-NTU-Hackathon 0:4233b7b10255 2204 this.transport.close();
group-Make-NTU-Hackathon 0:4233b7b10255 2205
group-Make-NTU-Hackathon 0:4233b7b10255 2206 // ignore further transport communication
group-Make-NTU-Hackathon 0:4233b7b10255 2207 this.transport.removeAllListeners();
group-Make-NTU-Hackathon 0:4233b7b10255 2208
group-Make-NTU-Hackathon 0:4233b7b10255 2209 // set ready state
group-Make-NTU-Hackathon 0:4233b7b10255 2210 this.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 2211
group-Make-NTU-Hackathon 0:4233b7b10255 2212 // clear session id
group-Make-NTU-Hackathon 0:4233b7b10255 2213 this.id = null;
group-Make-NTU-Hackathon 0:4233b7b10255 2214
group-Make-NTU-Hackathon 0:4233b7b10255 2215 // emit close event
group-Make-NTU-Hackathon 0:4233b7b10255 2216 this.emit('close', reason, desc);
group-Make-NTU-Hackathon 0:4233b7b10255 2217 }
group-Make-NTU-Hackathon 0:4233b7b10255 2218 };
group-Make-NTU-Hackathon 0:4233b7b10255 2219
group-Make-NTU-Hackathon 0:4233b7b10255 2220 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2221 * Filters upgrades, returning only those matching client transports.
group-Make-NTU-Hackathon 0:4233b7b10255 2222 *
group-Make-NTU-Hackathon 0:4233b7b10255 2223 * @param {Array} server upgrades
group-Make-NTU-Hackathon 0:4233b7b10255 2224 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2225 *
group-Make-NTU-Hackathon 0:4233b7b10255 2226 */
group-Make-NTU-Hackathon 0:4233b7b10255 2227
group-Make-NTU-Hackathon 0:4233b7b10255 2228 Socket.prototype.filterUpgrades = function (upgrades) {
group-Make-NTU-Hackathon 0:4233b7b10255 2229 var filteredUpgrades = [];
group-Make-NTU-Hackathon 0:4233b7b10255 2230 for (var i = 0, j = upgrades.length; i<j; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 2231 if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 2232 }
group-Make-NTU-Hackathon 0:4233b7b10255 2233 return filteredUpgrades;
group-Make-NTU-Hackathon 0:4233b7b10255 2234 };
group-Make-NTU-Hackathon 0:4233b7b10255 2235
group-Make-NTU-Hackathon 0:4233b7b10255 2236 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 2237 },{"./transport":14,"./transports":15,"component-emitter":9,"debug":22,"engine.io-parser":25,"indexof":42,"parsejson":34,"parseqs":35,"parseuri":36}],14:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 2238 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2239 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 2240 */
group-Make-NTU-Hackathon 0:4233b7b10255 2241
group-Make-NTU-Hackathon 0:4233b7b10255 2242 var parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 2243 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 2244
group-Make-NTU-Hackathon 0:4233b7b10255 2245 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2246 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 2247 */
group-Make-NTU-Hackathon 0:4233b7b10255 2248
group-Make-NTU-Hackathon 0:4233b7b10255 2249 module.exports = Transport;
group-Make-NTU-Hackathon 0:4233b7b10255 2250
group-Make-NTU-Hackathon 0:4233b7b10255 2251 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2252 * Transport abstract constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 2253 *
group-Make-NTU-Hackathon 0:4233b7b10255 2254 * @param {Object} options.
group-Make-NTU-Hackathon 0:4233b7b10255 2255 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2256 */
group-Make-NTU-Hackathon 0:4233b7b10255 2257
group-Make-NTU-Hackathon 0:4233b7b10255 2258 function Transport (opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 2259 this.path = opts.path;
group-Make-NTU-Hackathon 0:4233b7b10255 2260 this.hostname = opts.hostname;
group-Make-NTU-Hackathon 0:4233b7b10255 2261 this.port = opts.port;
group-Make-NTU-Hackathon 0:4233b7b10255 2262 this.secure = opts.secure;
group-Make-NTU-Hackathon 0:4233b7b10255 2263 this.query = opts.query;
group-Make-NTU-Hackathon 0:4233b7b10255 2264 this.timestampParam = opts.timestampParam;
group-Make-NTU-Hackathon 0:4233b7b10255 2265 this.timestampRequests = opts.timestampRequests;
group-Make-NTU-Hackathon 0:4233b7b10255 2266 this.readyState = '';
group-Make-NTU-Hackathon 0:4233b7b10255 2267 this.agent = opts.agent || false;
group-Make-NTU-Hackathon 0:4233b7b10255 2268 this.socket = opts.socket;
group-Make-NTU-Hackathon 0:4233b7b10255 2269 this.enablesXDR = opts.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 2270
group-Make-NTU-Hackathon 0:4233b7b10255 2271 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 2272 this.pfx = opts.pfx;
group-Make-NTU-Hackathon 0:4233b7b10255 2273 this.key = opts.key;
group-Make-NTU-Hackathon 0:4233b7b10255 2274 this.passphrase = opts.passphrase;
group-Make-NTU-Hackathon 0:4233b7b10255 2275 this.cert = opts.cert;
group-Make-NTU-Hackathon 0:4233b7b10255 2276 this.ca = opts.ca;
group-Make-NTU-Hackathon 0:4233b7b10255 2277 this.ciphers = opts.ciphers;
group-Make-NTU-Hackathon 0:4233b7b10255 2278 this.rejectUnauthorized = opts.rejectUnauthorized;
group-Make-NTU-Hackathon 0:4233b7b10255 2279 }
group-Make-NTU-Hackathon 0:4233b7b10255 2280
group-Make-NTU-Hackathon 0:4233b7b10255 2281 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2282 * Mix in `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 2283 */
group-Make-NTU-Hackathon 0:4233b7b10255 2284
group-Make-NTU-Hackathon 0:4233b7b10255 2285 Emitter(Transport.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 2286
group-Make-NTU-Hackathon 0:4233b7b10255 2287 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2288 * A counter used to prevent collisions in the timestamps used
group-Make-NTU-Hackathon 0:4233b7b10255 2289 * for cache busting.
group-Make-NTU-Hackathon 0:4233b7b10255 2290 */
group-Make-NTU-Hackathon 0:4233b7b10255 2291
group-Make-NTU-Hackathon 0:4233b7b10255 2292 Transport.timestamps = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 2293
group-Make-NTU-Hackathon 0:4233b7b10255 2294 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2295 * Emits an error.
group-Make-NTU-Hackathon 0:4233b7b10255 2296 *
group-Make-NTU-Hackathon 0:4233b7b10255 2297 * @param {String} str
group-Make-NTU-Hackathon 0:4233b7b10255 2298 * @return {Transport} for chaining
group-Make-NTU-Hackathon 0:4233b7b10255 2299 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2300 */
group-Make-NTU-Hackathon 0:4233b7b10255 2301
group-Make-NTU-Hackathon 0:4233b7b10255 2302 Transport.prototype.onError = function (msg, desc) {
group-Make-NTU-Hackathon 0:4233b7b10255 2303 var err = new Error(msg);
group-Make-NTU-Hackathon 0:4233b7b10255 2304 err.type = 'TransportError';
group-Make-NTU-Hackathon 0:4233b7b10255 2305 err.description = desc;
group-Make-NTU-Hackathon 0:4233b7b10255 2306 this.emit('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2307 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 2308 };
group-Make-NTU-Hackathon 0:4233b7b10255 2309
group-Make-NTU-Hackathon 0:4233b7b10255 2310 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2311 * Opens the transport.
group-Make-NTU-Hackathon 0:4233b7b10255 2312 *
group-Make-NTU-Hackathon 0:4233b7b10255 2313 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2314 */
group-Make-NTU-Hackathon 0:4233b7b10255 2315
group-Make-NTU-Hackathon 0:4233b7b10255 2316 Transport.prototype.open = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2317 if ('closed' == this.readyState || '' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2318 this.readyState = 'opening';
group-Make-NTU-Hackathon 0:4233b7b10255 2319 this.doOpen();
group-Make-NTU-Hackathon 0:4233b7b10255 2320 }
group-Make-NTU-Hackathon 0:4233b7b10255 2321
group-Make-NTU-Hackathon 0:4233b7b10255 2322 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 2323 };
group-Make-NTU-Hackathon 0:4233b7b10255 2324
group-Make-NTU-Hackathon 0:4233b7b10255 2325 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2326 * Closes the transport.
group-Make-NTU-Hackathon 0:4233b7b10255 2327 *
group-Make-NTU-Hackathon 0:4233b7b10255 2328 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2329 */
group-Make-NTU-Hackathon 0:4233b7b10255 2330
group-Make-NTU-Hackathon 0:4233b7b10255 2331 Transport.prototype.close = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2332 if ('opening' == this.readyState || 'open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2333 this.doClose();
group-Make-NTU-Hackathon 0:4233b7b10255 2334 this.onClose();
group-Make-NTU-Hackathon 0:4233b7b10255 2335 }
group-Make-NTU-Hackathon 0:4233b7b10255 2336
group-Make-NTU-Hackathon 0:4233b7b10255 2337 return this;
group-Make-NTU-Hackathon 0:4233b7b10255 2338 };
group-Make-NTU-Hackathon 0:4233b7b10255 2339
group-Make-NTU-Hackathon 0:4233b7b10255 2340 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2341 * Sends multiple packets.
group-Make-NTU-Hackathon 0:4233b7b10255 2342 *
group-Make-NTU-Hackathon 0:4233b7b10255 2343 * @param {Array} packets
group-Make-NTU-Hackathon 0:4233b7b10255 2344 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2345 */
group-Make-NTU-Hackathon 0:4233b7b10255 2346
group-Make-NTU-Hackathon 0:4233b7b10255 2347 Transport.prototype.send = function(packets){
group-Make-NTU-Hackathon 0:4233b7b10255 2348 if ('open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 2349 this.write(packets);
group-Make-NTU-Hackathon 0:4233b7b10255 2350 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2351 throw new Error('Transport not open');
group-Make-NTU-Hackathon 0:4233b7b10255 2352 }
group-Make-NTU-Hackathon 0:4233b7b10255 2353 };
group-Make-NTU-Hackathon 0:4233b7b10255 2354
group-Make-NTU-Hackathon 0:4233b7b10255 2355 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2356 * Called upon open
group-Make-NTU-Hackathon 0:4233b7b10255 2357 *
group-Make-NTU-Hackathon 0:4233b7b10255 2358 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2359 */
group-Make-NTU-Hackathon 0:4233b7b10255 2360
group-Make-NTU-Hackathon 0:4233b7b10255 2361 Transport.prototype.onOpen = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2362 this.readyState = 'open';
group-Make-NTU-Hackathon 0:4233b7b10255 2363 this.writable = true;
group-Make-NTU-Hackathon 0:4233b7b10255 2364 this.emit('open');
group-Make-NTU-Hackathon 0:4233b7b10255 2365 };
group-Make-NTU-Hackathon 0:4233b7b10255 2366
group-Make-NTU-Hackathon 0:4233b7b10255 2367 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2368 * Called with data.
group-Make-NTU-Hackathon 0:4233b7b10255 2369 *
group-Make-NTU-Hackathon 0:4233b7b10255 2370 * @param {String} data
group-Make-NTU-Hackathon 0:4233b7b10255 2371 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2372 */
group-Make-NTU-Hackathon 0:4233b7b10255 2373
group-Make-NTU-Hackathon 0:4233b7b10255 2374 Transport.prototype.onData = function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 2375 var packet = parser.decodePacket(data, this.socket.binaryType);
group-Make-NTU-Hackathon 0:4233b7b10255 2376 this.onPacket(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 2377 };
group-Make-NTU-Hackathon 0:4233b7b10255 2378
group-Make-NTU-Hackathon 0:4233b7b10255 2379 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2380 * Called with a decoded packet.
group-Make-NTU-Hackathon 0:4233b7b10255 2381 */
group-Make-NTU-Hackathon 0:4233b7b10255 2382
group-Make-NTU-Hackathon 0:4233b7b10255 2383 Transport.prototype.onPacket = function (packet) {
group-Make-NTU-Hackathon 0:4233b7b10255 2384 this.emit('packet', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 2385 };
group-Make-NTU-Hackathon 0:4233b7b10255 2386
group-Make-NTU-Hackathon 0:4233b7b10255 2387 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2388 * Called upon close.
group-Make-NTU-Hackathon 0:4233b7b10255 2389 *
group-Make-NTU-Hackathon 0:4233b7b10255 2390 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2391 */
group-Make-NTU-Hackathon 0:4233b7b10255 2392
group-Make-NTU-Hackathon 0:4233b7b10255 2393 Transport.prototype.onClose = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2394 this.readyState = 'closed';
group-Make-NTU-Hackathon 0:4233b7b10255 2395 this.emit('close');
group-Make-NTU-Hackathon 0:4233b7b10255 2396 };
group-Make-NTU-Hackathon 0:4233b7b10255 2397
group-Make-NTU-Hackathon 0:4233b7b10255 2398 },{"component-emitter":9,"engine.io-parser":25}],15:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 2399 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 2400 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2401 * Module dependencies
group-Make-NTU-Hackathon 0:4233b7b10255 2402 */
group-Make-NTU-Hackathon 0:4233b7b10255 2403
group-Make-NTU-Hackathon 0:4233b7b10255 2404 var XMLHttpRequest = _dereq_('xmlhttprequest');
group-Make-NTU-Hackathon 0:4233b7b10255 2405 var XHR = _dereq_('./polling-xhr');
group-Make-NTU-Hackathon 0:4233b7b10255 2406 var JSONP = _dereq_('./polling-jsonp');
group-Make-NTU-Hackathon 0:4233b7b10255 2407 var websocket = _dereq_('./websocket');
group-Make-NTU-Hackathon 0:4233b7b10255 2408
group-Make-NTU-Hackathon 0:4233b7b10255 2409 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2410 * Export transports.
group-Make-NTU-Hackathon 0:4233b7b10255 2411 */
group-Make-NTU-Hackathon 0:4233b7b10255 2412
group-Make-NTU-Hackathon 0:4233b7b10255 2413 exports.polling = polling;
group-Make-NTU-Hackathon 0:4233b7b10255 2414 exports.websocket = websocket;
group-Make-NTU-Hackathon 0:4233b7b10255 2415
group-Make-NTU-Hackathon 0:4233b7b10255 2416 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2417 * Polling transport polymorphic constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 2418 * Decides on xhr vs jsonp based on feature detection.
group-Make-NTU-Hackathon 0:4233b7b10255 2419 *
group-Make-NTU-Hackathon 0:4233b7b10255 2420 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2421 */
group-Make-NTU-Hackathon 0:4233b7b10255 2422
group-Make-NTU-Hackathon 0:4233b7b10255 2423 function polling(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 2424 var xhr;
group-Make-NTU-Hackathon 0:4233b7b10255 2425 var xd = false;
group-Make-NTU-Hackathon 0:4233b7b10255 2426 var xs = false;
group-Make-NTU-Hackathon 0:4233b7b10255 2427 var jsonp = false !== opts.jsonp;
group-Make-NTU-Hackathon 0:4233b7b10255 2428
group-Make-NTU-Hackathon 0:4233b7b10255 2429 if (global.location) {
group-Make-NTU-Hackathon 0:4233b7b10255 2430 var isSSL = 'https:' == location.protocol;
group-Make-NTU-Hackathon 0:4233b7b10255 2431 var port = location.port;
group-Make-NTU-Hackathon 0:4233b7b10255 2432
group-Make-NTU-Hackathon 0:4233b7b10255 2433 // some user agents have empty `location.port`
group-Make-NTU-Hackathon 0:4233b7b10255 2434 if (!port) {
group-Make-NTU-Hackathon 0:4233b7b10255 2435 port = isSSL ? 443 : 80;
group-Make-NTU-Hackathon 0:4233b7b10255 2436 }
group-Make-NTU-Hackathon 0:4233b7b10255 2437
group-Make-NTU-Hackathon 0:4233b7b10255 2438 xd = opts.hostname != location.hostname || port != opts.port;
group-Make-NTU-Hackathon 0:4233b7b10255 2439 xs = opts.secure != isSSL;
group-Make-NTU-Hackathon 0:4233b7b10255 2440 }
group-Make-NTU-Hackathon 0:4233b7b10255 2441
group-Make-NTU-Hackathon 0:4233b7b10255 2442 opts.xdomain = xd;
group-Make-NTU-Hackathon 0:4233b7b10255 2443 opts.xscheme = xs;
group-Make-NTU-Hackathon 0:4233b7b10255 2444 xhr = new XMLHttpRequest(opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2445
group-Make-NTU-Hackathon 0:4233b7b10255 2446 if ('open' in xhr && !opts.forceJSONP) {
group-Make-NTU-Hackathon 0:4233b7b10255 2447 return new XHR(opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2448 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2449 if (!jsonp) throw new Error('JSONP disabled');
group-Make-NTU-Hackathon 0:4233b7b10255 2450 return new JSONP(opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2451 }
group-Make-NTU-Hackathon 0:4233b7b10255 2452 }
group-Make-NTU-Hackathon 0:4233b7b10255 2453
group-Make-NTU-Hackathon 0:4233b7b10255 2454 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 2455 },{"./polling-jsonp":16,"./polling-xhr":17,"./websocket":19,"xmlhttprequest":20}],16:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 2456 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 2457
group-Make-NTU-Hackathon 0:4233b7b10255 2458 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2459 * Module requirements.
group-Make-NTU-Hackathon 0:4233b7b10255 2460 */
group-Make-NTU-Hackathon 0:4233b7b10255 2461
group-Make-NTU-Hackathon 0:4233b7b10255 2462 var Polling = _dereq_('./polling');
group-Make-NTU-Hackathon 0:4233b7b10255 2463 var inherit = _dereq_('component-inherit');
group-Make-NTU-Hackathon 0:4233b7b10255 2464
group-Make-NTU-Hackathon 0:4233b7b10255 2465 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2466 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 2467 */
group-Make-NTU-Hackathon 0:4233b7b10255 2468
group-Make-NTU-Hackathon 0:4233b7b10255 2469 module.exports = JSONPPolling;
group-Make-NTU-Hackathon 0:4233b7b10255 2470
group-Make-NTU-Hackathon 0:4233b7b10255 2471 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2472 * Cached regular expressions.
group-Make-NTU-Hackathon 0:4233b7b10255 2473 */
group-Make-NTU-Hackathon 0:4233b7b10255 2474
group-Make-NTU-Hackathon 0:4233b7b10255 2475 var rNewline = /\n/g;
group-Make-NTU-Hackathon 0:4233b7b10255 2476 var rEscapedNewline = /\\n/g;
group-Make-NTU-Hackathon 0:4233b7b10255 2477
group-Make-NTU-Hackathon 0:4233b7b10255 2478 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2479 * Global JSONP callbacks.
group-Make-NTU-Hackathon 0:4233b7b10255 2480 */
group-Make-NTU-Hackathon 0:4233b7b10255 2481
group-Make-NTU-Hackathon 0:4233b7b10255 2482 var callbacks;
group-Make-NTU-Hackathon 0:4233b7b10255 2483
group-Make-NTU-Hackathon 0:4233b7b10255 2484 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2485 * Callbacks count.
group-Make-NTU-Hackathon 0:4233b7b10255 2486 */
group-Make-NTU-Hackathon 0:4233b7b10255 2487
group-Make-NTU-Hackathon 0:4233b7b10255 2488 var index = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 2489
group-Make-NTU-Hackathon 0:4233b7b10255 2490 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2491 * Noop.
group-Make-NTU-Hackathon 0:4233b7b10255 2492 */
group-Make-NTU-Hackathon 0:4233b7b10255 2493
group-Make-NTU-Hackathon 0:4233b7b10255 2494 function empty () { }
group-Make-NTU-Hackathon 0:4233b7b10255 2495
group-Make-NTU-Hackathon 0:4233b7b10255 2496 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2497 * JSONP Polling constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 2498 *
group-Make-NTU-Hackathon 0:4233b7b10255 2499 * @param {Object} opts.
group-Make-NTU-Hackathon 0:4233b7b10255 2500 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2501 */
group-Make-NTU-Hackathon 0:4233b7b10255 2502
group-Make-NTU-Hackathon 0:4233b7b10255 2503 function JSONPPolling (opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 2504 Polling.call(this, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2505
group-Make-NTU-Hackathon 0:4233b7b10255 2506 this.query = this.query || {};
group-Make-NTU-Hackathon 0:4233b7b10255 2507
group-Make-NTU-Hackathon 0:4233b7b10255 2508 // define global callbacks array if not present
group-Make-NTU-Hackathon 0:4233b7b10255 2509 // we do this here (lazily) to avoid unneeded global pollution
group-Make-NTU-Hackathon 0:4233b7b10255 2510 if (!callbacks) {
group-Make-NTU-Hackathon 0:4233b7b10255 2511 // we need to consider multiple engines in the same page
group-Make-NTU-Hackathon 0:4233b7b10255 2512 if (!global.___eio) global.___eio = [];
group-Make-NTU-Hackathon 0:4233b7b10255 2513 callbacks = global.___eio;
group-Make-NTU-Hackathon 0:4233b7b10255 2514 }
group-Make-NTU-Hackathon 0:4233b7b10255 2515
group-Make-NTU-Hackathon 0:4233b7b10255 2516 // callback identifier
group-Make-NTU-Hackathon 0:4233b7b10255 2517 this.index = callbacks.length;
group-Make-NTU-Hackathon 0:4233b7b10255 2518
group-Make-NTU-Hackathon 0:4233b7b10255 2519 // add callback to jsonp global
group-Make-NTU-Hackathon 0:4233b7b10255 2520 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2521 callbacks.push(function (msg) {
group-Make-NTU-Hackathon 0:4233b7b10255 2522 self.onData(msg);
group-Make-NTU-Hackathon 0:4233b7b10255 2523 });
group-Make-NTU-Hackathon 0:4233b7b10255 2524
group-Make-NTU-Hackathon 0:4233b7b10255 2525 // append to query string
group-Make-NTU-Hackathon 0:4233b7b10255 2526 this.query.j = this.index;
group-Make-NTU-Hackathon 0:4233b7b10255 2527
group-Make-NTU-Hackathon 0:4233b7b10255 2528 // prevent spurious errors from being emitted when the window is unloaded
group-Make-NTU-Hackathon 0:4233b7b10255 2529 if (global.document && global.addEventListener) {
group-Make-NTU-Hackathon 0:4233b7b10255 2530 global.addEventListener('beforeunload', function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2531 if (self.script) self.script.onerror = empty;
group-Make-NTU-Hackathon 0:4233b7b10255 2532 }, false);
group-Make-NTU-Hackathon 0:4233b7b10255 2533 }
group-Make-NTU-Hackathon 0:4233b7b10255 2534 }
group-Make-NTU-Hackathon 0:4233b7b10255 2535
group-Make-NTU-Hackathon 0:4233b7b10255 2536 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2537 * Inherits from Polling.
group-Make-NTU-Hackathon 0:4233b7b10255 2538 */
group-Make-NTU-Hackathon 0:4233b7b10255 2539
group-Make-NTU-Hackathon 0:4233b7b10255 2540 inherit(JSONPPolling, Polling);
group-Make-NTU-Hackathon 0:4233b7b10255 2541
group-Make-NTU-Hackathon 0:4233b7b10255 2542 /*
group-Make-NTU-Hackathon 0:4233b7b10255 2543 * JSONP only supports binary as base64 encoded strings
group-Make-NTU-Hackathon 0:4233b7b10255 2544 */
group-Make-NTU-Hackathon 0:4233b7b10255 2545
group-Make-NTU-Hackathon 0:4233b7b10255 2546 JSONPPolling.prototype.supportsBinary = false;
group-Make-NTU-Hackathon 0:4233b7b10255 2547
group-Make-NTU-Hackathon 0:4233b7b10255 2548 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2549 * Closes the socket.
group-Make-NTU-Hackathon 0:4233b7b10255 2550 *
group-Make-NTU-Hackathon 0:4233b7b10255 2551 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2552 */
group-Make-NTU-Hackathon 0:4233b7b10255 2553
group-Make-NTU-Hackathon 0:4233b7b10255 2554 JSONPPolling.prototype.doClose = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2555 if (this.script) {
group-Make-NTU-Hackathon 0:4233b7b10255 2556 this.script.parentNode.removeChild(this.script);
group-Make-NTU-Hackathon 0:4233b7b10255 2557 this.script = null;
group-Make-NTU-Hackathon 0:4233b7b10255 2558 }
group-Make-NTU-Hackathon 0:4233b7b10255 2559
group-Make-NTU-Hackathon 0:4233b7b10255 2560 if (this.form) {
group-Make-NTU-Hackathon 0:4233b7b10255 2561 this.form.parentNode.removeChild(this.form);
group-Make-NTU-Hackathon 0:4233b7b10255 2562 this.form = null;
group-Make-NTU-Hackathon 0:4233b7b10255 2563 this.iframe = null;
group-Make-NTU-Hackathon 0:4233b7b10255 2564 }
group-Make-NTU-Hackathon 0:4233b7b10255 2565
group-Make-NTU-Hackathon 0:4233b7b10255 2566 Polling.prototype.doClose.call(this);
group-Make-NTU-Hackathon 0:4233b7b10255 2567 };
group-Make-NTU-Hackathon 0:4233b7b10255 2568
group-Make-NTU-Hackathon 0:4233b7b10255 2569 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2570 * Starts a poll cycle.
group-Make-NTU-Hackathon 0:4233b7b10255 2571 *
group-Make-NTU-Hackathon 0:4233b7b10255 2572 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2573 */
group-Make-NTU-Hackathon 0:4233b7b10255 2574
group-Make-NTU-Hackathon 0:4233b7b10255 2575 JSONPPolling.prototype.doPoll = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2576 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2577 var script = document.createElement('script');
group-Make-NTU-Hackathon 0:4233b7b10255 2578
group-Make-NTU-Hackathon 0:4233b7b10255 2579 if (this.script) {
group-Make-NTU-Hackathon 0:4233b7b10255 2580 this.script.parentNode.removeChild(this.script);
group-Make-NTU-Hackathon 0:4233b7b10255 2581 this.script = null;
group-Make-NTU-Hackathon 0:4233b7b10255 2582 }
group-Make-NTU-Hackathon 0:4233b7b10255 2583
group-Make-NTU-Hackathon 0:4233b7b10255 2584 script.async = true;
group-Make-NTU-Hackathon 0:4233b7b10255 2585 script.src = this.uri();
group-Make-NTU-Hackathon 0:4233b7b10255 2586 script.onerror = function(e){
group-Make-NTU-Hackathon 0:4233b7b10255 2587 self.onError('jsonp poll error',e);
group-Make-NTU-Hackathon 0:4233b7b10255 2588 };
group-Make-NTU-Hackathon 0:4233b7b10255 2589
group-Make-NTU-Hackathon 0:4233b7b10255 2590 var insertAt = document.getElementsByTagName('script')[0];
group-Make-NTU-Hackathon 0:4233b7b10255 2591 insertAt.parentNode.insertBefore(script, insertAt);
group-Make-NTU-Hackathon 0:4233b7b10255 2592 this.script = script;
group-Make-NTU-Hackathon 0:4233b7b10255 2593
group-Make-NTU-Hackathon 0:4233b7b10255 2594 var isUAgecko = 'undefined' != typeof navigator && /gecko/i.test(navigator.userAgent);
group-Make-NTU-Hackathon 0:4233b7b10255 2595
group-Make-NTU-Hackathon 0:4233b7b10255 2596 if (isUAgecko) {
group-Make-NTU-Hackathon 0:4233b7b10255 2597 setTimeout(function () {
group-Make-NTU-Hackathon 0:4233b7b10255 2598 var iframe = document.createElement('iframe');
group-Make-NTU-Hackathon 0:4233b7b10255 2599 document.body.appendChild(iframe);
group-Make-NTU-Hackathon 0:4233b7b10255 2600 document.body.removeChild(iframe);
group-Make-NTU-Hackathon 0:4233b7b10255 2601 }, 100);
group-Make-NTU-Hackathon 0:4233b7b10255 2602 }
group-Make-NTU-Hackathon 0:4233b7b10255 2603 };
group-Make-NTU-Hackathon 0:4233b7b10255 2604
group-Make-NTU-Hackathon 0:4233b7b10255 2605 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2606 * Writes with a hidden iframe.
group-Make-NTU-Hackathon 0:4233b7b10255 2607 *
group-Make-NTU-Hackathon 0:4233b7b10255 2608 * @param {String} data to send
group-Make-NTU-Hackathon 0:4233b7b10255 2609 * @param {Function} called upon flush.
group-Make-NTU-Hackathon 0:4233b7b10255 2610 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2611 */
group-Make-NTU-Hackathon 0:4233b7b10255 2612
group-Make-NTU-Hackathon 0:4233b7b10255 2613 JSONPPolling.prototype.doWrite = function (data, fn) {
group-Make-NTU-Hackathon 0:4233b7b10255 2614 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2615
group-Make-NTU-Hackathon 0:4233b7b10255 2616 if (!this.form) {
group-Make-NTU-Hackathon 0:4233b7b10255 2617 var form = document.createElement('form');
group-Make-NTU-Hackathon 0:4233b7b10255 2618 var area = document.createElement('textarea');
group-Make-NTU-Hackathon 0:4233b7b10255 2619 var id = this.iframeId = 'eio_iframe_' + this.index;
group-Make-NTU-Hackathon 0:4233b7b10255 2620 var iframe;
group-Make-NTU-Hackathon 0:4233b7b10255 2621
group-Make-NTU-Hackathon 0:4233b7b10255 2622 form.className = 'socketio';
group-Make-NTU-Hackathon 0:4233b7b10255 2623 form.style.position = 'absolute';
group-Make-NTU-Hackathon 0:4233b7b10255 2624 form.style.top = '-1000px';
group-Make-NTU-Hackathon 0:4233b7b10255 2625 form.style.left = '-1000px';
group-Make-NTU-Hackathon 0:4233b7b10255 2626 form.target = id;
group-Make-NTU-Hackathon 0:4233b7b10255 2627 form.method = 'POST';
group-Make-NTU-Hackathon 0:4233b7b10255 2628 form.setAttribute('accept-charset', 'utf-8');
group-Make-NTU-Hackathon 0:4233b7b10255 2629 area.name = 'd';
group-Make-NTU-Hackathon 0:4233b7b10255 2630 form.appendChild(area);
group-Make-NTU-Hackathon 0:4233b7b10255 2631 document.body.appendChild(form);
group-Make-NTU-Hackathon 0:4233b7b10255 2632
group-Make-NTU-Hackathon 0:4233b7b10255 2633 this.form = form;
group-Make-NTU-Hackathon 0:4233b7b10255 2634 this.area = area;
group-Make-NTU-Hackathon 0:4233b7b10255 2635 }
group-Make-NTU-Hackathon 0:4233b7b10255 2636
group-Make-NTU-Hackathon 0:4233b7b10255 2637 this.form.action = this.uri();
group-Make-NTU-Hackathon 0:4233b7b10255 2638
group-Make-NTU-Hackathon 0:4233b7b10255 2639 function complete () {
group-Make-NTU-Hackathon 0:4233b7b10255 2640 initIframe();
group-Make-NTU-Hackathon 0:4233b7b10255 2641 fn();
group-Make-NTU-Hackathon 0:4233b7b10255 2642 }
group-Make-NTU-Hackathon 0:4233b7b10255 2643
group-Make-NTU-Hackathon 0:4233b7b10255 2644 function initIframe () {
group-Make-NTU-Hackathon 0:4233b7b10255 2645 if (self.iframe) {
group-Make-NTU-Hackathon 0:4233b7b10255 2646 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2647 self.form.removeChild(self.iframe);
group-Make-NTU-Hackathon 0:4233b7b10255 2648 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 2649 self.onError('jsonp polling iframe removal error', e);
group-Make-NTU-Hackathon 0:4233b7b10255 2650 }
group-Make-NTU-Hackathon 0:4233b7b10255 2651 }
group-Make-NTU-Hackathon 0:4233b7b10255 2652
group-Make-NTU-Hackathon 0:4233b7b10255 2653 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2654 // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
group-Make-NTU-Hackathon 0:4233b7b10255 2655 var html = '<iframe src="javascript:0" name="'+ self.iframeId +'">';
group-Make-NTU-Hackathon 0:4233b7b10255 2656 iframe = document.createElement(html);
group-Make-NTU-Hackathon 0:4233b7b10255 2657 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 2658 iframe = document.createElement('iframe');
group-Make-NTU-Hackathon 0:4233b7b10255 2659 iframe.name = self.iframeId;
group-Make-NTU-Hackathon 0:4233b7b10255 2660 iframe.src = 'javascript:0';
group-Make-NTU-Hackathon 0:4233b7b10255 2661 }
group-Make-NTU-Hackathon 0:4233b7b10255 2662
group-Make-NTU-Hackathon 0:4233b7b10255 2663 iframe.id = self.iframeId;
group-Make-NTU-Hackathon 0:4233b7b10255 2664
group-Make-NTU-Hackathon 0:4233b7b10255 2665 self.form.appendChild(iframe);
group-Make-NTU-Hackathon 0:4233b7b10255 2666 self.iframe = iframe;
group-Make-NTU-Hackathon 0:4233b7b10255 2667 }
group-Make-NTU-Hackathon 0:4233b7b10255 2668
group-Make-NTU-Hackathon 0:4233b7b10255 2669 initIframe();
group-Make-NTU-Hackathon 0:4233b7b10255 2670
group-Make-NTU-Hackathon 0:4233b7b10255 2671 // escape \n to prevent it from being converted into \r\n by some UAs
group-Make-NTU-Hackathon 0:4233b7b10255 2672 // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
group-Make-NTU-Hackathon 0:4233b7b10255 2673 data = data.replace(rEscapedNewline, '\\\n');
group-Make-NTU-Hackathon 0:4233b7b10255 2674 this.area.value = data.replace(rNewline, '\\n');
group-Make-NTU-Hackathon 0:4233b7b10255 2675
group-Make-NTU-Hackathon 0:4233b7b10255 2676 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2677 this.form.submit();
group-Make-NTU-Hackathon 0:4233b7b10255 2678 } catch(e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 2679
group-Make-NTU-Hackathon 0:4233b7b10255 2680 if (this.iframe.attachEvent) {
group-Make-NTU-Hackathon 0:4233b7b10255 2681 this.iframe.onreadystatechange = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2682 if (self.iframe.readyState == 'complete') {
group-Make-NTU-Hackathon 0:4233b7b10255 2683 complete();
group-Make-NTU-Hackathon 0:4233b7b10255 2684 }
group-Make-NTU-Hackathon 0:4233b7b10255 2685 };
group-Make-NTU-Hackathon 0:4233b7b10255 2686 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2687 this.iframe.onload = complete;
group-Make-NTU-Hackathon 0:4233b7b10255 2688 }
group-Make-NTU-Hackathon 0:4233b7b10255 2689 };
group-Make-NTU-Hackathon 0:4233b7b10255 2690
group-Make-NTU-Hackathon 0:4233b7b10255 2691 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 2692 },{"./polling":18,"component-inherit":21}],17:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 2693 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 2694 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2695 * Module requirements.
group-Make-NTU-Hackathon 0:4233b7b10255 2696 */
group-Make-NTU-Hackathon 0:4233b7b10255 2697
group-Make-NTU-Hackathon 0:4233b7b10255 2698 var XMLHttpRequest = _dereq_('xmlhttprequest');
group-Make-NTU-Hackathon 0:4233b7b10255 2699 var Polling = _dereq_('./polling');
group-Make-NTU-Hackathon 0:4233b7b10255 2700 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 2701 var inherit = _dereq_('component-inherit');
group-Make-NTU-Hackathon 0:4233b7b10255 2702 var debug = _dereq_('debug')('engine.io-client:polling-xhr');
group-Make-NTU-Hackathon 0:4233b7b10255 2703
group-Make-NTU-Hackathon 0:4233b7b10255 2704 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2705 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 2706 */
group-Make-NTU-Hackathon 0:4233b7b10255 2707
group-Make-NTU-Hackathon 0:4233b7b10255 2708 module.exports = XHR;
group-Make-NTU-Hackathon 0:4233b7b10255 2709 module.exports.Request = Request;
group-Make-NTU-Hackathon 0:4233b7b10255 2710
group-Make-NTU-Hackathon 0:4233b7b10255 2711 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2712 * Empty function
group-Make-NTU-Hackathon 0:4233b7b10255 2713 */
group-Make-NTU-Hackathon 0:4233b7b10255 2714
group-Make-NTU-Hackathon 0:4233b7b10255 2715 function empty(){}
group-Make-NTU-Hackathon 0:4233b7b10255 2716
group-Make-NTU-Hackathon 0:4233b7b10255 2717 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2718 * XHR Polling constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 2719 *
group-Make-NTU-Hackathon 0:4233b7b10255 2720 * @param {Object} opts
group-Make-NTU-Hackathon 0:4233b7b10255 2721 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2722 */
group-Make-NTU-Hackathon 0:4233b7b10255 2723
group-Make-NTU-Hackathon 0:4233b7b10255 2724 function XHR(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 2725 Polling.call(this, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2726
group-Make-NTU-Hackathon 0:4233b7b10255 2727 if (global.location) {
group-Make-NTU-Hackathon 0:4233b7b10255 2728 var isSSL = 'https:' == location.protocol;
group-Make-NTU-Hackathon 0:4233b7b10255 2729 var port = location.port;
group-Make-NTU-Hackathon 0:4233b7b10255 2730
group-Make-NTU-Hackathon 0:4233b7b10255 2731 // some user agents have empty `location.port`
group-Make-NTU-Hackathon 0:4233b7b10255 2732 if (!port) {
group-Make-NTU-Hackathon 0:4233b7b10255 2733 port = isSSL ? 443 : 80;
group-Make-NTU-Hackathon 0:4233b7b10255 2734 }
group-Make-NTU-Hackathon 0:4233b7b10255 2735
group-Make-NTU-Hackathon 0:4233b7b10255 2736 this.xd = opts.hostname != global.location.hostname ||
group-Make-NTU-Hackathon 0:4233b7b10255 2737 port != opts.port;
group-Make-NTU-Hackathon 0:4233b7b10255 2738 this.xs = opts.secure != isSSL;
group-Make-NTU-Hackathon 0:4233b7b10255 2739 }
group-Make-NTU-Hackathon 0:4233b7b10255 2740 }
group-Make-NTU-Hackathon 0:4233b7b10255 2741
group-Make-NTU-Hackathon 0:4233b7b10255 2742 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2743 * Inherits from Polling.
group-Make-NTU-Hackathon 0:4233b7b10255 2744 */
group-Make-NTU-Hackathon 0:4233b7b10255 2745
group-Make-NTU-Hackathon 0:4233b7b10255 2746 inherit(XHR, Polling);
group-Make-NTU-Hackathon 0:4233b7b10255 2747
group-Make-NTU-Hackathon 0:4233b7b10255 2748 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2749 * XHR supports binary
group-Make-NTU-Hackathon 0:4233b7b10255 2750 */
group-Make-NTU-Hackathon 0:4233b7b10255 2751
group-Make-NTU-Hackathon 0:4233b7b10255 2752 XHR.prototype.supportsBinary = true;
group-Make-NTU-Hackathon 0:4233b7b10255 2753
group-Make-NTU-Hackathon 0:4233b7b10255 2754 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2755 * Creates a request.
group-Make-NTU-Hackathon 0:4233b7b10255 2756 *
group-Make-NTU-Hackathon 0:4233b7b10255 2757 * @param {String} method
group-Make-NTU-Hackathon 0:4233b7b10255 2758 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2759 */
group-Make-NTU-Hackathon 0:4233b7b10255 2760
group-Make-NTU-Hackathon 0:4233b7b10255 2761 XHR.prototype.request = function(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 2762 opts = opts || {};
group-Make-NTU-Hackathon 0:4233b7b10255 2763 opts.uri = this.uri();
group-Make-NTU-Hackathon 0:4233b7b10255 2764 opts.xd = this.xd;
group-Make-NTU-Hackathon 0:4233b7b10255 2765 opts.xs = this.xs;
group-Make-NTU-Hackathon 0:4233b7b10255 2766 opts.agent = this.agent || false;
group-Make-NTU-Hackathon 0:4233b7b10255 2767 opts.supportsBinary = this.supportsBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 2768 opts.enablesXDR = this.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 2769
group-Make-NTU-Hackathon 0:4233b7b10255 2770 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 2771 opts.pfx = this.pfx;
group-Make-NTU-Hackathon 0:4233b7b10255 2772 opts.key = this.key;
group-Make-NTU-Hackathon 0:4233b7b10255 2773 opts.passphrase = this.passphrase;
group-Make-NTU-Hackathon 0:4233b7b10255 2774 opts.cert = this.cert;
group-Make-NTU-Hackathon 0:4233b7b10255 2775 opts.ca = this.ca;
group-Make-NTU-Hackathon 0:4233b7b10255 2776 opts.ciphers = this.ciphers;
group-Make-NTU-Hackathon 0:4233b7b10255 2777 opts.rejectUnauthorized = this.rejectUnauthorized;
group-Make-NTU-Hackathon 0:4233b7b10255 2778
group-Make-NTU-Hackathon 0:4233b7b10255 2779 return new Request(opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2780 };
group-Make-NTU-Hackathon 0:4233b7b10255 2781
group-Make-NTU-Hackathon 0:4233b7b10255 2782 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2783 * Sends data.
group-Make-NTU-Hackathon 0:4233b7b10255 2784 *
group-Make-NTU-Hackathon 0:4233b7b10255 2785 * @param {String} data to send.
group-Make-NTU-Hackathon 0:4233b7b10255 2786 * @param {Function} called upon flush.
group-Make-NTU-Hackathon 0:4233b7b10255 2787 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2788 */
group-Make-NTU-Hackathon 0:4233b7b10255 2789
group-Make-NTU-Hackathon 0:4233b7b10255 2790 XHR.prototype.doWrite = function(data, fn){
group-Make-NTU-Hackathon 0:4233b7b10255 2791 var isBinary = typeof data !== 'string' && data !== undefined;
group-Make-NTU-Hackathon 0:4233b7b10255 2792 var req = this.request({ method: 'POST', data: data, isBinary: isBinary });
group-Make-NTU-Hackathon 0:4233b7b10255 2793 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2794 req.on('success', fn);
group-Make-NTU-Hackathon 0:4233b7b10255 2795 req.on('error', function(err){
group-Make-NTU-Hackathon 0:4233b7b10255 2796 self.onError('xhr post error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2797 });
group-Make-NTU-Hackathon 0:4233b7b10255 2798 this.sendXhr = req;
group-Make-NTU-Hackathon 0:4233b7b10255 2799 };
group-Make-NTU-Hackathon 0:4233b7b10255 2800
group-Make-NTU-Hackathon 0:4233b7b10255 2801 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2802 * Starts a poll cycle.
group-Make-NTU-Hackathon 0:4233b7b10255 2803 *
group-Make-NTU-Hackathon 0:4233b7b10255 2804 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2805 */
group-Make-NTU-Hackathon 0:4233b7b10255 2806
group-Make-NTU-Hackathon 0:4233b7b10255 2807 XHR.prototype.doPoll = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2808 debug('xhr poll');
group-Make-NTU-Hackathon 0:4233b7b10255 2809 var req = this.request();
group-Make-NTU-Hackathon 0:4233b7b10255 2810 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2811 req.on('data', function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 2812 self.onData(data);
group-Make-NTU-Hackathon 0:4233b7b10255 2813 });
group-Make-NTU-Hackathon 0:4233b7b10255 2814 req.on('error', function(err){
group-Make-NTU-Hackathon 0:4233b7b10255 2815 self.onError('xhr poll error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2816 });
group-Make-NTU-Hackathon 0:4233b7b10255 2817 this.pollXhr = req;
group-Make-NTU-Hackathon 0:4233b7b10255 2818 };
group-Make-NTU-Hackathon 0:4233b7b10255 2819
group-Make-NTU-Hackathon 0:4233b7b10255 2820 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2821 * Request constructor
group-Make-NTU-Hackathon 0:4233b7b10255 2822 *
group-Make-NTU-Hackathon 0:4233b7b10255 2823 * @param {Object} options
group-Make-NTU-Hackathon 0:4233b7b10255 2824 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 2825 */
group-Make-NTU-Hackathon 0:4233b7b10255 2826
group-Make-NTU-Hackathon 0:4233b7b10255 2827 function Request(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 2828 this.method = opts.method || 'GET';
group-Make-NTU-Hackathon 0:4233b7b10255 2829 this.uri = opts.uri;
group-Make-NTU-Hackathon 0:4233b7b10255 2830 this.xd = !!opts.xd;
group-Make-NTU-Hackathon 0:4233b7b10255 2831 this.xs = !!opts.xs;
group-Make-NTU-Hackathon 0:4233b7b10255 2832 this.async = false !== opts.async;
group-Make-NTU-Hackathon 0:4233b7b10255 2833 this.data = undefined != opts.data ? opts.data : null;
group-Make-NTU-Hackathon 0:4233b7b10255 2834 this.agent = opts.agent;
group-Make-NTU-Hackathon 0:4233b7b10255 2835 this.isBinary = opts.isBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 2836 this.supportsBinary = opts.supportsBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 2837 this.enablesXDR = opts.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 2838
group-Make-NTU-Hackathon 0:4233b7b10255 2839 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 2840 this.pfx = opts.pfx;
group-Make-NTU-Hackathon 0:4233b7b10255 2841 this.key = opts.key;
group-Make-NTU-Hackathon 0:4233b7b10255 2842 this.passphrase = opts.passphrase;
group-Make-NTU-Hackathon 0:4233b7b10255 2843 this.cert = opts.cert;
group-Make-NTU-Hackathon 0:4233b7b10255 2844 this.ca = opts.ca;
group-Make-NTU-Hackathon 0:4233b7b10255 2845 this.ciphers = opts.ciphers;
group-Make-NTU-Hackathon 0:4233b7b10255 2846 this.rejectUnauthorized = opts.rejectUnauthorized;
group-Make-NTU-Hackathon 0:4233b7b10255 2847
group-Make-NTU-Hackathon 0:4233b7b10255 2848 this.create();
group-Make-NTU-Hackathon 0:4233b7b10255 2849 }
group-Make-NTU-Hackathon 0:4233b7b10255 2850
group-Make-NTU-Hackathon 0:4233b7b10255 2851 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2852 * Mix in `Emitter`.
group-Make-NTU-Hackathon 0:4233b7b10255 2853 */
group-Make-NTU-Hackathon 0:4233b7b10255 2854
group-Make-NTU-Hackathon 0:4233b7b10255 2855 Emitter(Request.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 2856
group-Make-NTU-Hackathon 0:4233b7b10255 2857 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2858 * Creates the XHR object and sends the request.
group-Make-NTU-Hackathon 0:4233b7b10255 2859 *
group-Make-NTU-Hackathon 0:4233b7b10255 2860 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2861 */
group-Make-NTU-Hackathon 0:4233b7b10255 2862
group-Make-NTU-Hackathon 0:4233b7b10255 2863 Request.prototype.create = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2864 var opts = { agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR };
group-Make-NTU-Hackathon 0:4233b7b10255 2865
group-Make-NTU-Hackathon 0:4233b7b10255 2866 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 2867 opts.pfx = this.pfx;
group-Make-NTU-Hackathon 0:4233b7b10255 2868 opts.key = this.key;
group-Make-NTU-Hackathon 0:4233b7b10255 2869 opts.passphrase = this.passphrase;
group-Make-NTU-Hackathon 0:4233b7b10255 2870 opts.cert = this.cert;
group-Make-NTU-Hackathon 0:4233b7b10255 2871 opts.ca = this.ca;
group-Make-NTU-Hackathon 0:4233b7b10255 2872 opts.ciphers = this.ciphers;
group-Make-NTU-Hackathon 0:4233b7b10255 2873 opts.rejectUnauthorized = this.rejectUnauthorized;
group-Make-NTU-Hackathon 0:4233b7b10255 2874
group-Make-NTU-Hackathon 0:4233b7b10255 2875 var xhr = this.xhr = new XMLHttpRequest(opts);
group-Make-NTU-Hackathon 0:4233b7b10255 2876 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2877
group-Make-NTU-Hackathon 0:4233b7b10255 2878 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2879 debug('xhr open %s: %s', this.method, this.uri);
group-Make-NTU-Hackathon 0:4233b7b10255 2880 xhr.open(this.method, this.uri, this.async);
group-Make-NTU-Hackathon 0:4233b7b10255 2881 if (this.supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 2882 // This has to be done after open because Firefox is stupid
group-Make-NTU-Hackathon 0:4233b7b10255 2883 // http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension
group-Make-NTU-Hackathon 0:4233b7b10255 2884 xhr.responseType = 'arraybuffer';
group-Make-NTU-Hackathon 0:4233b7b10255 2885 }
group-Make-NTU-Hackathon 0:4233b7b10255 2886
group-Make-NTU-Hackathon 0:4233b7b10255 2887 if ('POST' == this.method) {
group-Make-NTU-Hackathon 0:4233b7b10255 2888 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2889 if (this.isBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 2890 xhr.setRequestHeader('Content-type', 'application/octet-stream');
group-Make-NTU-Hackathon 0:4233b7b10255 2891 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2892 xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
group-Make-NTU-Hackathon 0:4233b7b10255 2893 }
group-Make-NTU-Hackathon 0:4233b7b10255 2894 } catch (e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 2895 }
group-Make-NTU-Hackathon 0:4233b7b10255 2896
group-Make-NTU-Hackathon 0:4233b7b10255 2897 // ie6 check
group-Make-NTU-Hackathon 0:4233b7b10255 2898 if ('withCredentials' in xhr) {
group-Make-NTU-Hackathon 0:4233b7b10255 2899 xhr.withCredentials = true;
group-Make-NTU-Hackathon 0:4233b7b10255 2900 }
group-Make-NTU-Hackathon 0:4233b7b10255 2901
group-Make-NTU-Hackathon 0:4233b7b10255 2902 if (this.hasXDR()) {
group-Make-NTU-Hackathon 0:4233b7b10255 2903 xhr.onload = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2904 self.onLoad();
group-Make-NTU-Hackathon 0:4233b7b10255 2905 };
group-Make-NTU-Hackathon 0:4233b7b10255 2906 xhr.onerror = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2907 self.onError(xhr.responseText);
group-Make-NTU-Hackathon 0:4233b7b10255 2908 };
group-Make-NTU-Hackathon 0:4233b7b10255 2909 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2910 xhr.onreadystatechange = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2911 if (4 != xhr.readyState) return;
group-Make-NTU-Hackathon 0:4233b7b10255 2912 if (200 == xhr.status || 1223 == xhr.status) {
group-Make-NTU-Hackathon 0:4233b7b10255 2913 self.onLoad();
group-Make-NTU-Hackathon 0:4233b7b10255 2914 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2915 // make sure the `error` event handler that's user-set
group-Make-NTU-Hackathon 0:4233b7b10255 2916 // does not throw in the same tick and gets caught here
group-Make-NTU-Hackathon 0:4233b7b10255 2917 setTimeout(function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2918 self.onError(xhr.status);
group-Make-NTU-Hackathon 0:4233b7b10255 2919 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 2920 }
group-Make-NTU-Hackathon 0:4233b7b10255 2921 };
group-Make-NTU-Hackathon 0:4233b7b10255 2922 }
group-Make-NTU-Hackathon 0:4233b7b10255 2923
group-Make-NTU-Hackathon 0:4233b7b10255 2924 debug('xhr data %s', this.data);
group-Make-NTU-Hackathon 0:4233b7b10255 2925 xhr.send(this.data);
group-Make-NTU-Hackathon 0:4233b7b10255 2926 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 2927 // Need to defer since .create() is called directly fhrom the constructor
group-Make-NTU-Hackathon 0:4233b7b10255 2928 // and thus the 'error' event can only be only bound *after* this exception
group-Make-NTU-Hackathon 0:4233b7b10255 2929 // occurs. Therefore, also, we cannot throw here at all.
group-Make-NTU-Hackathon 0:4233b7b10255 2930 setTimeout(function() {
group-Make-NTU-Hackathon 0:4233b7b10255 2931 self.onError(e);
group-Make-NTU-Hackathon 0:4233b7b10255 2932 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 2933 return;
group-Make-NTU-Hackathon 0:4233b7b10255 2934 }
group-Make-NTU-Hackathon 0:4233b7b10255 2935
group-Make-NTU-Hackathon 0:4233b7b10255 2936 if (global.document) {
group-Make-NTU-Hackathon 0:4233b7b10255 2937 this.index = Request.requestsCount++;
group-Make-NTU-Hackathon 0:4233b7b10255 2938 Request.requests[this.index] = this;
group-Make-NTU-Hackathon 0:4233b7b10255 2939 }
group-Make-NTU-Hackathon 0:4233b7b10255 2940 };
group-Make-NTU-Hackathon 0:4233b7b10255 2941
group-Make-NTU-Hackathon 0:4233b7b10255 2942 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2943 * Called upon successful response.
group-Make-NTU-Hackathon 0:4233b7b10255 2944 *
group-Make-NTU-Hackathon 0:4233b7b10255 2945 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2946 */
group-Make-NTU-Hackathon 0:4233b7b10255 2947
group-Make-NTU-Hackathon 0:4233b7b10255 2948 Request.prototype.onSuccess = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 2949 this.emit('success');
group-Make-NTU-Hackathon 0:4233b7b10255 2950 this.cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 2951 };
group-Make-NTU-Hackathon 0:4233b7b10255 2952
group-Make-NTU-Hackathon 0:4233b7b10255 2953 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2954 * Called if we have data.
group-Make-NTU-Hackathon 0:4233b7b10255 2955 *
group-Make-NTU-Hackathon 0:4233b7b10255 2956 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2957 */
group-Make-NTU-Hackathon 0:4233b7b10255 2958
group-Make-NTU-Hackathon 0:4233b7b10255 2959 Request.prototype.onData = function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 2960 this.emit('data', data);
group-Make-NTU-Hackathon 0:4233b7b10255 2961 this.onSuccess();
group-Make-NTU-Hackathon 0:4233b7b10255 2962 };
group-Make-NTU-Hackathon 0:4233b7b10255 2963
group-Make-NTU-Hackathon 0:4233b7b10255 2964 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2965 * Called upon error.
group-Make-NTU-Hackathon 0:4233b7b10255 2966 *
group-Make-NTU-Hackathon 0:4233b7b10255 2967 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2968 */
group-Make-NTU-Hackathon 0:4233b7b10255 2969
group-Make-NTU-Hackathon 0:4233b7b10255 2970 Request.prototype.onError = function(err){
group-Make-NTU-Hackathon 0:4233b7b10255 2971 this.emit('error', err);
group-Make-NTU-Hackathon 0:4233b7b10255 2972 this.cleanup(true);
group-Make-NTU-Hackathon 0:4233b7b10255 2973 };
group-Make-NTU-Hackathon 0:4233b7b10255 2974
group-Make-NTU-Hackathon 0:4233b7b10255 2975 /**
group-Make-NTU-Hackathon 0:4233b7b10255 2976 * Cleans up house.
group-Make-NTU-Hackathon 0:4233b7b10255 2977 *
group-Make-NTU-Hackathon 0:4233b7b10255 2978 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 2979 */
group-Make-NTU-Hackathon 0:4233b7b10255 2980
group-Make-NTU-Hackathon 0:4233b7b10255 2981 Request.prototype.cleanup = function(fromError){
group-Make-NTU-Hackathon 0:4233b7b10255 2982 if ('undefined' == typeof this.xhr || null === this.xhr) {
group-Make-NTU-Hackathon 0:4233b7b10255 2983 return;
group-Make-NTU-Hackathon 0:4233b7b10255 2984 }
group-Make-NTU-Hackathon 0:4233b7b10255 2985 // xmlhttprequest
group-Make-NTU-Hackathon 0:4233b7b10255 2986 if (this.hasXDR()) {
group-Make-NTU-Hackathon 0:4233b7b10255 2987 this.xhr.onload = this.xhr.onerror = empty;
group-Make-NTU-Hackathon 0:4233b7b10255 2988 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 2989 this.xhr.onreadystatechange = empty;
group-Make-NTU-Hackathon 0:4233b7b10255 2990 }
group-Make-NTU-Hackathon 0:4233b7b10255 2991
group-Make-NTU-Hackathon 0:4233b7b10255 2992 if (fromError) {
group-Make-NTU-Hackathon 0:4233b7b10255 2993 try {
group-Make-NTU-Hackathon 0:4233b7b10255 2994 this.xhr.abort();
group-Make-NTU-Hackathon 0:4233b7b10255 2995 } catch(e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 2996 }
group-Make-NTU-Hackathon 0:4233b7b10255 2997
group-Make-NTU-Hackathon 0:4233b7b10255 2998 if (global.document) {
group-Make-NTU-Hackathon 0:4233b7b10255 2999 delete Request.requests[this.index];
group-Make-NTU-Hackathon 0:4233b7b10255 3000 }
group-Make-NTU-Hackathon 0:4233b7b10255 3001
group-Make-NTU-Hackathon 0:4233b7b10255 3002 this.xhr = null;
group-Make-NTU-Hackathon 0:4233b7b10255 3003 };
group-Make-NTU-Hackathon 0:4233b7b10255 3004
group-Make-NTU-Hackathon 0:4233b7b10255 3005 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3006 * Called upon load.
group-Make-NTU-Hackathon 0:4233b7b10255 3007 *
group-Make-NTU-Hackathon 0:4233b7b10255 3008 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3009 */
group-Make-NTU-Hackathon 0:4233b7b10255 3010
group-Make-NTU-Hackathon 0:4233b7b10255 3011 Request.prototype.onLoad = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3012 var data;
group-Make-NTU-Hackathon 0:4233b7b10255 3013 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3014 var contentType;
group-Make-NTU-Hackathon 0:4233b7b10255 3015 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3016 contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0];
group-Make-NTU-Hackathon 0:4233b7b10255 3017 } catch (e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 3018 if (contentType === 'application/octet-stream') {
group-Make-NTU-Hackathon 0:4233b7b10255 3019 data = this.xhr.response;
group-Make-NTU-Hackathon 0:4233b7b10255 3020 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3021 if (!this.supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 3022 data = this.xhr.responseText;
group-Make-NTU-Hackathon 0:4233b7b10255 3023 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3024 data = 'ok';
group-Make-NTU-Hackathon 0:4233b7b10255 3025 }
group-Make-NTU-Hackathon 0:4233b7b10255 3026 }
group-Make-NTU-Hackathon 0:4233b7b10255 3027 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 3028 this.onError(e);
group-Make-NTU-Hackathon 0:4233b7b10255 3029 }
group-Make-NTU-Hackathon 0:4233b7b10255 3030 if (null != data) {
group-Make-NTU-Hackathon 0:4233b7b10255 3031 this.onData(data);
group-Make-NTU-Hackathon 0:4233b7b10255 3032 }
group-Make-NTU-Hackathon 0:4233b7b10255 3033 };
group-Make-NTU-Hackathon 0:4233b7b10255 3034
group-Make-NTU-Hackathon 0:4233b7b10255 3035 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3036 * Check if it has XDomainRequest.
group-Make-NTU-Hackathon 0:4233b7b10255 3037 *
group-Make-NTU-Hackathon 0:4233b7b10255 3038 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3039 */
group-Make-NTU-Hackathon 0:4233b7b10255 3040
group-Make-NTU-Hackathon 0:4233b7b10255 3041 Request.prototype.hasXDR = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3042 return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 3043 };
group-Make-NTU-Hackathon 0:4233b7b10255 3044
group-Make-NTU-Hackathon 0:4233b7b10255 3045 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3046 * Aborts the request.
group-Make-NTU-Hackathon 0:4233b7b10255 3047 *
group-Make-NTU-Hackathon 0:4233b7b10255 3048 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3049 */
group-Make-NTU-Hackathon 0:4233b7b10255 3050
group-Make-NTU-Hackathon 0:4233b7b10255 3051 Request.prototype.abort = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3052 this.cleanup();
group-Make-NTU-Hackathon 0:4233b7b10255 3053 };
group-Make-NTU-Hackathon 0:4233b7b10255 3054
group-Make-NTU-Hackathon 0:4233b7b10255 3055 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3056 * Aborts pending requests when unloading the window. This is needed to prevent
group-Make-NTU-Hackathon 0:4233b7b10255 3057 * memory leaks (e.g. when using IE) and to ensure that no spurious error is
group-Make-NTU-Hackathon 0:4233b7b10255 3058 * emitted.
group-Make-NTU-Hackathon 0:4233b7b10255 3059 */
group-Make-NTU-Hackathon 0:4233b7b10255 3060
group-Make-NTU-Hackathon 0:4233b7b10255 3061 if (global.document) {
group-Make-NTU-Hackathon 0:4233b7b10255 3062 Request.requestsCount = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3063 Request.requests = {};
group-Make-NTU-Hackathon 0:4233b7b10255 3064 if (global.attachEvent) {
group-Make-NTU-Hackathon 0:4233b7b10255 3065 global.attachEvent('onunload', unloadHandler);
group-Make-NTU-Hackathon 0:4233b7b10255 3066 } else if (global.addEventListener) {
group-Make-NTU-Hackathon 0:4233b7b10255 3067 global.addEventListener('beforeunload', unloadHandler, false);
group-Make-NTU-Hackathon 0:4233b7b10255 3068 }
group-Make-NTU-Hackathon 0:4233b7b10255 3069 }
group-Make-NTU-Hackathon 0:4233b7b10255 3070
group-Make-NTU-Hackathon 0:4233b7b10255 3071 function unloadHandler() {
group-Make-NTU-Hackathon 0:4233b7b10255 3072 for (var i in Request.requests) {
group-Make-NTU-Hackathon 0:4233b7b10255 3073 if (Request.requests.hasOwnProperty(i)) {
group-Make-NTU-Hackathon 0:4233b7b10255 3074 Request.requests[i].abort();
group-Make-NTU-Hackathon 0:4233b7b10255 3075 }
group-Make-NTU-Hackathon 0:4233b7b10255 3076 }
group-Make-NTU-Hackathon 0:4233b7b10255 3077 }
group-Make-NTU-Hackathon 0:4233b7b10255 3078
group-Make-NTU-Hackathon 0:4233b7b10255 3079 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 3080 },{"./polling":18,"component-emitter":9,"component-inherit":21,"debug":22,"xmlhttprequest":20}],18:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3081 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3082 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 3083 */
group-Make-NTU-Hackathon 0:4233b7b10255 3084
group-Make-NTU-Hackathon 0:4233b7b10255 3085 var Transport = _dereq_('../transport');
group-Make-NTU-Hackathon 0:4233b7b10255 3086 var parseqs = _dereq_('parseqs');
group-Make-NTU-Hackathon 0:4233b7b10255 3087 var parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 3088 var inherit = _dereq_('component-inherit');
group-Make-NTU-Hackathon 0:4233b7b10255 3089 var debug = _dereq_('debug')('engine.io-client:polling');
group-Make-NTU-Hackathon 0:4233b7b10255 3090
group-Make-NTU-Hackathon 0:4233b7b10255 3091 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3092 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 3093 */
group-Make-NTU-Hackathon 0:4233b7b10255 3094
group-Make-NTU-Hackathon 0:4233b7b10255 3095 module.exports = Polling;
group-Make-NTU-Hackathon 0:4233b7b10255 3096
group-Make-NTU-Hackathon 0:4233b7b10255 3097 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3098 * Is XHR2 supported?
group-Make-NTU-Hackathon 0:4233b7b10255 3099 */
group-Make-NTU-Hackathon 0:4233b7b10255 3100
group-Make-NTU-Hackathon 0:4233b7b10255 3101 var hasXHR2 = (function() {
group-Make-NTU-Hackathon 0:4233b7b10255 3102 var XMLHttpRequest = _dereq_('xmlhttprequest');
group-Make-NTU-Hackathon 0:4233b7b10255 3103 var xhr = new XMLHttpRequest({ xdomain: false });
group-Make-NTU-Hackathon 0:4233b7b10255 3104 return null != xhr.responseType;
group-Make-NTU-Hackathon 0:4233b7b10255 3105 })();
group-Make-NTU-Hackathon 0:4233b7b10255 3106
group-Make-NTU-Hackathon 0:4233b7b10255 3107 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3108 * Polling interface.
group-Make-NTU-Hackathon 0:4233b7b10255 3109 *
group-Make-NTU-Hackathon 0:4233b7b10255 3110 * @param {Object} opts
group-Make-NTU-Hackathon 0:4233b7b10255 3111 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3112 */
group-Make-NTU-Hackathon 0:4233b7b10255 3113
group-Make-NTU-Hackathon 0:4233b7b10255 3114 function Polling(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 3115 var forceBase64 = (opts && opts.forceBase64);
group-Make-NTU-Hackathon 0:4233b7b10255 3116 if (!hasXHR2 || forceBase64) {
group-Make-NTU-Hackathon 0:4233b7b10255 3117 this.supportsBinary = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3118 }
group-Make-NTU-Hackathon 0:4233b7b10255 3119 Transport.call(this, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 3120 }
group-Make-NTU-Hackathon 0:4233b7b10255 3121
group-Make-NTU-Hackathon 0:4233b7b10255 3122 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3123 * Inherits from Transport.
group-Make-NTU-Hackathon 0:4233b7b10255 3124 */
group-Make-NTU-Hackathon 0:4233b7b10255 3125
group-Make-NTU-Hackathon 0:4233b7b10255 3126 inherit(Polling, Transport);
group-Make-NTU-Hackathon 0:4233b7b10255 3127
group-Make-NTU-Hackathon 0:4233b7b10255 3128 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3129 * Transport name.
group-Make-NTU-Hackathon 0:4233b7b10255 3130 */
group-Make-NTU-Hackathon 0:4233b7b10255 3131
group-Make-NTU-Hackathon 0:4233b7b10255 3132 Polling.prototype.name = 'polling';
group-Make-NTU-Hackathon 0:4233b7b10255 3133
group-Make-NTU-Hackathon 0:4233b7b10255 3134 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3135 * Opens the socket (triggers polling). We write a PING message to determine
group-Make-NTU-Hackathon 0:4233b7b10255 3136 * when the transport is open.
group-Make-NTU-Hackathon 0:4233b7b10255 3137 *
group-Make-NTU-Hackathon 0:4233b7b10255 3138 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3139 */
group-Make-NTU-Hackathon 0:4233b7b10255 3140
group-Make-NTU-Hackathon 0:4233b7b10255 3141 Polling.prototype.doOpen = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3142 this.poll();
group-Make-NTU-Hackathon 0:4233b7b10255 3143 };
group-Make-NTU-Hackathon 0:4233b7b10255 3144
group-Make-NTU-Hackathon 0:4233b7b10255 3145 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3146 * Pauses polling.
group-Make-NTU-Hackathon 0:4233b7b10255 3147 *
group-Make-NTU-Hackathon 0:4233b7b10255 3148 * @param {Function} callback upon buffers are flushed and transport is paused
group-Make-NTU-Hackathon 0:4233b7b10255 3149 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3150 */
group-Make-NTU-Hackathon 0:4233b7b10255 3151
group-Make-NTU-Hackathon 0:4233b7b10255 3152 Polling.prototype.pause = function(onPause){
group-Make-NTU-Hackathon 0:4233b7b10255 3153 var pending = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3154 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3155
group-Make-NTU-Hackathon 0:4233b7b10255 3156 this.readyState = 'pausing';
group-Make-NTU-Hackathon 0:4233b7b10255 3157
group-Make-NTU-Hackathon 0:4233b7b10255 3158 function pause(){
group-Make-NTU-Hackathon 0:4233b7b10255 3159 debug('paused');
group-Make-NTU-Hackathon 0:4233b7b10255 3160 self.readyState = 'paused';
group-Make-NTU-Hackathon 0:4233b7b10255 3161 onPause();
group-Make-NTU-Hackathon 0:4233b7b10255 3162 }
group-Make-NTU-Hackathon 0:4233b7b10255 3163
group-Make-NTU-Hackathon 0:4233b7b10255 3164 if (this.polling || !this.writable) {
group-Make-NTU-Hackathon 0:4233b7b10255 3165 var total = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3166
group-Make-NTU-Hackathon 0:4233b7b10255 3167 if (this.polling) {
group-Make-NTU-Hackathon 0:4233b7b10255 3168 debug('we are currently polling - waiting to pause');
group-Make-NTU-Hackathon 0:4233b7b10255 3169 total++;
group-Make-NTU-Hackathon 0:4233b7b10255 3170 this.once('pollComplete', function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3171 debug('pre-pause polling complete');
group-Make-NTU-Hackathon 0:4233b7b10255 3172 --total || pause();
group-Make-NTU-Hackathon 0:4233b7b10255 3173 });
group-Make-NTU-Hackathon 0:4233b7b10255 3174 }
group-Make-NTU-Hackathon 0:4233b7b10255 3175
group-Make-NTU-Hackathon 0:4233b7b10255 3176 if (!this.writable) {
group-Make-NTU-Hackathon 0:4233b7b10255 3177 debug('we are currently writing - waiting to pause');
group-Make-NTU-Hackathon 0:4233b7b10255 3178 total++;
group-Make-NTU-Hackathon 0:4233b7b10255 3179 this.once('drain', function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3180 debug('pre-pause writing complete');
group-Make-NTU-Hackathon 0:4233b7b10255 3181 --total || pause();
group-Make-NTU-Hackathon 0:4233b7b10255 3182 });
group-Make-NTU-Hackathon 0:4233b7b10255 3183 }
group-Make-NTU-Hackathon 0:4233b7b10255 3184 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3185 pause();
group-Make-NTU-Hackathon 0:4233b7b10255 3186 }
group-Make-NTU-Hackathon 0:4233b7b10255 3187 };
group-Make-NTU-Hackathon 0:4233b7b10255 3188
group-Make-NTU-Hackathon 0:4233b7b10255 3189 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3190 * Starts polling cycle.
group-Make-NTU-Hackathon 0:4233b7b10255 3191 *
group-Make-NTU-Hackathon 0:4233b7b10255 3192 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3193 */
group-Make-NTU-Hackathon 0:4233b7b10255 3194
group-Make-NTU-Hackathon 0:4233b7b10255 3195 Polling.prototype.poll = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3196 debug('polling');
group-Make-NTU-Hackathon 0:4233b7b10255 3197 this.polling = true;
group-Make-NTU-Hackathon 0:4233b7b10255 3198 this.doPoll();
group-Make-NTU-Hackathon 0:4233b7b10255 3199 this.emit('poll');
group-Make-NTU-Hackathon 0:4233b7b10255 3200 };
group-Make-NTU-Hackathon 0:4233b7b10255 3201
group-Make-NTU-Hackathon 0:4233b7b10255 3202 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3203 * Overloads onData to detect payloads.
group-Make-NTU-Hackathon 0:4233b7b10255 3204 *
group-Make-NTU-Hackathon 0:4233b7b10255 3205 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3206 */
group-Make-NTU-Hackathon 0:4233b7b10255 3207
group-Make-NTU-Hackathon 0:4233b7b10255 3208 Polling.prototype.onData = function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 3209 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3210 debug('polling got data %s', data);
group-Make-NTU-Hackathon 0:4233b7b10255 3211 var callback = function(packet, index, total) {
group-Make-NTU-Hackathon 0:4233b7b10255 3212 // if its the first message we consider the transport open
group-Make-NTU-Hackathon 0:4233b7b10255 3213 if ('opening' == self.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 3214 self.onOpen();
group-Make-NTU-Hackathon 0:4233b7b10255 3215 }
group-Make-NTU-Hackathon 0:4233b7b10255 3216
group-Make-NTU-Hackathon 0:4233b7b10255 3217 // if its a close packet, we close the ongoing requests
group-Make-NTU-Hackathon 0:4233b7b10255 3218 if ('close' == packet.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 3219 self.onClose();
group-Make-NTU-Hackathon 0:4233b7b10255 3220 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 3221 }
group-Make-NTU-Hackathon 0:4233b7b10255 3222
group-Make-NTU-Hackathon 0:4233b7b10255 3223 // otherwise bypass onData and handle the message
group-Make-NTU-Hackathon 0:4233b7b10255 3224 self.onPacket(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 3225 };
group-Make-NTU-Hackathon 0:4233b7b10255 3226
group-Make-NTU-Hackathon 0:4233b7b10255 3227 // decode payload
group-Make-NTU-Hackathon 0:4233b7b10255 3228 parser.decodePayload(data, this.socket.binaryType, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 3229
group-Make-NTU-Hackathon 0:4233b7b10255 3230 // if an event did not trigger closing
group-Make-NTU-Hackathon 0:4233b7b10255 3231 if ('closed' != this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 3232 // if we got data we're not polling
group-Make-NTU-Hackathon 0:4233b7b10255 3233 this.polling = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3234 this.emit('pollComplete');
group-Make-NTU-Hackathon 0:4233b7b10255 3235
group-Make-NTU-Hackathon 0:4233b7b10255 3236 if ('open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 3237 this.poll();
group-Make-NTU-Hackathon 0:4233b7b10255 3238 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3239 debug('ignoring poll - transport state "%s"', this.readyState);
group-Make-NTU-Hackathon 0:4233b7b10255 3240 }
group-Make-NTU-Hackathon 0:4233b7b10255 3241 }
group-Make-NTU-Hackathon 0:4233b7b10255 3242 };
group-Make-NTU-Hackathon 0:4233b7b10255 3243
group-Make-NTU-Hackathon 0:4233b7b10255 3244 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3245 * For polling, send a close packet.
group-Make-NTU-Hackathon 0:4233b7b10255 3246 *
group-Make-NTU-Hackathon 0:4233b7b10255 3247 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3248 */
group-Make-NTU-Hackathon 0:4233b7b10255 3249
group-Make-NTU-Hackathon 0:4233b7b10255 3250 Polling.prototype.doClose = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3251 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3252
group-Make-NTU-Hackathon 0:4233b7b10255 3253 function close(){
group-Make-NTU-Hackathon 0:4233b7b10255 3254 debug('writing close packet');
group-Make-NTU-Hackathon 0:4233b7b10255 3255 self.write([{ type: 'close' }]);
group-Make-NTU-Hackathon 0:4233b7b10255 3256 }
group-Make-NTU-Hackathon 0:4233b7b10255 3257
group-Make-NTU-Hackathon 0:4233b7b10255 3258 if ('open' == this.readyState) {
group-Make-NTU-Hackathon 0:4233b7b10255 3259 debug('transport open - closing');
group-Make-NTU-Hackathon 0:4233b7b10255 3260 close();
group-Make-NTU-Hackathon 0:4233b7b10255 3261 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3262 // in case we're trying to close while
group-Make-NTU-Hackathon 0:4233b7b10255 3263 // handshaking is in progress (GH-164)
group-Make-NTU-Hackathon 0:4233b7b10255 3264 debug('transport not open - deferring close');
group-Make-NTU-Hackathon 0:4233b7b10255 3265 this.once('open', close);
group-Make-NTU-Hackathon 0:4233b7b10255 3266 }
group-Make-NTU-Hackathon 0:4233b7b10255 3267 };
group-Make-NTU-Hackathon 0:4233b7b10255 3268
group-Make-NTU-Hackathon 0:4233b7b10255 3269 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3270 * Writes a packets payload.
group-Make-NTU-Hackathon 0:4233b7b10255 3271 *
group-Make-NTU-Hackathon 0:4233b7b10255 3272 * @param {Array} data packets
group-Make-NTU-Hackathon 0:4233b7b10255 3273 * @param {Function} drain callback
group-Make-NTU-Hackathon 0:4233b7b10255 3274 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3275 */
group-Make-NTU-Hackathon 0:4233b7b10255 3276
group-Make-NTU-Hackathon 0:4233b7b10255 3277 Polling.prototype.write = function(packets){
group-Make-NTU-Hackathon 0:4233b7b10255 3278 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3279 this.writable = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3280 var callbackfn = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 3281 self.writable = true;
group-Make-NTU-Hackathon 0:4233b7b10255 3282 self.emit('drain');
group-Make-NTU-Hackathon 0:4233b7b10255 3283 };
group-Make-NTU-Hackathon 0:4233b7b10255 3284
group-Make-NTU-Hackathon 0:4233b7b10255 3285 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3286 parser.encodePayload(packets, this.supportsBinary, function(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 3287 self.doWrite(data, callbackfn);
group-Make-NTU-Hackathon 0:4233b7b10255 3288 });
group-Make-NTU-Hackathon 0:4233b7b10255 3289 };
group-Make-NTU-Hackathon 0:4233b7b10255 3290
group-Make-NTU-Hackathon 0:4233b7b10255 3291 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3292 * Generates uri for connection.
group-Make-NTU-Hackathon 0:4233b7b10255 3293 *
group-Make-NTU-Hackathon 0:4233b7b10255 3294 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3295 */
group-Make-NTU-Hackathon 0:4233b7b10255 3296
group-Make-NTU-Hackathon 0:4233b7b10255 3297 Polling.prototype.uri = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3298 var query = this.query || {};
group-Make-NTU-Hackathon 0:4233b7b10255 3299 var schema = this.secure ? 'https' : 'http';
group-Make-NTU-Hackathon 0:4233b7b10255 3300 var port = '';
group-Make-NTU-Hackathon 0:4233b7b10255 3301
group-Make-NTU-Hackathon 0:4233b7b10255 3302 // cache busting is forced
group-Make-NTU-Hackathon 0:4233b7b10255 3303 if (false !== this.timestampRequests) {
group-Make-NTU-Hackathon 0:4233b7b10255 3304 query[this.timestampParam] = +new Date + '-' + Transport.timestamps++;
group-Make-NTU-Hackathon 0:4233b7b10255 3305 }
group-Make-NTU-Hackathon 0:4233b7b10255 3306
group-Make-NTU-Hackathon 0:4233b7b10255 3307 if (!this.supportsBinary && !query.sid) {
group-Make-NTU-Hackathon 0:4233b7b10255 3308 query.b64 = 1;
group-Make-NTU-Hackathon 0:4233b7b10255 3309 }
group-Make-NTU-Hackathon 0:4233b7b10255 3310
group-Make-NTU-Hackathon 0:4233b7b10255 3311 query = parseqs.encode(query);
group-Make-NTU-Hackathon 0:4233b7b10255 3312
group-Make-NTU-Hackathon 0:4233b7b10255 3313 // avoid port if default for schema
group-Make-NTU-Hackathon 0:4233b7b10255 3314 if (this.port && (('https' == schema && this.port != 443) ||
group-Make-NTU-Hackathon 0:4233b7b10255 3315 ('http' == schema && this.port != 80))) {
group-Make-NTU-Hackathon 0:4233b7b10255 3316 port = ':' + this.port;
group-Make-NTU-Hackathon 0:4233b7b10255 3317 }
group-Make-NTU-Hackathon 0:4233b7b10255 3318
group-Make-NTU-Hackathon 0:4233b7b10255 3319 // prepend ? to query
group-Make-NTU-Hackathon 0:4233b7b10255 3320 if (query.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 3321 query = '?' + query;
group-Make-NTU-Hackathon 0:4233b7b10255 3322 }
group-Make-NTU-Hackathon 0:4233b7b10255 3323
group-Make-NTU-Hackathon 0:4233b7b10255 3324 return schema + '://' + this.hostname + port + this.path + query;
group-Make-NTU-Hackathon 0:4233b7b10255 3325 };
group-Make-NTU-Hackathon 0:4233b7b10255 3326
group-Make-NTU-Hackathon 0:4233b7b10255 3327 },{"../transport":14,"component-inherit":21,"debug":22,"engine.io-parser":25,"parseqs":35,"xmlhttprequest":20}],19:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3328 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3329 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 3330 */
group-Make-NTU-Hackathon 0:4233b7b10255 3331
group-Make-NTU-Hackathon 0:4233b7b10255 3332 var Transport = _dereq_('../transport');
group-Make-NTU-Hackathon 0:4233b7b10255 3333 var parser = _dereq_('engine.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 3334 var parseqs = _dereq_('parseqs');
group-Make-NTU-Hackathon 0:4233b7b10255 3335 var inherit = _dereq_('component-inherit');
group-Make-NTU-Hackathon 0:4233b7b10255 3336 var debug = _dereq_('debug')('engine.io-client:websocket');
group-Make-NTU-Hackathon 0:4233b7b10255 3337
group-Make-NTU-Hackathon 0:4233b7b10255 3338 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3339 * `ws` exposes a WebSocket-compatible interface in
group-Make-NTU-Hackathon 0:4233b7b10255 3340 * Node, or the `WebSocket` or `MozWebSocket` globals
group-Make-NTU-Hackathon 0:4233b7b10255 3341 * in the browser.
group-Make-NTU-Hackathon 0:4233b7b10255 3342 */
group-Make-NTU-Hackathon 0:4233b7b10255 3343
group-Make-NTU-Hackathon 0:4233b7b10255 3344 var WebSocket = _dereq_('ws');
group-Make-NTU-Hackathon 0:4233b7b10255 3345
group-Make-NTU-Hackathon 0:4233b7b10255 3346 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3347 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 3348 */
group-Make-NTU-Hackathon 0:4233b7b10255 3349
group-Make-NTU-Hackathon 0:4233b7b10255 3350 module.exports = WS;
group-Make-NTU-Hackathon 0:4233b7b10255 3351
group-Make-NTU-Hackathon 0:4233b7b10255 3352 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3353 * WebSocket transport constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 3354 *
group-Make-NTU-Hackathon 0:4233b7b10255 3355 * @api {Object} connection options
group-Make-NTU-Hackathon 0:4233b7b10255 3356 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3357 */
group-Make-NTU-Hackathon 0:4233b7b10255 3358
group-Make-NTU-Hackathon 0:4233b7b10255 3359 function WS(opts){
group-Make-NTU-Hackathon 0:4233b7b10255 3360 var forceBase64 = (opts && opts.forceBase64);
group-Make-NTU-Hackathon 0:4233b7b10255 3361 if (forceBase64) {
group-Make-NTU-Hackathon 0:4233b7b10255 3362 this.supportsBinary = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3363 }
group-Make-NTU-Hackathon 0:4233b7b10255 3364 Transport.call(this, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 3365 }
group-Make-NTU-Hackathon 0:4233b7b10255 3366
group-Make-NTU-Hackathon 0:4233b7b10255 3367 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3368 * Inherits from Transport.
group-Make-NTU-Hackathon 0:4233b7b10255 3369 */
group-Make-NTU-Hackathon 0:4233b7b10255 3370
group-Make-NTU-Hackathon 0:4233b7b10255 3371 inherit(WS, Transport);
group-Make-NTU-Hackathon 0:4233b7b10255 3372
group-Make-NTU-Hackathon 0:4233b7b10255 3373 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3374 * Transport name.
group-Make-NTU-Hackathon 0:4233b7b10255 3375 *
group-Make-NTU-Hackathon 0:4233b7b10255 3376 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3377 */
group-Make-NTU-Hackathon 0:4233b7b10255 3378
group-Make-NTU-Hackathon 0:4233b7b10255 3379 WS.prototype.name = 'websocket';
group-Make-NTU-Hackathon 0:4233b7b10255 3380
group-Make-NTU-Hackathon 0:4233b7b10255 3381 /*
group-Make-NTU-Hackathon 0:4233b7b10255 3382 * WebSockets support binary
group-Make-NTU-Hackathon 0:4233b7b10255 3383 */
group-Make-NTU-Hackathon 0:4233b7b10255 3384
group-Make-NTU-Hackathon 0:4233b7b10255 3385 WS.prototype.supportsBinary = true;
group-Make-NTU-Hackathon 0:4233b7b10255 3386
group-Make-NTU-Hackathon 0:4233b7b10255 3387 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3388 * Opens socket.
group-Make-NTU-Hackathon 0:4233b7b10255 3389 *
group-Make-NTU-Hackathon 0:4233b7b10255 3390 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3391 */
group-Make-NTU-Hackathon 0:4233b7b10255 3392
group-Make-NTU-Hackathon 0:4233b7b10255 3393 WS.prototype.doOpen = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3394 if (!this.check()) {
group-Make-NTU-Hackathon 0:4233b7b10255 3395 // let probe timeout
group-Make-NTU-Hackathon 0:4233b7b10255 3396 return;
group-Make-NTU-Hackathon 0:4233b7b10255 3397 }
group-Make-NTU-Hackathon 0:4233b7b10255 3398
group-Make-NTU-Hackathon 0:4233b7b10255 3399 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3400 var uri = this.uri();
group-Make-NTU-Hackathon 0:4233b7b10255 3401 var protocols = void(0);
group-Make-NTU-Hackathon 0:4233b7b10255 3402 var opts = { agent: this.agent };
group-Make-NTU-Hackathon 0:4233b7b10255 3403
group-Make-NTU-Hackathon 0:4233b7b10255 3404 // SSL options for Node.js client
group-Make-NTU-Hackathon 0:4233b7b10255 3405 opts.pfx = this.pfx;
group-Make-NTU-Hackathon 0:4233b7b10255 3406 opts.key = this.key;
group-Make-NTU-Hackathon 0:4233b7b10255 3407 opts.passphrase = this.passphrase;
group-Make-NTU-Hackathon 0:4233b7b10255 3408 opts.cert = this.cert;
group-Make-NTU-Hackathon 0:4233b7b10255 3409 opts.ca = this.ca;
group-Make-NTU-Hackathon 0:4233b7b10255 3410 opts.ciphers = this.ciphers;
group-Make-NTU-Hackathon 0:4233b7b10255 3411 opts.rejectUnauthorized = this.rejectUnauthorized;
group-Make-NTU-Hackathon 0:4233b7b10255 3412
group-Make-NTU-Hackathon 0:4233b7b10255 3413 this.ws = new WebSocket(uri, protocols, opts);
group-Make-NTU-Hackathon 0:4233b7b10255 3414
group-Make-NTU-Hackathon 0:4233b7b10255 3415 if (this.ws.binaryType === undefined) {
group-Make-NTU-Hackathon 0:4233b7b10255 3416 this.supportsBinary = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3417 }
group-Make-NTU-Hackathon 0:4233b7b10255 3418
group-Make-NTU-Hackathon 0:4233b7b10255 3419 this.ws.binaryType = 'arraybuffer';
group-Make-NTU-Hackathon 0:4233b7b10255 3420 this.addEventListeners();
group-Make-NTU-Hackathon 0:4233b7b10255 3421 };
group-Make-NTU-Hackathon 0:4233b7b10255 3422
group-Make-NTU-Hackathon 0:4233b7b10255 3423 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3424 * Adds event listeners to the socket
group-Make-NTU-Hackathon 0:4233b7b10255 3425 *
group-Make-NTU-Hackathon 0:4233b7b10255 3426 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3427 */
group-Make-NTU-Hackathon 0:4233b7b10255 3428
group-Make-NTU-Hackathon 0:4233b7b10255 3429 WS.prototype.addEventListeners = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3430 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3431
group-Make-NTU-Hackathon 0:4233b7b10255 3432 this.ws.onopen = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3433 self.onOpen();
group-Make-NTU-Hackathon 0:4233b7b10255 3434 };
group-Make-NTU-Hackathon 0:4233b7b10255 3435 this.ws.onclose = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3436 self.onClose();
group-Make-NTU-Hackathon 0:4233b7b10255 3437 };
group-Make-NTU-Hackathon 0:4233b7b10255 3438 this.ws.onmessage = function(ev){
group-Make-NTU-Hackathon 0:4233b7b10255 3439 self.onData(ev.data);
group-Make-NTU-Hackathon 0:4233b7b10255 3440 };
group-Make-NTU-Hackathon 0:4233b7b10255 3441 this.ws.onerror = function(e){
group-Make-NTU-Hackathon 0:4233b7b10255 3442 self.onError('websocket error', e);
group-Make-NTU-Hackathon 0:4233b7b10255 3443 };
group-Make-NTU-Hackathon 0:4233b7b10255 3444 };
group-Make-NTU-Hackathon 0:4233b7b10255 3445
group-Make-NTU-Hackathon 0:4233b7b10255 3446 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3447 * Override `onData` to use a timer on iOS.
group-Make-NTU-Hackathon 0:4233b7b10255 3448 * See: https://gist.github.com/mloughran/2052006
group-Make-NTU-Hackathon 0:4233b7b10255 3449 *
group-Make-NTU-Hackathon 0:4233b7b10255 3450 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3451 */
group-Make-NTU-Hackathon 0:4233b7b10255 3452
group-Make-NTU-Hackathon 0:4233b7b10255 3453 if ('undefined' != typeof navigator
group-Make-NTU-Hackathon 0:4233b7b10255 3454 && /iPad|iPhone|iPod/i.test(navigator.userAgent)) {
group-Make-NTU-Hackathon 0:4233b7b10255 3455 WS.prototype.onData = function(data){
group-Make-NTU-Hackathon 0:4233b7b10255 3456 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3457 setTimeout(function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3458 Transport.prototype.onData.call(self, data);
group-Make-NTU-Hackathon 0:4233b7b10255 3459 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 3460 };
group-Make-NTU-Hackathon 0:4233b7b10255 3461 }
group-Make-NTU-Hackathon 0:4233b7b10255 3462
group-Make-NTU-Hackathon 0:4233b7b10255 3463 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3464 * Writes data to socket.
group-Make-NTU-Hackathon 0:4233b7b10255 3465 *
group-Make-NTU-Hackathon 0:4233b7b10255 3466 * @param {Array} array of packets.
group-Make-NTU-Hackathon 0:4233b7b10255 3467 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3468 */
group-Make-NTU-Hackathon 0:4233b7b10255 3469
group-Make-NTU-Hackathon 0:4233b7b10255 3470 WS.prototype.write = function(packets){
group-Make-NTU-Hackathon 0:4233b7b10255 3471 var self = this;
group-Make-NTU-Hackathon 0:4233b7b10255 3472 this.writable = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3473 // encodePacket efficient as it uses WS framing
group-Make-NTU-Hackathon 0:4233b7b10255 3474 // no need for encodePayload
group-Make-NTU-Hackathon 0:4233b7b10255 3475 for (var i = 0, l = packets.length; i < l; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 3476 parser.encodePacket(packets[i], this.supportsBinary, function(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 3477 //Sometimes the websocket has already been closed but the browser didn't
group-Make-NTU-Hackathon 0:4233b7b10255 3478 //have a chance of informing us about it yet, in that case send will
group-Make-NTU-Hackathon 0:4233b7b10255 3479 //throw an error
group-Make-NTU-Hackathon 0:4233b7b10255 3480 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3481 self.ws.send(data);
group-Make-NTU-Hackathon 0:4233b7b10255 3482 } catch (e){
group-Make-NTU-Hackathon 0:4233b7b10255 3483 debug('websocket closed before onclose event');
group-Make-NTU-Hackathon 0:4233b7b10255 3484 }
group-Make-NTU-Hackathon 0:4233b7b10255 3485 });
group-Make-NTU-Hackathon 0:4233b7b10255 3486 }
group-Make-NTU-Hackathon 0:4233b7b10255 3487
group-Make-NTU-Hackathon 0:4233b7b10255 3488 function ondrain() {
group-Make-NTU-Hackathon 0:4233b7b10255 3489 self.writable = true;
group-Make-NTU-Hackathon 0:4233b7b10255 3490 self.emit('drain');
group-Make-NTU-Hackathon 0:4233b7b10255 3491 }
group-Make-NTU-Hackathon 0:4233b7b10255 3492 // fake drain
group-Make-NTU-Hackathon 0:4233b7b10255 3493 // defer to next tick to allow Socket to clear writeBuffer
group-Make-NTU-Hackathon 0:4233b7b10255 3494 setTimeout(ondrain, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 3495 };
group-Make-NTU-Hackathon 0:4233b7b10255 3496
group-Make-NTU-Hackathon 0:4233b7b10255 3497 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3498 * Called upon close
group-Make-NTU-Hackathon 0:4233b7b10255 3499 *
group-Make-NTU-Hackathon 0:4233b7b10255 3500 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3501 */
group-Make-NTU-Hackathon 0:4233b7b10255 3502
group-Make-NTU-Hackathon 0:4233b7b10255 3503 WS.prototype.onClose = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3504 Transport.prototype.onClose.call(this);
group-Make-NTU-Hackathon 0:4233b7b10255 3505 };
group-Make-NTU-Hackathon 0:4233b7b10255 3506
group-Make-NTU-Hackathon 0:4233b7b10255 3507 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3508 * Closes socket.
group-Make-NTU-Hackathon 0:4233b7b10255 3509 *
group-Make-NTU-Hackathon 0:4233b7b10255 3510 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3511 */
group-Make-NTU-Hackathon 0:4233b7b10255 3512
group-Make-NTU-Hackathon 0:4233b7b10255 3513 WS.prototype.doClose = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3514 if (typeof this.ws !== 'undefined') {
group-Make-NTU-Hackathon 0:4233b7b10255 3515 this.ws.close();
group-Make-NTU-Hackathon 0:4233b7b10255 3516 }
group-Make-NTU-Hackathon 0:4233b7b10255 3517 };
group-Make-NTU-Hackathon 0:4233b7b10255 3518
group-Make-NTU-Hackathon 0:4233b7b10255 3519 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3520 * Generates uri for connection.
group-Make-NTU-Hackathon 0:4233b7b10255 3521 *
group-Make-NTU-Hackathon 0:4233b7b10255 3522 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3523 */
group-Make-NTU-Hackathon 0:4233b7b10255 3524
group-Make-NTU-Hackathon 0:4233b7b10255 3525 WS.prototype.uri = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3526 var query = this.query || {};
group-Make-NTU-Hackathon 0:4233b7b10255 3527 var schema = this.secure ? 'wss' : 'ws';
group-Make-NTU-Hackathon 0:4233b7b10255 3528 var port = '';
group-Make-NTU-Hackathon 0:4233b7b10255 3529
group-Make-NTU-Hackathon 0:4233b7b10255 3530 // avoid port if default for schema
group-Make-NTU-Hackathon 0:4233b7b10255 3531 if (this.port && (('wss' == schema && this.port != 443)
group-Make-NTU-Hackathon 0:4233b7b10255 3532 || ('ws' == schema && this.port != 80))) {
group-Make-NTU-Hackathon 0:4233b7b10255 3533 port = ':' + this.port;
group-Make-NTU-Hackathon 0:4233b7b10255 3534 }
group-Make-NTU-Hackathon 0:4233b7b10255 3535
group-Make-NTU-Hackathon 0:4233b7b10255 3536 // append timestamp to URI
group-Make-NTU-Hackathon 0:4233b7b10255 3537 if (this.timestampRequests) {
group-Make-NTU-Hackathon 0:4233b7b10255 3538 query[this.timestampParam] = +new Date;
group-Make-NTU-Hackathon 0:4233b7b10255 3539 }
group-Make-NTU-Hackathon 0:4233b7b10255 3540
group-Make-NTU-Hackathon 0:4233b7b10255 3541 // communicate binary support capabilities
group-Make-NTU-Hackathon 0:4233b7b10255 3542 if (!this.supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 3543 query.b64 = 1;
group-Make-NTU-Hackathon 0:4233b7b10255 3544 }
group-Make-NTU-Hackathon 0:4233b7b10255 3545
group-Make-NTU-Hackathon 0:4233b7b10255 3546 query = parseqs.encode(query);
group-Make-NTU-Hackathon 0:4233b7b10255 3547
group-Make-NTU-Hackathon 0:4233b7b10255 3548 // prepend ? to query
group-Make-NTU-Hackathon 0:4233b7b10255 3549 if (query.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 3550 query = '?' + query;
group-Make-NTU-Hackathon 0:4233b7b10255 3551 }
group-Make-NTU-Hackathon 0:4233b7b10255 3552
group-Make-NTU-Hackathon 0:4233b7b10255 3553 return schema + '://' + this.hostname + port + this.path + query;
group-Make-NTU-Hackathon 0:4233b7b10255 3554 };
group-Make-NTU-Hackathon 0:4233b7b10255 3555
group-Make-NTU-Hackathon 0:4233b7b10255 3556 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3557 * Feature detection for WebSocket.
group-Make-NTU-Hackathon 0:4233b7b10255 3558 *
group-Make-NTU-Hackathon 0:4233b7b10255 3559 * @return {Boolean} whether this transport is available.
group-Make-NTU-Hackathon 0:4233b7b10255 3560 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3561 */
group-Make-NTU-Hackathon 0:4233b7b10255 3562
group-Make-NTU-Hackathon 0:4233b7b10255 3563 WS.prototype.check = function(){
group-Make-NTU-Hackathon 0:4233b7b10255 3564 return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name);
group-Make-NTU-Hackathon 0:4233b7b10255 3565 };
group-Make-NTU-Hackathon 0:4233b7b10255 3566
group-Make-NTU-Hackathon 0:4233b7b10255 3567 },{"../transport":14,"component-inherit":21,"debug":22,"engine.io-parser":25,"parseqs":35,"ws":37}],20:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3568 // browser shim for xmlhttprequest module
group-Make-NTU-Hackathon 0:4233b7b10255 3569 var hasCORS = _dereq_('has-cors');
group-Make-NTU-Hackathon 0:4233b7b10255 3570
group-Make-NTU-Hackathon 0:4233b7b10255 3571 module.exports = function(opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 3572 var xdomain = opts.xdomain;
group-Make-NTU-Hackathon 0:4233b7b10255 3573
group-Make-NTU-Hackathon 0:4233b7b10255 3574 // scheme must be same when usign XDomainRequest
group-Make-NTU-Hackathon 0:4233b7b10255 3575 // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
group-Make-NTU-Hackathon 0:4233b7b10255 3576 var xscheme = opts.xscheme;
group-Make-NTU-Hackathon 0:4233b7b10255 3577
group-Make-NTU-Hackathon 0:4233b7b10255 3578 // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default.
group-Make-NTU-Hackathon 0:4233b7b10255 3579 // https://github.com/Automattic/engine.io-client/pull/217
group-Make-NTU-Hackathon 0:4233b7b10255 3580 var enablesXDR = opts.enablesXDR;
group-Make-NTU-Hackathon 0:4233b7b10255 3581
group-Make-NTU-Hackathon 0:4233b7b10255 3582 // XMLHttpRequest can be disabled on IE
group-Make-NTU-Hackathon 0:4233b7b10255 3583 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3584 if ('undefined' != typeof XMLHttpRequest && (!xdomain || hasCORS)) {
group-Make-NTU-Hackathon 0:4233b7b10255 3585 return new XMLHttpRequest();
group-Make-NTU-Hackathon 0:4233b7b10255 3586 }
group-Make-NTU-Hackathon 0:4233b7b10255 3587 } catch (e) { }
group-Make-NTU-Hackathon 0:4233b7b10255 3588
group-Make-NTU-Hackathon 0:4233b7b10255 3589 // Use XDomainRequest for IE8 if enablesXDR is true
group-Make-NTU-Hackathon 0:4233b7b10255 3590 // because loading bar keeps flashing when using jsonp-polling
group-Make-NTU-Hackathon 0:4233b7b10255 3591 // https://github.com/yujiosaka/socke.io-ie8-loading-example
group-Make-NTU-Hackathon 0:4233b7b10255 3592 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3593 if ('undefined' != typeof XDomainRequest && !xscheme && enablesXDR) {
group-Make-NTU-Hackathon 0:4233b7b10255 3594 return new XDomainRequest();
group-Make-NTU-Hackathon 0:4233b7b10255 3595 }
group-Make-NTU-Hackathon 0:4233b7b10255 3596 } catch (e) { }
group-Make-NTU-Hackathon 0:4233b7b10255 3597
group-Make-NTU-Hackathon 0:4233b7b10255 3598 if (!xdomain) {
group-Make-NTU-Hackathon 0:4233b7b10255 3599 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3600 return new ActiveXObject('Microsoft.XMLHTTP');
group-Make-NTU-Hackathon 0:4233b7b10255 3601 } catch(e) { }
group-Make-NTU-Hackathon 0:4233b7b10255 3602 }
group-Make-NTU-Hackathon 0:4233b7b10255 3603 }
group-Make-NTU-Hackathon 0:4233b7b10255 3604
group-Make-NTU-Hackathon 0:4233b7b10255 3605 },{"has-cors":40}],21:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3606
group-Make-NTU-Hackathon 0:4233b7b10255 3607 module.exports = function(a, b){
group-Make-NTU-Hackathon 0:4233b7b10255 3608 var fn = function(){};
group-Make-NTU-Hackathon 0:4233b7b10255 3609 fn.prototype = b.prototype;
group-Make-NTU-Hackathon 0:4233b7b10255 3610 a.prototype = new fn;
group-Make-NTU-Hackathon 0:4233b7b10255 3611 a.prototype.constructor = a;
group-Make-NTU-Hackathon 0:4233b7b10255 3612 };
group-Make-NTU-Hackathon 0:4233b7b10255 3613 },{}],22:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3614
group-Make-NTU-Hackathon 0:4233b7b10255 3615 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3616 * This is the web browser implementation of `debug()`.
group-Make-NTU-Hackathon 0:4233b7b10255 3617 *
group-Make-NTU-Hackathon 0:4233b7b10255 3618 * Expose `debug()` as the module.
group-Make-NTU-Hackathon 0:4233b7b10255 3619 */
group-Make-NTU-Hackathon 0:4233b7b10255 3620
group-Make-NTU-Hackathon 0:4233b7b10255 3621 exports = module.exports = _dereq_('./debug');
group-Make-NTU-Hackathon 0:4233b7b10255 3622 exports.log = log;
group-Make-NTU-Hackathon 0:4233b7b10255 3623 exports.formatArgs = formatArgs;
group-Make-NTU-Hackathon 0:4233b7b10255 3624 exports.save = save;
group-Make-NTU-Hackathon 0:4233b7b10255 3625 exports.load = load;
group-Make-NTU-Hackathon 0:4233b7b10255 3626 exports.useColors = useColors;
group-Make-NTU-Hackathon 0:4233b7b10255 3627
group-Make-NTU-Hackathon 0:4233b7b10255 3628 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3629 * Colors.
group-Make-NTU-Hackathon 0:4233b7b10255 3630 */
group-Make-NTU-Hackathon 0:4233b7b10255 3631
group-Make-NTU-Hackathon 0:4233b7b10255 3632 exports.colors = [
group-Make-NTU-Hackathon 0:4233b7b10255 3633 'lightseagreen',
group-Make-NTU-Hackathon 0:4233b7b10255 3634 'forestgreen',
group-Make-NTU-Hackathon 0:4233b7b10255 3635 'goldenrod',
group-Make-NTU-Hackathon 0:4233b7b10255 3636 'dodgerblue',
group-Make-NTU-Hackathon 0:4233b7b10255 3637 'darkorchid',
group-Make-NTU-Hackathon 0:4233b7b10255 3638 'crimson'
group-Make-NTU-Hackathon 0:4233b7b10255 3639 ];
group-Make-NTU-Hackathon 0:4233b7b10255 3640
group-Make-NTU-Hackathon 0:4233b7b10255 3641 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3642 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
group-Make-NTU-Hackathon 0:4233b7b10255 3643 * and the Firebug extension (any Firefox version) are known
group-Make-NTU-Hackathon 0:4233b7b10255 3644 * to support "%c" CSS customizations.
group-Make-NTU-Hackathon 0:4233b7b10255 3645 *
group-Make-NTU-Hackathon 0:4233b7b10255 3646 * TODO: add a `localStorage` variable to explicitly enable/disable colors
group-Make-NTU-Hackathon 0:4233b7b10255 3647 */
group-Make-NTU-Hackathon 0:4233b7b10255 3648
group-Make-NTU-Hackathon 0:4233b7b10255 3649 function useColors() {
group-Make-NTU-Hackathon 0:4233b7b10255 3650 // is webkit? http://stackoverflow.com/a/16459606/376773
group-Make-NTU-Hackathon 0:4233b7b10255 3651 return ('WebkitAppearance' in document.documentElement.style) ||
group-Make-NTU-Hackathon 0:4233b7b10255 3652 // is firebug? http://stackoverflow.com/a/398120/376773
group-Make-NTU-Hackathon 0:4233b7b10255 3653 (window.console && (console.firebug || (console.exception && console.table))) ||
group-Make-NTU-Hackathon 0:4233b7b10255 3654 // is firefox >= v31?
group-Make-NTU-Hackathon 0:4233b7b10255 3655 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
group-Make-NTU-Hackathon 0:4233b7b10255 3656 (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
group-Make-NTU-Hackathon 0:4233b7b10255 3657 }
group-Make-NTU-Hackathon 0:4233b7b10255 3658
group-Make-NTU-Hackathon 0:4233b7b10255 3659 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3660 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
group-Make-NTU-Hackathon 0:4233b7b10255 3661 */
group-Make-NTU-Hackathon 0:4233b7b10255 3662
group-Make-NTU-Hackathon 0:4233b7b10255 3663 exports.formatters.j = function(v) {
group-Make-NTU-Hackathon 0:4233b7b10255 3664 return JSON.stringify(v);
group-Make-NTU-Hackathon 0:4233b7b10255 3665 };
group-Make-NTU-Hackathon 0:4233b7b10255 3666
group-Make-NTU-Hackathon 0:4233b7b10255 3667
group-Make-NTU-Hackathon 0:4233b7b10255 3668 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3669 * Colorize log arguments if enabled.
group-Make-NTU-Hackathon 0:4233b7b10255 3670 *
group-Make-NTU-Hackathon 0:4233b7b10255 3671 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3672 */
group-Make-NTU-Hackathon 0:4233b7b10255 3673
group-Make-NTU-Hackathon 0:4233b7b10255 3674 function formatArgs() {
group-Make-NTU-Hackathon 0:4233b7b10255 3675 var args = arguments;
group-Make-NTU-Hackathon 0:4233b7b10255 3676 var useColors = this.useColors;
group-Make-NTU-Hackathon 0:4233b7b10255 3677
group-Make-NTU-Hackathon 0:4233b7b10255 3678 args[0] = (useColors ? '%c' : '')
group-Make-NTU-Hackathon 0:4233b7b10255 3679 + this.namespace
group-Make-NTU-Hackathon 0:4233b7b10255 3680 + (useColors ? ' %c' : ' ')
group-Make-NTU-Hackathon 0:4233b7b10255 3681 + args[0]
group-Make-NTU-Hackathon 0:4233b7b10255 3682 + (useColors ? '%c ' : ' ')
group-Make-NTU-Hackathon 0:4233b7b10255 3683 + '+' + exports.humanize(this.diff);
group-Make-NTU-Hackathon 0:4233b7b10255 3684
group-Make-NTU-Hackathon 0:4233b7b10255 3685 if (!useColors) return args;
group-Make-NTU-Hackathon 0:4233b7b10255 3686
group-Make-NTU-Hackathon 0:4233b7b10255 3687 var c = 'color: ' + this.color;
group-Make-NTU-Hackathon 0:4233b7b10255 3688 args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
group-Make-NTU-Hackathon 0:4233b7b10255 3689
group-Make-NTU-Hackathon 0:4233b7b10255 3690 // the final "%c" is somewhat tricky, because there could be other
group-Make-NTU-Hackathon 0:4233b7b10255 3691 // arguments passed either before or after the %c, so we need to
group-Make-NTU-Hackathon 0:4233b7b10255 3692 // figure out the correct index to insert the CSS into
group-Make-NTU-Hackathon 0:4233b7b10255 3693 var index = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3694 var lastC = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3695 args[0].replace(/%[a-z%]/g, function(match) {
group-Make-NTU-Hackathon 0:4233b7b10255 3696 if ('%' === match) return;
group-Make-NTU-Hackathon 0:4233b7b10255 3697 index++;
group-Make-NTU-Hackathon 0:4233b7b10255 3698 if ('%c' === match) {
group-Make-NTU-Hackathon 0:4233b7b10255 3699 // we only are interested in the *last* %c
group-Make-NTU-Hackathon 0:4233b7b10255 3700 // (the user may have provided their own)
group-Make-NTU-Hackathon 0:4233b7b10255 3701 lastC = index;
group-Make-NTU-Hackathon 0:4233b7b10255 3702 }
group-Make-NTU-Hackathon 0:4233b7b10255 3703 });
group-Make-NTU-Hackathon 0:4233b7b10255 3704
group-Make-NTU-Hackathon 0:4233b7b10255 3705 args.splice(lastC, 0, c);
group-Make-NTU-Hackathon 0:4233b7b10255 3706 return args;
group-Make-NTU-Hackathon 0:4233b7b10255 3707 }
group-Make-NTU-Hackathon 0:4233b7b10255 3708
group-Make-NTU-Hackathon 0:4233b7b10255 3709 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3710 * Invokes `console.log()` when available.
group-Make-NTU-Hackathon 0:4233b7b10255 3711 * No-op when `console.log` is not a "function".
group-Make-NTU-Hackathon 0:4233b7b10255 3712 *
group-Make-NTU-Hackathon 0:4233b7b10255 3713 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3714 */
group-Make-NTU-Hackathon 0:4233b7b10255 3715
group-Make-NTU-Hackathon 0:4233b7b10255 3716 function log() {
group-Make-NTU-Hackathon 0:4233b7b10255 3717 // This hackery is required for IE8,
group-Make-NTU-Hackathon 0:4233b7b10255 3718 // where the `console.log` function doesn't have 'apply'
group-Make-NTU-Hackathon 0:4233b7b10255 3719 return 'object' == typeof console
group-Make-NTU-Hackathon 0:4233b7b10255 3720 && 'function' == typeof console.log
group-Make-NTU-Hackathon 0:4233b7b10255 3721 && Function.prototype.apply.call(console.log, console, arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 3722 }
group-Make-NTU-Hackathon 0:4233b7b10255 3723
group-Make-NTU-Hackathon 0:4233b7b10255 3724 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3725 * Save `namespaces`.
group-Make-NTU-Hackathon 0:4233b7b10255 3726 *
group-Make-NTU-Hackathon 0:4233b7b10255 3727 * @param {String} namespaces
group-Make-NTU-Hackathon 0:4233b7b10255 3728 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3729 */
group-Make-NTU-Hackathon 0:4233b7b10255 3730
group-Make-NTU-Hackathon 0:4233b7b10255 3731 function save(namespaces) {
group-Make-NTU-Hackathon 0:4233b7b10255 3732 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3733 if (null == namespaces) {
group-Make-NTU-Hackathon 0:4233b7b10255 3734 localStorage.removeItem('debug');
group-Make-NTU-Hackathon 0:4233b7b10255 3735 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3736 localStorage.debug = namespaces;
group-Make-NTU-Hackathon 0:4233b7b10255 3737 }
group-Make-NTU-Hackathon 0:4233b7b10255 3738 } catch(e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 3739 }
group-Make-NTU-Hackathon 0:4233b7b10255 3740
group-Make-NTU-Hackathon 0:4233b7b10255 3741 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3742 * Load `namespaces`.
group-Make-NTU-Hackathon 0:4233b7b10255 3743 *
group-Make-NTU-Hackathon 0:4233b7b10255 3744 * @return {String} returns the previously persisted debug modes
group-Make-NTU-Hackathon 0:4233b7b10255 3745 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3746 */
group-Make-NTU-Hackathon 0:4233b7b10255 3747
group-Make-NTU-Hackathon 0:4233b7b10255 3748 function load() {
group-Make-NTU-Hackathon 0:4233b7b10255 3749 var r;
group-Make-NTU-Hackathon 0:4233b7b10255 3750 try {
group-Make-NTU-Hackathon 0:4233b7b10255 3751 r = localStorage.debug;
group-Make-NTU-Hackathon 0:4233b7b10255 3752 } catch(e) {}
group-Make-NTU-Hackathon 0:4233b7b10255 3753 return r;
group-Make-NTU-Hackathon 0:4233b7b10255 3754 }
group-Make-NTU-Hackathon 0:4233b7b10255 3755
group-Make-NTU-Hackathon 0:4233b7b10255 3756 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3757 * Enable namespaces listed in `localStorage.debug` initially.
group-Make-NTU-Hackathon 0:4233b7b10255 3758 */
group-Make-NTU-Hackathon 0:4233b7b10255 3759
group-Make-NTU-Hackathon 0:4233b7b10255 3760 exports.enable(load());
group-Make-NTU-Hackathon 0:4233b7b10255 3761
group-Make-NTU-Hackathon 0:4233b7b10255 3762 },{"./debug":23}],23:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3763
group-Make-NTU-Hackathon 0:4233b7b10255 3764 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3765 * This is the common logic for both the Node.js and web browser
group-Make-NTU-Hackathon 0:4233b7b10255 3766 * implementations of `debug()`.
group-Make-NTU-Hackathon 0:4233b7b10255 3767 *
group-Make-NTU-Hackathon 0:4233b7b10255 3768 * Expose `debug()` as the module.
group-Make-NTU-Hackathon 0:4233b7b10255 3769 */
group-Make-NTU-Hackathon 0:4233b7b10255 3770
group-Make-NTU-Hackathon 0:4233b7b10255 3771 exports = module.exports = debug;
group-Make-NTU-Hackathon 0:4233b7b10255 3772 exports.coerce = coerce;
group-Make-NTU-Hackathon 0:4233b7b10255 3773 exports.disable = disable;
group-Make-NTU-Hackathon 0:4233b7b10255 3774 exports.enable = enable;
group-Make-NTU-Hackathon 0:4233b7b10255 3775 exports.enabled = enabled;
group-Make-NTU-Hackathon 0:4233b7b10255 3776 exports.humanize = _dereq_('ms');
group-Make-NTU-Hackathon 0:4233b7b10255 3777
group-Make-NTU-Hackathon 0:4233b7b10255 3778 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3779 * The currently active debug mode names, and names to skip.
group-Make-NTU-Hackathon 0:4233b7b10255 3780 */
group-Make-NTU-Hackathon 0:4233b7b10255 3781
group-Make-NTU-Hackathon 0:4233b7b10255 3782 exports.names = [];
group-Make-NTU-Hackathon 0:4233b7b10255 3783 exports.skips = [];
group-Make-NTU-Hackathon 0:4233b7b10255 3784
group-Make-NTU-Hackathon 0:4233b7b10255 3785 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3786 * Map of special "%n" handling functions, for the debug "format" argument.
group-Make-NTU-Hackathon 0:4233b7b10255 3787 *
group-Make-NTU-Hackathon 0:4233b7b10255 3788 * Valid key names are a single, lowercased letter, i.e. "n".
group-Make-NTU-Hackathon 0:4233b7b10255 3789 */
group-Make-NTU-Hackathon 0:4233b7b10255 3790
group-Make-NTU-Hackathon 0:4233b7b10255 3791 exports.formatters = {};
group-Make-NTU-Hackathon 0:4233b7b10255 3792
group-Make-NTU-Hackathon 0:4233b7b10255 3793 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3794 * Previously assigned color.
group-Make-NTU-Hackathon 0:4233b7b10255 3795 */
group-Make-NTU-Hackathon 0:4233b7b10255 3796
group-Make-NTU-Hackathon 0:4233b7b10255 3797 var prevColor = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3798
group-Make-NTU-Hackathon 0:4233b7b10255 3799 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3800 * Previous log timestamp.
group-Make-NTU-Hackathon 0:4233b7b10255 3801 */
group-Make-NTU-Hackathon 0:4233b7b10255 3802
group-Make-NTU-Hackathon 0:4233b7b10255 3803 var prevTime;
group-Make-NTU-Hackathon 0:4233b7b10255 3804
group-Make-NTU-Hackathon 0:4233b7b10255 3805 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3806 * Select a color.
group-Make-NTU-Hackathon 0:4233b7b10255 3807 *
group-Make-NTU-Hackathon 0:4233b7b10255 3808 * @return {Number}
group-Make-NTU-Hackathon 0:4233b7b10255 3809 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3810 */
group-Make-NTU-Hackathon 0:4233b7b10255 3811
group-Make-NTU-Hackathon 0:4233b7b10255 3812 function selectColor() {
group-Make-NTU-Hackathon 0:4233b7b10255 3813 return exports.colors[prevColor++ % exports.colors.length];
group-Make-NTU-Hackathon 0:4233b7b10255 3814 }
group-Make-NTU-Hackathon 0:4233b7b10255 3815
group-Make-NTU-Hackathon 0:4233b7b10255 3816 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3817 * Create a debugger with the given `namespace`.
group-Make-NTU-Hackathon 0:4233b7b10255 3818 *
group-Make-NTU-Hackathon 0:4233b7b10255 3819 * @param {String} namespace
group-Make-NTU-Hackathon 0:4233b7b10255 3820 * @return {Function}
group-Make-NTU-Hackathon 0:4233b7b10255 3821 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3822 */
group-Make-NTU-Hackathon 0:4233b7b10255 3823
group-Make-NTU-Hackathon 0:4233b7b10255 3824 function debug(namespace) {
group-Make-NTU-Hackathon 0:4233b7b10255 3825
group-Make-NTU-Hackathon 0:4233b7b10255 3826 // define the `disabled` version
group-Make-NTU-Hackathon 0:4233b7b10255 3827 function disabled() {
group-Make-NTU-Hackathon 0:4233b7b10255 3828 }
group-Make-NTU-Hackathon 0:4233b7b10255 3829 disabled.enabled = false;
group-Make-NTU-Hackathon 0:4233b7b10255 3830
group-Make-NTU-Hackathon 0:4233b7b10255 3831 // define the `enabled` version
group-Make-NTU-Hackathon 0:4233b7b10255 3832 function enabled() {
group-Make-NTU-Hackathon 0:4233b7b10255 3833
group-Make-NTU-Hackathon 0:4233b7b10255 3834 var self = enabled;
group-Make-NTU-Hackathon 0:4233b7b10255 3835
group-Make-NTU-Hackathon 0:4233b7b10255 3836 // set `diff` timestamp
group-Make-NTU-Hackathon 0:4233b7b10255 3837 var curr = +new Date();
group-Make-NTU-Hackathon 0:4233b7b10255 3838 var ms = curr - (prevTime || curr);
group-Make-NTU-Hackathon 0:4233b7b10255 3839 self.diff = ms;
group-Make-NTU-Hackathon 0:4233b7b10255 3840 self.prev = prevTime;
group-Make-NTU-Hackathon 0:4233b7b10255 3841 self.curr = curr;
group-Make-NTU-Hackathon 0:4233b7b10255 3842 prevTime = curr;
group-Make-NTU-Hackathon 0:4233b7b10255 3843
group-Make-NTU-Hackathon 0:4233b7b10255 3844 // add the `color` if not set
group-Make-NTU-Hackathon 0:4233b7b10255 3845 if (null == self.useColors) self.useColors = exports.useColors();
group-Make-NTU-Hackathon 0:4233b7b10255 3846 if (null == self.color && self.useColors) self.color = selectColor();
group-Make-NTU-Hackathon 0:4233b7b10255 3847
group-Make-NTU-Hackathon 0:4233b7b10255 3848 var args = Array.prototype.slice.call(arguments);
group-Make-NTU-Hackathon 0:4233b7b10255 3849
group-Make-NTU-Hackathon 0:4233b7b10255 3850 args[0] = exports.coerce(args[0]);
group-Make-NTU-Hackathon 0:4233b7b10255 3851
group-Make-NTU-Hackathon 0:4233b7b10255 3852 if ('string' !== typeof args[0]) {
group-Make-NTU-Hackathon 0:4233b7b10255 3853 // anything else let's inspect with %o
group-Make-NTU-Hackathon 0:4233b7b10255 3854 args = ['%o'].concat(args);
group-Make-NTU-Hackathon 0:4233b7b10255 3855 }
group-Make-NTU-Hackathon 0:4233b7b10255 3856
group-Make-NTU-Hackathon 0:4233b7b10255 3857 // apply any `formatters` transformations
group-Make-NTU-Hackathon 0:4233b7b10255 3858 var index = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 3859 args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
group-Make-NTU-Hackathon 0:4233b7b10255 3860 // if we encounter an escaped % then don't increase the array index
group-Make-NTU-Hackathon 0:4233b7b10255 3861 if (match === '%') return match;
group-Make-NTU-Hackathon 0:4233b7b10255 3862 index++;
group-Make-NTU-Hackathon 0:4233b7b10255 3863 var formatter = exports.formatters[format];
group-Make-NTU-Hackathon 0:4233b7b10255 3864 if ('function' === typeof formatter) {
group-Make-NTU-Hackathon 0:4233b7b10255 3865 var val = args[index];
group-Make-NTU-Hackathon 0:4233b7b10255 3866 match = formatter.call(self, val);
group-Make-NTU-Hackathon 0:4233b7b10255 3867
group-Make-NTU-Hackathon 0:4233b7b10255 3868 // now we need to remove `args[index]` since it's inlined in the `format`
group-Make-NTU-Hackathon 0:4233b7b10255 3869 args.splice(index, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 3870 index--;
group-Make-NTU-Hackathon 0:4233b7b10255 3871 }
group-Make-NTU-Hackathon 0:4233b7b10255 3872 return match;
group-Make-NTU-Hackathon 0:4233b7b10255 3873 });
group-Make-NTU-Hackathon 0:4233b7b10255 3874
group-Make-NTU-Hackathon 0:4233b7b10255 3875 if ('function' === typeof exports.formatArgs) {
group-Make-NTU-Hackathon 0:4233b7b10255 3876 args = exports.formatArgs.apply(self, args);
group-Make-NTU-Hackathon 0:4233b7b10255 3877 }
group-Make-NTU-Hackathon 0:4233b7b10255 3878 var logFn = enabled.log || exports.log || console.log.bind(console);
group-Make-NTU-Hackathon 0:4233b7b10255 3879 logFn.apply(self, args);
group-Make-NTU-Hackathon 0:4233b7b10255 3880 }
group-Make-NTU-Hackathon 0:4233b7b10255 3881 enabled.enabled = true;
group-Make-NTU-Hackathon 0:4233b7b10255 3882
group-Make-NTU-Hackathon 0:4233b7b10255 3883 var fn = exports.enabled(namespace) ? enabled : disabled;
group-Make-NTU-Hackathon 0:4233b7b10255 3884
group-Make-NTU-Hackathon 0:4233b7b10255 3885 fn.namespace = namespace;
group-Make-NTU-Hackathon 0:4233b7b10255 3886
group-Make-NTU-Hackathon 0:4233b7b10255 3887 return fn;
group-Make-NTU-Hackathon 0:4233b7b10255 3888 }
group-Make-NTU-Hackathon 0:4233b7b10255 3889
group-Make-NTU-Hackathon 0:4233b7b10255 3890 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3891 * Enables a debug mode by namespaces. This can include modes
group-Make-NTU-Hackathon 0:4233b7b10255 3892 * separated by a colon and wildcards.
group-Make-NTU-Hackathon 0:4233b7b10255 3893 *
group-Make-NTU-Hackathon 0:4233b7b10255 3894 * @param {String} namespaces
group-Make-NTU-Hackathon 0:4233b7b10255 3895 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3896 */
group-Make-NTU-Hackathon 0:4233b7b10255 3897
group-Make-NTU-Hackathon 0:4233b7b10255 3898 function enable(namespaces) {
group-Make-NTU-Hackathon 0:4233b7b10255 3899 exports.save(namespaces);
group-Make-NTU-Hackathon 0:4233b7b10255 3900
group-Make-NTU-Hackathon 0:4233b7b10255 3901 var split = (namespaces || '').split(/[\s,]+/);
group-Make-NTU-Hackathon 0:4233b7b10255 3902 var len = split.length;
group-Make-NTU-Hackathon 0:4233b7b10255 3903
group-Make-NTU-Hackathon 0:4233b7b10255 3904 for (var i = 0; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 3905 if (!split[i]) continue; // ignore empty strings
group-Make-NTU-Hackathon 0:4233b7b10255 3906 namespaces = split[i].replace(/\*/g, '.*?');
group-Make-NTU-Hackathon 0:4233b7b10255 3907 if (namespaces[0] === '-') {
group-Make-NTU-Hackathon 0:4233b7b10255 3908 exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
group-Make-NTU-Hackathon 0:4233b7b10255 3909 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 3910 exports.names.push(new RegExp('^' + namespaces + '$'));
group-Make-NTU-Hackathon 0:4233b7b10255 3911 }
group-Make-NTU-Hackathon 0:4233b7b10255 3912 }
group-Make-NTU-Hackathon 0:4233b7b10255 3913 }
group-Make-NTU-Hackathon 0:4233b7b10255 3914
group-Make-NTU-Hackathon 0:4233b7b10255 3915 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3916 * Disable debug output.
group-Make-NTU-Hackathon 0:4233b7b10255 3917 *
group-Make-NTU-Hackathon 0:4233b7b10255 3918 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3919 */
group-Make-NTU-Hackathon 0:4233b7b10255 3920
group-Make-NTU-Hackathon 0:4233b7b10255 3921 function disable() {
group-Make-NTU-Hackathon 0:4233b7b10255 3922 exports.enable('');
group-Make-NTU-Hackathon 0:4233b7b10255 3923 }
group-Make-NTU-Hackathon 0:4233b7b10255 3924
group-Make-NTU-Hackathon 0:4233b7b10255 3925 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3926 * Returns true if the given mode name is enabled, false otherwise.
group-Make-NTU-Hackathon 0:4233b7b10255 3927 *
group-Make-NTU-Hackathon 0:4233b7b10255 3928 * @param {String} name
group-Make-NTU-Hackathon 0:4233b7b10255 3929 * @return {Boolean}
group-Make-NTU-Hackathon 0:4233b7b10255 3930 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3931 */
group-Make-NTU-Hackathon 0:4233b7b10255 3932
group-Make-NTU-Hackathon 0:4233b7b10255 3933 function enabled(name) {
group-Make-NTU-Hackathon 0:4233b7b10255 3934 var i, len;
group-Make-NTU-Hackathon 0:4233b7b10255 3935 for (i = 0, len = exports.skips.length; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 3936 if (exports.skips[i].test(name)) {
group-Make-NTU-Hackathon 0:4233b7b10255 3937 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 3938 }
group-Make-NTU-Hackathon 0:4233b7b10255 3939 }
group-Make-NTU-Hackathon 0:4233b7b10255 3940 for (i = 0, len = exports.names.length; i < len; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 3941 if (exports.names[i].test(name)) {
group-Make-NTU-Hackathon 0:4233b7b10255 3942 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 3943 }
group-Make-NTU-Hackathon 0:4233b7b10255 3944 }
group-Make-NTU-Hackathon 0:4233b7b10255 3945 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 3946 }
group-Make-NTU-Hackathon 0:4233b7b10255 3947
group-Make-NTU-Hackathon 0:4233b7b10255 3948 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3949 * Coerce `val`.
group-Make-NTU-Hackathon 0:4233b7b10255 3950 *
group-Make-NTU-Hackathon 0:4233b7b10255 3951 * @param {Mixed} val
group-Make-NTU-Hackathon 0:4233b7b10255 3952 * @return {Mixed}
group-Make-NTU-Hackathon 0:4233b7b10255 3953 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3954 */
group-Make-NTU-Hackathon 0:4233b7b10255 3955
group-Make-NTU-Hackathon 0:4233b7b10255 3956 function coerce(val) {
group-Make-NTU-Hackathon 0:4233b7b10255 3957 if (val instanceof Error) return val.stack || val.message;
group-Make-NTU-Hackathon 0:4233b7b10255 3958 return val;
group-Make-NTU-Hackathon 0:4233b7b10255 3959 }
group-Make-NTU-Hackathon 0:4233b7b10255 3960
group-Make-NTU-Hackathon 0:4233b7b10255 3961 },{"ms":24}],24:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 3962 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3963 * Helpers.
group-Make-NTU-Hackathon 0:4233b7b10255 3964 */
group-Make-NTU-Hackathon 0:4233b7b10255 3965
group-Make-NTU-Hackathon 0:4233b7b10255 3966 var s = 1000;
group-Make-NTU-Hackathon 0:4233b7b10255 3967 var m = s * 60;
group-Make-NTU-Hackathon 0:4233b7b10255 3968 var h = m * 60;
group-Make-NTU-Hackathon 0:4233b7b10255 3969 var d = h * 24;
group-Make-NTU-Hackathon 0:4233b7b10255 3970 var y = d * 365.25;
group-Make-NTU-Hackathon 0:4233b7b10255 3971
group-Make-NTU-Hackathon 0:4233b7b10255 3972 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3973 * Parse or format the given `val`.
group-Make-NTU-Hackathon 0:4233b7b10255 3974 *
group-Make-NTU-Hackathon 0:4233b7b10255 3975 * Options:
group-Make-NTU-Hackathon 0:4233b7b10255 3976 *
group-Make-NTU-Hackathon 0:4233b7b10255 3977 * - `long` verbose formatting [false]
group-Make-NTU-Hackathon 0:4233b7b10255 3978 *
group-Make-NTU-Hackathon 0:4233b7b10255 3979 * @param {String|Number} val
group-Make-NTU-Hackathon 0:4233b7b10255 3980 * @param {Object} options
group-Make-NTU-Hackathon 0:4233b7b10255 3981 * @return {String|Number}
group-Make-NTU-Hackathon 0:4233b7b10255 3982 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 3983 */
group-Make-NTU-Hackathon 0:4233b7b10255 3984
group-Make-NTU-Hackathon 0:4233b7b10255 3985 module.exports = function(val, options){
group-Make-NTU-Hackathon 0:4233b7b10255 3986 options = options || {};
group-Make-NTU-Hackathon 0:4233b7b10255 3987 if ('string' == typeof val) return parse(val);
group-Make-NTU-Hackathon 0:4233b7b10255 3988 return options.long
group-Make-NTU-Hackathon 0:4233b7b10255 3989 ? long(val)
group-Make-NTU-Hackathon 0:4233b7b10255 3990 : short(val);
group-Make-NTU-Hackathon 0:4233b7b10255 3991 };
group-Make-NTU-Hackathon 0:4233b7b10255 3992
group-Make-NTU-Hackathon 0:4233b7b10255 3993 /**
group-Make-NTU-Hackathon 0:4233b7b10255 3994 * Parse the given `str` and return milliseconds.
group-Make-NTU-Hackathon 0:4233b7b10255 3995 *
group-Make-NTU-Hackathon 0:4233b7b10255 3996 * @param {String} str
group-Make-NTU-Hackathon 0:4233b7b10255 3997 * @return {Number}
group-Make-NTU-Hackathon 0:4233b7b10255 3998 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 3999 */
group-Make-NTU-Hackathon 0:4233b7b10255 4000
group-Make-NTU-Hackathon 0:4233b7b10255 4001 function parse(str) {
group-Make-NTU-Hackathon 0:4233b7b10255 4002 var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
group-Make-NTU-Hackathon 0:4233b7b10255 4003 if (!match) return;
group-Make-NTU-Hackathon 0:4233b7b10255 4004 var n = parseFloat(match[1]);
group-Make-NTU-Hackathon 0:4233b7b10255 4005 var type = (match[2] || 'ms').toLowerCase();
group-Make-NTU-Hackathon 0:4233b7b10255 4006 switch (type) {
group-Make-NTU-Hackathon 0:4233b7b10255 4007 case 'years':
group-Make-NTU-Hackathon 0:4233b7b10255 4008 case 'year':
group-Make-NTU-Hackathon 0:4233b7b10255 4009 case 'y':
group-Make-NTU-Hackathon 0:4233b7b10255 4010 return n * y;
group-Make-NTU-Hackathon 0:4233b7b10255 4011 case 'days':
group-Make-NTU-Hackathon 0:4233b7b10255 4012 case 'day':
group-Make-NTU-Hackathon 0:4233b7b10255 4013 case 'd':
group-Make-NTU-Hackathon 0:4233b7b10255 4014 return n * d;
group-Make-NTU-Hackathon 0:4233b7b10255 4015 case 'hours':
group-Make-NTU-Hackathon 0:4233b7b10255 4016 case 'hour':
group-Make-NTU-Hackathon 0:4233b7b10255 4017 case 'h':
group-Make-NTU-Hackathon 0:4233b7b10255 4018 return n * h;
group-Make-NTU-Hackathon 0:4233b7b10255 4019 case 'minutes':
group-Make-NTU-Hackathon 0:4233b7b10255 4020 case 'minute':
group-Make-NTU-Hackathon 0:4233b7b10255 4021 case 'm':
group-Make-NTU-Hackathon 0:4233b7b10255 4022 return n * m;
group-Make-NTU-Hackathon 0:4233b7b10255 4023 case 'seconds':
group-Make-NTU-Hackathon 0:4233b7b10255 4024 case 'second':
group-Make-NTU-Hackathon 0:4233b7b10255 4025 case 's':
group-Make-NTU-Hackathon 0:4233b7b10255 4026 return n * s;
group-Make-NTU-Hackathon 0:4233b7b10255 4027 case 'ms':
group-Make-NTU-Hackathon 0:4233b7b10255 4028 return n;
group-Make-NTU-Hackathon 0:4233b7b10255 4029 }
group-Make-NTU-Hackathon 0:4233b7b10255 4030 }
group-Make-NTU-Hackathon 0:4233b7b10255 4031
group-Make-NTU-Hackathon 0:4233b7b10255 4032 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4033 * Short format for `ms`.
group-Make-NTU-Hackathon 0:4233b7b10255 4034 *
group-Make-NTU-Hackathon 0:4233b7b10255 4035 * @param {Number} ms
group-Make-NTU-Hackathon 0:4233b7b10255 4036 * @return {String}
group-Make-NTU-Hackathon 0:4233b7b10255 4037 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4038 */
group-Make-NTU-Hackathon 0:4233b7b10255 4039
group-Make-NTU-Hackathon 0:4233b7b10255 4040 function short(ms) {
group-Make-NTU-Hackathon 0:4233b7b10255 4041 if (ms >= d) return Math.round(ms / d) + 'd';
group-Make-NTU-Hackathon 0:4233b7b10255 4042 if (ms >= h) return Math.round(ms / h) + 'h';
group-Make-NTU-Hackathon 0:4233b7b10255 4043 if (ms >= m) return Math.round(ms / m) + 'm';
group-Make-NTU-Hackathon 0:4233b7b10255 4044 if (ms >= s) return Math.round(ms / s) + 's';
group-Make-NTU-Hackathon 0:4233b7b10255 4045 return ms + 'ms';
group-Make-NTU-Hackathon 0:4233b7b10255 4046 }
group-Make-NTU-Hackathon 0:4233b7b10255 4047
group-Make-NTU-Hackathon 0:4233b7b10255 4048 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4049 * Long format for `ms`.
group-Make-NTU-Hackathon 0:4233b7b10255 4050 *
group-Make-NTU-Hackathon 0:4233b7b10255 4051 * @param {Number} ms
group-Make-NTU-Hackathon 0:4233b7b10255 4052 * @return {String}
group-Make-NTU-Hackathon 0:4233b7b10255 4053 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4054 */
group-Make-NTU-Hackathon 0:4233b7b10255 4055
group-Make-NTU-Hackathon 0:4233b7b10255 4056 function long(ms) {
group-Make-NTU-Hackathon 0:4233b7b10255 4057 return plural(ms, d, 'day')
group-Make-NTU-Hackathon 0:4233b7b10255 4058 || plural(ms, h, 'hour')
group-Make-NTU-Hackathon 0:4233b7b10255 4059 || plural(ms, m, 'minute')
group-Make-NTU-Hackathon 0:4233b7b10255 4060 || plural(ms, s, 'second')
group-Make-NTU-Hackathon 0:4233b7b10255 4061 || ms + ' ms';
group-Make-NTU-Hackathon 0:4233b7b10255 4062 }
group-Make-NTU-Hackathon 0:4233b7b10255 4063
group-Make-NTU-Hackathon 0:4233b7b10255 4064 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4065 * Pluralization helper.
group-Make-NTU-Hackathon 0:4233b7b10255 4066 */
group-Make-NTU-Hackathon 0:4233b7b10255 4067
group-Make-NTU-Hackathon 0:4233b7b10255 4068 function plural(ms, n, name) {
group-Make-NTU-Hackathon 0:4233b7b10255 4069 if (ms < n) return;
group-Make-NTU-Hackathon 0:4233b7b10255 4070 if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
group-Make-NTU-Hackathon 0:4233b7b10255 4071 return Math.ceil(ms / n) + ' ' + name + 's';
group-Make-NTU-Hackathon 0:4233b7b10255 4072 }
group-Make-NTU-Hackathon 0:4233b7b10255 4073
group-Make-NTU-Hackathon 0:4233b7b10255 4074 },{}],25:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4075 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 4076 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4077 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 4078 */
group-Make-NTU-Hackathon 0:4233b7b10255 4079
group-Make-NTU-Hackathon 0:4233b7b10255 4080 var keys = _dereq_('./keys');
group-Make-NTU-Hackathon 0:4233b7b10255 4081 var hasBinary = _dereq_('has-binary');
group-Make-NTU-Hackathon 0:4233b7b10255 4082 var sliceBuffer = _dereq_('arraybuffer.slice');
group-Make-NTU-Hackathon 0:4233b7b10255 4083 var base64encoder = _dereq_('base64-arraybuffer');
group-Make-NTU-Hackathon 0:4233b7b10255 4084 var after = _dereq_('after');
group-Make-NTU-Hackathon 0:4233b7b10255 4085 var utf8 = _dereq_('utf8');
group-Make-NTU-Hackathon 0:4233b7b10255 4086
group-Make-NTU-Hackathon 0:4233b7b10255 4087 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4088 * Check if we are running an android browser. That requires us to use
group-Make-NTU-Hackathon 0:4233b7b10255 4089 * ArrayBuffer with polling transports...
group-Make-NTU-Hackathon 0:4233b7b10255 4090 *
group-Make-NTU-Hackathon 0:4233b7b10255 4091 * http://ghinda.net/jpeg-blob-ajax-android/
group-Make-NTU-Hackathon 0:4233b7b10255 4092 */
group-Make-NTU-Hackathon 0:4233b7b10255 4093
group-Make-NTU-Hackathon 0:4233b7b10255 4094 var isAndroid = navigator.userAgent.match(/Android/i);
group-Make-NTU-Hackathon 0:4233b7b10255 4095
group-Make-NTU-Hackathon 0:4233b7b10255 4096 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4097 * Check if we are running in PhantomJS.
group-Make-NTU-Hackathon 0:4233b7b10255 4098 * Uploading a Blob with PhantomJS does not work correctly, as reported here:
group-Make-NTU-Hackathon 0:4233b7b10255 4099 * https://github.com/ariya/phantomjs/issues/11395
group-Make-NTU-Hackathon 0:4233b7b10255 4100 * @type boolean
group-Make-NTU-Hackathon 0:4233b7b10255 4101 */
group-Make-NTU-Hackathon 0:4233b7b10255 4102 var isPhantomJS = /PhantomJS/i.test(navigator.userAgent);
group-Make-NTU-Hackathon 0:4233b7b10255 4103
group-Make-NTU-Hackathon 0:4233b7b10255 4104 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4105 * When true, avoids using Blobs to encode payloads.
group-Make-NTU-Hackathon 0:4233b7b10255 4106 * @type boolean
group-Make-NTU-Hackathon 0:4233b7b10255 4107 */
group-Make-NTU-Hackathon 0:4233b7b10255 4108 var dontSendBlobs = isAndroid || isPhantomJS;
group-Make-NTU-Hackathon 0:4233b7b10255 4109
group-Make-NTU-Hackathon 0:4233b7b10255 4110 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4111 * Current protocol version.
group-Make-NTU-Hackathon 0:4233b7b10255 4112 */
group-Make-NTU-Hackathon 0:4233b7b10255 4113
group-Make-NTU-Hackathon 0:4233b7b10255 4114 exports.protocol = 3;
group-Make-NTU-Hackathon 0:4233b7b10255 4115
group-Make-NTU-Hackathon 0:4233b7b10255 4116 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4117 * Packet types.
group-Make-NTU-Hackathon 0:4233b7b10255 4118 */
group-Make-NTU-Hackathon 0:4233b7b10255 4119
group-Make-NTU-Hackathon 0:4233b7b10255 4120 var packets = exports.packets = {
group-Make-NTU-Hackathon 0:4233b7b10255 4121 open: 0 // non-ws
group-Make-NTU-Hackathon 0:4233b7b10255 4122 , close: 1 // non-ws
group-Make-NTU-Hackathon 0:4233b7b10255 4123 , ping: 2
group-Make-NTU-Hackathon 0:4233b7b10255 4124 , pong: 3
group-Make-NTU-Hackathon 0:4233b7b10255 4125 , message: 4
group-Make-NTU-Hackathon 0:4233b7b10255 4126 , upgrade: 5
group-Make-NTU-Hackathon 0:4233b7b10255 4127 , noop: 6
group-Make-NTU-Hackathon 0:4233b7b10255 4128 };
group-Make-NTU-Hackathon 0:4233b7b10255 4129
group-Make-NTU-Hackathon 0:4233b7b10255 4130 var packetslist = keys(packets);
group-Make-NTU-Hackathon 0:4233b7b10255 4131
group-Make-NTU-Hackathon 0:4233b7b10255 4132 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4133 * Premade error packet.
group-Make-NTU-Hackathon 0:4233b7b10255 4134 */
group-Make-NTU-Hackathon 0:4233b7b10255 4135
group-Make-NTU-Hackathon 0:4233b7b10255 4136 var err = { type: 'error', data: 'parser error' };
group-Make-NTU-Hackathon 0:4233b7b10255 4137
group-Make-NTU-Hackathon 0:4233b7b10255 4138 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4139 * Create a blob api even for blob builder when vendor prefixes exist
group-Make-NTU-Hackathon 0:4233b7b10255 4140 */
group-Make-NTU-Hackathon 0:4233b7b10255 4141
group-Make-NTU-Hackathon 0:4233b7b10255 4142 var Blob = _dereq_('blob');
group-Make-NTU-Hackathon 0:4233b7b10255 4143
group-Make-NTU-Hackathon 0:4233b7b10255 4144 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4145 * Encodes a packet.
group-Make-NTU-Hackathon 0:4233b7b10255 4146 *
group-Make-NTU-Hackathon 0:4233b7b10255 4147 * <packet type id> [ <data> ]
group-Make-NTU-Hackathon 0:4233b7b10255 4148 *
group-Make-NTU-Hackathon 0:4233b7b10255 4149 * Example:
group-Make-NTU-Hackathon 0:4233b7b10255 4150 *
group-Make-NTU-Hackathon 0:4233b7b10255 4151 * 5hello world
group-Make-NTU-Hackathon 0:4233b7b10255 4152 * 3
group-Make-NTU-Hackathon 0:4233b7b10255 4153 * 4
group-Make-NTU-Hackathon 0:4233b7b10255 4154 *
group-Make-NTU-Hackathon 0:4233b7b10255 4155 * Binary is encoded in an identical principle
group-Make-NTU-Hackathon 0:4233b7b10255 4156 *
group-Make-NTU-Hackathon 0:4233b7b10255 4157 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4158 */
group-Make-NTU-Hackathon 0:4233b7b10255 4159
group-Make-NTU-Hackathon 0:4233b7b10255 4160 exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4161 if ('function' == typeof supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 4162 callback = supportsBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 4163 supportsBinary = false;
group-Make-NTU-Hackathon 0:4233b7b10255 4164 }
group-Make-NTU-Hackathon 0:4233b7b10255 4165
group-Make-NTU-Hackathon 0:4233b7b10255 4166 if ('function' == typeof utf8encode) {
group-Make-NTU-Hackathon 0:4233b7b10255 4167 callback = utf8encode;
group-Make-NTU-Hackathon 0:4233b7b10255 4168 utf8encode = null;
group-Make-NTU-Hackathon 0:4233b7b10255 4169 }
group-Make-NTU-Hackathon 0:4233b7b10255 4170
group-Make-NTU-Hackathon 0:4233b7b10255 4171 var data = (packet.data === undefined)
group-Make-NTU-Hackathon 0:4233b7b10255 4172 ? undefined
group-Make-NTU-Hackathon 0:4233b7b10255 4173 : packet.data.buffer || packet.data;
group-Make-NTU-Hackathon 0:4233b7b10255 4174
group-Make-NTU-Hackathon 0:4233b7b10255 4175 if (global.ArrayBuffer && data instanceof ArrayBuffer) {
group-Make-NTU-Hackathon 0:4233b7b10255 4176 return encodeArrayBuffer(packet, supportsBinary, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4177 } else if (Blob && data instanceof global.Blob) {
group-Make-NTU-Hackathon 0:4233b7b10255 4178 return encodeBlob(packet, supportsBinary, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4179 }
group-Make-NTU-Hackathon 0:4233b7b10255 4180
group-Make-NTU-Hackathon 0:4233b7b10255 4181 // might be an object with { base64: true, data: dataAsBase64String }
group-Make-NTU-Hackathon 0:4233b7b10255 4182 if (data && data.base64) {
group-Make-NTU-Hackathon 0:4233b7b10255 4183 return encodeBase64Object(packet, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4184 }
group-Make-NTU-Hackathon 0:4233b7b10255 4185
group-Make-NTU-Hackathon 0:4233b7b10255 4186 // Sending data as a utf-8 string
group-Make-NTU-Hackathon 0:4233b7b10255 4187 var encoded = packets[packet.type];
group-Make-NTU-Hackathon 0:4233b7b10255 4188
group-Make-NTU-Hackathon 0:4233b7b10255 4189 // data fragment is optional
group-Make-NTU-Hackathon 0:4233b7b10255 4190 if (undefined !== packet.data) {
group-Make-NTU-Hackathon 0:4233b7b10255 4191 encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 4192 }
group-Make-NTU-Hackathon 0:4233b7b10255 4193
group-Make-NTU-Hackathon 0:4233b7b10255 4194 return callback('' + encoded);
group-Make-NTU-Hackathon 0:4233b7b10255 4195
group-Make-NTU-Hackathon 0:4233b7b10255 4196 };
group-Make-NTU-Hackathon 0:4233b7b10255 4197
group-Make-NTU-Hackathon 0:4233b7b10255 4198 function encodeBase64Object(packet, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4199 // packet data is an object { base64: true, data: dataAsBase64String }
group-Make-NTU-Hackathon 0:4233b7b10255 4200 var message = 'b' + exports.packets[packet.type] + packet.data.data;
group-Make-NTU-Hackathon 0:4233b7b10255 4201 return callback(message);
group-Make-NTU-Hackathon 0:4233b7b10255 4202 }
group-Make-NTU-Hackathon 0:4233b7b10255 4203
group-Make-NTU-Hackathon 0:4233b7b10255 4204 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4205 * Encode packet helpers for binary types
group-Make-NTU-Hackathon 0:4233b7b10255 4206 */
group-Make-NTU-Hackathon 0:4233b7b10255 4207
group-Make-NTU-Hackathon 0:4233b7b10255 4208 function encodeArrayBuffer(packet, supportsBinary, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4209 if (!supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 4210 return exports.encodeBase64Packet(packet, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4211 }
group-Make-NTU-Hackathon 0:4233b7b10255 4212
group-Make-NTU-Hackathon 0:4233b7b10255 4213 var data = packet.data;
group-Make-NTU-Hackathon 0:4233b7b10255 4214 var contentArray = new Uint8Array(data);
group-Make-NTU-Hackathon 0:4233b7b10255 4215 var resultBuffer = new Uint8Array(1 + data.byteLength);
group-Make-NTU-Hackathon 0:4233b7b10255 4216
group-Make-NTU-Hackathon 0:4233b7b10255 4217 resultBuffer[0] = packets[packet.type];
group-Make-NTU-Hackathon 0:4233b7b10255 4218 for (var i = 0; i < contentArray.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4219 resultBuffer[i+1] = contentArray[i];
group-Make-NTU-Hackathon 0:4233b7b10255 4220 }
group-Make-NTU-Hackathon 0:4233b7b10255 4221
group-Make-NTU-Hackathon 0:4233b7b10255 4222 return callback(resultBuffer.buffer);
group-Make-NTU-Hackathon 0:4233b7b10255 4223 }
group-Make-NTU-Hackathon 0:4233b7b10255 4224
group-Make-NTU-Hackathon 0:4233b7b10255 4225 function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4226 if (!supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 4227 return exports.encodeBase64Packet(packet, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4228 }
group-Make-NTU-Hackathon 0:4233b7b10255 4229
group-Make-NTU-Hackathon 0:4233b7b10255 4230 var fr = new FileReader();
group-Make-NTU-Hackathon 0:4233b7b10255 4231 fr.onload = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 4232 packet.data = fr.result;
group-Make-NTU-Hackathon 0:4233b7b10255 4233 exports.encodePacket(packet, supportsBinary, true, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4234 };
group-Make-NTU-Hackathon 0:4233b7b10255 4235 return fr.readAsArrayBuffer(packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 4236 }
group-Make-NTU-Hackathon 0:4233b7b10255 4237
group-Make-NTU-Hackathon 0:4233b7b10255 4238 function encodeBlob(packet, supportsBinary, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4239 if (!supportsBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 4240 return exports.encodeBase64Packet(packet, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4241 }
group-Make-NTU-Hackathon 0:4233b7b10255 4242
group-Make-NTU-Hackathon 0:4233b7b10255 4243 if (dontSendBlobs) {
group-Make-NTU-Hackathon 0:4233b7b10255 4244 return encodeBlobAsArrayBuffer(packet, supportsBinary, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4245 }
group-Make-NTU-Hackathon 0:4233b7b10255 4246
group-Make-NTU-Hackathon 0:4233b7b10255 4247 var length = new Uint8Array(1);
group-Make-NTU-Hackathon 0:4233b7b10255 4248 length[0] = packets[packet.type];
group-Make-NTU-Hackathon 0:4233b7b10255 4249 var blob = new Blob([length.buffer, packet.data]);
group-Make-NTU-Hackathon 0:4233b7b10255 4250
group-Make-NTU-Hackathon 0:4233b7b10255 4251 return callback(blob);
group-Make-NTU-Hackathon 0:4233b7b10255 4252 }
group-Make-NTU-Hackathon 0:4233b7b10255 4253
group-Make-NTU-Hackathon 0:4233b7b10255 4254 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4255 * Encodes a packet with binary data in a base64 string
group-Make-NTU-Hackathon 0:4233b7b10255 4256 *
group-Make-NTU-Hackathon 0:4233b7b10255 4257 * @param {Object} packet, has `type` and `data`
group-Make-NTU-Hackathon 0:4233b7b10255 4258 * @return {String} base64 encoded message
group-Make-NTU-Hackathon 0:4233b7b10255 4259 */
group-Make-NTU-Hackathon 0:4233b7b10255 4260
group-Make-NTU-Hackathon 0:4233b7b10255 4261 exports.encodeBase64Packet = function(packet, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4262 var message = 'b' + exports.packets[packet.type];
group-Make-NTU-Hackathon 0:4233b7b10255 4263 if (Blob && packet.data instanceof Blob) {
group-Make-NTU-Hackathon 0:4233b7b10255 4264 var fr = new FileReader();
group-Make-NTU-Hackathon 0:4233b7b10255 4265 fr.onload = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 4266 var b64 = fr.result.split(',')[1];
group-Make-NTU-Hackathon 0:4233b7b10255 4267 callback(message + b64);
group-Make-NTU-Hackathon 0:4233b7b10255 4268 };
group-Make-NTU-Hackathon 0:4233b7b10255 4269 return fr.readAsDataURL(packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 4270 }
group-Make-NTU-Hackathon 0:4233b7b10255 4271
group-Make-NTU-Hackathon 0:4233b7b10255 4272 var b64data;
group-Make-NTU-Hackathon 0:4233b7b10255 4273 try {
group-Make-NTU-Hackathon 0:4233b7b10255 4274 b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data));
group-Make-NTU-Hackathon 0:4233b7b10255 4275 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 4276 // iPhone Safari doesn't let you apply with typed arrays
group-Make-NTU-Hackathon 0:4233b7b10255 4277 var typed = new Uint8Array(packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 4278 var basic = new Array(typed.length);
group-Make-NTU-Hackathon 0:4233b7b10255 4279 for (var i = 0; i < typed.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4280 basic[i] = typed[i];
group-Make-NTU-Hackathon 0:4233b7b10255 4281 }
group-Make-NTU-Hackathon 0:4233b7b10255 4282 b64data = String.fromCharCode.apply(null, basic);
group-Make-NTU-Hackathon 0:4233b7b10255 4283 }
group-Make-NTU-Hackathon 0:4233b7b10255 4284 message += global.btoa(b64data);
group-Make-NTU-Hackathon 0:4233b7b10255 4285 return callback(message);
group-Make-NTU-Hackathon 0:4233b7b10255 4286 };
group-Make-NTU-Hackathon 0:4233b7b10255 4287
group-Make-NTU-Hackathon 0:4233b7b10255 4288 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4289 * Decodes a packet. Changes format to Blob if requested.
group-Make-NTU-Hackathon 0:4233b7b10255 4290 *
group-Make-NTU-Hackathon 0:4233b7b10255 4291 * @return {Object} with `type` and `data` (if any)
group-Make-NTU-Hackathon 0:4233b7b10255 4292 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4293 */
group-Make-NTU-Hackathon 0:4233b7b10255 4294
group-Make-NTU-Hackathon 0:4233b7b10255 4295 exports.decodePacket = function (data, binaryType, utf8decode) {
group-Make-NTU-Hackathon 0:4233b7b10255 4296 // String data
group-Make-NTU-Hackathon 0:4233b7b10255 4297 if (typeof data == 'string' || data === undefined) {
group-Make-NTU-Hackathon 0:4233b7b10255 4298 if (data.charAt(0) == 'b') {
group-Make-NTU-Hackathon 0:4233b7b10255 4299 return exports.decodeBase64Packet(data.substr(1), binaryType);
group-Make-NTU-Hackathon 0:4233b7b10255 4300 }
group-Make-NTU-Hackathon 0:4233b7b10255 4301
group-Make-NTU-Hackathon 0:4233b7b10255 4302 if (utf8decode) {
group-Make-NTU-Hackathon 0:4233b7b10255 4303 try {
group-Make-NTU-Hackathon 0:4233b7b10255 4304 data = utf8.decode(data);
group-Make-NTU-Hackathon 0:4233b7b10255 4305 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 4306 return err;
group-Make-NTU-Hackathon 0:4233b7b10255 4307 }
group-Make-NTU-Hackathon 0:4233b7b10255 4308 }
group-Make-NTU-Hackathon 0:4233b7b10255 4309 var type = data.charAt(0);
group-Make-NTU-Hackathon 0:4233b7b10255 4310
group-Make-NTU-Hackathon 0:4233b7b10255 4311 if (Number(type) != type || !packetslist[type]) {
group-Make-NTU-Hackathon 0:4233b7b10255 4312 return err;
group-Make-NTU-Hackathon 0:4233b7b10255 4313 }
group-Make-NTU-Hackathon 0:4233b7b10255 4314
group-Make-NTU-Hackathon 0:4233b7b10255 4315 if (data.length > 1) {
group-Make-NTU-Hackathon 0:4233b7b10255 4316 return { type: packetslist[type], data: data.substring(1) };
group-Make-NTU-Hackathon 0:4233b7b10255 4317 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4318 return { type: packetslist[type] };
group-Make-NTU-Hackathon 0:4233b7b10255 4319 }
group-Make-NTU-Hackathon 0:4233b7b10255 4320 }
group-Make-NTU-Hackathon 0:4233b7b10255 4321
group-Make-NTU-Hackathon 0:4233b7b10255 4322 var asArray = new Uint8Array(data);
group-Make-NTU-Hackathon 0:4233b7b10255 4323 var type = asArray[0];
group-Make-NTU-Hackathon 0:4233b7b10255 4324 var rest = sliceBuffer(data, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4325 if (Blob && binaryType === 'blob') {
group-Make-NTU-Hackathon 0:4233b7b10255 4326 rest = new Blob([rest]);
group-Make-NTU-Hackathon 0:4233b7b10255 4327 }
group-Make-NTU-Hackathon 0:4233b7b10255 4328 return { type: packetslist[type], data: rest };
group-Make-NTU-Hackathon 0:4233b7b10255 4329 };
group-Make-NTU-Hackathon 0:4233b7b10255 4330
group-Make-NTU-Hackathon 0:4233b7b10255 4331 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4332 * Decodes a packet encoded in a base64 string
group-Make-NTU-Hackathon 0:4233b7b10255 4333 *
group-Make-NTU-Hackathon 0:4233b7b10255 4334 * @param {String} base64 encoded message
group-Make-NTU-Hackathon 0:4233b7b10255 4335 * @return {Object} with `type` and `data` (if any)
group-Make-NTU-Hackathon 0:4233b7b10255 4336 */
group-Make-NTU-Hackathon 0:4233b7b10255 4337
group-Make-NTU-Hackathon 0:4233b7b10255 4338 exports.decodeBase64Packet = function(msg, binaryType) {
group-Make-NTU-Hackathon 0:4233b7b10255 4339 var type = packetslist[msg.charAt(0)];
group-Make-NTU-Hackathon 0:4233b7b10255 4340 if (!global.ArrayBuffer) {
group-Make-NTU-Hackathon 0:4233b7b10255 4341 return { type: type, data: { base64: true, data: msg.substr(1) } };
group-Make-NTU-Hackathon 0:4233b7b10255 4342 }
group-Make-NTU-Hackathon 0:4233b7b10255 4343
group-Make-NTU-Hackathon 0:4233b7b10255 4344 var data = base64encoder.decode(msg.substr(1));
group-Make-NTU-Hackathon 0:4233b7b10255 4345
group-Make-NTU-Hackathon 0:4233b7b10255 4346 if (binaryType === 'blob' && Blob) {
group-Make-NTU-Hackathon 0:4233b7b10255 4347 data = new Blob([data]);
group-Make-NTU-Hackathon 0:4233b7b10255 4348 }
group-Make-NTU-Hackathon 0:4233b7b10255 4349
group-Make-NTU-Hackathon 0:4233b7b10255 4350 return { type: type, data: data };
group-Make-NTU-Hackathon 0:4233b7b10255 4351 };
group-Make-NTU-Hackathon 0:4233b7b10255 4352
group-Make-NTU-Hackathon 0:4233b7b10255 4353 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4354 * Encodes multiple messages (payload).
group-Make-NTU-Hackathon 0:4233b7b10255 4355 *
group-Make-NTU-Hackathon 0:4233b7b10255 4356 * <length>:data
group-Make-NTU-Hackathon 0:4233b7b10255 4357 *
group-Make-NTU-Hackathon 0:4233b7b10255 4358 * Example:
group-Make-NTU-Hackathon 0:4233b7b10255 4359 *
group-Make-NTU-Hackathon 0:4233b7b10255 4360 * 11:hello world2:hi
group-Make-NTU-Hackathon 0:4233b7b10255 4361 *
group-Make-NTU-Hackathon 0:4233b7b10255 4362 * If any contents are binary, they will be encoded as base64 strings. Base64
group-Make-NTU-Hackathon 0:4233b7b10255 4363 * encoded strings are marked with a b before the length specifier
group-Make-NTU-Hackathon 0:4233b7b10255 4364 *
group-Make-NTU-Hackathon 0:4233b7b10255 4365 * @param {Array} packets
group-Make-NTU-Hackathon 0:4233b7b10255 4366 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4367 */
group-Make-NTU-Hackathon 0:4233b7b10255 4368
group-Make-NTU-Hackathon 0:4233b7b10255 4369 exports.encodePayload = function (packets, supportsBinary, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4370 if (typeof supportsBinary == 'function') {
group-Make-NTU-Hackathon 0:4233b7b10255 4371 callback = supportsBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 4372 supportsBinary = null;
group-Make-NTU-Hackathon 0:4233b7b10255 4373 }
group-Make-NTU-Hackathon 0:4233b7b10255 4374
group-Make-NTU-Hackathon 0:4233b7b10255 4375 var isBinary = hasBinary(packets);
group-Make-NTU-Hackathon 0:4233b7b10255 4376
group-Make-NTU-Hackathon 0:4233b7b10255 4377 if (supportsBinary && isBinary) {
group-Make-NTU-Hackathon 0:4233b7b10255 4378 if (Blob && !dontSendBlobs) {
group-Make-NTU-Hackathon 0:4233b7b10255 4379 return exports.encodePayloadAsBlob(packets, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4380 }
group-Make-NTU-Hackathon 0:4233b7b10255 4381
group-Make-NTU-Hackathon 0:4233b7b10255 4382 return exports.encodePayloadAsArrayBuffer(packets, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4383 }
group-Make-NTU-Hackathon 0:4233b7b10255 4384
group-Make-NTU-Hackathon 0:4233b7b10255 4385 if (!packets.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4386 return callback('0:');
group-Make-NTU-Hackathon 0:4233b7b10255 4387 }
group-Make-NTU-Hackathon 0:4233b7b10255 4388
group-Make-NTU-Hackathon 0:4233b7b10255 4389 function setLengthHeader(message) {
group-Make-NTU-Hackathon 0:4233b7b10255 4390 return message.length + ':' + message;
group-Make-NTU-Hackathon 0:4233b7b10255 4391 }
group-Make-NTU-Hackathon 0:4233b7b10255 4392
group-Make-NTU-Hackathon 0:4233b7b10255 4393 function encodeOne(packet, doneCallback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4394 exports.encodePacket(packet, !isBinary ? false : supportsBinary, true, function(message) {
group-Make-NTU-Hackathon 0:4233b7b10255 4395 doneCallback(null, setLengthHeader(message));
group-Make-NTU-Hackathon 0:4233b7b10255 4396 });
group-Make-NTU-Hackathon 0:4233b7b10255 4397 }
group-Make-NTU-Hackathon 0:4233b7b10255 4398
group-Make-NTU-Hackathon 0:4233b7b10255 4399 map(packets, encodeOne, function(err, results) {
group-Make-NTU-Hackathon 0:4233b7b10255 4400 return callback(results.join(''));
group-Make-NTU-Hackathon 0:4233b7b10255 4401 });
group-Make-NTU-Hackathon 0:4233b7b10255 4402 };
group-Make-NTU-Hackathon 0:4233b7b10255 4403
group-Make-NTU-Hackathon 0:4233b7b10255 4404 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4405 * Async array map using after
group-Make-NTU-Hackathon 0:4233b7b10255 4406 */
group-Make-NTU-Hackathon 0:4233b7b10255 4407
group-Make-NTU-Hackathon 0:4233b7b10255 4408 function map(ary, each, done) {
group-Make-NTU-Hackathon 0:4233b7b10255 4409 var result = new Array(ary.length);
group-Make-NTU-Hackathon 0:4233b7b10255 4410 var next = after(ary.length, done);
group-Make-NTU-Hackathon 0:4233b7b10255 4411
group-Make-NTU-Hackathon 0:4233b7b10255 4412 var eachWithIndex = function(i, el, cb) {
group-Make-NTU-Hackathon 0:4233b7b10255 4413 each(el, function(error, msg) {
group-Make-NTU-Hackathon 0:4233b7b10255 4414 result[i] = msg;
group-Make-NTU-Hackathon 0:4233b7b10255 4415 cb(error, result);
group-Make-NTU-Hackathon 0:4233b7b10255 4416 });
group-Make-NTU-Hackathon 0:4233b7b10255 4417 };
group-Make-NTU-Hackathon 0:4233b7b10255 4418
group-Make-NTU-Hackathon 0:4233b7b10255 4419 for (var i = 0; i < ary.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4420 eachWithIndex(i, ary[i], next);
group-Make-NTU-Hackathon 0:4233b7b10255 4421 }
group-Make-NTU-Hackathon 0:4233b7b10255 4422 }
group-Make-NTU-Hackathon 0:4233b7b10255 4423
group-Make-NTU-Hackathon 0:4233b7b10255 4424 /*
group-Make-NTU-Hackathon 0:4233b7b10255 4425 * Decodes data when a payload is maybe expected. Possible binary contents are
group-Make-NTU-Hackathon 0:4233b7b10255 4426 * decoded from their base64 representation
group-Make-NTU-Hackathon 0:4233b7b10255 4427 *
group-Make-NTU-Hackathon 0:4233b7b10255 4428 * @param {String} data, callback method
group-Make-NTU-Hackathon 0:4233b7b10255 4429 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 4430 */
group-Make-NTU-Hackathon 0:4233b7b10255 4431
group-Make-NTU-Hackathon 0:4233b7b10255 4432 exports.decodePayload = function (data, binaryType, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4433 if (typeof data != 'string') {
group-Make-NTU-Hackathon 0:4233b7b10255 4434 return exports.decodePayloadAsBinary(data, binaryType, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 4435 }
group-Make-NTU-Hackathon 0:4233b7b10255 4436
group-Make-NTU-Hackathon 0:4233b7b10255 4437 if (typeof binaryType === 'function') {
group-Make-NTU-Hackathon 0:4233b7b10255 4438 callback = binaryType;
group-Make-NTU-Hackathon 0:4233b7b10255 4439 binaryType = null;
group-Make-NTU-Hackathon 0:4233b7b10255 4440 }
group-Make-NTU-Hackathon 0:4233b7b10255 4441
group-Make-NTU-Hackathon 0:4233b7b10255 4442 var packet;
group-Make-NTU-Hackathon 0:4233b7b10255 4443 if (data == '') {
group-Make-NTU-Hackathon 0:4233b7b10255 4444 // parser error - ignoring payload
group-Make-NTU-Hackathon 0:4233b7b10255 4445 return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4446 }
group-Make-NTU-Hackathon 0:4233b7b10255 4447
group-Make-NTU-Hackathon 0:4233b7b10255 4448 var length = ''
group-Make-NTU-Hackathon 0:4233b7b10255 4449 , n, msg;
group-Make-NTU-Hackathon 0:4233b7b10255 4450
group-Make-NTU-Hackathon 0:4233b7b10255 4451 for (var i = 0, l = data.length; i < l; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4452 var chr = data.charAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 4453
group-Make-NTU-Hackathon 0:4233b7b10255 4454 if (':' != chr) {
group-Make-NTU-Hackathon 0:4233b7b10255 4455 length += chr;
group-Make-NTU-Hackathon 0:4233b7b10255 4456 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4457 if ('' == length || (length != (n = Number(length)))) {
group-Make-NTU-Hackathon 0:4233b7b10255 4458 // parser error - ignoring payload
group-Make-NTU-Hackathon 0:4233b7b10255 4459 return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4460 }
group-Make-NTU-Hackathon 0:4233b7b10255 4461
group-Make-NTU-Hackathon 0:4233b7b10255 4462 msg = data.substr(i + 1, n);
group-Make-NTU-Hackathon 0:4233b7b10255 4463
group-Make-NTU-Hackathon 0:4233b7b10255 4464 if (length != msg.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4465 // parser error - ignoring payload
group-Make-NTU-Hackathon 0:4233b7b10255 4466 return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4467 }
group-Make-NTU-Hackathon 0:4233b7b10255 4468
group-Make-NTU-Hackathon 0:4233b7b10255 4469 if (msg.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4470 packet = exports.decodePacket(msg, binaryType, true);
group-Make-NTU-Hackathon 0:4233b7b10255 4471
group-Make-NTU-Hackathon 0:4233b7b10255 4472 if (err.type == packet.type && err.data == packet.data) {
group-Make-NTU-Hackathon 0:4233b7b10255 4473 // parser error in individual packet - ignoring payload
group-Make-NTU-Hackathon 0:4233b7b10255 4474 return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4475 }
group-Make-NTU-Hackathon 0:4233b7b10255 4476
group-Make-NTU-Hackathon 0:4233b7b10255 4477 var ret = callback(packet, i + n, l);
group-Make-NTU-Hackathon 0:4233b7b10255 4478 if (false === ret) return;
group-Make-NTU-Hackathon 0:4233b7b10255 4479 }
group-Make-NTU-Hackathon 0:4233b7b10255 4480
group-Make-NTU-Hackathon 0:4233b7b10255 4481 // advance cursor
group-Make-NTU-Hackathon 0:4233b7b10255 4482 i += n;
group-Make-NTU-Hackathon 0:4233b7b10255 4483 length = '';
group-Make-NTU-Hackathon 0:4233b7b10255 4484 }
group-Make-NTU-Hackathon 0:4233b7b10255 4485 }
group-Make-NTU-Hackathon 0:4233b7b10255 4486
group-Make-NTU-Hackathon 0:4233b7b10255 4487 if (length != '') {
group-Make-NTU-Hackathon 0:4233b7b10255 4488 // parser error - ignoring payload
group-Make-NTU-Hackathon 0:4233b7b10255 4489 return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4490 }
group-Make-NTU-Hackathon 0:4233b7b10255 4491
group-Make-NTU-Hackathon 0:4233b7b10255 4492 };
group-Make-NTU-Hackathon 0:4233b7b10255 4493
group-Make-NTU-Hackathon 0:4233b7b10255 4494 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4495 * Encodes multiple messages (payload) as binary.
group-Make-NTU-Hackathon 0:4233b7b10255 4496 *
group-Make-NTU-Hackathon 0:4233b7b10255 4497 * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number
group-Make-NTU-Hackathon 0:4233b7b10255 4498 * 255><data>
group-Make-NTU-Hackathon 0:4233b7b10255 4499 *
group-Make-NTU-Hackathon 0:4233b7b10255 4500 * Example:
group-Make-NTU-Hackathon 0:4233b7b10255 4501 * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers
group-Make-NTU-Hackathon 0:4233b7b10255 4502 *
group-Make-NTU-Hackathon 0:4233b7b10255 4503 * @param {Array} packets
group-Make-NTU-Hackathon 0:4233b7b10255 4504 * @return {ArrayBuffer} encoded payload
group-Make-NTU-Hackathon 0:4233b7b10255 4505 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4506 */
group-Make-NTU-Hackathon 0:4233b7b10255 4507
group-Make-NTU-Hackathon 0:4233b7b10255 4508 exports.encodePayloadAsArrayBuffer = function(packets, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4509 if (!packets.length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4510 return callback(new ArrayBuffer(0));
group-Make-NTU-Hackathon 0:4233b7b10255 4511 }
group-Make-NTU-Hackathon 0:4233b7b10255 4512
group-Make-NTU-Hackathon 0:4233b7b10255 4513 function encodeOne(packet, doneCallback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4514 exports.encodePacket(packet, true, true, function(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 4515 return doneCallback(null, data);
group-Make-NTU-Hackathon 0:4233b7b10255 4516 });
group-Make-NTU-Hackathon 0:4233b7b10255 4517 }
group-Make-NTU-Hackathon 0:4233b7b10255 4518
group-Make-NTU-Hackathon 0:4233b7b10255 4519 map(packets, encodeOne, function(err, encodedPackets) {
group-Make-NTU-Hackathon 0:4233b7b10255 4520 var totalLength = encodedPackets.reduce(function(acc, p) {
group-Make-NTU-Hackathon 0:4233b7b10255 4521 var len;
group-Make-NTU-Hackathon 0:4233b7b10255 4522 if (typeof p === 'string'){
group-Make-NTU-Hackathon 0:4233b7b10255 4523 len = p.length;
group-Make-NTU-Hackathon 0:4233b7b10255 4524 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4525 len = p.byteLength;
group-Make-NTU-Hackathon 0:4233b7b10255 4526 }
group-Make-NTU-Hackathon 0:4233b7b10255 4527 return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2
group-Make-NTU-Hackathon 0:4233b7b10255 4528 }, 0);
group-Make-NTU-Hackathon 0:4233b7b10255 4529
group-Make-NTU-Hackathon 0:4233b7b10255 4530 var resultArray = new Uint8Array(totalLength);
group-Make-NTU-Hackathon 0:4233b7b10255 4531
group-Make-NTU-Hackathon 0:4233b7b10255 4532 var bufferIndex = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4533 encodedPackets.forEach(function(p) {
group-Make-NTU-Hackathon 0:4233b7b10255 4534 var isString = typeof p === 'string';
group-Make-NTU-Hackathon 0:4233b7b10255 4535 var ab = p;
group-Make-NTU-Hackathon 0:4233b7b10255 4536 if (isString) {
group-Make-NTU-Hackathon 0:4233b7b10255 4537 var view = new Uint8Array(p.length);
group-Make-NTU-Hackathon 0:4233b7b10255 4538 for (var i = 0; i < p.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4539 view[i] = p.charCodeAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 4540 }
group-Make-NTU-Hackathon 0:4233b7b10255 4541 ab = view.buffer;
group-Make-NTU-Hackathon 0:4233b7b10255 4542 }
group-Make-NTU-Hackathon 0:4233b7b10255 4543
group-Make-NTU-Hackathon 0:4233b7b10255 4544 if (isString) { // not true binary
group-Make-NTU-Hackathon 0:4233b7b10255 4545 resultArray[bufferIndex++] = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4546 } else { // true binary
group-Make-NTU-Hackathon 0:4233b7b10255 4547 resultArray[bufferIndex++] = 1;
group-Make-NTU-Hackathon 0:4233b7b10255 4548 }
group-Make-NTU-Hackathon 0:4233b7b10255 4549
group-Make-NTU-Hackathon 0:4233b7b10255 4550 var lenStr = ab.byteLength.toString();
group-Make-NTU-Hackathon 0:4233b7b10255 4551 for (var i = 0; i < lenStr.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4552 resultArray[bufferIndex++] = parseInt(lenStr[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 4553 }
group-Make-NTU-Hackathon 0:4233b7b10255 4554 resultArray[bufferIndex++] = 255;
group-Make-NTU-Hackathon 0:4233b7b10255 4555
group-Make-NTU-Hackathon 0:4233b7b10255 4556 var view = new Uint8Array(ab);
group-Make-NTU-Hackathon 0:4233b7b10255 4557 for (var i = 0; i < view.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4558 resultArray[bufferIndex++] = view[i];
group-Make-NTU-Hackathon 0:4233b7b10255 4559 }
group-Make-NTU-Hackathon 0:4233b7b10255 4560 });
group-Make-NTU-Hackathon 0:4233b7b10255 4561
group-Make-NTU-Hackathon 0:4233b7b10255 4562 return callback(resultArray.buffer);
group-Make-NTU-Hackathon 0:4233b7b10255 4563 });
group-Make-NTU-Hackathon 0:4233b7b10255 4564 };
group-Make-NTU-Hackathon 0:4233b7b10255 4565
group-Make-NTU-Hackathon 0:4233b7b10255 4566 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4567 * Encode as Blob
group-Make-NTU-Hackathon 0:4233b7b10255 4568 */
group-Make-NTU-Hackathon 0:4233b7b10255 4569
group-Make-NTU-Hackathon 0:4233b7b10255 4570 exports.encodePayloadAsBlob = function(packets, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4571 function encodeOne(packet, doneCallback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4572 exports.encodePacket(packet, true, true, function(encoded) {
group-Make-NTU-Hackathon 0:4233b7b10255 4573 var binaryIdentifier = new Uint8Array(1);
group-Make-NTU-Hackathon 0:4233b7b10255 4574 binaryIdentifier[0] = 1;
group-Make-NTU-Hackathon 0:4233b7b10255 4575 if (typeof encoded === 'string') {
group-Make-NTU-Hackathon 0:4233b7b10255 4576 var view = new Uint8Array(encoded.length);
group-Make-NTU-Hackathon 0:4233b7b10255 4577 for (var i = 0; i < encoded.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4578 view[i] = encoded.charCodeAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 4579 }
group-Make-NTU-Hackathon 0:4233b7b10255 4580 encoded = view.buffer;
group-Make-NTU-Hackathon 0:4233b7b10255 4581 binaryIdentifier[0] = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4582 }
group-Make-NTU-Hackathon 0:4233b7b10255 4583
group-Make-NTU-Hackathon 0:4233b7b10255 4584 var len = (encoded instanceof ArrayBuffer)
group-Make-NTU-Hackathon 0:4233b7b10255 4585 ? encoded.byteLength
group-Make-NTU-Hackathon 0:4233b7b10255 4586 : encoded.size;
group-Make-NTU-Hackathon 0:4233b7b10255 4587
group-Make-NTU-Hackathon 0:4233b7b10255 4588 var lenStr = len.toString();
group-Make-NTU-Hackathon 0:4233b7b10255 4589 var lengthAry = new Uint8Array(lenStr.length + 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4590 for (var i = 0; i < lenStr.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4591 lengthAry[i] = parseInt(lenStr[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 4592 }
group-Make-NTU-Hackathon 0:4233b7b10255 4593 lengthAry[lenStr.length] = 255;
group-Make-NTU-Hackathon 0:4233b7b10255 4594
group-Make-NTU-Hackathon 0:4233b7b10255 4595 if (Blob) {
group-Make-NTU-Hackathon 0:4233b7b10255 4596 var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]);
group-Make-NTU-Hackathon 0:4233b7b10255 4597 doneCallback(null, blob);
group-Make-NTU-Hackathon 0:4233b7b10255 4598 }
group-Make-NTU-Hackathon 0:4233b7b10255 4599 });
group-Make-NTU-Hackathon 0:4233b7b10255 4600 }
group-Make-NTU-Hackathon 0:4233b7b10255 4601
group-Make-NTU-Hackathon 0:4233b7b10255 4602 map(packets, encodeOne, function(err, results) {
group-Make-NTU-Hackathon 0:4233b7b10255 4603 return callback(new Blob(results));
group-Make-NTU-Hackathon 0:4233b7b10255 4604 });
group-Make-NTU-Hackathon 0:4233b7b10255 4605 };
group-Make-NTU-Hackathon 0:4233b7b10255 4606
group-Make-NTU-Hackathon 0:4233b7b10255 4607 /*
group-Make-NTU-Hackathon 0:4233b7b10255 4608 * Decodes data when a payload is maybe expected. Strings are decoded by
group-Make-NTU-Hackathon 0:4233b7b10255 4609 * interpreting each byte as a key code for entries marked to start with 0. See
group-Make-NTU-Hackathon 0:4233b7b10255 4610 * description of encodePayloadAsBinary
group-Make-NTU-Hackathon 0:4233b7b10255 4611 *
group-Make-NTU-Hackathon 0:4233b7b10255 4612 * @param {ArrayBuffer} data, callback method
group-Make-NTU-Hackathon 0:4233b7b10255 4613 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 4614 */
group-Make-NTU-Hackathon 0:4233b7b10255 4615
group-Make-NTU-Hackathon 0:4233b7b10255 4616 exports.decodePayloadAsBinary = function (data, binaryType, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 4617 if (typeof binaryType === 'function') {
group-Make-NTU-Hackathon 0:4233b7b10255 4618 callback = binaryType;
group-Make-NTU-Hackathon 0:4233b7b10255 4619 binaryType = null;
group-Make-NTU-Hackathon 0:4233b7b10255 4620 }
group-Make-NTU-Hackathon 0:4233b7b10255 4621
group-Make-NTU-Hackathon 0:4233b7b10255 4622 var bufferTail = data;
group-Make-NTU-Hackathon 0:4233b7b10255 4623 var buffers = [];
group-Make-NTU-Hackathon 0:4233b7b10255 4624
group-Make-NTU-Hackathon 0:4233b7b10255 4625 var numberTooLong = false;
group-Make-NTU-Hackathon 0:4233b7b10255 4626 while (bufferTail.byteLength > 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 4627 var tailArray = new Uint8Array(bufferTail);
group-Make-NTU-Hackathon 0:4233b7b10255 4628 var isString = tailArray[0] === 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4629 var msgLength = '';
group-Make-NTU-Hackathon 0:4233b7b10255 4630
group-Make-NTU-Hackathon 0:4233b7b10255 4631 for (var i = 1; ; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4632 if (tailArray[i] == 255) break;
group-Make-NTU-Hackathon 0:4233b7b10255 4633
group-Make-NTU-Hackathon 0:4233b7b10255 4634 if (msgLength.length > 310) {
group-Make-NTU-Hackathon 0:4233b7b10255 4635 numberTooLong = true;
group-Make-NTU-Hackathon 0:4233b7b10255 4636 break;
group-Make-NTU-Hackathon 0:4233b7b10255 4637 }
group-Make-NTU-Hackathon 0:4233b7b10255 4638
group-Make-NTU-Hackathon 0:4233b7b10255 4639 msgLength += tailArray[i];
group-Make-NTU-Hackathon 0:4233b7b10255 4640 }
group-Make-NTU-Hackathon 0:4233b7b10255 4641
group-Make-NTU-Hackathon 0:4233b7b10255 4642 if(numberTooLong) return callback(err, 0, 1);
group-Make-NTU-Hackathon 0:4233b7b10255 4643
group-Make-NTU-Hackathon 0:4233b7b10255 4644 bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length);
group-Make-NTU-Hackathon 0:4233b7b10255 4645 msgLength = parseInt(msgLength);
group-Make-NTU-Hackathon 0:4233b7b10255 4646
group-Make-NTU-Hackathon 0:4233b7b10255 4647 var msg = sliceBuffer(bufferTail, 0, msgLength);
group-Make-NTU-Hackathon 0:4233b7b10255 4648 if (isString) {
group-Make-NTU-Hackathon 0:4233b7b10255 4649 try {
group-Make-NTU-Hackathon 0:4233b7b10255 4650 msg = String.fromCharCode.apply(null, new Uint8Array(msg));
group-Make-NTU-Hackathon 0:4233b7b10255 4651 } catch (e) {
group-Make-NTU-Hackathon 0:4233b7b10255 4652 // iPhone Safari doesn't let you apply to typed arrays
group-Make-NTU-Hackathon 0:4233b7b10255 4653 var typed = new Uint8Array(msg);
group-Make-NTU-Hackathon 0:4233b7b10255 4654 msg = '';
group-Make-NTU-Hackathon 0:4233b7b10255 4655 for (var i = 0; i < typed.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4656 msg += String.fromCharCode(typed[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 4657 }
group-Make-NTU-Hackathon 0:4233b7b10255 4658 }
group-Make-NTU-Hackathon 0:4233b7b10255 4659 }
group-Make-NTU-Hackathon 0:4233b7b10255 4660
group-Make-NTU-Hackathon 0:4233b7b10255 4661 buffers.push(msg);
group-Make-NTU-Hackathon 0:4233b7b10255 4662 bufferTail = sliceBuffer(bufferTail, msgLength);
group-Make-NTU-Hackathon 0:4233b7b10255 4663 }
group-Make-NTU-Hackathon 0:4233b7b10255 4664
group-Make-NTU-Hackathon 0:4233b7b10255 4665 var total = buffers.length;
group-Make-NTU-Hackathon 0:4233b7b10255 4666 buffers.forEach(function(buffer, i) {
group-Make-NTU-Hackathon 0:4233b7b10255 4667 callback(exports.decodePacket(buffer, binaryType, true), i, total);
group-Make-NTU-Hackathon 0:4233b7b10255 4668 });
group-Make-NTU-Hackathon 0:4233b7b10255 4669 };
group-Make-NTU-Hackathon 0:4233b7b10255 4670
group-Make-NTU-Hackathon 0:4233b7b10255 4671 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 4672 },{"./keys":26,"after":27,"arraybuffer.slice":28,"base64-arraybuffer":29,"blob":30,"has-binary":31,"utf8":33}],26:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4673
group-Make-NTU-Hackathon 0:4233b7b10255 4674 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4675 * Gets the keys for an object.
group-Make-NTU-Hackathon 0:4233b7b10255 4676 *
group-Make-NTU-Hackathon 0:4233b7b10255 4677 * @return {Array} keys
group-Make-NTU-Hackathon 0:4233b7b10255 4678 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 4679 */
group-Make-NTU-Hackathon 0:4233b7b10255 4680
group-Make-NTU-Hackathon 0:4233b7b10255 4681 module.exports = Object.keys || function keys (obj){
group-Make-NTU-Hackathon 0:4233b7b10255 4682 var arr = [];
group-Make-NTU-Hackathon 0:4233b7b10255 4683 var has = Object.prototype.hasOwnProperty;
group-Make-NTU-Hackathon 0:4233b7b10255 4684
group-Make-NTU-Hackathon 0:4233b7b10255 4685 for (var i in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 4686 if (has.call(obj, i)) {
group-Make-NTU-Hackathon 0:4233b7b10255 4687 arr.push(i);
group-Make-NTU-Hackathon 0:4233b7b10255 4688 }
group-Make-NTU-Hackathon 0:4233b7b10255 4689 }
group-Make-NTU-Hackathon 0:4233b7b10255 4690 return arr;
group-Make-NTU-Hackathon 0:4233b7b10255 4691 };
group-Make-NTU-Hackathon 0:4233b7b10255 4692
group-Make-NTU-Hackathon 0:4233b7b10255 4693 },{}],27:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4694 module.exports = after
group-Make-NTU-Hackathon 0:4233b7b10255 4695
group-Make-NTU-Hackathon 0:4233b7b10255 4696 function after(count, callback, err_cb) {
group-Make-NTU-Hackathon 0:4233b7b10255 4697 var bail = false
group-Make-NTU-Hackathon 0:4233b7b10255 4698 err_cb = err_cb || noop
group-Make-NTU-Hackathon 0:4233b7b10255 4699 proxy.count = count
group-Make-NTU-Hackathon 0:4233b7b10255 4700
group-Make-NTU-Hackathon 0:4233b7b10255 4701 return (count === 0) ? callback() : proxy
group-Make-NTU-Hackathon 0:4233b7b10255 4702
group-Make-NTU-Hackathon 0:4233b7b10255 4703 function proxy(err, result) {
group-Make-NTU-Hackathon 0:4233b7b10255 4704 if (proxy.count <= 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 4705 throw new Error('after called too many times')
group-Make-NTU-Hackathon 0:4233b7b10255 4706 }
group-Make-NTU-Hackathon 0:4233b7b10255 4707 --proxy.count
group-Make-NTU-Hackathon 0:4233b7b10255 4708
group-Make-NTU-Hackathon 0:4233b7b10255 4709 // after first error, rest are passed to err_cb
group-Make-NTU-Hackathon 0:4233b7b10255 4710 if (err) {
group-Make-NTU-Hackathon 0:4233b7b10255 4711 bail = true
group-Make-NTU-Hackathon 0:4233b7b10255 4712 callback(err)
group-Make-NTU-Hackathon 0:4233b7b10255 4713 // future error callbacks will go to error handler
group-Make-NTU-Hackathon 0:4233b7b10255 4714 callback = err_cb
group-Make-NTU-Hackathon 0:4233b7b10255 4715 } else if (proxy.count === 0 && !bail) {
group-Make-NTU-Hackathon 0:4233b7b10255 4716 callback(null, result)
group-Make-NTU-Hackathon 0:4233b7b10255 4717 }
group-Make-NTU-Hackathon 0:4233b7b10255 4718 }
group-Make-NTU-Hackathon 0:4233b7b10255 4719 }
group-Make-NTU-Hackathon 0:4233b7b10255 4720
group-Make-NTU-Hackathon 0:4233b7b10255 4721 function noop() {}
group-Make-NTU-Hackathon 0:4233b7b10255 4722
group-Make-NTU-Hackathon 0:4233b7b10255 4723 },{}],28:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4724 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4725 * An abstraction for slicing an arraybuffer even when
group-Make-NTU-Hackathon 0:4233b7b10255 4726 * ArrayBuffer.prototype.slice is not supported
group-Make-NTU-Hackathon 0:4233b7b10255 4727 *
group-Make-NTU-Hackathon 0:4233b7b10255 4728 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 4729 */
group-Make-NTU-Hackathon 0:4233b7b10255 4730
group-Make-NTU-Hackathon 0:4233b7b10255 4731 module.exports = function(arraybuffer, start, end) {
group-Make-NTU-Hackathon 0:4233b7b10255 4732 var bytes = arraybuffer.byteLength;
group-Make-NTU-Hackathon 0:4233b7b10255 4733 start = start || 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4734 end = end || bytes;
group-Make-NTU-Hackathon 0:4233b7b10255 4735
group-Make-NTU-Hackathon 0:4233b7b10255 4736 if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
group-Make-NTU-Hackathon 0:4233b7b10255 4737
group-Make-NTU-Hackathon 0:4233b7b10255 4738 if (start < 0) { start += bytes; }
group-Make-NTU-Hackathon 0:4233b7b10255 4739 if (end < 0) { end += bytes; }
group-Make-NTU-Hackathon 0:4233b7b10255 4740 if (end > bytes) { end = bytes; }
group-Make-NTU-Hackathon 0:4233b7b10255 4741
group-Make-NTU-Hackathon 0:4233b7b10255 4742 if (start >= bytes || start >= end || bytes === 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 4743 return new ArrayBuffer(0);
group-Make-NTU-Hackathon 0:4233b7b10255 4744 }
group-Make-NTU-Hackathon 0:4233b7b10255 4745
group-Make-NTU-Hackathon 0:4233b7b10255 4746 var abv = new Uint8Array(arraybuffer);
group-Make-NTU-Hackathon 0:4233b7b10255 4747 var result = new Uint8Array(end - start);
group-Make-NTU-Hackathon 0:4233b7b10255 4748 for (var i = start, ii = 0; i < end; i++, ii++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4749 result[ii] = abv[i];
group-Make-NTU-Hackathon 0:4233b7b10255 4750 }
group-Make-NTU-Hackathon 0:4233b7b10255 4751 return result.buffer;
group-Make-NTU-Hackathon 0:4233b7b10255 4752 };
group-Make-NTU-Hackathon 0:4233b7b10255 4753
group-Make-NTU-Hackathon 0:4233b7b10255 4754 },{}],29:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4755 /*
group-Make-NTU-Hackathon 0:4233b7b10255 4756 * base64-arraybuffer
group-Make-NTU-Hackathon 0:4233b7b10255 4757 * https://github.com/niklasvh/base64-arraybuffer
group-Make-NTU-Hackathon 0:4233b7b10255 4758 *
group-Make-NTU-Hackathon 0:4233b7b10255 4759 * Copyright (c) 2012 Niklas von Hertzen
group-Make-NTU-Hackathon 0:4233b7b10255 4760 * Licensed under the MIT license.
group-Make-NTU-Hackathon 0:4233b7b10255 4761 */
group-Make-NTU-Hackathon 0:4233b7b10255 4762 (function(chars){
group-Make-NTU-Hackathon 0:4233b7b10255 4763 "use strict";
group-Make-NTU-Hackathon 0:4233b7b10255 4764
group-Make-NTU-Hackathon 0:4233b7b10255 4765 exports.encode = function(arraybuffer) {
group-Make-NTU-Hackathon 0:4233b7b10255 4766 var bytes = new Uint8Array(arraybuffer),
group-Make-NTU-Hackathon 0:4233b7b10255 4767 i, len = bytes.length, base64 = "";
group-Make-NTU-Hackathon 0:4233b7b10255 4768
group-Make-NTU-Hackathon 0:4233b7b10255 4769 for (i = 0; i < len; i+=3) {
group-Make-NTU-Hackathon 0:4233b7b10255 4770 base64 += chars[bytes[i] >> 2];
group-Make-NTU-Hackathon 0:4233b7b10255 4771 base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
group-Make-NTU-Hackathon 0:4233b7b10255 4772 base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
group-Make-NTU-Hackathon 0:4233b7b10255 4773 base64 += chars[bytes[i + 2] & 63];
group-Make-NTU-Hackathon 0:4233b7b10255 4774 }
group-Make-NTU-Hackathon 0:4233b7b10255 4775
group-Make-NTU-Hackathon 0:4233b7b10255 4776 if ((len % 3) === 2) {
group-Make-NTU-Hackathon 0:4233b7b10255 4777 base64 = base64.substring(0, base64.length - 1) + "=";
group-Make-NTU-Hackathon 0:4233b7b10255 4778 } else if (len % 3 === 1) {
group-Make-NTU-Hackathon 0:4233b7b10255 4779 base64 = base64.substring(0, base64.length - 2) + "==";
group-Make-NTU-Hackathon 0:4233b7b10255 4780 }
group-Make-NTU-Hackathon 0:4233b7b10255 4781
group-Make-NTU-Hackathon 0:4233b7b10255 4782 return base64;
group-Make-NTU-Hackathon 0:4233b7b10255 4783 };
group-Make-NTU-Hackathon 0:4233b7b10255 4784
group-Make-NTU-Hackathon 0:4233b7b10255 4785 exports.decode = function(base64) {
group-Make-NTU-Hackathon 0:4233b7b10255 4786 var bufferLength = base64.length * 0.75,
group-Make-NTU-Hackathon 0:4233b7b10255 4787 len = base64.length, i, p = 0,
group-Make-NTU-Hackathon 0:4233b7b10255 4788 encoded1, encoded2, encoded3, encoded4;
group-Make-NTU-Hackathon 0:4233b7b10255 4789
group-Make-NTU-Hackathon 0:4233b7b10255 4790 if (base64[base64.length - 1] === "=") {
group-Make-NTU-Hackathon 0:4233b7b10255 4791 bufferLength--;
group-Make-NTU-Hackathon 0:4233b7b10255 4792 if (base64[base64.length - 2] === "=") {
group-Make-NTU-Hackathon 0:4233b7b10255 4793 bufferLength--;
group-Make-NTU-Hackathon 0:4233b7b10255 4794 }
group-Make-NTU-Hackathon 0:4233b7b10255 4795 }
group-Make-NTU-Hackathon 0:4233b7b10255 4796
group-Make-NTU-Hackathon 0:4233b7b10255 4797 var arraybuffer = new ArrayBuffer(bufferLength),
group-Make-NTU-Hackathon 0:4233b7b10255 4798 bytes = new Uint8Array(arraybuffer);
group-Make-NTU-Hackathon 0:4233b7b10255 4799
group-Make-NTU-Hackathon 0:4233b7b10255 4800 for (i = 0; i < len; i+=4) {
group-Make-NTU-Hackathon 0:4233b7b10255 4801 encoded1 = chars.indexOf(base64[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 4802 encoded2 = chars.indexOf(base64[i+1]);
group-Make-NTU-Hackathon 0:4233b7b10255 4803 encoded3 = chars.indexOf(base64[i+2]);
group-Make-NTU-Hackathon 0:4233b7b10255 4804 encoded4 = chars.indexOf(base64[i+3]);
group-Make-NTU-Hackathon 0:4233b7b10255 4805
group-Make-NTU-Hackathon 0:4233b7b10255 4806 bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
group-Make-NTU-Hackathon 0:4233b7b10255 4807 bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
group-Make-NTU-Hackathon 0:4233b7b10255 4808 bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
group-Make-NTU-Hackathon 0:4233b7b10255 4809 }
group-Make-NTU-Hackathon 0:4233b7b10255 4810
group-Make-NTU-Hackathon 0:4233b7b10255 4811 return arraybuffer;
group-Make-NTU-Hackathon 0:4233b7b10255 4812 };
group-Make-NTU-Hackathon 0:4233b7b10255 4813 })("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
group-Make-NTU-Hackathon 0:4233b7b10255 4814
group-Make-NTU-Hackathon 0:4233b7b10255 4815 },{}],30:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4816 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 4817 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4818 * Create a blob builder even when vendor prefixes exist
group-Make-NTU-Hackathon 0:4233b7b10255 4819 */
group-Make-NTU-Hackathon 0:4233b7b10255 4820
group-Make-NTU-Hackathon 0:4233b7b10255 4821 var BlobBuilder = global.BlobBuilder
group-Make-NTU-Hackathon 0:4233b7b10255 4822 || global.WebKitBlobBuilder
group-Make-NTU-Hackathon 0:4233b7b10255 4823 || global.MSBlobBuilder
group-Make-NTU-Hackathon 0:4233b7b10255 4824 || global.MozBlobBuilder;
group-Make-NTU-Hackathon 0:4233b7b10255 4825
group-Make-NTU-Hackathon 0:4233b7b10255 4826 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4827 * Check if Blob constructor is supported
group-Make-NTU-Hackathon 0:4233b7b10255 4828 */
group-Make-NTU-Hackathon 0:4233b7b10255 4829
group-Make-NTU-Hackathon 0:4233b7b10255 4830 var blobSupported = (function() {
group-Make-NTU-Hackathon 0:4233b7b10255 4831 try {
group-Make-NTU-Hackathon 0:4233b7b10255 4832 var b = new Blob(['hi']);
group-Make-NTU-Hackathon 0:4233b7b10255 4833 return b.size == 2;
group-Make-NTU-Hackathon 0:4233b7b10255 4834 } catch(e) {
group-Make-NTU-Hackathon 0:4233b7b10255 4835 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 4836 }
group-Make-NTU-Hackathon 0:4233b7b10255 4837 })();
group-Make-NTU-Hackathon 0:4233b7b10255 4838
group-Make-NTU-Hackathon 0:4233b7b10255 4839 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4840 * Check if BlobBuilder is supported
group-Make-NTU-Hackathon 0:4233b7b10255 4841 */
group-Make-NTU-Hackathon 0:4233b7b10255 4842
group-Make-NTU-Hackathon 0:4233b7b10255 4843 var blobBuilderSupported = BlobBuilder
group-Make-NTU-Hackathon 0:4233b7b10255 4844 && BlobBuilder.prototype.append
group-Make-NTU-Hackathon 0:4233b7b10255 4845 && BlobBuilder.prototype.getBlob;
group-Make-NTU-Hackathon 0:4233b7b10255 4846
group-Make-NTU-Hackathon 0:4233b7b10255 4847 function BlobBuilderConstructor(ary, options) {
group-Make-NTU-Hackathon 0:4233b7b10255 4848 options = options || {};
group-Make-NTU-Hackathon 0:4233b7b10255 4849
group-Make-NTU-Hackathon 0:4233b7b10255 4850 var bb = new BlobBuilder();
group-Make-NTU-Hackathon 0:4233b7b10255 4851 for (var i = 0; i < ary.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4852 bb.append(ary[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 4853 }
group-Make-NTU-Hackathon 0:4233b7b10255 4854 return (options.type) ? bb.getBlob(options.type) : bb.getBlob();
group-Make-NTU-Hackathon 0:4233b7b10255 4855 };
group-Make-NTU-Hackathon 0:4233b7b10255 4856
group-Make-NTU-Hackathon 0:4233b7b10255 4857 module.exports = (function() {
group-Make-NTU-Hackathon 0:4233b7b10255 4858 if (blobSupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 4859 return global.Blob;
group-Make-NTU-Hackathon 0:4233b7b10255 4860 } else if (blobBuilderSupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 4861 return BlobBuilderConstructor;
group-Make-NTU-Hackathon 0:4233b7b10255 4862 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4863 return undefined;
group-Make-NTU-Hackathon 0:4233b7b10255 4864 }
group-Make-NTU-Hackathon 0:4233b7b10255 4865 })();
group-Make-NTU-Hackathon 0:4233b7b10255 4866
group-Make-NTU-Hackathon 0:4233b7b10255 4867 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 4868 },{}],31:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4869 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 4870
group-Make-NTU-Hackathon 0:4233b7b10255 4871 /*
group-Make-NTU-Hackathon 0:4233b7b10255 4872 * Module requirements.
group-Make-NTU-Hackathon 0:4233b7b10255 4873 */
group-Make-NTU-Hackathon 0:4233b7b10255 4874
group-Make-NTU-Hackathon 0:4233b7b10255 4875 var isArray = _dereq_('isarray');
group-Make-NTU-Hackathon 0:4233b7b10255 4876
group-Make-NTU-Hackathon 0:4233b7b10255 4877 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4878 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 4879 */
group-Make-NTU-Hackathon 0:4233b7b10255 4880
group-Make-NTU-Hackathon 0:4233b7b10255 4881 module.exports = hasBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 4882
group-Make-NTU-Hackathon 0:4233b7b10255 4883 /**
group-Make-NTU-Hackathon 0:4233b7b10255 4884 * Checks for binary data.
group-Make-NTU-Hackathon 0:4233b7b10255 4885 *
group-Make-NTU-Hackathon 0:4233b7b10255 4886 * Right now only Buffer and ArrayBuffer are supported..
group-Make-NTU-Hackathon 0:4233b7b10255 4887 *
group-Make-NTU-Hackathon 0:4233b7b10255 4888 * @param {Object} anything
group-Make-NTU-Hackathon 0:4233b7b10255 4889 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 4890 */
group-Make-NTU-Hackathon 0:4233b7b10255 4891
group-Make-NTU-Hackathon 0:4233b7b10255 4892 function hasBinary(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 4893
group-Make-NTU-Hackathon 0:4233b7b10255 4894 function _hasBinary(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 4895 if (!obj) return false;
group-Make-NTU-Hackathon 0:4233b7b10255 4896
group-Make-NTU-Hackathon 0:4233b7b10255 4897 if ( (global.Buffer && global.Buffer.isBuffer(obj)) ||
group-Make-NTU-Hackathon 0:4233b7b10255 4898 (global.ArrayBuffer && obj instanceof ArrayBuffer) ||
group-Make-NTU-Hackathon 0:4233b7b10255 4899 (global.Blob && obj instanceof Blob) ||
group-Make-NTU-Hackathon 0:4233b7b10255 4900 (global.File && obj instanceof File)
group-Make-NTU-Hackathon 0:4233b7b10255 4901 ) {
group-Make-NTU-Hackathon 0:4233b7b10255 4902 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 4903 }
group-Make-NTU-Hackathon 0:4233b7b10255 4904
group-Make-NTU-Hackathon 0:4233b7b10255 4905 if (isArray(obj)) {
group-Make-NTU-Hackathon 0:4233b7b10255 4906 for (var i = 0; i < obj.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 4907 if (_hasBinary(obj[i])) {
group-Make-NTU-Hackathon 0:4233b7b10255 4908 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 4909 }
group-Make-NTU-Hackathon 0:4233b7b10255 4910 }
group-Make-NTU-Hackathon 0:4233b7b10255 4911 } else if (obj && 'object' == typeof obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 4912 if (obj.toJSON) {
group-Make-NTU-Hackathon 0:4233b7b10255 4913 obj = obj.toJSON();
group-Make-NTU-Hackathon 0:4233b7b10255 4914 }
group-Make-NTU-Hackathon 0:4233b7b10255 4915
group-Make-NTU-Hackathon 0:4233b7b10255 4916 for (var key in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 4917 if (obj.hasOwnProperty(key) && _hasBinary(obj[key])) {
group-Make-NTU-Hackathon 0:4233b7b10255 4918 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 4919 }
group-Make-NTU-Hackathon 0:4233b7b10255 4920 }
group-Make-NTU-Hackathon 0:4233b7b10255 4921 }
group-Make-NTU-Hackathon 0:4233b7b10255 4922
group-Make-NTU-Hackathon 0:4233b7b10255 4923 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 4924 }
group-Make-NTU-Hackathon 0:4233b7b10255 4925
group-Make-NTU-Hackathon 0:4233b7b10255 4926 return _hasBinary(data);
group-Make-NTU-Hackathon 0:4233b7b10255 4927 }
group-Make-NTU-Hackathon 0:4233b7b10255 4928
group-Make-NTU-Hackathon 0:4233b7b10255 4929 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 4930 },{"isarray":32}],32:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4931 module.exports = Array.isArray || function (arr) {
group-Make-NTU-Hackathon 0:4233b7b10255 4932 return Object.prototype.toString.call(arr) == '[object Array]';
group-Make-NTU-Hackathon 0:4233b7b10255 4933 };
group-Make-NTU-Hackathon 0:4233b7b10255 4934
group-Make-NTU-Hackathon 0:4233b7b10255 4935 },{}],33:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 4936 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 4937 /*! http://mths.be/utf8js v2.0.0 by @mathias */
group-Make-NTU-Hackathon 0:4233b7b10255 4938 ;(function(root) {
group-Make-NTU-Hackathon 0:4233b7b10255 4939
group-Make-NTU-Hackathon 0:4233b7b10255 4940 // Detect free variables `exports`
group-Make-NTU-Hackathon 0:4233b7b10255 4941 var freeExports = typeof exports == 'object' && exports;
group-Make-NTU-Hackathon 0:4233b7b10255 4942
group-Make-NTU-Hackathon 0:4233b7b10255 4943 // Detect free variable `module`
group-Make-NTU-Hackathon 0:4233b7b10255 4944 var freeModule = typeof module == 'object' && module &&
group-Make-NTU-Hackathon 0:4233b7b10255 4945 module.exports == freeExports && module;
group-Make-NTU-Hackathon 0:4233b7b10255 4946
group-Make-NTU-Hackathon 0:4233b7b10255 4947 // Detect free variable `global`, from Node.js or Browserified code,
group-Make-NTU-Hackathon 0:4233b7b10255 4948 // and use it as `root`
group-Make-NTU-Hackathon 0:4233b7b10255 4949 var freeGlobal = typeof global == 'object' && global;
group-Make-NTU-Hackathon 0:4233b7b10255 4950 if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
group-Make-NTU-Hackathon 0:4233b7b10255 4951 root = freeGlobal;
group-Make-NTU-Hackathon 0:4233b7b10255 4952 }
group-Make-NTU-Hackathon 0:4233b7b10255 4953
group-Make-NTU-Hackathon 0:4233b7b10255 4954 /*--------------------------------------------------------------------------*/
group-Make-NTU-Hackathon 0:4233b7b10255 4955
group-Make-NTU-Hackathon 0:4233b7b10255 4956 var stringFromCharCode = String.fromCharCode;
group-Make-NTU-Hackathon 0:4233b7b10255 4957
group-Make-NTU-Hackathon 0:4233b7b10255 4958 // Taken from http://mths.be/punycode
group-Make-NTU-Hackathon 0:4233b7b10255 4959 function ucs2decode(string) {
group-Make-NTU-Hackathon 0:4233b7b10255 4960 var output = [];
group-Make-NTU-Hackathon 0:4233b7b10255 4961 var counter = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 4962 var length = string.length;
group-Make-NTU-Hackathon 0:4233b7b10255 4963 var value;
group-Make-NTU-Hackathon 0:4233b7b10255 4964 var extra;
group-Make-NTU-Hackathon 0:4233b7b10255 4965 while (counter < length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4966 value = string.charCodeAt(counter++);
group-Make-NTU-Hackathon 0:4233b7b10255 4967 if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4968 // high surrogate, and there is a next character
group-Make-NTU-Hackathon 0:4233b7b10255 4969 extra = string.charCodeAt(counter++);
group-Make-NTU-Hackathon 0:4233b7b10255 4970 if ((extra & 0xFC00) == 0xDC00) { // low surrogate
group-Make-NTU-Hackathon 0:4233b7b10255 4971 output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
group-Make-NTU-Hackathon 0:4233b7b10255 4972 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4973 // unmatched surrogate; only append this code unit, in case the next
group-Make-NTU-Hackathon 0:4233b7b10255 4974 // code unit is the high surrogate of a surrogate pair
group-Make-NTU-Hackathon 0:4233b7b10255 4975 output.push(value);
group-Make-NTU-Hackathon 0:4233b7b10255 4976 counter--;
group-Make-NTU-Hackathon 0:4233b7b10255 4977 }
group-Make-NTU-Hackathon 0:4233b7b10255 4978 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 4979 output.push(value);
group-Make-NTU-Hackathon 0:4233b7b10255 4980 }
group-Make-NTU-Hackathon 0:4233b7b10255 4981 }
group-Make-NTU-Hackathon 0:4233b7b10255 4982 return output;
group-Make-NTU-Hackathon 0:4233b7b10255 4983 }
group-Make-NTU-Hackathon 0:4233b7b10255 4984
group-Make-NTU-Hackathon 0:4233b7b10255 4985 // Taken from http://mths.be/punycode
group-Make-NTU-Hackathon 0:4233b7b10255 4986 function ucs2encode(array) {
group-Make-NTU-Hackathon 0:4233b7b10255 4987 var length = array.length;
group-Make-NTU-Hackathon 0:4233b7b10255 4988 var index = -1;
group-Make-NTU-Hackathon 0:4233b7b10255 4989 var value;
group-Make-NTU-Hackathon 0:4233b7b10255 4990 var output = '';
group-Make-NTU-Hackathon 0:4233b7b10255 4991 while (++index < length) {
group-Make-NTU-Hackathon 0:4233b7b10255 4992 value = array[index];
group-Make-NTU-Hackathon 0:4233b7b10255 4993 if (value > 0xFFFF) {
group-Make-NTU-Hackathon 0:4233b7b10255 4994 value -= 0x10000;
group-Make-NTU-Hackathon 0:4233b7b10255 4995 output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
group-Make-NTU-Hackathon 0:4233b7b10255 4996 value = 0xDC00 | value & 0x3FF;
group-Make-NTU-Hackathon 0:4233b7b10255 4997 }
group-Make-NTU-Hackathon 0:4233b7b10255 4998 output += stringFromCharCode(value);
group-Make-NTU-Hackathon 0:4233b7b10255 4999 }
group-Make-NTU-Hackathon 0:4233b7b10255 5000 return output;
group-Make-NTU-Hackathon 0:4233b7b10255 5001 }
group-Make-NTU-Hackathon 0:4233b7b10255 5002
group-Make-NTU-Hackathon 0:4233b7b10255 5003 /*--------------------------------------------------------------------------*/
group-Make-NTU-Hackathon 0:4233b7b10255 5004
group-Make-NTU-Hackathon 0:4233b7b10255 5005 function createByte(codePoint, shift) {
group-Make-NTU-Hackathon 0:4233b7b10255 5006 return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
group-Make-NTU-Hackathon 0:4233b7b10255 5007 }
group-Make-NTU-Hackathon 0:4233b7b10255 5008
group-Make-NTU-Hackathon 0:4233b7b10255 5009 function encodeCodePoint(codePoint) {
group-Make-NTU-Hackathon 0:4233b7b10255 5010 if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5011 return stringFromCharCode(codePoint);
group-Make-NTU-Hackathon 0:4233b7b10255 5012 }
group-Make-NTU-Hackathon 0:4233b7b10255 5013 var symbol = '';
group-Make-NTU-Hackathon 0:4233b7b10255 5014 if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5015 symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
group-Make-NTU-Hackathon 0:4233b7b10255 5016 }
group-Make-NTU-Hackathon 0:4233b7b10255 5017 else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5018 symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
group-Make-NTU-Hackathon 0:4233b7b10255 5019 symbol += createByte(codePoint, 6);
group-Make-NTU-Hackathon 0:4233b7b10255 5020 }
group-Make-NTU-Hackathon 0:4233b7b10255 5021 else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5022 symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
group-Make-NTU-Hackathon 0:4233b7b10255 5023 symbol += createByte(codePoint, 12);
group-Make-NTU-Hackathon 0:4233b7b10255 5024 symbol += createByte(codePoint, 6);
group-Make-NTU-Hackathon 0:4233b7b10255 5025 }
group-Make-NTU-Hackathon 0:4233b7b10255 5026 symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
group-Make-NTU-Hackathon 0:4233b7b10255 5027 return symbol;
group-Make-NTU-Hackathon 0:4233b7b10255 5028 }
group-Make-NTU-Hackathon 0:4233b7b10255 5029
group-Make-NTU-Hackathon 0:4233b7b10255 5030 function utf8encode(string) {
group-Make-NTU-Hackathon 0:4233b7b10255 5031 var codePoints = ucs2decode(string);
group-Make-NTU-Hackathon 0:4233b7b10255 5032
group-Make-NTU-Hackathon 0:4233b7b10255 5033 // console.log(JSON.stringify(codePoints.map(function(x) {
group-Make-NTU-Hackathon 0:4233b7b10255 5034 // return 'U+' + x.toString(16).toUpperCase();
group-Make-NTU-Hackathon 0:4233b7b10255 5035 // })));
group-Make-NTU-Hackathon 0:4233b7b10255 5036
group-Make-NTU-Hackathon 0:4233b7b10255 5037 var length = codePoints.length;
group-Make-NTU-Hackathon 0:4233b7b10255 5038 var index = -1;
group-Make-NTU-Hackathon 0:4233b7b10255 5039 var codePoint;
group-Make-NTU-Hackathon 0:4233b7b10255 5040 var byteString = '';
group-Make-NTU-Hackathon 0:4233b7b10255 5041 while (++index < length) {
group-Make-NTU-Hackathon 0:4233b7b10255 5042 codePoint = codePoints[index];
group-Make-NTU-Hackathon 0:4233b7b10255 5043 byteString += encodeCodePoint(codePoint);
group-Make-NTU-Hackathon 0:4233b7b10255 5044 }
group-Make-NTU-Hackathon 0:4233b7b10255 5045 return byteString;
group-Make-NTU-Hackathon 0:4233b7b10255 5046 }
group-Make-NTU-Hackathon 0:4233b7b10255 5047
group-Make-NTU-Hackathon 0:4233b7b10255 5048 /*--------------------------------------------------------------------------*/
group-Make-NTU-Hackathon 0:4233b7b10255 5049
group-Make-NTU-Hackathon 0:4233b7b10255 5050 function readContinuationByte() {
group-Make-NTU-Hackathon 0:4233b7b10255 5051 if (byteIndex >= byteCount) {
group-Make-NTU-Hackathon 0:4233b7b10255 5052 throw Error('Invalid byte index');
group-Make-NTU-Hackathon 0:4233b7b10255 5053 }
group-Make-NTU-Hackathon 0:4233b7b10255 5054
group-Make-NTU-Hackathon 0:4233b7b10255 5055 var continuationByte = byteArray[byteIndex] & 0xFF;
group-Make-NTU-Hackathon 0:4233b7b10255 5056 byteIndex++;
group-Make-NTU-Hackathon 0:4233b7b10255 5057
group-Make-NTU-Hackathon 0:4233b7b10255 5058 if ((continuationByte & 0xC0) == 0x80) {
group-Make-NTU-Hackathon 0:4233b7b10255 5059 return continuationByte & 0x3F;
group-Make-NTU-Hackathon 0:4233b7b10255 5060 }
group-Make-NTU-Hackathon 0:4233b7b10255 5061
group-Make-NTU-Hackathon 0:4233b7b10255 5062 // If we end up here, it’s not a continuation byte
group-Make-NTU-Hackathon 0:4233b7b10255 5063 throw Error('Invalid continuation byte');
group-Make-NTU-Hackathon 0:4233b7b10255 5064 }
group-Make-NTU-Hackathon 0:4233b7b10255 5065
group-Make-NTU-Hackathon 0:4233b7b10255 5066 function decodeSymbol() {
group-Make-NTU-Hackathon 0:4233b7b10255 5067 var byte1;
group-Make-NTU-Hackathon 0:4233b7b10255 5068 var byte2;
group-Make-NTU-Hackathon 0:4233b7b10255 5069 var byte3;
group-Make-NTU-Hackathon 0:4233b7b10255 5070 var byte4;
group-Make-NTU-Hackathon 0:4233b7b10255 5071 var codePoint;
group-Make-NTU-Hackathon 0:4233b7b10255 5072
group-Make-NTU-Hackathon 0:4233b7b10255 5073 if (byteIndex > byteCount) {
group-Make-NTU-Hackathon 0:4233b7b10255 5074 throw Error('Invalid byte index');
group-Make-NTU-Hackathon 0:4233b7b10255 5075 }
group-Make-NTU-Hackathon 0:4233b7b10255 5076
group-Make-NTU-Hackathon 0:4233b7b10255 5077 if (byteIndex == byteCount) {
group-Make-NTU-Hackathon 0:4233b7b10255 5078 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 5079 }
group-Make-NTU-Hackathon 0:4233b7b10255 5080
group-Make-NTU-Hackathon 0:4233b7b10255 5081 // Read first byte
group-Make-NTU-Hackathon 0:4233b7b10255 5082 byte1 = byteArray[byteIndex] & 0xFF;
group-Make-NTU-Hackathon 0:4233b7b10255 5083 byteIndex++;
group-Make-NTU-Hackathon 0:4233b7b10255 5084
group-Make-NTU-Hackathon 0:4233b7b10255 5085 // 1-byte sequence (no continuation bytes)
group-Make-NTU-Hackathon 0:4233b7b10255 5086 if ((byte1 & 0x80) == 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 5087 return byte1;
group-Make-NTU-Hackathon 0:4233b7b10255 5088 }
group-Make-NTU-Hackathon 0:4233b7b10255 5089
group-Make-NTU-Hackathon 0:4233b7b10255 5090 // 2-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5091 if ((byte1 & 0xE0) == 0xC0) {
group-Make-NTU-Hackathon 0:4233b7b10255 5092 var byte2 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5093 codePoint = ((byte1 & 0x1F) << 6) | byte2;
group-Make-NTU-Hackathon 0:4233b7b10255 5094 if (codePoint >= 0x80) {
group-Make-NTU-Hackathon 0:4233b7b10255 5095 return codePoint;
group-Make-NTU-Hackathon 0:4233b7b10255 5096 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 5097 throw Error('Invalid continuation byte');
group-Make-NTU-Hackathon 0:4233b7b10255 5098 }
group-Make-NTU-Hackathon 0:4233b7b10255 5099 }
group-Make-NTU-Hackathon 0:4233b7b10255 5100
group-Make-NTU-Hackathon 0:4233b7b10255 5101 // 3-byte sequence (may include unpaired surrogates)
group-Make-NTU-Hackathon 0:4233b7b10255 5102 if ((byte1 & 0xF0) == 0xE0) {
group-Make-NTU-Hackathon 0:4233b7b10255 5103 byte2 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5104 byte3 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5105 codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
group-Make-NTU-Hackathon 0:4233b7b10255 5106 if (codePoint >= 0x0800) {
group-Make-NTU-Hackathon 0:4233b7b10255 5107 return codePoint;
group-Make-NTU-Hackathon 0:4233b7b10255 5108 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 5109 throw Error('Invalid continuation byte');
group-Make-NTU-Hackathon 0:4233b7b10255 5110 }
group-Make-NTU-Hackathon 0:4233b7b10255 5111 }
group-Make-NTU-Hackathon 0:4233b7b10255 5112
group-Make-NTU-Hackathon 0:4233b7b10255 5113 // 4-byte sequence
group-Make-NTU-Hackathon 0:4233b7b10255 5114 if ((byte1 & 0xF8) == 0xF0) {
group-Make-NTU-Hackathon 0:4233b7b10255 5115 byte2 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5116 byte3 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5117 byte4 = readContinuationByte();
group-Make-NTU-Hackathon 0:4233b7b10255 5118 codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
group-Make-NTU-Hackathon 0:4233b7b10255 5119 (byte3 << 0x06) | byte4;
group-Make-NTU-Hackathon 0:4233b7b10255 5120 if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
group-Make-NTU-Hackathon 0:4233b7b10255 5121 return codePoint;
group-Make-NTU-Hackathon 0:4233b7b10255 5122 }
group-Make-NTU-Hackathon 0:4233b7b10255 5123 }
group-Make-NTU-Hackathon 0:4233b7b10255 5124
group-Make-NTU-Hackathon 0:4233b7b10255 5125 throw Error('Invalid UTF-8 detected');
group-Make-NTU-Hackathon 0:4233b7b10255 5126 }
group-Make-NTU-Hackathon 0:4233b7b10255 5127
group-Make-NTU-Hackathon 0:4233b7b10255 5128 var byteArray;
group-Make-NTU-Hackathon 0:4233b7b10255 5129 var byteCount;
group-Make-NTU-Hackathon 0:4233b7b10255 5130 var byteIndex;
group-Make-NTU-Hackathon 0:4233b7b10255 5131 function utf8decode(byteString) {
group-Make-NTU-Hackathon 0:4233b7b10255 5132 byteArray = ucs2decode(byteString);
group-Make-NTU-Hackathon 0:4233b7b10255 5133 byteCount = byteArray.length;
group-Make-NTU-Hackathon 0:4233b7b10255 5134 byteIndex = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 5135 var codePoints = [];
group-Make-NTU-Hackathon 0:4233b7b10255 5136 var tmp;
group-Make-NTU-Hackathon 0:4233b7b10255 5137 while ((tmp = decodeSymbol()) !== false) {
group-Make-NTU-Hackathon 0:4233b7b10255 5138 codePoints.push(tmp);
group-Make-NTU-Hackathon 0:4233b7b10255 5139 }
group-Make-NTU-Hackathon 0:4233b7b10255 5140 return ucs2encode(codePoints);
group-Make-NTU-Hackathon 0:4233b7b10255 5141 }
group-Make-NTU-Hackathon 0:4233b7b10255 5142
group-Make-NTU-Hackathon 0:4233b7b10255 5143 /*--------------------------------------------------------------------------*/
group-Make-NTU-Hackathon 0:4233b7b10255 5144
group-Make-NTU-Hackathon 0:4233b7b10255 5145 var utf8 = {
group-Make-NTU-Hackathon 0:4233b7b10255 5146 'version': '2.0.0',
group-Make-NTU-Hackathon 0:4233b7b10255 5147 'encode': utf8encode,
group-Make-NTU-Hackathon 0:4233b7b10255 5148 'decode': utf8decode
group-Make-NTU-Hackathon 0:4233b7b10255 5149 };
group-Make-NTU-Hackathon 0:4233b7b10255 5150
group-Make-NTU-Hackathon 0:4233b7b10255 5151 // Some AMD build optimizers, like r.js, check for specific condition patterns
group-Make-NTU-Hackathon 0:4233b7b10255 5152 // like the following:
group-Make-NTU-Hackathon 0:4233b7b10255 5153 if (
group-Make-NTU-Hackathon 0:4233b7b10255 5154 typeof define == 'function' &&
group-Make-NTU-Hackathon 0:4233b7b10255 5155 typeof define.amd == 'object' &&
group-Make-NTU-Hackathon 0:4233b7b10255 5156 define.amd
group-Make-NTU-Hackathon 0:4233b7b10255 5157 ) {
group-Make-NTU-Hackathon 0:4233b7b10255 5158 define(function() {
group-Make-NTU-Hackathon 0:4233b7b10255 5159 return utf8;
group-Make-NTU-Hackathon 0:4233b7b10255 5160 });
group-Make-NTU-Hackathon 0:4233b7b10255 5161 } else if (freeExports && !freeExports.nodeType) {
group-Make-NTU-Hackathon 0:4233b7b10255 5162 if (freeModule) { // in Node.js or RingoJS v0.8.0+
group-Make-NTU-Hackathon 0:4233b7b10255 5163 freeModule.exports = utf8;
group-Make-NTU-Hackathon 0:4233b7b10255 5164 } else { // in Narwhal or RingoJS v0.7.0-
group-Make-NTU-Hackathon 0:4233b7b10255 5165 var object = {};
group-Make-NTU-Hackathon 0:4233b7b10255 5166 var hasOwnProperty = object.hasOwnProperty;
group-Make-NTU-Hackathon 0:4233b7b10255 5167 for (var key in utf8) {
group-Make-NTU-Hackathon 0:4233b7b10255 5168 hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]);
group-Make-NTU-Hackathon 0:4233b7b10255 5169 }
group-Make-NTU-Hackathon 0:4233b7b10255 5170 }
group-Make-NTU-Hackathon 0:4233b7b10255 5171 } else { // in Rhino or a web browser
group-Make-NTU-Hackathon 0:4233b7b10255 5172 root.utf8 = utf8;
group-Make-NTU-Hackathon 0:4233b7b10255 5173 }
group-Make-NTU-Hackathon 0:4233b7b10255 5174
group-Make-NTU-Hackathon 0:4233b7b10255 5175 }(this));
group-Make-NTU-Hackathon 0:4233b7b10255 5176
group-Make-NTU-Hackathon 0:4233b7b10255 5177 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 5178 },{}],34:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5179 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 5180 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5181 * JSON parse.
group-Make-NTU-Hackathon 0:4233b7b10255 5182 *
group-Make-NTU-Hackathon 0:4233b7b10255 5183 * @see Based on jQuery#parseJSON (MIT) and JSON2
group-Make-NTU-Hackathon 0:4233b7b10255 5184 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5185 */
group-Make-NTU-Hackathon 0:4233b7b10255 5186
group-Make-NTU-Hackathon 0:4233b7b10255 5187 var rvalidchars = /^[\],:{}\s]*$/;
group-Make-NTU-Hackathon 0:4233b7b10255 5188 var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
group-Make-NTU-Hackathon 0:4233b7b10255 5189 var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
group-Make-NTU-Hackathon 0:4233b7b10255 5190 var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
group-Make-NTU-Hackathon 0:4233b7b10255 5191 var rtrimLeft = /^\s+/;
group-Make-NTU-Hackathon 0:4233b7b10255 5192 var rtrimRight = /\s+$/;
group-Make-NTU-Hackathon 0:4233b7b10255 5193
group-Make-NTU-Hackathon 0:4233b7b10255 5194 module.exports = function parsejson(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5195 if ('string' != typeof data || !data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5196 return null;
group-Make-NTU-Hackathon 0:4233b7b10255 5197 }
group-Make-NTU-Hackathon 0:4233b7b10255 5198
group-Make-NTU-Hackathon 0:4233b7b10255 5199 data = data.replace(rtrimLeft, '').replace(rtrimRight, '');
group-Make-NTU-Hackathon 0:4233b7b10255 5200
group-Make-NTU-Hackathon 0:4233b7b10255 5201 // Attempt to parse using the native JSON parser first
group-Make-NTU-Hackathon 0:4233b7b10255 5202 if (global.JSON && JSON.parse) {
group-Make-NTU-Hackathon 0:4233b7b10255 5203 return JSON.parse(data);
group-Make-NTU-Hackathon 0:4233b7b10255 5204 }
group-Make-NTU-Hackathon 0:4233b7b10255 5205
group-Make-NTU-Hackathon 0:4233b7b10255 5206 if (rvalidchars.test(data.replace(rvalidescape, '@')
group-Make-NTU-Hackathon 0:4233b7b10255 5207 .replace(rvalidtokens, ']')
group-Make-NTU-Hackathon 0:4233b7b10255 5208 .replace(rvalidbraces, ''))) {
group-Make-NTU-Hackathon 0:4233b7b10255 5209 return (new Function('return ' + data))();
group-Make-NTU-Hackathon 0:4233b7b10255 5210 }
group-Make-NTU-Hackathon 0:4233b7b10255 5211 };
group-Make-NTU-Hackathon 0:4233b7b10255 5212 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 5213 },{}],35:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5214 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5215 * Compiles a querystring
group-Make-NTU-Hackathon 0:4233b7b10255 5216 * Returns string representation of the object
group-Make-NTU-Hackathon 0:4233b7b10255 5217 *
group-Make-NTU-Hackathon 0:4233b7b10255 5218 * @param {Object}
group-Make-NTU-Hackathon 0:4233b7b10255 5219 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5220 */
group-Make-NTU-Hackathon 0:4233b7b10255 5221
group-Make-NTU-Hackathon 0:4233b7b10255 5222 exports.encode = function (obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5223 var str = '';
group-Make-NTU-Hackathon 0:4233b7b10255 5224
group-Make-NTU-Hackathon 0:4233b7b10255 5225 for (var i in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5226 if (obj.hasOwnProperty(i)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5227 if (str.length) str += '&';
group-Make-NTU-Hackathon 0:4233b7b10255 5228 str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 5229 }
group-Make-NTU-Hackathon 0:4233b7b10255 5230 }
group-Make-NTU-Hackathon 0:4233b7b10255 5231
group-Make-NTU-Hackathon 0:4233b7b10255 5232 return str;
group-Make-NTU-Hackathon 0:4233b7b10255 5233 };
group-Make-NTU-Hackathon 0:4233b7b10255 5234
group-Make-NTU-Hackathon 0:4233b7b10255 5235 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5236 * Parses a simple querystring into an object
group-Make-NTU-Hackathon 0:4233b7b10255 5237 *
group-Make-NTU-Hackathon 0:4233b7b10255 5238 * @param {String} qs
group-Make-NTU-Hackathon 0:4233b7b10255 5239 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5240 */
group-Make-NTU-Hackathon 0:4233b7b10255 5241
group-Make-NTU-Hackathon 0:4233b7b10255 5242 exports.decode = function(qs){
group-Make-NTU-Hackathon 0:4233b7b10255 5243 var qry = {};
group-Make-NTU-Hackathon 0:4233b7b10255 5244 var pairs = qs.split('&');
group-Make-NTU-Hackathon 0:4233b7b10255 5245 for (var i = 0, l = pairs.length; i < l; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 5246 var pair = pairs[i].split('=');
group-Make-NTU-Hackathon 0:4233b7b10255 5247 qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
group-Make-NTU-Hackathon 0:4233b7b10255 5248 }
group-Make-NTU-Hackathon 0:4233b7b10255 5249 return qry;
group-Make-NTU-Hackathon 0:4233b7b10255 5250 };
group-Make-NTU-Hackathon 0:4233b7b10255 5251
group-Make-NTU-Hackathon 0:4233b7b10255 5252 },{}],36:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5253 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5254 * Parses an URI
group-Make-NTU-Hackathon 0:4233b7b10255 5255 *
group-Make-NTU-Hackathon 0:4233b7b10255 5256 * @author Steven Levithan <stevenlevithan.com> (MIT license)
group-Make-NTU-Hackathon 0:4233b7b10255 5257 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5258 */
group-Make-NTU-Hackathon 0:4233b7b10255 5259
group-Make-NTU-Hackathon 0:4233b7b10255 5260 var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
group-Make-NTU-Hackathon 0:4233b7b10255 5261
group-Make-NTU-Hackathon 0:4233b7b10255 5262 var parts = [
group-Make-NTU-Hackathon 0:4233b7b10255 5263 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
group-Make-NTU-Hackathon 0:4233b7b10255 5264 ];
group-Make-NTU-Hackathon 0:4233b7b10255 5265
group-Make-NTU-Hackathon 0:4233b7b10255 5266 module.exports = function parseuri(str) {
group-Make-NTU-Hackathon 0:4233b7b10255 5267 var src = str,
group-Make-NTU-Hackathon 0:4233b7b10255 5268 b = str.indexOf('['),
group-Make-NTU-Hackathon 0:4233b7b10255 5269 e = str.indexOf(']');
group-Make-NTU-Hackathon 0:4233b7b10255 5270
group-Make-NTU-Hackathon 0:4233b7b10255 5271 if (b != -1 && e != -1) {
group-Make-NTU-Hackathon 0:4233b7b10255 5272 str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
group-Make-NTU-Hackathon 0:4233b7b10255 5273 }
group-Make-NTU-Hackathon 0:4233b7b10255 5274
group-Make-NTU-Hackathon 0:4233b7b10255 5275 var m = re.exec(str || ''),
group-Make-NTU-Hackathon 0:4233b7b10255 5276 uri = {},
group-Make-NTU-Hackathon 0:4233b7b10255 5277 i = 14;
group-Make-NTU-Hackathon 0:4233b7b10255 5278
group-Make-NTU-Hackathon 0:4233b7b10255 5279 while (i--) {
group-Make-NTU-Hackathon 0:4233b7b10255 5280 uri[parts[i]] = m[i] || '';
group-Make-NTU-Hackathon 0:4233b7b10255 5281 }
group-Make-NTU-Hackathon 0:4233b7b10255 5282
group-Make-NTU-Hackathon 0:4233b7b10255 5283 if (b != -1 && e != -1) {
group-Make-NTU-Hackathon 0:4233b7b10255 5284 uri.source = src;
group-Make-NTU-Hackathon 0:4233b7b10255 5285 uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
group-Make-NTU-Hackathon 0:4233b7b10255 5286 uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
group-Make-NTU-Hackathon 0:4233b7b10255 5287 uri.ipv6uri = true;
group-Make-NTU-Hackathon 0:4233b7b10255 5288 }
group-Make-NTU-Hackathon 0:4233b7b10255 5289
group-Make-NTU-Hackathon 0:4233b7b10255 5290 return uri;
group-Make-NTU-Hackathon 0:4233b7b10255 5291 };
group-Make-NTU-Hackathon 0:4233b7b10255 5292
group-Make-NTU-Hackathon 0:4233b7b10255 5293 },{}],37:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5294
group-Make-NTU-Hackathon 0:4233b7b10255 5295 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5296 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 5297 */
group-Make-NTU-Hackathon 0:4233b7b10255 5298
group-Make-NTU-Hackathon 0:4233b7b10255 5299 var global = (function() { return this; })();
group-Make-NTU-Hackathon 0:4233b7b10255 5300
group-Make-NTU-Hackathon 0:4233b7b10255 5301 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5302 * WebSocket constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 5303 */
group-Make-NTU-Hackathon 0:4233b7b10255 5304
group-Make-NTU-Hackathon 0:4233b7b10255 5305 var WebSocket = global.WebSocket || global.MozWebSocket;
group-Make-NTU-Hackathon 0:4233b7b10255 5306
group-Make-NTU-Hackathon 0:4233b7b10255 5307 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5308 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 5309 */
group-Make-NTU-Hackathon 0:4233b7b10255 5310
group-Make-NTU-Hackathon 0:4233b7b10255 5311 module.exports = WebSocket ? ws : null;
group-Make-NTU-Hackathon 0:4233b7b10255 5312
group-Make-NTU-Hackathon 0:4233b7b10255 5313 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5314 * WebSocket constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 5315 *
group-Make-NTU-Hackathon 0:4233b7b10255 5316 * The third `opts` options object gets ignored in web browsers, since it's
group-Make-NTU-Hackathon 0:4233b7b10255 5317 * non-standard, and throws a TypeError if passed to the constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 5318 * See: https://github.com/einaros/ws/issues/227
group-Make-NTU-Hackathon 0:4233b7b10255 5319 *
group-Make-NTU-Hackathon 0:4233b7b10255 5320 * @param {String} uri
group-Make-NTU-Hackathon 0:4233b7b10255 5321 * @param {Array} protocols (optional)
group-Make-NTU-Hackathon 0:4233b7b10255 5322 * @param {Object) opts (optional)
group-Make-NTU-Hackathon 0:4233b7b10255 5323 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5324 */
group-Make-NTU-Hackathon 0:4233b7b10255 5325
group-Make-NTU-Hackathon 0:4233b7b10255 5326 function ws(uri, protocols, opts) {
group-Make-NTU-Hackathon 0:4233b7b10255 5327 var instance;
group-Make-NTU-Hackathon 0:4233b7b10255 5328 if (protocols) {
group-Make-NTU-Hackathon 0:4233b7b10255 5329 instance = new WebSocket(uri, protocols);
group-Make-NTU-Hackathon 0:4233b7b10255 5330 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 5331 instance = new WebSocket(uri);
group-Make-NTU-Hackathon 0:4233b7b10255 5332 }
group-Make-NTU-Hackathon 0:4233b7b10255 5333 return instance;
group-Make-NTU-Hackathon 0:4233b7b10255 5334 }
group-Make-NTU-Hackathon 0:4233b7b10255 5335
group-Make-NTU-Hackathon 0:4233b7b10255 5336 if (WebSocket) ws.prototype = WebSocket.prototype;
group-Make-NTU-Hackathon 0:4233b7b10255 5337
group-Make-NTU-Hackathon 0:4233b7b10255 5338 },{}],38:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5339 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 5340
group-Make-NTU-Hackathon 0:4233b7b10255 5341 /*
group-Make-NTU-Hackathon 0:4233b7b10255 5342 * Module requirements.
group-Make-NTU-Hackathon 0:4233b7b10255 5343 */
group-Make-NTU-Hackathon 0:4233b7b10255 5344
group-Make-NTU-Hackathon 0:4233b7b10255 5345 var isArray = _dereq_('isarray');
group-Make-NTU-Hackathon 0:4233b7b10255 5346
group-Make-NTU-Hackathon 0:4233b7b10255 5347 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5348 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 5349 */
group-Make-NTU-Hackathon 0:4233b7b10255 5350
group-Make-NTU-Hackathon 0:4233b7b10255 5351 module.exports = hasBinary;
group-Make-NTU-Hackathon 0:4233b7b10255 5352
group-Make-NTU-Hackathon 0:4233b7b10255 5353 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5354 * Checks for binary data.
group-Make-NTU-Hackathon 0:4233b7b10255 5355 *
group-Make-NTU-Hackathon 0:4233b7b10255 5356 * Right now only Buffer and ArrayBuffer are supported..
group-Make-NTU-Hackathon 0:4233b7b10255 5357 *
group-Make-NTU-Hackathon 0:4233b7b10255 5358 * @param {Object} anything
group-Make-NTU-Hackathon 0:4233b7b10255 5359 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5360 */
group-Make-NTU-Hackathon 0:4233b7b10255 5361
group-Make-NTU-Hackathon 0:4233b7b10255 5362 function hasBinary(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5363
group-Make-NTU-Hackathon 0:4233b7b10255 5364 function _hasBinary(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5365 if (!obj) return false;
group-Make-NTU-Hackathon 0:4233b7b10255 5366
group-Make-NTU-Hackathon 0:4233b7b10255 5367 if ( (global.Buffer && global.Buffer.isBuffer(obj)) ||
group-Make-NTU-Hackathon 0:4233b7b10255 5368 (global.ArrayBuffer && obj instanceof ArrayBuffer) ||
group-Make-NTU-Hackathon 0:4233b7b10255 5369 (global.Blob && obj instanceof Blob) ||
group-Make-NTU-Hackathon 0:4233b7b10255 5370 (global.File && obj instanceof File)
group-Make-NTU-Hackathon 0:4233b7b10255 5371 ) {
group-Make-NTU-Hackathon 0:4233b7b10255 5372 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 5373 }
group-Make-NTU-Hackathon 0:4233b7b10255 5374
group-Make-NTU-Hackathon 0:4233b7b10255 5375 if (isArray(obj)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5376 for (var i = 0; i < obj.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 5377 if (_hasBinary(obj[i])) {
group-Make-NTU-Hackathon 0:4233b7b10255 5378 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 5379 }
group-Make-NTU-Hackathon 0:4233b7b10255 5380 }
group-Make-NTU-Hackathon 0:4233b7b10255 5381 } else if (obj && 'object' == typeof obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5382 if (obj.toJSON) {
group-Make-NTU-Hackathon 0:4233b7b10255 5383 obj = obj.toJSON();
group-Make-NTU-Hackathon 0:4233b7b10255 5384 }
group-Make-NTU-Hackathon 0:4233b7b10255 5385
group-Make-NTU-Hackathon 0:4233b7b10255 5386 for (var key in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5387 if (Object.prototype.hasOwnProperty.call(obj, key) && _hasBinary(obj[key])) {
group-Make-NTU-Hackathon 0:4233b7b10255 5388 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 5389 }
group-Make-NTU-Hackathon 0:4233b7b10255 5390 }
group-Make-NTU-Hackathon 0:4233b7b10255 5391 }
group-Make-NTU-Hackathon 0:4233b7b10255 5392
group-Make-NTU-Hackathon 0:4233b7b10255 5393 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 5394 }
group-Make-NTU-Hackathon 0:4233b7b10255 5395
group-Make-NTU-Hackathon 0:4233b7b10255 5396 return _hasBinary(data);
group-Make-NTU-Hackathon 0:4233b7b10255 5397 }
group-Make-NTU-Hackathon 0:4233b7b10255 5398
group-Make-NTU-Hackathon 0:4233b7b10255 5399 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 5400 },{"isarray":39}],39:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5401 module.exports=_dereq_(32)
group-Make-NTU-Hackathon 0:4233b7b10255 5402 },{}],40:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5403
group-Make-NTU-Hackathon 0:4233b7b10255 5404 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5405 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 5406 */
group-Make-NTU-Hackathon 0:4233b7b10255 5407
group-Make-NTU-Hackathon 0:4233b7b10255 5408 var global = _dereq_('global');
group-Make-NTU-Hackathon 0:4233b7b10255 5409
group-Make-NTU-Hackathon 0:4233b7b10255 5410 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5411 * Module exports.
group-Make-NTU-Hackathon 0:4233b7b10255 5412 *
group-Make-NTU-Hackathon 0:4233b7b10255 5413 * Logic borrowed from Modernizr:
group-Make-NTU-Hackathon 0:4233b7b10255 5414 *
group-Make-NTU-Hackathon 0:4233b7b10255 5415 * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js
group-Make-NTU-Hackathon 0:4233b7b10255 5416 */
group-Make-NTU-Hackathon 0:4233b7b10255 5417
group-Make-NTU-Hackathon 0:4233b7b10255 5418 try {
group-Make-NTU-Hackathon 0:4233b7b10255 5419 module.exports = 'XMLHttpRequest' in global &&
group-Make-NTU-Hackathon 0:4233b7b10255 5420 'withCredentials' in new global.XMLHttpRequest();
group-Make-NTU-Hackathon 0:4233b7b10255 5421 } catch (err) {
group-Make-NTU-Hackathon 0:4233b7b10255 5422 // if XMLHttp support is disabled in IE then it will throw
group-Make-NTU-Hackathon 0:4233b7b10255 5423 // when trying to create
group-Make-NTU-Hackathon 0:4233b7b10255 5424 module.exports = false;
group-Make-NTU-Hackathon 0:4233b7b10255 5425 }
group-Make-NTU-Hackathon 0:4233b7b10255 5426
group-Make-NTU-Hackathon 0:4233b7b10255 5427 },{"global":41}],41:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5428
group-Make-NTU-Hackathon 0:4233b7b10255 5429 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5430 * Returns `this`. Execute this without a "context" (i.e. without it being
group-Make-NTU-Hackathon 0:4233b7b10255 5431 * attached to an object of the left-hand side), and `this` points to the
group-Make-NTU-Hackathon 0:4233b7b10255 5432 * "global" scope of the current JS execution.
group-Make-NTU-Hackathon 0:4233b7b10255 5433 */
group-Make-NTU-Hackathon 0:4233b7b10255 5434
group-Make-NTU-Hackathon 0:4233b7b10255 5435 module.exports = (function () { return this; })();
group-Make-NTU-Hackathon 0:4233b7b10255 5436
group-Make-NTU-Hackathon 0:4233b7b10255 5437 },{}],42:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5438
group-Make-NTU-Hackathon 0:4233b7b10255 5439 var indexOf = [].indexOf;
group-Make-NTU-Hackathon 0:4233b7b10255 5440
group-Make-NTU-Hackathon 0:4233b7b10255 5441 module.exports = function(arr, obj){
group-Make-NTU-Hackathon 0:4233b7b10255 5442 if (indexOf) return arr.indexOf(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5443 for (var i = 0; i < arr.length; ++i) {
group-Make-NTU-Hackathon 0:4233b7b10255 5444 if (arr[i] === obj) return i;
group-Make-NTU-Hackathon 0:4233b7b10255 5445 }
group-Make-NTU-Hackathon 0:4233b7b10255 5446 return -1;
group-Make-NTU-Hackathon 0:4233b7b10255 5447 };
group-Make-NTU-Hackathon 0:4233b7b10255 5448 },{}],43:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5449
group-Make-NTU-Hackathon 0:4233b7b10255 5450 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5451 * HOP ref.
group-Make-NTU-Hackathon 0:4233b7b10255 5452 */
group-Make-NTU-Hackathon 0:4233b7b10255 5453
group-Make-NTU-Hackathon 0:4233b7b10255 5454 var has = Object.prototype.hasOwnProperty;
group-Make-NTU-Hackathon 0:4233b7b10255 5455
group-Make-NTU-Hackathon 0:4233b7b10255 5456 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5457 * Return own keys in `obj`.
group-Make-NTU-Hackathon 0:4233b7b10255 5458 *
group-Make-NTU-Hackathon 0:4233b7b10255 5459 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 5460 * @return {Array}
group-Make-NTU-Hackathon 0:4233b7b10255 5461 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5462 */
group-Make-NTU-Hackathon 0:4233b7b10255 5463
group-Make-NTU-Hackathon 0:4233b7b10255 5464 exports.keys = Object.keys || function(obj){
group-Make-NTU-Hackathon 0:4233b7b10255 5465 var keys = [];
group-Make-NTU-Hackathon 0:4233b7b10255 5466 for (var key in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5467 if (has.call(obj, key)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5468 keys.push(key);
group-Make-NTU-Hackathon 0:4233b7b10255 5469 }
group-Make-NTU-Hackathon 0:4233b7b10255 5470 }
group-Make-NTU-Hackathon 0:4233b7b10255 5471 return keys;
group-Make-NTU-Hackathon 0:4233b7b10255 5472 };
group-Make-NTU-Hackathon 0:4233b7b10255 5473
group-Make-NTU-Hackathon 0:4233b7b10255 5474 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5475 * Return own values in `obj`.
group-Make-NTU-Hackathon 0:4233b7b10255 5476 *
group-Make-NTU-Hackathon 0:4233b7b10255 5477 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 5478 * @return {Array}
group-Make-NTU-Hackathon 0:4233b7b10255 5479 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5480 */
group-Make-NTU-Hackathon 0:4233b7b10255 5481
group-Make-NTU-Hackathon 0:4233b7b10255 5482 exports.values = function(obj){
group-Make-NTU-Hackathon 0:4233b7b10255 5483 var vals = [];
group-Make-NTU-Hackathon 0:4233b7b10255 5484 for (var key in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5485 if (has.call(obj, key)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5486 vals.push(obj[key]);
group-Make-NTU-Hackathon 0:4233b7b10255 5487 }
group-Make-NTU-Hackathon 0:4233b7b10255 5488 }
group-Make-NTU-Hackathon 0:4233b7b10255 5489 return vals;
group-Make-NTU-Hackathon 0:4233b7b10255 5490 };
group-Make-NTU-Hackathon 0:4233b7b10255 5491
group-Make-NTU-Hackathon 0:4233b7b10255 5492 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5493 * Merge `b` into `a`.
group-Make-NTU-Hackathon 0:4233b7b10255 5494 *
group-Make-NTU-Hackathon 0:4233b7b10255 5495 * @param {Object} a
group-Make-NTU-Hackathon 0:4233b7b10255 5496 * @param {Object} b
group-Make-NTU-Hackathon 0:4233b7b10255 5497 * @return {Object} a
group-Make-NTU-Hackathon 0:4233b7b10255 5498 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5499 */
group-Make-NTU-Hackathon 0:4233b7b10255 5500
group-Make-NTU-Hackathon 0:4233b7b10255 5501 exports.merge = function(a, b){
group-Make-NTU-Hackathon 0:4233b7b10255 5502 for (var key in b) {
group-Make-NTU-Hackathon 0:4233b7b10255 5503 if (has.call(b, key)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5504 a[key] = b[key];
group-Make-NTU-Hackathon 0:4233b7b10255 5505 }
group-Make-NTU-Hackathon 0:4233b7b10255 5506 }
group-Make-NTU-Hackathon 0:4233b7b10255 5507 return a;
group-Make-NTU-Hackathon 0:4233b7b10255 5508 };
group-Make-NTU-Hackathon 0:4233b7b10255 5509
group-Make-NTU-Hackathon 0:4233b7b10255 5510 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5511 * Return length of `obj`.
group-Make-NTU-Hackathon 0:4233b7b10255 5512 *
group-Make-NTU-Hackathon 0:4233b7b10255 5513 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 5514 * @return {Number}
group-Make-NTU-Hackathon 0:4233b7b10255 5515 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5516 */
group-Make-NTU-Hackathon 0:4233b7b10255 5517
group-Make-NTU-Hackathon 0:4233b7b10255 5518 exports.length = function(obj){
group-Make-NTU-Hackathon 0:4233b7b10255 5519 return exports.keys(obj).length;
group-Make-NTU-Hackathon 0:4233b7b10255 5520 };
group-Make-NTU-Hackathon 0:4233b7b10255 5521
group-Make-NTU-Hackathon 0:4233b7b10255 5522 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5523 * Check if `obj` is empty.
group-Make-NTU-Hackathon 0:4233b7b10255 5524 *
group-Make-NTU-Hackathon 0:4233b7b10255 5525 * @param {Object} obj
group-Make-NTU-Hackathon 0:4233b7b10255 5526 * @return {Boolean}
group-Make-NTU-Hackathon 0:4233b7b10255 5527 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5528 */
group-Make-NTU-Hackathon 0:4233b7b10255 5529
group-Make-NTU-Hackathon 0:4233b7b10255 5530 exports.isEmpty = function(obj){
group-Make-NTU-Hackathon 0:4233b7b10255 5531 return 0 == exports.length(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5532 };
group-Make-NTU-Hackathon 0:4233b7b10255 5533 },{}],44:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5534 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5535 * Parses an URI
group-Make-NTU-Hackathon 0:4233b7b10255 5536 *
group-Make-NTU-Hackathon 0:4233b7b10255 5537 * @author Steven Levithan <stevenlevithan.com> (MIT license)
group-Make-NTU-Hackathon 0:4233b7b10255 5538 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5539 */
group-Make-NTU-Hackathon 0:4233b7b10255 5540
group-Make-NTU-Hackathon 0:4233b7b10255 5541 var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
group-Make-NTU-Hackathon 0:4233b7b10255 5542
group-Make-NTU-Hackathon 0:4233b7b10255 5543 var parts = [
group-Make-NTU-Hackathon 0:4233b7b10255 5544 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host'
group-Make-NTU-Hackathon 0:4233b7b10255 5545 , 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
group-Make-NTU-Hackathon 0:4233b7b10255 5546 ];
group-Make-NTU-Hackathon 0:4233b7b10255 5547
group-Make-NTU-Hackathon 0:4233b7b10255 5548 module.exports = function parseuri(str) {
group-Make-NTU-Hackathon 0:4233b7b10255 5549 var m = re.exec(str || '')
group-Make-NTU-Hackathon 0:4233b7b10255 5550 , uri = {}
group-Make-NTU-Hackathon 0:4233b7b10255 5551 , i = 14;
group-Make-NTU-Hackathon 0:4233b7b10255 5552
group-Make-NTU-Hackathon 0:4233b7b10255 5553 while (i--) {
group-Make-NTU-Hackathon 0:4233b7b10255 5554 uri[parts[i]] = m[i] || '';
group-Make-NTU-Hackathon 0:4233b7b10255 5555 }
group-Make-NTU-Hackathon 0:4233b7b10255 5556
group-Make-NTU-Hackathon 0:4233b7b10255 5557 return uri;
group-Make-NTU-Hackathon 0:4233b7b10255 5558 };
group-Make-NTU-Hackathon 0:4233b7b10255 5559
group-Make-NTU-Hackathon 0:4233b7b10255 5560 },{}],45:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5561 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 5562 /*global Blob,File*/
group-Make-NTU-Hackathon 0:4233b7b10255 5563
group-Make-NTU-Hackathon 0:4233b7b10255 5564 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5565 * Module requirements
group-Make-NTU-Hackathon 0:4233b7b10255 5566 */
group-Make-NTU-Hackathon 0:4233b7b10255 5567
group-Make-NTU-Hackathon 0:4233b7b10255 5568 var isArray = _dereq_('isarray');
group-Make-NTU-Hackathon 0:4233b7b10255 5569 var isBuf = _dereq_('./is-buffer');
group-Make-NTU-Hackathon 0:4233b7b10255 5570
group-Make-NTU-Hackathon 0:4233b7b10255 5571 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5572 * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder.
group-Make-NTU-Hackathon 0:4233b7b10255 5573 * Anything with blobs or files should be fed through removeBlobs before coming
group-Make-NTU-Hackathon 0:4233b7b10255 5574 * here.
group-Make-NTU-Hackathon 0:4233b7b10255 5575 *
group-Make-NTU-Hackathon 0:4233b7b10255 5576 * @param {Object} packet - socket.io event packet
group-Make-NTU-Hackathon 0:4233b7b10255 5577 * @return {Object} with deconstructed packet and list of buffers
group-Make-NTU-Hackathon 0:4233b7b10255 5578 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5579 */
group-Make-NTU-Hackathon 0:4233b7b10255 5580
group-Make-NTU-Hackathon 0:4233b7b10255 5581 exports.deconstructPacket = function(packet){
group-Make-NTU-Hackathon 0:4233b7b10255 5582 var buffers = [];
group-Make-NTU-Hackathon 0:4233b7b10255 5583 var packetData = packet.data;
group-Make-NTU-Hackathon 0:4233b7b10255 5584
group-Make-NTU-Hackathon 0:4233b7b10255 5585 function _deconstructPacket(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5586 if (!data) return data;
group-Make-NTU-Hackathon 0:4233b7b10255 5587
group-Make-NTU-Hackathon 0:4233b7b10255 5588 if (isBuf(data)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5589 var placeholder = { _placeholder: true, num: buffers.length };
group-Make-NTU-Hackathon 0:4233b7b10255 5590 buffers.push(data);
group-Make-NTU-Hackathon 0:4233b7b10255 5591 return placeholder;
group-Make-NTU-Hackathon 0:4233b7b10255 5592 } else if (isArray(data)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5593 var newData = new Array(data.length);
group-Make-NTU-Hackathon 0:4233b7b10255 5594 for (var i = 0; i < data.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 5595 newData[i] = _deconstructPacket(data[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 5596 }
group-Make-NTU-Hackathon 0:4233b7b10255 5597 return newData;
group-Make-NTU-Hackathon 0:4233b7b10255 5598 } else if ('object' == typeof data && !(data instanceof Date)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5599 var newData = {};
group-Make-NTU-Hackathon 0:4233b7b10255 5600 for (var key in data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5601 newData[key] = _deconstructPacket(data[key]);
group-Make-NTU-Hackathon 0:4233b7b10255 5602 }
group-Make-NTU-Hackathon 0:4233b7b10255 5603 return newData;
group-Make-NTU-Hackathon 0:4233b7b10255 5604 }
group-Make-NTU-Hackathon 0:4233b7b10255 5605 return data;
group-Make-NTU-Hackathon 0:4233b7b10255 5606 }
group-Make-NTU-Hackathon 0:4233b7b10255 5607
group-Make-NTU-Hackathon 0:4233b7b10255 5608 var pack = packet;
group-Make-NTU-Hackathon 0:4233b7b10255 5609 pack.data = _deconstructPacket(packetData);
group-Make-NTU-Hackathon 0:4233b7b10255 5610 pack.attachments = buffers.length; // number of binary 'attachments'
group-Make-NTU-Hackathon 0:4233b7b10255 5611 return {packet: pack, buffers: buffers};
group-Make-NTU-Hackathon 0:4233b7b10255 5612 };
group-Make-NTU-Hackathon 0:4233b7b10255 5613
group-Make-NTU-Hackathon 0:4233b7b10255 5614 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5615 * Reconstructs a binary packet from its placeholder packet and buffers
group-Make-NTU-Hackathon 0:4233b7b10255 5616 *
group-Make-NTU-Hackathon 0:4233b7b10255 5617 * @param {Object} packet - event packet with placeholders
group-Make-NTU-Hackathon 0:4233b7b10255 5618 * @param {Array} buffers - binary buffers to put in placeholder positions
group-Make-NTU-Hackathon 0:4233b7b10255 5619 * @return {Object} reconstructed packet
group-Make-NTU-Hackathon 0:4233b7b10255 5620 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5621 */
group-Make-NTU-Hackathon 0:4233b7b10255 5622
group-Make-NTU-Hackathon 0:4233b7b10255 5623 exports.reconstructPacket = function(packet, buffers) {
group-Make-NTU-Hackathon 0:4233b7b10255 5624 var curPlaceHolder = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 5625
group-Make-NTU-Hackathon 0:4233b7b10255 5626 function _reconstructPacket(data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5627 if (data && data._placeholder) {
group-Make-NTU-Hackathon 0:4233b7b10255 5628 var buf = buffers[data.num]; // appropriate buffer (should be natural order anyway)
group-Make-NTU-Hackathon 0:4233b7b10255 5629 return buf;
group-Make-NTU-Hackathon 0:4233b7b10255 5630 } else if (isArray(data)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5631 for (var i = 0; i < data.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 5632 data[i] = _reconstructPacket(data[i]);
group-Make-NTU-Hackathon 0:4233b7b10255 5633 }
group-Make-NTU-Hackathon 0:4233b7b10255 5634 return data;
group-Make-NTU-Hackathon 0:4233b7b10255 5635 } else if (data && 'object' == typeof data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5636 for (var key in data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5637 data[key] = _reconstructPacket(data[key]);
group-Make-NTU-Hackathon 0:4233b7b10255 5638 }
group-Make-NTU-Hackathon 0:4233b7b10255 5639 return data;
group-Make-NTU-Hackathon 0:4233b7b10255 5640 }
group-Make-NTU-Hackathon 0:4233b7b10255 5641 return data;
group-Make-NTU-Hackathon 0:4233b7b10255 5642 }
group-Make-NTU-Hackathon 0:4233b7b10255 5643
group-Make-NTU-Hackathon 0:4233b7b10255 5644 packet.data = _reconstructPacket(packet.data);
group-Make-NTU-Hackathon 0:4233b7b10255 5645 packet.attachments = undefined; // no longer useful
group-Make-NTU-Hackathon 0:4233b7b10255 5646 return packet;
group-Make-NTU-Hackathon 0:4233b7b10255 5647 };
group-Make-NTU-Hackathon 0:4233b7b10255 5648
group-Make-NTU-Hackathon 0:4233b7b10255 5649 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5650 * Asynchronously removes Blobs or Files from data via
group-Make-NTU-Hackathon 0:4233b7b10255 5651 * FileReader's readAsArrayBuffer method. Used before encoding
group-Make-NTU-Hackathon 0:4233b7b10255 5652 * data as msgpack. Calls callback with the blobless data.
group-Make-NTU-Hackathon 0:4233b7b10255 5653 *
group-Make-NTU-Hackathon 0:4233b7b10255 5654 * @param {Object} data
group-Make-NTU-Hackathon 0:4233b7b10255 5655 * @param {Function} callback
group-Make-NTU-Hackathon 0:4233b7b10255 5656 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5657 */
group-Make-NTU-Hackathon 0:4233b7b10255 5658
group-Make-NTU-Hackathon 0:4233b7b10255 5659 exports.removeBlobs = function(data, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 5660 function _removeBlobs(obj, curKey, containingObject) {
group-Make-NTU-Hackathon 0:4233b7b10255 5661 if (!obj) return obj;
group-Make-NTU-Hackathon 0:4233b7b10255 5662
group-Make-NTU-Hackathon 0:4233b7b10255 5663 // convert any blob
group-Make-NTU-Hackathon 0:4233b7b10255 5664 if ((global.Blob && obj instanceof Blob) ||
group-Make-NTU-Hackathon 0:4233b7b10255 5665 (global.File && obj instanceof File)) {
group-Make-NTU-Hackathon 0:4233b7b10255 5666 pendingBlobs++;
group-Make-NTU-Hackathon 0:4233b7b10255 5667
group-Make-NTU-Hackathon 0:4233b7b10255 5668 // async filereader
group-Make-NTU-Hackathon 0:4233b7b10255 5669 var fileReader = new FileReader();
group-Make-NTU-Hackathon 0:4233b7b10255 5670 fileReader.onload = function() { // this.result == arraybuffer
group-Make-NTU-Hackathon 0:4233b7b10255 5671 if (containingObject) {
group-Make-NTU-Hackathon 0:4233b7b10255 5672 containingObject[curKey] = this.result;
group-Make-NTU-Hackathon 0:4233b7b10255 5673 }
group-Make-NTU-Hackathon 0:4233b7b10255 5674 else {
group-Make-NTU-Hackathon 0:4233b7b10255 5675 bloblessData = this.result;
group-Make-NTU-Hackathon 0:4233b7b10255 5676 }
group-Make-NTU-Hackathon 0:4233b7b10255 5677
group-Make-NTU-Hackathon 0:4233b7b10255 5678 // if nothing pending its callback time
group-Make-NTU-Hackathon 0:4233b7b10255 5679 if(! --pendingBlobs) {
group-Make-NTU-Hackathon 0:4233b7b10255 5680 callback(bloblessData);
group-Make-NTU-Hackathon 0:4233b7b10255 5681 }
group-Make-NTU-Hackathon 0:4233b7b10255 5682 };
group-Make-NTU-Hackathon 0:4233b7b10255 5683
group-Make-NTU-Hackathon 0:4233b7b10255 5684 fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer
group-Make-NTU-Hackathon 0:4233b7b10255 5685 } else if (isArray(obj)) { // handle array
group-Make-NTU-Hackathon 0:4233b7b10255 5686 for (var i = 0; i < obj.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 5687 _removeBlobs(obj[i], i, obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5688 }
group-Make-NTU-Hackathon 0:4233b7b10255 5689 } else if (obj && 'object' == typeof obj && !isBuf(obj)) { // and object
group-Make-NTU-Hackathon 0:4233b7b10255 5690 for (var key in obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5691 _removeBlobs(obj[key], key, obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5692 }
group-Make-NTU-Hackathon 0:4233b7b10255 5693 }
group-Make-NTU-Hackathon 0:4233b7b10255 5694 }
group-Make-NTU-Hackathon 0:4233b7b10255 5695
group-Make-NTU-Hackathon 0:4233b7b10255 5696 var pendingBlobs = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 5697 var bloblessData = data;
group-Make-NTU-Hackathon 0:4233b7b10255 5698 _removeBlobs(bloblessData);
group-Make-NTU-Hackathon 0:4233b7b10255 5699 if (!pendingBlobs) {
group-Make-NTU-Hackathon 0:4233b7b10255 5700 callback(bloblessData);
group-Make-NTU-Hackathon 0:4233b7b10255 5701 }
group-Make-NTU-Hackathon 0:4233b7b10255 5702 };
group-Make-NTU-Hackathon 0:4233b7b10255 5703
group-Make-NTU-Hackathon 0:4233b7b10255 5704 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 5705 },{"./is-buffer":47,"isarray":48}],46:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 5706
group-Make-NTU-Hackathon 0:4233b7b10255 5707 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5708 * Module dependencies.
group-Make-NTU-Hackathon 0:4233b7b10255 5709 */
group-Make-NTU-Hackathon 0:4233b7b10255 5710
group-Make-NTU-Hackathon 0:4233b7b10255 5711 var debug = _dereq_('debug')('socket.io-parser');
group-Make-NTU-Hackathon 0:4233b7b10255 5712 var json = _dereq_('json3');
group-Make-NTU-Hackathon 0:4233b7b10255 5713 var isArray = _dereq_('isarray');
group-Make-NTU-Hackathon 0:4233b7b10255 5714 var Emitter = _dereq_('component-emitter');
group-Make-NTU-Hackathon 0:4233b7b10255 5715 var binary = _dereq_('./binary');
group-Make-NTU-Hackathon 0:4233b7b10255 5716 var isBuf = _dereq_('./is-buffer');
group-Make-NTU-Hackathon 0:4233b7b10255 5717
group-Make-NTU-Hackathon 0:4233b7b10255 5718 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5719 * Protocol version.
group-Make-NTU-Hackathon 0:4233b7b10255 5720 *
group-Make-NTU-Hackathon 0:4233b7b10255 5721 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5722 */
group-Make-NTU-Hackathon 0:4233b7b10255 5723
group-Make-NTU-Hackathon 0:4233b7b10255 5724 exports.protocol = 4;
group-Make-NTU-Hackathon 0:4233b7b10255 5725
group-Make-NTU-Hackathon 0:4233b7b10255 5726 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5727 * Packet types.
group-Make-NTU-Hackathon 0:4233b7b10255 5728 *
group-Make-NTU-Hackathon 0:4233b7b10255 5729 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5730 */
group-Make-NTU-Hackathon 0:4233b7b10255 5731
group-Make-NTU-Hackathon 0:4233b7b10255 5732 exports.types = [
group-Make-NTU-Hackathon 0:4233b7b10255 5733 'CONNECT',
group-Make-NTU-Hackathon 0:4233b7b10255 5734 'DISCONNECT',
group-Make-NTU-Hackathon 0:4233b7b10255 5735 'EVENT',
group-Make-NTU-Hackathon 0:4233b7b10255 5736 'BINARY_EVENT',
group-Make-NTU-Hackathon 0:4233b7b10255 5737 'ACK',
group-Make-NTU-Hackathon 0:4233b7b10255 5738 'BINARY_ACK',
group-Make-NTU-Hackathon 0:4233b7b10255 5739 'ERROR'
group-Make-NTU-Hackathon 0:4233b7b10255 5740 ];
group-Make-NTU-Hackathon 0:4233b7b10255 5741
group-Make-NTU-Hackathon 0:4233b7b10255 5742 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5743 * Packet type `connect`.
group-Make-NTU-Hackathon 0:4233b7b10255 5744 *
group-Make-NTU-Hackathon 0:4233b7b10255 5745 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5746 */
group-Make-NTU-Hackathon 0:4233b7b10255 5747
group-Make-NTU-Hackathon 0:4233b7b10255 5748 exports.CONNECT = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 5749
group-Make-NTU-Hackathon 0:4233b7b10255 5750 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5751 * Packet type `disconnect`.
group-Make-NTU-Hackathon 0:4233b7b10255 5752 *
group-Make-NTU-Hackathon 0:4233b7b10255 5753 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5754 */
group-Make-NTU-Hackathon 0:4233b7b10255 5755
group-Make-NTU-Hackathon 0:4233b7b10255 5756 exports.DISCONNECT = 1;
group-Make-NTU-Hackathon 0:4233b7b10255 5757
group-Make-NTU-Hackathon 0:4233b7b10255 5758 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5759 * Packet type `event`.
group-Make-NTU-Hackathon 0:4233b7b10255 5760 *
group-Make-NTU-Hackathon 0:4233b7b10255 5761 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5762 */
group-Make-NTU-Hackathon 0:4233b7b10255 5763
group-Make-NTU-Hackathon 0:4233b7b10255 5764 exports.EVENT = 2;
group-Make-NTU-Hackathon 0:4233b7b10255 5765
group-Make-NTU-Hackathon 0:4233b7b10255 5766 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5767 * Packet type `ack`.
group-Make-NTU-Hackathon 0:4233b7b10255 5768 *
group-Make-NTU-Hackathon 0:4233b7b10255 5769 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5770 */
group-Make-NTU-Hackathon 0:4233b7b10255 5771
group-Make-NTU-Hackathon 0:4233b7b10255 5772 exports.ACK = 3;
group-Make-NTU-Hackathon 0:4233b7b10255 5773
group-Make-NTU-Hackathon 0:4233b7b10255 5774 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5775 * Packet type `error`.
group-Make-NTU-Hackathon 0:4233b7b10255 5776 *
group-Make-NTU-Hackathon 0:4233b7b10255 5777 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5778 */
group-Make-NTU-Hackathon 0:4233b7b10255 5779
group-Make-NTU-Hackathon 0:4233b7b10255 5780 exports.ERROR = 4;
group-Make-NTU-Hackathon 0:4233b7b10255 5781
group-Make-NTU-Hackathon 0:4233b7b10255 5782 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5783 * Packet type 'binary event'
group-Make-NTU-Hackathon 0:4233b7b10255 5784 *
group-Make-NTU-Hackathon 0:4233b7b10255 5785 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5786 */
group-Make-NTU-Hackathon 0:4233b7b10255 5787
group-Make-NTU-Hackathon 0:4233b7b10255 5788 exports.BINARY_EVENT = 5;
group-Make-NTU-Hackathon 0:4233b7b10255 5789
group-Make-NTU-Hackathon 0:4233b7b10255 5790 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5791 * Packet type `binary ack`. For acks with binary arguments.
group-Make-NTU-Hackathon 0:4233b7b10255 5792 *
group-Make-NTU-Hackathon 0:4233b7b10255 5793 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5794 */
group-Make-NTU-Hackathon 0:4233b7b10255 5795
group-Make-NTU-Hackathon 0:4233b7b10255 5796 exports.BINARY_ACK = 6;
group-Make-NTU-Hackathon 0:4233b7b10255 5797
group-Make-NTU-Hackathon 0:4233b7b10255 5798 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5799 * Encoder constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 5800 *
group-Make-NTU-Hackathon 0:4233b7b10255 5801 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5802 */
group-Make-NTU-Hackathon 0:4233b7b10255 5803
group-Make-NTU-Hackathon 0:4233b7b10255 5804 exports.Encoder = Encoder;
group-Make-NTU-Hackathon 0:4233b7b10255 5805
group-Make-NTU-Hackathon 0:4233b7b10255 5806 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5807 * Decoder constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 5808 *
group-Make-NTU-Hackathon 0:4233b7b10255 5809 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5810 */
group-Make-NTU-Hackathon 0:4233b7b10255 5811
group-Make-NTU-Hackathon 0:4233b7b10255 5812 exports.Decoder = Decoder;
group-Make-NTU-Hackathon 0:4233b7b10255 5813
group-Make-NTU-Hackathon 0:4233b7b10255 5814 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5815 * A socket.io Encoder instance
group-Make-NTU-Hackathon 0:4233b7b10255 5816 *
group-Make-NTU-Hackathon 0:4233b7b10255 5817 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5818 */
group-Make-NTU-Hackathon 0:4233b7b10255 5819
group-Make-NTU-Hackathon 0:4233b7b10255 5820 function Encoder() {}
group-Make-NTU-Hackathon 0:4233b7b10255 5821
group-Make-NTU-Hackathon 0:4233b7b10255 5822 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5823 * Encode a packet as a single string if non-binary, or as a
group-Make-NTU-Hackathon 0:4233b7b10255 5824 * buffer sequence, depending on packet type.
group-Make-NTU-Hackathon 0:4233b7b10255 5825 *
group-Make-NTU-Hackathon 0:4233b7b10255 5826 * @param {Object} obj - packet object
group-Make-NTU-Hackathon 0:4233b7b10255 5827 * @param {Function} callback - function to handle encodings (likely engine.write)
group-Make-NTU-Hackathon 0:4233b7b10255 5828 * @return Calls callback with Array of encodings
group-Make-NTU-Hackathon 0:4233b7b10255 5829 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5830 */
group-Make-NTU-Hackathon 0:4233b7b10255 5831
group-Make-NTU-Hackathon 0:4233b7b10255 5832 Encoder.prototype.encode = function(obj, callback){
group-Make-NTU-Hackathon 0:4233b7b10255 5833 debug('encoding packet %j', obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5834
group-Make-NTU-Hackathon 0:4233b7b10255 5835 if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 5836 encodeAsBinary(obj, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 5837 }
group-Make-NTU-Hackathon 0:4233b7b10255 5838 else {
group-Make-NTU-Hackathon 0:4233b7b10255 5839 var encoding = encodeAsString(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5840 callback([encoding]);
group-Make-NTU-Hackathon 0:4233b7b10255 5841 }
group-Make-NTU-Hackathon 0:4233b7b10255 5842 };
group-Make-NTU-Hackathon 0:4233b7b10255 5843
group-Make-NTU-Hackathon 0:4233b7b10255 5844 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5845 * Encode packet as string.
group-Make-NTU-Hackathon 0:4233b7b10255 5846 *
group-Make-NTU-Hackathon 0:4233b7b10255 5847 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 5848 * @return {String} encoded
group-Make-NTU-Hackathon 0:4233b7b10255 5849 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5850 */
group-Make-NTU-Hackathon 0:4233b7b10255 5851
group-Make-NTU-Hackathon 0:4233b7b10255 5852 function encodeAsString(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5853 var str = '';
group-Make-NTU-Hackathon 0:4233b7b10255 5854 var nsp = false;
group-Make-NTU-Hackathon 0:4233b7b10255 5855
group-Make-NTU-Hackathon 0:4233b7b10255 5856 // first is type
group-Make-NTU-Hackathon 0:4233b7b10255 5857 str += obj.type;
group-Make-NTU-Hackathon 0:4233b7b10255 5858
group-Make-NTU-Hackathon 0:4233b7b10255 5859 // attachments if we have them
group-Make-NTU-Hackathon 0:4233b7b10255 5860 if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 5861 str += obj.attachments;
group-Make-NTU-Hackathon 0:4233b7b10255 5862 str += '-';
group-Make-NTU-Hackathon 0:4233b7b10255 5863 }
group-Make-NTU-Hackathon 0:4233b7b10255 5864
group-Make-NTU-Hackathon 0:4233b7b10255 5865 // if we have a namespace other than `/`
group-Make-NTU-Hackathon 0:4233b7b10255 5866 // we append it followed by a comma `,`
group-Make-NTU-Hackathon 0:4233b7b10255 5867 if (obj.nsp && '/' != obj.nsp) {
group-Make-NTU-Hackathon 0:4233b7b10255 5868 nsp = true;
group-Make-NTU-Hackathon 0:4233b7b10255 5869 str += obj.nsp;
group-Make-NTU-Hackathon 0:4233b7b10255 5870 }
group-Make-NTU-Hackathon 0:4233b7b10255 5871
group-Make-NTU-Hackathon 0:4233b7b10255 5872 // immediately followed by the id
group-Make-NTU-Hackathon 0:4233b7b10255 5873 if (null != obj.id) {
group-Make-NTU-Hackathon 0:4233b7b10255 5874 if (nsp) {
group-Make-NTU-Hackathon 0:4233b7b10255 5875 str += ',';
group-Make-NTU-Hackathon 0:4233b7b10255 5876 nsp = false;
group-Make-NTU-Hackathon 0:4233b7b10255 5877 }
group-Make-NTU-Hackathon 0:4233b7b10255 5878 str += obj.id;
group-Make-NTU-Hackathon 0:4233b7b10255 5879 }
group-Make-NTU-Hackathon 0:4233b7b10255 5880
group-Make-NTU-Hackathon 0:4233b7b10255 5881 // json data
group-Make-NTU-Hackathon 0:4233b7b10255 5882 if (null != obj.data) {
group-Make-NTU-Hackathon 0:4233b7b10255 5883 if (nsp) str += ',';
group-Make-NTU-Hackathon 0:4233b7b10255 5884 str += json.stringify(obj.data);
group-Make-NTU-Hackathon 0:4233b7b10255 5885 }
group-Make-NTU-Hackathon 0:4233b7b10255 5886
group-Make-NTU-Hackathon 0:4233b7b10255 5887 debug('encoded %j as %s', obj, str);
group-Make-NTU-Hackathon 0:4233b7b10255 5888 return str;
group-Make-NTU-Hackathon 0:4233b7b10255 5889 }
group-Make-NTU-Hackathon 0:4233b7b10255 5890
group-Make-NTU-Hackathon 0:4233b7b10255 5891 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5892 * Encode packet as 'buffer sequence' by removing blobs, and
group-Make-NTU-Hackathon 0:4233b7b10255 5893 * deconstructing packet into object with placeholders and
group-Make-NTU-Hackathon 0:4233b7b10255 5894 * a list of buffers.
group-Make-NTU-Hackathon 0:4233b7b10255 5895 *
group-Make-NTU-Hackathon 0:4233b7b10255 5896 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 5897 * @return {Buffer} encoded
group-Make-NTU-Hackathon 0:4233b7b10255 5898 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5899 */
group-Make-NTU-Hackathon 0:4233b7b10255 5900
group-Make-NTU-Hackathon 0:4233b7b10255 5901 function encodeAsBinary(obj, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 5902
group-Make-NTU-Hackathon 0:4233b7b10255 5903 function writeEncoding(bloblessData) {
group-Make-NTU-Hackathon 0:4233b7b10255 5904 var deconstruction = binary.deconstructPacket(bloblessData);
group-Make-NTU-Hackathon 0:4233b7b10255 5905 var pack = encodeAsString(deconstruction.packet);
group-Make-NTU-Hackathon 0:4233b7b10255 5906 var buffers = deconstruction.buffers;
group-Make-NTU-Hackathon 0:4233b7b10255 5907
group-Make-NTU-Hackathon 0:4233b7b10255 5908 buffers.unshift(pack); // add packet info to beginning of data list
group-Make-NTU-Hackathon 0:4233b7b10255 5909 callback(buffers); // write all the buffers
group-Make-NTU-Hackathon 0:4233b7b10255 5910 }
group-Make-NTU-Hackathon 0:4233b7b10255 5911
group-Make-NTU-Hackathon 0:4233b7b10255 5912 binary.removeBlobs(obj, writeEncoding);
group-Make-NTU-Hackathon 0:4233b7b10255 5913 }
group-Make-NTU-Hackathon 0:4233b7b10255 5914
group-Make-NTU-Hackathon 0:4233b7b10255 5915 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5916 * A socket.io Decoder instance
group-Make-NTU-Hackathon 0:4233b7b10255 5917 *
group-Make-NTU-Hackathon 0:4233b7b10255 5918 * @return {Object} decoder
group-Make-NTU-Hackathon 0:4233b7b10255 5919 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5920 */
group-Make-NTU-Hackathon 0:4233b7b10255 5921
group-Make-NTU-Hackathon 0:4233b7b10255 5922 function Decoder() {
group-Make-NTU-Hackathon 0:4233b7b10255 5923 this.reconstructor = null;
group-Make-NTU-Hackathon 0:4233b7b10255 5924 }
group-Make-NTU-Hackathon 0:4233b7b10255 5925
group-Make-NTU-Hackathon 0:4233b7b10255 5926 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5927 * Mix in `Emitter` with Decoder.
group-Make-NTU-Hackathon 0:4233b7b10255 5928 */
group-Make-NTU-Hackathon 0:4233b7b10255 5929
group-Make-NTU-Hackathon 0:4233b7b10255 5930 Emitter(Decoder.prototype);
group-Make-NTU-Hackathon 0:4233b7b10255 5931
group-Make-NTU-Hackathon 0:4233b7b10255 5932 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5933 * Decodes an ecoded packet string into packet JSON.
group-Make-NTU-Hackathon 0:4233b7b10255 5934 *
group-Make-NTU-Hackathon 0:4233b7b10255 5935 * @param {String} obj - encoded packet
group-Make-NTU-Hackathon 0:4233b7b10255 5936 * @return {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 5937 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 5938 */
group-Make-NTU-Hackathon 0:4233b7b10255 5939
group-Make-NTU-Hackathon 0:4233b7b10255 5940 Decoder.prototype.add = function(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5941 var packet;
group-Make-NTU-Hackathon 0:4233b7b10255 5942 if ('string' == typeof obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 5943 packet = decodeString(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5944 if (exports.BINARY_EVENT == packet.type || exports.BINARY_ACK == packet.type) { // binary packet's json
group-Make-NTU-Hackathon 0:4233b7b10255 5945 this.reconstructor = new BinaryReconstructor(packet);
group-Make-NTU-Hackathon 0:4233b7b10255 5946
group-Make-NTU-Hackathon 0:4233b7b10255 5947 // no attachments, labeled binary but no binary data to follow
group-Make-NTU-Hackathon 0:4233b7b10255 5948 if (this.reconstructor.reconPack.attachments === 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 5949 this.emit('decoded', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 5950 }
group-Make-NTU-Hackathon 0:4233b7b10255 5951 } else { // non-binary full packet
group-Make-NTU-Hackathon 0:4233b7b10255 5952 this.emit('decoded', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 5953 }
group-Make-NTU-Hackathon 0:4233b7b10255 5954 }
group-Make-NTU-Hackathon 0:4233b7b10255 5955 else if (isBuf(obj) || obj.base64) { // raw binary data
group-Make-NTU-Hackathon 0:4233b7b10255 5956 if (!this.reconstructor) {
group-Make-NTU-Hackathon 0:4233b7b10255 5957 throw new Error('got binary data when not reconstructing a packet');
group-Make-NTU-Hackathon 0:4233b7b10255 5958 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 5959 packet = this.reconstructor.takeBinaryData(obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5960 if (packet) { // received final buffer
group-Make-NTU-Hackathon 0:4233b7b10255 5961 this.reconstructor = null;
group-Make-NTU-Hackathon 0:4233b7b10255 5962 this.emit('decoded', packet);
group-Make-NTU-Hackathon 0:4233b7b10255 5963 }
group-Make-NTU-Hackathon 0:4233b7b10255 5964 }
group-Make-NTU-Hackathon 0:4233b7b10255 5965 }
group-Make-NTU-Hackathon 0:4233b7b10255 5966 else {
group-Make-NTU-Hackathon 0:4233b7b10255 5967 throw new Error('Unknown type: ' + obj);
group-Make-NTU-Hackathon 0:4233b7b10255 5968 }
group-Make-NTU-Hackathon 0:4233b7b10255 5969 };
group-Make-NTU-Hackathon 0:4233b7b10255 5970
group-Make-NTU-Hackathon 0:4233b7b10255 5971 /**
group-Make-NTU-Hackathon 0:4233b7b10255 5972 * Decode a packet String (JSON data)
group-Make-NTU-Hackathon 0:4233b7b10255 5973 *
group-Make-NTU-Hackathon 0:4233b7b10255 5974 * @param {String} str
group-Make-NTU-Hackathon 0:4233b7b10255 5975 * @return {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 5976 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 5977 */
group-Make-NTU-Hackathon 0:4233b7b10255 5978
group-Make-NTU-Hackathon 0:4233b7b10255 5979 function decodeString(str) {
group-Make-NTU-Hackathon 0:4233b7b10255 5980 var p = {};
group-Make-NTU-Hackathon 0:4233b7b10255 5981 var i = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 5982
group-Make-NTU-Hackathon 0:4233b7b10255 5983 // look up type
group-Make-NTU-Hackathon 0:4233b7b10255 5984 p.type = Number(str.charAt(0));
group-Make-NTU-Hackathon 0:4233b7b10255 5985 if (null == exports.types[p.type]) return error();
group-Make-NTU-Hackathon 0:4233b7b10255 5986
group-Make-NTU-Hackathon 0:4233b7b10255 5987 // look up attachments if type binary
group-Make-NTU-Hackathon 0:4233b7b10255 5988 if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) {
group-Make-NTU-Hackathon 0:4233b7b10255 5989 var buf = '';
group-Make-NTU-Hackathon 0:4233b7b10255 5990 while (str.charAt(++i) != '-') {
group-Make-NTU-Hackathon 0:4233b7b10255 5991 buf += str.charAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 5992 if (i == str.length) break;
group-Make-NTU-Hackathon 0:4233b7b10255 5993 }
group-Make-NTU-Hackathon 0:4233b7b10255 5994 if (buf != Number(buf) || str.charAt(i) != '-') {
group-Make-NTU-Hackathon 0:4233b7b10255 5995 throw new Error('Illegal attachments');
group-Make-NTU-Hackathon 0:4233b7b10255 5996 }
group-Make-NTU-Hackathon 0:4233b7b10255 5997 p.attachments = Number(buf);
group-Make-NTU-Hackathon 0:4233b7b10255 5998 }
group-Make-NTU-Hackathon 0:4233b7b10255 5999
group-Make-NTU-Hackathon 0:4233b7b10255 6000 // look up namespace (if any)
group-Make-NTU-Hackathon 0:4233b7b10255 6001 if ('/' == str.charAt(i + 1)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6002 p.nsp = '';
group-Make-NTU-Hackathon 0:4233b7b10255 6003 while (++i) {
group-Make-NTU-Hackathon 0:4233b7b10255 6004 var c = str.charAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 6005 if (',' == c) break;
group-Make-NTU-Hackathon 0:4233b7b10255 6006 p.nsp += c;
group-Make-NTU-Hackathon 0:4233b7b10255 6007 if (i == str.length) break;
group-Make-NTU-Hackathon 0:4233b7b10255 6008 }
group-Make-NTU-Hackathon 0:4233b7b10255 6009 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6010 p.nsp = '/';
group-Make-NTU-Hackathon 0:4233b7b10255 6011 }
group-Make-NTU-Hackathon 0:4233b7b10255 6012
group-Make-NTU-Hackathon 0:4233b7b10255 6013 // look up id
group-Make-NTU-Hackathon 0:4233b7b10255 6014 var next = str.charAt(i + 1);
group-Make-NTU-Hackathon 0:4233b7b10255 6015 if ('' !== next && Number(next) == next) {
group-Make-NTU-Hackathon 0:4233b7b10255 6016 p.id = '';
group-Make-NTU-Hackathon 0:4233b7b10255 6017 while (++i) {
group-Make-NTU-Hackathon 0:4233b7b10255 6018 var c = str.charAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 6019 if (null == c || Number(c) != c) {
group-Make-NTU-Hackathon 0:4233b7b10255 6020 --i;
group-Make-NTU-Hackathon 0:4233b7b10255 6021 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6022 }
group-Make-NTU-Hackathon 0:4233b7b10255 6023 p.id += str.charAt(i);
group-Make-NTU-Hackathon 0:4233b7b10255 6024 if (i == str.length) break;
group-Make-NTU-Hackathon 0:4233b7b10255 6025 }
group-Make-NTU-Hackathon 0:4233b7b10255 6026 p.id = Number(p.id);
group-Make-NTU-Hackathon 0:4233b7b10255 6027 }
group-Make-NTU-Hackathon 0:4233b7b10255 6028
group-Make-NTU-Hackathon 0:4233b7b10255 6029 // look up json data
group-Make-NTU-Hackathon 0:4233b7b10255 6030 if (str.charAt(++i)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6031 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6032 p.data = json.parse(str.substr(i));
group-Make-NTU-Hackathon 0:4233b7b10255 6033 } catch(e){
group-Make-NTU-Hackathon 0:4233b7b10255 6034 return error();
group-Make-NTU-Hackathon 0:4233b7b10255 6035 }
group-Make-NTU-Hackathon 0:4233b7b10255 6036 }
group-Make-NTU-Hackathon 0:4233b7b10255 6037
group-Make-NTU-Hackathon 0:4233b7b10255 6038 debug('decoded %s as %j', str, p);
group-Make-NTU-Hackathon 0:4233b7b10255 6039 return p;
group-Make-NTU-Hackathon 0:4233b7b10255 6040 }
group-Make-NTU-Hackathon 0:4233b7b10255 6041
group-Make-NTU-Hackathon 0:4233b7b10255 6042 /**
group-Make-NTU-Hackathon 0:4233b7b10255 6043 * Deallocates a parser's resources
group-Make-NTU-Hackathon 0:4233b7b10255 6044 *
group-Make-NTU-Hackathon 0:4233b7b10255 6045 * @api public
group-Make-NTU-Hackathon 0:4233b7b10255 6046 */
group-Make-NTU-Hackathon 0:4233b7b10255 6047
group-Make-NTU-Hackathon 0:4233b7b10255 6048 Decoder.prototype.destroy = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 6049 if (this.reconstructor) {
group-Make-NTU-Hackathon 0:4233b7b10255 6050 this.reconstructor.finishedReconstruction();
group-Make-NTU-Hackathon 0:4233b7b10255 6051 }
group-Make-NTU-Hackathon 0:4233b7b10255 6052 };
group-Make-NTU-Hackathon 0:4233b7b10255 6053
group-Make-NTU-Hackathon 0:4233b7b10255 6054 /**
group-Make-NTU-Hackathon 0:4233b7b10255 6055 * A manager of a binary event's 'buffer sequence'. Should
group-Make-NTU-Hackathon 0:4233b7b10255 6056 * be constructed whenever a packet of type BINARY_EVENT is
group-Make-NTU-Hackathon 0:4233b7b10255 6057 * decoded.
group-Make-NTU-Hackathon 0:4233b7b10255 6058 *
group-Make-NTU-Hackathon 0:4233b7b10255 6059 * @param {Object} packet
group-Make-NTU-Hackathon 0:4233b7b10255 6060 * @return {BinaryReconstructor} initialized reconstructor
group-Make-NTU-Hackathon 0:4233b7b10255 6061 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 6062 */
group-Make-NTU-Hackathon 0:4233b7b10255 6063
group-Make-NTU-Hackathon 0:4233b7b10255 6064 function BinaryReconstructor(packet) {
group-Make-NTU-Hackathon 0:4233b7b10255 6065 this.reconPack = packet;
group-Make-NTU-Hackathon 0:4233b7b10255 6066 this.buffers = [];
group-Make-NTU-Hackathon 0:4233b7b10255 6067 }
group-Make-NTU-Hackathon 0:4233b7b10255 6068
group-Make-NTU-Hackathon 0:4233b7b10255 6069 /**
group-Make-NTU-Hackathon 0:4233b7b10255 6070 * Method to be called when binary data received from connection
group-Make-NTU-Hackathon 0:4233b7b10255 6071 * after a BINARY_EVENT packet.
group-Make-NTU-Hackathon 0:4233b7b10255 6072 *
group-Make-NTU-Hackathon 0:4233b7b10255 6073 * @param {Buffer | ArrayBuffer} binData - the raw binary data received
group-Make-NTU-Hackathon 0:4233b7b10255 6074 * @return {null | Object} returns null if more binary data is expected or
group-Make-NTU-Hackathon 0:4233b7b10255 6075 * a reconstructed packet object if all buffers have been received.
group-Make-NTU-Hackathon 0:4233b7b10255 6076 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 6077 */
group-Make-NTU-Hackathon 0:4233b7b10255 6078
group-Make-NTU-Hackathon 0:4233b7b10255 6079 BinaryReconstructor.prototype.takeBinaryData = function(binData) {
group-Make-NTU-Hackathon 0:4233b7b10255 6080 this.buffers.push(binData);
group-Make-NTU-Hackathon 0:4233b7b10255 6081 if (this.buffers.length == this.reconPack.attachments) { // done with buffer list
group-Make-NTU-Hackathon 0:4233b7b10255 6082 var packet = binary.reconstructPacket(this.reconPack, this.buffers);
group-Make-NTU-Hackathon 0:4233b7b10255 6083 this.finishedReconstruction();
group-Make-NTU-Hackathon 0:4233b7b10255 6084 return packet;
group-Make-NTU-Hackathon 0:4233b7b10255 6085 }
group-Make-NTU-Hackathon 0:4233b7b10255 6086 return null;
group-Make-NTU-Hackathon 0:4233b7b10255 6087 };
group-Make-NTU-Hackathon 0:4233b7b10255 6088
group-Make-NTU-Hackathon 0:4233b7b10255 6089 /**
group-Make-NTU-Hackathon 0:4233b7b10255 6090 * Cleans up binary packet reconstruction variables.
group-Make-NTU-Hackathon 0:4233b7b10255 6091 *
group-Make-NTU-Hackathon 0:4233b7b10255 6092 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 6093 */
group-Make-NTU-Hackathon 0:4233b7b10255 6094
group-Make-NTU-Hackathon 0:4233b7b10255 6095 BinaryReconstructor.prototype.finishedReconstruction = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 6096 this.reconPack = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6097 this.buffers = [];
group-Make-NTU-Hackathon 0:4233b7b10255 6098 };
group-Make-NTU-Hackathon 0:4233b7b10255 6099
group-Make-NTU-Hackathon 0:4233b7b10255 6100 function error(data){
group-Make-NTU-Hackathon 0:4233b7b10255 6101 return {
group-Make-NTU-Hackathon 0:4233b7b10255 6102 type: exports.ERROR,
group-Make-NTU-Hackathon 0:4233b7b10255 6103 data: 'parser error'
group-Make-NTU-Hackathon 0:4233b7b10255 6104 };
group-Make-NTU-Hackathon 0:4233b7b10255 6105 }
group-Make-NTU-Hackathon 0:4233b7b10255 6106
group-Make-NTU-Hackathon 0:4233b7b10255 6107 },{"./binary":45,"./is-buffer":47,"component-emitter":9,"debug":10,"isarray":48,"json3":49}],47:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 6108 (function (global){
group-Make-NTU-Hackathon 0:4233b7b10255 6109
group-Make-NTU-Hackathon 0:4233b7b10255 6110 module.exports = isBuf;
group-Make-NTU-Hackathon 0:4233b7b10255 6111
group-Make-NTU-Hackathon 0:4233b7b10255 6112 /**
group-Make-NTU-Hackathon 0:4233b7b10255 6113 * Returns true if obj is a buffer or an arraybuffer.
group-Make-NTU-Hackathon 0:4233b7b10255 6114 *
group-Make-NTU-Hackathon 0:4233b7b10255 6115 * @api private
group-Make-NTU-Hackathon 0:4233b7b10255 6116 */
group-Make-NTU-Hackathon 0:4233b7b10255 6117
group-Make-NTU-Hackathon 0:4233b7b10255 6118 function isBuf(obj) {
group-Make-NTU-Hackathon 0:4233b7b10255 6119 return (global.Buffer && global.Buffer.isBuffer(obj)) ||
group-Make-NTU-Hackathon 0:4233b7b10255 6120 (global.ArrayBuffer && obj instanceof ArrayBuffer);
group-Make-NTU-Hackathon 0:4233b7b10255 6121 }
group-Make-NTU-Hackathon 0:4233b7b10255 6122
group-Make-NTU-Hackathon 0:4233b7b10255 6123 }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
group-Make-NTU-Hackathon 0:4233b7b10255 6124 },{}],48:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 6125 module.exports=_dereq_(32)
group-Make-NTU-Hackathon 0:4233b7b10255 6126 },{}],49:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 6127 /*! JSON v3.2.6 | http://bestiejs.github.io/json3 | Copyright 2012-2013, Kit Cambridge | http://kit.mit-license.org */
group-Make-NTU-Hackathon 0:4233b7b10255 6128 ;(function (window) {
group-Make-NTU-Hackathon 0:4233b7b10255 6129 // Convenience aliases.
group-Make-NTU-Hackathon 0:4233b7b10255 6130 var getClass = {}.toString, isProperty, forEach, undef;
group-Make-NTU-Hackathon 0:4233b7b10255 6131
group-Make-NTU-Hackathon 0:4233b7b10255 6132 // Detect the `define` function exposed by asynchronous module loaders. The
group-Make-NTU-Hackathon 0:4233b7b10255 6133 // strict `define` check is necessary for compatibility with `r.js`.
group-Make-NTU-Hackathon 0:4233b7b10255 6134 var isLoader = typeof define === "function" && define.amd;
group-Make-NTU-Hackathon 0:4233b7b10255 6135
group-Make-NTU-Hackathon 0:4233b7b10255 6136 // Detect native implementations.
group-Make-NTU-Hackathon 0:4233b7b10255 6137 var nativeJSON = typeof JSON == "object" && JSON;
group-Make-NTU-Hackathon 0:4233b7b10255 6138
group-Make-NTU-Hackathon 0:4233b7b10255 6139 // Set up the JSON 3 namespace, preferring the CommonJS `exports` object if
group-Make-NTU-Hackathon 0:4233b7b10255 6140 // available.
group-Make-NTU-Hackathon 0:4233b7b10255 6141 var JSON3 = typeof exports == "object" && exports && !exports.nodeType && exports;
group-Make-NTU-Hackathon 0:4233b7b10255 6142
group-Make-NTU-Hackathon 0:4233b7b10255 6143 if (JSON3 && nativeJSON) {
group-Make-NTU-Hackathon 0:4233b7b10255 6144 // Explicitly delegate to the native `stringify` and `parse`
group-Make-NTU-Hackathon 0:4233b7b10255 6145 // implementations in CommonJS environments.
group-Make-NTU-Hackathon 0:4233b7b10255 6146 JSON3.stringify = nativeJSON.stringify;
group-Make-NTU-Hackathon 0:4233b7b10255 6147 JSON3.parse = nativeJSON.parse;
group-Make-NTU-Hackathon 0:4233b7b10255 6148 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6149 // Export for web browsers, JavaScript engines, and asynchronous module
group-Make-NTU-Hackathon 0:4233b7b10255 6150 // loaders, using the global `JSON` object if available.
group-Make-NTU-Hackathon 0:4233b7b10255 6151 JSON3 = window.JSON = nativeJSON || {};
group-Make-NTU-Hackathon 0:4233b7b10255 6152 }
group-Make-NTU-Hackathon 0:4233b7b10255 6153
group-Make-NTU-Hackathon 0:4233b7b10255 6154 // Test the `Date#getUTC*` methods. Based on work by @Yaffle.
group-Make-NTU-Hackathon 0:4233b7b10255 6155 var isExtended = new Date(-3509827334573292);
group-Make-NTU-Hackathon 0:4233b7b10255 6156 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6157 // The `getUTCFullYear`, `Month`, and `Date` methods return nonsensical
group-Make-NTU-Hackathon 0:4233b7b10255 6158 // results for certain dates in Opera >= 10.53.
group-Make-NTU-Hackathon 0:4233b7b10255 6159 isExtended = isExtended.getUTCFullYear() == -109252 && isExtended.getUTCMonth() === 0 && isExtended.getUTCDate() === 1 &&
group-Make-NTU-Hackathon 0:4233b7b10255 6160 // Safari < 2.0.2 stores the internal millisecond time value correctly,
group-Make-NTU-Hackathon 0:4233b7b10255 6161 // but clips the values returned by the date methods to the range of
group-Make-NTU-Hackathon 0:4233b7b10255 6162 // signed 32-bit integers ([-2 ** 31, 2 ** 31 - 1]).
group-Make-NTU-Hackathon 0:4233b7b10255 6163 isExtended.getUTCHours() == 10 && isExtended.getUTCMinutes() == 37 && isExtended.getUTCSeconds() == 6 && isExtended.getUTCMilliseconds() == 708;
group-Make-NTU-Hackathon 0:4233b7b10255 6164 } catch (exception) {}
group-Make-NTU-Hackathon 0:4233b7b10255 6165
group-Make-NTU-Hackathon 0:4233b7b10255 6166 // Internal: Determines whether the native `JSON.stringify` and `parse`
group-Make-NTU-Hackathon 0:4233b7b10255 6167 // implementations are spec-compliant. Based on work by Ken Snyder.
group-Make-NTU-Hackathon 0:4233b7b10255 6168 function has(name) {
group-Make-NTU-Hackathon 0:4233b7b10255 6169 if (has[name] !== undef) {
group-Make-NTU-Hackathon 0:4233b7b10255 6170 // Return cached feature test result.
group-Make-NTU-Hackathon 0:4233b7b10255 6171 return has[name];
group-Make-NTU-Hackathon 0:4233b7b10255 6172 }
group-Make-NTU-Hackathon 0:4233b7b10255 6173
group-Make-NTU-Hackathon 0:4233b7b10255 6174 var isSupported;
group-Make-NTU-Hackathon 0:4233b7b10255 6175 if (name == "bug-string-char-index") {
group-Make-NTU-Hackathon 0:4233b7b10255 6176 // IE <= 7 doesn't support accessing string characters using square
group-Make-NTU-Hackathon 0:4233b7b10255 6177 // bracket notation. IE 8 only supports this for primitives.
group-Make-NTU-Hackathon 0:4233b7b10255 6178 isSupported = "a"[0] != "a";
group-Make-NTU-Hackathon 0:4233b7b10255 6179 } else if (name == "json") {
group-Make-NTU-Hackathon 0:4233b7b10255 6180 // Indicates whether both `JSON.stringify` and `JSON.parse` are
group-Make-NTU-Hackathon 0:4233b7b10255 6181 // supported.
group-Make-NTU-Hackathon 0:4233b7b10255 6182 isSupported = has("json-stringify") && has("json-parse");
group-Make-NTU-Hackathon 0:4233b7b10255 6183 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6184 var value, serialized = '{"a":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}';
group-Make-NTU-Hackathon 0:4233b7b10255 6185 // Test `JSON.stringify`.
group-Make-NTU-Hackathon 0:4233b7b10255 6186 if (name == "json-stringify") {
group-Make-NTU-Hackathon 0:4233b7b10255 6187 var stringify = JSON3.stringify, stringifySupported = typeof stringify == "function" && isExtended;
group-Make-NTU-Hackathon 0:4233b7b10255 6188 if (stringifySupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 6189 // A test function object with a custom `toJSON` method.
group-Make-NTU-Hackathon 0:4233b7b10255 6190 (value = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 6191 return 1;
group-Make-NTU-Hackathon 0:4233b7b10255 6192 }).toJSON = value;
group-Make-NTU-Hackathon 0:4233b7b10255 6193 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6194 stringifySupported =
group-Make-NTU-Hackathon 0:4233b7b10255 6195 // Firefox 3.1b1 and b2 serialize string, number, and boolean
group-Make-NTU-Hackathon 0:4233b7b10255 6196 // primitives as object literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6197 stringify(0) === "0" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6198 // FF 3.1b1, b2, and JSON 2 serialize wrapped primitives as object
group-Make-NTU-Hackathon 0:4233b7b10255 6199 // literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6200 stringify(new Number()) === "0" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6201 stringify(new String()) == '""' &&
group-Make-NTU-Hackathon 0:4233b7b10255 6202 // FF 3.1b1, 2 throw an error if the value is `null`, `undefined`, or
group-Make-NTU-Hackathon 0:4233b7b10255 6203 // does not define a canonical JSON representation (this applies to
group-Make-NTU-Hackathon 0:4233b7b10255 6204 // objects with `toJSON` properties as well, *unless* they are nested
group-Make-NTU-Hackathon 0:4233b7b10255 6205 // within an object or array).
group-Make-NTU-Hackathon 0:4233b7b10255 6206 stringify(getClass) === undef &&
group-Make-NTU-Hackathon 0:4233b7b10255 6207 // IE 8 serializes `undefined` as `"undefined"`. Safari <= 5.1.7 and
group-Make-NTU-Hackathon 0:4233b7b10255 6208 // FF 3.1b3 pass this test.
group-Make-NTU-Hackathon 0:4233b7b10255 6209 stringify(undef) === undef &&
group-Make-NTU-Hackathon 0:4233b7b10255 6210 // Safari <= 5.1.7 and FF 3.1b3 throw `Error`s and `TypeError`s,
group-Make-NTU-Hackathon 0:4233b7b10255 6211 // respectively, if the value is omitted entirely.
group-Make-NTU-Hackathon 0:4233b7b10255 6212 stringify() === undef &&
group-Make-NTU-Hackathon 0:4233b7b10255 6213 // FF 3.1b1, 2 throw an error if the given value is not a number,
group-Make-NTU-Hackathon 0:4233b7b10255 6214 // string, array, object, Boolean, or `null` literal. This applies to
group-Make-NTU-Hackathon 0:4233b7b10255 6215 // objects with custom `toJSON` methods as well, unless they are nested
group-Make-NTU-Hackathon 0:4233b7b10255 6216 // inside object or array literals. YUI 3.0.0b1 ignores custom `toJSON`
group-Make-NTU-Hackathon 0:4233b7b10255 6217 // methods entirely.
group-Make-NTU-Hackathon 0:4233b7b10255 6218 stringify(value) === "1" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6219 stringify([value]) == "[1]" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6220 // Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of
group-Make-NTU-Hackathon 0:4233b7b10255 6221 // `"[null]"`.
group-Make-NTU-Hackathon 0:4233b7b10255 6222 stringify([undef]) == "[null]" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6223 // YUI 3.0.0b1 fails to serialize `null` literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6224 stringify(null) == "null" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6225 // FF 3.1b1, 2 halts serialization if an array contains a function:
group-Make-NTU-Hackathon 0:4233b7b10255 6226 // `[1, true, getClass, 1]` serializes as "[1,true,],". FF 3.1b3
group-Make-NTU-Hackathon 0:4233b7b10255 6227 // elides non-JSON values from objects and arrays, unless they
group-Make-NTU-Hackathon 0:4233b7b10255 6228 // define custom `toJSON` methods.
group-Make-NTU-Hackathon 0:4233b7b10255 6229 stringify([undef, getClass, null]) == "[null,null,null]" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6230 // Simple serialization test. FF 3.1b1 uses Unicode escape sequences
group-Make-NTU-Hackathon 0:4233b7b10255 6231 // where character escape codes are expected (e.g., `\b` => `\u0008`).
group-Make-NTU-Hackathon 0:4233b7b10255 6232 stringify({ "a": [value, true, false, null, "\x00\b\n\f\r\t"] }) == serialized &&
group-Make-NTU-Hackathon 0:4233b7b10255 6233 // FF 3.1b1 and b2 ignore the `filter` and `width` arguments.
group-Make-NTU-Hackathon 0:4233b7b10255 6234 stringify(null, value) === "1" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6235 stringify([1, 2], null, 1) == "[\n 1,\n 2\n]" &&
group-Make-NTU-Hackathon 0:4233b7b10255 6236 // JSON 2, Prototype <= 1.7, and older WebKit builds incorrectly
group-Make-NTU-Hackathon 0:4233b7b10255 6237 // serialize extended years.
group-Make-NTU-Hackathon 0:4233b7b10255 6238 stringify(new Date(-8.64e15)) == '"-271821-04-20T00:00:00.000Z"' &&
group-Make-NTU-Hackathon 0:4233b7b10255 6239 // The milliseconds are optional in ES 5, but required in 5.1.
group-Make-NTU-Hackathon 0:4233b7b10255 6240 stringify(new Date(8.64e15)) == '"+275760-09-13T00:00:00.000Z"' &&
group-Make-NTU-Hackathon 0:4233b7b10255 6241 // Firefox <= 11.0 incorrectly serializes years prior to 0 as negative
group-Make-NTU-Hackathon 0:4233b7b10255 6242 // four-digit years instead of six-digit years. Credits: @Yaffle.
group-Make-NTU-Hackathon 0:4233b7b10255 6243 stringify(new Date(-621987552e5)) == '"-000001-01-01T00:00:00.000Z"' &&
group-Make-NTU-Hackathon 0:4233b7b10255 6244 // Safari <= 5.1.5 and Opera >= 10.53 incorrectly serialize millisecond
group-Make-NTU-Hackathon 0:4233b7b10255 6245 // values less than 1000. Credits: @Yaffle.
group-Make-NTU-Hackathon 0:4233b7b10255 6246 stringify(new Date(-1)) == '"1969-12-31T23:59:59.999Z"';
group-Make-NTU-Hackathon 0:4233b7b10255 6247 } catch (exception) {
group-Make-NTU-Hackathon 0:4233b7b10255 6248 stringifySupported = false;
group-Make-NTU-Hackathon 0:4233b7b10255 6249 }
group-Make-NTU-Hackathon 0:4233b7b10255 6250 }
group-Make-NTU-Hackathon 0:4233b7b10255 6251 isSupported = stringifySupported;
group-Make-NTU-Hackathon 0:4233b7b10255 6252 }
group-Make-NTU-Hackathon 0:4233b7b10255 6253 // Test `JSON.parse`.
group-Make-NTU-Hackathon 0:4233b7b10255 6254 if (name == "json-parse") {
group-Make-NTU-Hackathon 0:4233b7b10255 6255 var parse = JSON3.parse;
group-Make-NTU-Hackathon 0:4233b7b10255 6256 if (typeof parse == "function") {
group-Make-NTU-Hackathon 0:4233b7b10255 6257 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6258 // FF 3.1b1, b2 will throw an exception if a bare literal is provided.
group-Make-NTU-Hackathon 0:4233b7b10255 6259 // Conforming implementations should also coerce the initial argument to
group-Make-NTU-Hackathon 0:4233b7b10255 6260 // a string prior to parsing.
group-Make-NTU-Hackathon 0:4233b7b10255 6261 if (parse("0") === 0 && !parse(false)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6262 // Simple parsing test.
group-Make-NTU-Hackathon 0:4233b7b10255 6263 value = parse(serialized);
group-Make-NTU-Hackathon 0:4233b7b10255 6264 var parseSupported = value["a"].length == 5 && value["a"][0] === 1;
group-Make-NTU-Hackathon 0:4233b7b10255 6265 if (parseSupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 6266 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6267 // Safari <= 5.1.2 and FF 3.1b1 allow unescaped tabs in strings.
group-Make-NTU-Hackathon 0:4233b7b10255 6268 parseSupported = !parse('"\t"');
group-Make-NTU-Hackathon 0:4233b7b10255 6269 } catch (exception) {}
group-Make-NTU-Hackathon 0:4233b7b10255 6270 if (parseSupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 6271 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6272 // FF 4.0 and 4.0.1 allow leading `+` signs and leading
group-Make-NTU-Hackathon 0:4233b7b10255 6273 // decimal points. FF 4.0, 4.0.1, and IE 9-10 also allow
group-Make-NTU-Hackathon 0:4233b7b10255 6274 // certain octal literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6275 parseSupported = parse("01") !== 1;
group-Make-NTU-Hackathon 0:4233b7b10255 6276 } catch (exception) {}
group-Make-NTU-Hackathon 0:4233b7b10255 6277 }
group-Make-NTU-Hackathon 0:4233b7b10255 6278 if (parseSupported) {
group-Make-NTU-Hackathon 0:4233b7b10255 6279 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6280 // FF 4.0, 4.0.1, and Rhino 1.7R3-R4 allow trailing decimal
group-Make-NTU-Hackathon 0:4233b7b10255 6281 // points. These environments, along with FF 3.1b1 and 2,
group-Make-NTU-Hackathon 0:4233b7b10255 6282 // also allow trailing commas in JSON objects and arrays.
group-Make-NTU-Hackathon 0:4233b7b10255 6283 parseSupported = parse("1.") !== 1;
group-Make-NTU-Hackathon 0:4233b7b10255 6284 } catch (exception) {}
group-Make-NTU-Hackathon 0:4233b7b10255 6285 }
group-Make-NTU-Hackathon 0:4233b7b10255 6286 }
group-Make-NTU-Hackathon 0:4233b7b10255 6287 }
group-Make-NTU-Hackathon 0:4233b7b10255 6288 } catch (exception) {
group-Make-NTU-Hackathon 0:4233b7b10255 6289 parseSupported = false;
group-Make-NTU-Hackathon 0:4233b7b10255 6290 }
group-Make-NTU-Hackathon 0:4233b7b10255 6291 }
group-Make-NTU-Hackathon 0:4233b7b10255 6292 isSupported = parseSupported;
group-Make-NTU-Hackathon 0:4233b7b10255 6293 }
group-Make-NTU-Hackathon 0:4233b7b10255 6294 }
group-Make-NTU-Hackathon 0:4233b7b10255 6295 return has[name] = !!isSupported;
group-Make-NTU-Hackathon 0:4233b7b10255 6296 }
group-Make-NTU-Hackathon 0:4233b7b10255 6297
group-Make-NTU-Hackathon 0:4233b7b10255 6298 if (!has("json")) {
group-Make-NTU-Hackathon 0:4233b7b10255 6299 // Common `[[Class]]` name aliases.
group-Make-NTU-Hackathon 0:4233b7b10255 6300 var functionClass = "[object Function]";
group-Make-NTU-Hackathon 0:4233b7b10255 6301 var dateClass = "[object Date]";
group-Make-NTU-Hackathon 0:4233b7b10255 6302 var numberClass = "[object Number]";
group-Make-NTU-Hackathon 0:4233b7b10255 6303 var stringClass = "[object String]";
group-Make-NTU-Hackathon 0:4233b7b10255 6304 var arrayClass = "[object Array]";
group-Make-NTU-Hackathon 0:4233b7b10255 6305 var booleanClass = "[object Boolean]";
group-Make-NTU-Hackathon 0:4233b7b10255 6306
group-Make-NTU-Hackathon 0:4233b7b10255 6307 // Detect incomplete support for accessing string characters by index.
group-Make-NTU-Hackathon 0:4233b7b10255 6308 var charIndexBuggy = has("bug-string-char-index");
group-Make-NTU-Hackathon 0:4233b7b10255 6309
group-Make-NTU-Hackathon 0:4233b7b10255 6310 // Define additional utility methods if the `Date` methods are buggy.
group-Make-NTU-Hackathon 0:4233b7b10255 6311 if (!isExtended) {
group-Make-NTU-Hackathon 0:4233b7b10255 6312 var floor = Math.floor;
group-Make-NTU-Hackathon 0:4233b7b10255 6313 // A mapping between the months of the year and the number of days between
group-Make-NTU-Hackathon 0:4233b7b10255 6314 // January 1st and the first of the respective month.
group-Make-NTU-Hackathon 0:4233b7b10255 6315 var Months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
group-Make-NTU-Hackathon 0:4233b7b10255 6316 // Internal: Calculates the number of days between the Unix epoch and the
group-Make-NTU-Hackathon 0:4233b7b10255 6317 // first day of the given month.
group-Make-NTU-Hackathon 0:4233b7b10255 6318 var getDay = function (year, month) {
group-Make-NTU-Hackathon 0:4233b7b10255 6319 return Months[month] + 365 * (year - 1970) + floor((year - 1969 + (month = +(month > 1))) / 4) - floor((year - 1901 + month) / 100) + floor((year - 1601 + month) / 400);
group-Make-NTU-Hackathon 0:4233b7b10255 6320 };
group-Make-NTU-Hackathon 0:4233b7b10255 6321 }
group-Make-NTU-Hackathon 0:4233b7b10255 6322
group-Make-NTU-Hackathon 0:4233b7b10255 6323 // Internal: Determines if a property is a direct property of the given
group-Make-NTU-Hackathon 0:4233b7b10255 6324 // object. Delegates to the native `Object#hasOwnProperty` method.
group-Make-NTU-Hackathon 0:4233b7b10255 6325 if (!(isProperty = {}.hasOwnProperty)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6326 isProperty = function (property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6327 var members = {}, constructor;
group-Make-NTU-Hackathon 0:4233b7b10255 6328 if ((members.__proto__ = null, members.__proto__ = {
group-Make-NTU-Hackathon 0:4233b7b10255 6329 // The *proto* property cannot be set multiple times in recent
group-Make-NTU-Hackathon 0:4233b7b10255 6330 // versions of Firefox and SeaMonkey.
group-Make-NTU-Hackathon 0:4233b7b10255 6331 "toString": 1
group-Make-NTU-Hackathon 0:4233b7b10255 6332 }, members).toString != getClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6333 // Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but
group-Make-NTU-Hackathon 0:4233b7b10255 6334 // supports the mutable *proto* property.
group-Make-NTU-Hackathon 0:4233b7b10255 6335 isProperty = function (property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6336 // Capture and break the object's prototype chain (see section 8.6.2
group-Make-NTU-Hackathon 0:4233b7b10255 6337 // of the ES 5.1 spec). The parenthesized expression prevents an
group-Make-NTU-Hackathon 0:4233b7b10255 6338 // unsafe transformation by the Closure Compiler.
group-Make-NTU-Hackathon 0:4233b7b10255 6339 var original = this.__proto__, result = property in (this.__proto__ = null, this);
group-Make-NTU-Hackathon 0:4233b7b10255 6340 // Restore the original prototype chain.
group-Make-NTU-Hackathon 0:4233b7b10255 6341 this.__proto__ = original;
group-Make-NTU-Hackathon 0:4233b7b10255 6342 return result;
group-Make-NTU-Hackathon 0:4233b7b10255 6343 };
group-Make-NTU-Hackathon 0:4233b7b10255 6344 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6345 // Capture a reference to the top-level `Object` constructor.
group-Make-NTU-Hackathon 0:4233b7b10255 6346 constructor = members.constructor;
group-Make-NTU-Hackathon 0:4233b7b10255 6347 // Use the `constructor` property to simulate `Object#hasOwnProperty` in
group-Make-NTU-Hackathon 0:4233b7b10255 6348 // other environments.
group-Make-NTU-Hackathon 0:4233b7b10255 6349 isProperty = function (property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6350 var parent = (this.constructor || constructor).prototype;
group-Make-NTU-Hackathon 0:4233b7b10255 6351 return property in this && !(property in parent && this[property] === parent[property]);
group-Make-NTU-Hackathon 0:4233b7b10255 6352 };
group-Make-NTU-Hackathon 0:4233b7b10255 6353 }
group-Make-NTU-Hackathon 0:4233b7b10255 6354 members = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6355 return isProperty.call(this, property);
group-Make-NTU-Hackathon 0:4233b7b10255 6356 };
group-Make-NTU-Hackathon 0:4233b7b10255 6357 }
group-Make-NTU-Hackathon 0:4233b7b10255 6358
group-Make-NTU-Hackathon 0:4233b7b10255 6359 // Internal: A set of primitive types used by `isHostType`.
group-Make-NTU-Hackathon 0:4233b7b10255 6360 var PrimitiveTypes = {
group-Make-NTU-Hackathon 0:4233b7b10255 6361 'boolean': 1,
group-Make-NTU-Hackathon 0:4233b7b10255 6362 'number': 1,
group-Make-NTU-Hackathon 0:4233b7b10255 6363 'string': 1,
group-Make-NTU-Hackathon 0:4233b7b10255 6364 'undefined': 1
group-Make-NTU-Hackathon 0:4233b7b10255 6365 };
group-Make-NTU-Hackathon 0:4233b7b10255 6366
group-Make-NTU-Hackathon 0:4233b7b10255 6367 // Internal: Determines if the given object `property` value is a
group-Make-NTU-Hackathon 0:4233b7b10255 6368 // non-primitive.
group-Make-NTU-Hackathon 0:4233b7b10255 6369 var isHostType = function (object, property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6370 var type = typeof object[property];
group-Make-NTU-Hackathon 0:4233b7b10255 6371 return type == 'object' ? !!object[property] : !PrimitiveTypes[type];
group-Make-NTU-Hackathon 0:4233b7b10255 6372 };
group-Make-NTU-Hackathon 0:4233b7b10255 6373
group-Make-NTU-Hackathon 0:4233b7b10255 6374 // Internal: Normalizes the `for...in` iteration algorithm across
group-Make-NTU-Hackathon 0:4233b7b10255 6375 // environments. Each enumerated key is yielded to a `callback` function.
group-Make-NTU-Hackathon 0:4233b7b10255 6376 forEach = function (object, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6377 var size = 0, Properties, members, property;
group-Make-NTU-Hackathon 0:4233b7b10255 6378
group-Make-NTU-Hackathon 0:4233b7b10255 6379 // Tests for bugs in the current environment's `for...in` algorithm. The
group-Make-NTU-Hackathon 0:4233b7b10255 6380 // `valueOf` property inherits the non-enumerable flag from
group-Make-NTU-Hackathon 0:4233b7b10255 6381 // `Object.prototype` in older versions of IE, Netscape, and Mozilla.
group-Make-NTU-Hackathon 0:4233b7b10255 6382 (Properties = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 6383 this.valueOf = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 6384 }).prototype.valueOf = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 6385
group-Make-NTU-Hackathon 0:4233b7b10255 6386 // Iterate over a new instance of the `Properties` class.
group-Make-NTU-Hackathon 0:4233b7b10255 6387 members = new Properties();
group-Make-NTU-Hackathon 0:4233b7b10255 6388 for (property in members) {
group-Make-NTU-Hackathon 0:4233b7b10255 6389 // Ignore all properties inherited from `Object.prototype`.
group-Make-NTU-Hackathon 0:4233b7b10255 6390 if (isProperty.call(members, property)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6391 size++;
group-Make-NTU-Hackathon 0:4233b7b10255 6392 }
group-Make-NTU-Hackathon 0:4233b7b10255 6393 }
group-Make-NTU-Hackathon 0:4233b7b10255 6394 Properties = members = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6395
group-Make-NTU-Hackathon 0:4233b7b10255 6396 // Normalize the iteration algorithm.
group-Make-NTU-Hackathon 0:4233b7b10255 6397 if (!size) {
group-Make-NTU-Hackathon 0:4233b7b10255 6398 // A list of non-enumerable properties inherited from `Object.prototype`.
group-Make-NTU-Hackathon 0:4233b7b10255 6399 members = ["valueOf", "toString", "toLocaleString", "propertyIsEnumerable", "isPrototypeOf", "hasOwnProperty", "constructor"];
group-Make-NTU-Hackathon 0:4233b7b10255 6400 // IE <= 8, Mozilla 1.0, and Netscape 6.2 ignore shadowed non-enumerable
group-Make-NTU-Hackathon 0:4233b7b10255 6401 // properties.
group-Make-NTU-Hackathon 0:4233b7b10255 6402 forEach = function (object, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6403 var isFunction = getClass.call(object) == functionClass, property, length;
group-Make-NTU-Hackathon 0:4233b7b10255 6404 var hasProperty = !isFunction && typeof object.constructor != 'function' && isHostType(object, 'hasOwnProperty') ? object.hasOwnProperty : isProperty;
group-Make-NTU-Hackathon 0:4233b7b10255 6405 for (property in object) {
group-Make-NTU-Hackathon 0:4233b7b10255 6406 // Gecko <= 1.0 enumerates the `prototype` property of functions under
group-Make-NTU-Hackathon 0:4233b7b10255 6407 // certain conditions; IE does not.
group-Make-NTU-Hackathon 0:4233b7b10255 6408 if (!(isFunction && property == "prototype") && hasProperty.call(object, property)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6409 callback(property);
group-Make-NTU-Hackathon 0:4233b7b10255 6410 }
group-Make-NTU-Hackathon 0:4233b7b10255 6411 }
group-Make-NTU-Hackathon 0:4233b7b10255 6412 // Manually invoke the callback for each non-enumerable property.
group-Make-NTU-Hackathon 0:4233b7b10255 6413 for (length = members.length; property = members[--length]; hasProperty.call(object, property) && callback(property));
group-Make-NTU-Hackathon 0:4233b7b10255 6414 };
group-Make-NTU-Hackathon 0:4233b7b10255 6415 } else if (size == 2) {
group-Make-NTU-Hackathon 0:4233b7b10255 6416 // Safari <= 2.0.4 enumerates shadowed properties twice.
group-Make-NTU-Hackathon 0:4233b7b10255 6417 forEach = function (object, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6418 // Create a set of iterated properties.
group-Make-NTU-Hackathon 0:4233b7b10255 6419 var members = {}, isFunction = getClass.call(object) == functionClass, property;
group-Make-NTU-Hackathon 0:4233b7b10255 6420 for (property in object) {
group-Make-NTU-Hackathon 0:4233b7b10255 6421 // Store each property name to prevent double enumeration. The
group-Make-NTU-Hackathon 0:4233b7b10255 6422 // `prototype` property of functions is not enumerated due to cross-
group-Make-NTU-Hackathon 0:4233b7b10255 6423 // environment inconsistencies.
group-Make-NTU-Hackathon 0:4233b7b10255 6424 if (!(isFunction && property == "prototype") && !isProperty.call(members, property) && (members[property] = 1) && isProperty.call(object, property)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6425 callback(property);
group-Make-NTU-Hackathon 0:4233b7b10255 6426 }
group-Make-NTU-Hackathon 0:4233b7b10255 6427 }
group-Make-NTU-Hackathon 0:4233b7b10255 6428 };
group-Make-NTU-Hackathon 0:4233b7b10255 6429 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6430 // No bugs detected; use the standard `for...in` algorithm.
group-Make-NTU-Hackathon 0:4233b7b10255 6431 forEach = function (object, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6432 var isFunction = getClass.call(object) == functionClass, property, isConstructor;
group-Make-NTU-Hackathon 0:4233b7b10255 6433 for (property in object) {
group-Make-NTU-Hackathon 0:4233b7b10255 6434 if (!(isFunction && property == "prototype") && isProperty.call(object, property) && !(isConstructor = property === "constructor")) {
group-Make-NTU-Hackathon 0:4233b7b10255 6435 callback(property);
group-Make-NTU-Hackathon 0:4233b7b10255 6436 }
group-Make-NTU-Hackathon 0:4233b7b10255 6437 }
group-Make-NTU-Hackathon 0:4233b7b10255 6438 // Manually invoke the callback for the `constructor` property due to
group-Make-NTU-Hackathon 0:4233b7b10255 6439 // cross-environment inconsistencies.
group-Make-NTU-Hackathon 0:4233b7b10255 6440 if (isConstructor || isProperty.call(object, (property = "constructor"))) {
group-Make-NTU-Hackathon 0:4233b7b10255 6441 callback(property);
group-Make-NTU-Hackathon 0:4233b7b10255 6442 }
group-Make-NTU-Hackathon 0:4233b7b10255 6443 };
group-Make-NTU-Hackathon 0:4233b7b10255 6444 }
group-Make-NTU-Hackathon 0:4233b7b10255 6445 return forEach(object, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 6446 };
group-Make-NTU-Hackathon 0:4233b7b10255 6447
group-Make-NTU-Hackathon 0:4233b7b10255 6448 // Public: Serializes a JavaScript `value` as a JSON string. The optional
group-Make-NTU-Hackathon 0:4233b7b10255 6449 // `filter` argument may specify either a function that alters how object and
group-Make-NTU-Hackathon 0:4233b7b10255 6450 // array members are serialized, or an array of strings and numbers that
group-Make-NTU-Hackathon 0:4233b7b10255 6451 // indicates which properties should be serialized. The optional `width`
group-Make-NTU-Hackathon 0:4233b7b10255 6452 // argument may be either a string or number that specifies the indentation
group-Make-NTU-Hackathon 0:4233b7b10255 6453 // level of the output.
group-Make-NTU-Hackathon 0:4233b7b10255 6454 if (!has("json-stringify")) {
group-Make-NTU-Hackathon 0:4233b7b10255 6455 // Internal: A map of control characters and their escaped equivalents.
group-Make-NTU-Hackathon 0:4233b7b10255 6456 var Escapes = {
group-Make-NTU-Hackathon 0:4233b7b10255 6457 92: "\\\\",
group-Make-NTU-Hackathon 0:4233b7b10255 6458 34: '\\"',
group-Make-NTU-Hackathon 0:4233b7b10255 6459 8: "\\b",
group-Make-NTU-Hackathon 0:4233b7b10255 6460 12: "\\f",
group-Make-NTU-Hackathon 0:4233b7b10255 6461 10: "\\n",
group-Make-NTU-Hackathon 0:4233b7b10255 6462 13: "\\r",
group-Make-NTU-Hackathon 0:4233b7b10255 6463 9: "\\t"
group-Make-NTU-Hackathon 0:4233b7b10255 6464 };
group-Make-NTU-Hackathon 0:4233b7b10255 6465
group-Make-NTU-Hackathon 0:4233b7b10255 6466 // Internal: Converts `value` into a zero-padded string such that its
group-Make-NTU-Hackathon 0:4233b7b10255 6467 // length is at least equal to `width`. The `width` must be <= 6.
group-Make-NTU-Hackathon 0:4233b7b10255 6468 var leadingZeroes = "000000";
group-Make-NTU-Hackathon 0:4233b7b10255 6469 var toPaddedString = function (width, value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6470 // The `|| 0` expression is necessary to work around a bug in
group-Make-NTU-Hackathon 0:4233b7b10255 6471 // Opera <= 7.54u2 where `0 == -0`, but `String(-0) !== "0"`.
group-Make-NTU-Hackathon 0:4233b7b10255 6472 return (leadingZeroes + (value || 0)).slice(-width);
group-Make-NTU-Hackathon 0:4233b7b10255 6473 };
group-Make-NTU-Hackathon 0:4233b7b10255 6474
group-Make-NTU-Hackathon 0:4233b7b10255 6475 // Internal: Double-quotes a string `value`, replacing all ASCII control
group-Make-NTU-Hackathon 0:4233b7b10255 6476 // characters (characters with code unit values between 0 and 31) with
group-Make-NTU-Hackathon 0:4233b7b10255 6477 // their escaped equivalents. This is an implementation of the
group-Make-NTU-Hackathon 0:4233b7b10255 6478 // `Quote(value)` operation defined in ES 5.1 section 15.12.3.
group-Make-NTU-Hackathon 0:4233b7b10255 6479 var unicodePrefix = "\\u00";
group-Make-NTU-Hackathon 0:4233b7b10255 6480 var quote = function (value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6481 var result = '"', index = 0, length = value.length, isLarge = length > 10 && charIndexBuggy, symbols;
group-Make-NTU-Hackathon 0:4233b7b10255 6482 if (isLarge) {
group-Make-NTU-Hackathon 0:4233b7b10255 6483 symbols = value.split("");
group-Make-NTU-Hackathon 0:4233b7b10255 6484 }
group-Make-NTU-Hackathon 0:4233b7b10255 6485 for (; index < length; index++) {
group-Make-NTU-Hackathon 0:4233b7b10255 6486 var charCode = value.charCodeAt(index);
group-Make-NTU-Hackathon 0:4233b7b10255 6487 // If the character is a control character, append its Unicode or
group-Make-NTU-Hackathon 0:4233b7b10255 6488 // shorthand escape sequence; otherwise, append the character as-is.
group-Make-NTU-Hackathon 0:4233b7b10255 6489 switch (charCode) {
group-Make-NTU-Hackathon 0:4233b7b10255 6490 case 8: case 9: case 10: case 12: case 13: case 34: case 92:
group-Make-NTU-Hackathon 0:4233b7b10255 6491 result += Escapes[charCode];
group-Make-NTU-Hackathon 0:4233b7b10255 6492 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6493 default:
group-Make-NTU-Hackathon 0:4233b7b10255 6494 if (charCode < 32) {
group-Make-NTU-Hackathon 0:4233b7b10255 6495 result += unicodePrefix + toPaddedString(2, charCode.toString(16));
group-Make-NTU-Hackathon 0:4233b7b10255 6496 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6497 }
group-Make-NTU-Hackathon 0:4233b7b10255 6498 result += isLarge ? symbols[index] : charIndexBuggy ? value.charAt(index) : value[index];
group-Make-NTU-Hackathon 0:4233b7b10255 6499 }
group-Make-NTU-Hackathon 0:4233b7b10255 6500 }
group-Make-NTU-Hackathon 0:4233b7b10255 6501 return result + '"';
group-Make-NTU-Hackathon 0:4233b7b10255 6502 };
group-Make-NTU-Hackathon 0:4233b7b10255 6503
group-Make-NTU-Hackathon 0:4233b7b10255 6504 // Internal: Recursively serializes an object. Implements the
group-Make-NTU-Hackathon 0:4233b7b10255 6505 // `Str(key, holder)`, `JO(value)`, and `JA(value)` operations.
group-Make-NTU-Hackathon 0:4233b7b10255 6506 var serialize = function (property, object, callback, properties, whitespace, indentation, stack) {
group-Make-NTU-Hackathon 0:4233b7b10255 6507 var value, className, year, month, date, time, hours, minutes, seconds, milliseconds, results, element, index, length, prefix, result;
group-Make-NTU-Hackathon 0:4233b7b10255 6508 try {
group-Make-NTU-Hackathon 0:4233b7b10255 6509 // Necessary for host object support.
group-Make-NTU-Hackathon 0:4233b7b10255 6510 value = object[property];
group-Make-NTU-Hackathon 0:4233b7b10255 6511 } catch (exception) {}
group-Make-NTU-Hackathon 0:4233b7b10255 6512 if (typeof value == "object" && value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6513 className = getClass.call(value);
group-Make-NTU-Hackathon 0:4233b7b10255 6514 if (className == dateClass && !isProperty.call(value, "toJSON")) {
group-Make-NTU-Hackathon 0:4233b7b10255 6515 if (value > -1 / 0 && value < 1 / 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 6516 // Dates are serialized according to the `Date#toJSON` method
group-Make-NTU-Hackathon 0:4233b7b10255 6517 // specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15
group-Make-NTU-Hackathon 0:4233b7b10255 6518 // for the ISO 8601 date time string format.
group-Make-NTU-Hackathon 0:4233b7b10255 6519 if (getDay) {
group-Make-NTU-Hackathon 0:4233b7b10255 6520 // Manually compute the year, month, date, hours, minutes,
group-Make-NTU-Hackathon 0:4233b7b10255 6521 // seconds, and milliseconds if the `getUTC*` methods are
group-Make-NTU-Hackathon 0:4233b7b10255 6522 // buggy. Adapted from @Yaffle's `date-shim` project.
group-Make-NTU-Hackathon 0:4233b7b10255 6523 date = floor(value / 864e5);
group-Make-NTU-Hackathon 0:4233b7b10255 6524 for (year = floor(date / 365.2425) + 1970 - 1; getDay(year + 1, 0) <= date; year++);
group-Make-NTU-Hackathon 0:4233b7b10255 6525 for (month = floor((date - getDay(year, 0)) / 30.42); getDay(year, month + 1) <= date; month++);
group-Make-NTU-Hackathon 0:4233b7b10255 6526 date = 1 + date - getDay(year, month);
group-Make-NTU-Hackathon 0:4233b7b10255 6527 // The `time` value specifies the time within the day (see ES
group-Make-NTU-Hackathon 0:4233b7b10255 6528 // 5.1 section 15.9.1.2). The formula `(A % B + B) % B` is used
group-Make-NTU-Hackathon 0:4233b7b10255 6529 // to compute `A modulo B`, as the `%` operator does not
group-Make-NTU-Hackathon 0:4233b7b10255 6530 // correspond to the `modulo` operation for negative numbers.
group-Make-NTU-Hackathon 0:4233b7b10255 6531 time = (value % 864e5 + 864e5) % 864e5;
group-Make-NTU-Hackathon 0:4233b7b10255 6532 // The hours, minutes, seconds, and milliseconds are obtained by
group-Make-NTU-Hackathon 0:4233b7b10255 6533 // decomposing the time within the day. See section 15.9.1.10.
group-Make-NTU-Hackathon 0:4233b7b10255 6534 hours = floor(time / 36e5) % 24;
group-Make-NTU-Hackathon 0:4233b7b10255 6535 minutes = floor(time / 6e4) % 60;
group-Make-NTU-Hackathon 0:4233b7b10255 6536 seconds = floor(time / 1e3) % 60;
group-Make-NTU-Hackathon 0:4233b7b10255 6537 milliseconds = time % 1e3;
group-Make-NTU-Hackathon 0:4233b7b10255 6538 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6539 year = value.getUTCFullYear();
group-Make-NTU-Hackathon 0:4233b7b10255 6540 month = value.getUTCMonth();
group-Make-NTU-Hackathon 0:4233b7b10255 6541 date = value.getUTCDate();
group-Make-NTU-Hackathon 0:4233b7b10255 6542 hours = value.getUTCHours();
group-Make-NTU-Hackathon 0:4233b7b10255 6543 minutes = value.getUTCMinutes();
group-Make-NTU-Hackathon 0:4233b7b10255 6544 seconds = value.getUTCSeconds();
group-Make-NTU-Hackathon 0:4233b7b10255 6545 milliseconds = value.getUTCMilliseconds();
group-Make-NTU-Hackathon 0:4233b7b10255 6546 }
group-Make-NTU-Hackathon 0:4233b7b10255 6547 // Serialize extended years correctly.
group-Make-NTU-Hackathon 0:4233b7b10255 6548 value = (year <= 0 || year >= 1e4 ? (year < 0 ? "-" : "+") + toPaddedString(6, year < 0 ? -year : year) : toPaddedString(4, year)) +
group-Make-NTU-Hackathon 0:4233b7b10255 6549 "-" + toPaddedString(2, month + 1) + "-" + toPaddedString(2, date) +
group-Make-NTU-Hackathon 0:4233b7b10255 6550 // Months, dates, hours, minutes, and seconds should have two
group-Make-NTU-Hackathon 0:4233b7b10255 6551 // digits; milliseconds should have three.
group-Make-NTU-Hackathon 0:4233b7b10255 6552 "T" + toPaddedString(2, hours) + ":" + toPaddedString(2, minutes) + ":" + toPaddedString(2, seconds) +
group-Make-NTU-Hackathon 0:4233b7b10255 6553 // Milliseconds are optional in ES 5.0, but required in 5.1.
group-Make-NTU-Hackathon 0:4233b7b10255 6554 "." + toPaddedString(3, milliseconds) + "Z";
group-Make-NTU-Hackathon 0:4233b7b10255 6555 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6556 value = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6557 }
group-Make-NTU-Hackathon 0:4233b7b10255 6558 } else if (typeof value.toJSON == "function" && ((className != numberClass && className != stringClass && className != arrayClass) || isProperty.call(value, "toJSON"))) {
group-Make-NTU-Hackathon 0:4233b7b10255 6559 // Prototype <= 1.6.1 adds non-standard `toJSON` methods to the
group-Make-NTU-Hackathon 0:4233b7b10255 6560 // `Number`, `String`, `Date`, and `Array` prototypes. JSON 3
group-Make-NTU-Hackathon 0:4233b7b10255 6561 // ignores all `toJSON` methods on these objects unless they are
group-Make-NTU-Hackathon 0:4233b7b10255 6562 // defined directly on an instance.
group-Make-NTU-Hackathon 0:4233b7b10255 6563 value = value.toJSON(property);
group-Make-NTU-Hackathon 0:4233b7b10255 6564 }
group-Make-NTU-Hackathon 0:4233b7b10255 6565 }
group-Make-NTU-Hackathon 0:4233b7b10255 6566 if (callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6567 // If a replacement function was provided, call it to obtain the value
group-Make-NTU-Hackathon 0:4233b7b10255 6568 // for serialization.
group-Make-NTU-Hackathon 0:4233b7b10255 6569 value = callback.call(object, property, value);
group-Make-NTU-Hackathon 0:4233b7b10255 6570 }
group-Make-NTU-Hackathon 0:4233b7b10255 6571 if (value === null) {
group-Make-NTU-Hackathon 0:4233b7b10255 6572 return "null";
group-Make-NTU-Hackathon 0:4233b7b10255 6573 }
group-Make-NTU-Hackathon 0:4233b7b10255 6574 className = getClass.call(value);
group-Make-NTU-Hackathon 0:4233b7b10255 6575 if (className == booleanClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6576 // Booleans are represented literally.
group-Make-NTU-Hackathon 0:4233b7b10255 6577 return "" + value;
group-Make-NTU-Hackathon 0:4233b7b10255 6578 } else if (className == numberClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6579 // JSON numbers must be finite. `Infinity` and `NaN` are serialized as
group-Make-NTU-Hackathon 0:4233b7b10255 6580 // `"null"`.
group-Make-NTU-Hackathon 0:4233b7b10255 6581 return value > -1 / 0 && value < 1 / 0 ? "" + value : "null";
group-Make-NTU-Hackathon 0:4233b7b10255 6582 } else if (className == stringClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6583 // Strings are double-quoted and escaped.
group-Make-NTU-Hackathon 0:4233b7b10255 6584 return quote("" + value);
group-Make-NTU-Hackathon 0:4233b7b10255 6585 }
group-Make-NTU-Hackathon 0:4233b7b10255 6586 // Recursively serialize objects and arrays.
group-Make-NTU-Hackathon 0:4233b7b10255 6587 if (typeof value == "object") {
group-Make-NTU-Hackathon 0:4233b7b10255 6588 // Check for cyclic structures. This is a linear search; performance
group-Make-NTU-Hackathon 0:4233b7b10255 6589 // is inversely proportional to the number of unique nested objects.
group-Make-NTU-Hackathon 0:4233b7b10255 6590 for (length = stack.length; length--;) {
group-Make-NTU-Hackathon 0:4233b7b10255 6591 if (stack[length] === value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6592 // Cyclic structures cannot be serialized by `JSON.stringify`.
group-Make-NTU-Hackathon 0:4233b7b10255 6593 throw TypeError();
group-Make-NTU-Hackathon 0:4233b7b10255 6594 }
group-Make-NTU-Hackathon 0:4233b7b10255 6595 }
group-Make-NTU-Hackathon 0:4233b7b10255 6596 // Add the object to the stack of traversed objects.
group-Make-NTU-Hackathon 0:4233b7b10255 6597 stack.push(value);
group-Make-NTU-Hackathon 0:4233b7b10255 6598 results = [];
group-Make-NTU-Hackathon 0:4233b7b10255 6599 // Save the current indentation level and indent one additional level.
group-Make-NTU-Hackathon 0:4233b7b10255 6600 prefix = indentation;
group-Make-NTU-Hackathon 0:4233b7b10255 6601 indentation += whitespace;
group-Make-NTU-Hackathon 0:4233b7b10255 6602 if (className == arrayClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6603 // Recursively serialize array elements.
group-Make-NTU-Hackathon 0:4233b7b10255 6604 for (index = 0, length = value.length; index < length; index++) {
group-Make-NTU-Hackathon 0:4233b7b10255 6605 element = serialize(index, value, callback, properties, whitespace, indentation, stack);
group-Make-NTU-Hackathon 0:4233b7b10255 6606 results.push(element === undef ? "null" : element);
group-Make-NTU-Hackathon 0:4233b7b10255 6607 }
group-Make-NTU-Hackathon 0:4233b7b10255 6608 result = results.length ? (whitespace ? "[\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "]" : ("[" + results.join(",") + "]")) : "[]";
group-Make-NTU-Hackathon 0:4233b7b10255 6609 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6610 // Recursively serialize object members. Members are selected from
group-Make-NTU-Hackathon 0:4233b7b10255 6611 // either a user-specified list of property names, or the object
group-Make-NTU-Hackathon 0:4233b7b10255 6612 // itself.
group-Make-NTU-Hackathon 0:4233b7b10255 6613 forEach(properties || value, function (property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6614 var element = serialize(property, value, callback, properties, whitespace, indentation, stack);
group-Make-NTU-Hackathon 0:4233b7b10255 6615 if (element !== undef) {
group-Make-NTU-Hackathon 0:4233b7b10255 6616 // According to ES 5.1 section 15.12.3: "If `gap` {whitespace}
group-Make-NTU-Hackathon 0:4233b7b10255 6617 // is not the empty string, let `member` {quote(property) + ":"}
group-Make-NTU-Hackathon 0:4233b7b10255 6618 // be the concatenation of `member` and the `space` character."
group-Make-NTU-Hackathon 0:4233b7b10255 6619 // The "`space` character" refers to the literal space
group-Make-NTU-Hackathon 0:4233b7b10255 6620 // character, not the `space` {width} argument provided to
group-Make-NTU-Hackathon 0:4233b7b10255 6621 // `JSON.stringify`.
group-Make-NTU-Hackathon 0:4233b7b10255 6622 results.push(quote(property) + ":" + (whitespace ? " " : "") + element);
group-Make-NTU-Hackathon 0:4233b7b10255 6623 }
group-Make-NTU-Hackathon 0:4233b7b10255 6624 });
group-Make-NTU-Hackathon 0:4233b7b10255 6625 result = results.length ? (whitespace ? "{\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "}" : ("{" + results.join(",") + "}")) : "{}";
group-Make-NTU-Hackathon 0:4233b7b10255 6626 }
group-Make-NTU-Hackathon 0:4233b7b10255 6627 // Remove the object from the traversed object stack.
group-Make-NTU-Hackathon 0:4233b7b10255 6628 stack.pop();
group-Make-NTU-Hackathon 0:4233b7b10255 6629 return result;
group-Make-NTU-Hackathon 0:4233b7b10255 6630 }
group-Make-NTU-Hackathon 0:4233b7b10255 6631 };
group-Make-NTU-Hackathon 0:4233b7b10255 6632
group-Make-NTU-Hackathon 0:4233b7b10255 6633 // Public: `JSON.stringify`. See ES 5.1 section 15.12.3.
group-Make-NTU-Hackathon 0:4233b7b10255 6634 JSON3.stringify = function (source, filter, width) {
group-Make-NTU-Hackathon 0:4233b7b10255 6635 var whitespace, callback, properties, className;
group-Make-NTU-Hackathon 0:4233b7b10255 6636 if (typeof filter == "function" || typeof filter == "object" && filter) {
group-Make-NTU-Hackathon 0:4233b7b10255 6637 if ((className = getClass.call(filter)) == functionClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6638 callback = filter;
group-Make-NTU-Hackathon 0:4233b7b10255 6639 } else if (className == arrayClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6640 // Convert the property names array into a makeshift set.
group-Make-NTU-Hackathon 0:4233b7b10255 6641 properties = {};
group-Make-NTU-Hackathon 0:4233b7b10255 6642 for (var index = 0, length = filter.length, value; index < length; value = filter[index++], ((className = getClass.call(value)), className == stringClass || className == numberClass) && (properties[value] = 1));
group-Make-NTU-Hackathon 0:4233b7b10255 6643 }
group-Make-NTU-Hackathon 0:4233b7b10255 6644 }
group-Make-NTU-Hackathon 0:4233b7b10255 6645 if (width) {
group-Make-NTU-Hackathon 0:4233b7b10255 6646 if ((className = getClass.call(width)) == numberClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6647 // Convert the `width` to an integer and create a string containing
group-Make-NTU-Hackathon 0:4233b7b10255 6648 // `width` number of space characters.
group-Make-NTU-Hackathon 0:4233b7b10255 6649 if ((width -= width % 1) > 0) {
group-Make-NTU-Hackathon 0:4233b7b10255 6650 for (whitespace = "", width > 10 && (width = 10); whitespace.length < width; whitespace += " ");
group-Make-NTU-Hackathon 0:4233b7b10255 6651 }
group-Make-NTU-Hackathon 0:4233b7b10255 6652 } else if (className == stringClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6653 whitespace = width.length <= 10 ? width : width.slice(0, 10);
group-Make-NTU-Hackathon 0:4233b7b10255 6654 }
group-Make-NTU-Hackathon 0:4233b7b10255 6655 }
group-Make-NTU-Hackathon 0:4233b7b10255 6656 // Opera <= 7.54u2 discards the values associated with empty string keys
group-Make-NTU-Hackathon 0:4233b7b10255 6657 // (`""`) only if they are used directly within an object member list
group-Make-NTU-Hackathon 0:4233b7b10255 6658 // (e.g., `!("" in { "": 1})`).
group-Make-NTU-Hackathon 0:4233b7b10255 6659 return serialize("", (value = {}, value[""] = source, value), callback, properties, whitespace, "", []);
group-Make-NTU-Hackathon 0:4233b7b10255 6660 };
group-Make-NTU-Hackathon 0:4233b7b10255 6661 }
group-Make-NTU-Hackathon 0:4233b7b10255 6662
group-Make-NTU-Hackathon 0:4233b7b10255 6663 // Public: Parses a JSON source string.
group-Make-NTU-Hackathon 0:4233b7b10255 6664 if (!has("json-parse")) {
group-Make-NTU-Hackathon 0:4233b7b10255 6665 var fromCharCode = String.fromCharCode;
group-Make-NTU-Hackathon 0:4233b7b10255 6666
group-Make-NTU-Hackathon 0:4233b7b10255 6667 // Internal: A map of escaped control characters and their unescaped
group-Make-NTU-Hackathon 0:4233b7b10255 6668 // equivalents.
group-Make-NTU-Hackathon 0:4233b7b10255 6669 var Unescapes = {
group-Make-NTU-Hackathon 0:4233b7b10255 6670 92: "\\",
group-Make-NTU-Hackathon 0:4233b7b10255 6671 34: '"',
group-Make-NTU-Hackathon 0:4233b7b10255 6672 47: "/",
group-Make-NTU-Hackathon 0:4233b7b10255 6673 98: "\b",
group-Make-NTU-Hackathon 0:4233b7b10255 6674 116: "\t",
group-Make-NTU-Hackathon 0:4233b7b10255 6675 110: "\n",
group-Make-NTU-Hackathon 0:4233b7b10255 6676 102: "\f",
group-Make-NTU-Hackathon 0:4233b7b10255 6677 114: "\r"
group-Make-NTU-Hackathon 0:4233b7b10255 6678 };
group-Make-NTU-Hackathon 0:4233b7b10255 6679
group-Make-NTU-Hackathon 0:4233b7b10255 6680 // Internal: Stores the parser state.
group-Make-NTU-Hackathon 0:4233b7b10255 6681 var Index, Source;
group-Make-NTU-Hackathon 0:4233b7b10255 6682
group-Make-NTU-Hackathon 0:4233b7b10255 6683 // Internal: Resets the parser state and throws a `SyntaxError`.
group-Make-NTU-Hackathon 0:4233b7b10255 6684 var abort = function() {
group-Make-NTU-Hackathon 0:4233b7b10255 6685 Index = Source = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6686 throw SyntaxError();
group-Make-NTU-Hackathon 0:4233b7b10255 6687 };
group-Make-NTU-Hackathon 0:4233b7b10255 6688
group-Make-NTU-Hackathon 0:4233b7b10255 6689 // Internal: Returns the next token, or `"$"` if the parser has reached
group-Make-NTU-Hackathon 0:4233b7b10255 6690 // the end of the source string. A token may be a string, number, `null`
group-Make-NTU-Hackathon 0:4233b7b10255 6691 // literal, or Boolean literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6692 var lex = function () {
group-Make-NTU-Hackathon 0:4233b7b10255 6693 var source = Source, length = source.length, value, begin, position, isSigned, charCode;
group-Make-NTU-Hackathon 0:4233b7b10255 6694 while (Index < length) {
group-Make-NTU-Hackathon 0:4233b7b10255 6695 charCode = source.charCodeAt(Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6696 switch (charCode) {
group-Make-NTU-Hackathon 0:4233b7b10255 6697 case 9: case 10: case 13: case 32:
group-Make-NTU-Hackathon 0:4233b7b10255 6698 // Skip whitespace tokens, including tabs, carriage returns, line
group-Make-NTU-Hackathon 0:4233b7b10255 6699 // feeds, and space characters.
group-Make-NTU-Hackathon 0:4233b7b10255 6700 Index++;
group-Make-NTU-Hackathon 0:4233b7b10255 6701 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6702 case 123: case 125: case 91: case 93: case 58: case 44:
group-Make-NTU-Hackathon 0:4233b7b10255 6703 // Parse a punctuator token (`{`, `}`, `[`, `]`, `:`, or `,`) at
group-Make-NTU-Hackathon 0:4233b7b10255 6704 // the current position.
group-Make-NTU-Hackathon 0:4233b7b10255 6705 value = charIndexBuggy ? source.charAt(Index) : source[Index];
group-Make-NTU-Hackathon 0:4233b7b10255 6706 Index++;
group-Make-NTU-Hackathon 0:4233b7b10255 6707 return value;
group-Make-NTU-Hackathon 0:4233b7b10255 6708 case 34:
group-Make-NTU-Hackathon 0:4233b7b10255 6709 // `"` delimits a JSON string; advance to the next character and
group-Make-NTU-Hackathon 0:4233b7b10255 6710 // begin parsing the string. String tokens are prefixed with the
group-Make-NTU-Hackathon 0:4233b7b10255 6711 // sentinel `@` character to distinguish them from punctuators and
group-Make-NTU-Hackathon 0:4233b7b10255 6712 // end-of-string tokens.
group-Make-NTU-Hackathon 0:4233b7b10255 6713 for (value = "@", Index++; Index < length;) {
group-Make-NTU-Hackathon 0:4233b7b10255 6714 charCode = source.charCodeAt(Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6715 if (charCode < 32) {
group-Make-NTU-Hackathon 0:4233b7b10255 6716 // Unescaped ASCII control characters (those with a code unit
group-Make-NTU-Hackathon 0:4233b7b10255 6717 // less than the space character) are not permitted.
group-Make-NTU-Hackathon 0:4233b7b10255 6718 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6719 } else if (charCode == 92) {
group-Make-NTU-Hackathon 0:4233b7b10255 6720 // A reverse solidus (`\`) marks the beginning of an escaped
group-Make-NTU-Hackathon 0:4233b7b10255 6721 // control character (including `"`, `\`, and `/`) or Unicode
group-Make-NTU-Hackathon 0:4233b7b10255 6722 // escape sequence.
group-Make-NTU-Hackathon 0:4233b7b10255 6723 charCode = source.charCodeAt(++Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6724 switch (charCode) {
group-Make-NTU-Hackathon 0:4233b7b10255 6725 case 92: case 34: case 47: case 98: case 116: case 110: case 102: case 114:
group-Make-NTU-Hackathon 0:4233b7b10255 6726 // Revive escaped control characters.
group-Make-NTU-Hackathon 0:4233b7b10255 6727 value += Unescapes[charCode];
group-Make-NTU-Hackathon 0:4233b7b10255 6728 Index++;
group-Make-NTU-Hackathon 0:4233b7b10255 6729 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6730 case 117:
group-Make-NTU-Hackathon 0:4233b7b10255 6731 // `\u` marks the beginning of a Unicode escape sequence.
group-Make-NTU-Hackathon 0:4233b7b10255 6732 // Advance to the first character and validate the
group-Make-NTU-Hackathon 0:4233b7b10255 6733 // four-digit code point.
group-Make-NTU-Hackathon 0:4233b7b10255 6734 begin = ++Index;
group-Make-NTU-Hackathon 0:4233b7b10255 6735 for (position = Index + 4; Index < position; Index++) {
group-Make-NTU-Hackathon 0:4233b7b10255 6736 charCode = source.charCodeAt(Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6737 // A valid sequence comprises four hexdigits (case-
group-Make-NTU-Hackathon 0:4233b7b10255 6738 // insensitive) that form a single hexadecimal value.
group-Make-NTU-Hackathon 0:4233b7b10255 6739 if (!(charCode >= 48 && charCode <= 57 || charCode >= 97 && charCode <= 102 || charCode >= 65 && charCode <= 70)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6740 // Invalid Unicode escape sequence.
group-Make-NTU-Hackathon 0:4233b7b10255 6741 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6742 }
group-Make-NTU-Hackathon 0:4233b7b10255 6743 }
group-Make-NTU-Hackathon 0:4233b7b10255 6744 // Revive the escaped character.
group-Make-NTU-Hackathon 0:4233b7b10255 6745 value += fromCharCode("0x" + source.slice(begin, Index));
group-Make-NTU-Hackathon 0:4233b7b10255 6746 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6747 default:
group-Make-NTU-Hackathon 0:4233b7b10255 6748 // Invalid escape sequence.
group-Make-NTU-Hackathon 0:4233b7b10255 6749 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6750 }
group-Make-NTU-Hackathon 0:4233b7b10255 6751 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6752 if (charCode == 34) {
group-Make-NTU-Hackathon 0:4233b7b10255 6753 // An unescaped double-quote character marks the end of the
group-Make-NTU-Hackathon 0:4233b7b10255 6754 // string.
group-Make-NTU-Hackathon 0:4233b7b10255 6755 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6756 }
group-Make-NTU-Hackathon 0:4233b7b10255 6757 charCode = source.charCodeAt(Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6758 begin = Index;
group-Make-NTU-Hackathon 0:4233b7b10255 6759 // Optimize for the common case where a string is valid.
group-Make-NTU-Hackathon 0:4233b7b10255 6760 while (charCode >= 32 && charCode != 92 && charCode != 34) {
group-Make-NTU-Hackathon 0:4233b7b10255 6761 charCode = source.charCodeAt(++Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6762 }
group-Make-NTU-Hackathon 0:4233b7b10255 6763 // Append the string as-is.
group-Make-NTU-Hackathon 0:4233b7b10255 6764 value += source.slice(begin, Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6765 }
group-Make-NTU-Hackathon 0:4233b7b10255 6766 }
group-Make-NTU-Hackathon 0:4233b7b10255 6767 if (source.charCodeAt(Index) == 34) {
group-Make-NTU-Hackathon 0:4233b7b10255 6768 // Advance to the next character and return the revived string.
group-Make-NTU-Hackathon 0:4233b7b10255 6769 Index++;
group-Make-NTU-Hackathon 0:4233b7b10255 6770 return value;
group-Make-NTU-Hackathon 0:4233b7b10255 6771 }
group-Make-NTU-Hackathon 0:4233b7b10255 6772 // Unterminated string.
group-Make-NTU-Hackathon 0:4233b7b10255 6773 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6774 default:
group-Make-NTU-Hackathon 0:4233b7b10255 6775 // Parse numbers and literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6776 begin = Index;
group-Make-NTU-Hackathon 0:4233b7b10255 6777 // Advance past the negative sign, if one is specified.
group-Make-NTU-Hackathon 0:4233b7b10255 6778 if (charCode == 45) {
group-Make-NTU-Hackathon 0:4233b7b10255 6779 isSigned = true;
group-Make-NTU-Hackathon 0:4233b7b10255 6780 charCode = source.charCodeAt(++Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6781 }
group-Make-NTU-Hackathon 0:4233b7b10255 6782 // Parse an integer or floating-point value.
group-Make-NTU-Hackathon 0:4233b7b10255 6783 if (charCode >= 48 && charCode <= 57) {
group-Make-NTU-Hackathon 0:4233b7b10255 6784 // Leading zeroes are interpreted as octal literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6785 if (charCode == 48 && ((charCode = source.charCodeAt(Index + 1)), charCode >= 48 && charCode <= 57)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6786 // Illegal octal literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6787 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6788 }
group-Make-NTU-Hackathon 0:4233b7b10255 6789 isSigned = false;
group-Make-NTU-Hackathon 0:4233b7b10255 6790 // Parse the integer component.
group-Make-NTU-Hackathon 0:4233b7b10255 6791 for (; Index < length && ((charCode = source.charCodeAt(Index)), charCode >= 48 && charCode <= 57); Index++);
group-Make-NTU-Hackathon 0:4233b7b10255 6792 // Floats cannot contain a leading decimal point; however, this
group-Make-NTU-Hackathon 0:4233b7b10255 6793 // case is already accounted for by the parser.
group-Make-NTU-Hackathon 0:4233b7b10255 6794 if (source.charCodeAt(Index) == 46) {
group-Make-NTU-Hackathon 0:4233b7b10255 6795 position = ++Index;
group-Make-NTU-Hackathon 0:4233b7b10255 6796 // Parse the decimal component.
group-Make-NTU-Hackathon 0:4233b7b10255 6797 for (; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
group-Make-NTU-Hackathon 0:4233b7b10255 6798 if (position == Index) {
group-Make-NTU-Hackathon 0:4233b7b10255 6799 // Illegal trailing decimal.
group-Make-NTU-Hackathon 0:4233b7b10255 6800 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6801 }
group-Make-NTU-Hackathon 0:4233b7b10255 6802 Index = position;
group-Make-NTU-Hackathon 0:4233b7b10255 6803 }
group-Make-NTU-Hackathon 0:4233b7b10255 6804 // Parse exponents. The `e` denoting the exponent is
group-Make-NTU-Hackathon 0:4233b7b10255 6805 // case-insensitive.
group-Make-NTU-Hackathon 0:4233b7b10255 6806 charCode = source.charCodeAt(Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6807 if (charCode == 101 || charCode == 69) {
group-Make-NTU-Hackathon 0:4233b7b10255 6808 charCode = source.charCodeAt(++Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6809 // Skip past the sign following the exponent, if one is
group-Make-NTU-Hackathon 0:4233b7b10255 6810 // specified.
group-Make-NTU-Hackathon 0:4233b7b10255 6811 if (charCode == 43 || charCode == 45) {
group-Make-NTU-Hackathon 0:4233b7b10255 6812 Index++;
group-Make-NTU-Hackathon 0:4233b7b10255 6813 }
group-Make-NTU-Hackathon 0:4233b7b10255 6814 // Parse the exponential component.
group-Make-NTU-Hackathon 0:4233b7b10255 6815 for (position = Index; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
group-Make-NTU-Hackathon 0:4233b7b10255 6816 if (position == Index) {
group-Make-NTU-Hackathon 0:4233b7b10255 6817 // Illegal empty exponent.
group-Make-NTU-Hackathon 0:4233b7b10255 6818 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6819 }
group-Make-NTU-Hackathon 0:4233b7b10255 6820 Index = position;
group-Make-NTU-Hackathon 0:4233b7b10255 6821 }
group-Make-NTU-Hackathon 0:4233b7b10255 6822 // Coerce the parsed value to a JavaScript number.
group-Make-NTU-Hackathon 0:4233b7b10255 6823 return +source.slice(begin, Index);
group-Make-NTU-Hackathon 0:4233b7b10255 6824 }
group-Make-NTU-Hackathon 0:4233b7b10255 6825 // A negative sign may only precede numbers.
group-Make-NTU-Hackathon 0:4233b7b10255 6826 if (isSigned) {
group-Make-NTU-Hackathon 0:4233b7b10255 6827 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6828 }
group-Make-NTU-Hackathon 0:4233b7b10255 6829 // `true`, `false`, and `null` literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6830 if (source.slice(Index, Index + 4) == "true") {
group-Make-NTU-Hackathon 0:4233b7b10255 6831 Index += 4;
group-Make-NTU-Hackathon 0:4233b7b10255 6832 return true;
group-Make-NTU-Hackathon 0:4233b7b10255 6833 } else if (source.slice(Index, Index + 5) == "false") {
group-Make-NTU-Hackathon 0:4233b7b10255 6834 Index += 5;
group-Make-NTU-Hackathon 0:4233b7b10255 6835 return false;
group-Make-NTU-Hackathon 0:4233b7b10255 6836 } else if (source.slice(Index, Index + 4) == "null") {
group-Make-NTU-Hackathon 0:4233b7b10255 6837 Index += 4;
group-Make-NTU-Hackathon 0:4233b7b10255 6838 return null;
group-Make-NTU-Hackathon 0:4233b7b10255 6839 }
group-Make-NTU-Hackathon 0:4233b7b10255 6840 // Unrecognized token.
group-Make-NTU-Hackathon 0:4233b7b10255 6841 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6842 }
group-Make-NTU-Hackathon 0:4233b7b10255 6843 }
group-Make-NTU-Hackathon 0:4233b7b10255 6844 // Return the sentinel `$` character if the parser has reached the end
group-Make-NTU-Hackathon 0:4233b7b10255 6845 // of the source string.
group-Make-NTU-Hackathon 0:4233b7b10255 6846 return "$";
group-Make-NTU-Hackathon 0:4233b7b10255 6847 };
group-Make-NTU-Hackathon 0:4233b7b10255 6848
group-Make-NTU-Hackathon 0:4233b7b10255 6849 // Internal: Parses a JSON `value` token.
group-Make-NTU-Hackathon 0:4233b7b10255 6850 var get = function (value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6851 var results, hasMembers;
group-Make-NTU-Hackathon 0:4233b7b10255 6852 if (value == "$") {
group-Make-NTU-Hackathon 0:4233b7b10255 6853 // Unexpected end of input.
group-Make-NTU-Hackathon 0:4233b7b10255 6854 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6855 }
group-Make-NTU-Hackathon 0:4233b7b10255 6856 if (typeof value == "string") {
group-Make-NTU-Hackathon 0:4233b7b10255 6857 if ((charIndexBuggy ? value.charAt(0) : value[0]) == "@") {
group-Make-NTU-Hackathon 0:4233b7b10255 6858 // Remove the sentinel `@` character.
group-Make-NTU-Hackathon 0:4233b7b10255 6859 return value.slice(1);
group-Make-NTU-Hackathon 0:4233b7b10255 6860 }
group-Make-NTU-Hackathon 0:4233b7b10255 6861 // Parse object and array literals.
group-Make-NTU-Hackathon 0:4233b7b10255 6862 if (value == "[") {
group-Make-NTU-Hackathon 0:4233b7b10255 6863 // Parses a JSON array, returning a new JavaScript array.
group-Make-NTU-Hackathon 0:4233b7b10255 6864 results = [];
group-Make-NTU-Hackathon 0:4233b7b10255 6865 for (;; hasMembers || (hasMembers = true)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6866 value = lex();
group-Make-NTU-Hackathon 0:4233b7b10255 6867 // A closing square bracket marks the end of the array literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6868 if (value == "]") {
group-Make-NTU-Hackathon 0:4233b7b10255 6869 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6870 }
group-Make-NTU-Hackathon 0:4233b7b10255 6871 // If the array literal contains elements, the current token
group-Make-NTU-Hackathon 0:4233b7b10255 6872 // should be a comma separating the previous element from the
group-Make-NTU-Hackathon 0:4233b7b10255 6873 // next.
group-Make-NTU-Hackathon 0:4233b7b10255 6874 if (hasMembers) {
group-Make-NTU-Hackathon 0:4233b7b10255 6875 if (value == ",") {
group-Make-NTU-Hackathon 0:4233b7b10255 6876 value = lex();
group-Make-NTU-Hackathon 0:4233b7b10255 6877 if (value == "]") {
group-Make-NTU-Hackathon 0:4233b7b10255 6878 // Unexpected trailing `,` in array literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6879 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6880 }
group-Make-NTU-Hackathon 0:4233b7b10255 6881 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6882 // A `,` must separate each array element.
group-Make-NTU-Hackathon 0:4233b7b10255 6883 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6884 }
group-Make-NTU-Hackathon 0:4233b7b10255 6885 }
group-Make-NTU-Hackathon 0:4233b7b10255 6886 // Elisions and leading commas are not permitted.
group-Make-NTU-Hackathon 0:4233b7b10255 6887 if (value == ",") {
group-Make-NTU-Hackathon 0:4233b7b10255 6888 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6889 }
group-Make-NTU-Hackathon 0:4233b7b10255 6890 results.push(get(value));
group-Make-NTU-Hackathon 0:4233b7b10255 6891 }
group-Make-NTU-Hackathon 0:4233b7b10255 6892 return results;
group-Make-NTU-Hackathon 0:4233b7b10255 6893 } else if (value == "{") {
group-Make-NTU-Hackathon 0:4233b7b10255 6894 // Parses a JSON object, returning a new JavaScript object.
group-Make-NTU-Hackathon 0:4233b7b10255 6895 results = {};
group-Make-NTU-Hackathon 0:4233b7b10255 6896 for (;; hasMembers || (hasMembers = true)) {
group-Make-NTU-Hackathon 0:4233b7b10255 6897 value = lex();
group-Make-NTU-Hackathon 0:4233b7b10255 6898 // A closing curly brace marks the end of the object literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6899 if (value == "}") {
group-Make-NTU-Hackathon 0:4233b7b10255 6900 break;
group-Make-NTU-Hackathon 0:4233b7b10255 6901 }
group-Make-NTU-Hackathon 0:4233b7b10255 6902 // If the object literal contains members, the current token
group-Make-NTU-Hackathon 0:4233b7b10255 6903 // should be a comma separator.
group-Make-NTU-Hackathon 0:4233b7b10255 6904 if (hasMembers) {
group-Make-NTU-Hackathon 0:4233b7b10255 6905 if (value == ",") {
group-Make-NTU-Hackathon 0:4233b7b10255 6906 value = lex();
group-Make-NTU-Hackathon 0:4233b7b10255 6907 if (value == "}") {
group-Make-NTU-Hackathon 0:4233b7b10255 6908 // Unexpected trailing `,` in object literal.
group-Make-NTU-Hackathon 0:4233b7b10255 6909 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6910 }
group-Make-NTU-Hackathon 0:4233b7b10255 6911 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6912 // A `,` must separate each object member.
group-Make-NTU-Hackathon 0:4233b7b10255 6913 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6914 }
group-Make-NTU-Hackathon 0:4233b7b10255 6915 }
group-Make-NTU-Hackathon 0:4233b7b10255 6916 // Leading commas are not permitted, object property names must be
group-Make-NTU-Hackathon 0:4233b7b10255 6917 // double-quoted strings, and a `:` must separate each property
group-Make-NTU-Hackathon 0:4233b7b10255 6918 // name and value.
group-Make-NTU-Hackathon 0:4233b7b10255 6919 if (value == "," || typeof value != "string" || (charIndexBuggy ? value.charAt(0) : value[0]) != "@" || lex() != ":") {
group-Make-NTU-Hackathon 0:4233b7b10255 6920 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6921 }
group-Make-NTU-Hackathon 0:4233b7b10255 6922 results[value.slice(1)] = get(lex());
group-Make-NTU-Hackathon 0:4233b7b10255 6923 }
group-Make-NTU-Hackathon 0:4233b7b10255 6924 return results;
group-Make-NTU-Hackathon 0:4233b7b10255 6925 }
group-Make-NTU-Hackathon 0:4233b7b10255 6926 // Unexpected token encountered.
group-Make-NTU-Hackathon 0:4233b7b10255 6927 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6928 }
group-Make-NTU-Hackathon 0:4233b7b10255 6929 return value;
group-Make-NTU-Hackathon 0:4233b7b10255 6930 };
group-Make-NTU-Hackathon 0:4233b7b10255 6931
group-Make-NTU-Hackathon 0:4233b7b10255 6932 // Internal: Updates a traversed object member.
group-Make-NTU-Hackathon 0:4233b7b10255 6933 var update = function(source, property, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6934 var element = walk(source, property, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 6935 if (element === undef) {
group-Make-NTU-Hackathon 0:4233b7b10255 6936 delete source[property];
group-Make-NTU-Hackathon 0:4233b7b10255 6937 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6938 source[property] = element;
group-Make-NTU-Hackathon 0:4233b7b10255 6939 }
group-Make-NTU-Hackathon 0:4233b7b10255 6940 };
group-Make-NTU-Hackathon 0:4233b7b10255 6941
group-Make-NTU-Hackathon 0:4233b7b10255 6942 // Internal: Recursively traverses a parsed JSON object, invoking the
group-Make-NTU-Hackathon 0:4233b7b10255 6943 // `callback` function for each value. This is an implementation of the
group-Make-NTU-Hackathon 0:4233b7b10255 6944 // `Walk(holder, name)` operation defined in ES 5.1 section 15.12.2.
group-Make-NTU-Hackathon 0:4233b7b10255 6945 var walk = function (source, property, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6946 var value = source[property], length;
group-Make-NTU-Hackathon 0:4233b7b10255 6947 if (typeof value == "object" && value) {
group-Make-NTU-Hackathon 0:4233b7b10255 6948 // `forEach` can't be used to traverse an array in Opera <= 8.54
group-Make-NTU-Hackathon 0:4233b7b10255 6949 // because its `Object#hasOwnProperty` implementation returns `false`
group-Make-NTU-Hackathon 0:4233b7b10255 6950 // for array indices (e.g., `![1, 2, 3].hasOwnProperty("0")`).
group-Make-NTU-Hackathon 0:4233b7b10255 6951 if (getClass.call(value) == arrayClass) {
group-Make-NTU-Hackathon 0:4233b7b10255 6952 for (length = value.length; length--;) {
group-Make-NTU-Hackathon 0:4233b7b10255 6953 update(value, length, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 6954 }
group-Make-NTU-Hackathon 0:4233b7b10255 6955 } else {
group-Make-NTU-Hackathon 0:4233b7b10255 6956 forEach(value, function (property) {
group-Make-NTU-Hackathon 0:4233b7b10255 6957 update(value, property, callback);
group-Make-NTU-Hackathon 0:4233b7b10255 6958 });
group-Make-NTU-Hackathon 0:4233b7b10255 6959 }
group-Make-NTU-Hackathon 0:4233b7b10255 6960 }
group-Make-NTU-Hackathon 0:4233b7b10255 6961 return callback.call(source, property, value);
group-Make-NTU-Hackathon 0:4233b7b10255 6962 };
group-Make-NTU-Hackathon 0:4233b7b10255 6963
group-Make-NTU-Hackathon 0:4233b7b10255 6964 // Public: `JSON.parse`. See ES 5.1 section 15.12.2.
group-Make-NTU-Hackathon 0:4233b7b10255 6965 JSON3.parse = function (source, callback) {
group-Make-NTU-Hackathon 0:4233b7b10255 6966 var result, value;
group-Make-NTU-Hackathon 0:4233b7b10255 6967 Index = 0;
group-Make-NTU-Hackathon 0:4233b7b10255 6968 Source = "" + source;
group-Make-NTU-Hackathon 0:4233b7b10255 6969 result = get(lex());
group-Make-NTU-Hackathon 0:4233b7b10255 6970 // If a JSON string contains multiple tokens, it is invalid.
group-Make-NTU-Hackathon 0:4233b7b10255 6971 if (lex() != "$") {
group-Make-NTU-Hackathon 0:4233b7b10255 6972 abort();
group-Make-NTU-Hackathon 0:4233b7b10255 6973 }
group-Make-NTU-Hackathon 0:4233b7b10255 6974 // Reset the parser state.
group-Make-NTU-Hackathon 0:4233b7b10255 6975 Index = Source = null;
group-Make-NTU-Hackathon 0:4233b7b10255 6976 return callback && getClass.call(callback) == functionClass ? walk((value = {}, value[""] = result, value), "", callback) : result;
group-Make-NTU-Hackathon 0:4233b7b10255 6977 };
group-Make-NTU-Hackathon 0:4233b7b10255 6978 }
group-Make-NTU-Hackathon 0:4233b7b10255 6979 }
group-Make-NTU-Hackathon 0:4233b7b10255 6980
group-Make-NTU-Hackathon 0:4233b7b10255 6981 // Export for asynchronous module loaders.
group-Make-NTU-Hackathon 0:4233b7b10255 6982 if (isLoader) {
group-Make-NTU-Hackathon 0:4233b7b10255 6983 define(function () {
group-Make-NTU-Hackathon 0:4233b7b10255 6984 return JSON3;
group-Make-NTU-Hackathon 0:4233b7b10255 6985 });
group-Make-NTU-Hackathon 0:4233b7b10255 6986 }
group-Make-NTU-Hackathon 0:4233b7b10255 6987 }(this));
group-Make-NTU-Hackathon 0:4233b7b10255 6988
group-Make-NTU-Hackathon 0:4233b7b10255 6989 },{}],50:[function(_dereq_,module,exports){
group-Make-NTU-Hackathon 0:4233b7b10255 6990 module.exports = toArray
group-Make-NTU-Hackathon 0:4233b7b10255 6991
group-Make-NTU-Hackathon 0:4233b7b10255 6992 function toArray(list, index) {
group-Make-NTU-Hackathon 0:4233b7b10255 6993 var array = []
group-Make-NTU-Hackathon 0:4233b7b10255 6994
group-Make-NTU-Hackathon 0:4233b7b10255 6995 index = index || 0
group-Make-NTU-Hackathon 0:4233b7b10255 6996
group-Make-NTU-Hackathon 0:4233b7b10255 6997 for (var i = index || 0; i < list.length; i++) {
group-Make-NTU-Hackathon 0:4233b7b10255 6998 array[i - index] = list[i]
group-Make-NTU-Hackathon 0:4233b7b10255 6999 }
group-Make-NTU-Hackathon 0:4233b7b10255 7000
group-Make-NTU-Hackathon 0:4233b7b10255 7001 return array
group-Make-NTU-Hackathon 0:4233b7b10255 7002 }
group-Make-NTU-Hackathon 0:4233b7b10255 7003
group-Make-NTU-Hackathon 0:4233b7b10255 7004 },{}]},{},[1])
group-Make-NTU-Hackathon 0:4233b7b10255 7005 (1)
group-Make-NTU-Hackathon 0:4233b7b10255 7006 });