1 |
|
2 |
|
3 |
|
4 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.socketCluster = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(_dereq_,module,exports){
|
5 | var SCClientSocket = _dereq_('./lib/scclientsocket');
|
6 | var factory = _dereq_('./lib/factory');
|
7 |
|
8 | module.exports.factory = factory;
|
9 | module.exports.SCClientSocket = SCClientSocket;
|
10 |
|
11 | module.exports.Emitter = _dereq_('component-emitter');
|
12 |
|
13 | module.exports.create = function (options) {
|
14 | return factory.create(options);
|
15 | };
|
16 |
|
17 | module.exports.connect = module.exports.create;
|
18 |
|
19 | module.exports.destroy = function (socket) {
|
20 | return factory.destroy(socket);
|
21 | };
|
22 |
|
23 | module.exports.clients = factory.clients;
|
24 |
|
25 | module.exports.version = '14.1.0';
|
26 |
|
27 | },{"./lib/factory":3,"./lib/scclientsocket":5,"component-emitter":12}],2:[function(_dereq_,module,exports){
|
28 | (function (global){
|
29 | var AuthEngine = function () {
|
30 | this._internalStorage = {};
|
31 | this.isLocalStorageEnabled = this._checkLocalStorageEnabled();
|
32 | };
|
33 |
|
34 | AuthEngine.prototype._checkLocalStorageEnabled = function () {
|
35 | var err;
|
36 | try {
|
37 |
|
38 | global.localStorage;
|
39 |
|
40 |
|
41 |
|
42 | global.localStorage.setItem('__scLocalStorageTest', 1);
|
43 | global.localStorage.removeItem('__scLocalStorageTest');
|
44 | } catch (e) {
|
45 | err = e;
|
46 | }
|
47 | return !err;
|
48 | };
|
49 |
|
50 | AuthEngine.prototype.saveToken = function (name, token, options, callback) {
|
51 | if (this.isLocalStorageEnabled && global.localStorage) {
|
52 | global.localStorage.setItem(name, token);
|
53 | } else {
|
54 | this._internalStorage[name] = token;
|
55 | }
|
56 | callback && callback(null, token);
|
57 | };
|
58 |
|
59 | AuthEngine.prototype.removeToken = function (name, callback) {
|
60 | var token;
|
61 |
|
62 | this.loadToken(name, function (err, authToken) {
|
63 | token = authToken;
|
64 | });
|
65 |
|
66 | if (this.isLocalStorageEnabled && global.localStorage) {
|
67 | global.localStorage.removeItem(name);
|
68 | } else {
|
69 | delete this._internalStorage[name];
|
70 | }
|
71 |
|
72 | callback && callback(null, token);
|
73 | };
|
74 |
|
75 | AuthEngine.prototype.loadToken = function (name, callback) {
|
76 | var token;
|
77 |
|
78 | if (this.isLocalStorageEnabled && global.localStorage) {
|
79 | token = global.localStorage.getItem(name);
|
80 | } else {
|
81 | token = this._internalStorage[name] || null;
|
82 | }
|
83 | callback(null, token);
|
84 | };
|
85 |
|
86 | module.exports.AuthEngine = AuthEngine;
|
87 |
|
88 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
89 | },{}],3:[function(_dereq_,module,exports){
|
90 | (function (global){
|
91 | var SCClientSocket = _dereq_('./scclientsocket');
|
92 | var scErrors = _dereq_('sc-errors');
|
93 | var uuid = _dereq_('uuid');
|
94 | var InvalidArgumentsError = scErrors.InvalidArgumentsError;
|
95 |
|
96 | var _clients = {};
|
97 |
|
98 | function getMultiplexId(options) {
|
99 | var protocolPrefix = options.secure ? 'https://' : 'http://';
|
100 | var queryString = '';
|
101 | if (options.query) {
|
102 | if (typeof options.query == 'string') {
|
103 | queryString = options.query;
|
104 | } else {
|
105 | var queryArray = [];
|
106 | var queryMap = options.query;
|
107 | for (var key in queryMap) {
|
108 | if (queryMap.hasOwnProperty(key)) {
|
109 | queryArray.push(key + '=' + queryMap[key]);
|
110 | }
|
111 | }
|
112 | if (queryArray.length) {
|
113 | queryString = '?' + queryArray.join('&');
|
114 | }
|
115 | }
|
116 | }
|
117 | var host;
|
118 | if (options.host) {
|
119 | host = options.host;
|
120 | } else {
|
121 | host = options.hostname + ':' + options.port;
|
122 | }
|
123 | return protocolPrefix + host + options.path + queryString;
|
124 | }
|
125 |
|
126 | function isUrlSecure() {
|
127 | return global.location && location.protocol == 'https:';
|
128 | }
|
129 |
|
130 | function getPort(options, isSecureDefault) {
|
131 | var isSecure = options.secure == null ? isSecureDefault : options.secure;
|
132 | return options.port || (global.location && location.port ? location.port : isSecure ? 443 : 80);
|
133 | }
|
134 |
|
135 | function create(options) {
|
136 | var self = this;
|
137 |
|
138 | options = options || {};
|
139 |
|
140 | if (options.host && !options.host.match(/[^:]+:\d{2,5}/)) {
|
141 | throw new InvalidArgumentsError('The host option should include both' +
|
142 | ' the hostname and the port number in the format "hostname:port"');
|
143 | }
|
144 |
|
145 | if (options.host && options.hostname) {
|
146 | throw new InvalidArgumentsError('The host option should already include' +
|
147 | ' the hostname and the port number in the format "hostname:port"' +
|
148 | ' - Because of this, you should never use host and hostname options together');
|
149 | }
|
150 |
|
151 | if (options.host && options.port) {
|
152 | throw new InvalidArgumentsError('The host option should already include' +
|
153 | ' the hostname and the port number in the format "hostname:port"' +
|
154 | ' - Because of this, you should never use host and port options together');
|
155 | }
|
156 |
|
157 | var isSecureDefault = isUrlSecure();
|
158 |
|
159 | var opts = {
|
160 | port: getPort(options, isSecureDefault),
|
161 | hostname: global.location && location.hostname || 'localhost',
|
162 | path: '/socketcluster/',
|
163 | secure: isSecureDefault,
|
164 | autoConnect: true,
|
165 | autoReconnect: true,
|
166 | autoSubscribeOnConnect: true,
|
167 | connectTimeout: 20000,
|
168 | ackTimeout: 10000,
|
169 | timestampRequests: false,
|
170 | timestampParam: 't',
|
171 | authEngine: null,
|
172 | authTokenName: 'socketCluster.authToken',
|
173 | binaryType: 'arraybuffer',
|
174 | multiplex: true,
|
175 | pubSubBatchDuration: null,
|
176 | cloneData: false
|
177 | };
|
178 | for (var i in options) {
|
179 | if (options.hasOwnProperty(i)) {
|
180 | opts[i] = options[i];
|
181 | }
|
182 | }
|
183 | opts.clientMap = _clients;
|
184 |
|
185 | if (opts.multiplex === false) {
|
186 | opts.clientId = uuid.v4();
|
187 | var socket = new SCClientSocket(opts);
|
188 | _clients[opts.clientId] = socket;
|
189 | return socket;
|
190 | }
|
191 | opts.clientId = getMultiplexId(opts);
|
192 |
|
193 | if (_clients[opts.clientId]) {
|
194 | if (opts.autoConnect) {
|
195 | _clients[opts.clientId].connect();
|
196 | }
|
197 | } else {
|
198 | _clients[opts.clientId] = new SCClientSocket(opts);
|
199 | }
|
200 | return _clients[opts.clientId];
|
201 | }
|
202 |
|
203 | function destroy(socket) {
|
204 | socket.destroy();
|
205 | }
|
206 |
|
207 | module.exports = {
|
208 | create: create,
|
209 | destroy: destroy,
|
210 | clients: _clients
|
211 | };
|
212 |
|
213 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
214 | },{"./scclientsocket":5,"sc-errors":21,"uuid":23}],4:[function(_dereq_,module,exports){
|
215 | var scErrors = _dereq_('sc-errors');
|
216 | var InvalidActionError = scErrors.InvalidActionError;
|
217 |
|
218 | var Response = function (socket, id) {
|
219 | this.socket = socket;
|
220 | this.id = id;
|
221 | this.sent = false;
|
222 | };
|
223 |
|
224 | Response.prototype._respond = function (responseData) {
|
225 | if (this.sent) {
|
226 | throw new InvalidActionError('Response ' + this.id + ' has already been sent');
|
227 | } else {
|
228 | this.sent = true;
|
229 | this.socket.send(this.socket.encode(responseData));
|
230 | }
|
231 | };
|
232 |
|
233 | Response.prototype.end = function (data) {
|
234 | if (this.id) {
|
235 | var responseData = {
|
236 | rid: this.id
|
237 | };
|
238 | if (data !== undefined) {
|
239 | responseData.data = data;
|
240 | }
|
241 | this._respond(responseData);
|
242 | }
|
243 | };
|
244 |
|
245 | Response.prototype.error = function (error, data) {
|
246 | if (this.id) {
|
247 | var err = scErrors.dehydrateError(error);
|
248 |
|
249 | var responseData = {
|
250 | rid: this.id,
|
251 | error: err
|
252 | };
|
253 | if (data !== undefined) {
|
254 | responseData.data = data;
|
255 | }
|
256 |
|
257 | this._respond(responseData);
|
258 | }
|
259 | };
|
260 |
|
261 | Response.prototype.callback = function (error, data) {
|
262 | if (error) {
|
263 | this.error(error, data);
|
264 | } else {
|
265 | this.end(data);
|
266 | }
|
267 | };
|
268 |
|
269 | module.exports.Response = Response;
|
270 |
|
271 | },{"sc-errors":21}],5:[function(_dereq_,module,exports){
|
272 | (function (global,Buffer){
|
273 | var Emitter = _dereq_('component-emitter');
|
274 | var SCChannel = _dereq_('sc-channel').SCChannel;
|
275 | var Response = _dereq_('./response').Response;
|
276 | var AuthEngine = _dereq_('./auth').AuthEngine;
|
277 | var formatter = _dereq_('sc-formatter');
|
278 | var SCTransport = _dereq_('./sctransport').SCTransport;
|
279 | var querystring = _dereq_('querystring');
|
280 | var LinkedList = _dereq_('linked-list');
|
281 | var base64 = _dereq_('base-64');
|
282 | var clone = _dereq_('clone');
|
283 |
|
284 | var scErrors = _dereq_('sc-errors');
|
285 | var InvalidArgumentsError = scErrors.InvalidArgumentsError;
|
286 | var InvalidMessageError = scErrors.InvalidMessageError;
|
287 | var InvalidActionError = scErrors.InvalidActionError;
|
288 | var SocketProtocolError = scErrors.SocketProtocolError;
|
289 | var TimeoutError = scErrors.TimeoutError;
|
290 | var BadConnectionError = scErrors.BadConnectionError;
|
291 |
|
292 | var isBrowser = typeof window != 'undefined';
|
293 |
|
294 |
|
295 | var SCClientSocket = function (opts) {
|
296 | var self = this;
|
297 |
|
298 | Emitter.call(this);
|
299 |
|
300 | this.id = null;
|
301 | this.state = this.CLOSED;
|
302 | this.authState = this.UNAUTHENTICATED;
|
303 | this.signedAuthToken = null;
|
304 | this.authToken = null;
|
305 | this.pendingReconnect = false;
|
306 | this.pendingReconnectTimeout = null;
|
307 | this.preparingPendingSubscriptions = false;
|
308 | this.clientId = opts.clientId;
|
309 |
|
310 | this.connectTimeout = opts.connectTimeout;
|
311 | this.ackTimeout = opts.ackTimeout;
|
312 | this.channelPrefix = opts.channelPrefix || null;
|
313 | this.disconnectOnUnload = opts.disconnectOnUnload == null ? true : opts.disconnectOnUnload;
|
314 | this.authTokenName = opts.authTokenName;
|
315 |
|
316 |
|
317 |
|
318 | this.pingTimeout = this.ackTimeout;
|
319 | this.pingTimeoutDisabled = !!opts.pingTimeoutDisabled;
|
320 | this.active = true;
|
321 |
|
322 | this._clientMap = opts.clientMap || {};
|
323 |
|
324 | var maxTimeout = Math.pow(2, 31) - 1;
|
325 |
|
326 | var verifyDuration = function (propertyName) {
|
327 | if (self[propertyName] > maxTimeout) {
|
328 | throw new InvalidArgumentsError('The ' + propertyName +
|
329 | ' value provided exceeded the maximum amount allowed');
|
330 | }
|
331 | };
|
332 |
|
333 | verifyDuration('connectTimeout');
|
334 | verifyDuration('ackTimeout');
|
335 |
|
336 | this._localEvents = {
|
337 | 'connect': 1,
|
338 | 'connectAbort': 1,
|
339 | 'close': 1,
|
340 | 'disconnect': 1,
|
341 | 'message': 1,
|
342 | 'error': 1,
|
343 | 'raw': 1,
|
344 | 'kickOut': 1,
|
345 | 'subscribe': 1,
|
346 | 'unsubscribe': 1,
|
347 | 'subscribeStateChange': 1,
|
348 | 'authStateChange': 1,
|
349 | 'authenticate': 1,
|
350 | 'deauthenticate': 1,
|
351 | 'removeAuthToken': 1,
|
352 | 'subscribeRequest': 1
|
353 | };
|
354 |
|
355 | this.connectAttempts = 0;
|
356 |
|
357 | this._emitBuffer = new LinkedList();
|
358 | this.channels = {};
|
359 |
|
360 | this.options = opts;
|
361 |
|
362 | this._cid = 1;
|
363 |
|
364 | this.options.callIdGenerator = function () {
|
365 | return self._cid++;
|
366 | };
|
367 |
|
368 | if (this.options.autoReconnect) {
|
369 | if (this.options.autoReconnectOptions == null) {
|
370 | this.options.autoReconnectOptions = {};
|
371 | }
|
372 |
|
373 |
|
374 |
|
375 | var reconnectOptions = this.options.autoReconnectOptions;
|
376 | if (reconnectOptions.initialDelay == null) {
|
377 | reconnectOptions.initialDelay = 10000;
|
378 | }
|
379 | if (reconnectOptions.randomness == null) {
|
380 | reconnectOptions.randomness = 10000;
|
381 | }
|
382 | if (reconnectOptions.multiplier == null) {
|
383 | reconnectOptions.multiplier = 1.5;
|
384 | }
|
385 | if (reconnectOptions.maxDelay == null) {
|
386 | reconnectOptions.maxDelay = 60000;
|
387 | }
|
388 | }
|
389 |
|
390 | if (this.options.subscriptionRetryOptions == null) {
|
391 | this.options.subscriptionRetryOptions = {};
|
392 | }
|
393 |
|
394 | if (this.options.authEngine) {
|
395 | this.auth = this.options.authEngine;
|
396 | } else {
|
397 | this.auth = new AuthEngine();
|
398 | }
|
399 |
|
400 | if (this.options.codecEngine) {
|
401 | this.codec = this.options.codecEngine;
|
402 | } else {
|
403 |
|
404 | this.codec = formatter;
|
405 | }
|
406 |
|
407 | if (this.options.protocol) {
|
408 | var protocolOptionError = new InvalidArgumentsError('The "protocol" option' +
|
409 | ' does not affect socketcluster-client. If you want to utilize SSL/TLS' +
|
410 | ' - use "secure" option instead');
|
411 | this._onSCError(protocolOptionError);
|
412 | }
|
413 |
|
414 | this.options.path = this.options.path.replace(/\/$/, '') + '/';
|
415 |
|
416 | this.options.query = opts.query || {};
|
417 | if (typeof this.options.query == 'string') {
|
418 | this.options.query = querystring.parse(this.options.query);
|
419 | }
|
420 |
|
421 | this._channelEmitter = new Emitter();
|
422 |
|
423 | this._unloadHandler = function () {
|
424 | self.disconnect();
|
425 | };
|
426 |
|
427 | if (isBrowser && this.disconnectOnUnload && global.addEventListener) {
|
428 | global.addEventListener('beforeunload', this._unloadHandler, false);
|
429 | }
|
430 | this._clientMap[this.clientId] = this;
|
431 |
|
432 | if (this.options.autoConnect) {
|
433 | this.connect();
|
434 | }
|
435 | };
|
436 |
|
437 | SCClientSocket.prototype = Object.create(Emitter.prototype);
|
438 |
|
439 | SCClientSocket.CONNECTING = SCClientSocket.prototype.CONNECTING = SCTransport.prototype.CONNECTING;
|
440 | SCClientSocket.OPEN = SCClientSocket.prototype.OPEN = SCTransport.prototype.OPEN;
|
441 | SCClientSocket.CLOSED = SCClientSocket.prototype.CLOSED = SCTransport.prototype.CLOSED;
|
442 |
|
443 | SCClientSocket.AUTHENTICATED = SCClientSocket.prototype.AUTHENTICATED = 'authenticated';
|
444 | SCClientSocket.UNAUTHENTICATED = SCClientSocket.prototype.UNAUTHENTICATED = 'unauthenticated';
|
445 |
|
446 | SCClientSocket.PENDING = SCClientSocket.prototype.PENDING = 'pending';
|
447 |
|
448 | SCClientSocket.ignoreStatuses = scErrors.socketProtocolIgnoreStatuses;
|
449 | SCClientSocket.errorStatuses = scErrors.socketProtocolErrorStatuses;
|
450 |
|
451 | SCClientSocket.prototype._privateEventHandlerMap = {
|
452 | '#publish': function (data) {
|
453 | var undecoratedChannelName = this._undecorateChannelName(data.channel);
|
454 | var isSubscribed = this.isSubscribed(undecoratedChannelName, true);
|
455 |
|
456 | if (isSubscribed) {
|
457 | this._channelEmitter.emit(undecoratedChannelName, data.data);
|
458 | }
|
459 | },
|
460 | '#kickOut': function (data) {
|
461 | var undecoratedChannelName = this._undecorateChannelName(data.channel);
|
462 | var channel = this.channels[undecoratedChannelName];
|
463 | if (channel) {
|
464 | Emitter.prototype.emit.call(this, 'kickOut', data.message, undecoratedChannelName);
|
465 | channel.emit('kickOut', data.message, undecoratedChannelName);
|
466 | this._triggerChannelUnsubscribe(channel);
|
467 | }
|
468 | },
|
469 | '#setAuthToken': function (data, response) {
|
470 | var self = this;
|
471 |
|
472 | if (data) {
|
473 | var triggerAuthenticate = function (err) {
|
474 | if (err) {
|
475 |
|
476 |
|
477 |
|
478 | response.error(err);
|
479 | self._onSCError(err);
|
480 | } else {
|
481 | self._changeToAuthenticatedState(data.token);
|
482 | response.end();
|
483 | }
|
484 | };
|
485 |
|
486 | this.auth.saveToken(this.authTokenName, data.token, {}, triggerAuthenticate);
|
487 | } else {
|
488 | response.error(new InvalidMessageError('No token data provided by #setAuthToken event'));
|
489 | }
|
490 | },
|
491 | '#removeAuthToken': function (data, response) {
|
492 | var self = this;
|
493 |
|
494 | this.auth.removeToken(this.authTokenName, function (err, oldToken) {
|
495 | if (err) {
|
496 |
|
497 | response.error(err);
|
498 | self._onSCError(err);
|
499 | } else {
|
500 | Emitter.prototype.emit.call(self, 'removeAuthToken', oldToken);
|
501 | self._changeToUnauthenticatedStateAndClearTokens();
|
502 | response.end();
|
503 | }
|
504 | });
|
505 | },
|
506 | '#disconnect': function (data) {
|
507 | this.transport.close(data.code, data.data);
|
508 | }
|
509 | };
|
510 |
|
511 | SCClientSocket.prototype.getState = function () {
|
512 | return this.state;
|
513 | };
|
514 |
|
515 | SCClientSocket.prototype.getBytesReceived = function () {
|
516 | return this.transport.getBytesReceived();
|
517 | };
|
518 |
|
519 | SCClientSocket.prototype.deauthenticate = function (callback) {
|
520 | var self = this;
|
521 |
|
522 | this.auth.removeToken(this.authTokenName, function (err, oldToken) {
|
523 | if (err) {
|
524 |
|
525 | self._onSCError(err);
|
526 | } else {
|
527 | Emitter.prototype.emit.call(self, 'removeAuthToken', oldToken);
|
528 | if (self.state != self.CLOSED) {
|
529 | self.emit('#removeAuthToken');
|
530 | }
|
531 | self._changeToUnauthenticatedStateAndClearTokens();
|
532 | }
|
533 | callback && callback(err);
|
534 | });
|
535 | };
|
536 |
|
537 | SCClientSocket.prototype.connect = SCClientSocket.prototype.open = function () {
|
538 | var self = this;
|
539 |
|
540 | if (!this.active) {
|
541 | var error = new InvalidActionError('Cannot connect a destroyed client');
|
542 | this._onSCError(error);
|
543 | return;
|
544 | }
|
545 |
|
546 | if (this.state == this.CLOSED) {
|
547 | this.pendingReconnect = false;
|
548 | this.pendingReconnectTimeout = null;
|
549 | clearTimeout(this._reconnectTimeoutRef);
|
550 |
|
551 | this.state = this.CONNECTING;
|
552 | Emitter.prototype.emit.call(this, 'connecting');
|
553 |
|
554 | if (this.transport) {
|
555 | this.transport.off();
|
556 | }
|
557 |
|
558 | this.transport = new SCTransport(this.auth, this.codec, this.options);
|
559 |
|
560 | this.transport.on('open', function (status) {
|
561 | self.state = self.OPEN;
|
562 | self._onSCOpen(status);
|
563 | });
|
564 |
|
565 | this.transport.on('error', function (err) {
|
566 | self._onSCError(err);
|
567 | });
|
568 |
|
569 | this.transport.on('close', function (code, data) {
|
570 | self.state = self.CLOSED;
|
571 | self._onSCClose(code, data);
|
572 | });
|
573 |
|
574 | this.transport.on('openAbort', function (code, data) {
|
575 | self.state = self.CLOSED;
|
576 | self._onSCClose(code, data, true);
|
577 | });
|
578 |
|
579 | this.transport.on('event', function (event, data, res) {
|
580 | self._onSCEvent(event, data, res);
|
581 | });
|
582 | }
|
583 | };
|
584 |
|
585 | SCClientSocket.prototype.reconnect = function (code, data) {
|
586 | this.disconnect(code, data);
|
587 | this.connect();
|
588 | };
|
589 |
|
590 | SCClientSocket.prototype.disconnect = function (code, data) {
|
591 | code = code || 1000;
|
592 |
|
593 | if (typeof code != 'number') {
|
594 | throw new InvalidArgumentsError('If specified, the code argument must be a number');
|
595 | }
|
596 |
|
597 | if (this.state == this.OPEN || this.state == this.CONNECTING) {
|
598 | this.transport.close(code, data);
|
599 | } else {
|
600 | this.pendingReconnect = false;
|
601 | this.pendingReconnectTimeout = null;
|
602 | clearTimeout(this._reconnectTimeoutRef);
|
603 | }
|
604 | };
|
605 |
|
606 | SCClientSocket.prototype.destroy = function (code, data) {
|
607 | if (isBrowser && global.removeEventListener) {
|
608 | global.removeEventListener('beforeunload', this._unloadHandler, false);
|
609 | }
|
610 | this.active = false;
|
611 | this.disconnect(code, data);
|
612 | delete this._clientMap[this.clientId];
|
613 | };
|
614 |
|
615 | SCClientSocket.prototype._changeToUnauthenticatedStateAndClearTokens = function () {
|
616 | if (this.authState != this.UNAUTHENTICATED) {
|
617 | var oldState = this.authState;
|
618 | var oldSignedToken = this.signedAuthToken;
|
619 | this.authState = this.UNAUTHENTICATED;
|
620 | this.signedAuthToken = null;
|
621 | this.authToken = null;
|
622 |
|
623 | var stateChangeData = {
|
624 | oldState: oldState,
|
625 | newState: this.authState
|
626 | };
|
627 | Emitter.prototype.emit.call(this, 'authStateChange', stateChangeData);
|
628 | Emitter.prototype.emit.call(this, 'deauthenticate', oldSignedToken);
|
629 | }
|
630 | };
|
631 |
|
632 | SCClientSocket.prototype._changeToAuthenticatedState = function (signedAuthToken) {
|
633 | this.signedAuthToken = signedAuthToken;
|
634 | this.authToken = this._extractAuthTokenData(signedAuthToken);
|
635 |
|
636 | if (this.authState != this.AUTHENTICATED) {
|
637 | var oldState = this.authState;
|
638 | this.authState = this.AUTHENTICATED;
|
639 | var stateChangeData = {
|
640 | oldState: oldState,
|
641 | newState: this.authState,
|
642 | signedAuthToken: signedAuthToken,
|
643 | authToken: this.authToken
|
644 | };
|
645 | if (!this.preparingPendingSubscriptions) {
|
646 | this.processPendingSubscriptions();
|
647 | }
|
648 |
|
649 | Emitter.prototype.emit.call(this, 'authStateChange', stateChangeData);
|
650 | }
|
651 | Emitter.prototype.emit.call(this, 'authenticate', signedAuthToken);
|
652 | };
|
653 |
|
654 | SCClientSocket.prototype.decodeBase64 = function (encodedString) {
|
655 | var decodedString;
|
656 | if (typeof Buffer == 'undefined') {
|
657 | if (global.atob) {
|
658 | decodedString = global.atob(encodedString);
|
659 | } else {
|
660 | decodedString = base64.decode(encodedString);
|
661 | }
|
662 | } else {
|
663 | var buffer = Buffer.from(encodedString, 'base64');
|
664 | decodedString = buffer.toString('utf8');
|
665 | }
|
666 | return decodedString;
|
667 | };
|
668 |
|
669 | SCClientSocket.prototype.encodeBase64 = function (decodedString) {
|
670 | var encodedString;
|
671 | if (typeof Buffer == 'undefined') {
|
672 | if (global.btoa) {
|
673 | encodedString = global.btoa(decodedString);
|
674 | } else {
|
675 | encodedString = base64.encode(decodedString);
|
676 | }
|
677 | } else {
|
678 | var buffer = Buffer.from(decodedString, 'utf8');
|
679 | encodedString = buffer.toString('base64');
|
680 | }
|
681 | return encodedString;
|
682 | };
|
683 |
|
684 | SCClientSocket.prototype._extractAuthTokenData = function (signedAuthToken) {
|
685 | var tokenParts = (signedAuthToken || '').split('.');
|
686 | var encodedTokenData = tokenParts[1];
|
687 | if (encodedTokenData != null) {
|
688 | var tokenData = encodedTokenData;
|
689 | try {
|
690 | tokenData = this.decodeBase64(tokenData);
|
691 | return JSON.parse(tokenData);
|
692 | } catch (e) {
|
693 | return tokenData;
|
694 | }
|
695 | }
|
696 | return null;
|
697 | };
|
698 |
|
699 | SCClientSocket.prototype.getAuthToken = function () {
|
700 | return this.authToken;
|
701 | };
|
702 |
|
703 | SCClientSocket.prototype.getSignedAuthToken = function () {
|
704 | return this.signedAuthToken;
|
705 | };
|
706 |
|
707 |
|
708 | SCClientSocket.prototype.authenticate = function (signedAuthToken, callback) {
|
709 | var self = this;
|
710 |
|
711 | this.emit('#authenticate', signedAuthToken, function (err, authStatus) {
|
712 | if (authStatus && authStatus.isAuthenticated != null) {
|
713 |
|
714 |
|
715 | if (authStatus.authError) {
|
716 | authStatus.authError = scErrors.hydrateError(authStatus.authError);
|
717 | }
|
718 | } else {
|
719 |
|
720 |
|
721 | authStatus = {
|
722 | isAuthenticated: self.authState,
|
723 | authError: null
|
724 | };
|
725 | }
|
726 | if (err) {
|
727 | if (err.name != 'BadConnectionError' && err.name != 'TimeoutError') {
|
728 |
|
729 |
|
730 |
|
731 | self._changeToUnauthenticatedStateAndClearTokens();
|
732 | }
|
733 | callback && callback(err, authStatus);
|
734 | } else {
|
735 | self.auth.saveToken(self.authTokenName, signedAuthToken, {}, function (err) {
|
736 | if (err) {
|
737 | self._onSCError(err);
|
738 | }
|
739 | if (authStatus.isAuthenticated) {
|
740 | self._changeToAuthenticatedState(signedAuthToken);
|
741 | } else {
|
742 | self._changeToUnauthenticatedStateAndClearTokens();
|
743 | }
|
744 | callback && callback(err, authStatus);
|
745 | });
|
746 | }
|
747 | });
|
748 | };
|
749 |
|
750 | SCClientSocket.prototype._tryReconnect = function (initialDelay) {
|
751 | var self = this;
|
752 |
|
753 | var exponent = this.connectAttempts++;
|
754 | var reconnectOptions = this.options.autoReconnectOptions;
|
755 | var timeout;
|
756 |
|
757 | if (initialDelay == null || exponent > 0) {
|
758 | var initialTimeout = Math.round(reconnectOptions.initialDelay + (reconnectOptions.randomness || 0) * Math.random());
|
759 |
|
760 | timeout = Math.round(initialTimeout * Math.pow(reconnectOptions.multiplier, exponent));
|
761 | } else {
|
762 | timeout = initialDelay;
|
763 | }
|
764 |
|
765 | if (timeout > reconnectOptions.maxDelay) {
|
766 | timeout = reconnectOptions.maxDelay;
|
767 | }
|
768 |
|
769 | clearTimeout(this._reconnectTimeoutRef);
|
770 |
|
771 | this.pendingReconnect = true;
|
772 | this.pendingReconnectTimeout = timeout;
|
773 | this._reconnectTimeoutRef = setTimeout(function () {
|
774 | self.connect();
|
775 | }, timeout);
|
776 | };
|
777 |
|
778 | SCClientSocket.prototype._onSCOpen = function (status) {
|
779 | var self = this;
|
780 |
|
781 | this.preparingPendingSubscriptions = true;
|
782 |
|
783 | if (status) {
|
784 | this.id = status.id;
|
785 | this.pingTimeout = status.pingTimeout;
|
786 | this.transport.pingTimeout = this.pingTimeout;
|
787 | if (status.isAuthenticated) {
|
788 | this._changeToAuthenticatedState(status.authToken);
|
789 | } else {
|
790 | this._changeToUnauthenticatedStateAndClearTokens();
|
791 | }
|
792 | } else {
|
793 |
|
794 |
|
795 |
|
796 | this._changeToUnauthenticatedStateAndClearTokens();
|
797 | }
|
798 |
|
799 | this.connectAttempts = 0;
|
800 |
|
801 | if (this.options.autoSubscribeOnConnect) {
|
802 | this.processPendingSubscriptions();
|
803 | }
|
804 |
|
805 |
|
806 |
|
807 | Emitter.prototype.emit.call(this, 'connect', status, function () {
|
808 | self.processPendingSubscriptions();
|
809 | });
|
810 |
|
811 | if (this.state == this.OPEN) {
|
812 | this._flushEmitBuffer();
|
813 | }
|
814 | };
|
815 |
|
816 | SCClientSocket.prototype._onSCError = function (err) {
|
817 | var self = this;
|
818 |
|
819 |
|
820 |
|
821 | setTimeout(function () {
|
822 | if (self.listeners('error').length < 1) {
|
823 | throw err;
|
824 | } else {
|
825 | Emitter.prototype.emit.call(self, 'error', err);
|
826 | }
|
827 | }, 0);
|
828 | };
|
829 |
|
830 | SCClientSocket.prototype._suspendSubscriptions = function () {
|
831 | var channel, newState;
|
832 | for (var channelName in this.channels) {
|
833 | if (this.channels.hasOwnProperty(channelName)) {
|
834 | channel = this.channels[channelName];
|
835 | if (channel.state == channel.SUBSCRIBED ||
|
836 | channel.state == channel.PENDING) {
|
837 |
|
838 | newState = channel.PENDING;
|
839 | } else {
|
840 | newState = channel.UNSUBSCRIBED;
|
841 | }
|
842 |
|
843 | this._triggerChannelUnsubscribe(channel, newState);
|
844 | }
|
845 | }
|
846 | };
|
847 |
|
848 | SCClientSocket.prototype._abortAllPendingEventsDueToBadConnection = function (failureType) {
|
849 | var currentNode = this._emitBuffer.head;
|
850 | var nextNode;
|
851 |
|
852 | while (currentNode) {
|
853 | nextNode = currentNode.next;
|
854 | var eventObject = currentNode.data;
|
855 | clearTimeout(eventObject.timeout);
|
856 | delete eventObject.timeout;
|
857 | currentNode.detach();
|
858 | currentNode = nextNode;
|
859 |
|
860 | var callback = eventObject.callback;
|
861 | if (callback) {
|
862 | delete eventObject.callback;
|
863 | var errorMessage = "Event '" + eventObject.event +
|
864 | "' was aborted due to a bad connection";
|
865 | var error = new BadConnectionError(errorMessage, failureType);
|
866 | callback.call(eventObject, error, eventObject);
|
867 | }
|
868 |
|
869 | if (eventObject.cid) {
|
870 | this.transport.cancelPendingResponse(eventObject.cid);
|
871 | }
|
872 | }
|
873 | };
|
874 |
|
875 | SCClientSocket.prototype._onSCClose = function (code, data, openAbort) {
|
876 | var self = this;
|
877 |
|
878 | this.id = null;
|
879 |
|
880 | if (this.transport) {
|
881 | this.transport.off();
|
882 | }
|
883 | this.pendingReconnect = false;
|
884 | this.pendingReconnectTimeout = null;
|
885 | clearTimeout(this._reconnectTimeoutRef);
|
886 |
|
887 | this._suspendSubscriptions();
|
888 | this._abortAllPendingEventsDueToBadConnection(openAbort ? 'connectAbort' : 'disconnect');
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 | if (this.options.autoReconnect) {
|
898 | if (code == 4000 || code == 4001 || code == 1005) {
|
899 |
|
900 |
|
901 |
|
902 |
|
903 | this._tryReconnect(0);
|
904 |
|
905 |
|
906 |
|
907 | } else if (code != 1000 && code < 4500) {
|
908 | this._tryReconnect();
|
909 | }
|
910 | }
|
911 |
|
912 | if (openAbort) {
|
913 | Emitter.prototype.emit.call(self, 'connectAbort', code, data);
|
914 | } else {
|
915 | Emitter.prototype.emit.call(self, 'disconnect', code, data);
|
916 | }
|
917 | Emitter.prototype.emit.call(self, 'close', code, data);
|
918 |
|
919 | if (!SCClientSocket.ignoreStatuses[code]) {
|
920 | var closeMessage;
|
921 | if (data) {
|
922 | closeMessage = 'Socket connection closed with status code ' + code + ' and reason: ' + data;
|
923 | } else {
|
924 | closeMessage = 'Socket connection closed with status code ' + code;
|
925 | }
|
926 | var err = new SocketProtocolError(SCClientSocket.errorStatuses[code] || closeMessage, code);
|
927 | this._onSCError(err);
|
928 | }
|
929 | };
|
930 |
|
931 | SCClientSocket.prototype._onSCEvent = function (event, data, res) {
|
932 | var handler = this._privateEventHandlerMap[event];
|
933 | if (handler) {
|
934 | handler.call(this, data, res);
|
935 | } else {
|
936 | Emitter.prototype.emit.call(this, event, data, function () {
|
937 | res && res.callback.apply(res, arguments);
|
938 | });
|
939 | }
|
940 | };
|
941 |
|
942 | SCClientSocket.prototype.decode = function (message) {
|
943 | return this.transport.decode(message);
|
944 | };
|
945 |
|
946 | SCClientSocket.prototype.encode = function (object) {
|
947 | return this.transport.encode(object);
|
948 | };
|
949 |
|
950 | SCClientSocket.prototype._flushEmitBuffer = function () {
|
951 | var currentNode = this._emitBuffer.head;
|
952 | var nextNode;
|
953 |
|
954 | while (currentNode) {
|
955 | nextNode = currentNode.next;
|
956 | var eventObject = currentNode.data;
|
957 | currentNode.detach();
|
958 | this.transport.emitObject(eventObject);
|
959 | currentNode = nextNode;
|
960 | }
|
961 | };
|
962 |
|
963 | SCClientSocket.prototype._handleEventAckTimeout = function (eventObject, eventNode) {
|
964 | if (eventNode) {
|
965 | eventNode.detach();
|
966 | }
|
967 | delete eventObject.timeout;
|
968 |
|
969 | var callback = eventObject.callback;
|
970 | if (callback) {
|
971 | delete eventObject.callback;
|
972 | var error = new TimeoutError("Event response for '" + eventObject.event + "' timed out");
|
973 | callback.call(eventObject, error, eventObject);
|
974 | }
|
975 |
|
976 | if (eventObject.cid) {
|
977 | this.transport.cancelPendingResponse(eventObject.cid);
|
978 | }
|
979 | };
|
980 |
|
981 | SCClientSocket.prototype._emit = function (event, data, callback) {
|
982 | var self = this;
|
983 |
|
984 | if (this.state == this.CLOSED) {
|
985 | this.connect();
|
986 | }
|
987 | var eventObject = {
|
988 | event: event,
|
989 | callback: callback
|
990 | };
|
991 |
|
992 | var eventNode = new LinkedList.Item();
|
993 |
|
994 | if (this.options.cloneData) {
|
995 | eventObject.data = clone(data);
|
996 | } else {
|
997 | eventObject.data = data;
|
998 | }
|
999 | eventNode.data = eventObject;
|
1000 |
|
1001 | eventObject.timeout = setTimeout(function () {
|
1002 | self._handleEventAckTimeout(eventObject, eventNode);
|
1003 | }, this.ackTimeout);
|
1004 |
|
1005 | this._emitBuffer.append(eventNode);
|
1006 | if (this.state == this.OPEN) {
|
1007 | this._flushEmitBuffer();
|
1008 | }
|
1009 | };
|
1010 |
|
1011 | SCClientSocket.prototype.send = function (data) {
|
1012 | this.transport.send(data);
|
1013 | };
|
1014 |
|
1015 | SCClientSocket.prototype.emit = function (event, data, callback) {
|
1016 | if (this._localEvents[event] == null) {
|
1017 | this._emit(event, data, callback);
|
1018 | } else if (event == 'error') {
|
1019 | Emitter.prototype.emit.call(this, event, data);
|
1020 | } else {
|
1021 | var error = new InvalidActionError('The "' + event + '" event is reserved and cannot be emitted on a client socket');
|
1022 | this._onSCError(error);
|
1023 | }
|
1024 | };
|
1025 |
|
1026 | SCClientSocket.prototype.publish = function (channelName, data, callback) {
|
1027 | var pubData = {
|
1028 | channel: this._decorateChannelName(channelName),
|
1029 | data: data
|
1030 | };
|
1031 | this.emit('#publish', pubData, callback);
|
1032 | };
|
1033 |
|
1034 | SCClientSocket.prototype._triggerChannelSubscribe = function (channel, subscriptionOptions) {
|
1035 | var channelName = channel.name;
|
1036 |
|
1037 | if (channel.state != channel.SUBSCRIBED) {
|
1038 | var oldState = channel.state;
|
1039 | channel.state = channel.SUBSCRIBED;
|
1040 |
|
1041 | var stateChangeData = {
|
1042 | channel: channelName,
|
1043 | oldState: oldState,
|
1044 | newState: channel.state,
|
1045 | subscriptionOptions: subscriptionOptions
|
1046 | };
|
1047 | channel.emit('subscribeStateChange', stateChangeData);
|
1048 | channel.emit('subscribe', channelName, subscriptionOptions);
|
1049 | Emitter.prototype.emit.call(this, 'subscribeStateChange', stateChangeData);
|
1050 | Emitter.prototype.emit.call(this, 'subscribe', channelName, subscriptionOptions);
|
1051 | }
|
1052 | };
|
1053 |
|
1054 | SCClientSocket.prototype._triggerChannelSubscribeFail = function (err, channel, subscriptionOptions) {
|
1055 | var channelName = channel.name;
|
1056 | var meetsAuthRequirements = !channel.waitForAuth || this.authState == this.AUTHENTICATED;
|
1057 |
|
1058 | if (channel.state != channel.UNSUBSCRIBED && meetsAuthRequirements) {
|
1059 | channel.state = channel.UNSUBSCRIBED;
|
1060 |
|
1061 | channel.emit('subscribeFail', err, channelName, subscriptionOptions);
|
1062 | Emitter.prototype.emit.call(this, 'subscribeFail', err, channelName, subscriptionOptions);
|
1063 | }
|
1064 | };
|
1065 |
|
1066 |
|
1067 | SCClientSocket.prototype._cancelPendingSubscribeCallback = function (channel) {
|
1068 | if (channel._pendingSubscriptionCid != null) {
|
1069 | this.transport.cancelPendingResponse(channel._pendingSubscriptionCid);
|
1070 | delete channel._pendingSubscriptionCid;
|
1071 | }
|
1072 | };
|
1073 |
|
1074 | SCClientSocket.prototype._decorateChannelName = function (channelName) {
|
1075 | if (this.channelPrefix) {
|
1076 | channelName = this.channelPrefix + channelName;
|
1077 | }
|
1078 | return channelName;
|
1079 | };
|
1080 |
|
1081 | SCClientSocket.prototype._undecorateChannelName = function (decoratedChannelName) {
|
1082 | if (this.channelPrefix && decoratedChannelName.indexOf(this.channelPrefix) == 0) {
|
1083 | return decoratedChannelName.replace(this.channelPrefix, '');
|
1084 | }
|
1085 | return decoratedChannelName;
|
1086 | };
|
1087 |
|
1088 | SCClientSocket.prototype._trySubscribe = function (channel) {
|
1089 | var self = this;
|
1090 |
|
1091 | var meetsAuthRequirements = !channel.waitForAuth || this.authState == this.AUTHENTICATED;
|
1092 |
|
1093 |
|
1094 | if (this.state == this.OPEN && !this.preparingPendingSubscriptions &&
|
1095 | channel._pendingSubscriptionCid == null && meetsAuthRequirements) {
|
1096 |
|
1097 | var options = {
|
1098 | noTimeout: true
|
1099 | };
|
1100 |
|
1101 | var subscriptionOptions = {
|
1102 | channel: this._decorateChannelName(channel.name)
|
1103 | };
|
1104 | if (channel.waitForAuth) {
|
1105 | options.waitForAuth = true;
|
1106 | subscriptionOptions.waitForAuth = options.waitForAuth;
|
1107 | }
|
1108 | if (channel.data) {
|
1109 | subscriptionOptions.data = channel.data;
|
1110 | }
|
1111 | if (channel.batch) {
|
1112 | options.batch = true;
|
1113 | subscriptionOptions.batch = true;
|
1114 | }
|
1115 |
|
1116 | channel._pendingSubscriptionCid = this.transport.emit(
|
1117 | '#subscribe', subscriptionOptions, options,
|
1118 | function (err) {
|
1119 | delete channel._pendingSubscriptionCid;
|
1120 | if (err) {
|
1121 | self._triggerChannelSubscribeFail(err, channel, subscriptionOptions);
|
1122 | } else {
|
1123 | self._triggerChannelSubscribe(channel, subscriptionOptions);
|
1124 | }
|
1125 | }
|
1126 | );
|
1127 | Emitter.prototype.emit.call(this, 'subscribeRequest', channel.name, subscriptionOptions);
|
1128 | }
|
1129 | };
|
1130 |
|
1131 | SCClientSocket.prototype.subscribe = function (channelName, options) {
|
1132 | var channel = this.channels[channelName];
|
1133 |
|
1134 | if (!channel) {
|
1135 | channel = new SCChannel(channelName, this, options);
|
1136 | this.channels[channelName] = channel;
|
1137 | } else if (options) {
|
1138 | channel.setOptions(options);
|
1139 | }
|
1140 |
|
1141 | if (channel.state == channel.UNSUBSCRIBED) {
|
1142 | channel.state = channel.PENDING;
|
1143 | this._trySubscribe(channel);
|
1144 | }
|
1145 |
|
1146 | return channel;
|
1147 | };
|
1148 |
|
1149 | SCClientSocket.prototype._triggerChannelUnsubscribe = function (channel, newState) {
|
1150 | var channelName = channel.name;
|
1151 | var oldState = channel.state;
|
1152 |
|
1153 | if (newState) {
|
1154 | channel.state = newState;
|
1155 | } else {
|
1156 | channel.state = channel.UNSUBSCRIBED;
|
1157 | }
|
1158 | this._cancelPendingSubscribeCallback(channel);
|
1159 |
|
1160 | if (oldState == channel.SUBSCRIBED) {
|
1161 | var stateChangeData = {
|
1162 | channel: channelName,
|
1163 | oldState: oldState,
|
1164 | newState: channel.state
|
1165 | };
|
1166 | channel.emit('subscribeStateChange', stateChangeData);
|
1167 | channel.emit('unsubscribe', channelName);
|
1168 | Emitter.prototype.emit.call(this, 'subscribeStateChange', stateChangeData);
|
1169 | Emitter.prototype.emit.call(this, 'unsubscribe', channelName);
|
1170 | }
|
1171 | };
|
1172 |
|
1173 | SCClientSocket.prototype._tryUnsubscribe = function (channel) {
|
1174 | var self = this;
|
1175 |
|
1176 | if (this.state == this.OPEN) {
|
1177 | var options = {
|
1178 | noTimeout: true
|
1179 | };
|
1180 | if (channel.batch) {
|
1181 | options.batch = true;
|
1182 | }
|
1183 |
|
1184 | this._cancelPendingSubscribeCallback(channel);
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 | var decoratedChannelName = this._decorateChannelName(channel.name);
|
1191 | this.transport.emit('#unsubscribe', decoratedChannelName, options);
|
1192 | }
|
1193 | };
|
1194 |
|
1195 | SCClientSocket.prototype.unsubscribe = function (channelName) {
|
1196 | var channel = this.channels[channelName];
|
1197 |
|
1198 | if (channel) {
|
1199 | if (channel.state != channel.UNSUBSCRIBED) {
|
1200 |
|
1201 | this._triggerChannelUnsubscribe(channel);
|
1202 | this._tryUnsubscribe(channel);
|
1203 | }
|
1204 | }
|
1205 | };
|
1206 |
|
1207 | SCClientSocket.prototype.channel = function (channelName, options) {
|
1208 | var currentChannel = this.channels[channelName];
|
1209 |
|
1210 | if (!currentChannel) {
|
1211 | currentChannel = new SCChannel(channelName, this, options);
|
1212 | this.channels[channelName] = currentChannel;
|
1213 | }
|
1214 | return currentChannel;
|
1215 | };
|
1216 |
|
1217 | SCClientSocket.prototype.destroyChannel = function (channelName) {
|
1218 | var channel = this.channels[channelName];
|
1219 |
|
1220 | if (channel) {
|
1221 | channel.unwatch();
|
1222 | channel.unsubscribe();
|
1223 | delete this.channels[channelName];
|
1224 | }
|
1225 | };
|
1226 |
|
1227 | SCClientSocket.prototype.subscriptions = function (includePending) {
|
1228 | var subs = [];
|
1229 | var channel, includeChannel;
|
1230 | for (var channelName in this.channels) {
|
1231 | if (this.channels.hasOwnProperty(channelName)) {
|
1232 | channel = this.channels[channelName];
|
1233 |
|
1234 | if (includePending) {
|
1235 | includeChannel = channel && (channel.state == channel.SUBSCRIBED ||
|
1236 | channel.state == channel.PENDING);
|
1237 | } else {
|
1238 | includeChannel = channel && channel.state == channel.SUBSCRIBED;
|
1239 | }
|
1240 |
|
1241 | if (includeChannel) {
|
1242 | subs.push(channelName);
|
1243 | }
|
1244 | }
|
1245 | }
|
1246 | return subs;
|
1247 | };
|
1248 |
|
1249 | SCClientSocket.prototype.isSubscribed = function (channelName, includePending) {
|
1250 | var channel = this.channels[channelName];
|
1251 | if (includePending) {
|
1252 | return !!channel && (channel.state == channel.SUBSCRIBED ||
|
1253 | channel.state == channel.PENDING);
|
1254 | }
|
1255 | return !!channel && channel.state == channel.SUBSCRIBED;
|
1256 | };
|
1257 |
|
1258 | SCClientSocket.prototype.processPendingSubscriptions = function () {
|
1259 | var self = this;
|
1260 |
|
1261 | this.preparingPendingSubscriptions = false;
|
1262 |
|
1263 | var pendingChannels = [];
|
1264 |
|
1265 | for (var i in this.channels) {
|
1266 | if (this.channels.hasOwnProperty(i)) {
|
1267 | var channel = this.channels[i];
|
1268 | if (channel.state == channel.PENDING) {
|
1269 | pendingChannels.push(channel);
|
1270 | }
|
1271 | }
|
1272 | }
|
1273 |
|
1274 | pendingChannels.sort(function (a, b) {
|
1275 | var ap = a.priority || 0;
|
1276 | var bp = b.priority || 0;
|
1277 | if (ap > bp) {
|
1278 | return -1;
|
1279 | }
|
1280 | if (ap < bp) {
|
1281 | return 1;
|
1282 | }
|
1283 | return 0;
|
1284 | });
|
1285 |
|
1286 | pendingChannels.forEach(function (channel) {
|
1287 | self._trySubscribe(channel);
|
1288 | });
|
1289 | };
|
1290 |
|
1291 | SCClientSocket.prototype.watch = function (channelName, handler) {
|
1292 | if (typeof handler != 'function') {
|
1293 | throw new InvalidArgumentsError('No handler function was provided');
|
1294 | }
|
1295 | this._channelEmitter.on(channelName, handler);
|
1296 | };
|
1297 |
|
1298 | SCClientSocket.prototype.unwatch = function (channelName, handler) {
|
1299 | if (handler) {
|
1300 | this._channelEmitter.removeListener(channelName, handler);
|
1301 | } else {
|
1302 | this._channelEmitter.removeAllListeners(channelName);
|
1303 | }
|
1304 | };
|
1305 |
|
1306 | SCClientSocket.prototype.watchers = function (channelName) {
|
1307 | return this._channelEmitter.listeners(channelName);
|
1308 | };
|
1309 |
|
1310 | module.exports = SCClientSocket;
|
1311 |
|
1312 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},_dereq_("buffer").Buffer)
|
1313 | },{"./auth":2,"./response":4,"./sctransport":6,"base-64":8,"buffer":10,"clone":11,"component-emitter":12,"linked-list":15,"querystring":18,"sc-channel":19,"sc-errors":21,"sc-formatter":22}],6:[function(_dereq_,module,exports){
|
1314 | (function (global){
|
1315 | var Emitter = _dereq_('component-emitter');
|
1316 | var Response = _dereq_('./response').Response;
|
1317 | var querystring = _dereq_('querystring');
|
1318 | var WebSocket;
|
1319 | var createWebSocket;
|
1320 |
|
1321 | if (global.WebSocket) {
|
1322 | WebSocket = global.WebSocket;
|
1323 | createWebSocket = function (uri, options) {
|
1324 | return new WebSocket(uri);
|
1325 | };
|
1326 | } else {
|
1327 | WebSocket = _dereq_('ws');
|
1328 | createWebSocket = function (uri, options) {
|
1329 | return new WebSocket(uri, null, options);
|
1330 | };
|
1331 | }
|
1332 |
|
1333 | var scErrors = _dereq_('sc-errors');
|
1334 | var TimeoutError = scErrors.TimeoutError;
|
1335 | var BadConnectionError = scErrors.BadConnectionError;
|
1336 |
|
1337 |
|
1338 | var SCTransport = function (authEngine, codecEngine, options) {
|
1339 | var self = this;
|
1340 |
|
1341 | this.state = this.CLOSED;
|
1342 | this.auth = authEngine;
|
1343 | this.codec = codecEngine;
|
1344 | this.options = options;
|
1345 | this.connectTimeout = options.connectTimeout;
|
1346 | this.pingTimeout = options.ackTimeout;
|
1347 | this.pingTimeoutDisabled = !!options.pingTimeoutDisabled;
|
1348 | this.callIdGenerator = options.callIdGenerator;
|
1349 | this.authTokenName = options.authTokenName;
|
1350 |
|
1351 | this._pingTimeoutTicker = null;
|
1352 | this._callbackMap = {};
|
1353 | this._batchSendList = [];
|
1354 |
|
1355 |
|
1356 |
|
1357 | this.state = this.CONNECTING;
|
1358 | var uri = this.uri();
|
1359 |
|
1360 | var wsSocket = createWebSocket(uri, this.options);
|
1361 | wsSocket.binaryType = this.options.binaryType;
|
1362 |
|
1363 | this.socket = wsSocket;
|
1364 |
|
1365 | wsSocket.onopen = function () {
|
1366 | self._onOpen();
|
1367 | };
|
1368 |
|
1369 | wsSocket.onclose = function (event) {
|
1370 | var code;
|
1371 | if (event.code == null) {
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 | code = 1005;
|
1377 | } else {
|
1378 | code = event.code;
|
1379 | }
|
1380 | self._onClose(code, event.reason);
|
1381 | };
|
1382 |
|
1383 | wsSocket.onmessage = function (message, flags) {
|
1384 | self._onMessage(message.data);
|
1385 | };
|
1386 |
|
1387 | wsSocket.onerror = function (error) {
|
1388 |
|
1389 |
|
1390 |
|
1391 |
|
1392 |
|
1393 |
|
1394 | if (self.state === self.CONNECTING) {
|
1395 | self._onClose(1006);
|
1396 | }
|
1397 | };
|
1398 |
|
1399 | this._connectTimeoutRef = setTimeout(function () {
|
1400 | self._onClose(4007);
|
1401 | self.socket.close(4007);
|
1402 | }, this.connectTimeout);
|
1403 | };
|
1404 |
|
1405 | SCTransport.prototype = Object.create(Emitter.prototype);
|
1406 |
|
1407 | SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting';
|
1408 | SCTransport.OPEN = SCTransport.prototype.OPEN = 'open';
|
1409 | SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed';
|
1410 |
|
1411 | SCTransport.prototype.uri = function () {
|
1412 | var query = this.options.query || {};
|
1413 | var schema = this.options.secure ? 'wss' : 'ws';
|
1414 |
|
1415 | if (this.options.timestampRequests) {
|
1416 | query[this.options.timestampParam] = (new Date()).getTime();
|
1417 | }
|
1418 |
|
1419 | query = querystring.encode(query);
|
1420 |
|
1421 | if (query.length) {
|
1422 | query = '?' + query;
|
1423 | }
|
1424 |
|
1425 | var host;
|
1426 | if (this.options.host) {
|
1427 | host = this.options.host;
|
1428 | } else {
|
1429 | var port = '';
|
1430 |
|
1431 | if (this.options.port && ((schema == 'wss' && this.options.port != 443)
|
1432 | || (schema == 'ws' && this.options.port != 80))) {
|
1433 | port = ':' + this.options.port;
|
1434 | }
|
1435 | host = this.options.hostname + port;
|
1436 | }
|
1437 |
|
1438 | return schema + '://' + host + this.options.path + query;
|
1439 | };
|
1440 |
|
1441 | SCTransport.prototype._onOpen = function () {
|
1442 | var self = this;
|
1443 |
|
1444 | clearTimeout(this._connectTimeoutRef);
|
1445 | this._resetPingTimeout();
|
1446 |
|
1447 | this._handshake(function (err, status) {
|
1448 | if (err) {
|
1449 | var statusCode;
|
1450 | if (status && status.code) {
|
1451 | statusCode = status.code;
|
1452 | } else {
|
1453 | statusCode = 4003;
|
1454 | }
|
1455 | self._onError(err);
|
1456 | self._onClose(statusCode, err.toString());
|
1457 | self.socket.close(statusCode);
|
1458 | } else {
|
1459 | self.state = self.OPEN;
|
1460 | Emitter.prototype.emit.call(self, 'open', status);
|
1461 | self._resetPingTimeout();
|
1462 | }
|
1463 | });
|
1464 | };
|
1465 |
|
1466 | SCTransport.prototype._handshake = function (callback) {
|
1467 | var self = this;
|
1468 | this.auth.loadToken(this.authTokenName, function (err, token) {
|
1469 | if (err) {
|
1470 | callback(err);
|
1471 | } else {
|
1472 |
|
1473 |
|
1474 | var options = {
|
1475 | force: true
|
1476 | };
|
1477 | self.emit('#handshake', {
|
1478 | authToken: token
|
1479 | }, options, function (err, status) {
|
1480 | if (status) {
|
1481 |
|
1482 |
|
1483 | status.authToken = token;
|
1484 | if (status.authError) {
|
1485 | status.authError = scErrors.hydrateError(status.authError);
|
1486 | }
|
1487 | }
|
1488 | callback(err, status);
|
1489 | });
|
1490 | }
|
1491 | });
|
1492 | };
|
1493 |
|
1494 | SCTransport.prototype._abortAllPendingEventsDueToBadConnection = function (failureType) {
|
1495 | for (var i in this._callbackMap) {
|
1496 | if (this._callbackMap.hasOwnProperty(i)) {
|
1497 | var eventObject = this._callbackMap[i];
|
1498 | delete this._callbackMap[i];
|
1499 |
|
1500 | clearTimeout(eventObject.timeout);
|
1501 | delete eventObject.timeout;
|
1502 |
|
1503 | var errorMessage = "Event '" + eventObject.event +
|
1504 | "' was aborted due to a bad connection";
|
1505 | var badConnectionError = new BadConnectionError(errorMessage, failureType);
|
1506 |
|
1507 | var callback = eventObject.callback;
|
1508 | delete eventObject.callback;
|
1509 | callback.call(eventObject, badConnectionError, eventObject);
|
1510 | }
|
1511 | }
|
1512 | };
|
1513 |
|
1514 | SCTransport.prototype._onClose = function (code, data) {
|
1515 | delete this.socket.onopen;
|
1516 | delete this.socket.onclose;
|
1517 | delete this.socket.onmessage;
|
1518 | delete this.socket.onerror;
|
1519 |
|
1520 | clearTimeout(this._connectTimeoutRef);
|
1521 | clearTimeout(this._pingTimeoutTicker);
|
1522 | clearTimeout(this._batchTimeout);
|
1523 |
|
1524 | if (this.state == this.OPEN) {
|
1525 | this.state = this.CLOSED;
|
1526 | Emitter.prototype.emit.call(this, 'close', code, data);
|
1527 | this._abortAllPendingEventsDueToBadConnection('disconnect');
|
1528 |
|
1529 | } else if (this.state == this.CONNECTING) {
|
1530 | this.state = this.CLOSED;
|
1531 | Emitter.prototype.emit.call(this, 'openAbort', code, data);
|
1532 | this._abortAllPendingEventsDueToBadConnection('connectAbort');
|
1533 | }
|
1534 | };
|
1535 |
|
1536 | SCTransport.prototype._handleEventObject = function (obj, message) {
|
1537 | if (obj && obj.event != null) {
|
1538 | var response = new Response(this, obj.cid);
|
1539 | Emitter.prototype.emit.call(this, 'event', obj.event, obj.data, response);
|
1540 | } else if (obj && obj.rid != null) {
|
1541 | var eventObject = this._callbackMap[obj.rid];
|
1542 | if (eventObject) {
|
1543 | clearTimeout(eventObject.timeout);
|
1544 | delete eventObject.timeout;
|
1545 | delete this._callbackMap[obj.rid];
|
1546 |
|
1547 | if (eventObject.callback) {
|
1548 | var rehydratedError = scErrors.hydrateError(obj.error);
|
1549 | eventObject.callback(rehydratedError, obj.data);
|
1550 | }
|
1551 | }
|
1552 | } else {
|
1553 | Emitter.prototype.emit.call(this, 'event', 'raw', message);
|
1554 | }
|
1555 | };
|
1556 |
|
1557 | SCTransport.prototype._onMessage = function (message) {
|
1558 | Emitter.prototype.emit.call(this, 'event', 'message', message);
|
1559 |
|
1560 | var obj = this.decode(message);
|
1561 |
|
1562 |
|
1563 | if (obj == '#1') {
|
1564 | this._resetPingTimeout();
|
1565 | if (this.socket.readyState == this.socket.OPEN) {
|
1566 | this.sendObject('#2');
|
1567 | }
|
1568 | } else {
|
1569 | if (Array.isArray(obj)) {
|
1570 | var len = obj.length;
|
1571 | for (var i = 0; i < len; i++) {
|
1572 | this._handleEventObject(obj[i], message);
|
1573 | }
|
1574 | } else {
|
1575 | this._handleEventObject(obj, message);
|
1576 | }
|
1577 | }
|
1578 | };
|
1579 |
|
1580 | SCTransport.prototype._onError = function (err) {
|
1581 | Emitter.prototype.emit.call(this, 'error', err);
|
1582 | };
|
1583 |
|
1584 | SCTransport.prototype._resetPingTimeout = function () {
|
1585 | if (this.pingTimeoutDisabled) {
|
1586 | return;
|
1587 | }
|
1588 | var self = this;
|
1589 |
|
1590 | var now = (new Date()).getTime();
|
1591 | clearTimeout(this._pingTimeoutTicker);
|
1592 |
|
1593 | this._pingTimeoutTicker = setTimeout(function () {
|
1594 | self._onClose(4000);
|
1595 | self.socket.close(4000);
|
1596 | }, this.pingTimeout);
|
1597 | };
|
1598 |
|
1599 | SCTransport.prototype.getBytesReceived = function () {
|
1600 | return this.socket.bytesReceived;
|
1601 | };
|
1602 |
|
1603 | SCTransport.prototype.close = function (code, data) {
|
1604 | code = code || 1000;
|
1605 |
|
1606 | if (this.state == this.OPEN) {
|
1607 | var packet = {
|
1608 | code: code,
|
1609 | data: data
|
1610 | };
|
1611 | this.emit('#disconnect', packet);
|
1612 |
|
1613 | this._onClose(code, data);
|
1614 | this.socket.close(code);
|
1615 |
|
1616 | } else if (this.state == this.CONNECTING) {
|
1617 | this._onClose(code, data);
|
1618 | this.socket.close(code);
|
1619 | }
|
1620 | };
|
1621 |
|
1622 | SCTransport.prototype.emitObject = function (eventObject, options) {
|
1623 | var simpleEventObject = {
|
1624 | event: eventObject.event,
|
1625 | data: eventObject.data
|
1626 | };
|
1627 |
|
1628 | if (eventObject.callback) {
|
1629 | simpleEventObject.cid = eventObject.cid = this.callIdGenerator();
|
1630 | this._callbackMap[eventObject.cid] = eventObject;
|
1631 | }
|
1632 |
|
1633 | this.sendObject(simpleEventObject, options);
|
1634 |
|
1635 | return eventObject.cid || null;
|
1636 | };
|
1637 |
|
1638 | SCTransport.prototype._handleEventAckTimeout = function (eventObject) {
|
1639 | if (eventObject.cid) {
|
1640 | delete this._callbackMap[eventObject.cid];
|
1641 | }
|
1642 | delete eventObject.timeout;
|
1643 |
|
1644 | var callback = eventObject.callback;
|
1645 | if (callback) {
|
1646 | delete eventObject.callback;
|
1647 | var error = new TimeoutError("Event response for '" + eventObject.event + "' timed out");
|
1648 | callback.call(eventObject, error, eventObject);
|
1649 | }
|
1650 | };
|
1651 |
|
1652 |
|
1653 | SCTransport.prototype.emit = function (event, data, a, b) {
|
1654 | var self = this;
|
1655 |
|
1656 | var callback, options;
|
1657 |
|
1658 | if (b) {
|
1659 | options = a;
|
1660 | callback = b;
|
1661 | } else {
|
1662 | if (a instanceof Function) {
|
1663 | options = {};
|
1664 | callback = a;
|
1665 | } else {
|
1666 | options = a;
|
1667 | }
|
1668 | }
|
1669 |
|
1670 | var eventObject = {
|
1671 | event: event,
|
1672 | data: data,
|
1673 | callback: callback
|
1674 | };
|
1675 |
|
1676 | if (callback && !options.noTimeout) {
|
1677 | eventObject.timeout = setTimeout(function () {
|
1678 | self._handleEventAckTimeout(eventObject);
|
1679 | }, this.options.ackTimeout);
|
1680 | }
|
1681 |
|
1682 | var cid = null;
|
1683 | if (this.state == this.OPEN || options.force) {
|
1684 | cid = this.emitObject(eventObject, options);
|
1685 | }
|
1686 | return cid;
|
1687 | };
|
1688 |
|
1689 | SCTransport.prototype.cancelPendingResponse = function (cid) {
|
1690 | delete this._callbackMap[cid];
|
1691 | };
|
1692 |
|
1693 | SCTransport.prototype.decode = function (message) {
|
1694 | return this.codec.decode(message);
|
1695 | };
|
1696 |
|
1697 | SCTransport.prototype.encode = function (object) {
|
1698 | return this.codec.encode(object);
|
1699 | };
|
1700 |
|
1701 | SCTransport.prototype.send = function (data) {
|
1702 | if (this.socket.readyState != this.socket.OPEN) {
|
1703 | this._onClose(1005);
|
1704 | } else {
|
1705 | this.socket.send(data);
|
1706 | }
|
1707 | };
|
1708 |
|
1709 | SCTransport.prototype.serializeObject = function (object) {
|
1710 | var str, formatError;
|
1711 | try {
|
1712 | str = this.encode(object);
|
1713 | } catch (err) {
|
1714 | formatError = err;
|
1715 | this._onError(formatError);
|
1716 | }
|
1717 | if (!formatError) {
|
1718 | return str;
|
1719 | }
|
1720 | return null;
|
1721 | };
|
1722 |
|
1723 | SCTransport.prototype.sendObjectBatch = function (object) {
|
1724 | var self = this;
|
1725 |
|
1726 | this._batchSendList.push(object);
|
1727 | if (this._batchTimeout) {
|
1728 | return;
|
1729 | }
|
1730 |
|
1731 | this._batchTimeout = setTimeout(function () {
|
1732 | delete self._batchTimeout;
|
1733 | if (self._batchSendList.length) {
|
1734 | var str = self.serializeObject(self._batchSendList);
|
1735 | if (str != null) {
|
1736 | self.send(str);
|
1737 | }
|
1738 | self._batchSendList = [];
|
1739 | }
|
1740 | }, this.options.pubSubBatchDuration || 0);
|
1741 | };
|
1742 |
|
1743 | SCTransport.prototype.sendObjectSingle = function (object) {
|
1744 | var str = this.serializeObject(object);
|
1745 | if (str != null) {
|
1746 | this.send(str);
|
1747 | }
|
1748 | };
|
1749 |
|
1750 | SCTransport.prototype.sendObject = function (object, options) {
|
1751 | if (options && options.batch) {
|
1752 | this.sendObjectBatch(object);
|
1753 | } else {
|
1754 | this.sendObjectSingle(object);
|
1755 | }
|
1756 | };
|
1757 |
|
1758 | module.exports.SCTransport = SCTransport;
|
1759 |
|
1760 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
1761 | },{"./response":4,"component-emitter":12,"querystring":18,"sc-errors":21,"ws":7}],7:[function(_dereq_,module,exports){
|
1762 | var global;
|
1763 | if (typeof WorkerGlobalScope !== 'undefined') {
|
1764 | global = self;
|
1765 | } else {
|
1766 | global = typeof window != 'undefined' && window || (function() { return this; })();
|
1767 | }
|
1768 |
|
1769 | var WebSocket = global.WebSocket || global.MozWebSocket;
|
1770 |
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 |
|
1784 | function ws(uri, protocols, opts) {
|
1785 | var instance;
|
1786 | if (protocols) {
|
1787 | instance = new WebSocket(uri, protocols);
|
1788 | } else {
|
1789 | instance = new WebSocket(uri);
|
1790 | }
|
1791 | return instance;
|
1792 | }
|
1793 |
|
1794 | if (WebSocket) ws.prototype = WebSocket.prototype;
|
1795 |
|
1796 | module.exports = WebSocket ? ws : null;
|
1797 |
|
1798 | },{}],8:[function(_dereq_,module,exports){
|
1799 | (function (global){
|
1800 |
|
1801 | ;(function(root) {
|
1802 |
|
1803 |
|
1804 | var freeExports = typeof exports == 'object' && exports;
|
1805 |
|
1806 |
|
1807 | var freeModule = typeof module == 'object' && module &&
|
1808 | module.exports == freeExports && module;
|
1809 |
|
1810 |
|
1811 |
|
1812 | var freeGlobal = typeof global == 'object' && global;
|
1813 | if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
|
1814 | root = freeGlobal;
|
1815 | }
|
1816 |
|
1817 |
|
1818 |
|
1819 | var InvalidCharacterError = function(message) {
|
1820 | this.message = message;
|
1821 | };
|
1822 | InvalidCharacterError.prototype = new Error;
|
1823 | InvalidCharacterError.prototype.name = 'InvalidCharacterError';
|
1824 |
|
1825 | var error = function(message) {
|
1826 |
|
1827 |
|
1828 | throw new InvalidCharacterError(message);
|
1829 | };
|
1830 |
|
1831 | var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
1832 |
|
1833 | var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g;
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 |
|
1839 | var decode = function(input) {
|
1840 | input = String(input)
|
1841 | .replace(REGEX_SPACE_CHARACTERS, '');
|
1842 | var length = input.length;
|
1843 | if (length % 4 == 0) {
|
1844 | input = input.replace(/==?$/, '');
|
1845 | length = input.length;
|
1846 | }
|
1847 | if (
|
1848 | length % 4 == 1 ||
|
1849 |
|
1850 | /[^+a-zA-Z0-9/]/.test(input)
|
1851 | ) {
|
1852 | error(
|
1853 | 'Invalid character: the string to be decoded is not correctly encoded.'
|
1854 | );
|
1855 | }
|
1856 | var bitCounter = 0;
|
1857 | var bitStorage;
|
1858 | var buffer;
|
1859 | var output = '';
|
1860 | var position = -1;
|
1861 | while (++position < length) {
|
1862 | buffer = TABLE.indexOf(input.charAt(position));
|
1863 | bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer;
|
1864 |
|
1865 | if (bitCounter++ % 4) {
|
1866 |
|
1867 | output += String.fromCharCode(
|
1868 | 0xFF & bitStorage >> (-2 * bitCounter & 6)
|
1869 | );
|
1870 | }
|
1871 | }
|
1872 | return output;
|
1873 | };
|
1874 |
|
1875 |
|
1876 |
|
1877 | var encode = function(input) {
|
1878 | input = String(input);
|
1879 | if (/[^\0-\xFF]/.test(input)) {
|
1880 |
|
1881 |
|
1882 | error(
|
1883 | 'The string to be encoded contains characters outside of the ' +
|
1884 | 'Latin1 range.'
|
1885 | );
|
1886 | }
|
1887 | var padding = input.length % 3;
|
1888 | var output = '';
|
1889 | var position = -1;
|
1890 | var a;
|
1891 | var b;
|
1892 | var c;
|
1893 | var d;
|
1894 | var buffer;
|
1895 |
|
1896 | var length = input.length - padding;
|
1897 |
|
1898 | while (++position < length) {
|
1899 |
|
1900 | a = input.charCodeAt(position) << 16;
|
1901 | b = input.charCodeAt(++position) << 8;
|
1902 | c = input.charCodeAt(++position);
|
1903 | buffer = a + b + c;
|
1904 |
|
1905 |
|
1906 | output += (
|
1907 | TABLE.charAt(buffer >> 18 & 0x3F) +
|
1908 | TABLE.charAt(buffer >> 12 & 0x3F) +
|
1909 | TABLE.charAt(buffer >> 6 & 0x3F) +
|
1910 | TABLE.charAt(buffer & 0x3F)
|
1911 | );
|
1912 | }
|
1913 |
|
1914 | if (padding == 2) {
|
1915 | a = input.charCodeAt(position) << 8;
|
1916 | b = input.charCodeAt(++position);
|
1917 | buffer = a + b;
|
1918 | output += (
|
1919 | TABLE.charAt(buffer >> 10) +
|
1920 | TABLE.charAt((buffer >> 4) & 0x3F) +
|
1921 | TABLE.charAt((buffer << 2) & 0x3F) +
|
1922 | '='
|
1923 | );
|
1924 | } else if (padding == 1) {
|
1925 | buffer = input.charCodeAt(position);
|
1926 | output += (
|
1927 | TABLE.charAt(buffer >> 2) +
|
1928 | TABLE.charAt((buffer << 4) & 0x3F) +
|
1929 | '=='
|
1930 | );
|
1931 | }
|
1932 |
|
1933 | return output;
|
1934 | };
|
1935 |
|
1936 | var base64 = {
|
1937 | 'encode': encode,
|
1938 | 'decode': decode,
|
1939 | 'version': '0.1.0'
|
1940 | };
|
1941 |
|
1942 |
|
1943 |
|
1944 | if (
|
1945 | typeof define == 'function' &&
|
1946 | typeof define.amd == 'object' &&
|
1947 | define.amd
|
1948 | ) {
|
1949 | define(function() {
|
1950 | return base64;
|
1951 | });
|
1952 | } else if (freeExports && !freeExports.nodeType) {
|
1953 | if (freeModule) {
|
1954 | freeModule.exports = base64;
|
1955 | } else {
|
1956 | for (var key in base64) {
|
1957 | base64.hasOwnProperty(key) && (freeExports[key] = base64[key]);
|
1958 | }
|
1959 | }
|
1960 | } else {
|
1961 | root.base64 = base64;
|
1962 | }
|
1963 |
|
1964 | }(this));
|
1965 |
|
1966 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
1967 | },{}],9:[function(_dereq_,module,exports){
|
1968 | 'use strict'
|
1969 |
|
1970 | exports.byteLength = byteLength
|
1971 | exports.toByteArray = toByteArray
|
1972 | exports.fromByteArray = fromByteArray
|
1973 |
|
1974 | var lookup = []
|
1975 | var revLookup = []
|
1976 | var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
|
1977 |
|
1978 | var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
1979 | for (var i = 0, len = code.length; i < len; ++i) {
|
1980 | lookup[i] = code[i]
|
1981 | revLookup[code.charCodeAt(i)] = i
|
1982 | }
|
1983 |
|
1984 |
|
1985 |
|
1986 | revLookup['-'.charCodeAt(0)] = 62
|
1987 | revLookup['_'.charCodeAt(0)] = 63
|
1988 |
|
1989 | function getLens (b64) {
|
1990 | var len = b64.length
|
1991 |
|
1992 | if (len % 4 > 0) {
|
1993 | throw new Error('Invalid string. Length must be a multiple of 4')
|
1994 | }
|
1995 |
|
1996 |
|
1997 |
|
1998 | var validLen = b64.indexOf('=')
|
1999 | if (validLen === -1) validLen = len
|
2000 |
|
2001 | var placeHoldersLen = validLen === len
|
2002 | ? 0
|
2003 | : 4 - (validLen % 4)
|
2004 |
|
2005 | return [validLen, placeHoldersLen]
|
2006 | }
|
2007 |
|
2008 |
|
2009 | function byteLength (b64) {
|
2010 | var lens = getLens(b64)
|
2011 | var validLen = lens[0]
|
2012 | var placeHoldersLen = lens[1]
|
2013 | return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
|
2014 | }
|
2015 |
|
2016 | function _byteLength (b64, validLen, placeHoldersLen) {
|
2017 | return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
|
2018 | }
|
2019 |
|
2020 | function toByteArray (b64) {
|
2021 | var tmp
|
2022 | var lens = getLens(b64)
|
2023 | var validLen = lens[0]
|
2024 | var placeHoldersLen = lens[1]
|
2025 |
|
2026 | var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
|
2027 |
|
2028 | var curByte = 0
|
2029 |
|
2030 |
|
2031 | var len = placeHoldersLen > 0
|
2032 | ? validLen - 4
|
2033 | : validLen
|
2034 |
|
2035 | for (var i = 0; i < len; i += 4) {
|
2036 | tmp =
|
2037 | (revLookup[b64.charCodeAt(i)] << 18) |
|
2038 | (revLookup[b64.charCodeAt(i + 1)] << 12) |
|
2039 | (revLookup[b64.charCodeAt(i + 2)] << 6) |
|
2040 | revLookup[b64.charCodeAt(i + 3)]
|
2041 | arr[curByte++] = (tmp >> 16) & 0xFF
|
2042 | arr[curByte++] = (tmp >> 8) & 0xFF
|
2043 | arr[curByte++] = tmp & 0xFF
|
2044 | }
|
2045 |
|
2046 | if (placeHoldersLen === 2) {
|
2047 | tmp =
|
2048 | (revLookup[b64.charCodeAt(i)] << 2) |
|
2049 | (revLookup[b64.charCodeAt(i + 1)] >> 4)
|
2050 | arr[curByte++] = tmp & 0xFF
|
2051 | }
|
2052 |
|
2053 | if (placeHoldersLen === 1) {
|
2054 | tmp =
|
2055 | (revLookup[b64.charCodeAt(i)] << 10) |
|
2056 | (revLookup[b64.charCodeAt(i + 1)] << 4) |
|
2057 | (revLookup[b64.charCodeAt(i + 2)] >> 2)
|
2058 | arr[curByte++] = (tmp >> 8) & 0xFF
|
2059 | arr[curByte++] = tmp & 0xFF
|
2060 | }
|
2061 |
|
2062 | return arr
|
2063 | }
|
2064 |
|
2065 | function tripletToBase64 (num) {
|
2066 | return lookup[num >> 18 & 0x3F] +
|
2067 | lookup[num >> 12 & 0x3F] +
|
2068 | lookup[num >> 6 & 0x3F] +
|
2069 | lookup[num & 0x3F]
|
2070 | }
|
2071 |
|
2072 | function encodeChunk (uint8, start, end) {
|
2073 | var tmp
|
2074 | var output = []
|
2075 | for (var i = start; i < end; i += 3) {
|
2076 | tmp =
|
2077 | ((uint8[i] << 16) & 0xFF0000) +
|
2078 | ((uint8[i + 1] << 8) & 0xFF00) +
|
2079 | (uint8[i + 2] & 0xFF)
|
2080 | output.push(tripletToBase64(tmp))
|
2081 | }
|
2082 | return output.join('')
|
2083 | }
|
2084 |
|
2085 | function fromByteArray (uint8) {
|
2086 | var tmp
|
2087 | var len = uint8.length
|
2088 | var extraBytes = len % 3
|
2089 | var parts = []
|
2090 | var maxChunkLength = 16383
|
2091 |
|
2092 |
|
2093 | for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
2094 | parts.push(encodeChunk(
|
2095 | uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)
|
2096 | ))
|
2097 | }
|
2098 |
|
2099 |
|
2100 | if (extraBytes === 1) {
|
2101 | tmp = uint8[len - 1]
|
2102 | parts.push(
|
2103 | lookup[tmp >> 2] +
|
2104 | lookup[(tmp << 4) & 0x3F] +
|
2105 | '=='
|
2106 | )
|
2107 | } else if (extraBytes === 2) {
|
2108 | tmp = (uint8[len - 2] << 8) + uint8[len - 1]
|
2109 | parts.push(
|
2110 | lookup[tmp >> 10] +
|
2111 | lookup[(tmp >> 4) & 0x3F] +
|
2112 | lookup[(tmp << 2) & 0x3F] +
|
2113 | '='
|
2114 | )
|
2115 | }
|
2116 |
|
2117 | return parts.join('')
|
2118 | }
|
2119 |
|
2120 | },{}],10:[function(_dereq_,module,exports){
|
2121 |
|
2122 |
|
2123 |
|
2124 |
|
2125 |
|
2126 |
|
2127 |
|
2128 |
|
2129 | 'use strict'
|
2130 |
|
2131 | var base64 = _dereq_('base64-js')
|
2132 | var ieee754 = _dereq_('ieee754')
|
2133 |
|
2134 | exports.Buffer = Buffer
|
2135 | exports.SlowBuffer = SlowBuffer
|
2136 | exports.INSPECT_MAX_BYTES = 50
|
2137 |
|
2138 | var K_MAX_LENGTH = 0x7fffffff
|
2139 | exports.kMaxLength = K_MAX_LENGTH
|
2140 |
|
2141 |
|
2142 |
|
2143 |
|
2144 |
|
2145 |
|
2146 |
|
2147 |
|
2148 |
|
2149 |
|
2150 |
|
2151 |
|
2152 |
|
2153 |
|
2154 |
|
2155 | Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
|
2156 |
|
2157 | if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
|
2158 | typeof console.error === 'function') {
|
2159 | console.error(
|
2160 | 'This browser lacks typed array (Uint8Array) support which is required by ' +
|
2161 | '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
|
2162 | )
|
2163 | }
|
2164 |
|
2165 | function typedArraySupport () {
|
2166 |
|
2167 | try {
|
2168 | var arr = new Uint8Array(1)
|
2169 | arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } }
|
2170 | return arr.foo() === 42
|
2171 | } catch (e) {
|
2172 | return false
|
2173 | }
|
2174 | }
|
2175 |
|
2176 | Object.defineProperty(Buffer.prototype, 'parent', {
|
2177 | enumerable: true,
|
2178 | get: function () {
|
2179 | if (!Buffer.isBuffer(this)) return undefined
|
2180 | return this.buffer
|
2181 | }
|
2182 | })
|
2183 |
|
2184 | Object.defineProperty(Buffer.prototype, 'offset', {
|
2185 | enumerable: true,
|
2186 | get: function () {
|
2187 | if (!Buffer.isBuffer(this)) return undefined
|
2188 | return this.byteOffset
|
2189 | }
|
2190 | })
|
2191 |
|
2192 | function createBuffer (length) {
|
2193 | if (length > K_MAX_LENGTH) {
|
2194 | throw new RangeError('The value "' + length + '" is invalid for option "size"')
|
2195 | }
|
2196 |
|
2197 | var buf = new Uint8Array(length)
|
2198 | buf.__proto__ = Buffer.prototype
|
2199 | return buf
|
2200 | }
|
2201 |
|
2202 |
|
2203 |
|
2204 |
|
2205 |
|
2206 |
|
2207 |
|
2208 |
|
2209 |
|
2210 |
|
2211 |
|
2212 | function Buffer (arg, encodingOrOffset, length) {
|
2213 |
|
2214 | if (typeof arg === 'number') {
|
2215 | if (typeof encodingOrOffset === 'string') {
|
2216 | throw new TypeError(
|
2217 | 'The "string" argument must be of type string. Received type number'
|
2218 | )
|
2219 | }
|
2220 | return allocUnsafe(arg)
|
2221 | }
|
2222 | return from(arg, encodingOrOffset, length)
|
2223 | }
|
2224 |
|
2225 |
|
2226 | if (typeof Symbol !== 'undefined' && Symbol.species != null &&
|
2227 | Buffer[Symbol.species] === Buffer) {
|
2228 | Object.defineProperty(Buffer, Symbol.species, {
|
2229 | value: null,
|
2230 | configurable: true,
|
2231 | enumerable: false,
|
2232 | writable: false
|
2233 | })
|
2234 | }
|
2235 |
|
2236 | Buffer.poolSize = 8192
|
2237 |
|
2238 | function from (value, encodingOrOffset, length) {
|
2239 | if (typeof value === 'string') {
|
2240 | return fromString(value, encodingOrOffset)
|
2241 | }
|
2242 |
|
2243 | if (ArrayBuffer.isView(value)) {
|
2244 | return fromArrayLike(value)
|
2245 | }
|
2246 |
|
2247 | if (value == null) {
|
2248 | throw TypeError(
|
2249 | 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
|
2250 | 'or Array-like Object. Received type ' + (typeof value)
|
2251 | )
|
2252 | }
|
2253 |
|
2254 | if (isInstance(value, ArrayBuffer) ||
|
2255 | (value && isInstance(value.buffer, ArrayBuffer))) {
|
2256 | return fromArrayBuffer(value, encodingOrOffset, length)
|
2257 | }
|
2258 |
|
2259 | if (typeof value === 'number') {
|
2260 | throw new TypeError(
|
2261 | 'The "value" argument must not be of type number. Received type number'
|
2262 | )
|
2263 | }
|
2264 |
|
2265 | var valueOf = value.valueOf && value.valueOf()
|
2266 | if (valueOf != null && valueOf !== value) {
|
2267 | return Buffer.from(valueOf, encodingOrOffset, length)
|
2268 | }
|
2269 |
|
2270 | var b = fromObject(value)
|
2271 | if (b) return b
|
2272 |
|
2273 | if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
|
2274 | typeof value[Symbol.toPrimitive] === 'function') {
|
2275 | return Buffer.from(
|
2276 | value[Symbol.toPrimitive]('string'), encodingOrOffset, length
|
2277 | )
|
2278 | }
|
2279 |
|
2280 | throw new TypeError(
|
2281 | 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
|
2282 | 'or Array-like Object. Received type ' + (typeof value)
|
2283 | )
|
2284 | }
|
2285 |
|
2286 |
|
2287 |
|
2288 |
|
2289 |
|
2290 |
|
2291 |
|
2292 |
|
2293 |
|
2294 | Buffer.from = function (value, encodingOrOffset, length) {
|
2295 | return from(value, encodingOrOffset, length)
|
2296 | }
|
2297 |
|
2298 |
|
2299 |
|
2300 | Buffer.prototype.__proto__ = Uint8Array.prototype
|
2301 | Buffer.__proto__ = Uint8Array
|
2302 |
|
2303 | function assertSize (size) {
|
2304 | if (typeof size !== 'number') {
|
2305 | throw new TypeError('"size" argument must be of type number')
|
2306 | } else if (size < 0) {
|
2307 | throw new RangeError('The value "' + size + '" is invalid for option "size"')
|
2308 | }
|
2309 | }
|
2310 |
|
2311 | function alloc (size, fill, encoding) {
|
2312 | assertSize(size)
|
2313 | if (size <= 0) {
|
2314 | return createBuffer(size)
|
2315 | }
|
2316 | if (fill !== undefined) {
|
2317 |
|
2318 |
|
2319 |
|
2320 | return typeof encoding === 'string'
|
2321 | ? createBuffer(size).fill(fill, encoding)
|
2322 | : createBuffer(size).fill(fill)
|
2323 | }
|
2324 | return createBuffer(size)
|
2325 | }
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 |
|
2331 | Buffer.alloc = function (size, fill, encoding) {
|
2332 | return alloc(size, fill, encoding)
|
2333 | }
|
2334 |
|
2335 | function allocUnsafe (size) {
|
2336 | assertSize(size)
|
2337 | return createBuffer(size < 0 ? 0 : checked(size) | 0)
|
2338 | }
|
2339 |
|
2340 |
|
2341 |
|
2342 |
|
2343 | Buffer.allocUnsafe = function (size) {
|
2344 | return allocUnsafe(size)
|
2345 | }
|
2346 |
|
2347 |
|
2348 |
|
2349 | Buffer.allocUnsafeSlow = function (size) {
|
2350 | return allocUnsafe(size)
|
2351 | }
|
2352 |
|
2353 | function fromString (string, encoding) {
|
2354 | if (typeof encoding !== 'string' || encoding === '') {
|
2355 | encoding = 'utf8'
|
2356 | }
|
2357 |
|
2358 | if (!Buffer.isEncoding(encoding)) {
|
2359 | throw new TypeError('Unknown encoding: ' + encoding)
|
2360 | }
|
2361 |
|
2362 | var length = byteLength(string, encoding) | 0
|
2363 | var buf = createBuffer(length)
|
2364 |
|
2365 | var actual = buf.write(string, encoding)
|
2366 |
|
2367 | if (actual !== length) {
|
2368 |
|
2369 |
|
2370 |
|
2371 | buf = buf.slice(0, actual)
|
2372 | }
|
2373 |
|
2374 | return buf
|
2375 | }
|
2376 |
|
2377 | function fromArrayLike (array) {
|
2378 | var length = array.length < 0 ? 0 : checked(array.length) | 0
|
2379 | var buf = createBuffer(length)
|
2380 | for (var i = 0; i < length; i += 1) {
|
2381 | buf[i] = array[i] & 255
|
2382 | }
|
2383 | return buf
|
2384 | }
|
2385 |
|
2386 | function fromArrayBuffer (array, byteOffset, length) {
|
2387 | if (byteOffset < 0 || array.byteLength < byteOffset) {
|
2388 | throw new RangeError('"offset" is outside of buffer bounds')
|
2389 | }
|
2390 |
|
2391 | if (array.byteLength < byteOffset + (length || 0)) {
|
2392 | throw new RangeError('"length" is outside of buffer bounds')
|
2393 | }
|
2394 |
|
2395 | var buf
|
2396 | if (byteOffset === undefined && length === undefined) {
|
2397 | buf = new Uint8Array(array)
|
2398 | } else if (length === undefined) {
|
2399 | buf = new Uint8Array(array, byteOffset)
|
2400 | } else {
|
2401 | buf = new Uint8Array(array, byteOffset, length)
|
2402 | }
|
2403 |
|
2404 |
|
2405 | buf.__proto__ = Buffer.prototype
|
2406 | return buf
|
2407 | }
|
2408 |
|
2409 | function fromObject (obj) {
|
2410 | if (Buffer.isBuffer(obj)) {
|
2411 | var len = checked(obj.length) | 0
|
2412 | var buf = createBuffer(len)
|
2413 |
|
2414 | if (buf.length === 0) {
|
2415 | return buf
|
2416 | }
|
2417 |
|
2418 | obj.copy(buf, 0, 0, len)
|
2419 | return buf
|
2420 | }
|
2421 |
|
2422 | if (obj.length !== undefined) {
|
2423 | if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
|
2424 | return createBuffer(0)
|
2425 | }
|
2426 | return fromArrayLike(obj)
|
2427 | }
|
2428 |
|
2429 | if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
2430 | return fromArrayLike(obj.data)
|
2431 | }
|
2432 | }
|
2433 |
|
2434 | function checked (length) {
|
2435 |
|
2436 |
|
2437 | if (length >= K_MAX_LENGTH) {
|
2438 | throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
2439 | 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
|
2440 | }
|
2441 | return length | 0
|
2442 | }
|
2443 |
|
2444 | function SlowBuffer (length) {
|
2445 | if (+length != length) {
|
2446 | length = 0
|
2447 | }
|
2448 | return Buffer.alloc(+length)
|
2449 | }
|
2450 |
|
2451 | Buffer.isBuffer = function isBuffer (b) {
|
2452 | return b != null && b._isBuffer === true &&
|
2453 | b !== Buffer.prototype
|
2454 | }
|
2455 |
|
2456 | Buffer.compare = function compare (a, b) {
|
2457 | if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
|
2458 | if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
|
2459 | if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
|
2460 | throw new TypeError(
|
2461 | 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
|
2462 | )
|
2463 | }
|
2464 |
|
2465 | if (a === b) return 0
|
2466 |
|
2467 | var x = a.length
|
2468 | var y = b.length
|
2469 |
|
2470 | for (var i = 0, len = Math.min(x, y); i < len; ++i) {
|
2471 | if (a[i] !== b[i]) {
|
2472 | x = a[i]
|
2473 | y = b[i]
|
2474 | break
|
2475 | }
|
2476 | }
|
2477 |
|
2478 | if (x < y) return -1
|
2479 | if (y < x) return 1
|
2480 | return 0
|
2481 | }
|
2482 |
|
2483 | Buffer.isEncoding = function isEncoding (encoding) {
|
2484 | switch (String(encoding).toLowerCase()) {
|
2485 | case 'hex':
|
2486 | case 'utf8':
|
2487 | case 'utf-8':
|
2488 | case 'ascii':
|
2489 | case 'latin1':
|
2490 | case 'binary':
|
2491 | case 'base64':
|
2492 | case 'ucs2':
|
2493 | case 'ucs-2':
|
2494 | case 'utf16le':
|
2495 | case 'utf-16le':
|
2496 | return true
|
2497 | default:
|
2498 | return false
|
2499 | }
|
2500 | }
|
2501 |
|
2502 | Buffer.concat = function concat (list, length) {
|
2503 | if (!Array.isArray(list)) {
|
2504 | throw new TypeError('"list" argument must be an Array of Buffers')
|
2505 | }
|
2506 |
|
2507 | if (list.length === 0) {
|
2508 | return Buffer.alloc(0)
|
2509 | }
|
2510 |
|
2511 | var i
|
2512 | if (length === undefined) {
|
2513 | length = 0
|
2514 | for (i = 0; i < list.length; ++i) {
|
2515 | length += list[i].length
|
2516 | }
|
2517 | }
|
2518 |
|
2519 | var buffer = Buffer.allocUnsafe(length)
|
2520 | var pos = 0
|
2521 | for (i = 0; i < list.length; ++i) {
|
2522 | var buf = list[i]
|
2523 | if (isInstance(buf, Uint8Array)) {
|
2524 | buf = Buffer.from(buf)
|
2525 | }
|
2526 | if (!Buffer.isBuffer(buf)) {
|
2527 | throw new TypeError('"list" argument must be an Array of Buffers')
|
2528 | }
|
2529 | buf.copy(buffer, pos)
|
2530 | pos += buf.length
|
2531 | }
|
2532 | return buffer
|
2533 | }
|
2534 |
|
2535 | function byteLength (string, encoding) {
|
2536 | if (Buffer.isBuffer(string)) {
|
2537 | return string.length
|
2538 | }
|
2539 | if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
|
2540 | return string.byteLength
|
2541 | }
|
2542 | if (typeof string !== 'string') {
|
2543 | throw new TypeError(
|
2544 | 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
|
2545 | 'Received type ' + typeof string
|
2546 | )
|
2547 | }
|
2548 |
|
2549 | var len = string.length
|
2550 | var mustMatch = (arguments.length > 2 && arguments[2] === true)
|
2551 | if (!mustMatch && len === 0) return 0
|
2552 |
|
2553 |
|
2554 | var loweredCase = false
|
2555 | for (;;) {
|
2556 | switch (encoding) {
|
2557 | case 'ascii':
|
2558 | case 'latin1':
|
2559 | case 'binary':
|
2560 | return len
|
2561 | case 'utf8':
|
2562 | case 'utf-8':
|
2563 | return utf8ToBytes(string).length
|
2564 | case 'ucs2':
|
2565 | case 'ucs-2':
|
2566 | case 'utf16le':
|
2567 | case 'utf-16le':
|
2568 | return len * 2
|
2569 | case 'hex':
|
2570 | return len >>> 1
|
2571 | case 'base64':
|
2572 | return base64ToBytes(string).length
|
2573 | default:
|
2574 | if (loweredCase) {
|
2575 | return mustMatch ? -1 : utf8ToBytes(string).length
|
2576 | }
|
2577 | encoding = ('' + encoding).toLowerCase()
|
2578 | loweredCase = true
|
2579 | }
|
2580 | }
|
2581 | }
|
2582 | Buffer.byteLength = byteLength
|
2583 |
|
2584 | function slowToString (encoding, start, end) {
|
2585 | var loweredCase = false
|
2586 |
|
2587 |
|
2588 |
|
2589 |
|
2590 |
|
2591 |
|
2592 |
|
2593 |
|
2594 | if (start === undefined || start < 0) {
|
2595 | start = 0
|
2596 | }
|
2597 |
|
2598 |
|
2599 | if (start > this.length) {
|
2600 | return ''
|
2601 | }
|
2602 |
|
2603 | if (end === undefined || end > this.length) {
|
2604 | end = this.length
|
2605 | }
|
2606 |
|
2607 | if (end <= 0) {
|
2608 | return ''
|
2609 | }
|
2610 |
|
2611 |
|
2612 | end >>>= 0
|
2613 | start >>>= 0
|
2614 |
|
2615 | if (end <= start) {
|
2616 | return ''
|
2617 | }
|
2618 |
|
2619 | if (!encoding) encoding = 'utf8'
|
2620 |
|
2621 | while (true) {
|
2622 | switch (encoding) {
|
2623 | case 'hex':
|
2624 | return hexSlice(this, start, end)
|
2625 |
|
2626 | case 'utf8':
|
2627 | case 'utf-8':
|
2628 | return utf8Slice(this, start, end)
|
2629 |
|
2630 | case 'ascii':
|
2631 | return asciiSlice(this, start, end)
|
2632 |
|
2633 | case 'latin1':
|
2634 | case 'binary':
|
2635 | return latin1Slice(this, start, end)
|
2636 |
|
2637 | case 'base64':
|
2638 | return base64Slice(this, start, end)
|
2639 |
|
2640 | case 'ucs2':
|
2641 | case 'ucs-2':
|
2642 | case 'utf16le':
|
2643 | case 'utf-16le':
|
2644 | return utf16leSlice(this, start, end)
|
2645 |
|
2646 | default:
|
2647 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
2648 | encoding = (encoding + '').toLowerCase()
|
2649 | loweredCase = true
|
2650 | }
|
2651 | }
|
2652 | }
|
2653 |
|
2654 |
|
2655 |
|
2656 |
|
2657 |
|
2658 |
|
2659 |
|
2660 | Buffer.prototype._isBuffer = true
|
2661 |
|
2662 | function swap (b, n, m) {
|
2663 | var i = b[n]
|
2664 | b[n] = b[m]
|
2665 | b[m] = i
|
2666 | }
|
2667 |
|
2668 | Buffer.prototype.swap16 = function swap16 () {
|
2669 | var len = this.length
|
2670 | if (len % 2 !== 0) {
|
2671 | throw new RangeError('Buffer size must be a multiple of 16-bits')
|
2672 | }
|
2673 | for (var i = 0; i < len; i += 2) {
|
2674 | swap(this, i, i + 1)
|
2675 | }
|
2676 | return this
|
2677 | }
|
2678 |
|
2679 | Buffer.prototype.swap32 = function swap32 () {
|
2680 | var len = this.length
|
2681 | if (len % 4 !== 0) {
|
2682 | throw new RangeError('Buffer size must be a multiple of 32-bits')
|
2683 | }
|
2684 | for (var i = 0; i < len; i += 4) {
|
2685 | swap(this, i, i + 3)
|
2686 | swap(this, i + 1, i + 2)
|
2687 | }
|
2688 | return this
|
2689 | }
|
2690 |
|
2691 | Buffer.prototype.swap64 = function swap64 () {
|
2692 | var len = this.length
|
2693 | if (len % 8 !== 0) {
|
2694 | throw new RangeError('Buffer size must be a multiple of 64-bits')
|
2695 | }
|
2696 | for (var i = 0; i < len; i += 8) {
|
2697 | swap(this, i, i + 7)
|
2698 | swap(this, i + 1, i + 6)
|
2699 | swap(this, i + 2, i + 5)
|
2700 | swap(this, i + 3, i + 4)
|
2701 | }
|
2702 | return this
|
2703 | }
|
2704 |
|
2705 | Buffer.prototype.toString = function toString () {
|
2706 | var length = this.length
|
2707 | if (length === 0) return ''
|
2708 | if (arguments.length === 0) return utf8Slice(this, 0, length)
|
2709 | return slowToString.apply(this, arguments)
|
2710 | }
|
2711 |
|
2712 | Buffer.prototype.toLocaleString = Buffer.prototype.toString
|
2713 |
|
2714 | Buffer.prototype.equals = function equals (b) {
|
2715 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
|
2716 | if (this === b) return true
|
2717 | return Buffer.compare(this, b) === 0
|
2718 | }
|
2719 |
|
2720 | Buffer.prototype.inspect = function inspect () {
|
2721 | var str = ''
|
2722 | var max = exports.INSPECT_MAX_BYTES
|
2723 | str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
|
2724 | if (this.length > max) str += ' ... '
|
2725 | return '<Buffer ' + str + '>'
|
2726 | }
|
2727 |
|
2728 | Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
|
2729 | if (isInstance(target, Uint8Array)) {
|
2730 | target = Buffer.from(target, target.offset, target.byteLength)
|
2731 | }
|
2732 | if (!Buffer.isBuffer(target)) {
|
2733 | throw new TypeError(
|
2734 | 'The "target" argument must be one of type Buffer or Uint8Array. ' +
|
2735 | 'Received type ' + (typeof target)
|
2736 | )
|
2737 | }
|
2738 |
|
2739 | if (start === undefined) {
|
2740 | start = 0
|
2741 | }
|
2742 | if (end === undefined) {
|
2743 | end = target ? target.length : 0
|
2744 | }
|
2745 | if (thisStart === undefined) {
|
2746 | thisStart = 0
|
2747 | }
|
2748 | if (thisEnd === undefined) {
|
2749 | thisEnd = this.length
|
2750 | }
|
2751 |
|
2752 | if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
|
2753 | throw new RangeError('out of range index')
|
2754 | }
|
2755 |
|
2756 | if (thisStart >= thisEnd && start >= end) {
|
2757 | return 0
|
2758 | }
|
2759 | if (thisStart >= thisEnd) {
|
2760 | return -1
|
2761 | }
|
2762 | if (start >= end) {
|
2763 | return 1
|
2764 | }
|
2765 |
|
2766 | start >>>= 0
|
2767 | end >>>= 0
|
2768 | thisStart >>>= 0
|
2769 | thisEnd >>>= 0
|
2770 |
|
2771 | if (this === target) return 0
|
2772 |
|
2773 | var x = thisEnd - thisStart
|
2774 | var y = end - start
|
2775 | var len = Math.min(x, y)
|
2776 |
|
2777 | var thisCopy = this.slice(thisStart, thisEnd)
|
2778 | var targetCopy = target.slice(start, end)
|
2779 |
|
2780 | for (var i = 0; i < len; ++i) {
|
2781 | if (thisCopy[i] !== targetCopy[i]) {
|
2782 | x = thisCopy[i]
|
2783 | y = targetCopy[i]
|
2784 | break
|
2785 | }
|
2786 | }
|
2787 |
|
2788 | if (x < y) return -1
|
2789 | if (y < x) return 1
|
2790 | return 0
|
2791 | }
|
2792 |
|
2793 |
|
2794 |
|
2795 |
|
2796 |
|
2797 |
|
2798 |
|
2799 |
|
2800 |
|
2801 |
|
2802 | function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
|
2803 |
|
2804 | if (buffer.length === 0) return -1
|
2805 |
|
2806 |
|
2807 | if (typeof byteOffset === 'string') {
|
2808 | encoding = byteOffset
|
2809 | byteOffset = 0
|
2810 | } else if (byteOffset > 0x7fffffff) {
|
2811 | byteOffset = 0x7fffffff
|
2812 | } else if (byteOffset < -0x80000000) {
|
2813 | byteOffset = -0x80000000
|
2814 | }
|
2815 | byteOffset = +byteOffset
|
2816 | if (numberIsNaN(byteOffset)) {
|
2817 |
|
2818 | byteOffset = dir ? 0 : (buffer.length - 1)
|
2819 | }
|
2820 |
|
2821 |
|
2822 | if (byteOffset < 0) byteOffset = buffer.length + byteOffset
|
2823 | if (byteOffset >= buffer.length) {
|
2824 | if (dir) return -1
|
2825 | else byteOffset = buffer.length - 1
|
2826 | } else if (byteOffset < 0) {
|
2827 | if (dir) byteOffset = 0
|
2828 | else return -1
|
2829 | }
|
2830 |
|
2831 |
|
2832 | if (typeof val === 'string') {
|
2833 | val = Buffer.from(val, encoding)
|
2834 | }
|
2835 |
|
2836 |
|
2837 | if (Buffer.isBuffer(val)) {
|
2838 |
|
2839 | if (val.length === 0) {
|
2840 | return -1
|
2841 | }
|
2842 | return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
|
2843 | } else if (typeof val === 'number') {
|
2844 | val = val & 0xFF
|
2845 | if (typeof Uint8Array.prototype.indexOf === 'function') {
|
2846 | if (dir) {
|
2847 | return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
|
2848 | } else {
|
2849 | return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
|
2850 | }
|
2851 | }
|
2852 | return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
|
2853 | }
|
2854 |
|
2855 | throw new TypeError('val must be string, number or Buffer')
|
2856 | }
|
2857 |
|
2858 | function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
|
2859 | var indexSize = 1
|
2860 | var arrLength = arr.length
|
2861 | var valLength = val.length
|
2862 |
|
2863 | if (encoding !== undefined) {
|
2864 | encoding = String(encoding).toLowerCase()
|
2865 | if (encoding === 'ucs2' || encoding === 'ucs-2' ||
|
2866 | encoding === 'utf16le' || encoding === 'utf-16le') {
|
2867 | if (arr.length < 2 || val.length < 2) {
|
2868 | return -1
|
2869 | }
|
2870 | indexSize = 2
|
2871 | arrLength /= 2
|
2872 | valLength /= 2
|
2873 | byteOffset /= 2
|
2874 | }
|
2875 | }
|
2876 |
|
2877 | function read (buf, i) {
|
2878 | if (indexSize === 1) {
|
2879 | return buf[i]
|
2880 | } else {
|
2881 | return buf.readUInt16BE(i * indexSize)
|
2882 | }
|
2883 | }
|
2884 |
|
2885 | var i
|
2886 | if (dir) {
|
2887 | var foundIndex = -1
|
2888 | for (i = byteOffset; i < arrLength; i++) {
|
2889 | if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
|
2890 | if (foundIndex === -1) foundIndex = i
|
2891 | if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
|
2892 | } else {
|
2893 | if (foundIndex !== -1) i -= i - foundIndex
|
2894 | foundIndex = -1
|
2895 | }
|
2896 | }
|
2897 | } else {
|
2898 | if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
|
2899 | for (i = byteOffset; i >= 0; i--) {
|
2900 | var found = true
|
2901 | for (var j = 0; j < valLength; j++) {
|
2902 | if (read(arr, i + j) !== read(val, j)) {
|
2903 | found = false
|
2904 | break
|
2905 | }
|
2906 | }
|
2907 | if (found) return i
|
2908 | }
|
2909 | }
|
2910 |
|
2911 | return -1
|
2912 | }
|
2913 |
|
2914 | Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
|
2915 | return this.indexOf(val, byteOffset, encoding) !== -1
|
2916 | }
|
2917 |
|
2918 | Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
|
2919 | return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
|
2920 | }
|
2921 |
|
2922 | Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
|
2923 | return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
|
2924 | }
|
2925 |
|
2926 | function hexWrite (buf, string, offset, length) {
|
2927 | offset = Number(offset) || 0
|
2928 | var remaining = buf.length - offset
|
2929 | if (!length) {
|
2930 | length = remaining
|
2931 | } else {
|
2932 | length = Number(length)
|
2933 | if (length > remaining) {
|
2934 | length = remaining
|
2935 | }
|
2936 | }
|
2937 |
|
2938 | var strLen = string.length
|
2939 |
|
2940 | if (length > strLen / 2) {
|
2941 | length = strLen / 2
|
2942 | }
|
2943 | for (var i = 0; i < length; ++i) {
|
2944 | var parsed = parseInt(string.substr(i * 2, 2), 16)
|
2945 | if (numberIsNaN(parsed)) return i
|
2946 | buf[offset + i] = parsed
|
2947 | }
|
2948 | return i
|
2949 | }
|
2950 |
|
2951 | function utf8Write (buf, string, offset, length) {
|
2952 | return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
|
2953 | }
|
2954 |
|
2955 | function asciiWrite (buf, string, offset, length) {
|
2956 | return blitBuffer(asciiToBytes(string), buf, offset, length)
|
2957 | }
|
2958 |
|
2959 | function latin1Write (buf, string, offset, length) {
|
2960 | return asciiWrite(buf, string, offset, length)
|
2961 | }
|
2962 |
|
2963 | function base64Write (buf, string, offset, length) {
|
2964 | return blitBuffer(base64ToBytes(string), buf, offset, length)
|
2965 | }
|
2966 |
|
2967 | function ucs2Write (buf, string, offset, length) {
|
2968 | return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
|
2969 | }
|
2970 |
|
2971 | Buffer.prototype.write = function write (string, offset, length, encoding) {
|
2972 |
|
2973 | if (offset === undefined) {
|
2974 | encoding = 'utf8'
|
2975 | length = this.length
|
2976 | offset = 0
|
2977 |
|
2978 | } else if (length === undefined && typeof offset === 'string') {
|
2979 | encoding = offset
|
2980 | length = this.length
|
2981 | offset = 0
|
2982 |
|
2983 | } else if (isFinite(offset)) {
|
2984 | offset = offset >>> 0
|
2985 | if (isFinite(length)) {
|
2986 | length = length >>> 0
|
2987 | if (encoding === undefined) encoding = 'utf8'
|
2988 | } else {
|
2989 | encoding = length
|
2990 | length = undefined
|
2991 | }
|
2992 | } else {
|
2993 | throw new Error(
|
2994 | 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
|
2995 | )
|
2996 | }
|
2997 |
|
2998 | var remaining = this.length - offset
|
2999 | if (length === undefined || length > remaining) length = remaining
|
3000 |
|
3001 | if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
|
3002 | throw new RangeError('Attempt to write outside buffer bounds')
|
3003 | }
|
3004 |
|
3005 | if (!encoding) encoding = 'utf8'
|
3006 |
|
3007 | var loweredCase = false
|
3008 | for (;;) {
|
3009 | switch (encoding) {
|
3010 | case 'hex':
|
3011 | return hexWrite(this, string, offset, length)
|
3012 |
|
3013 | case 'utf8':
|
3014 | case 'utf-8':
|
3015 | return utf8Write(this, string, offset, length)
|
3016 |
|
3017 | case 'ascii':
|
3018 | return asciiWrite(this, string, offset, length)
|
3019 |
|
3020 | case 'latin1':
|
3021 | case 'binary':
|
3022 | return latin1Write(this, string, offset, length)
|
3023 |
|
3024 | case 'base64':
|
3025 |
|
3026 | return base64Write(this, string, offset, length)
|
3027 |
|
3028 | case 'ucs2':
|
3029 | case 'ucs-2':
|
3030 | case 'utf16le':
|
3031 | case 'utf-16le':
|
3032 | return ucs2Write(this, string, offset, length)
|
3033 |
|
3034 | default:
|
3035 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
3036 | encoding = ('' + encoding).toLowerCase()
|
3037 | loweredCase = true
|
3038 | }
|
3039 | }
|
3040 | }
|
3041 |
|
3042 | Buffer.prototype.toJSON = function toJSON () {
|
3043 | return {
|
3044 | type: 'Buffer',
|
3045 | data: Array.prototype.slice.call(this._arr || this, 0)
|
3046 | }
|
3047 | }
|
3048 |
|
3049 | function base64Slice (buf, start, end) {
|
3050 | if (start === 0 && end === buf.length) {
|
3051 | return base64.fromByteArray(buf)
|
3052 | } else {
|
3053 | return base64.fromByteArray(buf.slice(start, end))
|
3054 | }
|
3055 | }
|
3056 |
|
3057 | function utf8Slice (buf, start, end) {
|
3058 | end = Math.min(buf.length, end)
|
3059 | var res = []
|
3060 |
|
3061 | var i = start
|
3062 | while (i < end) {
|
3063 | var firstByte = buf[i]
|
3064 | var codePoint = null
|
3065 | var bytesPerSequence = (firstByte > 0xEF) ? 4
|
3066 | : (firstByte > 0xDF) ? 3
|
3067 | : (firstByte > 0xBF) ? 2
|
3068 | : 1
|
3069 |
|
3070 | if (i + bytesPerSequence <= end) {
|
3071 | var secondByte, thirdByte, fourthByte, tempCodePoint
|
3072 |
|
3073 | switch (bytesPerSequence) {
|
3074 | case 1:
|
3075 | if (firstByte < 0x80) {
|
3076 | codePoint = firstByte
|
3077 | }
|
3078 | break
|
3079 | case 2:
|
3080 | secondByte = buf[i + 1]
|
3081 | if ((secondByte & 0xC0) === 0x80) {
|
3082 | tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
|
3083 | if (tempCodePoint > 0x7F) {
|
3084 | codePoint = tempCodePoint
|
3085 | }
|
3086 | }
|
3087 | break
|
3088 | case 3:
|
3089 | secondByte = buf[i + 1]
|
3090 | thirdByte = buf[i + 2]
|
3091 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
|
3092 | tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
|
3093 | if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
|
3094 | codePoint = tempCodePoint
|
3095 | }
|
3096 | }
|
3097 | break
|
3098 | case 4:
|
3099 | secondByte = buf[i + 1]
|
3100 | thirdByte = buf[i + 2]
|
3101 | fourthByte = buf[i + 3]
|
3102 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
|
3103 | tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
|
3104 | if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
|
3105 | codePoint = tempCodePoint
|
3106 | }
|
3107 | }
|
3108 | }
|
3109 | }
|
3110 |
|
3111 | if (codePoint === null) {
|
3112 |
|
3113 |
|
3114 | codePoint = 0xFFFD
|
3115 | bytesPerSequence = 1
|
3116 | } else if (codePoint > 0xFFFF) {
|
3117 |
|
3118 | codePoint -= 0x10000
|
3119 | res.push(codePoint >>> 10 & 0x3FF | 0xD800)
|
3120 | codePoint = 0xDC00 | codePoint & 0x3FF
|
3121 | }
|
3122 |
|
3123 | res.push(codePoint)
|
3124 | i += bytesPerSequence
|
3125 | }
|
3126 |
|
3127 | return decodeCodePointsArray(res)
|
3128 | }
|
3129 |
|
3130 |
|
3131 |
|
3132 |
|
3133 | var MAX_ARGUMENTS_LENGTH = 0x1000
|
3134 |
|
3135 | function decodeCodePointsArray (codePoints) {
|
3136 | var len = codePoints.length
|
3137 | if (len <= MAX_ARGUMENTS_LENGTH) {
|
3138 | return String.fromCharCode.apply(String, codePoints)
|
3139 | }
|
3140 |
|
3141 |
|
3142 | var res = ''
|
3143 | var i = 0
|
3144 | while (i < len) {
|
3145 | res += String.fromCharCode.apply(
|
3146 | String,
|
3147 | codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
|
3148 | )
|
3149 | }
|
3150 | return res
|
3151 | }
|
3152 |
|
3153 | function asciiSlice (buf, start, end) {
|
3154 | var ret = ''
|
3155 | end = Math.min(buf.length, end)
|
3156 |
|
3157 | for (var i = start; i < end; ++i) {
|
3158 | ret += String.fromCharCode(buf[i] & 0x7F)
|
3159 | }
|
3160 | return ret
|
3161 | }
|
3162 |
|
3163 | function latin1Slice (buf, start, end) {
|
3164 | var ret = ''
|
3165 | end = Math.min(buf.length, end)
|
3166 |
|
3167 | for (var i = start; i < end; ++i) {
|
3168 | ret += String.fromCharCode(buf[i])
|
3169 | }
|
3170 | return ret
|
3171 | }
|
3172 |
|
3173 | function hexSlice (buf, start, end) {
|
3174 | var len = buf.length
|
3175 |
|
3176 | if (!start || start < 0) start = 0
|
3177 | if (!end || end < 0 || end > len) end = len
|
3178 |
|
3179 | var out = ''
|
3180 | for (var i = start; i < end; ++i) {
|
3181 | out += toHex(buf[i])
|
3182 | }
|
3183 | return out
|
3184 | }
|
3185 |
|
3186 | function utf16leSlice (buf, start, end) {
|
3187 | var bytes = buf.slice(start, end)
|
3188 | var res = ''
|
3189 | for (var i = 0; i < bytes.length; i += 2) {
|
3190 | res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
|
3191 | }
|
3192 | return res
|
3193 | }
|
3194 |
|
3195 | Buffer.prototype.slice = function slice (start, end) {
|
3196 | var len = this.length
|
3197 | start = ~~start
|
3198 | end = end === undefined ? len : ~~end
|
3199 |
|
3200 | if (start < 0) {
|
3201 | start += len
|
3202 | if (start < 0) start = 0
|
3203 | } else if (start > len) {
|
3204 | start = len
|
3205 | }
|
3206 |
|
3207 | if (end < 0) {
|
3208 | end += len
|
3209 | if (end < 0) end = 0
|
3210 | } else if (end > len) {
|
3211 | end = len
|
3212 | }
|
3213 |
|
3214 | if (end < start) end = start
|
3215 |
|
3216 | var newBuf = this.subarray(start, end)
|
3217 |
|
3218 | newBuf.__proto__ = Buffer.prototype
|
3219 | return newBuf
|
3220 | }
|
3221 |
|
3222 |
|
3223 |
|
3224 |
|
3225 | function checkOffset (offset, ext, length) {
|
3226 | if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
|
3227 | if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
|
3228 | }
|
3229 |
|
3230 | Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
|
3231 | offset = offset >>> 0
|
3232 | byteLength = byteLength >>> 0
|
3233 | if (!noAssert) checkOffset(offset, byteLength, this.length)
|
3234 |
|
3235 | var val = this[offset]
|
3236 | var mul = 1
|
3237 | var i = 0
|
3238 | while (++i < byteLength && (mul *= 0x100)) {
|
3239 | val += this[offset + i] * mul
|
3240 | }
|
3241 |
|
3242 | return val
|
3243 | }
|
3244 |
|
3245 | Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
|
3246 | offset = offset >>> 0
|
3247 | byteLength = byteLength >>> 0
|
3248 | if (!noAssert) {
|
3249 | checkOffset(offset, byteLength, this.length)
|
3250 | }
|
3251 |
|
3252 | var val = this[offset + --byteLength]
|
3253 | var mul = 1
|
3254 | while (byteLength > 0 && (mul *= 0x100)) {
|
3255 | val += this[offset + --byteLength] * mul
|
3256 | }
|
3257 |
|
3258 | return val
|
3259 | }
|
3260 |
|
3261 | Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
|
3262 | offset = offset >>> 0
|
3263 | if (!noAssert) checkOffset(offset, 1, this.length)
|
3264 | return this[offset]
|
3265 | }
|
3266 |
|
3267 | Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
|
3268 | offset = offset >>> 0
|
3269 | if (!noAssert) checkOffset(offset, 2, this.length)
|
3270 | return this[offset] | (this[offset + 1] << 8)
|
3271 | }
|
3272 |
|
3273 | Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
|
3274 | offset = offset >>> 0
|
3275 | if (!noAssert) checkOffset(offset, 2, this.length)
|
3276 | return (this[offset] << 8) | this[offset + 1]
|
3277 | }
|
3278 |
|
3279 | Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
|
3280 | offset = offset >>> 0
|
3281 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3282 |
|
3283 | return ((this[offset]) |
|
3284 | (this[offset + 1] << 8) |
|
3285 | (this[offset + 2] << 16)) +
|
3286 | (this[offset + 3] * 0x1000000)
|
3287 | }
|
3288 |
|
3289 | Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
|
3290 | offset = offset >>> 0
|
3291 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3292 |
|
3293 | return (this[offset] * 0x1000000) +
|
3294 | ((this[offset + 1] << 16) |
|
3295 | (this[offset + 2] << 8) |
|
3296 | this[offset + 3])
|
3297 | }
|
3298 |
|
3299 | Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
|
3300 | offset = offset >>> 0
|
3301 | byteLength = byteLength >>> 0
|
3302 | if (!noAssert) checkOffset(offset, byteLength, this.length)
|
3303 |
|
3304 | var val = this[offset]
|
3305 | var mul = 1
|
3306 | var i = 0
|
3307 | while (++i < byteLength && (mul *= 0x100)) {
|
3308 | val += this[offset + i] * mul
|
3309 | }
|
3310 | mul *= 0x80
|
3311 |
|
3312 | if (val >= mul) val -= Math.pow(2, 8 * byteLength)
|
3313 |
|
3314 | return val
|
3315 | }
|
3316 |
|
3317 | Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
|
3318 | offset = offset >>> 0
|
3319 | byteLength = byteLength >>> 0
|
3320 | if (!noAssert) checkOffset(offset, byteLength, this.length)
|
3321 |
|
3322 | var i = byteLength
|
3323 | var mul = 1
|
3324 | var val = this[offset + --i]
|
3325 | while (i > 0 && (mul *= 0x100)) {
|
3326 | val += this[offset + --i] * mul
|
3327 | }
|
3328 | mul *= 0x80
|
3329 |
|
3330 | if (val >= mul) val -= Math.pow(2, 8 * byteLength)
|
3331 |
|
3332 | return val
|
3333 | }
|
3334 |
|
3335 | Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
|
3336 | offset = offset >>> 0
|
3337 | if (!noAssert) checkOffset(offset, 1, this.length)
|
3338 | if (!(this[offset] & 0x80)) return (this[offset])
|
3339 | return ((0xff - this[offset] + 1) * -1)
|
3340 | }
|
3341 |
|
3342 | Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
|
3343 | offset = offset >>> 0
|
3344 | if (!noAssert) checkOffset(offset, 2, this.length)
|
3345 | var val = this[offset] | (this[offset + 1] << 8)
|
3346 | return (val & 0x8000) ? val | 0xFFFF0000 : val
|
3347 | }
|
3348 |
|
3349 | Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
|
3350 | offset = offset >>> 0
|
3351 | if (!noAssert) checkOffset(offset, 2, this.length)
|
3352 | var val = this[offset + 1] | (this[offset] << 8)
|
3353 | return (val & 0x8000) ? val | 0xFFFF0000 : val
|
3354 | }
|
3355 |
|
3356 | Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
|
3357 | offset = offset >>> 0
|
3358 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3359 |
|
3360 | return (this[offset]) |
|
3361 | (this[offset + 1] << 8) |
|
3362 | (this[offset + 2] << 16) |
|
3363 | (this[offset + 3] << 24)
|
3364 | }
|
3365 |
|
3366 | Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
|
3367 | offset = offset >>> 0
|
3368 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3369 |
|
3370 | return (this[offset] << 24) |
|
3371 | (this[offset + 1] << 16) |
|
3372 | (this[offset + 2] << 8) |
|
3373 | (this[offset + 3])
|
3374 | }
|
3375 |
|
3376 | Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
|
3377 | offset = offset >>> 0
|
3378 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3379 | return ieee754.read(this, offset, true, 23, 4)
|
3380 | }
|
3381 |
|
3382 | Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
|
3383 | offset = offset >>> 0
|
3384 | if (!noAssert) checkOffset(offset, 4, this.length)
|
3385 | return ieee754.read(this, offset, false, 23, 4)
|
3386 | }
|
3387 |
|
3388 | Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
|
3389 | offset = offset >>> 0
|
3390 | if (!noAssert) checkOffset(offset, 8, this.length)
|
3391 | return ieee754.read(this, offset, true, 52, 8)
|
3392 | }
|
3393 |
|
3394 | Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
|
3395 | offset = offset >>> 0
|
3396 | if (!noAssert) checkOffset(offset, 8, this.length)
|
3397 | return ieee754.read(this, offset, false, 52, 8)
|
3398 | }
|
3399 |
|
3400 | function checkInt (buf, value, offset, ext, max, min) {
|
3401 | if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
|
3402 | if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
|
3403 | if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
3404 | }
|
3405 |
|
3406 | Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
|
3407 | value = +value
|
3408 | offset = offset >>> 0
|
3409 | byteLength = byteLength >>> 0
|
3410 | if (!noAssert) {
|
3411 | var maxBytes = Math.pow(2, 8 * byteLength) - 1
|
3412 | checkInt(this, value, offset, byteLength, maxBytes, 0)
|
3413 | }
|
3414 |
|
3415 | var mul = 1
|
3416 | var i = 0
|
3417 | this[offset] = value & 0xFF
|
3418 | while (++i < byteLength && (mul *= 0x100)) {
|
3419 | this[offset + i] = (value / mul) & 0xFF
|
3420 | }
|
3421 |
|
3422 | return offset + byteLength
|
3423 | }
|
3424 |
|
3425 | Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
|
3426 | value = +value
|
3427 | offset = offset >>> 0
|
3428 | byteLength = byteLength >>> 0
|
3429 | if (!noAssert) {
|
3430 | var maxBytes = Math.pow(2, 8 * byteLength) - 1
|
3431 | checkInt(this, value, offset, byteLength, maxBytes, 0)
|
3432 | }
|
3433 |
|
3434 | var i = byteLength - 1
|
3435 | var mul = 1
|
3436 | this[offset + i] = value & 0xFF
|
3437 | while (--i >= 0 && (mul *= 0x100)) {
|
3438 | this[offset + i] = (value / mul) & 0xFF
|
3439 | }
|
3440 |
|
3441 | return offset + byteLength
|
3442 | }
|
3443 |
|
3444 | Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
|
3445 | value = +value
|
3446 | offset = offset >>> 0
|
3447 | if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
|
3448 | this[offset] = (value & 0xff)
|
3449 | return offset + 1
|
3450 | }
|
3451 |
|
3452 | Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
|
3453 | value = +value
|
3454 | offset = offset >>> 0
|
3455 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
|
3456 | this[offset] = (value & 0xff)
|
3457 | this[offset + 1] = (value >>> 8)
|
3458 | return offset + 2
|
3459 | }
|
3460 |
|
3461 | Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
|
3462 | value = +value
|
3463 | offset = offset >>> 0
|
3464 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
|
3465 | this[offset] = (value >>> 8)
|
3466 | this[offset + 1] = (value & 0xff)
|
3467 | return offset + 2
|
3468 | }
|
3469 |
|
3470 | Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
|
3471 | value = +value
|
3472 | offset = offset >>> 0
|
3473 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
|
3474 | this[offset + 3] = (value >>> 24)
|
3475 | this[offset + 2] = (value >>> 16)
|
3476 | this[offset + 1] = (value >>> 8)
|
3477 | this[offset] = (value & 0xff)
|
3478 | return offset + 4
|
3479 | }
|
3480 |
|
3481 | Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
|
3482 | value = +value
|
3483 | offset = offset >>> 0
|
3484 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
|
3485 | this[offset] = (value >>> 24)
|
3486 | this[offset + 1] = (value >>> 16)
|
3487 | this[offset + 2] = (value >>> 8)
|
3488 | this[offset + 3] = (value & 0xff)
|
3489 | return offset + 4
|
3490 | }
|
3491 |
|
3492 | Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
|
3493 | value = +value
|
3494 | offset = offset >>> 0
|
3495 | if (!noAssert) {
|
3496 | var limit = Math.pow(2, (8 * byteLength) - 1)
|
3497 |
|
3498 | checkInt(this, value, offset, byteLength, limit - 1, -limit)
|
3499 | }
|
3500 |
|
3501 | var i = 0
|
3502 | var mul = 1
|
3503 | var sub = 0
|
3504 | this[offset] = value & 0xFF
|
3505 | while (++i < byteLength && (mul *= 0x100)) {
|
3506 | if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
|
3507 | sub = 1
|
3508 | }
|
3509 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
|
3510 | }
|
3511 |
|
3512 | return offset + byteLength
|
3513 | }
|
3514 |
|
3515 | Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
|
3516 | value = +value
|
3517 | offset = offset >>> 0
|
3518 | if (!noAssert) {
|
3519 | var limit = Math.pow(2, (8 * byteLength) - 1)
|
3520 |
|
3521 | checkInt(this, value, offset, byteLength, limit - 1, -limit)
|
3522 | }
|
3523 |
|
3524 | var i = byteLength - 1
|
3525 | var mul = 1
|
3526 | var sub = 0
|
3527 | this[offset + i] = value & 0xFF
|
3528 | while (--i >= 0 && (mul *= 0x100)) {
|
3529 | if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
|
3530 | sub = 1
|
3531 | }
|
3532 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
|
3533 | }
|
3534 |
|
3535 | return offset + byteLength
|
3536 | }
|
3537 |
|
3538 | Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
|
3539 | value = +value
|
3540 | offset = offset >>> 0
|
3541 | if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
|
3542 | if (value < 0) value = 0xff + value + 1
|
3543 | this[offset] = (value & 0xff)
|
3544 | return offset + 1
|
3545 | }
|
3546 |
|
3547 | Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
|
3548 | value = +value
|
3549 | offset = offset >>> 0
|
3550 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
|
3551 | this[offset] = (value & 0xff)
|
3552 | this[offset + 1] = (value >>> 8)
|
3553 | return offset + 2
|
3554 | }
|
3555 |
|
3556 | Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
|
3557 | value = +value
|
3558 | offset = offset >>> 0
|
3559 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
|
3560 | this[offset] = (value >>> 8)
|
3561 | this[offset + 1] = (value & 0xff)
|
3562 | return offset + 2
|
3563 | }
|
3564 |
|
3565 | Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
|
3566 | value = +value
|
3567 | offset = offset >>> 0
|
3568 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
|
3569 | this[offset] = (value & 0xff)
|
3570 | this[offset + 1] = (value >>> 8)
|
3571 | this[offset + 2] = (value >>> 16)
|
3572 | this[offset + 3] = (value >>> 24)
|
3573 | return offset + 4
|
3574 | }
|
3575 |
|
3576 | Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
|
3577 | value = +value
|
3578 | offset = offset >>> 0
|
3579 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
|
3580 | if (value < 0) value = 0xffffffff + value + 1
|
3581 | this[offset] = (value >>> 24)
|
3582 | this[offset + 1] = (value >>> 16)
|
3583 | this[offset + 2] = (value >>> 8)
|
3584 | this[offset + 3] = (value & 0xff)
|
3585 | return offset + 4
|
3586 | }
|
3587 |
|
3588 | function checkIEEE754 (buf, value, offset, ext, max, min) {
|
3589 | if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
3590 | if (offset < 0) throw new RangeError('Index out of range')
|
3591 | }
|
3592 |
|
3593 | function writeFloat (buf, value, offset, littleEndian, noAssert) {
|
3594 | value = +value
|
3595 | offset = offset >>> 0
|
3596 | if (!noAssert) {
|
3597 | checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
|
3598 | }
|
3599 | ieee754.write(buf, value, offset, littleEndian, 23, 4)
|
3600 | return offset + 4
|
3601 | }
|
3602 |
|
3603 | Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
|
3604 | return writeFloat(this, value, offset, true, noAssert)
|
3605 | }
|
3606 |
|
3607 | Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
|
3608 | return writeFloat(this, value, offset, false, noAssert)
|
3609 | }
|
3610 |
|
3611 | function writeDouble (buf, value, offset, littleEndian, noAssert) {
|
3612 | value = +value
|
3613 | offset = offset >>> 0
|
3614 | if (!noAssert) {
|
3615 | checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
|
3616 | }
|
3617 | ieee754.write(buf, value, offset, littleEndian, 52, 8)
|
3618 | return offset + 8
|
3619 | }
|
3620 |
|
3621 | Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
|
3622 | return writeDouble(this, value, offset, true, noAssert)
|
3623 | }
|
3624 |
|
3625 | Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
|
3626 | return writeDouble(this, value, offset, false, noAssert)
|
3627 | }
|
3628 |
|
3629 |
|
3630 | Buffer.prototype.copy = function copy (target, targetStart, start, end) {
|
3631 | if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
|
3632 | if (!start) start = 0
|
3633 | if (!end && end !== 0) end = this.length
|
3634 | if (targetStart >= target.length) targetStart = target.length
|
3635 | if (!targetStart) targetStart = 0
|
3636 | if (end > 0 && end < start) end = start
|
3637 |
|
3638 |
|
3639 | if (end === start) return 0
|
3640 | if (target.length === 0 || this.length === 0) return 0
|
3641 |
|
3642 |
|
3643 | if (targetStart < 0) {
|
3644 | throw new RangeError('targetStart out of bounds')
|
3645 | }
|
3646 | if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
|
3647 | if (end < 0) throw new RangeError('sourceEnd out of bounds')
|
3648 |
|
3649 |
|
3650 | if (end > this.length) end = this.length
|
3651 | if (target.length - targetStart < end - start) {
|
3652 | end = target.length - targetStart + start
|
3653 | }
|
3654 |
|
3655 | var len = end - start
|
3656 |
|
3657 | if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
|
3658 |
|
3659 | this.copyWithin(targetStart, start, end)
|
3660 | } else if (this === target && start < targetStart && targetStart < end) {
|
3661 |
|
3662 | for (var i = len - 1; i >= 0; --i) {
|
3663 | target[i + targetStart] = this[i + start]
|
3664 | }
|
3665 | } else {
|
3666 | Uint8Array.prototype.set.call(
|
3667 | target,
|
3668 | this.subarray(start, end),
|
3669 | targetStart
|
3670 | )
|
3671 | }
|
3672 |
|
3673 | return len
|
3674 | }
|
3675 |
|
3676 |
|
3677 |
|
3678 |
|
3679 |
|
3680 | Buffer.prototype.fill = function fill (val, start, end, encoding) {
|
3681 |
|
3682 | if (typeof val === 'string') {
|
3683 | if (typeof start === 'string') {
|
3684 | encoding = start
|
3685 | start = 0
|
3686 | end = this.length
|
3687 | } else if (typeof end === 'string') {
|
3688 | encoding = end
|
3689 | end = this.length
|
3690 | }
|
3691 | if (encoding !== undefined && typeof encoding !== 'string') {
|
3692 | throw new TypeError('encoding must be a string')
|
3693 | }
|
3694 | if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
|
3695 | throw new TypeError('Unknown encoding: ' + encoding)
|
3696 | }
|
3697 | if (val.length === 1) {
|
3698 | var code = val.charCodeAt(0)
|
3699 | if ((encoding === 'utf8' && code < 128) ||
|
3700 | encoding === 'latin1') {
|
3701 |
|
3702 | val = code
|
3703 | }
|
3704 | }
|
3705 | } else if (typeof val === 'number') {
|
3706 | val = val & 255
|
3707 | }
|
3708 |
|
3709 |
|
3710 | if (start < 0 || this.length < start || this.length < end) {
|
3711 | throw new RangeError('Out of range index')
|
3712 | }
|
3713 |
|
3714 | if (end <= start) {
|
3715 | return this
|
3716 | }
|
3717 |
|
3718 | start = start >>> 0
|
3719 | end = end === undefined ? this.length : end >>> 0
|
3720 |
|
3721 | if (!val) val = 0
|
3722 |
|
3723 | var i
|
3724 | if (typeof val === 'number') {
|
3725 | for (i = start; i < end; ++i) {
|
3726 | this[i] = val
|
3727 | }
|
3728 | } else {
|
3729 | var bytes = Buffer.isBuffer(val)
|
3730 | ? val
|
3731 | : Buffer.from(val, encoding)
|
3732 | var len = bytes.length
|
3733 | if (len === 0) {
|
3734 | throw new TypeError('The value "' + val +
|
3735 | '" is invalid for argument "value"')
|
3736 | }
|
3737 | for (i = 0; i < end - start; ++i) {
|
3738 | this[i + start] = bytes[i % len]
|
3739 | }
|
3740 | }
|
3741 |
|
3742 | return this
|
3743 | }
|
3744 |
|
3745 |
|
3746 |
|
3747 |
|
3748 | var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
|
3749 |
|
3750 | function base64clean (str) {
|
3751 |
|
3752 | str = str.split('=')[0]
|
3753 |
|
3754 | str = str.trim().replace(INVALID_BASE64_RE, '')
|
3755 |
|
3756 | if (str.length < 2) return ''
|
3757 |
|
3758 | while (str.length % 4 !== 0) {
|
3759 | str = str + '='
|
3760 | }
|
3761 | return str
|
3762 | }
|
3763 |
|
3764 | function toHex (n) {
|
3765 | if (n < 16) return '0' + n.toString(16)
|
3766 | return n.toString(16)
|
3767 | }
|
3768 |
|
3769 | function utf8ToBytes (string, units) {
|
3770 | units = units || Infinity
|
3771 | var codePoint
|
3772 | var length = string.length
|
3773 | var leadSurrogate = null
|
3774 | var bytes = []
|
3775 |
|
3776 | for (var i = 0; i < length; ++i) {
|
3777 | codePoint = string.charCodeAt(i)
|
3778 |
|
3779 |
|
3780 | if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
3781 |
|
3782 | if (!leadSurrogate) {
|
3783 |
|
3784 | if (codePoint > 0xDBFF) {
|
3785 |
|
3786 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
3787 | continue
|
3788 | } else if (i + 1 === length) {
|
3789 |
|
3790 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
3791 | continue
|
3792 | }
|
3793 |
|
3794 |
|
3795 | leadSurrogate = codePoint
|
3796 |
|
3797 | continue
|
3798 | }
|
3799 |
|
3800 |
|
3801 | if (codePoint < 0xDC00) {
|
3802 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
3803 | leadSurrogate = codePoint
|
3804 | continue
|
3805 | }
|
3806 |
|
3807 |
|
3808 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
|
3809 | } else if (leadSurrogate) {
|
3810 |
|
3811 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
3812 | }
|
3813 |
|
3814 | leadSurrogate = null
|
3815 |
|
3816 |
|
3817 | if (codePoint < 0x80) {
|
3818 | if ((units -= 1) < 0) break
|
3819 | bytes.push(codePoint)
|
3820 | } else if (codePoint < 0x800) {
|
3821 | if ((units -= 2) < 0) break
|
3822 | bytes.push(
|
3823 | codePoint >> 0x6 | 0xC0,
|
3824 | codePoint & 0x3F | 0x80
|
3825 | )
|
3826 | } else if (codePoint < 0x10000) {
|
3827 | if ((units -= 3) < 0) break
|
3828 | bytes.push(
|
3829 | codePoint >> 0xC | 0xE0,
|
3830 | codePoint >> 0x6 & 0x3F | 0x80,
|
3831 | codePoint & 0x3F | 0x80
|
3832 | )
|
3833 | } else if (codePoint < 0x110000) {
|
3834 | if ((units -= 4) < 0) break
|
3835 | bytes.push(
|
3836 | codePoint >> 0x12 | 0xF0,
|
3837 | codePoint >> 0xC & 0x3F | 0x80,
|
3838 | codePoint >> 0x6 & 0x3F | 0x80,
|
3839 | codePoint & 0x3F | 0x80
|
3840 | )
|
3841 | } else {
|
3842 | throw new Error('Invalid code point')
|
3843 | }
|
3844 | }
|
3845 |
|
3846 | return bytes
|
3847 | }
|
3848 |
|
3849 | function asciiToBytes (str) {
|
3850 | var byteArray = []
|
3851 | for (var i = 0; i < str.length; ++i) {
|
3852 |
|
3853 | byteArray.push(str.charCodeAt(i) & 0xFF)
|
3854 | }
|
3855 | return byteArray
|
3856 | }
|
3857 |
|
3858 | function utf16leToBytes (str, units) {
|
3859 | var c, hi, lo
|
3860 | var byteArray = []
|
3861 | for (var i = 0; i < str.length; ++i) {
|
3862 | if ((units -= 2) < 0) break
|
3863 |
|
3864 | c = str.charCodeAt(i)
|
3865 | hi = c >> 8
|
3866 | lo = c % 256
|
3867 | byteArray.push(lo)
|
3868 | byteArray.push(hi)
|
3869 | }
|
3870 |
|
3871 | return byteArray
|
3872 | }
|
3873 |
|
3874 | function base64ToBytes (str) {
|
3875 | return base64.toByteArray(base64clean(str))
|
3876 | }
|
3877 |
|
3878 | function blitBuffer (src, dst, offset, length) {
|
3879 | for (var i = 0; i < length; ++i) {
|
3880 | if ((i + offset >= dst.length) || (i >= src.length)) break
|
3881 | dst[i + offset] = src[i]
|
3882 | }
|
3883 | return i
|
3884 | }
|
3885 |
|
3886 |
|
3887 |
|
3888 |
|
3889 | function isInstance (obj, type) {
|
3890 | return obj instanceof type ||
|
3891 | (obj != null && obj.constructor != null && obj.constructor.name != null &&
|
3892 | obj.constructor.name === type.name)
|
3893 | }
|
3894 | function numberIsNaN (obj) {
|
3895 |
|
3896 | return obj !== obj
|
3897 | }
|
3898 |
|
3899 | },{"base64-js":9,"ieee754":13}],11:[function(_dereq_,module,exports){
|
3900 | (function (Buffer){
|
3901 | var clone = (function() {
|
3902 | 'use strict';
|
3903 |
|
3904 | function _instanceof(obj, type) {
|
3905 | return type != null && obj instanceof type;
|
3906 | }
|
3907 |
|
3908 | var nativeMap;
|
3909 | try {
|
3910 | nativeMap = Map;
|
3911 | } catch(_) {
|
3912 |
|
3913 |
|
3914 | nativeMap = function() {};
|
3915 | }
|
3916 |
|
3917 | var nativeSet;
|
3918 | try {
|
3919 | nativeSet = Set;
|
3920 | } catch(_) {
|
3921 | nativeSet = function() {};
|
3922 | }
|
3923 |
|
3924 | var nativePromise;
|
3925 | try {
|
3926 | nativePromise = Promise;
|
3927 | } catch(_) {
|
3928 | nativePromise = function() {};
|
3929 | }
|
3930 |
|
3931 |
|
3932 |
|
3933 |
|
3934 |
|
3935 |
|
3936 |
|
3937 |
|
3938 |
|
3939 |
|
3940 |
|
3941 |
|
3942 |
|
3943 |
|
3944 |
|
3945 |
|
3946 |
|
3947 |
|
3948 |
|
3949 |
|
3950 |
|
3951 |
|
3952 | function clone(parent, circular, depth, prototype, includeNonEnumerable) {
|
3953 | if (typeof circular === 'object') {
|
3954 | depth = circular.depth;
|
3955 | prototype = circular.prototype;
|
3956 | includeNonEnumerable = circular.includeNonEnumerable;
|
3957 | circular = circular.circular;
|
3958 | }
|
3959 |
|
3960 |
|
3961 | var allParents = [];
|
3962 | var allChildren = [];
|
3963 |
|
3964 | var useBuffer = typeof Buffer != 'undefined';
|
3965 |
|
3966 | if (typeof circular == 'undefined')
|
3967 | circular = true;
|
3968 |
|
3969 | if (typeof depth == 'undefined')
|
3970 | depth = Infinity;
|
3971 |
|
3972 |
|
3973 | function _clone(parent, depth) {
|
3974 |
|
3975 | if (parent === null)
|
3976 | return null;
|
3977 |
|
3978 | if (depth === 0)
|
3979 | return parent;
|
3980 |
|
3981 | var child;
|
3982 | var proto;
|
3983 | if (typeof parent != 'object') {
|
3984 | return parent;
|
3985 | }
|
3986 |
|
3987 | if (_instanceof(parent, nativeMap)) {
|
3988 | child = new nativeMap();
|
3989 | } else if (_instanceof(parent, nativeSet)) {
|
3990 | child = new nativeSet();
|
3991 | } else if (_instanceof(parent, nativePromise)) {
|
3992 | child = new nativePromise(function (resolve, reject) {
|
3993 | parent.then(function(value) {
|
3994 | resolve(_clone(value, depth - 1));
|
3995 | }, function(err) {
|
3996 | reject(_clone(err, depth - 1));
|
3997 | });
|
3998 | });
|
3999 | } else if (clone.__isArray(parent)) {
|
4000 | child = [];
|
4001 | } else if (clone.__isRegExp(parent)) {
|
4002 | child = new RegExp(parent.source, __getRegExpFlags(parent));
|
4003 | if (parent.lastIndex) child.lastIndex = parent.lastIndex;
|
4004 | } else if (clone.__isDate(parent)) {
|
4005 | child = new Date(parent.getTime());
|
4006 | } else if (useBuffer && Buffer.isBuffer(parent)) {
|
4007 | child = new Buffer(parent.length);
|
4008 | parent.copy(child);
|
4009 | return child;
|
4010 | } else if (_instanceof(parent, Error)) {
|
4011 | child = Object.create(parent);
|
4012 | } else {
|
4013 | if (typeof prototype == 'undefined') {
|
4014 | proto = Object.getPrototypeOf(parent);
|
4015 | child = Object.create(proto);
|
4016 | }
|
4017 | else {
|
4018 | child = Object.create(prototype);
|
4019 | proto = prototype;
|
4020 | }
|
4021 | }
|
4022 |
|
4023 | if (circular) {
|
4024 | var index = allParents.indexOf(parent);
|
4025 |
|
4026 | if (index != -1) {
|
4027 | return allChildren[index];
|
4028 | }
|
4029 | allParents.push(parent);
|
4030 | allChildren.push(child);
|
4031 | }
|
4032 |
|
4033 | if (_instanceof(parent, nativeMap)) {
|
4034 | parent.forEach(function(value, key) {
|
4035 | var keyChild = _clone(key, depth - 1);
|
4036 | var valueChild = _clone(value, depth - 1);
|
4037 | child.set(keyChild, valueChild);
|
4038 | });
|
4039 | }
|
4040 | if (_instanceof(parent, nativeSet)) {
|
4041 | parent.forEach(function(value) {
|
4042 | var entryChild = _clone(value, depth - 1);
|
4043 | child.add(entryChild);
|
4044 | });
|
4045 | }
|
4046 |
|
4047 | for (var i in parent) {
|
4048 | var attrs;
|
4049 | if (proto) {
|
4050 | attrs = Object.getOwnPropertyDescriptor(proto, i);
|
4051 | }
|
4052 |
|
4053 | if (attrs && attrs.set == null) {
|
4054 | continue;
|
4055 | }
|
4056 | child[i] = _clone(parent[i], depth - 1);
|
4057 | }
|
4058 |
|
4059 | if (Object.getOwnPropertySymbols) {
|
4060 | var symbols = Object.getOwnPropertySymbols(parent);
|
4061 | for (var i = 0; i < symbols.length; i++) {
|
4062 |
|
4063 |
|
4064 | var symbol = symbols[i];
|
4065 | var descriptor = Object.getOwnPropertyDescriptor(parent, symbol);
|
4066 | if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
|
4067 | continue;
|
4068 | }
|
4069 | child[symbol] = _clone(parent[symbol], depth - 1);
|
4070 | if (!descriptor.enumerable) {
|
4071 | Object.defineProperty(child, symbol, {
|
4072 | enumerable: false
|
4073 | });
|
4074 | }
|
4075 | }
|
4076 | }
|
4077 |
|
4078 | if (includeNonEnumerable) {
|
4079 | var allPropertyNames = Object.getOwnPropertyNames(parent);
|
4080 | for (var i = 0; i < allPropertyNames.length; i++) {
|
4081 | var propertyName = allPropertyNames[i];
|
4082 | var descriptor = Object.getOwnPropertyDescriptor(parent, propertyName);
|
4083 | if (descriptor && descriptor.enumerable) {
|
4084 | continue;
|
4085 | }
|
4086 | child[propertyName] = _clone(parent[propertyName], depth - 1);
|
4087 | Object.defineProperty(child, propertyName, {
|
4088 | enumerable: false
|
4089 | });
|
4090 | }
|
4091 | }
|
4092 |
|
4093 | return child;
|
4094 | }
|
4095 |
|
4096 | return _clone(parent, depth);
|
4097 | }
|
4098 |
|
4099 |
|
4100 |
|
4101 |
|
4102 |
|
4103 |
|
4104 |
|
4105 |
|
4106 | clone.clonePrototype = function clonePrototype(parent) {
|
4107 | if (parent === null)
|
4108 | return null;
|
4109 |
|
4110 | var c = function () {};
|
4111 | c.prototype = parent;
|
4112 | return new c();
|
4113 | };
|
4114 |
|
4115 |
|
4116 |
|
4117 | function __objToStr(o) {
|
4118 | return Object.prototype.toString.call(o);
|
4119 | }
|
4120 | clone.__objToStr = __objToStr;
|
4121 |
|
4122 | function __isDate(o) {
|
4123 | return typeof o === 'object' && __objToStr(o) === '[object Date]';
|
4124 | }
|
4125 | clone.__isDate = __isDate;
|
4126 |
|
4127 | function __isArray(o) {
|
4128 | return typeof o === 'object' && __objToStr(o) === '[object Array]';
|
4129 | }
|
4130 | clone.__isArray = __isArray;
|
4131 |
|
4132 | function __isRegExp(o) {
|
4133 | return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
|
4134 | }
|
4135 | clone.__isRegExp = __isRegExp;
|
4136 |
|
4137 | function __getRegExpFlags(re) {
|
4138 | var flags = '';
|
4139 | if (re.global) flags += 'g';
|
4140 | if (re.ignoreCase) flags += 'i';
|
4141 | if (re.multiline) flags += 'm';
|
4142 | return flags;
|
4143 | }
|
4144 | clone.__getRegExpFlags = __getRegExpFlags;
|
4145 |
|
4146 | return clone;
|
4147 | })();
|
4148 |
|
4149 | if (typeof module === 'object' && module.exports) {
|
4150 | module.exports = clone;
|
4151 | }
|
4152 |
|
4153 | }).call(this,_dereq_("buffer").Buffer)
|
4154 | },{"buffer":10}],12:[function(_dereq_,module,exports){
|
4155 |
|
4156 |
|
4157 |
|
4158 |
|
4159 |
|
4160 | if (typeof module !== 'undefined') {
|
4161 | module.exports = Emitter;
|
4162 | }
|
4163 |
|
4164 |
|
4165 |
|
4166 |
|
4167 |
|
4168 |
|
4169 |
|
4170 | function Emitter(obj) {
|
4171 | if (obj) return mixin(obj);
|
4172 | };
|
4173 |
|
4174 |
|
4175 |
|
4176 |
|
4177 |
|
4178 |
|
4179 |
|
4180 |
|
4181 |
|
4182 | function mixin(obj) {
|
4183 | for (var key in Emitter.prototype) {
|
4184 | obj[key] = Emitter.prototype[key];
|
4185 | }
|
4186 | return obj;
|
4187 | }
|
4188 |
|
4189 |
|
4190 |
|
4191 |
|
4192 |
|
4193 |
|
4194 |
|
4195 |
|
4196 |
|
4197 |
|
4198 | Emitter.prototype.on =
|
4199 | Emitter.prototype.addEventListener = function(event, fn){
|
4200 | this._callbacks = this._callbacks || {};
|
4201 | (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
|
4202 | .push(fn);
|
4203 | return this;
|
4204 | };
|
4205 |
|
4206 |
|
4207 |
|
4208 |
|
4209 |
|
4210 |
|
4211 |
|
4212 |
|
4213 |
|
4214 |
|
4215 |
|
4216 | Emitter.prototype.once = function(event, fn){
|
4217 | function on() {
|
4218 | this.off(event, on);
|
4219 | fn.apply(this, arguments);
|
4220 | }
|
4221 |
|
4222 | on.fn = fn;
|
4223 | this.on(event, on);
|
4224 | return this;
|
4225 | };
|
4226 |
|
4227 |
|
4228 |
|
4229 |
|
4230 |
|
4231 |
|
4232 |
|
4233 |
|
4234 |
|
4235 |
|
4236 |
|
4237 | Emitter.prototype.off =
|
4238 | Emitter.prototype.removeListener =
|
4239 | Emitter.prototype.removeAllListeners =
|
4240 | Emitter.prototype.removeEventListener = function(event, fn){
|
4241 | this._callbacks = this._callbacks || {};
|
4242 |
|
4243 |
|
4244 | if (0 == arguments.length) {
|
4245 | this._callbacks = {};
|
4246 | return this;
|
4247 | }
|
4248 |
|
4249 |
|
4250 | var callbacks = this._callbacks['$' + event];
|
4251 | if (!callbacks) return this;
|
4252 |
|
4253 |
|
4254 | if (1 == arguments.length) {
|
4255 | delete this._callbacks['$' + event];
|
4256 | return this;
|
4257 | }
|
4258 |
|
4259 |
|
4260 | var cb;
|
4261 | for (var i = 0; i < callbacks.length; i++) {
|
4262 | cb = callbacks[i];
|
4263 | if (cb === fn || cb.fn === fn) {
|
4264 | callbacks.splice(i, 1);
|
4265 | break;
|
4266 | }
|
4267 | }
|
4268 | return this;
|
4269 | };
|
4270 |
|
4271 |
|
4272 |
|
4273 |
|
4274 |
|
4275 |
|
4276 |
|
4277 |
|
4278 |
|
4279 | Emitter.prototype.emit = function(event){
|
4280 | this._callbacks = this._callbacks || {};
|
4281 | var args = [].slice.call(arguments, 1)
|
4282 | , callbacks = this._callbacks['$' + event];
|
4283 |
|
4284 | if (callbacks) {
|
4285 | callbacks = callbacks.slice(0);
|
4286 | for (var i = 0, len = callbacks.length; i < len; ++i) {
|
4287 | callbacks[i].apply(this, args);
|
4288 | }
|
4289 | }
|
4290 |
|
4291 | return this;
|
4292 | };
|
4293 |
|
4294 |
|
4295 |
|
4296 |
|
4297 |
|
4298 |
|
4299 |
|
4300 |
|
4301 |
|
4302 | Emitter.prototype.listeners = function(event){
|
4303 | this._callbacks = this._callbacks || {};
|
4304 | return this._callbacks['$' + event] || [];
|
4305 | };
|
4306 |
|
4307 |
|
4308 |
|
4309 |
|
4310 |
|
4311 |
|
4312 |
|
4313 |
|
4314 |
|
4315 | Emitter.prototype.hasListeners = function(event){
|
4316 | return !! this.listeners(event).length;
|
4317 | };
|
4318 |
|
4319 | },{}],13:[function(_dereq_,module,exports){
|
4320 | exports.read = function (buffer, offset, isLE, mLen, nBytes) {
|
4321 | var e, m
|
4322 | var eLen = (nBytes * 8) - mLen - 1
|
4323 | var eMax = (1 << eLen) - 1
|
4324 | var eBias = eMax >> 1
|
4325 | var nBits = -7
|
4326 | var i = isLE ? (nBytes - 1) : 0
|
4327 | var d = isLE ? -1 : 1
|
4328 | var s = buffer[offset + i]
|
4329 |
|
4330 | i += d
|
4331 |
|
4332 | e = s & ((1 << (-nBits)) - 1)
|
4333 | s >>= (-nBits)
|
4334 | nBits += eLen
|
4335 | for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
4336 |
|
4337 | m = e & ((1 << (-nBits)) - 1)
|
4338 | e >>= (-nBits)
|
4339 | nBits += mLen
|
4340 | for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
4341 |
|
4342 | if (e === 0) {
|
4343 | e = 1 - eBias
|
4344 | } else if (e === eMax) {
|
4345 | return m ? NaN : ((s ? -1 : 1) * Infinity)
|
4346 | } else {
|
4347 | m = m + Math.pow(2, mLen)
|
4348 | e = e - eBias
|
4349 | }
|
4350 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
|
4351 | }
|
4352 |
|
4353 | exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
|
4354 | var e, m, c
|
4355 | var eLen = (nBytes * 8) - mLen - 1
|
4356 | var eMax = (1 << eLen) - 1
|
4357 | var eBias = eMax >> 1
|
4358 | var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
|
4359 | var i = isLE ? 0 : (nBytes - 1)
|
4360 | var d = isLE ? 1 : -1
|
4361 | var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
|
4362 |
|
4363 | value = Math.abs(value)
|
4364 |
|
4365 | if (isNaN(value) || value === Infinity) {
|
4366 | m = isNaN(value) ? 1 : 0
|
4367 | e = eMax
|
4368 | } else {
|
4369 | e = Math.floor(Math.log(value) / Math.LN2)
|
4370 | if (value * (c = Math.pow(2, -e)) < 1) {
|
4371 | e--
|
4372 | c *= 2
|
4373 | }
|
4374 | if (e + eBias >= 1) {
|
4375 | value += rt / c
|
4376 | } else {
|
4377 | value += rt * Math.pow(2, 1 - eBias)
|
4378 | }
|
4379 | if (value * c >= 2) {
|
4380 | e++
|
4381 | c /= 2
|
4382 | }
|
4383 |
|
4384 | if (e + eBias >= eMax) {
|
4385 | m = 0
|
4386 | e = eMax
|
4387 | } else if (e + eBias >= 1) {
|
4388 | m = ((value * c) - 1) * Math.pow(2, mLen)
|
4389 | e = e + eBias
|
4390 | } else {
|
4391 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
|
4392 | e = 0
|
4393 | }
|
4394 | }
|
4395 |
|
4396 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
|
4397 |
|
4398 | e = (e << mLen) | m
|
4399 | eLen += mLen
|
4400 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
|
4401 |
|
4402 | buffer[offset + i - d] |= s * 128
|
4403 | }
|
4404 |
|
4405 | },{}],14:[function(_dereq_,module,exports){
|
4406 | 'use strict';
|
4407 |
|
4408 |
|
4409 |
|
4410 |
|
4411 |
|
4412 | var errorMessage;
|
4413 |
|
4414 | errorMessage = 'An argument without append, prepend, ' +
|
4415 | 'or detach methods was given to `List';
|
4416 |
|
4417 |
|
4418 |
|
4419 |
|
4420 |
|
4421 |
|
4422 |
|
4423 |
|
4424 |
|
4425 |
|
4426 |
|
4427 |
|
4428 |
|
4429 | function List(/*items...*/) {
|
4430 | if (arguments.length) {
|
4431 | return List.from(arguments);
|
4432 | }
|
4433 | }
|
4434 |
|
4435 | var ListPrototype;
|
4436 |
|
4437 | ListPrototype = List.prototype;
|
4438 |
|
4439 |
|
4440 |
|
4441 |
|
4442 |
|
4443 |
|
4444 |
|
4445 |
|
4446 | List.of = function (/*items...*/) {
|
4447 | return List.from.call(this, arguments);
|
4448 | };
|
4449 |
|
4450 |
|
4451 |
|
4452 |
|
4453 |
|
4454 |
|
4455 |
|
4456 |
|
4457 | List.from = function (items) {
|
4458 | var list = new this(), length, iterator, item;
|
4459 |
|
4460 | if (items && (length = items.length)) {
|
4461 | iterator = -1;
|
4462 |
|
4463 | while (++iterator < length) {
|
4464 | item = items[iterator];
|
4465 |
|
4466 | if (item !== null && item !== undefined) {
|
4467 | list.append(item);
|
4468 | }
|
4469 | }
|
4470 | }
|
4471 |
|
4472 | return list;
|
4473 | };
|
4474 |
|
4475 |
|
4476 |
|
4477 |
|
4478 |
|
4479 | ListPrototype.head = null;
|
4480 |
|
4481 |
|
4482 |
|
4483 |
|
4484 |
|
4485 | ListPrototype.tail = null;
|
4486 |
|
4487 |
|
4488 |
|
4489 |
|
4490 |
|
4491 |
|
4492 | ListPrototype.toArray = function () {
|
4493 | var item = this.head,
|
4494 | result = [];
|
4495 |
|
4496 | while (item) {
|
4497 | result.push(item);
|
4498 | item = item.next;
|
4499 | }
|
4500 |
|
4501 | return result;
|
4502 | };
|
4503 |
|
4504 |
|
4505 |
|
4506 |
|
4507 |
|
4508 |
|
4509 |
|
4510 |
|
4511 | ListPrototype.prepend = function (item) {
|
4512 | if (!item) {
|
4513 | return false;
|
4514 | }
|
4515 |
|
4516 | if (!item.append || !item.prepend || !item.detach) {
|
4517 | throw new Error(errorMessage + '#prepend`.');
|
4518 | }
|
4519 |
|
4520 | var self, head;
|
4521 |
|
4522 |
|
4523 | self = this;
|
4524 |
|
4525 |
|
4526 |
|
4527 | head = self.head;
|
4528 |
|
4529 | if (head) {
|
4530 | return head.prepend(item);
|
4531 | }
|
4532 |
|
4533 |
|
4534 |
|
4535 |
|
4536 | item.detach();
|
4537 |
|
4538 |
|
4539 | item.list = self;
|
4540 |
|
4541 |
|
4542 | self.head = item;
|
4543 |
|
4544 | return item;
|
4545 | };
|
4546 |
|
4547 |
|
4548 |
|
4549 |
|
4550 |
|
4551 |
|
4552 |
|
4553 |
|
4554 |
|
4555 | ListPrototype.append = function (item) {
|
4556 | if (!item) {
|
4557 | return false;
|
4558 | }
|
4559 |
|
4560 | if (!item.append || !item.prepend || !item.detach) {
|
4561 | throw new Error(errorMessage + '#append`.');
|
4562 | }
|
4563 |
|
4564 | var self, head, tail;
|
4565 |
|
4566 |
|
4567 | self = this;
|
4568 |
|
4569 |
|
4570 |
|
4571 | tail = self.tail;
|
4572 |
|
4573 | if (tail) {
|
4574 | return tail.append(item);
|
4575 | }
|
4576 |
|
4577 |
|
4578 |
|
4579 | head = self.head;
|
4580 |
|
4581 | if (head) {
|
4582 | return head.append(item);
|
4583 | }
|
4584 |
|
4585 |
|
4586 |
|
4587 |
|
4588 | item.detach();
|
4589 |
|
4590 |
|
4591 | item.list = self;
|
4592 |
|
4593 |
|
4594 | self.head = item;
|
4595 |
|
4596 | return item;
|
4597 | };
|
4598 |
|
4599 |
|
4600 |
|
4601 |
|
4602 |
|
4603 |
|
4604 |
|
4605 |
|
4606 |
|
4607 |
|
4608 |
|
4609 | function ListItem() {}
|
4610 |
|
4611 | List.Item = ListItem;
|
4612 |
|
4613 | var ListItemPrototype = ListItem.prototype;
|
4614 |
|
4615 | ListItemPrototype.next = null;
|
4616 |
|
4617 | ListItemPrototype.prev = null;
|
4618 |
|
4619 | ListItemPrototype.list = null;
|
4620 |
|
4621 |
|
4622 |
|
4623 |
|
4624 |
|
4625 |
|
4626 | ListItemPrototype.detach = function () {
|
4627 |
|
4628 | var self = this,
|
4629 | list = self.list,
|
4630 | prev = self.prev,
|
4631 | next = self.next;
|
4632 |
|
4633 |
|
4634 | if (!list) {
|
4635 | return self;
|
4636 | }
|
4637 |
|
4638 |
|
4639 |
|
4640 | if (list.tail === self) {
|
4641 | list.tail = prev;
|
4642 | }
|
4643 |
|
4644 |
|
4645 |
|
4646 | if (list.head === self) {
|
4647 | list.head = next;
|
4648 | }
|
4649 |
|
4650 |
|
4651 |
|
4652 | if (list.tail === list.head) {
|
4653 | list.tail = null;
|
4654 | }
|
4655 |
|
4656 |
|
4657 | if (prev) {
|
4658 | prev.next = next;
|
4659 | }
|
4660 |
|
4661 |
|
4662 | if (next) {
|
4663 | next.prev = prev;
|
4664 | }
|
4665 |
|
4666 |
|
4667 |
|
4668 | self.prev = self.next = self.list = null;
|
4669 |
|
4670 |
|
4671 | return self;
|
4672 | };
|
4673 |
|
4674 |
|
4675 |
|
4676 |
|
4677 |
|
4678 |
|
4679 |
|
4680 |
|
4681 | ListItemPrototype.prepend = function (item) {
|
4682 | if (!item || !item.append || !item.prepend || !item.detach) {
|
4683 | throw new Error(errorMessage + 'Item#prepend`.');
|
4684 | }
|
4685 |
|
4686 |
|
4687 | var self = this,
|
4688 | list = self.list,
|
4689 | prev = self.prev;
|
4690 |
|
4691 |
|
4692 | if (!list) {
|
4693 | return false;
|
4694 | }
|
4695 |
|
4696 |
|
4697 | item.detach();
|
4698 |
|
4699 |
|
4700 | if (prev) {
|
4701 |
|
4702 | item.prev = prev;
|
4703 |
|
4704 |
|
4705 | prev.next = item;
|
4706 | }
|
4707 |
|
4708 |
|
4709 | item.next = self;
|
4710 |
|
4711 |
|
4712 | item.list = list;
|
4713 |
|
4714 |
|
4715 | self.prev = item;
|
4716 |
|
4717 |
|
4718 |
|
4719 | if (self === list.head) {
|
4720 | list.head = item;
|
4721 | }
|
4722 |
|
4723 |
|
4724 |
|
4725 | if (!list.tail) {
|
4726 | list.tail = self;
|
4727 | }
|
4728 |
|
4729 |
|
4730 | return item;
|
4731 | };
|
4732 |
|
4733 |
|
4734 |
|
4735 |
|
4736 |
|
4737 |
|
4738 |
|
4739 |
|
4740 | ListItemPrototype.append = function (item) {
|
4741 |
|
4742 | if (!item || !item.append || !item.prepend || !item.detach) {
|
4743 | throw new Error(errorMessage + 'Item#append`.');
|
4744 | }
|
4745 |
|
4746 |
|
4747 | var self = this,
|
4748 | list = self.list,
|
4749 | next = self.next;
|
4750 |
|
4751 |
|
4752 | if (!list) {
|
4753 | return false;
|
4754 | }
|
4755 |
|
4756 |
|
4757 | item.detach();
|
4758 |
|
4759 |
|
4760 | if (next) {
|
4761 |
|
4762 | item.next = next;
|
4763 |
|
4764 |
|
4765 | next.prev = item;
|
4766 | }
|
4767 |
|
4768 |
|
4769 | item.prev = self;
|
4770 |
|
4771 |
|
4772 | item.list = list;
|
4773 |
|
4774 |
|
4775 | self.next = item;
|
4776 |
|
4777 |
|
4778 |
|
4779 | if (self === list.tail || !list.tail) {
|
4780 | list.tail = item;
|
4781 | }
|
4782 |
|
4783 |
|
4784 | return item;
|
4785 | };
|
4786 |
|
4787 |
|
4788 |
|
4789 |
|
4790 |
|
4791 | module.exports = List;
|
4792 |
|
4793 | },{}],15:[function(_dereq_,module,exports){
|
4794 | 'use strict';
|
4795 |
|
4796 | module.exports = _dereq_('./_source/linked-list.js');
|
4797 |
|
4798 | },{"./_source/linked-list.js":14}],16:[function(_dereq_,module,exports){
|
4799 |
|
4800 |
|
4801 |
|
4802 |
|
4803 |
|
4804 |
|
4805 |
|
4806 |
|
4807 |
|
4808 |
|
4809 |
|
4810 |
|
4811 |
|
4812 |
|
4813 |
|
4814 |
|
4815 |
|
4816 |
|
4817 |
|
4818 |
|
4819 |
|
4820 | 'use strict';
|
4821 |
|
4822 |
|
4823 |
|
4824 |
|
4825 | function hasOwnProperty(obj, prop) {
|
4826 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
4827 | }
|
4828 |
|
4829 | module.exports = function(qs, sep, eq, options) {
|
4830 | sep = sep || '&';
|
4831 | eq = eq || '=';
|
4832 | var obj = {};
|
4833 |
|
4834 | if (typeof qs !== 'string' || qs.length === 0) {
|
4835 | return obj;
|
4836 | }
|
4837 |
|
4838 | var regexp = /\+/g;
|
4839 | qs = qs.split(sep);
|
4840 |
|
4841 | var maxKeys = 1000;
|
4842 | if (options && typeof options.maxKeys === 'number') {
|
4843 | maxKeys = options.maxKeys;
|
4844 | }
|
4845 |
|
4846 | var len = qs.length;
|
4847 |
|
4848 | if (maxKeys > 0 && len > maxKeys) {
|
4849 | len = maxKeys;
|
4850 | }
|
4851 |
|
4852 | for (var i = 0; i < len; ++i) {
|
4853 | var x = qs[i].replace(regexp, '%20'),
|
4854 | idx = x.indexOf(eq),
|
4855 | kstr, vstr, k, v;
|
4856 |
|
4857 | if (idx >= 0) {
|
4858 | kstr = x.substr(0, idx);
|
4859 | vstr = x.substr(idx + 1);
|
4860 | } else {
|
4861 | kstr = x;
|
4862 | vstr = '';
|
4863 | }
|
4864 |
|
4865 | k = decodeURIComponent(kstr);
|
4866 | v = decodeURIComponent(vstr);
|
4867 |
|
4868 | if (!hasOwnProperty(obj, k)) {
|
4869 | obj[k] = v;
|
4870 | } else if (isArray(obj[k])) {
|
4871 | obj[k].push(v);
|
4872 | } else {
|
4873 | obj[k] = [obj[k], v];
|
4874 | }
|
4875 | }
|
4876 |
|
4877 | return obj;
|
4878 | };
|
4879 |
|
4880 | var isArray = Array.isArray || function (xs) {
|
4881 | return Object.prototype.toString.call(xs) === '[object Array]';
|
4882 | };
|
4883 |
|
4884 | },{}],17:[function(_dereq_,module,exports){
|
4885 |
|
4886 |
|
4887 |
|
4888 |
|
4889 |
|
4890 |
|
4891 |
|
4892 |
|
4893 |
|
4894 |
|
4895 |
|
4896 |
|
4897 |
|
4898 |
|
4899 |
|
4900 |
|
4901 |
|
4902 |
|
4903 |
|
4904 |
|
4905 |
|
4906 | 'use strict';
|
4907 |
|
4908 | var stringifyPrimitive = function(v) {
|
4909 | switch (typeof v) {
|
4910 | case 'string':
|
4911 | return v;
|
4912 |
|
4913 | case 'boolean':
|
4914 | return v ? 'true' : 'false';
|
4915 |
|
4916 | case 'number':
|
4917 | return isFinite(v) ? v : '';
|
4918 |
|
4919 | default:
|
4920 | return '';
|
4921 | }
|
4922 | };
|
4923 |
|
4924 | module.exports = function(obj, sep, eq, name) {
|
4925 | sep = sep || '&';
|
4926 | eq = eq || '=';
|
4927 | if (obj === null) {
|
4928 | obj = undefined;
|
4929 | }
|
4930 |
|
4931 | if (typeof obj === 'object') {
|
4932 | return map(objectKeys(obj), function(k) {
|
4933 | var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
|
4934 | if (isArray(obj[k])) {
|
4935 | return map(obj[k], function(v) {
|
4936 | return ks + encodeURIComponent(stringifyPrimitive(v));
|
4937 | }).join(sep);
|
4938 | } else {
|
4939 | return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
|
4940 | }
|
4941 | }).join(sep);
|
4942 |
|
4943 | }
|
4944 |
|
4945 | if (!name) return '';
|
4946 | return encodeURIComponent(stringifyPrimitive(name)) + eq +
|
4947 | encodeURIComponent(stringifyPrimitive(obj));
|
4948 | };
|
4949 |
|
4950 | var isArray = Array.isArray || function (xs) {
|
4951 | return Object.prototype.toString.call(xs) === '[object Array]';
|
4952 | };
|
4953 |
|
4954 | function map (xs, f) {
|
4955 | if (xs.map) return xs.map(f);
|
4956 | var res = [];
|
4957 | for (var i = 0; i < xs.length; i++) {
|
4958 | res.push(f(xs[i], i));
|
4959 | }
|
4960 | return res;
|
4961 | }
|
4962 |
|
4963 | var objectKeys = Object.keys || function (obj) {
|
4964 | var res = [];
|
4965 | for (var key in obj) {
|
4966 | if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key);
|
4967 | }
|
4968 | return res;
|
4969 | };
|
4970 |
|
4971 | },{}],18:[function(_dereq_,module,exports){
|
4972 | 'use strict';
|
4973 |
|
4974 | exports.decode = exports.parse = _dereq_('./decode');
|
4975 | exports.encode = exports.stringify = _dereq_('./encode');
|
4976 |
|
4977 | },{"./decode":16,"./encode":17}],19:[function(_dereq_,module,exports){
|
4978 | var Emitter = _dereq_('component-emitter');
|
4979 |
|
4980 | var SCChannel = function (name, client, options) {
|
4981 | var self = this;
|
4982 |
|
4983 | Emitter.call(this);
|
4984 |
|
4985 | this.PENDING = 'pending';
|
4986 | this.SUBSCRIBED = 'subscribed';
|
4987 | this.UNSUBSCRIBED = 'unsubscribed';
|
4988 |
|
4989 | this.name = name;
|
4990 | this.state = this.UNSUBSCRIBED;
|
4991 | this.client = client;
|
4992 |
|
4993 | this.options = options || {};
|
4994 | this.setOptions(this.options);
|
4995 | };
|
4996 |
|
4997 | SCChannel.prototype = Object.create(Emitter.prototype);
|
4998 |
|
4999 | SCChannel.prototype.setOptions = function (options) {
|
5000 | if (!options) {
|
5001 | options = {};
|
5002 | }
|
5003 | this.waitForAuth = options.waitForAuth || false;
|
5004 | this.batch = options.batch || false;
|
5005 |
|
5006 | if (options.data !== undefined) {
|
5007 | this.data = options.data;
|
5008 | }
|
5009 | };
|
5010 |
|
5011 | SCChannel.prototype.getState = function () {
|
5012 | return this.state;
|
5013 | };
|
5014 |
|
5015 | SCChannel.prototype.subscribe = function (options) {
|
5016 | this.client.subscribe(this.name, options);
|
5017 | };
|
5018 |
|
5019 | SCChannel.prototype.unsubscribe = function () {
|
5020 | this.client.unsubscribe(this.name);
|
5021 | };
|
5022 |
|
5023 | SCChannel.prototype.isSubscribed = function (includePending) {
|
5024 | return this.client.isSubscribed(this.name, includePending);
|
5025 | };
|
5026 |
|
5027 | SCChannel.prototype.publish = function (data, callback) {
|
5028 | this.client.publish(this.name, data, callback);
|
5029 | };
|
5030 |
|
5031 | SCChannel.prototype.watch = function (handler) {
|
5032 | this.client.watch(this.name, handler);
|
5033 | };
|
5034 |
|
5035 | SCChannel.prototype.unwatch = function (handler) {
|
5036 | this.client.unwatch(this.name, handler);
|
5037 | };
|
5038 |
|
5039 | SCChannel.prototype.watchers = function () {
|
5040 | return this.client.watchers(this.name);
|
5041 | };
|
5042 |
|
5043 | SCChannel.prototype.destroy = function () {
|
5044 | this.client.destroyChannel(this.name);
|
5045 | };
|
5046 |
|
5047 | module.exports.SCChannel = SCChannel;
|
5048 |
|
5049 | },{"component-emitter":12}],20:[function(_dereq_,module,exports){
|
5050 |
|
5051 |
|
5052 | module.exports = function decycle(object) {
|
5053 |
|
5054 |
|
5055 |
|
5056 |
|
5057 |
|
5058 |
|
5059 |
|
5060 |
|
5061 |
|
5062 |
|
5063 |
|
5064 |
|
5065 |
|
5066 |
|
5067 |
|
5068 |
|
5069 | var objects = [],
|
5070 | paths = [];
|
5071 |
|
5072 | return (function derez(value, path) {
|
5073 |
|
5074 |
|
5075 |
|
5076 | var i,
|
5077 | name,
|
5078 | nu;
|
5079 |
|
5080 |
|
5081 |
|
5082 |
|
5083 | if (typeof value === 'object' && value !== null &&
|
5084 | !(value instanceof Boolean) &&
|
5085 | !(value instanceof Date) &&
|
5086 | !(value instanceof Number) &&
|
5087 | !(value instanceof RegExp) &&
|
5088 | !(value instanceof String)) {
|
5089 |
|
5090 |
|
5091 |
|
5092 |
|
5093 |
|
5094 | for (i = 0; i < objects.length; i += 1) {
|
5095 | if (objects[i] === value) {
|
5096 | return {$ref: paths[i]};
|
5097 | }
|
5098 | }
|
5099 |
|
5100 |
|
5101 |
|
5102 | objects.push(value);
|
5103 | paths.push(path);
|
5104 |
|
5105 |
|
5106 |
|
5107 | if (Object.prototype.toString.apply(value) === '[object Array]') {
|
5108 | nu = [];
|
5109 | for (i = 0; i < value.length; i += 1) {
|
5110 | nu[i] = derez(value[i], path + '[' + i + ']');
|
5111 | }
|
5112 | } else {
|
5113 |
|
5114 |
|
5115 |
|
5116 | nu = {};
|
5117 | for (name in value) {
|
5118 | if (Object.prototype.hasOwnProperty.call(value, name)) {
|
5119 | nu[name] = derez(value[name],
|
5120 | path + '[' + JSON.stringify(name) + ']');
|
5121 | }
|
5122 | }
|
5123 | }
|
5124 | return nu;
|
5125 | }
|
5126 | return value;
|
5127 | }(object, '$'));
|
5128 | };
|
5129 |
|
5130 | },{}],21:[function(_dereq_,module,exports){
|
5131 | var decycle = _dereq_('./decycle');
|
5132 |
|
5133 | var isStrict = (function () { return !this; })();
|
5134 |
|
5135 | function AuthTokenExpiredError(message, expiry) {
|
5136 | this.name = 'AuthTokenExpiredError';
|
5137 | this.message = message;
|
5138 | this.expiry = expiry;
|
5139 | if (Error.captureStackTrace && !isStrict) {
|
5140 | Error.captureStackTrace(this, arguments.callee);
|
5141 | } else {
|
5142 | this.stack = (new Error()).stack;
|
5143 | }
|
5144 | }
|
5145 | AuthTokenExpiredError.prototype = Object.create(Error.prototype);
|
5146 |
|
5147 |
|
5148 | function AuthTokenInvalidError(message) {
|
5149 | this.name = 'AuthTokenInvalidError';
|
5150 | this.message = message;
|
5151 | if (Error.captureStackTrace && !isStrict) {
|
5152 | Error.captureStackTrace(this, arguments.callee);
|
5153 | } else {
|
5154 | this.stack = (new Error()).stack;
|
5155 | }
|
5156 | }
|
5157 | AuthTokenInvalidError.prototype = Object.create(Error.prototype);
|
5158 |
|
5159 |
|
5160 | function AuthTokenNotBeforeError(message, date) {
|
5161 | this.name = 'AuthTokenNotBeforeError';
|
5162 | this.message = message;
|
5163 | this.date = date;
|
5164 | if (Error.captureStackTrace && !isStrict) {
|
5165 | Error.captureStackTrace(this, arguments.callee);
|
5166 | } else {
|
5167 | this.stack = (new Error()).stack;
|
5168 | }
|
5169 | }
|
5170 | AuthTokenNotBeforeError.prototype = Object.create(Error.prototype);
|
5171 |
|
5172 |
|
5173 |
|
5174 | function AuthTokenError(message) {
|
5175 | this.name = 'AuthTokenError';
|
5176 | this.message = message;
|
5177 | if (Error.captureStackTrace && !isStrict) {
|
5178 | Error.captureStackTrace(this, arguments.callee);
|
5179 | } else {
|
5180 | this.stack = (new Error()).stack;
|
5181 | }
|
5182 | }
|
5183 | AuthTokenError.prototype = Object.create(Error.prototype);
|
5184 |
|
5185 |
|
5186 | function SilentMiddlewareBlockedError(message, type) {
|
5187 | this.name = 'SilentMiddlewareBlockedError';
|
5188 | this.message = message;
|
5189 | this.type = type;
|
5190 | if (Error.captureStackTrace && !isStrict) {
|
5191 | Error.captureStackTrace(this, arguments.callee);
|
5192 | } else {
|
5193 | this.stack = (new Error()).stack;
|
5194 | }
|
5195 | }
|
5196 | SilentMiddlewareBlockedError.prototype = Object.create(Error.prototype);
|
5197 |
|
5198 |
|
5199 | function InvalidActionError(message) {
|
5200 | this.name = 'InvalidActionError';
|
5201 | this.message = message;
|
5202 | if (Error.captureStackTrace && !isStrict) {
|
5203 | Error.captureStackTrace(this, arguments.callee);
|
5204 | } else {
|
5205 | this.stack = (new Error()).stack;
|
5206 | }
|
5207 | }
|
5208 | InvalidActionError.prototype = Object.create(Error.prototype);
|
5209 |
|
5210 | function InvalidArgumentsError(message) {
|
5211 | this.name = 'InvalidArgumentsError';
|
5212 | this.message = message;
|
5213 | if (Error.captureStackTrace && !isStrict) {
|
5214 | Error.captureStackTrace(this, arguments.callee);
|
5215 | } else {
|
5216 | this.stack = (new Error()).stack;
|
5217 | }
|
5218 | }
|
5219 | InvalidArgumentsError.prototype = Object.create(Error.prototype);
|
5220 |
|
5221 | function InvalidOptionsError(message) {
|
5222 | this.name = 'InvalidOptionsError';
|
5223 | this.message = message;
|
5224 | if (Error.captureStackTrace && !isStrict) {
|
5225 | Error.captureStackTrace(this, arguments.callee);
|
5226 | } else {
|
5227 | this.stack = (new Error()).stack;
|
5228 | }
|
5229 | }
|
5230 | InvalidOptionsError.prototype = Object.create(Error.prototype);
|
5231 |
|
5232 |
|
5233 | function InvalidMessageError(message) {
|
5234 | this.name = 'InvalidMessageError';
|
5235 | this.message = message;
|
5236 | if (Error.captureStackTrace && !isStrict) {
|
5237 | Error.captureStackTrace(this, arguments.callee);
|
5238 | } else {
|
5239 | this.stack = (new Error()).stack;
|
5240 | }
|
5241 | }
|
5242 | InvalidMessageError.prototype = Object.create(Error.prototype);
|
5243 |
|
5244 |
|
5245 | function SocketProtocolError(message, code) {
|
5246 | this.name = 'SocketProtocolError';
|
5247 | this.message = message;
|
5248 | this.code = code;
|
5249 | if (Error.captureStackTrace && !isStrict) {
|
5250 | Error.captureStackTrace(this, arguments.callee);
|
5251 | } else {
|
5252 | this.stack = (new Error()).stack;
|
5253 | }
|
5254 | }
|
5255 | SocketProtocolError.prototype = Object.create(Error.prototype);
|
5256 |
|
5257 |
|
5258 | function ServerProtocolError(message) {
|
5259 | this.name = 'ServerProtocolError';
|
5260 | this.message = message;
|
5261 | if (Error.captureStackTrace && !isStrict) {
|
5262 | Error.captureStackTrace(this, arguments.callee);
|
5263 | } else {
|
5264 | this.stack = (new Error()).stack;
|
5265 | }
|
5266 | }
|
5267 | ServerProtocolError.prototype = Object.create(Error.prototype);
|
5268 |
|
5269 | function HTTPServerError(message) {
|
5270 | this.name = 'HTTPServerError';
|
5271 | this.message = message;
|
5272 | if (Error.captureStackTrace && !isStrict) {
|
5273 | Error.captureStackTrace(this, arguments.callee);
|
5274 | } else {
|
5275 | this.stack = (new Error()).stack;
|
5276 | }
|
5277 | }
|
5278 | HTTPServerError.prototype = Object.create(Error.prototype);
|
5279 |
|
5280 |
|
5281 | function ResourceLimitError(message) {
|
5282 | this.name = 'ResourceLimitError';
|
5283 | this.message = message;
|
5284 | if (Error.captureStackTrace && !isStrict) {
|
5285 | Error.captureStackTrace(this, arguments.callee);
|
5286 | } else {
|
5287 | this.stack = (new Error()).stack;
|
5288 | }
|
5289 | }
|
5290 | ResourceLimitError.prototype = Object.create(Error.prototype);
|
5291 |
|
5292 |
|
5293 | function TimeoutError(message) {
|
5294 | this.name = 'TimeoutError';
|
5295 | this.message = message;
|
5296 | if (Error.captureStackTrace && !isStrict) {
|
5297 | Error.captureStackTrace(this, arguments.callee);
|
5298 | } else {
|
5299 | this.stack = (new Error()).stack;
|
5300 | }
|
5301 | }
|
5302 | TimeoutError.prototype = Object.create(Error.prototype);
|
5303 |
|
5304 |
|
5305 | function BadConnectionError(message, type) {
|
5306 | this.name = 'BadConnectionError';
|
5307 | this.message = message;
|
5308 | this.type = type;
|
5309 | if (Error.captureStackTrace && !isStrict) {
|
5310 | Error.captureStackTrace(this, arguments.callee);
|
5311 | } else {
|
5312 | this.stack = (new Error()).stack;
|
5313 | }
|
5314 | }
|
5315 | BadConnectionError.prototype = Object.create(Error.prototype);
|
5316 |
|
5317 |
|
5318 | function BrokerError(message) {
|
5319 | this.name = 'BrokerError';
|
5320 | this.message = message;
|
5321 | if (Error.captureStackTrace && !isStrict) {
|
5322 | Error.captureStackTrace(this, arguments.callee);
|
5323 | } else {
|
5324 | this.stack = (new Error()).stack;
|
5325 | }
|
5326 | }
|
5327 | BrokerError.prototype = Object.create(Error.prototype);
|
5328 |
|
5329 |
|
5330 | function ProcessExitError(message, code) {
|
5331 | this.name = 'ProcessExitError';
|
5332 | this.message = message;
|
5333 | this.code = code;
|
5334 | if (Error.captureStackTrace && !isStrict) {
|
5335 | Error.captureStackTrace(this, arguments.callee);
|
5336 | } else {
|
5337 | this.stack = (new Error()).stack;
|
5338 | }
|
5339 | }
|
5340 | ProcessExitError.prototype = Object.create(Error.prototype);
|
5341 |
|
5342 |
|
5343 | function UnknownError(message) {
|
5344 | this.name = 'UnknownError';
|
5345 | this.message = message;
|
5346 | if (Error.captureStackTrace && !isStrict) {
|
5347 | Error.captureStackTrace(this, arguments.callee);
|
5348 | } else {
|
5349 | this.stack = (new Error()).stack;
|
5350 | }
|
5351 | }
|
5352 | UnknownError.prototype = Object.create(Error.prototype);
|
5353 |
|
5354 |
|
5355 |
|
5356 |
|
5357 | module.exports = {
|
5358 | AuthTokenExpiredError: AuthTokenExpiredError,
|
5359 | AuthTokenInvalidError: AuthTokenInvalidError,
|
5360 | AuthTokenNotBeforeError: AuthTokenNotBeforeError,
|
5361 | AuthTokenError: AuthTokenError,
|
5362 | SilentMiddlewareBlockedError: SilentMiddlewareBlockedError,
|
5363 | InvalidActionError: InvalidActionError,
|
5364 | InvalidArgumentsError: InvalidArgumentsError,
|
5365 | InvalidOptionsError: InvalidOptionsError,
|
5366 | InvalidMessageError: InvalidMessageError,
|
5367 | SocketProtocolError: SocketProtocolError,
|
5368 | ServerProtocolError: ServerProtocolError,
|
5369 | HTTPServerError: HTTPServerError,
|
5370 | ResourceLimitError: ResourceLimitError,
|
5371 | TimeoutError: TimeoutError,
|
5372 | BadConnectionError: BadConnectionError,
|
5373 | BrokerError: BrokerError,
|
5374 | ProcessExitError: ProcessExitError,
|
5375 | UnknownError: UnknownError
|
5376 | };
|
5377 |
|
5378 | module.exports.socketProtocolErrorStatuses = {
|
5379 | 1001: 'Socket was disconnected',
|
5380 | 1002: 'A WebSocket protocol error was encountered',
|
5381 | 1003: 'Server terminated socket because it received invalid data',
|
5382 | 1005: 'Socket closed without status code',
|
5383 | 1006: 'Socket hung up',
|
5384 | 1007: 'Message format was incorrect',
|
5385 | 1008: 'Encountered a policy violation',
|
5386 | 1009: 'Message was too big to process',
|
5387 | 1010: 'Client ended the connection because the server did not comply with extension requirements',
|
5388 | 1011: 'Server encountered an unexpected fatal condition',
|
5389 | 4000: 'Server ping timed out',
|
5390 | 4001: 'Client pong timed out',
|
5391 | 4002: 'Server failed to sign auth token',
|
5392 | 4003: 'Failed to complete handshake',
|
5393 | 4004: 'Client failed to save auth token',
|
5394 | 4005: 'Did not receive #handshake from client before timeout',
|
5395 | 4006: 'Failed to bind socket to message broker',
|
5396 | 4007: 'Client connection establishment timed out',
|
5397 | 4008: 'Server rejected handshake from client'
|
5398 | };
|
5399 |
|
5400 | module.exports.socketProtocolIgnoreStatuses = {
|
5401 | 1000: 'Socket closed normally',
|
5402 | 1001: 'Socket hung up'
|
5403 | };
|
5404 |
|
5405 |
|
5406 | var unserializableErrorProperties = {
|
5407 | domain: 1,
|
5408 | domainEmitter: 1,
|
5409 | domainThrown: 1
|
5410 | };
|
5411 |
|
5412 |
|
5413 |
|
5414 | module.exports.dehydrateError = function dehydrateError(error, includeStackTrace) {
|
5415 | var dehydratedError;
|
5416 |
|
5417 | if (error && typeof error == 'object') {
|
5418 | dehydratedError = {
|
5419 | message: error.message
|
5420 | };
|
5421 | if (includeStackTrace) {
|
5422 | dehydratedError.stack = error.stack;
|
5423 | }
|
5424 | for (var i in error) {
|
5425 | if (!unserializableErrorProperties[i]) {
|
5426 | dehydratedError[i] = error[i];
|
5427 | }
|
5428 | }
|
5429 | } else if (typeof error == 'function') {
|
5430 | dehydratedError = '[function ' + (error.name || 'anonymous') + ']';
|
5431 | } else {
|
5432 | dehydratedError = error;
|
5433 | }
|
5434 |
|
5435 | return decycle(dehydratedError);
|
5436 | };
|
5437 |
|
5438 |
|
5439 | module.exports.hydrateError = function hydrateError(error) {
|
5440 | var hydratedError = null;
|
5441 | if (error != null) {
|
5442 | if (typeof error == 'object') {
|
5443 | hydratedError = new Error(error.message);
|
5444 | for (var i in error) {
|
5445 | if (error.hasOwnProperty(i)) {
|
5446 | hydratedError[i] = error[i];
|
5447 | }
|
5448 | }
|
5449 | } else {
|
5450 | hydratedError = error;
|
5451 | }
|
5452 | }
|
5453 | return hydratedError;
|
5454 | };
|
5455 |
|
5456 | module.exports.decycle = decycle;
|
5457 |
|
5458 | },{"./decycle":20}],22:[function(_dereq_,module,exports){
|
5459 | (function (global){
|
5460 | var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
5461 | var validJSONStartRegex = /^[ \n\r\t]*[{\[]/;
|
5462 |
|
5463 | var arrayBufferToBase64 = function (arraybuffer) {
|
5464 | var bytes = new Uint8Array(arraybuffer);
|
5465 | var len = bytes.length;
|
5466 | var base64 = '';
|
5467 |
|
5468 | for (var i = 0; i < len; i += 3) {
|
5469 | base64 += base64Chars[bytes[i] >> 2];
|
5470 | base64 += base64Chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
|
5471 | base64 += base64Chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
|
5472 | base64 += base64Chars[bytes[i + 2] & 63];
|
5473 | }
|
5474 |
|
5475 | if ((len % 3) === 2) {
|
5476 | base64 = base64.substring(0, base64.length - 1) + '=';
|
5477 | } else if (len % 3 === 1) {
|
5478 | base64 = base64.substring(0, base64.length - 2) + '==';
|
5479 | }
|
5480 |
|
5481 | return base64;
|
5482 | };
|
5483 |
|
5484 | var binaryToBase64Replacer = function (key, value) {
|
5485 | if (global.ArrayBuffer && value instanceof global.ArrayBuffer) {
|
5486 | return {
|
5487 | base64: true,
|
5488 | data: arrayBufferToBase64(value)
|
5489 | };
|
5490 | } else if (global.Buffer) {
|
5491 | if (value instanceof global.Buffer){
|
5492 | return {
|
5493 | base64: true,
|
5494 | data: value.toString('base64')
|
5495 | };
|
5496 | }
|
5497 |
|
5498 |
|
5499 |
|
5500 | if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
|
5501 | var rehydratedBuffer;
|
5502 | if (global.Buffer.from) {
|
5503 | rehydratedBuffer = global.Buffer.from(value.data);
|
5504 | } else {
|
5505 | rehydratedBuffer = new global.Buffer(value.data);
|
5506 | }
|
5507 | return {
|
5508 | base64: true,
|
5509 | data: rehydratedBuffer.toString('base64')
|
5510 | };
|
5511 | }
|
5512 | }
|
5513 | return value;
|
5514 | };
|
5515 |
|
5516 |
|
5517 |
|
5518 | module.exports.decode = function (input) {
|
5519 | if (input == null) {
|
5520 | return null;
|
5521 | }
|
5522 |
|
5523 | if (input === '#1' || input === '#2') {
|
5524 | return input;
|
5525 | }
|
5526 | var message = input.toString();
|
5527 |
|
5528 |
|
5529 | if (!validJSONStartRegex.test(message)) {
|
5530 | return message;
|
5531 | }
|
5532 |
|
5533 | try {
|
5534 | return JSON.parse(message);
|
5535 | } catch (err) {}
|
5536 | return message;
|
5537 | };
|
5538 |
|
5539 |
|
5540 |
|
5541 |
|
5542 |
|
5543 |
|
5544 |
|
5545 |
|
5546 |
|
5547 | module.exports.encode = function (object) {
|
5548 |
|
5549 | if (object === '#1' || object === '#2') {
|
5550 | return object;
|
5551 | }
|
5552 | return JSON.stringify(object, binaryToBase64Replacer);
|
5553 | };
|
5554 |
|
5555 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
5556 | },{}],23:[function(_dereq_,module,exports){
|
5557 | var v1 = _dereq_('./v1');
|
5558 | var v4 = _dereq_('./v4');
|
5559 |
|
5560 | var uuid = v4;
|
5561 | uuid.v1 = v1;
|
5562 | uuid.v4 = v4;
|
5563 |
|
5564 | module.exports = uuid;
|
5565 |
|
5566 | },{"./v1":26,"./v4":27}],24:[function(_dereq_,module,exports){
|
5567 |
|
5568 |
|
5569 |
|
5570 |
|
5571 | var byteToHex = [];
|
5572 | for (var i = 0; i < 256; ++i) {
|
5573 | byteToHex[i] = (i + 0x100).toString(16).substr(1);
|
5574 | }
|
5575 |
|
5576 | function bytesToUuid(buf, offset) {
|
5577 | var i = offset || 0;
|
5578 | var bth = byteToHex;
|
5579 | return bth[buf[i++]] + bth[buf[i++]] +
|
5580 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
5581 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
5582 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
5583 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
5584 | bth[buf[i++]] + bth[buf[i++]] +
|
5585 | bth[buf[i++]] + bth[buf[i++]] +
|
5586 | bth[buf[i++]] + bth[buf[i++]];
|
5587 | }
|
5588 |
|
5589 | module.exports = bytesToUuid;
|
5590 |
|
5591 | },{}],25:[function(_dereq_,module,exports){
|
5592 |
|
5593 |
|
5594 |
|
5595 |
|
5596 |
|
5597 |
|
5598 | var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues.bind(crypto)) ||
|
5599 | (typeof(msCrypto) != 'undefined' && msCrypto.getRandomValues.bind(msCrypto));
|
5600 | if (getRandomValues) {
|
5601 |
|
5602 | var rnds8 = new Uint8Array(16);
|
5603 |
|
5604 | module.exports = function whatwgRNG() {
|
5605 | getRandomValues(rnds8);
|
5606 | return rnds8;
|
5607 | };
|
5608 | } else {
|
5609 |
|
5610 |
|
5611 |
|
5612 |
|
5613 | var rnds = new Array(16);
|
5614 |
|
5615 | module.exports = function mathRNG() {
|
5616 | for (var i = 0, r; i < 16; i++) {
|
5617 | if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
|
5618 | rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
5619 | }
|
5620 |
|
5621 | return rnds;
|
5622 | };
|
5623 | }
|
5624 |
|
5625 | },{}],26:[function(_dereq_,module,exports){
|
5626 | var rng = _dereq_('./lib/rng');
|
5627 | var bytesToUuid = _dereq_('./lib/bytesToUuid');
|
5628 |
|
5629 |
|
5630 |
|
5631 |
|
5632 |
|
5633 |
|
5634 | var _nodeId;
|
5635 | var _clockseq;
|
5636 |
|
5637 |
|
5638 | var _lastMSecs = 0;
|
5639 | var _lastNSecs = 0;
|
5640 |
|
5641 |
|
5642 | function v1(options, buf, offset) {
|
5643 | var i = buf && offset || 0;
|
5644 | var b = buf || [];
|
5645 |
|
5646 | options = options || {};
|
5647 | var node = options.node || _nodeId;
|
5648 | var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
|
5649 |
|
5650 |
|
5651 |
|
5652 |
|
5653 | if (node == null || clockseq == null) {
|
5654 | var seedBytes = rng();
|
5655 | if (node == null) {
|
5656 |
|
5657 | node = _nodeId = [
|
5658 | seedBytes[0] | 0x01,
|
5659 | seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
|
5660 | ];
|
5661 | }
|
5662 | if (clockseq == null) {
|
5663 |
|
5664 | clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
|
5665 | }
|
5666 | }
|
5667 |
|
5668 |
|
5669 |
|
5670 |
|
5671 |
|
5672 | var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
|
5673 |
|
5674 |
|
5675 |
|
5676 | var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
|
5677 |
|
5678 |
|
5679 | var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
|
5680 |
|
5681 |
|
5682 | if (dt < 0 && options.clockseq === undefined) {
|
5683 | clockseq = clockseq + 1 & 0x3fff;
|
5684 | }
|
5685 |
|
5686 |
|
5687 |
|
5688 | if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
|
5689 | nsecs = 0;
|
5690 | }
|
5691 |
|
5692 |
|
5693 | if (nsecs >= 10000) {
|
5694 | throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
|
5695 | }
|
5696 |
|
5697 | _lastMSecs = msecs;
|
5698 | _lastNSecs = nsecs;
|
5699 | _clockseq = clockseq;
|
5700 |
|
5701 |
|
5702 | msecs += 12219292800000;
|
5703 |
|
5704 |
|
5705 | var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
|
5706 | b[i++] = tl >>> 24 & 0xff;
|
5707 | b[i++] = tl >>> 16 & 0xff;
|
5708 | b[i++] = tl >>> 8 & 0xff;
|
5709 | b[i++] = tl & 0xff;
|
5710 |
|
5711 |
|
5712 | var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
|
5713 | b[i++] = tmh >>> 8 & 0xff;
|
5714 | b[i++] = tmh & 0xff;
|
5715 |
|
5716 |
|
5717 | b[i++] = tmh >>> 24 & 0xf | 0x10;
|
5718 | b[i++] = tmh >>> 16 & 0xff;
|
5719 |
|
5720 |
|
5721 | b[i++] = clockseq >>> 8 | 0x80;
|
5722 |
|
5723 |
|
5724 | b[i++] = clockseq & 0xff;
|
5725 |
|
5726 |
|
5727 | for (var n = 0; n < 6; ++n) {
|
5728 | b[i + n] = node[n];
|
5729 | }
|
5730 |
|
5731 | return buf ? buf : bytesToUuid(b);
|
5732 | }
|
5733 |
|
5734 | module.exports = v1;
|
5735 |
|
5736 | },{"./lib/bytesToUuid":24,"./lib/rng":25}],27:[function(_dereq_,module,exports){
|
5737 | var rng = _dereq_('./lib/rng');
|
5738 | var bytesToUuid = _dereq_('./lib/bytesToUuid');
|
5739 |
|
5740 | function v4(options, buf, offset) {
|
5741 | var i = buf && offset || 0;
|
5742 |
|
5743 | if (typeof(options) == 'string') {
|
5744 | buf = options === 'binary' ? new Array(16) : null;
|
5745 | options = null;
|
5746 | }
|
5747 | options = options || {};
|
5748 |
|
5749 | var rnds = options.random || (options.rng || rng)();
|
5750 |
|
5751 |
|
5752 | rnds[6] = (rnds[6] & 0x0f) | 0x40;
|
5753 | rnds[8] = (rnds[8] & 0x3f) | 0x80;
|
5754 |
|
5755 |
|
5756 | if (buf) {
|
5757 | for (var ii = 0; ii < 16; ++ii) {
|
5758 | buf[i + ii] = rnds[ii];
|
5759 | }
|
5760 | }
|
5761 |
|
5762 | return buf || bytesToUuid(rnds);
|
5763 | }
|
5764 |
|
5765 | module.exports = v4;
|
5766 |
|
5767 | },{"./lib/bytesToUuid":24,"./lib/rng":25}]},{},[1])(1)
|
5768 | });
|