UNPKG

161 kBJavaScriptView Raw
1/**
2 * SocketCluster JavaScript client v14.1.0
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){
5var SCClientSocket = _dereq_('./lib/scclientsocket');
6var factory = _dereq_('./lib/factory');
7
8module.exports.factory = factory;
9module.exports.SCClientSocket = SCClientSocket;
10
11module.exports.Emitter = _dereq_('component-emitter');
12
13module.exports.create = function (options) {
14 return factory.create(options);
15};
16
17module.exports.connect = module.exports.create;
18
19module.exports.destroy = function (socket) {
20 return factory.destroy(socket);
21};
22
23module.exports.clients = factory.clients;
24
25module.exports.version = '14.1.0';
26
27},{"./lib/factory":3,"./lib/scclientsocket":5,"component-emitter":12}],2:[function(_dereq_,module,exports){
28(function (global){
29var AuthEngine = function () {
30 this._internalStorage = {};
31 this.isLocalStorageEnabled = this._checkLocalStorageEnabled();
32};
33
34AuthEngine.prototype._checkLocalStorageEnabled = function () {
35 var err;
36 try {
37 // Some browsers will throw an error here if localStorage is disabled.
38 global.localStorage;
39
40 // Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
41 // throw QuotaExceededError. We're going to detect this and avoid hard to debug edge cases.
42 global.localStorage.setItem('__scLocalStorageTest', 1);
43 global.localStorage.removeItem('__scLocalStorageTest');
44 } catch (e) {
45 err = e;
46 }
47 return !err;
48};
49
50AuthEngine.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
59AuthEngine.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
75AuthEngine.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
86module.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){
91var SCClientSocket = _dereq_('./scclientsocket');
92var scErrors = _dereq_('sc-errors');
93var uuid = _dereq_('uuid');
94var InvalidArgumentsError = scErrors.InvalidArgumentsError;
95
96var _clients = {};
97
98function 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
126function isUrlSecure() {
127 return global.location && location.protocol == 'https:';
128}
129
130function 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
135function 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
203function destroy(socket) {
204 socket.destroy();
205}
206
207module.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){
215var scErrors = _dereq_('sc-errors');
216var InvalidActionError = scErrors.InvalidActionError;
217
218var Response = function (socket, id) {
219 this.socket = socket;
220 this.id = id;
221 this.sent = false;
222};
223
224Response.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
233Response.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
245Response.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
261Response.prototype.callback = function (error, data) {
262 if (error) {
263 this.error(error, data);
264 } else {
265 this.end(data);
266 }
267};
268
269module.exports.Response = Response;
270
271},{"sc-errors":21}],5:[function(_dereq_,module,exports){
272(function (global,Buffer){
273var Emitter = _dereq_('component-emitter');
274var SCChannel = _dereq_('sc-channel').SCChannel;
275var Response = _dereq_('./response').Response;
276var AuthEngine = _dereq_('./auth').AuthEngine;
277var formatter = _dereq_('sc-formatter');
278var SCTransport = _dereq_('./sctransport').SCTransport;
279var querystring = _dereq_('querystring');
280var LinkedList = _dereq_('linked-list');
281var base64 = _dereq_('base-64');
282var clone = _dereq_('clone');
283
284var scErrors = _dereq_('sc-errors');
285var InvalidArgumentsError = scErrors.InvalidArgumentsError;
286var InvalidMessageError = scErrors.InvalidMessageError;
287var InvalidActionError = scErrors.InvalidActionError;
288var SocketProtocolError = scErrors.SocketProtocolError;
289var TimeoutError = scErrors.TimeoutError;
290var BadConnectionError = scErrors.BadConnectionError;
291
292var isBrowser = typeof window != 'undefined';
293
294
295var 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 // pingTimeout will be ackTimeout at the start, but it will
317 // be updated with values provided by the 'connect' event
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 // Add properties to the this.options.autoReconnectOptions object.
374 // We assign the reference to a reconnectOptions variable to avoid repetition.
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 // Default codec engine
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
437SCClientSocket.prototype = Object.create(Emitter.prototype);
438
439SCClientSocket.CONNECTING = SCClientSocket.prototype.CONNECTING = SCTransport.prototype.CONNECTING;
440SCClientSocket.OPEN = SCClientSocket.prototype.OPEN = SCTransport.prototype.OPEN;
441SCClientSocket.CLOSED = SCClientSocket.prototype.CLOSED = SCTransport.prototype.CLOSED;
442
443SCClientSocket.AUTHENTICATED = SCClientSocket.prototype.AUTHENTICATED = 'authenticated';
444SCClientSocket.UNAUTHENTICATED = SCClientSocket.prototype.UNAUTHENTICATED = 'unauthenticated';
445
446SCClientSocket.PENDING = SCClientSocket.prototype.PENDING = 'pending';
447
448SCClientSocket.ignoreStatuses = scErrors.socketProtocolIgnoreStatuses;
449SCClientSocket.errorStatuses = scErrors.socketProtocolErrorStatuses;
450
451SCClientSocket.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 // This is a non-fatal error, we don't want to close the connection
476 // because of this but we do want to notify the server and throw an error
477 // on the client.
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 // Non-fatal error - Do not close the connection
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
511SCClientSocket.prototype.getState = function () {
512 return this.state;
513};
514
515SCClientSocket.prototype.getBytesReceived = function () {
516 return this.transport.getBytesReceived();
517};
518
519SCClientSocket.prototype.deauthenticate = function (callback) {
520 var self = this;
521
522 this.auth.removeToken(this.authTokenName, function (err, oldToken) {
523 if (err) {
524 // Non-fatal error - Do not close the connection
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
537SCClientSocket.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
585SCClientSocket.prototype.reconnect = function (code, data) {
586 this.disconnect(code, data);
587 this.connect();
588};
589
590SCClientSocket.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
606SCClientSocket.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
615SCClientSocket.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
632SCClientSocket.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
654SCClientSocket.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
669SCClientSocket.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
684SCClientSocket.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
699SCClientSocket.prototype.getAuthToken = function () {
700 return this.authToken;
701};
702
703SCClientSocket.prototype.getSignedAuthToken = function () {
704 return this.signedAuthToken;
705};
706
707// Perform client-initiated authentication by providing an encrypted token string.
708SCClientSocket.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 // If authStatus is correctly formatted (has an isAuthenticated property),
714 // then we will rehydrate the authError.
715 if (authStatus.authError) {
716 authStatus.authError = scErrors.hydrateError(authStatus.authError);
717 }
718 } else {
719 // Some errors like BadConnectionError and TimeoutError will not pass a valid
720 // authStatus object to the current function, so we need to create it ourselves.
721 authStatus = {
722 isAuthenticated: self.authState,
723 authError: null
724 };
725 }
726 if (err) {
727 if (err.name != 'BadConnectionError' && err.name != 'TimeoutError') {
728 // In case of a bad/closed connection or a timeout, we maintain the last
729 // known auth state since those errors don't mean that the token is invalid.
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
750SCClientSocket.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
778SCClientSocket.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 // This can happen if auth.loadToken (in sctransport.js) fails with
794 // an error - This means that the signedAuthToken cannot be loaded by
795 // the auth engine and therefore, we need to unauthenticate the client.
796 this._changeToUnauthenticatedStateAndClearTokens();
797 }
798
799 this.connectAttempts = 0;
800
801 if (this.options.autoSubscribeOnConnect) {
802 this.processPendingSubscriptions();
803 }
804
805 // If the user invokes the callback while in autoSubscribeOnConnect mode, it
806 // won't break anything.
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
816SCClientSocket.prototype._onSCError = function (err) {
817 var self = this;
818
819 // Throw error in different stack frame so that error handling
820 // cannot interfere with a reconnect action.
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
830SCClientSocket.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
848SCClientSocket.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 // Cleanup any pending response callback in the transport layer too.
869 if (eventObject.cid) {
870 this.transport.cancelPendingResponse(eventObject.cid);
871 }
872 }
873};
874
875SCClientSocket.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 // Try to reconnect
891 // on server ping timeout (4000)
892 // or on client pong timeout (4001)
893 // or on close without status (1005)
894 // or on handshake failure (4003)
895 // or on handshake rejection (4008)
896 // or on socket hung up (1006)
897 if (this.options.autoReconnect) {
898 if (code == 4000 || code == 4001 || code == 1005) {
899 // If there is a ping or pong timeout or socket closes without
900 // status, don't wait before trying to reconnect - These could happen
901 // if the client wakes up after a period of inactivity and in this case we
902 // want to re-establish the connection as soon as possible.
903 this._tryReconnect(0);
904
905 // Codes 4500 and above will be treated as permanent disconnects.
906 // Socket will not try to auto-reconnect.
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
931SCClientSocket.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
942SCClientSocket.prototype.decode = function (message) {
943 return this.transport.decode(message);
944};
945
946SCClientSocket.prototype.encode = function (object) {
947 return this.transport.encode(object);
948};
949
950SCClientSocket.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
963SCClientSocket.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 // Cleanup any pending response callback in the transport layer too.
976 if (eventObject.cid) {
977 this.transport.cancelPendingResponse(eventObject.cid);
978 }
979};
980
981SCClientSocket.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
1011SCClientSocket.prototype.send = function (data) {
1012 this.transport.send(data);
1013};
1014
1015SCClientSocket.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
1026SCClientSocket.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
1034SCClientSocket.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
1054SCClientSocket.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// Cancel any pending subscribe callback
1067SCClientSocket.prototype._cancelPendingSubscribeCallback = function (channel) {
1068 if (channel._pendingSubscriptionCid != null) {
1069 this.transport.cancelPendingResponse(channel._pendingSubscriptionCid);
1070 delete channel._pendingSubscriptionCid;
1071 }
1072};
1073
1074SCClientSocket.prototype._decorateChannelName = function (channelName) {
1075 if (this.channelPrefix) {
1076 channelName = this.channelPrefix + channelName;
1077 }
1078 return channelName;
1079};
1080
1081SCClientSocket.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
1088SCClientSocket.prototype._trySubscribe = function (channel) {
1089 var self = this;
1090
1091 var meetsAuthRequirements = !channel.waitForAuth || this.authState == this.AUTHENTICATED;
1092
1093 // We can only ever have one pending subscribe action at any given time on a channel
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
1131SCClientSocket.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
1149SCClientSocket.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
1173SCClientSocket.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 // If there is a pending subscribe action, cancel the callback
1184 this._cancelPendingSubscribeCallback(channel);
1185
1186 // This operation cannot fail because the TCP protocol guarantees delivery
1187 // so long as the connection remains open. If the connection closes,
1188 // the server will automatically unsubscribe the client and thus complete
1189 // the operation on the server side.
1190 var decoratedChannelName = this._decorateChannelName(channel.name);
1191 this.transport.emit('#unsubscribe', decoratedChannelName, options);
1192 }
1193};
1194
1195SCClientSocket.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
1207SCClientSocket.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
1217SCClientSocket.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
1227SCClientSocket.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
1249SCClientSocket.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
1258SCClientSocket.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
1291SCClientSocket.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
1298SCClientSocket.prototype.unwatch = function (channelName, handler) {
1299 if (handler) {
1300 this._channelEmitter.removeListener(channelName, handler);
1301 } else {
1302 this._channelEmitter.removeAllListeners(channelName);
1303 }
1304};
1305
1306SCClientSocket.prototype.watchers = function (channelName) {
1307 return this._channelEmitter.listeners(channelName);
1308};
1309
1310module.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){
1315var Emitter = _dereq_('component-emitter');
1316var Response = _dereq_('./response').Response;
1317var querystring = _dereq_('querystring');
1318var WebSocket;
1319var createWebSocket;
1320
1321if (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
1333var scErrors = _dereq_('sc-errors');
1334var TimeoutError = scErrors.TimeoutError;
1335var BadConnectionError = scErrors.BadConnectionError;
1336
1337
1338var 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 // Open the connection.
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 // This is to handle an edge case in React Native whereby
1373 // event.code is undefined when the mobile device is locked.
1374 // TODO: This is not perfect since this condition could also apply to
1375 // an abnormal close (no close control frame) which would be a 1006.
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 // The onclose event will be called automatically after the onerror event
1389 // if the socket is connected - Otherwise, if it's in the middle of
1390 // connecting, we want to close it manually with a 1006 - This is necessary
1391 // to prevent inconsistent behavior when running the client in Node.js
1392 // vs in a browser.
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
1405SCTransport.prototype = Object.create(Emitter.prototype);
1406
1407SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting';
1408SCTransport.OPEN = SCTransport.prototype.OPEN = 'open';
1409SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed';
1410
1411SCTransport.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
1441SCTransport.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
1466SCTransport.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 // Don't wait for this.state to be 'open'.
1473 // The underlying WebSocket (this.socket) is already open.
1474 var options = {
1475 force: true
1476 };
1477 self.emit('#handshake', {
1478 authToken: token
1479 }, options, function (err, status) {
1480 if (status) {
1481 // Add the token which was used as part of authentication attempt
1482 // to the status object.
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
1494SCTransport.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
1514SCTransport.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
1536SCTransport.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
1557SCTransport.prototype._onMessage = function (message) {
1558 Emitter.prototype.emit.call(this, 'event', 'message', message);
1559
1560 var obj = this.decode(message);
1561
1562 // If ping
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
1580SCTransport.prototype._onError = function (err) {
1581 Emitter.prototype.emit.call(this, 'error', err);
1582};
1583
1584SCTransport.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
1599SCTransport.prototype.getBytesReceived = function () {
1600 return this.socket.bytesReceived;
1601};
1602
1603SCTransport.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
1622SCTransport.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
1638SCTransport.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// The last two optional arguments (a and b) can be options and/or callback
1653SCTransport.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
1689SCTransport.prototype.cancelPendingResponse = function (cid) {
1690 delete this._callbackMap[cid];
1691};
1692
1693SCTransport.prototype.decode = function (message) {
1694 return this.codec.decode(message);
1695};
1696
1697SCTransport.prototype.encode = function (object) {
1698 return this.codec.encode(object);
1699};
1700
1701SCTransport.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
1709SCTransport.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
1723SCTransport.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
1743SCTransport.prototype.sendObjectSingle = function (object) {
1744 var str = this.serializeObject(object);
1745 if (str != null) {
1746 this.send(str);
1747 }
1748};
1749
1750SCTransport.prototype.sendObject = function (object, options) {
1751 if (options && options.batch) {
1752 this.sendObjectBatch(object);
1753 } else {
1754 this.sendObjectSingle(object);
1755 }
1756};
1757
1758module.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){
1762var global;
1763if (typeof WorkerGlobalScope !== 'undefined') {
1764 global = self;
1765} else {
1766 global = typeof window != 'undefined' && window || (function() { return this; })();
1767}
1768
1769var WebSocket = global.WebSocket || global.MozWebSocket;
1770
1771/**
1772 * WebSocket constructor.
1773 *
1774 * The third `opts` options object gets ignored in web browsers, since it's
1775 * non-standard, and throws a TypeError if passed to the constructor.
1776 * See: https://github.com/einaros/ws/issues/227
1777 *
1778 * @param {String} uri
1779 * @param {Array} protocols (optional)
1780 * @param {Object} opts (optional)
1781 * @api public
1782 */
1783
1784function 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
1794if (WebSocket) ws.prototype = WebSocket.prototype;
1795
1796module.exports = WebSocket ? ws : null;
1797
1798},{}],8:[function(_dereq_,module,exports){
1799(function (global){
1800/*! http://mths.be/base64 v0.1.0 by @mathias | MIT license */
1801;(function(root) {
1802
1803 // Detect free variables `exports`.
1804 var freeExports = typeof exports == 'object' && exports;
1805
1806 // Detect free variable `module`.
1807 var freeModule = typeof module == 'object' && module &&
1808 module.exports == freeExports && module;
1809
1810 // Detect free variable `global`, from Node.js or Browserified code, and use
1811 // it as `root`.
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 // Note: the error messages used throughout this file match those used by
1827 // the native `atob`/`btoa` implementation in Chromium.
1828 throw new InvalidCharacterError(message);
1829 };
1830
1831 var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
1832 // http://whatwg.org/html/common-microsyntaxes.html#space-character
1833 var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g;
1834
1835 // `decode` is designed to be fully compatible with `atob` as described in the
1836 // HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob
1837 // The optimized base64-decoding algorithm used is based on @atk’s excellent
1838 // implementation. https://gist.github.com/atk/1020396
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 // http://whatwg.org/C#alphanumeric-ascii-characters
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 // Unless this is the first of a group of 4 characters…
1865 if (bitCounter++ % 4) {
1866 // …convert the first 8 bits to a single ASCII character.
1867 output += String.fromCharCode(
1868 0xFF & bitStorage >> (-2 * bitCounter & 6)
1869 );
1870 }
1871 }
1872 return output;
1873 };
1874
1875 // `encode` is designed to be fully compatible with `btoa` as described in the
1876 // HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa
1877 var encode = function(input) {
1878 input = String(input);
1879 if (/[^\0-\xFF]/.test(input)) {
1880 // Note: no need to special-case astral symbols here, as surrogates are
1881 // matched, and the input is supposed to only contain ASCII anyway.
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 // Make sure any padding is handled outside of the loop.
1896 var length = input.length - padding;
1897
1898 while (++position < length) {
1899 // Read three bytes, i.e. 24 bits.
1900 a = input.charCodeAt(position) << 16;
1901 b = input.charCodeAt(++position) << 8;
1902 c = input.charCodeAt(++position);
1903 buffer = a + b + c;
1904 // Turn the 24 bits into four chunks of 6 bits each, and append the
1905 // matching character for each of them to the output.
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 // Some AMD build optimizers, like r.js, check for specific condition patterns
1943 // like the following:
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) { // in Node.js or RingoJS v0.8.0+
1954 freeModule.exports = base64;
1955 } else { // in Narwhal or RingoJS v0.7.0-
1956 for (var key in base64) {
1957 base64.hasOwnProperty(key) && (freeExports[key] = base64[key]);
1958 }
1959 }
1960 } else { // in Rhino or a web browser
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
1970exports.byteLength = byteLength
1971exports.toByteArray = toByteArray
1972exports.fromByteArray = fromByteArray
1973
1974var lookup = []
1975var revLookup = []
1976var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
1977
1978var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
1979for (var i = 0, len = code.length; i < len; ++i) {
1980 lookup[i] = code[i]
1981 revLookup[code.charCodeAt(i)] = i
1982}
1983
1984// Support decoding URL-safe base64 strings, as Node.js does.
1985// See: https://en.wikipedia.org/wiki/Base64#URL_applications
1986revLookup['-'.charCodeAt(0)] = 62
1987revLookup['_'.charCodeAt(0)] = 63
1988
1989function 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 // Trim off extra bytes after placeholder bytes are found
1997 // See: https://github.com/beatgammit/base64-js/issues/42
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// base64 is 4/3 + up to two characters of the original data
2009function 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
2016function _byteLength (b64, validLen, placeHoldersLen) {
2017 return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
2018}
2019
2020function 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 // if there are placeholders, only get up to the last complete 4 chars
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
2065function tripletToBase64 (num) {
2066 return lookup[num >> 18 & 0x3F] +
2067 lookup[num >> 12 & 0x3F] +
2068 lookup[num >> 6 & 0x3F] +
2069 lookup[num & 0x3F]
2070}
2071
2072function 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
2085function fromByteArray (uint8) {
2086 var tmp
2087 var len = uint8.length
2088 var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
2089 var parts = []
2090 var maxChunkLength = 16383 // must be multiple of 3
2091
2092 // go through the array every three bytes, we'll deal with trailing stuff later
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 // pad the end with zeros, but make sure to not forget the extra bytes
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 * The buffer module from node.js, for the browser.
2123 *
2124 * @author Feross Aboukhadijeh <https://feross.org>
2125 * @license MIT
2126 */
2127/* eslint-disable no-proto */
2128
2129'use strict'
2130
2131var base64 = _dereq_('base64-js')
2132var ieee754 = _dereq_('ieee754')
2133
2134exports.Buffer = Buffer
2135exports.SlowBuffer = SlowBuffer
2136exports.INSPECT_MAX_BYTES = 50
2137
2138var K_MAX_LENGTH = 0x7fffffff
2139exports.kMaxLength = K_MAX_LENGTH
2140
2141/**
2142 * If `Buffer.TYPED_ARRAY_SUPPORT`:
2143 * === true Use Uint8Array implementation (fastest)
2144 * === false Print warning and recommend using `buffer` v4.x which has an Object
2145 * implementation (most compatible, even IE6)
2146 *
2147 * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
2148 * Opera 11.6+, iOS 4.2+.
2149 *
2150 * We report that the browser does not support typed arrays if the are not subclassable
2151 * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
2152 * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
2153 * for __proto__ and has a buggy typed array implementation.
2154 */
2155Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
2156
2157if (!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
2165function typedArraySupport () {
2166 // Can typed array instances can be augmented?
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
2176Object.defineProperty(Buffer.prototype, 'parent', {
2177 enumerable: true,
2178 get: function () {
2179 if (!Buffer.isBuffer(this)) return undefined
2180 return this.buffer
2181 }
2182})
2183
2184Object.defineProperty(Buffer.prototype, 'offset', {
2185 enumerable: true,
2186 get: function () {
2187 if (!Buffer.isBuffer(this)) return undefined
2188 return this.byteOffset
2189 }
2190})
2191
2192function createBuffer (length) {
2193 if (length > K_MAX_LENGTH) {
2194 throw new RangeError('The value "' + length + '" is invalid for option "size"')
2195 }
2196 // Return an augmented `Uint8Array` instance
2197 var buf = new Uint8Array(length)
2198 buf.__proto__ = Buffer.prototype
2199 return buf
2200}
2201
2202/**
2203 * The Buffer constructor returns instances of `Uint8Array` that have their
2204 * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
2205 * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
2206 * and the `Uint8Array` methods. Square bracket notation works as expected -- it
2207 * returns a single octet.
2208 *
2209 * The `Uint8Array` prototype remains unmodified.
2210 */
2211
2212function Buffer (arg, encodingOrOffset, length) {
2213 // Common case.
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// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
2226if (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
2236Buffer.poolSize = 8192 // not used by this implementation
2237
2238function 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 * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
2288 * if value is a number.
2289 * Buffer.from(str[, encoding])
2290 * Buffer.from(array)
2291 * Buffer.from(buffer)
2292 * Buffer.from(arrayBuffer[, byteOffset[, length]])
2293 **/
2294Buffer.from = function (value, encodingOrOffset, length) {
2295 return from(value, encodingOrOffset, length)
2296}
2297
2298// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
2299// https://github.com/feross/buffer/pull/148
2300Buffer.prototype.__proto__ = Uint8Array.prototype
2301Buffer.__proto__ = Uint8Array
2302
2303function 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
2311function alloc (size, fill, encoding) {
2312 assertSize(size)
2313 if (size <= 0) {
2314 return createBuffer(size)
2315 }
2316 if (fill !== undefined) {
2317 // Only pay attention to encoding if it's a string. This
2318 // prevents accidentally sending in a number that would
2319 // be interpretted as a start offset.
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 * Creates a new filled Buffer instance.
2329 * alloc(size[, fill[, encoding]])
2330 **/
2331Buffer.alloc = function (size, fill, encoding) {
2332 return alloc(size, fill, encoding)
2333}
2334
2335function allocUnsafe (size) {
2336 assertSize(size)
2337 return createBuffer(size < 0 ? 0 : checked(size) | 0)
2338}
2339
2340/**
2341 * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
2342 * */
2343Buffer.allocUnsafe = function (size) {
2344 return allocUnsafe(size)
2345}
2346/**
2347 * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
2348 */
2349Buffer.allocUnsafeSlow = function (size) {
2350 return allocUnsafe(size)
2351}
2352
2353function 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 // Writing a hex string, for example, that contains invalid characters will
2369 // cause everything after the first invalid character to be ignored. (e.g.
2370 // 'abxxcd' will be treated as 'ab')
2371 buf = buf.slice(0, actual)
2372 }
2373
2374 return buf
2375}
2376
2377function 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
2386function 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 // Return an augmented `Uint8Array` instance
2405 buf.__proto__ = Buffer.prototype
2406 return buf
2407}
2408
2409function 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
2434function checked (length) {
2435 // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
2436 // length is NaN (which is otherwise coerced to zero.)
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
2444function SlowBuffer (length) {
2445 if (+length != length) { // eslint-disable-line eqeqeq
2446 length = 0
2447 }
2448 return Buffer.alloc(+length)
2449}
2450
2451Buffer.isBuffer = function isBuffer (b) {
2452 return b != null && b._isBuffer === true &&
2453 b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
2454}
2455
2456Buffer.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
2483Buffer.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
2502Buffer.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
2535function 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 // Use a for loop to avoid recursion
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 // assume utf8
2576 }
2577 encoding = ('' + encoding).toLowerCase()
2578 loweredCase = true
2579 }
2580 }
2581}
2582Buffer.byteLength = byteLength
2583
2584function slowToString (encoding, start, end) {
2585 var loweredCase = false
2586
2587 // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
2588 // property of a typed array.
2589
2590 // This behaves neither like String nor Uint8Array in that we set start/end
2591 // to their upper/lower bounds if the value passed is out of range.
2592 // undefined is handled specially as per ECMA-262 6th Edition,
2593 // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
2594 if (start === undefined || start < 0) {
2595 start = 0
2596 }
2597 // Return early if start > this.length. Done here to prevent potential uint32
2598 // coercion fail below.
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 // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
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// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
2655// to detect a Buffer instance. It's not possible to use `instanceof Buffer`
2656// reliably in a browserify context because there could be multiple different
2657// copies of the 'buffer' package in use. This method works even for Buffer
2658// instances that were created from another copy of the `buffer` package.
2659// See: https://github.com/feross/buffer/issues/154
2660Buffer.prototype._isBuffer = true
2661
2662function swap (b, n, m) {
2663 var i = b[n]
2664 b[n] = b[m]
2665 b[m] = i
2666}
2667
2668Buffer.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
2679Buffer.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
2691Buffer.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
2705Buffer.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
2712Buffer.prototype.toLocaleString = Buffer.prototype.toString
2713
2714Buffer.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
2720Buffer.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
2728Buffer.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// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
2794// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
2795//
2796// Arguments:
2797// - buffer - a Buffer to search
2798// - val - a string, Buffer, or number
2799// - byteOffset - an index into `buffer`; will be clamped to an int32
2800// - encoding - an optional encoding, relevant is val is a string
2801// - dir - true for indexOf, false for lastIndexOf
2802function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
2803 // Empty buffer means no match
2804 if (buffer.length === 0) return -1
2805
2806 // Normalize byteOffset
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 // Coerce to Number.
2816 if (numberIsNaN(byteOffset)) {
2817 // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
2818 byteOffset = dir ? 0 : (buffer.length - 1)
2819 }
2820
2821 // Normalize byteOffset: negative offsets start from the end of the buffer
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 // Normalize val
2832 if (typeof val === 'string') {
2833 val = Buffer.from(val, encoding)
2834 }
2835
2836 // Finally, search either indexOf (if dir is true) or lastIndexOf
2837 if (Buffer.isBuffer(val)) {
2838 // Special case: looking for empty string/buffer always fails
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 // Search for a byte value [0-255]
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
2858function 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
2914Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
2915 return this.indexOf(val, byteOffset, encoding) !== -1
2916}
2917
2918Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
2919 return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
2920}
2921
2922Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
2923 return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
2924}
2925
2926function 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
2951function utf8Write (buf, string, offset, length) {
2952 return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
2953}
2954
2955function asciiWrite (buf, string, offset, length) {
2956 return blitBuffer(asciiToBytes(string), buf, offset, length)
2957}
2958
2959function latin1Write (buf, string, offset, length) {
2960 return asciiWrite(buf, string, offset, length)
2961}
2962
2963function base64Write (buf, string, offset, length) {
2964 return blitBuffer(base64ToBytes(string), buf, offset, length)
2965}
2966
2967function ucs2Write (buf, string, offset, length) {
2968 return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
2969}
2970
2971Buffer.prototype.write = function write (string, offset, length, encoding) {
2972 // Buffer#write(string)
2973 if (offset === undefined) {
2974 encoding = 'utf8'
2975 length = this.length
2976 offset = 0
2977 // Buffer#write(string, encoding)
2978 } else if (length === undefined && typeof offset === 'string') {
2979 encoding = offset
2980 length = this.length
2981 offset = 0
2982 // Buffer#write(string, offset[, length][, encoding])
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 // Warning: maxLength not taken into account in base64Write
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
3042Buffer.prototype.toJSON = function toJSON () {
3043 return {
3044 type: 'Buffer',
3045 data: Array.prototype.slice.call(this._arr || this, 0)
3046 }
3047}
3048
3049function 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
3057function 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 // we did not generate a valid codePoint so insert a
3113 // replacement char (U+FFFD) and advance only 1 byte
3114 codePoint = 0xFFFD
3115 bytesPerSequence = 1
3116 } else if (codePoint > 0xFFFF) {
3117 // encode to utf16 (surrogate pair dance)
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// Based on http://stackoverflow.com/a/22747272/680742, the browser with
3131// the lowest limit is Chrome, with 0x10000 args.
3132// We go 1 magnitude less, for safety
3133var MAX_ARGUMENTS_LENGTH = 0x1000
3134
3135function decodeCodePointsArray (codePoints) {
3136 var len = codePoints.length
3137 if (len <= MAX_ARGUMENTS_LENGTH) {
3138 return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
3139 }
3140
3141 // Decode in chunks to avoid "call stack size exceeded".
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
3153function 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
3163function 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
3173function 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
3186function 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
3195Buffer.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 // Return an augmented `Uint8Array` instance
3218 newBuf.__proto__ = Buffer.prototype
3219 return newBuf
3220}
3221
3222/*
3223 * Need to make sure that buffer isn't trying to write out of bounds.
3224 */
3225function 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
3230Buffer.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
3245Buffer.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
3261Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
3262 offset = offset >>> 0
3263 if (!noAssert) checkOffset(offset, 1, this.length)
3264 return this[offset]
3265}
3266
3267Buffer.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
3273Buffer.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
3279Buffer.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
3289Buffer.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
3299Buffer.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
3317Buffer.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
3335Buffer.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
3342Buffer.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
3349Buffer.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
3356Buffer.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
3366Buffer.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
3376Buffer.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
3382Buffer.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
3388Buffer.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
3394Buffer.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
3400function 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
3406Buffer.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
3425Buffer.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
3444Buffer.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
3452Buffer.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
3461Buffer.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
3470Buffer.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
3481Buffer.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
3492Buffer.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
3515Buffer.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
3538Buffer.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
3547Buffer.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
3556Buffer.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
3565Buffer.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
3576Buffer.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
3588function 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
3593function 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
3603Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
3604 return writeFloat(this, value, offset, true, noAssert)
3605}
3606
3607Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
3608 return writeFloat(this, value, offset, false, noAssert)
3609}
3610
3611function 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
3621Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
3622 return writeDouble(this, value, offset, true, noAssert)
3623}
3624
3625Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
3626 return writeDouble(this, value, offset, false, noAssert)
3627}
3628
3629// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
3630Buffer.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 // Copy 0 bytes; we're done
3639 if (end === start) return 0
3640 if (target.length === 0 || this.length === 0) return 0
3641
3642 // Fatal error conditions
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 // Are we oob?
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 // Use built-in when available, missing from IE11
3659 this.copyWithin(targetStart, start, end)
3660 } else if (this === target && start < targetStart && targetStart < end) {
3661 // descending copy from end
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// Usage:
3677// buffer.fill(number[, offset[, end]])
3678// buffer.fill(buffer[, offset[, end]])
3679// buffer.fill(string[, offset[, end]][, encoding])
3680Buffer.prototype.fill = function fill (val, start, end, encoding) {
3681 // Handle string cases:
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 // Fast path: If `val` fits into a single byte, use that numeric value.
3702 val = code
3703 }
3704 }
3705 } else if (typeof val === 'number') {
3706 val = val & 255
3707 }
3708
3709 // Invalid ranges are not set to a default, so can range check early.
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// HELPER FUNCTIONS
3746// ================
3747
3748var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
3749
3750function base64clean (str) {
3751 // Node takes equal signs as end of the Base64 encoding
3752 str = str.split('=')[0]
3753 // Node strips out invalid characters like \n and \t from the string, base64-js does not
3754 str = str.trim().replace(INVALID_BASE64_RE, '')
3755 // Node converts strings with length < 2 to ''
3756 if (str.length < 2) return ''
3757 // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
3758 while (str.length % 4 !== 0) {
3759 str = str + '='
3760 }
3761 return str
3762}
3763
3764function toHex (n) {
3765 if (n < 16) return '0' + n.toString(16)
3766 return n.toString(16)
3767}
3768
3769function 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 // is surrogate component
3780 if (codePoint > 0xD7FF && codePoint < 0xE000) {
3781 // last char was a lead
3782 if (!leadSurrogate) {
3783 // no lead yet
3784 if (codePoint > 0xDBFF) {
3785 // unexpected trail
3786 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
3787 continue
3788 } else if (i + 1 === length) {
3789 // unpaired lead
3790 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
3791 continue
3792 }
3793
3794 // valid lead
3795 leadSurrogate = codePoint
3796
3797 continue
3798 }
3799
3800 // 2 leads in a row
3801 if (codePoint < 0xDC00) {
3802 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
3803 leadSurrogate = codePoint
3804 continue
3805 }
3806
3807 // valid surrogate pair
3808 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
3809 } else if (leadSurrogate) {
3810 // valid bmp char, but last char was a lead
3811 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
3812 }
3813
3814 leadSurrogate = null
3815
3816 // encode utf8
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
3849function asciiToBytes (str) {
3850 var byteArray = []
3851 for (var i = 0; i < str.length; ++i) {
3852 // Node's code seems to be doing this and not & 0x7F..
3853 byteArray.push(str.charCodeAt(i) & 0xFF)
3854 }
3855 return byteArray
3856}
3857
3858function 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
3874function base64ToBytes (str) {
3875 return base64.toByteArray(base64clean(str))
3876}
3877
3878function 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// ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
3887// the `instanceof` check but they should be treated as of that type.
3888// See: https://github.com/feross/buffer/issues/166
3889function 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}
3894function numberIsNaN (obj) {
3895 // For IE11 support
3896 return obj !== obj // eslint-disable-line no-self-compare
3897}
3898
3899},{"base64-js":9,"ieee754":13}],11:[function(_dereq_,module,exports){
3900(function (Buffer){
3901var clone = (function() {
3902'use strict';
3903
3904function _instanceof(obj, type) {
3905 return type != null && obj instanceof type;
3906}
3907
3908var nativeMap;
3909try {
3910 nativeMap = Map;
3911} catch(_) {
3912 // maybe a reference error because no `Map`. Give it a dummy value that no
3913 // value will ever be an instanceof.
3914 nativeMap = function() {};
3915}
3916
3917var nativeSet;
3918try {
3919 nativeSet = Set;
3920} catch(_) {
3921 nativeSet = function() {};
3922}
3923
3924var nativePromise;
3925try {
3926 nativePromise = Promise;
3927} catch(_) {
3928 nativePromise = function() {};
3929}
3930
3931/**
3932 * Clones (copies) an Object using deep copying.
3933 *
3934 * This function supports circular references by default, but if you are certain
3935 * there are no circular references in your object, you can save some CPU time
3936 * by calling clone(obj, false).
3937 *
3938 * Caution: if `circular` is false and `parent` contains circular references,
3939 * your program may enter an infinite loop and crash.
3940 *
3941 * @param `parent` - the object to be cloned
3942 * @param `circular` - set to true if the object to be cloned may contain
3943 * circular references. (optional - true by default)
3944 * @param `depth` - set to a number if the object is only to be cloned to
3945 * a particular depth. (optional - defaults to Infinity)
3946 * @param `prototype` - sets the prototype to be used when cloning an object.
3947 * (optional - defaults to parent prototype).
3948 * @param `includeNonEnumerable` - set to true if the non-enumerable properties
3949 * should be cloned as well. Non-enumerable properties on the prototype
3950 * chain will be ignored. (optional - false by default)
3951*/
3952function 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 // maintain two arrays for circular references, where corresponding parents
3960 // and children have the same index
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 // recurse this function so we don't reset allParents and allChildren
3973 function _clone(parent, depth) {
3974 // cloning null always returns null
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 // Don't need to worry about cloning a symbol because it is a primitive,
4063 // like a number or string.
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 * Simple flat clone using prototype, accepts only objects, usefull for property
4101 * override on FLAT configuration object (no nested props).
4102 *
4103 * USE WITH CAUTION! This may not behave as you wish if you do not know how this
4104 * works.
4105 */
4106clone.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// private utility functions
4116
4117function __objToStr(o) {
4118 return Object.prototype.toString.call(o);
4119}
4120clone.__objToStr = __objToStr;
4121
4122function __isDate(o) {
4123 return typeof o === 'object' && __objToStr(o) === '[object Date]';
4124}
4125clone.__isDate = __isDate;
4126
4127function __isArray(o) {
4128 return typeof o === 'object' && __objToStr(o) === '[object Array]';
4129}
4130clone.__isArray = __isArray;
4131
4132function __isRegExp(o) {
4133 return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
4134}
4135clone.__isRegExp = __isRegExp;
4136
4137function __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}
4144clone.__getRegExpFlags = __getRegExpFlags;
4145
4146return clone;
4147})();
4148
4149if (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 * Expose `Emitter`.
4158 */
4159
4160if (typeof module !== 'undefined') {
4161 module.exports = Emitter;
4162}
4163
4164/**
4165 * Initialize a new `Emitter`.
4166 *
4167 * @api public
4168 */
4169
4170function Emitter(obj) {
4171 if (obj) return mixin(obj);
4172};
4173
4174/**
4175 * Mixin the emitter properties.
4176 *
4177 * @param {Object} obj
4178 * @return {Object}
4179 * @api private
4180 */
4181
4182function mixin(obj) {
4183 for (var key in Emitter.prototype) {
4184 obj[key] = Emitter.prototype[key];
4185 }
4186 return obj;
4187}
4188
4189/**
4190 * Listen on the given `event` with `fn`.
4191 *
4192 * @param {String} event
4193 * @param {Function} fn
4194 * @return {Emitter}
4195 * @api public
4196 */
4197
4198Emitter.prototype.on =
4199Emitter.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 * Adds an `event` listener that will be invoked a single
4208 * time then automatically removed.
4209 *
4210 * @param {String} event
4211 * @param {Function} fn
4212 * @return {Emitter}
4213 * @api public
4214 */
4215
4216Emitter.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 * Remove the given callback for `event` or all
4229 * registered callbacks.
4230 *
4231 * @param {String} event
4232 * @param {Function} fn
4233 * @return {Emitter}
4234 * @api public
4235 */
4236
4237Emitter.prototype.off =
4238Emitter.prototype.removeListener =
4239Emitter.prototype.removeAllListeners =
4240Emitter.prototype.removeEventListener = function(event, fn){
4241 this._callbacks = this._callbacks || {};
4242
4243 // all
4244 if (0 == arguments.length) {
4245 this._callbacks = {};
4246 return this;
4247 }
4248
4249 // specific event
4250 var callbacks = this._callbacks['$' + event];
4251 if (!callbacks) return this;
4252
4253 // remove all handlers
4254 if (1 == arguments.length) {
4255 delete this._callbacks['$' + event];
4256 return this;
4257 }
4258
4259 // remove specific handler
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 * Emit `event` with the given args.
4273 *
4274 * @param {String} event
4275 * @param {Mixed} ...
4276 * @return {Emitter}
4277 */
4278
4279Emitter.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 * Return array of callbacks for `event`.
4296 *
4297 * @param {String} event
4298 * @return {Array}
4299 * @api public
4300 */
4301
4302Emitter.prototype.listeners = function(event){
4303 this._callbacks = this._callbacks || {};
4304 return this._callbacks['$' + event] || [];
4305};
4306
4307/**
4308 * Check if this emitter has `event` handlers.
4309 *
4310 * @param {String} event
4311 * @return {Boolean}
4312 * @api public
4313 */
4314
4315Emitter.prototype.hasListeners = function(event){
4316 return !! this.listeners(event).length;
4317};
4318
4319},{}],13:[function(_dereq_,module,exports){
4320exports.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
4353exports.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 * Constants.
4410 */
4411
4412var errorMessage;
4413
4414errorMessage = 'An argument without append, prepend, ' +
4415 'or detach methods was given to `List';
4416
4417/**
4418 * Creates a new List: A linked list is a bit like an Array, but
4419 * knows nothing about how many items are in it, and knows only about its
4420 * first (`head`) and last (`tail`) items. Each item (e.g. `head`, `tail`,
4421 * &c.) knows which item comes before or after it (its more like the
4422 * implementation of the DOM in JavaScript).
4423 * @global
4424 * @private
4425 * @constructor
4426 * @class Represents an instance of List.
4427 */
4428
4429function List(/*items...*/) {
4430 if (arguments.length) {
4431 return List.from(arguments);
4432 }
4433}
4434
4435var ListPrototype;
4436
4437ListPrototype = List.prototype;
4438
4439/**
4440 * Creates a new list from the arguments (each a list item) passed in.
4441 * @name List.of
4442 * @param {...ListItem} [items] - Zero or more items to attach.
4443 * @returns {list} - A new instance of List.
4444 */
4445
4446List.of = function (/*items...*/) {
4447 return List.from.call(this, arguments);
4448};
4449
4450/**
4451 * Creates a new list from the given array-like object (each a list item)
4452 * passed in.
4453 * @name List.from
4454 * @param {ListItem[]} [items] - The items to append.
4455 * @returns {list} - A new instance of List.
4456 */
4457List.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 * List#head
4477 * Default to `null`.
4478 */
4479ListPrototype.head = null;
4480
4481/**
4482 * List#tail
4483 * Default to `null`.
4484 */
4485ListPrototype.tail = null;
4486
4487/**
4488 * Returns the list's items as an array. This does *not* detach the items.
4489 * @name List#toArray
4490 * @returns {ListItem[]} - An array of (still attached) ListItems.
4491 */
4492ListPrototype.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 * Prepends the given item to the list: Item will be the new first item
4506 * (`head`).
4507 * @name List#prepend
4508 * @param {ListItem} item - The item to prepend.
4509 * @returns {ListItem} - An instance of ListItem (the given item).
4510 */
4511ListPrototype.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 // Cache self.
4523 self = this;
4524
4525 // If self has a first item, defer prepend to the first items prepend
4526 // method, and return the result.
4527 head = self.head;
4528
4529 if (head) {
4530 return head.prepend(item);
4531 }
4532
4533 // ...otherwise, there is no `head` (or `tail`) item yet.
4534
4535 // Detach the prependee.
4536 item.detach();
4537
4538 // Set the prependees parent list to reference self.
4539 item.list = self;
4540
4541 // Set self's first item to the prependee, and return the item.
4542 self.head = item;
4543
4544 return item;
4545};
4546
4547/**
4548 * Appends the given item to the list: Item will be the new last item (`tail`)
4549 * if the list had a first item, and its first item (`head`) otherwise.
4550 * @name List#append
4551 * @param {ListItem} item - The item to append.
4552 * @returns {ListItem} - An instance of ListItem (the given item).
4553 */
4554
4555ListPrototype.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 // Cache self.
4567 self = this;
4568
4569 // If self has a last item, defer appending to the last items append
4570 // method, and return the result.
4571 tail = self.tail;
4572
4573 if (tail) {
4574 return tail.append(item);
4575 }
4576
4577 // If self has a first item, defer appending to the first items append
4578 // method, and return the result.
4579 head = self.head;
4580
4581 if (head) {
4582 return head.append(item);
4583 }
4584
4585 // ...otherwise, there is no `tail` or `head` item yet.
4586
4587 // Detach the appendee.
4588 item.detach();
4589
4590 // Set the appendees parent list to reference self.
4591 item.list = self;
4592
4593 // Set self's first item to the appendee, and return the item.
4594 self.head = item;
4595
4596 return item;
4597};
4598
4599/**
4600 * Creates a new ListItem: A linked list item is a bit like DOM node:
4601 * It knows only about its "parent" (`list`), the item before it (`prev`),
4602 * and the item after it (`next`).
4603 * @global
4604 * @private
4605 * @constructor
4606 * @class Represents an instance of ListItem.
4607 */
4608
4609function ListItem() {}
4610
4611List.Item = ListItem;
4612
4613var ListItemPrototype = ListItem.prototype;
4614
4615ListItemPrototype.next = null;
4616
4617ListItemPrototype.prev = null;
4618
4619ListItemPrototype.list = null;
4620
4621/**
4622 * Detaches the item operated on from its parent list.
4623 * @name ListItem#detach
4624 * @returns {ListItem} - The item operated on.
4625 */
4626ListItemPrototype.detach = function () {
4627 // Cache self, the parent list, and the previous and next items.
4628 var self = this,
4629 list = self.list,
4630 prev = self.prev,
4631 next = self.next;
4632
4633 // If the item is already detached, return self.
4634 if (!list) {
4635 return self;
4636 }
4637
4638 // If self is the last item in the parent list, link the lists last item
4639 // to the previous item.
4640 if (list.tail === self) {
4641 list.tail = prev;
4642 }
4643
4644 // If self is the first item in the parent list, link the lists first item
4645 // to the next item.
4646 if (list.head === self) {
4647 list.head = next;
4648 }
4649
4650 // If both the last and first items in the parent list are the same,
4651 // remove the link to the last item.
4652 if (list.tail === list.head) {
4653 list.tail = null;
4654 }
4655
4656 // If a previous item exists, link its next item to selfs next item.
4657 if (prev) {
4658 prev.next = next;
4659 }
4660
4661 // If a next item exists, link its previous item to selfs previous item.
4662 if (next) {
4663 next.prev = prev;
4664 }
4665
4666 // Remove links from self to both the next and previous items, and to the
4667 // parent list.
4668 self.prev = self.next = self.list = null;
4669
4670 // Return self.
4671 return self;
4672};
4673
4674/**
4675 * Prepends the given item *before* the item operated on.
4676 * @name ListItem#prepend
4677 * @param {ListItem} item - The item to prepend.
4678 * @returns {ListItem} - The item operated on, or false when that item is not
4679 * attached.
4680 */
4681ListItemPrototype.prepend = function (item) {
4682 if (!item || !item.append || !item.prepend || !item.detach) {
4683 throw new Error(errorMessage + 'Item#prepend`.');
4684 }
4685
4686 // Cache self, the parent list, and the previous item.
4687 var self = this,
4688 list = self.list,
4689 prev = self.prev;
4690
4691 // If self is detached, return false.
4692 if (!list) {
4693 return false;
4694 }
4695
4696 // Detach the prependee.
4697 item.detach();
4698
4699 // If self has a previous item...
4700 if (prev) {
4701 // ...link the prependees previous item, to selfs previous item.
4702 item.prev = prev;
4703
4704 // ...link the previous items next item, to self.
4705 prev.next = item;
4706 }
4707
4708 // Set the prependees next item to self.
4709 item.next = self;
4710
4711 // Set the prependees parent list to selfs parent list.
4712 item.list = list;
4713
4714 // Set the previous item of self to the prependee.
4715 self.prev = item;
4716
4717 // If self is the first item in the parent list, link the lists first item
4718 // to the prependee.
4719 if (self === list.head) {
4720 list.head = item;
4721 }
4722
4723 // If the the parent list has no last item, link the lists last item to
4724 // self.
4725 if (!list.tail) {
4726 list.tail = self;
4727 }
4728
4729 // Return the prependee.
4730 return item;
4731};
4732
4733/**
4734 * Appends the given item *after* the item operated on.
4735 * @name ListItem#append
4736 * @param {ListItem} item - The item to append.
4737 * @returns {ListItem} - The item operated on, or false when that item is not
4738 * attached.
4739 */
4740ListItemPrototype.append = function (item) {
4741 // If item is falsey, return false.
4742 if (!item || !item.append || !item.prepend || !item.detach) {
4743 throw new Error(errorMessage + 'Item#append`.');
4744 }
4745
4746 // Cache self, the parent list, and the next item.
4747 var self = this,
4748 list = self.list,
4749 next = self.next;
4750
4751 // If self is detached, return false.
4752 if (!list) {
4753 return false;
4754 }
4755
4756 // Detach the appendee.
4757 item.detach();
4758
4759 // If self has a next item...
4760 if (next) {
4761 // ...link the appendees next item, to selfs next item.
4762 item.next = next;
4763
4764 // ...link the next items previous item, to the appendee.
4765 next.prev = item;
4766 }
4767
4768 // Set the appendees previous item to self.
4769 item.prev = self;
4770
4771 // Set the appendees parent list to selfs parent list.
4772 item.list = list;
4773
4774 // Set the next item of self to the appendee.
4775 self.next = item;
4776
4777 // If the the parent list has no last item or if self is the parent lists
4778 // last item, link the lists last item to the appendee.
4779 if (self === list.tail || !list.tail) {
4780 list.tail = item;
4781 }
4782
4783 // Return the appendee.
4784 return item;
4785};
4786
4787/**
4788 * Expose `List`.
4789 */
4790
4791module.exports = List;
4792
4793},{}],15:[function(_dereq_,module,exports){
4794'use strict';
4795
4796module.exports = _dereq_('./_source/linked-list.js');
4797
4798},{"./_source/linked-list.js":14}],16:[function(_dereq_,module,exports){
4799// Copyright Joyent, Inc. and other Node contributors.
4800//
4801// Permission is hereby granted, free of charge, to any person obtaining a
4802// copy of this software and associated documentation files (the
4803// "Software"), to deal in the Software without restriction, including
4804// without limitation the rights to use, copy, modify, merge, publish,
4805// distribute, sublicense, and/or sell copies of the Software, and to permit
4806// persons to whom the Software is furnished to do so, subject to the
4807// following conditions:
4808//
4809// The above copyright notice and this permission notice shall be included
4810// in all copies or substantial portions of the Software.
4811//
4812// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
4813// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4814// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
4815// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
4816// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
4817// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
4818// USE OR OTHER DEALINGS IN THE SOFTWARE.
4819
4820'use strict';
4821
4822// If obj.hasOwnProperty has been overridden, then calling
4823// obj.hasOwnProperty(prop) will break.
4824// See: https://github.com/joyent/node/issues/1707
4825function hasOwnProperty(obj, prop) {
4826 return Object.prototype.hasOwnProperty.call(obj, prop);
4827}
4828
4829module.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 // maxKeys <= 0 means that we should not limit keys count
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
4880var isArray = Array.isArray || function (xs) {
4881 return Object.prototype.toString.call(xs) === '[object Array]';
4882};
4883
4884},{}],17:[function(_dereq_,module,exports){
4885// Copyright Joyent, Inc. and other Node contributors.
4886//
4887// Permission is hereby granted, free of charge, to any person obtaining a
4888// copy of this software and associated documentation files (the
4889// "Software"), to deal in the Software without restriction, including
4890// without limitation the rights to use, copy, modify, merge, publish,
4891// distribute, sublicense, and/or sell copies of the Software, and to permit
4892// persons to whom the Software is furnished to do so, subject to the
4893// following conditions:
4894//
4895// The above copyright notice and this permission notice shall be included
4896// in all copies or substantial portions of the Software.
4897//
4898// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
4899// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4900// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
4901// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
4902// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
4903// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
4904// USE OR OTHER DEALINGS IN THE SOFTWARE.
4905
4906'use strict';
4907
4908var 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
4924module.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
4950var isArray = Array.isArray || function (xs) {
4951 return Object.prototype.toString.call(xs) === '[object Array]';
4952};
4953
4954function 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
4963var 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
4974exports.decode = exports.parse = _dereq_('./decode');
4975exports.encode = exports.stringify = _dereq_('./encode');
4976
4977},{"./decode":16,"./encode":17}],19:[function(_dereq_,module,exports){
4978var Emitter = _dereq_('component-emitter');
4979
4980var 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
4997SCChannel.prototype = Object.create(Emitter.prototype);
4998
4999SCChannel.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
5011SCChannel.prototype.getState = function () {
5012 return this.state;
5013};
5014
5015SCChannel.prototype.subscribe = function (options) {
5016 this.client.subscribe(this.name, options);
5017};
5018
5019SCChannel.prototype.unsubscribe = function () {
5020 this.client.unsubscribe(this.name);
5021};
5022
5023SCChannel.prototype.isSubscribed = function (includePending) {
5024 return this.client.isSubscribed(this.name, includePending);
5025};
5026
5027SCChannel.prototype.publish = function (data, callback) {
5028 this.client.publish(this.name, data, callback);
5029};
5030
5031SCChannel.prototype.watch = function (handler) {
5032 this.client.watch(this.name, handler);
5033};
5034
5035SCChannel.prototype.unwatch = function (handler) {
5036 this.client.unwatch(this.name, handler);
5037};
5038
5039SCChannel.prototype.watchers = function () {
5040 return this.client.watchers(this.name);
5041};
5042
5043SCChannel.prototype.destroy = function () {
5044 this.client.destroyChannel(this.name);
5045};
5046
5047module.exports.SCChannel = SCChannel;
5048
5049},{"component-emitter":12}],20:[function(_dereq_,module,exports){
5050// Based on https://github.com/dscape/cycle/blob/master/cycle.js
5051
5052module.exports = function decycle(object) {
5053// Make a deep copy of an object or array, assuring that there is at most
5054// one instance of each object or array in the resulting structure. The
5055// duplicate references (which might be forming cycles) are replaced with
5056// an object of the form
5057// {$ref: PATH}
5058// where the PATH is a JSONPath string that locates the first occurance.
5059// So,
5060// var a = [];
5061// a[0] = a;
5062// return JSON.stringify(JSON.decycle(a));
5063// produces the string '[{"$ref":"$"}]'.
5064
5065// JSONPath is used to locate the unique object. $ indicates the top level of
5066// the object or array. [NUMBER] or [STRING] indicates a child member or
5067// property.
5068
5069 var objects = [], // Keep a reference to each unique object or array
5070 paths = []; // Keep the path to each unique object or array
5071
5072 return (function derez(value, path) {
5073
5074// The derez recurses through the object, producing the deep copy.
5075
5076 var i, // The loop counter
5077 name, // Property name
5078 nu; // The new object or array
5079
5080// typeof null === 'object', so go on if this value is really an object but not
5081// one of the weird builtin objects.
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// If the value is an object or array, look to see if we have already
5091// encountered it. If so, return a $ref/path object. This is a hard way,
5092// linear search that will get slower as the number of unique objects grows.
5093
5094 for (i = 0; i < objects.length; i += 1) {
5095 if (objects[i] === value) {
5096 return {$ref: paths[i]};
5097 }
5098 }
5099
5100// Otherwise, accumulate the unique value and its path.
5101
5102 objects.push(value);
5103 paths.push(path);
5104
5105// If it is an array, replicate the array.
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// If it is an object, replicate the object.
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){
5131var decycle = _dereq_('./decycle');
5132
5133var isStrict = (function () { return !this; })();
5134
5135function 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}
5145AuthTokenExpiredError.prototype = Object.create(Error.prototype);
5146
5147
5148function 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}
5157AuthTokenInvalidError.prototype = Object.create(Error.prototype);
5158
5159
5160function 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}
5170AuthTokenNotBeforeError.prototype = Object.create(Error.prototype);
5171
5172
5173// For any other auth token error.
5174function 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}
5183AuthTokenError.prototype = Object.create(Error.prototype);
5184
5185
5186function 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}
5196SilentMiddlewareBlockedError.prototype = Object.create(Error.prototype);
5197
5198
5199function 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}
5208InvalidActionError.prototype = Object.create(Error.prototype);
5209
5210function 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}
5219InvalidArgumentsError.prototype = Object.create(Error.prototype);
5220
5221function 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}
5230InvalidOptionsError.prototype = Object.create(Error.prototype);
5231
5232
5233function 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}
5242InvalidMessageError.prototype = Object.create(Error.prototype);
5243
5244
5245function 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}
5255SocketProtocolError.prototype = Object.create(Error.prototype);
5256
5257
5258function 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}
5267ServerProtocolError.prototype = Object.create(Error.prototype);
5268
5269function 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}
5278HTTPServerError.prototype = Object.create(Error.prototype);
5279
5280
5281function 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}
5290ResourceLimitError.prototype = Object.create(Error.prototype);
5291
5292
5293function 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}
5302TimeoutError.prototype = Object.create(Error.prototype);
5303
5304
5305function 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}
5315BadConnectionError.prototype = Object.create(Error.prototype);
5316
5317
5318function 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}
5327BrokerError.prototype = Object.create(Error.prototype);
5328
5329
5330function 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}
5340ProcessExitError.prototype = Object.create(Error.prototype);
5341
5342
5343function 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}
5352UnknownError.prototype = Object.create(Error.prototype);
5353
5354
5355// Expose all error types.
5356
5357module.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
5378module.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
5400module.exports.socketProtocolIgnoreStatuses = {
5401 1000: 'Socket closed normally',
5402 1001: 'Socket hung up'
5403};
5404
5405// Properties related to error domains cannot be serialized.
5406var unserializableErrorProperties = {
5407 domain: 1,
5408 domainEmitter: 1,
5409 domainThrown: 1
5410};
5411
5412// Convert an error into a JSON-compatible type which can later be hydrated
5413// back to its *original* form.
5414module.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// Convert a dehydrated error back to its *original* form.
5439module.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
5456module.exports.decycle = decycle;
5457
5458},{"./decycle":20}],22:[function(_dereq_,module,exports){
5459(function (global){
5460var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5461var validJSONStartRegex = /^[ \n\r\t]*[{\[]/;
5462
5463var 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
5484var 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 // Some versions of Node.js convert Buffers to Objects before they are passed to
5498 // the replacer function - Because of this, we need to rehydrate Buffers
5499 // before we can convert them to base64 strings.
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// Decode the data which was transmitted over the wire to a JavaScript Object in a format which SC understands.
5517// See encode function below for more details.
5518module.exports.decode = function (input) {
5519 if (input == null) {
5520 return null;
5521 }
5522 // Leave ping or pong message as is
5523 if (input === '#1' || input === '#2') {
5524 return input;
5525 }
5526 var message = input.toString();
5527
5528 // Performance optimization to detect invalid JSON packet sooner.
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// Encode a raw JavaScript object (which is in the SC protocol format) into a format for
5540// transfering it over the wire. In this case, we just convert it into a simple JSON string.
5541// If you want to create your own custom codec, you can encode the object into any format
5542// (e.g. binary ArrayBuffer or string with any kind of compression) so long as your decode
5543// function is able to rehydrate that object back into its original JavaScript Object format
5544// (which adheres to the SC protocol).
5545// See https://github.com/SocketCluster/socketcluster/blob/master/socketcluster-protocol.md
5546// for details about the SC protocol.
5547module.exports.encode = function (object) {
5548 // Leave ping or pong message as is
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){
5557var v1 = _dereq_('./v1');
5558var v4 = _dereq_('./v4');
5559
5560var uuid = v4;
5561uuid.v1 = v1;
5562uuid.v4 = v4;
5563
5564module.exports = uuid;
5565
5566},{"./v1":26,"./v4":27}],24:[function(_dereq_,module,exports){
5567/**
5568 * Convert array of 16 byte values to UUID string format of the form:
5569 * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
5570 */
5571var byteToHex = [];
5572for (var i = 0; i < 256; ++i) {
5573 byteToHex[i] = (i + 0x100).toString(16).substr(1);
5574}
5575
5576function 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
5589module.exports = bytesToUuid;
5590
5591},{}],25:[function(_dereq_,module,exports){
5592// Unique ID creation requires a high quality random # generator. In the
5593// browser this is a little complicated due to unknown quality of Math.random()
5594// and inconsistent support for the `crypto` API. We do the best we can via
5595// feature-detection
5596
5597// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
5598var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues.bind(crypto)) ||
5599 (typeof(msCrypto) != 'undefined' && msCrypto.getRandomValues.bind(msCrypto));
5600if (getRandomValues) {
5601 // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
5602 var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
5603
5604 module.exports = function whatwgRNG() {
5605 getRandomValues(rnds8);
5606 return rnds8;
5607 };
5608} else {
5609 // Math.random()-based (RNG)
5610 //
5611 // If all else fails, use Math.random(). It's fast, but is of unspecified
5612 // quality.
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){
5626var rng = _dereq_('./lib/rng');
5627var bytesToUuid = _dereq_('./lib/bytesToUuid');
5628
5629// **`v1()` - Generate time-based UUID**
5630//
5631// Inspired by https://github.com/LiosK/UUID.js
5632// and http://docs.python.org/library/uuid.html
5633
5634var _nodeId;
5635var _clockseq;
5636
5637// Previous uuid creation time
5638var _lastMSecs = 0;
5639var _lastNSecs = 0;
5640
5641// See https://github.com/broofa/node-uuid for API details
5642function 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 // node and clockseq need to be initialized to random values if they're not
5651 // specified. We do this lazily to minimize issues related to insufficient
5652 // system entropy. See #189
5653 if (node == null || clockseq == null) {
5654 var seedBytes = rng();
5655 if (node == null) {
5656 // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
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 // Per 4.2.2, randomize (14 bit) clockseq
5664 clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
5665 }
5666 }
5667
5668 // UUID timestamps are 100 nano-second units since the Gregorian epoch,
5669 // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
5670 // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
5671 // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
5672 var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
5673
5674 // Per 4.2.1.2, use count of uuid's generated during the current clock
5675 // cycle to simulate higher resolution clock
5676 var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
5677
5678 // Time since last uuid creation (in msecs)
5679 var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
5680
5681 // Per 4.2.1.2, Bump clockseq on clock regression
5682 if (dt < 0 && options.clockseq === undefined) {
5683 clockseq = clockseq + 1 & 0x3fff;
5684 }
5685
5686 // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
5687 // time interval
5688 if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
5689 nsecs = 0;
5690 }
5691
5692 // Per 4.2.1.2 Throw error if too many uuids are requested
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 // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
5702 msecs += 12219292800000;
5703
5704 // `time_low`
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 // `time_mid`
5712 var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
5713 b[i++] = tmh >>> 8 & 0xff;
5714 b[i++] = tmh & 0xff;
5715
5716 // `time_high_and_version`
5717 b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
5718 b[i++] = tmh >>> 16 & 0xff;
5719
5720 // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
5721 b[i++] = clockseq >>> 8 | 0x80;
5722
5723 // `clock_seq_low`
5724 b[i++] = clockseq & 0xff;
5725
5726 // `node`
5727 for (var n = 0; n < 6; ++n) {
5728 b[i + n] = node[n];
5729 }
5730
5731 return buf ? buf : bytesToUuid(b);
5732}
5733
5734module.exports = v1;
5735
5736},{"./lib/bytesToUuid":24,"./lib/rng":25}],27:[function(_dereq_,module,exports){
5737var rng = _dereq_('./lib/rng');
5738var bytesToUuid = _dereq_('./lib/bytesToUuid');
5739
5740function 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 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
5752 rnds[6] = (rnds[6] & 0x0f) | 0x40;
5753 rnds[8] = (rnds[8] & 0x3f) | 0x80;
5754
5755 // Copy bytes to buffer, if provided
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
5765module.exports = v4;
5766
5767},{"./lib/bytesToUuid":24,"./lib/rng":25}]},{},[1])(1)
5768});