UNPKG

995 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define('leancloud-realtime', ['exports'], factory) :
4 (global = global || self, factory(global.AV = global.AV || {}));
5}(this, function (exports) { 'use strict';
6
7 var define = undefined;
8 var require = require || function(id) {throw new Error('Unexpected required ' + id)};
9
10
11
12 var process = (typeof window !== 'undefined' && window.process) || {};
13 process.env = process.env || {};
14
15 var XMLHttpRequest;
16
17 function _typeof(obj) {
18 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
19 _typeof = function (obj) {
20 return typeof obj;
21 };
22 } else {
23 _typeof = function (obj) {
24 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
25 };
26 }
27
28 return _typeof(obj);
29 }
30
31 function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
32 try {
33 var info = gen[key](arg);
34 var value = info.value;
35 } catch (error) {
36 reject(error);
37 return;
38 }
39
40 if (info.done) {
41 resolve(value);
42 } else {
43 Promise.resolve(value).then(_next, _throw);
44 }
45 }
46
47 function _asyncToGenerator(fn) {
48 return function () {
49 var self = this,
50 args = arguments;
51 return new Promise(function (resolve, reject) {
52 var gen = fn.apply(self, args);
53
54 function _next(value) {
55 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
56 }
57
58 function _throw(err) {
59 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
60 }
61
62 _next(undefined);
63 });
64 };
65 }
66
67 function _defineProperties(target, props) {
68 for (var i = 0; i < props.length; i++) {
69 var descriptor = props[i];
70 descriptor.enumerable = descriptor.enumerable || false;
71 descriptor.configurable = true;
72 if ("value" in descriptor) descriptor.writable = true;
73 Object.defineProperty(target, descriptor.key, descriptor);
74 }
75 }
76
77 function _createClass(Constructor, protoProps, staticProps) {
78 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
79 if (staticProps) _defineProperties(Constructor, staticProps);
80 return Constructor;
81 }
82
83 function _defineProperty(obj, key, value) {
84 if (key in obj) {
85 Object.defineProperty(obj, key, {
86 value: value,
87 enumerable: true,
88 configurable: true,
89 writable: true
90 });
91 } else {
92 obj[key] = value;
93 }
94
95 return obj;
96 }
97
98 function _objectSpread(target) {
99 for (var i = 1; i < arguments.length; i++) {
100 var source = arguments[i] != null ? arguments[i] : {};
101 var ownKeys = Object.keys(source);
102
103 if (typeof Object.getOwnPropertySymbols === 'function') {
104 ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
105 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
106 }));
107 }
108
109 ownKeys.forEach(function (key) {
110 _defineProperty(target, key, source[key]);
111 });
112 }
113
114 return target;
115 }
116
117 function _inheritsLoose(subClass, superClass) {
118 subClass.prototype = Object.create(superClass.prototype);
119 subClass.prototype.constructor = subClass;
120 subClass.__proto__ = superClass;
121 }
122
123 function _objectWithoutPropertiesLoose(source, excluded) {
124 if (source == null) return {};
125 var target = {};
126 var sourceKeys = Object.keys(source);
127 var key, i;
128
129 for (i = 0; i < sourceKeys.length; i++) {
130 key = sourceKeys[i];
131 if (excluded.indexOf(key) >= 0) continue;
132 target[key] = source[key];
133 }
134
135 return target;
136 }
137
138 function _objectWithoutProperties(source, excluded) {
139 if (source == null) return {};
140
141 var target = _objectWithoutPropertiesLoose(source, excluded);
142
143 var key, i;
144
145 if (Object.getOwnPropertySymbols) {
146 var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
147
148 for (i = 0; i < sourceSymbolKeys.length; i++) {
149 key = sourceSymbolKeys[i];
150 if (excluded.indexOf(key) >= 0) continue;
151 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
152 target[key] = source[key];
153 }
154 }
155
156 return target;
157 }
158
159 function _assertThisInitialized(self) {
160 if (self === void 0) {
161 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
162 }
163
164 return self;
165 }
166
167 function _slicedToArray(arr, i) {
168 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
169 }
170
171 function _toArray(arr) {
172 return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
173 }
174
175 function _toConsumableArray(arr) {
176 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
177 }
178
179 function _arrayWithoutHoles(arr) {
180 if (Array.isArray(arr)) {
181 for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
182
183 return arr2;
184 }
185 }
186
187 function _arrayWithHoles(arr) {
188 if (Array.isArray(arr)) return arr;
189 }
190
191 function _iterableToArray(iter) {
192 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
193 }
194
195 function _iterableToArrayLimit(arr, i) {
196 var _arr = [];
197 var _n = true;
198 var _d = false;
199 var _e = undefined;
200
201 try {
202 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
203 _arr.push(_s.value);
204
205 if (i && _arr.length === i) break;
206 }
207 } catch (err) {
208 _d = true;
209 _e = err;
210 } finally {
211 try {
212 if (!_n && _i["return"] != null) _i["return"]();
213 } finally {
214 if (_d) throw _e;
215 }
216 }
217
218 return _arr;
219 }
220
221 function _nonIterableSpread() {
222 throw new TypeError("Invalid attempt to spread non-iterable instance");
223 }
224
225 function _nonIterableRest() {
226 throw new TypeError("Invalid attempt to destructure non-iterable instance");
227 }
228
229 function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
230 var desc = {};
231 Object['ke' + 'ys'](descriptor).forEach(function (key) {
232 desc[key] = descriptor[key];
233 });
234 desc.enumerable = !!desc.enumerable;
235 desc.configurable = !!desc.configurable;
236
237 if ('value' in desc || desc.initializer) {
238 desc.writable = true;
239 }
240
241 desc = decorators.slice().reverse().reduce(function (desc, decorator) {
242 return decorator(target, property, desc) || desc;
243 }, desc);
244
245 if (context && desc.initializer !== void 0) {
246 desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
247 desc.initializer = undefined;
248 }
249
250 if (desc.initializer === void 0) {
251 Object['define' + 'Property'](target, property, desc);
252 desc = null;
253 }
254
255 return desc;
256 }
257
258 var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
259
260 function commonjsRequire () {
261 throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
262 }
263
264 function createCommonjsModule(fn, module) {
265 return module = { exports: {} }, fn(module, module.exports), module.exports;
266 }
267
268 function getCjsExportFromNamespace (n) {
269 return n && n.default || n;
270 }
271
272 var Storage =
273 /*#__PURE__*/
274 function () {
275 function Storage() {}
276
277 var _proto = Storage.prototype;
278
279 _proto.getItem = function getItem(key) {
280 return wx.getStorageSync(key);
281 };
282
283 _proto.setItem = function setItem(key, value) {
284 return wx.setStorageSync(key, value);
285 };
286
287 _proto.removeItem = function removeItem(key) {
288 return this.setItem(key, '');
289 };
290
291 _proto.clear = function clear() {
292 return wx.clearStorageSync();
293 };
294
295 return Storage;
296 }();
297
298 var localstorage = new Storage();
299
300 /*
301 object-assign
302 (c) Sindre Sorhus
303 @license MIT
304 */
305 /* eslint-disable no-unused-vars */
306 var getOwnPropertySymbols = Object.getOwnPropertySymbols;
307 var hasOwnProperty = Object.prototype.hasOwnProperty;
308 var propIsEnumerable = Object.prototype.propertyIsEnumerable;
309
310 function toObject(val) {
311 if (val === null || val === undefined) {
312 throw new TypeError('Object.assign cannot be called with null or undefined');
313 }
314
315 return Object(val);
316 }
317
318 function shouldUseNative() {
319 try {
320 if (!Object.assign) {
321 return false;
322 }
323
324 // Detect buggy property enumeration order in older V8 versions.
325
326 // https://bugs.chromium.org/p/v8/issues/detail?id=4118
327 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
328 test1[5] = 'de';
329 if (Object.getOwnPropertyNames(test1)[0] === '5') {
330 return false;
331 }
332
333 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
334 var test2 = {};
335 for (var i = 0; i < 10; i++) {
336 test2['_' + String.fromCharCode(i)] = i;
337 }
338 var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
339 return test2[n];
340 });
341 if (order2.join('') !== '0123456789') {
342 return false;
343 }
344
345 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
346 var test3 = {};
347 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
348 test3[letter] = letter;
349 });
350 if (Object.keys(Object.assign({}, test3)).join('') !==
351 'abcdefghijklmnopqrst') {
352 return false;
353 }
354
355 return true;
356 } catch (err) {
357 // We don't expect any of the above to throw, but better to be safe.
358 return false;
359 }
360 }
361
362 var objectAssign = shouldUseNative() ? Object.assign : function (target, source) {
363 var from;
364 var to = toObject(target);
365 var symbols;
366
367 for (var s = 1; s < arguments.length; s++) {
368 from = Object(arguments[s]);
369
370 for (var key in from) {
371 if (hasOwnProperty.call(from, key)) {
372 to[key] = from[key];
373 }
374 }
375
376 if (getOwnPropertySymbols) {
377 symbols = getOwnPropertySymbols(from);
378 for (var i = 0; i < symbols.length; i++) {
379 if (propIsEnumerable.call(from, symbols[i])) {
380 to[symbols[i]] = from[symbols[i]];
381 }
382 }
383 }
384 }
385
386 return to;
387 };
388
389 var commons = createCommonjsModule(function (module, exports) {
390
391 /**
392 * Creates a unique key.
393 *
394 * @param {string} name - A name to create.
395 * @returns {symbol|string}
396 * @private
397 */
398 var createUniqueKey = exports.createUniqueKey = (typeof Symbol !== "undefined" ?
399 Symbol :
400 function createUniqueKey(name) {
401 return "[[" + name + "_" + Math.random().toFixed(8).slice(2) + "]]";
402 });
403
404 /**
405 * The key of listeners.
406 *
407 * @type {symbol|string}
408 * @private
409 */
410 exports.LISTENERS = createUniqueKey("listeners");
411
412 /**
413 * A value of kind for listeners which are registered in the capturing phase.
414 *
415 * @type {number}
416 * @private
417 */
418 exports.CAPTURE = 1;
419
420 /**
421 * A value of kind for listeners which are registered in the bubbling phase.
422 *
423 * @type {number}
424 * @private
425 */
426 exports.BUBBLE = 2;
427
428 /**
429 * A value of kind for listeners which are registered as an attribute.
430 *
431 * @type {number}
432 * @private
433 */
434 exports.ATTRIBUTE = 3;
435
436 /**
437 * @typedef object ListenerNode
438 * @property {function} listener - A listener function.
439 * @property {number} kind - The kind of the listener.
440 * @property {ListenerNode|null} next - The next node.
441 * If this node is the last, this is `null`.
442 */
443
444 /**
445 * Creates a node of singly linked list for a list of listeners.
446 *
447 * @param {function} listener - A listener function.
448 * @param {number} kind - The kind of the listener.
449 * @returns {ListenerNode} The created listener node.
450 */
451 exports.newNode = function newNode(listener, kind) {
452 return {listener: listener, kind: kind, next: null};
453 };
454 });
455 var commons_1 = commons.createUniqueKey;
456 var commons_2 = commons.LISTENERS;
457 var commons_3 = commons.CAPTURE;
458 var commons_4 = commons.BUBBLE;
459 var commons_5 = commons.ATTRIBUTE;
460 var commons_6 = commons.newNode;
461
462 //-----------------------------------------------------------------------------
463 // Requirements
464 //-----------------------------------------------------------------------------
465
466
467 var LISTENERS = commons.LISTENERS;
468 var ATTRIBUTE = commons.ATTRIBUTE;
469 var newNode = commons.newNode;
470
471 //-----------------------------------------------------------------------------
472 // Helpers
473 //-----------------------------------------------------------------------------
474
475 /**
476 * Gets a specified attribute listener from a given EventTarget object.
477 *
478 * @param {EventTarget} eventTarget - An EventTarget object to get.
479 * @param {string} type - An event type to get.
480 * @returns {function|null} The found attribute listener.
481 */
482 function getAttributeListener(eventTarget, type) {
483 var node = eventTarget[LISTENERS][type];
484 while (node != null) {
485 if (node.kind === ATTRIBUTE) {
486 return node.listener;
487 }
488 node = node.next;
489 }
490 return null;
491 }
492
493 /**
494 * Sets a specified attribute listener to a given EventTarget object.
495 *
496 * @param {EventTarget} eventTarget - An EventTarget object to set.
497 * @param {string} type - An event type to set.
498 * @param {function|null} listener - A listener to be set.
499 * @returns {void}
500 */
501 function setAttributeListener(eventTarget, type, listener) {
502 if (typeof listener !== "function" && typeof listener !== "object") {
503 listener = null; // eslint-disable-line no-param-reassign
504 }
505
506 var prev = null;
507 var node = eventTarget[LISTENERS][type];
508 while (node != null) {
509 if (node.kind === ATTRIBUTE) {
510 // Remove old value.
511 if (prev == null) {
512 eventTarget[LISTENERS][type] = node.next;
513 }
514 else {
515 prev.next = node.next;
516 }
517 }
518 else {
519 prev = node;
520 }
521
522 node = node.next;
523 }
524
525 // Add new value.
526 if (listener != null) {
527 if (prev == null) {
528 eventTarget[LISTENERS][type] = newNode(listener, ATTRIBUTE);
529 }
530 else {
531 prev.next = newNode(listener, ATTRIBUTE);
532 }
533 }
534 }
535
536 //-----------------------------------------------------------------------------
537 // Public Interface
538 //-----------------------------------------------------------------------------
539
540 /**
541 * Defines an `EventTarget` implementation which has `onfoobar` attributes.
542 *
543 * @param {EventTarget} EventTargetBase - A base implementation of EventTarget.
544 * @param {string[]} types - A list of event types which are defined as attribute listeners.
545 * @returns {EventTarget} The defined `EventTarget` implementation which has attribute listeners.
546 */
547 var defineCustomEventTarget = function(EventTargetBase, types) {
548 function EventTarget() {
549 EventTargetBase.call(this);
550 }
551
552 var descripter = {
553 constructor: {
554 value: EventTarget,
555 configurable: true,
556 writable: true
557 }
558 };
559
560 types.forEach(function(type) {
561 descripter["on" + type] = {
562 get: function() { return getAttributeListener(this, type); },
563 set: function(listener) { setAttributeListener(this, type, listener); },
564 configurable: true,
565 enumerable: true
566 };
567 });
568
569 EventTarget.prototype = Object.create(EventTargetBase.prototype, descripter);
570
571 return EventTarget;
572 };
573
574 var customEventTarget = {
575 defineCustomEventTarget: defineCustomEventTarget
576 };
577
578 //-----------------------------------------------------------------------------
579 // Requirements
580 //-----------------------------------------------------------------------------
581
582 var createUniqueKey = commons.createUniqueKey;
583
584 //-----------------------------------------------------------------------------
585 // Constsnts
586 //-----------------------------------------------------------------------------
587
588 /**
589 * The key of the flag which is turned on by `stopImmediatePropagation` method.
590 *
591 * @type {symbol|string}
592 * @private
593 */
594 var STOP_IMMEDIATE_PROPAGATION_FLAG =
595 createUniqueKey("stop_immediate_propagation_flag");
596
597 /**
598 * The key of the flag which is turned on by `preventDefault` method.
599 *
600 * @type {symbol|string}
601 * @private
602 */
603 var CANCELED_FLAG = createUniqueKey("canceled_flag");
604
605 /**
606 * The key of the original event object.
607 *
608 * @type {symbol|string}
609 * @private
610 */
611 var ORIGINAL_EVENT = createUniqueKey("original_event");
612
613 /**
614 * Method definitions for the event wrapper.
615 *
616 * @type {object}
617 * @private
618 */
619 var wrapperPrototypeDefinition = Object.freeze({
620 stopPropagation: Object.freeze({
621 value: function stopPropagation() {
622 var e = this[ORIGINAL_EVENT];
623 if (typeof e.stopPropagation === "function") {
624 e.stopPropagation();
625 }
626 },
627 writable: true,
628 configurable: true
629 }),
630
631 stopImmediatePropagation: Object.freeze({
632 value: function stopImmediatePropagation() {
633 this[STOP_IMMEDIATE_PROPAGATION_FLAG] = true;
634
635 var e = this[ORIGINAL_EVENT];
636 if (typeof e.stopImmediatePropagation === "function") {
637 e.stopImmediatePropagation();
638 }
639 },
640 writable: true,
641 configurable: true
642 }),
643
644 preventDefault: Object.freeze({
645 value: function preventDefault() {
646 if (this.cancelable === true) {
647 this[CANCELED_FLAG] = true;
648 }
649
650 var e = this[ORIGINAL_EVENT];
651 if (typeof e.preventDefault === "function") {
652 e.preventDefault();
653 }
654 },
655 writable: true,
656 configurable: true
657 }),
658
659 defaultPrevented: Object.freeze({
660 get: function defaultPrevented() { return this[CANCELED_FLAG]; },
661 enumerable: true,
662 configurable: true
663 })
664 });
665
666 //-----------------------------------------------------------------------------
667 // Public Interface
668 //-----------------------------------------------------------------------------
669
670 var STOP_IMMEDIATE_PROPAGATION_FLAG_1 = STOP_IMMEDIATE_PROPAGATION_FLAG;
671
672 /**
673 * Creates an event wrapper.
674 *
675 * We cannot modify several properties of `Event` object, so we need to create the wrapper.
676 * Plus, this wrapper supports non `Event` objects.
677 *
678 * @param {Event|{type: string}} event - An original event to create the wrapper.
679 * @param {EventTarget} eventTarget - The event target of the event.
680 * @returns {Event} The created wrapper. This object is implemented `Event` interface.
681 * @private
682 */
683 var createEventWrapper = function createEventWrapper(event, eventTarget) {
684 var timeStamp = (
685 typeof event.timeStamp === "number" ? event.timeStamp : Date.now()
686 );
687 var propertyDefinition = {
688 type: {value: event.type, enumerable: true},
689 target: {value: eventTarget, enumerable: true},
690 currentTarget: {value: eventTarget, enumerable: true},
691 eventPhase: {value: 2, enumerable: true},
692 bubbles: {value: Boolean(event.bubbles), enumerable: true},
693 cancelable: {value: Boolean(event.cancelable), enumerable: true},
694 timeStamp: {value: timeStamp, enumerable: true},
695 isTrusted: {value: false, enumerable: true}
696 };
697 propertyDefinition[STOP_IMMEDIATE_PROPAGATION_FLAG] = {value: false, writable: true};
698 propertyDefinition[CANCELED_FLAG] = {value: false, writable: true};
699 propertyDefinition[ORIGINAL_EVENT] = {value: event};
700
701 // For CustomEvent.
702 if (typeof event.detail !== "undefined") {
703 propertyDefinition.detail = {value: event.detail, enumerable: true};
704 }
705
706 return Object.create(
707 Object.create(event, wrapperPrototypeDefinition),
708 propertyDefinition
709 );
710 };
711
712 var eventWrapper = {
713 STOP_IMMEDIATE_PROPAGATION_FLAG: STOP_IMMEDIATE_PROPAGATION_FLAG_1,
714 createEventWrapper: createEventWrapper
715 };
716
717 var eventTarget = createCommonjsModule(function (module) {
718
719 //-----------------------------------------------------------------------------
720 // Requirements
721 //-----------------------------------------------------------------------------
722
723
724
725
726 var LISTENERS = commons.LISTENERS;
727 var CAPTURE = commons.CAPTURE;
728 var BUBBLE = commons.BUBBLE;
729 var ATTRIBUTE = commons.ATTRIBUTE;
730 var newNode = commons.newNode;
731 var defineCustomEventTarget = customEventTarget.defineCustomEventTarget;
732 var createEventWrapper = eventWrapper.createEventWrapper;
733 var STOP_IMMEDIATE_PROPAGATION_FLAG =
734 eventWrapper.STOP_IMMEDIATE_PROPAGATION_FLAG;
735
736 //-----------------------------------------------------------------------------
737 // Constants
738 //-----------------------------------------------------------------------------
739
740 /**
741 * A flag which shows there is the native `EventTarget` interface object.
742 *
743 * @type {boolean}
744 * @private
745 */
746 var HAS_EVENTTARGET_INTERFACE = (
747 typeof window !== "undefined" &&
748 typeof window.EventTarget !== "undefined"
749 );
750
751 //-----------------------------------------------------------------------------
752 // Public Interface
753 //-----------------------------------------------------------------------------
754
755 /**
756 * An implementation for `EventTarget` interface.
757 *
758 * @constructor
759 * @public
760 */
761 var EventTarget = module.exports = function EventTarget() {
762 if (this instanceof EventTarget) {
763 // this[LISTENERS] is a Map.
764 // Its key is event type.
765 // Its value is ListenerNode object or null.
766 //
767 // interface ListenerNode {
768 // var listener: Function
769 // var kind: CAPTURE|BUBBLE|ATTRIBUTE
770 // var next: ListenerNode|null
771 // }
772 Object.defineProperty(this, LISTENERS, {value: Object.create(null)});
773 }
774 else if (arguments.length === 1 && Array.isArray(arguments[0])) {
775 return defineCustomEventTarget(EventTarget, arguments[0]);
776 }
777 else if (arguments.length > 0) {
778 var types = Array(arguments.length);
779 for (var i = 0; i < arguments.length; ++i) {
780 types[i] = arguments[i];
781 }
782
783 // To use to extend with attribute listener properties.
784 // e.g.
785 // class MyCustomObject extends EventTarget("message", "error") {
786 // //...
787 // }
788 return defineCustomEventTarget(EventTarget, types);
789 }
790 else {
791 throw new TypeError("Cannot call a class as a function");
792 }
793 };
794
795 EventTarget.prototype = Object.create(
796 (HAS_EVENTTARGET_INTERFACE ? window.EventTarget : Object).prototype,
797 {
798 constructor: {
799 value: EventTarget,
800 writable: true,
801 configurable: true
802 },
803
804 addEventListener: {
805 value: function addEventListener(type, listener, capture) {
806 if (listener == null) {
807 return false;
808 }
809 if (typeof listener !== "function" && typeof listener !== "object") {
810 throw new TypeError("\"listener\" is not an object.");
811 }
812
813 var kind = (capture ? CAPTURE : BUBBLE);
814 var node = this[LISTENERS][type];
815 if (node == null) {
816 this[LISTENERS][type] = newNode(listener, kind);
817 return true;
818 }
819
820 var prev = null;
821 while (node != null) {
822 if (node.listener === listener && node.kind === kind) {
823 // Should ignore a duplicated listener.
824 return false;
825 }
826 prev = node;
827 node = node.next;
828 }
829
830 prev.next = newNode(listener, kind);
831 return true;
832 },
833 configurable: true,
834 writable: true
835 },
836
837 removeEventListener: {
838 value: function removeEventListener(type, listener, capture) {
839 if (listener == null) {
840 return false;
841 }
842
843 var kind = (capture ? CAPTURE : BUBBLE);
844 var prev = null;
845 var node = this[LISTENERS][type];
846 while (node != null) {
847 if (node.listener === listener && node.kind === kind) {
848 if (prev == null) {
849 this[LISTENERS][type] = node.next;
850 }
851 else {
852 prev.next = node.next;
853 }
854 return true;
855 }
856
857 prev = node;
858 node = node.next;
859 }
860
861 return false;
862 },
863 configurable: true,
864 writable: true
865 },
866
867 dispatchEvent: {
868 value: function dispatchEvent(event) {
869 // If listeners aren't registered, terminate.
870 var node = this[LISTENERS][event.type];
871 if (node == null) {
872 return true;
873 }
874
875 // Since we cannot rewrite several properties, so wrap object.
876 var wrapped = createEventWrapper(event, this);
877
878 // This doesn't process capturing phase and bubbling phase.
879 // This isn't participating in a tree.
880 while (node != null) {
881 if (typeof node.listener === "function") {
882 node.listener.call(this, wrapped);
883 }
884 else if (node.kind !== ATTRIBUTE && typeof node.listener.handleEvent === "function") {
885 node.listener.handleEvent(wrapped);
886 }
887
888 if (wrapped[STOP_IMMEDIATE_PROPAGATION_FLAG]) {
889 break;
890 }
891 node = node.next;
892 }
893
894 return !wrapped.defaultPrevented;
895 },
896 configurable: true,
897 writable: true
898 }
899 }
900 );
901 });
902
903 var FormData$1 =
904 /*#__PURE__*/
905 function () {
906 function FormData() {
907 this._entries = [];
908 }
909
910 var _proto = FormData.prototype;
911
912 _proto.append = function append(name, value) {
913 if (typeof name !== 'string') {
914 throw new TypeError('FormData name must be a string');
915 }
916
917 if (typeof value !== 'string') {
918 if (_typeof(value) !== 'object' || typeof value.uri !== 'string') {
919 throw new TypeError('FormData value must be a string or { uri: tempFilePath }');
920 }
921 }
922
923 this._entries.push([name, value]);
924 };
925
926 _proto.set = function set(name, value) {
927 var entry = this.get(name);
928
929 if (entry) {
930 entry[1] = value;
931 } else {
932 this.append(name, value);
933 }
934 };
935
936 _proto.delete = function _delete(name) {
937 this._entries = this._entries.filter(function (entry) {
938 return entry[0] !== name;
939 });
940 };
941
942 _proto.entries = function entries() {
943 return this._entries;
944 };
945
946 _proto.get = function get(name) {
947 return this._entries.find(function (entry) {
948 return entry[0] === name;
949 });
950 };
951
952 _proto.getAll = function getAll(name) {
953 return this._entries.filter(function (entry) {
954 return entry[0] === name;
955 });
956 };
957
958 _proto.has = function has(name) {
959 return this._entries.some(function (entry) {
960 return entry[0] === name;
961 });
962 };
963
964 _proto.keys = function keys() {
965 return this._entries.map(function (entry) {
966 return entry[0];
967 });
968 };
969
970 _proto.values = function values() {
971 return this._entries.map(function (entry) {
972 return entry[1];
973 });
974 };
975
976 return FormData;
977 }();
978
979 var formdata = FormData$1;
980
981 var UNSENT = 0;
982 var OPENED = 1;
983 var HEADERS_RECEIVED = 2;
984 var LOADING = 3;
985 var DONE = 4;
986 var REQUEST_EVENTS = ['abort', 'error', 'load', 'loadstart', 'progress', 'timeout', 'loadend', 'readystatechange'];
987 var REQUEST_UPLOAD_EVENTS = ['abort', 'error', 'load', 'loadstart', 'progress', 'timeout', 'loadend'];
988
989 function successCallback(response) {
990 this.status = response.statusCode;
991 this.statusText = response.statusCode; // 基础库 1.2.0 开始支持
992
993 if (response.header) {
994 this._responseHeaders = Object.keys(response.header).reduce(function (headers, key) {
995 headers[key.toLowerCase()] = response.header[key];
996 return headers;
997 }, {});
998 }
999
1000 var text = response.data;
1001
1002 if (typeof text !== 'string') {
1003 text = JSON.stringify(text);
1004 }
1005
1006 this.responseText = this.response = text;
1007 this.readyState = DONE;
1008 this.dispatchEvent({
1009 type: 'readystatechange'
1010 });
1011 }
1012
1013 var XMLHttpRequestUpload =
1014 /*#__PURE__*/
1015 function (_EventTarget) {
1016 _inheritsLoose(XMLHttpRequestUpload, _EventTarget);
1017
1018 function XMLHttpRequestUpload() {
1019 return _EventTarget.apply(this, arguments) || this;
1020 }
1021
1022 return XMLHttpRequestUpload;
1023 }(eventTarget(REQUEST_UPLOAD_EVENTS));
1024
1025 var XMLHttpRequest$1 =
1026 /*#__PURE__*/
1027 function (_EventTarget2) {
1028 _inheritsLoose(XMLHttpRequest, _EventTarget2);
1029
1030 function XMLHttpRequest() {
1031 var _this;
1032
1033 _this = _EventTarget2.call(this) || this;
1034 _this.readyState = UNSENT;
1035 _this._headers = {};
1036 _this.upload = new XMLHttpRequestUpload();
1037 return _this;
1038 }
1039
1040 var _proto = XMLHttpRequest.prototype;
1041
1042 _proto.abort = function abort() {
1043 // 基础库 1.4.0 开始支持
1044 if (!this._request || this._request.abort) {
1045 this.status = 0;
1046 this.readyState = DONE;
1047 return this._request.abort();
1048 }
1049
1050 throw new Error('该版本基础库不支持 abort request');
1051 };
1052
1053 _proto.getAllResponseHeaders = function getAllResponseHeaders() {
1054 var _this2 = this;
1055
1056 return this._responseHeaders ? Object.keys(this._responseHeaders).map(function (key) {
1057 return "".concat(key, ": ").concat(_this2._responseHeaders[key]);
1058 }).join('\r\n') : '';
1059 };
1060
1061 _proto.getResponseHeader = function getResponseHeader(key) {
1062 var lowserCasedKey = key.toLowerCase();
1063
1064 if (this._responseHeaders && this._responseHeaders[lowserCasedKey]) {
1065 return this._responseHeaders[lowserCasedKey];
1066 }
1067
1068 return null;
1069 };
1070
1071 _proto.overrideMimeType = function overrideMimeType() {
1072 throw new Error('not supported in weapp');
1073 };
1074
1075 _proto.open = function open(method, url) {
1076 var async = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
1077
1078 if (this.readyState !== UNSENT) {
1079 throw new Error('request is already opened');
1080 }
1081
1082 if (!async) {
1083 throw new Error('sync request is not supported');
1084 }
1085
1086 this._method = method;
1087 this._url = url;
1088 this.readyState = OPENED;
1089 this.dispatchEvent({
1090 type: 'readystatechange'
1091 });
1092 };
1093
1094 _proto.setRequestHeader = function setRequestHeader(header, value) {
1095 if (this.readyState !== OPENED) {
1096 throw new Error('request is not opened');
1097 }
1098
1099 this._headers[header.toLowerCase()] = value;
1100 };
1101
1102 _proto.send = function send(data) {
1103 var _this3 = this;
1104
1105 if (this.readyState !== OPENED) {
1106 throw new Error('request is not opened');
1107 }
1108
1109 if (data instanceof formdata) {
1110 var entries = data.entries();
1111 var blobs = entries.filter(function (entry) {
1112 return typeof entry[1] !== 'string';
1113 });
1114
1115 if (blobs.length === 0) {
1116 throw new Error('Must specify a Blob field in FormData');
1117 }
1118
1119 if (blobs.length > 1) {
1120 console.warn('Only the first Blob will be send in Weapp');
1121 }
1122
1123 var restData = entries.filter(function (entry) {
1124 return typeof entry[1] === 'string';
1125 }).reduce(function (result, entry) {
1126 return objectAssign(result, _defineProperty({}, entry[0], entry[1]));
1127 }, {});
1128 this._request = wx.uploadFile({
1129 url: this._url,
1130 name: blobs[0][0],
1131 filePath: blobs[0][1].uri,
1132 formData: restData,
1133 header: this._headers,
1134 success: successCallback.bind(this),
1135 fail: function fail(error) {
1136 _this3.status = 0;
1137 _this3.readyState = DONE;
1138
1139 _this3.dispatchEvent({
1140 type: 'readystatechange'
1141 });
1142
1143 _this3.dispatchEvent({
1144 type: 'error'
1145 });
1146 }
1147 }); // 基础库 1.4.0 开始支持
1148
1149 if (this._request && this._request.onProgressUpdate) {
1150 this._request.onProgressUpdate(function (_ref) {
1151 var totalBytesSent = _ref.totalBytesSent,
1152 totalBytesExpectedToSend = _ref.totalBytesExpectedToSend;
1153
1154 _this3.upload.dispatchEvent({
1155 type: 'progress',
1156 loaded: totalBytesSent,
1157 total: totalBytesExpectedToSend
1158 });
1159 });
1160 }
1161 } else {
1162 this._request = wx.request({
1163 url: this._url,
1164 data: data || '',
1165 // method 的 value 居然必须为大写
1166 method: this._method.toUpperCase(),
1167 header: this._headers,
1168 success: successCallback.bind(this),
1169 fail: function fail(error) {
1170 _this3.status = 0;
1171 _this3.readyState = DONE;
1172
1173 _this3.dispatchEvent({
1174 type: 'readystatechange'
1175 });
1176
1177 _this3.dispatchEvent({
1178 type: 'error'
1179 });
1180 }
1181 });
1182 }
1183 };
1184
1185 return XMLHttpRequest;
1186 }(eventTarget(REQUEST_EVENTS));
1187
1188 objectAssign(XMLHttpRequest$1, {
1189 UNSENT: UNSENT,
1190 OPENED: OPENED,
1191 HEADERS_RECEIVED: HEADERS_RECEIVED,
1192 LOADING: LOADING,
1193 DONE: DONE
1194 });
1195 var xmlhttprequest = XMLHttpRequest$1;
1196
1197 var CONNECTING = 0;
1198 var OPEN = 1;
1199 var CLOSING = 2;
1200 var CLOSED = 3;
1201 var EVENTS = ['open', 'error', 'message', 'close'];
1202
1203 var WebSocket$1 =
1204 /*#__PURE__*/
1205 function (_EventTarget) {
1206 _inheritsLoose(WebSocket, _EventTarget);
1207
1208 function WebSocket(url, protocal) {
1209 var _this;
1210
1211 if (!url) {
1212 throw new TypeError('Failed to construct \'WebSocket\': url required');
1213 }
1214
1215 if (protocal && !(wx.canIUse && wx.canIUse('connectSocket.object.protocols'))) {
1216 throw new Error('subprotocal not supported in weapp');
1217 }
1218
1219 _this = _EventTarget.call(this) || this;
1220 _this._url = url;
1221 _this._protocal = protocal || ''; // default value according to specs
1222
1223 _this._readyState = CONNECTING;
1224
1225 var errorHandler = function errorHandler(event) {
1226 _this._readyState = CLOSED;
1227
1228 _this.dispatchEvent({
1229 type: 'error',
1230 message: event.errMsg
1231 });
1232 };
1233
1234 var socketTask = wx.connectSocket({
1235 url: url,
1236 protocals: _this._protocal,
1237 fail: function fail(error) {
1238 return setTimeout(function () {
1239 return errorHandler(error);
1240 }, 0);
1241 }
1242 });
1243 _this._socketTask = socketTask;
1244 socketTask.onOpen(function (event) {
1245 _this._readyState = OPEN;
1246
1247 _this.dispatchEvent({
1248 type: 'open'
1249 });
1250 });
1251 socketTask.onError(errorHandler);
1252 socketTask.onMessage(function (event) {
1253 var data = event.data,
1254 origin = event.origin,
1255 ports = event.ports,
1256 source = event.source;
1257
1258 _this.dispatchEvent({
1259 data: data,
1260 origin: origin,
1261 ports: ports,
1262 source: source,
1263 type: 'message'
1264 });
1265 });
1266 socketTask.onClose(function (event) {
1267 _this._readyState = CLOSED;
1268 var code = event.code,
1269 reason = event.reason,
1270 wasClean = event.wasClean;
1271
1272 _this.dispatchEvent({
1273 code: code,
1274 reason: reason,
1275 wasClean: wasClean,
1276 type: 'close'
1277 });
1278 });
1279 return _this;
1280 }
1281
1282 var _proto = WebSocket.prototype;
1283
1284 _proto.close = function close() {
1285 if (this.readyState === CLOSED) return;
1286
1287 if (this.readyState === CONNECTING) {
1288 console.warn('close WebSocket which is connecting might not work');
1289 }
1290
1291 this._socketTask.close();
1292 };
1293
1294 _proto.send = function send(data) {
1295 if (this.readyState !== OPEN) {
1296 throw new Error('INVALID_STATE_ERR');
1297 }
1298
1299 if (!(typeof data === 'string' || data instanceof ArrayBuffer)) {
1300 throw new TypeError('only String/ArrayBuffer supported');
1301 }
1302
1303 this._socketTask.send({
1304 data: data
1305 });
1306 };
1307
1308 _createClass(WebSocket, [{
1309 key: "url",
1310 get: function get() {
1311 return this._url;
1312 }
1313 }, {
1314 key: "protocal",
1315 get: function get() {
1316 return this._protocal;
1317 }
1318 }, {
1319 key: "readyState",
1320 get: function get() {
1321 return this._readyState;
1322 }
1323 }]);
1324
1325 return WebSocket;
1326 }(eventTarget(EVENTS));
1327
1328 objectAssign(WebSocket$1, {
1329 CONNECTING: CONNECTING,
1330 OPEN: OPEN,
1331 CLOSING: CLOSING,
1332 CLOSED: CLOSED
1333 });
1334 var websocket = WebSocket$1;
1335
1336 var polyfill = function polyfill(target) {
1337 if (!wx.onNetworkStatusChange) return;
1338 if (target.__onlineOfflinePolyfilled) return;
1339 target.__onlineOfflinePolyfilled = true;
1340 var internalEventTarget = new eventTarget();
1341
1342 if (!target.dispatchEvent) {
1343 target.addEventListener = internalEventTarget.addEventListener.bind(internalEventTarget);
1344 target.removeEventListener = internalEventTarget.removeEventListener.bind(internalEventTarget);
1345 target.dispatchEvent = internalEventTarget.dispatchEvent.bind(internalEventTarget); // avoid this condition to be true:
1346 // https://github.com/zloirock/core-js/blob/9f051803760c02b306aae2595621bb7ef698fc29/library/modules/_task.js#L61
1347
1348 if (typeof postMessage == 'function' && !target.importScripts) {
1349 target.importScripts = function () {
1350 throw new Error('mocked');
1351 };
1352 }
1353 }
1354
1355 wx.getNetworkType({
1356 success: function success(_ref) {
1357 var networkType = _ref.networkType;
1358 target.onLine = networkType !== 'none';
1359 wx.onNetworkStatusChange(function (_ref2) {
1360 var isConnected = _ref2.isConnected;
1361 if (target.onLine === isConnected) return;
1362 target.onLine = isConnected;
1363 target.dispatchEvent({
1364 type: isConnected ? 'online' : 'offline'
1365 });
1366 });
1367 }
1368 });
1369 };
1370
1371 var onlineOfflineEvents = {
1372 polyfill: polyfill
1373 };
1374
1375 var navigator$1 = {};
1376
1377 var weappPolyfill = {
1378 polyfill: function polyfill() {
1379 var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : commonjsGlobal || window;
1380
1381 if (_typeof(target) !== 'object') {
1382 throw new Error('polyfill target is not an Object');
1383 }
1384
1385 var polyfills = {
1386 localStorage: localstorage,
1387 XMLHttpRequest: xmlhttprequest,
1388 FormData: formdata,
1389 WebSocket: websocket,
1390 Object: Object,
1391 navigator: navigator$1
1392 };
1393
1394 for (var k in polyfills) {
1395 if (!target[k]) target[k] = polyfills[k];
1396 }
1397
1398 onlineOfflineEvents.polyfill(target); // 假扮为 React Native,让 axios 认为这是一个 nonStandardBrowserEnv
1399
1400 target.navigator.product = 'ReactNative';
1401 },
1402 localStorage: localstorage,
1403 XMLHttpRequest: xmlhttprequest,
1404 FormData: formdata,
1405 WebSocket: websocket
1406 };
1407
1408 var polyfill$1 = weappPolyfill.polyfill;
1409
1410 try {
1411 polyfill$1();
1412 } catch (e) {}
1413
1414 try {
1415 polyfill$1(GameGlobal);
1416 } catch (e) {}
1417
1418 try {
1419 window = window || {};
1420 polyfill$1(window);
1421 } catch (e) {}
1422
1423 try {
1424 localStorage = localStorage || localstorage;
1425 } catch (e) {}
1426
1427 try {
1428 XMLHttpRequest = XMLHttpRequest || xmlhttprequest;
1429 } catch (e) {}
1430
1431 try {
1432 FormData = FormData || formdata;
1433 } catch (e) {}
1434
1435 try {
1436 WebSocket = WebSocket || websocket;
1437 } catch (e) {}
1438
1439 try {
1440 navigator = navigator || navigator$1;
1441 } catch (e) {}
1442
1443 // eslint-disable-next-line import/no-extraneous-dependencies
1444
1445 var long_1 = createCommonjsModule(function (module) {
1446 /*
1447 Copyright 2013 Daniel Wirtz <dcode@dcode.io>
1448 Copyright 2009 The Closure Library Authors. All Rights Reserved.
1449
1450 Licensed under the Apache License, Version 2.0 (the "License");
1451 you may not use this file except in compliance with the License.
1452 You may obtain a copy of the License at
1453
1454 http://www.apache.org/licenses/LICENSE-2.0
1455
1456 Unless required by applicable law or agreed to in writing, software
1457 distributed under the License is distributed on an "AS-IS" BASIS,
1458 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1459 See the License for the specific language governing permissions and
1460 limitations under the License.
1461 */
1462
1463 /**
1464 * @license long.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
1465 * Released under the Apache License, Version 2.0
1466 * see: https://github.com/dcodeIO/long.js for details
1467 */
1468 (function(global, factory) {
1469
1470 /* AMD */ if (typeof commonjsRequire === 'function' && 'object' === "object" && module && module["exports"])
1471 module["exports"] = factory();
1472 /* Global */ else
1473 (global["dcodeIO"] = global["dcodeIO"] || {})["Long"] = factory();
1474
1475 })(commonjsGlobal, function() {
1476
1477 /**
1478 * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
1479 * See the from* functions below for more convenient ways of constructing Longs.
1480 * @exports Long
1481 * @class A Long class for representing a 64 bit two's-complement integer value.
1482 * @param {number} low The low (signed) 32 bits of the long
1483 * @param {number} high The high (signed) 32 bits of the long
1484 * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
1485 * @constructor
1486 */
1487 function Long(low, high, unsigned) {
1488
1489 /**
1490 * The low 32 bits as a signed value.
1491 * @type {number}
1492 */
1493 this.low = low | 0;
1494
1495 /**
1496 * The high 32 bits as a signed value.
1497 * @type {number}
1498 */
1499 this.high = high | 0;
1500
1501 /**
1502 * Whether unsigned or not.
1503 * @type {boolean}
1504 */
1505 this.unsigned = !!unsigned;
1506 }
1507
1508 // The internal representation of a long is the two given signed, 32-bit values.
1509 // We use 32-bit pieces because these are the size of integers on which
1510 // Javascript performs bit-operations. For operations like addition and
1511 // multiplication, we split each number into 16 bit pieces, which can easily be
1512 // multiplied within Javascript's floating-point representation without overflow
1513 // or change in sign.
1514 //
1515 // In the algorithms below, we frequently reduce the negative case to the
1516 // positive case by negating the input(s) and then post-processing the result.
1517 // Note that we must ALWAYS check specially whether those values are MIN_VALUE
1518 // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
1519 // a positive number, it overflows back into a negative). Not handling this
1520 // case would often result in infinite recursion.
1521 //
1522 // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
1523 // methods on which they depend.
1524
1525 /**
1526 * An indicator used to reliably determine if an object is a Long or not.
1527 * @type {boolean}
1528 * @const
1529 * @private
1530 */
1531 Long.prototype.__isLong__;
1532
1533 Object.defineProperty(Long.prototype, "__isLong__", {
1534 value: true,
1535 enumerable: false,
1536 configurable: false
1537 });
1538
1539 /**
1540 * @function
1541 * @param {*} obj Object
1542 * @returns {boolean}
1543 * @inner
1544 */
1545 function isLong(obj) {
1546 return (obj && obj["__isLong__"]) === true;
1547 }
1548
1549 /**
1550 * Tests if the specified object is a Long.
1551 * @function
1552 * @param {*} obj Object
1553 * @returns {boolean}
1554 */
1555 Long.isLong = isLong;
1556
1557 /**
1558 * A cache of the Long representations of small integer values.
1559 * @type {!Object}
1560 * @inner
1561 */
1562 var INT_CACHE = {};
1563
1564 /**
1565 * A cache of the Long representations of small unsigned integer values.
1566 * @type {!Object}
1567 * @inner
1568 */
1569 var UINT_CACHE = {};
1570
1571 /**
1572 * @param {number} value
1573 * @param {boolean=} unsigned
1574 * @returns {!Long}
1575 * @inner
1576 */
1577 function fromInt(value, unsigned) {
1578 var obj, cachedObj, cache;
1579 if (unsigned) {
1580 value >>>= 0;
1581 if (cache = (0 <= value && value < 256)) {
1582 cachedObj = UINT_CACHE[value];
1583 if (cachedObj)
1584 return cachedObj;
1585 }
1586 obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
1587 if (cache)
1588 UINT_CACHE[value] = obj;
1589 return obj;
1590 } else {
1591 value |= 0;
1592 if (cache = (-128 <= value && value < 128)) {
1593 cachedObj = INT_CACHE[value];
1594 if (cachedObj)
1595 return cachedObj;
1596 }
1597 obj = fromBits(value, value < 0 ? -1 : 0, false);
1598 if (cache)
1599 INT_CACHE[value] = obj;
1600 return obj;
1601 }
1602 }
1603
1604 /**
1605 * Returns a Long representing the given 32 bit integer value.
1606 * @function
1607 * @param {number} value The 32 bit integer in question
1608 * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
1609 * @returns {!Long} The corresponding Long value
1610 */
1611 Long.fromInt = fromInt;
1612
1613 /**
1614 * @param {number} value
1615 * @param {boolean=} unsigned
1616 * @returns {!Long}
1617 * @inner
1618 */
1619 function fromNumber(value, unsigned) {
1620 if (isNaN(value) || !isFinite(value))
1621 return unsigned ? UZERO : ZERO;
1622 if (unsigned) {
1623 if (value < 0)
1624 return UZERO;
1625 if (value >= TWO_PWR_64_DBL)
1626 return MAX_UNSIGNED_VALUE;
1627 } else {
1628 if (value <= -TWO_PWR_63_DBL)
1629 return MIN_VALUE;
1630 if (value + 1 >= TWO_PWR_63_DBL)
1631 return MAX_VALUE;
1632 }
1633 if (value < 0)
1634 return fromNumber(-value, unsigned).neg();
1635 return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1636 }
1637
1638 /**
1639 * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
1640 * @function
1641 * @param {number} value The number in question
1642 * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
1643 * @returns {!Long} The corresponding Long value
1644 */
1645 Long.fromNumber = fromNumber;
1646
1647 /**
1648 * @param {number} lowBits
1649 * @param {number} highBits
1650 * @param {boolean=} unsigned
1651 * @returns {!Long}
1652 * @inner
1653 */
1654 function fromBits(lowBits, highBits, unsigned) {
1655 return new Long(lowBits, highBits, unsigned);
1656 }
1657
1658 /**
1659 * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
1660 * assumed to use 32 bits.
1661 * @function
1662 * @param {number} lowBits The low 32 bits
1663 * @param {number} highBits The high 32 bits
1664 * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
1665 * @returns {!Long} The corresponding Long value
1666 */
1667 Long.fromBits = fromBits;
1668
1669 /**
1670 * @function
1671 * @param {number} base
1672 * @param {number} exponent
1673 * @returns {number}
1674 * @inner
1675 */
1676 var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
1677
1678 /**
1679 * @param {string} str
1680 * @param {(boolean|number)=} unsigned
1681 * @param {number=} radix
1682 * @returns {!Long}
1683 * @inner
1684 */
1685 function fromString(str, unsigned, radix) {
1686 if (str.length === 0)
1687 throw Error('empty string');
1688 if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
1689 return ZERO;
1690 if (typeof unsigned === 'number') {
1691 // For goog.math.long compatibility
1692 radix = unsigned,
1693 unsigned = false;
1694 } else {
1695 unsigned = !! unsigned;
1696 }
1697 radix = radix || 10;
1698 if (radix < 2 || 36 < radix)
1699 throw RangeError('radix');
1700
1701 var p;
1702 if ((p = str.indexOf('-')) > 0)
1703 throw Error('interior hyphen');
1704 else if (p === 0) {
1705 return fromString(str.substring(1), unsigned, radix).neg();
1706 }
1707
1708 // Do several (8) digits each time through the loop, so as to
1709 // minimize the calls to the very expensive emulated div.
1710 var radixToPower = fromNumber(pow_dbl(radix, 8));
1711
1712 var result = ZERO;
1713 for (var i = 0; i < str.length; i += 8) {
1714 var size = Math.min(8, str.length - i),
1715 value = parseInt(str.substring(i, i + size), radix);
1716 if (size < 8) {
1717 var power = fromNumber(pow_dbl(radix, size));
1718 result = result.mul(power).add(fromNumber(value));
1719 } else {
1720 result = result.mul(radixToPower);
1721 result = result.add(fromNumber(value));
1722 }
1723 }
1724 result.unsigned = unsigned;
1725 return result;
1726 }
1727
1728 /**
1729 * Returns a Long representation of the given string, written using the specified radix.
1730 * @function
1731 * @param {string} str The textual representation of the Long
1732 * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed
1733 * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
1734 * @returns {!Long} The corresponding Long value
1735 */
1736 Long.fromString = fromString;
1737
1738 /**
1739 * @function
1740 * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
1741 * @returns {!Long}
1742 * @inner
1743 */
1744 function fromValue(val) {
1745 if (val /* is compatible */ instanceof Long)
1746 return val;
1747 if (typeof val === 'number')
1748 return fromNumber(val);
1749 if (typeof val === 'string')
1750 return fromString(val);
1751 // Throws for non-objects, converts non-instanceof Long:
1752 return fromBits(val.low, val.high, val.unsigned);
1753 }
1754
1755 /**
1756 * Converts the specified value to a Long.
1757 * @function
1758 * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
1759 * @returns {!Long}
1760 */
1761 Long.fromValue = fromValue;
1762
1763 // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
1764 // no runtime penalty for these.
1765
1766 /**
1767 * @type {number}
1768 * @const
1769 * @inner
1770 */
1771 var TWO_PWR_16_DBL = 1 << 16;
1772
1773 /**
1774 * @type {number}
1775 * @const
1776 * @inner
1777 */
1778 var TWO_PWR_24_DBL = 1 << 24;
1779
1780 /**
1781 * @type {number}
1782 * @const
1783 * @inner
1784 */
1785 var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
1786
1787 /**
1788 * @type {number}
1789 * @const
1790 * @inner
1791 */
1792 var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
1793
1794 /**
1795 * @type {number}
1796 * @const
1797 * @inner
1798 */
1799 var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
1800
1801 /**
1802 * @type {!Long}
1803 * @const
1804 * @inner
1805 */
1806 var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
1807
1808 /**
1809 * @type {!Long}
1810 * @inner
1811 */
1812 var ZERO = fromInt(0);
1813
1814 /**
1815 * Signed zero.
1816 * @type {!Long}
1817 */
1818 Long.ZERO = ZERO;
1819
1820 /**
1821 * @type {!Long}
1822 * @inner
1823 */
1824 var UZERO = fromInt(0, true);
1825
1826 /**
1827 * Unsigned zero.
1828 * @type {!Long}
1829 */
1830 Long.UZERO = UZERO;
1831
1832 /**
1833 * @type {!Long}
1834 * @inner
1835 */
1836 var ONE = fromInt(1);
1837
1838 /**
1839 * Signed one.
1840 * @type {!Long}
1841 */
1842 Long.ONE = ONE;
1843
1844 /**
1845 * @type {!Long}
1846 * @inner
1847 */
1848 var UONE = fromInt(1, true);
1849
1850 /**
1851 * Unsigned one.
1852 * @type {!Long}
1853 */
1854 Long.UONE = UONE;
1855
1856 /**
1857 * @type {!Long}
1858 * @inner
1859 */
1860 var NEG_ONE = fromInt(-1);
1861
1862 /**
1863 * Signed negative one.
1864 * @type {!Long}
1865 */
1866 Long.NEG_ONE = NEG_ONE;
1867
1868 /**
1869 * @type {!Long}
1870 * @inner
1871 */
1872 var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
1873
1874 /**
1875 * Maximum signed value.
1876 * @type {!Long}
1877 */
1878 Long.MAX_VALUE = MAX_VALUE;
1879
1880 /**
1881 * @type {!Long}
1882 * @inner
1883 */
1884 var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
1885
1886 /**
1887 * Maximum unsigned value.
1888 * @type {!Long}
1889 */
1890 Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
1891
1892 /**
1893 * @type {!Long}
1894 * @inner
1895 */
1896 var MIN_VALUE = fromBits(0, 0x80000000|0, false);
1897
1898 /**
1899 * Minimum signed value.
1900 * @type {!Long}
1901 */
1902 Long.MIN_VALUE = MIN_VALUE;
1903
1904 /**
1905 * @alias Long.prototype
1906 * @inner
1907 */
1908 var LongPrototype = Long.prototype;
1909
1910 /**
1911 * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
1912 * @returns {number}
1913 */
1914 LongPrototype.toInt = function toInt() {
1915 return this.unsigned ? this.low >>> 0 : this.low;
1916 };
1917
1918 /**
1919 * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
1920 * @returns {number}
1921 */
1922 LongPrototype.toNumber = function toNumber() {
1923 if (this.unsigned)
1924 return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
1925 return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
1926 };
1927
1928 /**
1929 * Converts the Long to a string written in the specified radix.
1930 * @param {number=} radix Radix (2-36), defaults to 10
1931 * @returns {string}
1932 * @override
1933 * @throws {RangeError} If `radix` is out of range
1934 */
1935 LongPrototype.toString = function toString(radix) {
1936 radix = radix || 10;
1937 if (radix < 2 || 36 < radix)
1938 throw RangeError('radix');
1939 if (this.isZero())
1940 return '0';
1941 if (this.isNegative()) { // Unsigned Longs are never negative
1942 if (this.eq(MIN_VALUE)) {
1943 // We need to change the Long value before it can be negated, so we remove
1944 // the bottom-most digit in this base and then recurse to do the rest.
1945 var radixLong = fromNumber(radix),
1946 div = this.div(radixLong),
1947 rem1 = div.mul(radixLong).sub(this);
1948 return div.toString(radix) + rem1.toInt().toString(radix);
1949 } else
1950 return '-' + this.neg().toString(radix);
1951 }
1952
1953 // Do several (6) digits each time through the loop, so as to
1954 // minimize the calls to the very expensive emulated div.
1955 var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
1956 rem = this;
1957 var result = '';
1958 while (true) {
1959 var remDiv = rem.div(radixToPower),
1960 intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
1961 digits = intval.toString(radix);
1962 rem = remDiv;
1963 if (rem.isZero())
1964 return digits + result;
1965 else {
1966 while (digits.length < 6)
1967 digits = '0' + digits;
1968 result = '' + digits + result;
1969 }
1970 }
1971 };
1972
1973 /**
1974 * Gets the high 32 bits as a signed integer.
1975 * @returns {number} Signed high bits
1976 */
1977 LongPrototype.getHighBits = function getHighBits() {
1978 return this.high;
1979 };
1980
1981 /**
1982 * Gets the high 32 bits as an unsigned integer.
1983 * @returns {number} Unsigned high bits
1984 */
1985 LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
1986 return this.high >>> 0;
1987 };
1988
1989 /**
1990 * Gets the low 32 bits as a signed integer.
1991 * @returns {number} Signed low bits
1992 */
1993 LongPrototype.getLowBits = function getLowBits() {
1994 return this.low;
1995 };
1996
1997 /**
1998 * Gets the low 32 bits as an unsigned integer.
1999 * @returns {number} Unsigned low bits
2000 */
2001 LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
2002 return this.low >>> 0;
2003 };
2004
2005 /**
2006 * Gets the number of bits needed to represent the absolute value of this Long.
2007 * @returns {number}
2008 */
2009 LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
2010 if (this.isNegative()) // Unsigned Longs are never negative
2011 return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
2012 var val = this.high != 0 ? this.high : this.low;
2013 for (var bit = 31; bit > 0; bit--)
2014 if ((val & (1 << bit)) != 0)
2015 break;
2016 return this.high != 0 ? bit + 33 : bit + 1;
2017 };
2018
2019 /**
2020 * Tests if this Long's value equals zero.
2021 * @returns {boolean}
2022 */
2023 LongPrototype.isZero = function isZero() {
2024 return this.high === 0 && this.low === 0;
2025 };
2026
2027 /**
2028 * Tests if this Long's value is negative.
2029 * @returns {boolean}
2030 */
2031 LongPrototype.isNegative = function isNegative() {
2032 return !this.unsigned && this.high < 0;
2033 };
2034
2035 /**
2036 * Tests if this Long's value is positive.
2037 * @returns {boolean}
2038 */
2039 LongPrototype.isPositive = function isPositive() {
2040 return this.unsigned || this.high >= 0;
2041 };
2042
2043 /**
2044 * Tests if this Long's value is odd.
2045 * @returns {boolean}
2046 */
2047 LongPrototype.isOdd = function isOdd() {
2048 return (this.low & 1) === 1;
2049 };
2050
2051 /**
2052 * Tests if this Long's value is even.
2053 * @returns {boolean}
2054 */
2055 LongPrototype.isEven = function isEven() {
2056 return (this.low & 1) === 0;
2057 };
2058
2059 /**
2060 * Tests if this Long's value equals the specified's.
2061 * @param {!Long|number|string} other Other value
2062 * @returns {boolean}
2063 */
2064 LongPrototype.equals = function equals(other) {
2065 if (!isLong(other))
2066 other = fromValue(other);
2067 if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
2068 return false;
2069 return this.high === other.high && this.low === other.low;
2070 };
2071
2072 /**
2073 * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
2074 * @function
2075 * @param {!Long|number|string} other Other value
2076 * @returns {boolean}
2077 */
2078 LongPrototype.eq = LongPrototype.equals;
2079
2080 /**
2081 * Tests if this Long's value differs from the specified's.
2082 * @param {!Long|number|string} other Other value
2083 * @returns {boolean}
2084 */
2085 LongPrototype.notEquals = function notEquals(other) {
2086 return !this.eq(/* validates */ other);
2087 };
2088
2089 /**
2090 * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
2091 * @function
2092 * @param {!Long|number|string} other Other value
2093 * @returns {boolean}
2094 */
2095 LongPrototype.neq = LongPrototype.notEquals;
2096
2097 /**
2098 * Tests if this Long's value is less than the specified's.
2099 * @param {!Long|number|string} other Other value
2100 * @returns {boolean}
2101 */
2102 LongPrototype.lessThan = function lessThan(other) {
2103 return this.comp(/* validates */ other) < 0;
2104 };
2105
2106 /**
2107 * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
2108 * @function
2109 * @param {!Long|number|string} other Other value
2110 * @returns {boolean}
2111 */
2112 LongPrototype.lt = LongPrototype.lessThan;
2113
2114 /**
2115 * Tests if this Long's value is less than or equal the specified's.
2116 * @param {!Long|number|string} other Other value
2117 * @returns {boolean}
2118 */
2119 LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
2120 return this.comp(/* validates */ other) <= 0;
2121 };
2122
2123 /**
2124 * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
2125 * @function
2126 * @param {!Long|number|string} other Other value
2127 * @returns {boolean}
2128 */
2129 LongPrototype.lte = LongPrototype.lessThanOrEqual;
2130
2131 /**
2132 * Tests if this Long's value is greater than the specified's.
2133 * @param {!Long|number|string} other Other value
2134 * @returns {boolean}
2135 */
2136 LongPrototype.greaterThan = function greaterThan(other) {
2137 return this.comp(/* validates */ other) > 0;
2138 };
2139
2140 /**
2141 * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
2142 * @function
2143 * @param {!Long|number|string} other Other value
2144 * @returns {boolean}
2145 */
2146 LongPrototype.gt = LongPrototype.greaterThan;
2147
2148 /**
2149 * Tests if this Long's value is greater than or equal the specified's.
2150 * @param {!Long|number|string} other Other value
2151 * @returns {boolean}
2152 */
2153 LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
2154 return this.comp(/* validates */ other) >= 0;
2155 };
2156
2157 /**
2158 * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
2159 * @function
2160 * @param {!Long|number|string} other Other value
2161 * @returns {boolean}
2162 */
2163 LongPrototype.gte = LongPrototype.greaterThanOrEqual;
2164
2165 /**
2166 * Compares this Long's value with the specified's.
2167 * @param {!Long|number|string} other Other value
2168 * @returns {number} 0 if they are the same, 1 if the this is greater and -1
2169 * if the given one is greater
2170 */
2171 LongPrototype.compare = function compare(other) {
2172 if (!isLong(other))
2173 other = fromValue(other);
2174 if (this.eq(other))
2175 return 0;
2176 var thisNeg = this.isNegative(),
2177 otherNeg = other.isNegative();
2178 if (thisNeg && !otherNeg)
2179 return -1;
2180 if (!thisNeg && otherNeg)
2181 return 1;
2182 // At this point the sign bits are the same
2183 if (!this.unsigned)
2184 return this.sub(other).isNegative() ? -1 : 1;
2185 // Both are positive if at least one is unsigned
2186 return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
2187 };
2188
2189 /**
2190 * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
2191 * @function
2192 * @param {!Long|number|string} other Other value
2193 * @returns {number} 0 if they are the same, 1 if the this is greater and -1
2194 * if the given one is greater
2195 */
2196 LongPrototype.comp = LongPrototype.compare;
2197
2198 /**
2199 * Negates this Long's value.
2200 * @returns {!Long} Negated Long
2201 */
2202 LongPrototype.negate = function negate() {
2203 if (!this.unsigned && this.eq(MIN_VALUE))
2204 return MIN_VALUE;
2205 return this.not().add(ONE);
2206 };
2207
2208 /**
2209 * Negates this Long's value. This is an alias of {@link Long#negate}.
2210 * @function
2211 * @returns {!Long} Negated Long
2212 */
2213 LongPrototype.neg = LongPrototype.negate;
2214
2215 /**
2216 * Returns the sum of this and the specified Long.
2217 * @param {!Long|number|string} addend Addend
2218 * @returns {!Long} Sum
2219 */
2220 LongPrototype.add = function add(addend) {
2221 if (!isLong(addend))
2222 addend = fromValue(addend);
2223
2224 // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
2225
2226 var a48 = this.high >>> 16;
2227 var a32 = this.high & 0xFFFF;
2228 var a16 = this.low >>> 16;
2229 var a00 = this.low & 0xFFFF;
2230
2231 var b48 = addend.high >>> 16;
2232 var b32 = addend.high & 0xFFFF;
2233 var b16 = addend.low >>> 16;
2234 var b00 = addend.low & 0xFFFF;
2235
2236 var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
2237 c00 += a00 + b00;
2238 c16 += c00 >>> 16;
2239 c00 &= 0xFFFF;
2240 c16 += a16 + b16;
2241 c32 += c16 >>> 16;
2242 c16 &= 0xFFFF;
2243 c32 += a32 + b32;
2244 c48 += c32 >>> 16;
2245 c32 &= 0xFFFF;
2246 c48 += a48 + b48;
2247 c48 &= 0xFFFF;
2248 return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
2249 };
2250
2251 /**
2252 * Returns the difference of this and the specified Long.
2253 * @param {!Long|number|string} subtrahend Subtrahend
2254 * @returns {!Long} Difference
2255 */
2256 LongPrototype.subtract = function subtract(subtrahend) {
2257 if (!isLong(subtrahend))
2258 subtrahend = fromValue(subtrahend);
2259 return this.add(subtrahend.neg());
2260 };
2261
2262 /**
2263 * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
2264 * @function
2265 * @param {!Long|number|string} subtrahend Subtrahend
2266 * @returns {!Long} Difference
2267 */
2268 LongPrototype.sub = LongPrototype.subtract;
2269
2270 /**
2271 * Returns the product of this and the specified Long.
2272 * @param {!Long|number|string} multiplier Multiplier
2273 * @returns {!Long} Product
2274 */
2275 LongPrototype.multiply = function multiply(multiplier) {
2276 if (this.isZero())
2277 return ZERO;
2278 if (!isLong(multiplier))
2279 multiplier = fromValue(multiplier);
2280 if (multiplier.isZero())
2281 return ZERO;
2282 if (this.eq(MIN_VALUE))
2283 return multiplier.isOdd() ? MIN_VALUE : ZERO;
2284 if (multiplier.eq(MIN_VALUE))
2285 return this.isOdd() ? MIN_VALUE : ZERO;
2286
2287 if (this.isNegative()) {
2288 if (multiplier.isNegative())
2289 return this.neg().mul(multiplier.neg());
2290 else
2291 return this.neg().mul(multiplier).neg();
2292 } else if (multiplier.isNegative())
2293 return this.mul(multiplier.neg()).neg();
2294
2295 // If both longs are small, use float multiplication
2296 if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
2297 return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
2298
2299 // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
2300 // We can skip products that would overflow.
2301
2302 var a48 = this.high >>> 16;
2303 var a32 = this.high & 0xFFFF;
2304 var a16 = this.low >>> 16;
2305 var a00 = this.low & 0xFFFF;
2306
2307 var b48 = multiplier.high >>> 16;
2308 var b32 = multiplier.high & 0xFFFF;
2309 var b16 = multiplier.low >>> 16;
2310 var b00 = multiplier.low & 0xFFFF;
2311
2312 var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
2313 c00 += a00 * b00;
2314 c16 += c00 >>> 16;
2315 c00 &= 0xFFFF;
2316 c16 += a16 * b00;
2317 c32 += c16 >>> 16;
2318 c16 &= 0xFFFF;
2319 c16 += a00 * b16;
2320 c32 += c16 >>> 16;
2321 c16 &= 0xFFFF;
2322 c32 += a32 * b00;
2323 c48 += c32 >>> 16;
2324 c32 &= 0xFFFF;
2325 c32 += a16 * b16;
2326 c48 += c32 >>> 16;
2327 c32 &= 0xFFFF;
2328 c32 += a00 * b32;
2329 c48 += c32 >>> 16;
2330 c32 &= 0xFFFF;
2331 c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
2332 c48 &= 0xFFFF;
2333 return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
2334 };
2335
2336 /**
2337 * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
2338 * @function
2339 * @param {!Long|number|string} multiplier Multiplier
2340 * @returns {!Long} Product
2341 */
2342 LongPrototype.mul = LongPrototype.multiply;
2343
2344 /**
2345 * Returns this Long divided by the specified. The result is signed if this Long is signed or
2346 * unsigned if this Long is unsigned.
2347 * @param {!Long|number|string} divisor Divisor
2348 * @returns {!Long} Quotient
2349 */
2350 LongPrototype.divide = function divide(divisor) {
2351 if (!isLong(divisor))
2352 divisor = fromValue(divisor);
2353 if (divisor.isZero())
2354 throw Error('division by zero');
2355 if (this.isZero())
2356 return this.unsigned ? UZERO : ZERO;
2357 var approx, rem, res;
2358 if (!this.unsigned) {
2359 // This section is only relevant for signed longs and is derived from the
2360 // closure library as a whole.
2361 if (this.eq(MIN_VALUE)) {
2362 if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
2363 return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
2364 else if (divisor.eq(MIN_VALUE))
2365 return ONE;
2366 else {
2367 // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
2368 var halfThis = this.shr(1);
2369 approx = halfThis.div(divisor).shl(1);
2370 if (approx.eq(ZERO)) {
2371 return divisor.isNegative() ? ONE : NEG_ONE;
2372 } else {
2373 rem = this.sub(divisor.mul(approx));
2374 res = approx.add(rem.div(divisor));
2375 return res;
2376 }
2377 }
2378 } else if (divisor.eq(MIN_VALUE))
2379 return this.unsigned ? UZERO : ZERO;
2380 if (this.isNegative()) {
2381 if (divisor.isNegative())
2382 return this.neg().div(divisor.neg());
2383 return this.neg().div(divisor).neg();
2384 } else if (divisor.isNegative())
2385 return this.div(divisor.neg()).neg();
2386 res = ZERO;
2387 } else {
2388 // The algorithm below has not been made for unsigned longs. It's therefore
2389 // required to take special care of the MSB prior to running it.
2390 if (!divisor.unsigned)
2391 divisor = divisor.toUnsigned();
2392 if (divisor.gt(this))
2393 return UZERO;
2394 if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
2395 return UONE;
2396 res = UZERO;
2397 }
2398
2399 // Repeat the following until the remainder is less than other: find a
2400 // floating-point that approximates remainder / other *from below*, add this
2401 // into the result, and subtract it from the remainder. It is critical that
2402 // the approximate value is less than or equal to the real value so that the
2403 // remainder never becomes negative.
2404 rem = this;
2405 while (rem.gte(divisor)) {
2406 // Approximate the result of division. This may be a little greater or
2407 // smaller than the actual value.
2408 approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
2409
2410 // We will tweak the approximate result by changing it in the 48-th digit or
2411 // the smallest non-fractional digit, whichever is larger.
2412 var log2 = Math.ceil(Math.log(approx) / Math.LN2),
2413 delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
2414
2415 // Decrease the approximation until it is smaller than the remainder. Note
2416 // that if it is too large, the product overflows and is negative.
2417 approxRes = fromNumber(approx),
2418 approxRem = approxRes.mul(divisor);
2419 while (approxRem.isNegative() || approxRem.gt(rem)) {
2420 approx -= delta;
2421 approxRes = fromNumber(approx, this.unsigned);
2422 approxRem = approxRes.mul(divisor);
2423 }
2424
2425 // We know the answer can't be zero... and actually, zero would cause
2426 // infinite recursion since we would make no progress.
2427 if (approxRes.isZero())
2428 approxRes = ONE;
2429
2430 res = res.add(approxRes);
2431 rem = rem.sub(approxRem);
2432 }
2433 return res;
2434 };
2435
2436 /**
2437 * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
2438 * @function
2439 * @param {!Long|number|string} divisor Divisor
2440 * @returns {!Long} Quotient
2441 */
2442 LongPrototype.div = LongPrototype.divide;
2443
2444 /**
2445 * Returns this Long modulo the specified.
2446 * @param {!Long|number|string} divisor Divisor
2447 * @returns {!Long} Remainder
2448 */
2449 LongPrototype.modulo = function modulo(divisor) {
2450 if (!isLong(divisor))
2451 divisor = fromValue(divisor);
2452 return this.sub(this.div(divisor).mul(divisor));
2453 };
2454
2455 /**
2456 * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
2457 * @function
2458 * @param {!Long|number|string} divisor Divisor
2459 * @returns {!Long} Remainder
2460 */
2461 LongPrototype.mod = LongPrototype.modulo;
2462
2463 /**
2464 * Returns the bitwise NOT of this Long.
2465 * @returns {!Long}
2466 */
2467 LongPrototype.not = function not() {
2468 return fromBits(~this.low, ~this.high, this.unsigned);
2469 };
2470
2471 /**
2472 * Returns the bitwise AND of this Long and the specified.
2473 * @param {!Long|number|string} other Other Long
2474 * @returns {!Long}
2475 */
2476 LongPrototype.and = function and(other) {
2477 if (!isLong(other))
2478 other = fromValue(other);
2479 return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
2480 };
2481
2482 /**
2483 * Returns the bitwise OR of this Long and the specified.
2484 * @param {!Long|number|string} other Other Long
2485 * @returns {!Long}
2486 */
2487 LongPrototype.or = function or(other) {
2488 if (!isLong(other))
2489 other = fromValue(other);
2490 return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
2491 };
2492
2493 /**
2494 * Returns the bitwise XOR of this Long and the given one.
2495 * @param {!Long|number|string} other Other Long
2496 * @returns {!Long}
2497 */
2498 LongPrototype.xor = function xor(other) {
2499 if (!isLong(other))
2500 other = fromValue(other);
2501 return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
2502 };
2503
2504 /**
2505 * Returns this Long with bits shifted to the left by the given amount.
2506 * @param {number|!Long} numBits Number of bits
2507 * @returns {!Long} Shifted Long
2508 */
2509 LongPrototype.shiftLeft = function shiftLeft(numBits) {
2510 if (isLong(numBits))
2511 numBits = numBits.toInt();
2512 if ((numBits &= 63) === 0)
2513 return this;
2514 else if (numBits < 32)
2515 return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
2516 else
2517 return fromBits(0, this.low << (numBits - 32), this.unsigned);
2518 };
2519
2520 /**
2521 * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
2522 * @function
2523 * @param {number|!Long} numBits Number of bits
2524 * @returns {!Long} Shifted Long
2525 */
2526 LongPrototype.shl = LongPrototype.shiftLeft;
2527
2528 /**
2529 * Returns this Long with bits arithmetically shifted to the right by the given amount.
2530 * @param {number|!Long} numBits Number of bits
2531 * @returns {!Long} Shifted Long
2532 */
2533 LongPrototype.shiftRight = function shiftRight(numBits) {
2534 if (isLong(numBits))
2535 numBits = numBits.toInt();
2536 if ((numBits &= 63) === 0)
2537 return this;
2538 else if (numBits < 32)
2539 return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
2540 else
2541 return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
2542 };
2543
2544 /**
2545 * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
2546 * @function
2547 * @param {number|!Long} numBits Number of bits
2548 * @returns {!Long} Shifted Long
2549 */
2550 LongPrototype.shr = LongPrototype.shiftRight;
2551
2552 /**
2553 * Returns this Long with bits logically shifted to the right by the given amount.
2554 * @param {number|!Long} numBits Number of bits
2555 * @returns {!Long} Shifted Long
2556 */
2557 LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
2558 if (isLong(numBits))
2559 numBits = numBits.toInt();
2560 numBits &= 63;
2561 if (numBits === 0)
2562 return this;
2563 else {
2564 var high = this.high;
2565 if (numBits < 32) {
2566 var low = this.low;
2567 return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
2568 } else if (numBits === 32)
2569 return fromBits(high, 0, this.unsigned);
2570 else
2571 return fromBits(high >>> (numBits - 32), 0, this.unsigned);
2572 }
2573 };
2574
2575 /**
2576 * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
2577 * @function
2578 * @param {number|!Long} numBits Number of bits
2579 * @returns {!Long} Shifted Long
2580 */
2581 LongPrototype.shru = LongPrototype.shiftRightUnsigned;
2582
2583 /**
2584 * Converts this Long to signed.
2585 * @returns {!Long} Signed long
2586 */
2587 LongPrototype.toSigned = function toSigned() {
2588 if (!this.unsigned)
2589 return this;
2590 return fromBits(this.low, this.high, false);
2591 };
2592
2593 /**
2594 * Converts this Long to unsigned.
2595 * @returns {!Long} Unsigned long
2596 */
2597 LongPrototype.toUnsigned = function toUnsigned() {
2598 if (this.unsigned)
2599 return this;
2600 return fromBits(this.low, this.high, true);
2601 };
2602
2603 /**
2604 * Converts this Long to its byte representation.
2605 * @param {boolean=} le Whether little or big endian, defaults to big endian
2606 * @returns {!Array.<number>} Byte representation
2607 */
2608 LongPrototype.toBytes = function(le) {
2609 return le ? this.toBytesLE() : this.toBytesBE();
2610 };
2611
2612 /**
2613 * Converts this Long to its little endian byte representation.
2614 * @returns {!Array.<number>} Little endian byte representation
2615 */
2616 LongPrototype.toBytesLE = function() {
2617 var hi = this.high,
2618 lo = this.low;
2619 return [
2620 lo & 0xff,
2621 (lo >>> 8) & 0xff,
2622 (lo >>> 16) & 0xff,
2623 (lo >>> 24) & 0xff,
2624 hi & 0xff,
2625 (hi >>> 8) & 0xff,
2626 (hi >>> 16) & 0xff,
2627 (hi >>> 24) & 0xff
2628 ];
2629 };
2630
2631 /**
2632 * Converts this Long to its big endian byte representation.
2633 * @returns {!Array.<number>} Big endian byte representation
2634 */
2635 LongPrototype.toBytesBE = function() {
2636 var hi = this.high,
2637 lo = this.low;
2638 return [
2639 (hi >>> 24) & 0xff,
2640 (hi >>> 16) & 0xff,
2641 (hi >>> 8) & 0xff,
2642 hi & 0xff,
2643 (lo >>> 24) & 0xff,
2644 (lo >>> 16) & 0xff,
2645 (lo >>> 8) & 0xff,
2646 lo & 0xff
2647 ];
2648 };
2649
2650 return Long;
2651 });
2652 });
2653
2654 var bytebuffer = createCommonjsModule(function (module) {
2655 /*
2656 Copyright 2013-2014 Daniel Wirtz <dcode@dcode.io>
2657
2658 Licensed under the Apache License, Version 2.0 (the "License");
2659 you may not use this file except in compliance with the License.
2660 You may obtain a copy of the License at
2661
2662 http://www.apache.org/licenses/LICENSE-2.0
2663
2664 Unless required by applicable law or agreed to in writing, software
2665 distributed under the License is distributed on an "AS IS" BASIS,
2666 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2667 See the License for the specific language governing permissions and
2668 limitations under the License.
2669 */
2670
2671 /**
2672 * @license bytebuffer.js (c) 2015 Daniel Wirtz <dcode@dcode.io>
2673 * Backing buffer: ArrayBuffer, Accessor: Uint8Array
2674 * Released under the Apache License, Version 2.0
2675 * see: https://github.com/dcodeIO/bytebuffer.js for details
2676 */
2677 (function(global, factory) {
2678
2679 /* AMD */ if (typeof commonjsRequire === 'function' && 'object' === "object" && module && module["exports"])
2680 module['exports'] = (function() {
2681 var Long; try { Long = long_1; } catch (e) {}
2682 return factory(Long);
2683 })();
2684 /* Global */ else
2685 (global["dcodeIO"] = global["dcodeIO"] || {})["ByteBuffer"] = factory(global["dcodeIO"]["Long"]);
2686
2687 })(commonjsGlobal, function(Long) {
2688
2689 /**
2690 * Constructs a new ByteBuffer.
2691 * @class The swiss army knife for binary data in JavaScript.
2692 * @exports ByteBuffer
2693 * @constructor
2694 * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
2695 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
2696 * {@link ByteBuffer.DEFAULT_ENDIAN}.
2697 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
2698 * {@link ByteBuffer.DEFAULT_NOASSERT}.
2699 * @expose
2700 */
2701 var ByteBuffer = function(capacity, littleEndian, noAssert) {
2702 if (typeof capacity === 'undefined')
2703 capacity = ByteBuffer.DEFAULT_CAPACITY;
2704 if (typeof littleEndian === 'undefined')
2705 littleEndian = ByteBuffer.DEFAULT_ENDIAN;
2706 if (typeof noAssert === 'undefined')
2707 noAssert = ByteBuffer.DEFAULT_NOASSERT;
2708 if (!noAssert) {
2709 capacity = capacity | 0;
2710 if (capacity < 0)
2711 throw RangeError("Illegal capacity");
2712 littleEndian = !!littleEndian;
2713 noAssert = !!noAssert;
2714 }
2715
2716 /**
2717 * Backing ArrayBuffer.
2718 * @type {!ArrayBuffer}
2719 * @expose
2720 */
2721 this.buffer = capacity === 0 ? EMPTY_BUFFER : new ArrayBuffer(capacity);
2722
2723 /**
2724 * Uint8Array utilized to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
2725 * @type {?Uint8Array}
2726 * @expose
2727 */
2728 this.view = capacity === 0 ? null : new Uint8Array(this.buffer);
2729
2730 /**
2731 * Absolute read/write offset.
2732 * @type {number}
2733 * @expose
2734 * @see ByteBuffer#flip
2735 * @see ByteBuffer#clear
2736 */
2737 this.offset = 0;
2738
2739 /**
2740 * Marked offset.
2741 * @type {number}
2742 * @expose
2743 * @see ByteBuffer#mark
2744 * @see ByteBuffer#reset
2745 */
2746 this.markedOffset = -1;
2747
2748 /**
2749 * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
2750 * @type {number}
2751 * @expose
2752 * @see ByteBuffer#flip
2753 * @see ByteBuffer#clear
2754 */
2755 this.limit = capacity;
2756
2757 /**
2758 * Whether to use little endian byte order, defaults to `false` for big endian.
2759 * @type {boolean}
2760 * @expose
2761 */
2762 this.littleEndian = littleEndian;
2763
2764 /**
2765 * Whether to skip assertions of offsets and values, defaults to `false`.
2766 * @type {boolean}
2767 * @expose
2768 */
2769 this.noAssert = noAssert;
2770 };
2771
2772 /**
2773 * ByteBuffer version.
2774 * @type {string}
2775 * @const
2776 * @expose
2777 */
2778 ByteBuffer.VERSION = "5.0.1";
2779
2780 /**
2781 * Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
2782 * @type {boolean}
2783 * @const
2784 * @expose
2785 */
2786 ByteBuffer.LITTLE_ENDIAN = true;
2787
2788 /**
2789 * Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
2790 * @type {boolean}
2791 * @const
2792 * @expose
2793 */
2794 ByteBuffer.BIG_ENDIAN = false;
2795
2796 /**
2797 * Default initial capacity of `16`.
2798 * @type {number}
2799 * @expose
2800 */
2801 ByteBuffer.DEFAULT_CAPACITY = 16;
2802
2803 /**
2804 * Default endianess of `false` for big endian.
2805 * @type {boolean}
2806 * @expose
2807 */
2808 ByteBuffer.DEFAULT_ENDIAN = ByteBuffer.BIG_ENDIAN;
2809
2810 /**
2811 * Default no assertions flag of `false`.
2812 * @type {boolean}
2813 * @expose
2814 */
2815 ByteBuffer.DEFAULT_NOASSERT = false;
2816
2817 /**
2818 * A `Long` class for representing a 64-bit two's-complement integer value. May be `null` if Long.js has not been loaded
2819 * and int64 support is not available.
2820 * @type {?Long}
2821 * @const
2822 * @see https://github.com/dcodeIO/long.js
2823 * @expose
2824 */
2825 ByteBuffer.Long = Long || null;
2826
2827 /**
2828 * @alias ByteBuffer.prototype
2829 * @inner
2830 */
2831 var ByteBufferPrototype = ByteBuffer.prototype;
2832
2833 /**
2834 * An indicator used to reliably determine if an object is a ByteBuffer or not.
2835 * @type {boolean}
2836 * @const
2837 * @expose
2838 * @private
2839 */
2840 ByteBufferPrototype.__isByteBuffer__;
2841
2842 Object.defineProperty(ByteBufferPrototype, "__isByteBuffer__", {
2843 value: true,
2844 enumerable: false,
2845 configurable: false
2846 });
2847
2848 // helpers
2849
2850 /**
2851 * @type {!ArrayBuffer}
2852 * @inner
2853 */
2854 var EMPTY_BUFFER = new ArrayBuffer(0);
2855
2856 /**
2857 * String.fromCharCode reference for compile-time renaming.
2858 * @type {function(...number):string}
2859 * @inner
2860 */
2861 var stringFromCharCode = String.fromCharCode;
2862
2863 /**
2864 * Creates a source function for a string.
2865 * @param {string} s String to read from
2866 * @returns {function():number|null} Source function returning the next char code respectively `null` if there are
2867 * no more characters left.
2868 * @throws {TypeError} If the argument is invalid
2869 * @inner
2870 */
2871 function stringSource(s) {
2872 var i=0; return function() {
2873 return i < s.length ? s.charCodeAt(i++) : null;
2874 };
2875 }
2876
2877 /**
2878 * Creates a destination function for a string.
2879 * @returns {function(number=):undefined|string} Destination function successively called with the next char code.
2880 * Returns the final string when called without arguments.
2881 * @inner
2882 */
2883 function stringDestination() {
2884 var cs = [], ps = []; return function() {
2885 if (arguments.length === 0)
2886 return ps.join('')+stringFromCharCode.apply(String, cs);
2887 if (cs.length + arguments.length > 1024)
2888 ps.push(stringFromCharCode.apply(String, cs)),
2889 cs.length = 0;
2890 Array.prototype.push.apply(cs, arguments);
2891 };
2892 }
2893
2894 /**
2895 * Gets the accessor type.
2896 * @returns {Function} `Buffer` under node.js, `Uint8Array` respectively `DataView` in the browser (classes)
2897 * @expose
2898 */
2899 ByteBuffer.accessor = function() {
2900 return Uint8Array;
2901 };
2902 /**
2903 * Allocates a new ByteBuffer backed by a buffer of the specified capacity.
2904 * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
2905 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
2906 * {@link ByteBuffer.DEFAULT_ENDIAN}.
2907 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
2908 * {@link ByteBuffer.DEFAULT_NOASSERT}.
2909 * @returns {!ByteBuffer}
2910 * @expose
2911 */
2912 ByteBuffer.allocate = function(capacity, littleEndian, noAssert) {
2913 return new ByteBuffer(capacity, littleEndian, noAssert);
2914 };
2915
2916 /**
2917 * Concatenates multiple ByteBuffers into one.
2918 * @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array|string>} buffers Buffers to concatenate
2919 * @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
2920 * defaults to "utf8")
2921 * @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
2922 * to {@link ByteBuffer.DEFAULT_ENDIAN}.
2923 * @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
2924 * {@link ByteBuffer.DEFAULT_NOASSERT}.
2925 * @returns {!ByteBuffer} Concatenated ByteBuffer
2926 * @expose
2927 */
2928 ByteBuffer.concat = function(buffers, encoding, littleEndian, noAssert) {
2929 if (typeof encoding === 'boolean' || typeof encoding !== 'string') {
2930 noAssert = littleEndian;
2931 littleEndian = encoding;
2932 encoding = undefined;
2933 }
2934 var capacity = 0;
2935 for (var i=0, k=buffers.length, length; i<k; ++i) {
2936 if (!ByteBuffer.isByteBuffer(buffers[i]))
2937 buffers[i] = ByteBuffer.wrap(buffers[i], encoding);
2938 length = buffers[i].limit - buffers[i].offset;
2939 if (length > 0) capacity += length;
2940 }
2941 if (capacity === 0)
2942 return new ByteBuffer(0, littleEndian, noAssert);
2943 var bb = new ByteBuffer(capacity, littleEndian, noAssert),
2944 bi;
2945 i=0; while (i<k) {
2946 bi = buffers[i++];
2947 length = bi.limit - bi.offset;
2948 if (length <= 0) continue;
2949 bb.view.set(bi.view.subarray(bi.offset, bi.limit), bb.offset);
2950 bb.offset += length;
2951 }
2952 bb.limit = bb.offset;
2953 bb.offset = 0;
2954 return bb;
2955 };
2956
2957 /**
2958 * Tests if the specified type is a ByteBuffer.
2959 * @param {*} bb ByteBuffer to test
2960 * @returns {boolean} `true` if it is a ByteBuffer, otherwise `false`
2961 * @expose
2962 */
2963 ByteBuffer.isByteBuffer = function(bb) {
2964 return (bb && bb["__isByteBuffer__"]) === true;
2965 };
2966 /**
2967 * Gets the backing buffer type.
2968 * @returns {Function} `Buffer` under node.js, `ArrayBuffer` in the browser (classes)
2969 * @expose
2970 */
2971 ByteBuffer.type = function() {
2972 return ArrayBuffer;
2973 };
2974 /**
2975 * Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
2976 * {@link ByteBuffer#limit} to the length of the wrapped data.
2977 * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
2978 * @param {(string|boolean)=} encoding String encoding if `buffer` is a string ("base64", "hex", "binary", defaults to
2979 * "utf8")
2980 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
2981 * {@link ByteBuffer.DEFAULT_ENDIAN}.
2982 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
2983 * {@link ByteBuffer.DEFAULT_NOASSERT}.
2984 * @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
2985 * @expose
2986 */
2987 ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) {
2988 if (typeof encoding !== 'string') {
2989 noAssert = littleEndian;
2990 littleEndian = encoding;
2991 encoding = undefined;
2992 }
2993 if (typeof buffer === 'string') {
2994 if (typeof encoding === 'undefined')
2995 encoding = "utf8";
2996 switch (encoding) {
2997 case "base64":
2998 return ByteBuffer.fromBase64(buffer, littleEndian);
2999 case "hex":
3000 return ByteBuffer.fromHex(buffer, littleEndian);
3001 case "binary":
3002 return ByteBuffer.fromBinary(buffer, littleEndian);
3003 case "utf8":
3004 return ByteBuffer.fromUTF8(buffer, littleEndian);
3005 case "debug":
3006 return ByteBuffer.fromDebug(buffer, littleEndian);
3007 default:
3008 throw Error("Unsupported encoding: "+encoding);
3009 }
3010 }
3011 if (buffer === null || typeof buffer !== 'object')
3012 throw TypeError("Illegal buffer");
3013 var bb;
3014 if (ByteBuffer.isByteBuffer(buffer)) {
3015 bb = ByteBufferPrototype.clone.call(buffer);
3016 bb.markedOffset = -1;
3017 return bb;
3018 }
3019 if (buffer instanceof Uint8Array) { // Extract ArrayBuffer from Uint8Array
3020 bb = new ByteBuffer(0, littleEndian, noAssert);
3021 if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
3022 bb.buffer = buffer.buffer;
3023 bb.offset = buffer.byteOffset;
3024 bb.limit = buffer.byteOffset + buffer.byteLength;
3025 bb.view = new Uint8Array(buffer.buffer);
3026 }
3027 } else if (buffer instanceof ArrayBuffer) { // Reuse ArrayBuffer
3028 bb = new ByteBuffer(0, littleEndian, noAssert);
3029 if (buffer.byteLength > 0) {
3030 bb.buffer = buffer;
3031 bb.offset = 0;
3032 bb.limit = buffer.byteLength;
3033 bb.view = buffer.byteLength > 0 ? new Uint8Array(buffer) : null;
3034 }
3035 } else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
3036 bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
3037 bb.limit = buffer.length;
3038 for (var i=0; i<buffer.length; ++i)
3039 bb.view[i] = buffer[i];
3040 } else
3041 throw TypeError("Illegal buffer"); // Otherwise fail
3042 return bb;
3043 };
3044
3045 /**
3046 * Writes the array as a bitset.
3047 * @param {Array<boolean>} value Array of booleans to write
3048 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
3049 * @returns {!ByteBuffer}
3050 * @expose
3051 */
3052 ByteBufferPrototype.writeBitSet = function(value, offset) {
3053 var relative = typeof offset === 'undefined';
3054 if (relative) offset = this.offset;
3055 if (!this.noAssert) {
3056 if (!(value instanceof Array))
3057 throw TypeError("Illegal BitSet: Not an array");
3058 if (typeof offset !== 'number' || offset % 1 !== 0)
3059 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3060 offset >>>= 0;
3061 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3062 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3063 }
3064
3065 var start = offset,
3066 bits = value.length,
3067 bytes = (bits >> 3),
3068 bit = 0,
3069 k;
3070
3071 offset += this.writeVarint32(bits,offset);
3072
3073 while(bytes--) {
3074 k = (!!value[bit++] & 1) |
3075 ((!!value[bit++] & 1) << 1) |
3076 ((!!value[bit++] & 1) << 2) |
3077 ((!!value[bit++] & 1) << 3) |
3078 ((!!value[bit++] & 1) << 4) |
3079 ((!!value[bit++] & 1) << 5) |
3080 ((!!value[bit++] & 1) << 6) |
3081 ((!!value[bit++] & 1) << 7);
3082 this.writeByte(k,offset++);
3083 }
3084
3085 if(bit < bits) {
3086 var m = 0; k = 0;
3087 while(bit < bits) k = k | ((!!value[bit++] & 1) << (m++));
3088 this.writeByte(k,offset++);
3089 }
3090
3091 if (relative) {
3092 this.offset = offset;
3093 return this;
3094 }
3095 return offset - start;
3096 };
3097
3098 /**
3099 * Reads a BitSet as an array of booleans.
3100 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
3101 * @returns {Array<boolean>
3102 * @expose
3103 */
3104 ByteBufferPrototype.readBitSet = function(offset) {
3105 var relative = typeof offset === 'undefined';
3106 if (relative) offset = this.offset;
3107
3108 var ret = this.readVarint32(offset),
3109 bits = ret.value,
3110 bytes = (bits >> 3),
3111 bit = 0,
3112 value = [],
3113 k;
3114
3115 offset += ret.length;
3116
3117 while(bytes--) {
3118 k = this.readByte(offset++);
3119 value[bit++] = !!(k & 0x01);
3120 value[bit++] = !!(k & 0x02);
3121 value[bit++] = !!(k & 0x04);
3122 value[bit++] = !!(k & 0x08);
3123 value[bit++] = !!(k & 0x10);
3124 value[bit++] = !!(k & 0x20);
3125 value[bit++] = !!(k & 0x40);
3126 value[bit++] = !!(k & 0x80);
3127 }
3128
3129 if(bit < bits) {
3130 var m = 0;
3131 k = this.readByte(offset++);
3132 while(bit < bits) value[bit++] = !!((k >> (m++)) & 1);
3133 }
3134
3135 if (relative) {
3136 this.offset = offset;
3137 }
3138 return value;
3139 };
3140 /**
3141 * Reads the specified number of bytes.
3142 * @param {number} length Number of bytes to read
3143 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
3144 * @returns {!ByteBuffer}
3145 * @expose
3146 */
3147 ByteBufferPrototype.readBytes = function(length, offset) {
3148 var relative = typeof offset === 'undefined';
3149 if (relative) offset = this.offset;
3150 if (!this.noAssert) {
3151 if (typeof offset !== 'number' || offset % 1 !== 0)
3152 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3153 offset >>>= 0;
3154 if (offset < 0 || offset + length > this.buffer.byteLength)
3155 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
3156 }
3157 var slice = this.slice(offset, offset + length);
3158 if (relative) this.offset += length;
3159 return slice;
3160 };
3161
3162 /**
3163 * Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
3164 * @function
3165 * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
3166 * will be modified according to the performed read operation.
3167 * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
3168 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
3169 * written if omitted.
3170 * @returns {!ByteBuffer} this
3171 * @expose
3172 */
3173 ByteBufferPrototype.writeBytes = ByteBufferPrototype.append;
3174
3175 // types/ints/int8
3176
3177 /**
3178 * Writes an 8bit signed integer.
3179 * @param {number} value Value to write
3180 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3181 * @returns {!ByteBuffer} this
3182 * @expose
3183 */
3184 ByteBufferPrototype.writeInt8 = function(value, offset) {
3185 var relative = typeof offset === 'undefined';
3186 if (relative) offset = this.offset;
3187 if (!this.noAssert) {
3188 if (typeof value !== 'number' || value % 1 !== 0)
3189 throw TypeError("Illegal value: "+value+" (not an integer)");
3190 value |= 0;
3191 if (typeof offset !== 'number' || offset % 1 !== 0)
3192 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3193 offset >>>= 0;
3194 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3195 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3196 }
3197 offset += 1;
3198 var capacity0 = this.buffer.byteLength;
3199 if (offset > capacity0)
3200 this.resize((capacity0 *= 2) > offset ? capacity0 : offset);
3201 offset -= 1;
3202 this.view[offset] = value;
3203 if (relative) this.offset += 1;
3204 return this;
3205 };
3206
3207 /**
3208 * Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
3209 * @function
3210 * @param {number} value Value to write
3211 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3212 * @returns {!ByteBuffer} this
3213 * @expose
3214 */
3215 ByteBufferPrototype.writeByte = ByteBufferPrototype.writeInt8;
3216
3217 /**
3218 * Reads an 8bit signed integer.
3219 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3220 * @returns {number} Value read
3221 * @expose
3222 */
3223 ByteBufferPrototype.readInt8 = function(offset) {
3224 var relative = typeof offset === 'undefined';
3225 if (relative) offset = this.offset;
3226 if (!this.noAssert) {
3227 if (typeof offset !== 'number' || offset % 1 !== 0)
3228 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3229 offset >>>= 0;
3230 if (offset < 0 || offset + 1 > this.buffer.byteLength)
3231 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
3232 }
3233 var value = this.view[offset];
3234 if ((value & 0x80) === 0x80) value = -(0xFF - value + 1); // Cast to signed
3235 if (relative) this.offset += 1;
3236 return value;
3237 };
3238
3239 /**
3240 * Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.
3241 * @function
3242 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3243 * @returns {number} Value read
3244 * @expose
3245 */
3246 ByteBufferPrototype.readByte = ByteBufferPrototype.readInt8;
3247
3248 /**
3249 * Writes an 8bit unsigned integer.
3250 * @param {number} value Value to write
3251 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3252 * @returns {!ByteBuffer} this
3253 * @expose
3254 */
3255 ByteBufferPrototype.writeUint8 = function(value, offset) {
3256 var relative = typeof offset === 'undefined';
3257 if (relative) offset = this.offset;
3258 if (!this.noAssert) {
3259 if (typeof value !== 'number' || value % 1 !== 0)
3260 throw TypeError("Illegal value: "+value+" (not an integer)");
3261 value >>>= 0;
3262 if (typeof offset !== 'number' || offset % 1 !== 0)
3263 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3264 offset >>>= 0;
3265 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3266 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3267 }
3268 offset += 1;
3269 var capacity1 = this.buffer.byteLength;
3270 if (offset > capacity1)
3271 this.resize((capacity1 *= 2) > offset ? capacity1 : offset);
3272 offset -= 1;
3273 this.view[offset] = value;
3274 if (relative) this.offset += 1;
3275 return this;
3276 };
3277
3278 /**
3279 * Writes an 8bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint8}.
3280 * @function
3281 * @param {number} value Value to write
3282 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3283 * @returns {!ByteBuffer} this
3284 * @expose
3285 */
3286 ByteBufferPrototype.writeUInt8 = ByteBufferPrototype.writeUint8;
3287
3288 /**
3289 * Reads an 8bit unsigned integer.
3290 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3291 * @returns {number} Value read
3292 * @expose
3293 */
3294 ByteBufferPrototype.readUint8 = function(offset) {
3295 var relative = typeof offset === 'undefined';
3296 if (relative) offset = this.offset;
3297 if (!this.noAssert) {
3298 if (typeof offset !== 'number' || offset % 1 !== 0)
3299 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3300 offset >>>= 0;
3301 if (offset < 0 || offset + 1 > this.buffer.byteLength)
3302 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
3303 }
3304 var value = this.view[offset];
3305 if (relative) this.offset += 1;
3306 return value;
3307 };
3308
3309 /**
3310 * Reads an 8bit unsigned integer. This is an alias of {@link ByteBuffer#readUint8}.
3311 * @function
3312 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
3313 * @returns {number} Value read
3314 * @expose
3315 */
3316 ByteBufferPrototype.readUInt8 = ByteBufferPrototype.readUint8;
3317
3318 // types/ints/int16
3319
3320 /**
3321 * Writes a 16bit signed integer.
3322 * @param {number} value Value to write
3323 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3324 * @throws {TypeError} If `offset` or `value` is not a valid number
3325 * @throws {RangeError} If `offset` is out of bounds
3326 * @expose
3327 */
3328 ByteBufferPrototype.writeInt16 = function(value, offset) {
3329 var relative = typeof offset === 'undefined';
3330 if (relative) offset = this.offset;
3331 if (!this.noAssert) {
3332 if (typeof value !== 'number' || value % 1 !== 0)
3333 throw TypeError("Illegal value: "+value+" (not an integer)");
3334 value |= 0;
3335 if (typeof offset !== 'number' || offset % 1 !== 0)
3336 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3337 offset >>>= 0;
3338 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3339 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3340 }
3341 offset += 2;
3342 var capacity2 = this.buffer.byteLength;
3343 if (offset > capacity2)
3344 this.resize((capacity2 *= 2) > offset ? capacity2 : offset);
3345 offset -= 2;
3346 if (this.littleEndian) {
3347 this.view[offset+1] = (value & 0xFF00) >>> 8;
3348 this.view[offset ] = value & 0x00FF;
3349 } else {
3350 this.view[offset] = (value & 0xFF00) >>> 8;
3351 this.view[offset+1] = value & 0x00FF;
3352 }
3353 if (relative) this.offset += 2;
3354 return this;
3355 };
3356
3357 /**
3358 * Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
3359 * @function
3360 * @param {number} value Value to write
3361 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3362 * @throws {TypeError} If `offset` or `value` is not a valid number
3363 * @throws {RangeError} If `offset` is out of bounds
3364 * @expose
3365 */
3366 ByteBufferPrototype.writeShort = ByteBufferPrototype.writeInt16;
3367
3368 /**
3369 * Reads a 16bit signed integer.
3370 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3371 * @returns {number} Value read
3372 * @throws {TypeError} If `offset` is not a valid number
3373 * @throws {RangeError} If `offset` is out of bounds
3374 * @expose
3375 */
3376 ByteBufferPrototype.readInt16 = function(offset) {
3377 var relative = typeof offset === 'undefined';
3378 if (relative) offset = this.offset;
3379 if (!this.noAssert) {
3380 if (typeof offset !== 'number' || offset % 1 !== 0)
3381 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3382 offset >>>= 0;
3383 if (offset < 0 || offset + 2 > this.buffer.byteLength)
3384 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
3385 }
3386 var value = 0;
3387 if (this.littleEndian) {
3388 value = this.view[offset ];
3389 value |= this.view[offset+1] << 8;
3390 } else {
3391 value = this.view[offset ] << 8;
3392 value |= this.view[offset+1];
3393 }
3394 if ((value & 0x8000) === 0x8000) value = -(0xFFFF - value + 1); // Cast to signed
3395 if (relative) this.offset += 2;
3396 return value;
3397 };
3398
3399 /**
3400 * Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
3401 * @function
3402 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3403 * @returns {number} Value read
3404 * @throws {TypeError} If `offset` is not a valid number
3405 * @throws {RangeError} If `offset` is out of bounds
3406 * @expose
3407 */
3408 ByteBufferPrototype.readShort = ByteBufferPrototype.readInt16;
3409
3410 /**
3411 * Writes a 16bit unsigned integer.
3412 * @param {number} value Value to write
3413 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3414 * @throws {TypeError} If `offset` or `value` is not a valid number
3415 * @throws {RangeError} If `offset` is out of bounds
3416 * @expose
3417 */
3418 ByteBufferPrototype.writeUint16 = function(value, offset) {
3419 var relative = typeof offset === 'undefined';
3420 if (relative) offset = this.offset;
3421 if (!this.noAssert) {
3422 if (typeof value !== 'number' || value % 1 !== 0)
3423 throw TypeError("Illegal value: "+value+" (not an integer)");
3424 value >>>= 0;
3425 if (typeof offset !== 'number' || offset % 1 !== 0)
3426 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3427 offset >>>= 0;
3428 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3429 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3430 }
3431 offset += 2;
3432 var capacity3 = this.buffer.byteLength;
3433 if (offset > capacity3)
3434 this.resize((capacity3 *= 2) > offset ? capacity3 : offset);
3435 offset -= 2;
3436 if (this.littleEndian) {
3437 this.view[offset+1] = (value & 0xFF00) >>> 8;
3438 this.view[offset ] = value & 0x00FF;
3439 } else {
3440 this.view[offset] = (value & 0xFF00) >>> 8;
3441 this.view[offset+1] = value & 0x00FF;
3442 }
3443 if (relative) this.offset += 2;
3444 return this;
3445 };
3446
3447 /**
3448 * Writes a 16bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint16}.
3449 * @function
3450 * @param {number} value Value to write
3451 * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3452 * @throws {TypeError} If `offset` or `value` is not a valid number
3453 * @throws {RangeError} If `offset` is out of bounds
3454 * @expose
3455 */
3456 ByteBufferPrototype.writeUInt16 = ByteBufferPrototype.writeUint16;
3457
3458 /**
3459 * Reads a 16bit unsigned integer.
3460 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3461 * @returns {number} Value read
3462 * @throws {TypeError} If `offset` is not a valid number
3463 * @throws {RangeError} If `offset` is out of bounds
3464 * @expose
3465 */
3466 ByteBufferPrototype.readUint16 = function(offset) {
3467 var relative = typeof offset === 'undefined';
3468 if (relative) offset = this.offset;
3469 if (!this.noAssert) {
3470 if (typeof offset !== 'number' || offset % 1 !== 0)
3471 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3472 offset >>>= 0;
3473 if (offset < 0 || offset + 2 > this.buffer.byteLength)
3474 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
3475 }
3476 var value = 0;
3477 if (this.littleEndian) {
3478 value = this.view[offset ];
3479 value |= this.view[offset+1] << 8;
3480 } else {
3481 value = this.view[offset ] << 8;
3482 value |= this.view[offset+1];
3483 }
3484 if (relative) this.offset += 2;
3485 return value;
3486 };
3487
3488 /**
3489 * Reads a 16bit unsigned integer. This is an alias of {@link ByteBuffer#readUint16}.
3490 * @function
3491 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
3492 * @returns {number} Value read
3493 * @throws {TypeError} If `offset` is not a valid number
3494 * @throws {RangeError} If `offset` is out of bounds
3495 * @expose
3496 */
3497 ByteBufferPrototype.readUInt16 = ByteBufferPrototype.readUint16;
3498
3499 // types/ints/int32
3500
3501 /**
3502 * Writes a 32bit signed integer.
3503 * @param {number} value Value to write
3504 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3505 * @expose
3506 */
3507 ByteBufferPrototype.writeInt32 = function(value, offset) {
3508 var relative = typeof offset === 'undefined';
3509 if (relative) offset = this.offset;
3510 if (!this.noAssert) {
3511 if (typeof value !== 'number' || value % 1 !== 0)
3512 throw TypeError("Illegal value: "+value+" (not an integer)");
3513 value |= 0;
3514 if (typeof offset !== 'number' || offset % 1 !== 0)
3515 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3516 offset >>>= 0;
3517 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3518 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3519 }
3520 offset += 4;
3521 var capacity4 = this.buffer.byteLength;
3522 if (offset > capacity4)
3523 this.resize((capacity4 *= 2) > offset ? capacity4 : offset);
3524 offset -= 4;
3525 if (this.littleEndian) {
3526 this.view[offset+3] = (value >>> 24) & 0xFF;
3527 this.view[offset+2] = (value >>> 16) & 0xFF;
3528 this.view[offset+1] = (value >>> 8) & 0xFF;
3529 this.view[offset ] = value & 0xFF;
3530 } else {
3531 this.view[offset ] = (value >>> 24) & 0xFF;
3532 this.view[offset+1] = (value >>> 16) & 0xFF;
3533 this.view[offset+2] = (value >>> 8) & 0xFF;
3534 this.view[offset+3] = value & 0xFF;
3535 }
3536 if (relative) this.offset += 4;
3537 return this;
3538 };
3539
3540 /**
3541 * Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.
3542 * @param {number} value Value to write
3543 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3544 * @expose
3545 */
3546 ByteBufferPrototype.writeInt = ByteBufferPrototype.writeInt32;
3547
3548 /**
3549 * Reads a 32bit signed integer.
3550 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3551 * @returns {number} Value read
3552 * @expose
3553 */
3554 ByteBufferPrototype.readInt32 = function(offset) {
3555 var relative = typeof offset === 'undefined';
3556 if (relative) offset = this.offset;
3557 if (!this.noAssert) {
3558 if (typeof offset !== 'number' || offset % 1 !== 0)
3559 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3560 offset >>>= 0;
3561 if (offset < 0 || offset + 4 > this.buffer.byteLength)
3562 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
3563 }
3564 var value = 0;
3565 if (this.littleEndian) {
3566 value = this.view[offset+2] << 16;
3567 value |= this.view[offset+1] << 8;
3568 value |= this.view[offset ];
3569 value += this.view[offset+3] << 24 >>> 0;
3570 } else {
3571 value = this.view[offset+1] << 16;
3572 value |= this.view[offset+2] << 8;
3573 value |= this.view[offset+3];
3574 value += this.view[offset ] << 24 >>> 0;
3575 }
3576 value |= 0; // Cast to signed
3577 if (relative) this.offset += 4;
3578 return value;
3579 };
3580
3581 /**
3582 * Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.
3583 * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `4` if omitted.
3584 * @returns {number} Value read
3585 * @expose
3586 */
3587 ByteBufferPrototype.readInt = ByteBufferPrototype.readInt32;
3588
3589 /**
3590 * Writes a 32bit unsigned integer.
3591 * @param {number} value Value to write
3592 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3593 * @expose
3594 */
3595 ByteBufferPrototype.writeUint32 = function(value, offset) {
3596 var relative = typeof offset === 'undefined';
3597 if (relative) offset = this.offset;
3598 if (!this.noAssert) {
3599 if (typeof value !== 'number' || value % 1 !== 0)
3600 throw TypeError("Illegal value: "+value+" (not an integer)");
3601 value >>>= 0;
3602 if (typeof offset !== 'number' || offset % 1 !== 0)
3603 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3604 offset >>>= 0;
3605 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3606 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3607 }
3608 offset += 4;
3609 var capacity5 = this.buffer.byteLength;
3610 if (offset > capacity5)
3611 this.resize((capacity5 *= 2) > offset ? capacity5 : offset);
3612 offset -= 4;
3613 if (this.littleEndian) {
3614 this.view[offset+3] = (value >>> 24) & 0xFF;
3615 this.view[offset+2] = (value >>> 16) & 0xFF;
3616 this.view[offset+1] = (value >>> 8) & 0xFF;
3617 this.view[offset ] = value & 0xFF;
3618 } else {
3619 this.view[offset ] = (value >>> 24) & 0xFF;
3620 this.view[offset+1] = (value >>> 16) & 0xFF;
3621 this.view[offset+2] = (value >>> 8) & 0xFF;
3622 this.view[offset+3] = value & 0xFF;
3623 }
3624 if (relative) this.offset += 4;
3625 return this;
3626 };
3627
3628 /**
3629 * Writes a 32bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint32}.
3630 * @function
3631 * @param {number} value Value to write
3632 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3633 * @expose
3634 */
3635 ByteBufferPrototype.writeUInt32 = ByteBufferPrototype.writeUint32;
3636
3637 /**
3638 * Reads a 32bit unsigned integer.
3639 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3640 * @returns {number} Value read
3641 * @expose
3642 */
3643 ByteBufferPrototype.readUint32 = function(offset) {
3644 var relative = typeof offset === 'undefined';
3645 if (relative) offset = this.offset;
3646 if (!this.noAssert) {
3647 if (typeof offset !== 'number' || offset % 1 !== 0)
3648 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3649 offset >>>= 0;
3650 if (offset < 0 || offset + 4 > this.buffer.byteLength)
3651 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
3652 }
3653 var value = 0;
3654 if (this.littleEndian) {
3655 value = this.view[offset+2] << 16;
3656 value |= this.view[offset+1] << 8;
3657 value |= this.view[offset ];
3658 value += this.view[offset+3] << 24 >>> 0;
3659 } else {
3660 value = this.view[offset+1] << 16;
3661 value |= this.view[offset+2] << 8;
3662 value |= this.view[offset+3];
3663 value += this.view[offset ] << 24 >>> 0;
3664 }
3665 if (relative) this.offset += 4;
3666 return value;
3667 };
3668
3669 /**
3670 * Reads a 32bit unsigned integer. This is an alias of {@link ByteBuffer#readUint32}.
3671 * @function
3672 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
3673 * @returns {number} Value read
3674 * @expose
3675 */
3676 ByteBufferPrototype.readUInt32 = ByteBufferPrototype.readUint32;
3677
3678 // types/ints/int64
3679
3680 if (Long) {
3681
3682 /**
3683 * Writes a 64bit signed integer.
3684 * @param {number|!Long} value Value to write
3685 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3686 * @returns {!ByteBuffer} this
3687 * @expose
3688 */
3689 ByteBufferPrototype.writeInt64 = function(value, offset) {
3690 var relative = typeof offset === 'undefined';
3691 if (relative) offset = this.offset;
3692 if (!this.noAssert) {
3693 if (typeof value === 'number')
3694 value = Long.fromNumber(value);
3695 else if (typeof value === 'string')
3696 value = Long.fromString(value);
3697 else if (!(value && value instanceof Long))
3698 throw TypeError("Illegal value: "+value+" (not an integer or Long)");
3699 if (typeof offset !== 'number' || offset % 1 !== 0)
3700 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3701 offset >>>= 0;
3702 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3703 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3704 }
3705 if (typeof value === 'number')
3706 value = Long.fromNumber(value);
3707 else if (typeof value === 'string')
3708 value = Long.fromString(value);
3709 offset += 8;
3710 var capacity6 = this.buffer.byteLength;
3711 if (offset > capacity6)
3712 this.resize((capacity6 *= 2) > offset ? capacity6 : offset);
3713 offset -= 8;
3714 var lo = value.low,
3715 hi = value.high;
3716 if (this.littleEndian) {
3717 this.view[offset+3] = (lo >>> 24) & 0xFF;
3718 this.view[offset+2] = (lo >>> 16) & 0xFF;
3719 this.view[offset+1] = (lo >>> 8) & 0xFF;
3720 this.view[offset ] = lo & 0xFF;
3721 offset += 4;
3722 this.view[offset+3] = (hi >>> 24) & 0xFF;
3723 this.view[offset+2] = (hi >>> 16) & 0xFF;
3724 this.view[offset+1] = (hi >>> 8) & 0xFF;
3725 this.view[offset ] = hi & 0xFF;
3726 } else {
3727 this.view[offset ] = (hi >>> 24) & 0xFF;
3728 this.view[offset+1] = (hi >>> 16) & 0xFF;
3729 this.view[offset+2] = (hi >>> 8) & 0xFF;
3730 this.view[offset+3] = hi & 0xFF;
3731 offset += 4;
3732 this.view[offset ] = (lo >>> 24) & 0xFF;
3733 this.view[offset+1] = (lo >>> 16) & 0xFF;
3734 this.view[offset+2] = (lo >>> 8) & 0xFF;
3735 this.view[offset+3] = lo & 0xFF;
3736 }
3737 if (relative) this.offset += 8;
3738 return this;
3739 };
3740
3741 /**
3742 * Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.
3743 * @param {number|!Long} value Value to write
3744 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3745 * @returns {!ByteBuffer} this
3746 * @expose
3747 */
3748 ByteBufferPrototype.writeLong = ByteBufferPrototype.writeInt64;
3749
3750 /**
3751 * Reads a 64bit signed integer.
3752 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3753 * @returns {!Long}
3754 * @expose
3755 */
3756 ByteBufferPrototype.readInt64 = function(offset) {
3757 var relative = typeof offset === 'undefined';
3758 if (relative) offset = this.offset;
3759 if (!this.noAssert) {
3760 if (typeof offset !== 'number' || offset % 1 !== 0)
3761 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3762 offset >>>= 0;
3763 if (offset < 0 || offset + 8 > this.buffer.byteLength)
3764 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
3765 }
3766 var lo = 0,
3767 hi = 0;
3768 if (this.littleEndian) {
3769 lo = this.view[offset+2] << 16;
3770 lo |= this.view[offset+1] << 8;
3771 lo |= this.view[offset ];
3772 lo += this.view[offset+3] << 24 >>> 0;
3773 offset += 4;
3774 hi = this.view[offset+2] << 16;
3775 hi |= this.view[offset+1] << 8;
3776 hi |= this.view[offset ];
3777 hi += this.view[offset+3] << 24 >>> 0;
3778 } else {
3779 hi = this.view[offset+1] << 16;
3780 hi |= this.view[offset+2] << 8;
3781 hi |= this.view[offset+3];
3782 hi += this.view[offset ] << 24 >>> 0;
3783 offset += 4;
3784 lo = this.view[offset+1] << 16;
3785 lo |= this.view[offset+2] << 8;
3786 lo |= this.view[offset+3];
3787 lo += this.view[offset ] << 24 >>> 0;
3788 }
3789 var value = new Long(lo, hi, false);
3790 if (relative) this.offset += 8;
3791 return value;
3792 };
3793
3794 /**
3795 * Reads a 64bit signed integer. This is an alias of {@link ByteBuffer#readInt64}.
3796 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3797 * @returns {!Long}
3798 * @expose
3799 */
3800 ByteBufferPrototype.readLong = ByteBufferPrototype.readInt64;
3801
3802 /**
3803 * Writes a 64bit unsigned integer.
3804 * @param {number|!Long} value Value to write
3805 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3806 * @returns {!ByteBuffer} this
3807 * @expose
3808 */
3809 ByteBufferPrototype.writeUint64 = function(value, offset) {
3810 var relative = typeof offset === 'undefined';
3811 if (relative) offset = this.offset;
3812 if (!this.noAssert) {
3813 if (typeof value === 'number')
3814 value = Long.fromNumber(value);
3815 else if (typeof value === 'string')
3816 value = Long.fromString(value);
3817 else if (!(value && value instanceof Long))
3818 throw TypeError("Illegal value: "+value+" (not an integer or Long)");
3819 if (typeof offset !== 'number' || offset % 1 !== 0)
3820 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3821 offset >>>= 0;
3822 if (offset < 0 || offset + 0 > this.buffer.byteLength)
3823 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
3824 }
3825 if (typeof value === 'number')
3826 value = Long.fromNumber(value);
3827 else if (typeof value === 'string')
3828 value = Long.fromString(value);
3829 offset += 8;
3830 var capacity7 = this.buffer.byteLength;
3831 if (offset > capacity7)
3832 this.resize((capacity7 *= 2) > offset ? capacity7 : offset);
3833 offset -= 8;
3834 var lo = value.low,
3835 hi = value.high;
3836 if (this.littleEndian) {
3837 this.view[offset+3] = (lo >>> 24) & 0xFF;
3838 this.view[offset+2] = (lo >>> 16) & 0xFF;
3839 this.view[offset+1] = (lo >>> 8) & 0xFF;
3840 this.view[offset ] = lo & 0xFF;
3841 offset += 4;
3842 this.view[offset+3] = (hi >>> 24) & 0xFF;
3843 this.view[offset+2] = (hi >>> 16) & 0xFF;
3844 this.view[offset+1] = (hi >>> 8) & 0xFF;
3845 this.view[offset ] = hi & 0xFF;
3846 } else {
3847 this.view[offset ] = (hi >>> 24) & 0xFF;
3848 this.view[offset+1] = (hi >>> 16) & 0xFF;
3849 this.view[offset+2] = (hi >>> 8) & 0xFF;
3850 this.view[offset+3] = hi & 0xFF;
3851 offset += 4;
3852 this.view[offset ] = (lo >>> 24) & 0xFF;
3853 this.view[offset+1] = (lo >>> 16) & 0xFF;
3854 this.view[offset+2] = (lo >>> 8) & 0xFF;
3855 this.view[offset+3] = lo & 0xFF;
3856 }
3857 if (relative) this.offset += 8;
3858 return this;
3859 };
3860
3861 /**
3862 * Writes a 64bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint64}.
3863 * @function
3864 * @param {number|!Long} value Value to write
3865 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3866 * @returns {!ByteBuffer} this
3867 * @expose
3868 */
3869 ByteBufferPrototype.writeUInt64 = ByteBufferPrototype.writeUint64;
3870
3871 /**
3872 * Reads a 64bit unsigned integer.
3873 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3874 * @returns {!Long}
3875 * @expose
3876 */
3877 ByteBufferPrototype.readUint64 = function(offset) {
3878 var relative = typeof offset === 'undefined';
3879 if (relative) offset = this.offset;
3880 if (!this.noAssert) {
3881 if (typeof offset !== 'number' || offset % 1 !== 0)
3882 throw TypeError("Illegal offset: "+offset+" (not an integer)");
3883 offset >>>= 0;
3884 if (offset < 0 || offset + 8 > this.buffer.byteLength)
3885 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
3886 }
3887 var lo = 0,
3888 hi = 0;
3889 if (this.littleEndian) {
3890 lo = this.view[offset+2] << 16;
3891 lo |= this.view[offset+1] << 8;
3892 lo |= this.view[offset ];
3893 lo += this.view[offset+3] << 24 >>> 0;
3894 offset += 4;
3895 hi = this.view[offset+2] << 16;
3896 hi |= this.view[offset+1] << 8;
3897 hi |= this.view[offset ];
3898 hi += this.view[offset+3] << 24 >>> 0;
3899 } else {
3900 hi = this.view[offset+1] << 16;
3901 hi |= this.view[offset+2] << 8;
3902 hi |= this.view[offset+3];
3903 hi += this.view[offset ] << 24 >>> 0;
3904 offset += 4;
3905 lo = this.view[offset+1] << 16;
3906 lo |= this.view[offset+2] << 8;
3907 lo |= this.view[offset+3];
3908 lo += this.view[offset ] << 24 >>> 0;
3909 }
3910 var value = new Long(lo, hi, true);
3911 if (relative) this.offset += 8;
3912 return value;
3913 };
3914
3915 /**
3916 * Reads a 64bit unsigned integer. This is an alias of {@link ByteBuffer#readUint64}.
3917 * @function
3918 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
3919 * @returns {!Long}
3920 * @expose
3921 */
3922 ByteBufferPrototype.readUInt64 = ByteBufferPrototype.readUint64;
3923
3924 } // Long
3925
3926
3927 // types/floats/float32
3928
3929 /*
3930 ieee754 - https://github.com/feross/ieee754
3931
3932 The MIT License (MIT)
3933
3934 Copyright (c) Feross Aboukhadijeh
3935
3936 Permission is hereby granted, free of charge, to any person obtaining a copy
3937 of this software and associated documentation files (the "Software"), to deal
3938 in the Software without restriction, including without limitation the rights
3939 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3940 copies of the Software, and to permit persons to whom the Software is
3941 furnished to do so, subject to the following conditions:
3942
3943 The above copyright notice and this permission notice shall be included in
3944 all copies or substantial portions of the Software.
3945
3946 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3947 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3948 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3949 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3950 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3951 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3952 THE SOFTWARE.
3953 */
3954
3955 /**
3956 * Reads an IEEE754 float from a byte array.
3957 * @param {!Array} buffer
3958 * @param {number} offset
3959 * @param {boolean} isLE
3960 * @param {number} mLen
3961 * @param {number} nBytes
3962 * @returns {number}
3963 * @inner
3964 */
3965 function ieee754_read(buffer, offset, isLE, mLen, nBytes) {
3966 var e, m,
3967 eLen = nBytes * 8 - mLen - 1,
3968 eMax = (1 << eLen) - 1,
3969 eBias = eMax >> 1,
3970 nBits = -7,
3971 i = isLE ? (nBytes - 1) : 0,
3972 d = isLE ? -1 : 1,
3973 s = buffer[offset + i];
3974
3975 i += d;
3976
3977 e = s & ((1 << (-nBits)) - 1);
3978 s >>= (-nBits);
3979 nBits += eLen;
3980 for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
3981
3982 m = e & ((1 << (-nBits)) - 1);
3983 e >>= (-nBits);
3984 nBits += mLen;
3985 for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
3986
3987 if (e === 0) {
3988 e = 1 - eBias;
3989 } else if (e === eMax) {
3990 return m ? NaN : ((s ? -1 : 1) * Infinity);
3991 } else {
3992 m = m + Math.pow(2, mLen);
3993 e = e - eBias;
3994 }
3995 return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
3996 }
3997
3998 /**
3999 * Writes an IEEE754 float to a byte array.
4000 * @param {!Array} buffer
4001 * @param {number} value
4002 * @param {number} offset
4003 * @param {boolean} isLE
4004 * @param {number} mLen
4005 * @param {number} nBytes
4006 * @inner
4007 */
4008 function ieee754_write(buffer, value, offset, isLE, mLen, nBytes) {
4009 var e, m, c,
4010 eLen = nBytes * 8 - mLen - 1,
4011 eMax = (1 << eLen) - 1,
4012 eBias = eMax >> 1,
4013 rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
4014 i = isLE ? 0 : (nBytes - 1),
4015 d = isLE ? 1 : -1,
4016 s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
4017
4018 value = Math.abs(value);
4019
4020 if (isNaN(value) || value === Infinity) {
4021 m = isNaN(value) ? 1 : 0;
4022 e = eMax;
4023 } else {
4024 e = Math.floor(Math.log(value) / Math.LN2);
4025 if (value * (c = Math.pow(2, -e)) < 1) {
4026 e--;
4027 c *= 2;
4028 }
4029 if (e + eBias >= 1) {
4030 value += rt / c;
4031 } else {
4032 value += rt * Math.pow(2, 1 - eBias);
4033 }
4034 if (value * c >= 2) {
4035 e++;
4036 c /= 2;
4037 }
4038
4039 if (e + eBias >= eMax) {
4040 m = 0;
4041 e = eMax;
4042 } else if (e + eBias >= 1) {
4043 m = (value * c - 1) * Math.pow(2, mLen);
4044 e = e + eBias;
4045 } else {
4046 m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
4047 e = 0;
4048 }
4049 }
4050
4051 for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
4052
4053 e = (e << mLen) | m;
4054 eLen += mLen;
4055 for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
4056
4057 buffer[offset + i - d] |= s * 128;
4058 }
4059
4060 /**
4061 * Writes a 32bit float.
4062 * @param {number} value Value to write
4063 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
4064 * @returns {!ByteBuffer} this
4065 * @expose
4066 */
4067 ByteBufferPrototype.writeFloat32 = function(value, offset) {
4068 var relative = typeof offset === 'undefined';
4069 if (relative) offset = this.offset;
4070 if (!this.noAssert) {
4071 if (typeof value !== 'number')
4072 throw TypeError("Illegal value: "+value+" (not a number)");
4073 if (typeof offset !== 'number' || offset % 1 !== 0)
4074 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4075 offset >>>= 0;
4076 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4077 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4078 }
4079 offset += 4;
4080 var capacity8 = this.buffer.byteLength;
4081 if (offset > capacity8)
4082 this.resize((capacity8 *= 2) > offset ? capacity8 : offset);
4083 offset -= 4;
4084 ieee754_write(this.view, value, offset, this.littleEndian, 23, 4);
4085 if (relative) this.offset += 4;
4086 return this;
4087 };
4088
4089 /**
4090 * Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.
4091 * @function
4092 * @param {number} value Value to write
4093 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
4094 * @returns {!ByteBuffer} this
4095 * @expose
4096 */
4097 ByteBufferPrototype.writeFloat = ByteBufferPrototype.writeFloat32;
4098
4099 /**
4100 * Reads a 32bit float.
4101 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
4102 * @returns {number}
4103 * @expose
4104 */
4105 ByteBufferPrototype.readFloat32 = function(offset) {
4106 var relative = typeof offset === 'undefined';
4107 if (relative) offset = this.offset;
4108 if (!this.noAssert) {
4109 if (typeof offset !== 'number' || offset % 1 !== 0)
4110 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4111 offset >>>= 0;
4112 if (offset < 0 || offset + 4 > this.buffer.byteLength)
4113 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
4114 }
4115 var value = ieee754_read(this.view, offset, this.littleEndian, 23, 4);
4116 if (relative) this.offset += 4;
4117 return value;
4118 };
4119
4120 /**
4121 * Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.
4122 * @function
4123 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
4124 * @returns {number}
4125 * @expose
4126 */
4127 ByteBufferPrototype.readFloat = ByteBufferPrototype.readFloat32;
4128
4129 // types/floats/float64
4130
4131 /**
4132 * Writes a 64bit float.
4133 * @param {number} value Value to write
4134 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
4135 * @returns {!ByteBuffer} this
4136 * @expose
4137 */
4138 ByteBufferPrototype.writeFloat64 = function(value, offset) {
4139 var relative = typeof offset === 'undefined';
4140 if (relative) offset = this.offset;
4141 if (!this.noAssert) {
4142 if (typeof value !== 'number')
4143 throw TypeError("Illegal value: "+value+" (not a number)");
4144 if (typeof offset !== 'number' || offset % 1 !== 0)
4145 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4146 offset >>>= 0;
4147 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4148 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4149 }
4150 offset += 8;
4151 var capacity9 = this.buffer.byteLength;
4152 if (offset > capacity9)
4153 this.resize((capacity9 *= 2) > offset ? capacity9 : offset);
4154 offset -= 8;
4155 ieee754_write(this.view, value, offset, this.littleEndian, 52, 8);
4156 if (relative) this.offset += 8;
4157 return this;
4158 };
4159
4160 /**
4161 * Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.
4162 * @function
4163 * @param {number} value Value to write
4164 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
4165 * @returns {!ByteBuffer} this
4166 * @expose
4167 */
4168 ByteBufferPrototype.writeDouble = ByteBufferPrototype.writeFloat64;
4169
4170 /**
4171 * Reads a 64bit float.
4172 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
4173 * @returns {number}
4174 * @expose
4175 */
4176 ByteBufferPrototype.readFloat64 = function(offset) {
4177 var relative = typeof offset === 'undefined';
4178 if (relative) offset = this.offset;
4179 if (!this.noAssert) {
4180 if (typeof offset !== 'number' || offset % 1 !== 0)
4181 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4182 offset >>>= 0;
4183 if (offset < 0 || offset + 8 > this.buffer.byteLength)
4184 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
4185 }
4186 var value = ieee754_read(this.view, offset, this.littleEndian, 52, 8);
4187 if (relative) this.offset += 8;
4188 return value;
4189 };
4190
4191 /**
4192 * Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.
4193 * @function
4194 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
4195 * @returns {number}
4196 * @expose
4197 */
4198 ByteBufferPrototype.readDouble = ByteBufferPrototype.readFloat64;
4199
4200
4201 // types/varints/varint32
4202
4203 /**
4204 * Maximum number of bytes required to store a 32bit base 128 variable-length integer.
4205 * @type {number}
4206 * @const
4207 * @expose
4208 */
4209 ByteBuffer.MAX_VARINT32_BYTES = 5;
4210
4211 /**
4212 * Calculates the actual number of bytes required to store a 32bit base 128 variable-length integer.
4213 * @param {number} value Value to encode
4214 * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT32_BYTES}
4215 * @expose
4216 */
4217 ByteBuffer.calculateVarint32 = function(value) {
4218 // ref: src/google/protobuf/io/coded_stream.cc
4219 value = value >>> 0;
4220 if (value < 1 << 7 ) return 1;
4221 else if (value < 1 << 14) return 2;
4222 else if (value < 1 << 21) return 3;
4223 else if (value < 1 << 28) return 4;
4224 else return 5;
4225 };
4226
4227 /**
4228 * Zigzag encodes a signed 32bit integer so that it can be effectively used with varint encoding.
4229 * @param {number} n Signed 32bit integer
4230 * @returns {number} Unsigned zigzag encoded 32bit integer
4231 * @expose
4232 */
4233 ByteBuffer.zigZagEncode32 = function(n) {
4234 return (((n |= 0) << 1) ^ (n >> 31)) >>> 0; // ref: src/google/protobuf/wire_format_lite.h
4235 };
4236
4237 /**
4238 * Decodes a zigzag encoded signed 32bit integer.
4239 * @param {number} n Unsigned zigzag encoded 32bit integer
4240 * @returns {number} Signed 32bit integer
4241 * @expose
4242 */
4243 ByteBuffer.zigZagDecode32 = function(n) {
4244 return ((n >>> 1) ^ -(n & 1)) | 0; // // ref: src/google/protobuf/wire_format_lite.h
4245 };
4246
4247 /**
4248 * Writes a 32bit base 128 variable-length integer.
4249 * @param {number} value Value to write
4250 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4251 * written if omitted.
4252 * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
4253 * @expose
4254 */
4255 ByteBufferPrototype.writeVarint32 = function(value, offset) {
4256 var relative = typeof offset === 'undefined';
4257 if (relative) offset = this.offset;
4258 if (!this.noAssert) {
4259 if (typeof value !== 'number' || value % 1 !== 0)
4260 throw TypeError("Illegal value: "+value+" (not an integer)");
4261 value |= 0;
4262 if (typeof offset !== 'number' || offset % 1 !== 0)
4263 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4264 offset >>>= 0;
4265 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4266 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4267 }
4268 var size = ByteBuffer.calculateVarint32(value),
4269 b;
4270 offset += size;
4271 var capacity10 = this.buffer.byteLength;
4272 if (offset > capacity10)
4273 this.resize((capacity10 *= 2) > offset ? capacity10 : offset);
4274 offset -= size;
4275 value >>>= 0;
4276 while (value >= 0x80) {
4277 b = (value & 0x7f) | 0x80;
4278 this.view[offset++] = b;
4279 value >>>= 7;
4280 }
4281 this.view[offset++] = value;
4282 if (relative) {
4283 this.offset = offset;
4284 return this;
4285 }
4286 return size;
4287 };
4288
4289 /**
4290 * Writes a zig-zag encoded (signed) 32bit base 128 variable-length integer.
4291 * @param {number} value Value to write
4292 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4293 * written if omitted.
4294 * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
4295 * @expose
4296 */
4297 ByteBufferPrototype.writeVarint32ZigZag = function(value, offset) {
4298 return this.writeVarint32(ByteBuffer.zigZagEncode32(value), offset);
4299 };
4300
4301 /**
4302 * Reads a 32bit base 128 variable-length integer.
4303 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4304 * written if omitted.
4305 * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
4306 * and the actual number of bytes read.
4307 * @throws {Error} If it's not a valid varint. Has a property `truncated = true` if there is not enough data available
4308 * to fully decode the varint.
4309 * @expose
4310 */
4311 ByteBufferPrototype.readVarint32 = function(offset) {
4312 var relative = typeof offset === 'undefined';
4313 if (relative) offset = this.offset;
4314 if (!this.noAssert) {
4315 if (typeof offset !== 'number' || offset % 1 !== 0)
4316 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4317 offset >>>= 0;
4318 if (offset < 0 || offset + 1 > this.buffer.byteLength)
4319 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
4320 }
4321 var c = 0,
4322 value = 0 >>> 0,
4323 b;
4324 do {
4325 if (!this.noAssert && offset > this.limit) {
4326 var err = Error("Truncated");
4327 err['truncated'] = true;
4328 throw err;
4329 }
4330 b = this.view[offset++];
4331 if (c < 5)
4332 value |= (b & 0x7f) << (7*c);
4333 ++c;
4334 } while ((b & 0x80) !== 0);
4335 value |= 0;
4336 if (relative) {
4337 this.offset = offset;
4338 return value;
4339 }
4340 return {
4341 "value": value,
4342 "length": c
4343 };
4344 };
4345
4346 /**
4347 * Reads a zig-zag encoded (signed) 32bit base 128 variable-length integer.
4348 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4349 * written if omitted.
4350 * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
4351 * and the actual number of bytes read.
4352 * @throws {Error} If it's not a valid varint
4353 * @expose
4354 */
4355 ByteBufferPrototype.readVarint32ZigZag = function(offset) {
4356 var val = this.readVarint32(offset);
4357 if (typeof val === 'object')
4358 val["value"] = ByteBuffer.zigZagDecode32(val["value"]);
4359 else
4360 val = ByteBuffer.zigZagDecode32(val);
4361 return val;
4362 };
4363
4364 // types/varints/varint64
4365
4366 if (Long) {
4367
4368 /**
4369 * Maximum number of bytes required to store a 64bit base 128 variable-length integer.
4370 * @type {number}
4371 * @const
4372 * @expose
4373 */
4374 ByteBuffer.MAX_VARINT64_BYTES = 10;
4375
4376 /**
4377 * Calculates the actual number of bytes required to store a 64bit base 128 variable-length integer.
4378 * @param {number|!Long} value Value to encode
4379 * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT64_BYTES}
4380 * @expose
4381 */
4382 ByteBuffer.calculateVarint64 = function(value) {
4383 if (typeof value === 'number')
4384 value = Long.fromNumber(value);
4385 else if (typeof value === 'string')
4386 value = Long.fromString(value);
4387 // ref: src/google/protobuf/io/coded_stream.cc
4388 var part0 = value.toInt() >>> 0,
4389 part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
4390 part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
4391 if (part2 == 0) {
4392 if (part1 == 0) {
4393 if (part0 < 1 << 14)
4394 return part0 < 1 << 7 ? 1 : 2;
4395 else
4396 return part0 < 1 << 21 ? 3 : 4;
4397 } else {
4398 if (part1 < 1 << 14)
4399 return part1 < 1 << 7 ? 5 : 6;
4400 else
4401 return part1 < 1 << 21 ? 7 : 8;
4402 }
4403 } else
4404 return part2 < 1 << 7 ? 9 : 10;
4405 };
4406
4407 /**
4408 * Zigzag encodes a signed 64bit integer so that it can be effectively used with varint encoding.
4409 * @param {number|!Long} value Signed long
4410 * @returns {!Long} Unsigned zigzag encoded long
4411 * @expose
4412 */
4413 ByteBuffer.zigZagEncode64 = function(value) {
4414 if (typeof value === 'number')
4415 value = Long.fromNumber(value, false);
4416 else if (typeof value === 'string')
4417 value = Long.fromString(value, false);
4418 else if (value.unsigned !== false) value = value.toSigned();
4419 // ref: src/google/protobuf/wire_format_lite.h
4420 return value.shiftLeft(1).xor(value.shiftRight(63)).toUnsigned();
4421 };
4422
4423 /**
4424 * Decodes a zigzag encoded signed 64bit integer.
4425 * @param {!Long|number} value Unsigned zigzag encoded long or JavaScript number
4426 * @returns {!Long} Signed long
4427 * @expose
4428 */
4429 ByteBuffer.zigZagDecode64 = function(value) {
4430 if (typeof value === 'number')
4431 value = Long.fromNumber(value, false);
4432 else if (typeof value === 'string')
4433 value = Long.fromString(value, false);
4434 else if (value.unsigned !== false) value = value.toSigned();
4435 // ref: src/google/protobuf/wire_format_lite.h
4436 return value.shiftRightUnsigned(1).xor(value.and(Long.ONE).toSigned().negate()).toSigned();
4437 };
4438
4439 /**
4440 * Writes a 64bit base 128 variable-length integer.
4441 * @param {number|Long} value Value to write
4442 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4443 * written if omitted.
4444 * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
4445 * @expose
4446 */
4447 ByteBufferPrototype.writeVarint64 = function(value, offset) {
4448 var relative = typeof offset === 'undefined';
4449 if (relative) offset = this.offset;
4450 if (!this.noAssert) {
4451 if (typeof value === 'number')
4452 value = Long.fromNumber(value);
4453 else if (typeof value === 'string')
4454 value = Long.fromString(value);
4455 else if (!(value && value instanceof Long))
4456 throw TypeError("Illegal value: "+value+" (not an integer or Long)");
4457 if (typeof offset !== 'number' || offset % 1 !== 0)
4458 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4459 offset >>>= 0;
4460 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4461 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4462 }
4463 if (typeof value === 'number')
4464 value = Long.fromNumber(value, false);
4465 else if (typeof value === 'string')
4466 value = Long.fromString(value, false);
4467 else if (value.unsigned !== false) value = value.toSigned();
4468 var size = ByteBuffer.calculateVarint64(value),
4469 part0 = value.toInt() >>> 0,
4470 part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
4471 part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
4472 offset += size;
4473 var capacity11 = this.buffer.byteLength;
4474 if (offset > capacity11)
4475 this.resize((capacity11 *= 2) > offset ? capacity11 : offset);
4476 offset -= size;
4477 switch (size) {
4478 case 10: this.view[offset+9] = (part2 >>> 7) & 0x01;
4479 case 9 : this.view[offset+8] = size !== 9 ? (part2 ) | 0x80 : (part2 ) & 0x7F;
4480 case 8 : this.view[offset+7] = size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7F;
4481 case 7 : this.view[offset+6] = size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7F;
4482 case 6 : this.view[offset+5] = size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7F;
4483 case 5 : this.view[offset+4] = size !== 5 ? (part1 ) | 0x80 : (part1 ) & 0x7F;
4484 case 4 : this.view[offset+3] = size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7F;
4485 case 3 : this.view[offset+2] = size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7F;
4486 case 2 : this.view[offset+1] = size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7F;
4487 case 1 : this.view[offset ] = size !== 1 ? (part0 ) | 0x80 : (part0 ) & 0x7F;
4488 }
4489 if (relative) {
4490 this.offset += size;
4491 return this;
4492 } else {
4493 return size;
4494 }
4495 };
4496
4497 /**
4498 * Writes a zig-zag encoded 64bit base 128 variable-length integer.
4499 * @param {number|Long} value Value to write
4500 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4501 * written if omitted.
4502 * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
4503 * @expose
4504 */
4505 ByteBufferPrototype.writeVarint64ZigZag = function(value, offset) {
4506 return this.writeVarint64(ByteBuffer.zigZagEncode64(value), offset);
4507 };
4508
4509 /**
4510 * Reads a 64bit base 128 variable-length integer. Requires Long.js.
4511 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4512 * read if omitted.
4513 * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
4514 * the actual number of bytes read.
4515 * @throws {Error} If it's not a valid varint
4516 * @expose
4517 */
4518 ByteBufferPrototype.readVarint64 = function(offset) {
4519 var relative = typeof offset === 'undefined';
4520 if (relative) offset = this.offset;
4521 if (!this.noAssert) {
4522 if (typeof offset !== 'number' || offset % 1 !== 0)
4523 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4524 offset >>>= 0;
4525 if (offset < 0 || offset + 1 > this.buffer.byteLength)
4526 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
4527 }
4528 // ref: src/google/protobuf/io/coded_stream.cc
4529 var start = offset,
4530 part0 = 0,
4531 part1 = 0,
4532 part2 = 0,
4533 b = 0;
4534 b = this.view[offset++]; part0 = (b & 0x7F) ; if ( b & 0x80 ) {
4535 b = this.view[offset++]; part0 |= (b & 0x7F) << 7; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4536 b = this.view[offset++]; part0 |= (b & 0x7F) << 14; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4537 b = this.view[offset++]; part0 |= (b & 0x7F) << 21; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4538 b = this.view[offset++]; part1 = (b & 0x7F) ; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4539 b = this.view[offset++]; part1 |= (b & 0x7F) << 7; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4540 b = this.view[offset++]; part1 |= (b & 0x7F) << 14; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4541 b = this.view[offset++]; part1 |= (b & 0x7F) << 21; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4542 b = this.view[offset++]; part2 = (b & 0x7F) ; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4543 b = this.view[offset++]; part2 |= (b & 0x7F) << 7; if ((b & 0x80) || (this.noAssert && typeof b === 'undefined')) {
4544 throw Error("Buffer overrun"); }}}}}}}}}}
4545 var value = Long.fromBits(part0 | (part1 << 28), (part1 >>> 4) | (part2) << 24, false);
4546 if (relative) {
4547 this.offset = offset;
4548 return value;
4549 } else {
4550 return {
4551 'value': value,
4552 'length': offset-start
4553 };
4554 }
4555 };
4556
4557 /**
4558 * Reads a zig-zag encoded 64bit base 128 variable-length integer. Requires Long.js.
4559 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4560 * read if omitted.
4561 * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
4562 * the actual number of bytes read.
4563 * @throws {Error} If it's not a valid varint
4564 * @expose
4565 */
4566 ByteBufferPrototype.readVarint64ZigZag = function(offset) {
4567 var val = this.readVarint64(offset);
4568 if (val && val['value'] instanceof Long)
4569 val["value"] = ByteBuffer.zigZagDecode64(val["value"]);
4570 else
4571 val = ByteBuffer.zigZagDecode64(val);
4572 return val;
4573 };
4574
4575 } // Long
4576
4577
4578 // types/strings/cstring
4579
4580 /**
4581 * Writes a NULL-terminated UTF8 encoded string. For this to work the specified string must not contain any NULL
4582 * characters itself.
4583 * @param {string} str String to write
4584 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4585 * contained in `str` + 1 if omitted.
4586 * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written
4587 * @expose
4588 */
4589 ByteBufferPrototype.writeCString = function(str, offset) {
4590 var relative = typeof offset === 'undefined';
4591 if (relative) offset = this.offset;
4592 var i,
4593 k = str.length;
4594 if (!this.noAssert) {
4595 if (typeof str !== 'string')
4596 throw TypeError("Illegal str: Not a string");
4597 for (i=0; i<k; ++i) {
4598 if (str.charCodeAt(i) === 0)
4599 throw RangeError("Illegal str: Contains NULL-characters");
4600 }
4601 if (typeof offset !== 'number' || offset % 1 !== 0)
4602 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4603 offset >>>= 0;
4604 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4605 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4606 }
4607 // UTF8 strings do not contain zero bytes in between except for the zero character, so:
4608 k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
4609 offset += k+1;
4610 var capacity12 = this.buffer.byteLength;
4611 if (offset > capacity12)
4612 this.resize((capacity12 *= 2) > offset ? capacity12 : offset);
4613 offset -= k+1;
4614 utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
4615 this.view[offset++] = b;
4616 }.bind(this));
4617 this.view[offset++] = 0;
4618 if (relative) {
4619 this.offset = offset;
4620 return this;
4621 }
4622 return k;
4623 };
4624
4625 /**
4626 * Reads a NULL-terminated UTF8 encoded string. For this to work the string read must not contain any NULL characters
4627 * itself.
4628 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4629 * read if omitted.
4630 * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
4631 * read and the actual number of bytes read.
4632 * @expose
4633 */
4634 ByteBufferPrototype.readCString = function(offset) {
4635 var relative = typeof offset === 'undefined';
4636 if (relative) offset = this.offset;
4637 if (!this.noAssert) {
4638 if (typeof offset !== 'number' || offset % 1 !== 0)
4639 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4640 offset >>>= 0;
4641 if (offset < 0 || offset + 1 > this.buffer.byteLength)
4642 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
4643 }
4644 var start = offset;
4645 // UTF8 strings do not contain zero bytes in between except for the zero character itself, so:
4646 var sd, b = -1;
4647 utfx.decodeUTF8toUTF16(function() {
4648 if (b === 0) return null;
4649 if (offset >= this.limit)
4650 throw RangeError("Illegal range: Truncated data, "+offset+" < "+this.limit);
4651 b = this.view[offset++];
4652 return b === 0 ? null : b;
4653 }.bind(this), sd = stringDestination(), true);
4654 if (relative) {
4655 this.offset = offset;
4656 return sd();
4657 } else {
4658 return {
4659 "string": sd(),
4660 "length": offset - start
4661 };
4662 }
4663 };
4664
4665 // types/strings/istring
4666
4667 /**
4668 * Writes a length as uint32 prefixed UTF8 encoded string.
4669 * @param {string} str String to write
4670 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4671 * written if omitted.
4672 * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
4673 * @expose
4674 * @see ByteBuffer#writeVarint32
4675 */
4676 ByteBufferPrototype.writeIString = function(str, offset) {
4677 var relative = typeof offset === 'undefined';
4678 if (relative) offset = this.offset;
4679 if (!this.noAssert) {
4680 if (typeof str !== 'string')
4681 throw TypeError("Illegal str: Not a string");
4682 if (typeof offset !== 'number' || offset % 1 !== 0)
4683 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4684 offset >>>= 0;
4685 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4686 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4687 }
4688 var start = offset,
4689 k;
4690 k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
4691 offset += 4+k;
4692 var capacity13 = this.buffer.byteLength;
4693 if (offset > capacity13)
4694 this.resize((capacity13 *= 2) > offset ? capacity13 : offset);
4695 offset -= 4+k;
4696 if (this.littleEndian) {
4697 this.view[offset+3] = (k >>> 24) & 0xFF;
4698 this.view[offset+2] = (k >>> 16) & 0xFF;
4699 this.view[offset+1] = (k >>> 8) & 0xFF;
4700 this.view[offset ] = k & 0xFF;
4701 } else {
4702 this.view[offset ] = (k >>> 24) & 0xFF;
4703 this.view[offset+1] = (k >>> 16) & 0xFF;
4704 this.view[offset+2] = (k >>> 8) & 0xFF;
4705 this.view[offset+3] = k & 0xFF;
4706 }
4707 offset += 4;
4708 utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
4709 this.view[offset++] = b;
4710 }.bind(this));
4711 if (offset !== start + 4 + k)
4712 throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+4+k));
4713 if (relative) {
4714 this.offset = offset;
4715 return this;
4716 }
4717 return offset - start;
4718 };
4719
4720 /**
4721 * Reads a length as uint32 prefixed UTF8 encoded string.
4722 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4723 * read if omitted.
4724 * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
4725 * read and the actual number of bytes read.
4726 * @expose
4727 * @see ByteBuffer#readVarint32
4728 */
4729 ByteBufferPrototype.readIString = function(offset) {
4730 var relative = typeof offset === 'undefined';
4731 if (relative) offset = this.offset;
4732 if (!this.noAssert) {
4733 if (typeof offset !== 'number' || offset % 1 !== 0)
4734 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4735 offset >>>= 0;
4736 if (offset < 0 || offset + 4 > this.buffer.byteLength)
4737 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
4738 }
4739 var start = offset;
4740 var len = this.readUint32(offset);
4741 var str = this.readUTF8String(len, ByteBuffer.METRICS_BYTES, offset += 4);
4742 offset += str['length'];
4743 if (relative) {
4744 this.offset = offset;
4745 return str['string'];
4746 } else {
4747 return {
4748 'string': str['string'],
4749 'length': offset - start
4750 };
4751 }
4752 };
4753
4754 // types/strings/utf8string
4755
4756 /**
4757 * Metrics representing number of UTF8 characters. Evaluates to `c`.
4758 * @type {string}
4759 * @const
4760 * @expose
4761 */
4762 ByteBuffer.METRICS_CHARS = 'c';
4763
4764 /**
4765 * Metrics representing number of bytes. Evaluates to `b`.
4766 * @type {string}
4767 * @const
4768 * @expose
4769 */
4770 ByteBuffer.METRICS_BYTES = 'b';
4771
4772 /**
4773 * Writes an UTF8 encoded string.
4774 * @param {string} str String to write
4775 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
4776 * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
4777 * @expose
4778 */
4779 ByteBufferPrototype.writeUTF8String = function(str, offset) {
4780 var relative = typeof offset === 'undefined';
4781 if (relative) offset = this.offset;
4782 if (!this.noAssert) {
4783 if (typeof offset !== 'number' || offset % 1 !== 0)
4784 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4785 offset >>>= 0;
4786 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4787 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4788 }
4789 var k;
4790 var start = offset;
4791 k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
4792 offset += k;
4793 var capacity14 = this.buffer.byteLength;
4794 if (offset > capacity14)
4795 this.resize((capacity14 *= 2) > offset ? capacity14 : offset);
4796 offset -= k;
4797 utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
4798 this.view[offset++] = b;
4799 }.bind(this));
4800 if (relative) {
4801 this.offset = offset;
4802 return this;
4803 }
4804 return offset - start;
4805 };
4806
4807 /**
4808 * Writes an UTF8 encoded string. This is an alias of {@link ByteBuffer#writeUTF8String}.
4809 * @function
4810 * @param {string} str String to write
4811 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
4812 * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
4813 * @expose
4814 */
4815 ByteBufferPrototype.writeString = ByteBufferPrototype.writeUTF8String;
4816
4817 /**
4818 * Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's
4819 * `length` property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
4820 * @param {string} str String to calculate
4821 * @returns {number} Number of UTF8 characters
4822 * @expose
4823 */
4824 ByteBuffer.calculateUTF8Chars = function(str) {
4825 return utfx.calculateUTF16asUTF8(stringSource(str))[0];
4826 };
4827
4828 /**
4829 * Calculates the number of UTF8 bytes of a string.
4830 * @param {string} str String to calculate
4831 * @returns {number} Number of UTF8 bytes
4832 * @expose
4833 */
4834 ByteBuffer.calculateUTF8Bytes = function(str) {
4835 return utfx.calculateUTF16asUTF8(stringSource(str))[1];
4836 };
4837
4838 /**
4839 * Calculates the number of UTF8 bytes of a string. This is an alias of {@link ByteBuffer.calculateUTF8Bytes}.
4840 * @function
4841 * @param {string} str String to calculate
4842 * @returns {number} Number of UTF8 bytes
4843 * @expose
4844 */
4845 ByteBuffer.calculateString = ByteBuffer.calculateUTF8Bytes;
4846
4847 /**
4848 * Reads an UTF8 encoded string.
4849 * @param {number} length Number of characters or bytes to read.
4850 * @param {string=} metrics Metrics specifying what `length` is meant to count. Defaults to
4851 * {@link ByteBuffer.METRICS_CHARS}.
4852 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4853 * read if omitted.
4854 * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
4855 * read and the actual number of bytes read.
4856 * @expose
4857 */
4858 ByteBufferPrototype.readUTF8String = function(length, metrics, offset) {
4859 if (typeof metrics === 'number') {
4860 offset = metrics;
4861 metrics = undefined;
4862 }
4863 var relative = typeof offset === 'undefined';
4864 if (relative) offset = this.offset;
4865 if (typeof metrics === 'undefined') metrics = ByteBuffer.METRICS_CHARS;
4866 if (!this.noAssert) {
4867 if (typeof length !== 'number' || length % 1 !== 0)
4868 throw TypeError("Illegal length: "+length+" (not an integer)");
4869 length |= 0;
4870 if (typeof offset !== 'number' || offset % 1 !== 0)
4871 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4872 offset >>>= 0;
4873 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4874 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4875 }
4876 var i = 0,
4877 start = offset,
4878 sd;
4879 if (metrics === ByteBuffer.METRICS_CHARS) { // The same for node and the browser
4880 sd = stringDestination();
4881 utfx.decodeUTF8(function() {
4882 return i < length && offset < this.limit ? this.view[offset++] : null;
4883 }.bind(this), function(cp) {
4884 ++i; utfx.UTF8toUTF16(cp, sd);
4885 });
4886 if (i !== length)
4887 throw RangeError("Illegal range: Truncated data, "+i+" == "+length);
4888 if (relative) {
4889 this.offset = offset;
4890 return sd();
4891 } else {
4892 return {
4893 "string": sd(),
4894 "length": offset - start
4895 };
4896 }
4897 } else if (metrics === ByteBuffer.METRICS_BYTES) {
4898 if (!this.noAssert) {
4899 if (typeof offset !== 'number' || offset % 1 !== 0)
4900 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4901 offset >>>= 0;
4902 if (offset < 0 || offset + length > this.buffer.byteLength)
4903 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
4904 }
4905 var k = offset + length;
4906 utfx.decodeUTF8toUTF16(function() {
4907 return offset < k ? this.view[offset++] : null;
4908 }.bind(this), sd = stringDestination(), this.noAssert);
4909 if (offset !== k)
4910 throw RangeError("Illegal range: Truncated data, "+offset+" == "+k);
4911 if (relative) {
4912 this.offset = offset;
4913 return sd();
4914 } else {
4915 return {
4916 'string': sd(),
4917 'length': offset - start
4918 };
4919 }
4920 } else
4921 throw TypeError("Unsupported metrics: "+metrics);
4922 };
4923
4924 /**
4925 * Reads an UTF8 encoded string. This is an alias of {@link ByteBuffer#readUTF8String}.
4926 * @function
4927 * @param {number} length Number of characters or bytes to read
4928 * @param {number=} metrics Metrics specifying what `n` is meant to count. Defaults to
4929 * {@link ByteBuffer.METRICS_CHARS}.
4930 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4931 * read if omitted.
4932 * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
4933 * read and the actual number of bytes read.
4934 * @expose
4935 */
4936 ByteBufferPrototype.readString = ByteBufferPrototype.readUTF8String;
4937
4938 // types/strings/vstring
4939
4940 /**
4941 * Writes a length as varint32 prefixed UTF8 encoded string.
4942 * @param {string} str String to write
4943 * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4944 * written if omitted.
4945 * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
4946 * @expose
4947 * @see ByteBuffer#writeVarint32
4948 */
4949 ByteBufferPrototype.writeVString = function(str, offset) {
4950 var relative = typeof offset === 'undefined';
4951 if (relative) offset = this.offset;
4952 if (!this.noAssert) {
4953 if (typeof str !== 'string')
4954 throw TypeError("Illegal str: Not a string");
4955 if (typeof offset !== 'number' || offset % 1 !== 0)
4956 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4957 offset >>>= 0;
4958 if (offset < 0 || offset + 0 > this.buffer.byteLength)
4959 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
4960 }
4961 var start = offset,
4962 k, l;
4963 k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
4964 l = ByteBuffer.calculateVarint32(k);
4965 offset += l+k;
4966 var capacity15 = this.buffer.byteLength;
4967 if (offset > capacity15)
4968 this.resize((capacity15 *= 2) > offset ? capacity15 : offset);
4969 offset -= l+k;
4970 offset += this.writeVarint32(k, offset);
4971 utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
4972 this.view[offset++] = b;
4973 }.bind(this));
4974 if (offset !== start+k+l)
4975 throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+k+l));
4976 if (relative) {
4977 this.offset = offset;
4978 return this;
4979 }
4980 return offset - start;
4981 };
4982
4983 /**
4984 * Reads a length as varint32 prefixed UTF8 encoded string.
4985 * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
4986 * read if omitted.
4987 * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
4988 * read and the actual number of bytes read.
4989 * @expose
4990 * @see ByteBuffer#readVarint32
4991 */
4992 ByteBufferPrototype.readVString = function(offset) {
4993 var relative = typeof offset === 'undefined';
4994 if (relative) offset = this.offset;
4995 if (!this.noAssert) {
4996 if (typeof offset !== 'number' || offset % 1 !== 0)
4997 throw TypeError("Illegal offset: "+offset+" (not an integer)");
4998 offset >>>= 0;
4999 if (offset < 0 || offset + 1 > this.buffer.byteLength)
5000 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
5001 }
5002 var start = offset;
5003 var len = this.readVarint32(offset);
5004 var str = this.readUTF8String(len['value'], ByteBuffer.METRICS_BYTES, offset += len['length']);
5005 offset += str['length'];
5006 if (relative) {
5007 this.offset = offset;
5008 return str['string'];
5009 } else {
5010 return {
5011 'string': str['string'],
5012 'length': offset - start
5013 };
5014 }
5015 };
5016
5017
5018 /**
5019 * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
5020 * data's length.
5021 * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to append. If `source` is a ByteBuffer, its offsets
5022 * will be modified according to the performed read operation.
5023 * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
5024 * @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
5025 * written if omitted.
5026 * @returns {!ByteBuffer} this
5027 * @expose
5028 * @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
5029 * @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
5030 */
5031 ByteBufferPrototype.append = function(source, encoding, offset) {
5032 if (typeof encoding === 'number' || typeof encoding !== 'string') {
5033 offset = encoding;
5034 encoding = undefined;
5035 }
5036 var relative = typeof offset === 'undefined';
5037 if (relative) offset = this.offset;
5038 if (!this.noAssert) {
5039 if (typeof offset !== 'number' || offset % 1 !== 0)
5040 throw TypeError("Illegal offset: "+offset+" (not an integer)");
5041 offset >>>= 0;
5042 if (offset < 0 || offset + 0 > this.buffer.byteLength)
5043 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
5044 }
5045 if (!(source instanceof ByteBuffer))
5046 source = ByteBuffer.wrap(source, encoding);
5047 var length = source.limit - source.offset;
5048 if (length <= 0) return this; // Nothing to append
5049 offset += length;
5050 var capacity16 = this.buffer.byteLength;
5051 if (offset > capacity16)
5052 this.resize((capacity16 *= 2) > offset ? capacity16 : offset);
5053 offset -= length;
5054 this.view.set(source.view.subarray(source.offset, source.limit), offset);
5055 source.offset += length;
5056 if (relative) this.offset += length;
5057 return this;
5058 };
5059
5060 /**
5061 * Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents at and after the
5062 specified offset up to the length of this ByteBuffer's data.
5063 * @param {!ByteBuffer} target Target ByteBuffer
5064 * @param {number=} offset Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
5065 * read if omitted.
5066 * @returns {!ByteBuffer} this
5067 * @expose
5068 * @see ByteBuffer#append
5069 */
5070 ByteBufferPrototype.appendTo = function(target, offset) {
5071 target.append(this, offset);
5072 return this;
5073 };
5074
5075 /**
5076 * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
5077 * disable them if your code already makes sure that everything is valid.
5078 * @param {boolean} assert `true` to enable assertions, otherwise `false`
5079 * @returns {!ByteBuffer} this
5080 * @expose
5081 */
5082 ByteBufferPrototype.assert = function(assert) {
5083 this.noAssert = !assert;
5084 return this;
5085 };
5086
5087 /**
5088 * Gets the capacity of this ByteBuffer's backing buffer.
5089 * @returns {number} Capacity of the backing buffer
5090 * @expose
5091 */
5092 ByteBufferPrototype.capacity = function() {
5093 return this.buffer.byteLength;
5094 };
5095 /**
5096 * Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
5097 * backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
5098 * @returns {!ByteBuffer} this
5099 * @expose
5100 */
5101 ByteBufferPrototype.clear = function() {
5102 this.offset = 0;
5103 this.limit = this.buffer.byteLength;
5104 this.markedOffset = -1;
5105 return this;
5106 };
5107
5108 /**
5109 * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
5110 * {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
5111 * @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
5112 * @returns {!ByteBuffer} Cloned instance
5113 * @expose
5114 */
5115 ByteBufferPrototype.clone = function(copy) {
5116 var bb = new ByteBuffer(0, this.littleEndian, this.noAssert);
5117 if (copy) {
5118 bb.buffer = new ArrayBuffer(this.buffer.byteLength);
5119 bb.view = new Uint8Array(bb.buffer);
5120 } else {
5121 bb.buffer = this.buffer;
5122 bb.view = this.view;
5123 }
5124 bb.offset = this.offset;
5125 bb.markedOffset = this.markedOffset;
5126 bb.limit = this.limit;
5127 return bb;
5128 };
5129
5130 /**
5131 * Compacts this ByteBuffer to be backed by a {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes
5132 * between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set `offset = 0` and `limit = capacity` and
5133 * adapt {@link ByteBuffer#markedOffset} to the same relative position if set.
5134 * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
5135 * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
5136 * @returns {!ByteBuffer} this
5137 * @expose
5138 */
5139 ByteBufferPrototype.compact = function(begin, end) {
5140 if (typeof begin === 'undefined') begin = this.offset;
5141 if (typeof end === 'undefined') end = this.limit;
5142 if (!this.noAssert) {
5143 if (typeof begin !== 'number' || begin % 1 !== 0)
5144 throw TypeError("Illegal begin: Not an integer");
5145 begin >>>= 0;
5146 if (typeof end !== 'number' || end % 1 !== 0)
5147 throw TypeError("Illegal end: Not an integer");
5148 end >>>= 0;
5149 if (begin < 0 || begin > end || end > this.buffer.byteLength)
5150 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
5151 }
5152 if (begin === 0 && end === this.buffer.byteLength)
5153 return this; // Already compacted
5154 var len = end - begin;
5155 if (len === 0) {
5156 this.buffer = EMPTY_BUFFER;
5157 this.view = null;
5158 if (this.markedOffset >= 0) this.markedOffset -= begin;
5159 this.offset = 0;
5160 this.limit = 0;
5161 return this;
5162 }
5163 var buffer = new ArrayBuffer(len);
5164 var view = new Uint8Array(buffer);
5165 view.set(this.view.subarray(begin, end));
5166 this.buffer = buffer;
5167 this.view = view;
5168 if (this.markedOffset >= 0) this.markedOffset -= begin;
5169 this.offset = 0;
5170 this.limit = len;
5171 return this;
5172 };
5173
5174 /**
5175 * Creates a copy of this ByteBuffer's contents. Contents are the bytes between {@link ByteBuffer#offset} and
5176 * {@link ByteBuffer#limit}.
5177 * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
5178 * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
5179 * @returns {!ByteBuffer} Copy
5180 * @expose
5181 */
5182 ByteBufferPrototype.copy = function(begin, end) {
5183 if (typeof begin === 'undefined') begin = this.offset;
5184 if (typeof end === 'undefined') end = this.limit;
5185 if (!this.noAssert) {
5186 if (typeof begin !== 'number' || begin % 1 !== 0)
5187 throw TypeError("Illegal begin: Not an integer");
5188 begin >>>= 0;
5189 if (typeof end !== 'number' || end % 1 !== 0)
5190 throw TypeError("Illegal end: Not an integer");
5191 end >>>= 0;
5192 if (begin < 0 || begin > end || end > this.buffer.byteLength)
5193 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
5194 }
5195 if (begin === end)
5196 return new ByteBuffer(0, this.littleEndian, this.noAssert);
5197 var capacity = end - begin,
5198 bb = new ByteBuffer(capacity, this.littleEndian, this.noAssert);
5199 bb.offset = 0;
5200 bb.limit = capacity;
5201 if (bb.markedOffset >= 0) bb.markedOffset -= begin;
5202 this.copyTo(bb, 0, begin, end);
5203 return bb;
5204 };
5205
5206 /**
5207 * Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between {@link ByteBuffer#offset} and
5208 * {@link ByteBuffer#limit}.
5209 * @param {!ByteBuffer} target Target ByteBuffer
5210 * @param {number=} targetOffset Offset to copy to. Will use and increase the target's {@link ByteBuffer#offset}
5211 * by the number of bytes copied if omitted.
5212 * @param {number=} sourceOffset Offset to start copying from. Will use and increase {@link ByteBuffer#offset} by the
5213 * number of bytes copied if omitted.
5214 * @param {number=} sourceLimit Offset to end copying from, defaults to {@link ByteBuffer#limit}
5215 * @returns {!ByteBuffer} this
5216 * @expose
5217 */
5218 ByteBufferPrototype.copyTo = function(target, targetOffset, sourceOffset, sourceLimit) {
5219 var relative,
5220 targetRelative;
5221 if (!this.noAssert) {
5222 if (!ByteBuffer.isByteBuffer(target))
5223 throw TypeError("Illegal target: Not a ByteBuffer");
5224 }
5225 targetOffset = (targetRelative = typeof targetOffset === 'undefined') ? target.offset : targetOffset | 0;
5226 sourceOffset = (relative = typeof sourceOffset === 'undefined') ? this.offset : sourceOffset | 0;
5227 sourceLimit = typeof sourceLimit === 'undefined' ? this.limit : sourceLimit | 0;
5228
5229 if (targetOffset < 0 || targetOffset > target.buffer.byteLength)
5230 throw RangeError("Illegal target range: 0 <= "+targetOffset+" <= "+target.buffer.byteLength);
5231 if (sourceOffset < 0 || sourceLimit > this.buffer.byteLength)
5232 throw RangeError("Illegal source range: 0 <= "+sourceOffset+" <= "+this.buffer.byteLength);
5233
5234 var len = sourceLimit - sourceOffset;
5235 if (len === 0)
5236 return target; // Nothing to copy
5237
5238 target.ensureCapacity(targetOffset + len);
5239
5240 target.view.set(this.view.subarray(sourceOffset, sourceLimit), targetOffset);
5241
5242 if (relative) this.offset += len;
5243 if (targetRelative) target.offset += len;
5244
5245 return this;
5246 };
5247
5248 /**
5249 * Makes sure that this ByteBuffer is backed by a {@link ByteBuffer#buffer} of at least the specified capacity. If the
5250 * current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity,
5251 * the required capacity will be used instead.
5252 * @param {number} capacity Required capacity
5253 * @returns {!ByteBuffer} this
5254 * @expose
5255 */
5256 ByteBufferPrototype.ensureCapacity = function(capacity) {
5257 var current = this.buffer.byteLength;
5258 if (current < capacity)
5259 return this.resize((current *= 2) > capacity ? current : capacity);
5260 return this;
5261 };
5262
5263 /**
5264 * Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between
5265 * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
5266 * @param {number|string} value Byte value to fill with. If given as a string, the first character is used.
5267 * @param {number=} begin Begin offset. Will use and increase {@link ByteBuffer#offset} by the number of bytes
5268 * written if omitted. defaults to {@link ByteBuffer#offset}.
5269 * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
5270 * @returns {!ByteBuffer} this
5271 * @expose
5272 * @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
5273 */
5274 ByteBufferPrototype.fill = function(value, begin, end) {
5275 var relative = typeof begin === 'undefined';
5276 if (relative) begin = this.offset;
5277 if (typeof value === 'string' && value.length > 0)
5278 value = value.charCodeAt(0);
5279 if (typeof begin === 'undefined') begin = this.offset;
5280 if (typeof end === 'undefined') end = this.limit;
5281 if (!this.noAssert) {
5282 if (typeof value !== 'number' || value % 1 !== 0)
5283 throw TypeError("Illegal value: "+value+" (not an integer)");
5284 value |= 0;
5285 if (typeof begin !== 'number' || begin % 1 !== 0)
5286 throw TypeError("Illegal begin: Not an integer");
5287 begin >>>= 0;
5288 if (typeof end !== 'number' || end % 1 !== 0)
5289 throw TypeError("Illegal end: Not an integer");
5290 end >>>= 0;
5291 if (begin < 0 || begin > end || end > this.buffer.byteLength)
5292 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
5293 }
5294 if (begin >= end)
5295 return this; // Nothing to fill
5296 while (begin < end) this.view[begin++] = value;
5297 if (relative) this.offset = begin;
5298 return this;
5299 };
5300
5301 /**
5302 * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets `limit = offset` and
5303 * `offset = 0`. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
5304 * @returns {!ByteBuffer} this
5305 * @expose
5306 */
5307 ByteBufferPrototype.flip = function() {
5308 this.limit = this.offset;
5309 this.offset = 0;
5310 return this;
5311 };
5312 /**
5313 * Marks an offset on this ByteBuffer to be used later.
5314 * @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
5315 * @returns {!ByteBuffer} this
5316 * @throws {TypeError} If `offset` is not a valid number
5317 * @throws {RangeError} If `offset` is out of bounds
5318 * @see ByteBuffer#reset
5319 * @expose
5320 */
5321 ByteBufferPrototype.mark = function(offset) {
5322 offset = typeof offset === 'undefined' ? this.offset : offset;
5323 if (!this.noAssert) {
5324 if (typeof offset !== 'number' || offset % 1 !== 0)
5325 throw TypeError("Illegal offset: "+offset+" (not an integer)");
5326 offset >>>= 0;
5327 if (offset < 0 || offset + 0 > this.buffer.byteLength)
5328 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
5329 }
5330 this.markedOffset = offset;
5331 return this;
5332 };
5333 /**
5334 * Sets the byte order.
5335 * @param {boolean} littleEndian `true` for little endian byte order, `false` for big endian
5336 * @returns {!ByteBuffer} this
5337 * @expose
5338 */
5339 ByteBufferPrototype.order = function(littleEndian) {
5340 if (!this.noAssert) {
5341 if (typeof littleEndian !== 'boolean')
5342 throw TypeError("Illegal littleEndian: Not a boolean");
5343 }
5344 this.littleEndian = !!littleEndian;
5345 return this;
5346 };
5347
5348 /**
5349 * Switches (to) little endian byte order.
5350 * @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
5351 * @returns {!ByteBuffer} this
5352 * @expose
5353 */
5354 ByteBufferPrototype.LE = function(littleEndian) {
5355 this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
5356 return this;
5357 };
5358
5359 /**
5360 * Switches (to) big endian byte order.
5361 * @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
5362 * @returns {!ByteBuffer} this
5363 * @expose
5364 */
5365 ByteBufferPrototype.BE = function(bigEndian) {
5366 this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
5367 return this;
5368 };
5369 /**
5370 * Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the
5371 * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
5372 * will be resized and its contents moved accordingly.
5373 * @param {!ByteBuffer|string|!ArrayBuffer} source Data to prepend. If `source` is a ByteBuffer, its offset will be
5374 * modified according to the performed read operation.
5375 * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
5376 * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
5377 * prepended if omitted.
5378 * @returns {!ByteBuffer} this
5379 * @expose
5380 * @example A relative `00<01 02 03>.prepend(<04 05>)` results in `<04 05 01 02 03>, 04 05|`
5381 * @example An absolute `00<01 02 03>.prepend(<04 05>, 2)` results in `04<05 02 03>, 04 05|`
5382 */
5383 ByteBufferPrototype.prepend = function(source, encoding, offset) {
5384 if (typeof encoding === 'number' || typeof encoding !== 'string') {
5385 offset = encoding;
5386 encoding = undefined;
5387 }
5388 var relative = typeof offset === 'undefined';
5389 if (relative) offset = this.offset;
5390 if (!this.noAssert) {
5391 if (typeof offset !== 'number' || offset % 1 !== 0)
5392 throw TypeError("Illegal offset: "+offset+" (not an integer)");
5393 offset >>>= 0;
5394 if (offset < 0 || offset + 0 > this.buffer.byteLength)
5395 throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
5396 }
5397 if (!(source instanceof ByteBuffer))
5398 source = ByteBuffer.wrap(source, encoding);
5399 var len = source.limit - source.offset;
5400 if (len <= 0) return this; // Nothing to prepend
5401 var diff = len - offset;
5402 if (diff > 0) { // Not enough space before offset, so resize + move
5403 var buffer = new ArrayBuffer(this.buffer.byteLength + diff);
5404 var view = new Uint8Array(buffer);
5405 view.set(this.view.subarray(offset, this.buffer.byteLength), len);
5406 this.buffer = buffer;
5407 this.view = view;
5408 this.offset += diff;
5409 if (this.markedOffset >= 0) this.markedOffset += diff;
5410 this.limit += diff;
5411 offset += diff;
5412 } else {
5413 var arrayView = new Uint8Array(this.buffer);
5414 }
5415 this.view.set(source.view.subarray(source.offset, source.limit), offset - len);
5416
5417 source.offset = source.limit;
5418 if (relative)
5419 this.offset -= len;
5420 return this;
5421 };
5422
5423 /**
5424 * Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the
5425 * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
5426 * will be resized and its contents moved accordingly.
5427 * @param {!ByteBuffer} target Target ByteBuffer
5428 * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
5429 * prepended if omitted.
5430 * @returns {!ByteBuffer} this
5431 * @expose
5432 * @see ByteBuffer#prepend
5433 */
5434 ByteBufferPrototype.prependTo = function(target, offset) {
5435 target.prepend(this, offset);
5436 return this;
5437 };
5438 /**
5439 * Prints debug information about this ByteBuffer's contents.
5440 * @param {function(string)=} out Output function to call, defaults to console.log
5441 * @expose
5442 */
5443 ByteBufferPrototype.printDebug = function(out) {
5444 if (typeof out !== 'function') out = console.log.bind(console);
5445 out(
5446 this.toString()+"\n"+
5447 "-------------------------------------------------------------------\n"+
5448 this.toDebug(/* columns */ true)
5449 );
5450 };
5451
5452 /**
5453 * Gets the number of remaining readable bytes. Contents are the bytes between {@link ByteBuffer#offset} and
5454 * {@link ByteBuffer#limit}, so this returns `limit - offset`.
5455 * @returns {number} Remaining readable bytes. May be negative if `offset > limit`.
5456 * @expose
5457 */
5458 ByteBufferPrototype.remaining = function() {
5459 return this.limit - this.offset;
5460 };
5461 /**
5462 * Resets this ByteBuffer's {@link ByteBuffer#offset}. If an offset has been marked through {@link ByteBuffer#mark}
5463 * before, `offset` will be set to {@link ByteBuffer#markedOffset}, which will then be discarded. If no offset has been
5464 * marked, sets `offset = 0`.
5465 * @returns {!ByteBuffer} this
5466 * @see ByteBuffer#mark
5467 * @expose
5468 */
5469 ByteBufferPrototype.reset = function() {
5470 if (this.markedOffset >= 0) {
5471 this.offset = this.markedOffset;
5472 this.markedOffset = -1;
5473 } else {
5474 this.offset = 0;
5475 }
5476 return this;
5477 };
5478 /**
5479 * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that
5480 * large or larger.
5481 * @param {number} capacity Capacity required
5482 * @returns {!ByteBuffer} this
5483 * @throws {TypeError} If `capacity` is not a number
5484 * @throws {RangeError} If `capacity < 0`
5485 * @expose
5486 */
5487 ByteBufferPrototype.resize = function(capacity) {
5488 if (!this.noAssert) {
5489 if (typeof capacity !== 'number' || capacity % 1 !== 0)
5490 throw TypeError("Illegal capacity: "+capacity+" (not an integer)");
5491 capacity |= 0;
5492 if (capacity < 0)
5493 throw RangeError("Illegal capacity: 0 <= "+capacity);
5494 }
5495 if (this.buffer.byteLength < capacity) {
5496 var buffer = new ArrayBuffer(capacity);
5497 var view = new Uint8Array(buffer);
5498 view.set(this.view);
5499 this.buffer = buffer;
5500 this.view = view;
5501 }
5502 return this;
5503 };
5504 /**
5505 * Reverses this ByteBuffer's contents.
5506 * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
5507 * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
5508 * @returns {!ByteBuffer} this
5509 * @expose
5510 */
5511 ByteBufferPrototype.reverse = function(begin, end) {
5512 if (typeof begin === 'undefined') begin = this.offset;
5513 if (typeof end === 'undefined') end = this.limit;
5514 if (!this.noAssert) {
5515 if (typeof begin !== 'number' || begin % 1 !== 0)
5516 throw TypeError("Illegal begin: Not an integer");
5517 begin >>>= 0;
5518 if (typeof end !== 'number' || end % 1 !== 0)
5519 throw TypeError("Illegal end: Not an integer");
5520 end >>>= 0;
5521 if (begin < 0 || begin > end || end > this.buffer.byteLength)
5522 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
5523 }
5524 if (begin === end)
5525 return this; // Nothing to reverse
5526 Array.prototype.reverse.call(this.view.subarray(begin, end));
5527 return this;
5528 };
5529 /**
5530 * Skips the next `length` bytes. This will just advance
5531 * @param {number} length Number of bytes to skip. May also be negative to move the offset back.
5532 * @returns {!ByteBuffer} this
5533 * @expose
5534 */
5535 ByteBufferPrototype.skip = function(length) {
5536 if (!this.noAssert) {
5537 if (typeof length !== 'number' || length % 1 !== 0)
5538 throw TypeError("Illegal length: "+length+" (not an integer)");
5539 length |= 0;
5540 }
5541 var offset = this.offset + length;
5542 if (!this.noAssert) {
5543 if (offset < 0 || offset > this.buffer.byteLength)
5544 throw RangeError("Illegal length: 0 <= "+this.offset+" + "+length+" <= "+this.buffer.byteLength);
5545 }
5546 this.offset = offset;
5547 return this;
5548 };
5549
5550 /**
5551 * Slices this ByteBuffer by creating a cloned instance with `offset = begin` and `limit = end`.
5552 * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
5553 * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
5554 * @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same {@link ByteBuffer#buffer}
5555 * @expose
5556 */
5557 ByteBufferPrototype.slice = function(begin, end) {
5558 if (typeof begin === 'undefined') begin = this.offset;
5559 if (typeof end === 'undefined') end = this.limit;
5560 if (!this.noAssert) {
5561 if (typeof begin !== 'number' || begin % 1 !== 0)
5562 throw TypeError("Illegal begin: Not an integer");
5563 begin >>>= 0;
5564 if (typeof end !== 'number' || end % 1 !== 0)
5565 throw TypeError("Illegal end: Not an integer");
5566 end >>>= 0;
5567 if (begin < 0 || begin > end || end > this.buffer.byteLength)
5568 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
5569 }
5570 var bb = this.clone();
5571 bb.offset = begin;
5572 bb.limit = end;
5573 return bb;
5574 };
5575 /**
5576 * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between
5577 * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
5578 * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory if
5579 * possible. Defaults to `false`
5580 * @returns {!ArrayBuffer} Contents as an ArrayBuffer
5581 * @expose
5582 */
5583 ByteBufferPrototype.toBuffer = function(forceCopy) {
5584 var offset = this.offset,
5585 limit = this.limit;
5586 if (!this.noAssert) {
5587 if (typeof offset !== 'number' || offset % 1 !== 0)
5588 throw TypeError("Illegal offset: Not an integer");
5589 offset >>>= 0;
5590 if (typeof limit !== 'number' || limit % 1 !== 0)
5591 throw TypeError("Illegal limit: Not an integer");
5592 limit >>>= 0;
5593 if (offset < 0 || offset > limit || limit > this.buffer.byteLength)
5594 throw RangeError("Illegal range: 0 <= "+offset+" <= "+limit+" <= "+this.buffer.byteLength);
5595 }
5596 // NOTE: It's not possible to have another ArrayBuffer reference the same memory as the backing buffer. This is
5597 // possible with Uint8Array#subarray only, but we have to return an ArrayBuffer by contract. So:
5598 if (!forceCopy && offset === 0 && limit === this.buffer.byteLength)
5599 return this.buffer;
5600 if (offset === limit)
5601 return EMPTY_BUFFER;
5602 var buffer = new ArrayBuffer(limit - offset);
5603 new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(offset, limit), 0);
5604 return buffer;
5605 };
5606
5607 /**
5608 * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between
5609 * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. This is an alias of {@link ByteBuffer#toBuffer}.
5610 * @function
5611 * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory.
5612 * Defaults to `false`
5613 * @returns {!ArrayBuffer} Contents as an ArrayBuffer
5614 * @expose
5615 */
5616 ByteBufferPrototype.toArrayBuffer = ByteBufferPrototype.toBuffer;
5617
5618 /**
5619 * Converts the ByteBuffer's contents to a string.
5620 * @param {string=} encoding Output encoding. Returns an informative string representation if omitted but also allows
5621 * direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with
5622 * highlighted offsets.
5623 * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}
5624 * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
5625 * @returns {string} String representation
5626 * @throws {Error} If `encoding` is invalid
5627 * @expose
5628 */
5629 ByteBufferPrototype.toString = function(encoding, begin, end) {
5630 if (typeof encoding === 'undefined')
5631 return "ByteBufferAB(offset="+this.offset+",markedOffset="+this.markedOffset+",limit="+this.limit+",capacity="+this.capacity()+")";
5632 if (typeof encoding === 'number')
5633 encoding = "utf8",
5634 begin = encoding,
5635 end = begin;
5636 switch (encoding) {
5637 case "utf8":
5638 return this.toUTF8(begin, end);
5639 case "base64":
5640 return this.toBase64(begin, end);
5641 case "hex":
5642 return this.toHex(begin, end);
5643 case "binary":
5644 return this.toBinary(begin, end);
5645 case "debug":
5646 return this.toDebug();
5647 case "columns":
5648 return this.toColumns();
5649 default:
5650 throw Error("Unsupported encoding: "+encoding);
5651 }
5652 };
5653
5654 // lxiv-embeddable
5655
5656 /**
5657 * lxiv-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
5658 * Released under the Apache License, Version 2.0
5659 * see: https://github.com/dcodeIO/lxiv for details
5660 */
5661 var lxiv = function() {
5662
5663 /**
5664 * lxiv namespace.
5665 * @type {!Object.<string,*>}
5666 * @exports lxiv
5667 */
5668 var lxiv = {};
5669
5670 /**
5671 * Character codes for output.
5672 * @type {!Array.<number>}
5673 * @inner
5674 */
5675 var aout = [
5676 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
5677 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102,
5678 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
5679 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47
5680 ];
5681
5682 /**
5683 * Character codes for input.
5684 * @type {!Array.<number>}
5685 * @inner
5686 */
5687 var ain = [];
5688 for (var i=0, k=aout.length; i<k; ++i)
5689 ain[aout[i]] = i;
5690
5691 /**
5692 * Encodes bytes to base64 char codes.
5693 * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if
5694 * there are no more bytes left.
5695 * @param {!function(number)} dst Characters destination as a function successively called with each encoded char
5696 * code.
5697 */
5698 lxiv.encode = function(src, dst) {
5699 var b, t;
5700 while ((b = src()) !== null) {
5701 dst(aout[(b>>2)&0x3f]);
5702 t = (b&0x3)<<4;
5703 if ((b = src()) !== null) {
5704 t |= (b>>4)&0xf;
5705 dst(aout[(t|((b>>4)&0xf))&0x3f]);
5706 t = (b&0xf)<<2;
5707 if ((b = src()) !== null)
5708 dst(aout[(t|((b>>6)&0x3))&0x3f]),
5709 dst(aout[b&0x3f]);
5710 else
5711 dst(aout[t&0x3f]),
5712 dst(61);
5713 } else
5714 dst(aout[t&0x3f]),
5715 dst(61),
5716 dst(61);
5717 }
5718 };
5719
5720 /**
5721 * Decodes base64 char codes to bytes.
5722 * @param {!function():number|null} src Characters source as a function returning the next char code respectively
5723 * `null` if there are no more characters left.
5724 * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
5725 * @throws {Error} If a character code is invalid
5726 */
5727 lxiv.decode = function(src, dst) {
5728 var c, t1, t2;
5729 function fail(c) {
5730 throw Error("Illegal character code: "+c);
5731 }
5732 while ((c = src()) !== null) {
5733 t1 = ain[c];
5734 if (typeof t1 === 'undefined') fail(c);
5735 if ((c = src()) !== null) {
5736 t2 = ain[c];
5737 if (typeof t2 === 'undefined') fail(c);
5738 dst((t1<<2)>>>0|(t2&0x30)>>4);
5739 if ((c = src()) !== null) {
5740 t1 = ain[c];
5741 if (typeof t1 === 'undefined')
5742 if (c === 61) break; else fail(c);
5743 dst(((t2&0xf)<<4)>>>0|(t1&0x3c)>>2);
5744 if ((c = src()) !== null) {
5745 t2 = ain[c];
5746 if (typeof t2 === 'undefined')
5747 if (c === 61) break; else fail(c);
5748 dst(((t1&0x3)<<6)>>>0|t2);
5749 }
5750 }
5751 }
5752 }
5753 };
5754
5755 /**
5756 * Tests if a string is valid base64.
5757 * @param {string} str String to test
5758 * @returns {boolean} `true` if valid, otherwise `false`
5759 */
5760 lxiv.test = function(str) {
5761 return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(str);
5762 };
5763
5764 return lxiv;
5765 }();
5766
5767 // encodings/base64
5768
5769 /**
5770 * Encodes this ByteBuffer's contents to a base64 encoded string.
5771 * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}.
5772 * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}.
5773 * @returns {string} Base64 encoded string
5774 * @throws {RangeError} If `begin` or `end` is out of bounds
5775 * @expose
5776 */
5777 ByteBufferPrototype.toBase64 = function(begin, end) {
5778 if (typeof begin === 'undefined')
5779 begin = this.offset;
5780 if (typeof end === 'undefined')
5781 end = this.limit;
5782 begin = begin | 0; end = end | 0;
5783 if (begin < 0 || end > this.capacity || begin > end)
5784 throw RangeError("begin, end");
5785 var sd; lxiv.encode(function() {
5786 return begin < end ? this.view[begin++] : null;
5787 }.bind(this), sd = stringDestination());
5788 return sd();
5789 };
5790
5791 /**
5792 * Decodes a base64 encoded string to a ByteBuffer.
5793 * @param {string} str String to decode
5794 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
5795 * {@link ByteBuffer.DEFAULT_ENDIAN}.
5796 * @returns {!ByteBuffer} ByteBuffer
5797 * @expose
5798 */
5799 ByteBuffer.fromBase64 = function(str, littleEndian) {
5800 if (typeof str !== 'string')
5801 throw TypeError("str");
5802 var bb = new ByteBuffer(str.length/4*3, littleEndian),
5803 i = 0;
5804 lxiv.decode(stringSource(str), function(b) {
5805 bb.view[i++] = b;
5806 });
5807 bb.limit = i;
5808 return bb;
5809 };
5810
5811 /**
5812 * Encodes a binary string to base64 like `window.btoa` does.
5813 * @param {string} str Binary string
5814 * @returns {string} Base64 encoded string
5815 * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa
5816 * @expose
5817 */
5818 ByteBuffer.btoa = function(str) {
5819 return ByteBuffer.fromBinary(str).toBase64();
5820 };
5821
5822 /**
5823 * Decodes a base64 encoded string to binary like `window.atob` does.
5824 * @param {string} b64 Base64 encoded string
5825 * @returns {string} Binary string
5826 * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.atob
5827 * @expose
5828 */
5829 ByteBuffer.atob = function(b64) {
5830 return ByteBuffer.fromBase64(b64).toBinary();
5831 };
5832
5833 // encodings/binary
5834
5835 /**
5836 * Encodes this ByteBuffer to a binary encoded string, that is using only characters 0x00-0xFF as bytes.
5837 * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
5838 * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
5839 * @returns {string} Binary encoded string
5840 * @throws {RangeError} If `offset > limit`
5841 * @expose
5842 */
5843 ByteBufferPrototype.toBinary = function(begin, end) {
5844 if (typeof begin === 'undefined')
5845 begin = this.offset;
5846 if (typeof end === 'undefined')
5847 end = this.limit;
5848 begin |= 0; end |= 0;
5849 if (begin < 0 || end > this.capacity() || begin > end)
5850 throw RangeError("begin, end");
5851 if (begin === end)
5852 return "";
5853 var chars = [],
5854 parts = [];
5855 while (begin < end) {
5856 chars.push(this.view[begin++]);
5857 if (chars.length >= 1024)
5858 parts.push(String.fromCharCode.apply(String, chars)),
5859 chars = [];
5860 }
5861 return parts.join('') + String.fromCharCode.apply(String, chars);
5862 };
5863
5864 /**
5865 * Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.
5866 * @param {string} str String to decode
5867 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
5868 * {@link ByteBuffer.DEFAULT_ENDIAN}.
5869 * @returns {!ByteBuffer} ByteBuffer
5870 * @expose
5871 */
5872 ByteBuffer.fromBinary = function(str, littleEndian) {
5873 if (typeof str !== 'string')
5874 throw TypeError("str");
5875 var i = 0,
5876 k = str.length,
5877 charCode,
5878 bb = new ByteBuffer(k, littleEndian);
5879 while (i<k) {
5880 charCode = str.charCodeAt(i);
5881 if (charCode > 0xff)
5882 throw RangeError("illegal char code: "+charCode);
5883 bb.view[i++] = charCode;
5884 }
5885 bb.limit = k;
5886 return bb;
5887 };
5888
5889 // encodings/debug
5890
5891 /**
5892 * Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:
5893 * * `<` : offset,
5894 * * `'` : markedOffset,
5895 * * `>` : limit,
5896 * * `|` : offset and limit,
5897 * * `[` : offset and markedOffset,
5898 * * `]` : markedOffset and limit,
5899 * * `!` : offset, markedOffset and limit
5900 * @param {boolean=} columns If `true` returns two columns hex + ascii, defaults to `false`
5901 * @returns {string|!Array.<string>} Debug string or array of lines if `asArray = true`
5902 * @expose
5903 * @example `>00'01 02<03` contains four bytes with `limit=0, markedOffset=1, offset=3`
5904 * @example `00[01 02 03>` contains four bytes with `offset=markedOffset=1, limit=4`
5905 * @example `00|01 02 03` contains four bytes with `offset=limit=1, markedOffset=-1`
5906 * @example `|` contains zero bytes with `offset=limit=0, markedOffset=-1`
5907 */
5908 ByteBufferPrototype.toDebug = function(columns) {
5909 var i = -1,
5910 k = this.buffer.byteLength,
5911 b,
5912 hex = "",
5913 asc = "",
5914 out = "";
5915 while (i<k) {
5916 if (i !== -1) {
5917 b = this.view[i];
5918 if (b < 0x10) hex += "0"+b.toString(16).toUpperCase();
5919 else hex += b.toString(16).toUpperCase();
5920 if (columns)
5921 asc += b > 32 && b < 127 ? String.fromCharCode(b) : '.';
5922 }
5923 ++i;
5924 if (columns) {
5925 if (i > 0 && i % 16 === 0 && i !== k) {
5926 while (hex.length < 3*16+3) hex += " ";
5927 out += hex+asc+"\n";
5928 hex = asc = "";
5929 }
5930 }
5931 if (i === this.offset && i === this.limit)
5932 hex += i === this.markedOffset ? "!" : "|";
5933 else if (i === this.offset)
5934 hex += i === this.markedOffset ? "[" : "<";
5935 else if (i === this.limit)
5936 hex += i === this.markedOffset ? "]" : ">";
5937 else
5938 hex += i === this.markedOffset ? "'" : (columns || (i !== 0 && i !== k) ? " " : "");
5939 }
5940 if (columns && hex !== " ") {
5941 while (hex.length < 3*16+3)
5942 hex += " ";
5943 out += hex + asc + "\n";
5944 }
5945 return columns ? out : hex;
5946 };
5947
5948 /**
5949 * Decodes a hex encoded string with marked offsets to a ByteBuffer.
5950 * @param {string} str Debug string to decode (not be generated with `columns = true`)
5951 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
5952 * {@link ByteBuffer.DEFAULT_ENDIAN}.
5953 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
5954 * {@link ByteBuffer.DEFAULT_NOASSERT}.
5955 * @returns {!ByteBuffer} ByteBuffer
5956 * @expose
5957 * @see ByteBuffer#toDebug
5958 */
5959 ByteBuffer.fromDebug = function(str, littleEndian, noAssert) {
5960 var k = str.length,
5961 bb = new ByteBuffer(((k+1)/3)|0, littleEndian, noAssert);
5962 var i = 0, j = 0, ch, b,
5963 rs = false, // Require symbol next
5964 ho = false, hm = false, hl = false, // Already has offset (ho), markedOffset (hm), limit (hl)?
5965 fail = false;
5966 while (i<k) {
5967 switch (ch = str.charAt(i++)) {
5968 case '!':
5969 if (!noAssert) {
5970 if (ho || hm || hl) {
5971 fail = true;
5972 break;
5973 }
5974 ho = hm = hl = true;
5975 }
5976 bb.offset = bb.markedOffset = bb.limit = j;
5977 rs = false;
5978 break;
5979 case '|':
5980 if (!noAssert) {
5981 if (ho || hl) {
5982 fail = true;
5983 break;
5984 }
5985 ho = hl = true;
5986 }
5987 bb.offset = bb.limit = j;
5988 rs = false;
5989 break;
5990 case '[':
5991 if (!noAssert) {
5992 if (ho || hm) {
5993 fail = true;
5994 break;
5995 }
5996 ho = hm = true;
5997 }
5998 bb.offset = bb.markedOffset = j;
5999 rs = false;
6000 break;
6001 case '<':
6002 if (!noAssert) {
6003 if (ho) {
6004 fail = true;
6005 break;
6006 }
6007 ho = true;
6008 }
6009 bb.offset = j;
6010 rs = false;
6011 break;
6012 case ']':
6013 if (!noAssert) {
6014 if (hl || hm) {
6015 fail = true;
6016 break;
6017 }
6018 hl = hm = true;
6019 }
6020 bb.limit = bb.markedOffset = j;
6021 rs = false;
6022 break;
6023 case '>':
6024 if (!noAssert) {
6025 if (hl) {
6026 fail = true;
6027 break;
6028 }
6029 hl = true;
6030 }
6031 bb.limit = j;
6032 rs = false;
6033 break;
6034 case "'":
6035 if (!noAssert) {
6036 if (hm) {
6037 fail = true;
6038 break;
6039 }
6040 hm = true;
6041 }
6042 bb.markedOffset = j;
6043 rs = false;
6044 break;
6045 case ' ':
6046 rs = false;
6047 break;
6048 default:
6049 if (!noAssert) {
6050 if (rs) {
6051 fail = true;
6052 break;
6053 }
6054 }
6055 b = parseInt(ch+str.charAt(i++), 16);
6056 if (!noAssert) {
6057 if (isNaN(b) || b < 0 || b > 255)
6058 throw TypeError("Illegal str: Not a debug encoded string");
6059 }
6060 bb.view[j++] = b;
6061 rs = true;
6062 }
6063 if (fail)
6064 throw TypeError("Illegal str: Invalid symbol at "+i);
6065 }
6066 if (!noAssert) {
6067 if (!ho || !hl)
6068 throw TypeError("Illegal str: Missing offset or limit");
6069 if (j<bb.buffer.byteLength)
6070 throw TypeError("Illegal str: Not a debug encoded string (is it hex?) "+j+" < "+k);
6071 }
6072 return bb;
6073 };
6074
6075 // encodings/hex
6076
6077 /**
6078 * Encodes this ByteBuffer's contents to a hex encoded string.
6079 * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
6080 * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
6081 * @returns {string} Hex encoded string
6082 * @expose
6083 */
6084 ByteBufferPrototype.toHex = function(begin, end) {
6085 begin = typeof begin === 'undefined' ? this.offset : begin;
6086 end = typeof end === 'undefined' ? this.limit : end;
6087 if (!this.noAssert) {
6088 if (typeof begin !== 'number' || begin % 1 !== 0)
6089 throw TypeError("Illegal begin: Not an integer");
6090 begin >>>= 0;
6091 if (typeof end !== 'number' || end % 1 !== 0)
6092 throw TypeError("Illegal end: Not an integer");
6093 end >>>= 0;
6094 if (begin < 0 || begin > end || end > this.buffer.byteLength)
6095 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
6096 }
6097 var out = new Array(end - begin),
6098 b;
6099 while (begin < end) {
6100 b = this.view[begin++];
6101 if (b < 0x10)
6102 out.push("0", b.toString(16));
6103 else out.push(b.toString(16));
6104 }
6105 return out.join('');
6106 };
6107
6108 /**
6109 * Decodes a hex encoded string to a ByteBuffer.
6110 * @param {string} str String to decode
6111 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
6112 * {@link ByteBuffer.DEFAULT_ENDIAN}.
6113 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
6114 * {@link ByteBuffer.DEFAULT_NOASSERT}.
6115 * @returns {!ByteBuffer} ByteBuffer
6116 * @expose
6117 */
6118 ByteBuffer.fromHex = function(str, littleEndian, noAssert) {
6119 if (!noAssert) {
6120 if (typeof str !== 'string')
6121 throw TypeError("Illegal str: Not a string");
6122 if (str.length % 2 !== 0)
6123 throw TypeError("Illegal str: Length not a multiple of 2");
6124 }
6125 var k = str.length,
6126 bb = new ByteBuffer((k / 2) | 0, littleEndian),
6127 b;
6128 for (var i=0, j=0; i<k; i+=2) {
6129 b = parseInt(str.substring(i, i+2), 16);
6130 if (!noAssert)
6131 if (!isFinite(b) || b < 0 || b > 255)
6132 throw TypeError("Illegal str: Contains non-hex characters");
6133 bb.view[j++] = b;
6134 }
6135 bb.limit = j;
6136 return bb;
6137 };
6138
6139 // utfx-embeddable
6140
6141 /**
6142 * utfx-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
6143 * Released under the Apache License, Version 2.0
6144 * see: https://github.com/dcodeIO/utfx for details
6145 */
6146 var utfx = function() {
6147
6148 /**
6149 * utfx namespace.
6150 * @inner
6151 * @type {!Object.<string,*>}
6152 */
6153 var utfx = {};
6154
6155 /**
6156 * Maximum valid code point.
6157 * @type {number}
6158 * @const
6159 */
6160 utfx.MAX_CODEPOINT = 0x10FFFF;
6161
6162 /**
6163 * Encodes UTF8 code points to UTF8 bytes.
6164 * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
6165 * respectively `null` if there are no more code points left or a single numeric code point.
6166 * @param {!function(number)} dst Bytes destination as a function successively called with the next byte
6167 */
6168 utfx.encodeUTF8 = function(src, dst) {
6169 var cp = null;
6170 if (typeof src === 'number')
6171 cp = src,
6172 src = function() { return null; };
6173 while (cp !== null || (cp = src()) !== null) {
6174 if (cp < 0x80)
6175 dst(cp&0x7F);
6176 else if (cp < 0x800)
6177 dst(((cp>>6)&0x1F)|0xC0),
6178 dst((cp&0x3F)|0x80);
6179 else if (cp < 0x10000)
6180 dst(((cp>>12)&0x0F)|0xE0),
6181 dst(((cp>>6)&0x3F)|0x80),
6182 dst((cp&0x3F)|0x80);
6183 else
6184 dst(((cp>>18)&0x07)|0xF0),
6185 dst(((cp>>12)&0x3F)|0x80),
6186 dst(((cp>>6)&0x3F)|0x80),
6187 dst((cp&0x3F)|0x80);
6188 cp = null;
6189 }
6190 };
6191
6192 /**
6193 * Decodes UTF8 bytes to UTF8 code points.
6194 * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
6195 * are no more bytes left.
6196 * @param {!function(number)} dst Code points destination as a function successively called with each decoded code point.
6197 * @throws {RangeError} If a starting byte is invalid in UTF8
6198 * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the
6199 * remaining bytes.
6200 */
6201 utfx.decodeUTF8 = function(src, dst) {
6202 var a, b, c, d, fail = function(b) {
6203 b = b.slice(0, b.indexOf(null));
6204 var err = Error(b.toString());
6205 err.name = "TruncatedError";
6206 err['bytes'] = b;
6207 throw err;
6208 };
6209 while ((a = src()) !== null) {
6210 if ((a&0x80) === 0)
6211 dst(a);
6212 else if ((a&0xE0) === 0xC0)
6213 ((b = src()) === null) && fail([a, b]),
6214 dst(((a&0x1F)<<6) | (b&0x3F));
6215 else if ((a&0xF0) === 0xE0)
6216 ((b=src()) === null || (c=src()) === null) && fail([a, b, c]),
6217 dst(((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F));
6218 else if ((a&0xF8) === 0xF0)
6219 ((b=src()) === null || (c=src()) === null || (d=src()) === null) && fail([a, b, c ,d]),
6220 dst(((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F));
6221 else throw RangeError("Illegal starting byte: "+a);
6222 }
6223 };
6224
6225 /**
6226 * Converts UTF16 characters to UTF8 code points.
6227 * @param {!function():number|null} src Characters source as a function returning the next char code respectively
6228 * `null` if there are no more characters left.
6229 * @param {!function(number)} dst Code points destination as a function successively called with each converted code
6230 * point.
6231 */
6232 utfx.UTF16toUTF8 = function(src, dst) {
6233 var c1, c2 = null;
6234 while (true) {
6235 if ((c1 = c2 !== null ? c2 : src()) === null)
6236 break;
6237 if (c1 >= 0xD800 && c1 <= 0xDFFF) {
6238 if ((c2 = src()) !== null) {
6239 if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
6240 dst((c1-0xD800)*0x400+c2-0xDC00+0x10000);
6241 c2 = null; continue;
6242 }
6243 }
6244 }
6245 dst(c1);
6246 }
6247 if (c2 !== null) dst(c2);
6248 };
6249
6250 /**
6251 * Converts UTF8 code points to UTF16 characters.
6252 * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
6253 * respectively `null` if there are no more code points left or a single numeric code point.
6254 * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
6255 * @throws {RangeError} If a code point is out of range
6256 */
6257 utfx.UTF8toUTF16 = function(src, dst) {
6258 var cp = null;
6259 if (typeof src === 'number')
6260 cp = src, src = function() { return null; };
6261 while (cp !== null || (cp = src()) !== null) {
6262 if (cp <= 0xFFFF)
6263 dst(cp);
6264 else
6265 cp -= 0x10000,
6266 dst((cp>>10)+0xD800),
6267 dst((cp%0x400)+0xDC00);
6268 cp = null;
6269 }
6270 };
6271
6272 /**
6273 * Converts and encodes UTF16 characters to UTF8 bytes.
6274 * @param {!function():number|null} src Characters source as a function returning the next char code respectively `null`
6275 * if there are no more characters left.
6276 * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
6277 */
6278 utfx.encodeUTF16toUTF8 = function(src, dst) {
6279 utfx.UTF16toUTF8(src, function(cp) {
6280 utfx.encodeUTF8(cp, dst);
6281 });
6282 };
6283
6284 /**
6285 * Decodes and converts UTF8 bytes to UTF16 characters.
6286 * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
6287 * are no more bytes left.
6288 * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
6289 * @throws {RangeError} If a starting byte is invalid in UTF8
6290 * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the remaining bytes.
6291 */
6292 utfx.decodeUTF8toUTF16 = function(src, dst) {
6293 utfx.decodeUTF8(src, function(cp) {
6294 utfx.UTF8toUTF16(cp, dst);
6295 });
6296 };
6297
6298 /**
6299 * Calculates the byte length of an UTF8 code point.
6300 * @param {number} cp UTF8 code point
6301 * @returns {number} Byte length
6302 */
6303 utfx.calculateCodePoint = function(cp) {
6304 return (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
6305 };
6306
6307 /**
6308 * Calculates the number of UTF8 bytes required to store UTF8 code points.
6309 * @param {(!function():number|null)} src Code points source as a function returning the next code point respectively
6310 * `null` if there are no more code points left.
6311 * @returns {number} The number of UTF8 bytes required
6312 */
6313 utfx.calculateUTF8 = function(src) {
6314 var cp, l=0;
6315 while ((cp = src()) !== null)
6316 l += (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
6317 return l;
6318 };
6319
6320 /**
6321 * Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.
6322 * @param {(!function():number|null)} src Characters source as a function returning the next char code respectively
6323 * `null` if there are no more characters left.
6324 * @returns {!Array.<number>} The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.
6325 */
6326 utfx.calculateUTF16asUTF8 = function(src) {
6327 var n=0, l=0;
6328 utfx.UTF16toUTF8(src, function(cp) {
6329 ++n; l += (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
6330 });
6331 return [n,l];
6332 };
6333
6334 return utfx;
6335 }();
6336
6337 // encodings/utf8
6338
6339 /**
6340 * Encodes this ByteBuffer's contents between {@link ByteBuffer#offset} and {@link ByteBuffer#limit} to an UTF8 encoded
6341 * string.
6342 * @returns {string} Hex encoded string
6343 * @throws {RangeError} If `offset > limit`
6344 * @expose
6345 */
6346 ByteBufferPrototype.toUTF8 = function(begin, end) {
6347 if (typeof begin === 'undefined') begin = this.offset;
6348 if (typeof end === 'undefined') end = this.limit;
6349 if (!this.noAssert) {
6350 if (typeof begin !== 'number' || begin % 1 !== 0)
6351 throw TypeError("Illegal begin: Not an integer");
6352 begin >>>= 0;
6353 if (typeof end !== 'number' || end % 1 !== 0)
6354 throw TypeError("Illegal end: Not an integer");
6355 end >>>= 0;
6356 if (begin < 0 || begin > end || end > this.buffer.byteLength)
6357 throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
6358 }
6359 var sd; try {
6360 utfx.decodeUTF8toUTF16(function() {
6361 return begin < end ? this.view[begin++] : null;
6362 }.bind(this), sd = stringDestination());
6363 } catch (e) {
6364 if (begin !== end)
6365 throw RangeError("Illegal range: Truncated data, "+begin+" != "+end);
6366 }
6367 return sd();
6368 };
6369
6370 /**
6371 * Decodes an UTF8 encoded string to a ByteBuffer.
6372 * @param {string} str String to decode
6373 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
6374 * {@link ByteBuffer.DEFAULT_ENDIAN}.
6375 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
6376 * {@link ByteBuffer.DEFAULT_NOASSERT}.
6377 * @returns {!ByteBuffer} ByteBuffer
6378 * @expose
6379 */
6380 ByteBuffer.fromUTF8 = function(str, littleEndian, noAssert) {
6381 if (!noAssert)
6382 if (typeof str !== 'string')
6383 throw TypeError("Illegal str: Not a string");
6384 var bb = new ByteBuffer(utfx.calculateUTF16asUTF8(stringSource(str), true)[1], littleEndian, noAssert),
6385 i = 0;
6386 utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
6387 bb.view[i++] = b;
6388 });
6389 bb.limit = i;
6390 return bb;
6391 };
6392
6393 return ByteBuffer;
6394 });
6395 });
6396
6397 var empty = {};
6398
6399 var empty$1 = /*#__PURE__*/Object.freeze({
6400 default: empty
6401 });
6402
6403 var require$$2 = getCjsExportFromNamespace(empty$1);
6404
6405 var protobufLight = createCommonjsModule(function (module) {
6406 /*
6407 Copyright 2013 Daniel Wirtz <dcode@dcode.io>
6408
6409 Licensed under the Apache License, Version 2.0 (the "License");
6410 you may not use this file except in compliance with the License.
6411 You may obtain a copy of the License at
6412
6413 http://www.apache.org/licenses/LICENSE-2.0
6414
6415 Unless required by applicable law or agreed to in writing, software
6416 distributed under the License is distributed on an "AS IS" BASIS,
6417 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6418 See the License for the specific language governing permissions and
6419 limitations under the License.
6420 */
6421
6422 /**
6423 * @license protobuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
6424 * Released under the Apache License, Version 2.0
6425 * see: https://github.com/dcodeIO/protobuf.js for details
6426 */
6427 (function(global, factory) {
6428
6429 /* AMD */ if (typeof commonjsRequire === "function" && 'object' === "object" && module && module["exports"])
6430 module["exports"] = factory(bytebuffer, true);
6431 /* Global */ else
6432 (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
6433
6434 })(commonjsGlobal, function(ByteBuffer, isCommonJS) {
6435
6436 /**
6437 * The ProtoBuf namespace.
6438 * @exports ProtoBuf
6439 * @namespace
6440 * @expose
6441 */
6442 var ProtoBuf = {};
6443
6444 /**
6445 * @type {!function(new: ByteBuffer, ...[*])}
6446 * @expose
6447 */
6448 ProtoBuf.ByteBuffer = ByteBuffer;
6449
6450 /**
6451 * @type {?function(new: Long, ...[*])}
6452 * @expose
6453 */
6454 ProtoBuf.Long = ByteBuffer.Long || null;
6455
6456 /**
6457 * ProtoBuf.js version.
6458 * @type {string}
6459 * @const
6460 * @expose
6461 */
6462 ProtoBuf.VERSION = "5.0.3";
6463
6464 /**
6465 * Wire types.
6466 * @type {Object.<string,number>}
6467 * @const
6468 * @expose
6469 */
6470 ProtoBuf.WIRE_TYPES = {};
6471
6472 /**
6473 * Varint wire type.
6474 * @type {number}
6475 * @expose
6476 */
6477 ProtoBuf.WIRE_TYPES.VARINT = 0;
6478
6479 /**
6480 * Fixed 64 bits wire type.
6481 * @type {number}
6482 * @const
6483 * @expose
6484 */
6485 ProtoBuf.WIRE_TYPES.BITS64 = 1;
6486
6487 /**
6488 * Length delimited wire type.
6489 * @type {number}
6490 * @const
6491 * @expose
6492 */
6493 ProtoBuf.WIRE_TYPES.LDELIM = 2;
6494
6495 /**
6496 * Start group wire type.
6497 * @type {number}
6498 * @const
6499 * @expose
6500 */
6501 ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
6502
6503 /**
6504 * End group wire type.
6505 * @type {number}
6506 * @const
6507 * @expose
6508 */
6509 ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
6510
6511 /**
6512 * Fixed 32 bits wire type.
6513 * @type {number}
6514 * @const
6515 * @expose
6516 */
6517 ProtoBuf.WIRE_TYPES.BITS32 = 5;
6518
6519 /**
6520 * Packable wire types.
6521 * @type {!Array.<number>}
6522 * @const
6523 * @expose
6524 */
6525 ProtoBuf.PACKABLE_WIRE_TYPES = [
6526 ProtoBuf.WIRE_TYPES.VARINT,
6527 ProtoBuf.WIRE_TYPES.BITS64,
6528 ProtoBuf.WIRE_TYPES.BITS32
6529 ];
6530
6531 /**
6532 * Types.
6533 * @dict
6534 * @type {!Object.<string,{name: string, wireType: number, defaultValue: *}>}
6535 * @const
6536 * @expose
6537 */
6538 ProtoBuf.TYPES = {
6539 // According to the protobuf spec.
6540 "int32": {
6541 name: "int32",
6542 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6543 defaultValue: 0
6544 },
6545 "uint32": {
6546 name: "uint32",
6547 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6548 defaultValue: 0
6549 },
6550 "sint32": {
6551 name: "sint32",
6552 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6553 defaultValue: 0
6554 },
6555 "int64": {
6556 name: "int64",
6557 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6558 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
6559 },
6560 "uint64": {
6561 name: "uint64",
6562 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6563 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
6564 },
6565 "sint64": {
6566 name: "sint64",
6567 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6568 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
6569 },
6570 "bool": {
6571 name: "bool",
6572 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6573 defaultValue: false
6574 },
6575 "double": {
6576 name: "double",
6577 wireType: ProtoBuf.WIRE_TYPES.BITS64,
6578 defaultValue: 0
6579 },
6580 "string": {
6581 name: "string",
6582 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
6583 defaultValue: ""
6584 },
6585 "bytes": {
6586 name: "bytes",
6587 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
6588 defaultValue: null // overridden in the code, must be a unique instance
6589 },
6590 "fixed32": {
6591 name: "fixed32",
6592 wireType: ProtoBuf.WIRE_TYPES.BITS32,
6593 defaultValue: 0
6594 },
6595 "sfixed32": {
6596 name: "sfixed32",
6597 wireType: ProtoBuf.WIRE_TYPES.BITS32,
6598 defaultValue: 0
6599 },
6600 "fixed64": {
6601 name: "fixed64",
6602 wireType: ProtoBuf.WIRE_TYPES.BITS64,
6603 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
6604 },
6605 "sfixed64": {
6606 name: "sfixed64",
6607 wireType: ProtoBuf.WIRE_TYPES.BITS64,
6608 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
6609 },
6610 "float": {
6611 name: "float",
6612 wireType: ProtoBuf.WIRE_TYPES.BITS32,
6613 defaultValue: 0
6614 },
6615 "enum": {
6616 name: "enum",
6617 wireType: ProtoBuf.WIRE_TYPES.VARINT,
6618 defaultValue: 0
6619 },
6620 "message": {
6621 name: "message",
6622 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
6623 defaultValue: null
6624 },
6625 "group": {
6626 name: "group",
6627 wireType: ProtoBuf.WIRE_TYPES.STARTGROUP,
6628 defaultValue: null
6629 }
6630 };
6631
6632 /**
6633 * Valid map key types.
6634 * @type {!Array.<!Object.<string,{name: string, wireType: number, defaultValue: *}>>}
6635 * @const
6636 * @expose
6637 */
6638 ProtoBuf.MAP_KEY_TYPES = [
6639 ProtoBuf.TYPES["int32"],
6640 ProtoBuf.TYPES["sint32"],
6641 ProtoBuf.TYPES["sfixed32"],
6642 ProtoBuf.TYPES["uint32"],
6643 ProtoBuf.TYPES["fixed32"],
6644 ProtoBuf.TYPES["int64"],
6645 ProtoBuf.TYPES["sint64"],
6646 ProtoBuf.TYPES["sfixed64"],
6647 ProtoBuf.TYPES["uint64"],
6648 ProtoBuf.TYPES["fixed64"],
6649 ProtoBuf.TYPES["bool"],
6650 ProtoBuf.TYPES["string"],
6651 ProtoBuf.TYPES["bytes"]
6652 ];
6653
6654 /**
6655 * Minimum field id.
6656 * @type {number}
6657 * @const
6658 * @expose
6659 */
6660 ProtoBuf.ID_MIN = 1;
6661
6662 /**
6663 * Maximum field id.
6664 * @type {number}
6665 * @const
6666 * @expose
6667 */
6668 ProtoBuf.ID_MAX = 0x1FFFFFFF;
6669
6670 /**
6671 * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
6672 * Must be set prior to parsing.
6673 * @type {boolean}
6674 * @expose
6675 */
6676 ProtoBuf.convertFieldsToCamelCase = false;
6677
6678 /**
6679 * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
6680 * setting this to `false` prior to building messages.
6681 * @type {boolean}
6682 * @expose
6683 */
6684 ProtoBuf.populateAccessors = true;
6685
6686 /**
6687 * By default, messages are populated with default values if a field is not present on the wire. To disable
6688 * this behavior, set this setting to `false`.
6689 * @type {boolean}
6690 * @expose
6691 */
6692 ProtoBuf.populateDefaults = true;
6693
6694 /**
6695 * @alias ProtoBuf.Util
6696 * @expose
6697 */
6698 ProtoBuf.Util = (function() {
6699
6700 /**
6701 * ProtoBuf utilities.
6702 * @exports ProtoBuf.Util
6703 * @namespace
6704 */
6705 var Util = {};
6706
6707 /**
6708 * Flag if running in node or not.
6709 * @type {boolean}
6710 * @const
6711 * @expose
6712 */
6713 Util.IS_NODE = !!(
6714 typeof process === 'object' && process+'' === '[object process]' && !process['browser']
6715 );
6716
6717 /**
6718 * Constructs a XMLHttpRequest object.
6719 * @return {XMLHttpRequest}
6720 * @throws {Error} If XMLHttpRequest is not supported
6721 * @expose
6722 */
6723 Util.XHR = function() {
6724 // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html
6725 var XMLHttpFactories = [
6726 function () {return new XMLHttpRequest()},
6727 function () {return new ActiveXObject("Msxml2.XMLHTTP")},
6728 function () {return new ActiveXObject("Msxml3.XMLHTTP")},
6729 function () {return new ActiveXObject("Microsoft.XMLHTTP")}
6730 ];
6731 /** @type {?XMLHttpRequest} */
6732 var xhr = null;
6733 for (var i=0;i<XMLHttpFactories.length;i++) {
6734 try { xhr = XMLHttpFactories[i](); }
6735 catch (e) { continue; }
6736 break;
6737 }
6738 if (!xhr)
6739 throw Error("XMLHttpRequest is not supported");
6740 return xhr;
6741 };
6742
6743 /**
6744 * Fetches a resource.
6745 * @param {string} path Resource path
6746 * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will
6747 * be fetched synchronously. If the request failed, contents will be null.
6748 * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
6749 * @expose
6750 */
6751 Util.fetch = function(path, callback) {
6752 if (callback && typeof callback != 'function')
6753 callback = null;
6754 if (Util.IS_NODE) {
6755 var fs = require$$2;
6756 if (callback) {
6757 fs.readFile(path, function(err, data) {
6758 if (err)
6759 callback(null);
6760 else
6761 callback(""+data);
6762 });
6763 } else
6764 try {
6765 return fs.readFileSync(path);
6766 } catch (e) {
6767 return null;
6768 }
6769 } else {
6770 var xhr = Util.XHR();
6771 xhr.open('GET', path, callback ? true : false);
6772 // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
6773 xhr.setRequestHeader('Accept', 'text/plain');
6774 if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
6775 if (callback) {
6776 xhr.onreadystatechange = function() {
6777 if (xhr.readyState != 4) return;
6778 if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
6779 callback(xhr.responseText);
6780 else
6781 callback(null);
6782 };
6783 if (xhr.readyState == 4)
6784 return;
6785 xhr.send(null);
6786 } else {
6787 xhr.send(null);
6788 if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
6789 return xhr.responseText;
6790 return null;
6791 }
6792 }
6793 };
6794
6795 /**
6796 * Converts a string to camel case.
6797 * @param {string} str
6798 * @returns {string}
6799 * @expose
6800 */
6801 Util.toCamelCase = function(str) {
6802 return str.replace(/_([a-zA-Z])/g, function ($0, $1) {
6803 return $1.toUpperCase();
6804 });
6805 };
6806
6807 return Util;
6808 })();
6809
6810 /**
6811 * Language expressions.
6812 * @type {!Object.<string,!RegExp>}
6813 * @expose
6814 */
6815 ProtoBuf.Lang = {
6816
6817 // Characters always ending a statement
6818 DELIM: /[\s\{\}=;:\[\],'"\(\)<>]/g,
6819
6820 // Field rules
6821 RULE: /^(?:required|optional|repeated|map)$/,
6822
6823 // Field types
6824 TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/,
6825
6826 // Names
6827 NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
6828
6829 // Type definitions
6830 TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
6831
6832 // Type references
6833 TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/,
6834
6835 // Fully qualified type references
6836 FQTYPEREF: /^(?:\.[a-zA-Z_][a-zA-Z_0-9]*)+$/,
6837
6838 // All numbers
6839 NUMBER: /^-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+|([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?)|inf|nan)$/,
6840
6841 // Decimal numbers
6842 NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/,
6843
6844 // Hexadecimal numbers
6845 NUMBER_HEX: /^0[xX][0-9a-fA-F]+$/,
6846
6847 // Octal numbers
6848 NUMBER_OCT: /^0[0-7]+$/,
6849
6850 // Floating point numbers
6851 NUMBER_FLT: /^([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?|inf|nan)$/,
6852
6853 // Booleans
6854 BOOL: /^(?:true|false)$/i,
6855
6856 // Id numbers
6857 ID: /^(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
6858
6859 // Negative id numbers (enum values)
6860 NEGID: /^\-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
6861
6862 // Whitespaces
6863 WHITESPACE: /\s/,
6864
6865 // All strings
6866 STRING: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")|(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g,
6867
6868 // Double quoted strings
6869 STRING_DQ: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g,
6870
6871 // Single quoted strings
6872 STRING_SQ: /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g
6873 };
6874
6875
6876 /**
6877 * @alias ProtoBuf.Reflect
6878 * @expose
6879 */
6880 ProtoBuf.Reflect = (function(ProtoBuf) {
6881
6882 /**
6883 * Reflection types.
6884 * @exports ProtoBuf.Reflect
6885 * @namespace
6886 */
6887 var Reflect = {};
6888
6889 /**
6890 * Constructs a Reflect base class.
6891 * @exports ProtoBuf.Reflect.T
6892 * @constructor
6893 * @abstract
6894 * @param {!ProtoBuf.Builder} builder Builder reference
6895 * @param {?ProtoBuf.Reflect.T} parent Parent object
6896 * @param {string} name Object name
6897 */
6898 var T = function(builder, parent, name) {
6899
6900 /**
6901 * Builder reference.
6902 * @type {!ProtoBuf.Builder}
6903 * @expose
6904 */
6905 this.builder = builder;
6906
6907 /**
6908 * Parent object.
6909 * @type {?ProtoBuf.Reflect.T}
6910 * @expose
6911 */
6912 this.parent = parent;
6913
6914 /**
6915 * Object name in namespace.
6916 * @type {string}
6917 * @expose
6918 */
6919 this.name = name;
6920
6921 /**
6922 * Fully qualified class name
6923 * @type {string}
6924 * @expose
6925 */
6926 this.className;
6927 };
6928
6929 /**
6930 * @alias ProtoBuf.Reflect.T.prototype
6931 * @inner
6932 */
6933 var TPrototype = T.prototype;
6934
6935 /**
6936 * Returns the fully qualified name of this object.
6937 * @returns {string} Fully qualified name as of ".PATH.TO.THIS"
6938 * @expose
6939 */
6940 TPrototype.fqn = function() {
6941 var name = this.name,
6942 ptr = this;
6943 do {
6944 ptr = ptr.parent;
6945 if (ptr == null)
6946 break;
6947 name = ptr.name+"."+name;
6948 } while (true);
6949 return name;
6950 };
6951
6952 /**
6953 * Returns a string representation of this Reflect object (its fully qualified name).
6954 * @param {boolean=} includeClass Set to true to include the class name. Defaults to false.
6955 * @return String representation
6956 * @expose
6957 */
6958 TPrototype.toString = function(includeClass) {
6959 return (includeClass ? this.className + " " : "") + this.fqn();
6960 };
6961
6962 /**
6963 * Builds this type.
6964 * @throws {Error} If this type cannot be built directly
6965 * @expose
6966 */
6967 TPrototype.build = function() {
6968 throw Error(this.toString(true)+" cannot be built directly");
6969 };
6970
6971 /**
6972 * @alias ProtoBuf.Reflect.T
6973 * @expose
6974 */
6975 Reflect.T = T;
6976
6977 /**
6978 * Constructs a new Namespace.
6979 * @exports ProtoBuf.Reflect.Namespace
6980 * @param {!ProtoBuf.Builder} builder Builder reference
6981 * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent
6982 * @param {string} name Namespace name
6983 * @param {Object.<string,*>=} options Namespace options
6984 * @param {string?} syntax The syntax level of this definition (e.g., proto3)
6985 * @constructor
6986 * @extends ProtoBuf.Reflect.T
6987 */
6988 var Namespace = function(builder, parent, name, options, syntax) {
6989 T.call(this, builder, parent, name);
6990
6991 /**
6992 * @override
6993 */
6994 this.className = "Namespace";
6995
6996 /**
6997 * Children inside the namespace.
6998 * @type {!Array.<ProtoBuf.Reflect.T>}
6999 */
7000 this.children = [];
7001
7002 /**
7003 * Options.
7004 * @type {!Object.<string, *>}
7005 */
7006 this.options = options || {};
7007
7008 /**
7009 * Syntax level (e.g., proto2 or proto3).
7010 * @type {!string}
7011 */
7012 this.syntax = syntax || "proto2";
7013 };
7014
7015 /**
7016 * @alias ProtoBuf.Reflect.Namespace.prototype
7017 * @inner
7018 */
7019 var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);
7020
7021 /**
7022 * Returns an array of the namespace's children.
7023 * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).
7024 * @return {Array.<ProtoBuf.Reflect.T>}
7025 * @expose
7026 */
7027 NamespacePrototype.getChildren = function(type) {
7028 type = type || null;
7029 if (type == null)
7030 return this.children.slice();
7031 var children = [];
7032 for (var i=0, k=this.children.length; i<k; ++i)
7033 if (this.children[i] instanceof type)
7034 children.push(this.children[i]);
7035 return children;
7036 };
7037
7038 /**
7039 * Adds a child to the namespace.
7040 * @param {ProtoBuf.Reflect.T} child Child
7041 * @throws {Error} If the child cannot be added (duplicate)
7042 * @expose
7043 */
7044 NamespacePrototype.addChild = function(child) {
7045 var other;
7046 if (other = this.getChild(child.name)) {
7047 // Try to revert camelcase transformation on collision
7048 if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)
7049 other.name = other.originalName; // Revert previous first (effectively keeps both originals)
7050 else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)
7051 child.name = child.originalName;
7052 else
7053 throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);
7054 }
7055 this.children.push(child);
7056 };
7057
7058 /**
7059 * Gets a child by its name or id.
7060 * @param {string|number} nameOrId Child name or id
7061 * @return {?ProtoBuf.Reflect.T} The child or null if not found
7062 * @expose
7063 */
7064 NamespacePrototype.getChild = function(nameOrId) {
7065 var key = typeof nameOrId === 'number' ? 'id' : 'name';
7066 for (var i=0, k=this.children.length; i<k; ++i)
7067 if (this.children[i][key] === nameOrId)
7068 return this.children[i];
7069 return null;
7070 };
7071
7072 /**
7073 * Resolves a reflect object inside of this namespace.
7074 * @param {string|!Array.<string>} qn Qualified name to resolve
7075 * @param {boolean=} excludeNonNamespace Excludes non-namespace types, defaults to `false`
7076 * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found
7077 * @expose
7078 */
7079 NamespacePrototype.resolve = function(qn, excludeNonNamespace) {
7080 var part = typeof qn === 'string' ? qn.split(".") : qn,
7081 ptr = this,
7082 i = 0;
7083 if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'
7084 while (ptr.parent !== null)
7085 ptr = ptr.parent;
7086 i++;
7087 }
7088 var child;
7089 do {
7090 do {
7091 if (!(ptr instanceof Reflect.Namespace)) {
7092 ptr = null;
7093 break;
7094 }
7095 child = ptr.getChild(part[i]);
7096 if (!child || !(child instanceof Reflect.T) || (excludeNonNamespace && !(child instanceof Reflect.Namespace))) {
7097 ptr = null;
7098 break;
7099 }
7100 ptr = child; i++;
7101 } while (i < part.length);
7102 if (ptr != null)
7103 break; // Found
7104 // Else search the parent
7105 if (this.parent !== null)
7106 return this.parent.resolve(qn, excludeNonNamespace);
7107 } while (ptr != null);
7108 return ptr;
7109 };
7110
7111 /**
7112 * Determines the shortest qualified name of the specified type, if any, relative to this namespace.
7113 * @param {!ProtoBuf.Reflect.T} t Reflection type
7114 * @returns {string} The shortest qualified name or, if there is none, the fqn
7115 * @expose
7116 */
7117 NamespacePrototype.qn = function(t) {
7118 var part = [], ptr = t;
7119 do {
7120 part.unshift(ptr.name);
7121 ptr = ptr.parent;
7122 } while (ptr !== null);
7123 for (var len=1; len <= part.length; len++) {
7124 var qn = part.slice(part.length-len);
7125 if (t === this.resolve(qn, t instanceof Reflect.Namespace))
7126 return qn.join(".");
7127 }
7128 return t.fqn();
7129 };
7130
7131 /**
7132 * Builds the namespace and returns the runtime counterpart.
7133 * @return {Object.<string,Function|Object>} Runtime namespace
7134 * @expose
7135 */
7136 NamespacePrototype.build = function() {
7137 /** @dict */
7138 var ns = {};
7139 var children = this.children;
7140 for (var i=0, k=children.length, child; i<k; ++i) {
7141 child = children[i];
7142 if (child instanceof Namespace)
7143 ns[child.name] = child.build();
7144 }
7145 if (Object.defineProperty)
7146 Object.defineProperty(ns, "$options", { "value": this.buildOpt() });
7147 return ns;
7148 };
7149
7150 /**
7151 * Builds the namespace's '$options' property.
7152 * @return {Object.<string,*>}
7153 */
7154 NamespacePrototype.buildOpt = function() {
7155 var opt = {},
7156 keys = Object.keys(this.options);
7157 for (var i=0, k=keys.length; i<k; ++i) {
7158 var key = keys[i],
7159 val = this.options[keys[i]];
7160 // TODO: Options are not resolved, yet.
7161 // if (val instanceof Namespace) {
7162 // opt[key] = val.build();
7163 // } else {
7164 opt[key] = val;
7165 // }
7166 }
7167 return opt;
7168 };
7169
7170 /**
7171 * Gets the value assigned to the option with the specified name.
7172 * @param {string=} name Returns the option value if specified, otherwise all options are returned.
7173 * @return {*|Object.<string,*>}null} Option value or NULL if there is no such option
7174 */
7175 NamespacePrototype.getOption = function(name) {
7176 if (typeof name === 'undefined')
7177 return this.options;
7178 return typeof this.options[name] !== 'undefined' ? this.options[name] : null;
7179 };
7180
7181 /**
7182 * @alias ProtoBuf.Reflect.Namespace
7183 * @expose
7184 */
7185 Reflect.Namespace = Namespace;
7186
7187 /**
7188 * Constructs a new Element implementation that checks and converts values for a
7189 * particular field type, as appropriate.
7190 *
7191 * An Element represents a single value: either the value of a singular field,
7192 * or a value contained in one entry of a repeated field or map field. This
7193 * class does not implement these higher-level concepts; it only encapsulates
7194 * the low-level typechecking and conversion.
7195 *
7196 * @exports ProtoBuf.Reflect.Element
7197 * @param {{name: string, wireType: number}} type Resolved data type
7198 * @param {ProtoBuf.Reflect.T|null} resolvedType Resolved type, if relevant
7199 * (e.g. submessage field).
7200 * @param {boolean} isMapKey Is this element a Map key? The value will be
7201 * converted to string form if so.
7202 * @param {string} syntax Syntax level of defining message type, e.g.,
7203 * proto2 or proto3.
7204 * @param {string} name Name of the field containing this element (for error
7205 * messages)
7206 * @constructor
7207 */
7208 var Element = function(type, resolvedType, isMapKey, syntax, name) {
7209
7210 /**
7211 * Element type, as a string (e.g., int32).
7212 * @type {{name: string, wireType: number}}
7213 */
7214 this.type = type;
7215
7216 /**
7217 * Element type reference to submessage or enum definition, if needed.
7218 * @type {ProtoBuf.Reflect.T|null}
7219 */
7220 this.resolvedType = resolvedType;
7221
7222 /**
7223 * Element is a map key.
7224 * @type {boolean}
7225 */
7226 this.isMapKey = isMapKey;
7227
7228 /**
7229 * Syntax level of defining message type, e.g., proto2 or proto3.
7230 * @type {string}
7231 */
7232 this.syntax = syntax;
7233
7234 /**
7235 * Name of the field containing this element (for error messages)
7236 * @type {string}
7237 */
7238 this.name = name;
7239
7240 if (isMapKey && ProtoBuf.MAP_KEY_TYPES.indexOf(type) < 0)
7241 throw Error("Invalid map key type: " + type.name);
7242 };
7243
7244 var ElementPrototype = Element.prototype;
7245
7246 /**
7247 * Obtains a (new) default value for the specified type.
7248 * @param type {string|{name: string, wireType: number}} Field type
7249 * @returns {*} Default value
7250 * @inner
7251 */
7252 function mkDefault(type) {
7253 if (typeof type === 'string')
7254 type = ProtoBuf.TYPES[type];
7255 if (typeof type.defaultValue === 'undefined')
7256 throw Error("default value for type "+type.name+" is not supported");
7257 if (type == ProtoBuf.TYPES["bytes"])
7258 return new ByteBuffer(0);
7259 return type.defaultValue;
7260 }
7261
7262 /**
7263 * Returns the default value for this field in proto3.
7264 * @function
7265 * @param type {string|{name: string, wireType: number}} the field type
7266 * @returns {*} Default value
7267 */
7268 Element.defaultFieldValue = mkDefault;
7269
7270 /**
7271 * Makes a Long from a value.
7272 * @param {{low: number, high: number, unsigned: boolean}|string|number} value Value
7273 * @param {boolean=} unsigned Whether unsigned or not, defaults to reuse it from Long-like objects or to signed for
7274 * strings and numbers
7275 * @returns {!Long}
7276 * @throws {Error} If the value cannot be converted to a Long
7277 * @inner
7278 */
7279 function mkLong(value, unsigned) {
7280 if (value && typeof value.low === 'number' && typeof value.high === 'number' && typeof value.unsigned === 'boolean'
7281 && value.low === value.low && value.high === value.high)
7282 return new ProtoBuf.Long(value.low, value.high, typeof unsigned === 'undefined' ? value.unsigned : unsigned);
7283 if (typeof value === 'string')
7284 return ProtoBuf.Long.fromString(value, unsigned || false, 10);
7285 if (typeof value === 'number')
7286 return ProtoBuf.Long.fromNumber(value, unsigned || false);
7287 throw Error("not convertible to Long");
7288 }
7289
7290 ElementPrototype.toString = function() {
7291 return (this.name || '') + (this.isMapKey ? 'map' : 'value') + ' element';
7292 };
7293
7294 /**
7295 * Checks if the given value can be set for an element of this type (singular
7296 * field or one element of a repeated field or map).
7297 * @param {*} value Value to check
7298 * @return {*} Verified, maybe adjusted, value
7299 * @throws {Error} If the value cannot be verified for this element slot
7300 * @expose
7301 */
7302 ElementPrototype.verifyValue = function(value) {
7303 var self = this;
7304 function fail(val, msg) {
7305 throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
7306 }
7307 switch (this.type) {
7308 // Signed 32bit
7309 case ProtoBuf.TYPES["int32"]:
7310 case ProtoBuf.TYPES["sint32"]:
7311 case ProtoBuf.TYPES["sfixed32"]:
7312 // Account for !NaN: value === value
7313 if (typeof value !== 'number' || (value === value && value % 1 !== 0))
7314 fail(typeof value, "not an integer");
7315 return value > 4294967295 ? value | 0 : value;
7316
7317 // Unsigned 32bit
7318 case ProtoBuf.TYPES["uint32"]:
7319 case ProtoBuf.TYPES["fixed32"]:
7320 if (typeof value !== 'number' || (value === value && value % 1 !== 0))
7321 fail(typeof value, "not an integer");
7322 return value < 0 ? value >>> 0 : value;
7323
7324 // Signed 64bit
7325 case ProtoBuf.TYPES["int64"]:
7326 case ProtoBuf.TYPES["sint64"]:
7327 case ProtoBuf.TYPES["sfixed64"]: {
7328 if (ProtoBuf.Long)
7329 try {
7330 return mkLong(value, false);
7331 } catch (e) {
7332 fail(typeof value, e.message);
7333 }
7334 else
7335 fail(typeof value, "requires Long.js");
7336 }
7337
7338 // Unsigned 64bit
7339 case ProtoBuf.TYPES["uint64"]:
7340 case ProtoBuf.TYPES["fixed64"]: {
7341 if (ProtoBuf.Long)
7342 try {
7343 return mkLong(value, true);
7344 } catch (e) {
7345 fail(typeof value, e.message);
7346 }
7347 else
7348 fail(typeof value, "requires Long.js");
7349 }
7350
7351 // Bool
7352 case ProtoBuf.TYPES["bool"]:
7353 if (typeof value !== 'boolean')
7354 fail(typeof value, "not a boolean");
7355 return value;
7356
7357 // Float
7358 case ProtoBuf.TYPES["float"]:
7359 case ProtoBuf.TYPES["double"]:
7360 if (typeof value !== 'number')
7361 fail(typeof value, "not a number");
7362 return value;
7363
7364 // Length-delimited string
7365 case ProtoBuf.TYPES["string"]:
7366 if (typeof value !== 'string' && !(value && value instanceof String))
7367 fail(typeof value, "not a string");
7368 return ""+value; // Convert String object to string
7369
7370 // Length-delimited bytes
7371 case ProtoBuf.TYPES["bytes"]:
7372 if (ByteBuffer.isByteBuffer(value))
7373 return value;
7374 return ByteBuffer.wrap(value, "base64");
7375
7376 // Constant enum value
7377 case ProtoBuf.TYPES["enum"]: {
7378 var values = this.resolvedType.getChildren(ProtoBuf.Reflect.Enum.Value);
7379 for (i=0; i<values.length; i++)
7380 if (values[i].name == value)
7381 return values[i].id;
7382 else if (values[i].id == value)
7383 return values[i].id;
7384
7385 if (this.syntax === 'proto3') {
7386 // proto3: just make sure it's an integer.
7387 if (typeof value !== 'number' || (value === value && value % 1 !== 0))
7388 fail(typeof value, "not an integer");
7389 if (value > 4294967295 || value < 0)
7390 fail(typeof value, "not in range for uint32");
7391 return value;
7392 } else {
7393 // proto2 requires enum values to be valid.
7394 fail(value, "not a valid enum value");
7395 }
7396 }
7397 // Embedded message
7398 case ProtoBuf.TYPES["group"]:
7399 case ProtoBuf.TYPES["message"]: {
7400 if (!value || typeof value !== 'object')
7401 fail(typeof value, "object expected");
7402 if (value instanceof this.resolvedType.clazz)
7403 return value;
7404 if (value instanceof ProtoBuf.Builder.Message) {
7405 // Mismatched type: Convert to object (see: https://github.com/dcodeIO/ProtoBuf.js/issues/180)
7406 var obj = {};
7407 for (var i in value)
7408 if (value.hasOwnProperty(i))
7409 obj[i] = value[i];
7410 value = obj;
7411 }
7412 // Else let's try to construct one from a key-value object
7413 return new (this.resolvedType.clazz)(value); // May throw for a hundred of reasons
7414 }
7415 }
7416
7417 // We should never end here
7418 throw Error("[INTERNAL] Illegal value for "+this.toString(true)+": "+value+" (undefined type "+this.type+")");
7419 };
7420
7421 /**
7422 * Calculates the byte length of an element on the wire.
7423 * @param {number} id Field number
7424 * @param {*} value Field value
7425 * @returns {number} Byte length
7426 * @throws {Error} If the value cannot be calculated
7427 * @expose
7428 */
7429 ElementPrototype.calculateLength = function(id, value) {
7430 if (value === null) return 0; // Nothing to encode
7431 // Tag has already been written
7432 var n;
7433 switch (this.type) {
7434 case ProtoBuf.TYPES["int32"]:
7435 return value < 0 ? ByteBuffer.calculateVarint64(value) : ByteBuffer.calculateVarint32(value);
7436 case ProtoBuf.TYPES["uint32"]:
7437 return ByteBuffer.calculateVarint32(value);
7438 case ProtoBuf.TYPES["sint32"]:
7439 return ByteBuffer.calculateVarint32(ByteBuffer.zigZagEncode32(value));
7440 case ProtoBuf.TYPES["fixed32"]:
7441 case ProtoBuf.TYPES["sfixed32"]:
7442 case ProtoBuf.TYPES["float"]:
7443 return 4;
7444 case ProtoBuf.TYPES["int64"]:
7445 case ProtoBuf.TYPES["uint64"]:
7446 return ByteBuffer.calculateVarint64(value);
7447 case ProtoBuf.TYPES["sint64"]:
7448 return ByteBuffer.calculateVarint64(ByteBuffer.zigZagEncode64(value));
7449 case ProtoBuf.TYPES["fixed64"]:
7450 case ProtoBuf.TYPES["sfixed64"]:
7451 return 8;
7452 case ProtoBuf.TYPES["bool"]:
7453 return 1;
7454 case ProtoBuf.TYPES["enum"]:
7455 return ByteBuffer.calculateVarint32(value);
7456 case ProtoBuf.TYPES["double"]:
7457 return 8;
7458 case ProtoBuf.TYPES["string"]:
7459 n = ByteBuffer.calculateUTF8Bytes(value);
7460 return ByteBuffer.calculateVarint32(n) + n;
7461 case ProtoBuf.TYPES["bytes"]:
7462 if (value.remaining() < 0)
7463 throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
7464 return ByteBuffer.calculateVarint32(value.remaining()) + value.remaining();
7465 case ProtoBuf.TYPES["message"]:
7466 n = this.resolvedType.calculate(value);
7467 return ByteBuffer.calculateVarint32(n) + n;
7468 case ProtoBuf.TYPES["group"]:
7469 n = this.resolvedType.calculate(value);
7470 return n + ByteBuffer.calculateVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
7471 }
7472 // We should never end here
7473 throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
7474 };
7475
7476 /**
7477 * Encodes a value to the specified buffer. Does not encode the key.
7478 * @param {number} id Field number
7479 * @param {*} value Field value
7480 * @param {ByteBuffer} buffer ByteBuffer to encode to
7481 * @return {ByteBuffer} The ByteBuffer for chaining
7482 * @throws {Error} If the value cannot be encoded
7483 * @expose
7484 */
7485 ElementPrototype.encodeValue = function(id, value, buffer) {
7486 if (value === null) return buffer; // Nothing to encode
7487 // Tag has already been written
7488
7489 switch (this.type) {
7490 // 32bit signed varint
7491 case ProtoBuf.TYPES["int32"]:
7492 // "If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes
7493 // long – it is, effectively, treated like a very large unsigned integer." (see #122)
7494 if (value < 0)
7495 buffer.writeVarint64(value);
7496 else
7497 buffer.writeVarint32(value);
7498 break;
7499
7500 // 32bit unsigned varint
7501 case ProtoBuf.TYPES["uint32"]:
7502 buffer.writeVarint32(value);
7503 break;
7504
7505 // 32bit varint zig-zag
7506 case ProtoBuf.TYPES["sint32"]:
7507 buffer.writeVarint32ZigZag(value);
7508 break;
7509
7510 // Fixed unsigned 32bit
7511 case ProtoBuf.TYPES["fixed32"]:
7512 buffer.writeUint32(value);
7513 break;
7514
7515 // Fixed signed 32bit
7516 case ProtoBuf.TYPES["sfixed32"]:
7517 buffer.writeInt32(value);
7518 break;
7519
7520 // 64bit varint as-is
7521 case ProtoBuf.TYPES["int64"]:
7522 case ProtoBuf.TYPES["uint64"]:
7523 buffer.writeVarint64(value); // throws
7524 break;
7525
7526 // 64bit varint zig-zag
7527 case ProtoBuf.TYPES["sint64"]:
7528 buffer.writeVarint64ZigZag(value); // throws
7529 break;
7530
7531 // Fixed unsigned 64bit
7532 case ProtoBuf.TYPES["fixed64"]:
7533 buffer.writeUint64(value); // throws
7534 break;
7535
7536 // Fixed signed 64bit
7537 case ProtoBuf.TYPES["sfixed64"]:
7538 buffer.writeInt64(value); // throws
7539 break;
7540
7541 // Bool
7542 case ProtoBuf.TYPES["bool"]:
7543 if (typeof value === 'string')
7544 buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : !!value);
7545 else
7546 buffer.writeVarint32(value ? 1 : 0);
7547 break;
7548
7549 // Constant enum value
7550 case ProtoBuf.TYPES["enum"]:
7551 buffer.writeVarint32(value);
7552 break;
7553
7554 // 32bit float
7555 case ProtoBuf.TYPES["float"]:
7556 buffer.writeFloat32(value);
7557 break;
7558
7559 // 64bit float
7560 case ProtoBuf.TYPES["double"]:
7561 buffer.writeFloat64(value);
7562 break;
7563
7564 // Length-delimited string
7565 case ProtoBuf.TYPES["string"]:
7566 buffer.writeVString(value);
7567 break;
7568
7569 // Length-delimited bytes
7570 case ProtoBuf.TYPES["bytes"]:
7571 if (value.remaining() < 0)
7572 throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
7573 var prevOffset = value.offset;
7574 buffer.writeVarint32(value.remaining());
7575 buffer.append(value);
7576 value.offset = prevOffset;
7577 break;
7578
7579 // Embedded message
7580 case ProtoBuf.TYPES["message"]:
7581 var bb = new ByteBuffer().LE();
7582 this.resolvedType.encode(value, bb);
7583 buffer.writeVarint32(bb.offset);
7584 buffer.append(bb.flip());
7585 break;
7586
7587 // Legacy group
7588 case ProtoBuf.TYPES["group"]:
7589 this.resolvedType.encode(value, buffer);
7590 buffer.writeVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
7591 break;
7592
7593 default:
7594 // We should never end here
7595 throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
7596 }
7597 return buffer;
7598 };
7599
7600 /**
7601 * Decode one element value from the specified buffer.
7602 * @param {ByteBuffer} buffer ByteBuffer to decode from
7603 * @param {number} wireType The field wire type
7604 * @param {number} id The field number
7605 * @return {*} Decoded value
7606 * @throws {Error} If the field cannot be decoded
7607 * @expose
7608 */
7609 ElementPrototype.decode = function(buffer, wireType, id) {
7610 if (wireType != this.type.wireType)
7611 throw Error("Unexpected wire type for element");
7612
7613 var value, nBytes;
7614 switch (this.type) {
7615 // 32bit signed varint
7616 case ProtoBuf.TYPES["int32"]:
7617 return buffer.readVarint32() | 0;
7618
7619 // 32bit unsigned varint
7620 case ProtoBuf.TYPES["uint32"]:
7621 return buffer.readVarint32() >>> 0;
7622
7623 // 32bit signed varint zig-zag
7624 case ProtoBuf.TYPES["sint32"]:
7625 return buffer.readVarint32ZigZag() | 0;
7626
7627 // Fixed 32bit unsigned
7628 case ProtoBuf.TYPES["fixed32"]:
7629 return buffer.readUint32() >>> 0;
7630
7631 case ProtoBuf.TYPES["sfixed32"]:
7632 return buffer.readInt32() | 0;
7633
7634 // 64bit signed varint
7635 case ProtoBuf.TYPES["int64"]:
7636 return buffer.readVarint64();
7637
7638 // 64bit unsigned varint
7639 case ProtoBuf.TYPES["uint64"]:
7640 return buffer.readVarint64().toUnsigned();
7641
7642 // 64bit signed varint zig-zag
7643 case ProtoBuf.TYPES["sint64"]:
7644 return buffer.readVarint64ZigZag();
7645
7646 // Fixed 64bit unsigned
7647 case ProtoBuf.TYPES["fixed64"]:
7648 return buffer.readUint64();
7649
7650 // Fixed 64bit signed
7651 case ProtoBuf.TYPES["sfixed64"]:
7652 return buffer.readInt64();
7653
7654 // Bool varint
7655 case ProtoBuf.TYPES["bool"]:
7656 return !!buffer.readVarint32();
7657
7658 // Constant enum value (varint)
7659 case ProtoBuf.TYPES["enum"]:
7660 // The following Builder.Message#set will already throw
7661 return buffer.readVarint32();
7662
7663 // 32bit float
7664 case ProtoBuf.TYPES["float"]:
7665 return buffer.readFloat();
7666
7667 // 64bit float
7668 case ProtoBuf.TYPES["double"]:
7669 return buffer.readDouble();
7670
7671 // Length-delimited string
7672 case ProtoBuf.TYPES["string"]:
7673 return buffer.readVString();
7674
7675 // Length-delimited bytes
7676 case ProtoBuf.TYPES["bytes"]: {
7677 nBytes = buffer.readVarint32();
7678 if (buffer.remaining() < nBytes)
7679 throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
7680 value = buffer.clone(); // Offset already set
7681 value.limit = value.offset+nBytes;
7682 buffer.offset += nBytes;
7683 return value;
7684 }
7685
7686 // Length-delimited embedded message
7687 case ProtoBuf.TYPES["message"]: {
7688 nBytes = buffer.readVarint32();
7689 return this.resolvedType.decode(buffer, nBytes);
7690 }
7691
7692 // Legacy group
7693 case ProtoBuf.TYPES["group"]:
7694 return this.resolvedType.decode(buffer, -1, id);
7695 }
7696
7697 // We should never end here
7698 throw Error("[INTERNAL] Illegal decode type");
7699 };
7700
7701 /**
7702 * Converts a value from a string to the canonical element type.
7703 *
7704 * Legal only when isMapKey is true.
7705 *
7706 * @param {string} str The string value
7707 * @returns {*} The value
7708 */
7709 ElementPrototype.valueFromString = function(str) {
7710 if (!this.isMapKey) {
7711 throw Error("valueFromString() called on non-map-key element");
7712 }
7713
7714 switch (this.type) {
7715 case ProtoBuf.TYPES["int32"]:
7716 case ProtoBuf.TYPES["sint32"]:
7717 case ProtoBuf.TYPES["sfixed32"]:
7718 case ProtoBuf.TYPES["uint32"]:
7719 case ProtoBuf.TYPES["fixed32"]:
7720 return this.verifyValue(parseInt(str));
7721
7722 case ProtoBuf.TYPES["int64"]:
7723 case ProtoBuf.TYPES["sint64"]:
7724 case ProtoBuf.TYPES["sfixed64"]:
7725 case ProtoBuf.TYPES["uint64"]:
7726 case ProtoBuf.TYPES["fixed64"]:
7727 // Long-based fields support conversions from string already.
7728 return this.verifyValue(str);
7729
7730 case ProtoBuf.TYPES["bool"]:
7731 return str === "true";
7732
7733 case ProtoBuf.TYPES["string"]:
7734 return this.verifyValue(str);
7735
7736 case ProtoBuf.TYPES["bytes"]:
7737 return ByteBuffer.fromBinary(str);
7738 }
7739 };
7740
7741 /**
7742 * Converts a value from the canonical element type to a string.
7743 *
7744 * It should be the case that `valueFromString(valueToString(val))` returns
7745 * a value equivalent to `verifyValue(val)` for every legal value of `val`
7746 * according to this element type.
7747 *
7748 * This may be used when the element must be stored or used as a string,
7749 * e.g., as a map key on an Object.
7750 *
7751 * Legal only when isMapKey is true.
7752 *
7753 * @param {*} val The value
7754 * @returns {string} The string form of the value.
7755 */
7756 ElementPrototype.valueToString = function(value) {
7757 if (!this.isMapKey) {
7758 throw Error("valueToString() called on non-map-key element");
7759 }
7760
7761 if (this.type === ProtoBuf.TYPES["bytes"]) {
7762 return value.toString("binary");
7763 } else {
7764 return value.toString();
7765 }
7766 };
7767
7768 /**
7769 * @alias ProtoBuf.Reflect.Element
7770 * @expose
7771 */
7772 Reflect.Element = Element;
7773
7774 /**
7775 * Constructs a new Message.
7776 * @exports ProtoBuf.Reflect.Message
7777 * @param {!ProtoBuf.Builder} builder Builder reference
7778 * @param {!ProtoBuf.Reflect.Namespace} parent Parent message or namespace
7779 * @param {string} name Message name
7780 * @param {Object.<string,*>=} options Message options
7781 * @param {boolean=} isGroup `true` if this is a legacy group
7782 * @param {string?} syntax The syntax level of this definition (e.g., proto3)
7783 * @constructor
7784 * @extends ProtoBuf.Reflect.Namespace
7785 */
7786 var Message = function(builder, parent, name, options, isGroup, syntax) {
7787 Namespace.call(this, builder, parent, name, options, syntax);
7788
7789 /**
7790 * @override
7791 */
7792 this.className = "Message";
7793
7794 /**
7795 * Extensions range.
7796 * @type {!Array.<number>|undefined}
7797 * @expose
7798 */
7799 this.extensions = undefined;
7800
7801 /**
7802 * Runtime message class.
7803 * @type {?function(new:ProtoBuf.Builder.Message)}
7804 * @expose
7805 */
7806 this.clazz = null;
7807
7808 /**
7809 * Whether this is a legacy group or not.
7810 * @type {boolean}
7811 * @expose
7812 */
7813 this.isGroup = !!isGroup;
7814
7815 // The following cached collections are used to efficiently iterate over or look up fields when decoding.
7816
7817 /**
7818 * Cached fields.
7819 * @type {?Array.<!ProtoBuf.Reflect.Message.Field>}
7820 * @private
7821 */
7822 this._fields = null;
7823
7824 /**
7825 * Cached fields by id.
7826 * @type {?Object.<number,!ProtoBuf.Reflect.Message.Field>}
7827 * @private
7828 */
7829 this._fieldsById = null;
7830
7831 /**
7832 * Cached fields by name.
7833 * @type {?Object.<string,!ProtoBuf.Reflect.Message.Field>}
7834 * @private
7835 */
7836 this._fieldsByName = null;
7837 };
7838
7839 /**
7840 * @alias ProtoBuf.Reflect.Message.prototype
7841 * @inner
7842 */
7843 var MessagePrototype = Message.prototype = Object.create(Namespace.prototype);
7844
7845 /**
7846 * Builds the message and returns the runtime counterpart, which is a fully functional class.
7847 * @see ProtoBuf.Builder.Message
7848 * @param {boolean=} rebuild Whether to rebuild or not, defaults to false
7849 * @return {ProtoBuf.Reflect.Message} Message class
7850 * @throws {Error} If the message cannot be built
7851 * @expose
7852 */
7853 MessagePrototype.build = function(rebuild) {
7854 if (this.clazz && !rebuild)
7855 return this.clazz;
7856
7857 // Create the runtime Message class in its own scope
7858 var clazz = (function(ProtoBuf, T) {
7859
7860 var fields = T.getChildren(ProtoBuf.Reflect.Message.Field),
7861 oneofs = T.getChildren(ProtoBuf.Reflect.Message.OneOf);
7862
7863 /**
7864 * Constructs a new runtime Message.
7865 * @name ProtoBuf.Builder.Message
7866 * @class Barebone of all runtime messages.
7867 * @param {!Object.<string,*>|string} values Preset values
7868 * @param {...string} var_args
7869 * @constructor
7870 * @throws {Error} If the message cannot be created
7871 */
7872 var Message = function(values, var_args) {
7873 ProtoBuf.Builder.Message.call(this);
7874
7875 // Create virtual oneof properties
7876 for (var i=0, k=oneofs.length; i<k; ++i)
7877 this[oneofs[i].name] = null;
7878 // Create fields and set default values
7879 for (i=0, k=fields.length; i<k; ++i) {
7880 var field = fields[i];
7881 this[field.name] =
7882 field.repeated ? [] :
7883 (field.map ? new ProtoBuf.Map(field) : null);
7884 if ((field.required || T.syntax === 'proto3') &&
7885 field.defaultValue !== null)
7886 this[field.name] = field.defaultValue;
7887 }
7888
7889 if (arguments.length > 0) {
7890 var value;
7891 // Set field values from a values object
7892 if (arguments.length === 1 && values !== null && typeof values === 'object' &&
7893 /* not _another_ Message */ (typeof values.encode !== 'function' || values instanceof Message) &&
7894 /* not a repeated field */ !Array.isArray(values) &&
7895 /* not a Map */ !(values instanceof ProtoBuf.Map) &&
7896 /* not a ByteBuffer */ !ByteBuffer.isByteBuffer(values) &&
7897 /* not an ArrayBuffer */ !(values instanceof ArrayBuffer) &&
7898 /* not a Long */ !(ProtoBuf.Long && values instanceof ProtoBuf.Long)) {
7899 this.$set(values);
7900 } else // Set field values from arguments, in declaration order
7901 for (i=0, k=arguments.length; i<k; ++i)
7902 if (typeof (value = arguments[i]) !== 'undefined')
7903 this.$set(fields[i].name, value); // May throw
7904 }
7905 };
7906
7907 /**
7908 * @alias ProtoBuf.Builder.Message.prototype
7909 * @inner
7910 */
7911 var MessagePrototype = Message.prototype = Object.create(ProtoBuf.Builder.Message.prototype);
7912
7913 /**
7914 * Adds a value to a repeated field.
7915 * @name ProtoBuf.Builder.Message#add
7916 * @function
7917 * @param {string} key Field name
7918 * @param {*} value Value to add
7919 * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
7920 * @returns {!ProtoBuf.Builder.Message} this
7921 * @throws {Error} If the value cannot be added
7922 * @expose
7923 */
7924 MessagePrototype.add = function(key, value, noAssert) {
7925 var field = T._fieldsByName[key];
7926 if (!noAssert) {
7927 if (!field)
7928 throw Error(this+"#"+key+" is undefined");
7929 if (!(field instanceof ProtoBuf.Reflect.Message.Field))
7930 throw Error(this+"#"+key+" is not a field: "+field.toString(true)); // May throw if it's an enum or embedded message
7931 if (!field.repeated)
7932 throw Error(this+"#"+key+" is not a repeated field");
7933 value = field.verifyValue(value, true);
7934 }
7935 if (this[key] === null)
7936 this[key] = [];
7937 this[key].push(value);
7938 return this;
7939 };
7940
7941 /**
7942 * Adds a value to a repeated field. This is an alias for {@link ProtoBuf.Builder.Message#add}.
7943 * @name ProtoBuf.Builder.Message#$add
7944 * @function
7945 * @param {string} key Field name
7946 * @param {*} value Value to add
7947 * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
7948 * @returns {!ProtoBuf.Builder.Message} this
7949 * @throws {Error} If the value cannot be added
7950 * @expose
7951 */
7952 MessagePrototype.$add = MessagePrototype.add;
7953
7954 /**
7955 * Sets a field's value.
7956 * @name ProtoBuf.Builder.Message#set
7957 * @function
7958 * @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
7959 * @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
7960 * @param {boolean=} noAssert Whether to not assert for an actual field / proper value type, defaults to `false`
7961 * @returns {!ProtoBuf.Builder.Message} this
7962 * @throws {Error} If the value cannot be set
7963 * @expose
7964 */
7965 MessagePrototype.set = function(keyOrObj, value, noAssert) {
7966 if (keyOrObj && typeof keyOrObj === 'object') {
7967 noAssert = value;
7968 for (var ikey in keyOrObj) {
7969 // Check if virtual oneof field - don't set these
7970 if (keyOrObj.hasOwnProperty(ikey) && typeof (value = keyOrObj[ikey]) !== 'undefined' && T._oneofsByName[ikey] === undefined)
7971 this.$set(ikey, value, noAssert);
7972 }
7973 return this;
7974 }
7975 var field = T._fieldsByName[keyOrObj];
7976 if (!noAssert) {
7977 if (!field)
7978 throw Error(this+"#"+keyOrObj+" is not a field: undefined");
7979 if (!(field instanceof ProtoBuf.Reflect.Message.Field))
7980 throw Error(this+"#"+keyOrObj+" is not a field: "+field.toString(true));
7981 this[field.name] = (value = field.verifyValue(value)); // May throw
7982 } else
7983 this[keyOrObj] = value;
7984 if (field && field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
7985 var currentField = this[field.oneof.name]; // Virtual field references currently set field
7986 if (value !== null) {
7987 if (currentField !== null && currentField !== field.name)
7988 this[currentField] = null; // Clear currently set field
7989 this[field.oneof.name] = field.name; // Point virtual field at this field
7990 } else if (/* value === null && */currentField === keyOrObj)
7991 this[field.oneof.name] = null; // Clear virtual field (current field explicitly cleared)
7992 }
7993 return this;
7994 };
7995
7996 /**
7997 * Sets a field's value. This is an alias for [@link ProtoBuf.Builder.Message#set}.
7998 * @name ProtoBuf.Builder.Message#$set
7999 * @function
8000 * @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
8001 * @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
8002 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
8003 * @throws {Error} If the value cannot be set
8004 * @expose
8005 */
8006 MessagePrototype.$set = MessagePrototype.set;
8007
8008 /**
8009 * Gets a field's value.
8010 * @name ProtoBuf.Builder.Message#get
8011 * @function
8012 * @param {string} key Key
8013 * @param {boolean=} noAssert Whether to not assert for an actual field, defaults to `false`
8014 * @return {*} Value
8015 * @throws {Error} If there is no such field
8016 * @expose
8017 */
8018 MessagePrototype.get = function(key, noAssert) {
8019 if (noAssert)
8020 return this[key];
8021 var field = T._fieldsByName[key];
8022 if (!field || !(field instanceof ProtoBuf.Reflect.Message.Field))
8023 throw Error(this+"#"+key+" is not a field: undefined");
8024 if (!(field instanceof ProtoBuf.Reflect.Message.Field))
8025 throw Error(this+"#"+key+" is not a field: "+field.toString(true));
8026 return this[field.name];
8027 };
8028
8029 /**
8030 * Gets a field's value. This is an alias for {@link ProtoBuf.Builder.Message#$get}.
8031 * @name ProtoBuf.Builder.Message#$get
8032 * @function
8033 * @param {string} key Key
8034 * @return {*} Value
8035 * @throws {Error} If there is no such field
8036 * @expose
8037 */
8038 MessagePrototype.$get = MessagePrototype.get;
8039
8040 // Getters and setters
8041
8042 for (var i=0; i<fields.length; i++) {
8043 var field = fields[i];
8044 // no setters for extension fields as these are named by their fqn
8045 if (field instanceof ProtoBuf.Reflect.Message.ExtensionField)
8046 continue;
8047
8048 if (T.builder.options['populateAccessors'])
8049 (function(field) {
8050 // set/get[SomeValue]
8051 var Name = field.originalName.replace(/(_[a-zA-Z])/g, function(match) {
8052 return match.toUpperCase().replace('_','');
8053 });
8054 Name = Name.substring(0,1).toUpperCase() + Name.substring(1);
8055
8056 // set/get_[some_value] FIXME: Do we really need these?
8057 var name = field.originalName.replace(/([A-Z])/g, function(match) {
8058 return "_"+match;
8059 });
8060
8061 /**
8062 * The current field's unbound setter function.
8063 * @function
8064 * @param {*} value
8065 * @param {boolean=} noAssert
8066 * @returns {!ProtoBuf.Builder.Message}
8067 * @inner
8068 */
8069 var setter = function(value, noAssert) {
8070 this[field.name] = noAssert ? value : field.verifyValue(value);
8071 return this;
8072 };
8073
8074 /**
8075 * The current field's unbound getter function.
8076 * @function
8077 * @returns {*}
8078 * @inner
8079 */
8080 var getter = function() {
8081 return this[field.name];
8082 };
8083
8084 if (T.getChild("set"+Name) === null)
8085 /**
8086 * Sets a value. This method is present for each field, but only if there is no name conflict with
8087 * another field.
8088 * @name ProtoBuf.Builder.Message#set[SomeField]
8089 * @function
8090 * @param {*} value Value to set
8091 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
8092 * @returns {!ProtoBuf.Builder.Message} this
8093 * @abstract
8094 * @throws {Error} If the value cannot be set
8095 */
8096 MessagePrototype["set"+Name] = setter;
8097
8098 if (T.getChild("set_"+name) === null)
8099 /**
8100 * Sets a value. This method is present for each field, but only if there is no name conflict with
8101 * another field.
8102 * @name ProtoBuf.Builder.Message#set_[some_field]
8103 * @function
8104 * @param {*} value Value to set
8105 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
8106 * @returns {!ProtoBuf.Builder.Message} this
8107 * @abstract
8108 * @throws {Error} If the value cannot be set
8109 */
8110 MessagePrototype["set_"+name] = setter;
8111
8112 if (T.getChild("get"+Name) === null)
8113 /**
8114 * Gets a value. This method is present for each field, but only if there is no name conflict with
8115 * another field.
8116 * @name ProtoBuf.Builder.Message#get[SomeField]
8117 * @function
8118 * @abstract
8119 * @return {*} The value
8120 */
8121 MessagePrototype["get"+Name] = getter;
8122
8123 if (T.getChild("get_"+name) === null)
8124 /**
8125 * Gets a value. This method is present for each field, but only if there is no name conflict with
8126 * another field.
8127 * @name ProtoBuf.Builder.Message#get_[some_field]
8128 * @function
8129 * @return {*} The value
8130 * @abstract
8131 */
8132 MessagePrototype["get_"+name] = getter;
8133
8134 })(field);
8135 }
8136
8137 // En-/decoding
8138
8139 /**
8140 * Encodes the message.
8141 * @name ProtoBuf.Builder.Message#$encode
8142 * @function
8143 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
8144 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
8145 * @return {!ByteBuffer} Encoded message as a ByteBuffer
8146 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8147 * returns the encoded ByteBuffer in the `encoded` property on the error.
8148 * @expose
8149 * @see ProtoBuf.Builder.Message#encode64
8150 * @see ProtoBuf.Builder.Message#encodeHex
8151 * @see ProtoBuf.Builder.Message#encodeAB
8152 */
8153 MessagePrototype.encode = function(buffer, noVerify) {
8154 if (typeof buffer === 'boolean')
8155 noVerify = buffer,
8156 buffer = undefined;
8157 var isNew = false;
8158 if (!buffer)
8159 buffer = new ByteBuffer(),
8160 isNew = true;
8161 var le = buffer.littleEndian;
8162 try {
8163 T.encode(this, buffer.LE(), noVerify);
8164 return (isNew ? buffer.flip() : buffer).LE(le);
8165 } catch (e) {
8166 buffer.LE(le);
8167 throw(e);
8168 }
8169 };
8170
8171 /**
8172 * Encodes a message using the specified data payload.
8173 * @param {!Object.<string,*>} data Data payload
8174 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
8175 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
8176 * @return {!ByteBuffer} Encoded message as a ByteBuffer
8177 * @expose
8178 */
8179 Message.encode = function(data, buffer, noVerify) {
8180 return new Message(data).encode(buffer, noVerify);
8181 };
8182
8183 /**
8184 * Calculates the byte length of the message.
8185 * @name ProtoBuf.Builder.Message#calculate
8186 * @function
8187 * @returns {number} Byte length
8188 * @throws {Error} If the message cannot be calculated or if required fields are missing.
8189 * @expose
8190 */
8191 MessagePrototype.calculate = function() {
8192 return T.calculate(this);
8193 };
8194
8195 /**
8196 * Encodes the varint32 length-delimited message.
8197 * @name ProtoBuf.Builder.Message#encodeDelimited
8198 * @function
8199 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
8200 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
8201 * @return {!ByteBuffer} Encoded message as a ByteBuffer
8202 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8203 * returns the encoded ByteBuffer in the `encoded` property on the error.
8204 * @expose
8205 */
8206 MessagePrototype.encodeDelimited = function(buffer, noVerify) {
8207 var isNew = false;
8208 if (!buffer)
8209 buffer = new ByteBuffer(),
8210 isNew = true;
8211 var enc = new ByteBuffer().LE();
8212 T.encode(this, enc, noVerify).flip();
8213 buffer.writeVarint32(enc.remaining());
8214 buffer.append(enc);
8215 return isNew ? buffer.flip() : buffer;
8216 };
8217
8218 /**
8219 * Directly encodes the message to an ArrayBuffer.
8220 * @name ProtoBuf.Builder.Message#encodeAB
8221 * @function
8222 * @return {ArrayBuffer} Encoded message as ArrayBuffer
8223 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8224 * returns the encoded ArrayBuffer in the `encoded` property on the error.
8225 * @expose
8226 */
8227 MessagePrototype.encodeAB = function() {
8228 try {
8229 return this.encode().toArrayBuffer();
8230 } catch (e) {
8231 if (e["encoded"]) e["encoded"] = e["encoded"].toArrayBuffer();
8232 throw(e);
8233 }
8234 };
8235
8236 /**
8237 * Returns the message as an ArrayBuffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeAB}.
8238 * @name ProtoBuf.Builder.Message#toArrayBuffer
8239 * @function
8240 * @return {ArrayBuffer} Encoded message as ArrayBuffer
8241 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8242 * returns the encoded ArrayBuffer in the `encoded` property on the error.
8243 * @expose
8244 */
8245 MessagePrototype.toArrayBuffer = MessagePrototype.encodeAB;
8246
8247 /**
8248 * Directly encodes the message to a node Buffer.
8249 * @name ProtoBuf.Builder.Message#encodeNB
8250 * @function
8251 * @return {!Buffer}
8252 * @throws {Error} If the message cannot be encoded, not running under node.js or if required fields are
8253 * missing. The later still returns the encoded node Buffer in the `encoded` property on the error.
8254 * @expose
8255 */
8256 MessagePrototype.encodeNB = function() {
8257 try {
8258 return this.encode().toBuffer();
8259 } catch (e) {
8260 if (e["encoded"]) e["encoded"] = e["encoded"].toBuffer();
8261 throw(e);
8262 }
8263 };
8264
8265 /**
8266 * Returns the message as a node Buffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeNB}.
8267 * @name ProtoBuf.Builder.Message#toBuffer
8268 * @function
8269 * @return {!Buffer}
8270 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8271 * returns the encoded node Buffer in the `encoded` property on the error.
8272 * @expose
8273 */
8274 MessagePrototype.toBuffer = MessagePrototype.encodeNB;
8275
8276 /**
8277 * Directly encodes the message to a base64 encoded string.
8278 * @name ProtoBuf.Builder.Message#encode64
8279 * @function
8280 * @return {string} Base64 encoded string
8281 * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
8282 * still returns the encoded base64 string in the `encoded` property on the error.
8283 * @expose
8284 */
8285 MessagePrototype.encode64 = function() {
8286 try {
8287 return this.encode().toBase64();
8288 } catch (e) {
8289 if (e["encoded"]) e["encoded"] = e["encoded"].toBase64();
8290 throw(e);
8291 }
8292 };
8293
8294 /**
8295 * Returns the message as a base64 encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encode64}.
8296 * @name ProtoBuf.Builder.Message#toBase64
8297 * @function
8298 * @return {string} Base64 encoded string
8299 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8300 * returns the encoded base64 string in the `encoded` property on the error.
8301 * @expose
8302 */
8303 MessagePrototype.toBase64 = MessagePrototype.encode64;
8304
8305 /**
8306 * Directly encodes the message to a hex encoded string.
8307 * @name ProtoBuf.Builder.Message#encodeHex
8308 * @function
8309 * @return {string} Hex encoded string
8310 * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
8311 * still returns the encoded hex string in the `encoded` property on the error.
8312 * @expose
8313 */
8314 MessagePrototype.encodeHex = function() {
8315 try {
8316 return this.encode().toHex();
8317 } catch (e) {
8318 if (e["encoded"]) e["encoded"] = e["encoded"].toHex();
8319 throw(e);
8320 }
8321 };
8322
8323 /**
8324 * Returns the message as a hex encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encodeHex}.
8325 * @name ProtoBuf.Builder.Message#toHex
8326 * @function
8327 * @return {string} Hex encoded string
8328 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
8329 * returns the encoded hex string in the `encoded` property on the error.
8330 * @expose
8331 */
8332 MessagePrototype.toHex = MessagePrototype.encodeHex;
8333
8334 /**
8335 * Clones a message object or field value to a raw object.
8336 * @param {*} obj Object to clone
8337 * @param {boolean} binaryAsBase64 Whether to include binary data as base64 strings or as a buffer otherwise
8338 * @param {boolean} longsAsStrings Whether to encode longs as strings
8339 * @param {!ProtoBuf.Reflect.T=} resolvedType The resolved field type if a field
8340 * @returns {*} Cloned object
8341 * @inner
8342 */
8343 function cloneRaw(obj, binaryAsBase64, longsAsStrings, resolvedType) {
8344 if (obj === null || typeof obj !== 'object') {
8345 // Convert enum values to their respective names
8346 if (resolvedType && resolvedType instanceof ProtoBuf.Reflect.Enum) {
8347 var name = ProtoBuf.Reflect.Enum.getName(resolvedType.object, obj);
8348 if (name !== null)
8349 return name;
8350 }
8351 // Pass-through string, number, boolean, null...
8352 return obj;
8353 }
8354 // Convert ByteBuffers to raw buffer or strings
8355 if (ByteBuffer.isByteBuffer(obj))
8356 return binaryAsBase64 ? obj.toBase64() : obj.toBuffer();
8357 // Convert Longs to proper objects or strings
8358 if (ProtoBuf.Long.isLong(obj))
8359 return longsAsStrings ? obj.toString() : ProtoBuf.Long.fromValue(obj);
8360 var clone;
8361 // Clone arrays
8362 if (Array.isArray(obj)) {
8363 clone = [];
8364 obj.forEach(function(v, k) {
8365 clone[k] = cloneRaw(v, binaryAsBase64, longsAsStrings, resolvedType);
8366 });
8367 return clone;
8368 }
8369 clone = {};
8370 // Convert maps to objects
8371 if (obj instanceof ProtoBuf.Map) {
8372 var it = obj.entries();
8373 for (var e = it.next(); !e.done; e = it.next())
8374 clone[obj.keyElem.valueToString(e.value[0])] = cloneRaw(e.value[1], binaryAsBase64, longsAsStrings, obj.valueElem.resolvedType);
8375 return clone;
8376 }
8377 // Everything else is a non-null object
8378 var type = obj.$type,
8379 field = undefined;
8380 for (var i in obj)
8381 if (obj.hasOwnProperty(i)) {
8382 if (type && (field = type.getChild(i)))
8383 clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings, field.resolvedType);
8384 else
8385 clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings);
8386 }
8387 return clone;
8388 }
8389
8390 /**
8391 * Returns the message's raw payload.
8392 * @param {boolean=} binaryAsBase64 Whether to include binary data as base64 strings instead of Buffers, defaults to `false`
8393 * @param {boolean} longsAsStrings Whether to encode longs as strings
8394 * @returns {Object.<string,*>} Raw payload
8395 * @expose
8396 */
8397 MessagePrototype.toRaw = function(binaryAsBase64, longsAsStrings) {
8398 return cloneRaw(this, !!binaryAsBase64, !!longsAsStrings, this.$type);
8399 };
8400
8401 /**
8402 * Encodes a message to JSON.
8403 * @returns {string} JSON string
8404 * @expose
8405 */
8406 MessagePrototype.encodeJSON = function() {
8407 return JSON.stringify(
8408 cloneRaw(this,
8409 /* binary-as-base64 */ true,
8410 /* longs-as-strings */ true,
8411 this.$type
8412 )
8413 );
8414 };
8415
8416 /**
8417 * Decodes a message from the specified buffer or string.
8418 * @name ProtoBuf.Builder.Message.decode
8419 * @function
8420 * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
8421 * @param {(number|string)=} length Message length. Defaults to decode all the remainig data.
8422 * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
8423 * @return {!ProtoBuf.Builder.Message} Decoded message
8424 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
8425 * returns the decoded message with missing fields in the `decoded` property on the error.
8426 * @expose
8427 * @see ProtoBuf.Builder.Message.decode64
8428 * @see ProtoBuf.Builder.Message.decodeHex
8429 */
8430 Message.decode = function(buffer, length, enc) {
8431 if (typeof length === 'string')
8432 enc = length,
8433 length = -1;
8434 if (typeof buffer === 'string')
8435 buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
8436 else if (!ByteBuffer.isByteBuffer(buffer))
8437 buffer = ByteBuffer.wrap(buffer); // May throw
8438 var le = buffer.littleEndian;
8439 try {
8440 var msg = T.decode(buffer.LE(), length);
8441 buffer.LE(le);
8442 return msg;
8443 } catch (e) {
8444 buffer.LE(le);
8445 throw(e);
8446 }
8447 };
8448
8449 /**
8450 * Decodes a varint32 length-delimited message from the specified buffer or string.
8451 * @name ProtoBuf.Builder.Message.decodeDelimited
8452 * @function
8453 * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
8454 * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
8455 * @return {ProtoBuf.Builder.Message} Decoded message or `null` if not enough bytes are available yet
8456 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
8457 * returns the decoded message with missing fields in the `decoded` property on the error.
8458 * @expose
8459 */
8460 Message.decodeDelimited = function(buffer, enc) {
8461 if (typeof buffer === 'string')
8462 buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
8463 else if (!ByteBuffer.isByteBuffer(buffer))
8464 buffer = ByteBuffer.wrap(buffer); // May throw
8465 if (buffer.remaining() < 1)
8466 return null;
8467 var off = buffer.offset,
8468 len = buffer.readVarint32();
8469 if (buffer.remaining() < len) {
8470 buffer.offset = off;
8471 return null;
8472 }
8473 try {
8474 var msg = T.decode(buffer.slice(buffer.offset, buffer.offset + len).LE());
8475 buffer.offset += len;
8476 return msg;
8477 } catch (err) {
8478 buffer.offset += len;
8479 throw err;
8480 }
8481 };
8482
8483 /**
8484 * Decodes the message from the specified base64 encoded string.
8485 * @name ProtoBuf.Builder.Message.decode64
8486 * @function
8487 * @param {string} str String to decode from
8488 * @return {!ProtoBuf.Builder.Message} Decoded message
8489 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
8490 * returns the decoded message with missing fields in the `decoded` property on the error.
8491 * @expose
8492 */
8493 Message.decode64 = function(str) {
8494 return Message.decode(str, "base64");
8495 };
8496
8497 /**
8498 * Decodes the message from the specified hex encoded string.
8499 * @name ProtoBuf.Builder.Message.decodeHex
8500 * @function
8501 * @param {string} str String to decode from
8502 * @return {!ProtoBuf.Builder.Message} Decoded message
8503 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
8504 * returns the decoded message with missing fields in the `decoded` property on the error.
8505 * @expose
8506 */
8507 Message.decodeHex = function(str) {
8508 return Message.decode(str, "hex");
8509 };
8510
8511 /**
8512 * Decodes the message from a JSON string.
8513 * @name ProtoBuf.Builder.Message.decodeJSON
8514 * @function
8515 * @param {string} str String to decode from
8516 * @return {!ProtoBuf.Builder.Message} Decoded message
8517 * @throws {Error} If the message cannot be decoded or if required fields are
8518 * missing.
8519 * @expose
8520 */
8521 Message.decodeJSON = function(str) {
8522 return new Message(JSON.parse(str));
8523 };
8524
8525 // Utility
8526
8527 /**
8528 * Returns a string representation of this Message.
8529 * @name ProtoBuf.Builder.Message#toString
8530 * @function
8531 * @return {string} String representation as of ".Fully.Qualified.MessageName"
8532 * @expose
8533 */
8534 MessagePrototype.toString = function() {
8535 return T.toString();
8536 };
8537
8538 if (Object.defineProperty)
8539 Object.defineProperty(Message, '$options', { "value": T.buildOpt() }),
8540 Object.defineProperty(MessagePrototype, "$options", { "value": Message["$options"] }),
8541 Object.defineProperty(Message, "$type", { "value": T }),
8542 Object.defineProperty(MessagePrototype, "$type", { "value": T });
8543
8544 return Message;
8545
8546 })(ProtoBuf, this);
8547
8548 // Static enums and prototyped sub-messages / cached collections
8549 this._fields = [];
8550 this._fieldsById = {};
8551 this._fieldsByName = {};
8552 this._oneofsByName = {};
8553 for (var i=0, k=this.children.length, child; i<k; i++) {
8554 child = this.children[i];
8555 if (child instanceof Enum || child instanceof Message || child instanceof Service) {
8556 if (clazz.hasOwnProperty(child.name))
8557 throw Error("Illegal reflect child of "+this.toString(true)+": "+child.toString(true)+" cannot override static property '"+child.name+"'");
8558 clazz[child.name] = child.build();
8559 } else if (child instanceof Message.Field)
8560 child.build(),
8561 this._fields.push(child),
8562 this._fieldsById[child.id] = child,
8563 this._fieldsByName[child.name] = child;
8564 else if (child instanceof Message.OneOf) {
8565 this._oneofsByName[child.name] = child;
8566 }
8567 else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
8568 throw Error("Illegal reflect child of "+this.toString(true)+": "+this.children[i].toString(true));
8569 }
8570
8571 return this.clazz = clazz;
8572 };
8573
8574 /**
8575 * Encodes a runtime message's contents to the specified buffer.
8576 * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
8577 * @param {ByteBuffer} buffer ByteBuffer to write to
8578 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
8579 * @return {ByteBuffer} The ByteBuffer for chaining
8580 * @throws {Error} If required fields are missing or the message cannot be encoded for another reason
8581 * @expose
8582 */
8583 MessagePrototype.encode = function(message, buffer, noVerify) {
8584 var fieldMissing = null,
8585 field;
8586 for (var i=0, k=this._fields.length, val; i<k; ++i) {
8587 field = this._fields[i];
8588 val = message[field.name];
8589 if (field.required && val === null) {
8590 if (fieldMissing === null)
8591 fieldMissing = field;
8592 } else
8593 field.encode(noVerify ? val : field.verifyValue(val), buffer, message);
8594 }
8595 if (fieldMissing !== null) {
8596 var err = Error("Missing at least one required field for "+this.toString(true)+": "+fieldMissing);
8597 err["encoded"] = buffer; // Still expose what we got
8598 throw(err);
8599 }
8600 return buffer;
8601 };
8602
8603 /**
8604 * Calculates a runtime message's byte length.
8605 * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
8606 * @returns {number} Byte length
8607 * @throws {Error} If required fields are missing or the message cannot be calculated for another reason
8608 * @expose
8609 */
8610 MessagePrototype.calculate = function(message) {
8611 for (var n=0, i=0, k=this._fields.length, field, val; i<k; ++i) {
8612 field = this._fields[i];
8613 val = message[field.name];
8614 if (field.required && val === null)
8615 throw Error("Missing at least one required field for "+this.toString(true)+": "+field);
8616 else
8617 n += field.calculate(val, message);
8618 }
8619 return n;
8620 };
8621
8622 /**
8623 * Skips all data until the end of the specified group has been reached.
8624 * @param {number} expectedId Expected GROUPEND id
8625 * @param {!ByteBuffer} buf ByteBuffer
8626 * @returns {boolean} `true` if a value as been skipped, `false` if the end has been reached
8627 * @throws {Error} If it wasn't possible to find the end of the group (buffer overrun or end tag mismatch)
8628 * @inner
8629 */
8630 function skipTillGroupEnd(expectedId, buf) {
8631 var tag = buf.readVarint32(), // Throws on OOB
8632 wireType = tag & 0x07,
8633 id = tag >>> 3;
8634 switch (wireType) {
8635 case ProtoBuf.WIRE_TYPES.VARINT:
8636 do tag = buf.readUint8();
8637 while ((tag & 0x80) === 0x80);
8638 break;
8639 case ProtoBuf.WIRE_TYPES.BITS64:
8640 buf.offset += 8;
8641 break;
8642 case ProtoBuf.WIRE_TYPES.LDELIM:
8643 tag = buf.readVarint32(); // reads the varint
8644 buf.offset += tag; // skips n bytes
8645 break;
8646 case ProtoBuf.WIRE_TYPES.STARTGROUP:
8647 skipTillGroupEnd(id, buf);
8648 break;
8649 case ProtoBuf.WIRE_TYPES.ENDGROUP:
8650 if (id === expectedId)
8651 return false;
8652 else
8653 throw Error("Illegal GROUPEND after unknown group: "+id+" ("+expectedId+" expected)");
8654 case ProtoBuf.WIRE_TYPES.BITS32:
8655 buf.offset += 4;
8656 break;
8657 default:
8658 throw Error("Illegal wire type in unknown group "+expectedId+": "+wireType);
8659 }
8660 return true;
8661 }
8662
8663 /**
8664 * Decodes an encoded message and returns the decoded message.
8665 * @param {ByteBuffer} buffer ByteBuffer to decode from
8666 * @param {number=} length Message length. Defaults to decode all remaining data.
8667 * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
8668 * @return {ProtoBuf.Builder.Message} Decoded message
8669 * @throws {Error} If the message cannot be decoded
8670 * @expose
8671 */
8672 MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
8673 if (typeof length !== 'number')
8674 length = -1;
8675 var start = buffer.offset,
8676 msg = new (this.clazz)(),
8677 tag, wireType, id, field;
8678 while (buffer.offset < start+length || (length === -1 && buffer.remaining() > 0)) {
8679 tag = buffer.readVarint32();
8680 wireType = tag & 0x07;
8681 id = tag >>> 3;
8682 if (wireType === ProtoBuf.WIRE_TYPES.ENDGROUP) {
8683 if (id !== expectedGroupEndId)
8684 throw Error("Illegal group end indicator for "+this.toString(true)+": "+id+" ("+(expectedGroupEndId ? expectedGroupEndId+" expected" : "not a group")+")");
8685 break;
8686 }
8687 if (!(field = this._fieldsById[id])) {
8688 // "messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing."
8689 switch (wireType) {
8690 case ProtoBuf.WIRE_TYPES.VARINT:
8691 buffer.readVarint32();
8692 break;
8693 case ProtoBuf.WIRE_TYPES.BITS32:
8694 buffer.offset += 4;
8695 break;
8696 case ProtoBuf.WIRE_TYPES.BITS64:
8697 buffer.offset += 8;
8698 break;
8699 case ProtoBuf.WIRE_TYPES.LDELIM:
8700 var len = buffer.readVarint32();
8701 buffer.offset += len;
8702 break;
8703 case ProtoBuf.WIRE_TYPES.STARTGROUP:
8704 while (skipTillGroupEnd(id, buffer)) {}
8705 break;
8706 default:
8707 throw Error("Illegal wire type for unknown field "+id+" in "+this.toString(true)+"#decode: "+wireType);
8708 }
8709 continue;
8710 }
8711 if (field.repeated && !field.options["packed"]) {
8712 msg[field.name].push(field.decode(wireType, buffer));
8713 } else if (field.map) {
8714 var keyval = field.decode(wireType, buffer);
8715 msg[field.name].set(keyval[0], keyval[1]);
8716 } else {
8717 msg[field.name] = field.decode(wireType, buffer);
8718 if (field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
8719 var currentField = msg[field.oneof.name]; // Virtual field references currently set field
8720 if (currentField !== null && currentField !== field.name)
8721 msg[currentField] = null; // Clear currently set field
8722 msg[field.oneof.name] = field.name; // Point virtual field at this field
8723 }
8724 }
8725 }
8726
8727 // Check if all required fields are present and set default values for optional fields that are not
8728 for (var i=0, k=this._fields.length; i<k; ++i) {
8729 field = this._fields[i];
8730 if (msg[field.name] === null) {
8731 if (this.syntax === "proto3") { // Proto3 sets default values by specification
8732 msg[field.name] = field.defaultValue;
8733 } else if (field.required) {
8734 var err = Error("Missing at least one required field for " + this.toString(true) + ": " + field.name);
8735 err["decoded"] = msg; // Still expose what we got
8736 throw(err);
8737 } else if (ProtoBuf.populateDefaults && field.defaultValue !== null)
8738 msg[field.name] = field.defaultValue;
8739 }
8740 }
8741 return msg;
8742 };
8743
8744 /**
8745 * @alias ProtoBuf.Reflect.Message
8746 * @expose
8747 */
8748 Reflect.Message = Message;
8749
8750 /**
8751 * Constructs a new Message Field.
8752 * @exports ProtoBuf.Reflect.Message.Field
8753 * @param {!ProtoBuf.Builder} builder Builder reference
8754 * @param {!ProtoBuf.Reflect.Message} message Message reference
8755 * @param {string} rule Rule, one of requried, optional, repeated
8756 * @param {string?} keytype Key data type, if any.
8757 * @param {string} type Data type, e.g. int32
8758 * @param {string} name Field name
8759 * @param {number} id Unique field id
8760 * @param {Object.<string,*>=} options Options
8761 * @param {!ProtoBuf.Reflect.Message.OneOf=} oneof Enclosing OneOf
8762 * @param {string?} syntax The syntax level of this definition (e.g., proto3)
8763 * @constructor
8764 * @extends ProtoBuf.Reflect.T
8765 */
8766 var Field = function(builder, message, rule, keytype, type, name, id, options, oneof, syntax) {
8767 T.call(this, builder, message, name);
8768
8769 /**
8770 * @override
8771 */
8772 this.className = "Message.Field";
8773
8774 /**
8775 * Message field required flag.
8776 * @type {boolean}
8777 * @expose
8778 */
8779 this.required = rule === "required";
8780
8781 /**
8782 * Message field repeated flag.
8783 * @type {boolean}
8784 * @expose
8785 */
8786 this.repeated = rule === "repeated";
8787
8788 /**
8789 * Message field map flag.
8790 * @type {boolean}
8791 * @expose
8792 */
8793 this.map = rule === "map";
8794
8795 /**
8796 * Message field key type. Type reference string if unresolved, protobuf
8797 * type if resolved. Valid only if this.map === true, null otherwise.
8798 * @type {string|{name: string, wireType: number}|null}
8799 * @expose
8800 */
8801 this.keyType = keytype || null;
8802
8803 /**
8804 * Message field type. Type reference string if unresolved, protobuf type if
8805 * resolved. In a map field, this is the value type.
8806 * @type {string|{name: string, wireType: number}}
8807 * @expose
8808 */
8809 this.type = type;
8810
8811 /**
8812 * Resolved type reference inside the global namespace.
8813 * @type {ProtoBuf.Reflect.T|null}
8814 * @expose
8815 */
8816 this.resolvedType = null;
8817
8818 /**
8819 * Unique message field id.
8820 * @type {number}
8821 * @expose
8822 */
8823 this.id = id;
8824
8825 /**
8826 * Message field options.
8827 * @type {!Object.<string,*>}
8828 * @dict
8829 * @expose
8830 */
8831 this.options = options || {};
8832
8833 /**
8834 * Default value.
8835 * @type {*}
8836 * @expose
8837 */
8838 this.defaultValue = null;
8839
8840 /**
8841 * Enclosing OneOf.
8842 * @type {?ProtoBuf.Reflect.Message.OneOf}
8843 * @expose
8844 */
8845 this.oneof = oneof || null;
8846
8847 /**
8848 * Syntax level of this definition (e.g., proto3).
8849 * @type {string}
8850 * @expose
8851 */
8852 this.syntax = syntax || 'proto2';
8853
8854 /**
8855 * Original field name.
8856 * @type {string}
8857 * @expose
8858 */
8859 this.originalName = this.name; // Used to revert camelcase transformation on naming collisions
8860
8861 /**
8862 * Element implementation. Created in build() after types are resolved.
8863 * @type {ProtoBuf.Element}
8864 * @expose
8865 */
8866 this.element = null;
8867
8868 /**
8869 * Key element implementation, for map fields. Created in build() after
8870 * types are resolved.
8871 * @type {ProtoBuf.Element}
8872 * @expose
8873 */
8874 this.keyElement = null;
8875
8876 // Convert field names to camel case notation if the override is set
8877 if (this.builder.options['convertFieldsToCamelCase'] && !(this instanceof Message.ExtensionField))
8878 this.name = ProtoBuf.Util.toCamelCase(this.name);
8879 };
8880
8881 /**
8882 * @alias ProtoBuf.Reflect.Message.Field.prototype
8883 * @inner
8884 */
8885 var FieldPrototype = Field.prototype = Object.create(T.prototype);
8886
8887 /**
8888 * Builds the field.
8889 * @override
8890 * @expose
8891 */
8892 FieldPrototype.build = function() {
8893 this.element = new Element(this.type, this.resolvedType, false, this.syntax, this.name);
8894 if (this.map)
8895 this.keyElement = new Element(this.keyType, undefined, true, this.syntax, this.name);
8896
8897 // In proto3, fields do not have field presence, and every field is set to
8898 // its type's default value ("", 0, 0.0, or false).
8899 if (this.syntax === 'proto3' && !this.repeated && !this.map)
8900 this.defaultValue = Element.defaultFieldValue(this.type);
8901
8902 // Otherwise, default values are present when explicitly specified
8903 else if (typeof this.options['default'] !== 'undefined')
8904 this.defaultValue = this.verifyValue(this.options['default']);
8905 };
8906
8907 /**
8908 * Checks if the given value can be set for this field.
8909 * @param {*} value Value to check
8910 * @param {boolean=} skipRepeated Whether to skip the repeated value check or not. Defaults to false.
8911 * @return {*} Verified, maybe adjusted, value
8912 * @throws {Error} If the value cannot be set for this field
8913 * @expose
8914 */
8915 FieldPrototype.verifyValue = function(value, skipRepeated) {
8916 skipRepeated = skipRepeated || false;
8917 var self = this;
8918 function fail(val, msg) {
8919 throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
8920 }
8921 if (value === null) { // NULL values for optional fields
8922 if (this.required)
8923 fail(typeof value, "required");
8924 if (this.syntax === 'proto3' && this.type !== ProtoBuf.TYPES["message"])
8925 fail(typeof value, "proto3 field without field presence cannot be null");
8926 return null;
8927 }
8928 var i;
8929 if (this.repeated && !skipRepeated) { // Repeated values as arrays
8930 if (!Array.isArray(value))
8931 value = [value];
8932 var res = [];
8933 for (i=0; i<value.length; i++)
8934 res.push(this.element.verifyValue(value[i]));
8935 return res;
8936 }
8937 if (this.map && !skipRepeated) { // Map values as objects
8938 if (!(value instanceof ProtoBuf.Map)) {
8939 // If not already a Map, attempt to convert.
8940 if (!(value instanceof Object)) {
8941 fail(typeof value,
8942 "expected ProtoBuf.Map or raw object for map field");
8943 }
8944 return new ProtoBuf.Map(this, value);
8945 } else {
8946 return value;
8947 }
8948 }
8949 // All non-repeated fields expect no array
8950 if (!this.repeated && Array.isArray(value))
8951 fail(typeof value, "no array expected");
8952
8953 return this.element.verifyValue(value);
8954 };
8955
8956 /**
8957 * Determines whether the field will have a presence on the wire given its
8958 * value.
8959 * @param {*} value Verified field value
8960 * @param {!ProtoBuf.Builder.Message} message Runtime message
8961 * @return {boolean} Whether the field will be present on the wire
8962 */
8963 FieldPrototype.hasWirePresence = function(value, message) {
8964 if (this.syntax !== 'proto3')
8965 return (value !== null);
8966 if (this.oneof && message[this.oneof.name] === this.name)
8967 return true;
8968 switch (this.type) {
8969 case ProtoBuf.TYPES["int32"]:
8970 case ProtoBuf.TYPES["sint32"]:
8971 case ProtoBuf.TYPES["sfixed32"]:
8972 case ProtoBuf.TYPES["uint32"]:
8973 case ProtoBuf.TYPES["fixed32"]:
8974 return value !== 0;
8975
8976 case ProtoBuf.TYPES["int64"]:
8977 case ProtoBuf.TYPES["sint64"]:
8978 case ProtoBuf.TYPES["sfixed64"]:
8979 case ProtoBuf.TYPES["uint64"]:
8980 case ProtoBuf.TYPES["fixed64"]:
8981 return value.low !== 0 || value.high !== 0;
8982
8983 case ProtoBuf.TYPES["bool"]:
8984 return value;
8985
8986 case ProtoBuf.TYPES["float"]:
8987 case ProtoBuf.TYPES["double"]:
8988 return value !== 0.0;
8989
8990 case ProtoBuf.TYPES["string"]:
8991 return value.length > 0;
8992
8993 case ProtoBuf.TYPES["bytes"]:
8994 return value.remaining() > 0;
8995
8996 case ProtoBuf.TYPES["enum"]:
8997 return value !== 0;
8998
8999 case ProtoBuf.TYPES["message"]:
9000 return value !== null;
9001 default:
9002 return true;
9003 }
9004 };
9005
9006 /**
9007 * Encodes the specified field value to the specified buffer.
9008 * @param {*} value Verified field value
9009 * @param {ByteBuffer} buffer ByteBuffer to encode to
9010 * @param {!ProtoBuf.Builder.Message} message Runtime message
9011 * @return {ByteBuffer} The ByteBuffer for chaining
9012 * @throws {Error} If the field cannot be encoded
9013 * @expose
9014 */
9015 FieldPrototype.encode = function(value, buffer, message) {
9016 if (this.type === null || typeof this.type !== 'object')
9017 throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
9018 if (value === null || (this.repeated && value.length == 0))
9019 return buffer; // Optional omitted
9020 try {
9021 if (this.repeated) {
9022 var i;
9023 // "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire
9024 // types) can be declared 'packed'."
9025 if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
9026 // "All of the elements of the field are packed into a single key-value pair with wire type 2
9027 // (length-delimited). Each element is encoded the same way it would be normally, except without a
9028 // tag preceding it."
9029 buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
9030 buffer.ensureCapacity(buffer.offset += 1); // We do not know the length yet, so let's assume a varint of length 1
9031 var start = buffer.offset; // Remember where the contents begin
9032 for (i=0; i<value.length; i++)
9033 this.element.encodeValue(this.id, value[i], buffer);
9034 var len = buffer.offset-start,
9035 varintLen = ByteBuffer.calculateVarint32(len);
9036 if (varintLen > 1) { // We need to move the contents
9037 var contents = buffer.slice(start, buffer.offset);
9038 start += varintLen-1;
9039 buffer.offset = start;
9040 buffer.append(contents);
9041 }
9042 buffer.writeVarint32(len, start-varintLen);
9043 } else {
9044 // "If your message definition has repeated elements (without the [packed=true] option), the encoded
9045 // message has zero or more key-value pairs with the same tag number"
9046 for (i=0; i<value.length; i++)
9047 buffer.writeVarint32((this.id << 3) | this.type.wireType),
9048 this.element.encodeValue(this.id, value[i], buffer);
9049 }
9050 } else if (this.map) {
9051 // Write out each map entry as a submessage.
9052 value.forEach(function(val, key, m) {
9053 // Compute the length of the submessage (key, val) pair.
9054 var length =
9055 ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
9056 this.keyElement.calculateLength(1, key) +
9057 ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
9058 this.element.calculateLength(2, val);
9059
9060 // Submessage with wire type of length-delimited.
9061 buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
9062 buffer.writeVarint32(length);
9063
9064 // Write out the key and val.
9065 buffer.writeVarint32((1 << 3) | this.keyType.wireType);
9066 this.keyElement.encodeValue(1, key, buffer);
9067 buffer.writeVarint32((2 << 3) | this.type.wireType);
9068 this.element.encodeValue(2, val, buffer);
9069 }, this);
9070 } else {
9071 if (this.hasWirePresence(value, message)) {
9072 buffer.writeVarint32((this.id << 3) | this.type.wireType);
9073 this.element.encodeValue(this.id, value, buffer);
9074 }
9075 }
9076 } catch (e) {
9077 throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
9078 }
9079 return buffer;
9080 };
9081
9082 /**
9083 * Calculates the length of this field's value on the network level.
9084 * @param {*} value Field value
9085 * @param {!ProtoBuf.Builder.Message} message Runtime message
9086 * @returns {number} Byte length
9087 * @expose
9088 */
9089 FieldPrototype.calculate = function(value, message) {
9090 value = this.verifyValue(value); // May throw
9091 if (this.type === null || typeof this.type !== 'object')
9092 throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
9093 if (value === null || (this.repeated && value.length == 0))
9094 return 0; // Optional omitted
9095 var n = 0;
9096 try {
9097 if (this.repeated) {
9098 var i, ni;
9099 if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
9100 n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
9101 ni = 0;
9102 for (i=0; i<value.length; i++)
9103 ni += this.element.calculateLength(this.id, value[i]);
9104 n += ByteBuffer.calculateVarint32(ni);
9105 n += ni;
9106 } else {
9107 for (i=0; i<value.length; i++)
9108 n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType),
9109 n += this.element.calculateLength(this.id, value[i]);
9110 }
9111 } else if (this.map) {
9112 // Each map entry becomes a submessage.
9113 value.forEach(function(val, key, m) {
9114 // Compute the length of the submessage (key, val) pair.
9115 var length =
9116 ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
9117 this.keyElement.calculateLength(1, key) +
9118 ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
9119 this.element.calculateLength(2, val);
9120
9121 n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
9122 n += ByteBuffer.calculateVarint32(length);
9123 n += length;
9124 }, this);
9125 } else {
9126 if (this.hasWirePresence(value, message)) {
9127 n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType);
9128 n += this.element.calculateLength(this.id, value);
9129 }
9130 }
9131 } catch (e) {
9132 throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
9133 }
9134 return n;
9135 };
9136
9137 /**
9138 * Decode the field value from the specified buffer.
9139 * @param {number} wireType Leading wire type
9140 * @param {ByteBuffer} buffer ByteBuffer to decode from
9141 * @param {boolean=} skipRepeated Whether to skip the repeated check or not. Defaults to false.
9142 * @return {*} Decoded value: array for packed repeated fields, [key, value] for
9143 * map fields, or an individual value otherwise.
9144 * @throws {Error} If the field cannot be decoded
9145 * @expose
9146 */
9147 FieldPrototype.decode = function(wireType, buffer, skipRepeated) {
9148 var value, nBytes;
9149
9150 // We expect wireType to match the underlying type's wireType unless we see
9151 // a packed repeated field, or unless this is a map field.
9152 var wireTypeOK =
9153 (!this.map && wireType == this.type.wireType) ||
9154 (!skipRepeated && this.repeated && this.options["packed"] &&
9155 wireType == ProtoBuf.WIRE_TYPES.LDELIM) ||
9156 (this.map && wireType == ProtoBuf.WIRE_TYPES.LDELIM);
9157 if (!wireTypeOK)
9158 throw Error("Illegal wire type for field "+this.toString(true)+": "+wireType+" ("+this.type.wireType+" expected)");
9159
9160 // Handle packed repeated fields.
9161 if (wireType == ProtoBuf.WIRE_TYPES.LDELIM && this.repeated && this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
9162 if (!skipRepeated) {
9163 nBytes = buffer.readVarint32();
9164 nBytes = buffer.offset + nBytes; // Limit
9165 var values = [];
9166 while (buffer.offset < nBytes)
9167 values.push(this.decode(this.type.wireType, buffer, true));
9168 return values;
9169 }
9170 // Read the next value otherwise...
9171 }
9172
9173 // Handle maps.
9174 if (this.map) {
9175 // Read one (key, value) submessage, and return [key, value]
9176 var key = Element.defaultFieldValue(this.keyType);
9177 value = Element.defaultFieldValue(this.type);
9178
9179 // Read the length
9180 nBytes = buffer.readVarint32();
9181 if (buffer.remaining() < nBytes)
9182 throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
9183
9184 // Get a sub-buffer of this key/value submessage
9185 var msgbuf = buffer.clone();
9186 msgbuf.limit = msgbuf.offset + nBytes;
9187 buffer.offset += nBytes;
9188
9189 while (msgbuf.remaining() > 0) {
9190 var tag = msgbuf.readVarint32();
9191 wireType = tag & 0x07;
9192 var id = tag >>> 3;
9193 if (id === 1) {
9194 key = this.keyElement.decode(msgbuf, wireType, id);
9195 } else if (id === 2) {
9196 value = this.element.decode(msgbuf, wireType, id);
9197 } else {
9198 throw Error("Unexpected tag in map field key/value submessage");
9199 }
9200 }
9201
9202 return [key, value];
9203 }
9204
9205 // Handle singular and non-packed repeated field values.
9206 return this.element.decode(buffer, wireType, this.id);
9207 };
9208
9209 /**
9210 * @alias ProtoBuf.Reflect.Message.Field
9211 * @expose
9212 */
9213 Reflect.Message.Field = Field;
9214
9215 /**
9216 * Constructs a new Message ExtensionField.
9217 * @exports ProtoBuf.Reflect.Message.ExtensionField
9218 * @param {!ProtoBuf.Builder} builder Builder reference
9219 * @param {!ProtoBuf.Reflect.Message} message Message reference
9220 * @param {string} rule Rule, one of requried, optional, repeated
9221 * @param {string} type Data type, e.g. int32
9222 * @param {string} name Field name
9223 * @param {number} id Unique field id
9224 * @param {!Object.<string,*>=} options Options
9225 * @constructor
9226 * @extends ProtoBuf.Reflect.Message.Field
9227 */
9228 var ExtensionField = function(builder, message, rule, type, name, id, options) {
9229 Field.call(this, builder, message, rule, /* keytype = */ null, type, name, id, options);
9230
9231 /**
9232 * Extension reference.
9233 * @type {!ProtoBuf.Reflect.Extension}
9234 * @expose
9235 */
9236 this.extension;
9237 };
9238
9239 // Extends Field
9240 ExtensionField.prototype = Object.create(Field.prototype);
9241
9242 /**
9243 * @alias ProtoBuf.Reflect.Message.ExtensionField
9244 * @expose
9245 */
9246 Reflect.Message.ExtensionField = ExtensionField;
9247
9248 /**
9249 * Constructs a new Message OneOf.
9250 * @exports ProtoBuf.Reflect.Message.OneOf
9251 * @param {!ProtoBuf.Builder} builder Builder reference
9252 * @param {!ProtoBuf.Reflect.Message} message Message reference
9253 * @param {string} name OneOf name
9254 * @constructor
9255 * @extends ProtoBuf.Reflect.T
9256 */
9257 var OneOf = function(builder, message, name) {
9258 T.call(this, builder, message, name);
9259
9260 /**
9261 * Enclosed fields.
9262 * @type {!Array.<!ProtoBuf.Reflect.Message.Field>}
9263 * @expose
9264 */
9265 this.fields = [];
9266 };
9267
9268 /**
9269 * @alias ProtoBuf.Reflect.Message.OneOf
9270 * @expose
9271 */
9272 Reflect.Message.OneOf = OneOf;
9273
9274 /**
9275 * Constructs a new Enum.
9276 * @exports ProtoBuf.Reflect.Enum
9277 * @param {!ProtoBuf.Builder} builder Builder reference
9278 * @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
9279 * @param {string} name Enum name
9280 * @param {Object.<string,*>=} options Enum options
9281 * @param {string?} syntax The syntax level (e.g., proto3)
9282 * @constructor
9283 * @extends ProtoBuf.Reflect.Namespace
9284 */
9285 var Enum = function(builder, parent, name, options, syntax) {
9286 Namespace.call(this, builder, parent, name, options, syntax);
9287
9288 /**
9289 * @override
9290 */
9291 this.className = "Enum";
9292
9293 /**
9294 * Runtime enum object.
9295 * @type {Object.<string,number>|null}
9296 * @expose
9297 */
9298 this.object = null;
9299 };
9300
9301 /**
9302 * Gets the string name of an enum value.
9303 * @param {!ProtoBuf.Builder.Enum} enm Runtime enum
9304 * @param {number} value Enum value
9305 * @returns {?string} Name or `null` if not present
9306 * @expose
9307 */
9308 Enum.getName = function(enm, value) {
9309 var keys = Object.keys(enm);
9310 for (var i=0, key; i<keys.length; ++i)
9311 if (enm[key = keys[i]] === value)
9312 return key;
9313 return null;
9314 };
9315
9316 /**
9317 * @alias ProtoBuf.Reflect.Enum.prototype
9318 * @inner
9319 */
9320 var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
9321
9322 /**
9323 * Builds this enum and returns the runtime counterpart.
9324 * @param {boolean} rebuild Whether to rebuild or not, defaults to false
9325 * @returns {!Object.<string,number>}
9326 * @expose
9327 */
9328 EnumPrototype.build = function(rebuild) {
9329 if (this.object && !rebuild)
9330 return this.object;
9331 var enm = new ProtoBuf.Builder.Enum(),
9332 values = this.getChildren(Enum.Value);
9333 for (var i=0, k=values.length; i<k; ++i)
9334 enm[values[i]['name']] = values[i]['id'];
9335 if (Object.defineProperty)
9336 Object.defineProperty(enm, '$options', {
9337 "value": this.buildOpt(),
9338 "enumerable": false
9339 });
9340 return this.object = enm;
9341 };
9342
9343 /**
9344 * @alias ProtoBuf.Reflect.Enum
9345 * @expose
9346 */
9347 Reflect.Enum = Enum;
9348
9349 /**
9350 * Constructs a new Enum Value.
9351 * @exports ProtoBuf.Reflect.Enum.Value
9352 * @param {!ProtoBuf.Builder} builder Builder reference
9353 * @param {!ProtoBuf.Reflect.Enum} enm Enum reference
9354 * @param {string} name Field name
9355 * @param {number} id Unique field id
9356 * @constructor
9357 * @extends ProtoBuf.Reflect.T
9358 */
9359 var Value = function(builder, enm, name, id) {
9360 T.call(this, builder, enm, name);
9361
9362 /**
9363 * @override
9364 */
9365 this.className = "Enum.Value";
9366
9367 /**
9368 * Unique enum value id.
9369 * @type {number}
9370 * @expose
9371 */
9372 this.id = id;
9373 };
9374
9375 // Extends T
9376 Value.prototype = Object.create(T.prototype);
9377
9378 /**
9379 * @alias ProtoBuf.Reflect.Enum.Value
9380 * @expose
9381 */
9382 Reflect.Enum.Value = Value;
9383
9384 /**
9385 * An extension (field).
9386 * @exports ProtoBuf.Reflect.Extension
9387 * @constructor
9388 * @param {!ProtoBuf.Builder} builder Builder reference
9389 * @param {!ProtoBuf.Reflect.T} parent Parent object
9390 * @param {string} name Object name
9391 * @param {!ProtoBuf.Reflect.Message.Field} field Extension field
9392 */
9393 var Extension = function(builder, parent, name, field) {
9394 T.call(this, builder, parent, name);
9395
9396 /**
9397 * Extended message field.
9398 * @type {!ProtoBuf.Reflect.Message.Field}
9399 * @expose
9400 */
9401 this.field = field;
9402 };
9403
9404 // Extends T
9405 Extension.prototype = Object.create(T.prototype);
9406
9407 /**
9408 * @alias ProtoBuf.Reflect.Extension
9409 * @expose
9410 */
9411 Reflect.Extension = Extension;
9412
9413 /**
9414 * Constructs a new Service.
9415 * @exports ProtoBuf.Reflect.Service
9416 * @param {!ProtoBuf.Builder} builder Builder reference
9417 * @param {!ProtoBuf.Reflect.Namespace} root Root
9418 * @param {string} name Service name
9419 * @param {Object.<string,*>=} options Options
9420 * @constructor
9421 * @extends ProtoBuf.Reflect.Namespace
9422 */
9423 var Service = function(builder, root, name, options) {
9424 Namespace.call(this, builder, root, name, options);
9425
9426 /**
9427 * @override
9428 */
9429 this.className = "Service";
9430
9431 /**
9432 * Built runtime service class.
9433 * @type {?function(new:ProtoBuf.Builder.Service)}
9434 */
9435 this.clazz = null;
9436 };
9437
9438 /**
9439 * @alias ProtoBuf.Reflect.Service.prototype
9440 * @inner
9441 */
9442 var ServicePrototype = Service.prototype = Object.create(Namespace.prototype);
9443
9444 /**
9445 * Builds the service and returns the runtime counterpart, which is a fully functional class.
9446 * @see ProtoBuf.Builder.Service
9447 * @param {boolean=} rebuild Whether to rebuild or not
9448 * @return {Function} Service class
9449 * @throws {Error} If the message cannot be built
9450 * @expose
9451 */
9452 ServicePrototype.build = function(rebuild) {
9453 if (this.clazz && !rebuild)
9454 return this.clazz;
9455
9456 // Create the runtime Service class in its own scope
9457 return this.clazz = (function(ProtoBuf, T) {
9458
9459 /**
9460 * Constructs a new runtime Service.
9461 * @name ProtoBuf.Builder.Service
9462 * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
9463 * @class Barebone of all runtime services.
9464 * @constructor
9465 * @throws {Error} If the service cannot be created
9466 */
9467 var Service = function(rpcImpl) {
9468 ProtoBuf.Builder.Service.call(this);
9469
9470 /**
9471 * Service implementation.
9472 * @name ProtoBuf.Builder.Service#rpcImpl
9473 * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
9474 * @expose
9475 */
9476 this.rpcImpl = rpcImpl || function(name, msg, callback) {
9477 // This is what a user has to implement: A function receiving the method name, the actual message to
9478 // send (type checked) and the callback that's either provided with the error as its first
9479 // argument or null and the actual response message.
9480 setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
9481 };
9482 };
9483
9484 /**
9485 * @alias ProtoBuf.Builder.Service.prototype
9486 * @inner
9487 */
9488 var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
9489
9490 /**
9491 * Asynchronously performs an RPC call using the given RPC implementation.
9492 * @name ProtoBuf.Builder.Service.[Method]
9493 * @function
9494 * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
9495 * @param {ProtoBuf.Builder.Message} req Request
9496 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
9497 * the error if any and the response either as a pre-parsed message or as its raw bytes
9498 * @abstract
9499 */
9500
9501 /**
9502 * Asynchronously performs an RPC call using the instance's RPC implementation.
9503 * @name ProtoBuf.Builder.Service#[Method]
9504 * @function
9505 * @param {ProtoBuf.Builder.Message} req Request
9506 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
9507 * the error if any and the response either as a pre-parsed message or as its raw bytes
9508 * @abstract
9509 */
9510
9511 var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
9512 for (var i=0; i<rpc.length; i++) {
9513 (function(method) {
9514
9515 // service#Method(message, callback)
9516 ServicePrototype[method.name] = function(req, callback) {
9517 try {
9518 try {
9519 // If given as a buffer, decode the request. Will throw a TypeError if not a valid buffer.
9520 req = method.resolvedRequestType.clazz.decode(ByteBuffer.wrap(req));
9521 } catch (err) {
9522 if (!(err instanceof TypeError))
9523 throw err;
9524 }
9525 if (req === null || typeof req !== 'object')
9526 throw Error("Illegal arguments");
9527 if (!(req instanceof method.resolvedRequestType.clazz))
9528 req = new method.resolvedRequestType.clazz(req);
9529 this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
9530 if (err) {
9531 callback(err);
9532 return;
9533 }
9534 // Coalesce to empty string when service response has empty content
9535 if (res === null)
9536 res = '';
9537 try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
9538 if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
9539 callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
9540 return;
9541 }
9542 callback(null, res);
9543 });
9544 } catch (err) {
9545 setTimeout(callback.bind(this, err), 0);
9546 }
9547 };
9548
9549 // Service.Method(rpcImpl, message, callback)
9550 Service[method.name] = function(rpcImpl, req, callback) {
9551 new Service(rpcImpl)[method.name](req, callback);
9552 };
9553
9554 if (Object.defineProperty)
9555 Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
9556 Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
9557 })(rpc[i]);
9558 }
9559
9560 if (Object.defineProperty)
9561 Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
9562 Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }),
9563 Object.defineProperty(Service, "$type", { "value": T }),
9564 Object.defineProperty(ServicePrototype, "$type", { "value": T });
9565
9566 return Service;
9567
9568 })(ProtoBuf, this);
9569 };
9570
9571 /**
9572 * @alias ProtoBuf.Reflect.Service
9573 * @expose
9574 */
9575 Reflect.Service = Service;
9576
9577 /**
9578 * Abstract service method.
9579 * @exports ProtoBuf.Reflect.Service.Method
9580 * @param {!ProtoBuf.Builder} builder Builder reference
9581 * @param {!ProtoBuf.Reflect.Service} svc Service
9582 * @param {string} name Method name
9583 * @param {Object.<string,*>=} options Options
9584 * @constructor
9585 * @extends ProtoBuf.Reflect.T
9586 */
9587 var Method = function(builder, svc, name, options) {
9588 T.call(this, builder, svc, name);
9589
9590 /**
9591 * @override
9592 */
9593 this.className = "Service.Method";
9594
9595 /**
9596 * Options.
9597 * @type {Object.<string, *>}
9598 * @expose
9599 */
9600 this.options = options || {};
9601 };
9602
9603 /**
9604 * @alias ProtoBuf.Reflect.Service.Method.prototype
9605 * @inner
9606 */
9607 var MethodPrototype = Method.prototype = Object.create(T.prototype);
9608
9609 /**
9610 * Builds the method's '$options' property.
9611 * @name ProtoBuf.Reflect.Service.Method#buildOpt
9612 * @function
9613 * @return {Object.<string,*>}
9614 */
9615 MethodPrototype.buildOpt = NamespacePrototype.buildOpt;
9616
9617 /**
9618 * @alias ProtoBuf.Reflect.Service.Method
9619 * @expose
9620 */
9621 Reflect.Service.Method = Method;
9622
9623 /**
9624 * RPC service method.
9625 * @exports ProtoBuf.Reflect.Service.RPCMethod
9626 * @param {!ProtoBuf.Builder} builder Builder reference
9627 * @param {!ProtoBuf.Reflect.Service} svc Service
9628 * @param {string} name Method name
9629 * @param {string} request Request message name
9630 * @param {string} response Response message name
9631 * @param {boolean} request_stream Whether requests are streamed
9632 * @param {boolean} response_stream Whether responses are streamed
9633 * @param {Object.<string,*>=} options Options
9634 * @constructor
9635 * @extends ProtoBuf.Reflect.Service.Method
9636 */
9637 var RPCMethod = function(builder, svc, name, request, response, request_stream, response_stream, options) {
9638 Method.call(this, builder, svc, name, options);
9639
9640 /**
9641 * @override
9642 */
9643 this.className = "Service.RPCMethod";
9644
9645 /**
9646 * Request message name.
9647 * @type {string}
9648 * @expose
9649 */
9650 this.requestName = request;
9651
9652 /**
9653 * Response message name.
9654 * @type {string}
9655 * @expose
9656 */
9657 this.responseName = response;
9658
9659 /**
9660 * Whether requests are streamed
9661 * @type {bool}
9662 * @expose
9663 */
9664 this.requestStream = request_stream;
9665
9666 /**
9667 * Whether responses are streamed
9668 * @type {bool}
9669 * @expose
9670 */
9671 this.responseStream = response_stream;
9672
9673 /**
9674 * Resolved request message type.
9675 * @type {ProtoBuf.Reflect.Message}
9676 * @expose
9677 */
9678 this.resolvedRequestType = null;
9679
9680 /**
9681 * Resolved response message type.
9682 * @type {ProtoBuf.Reflect.Message}
9683 * @expose
9684 */
9685 this.resolvedResponseType = null;
9686 };
9687
9688 // Extends Method
9689 RPCMethod.prototype = Object.create(Method.prototype);
9690
9691 /**
9692 * @alias ProtoBuf.Reflect.Service.RPCMethod
9693 * @expose
9694 */
9695 Reflect.Service.RPCMethod = RPCMethod;
9696
9697 return Reflect;
9698
9699 })(ProtoBuf);
9700
9701 /**
9702 * @alias ProtoBuf.Builder
9703 * @expose
9704 */
9705 ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
9706
9707 /**
9708 * Constructs a new Builder.
9709 * @exports ProtoBuf.Builder
9710 * @class Provides the functionality to build protocol messages.
9711 * @param {Object.<string,*>=} options Options
9712 * @constructor
9713 */
9714 var Builder = function(options) {
9715
9716 /**
9717 * Namespace.
9718 * @type {ProtoBuf.Reflect.Namespace}
9719 * @expose
9720 */
9721 this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
9722
9723 /**
9724 * Namespace pointer.
9725 * @type {ProtoBuf.Reflect.T}
9726 * @expose
9727 */
9728 this.ptr = this.ns;
9729
9730 /**
9731 * Resolved flag.
9732 * @type {boolean}
9733 * @expose
9734 */
9735 this.resolved = false;
9736
9737 /**
9738 * The current building result.
9739 * @type {Object.<string,ProtoBuf.Builder.Message|Object>|null}
9740 * @expose
9741 */
9742 this.result = null;
9743
9744 /**
9745 * Imported files.
9746 * @type {Array.<string>}
9747 * @expose
9748 */
9749 this.files = {};
9750
9751 /**
9752 * Import root override.
9753 * @type {?string}
9754 * @expose
9755 */
9756 this.importRoot = null;
9757
9758 /**
9759 * Options.
9760 * @type {!Object.<string, *>}
9761 * @expose
9762 */
9763 this.options = options || {};
9764 };
9765
9766 /**
9767 * @alias ProtoBuf.Builder.prototype
9768 * @inner
9769 */
9770 var BuilderPrototype = Builder.prototype;
9771
9772 // ----- Definition tests -----
9773
9774 /**
9775 * Tests if a definition most likely describes a message.
9776 * @param {!Object} def
9777 * @returns {boolean}
9778 * @expose
9779 */
9780 Builder.isMessage = function(def) {
9781 // Messages require a string name
9782 if (typeof def["name"] !== 'string')
9783 return false;
9784 // Messages do not contain values (enum) or rpc methods (service)
9785 if (typeof def["values"] !== 'undefined' || typeof def["rpc"] !== 'undefined')
9786 return false;
9787 return true;
9788 };
9789
9790 /**
9791 * Tests if a definition most likely describes a message field.
9792 * @param {!Object} def
9793 * @returns {boolean}
9794 * @expose
9795 */
9796 Builder.isMessageField = function(def) {
9797 // Message fields require a string rule, name and type and an id
9798 if (typeof def["rule"] !== 'string' || typeof def["name"] !== 'string' || typeof def["type"] !== 'string' || typeof def["id"] === 'undefined')
9799 return false;
9800 return true;
9801 };
9802
9803 /**
9804 * Tests if a definition most likely describes an enum.
9805 * @param {!Object} def
9806 * @returns {boolean}
9807 * @expose
9808 */
9809 Builder.isEnum = function(def) {
9810 // Enums require a string name
9811 if (typeof def["name"] !== 'string')
9812 return false;
9813 // Enums require at least one value
9814 if (typeof def["values"] === 'undefined' || !Array.isArray(def["values"]) || def["values"].length === 0)
9815 return false;
9816 return true;
9817 };
9818
9819 /**
9820 * Tests if a definition most likely describes a service.
9821 * @param {!Object} def
9822 * @returns {boolean}
9823 * @expose
9824 */
9825 Builder.isService = function(def) {
9826 // Services require a string name and an rpc object
9827 if (typeof def["name"] !== 'string' || typeof def["rpc"] !== 'object' || !def["rpc"])
9828 return false;
9829 return true;
9830 };
9831
9832 /**
9833 * Tests if a definition most likely describes an extended message
9834 * @param {!Object} def
9835 * @returns {boolean}
9836 * @expose
9837 */
9838 Builder.isExtend = function(def) {
9839 // Extends rquire a string ref
9840 if (typeof def["ref"] !== 'string')
9841 return false;
9842 return true;
9843 };
9844
9845 // ----- Building -----
9846
9847 /**
9848 * Resets the pointer to the root namespace.
9849 * @returns {!ProtoBuf.Builder} this
9850 * @expose
9851 */
9852 BuilderPrototype.reset = function() {
9853 this.ptr = this.ns;
9854 return this;
9855 };
9856
9857 /**
9858 * Defines a namespace on top of the current pointer position and places the pointer on it.
9859 * @param {string} namespace
9860 * @return {!ProtoBuf.Builder} this
9861 * @expose
9862 */
9863 BuilderPrototype.define = function(namespace) {
9864 if (typeof namespace !== 'string' || !Lang.TYPEREF.test(namespace))
9865 throw Error("illegal namespace: "+namespace);
9866 namespace.split(".").forEach(function(part) {
9867 var ns = this.ptr.getChild(part);
9868 if (ns === null) // Keep existing
9869 this.ptr.addChild(ns = new Reflect.Namespace(this, this.ptr, part));
9870 this.ptr = ns;
9871 }, this);
9872 return this;
9873 };
9874
9875 /**
9876 * Creates the specified definitions at the current pointer position.
9877 * @param {!Array.<!Object>} defs Messages, enums or services to create
9878 * @returns {!ProtoBuf.Builder} this
9879 * @throws {Error} If a message definition is invalid
9880 * @expose
9881 */
9882 BuilderPrototype.create = function(defs) {
9883 if (!defs)
9884 return this; // Nothing to create
9885 if (!Array.isArray(defs))
9886 defs = [defs];
9887 else {
9888 if (defs.length === 0)
9889 return this;
9890 defs = defs.slice();
9891 }
9892
9893 // It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
9894 var stack = [defs];
9895 while (stack.length > 0) {
9896 defs = stack.pop();
9897
9898 if (!Array.isArray(defs)) // Stack always contains entire namespaces
9899 throw Error("not a valid namespace: "+JSON.stringify(defs));
9900
9901 while (defs.length > 0) {
9902 var def = defs.shift(); // Namespaces always contain an array of messages, enums and services
9903
9904 if (Builder.isMessage(def)) {
9905 var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"], def["syntax"]);
9906
9907 // Create OneOfs
9908 var oneofs = {};
9909 if (def["oneofs"])
9910 Object.keys(def["oneofs"]).forEach(function(name) {
9911 obj.addChild(oneofs[name] = new Reflect.Message.OneOf(this, obj, name));
9912 }, this);
9913
9914 // Create fields
9915 if (def["fields"])
9916 def["fields"].forEach(function(fld) {
9917 if (obj.getChild(fld["id"]|0) !== null)
9918 throw Error("duplicate or invalid field id in "+obj.name+": "+fld['id']);
9919 if (fld["options"] && typeof fld["options"] !== 'object')
9920 throw Error("illegal field options in "+obj.name+"#"+fld["name"]);
9921 var oneof = null;
9922 if (typeof fld["oneof"] === 'string' && !(oneof = oneofs[fld["oneof"]]))
9923 throw Error("illegal oneof in "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
9924 fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["keytype"], fld["type"], fld["name"], fld["id"], fld["options"], oneof, def["syntax"]);
9925 if (oneof)
9926 oneof.fields.push(fld);
9927 obj.addChild(fld);
9928 }, this);
9929
9930 // Push children to stack
9931 var subObj = [];
9932 if (def["enums"])
9933 def["enums"].forEach(function(enm) {
9934 subObj.push(enm);
9935 });
9936 if (def["messages"])
9937 def["messages"].forEach(function(msg) {
9938 subObj.push(msg);
9939 });
9940 if (def["services"])
9941 def["services"].forEach(function(svc) {
9942 subObj.push(svc);
9943 });
9944
9945 // Set extension ranges
9946 if (def["extensions"]) {
9947 if (typeof def["extensions"][0] === 'number') // pre 5.0.1
9948 obj.extensions = [ def["extensions"] ];
9949 else
9950 obj.extensions = def["extensions"];
9951 }
9952
9953 // Create on top of current namespace
9954 this.ptr.addChild(obj);
9955 if (subObj.length > 0) {
9956 stack.push(defs); // Push the current level back
9957 defs = subObj; // Continue processing sub level
9958 subObj = null;
9959 this.ptr = obj; // And move the pointer to this namespace
9960 obj = null;
9961 continue;
9962 }
9963 subObj = null;
9964
9965 } else if (Builder.isEnum(def)) {
9966
9967 obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"], def["syntax"]);
9968 def["values"].forEach(function(val) {
9969 obj.addChild(new Reflect.Enum.Value(this, obj, val["name"], val["id"]));
9970 }, this);
9971 this.ptr.addChild(obj);
9972
9973 } else if (Builder.isService(def)) {
9974
9975 obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
9976 Object.keys(def["rpc"]).forEach(function(name) {
9977 var mtd = def["rpc"][name];
9978 obj.addChild(new Reflect.Service.RPCMethod(this, obj, name, mtd["request"], mtd["response"], !!mtd["request_stream"], !!mtd["response_stream"], mtd["options"]));
9979 }, this);
9980 this.ptr.addChild(obj);
9981
9982 } else if (Builder.isExtend(def)) {
9983
9984 obj = this.ptr.resolve(def["ref"], true);
9985 if (obj) {
9986 def["fields"].forEach(function(fld) {
9987 if (obj.getChild(fld['id']|0) !== null)
9988 throw Error("duplicate extended field id in "+obj.name+": "+fld['id']);
9989 // Check if field id is allowed to be extended
9990 if (obj.extensions) {
9991 var valid = false;
9992 obj.extensions.forEach(function(range) {
9993 if (fld["id"] >= range[0] && fld["id"] <= range[1])
9994 valid = true;
9995 });
9996 if (!valid)
9997 throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" (not within valid ranges)");
9998 }
9999 // Convert extension field names to camel case notation if the override is set
10000 var name = fld["name"];
10001 if (this.options['convertFieldsToCamelCase'])
10002 name = ProtoBuf.Util.toCamelCase(name);
10003 // see #161: Extensions use their fully qualified name as their runtime key and...
10004 var field = new Reflect.Message.ExtensionField(this, obj, fld["rule"], fld["type"], this.ptr.fqn()+'.'+name, fld["id"], fld["options"]);
10005 // ...are added on top of the current namespace as an extension which is used for
10006 // resolving their type later on (the extension always keeps the original name to
10007 // prevent naming collisions)
10008 var ext = new Reflect.Extension(this, this.ptr, fld["name"], field);
10009 field.extension = ext;
10010 this.ptr.addChild(ext);
10011 obj.addChild(field);
10012 }, this);
10013
10014 } else if (!/\.?google\.protobuf\./.test(def["ref"])) // Silently skip internal extensions
10015 throw Error("extended message "+def["ref"]+" is not defined");
10016
10017 } else
10018 throw Error("not a valid definition: "+JSON.stringify(def));
10019
10020 def = null;
10021 obj = null;
10022 }
10023 // Break goes here
10024 defs = null;
10025 this.ptr = this.ptr.parent; // Namespace done, continue at parent
10026 }
10027 this.resolved = false; // Require re-resolve
10028 this.result = null; // Require re-build
10029 return this;
10030 };
10031
10032 /**
10033 * Propagates syntax to all children.
10034 * @param {!Object} parent
10035 * @inner
10036 */
10037 function propagateSyntax(parent) {
10038 if (parent['messages']) {
10039 parent['messages'].forEach(function(child) {
10040 child["syntax"] = parent["syntax"];
10041 propagateSyntax(child);
10042 });
10043 }
10044 if (parent['enums']) {
10045 parent['enums'].forEach(function(child) {
10046 child["syntax"] = parent["syntax"];
10047 });
10048 }
10049 }
10050
10051 /**
10052 * Imports another definition into this builder.
10053 * @param {Object.<string,*>} json Parsed import
10054 * @param {(string|{root: string, file: string})=} filename Imported file name
10055 * @returns {!ProtoBuf.Builder} this
10056 * @throws {Error} If the definition or file cannot be imported
10057 * @expose
10058 */
10059 BuilderPrototype["import"] = function(json, filename) {
10060 var delim = '/';
10061
10062 // Make sure to skip duplicate imports
10063
10064 if (typeof filename === 'string') {
10065
10066 if (ProtoBuf.Util.IS_NODE)
10067 filename = require$$2['resolve'](filename);
10068 if (this.files[filename] === true)
10069 return this.reset();
10070 this.files[filename] = true;
10071
10072 } else if (typeof filename === 'object') { // Object with root, file.
10073
10074 var root = filename.root;
10075 if (ProtoBuf.Util.IS_NODE)
10076 root = require$$2['resolve'](root);
10077 if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
10078 delim = '\\';
10079 var fname;
10080 if (ProtoBuf.Util.IS_NODE)
10081 fname = require$$2['join'](root, filename.file);
10082 else
10083 fname = root + delim + filename.file;
10084 if (this.files[fname] === true)
10085 return this.reset();
10086 this.files[fname] = true;
10087 }
10088
10089 // Import imports
10090
10091 if (json['imports'] && json['imports'].length > 0) {
10092 var importRoot,
10093 resetRoot = false;
10094
10095 if (typeof filename === 'object') { // If an import root is specified, override
10096
10097 this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
10098 importRoot = this.importRoot;
10099 filename = filename["file"];
10100 if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0)
10101 delim = '\\';
10102
10103 } else if (typeof filename === 'string') {
10104
10105 if (this.importRoot) // If import root is overridden, use it
10106 importRoot = this.importRoot;
10107 else { // Otherwise compute from filename
10108 if (filename.indexOf("/") >= 0) { // Unix
10109 importRoot = filename.replace(/\/[^\/]*$/, "");
10110 if (/* /file.proto */ importRoot === "")
10111 importRoot = "/";
10112 } else if (filename.indexOf("\\") >= 0) { // Windows
10113 importRoot = filename.replace(/\\[^\\]*$/, "");
10114 delim = '\\';
10115 } else
10116 importRoot = ".";
10117 }
10118
10119 } else
10120 importRoot = null;
10121
10122 for (var i=0; i<json['imports'].length; i++) {
10123 if (typeof json['imports'][i] === 'string') { // Import file
10124 if (!importRoot)
10125 throw Error("cannot determine import root");
10126 var importFilename = json['imports'][i];
10127 if (importFilename === "google/protobuf/descriptor.proto")
10128 continue; // Not needed and therefore not used
10129 if (ProtoBuf.Util.IS_NODE)
10130 importFilename = require$$2['join'](importRoot, importFilename);
10131 else
10132 importFilename = importRoot + delim + importFilename;
10133 if (this.files[importFilename] === true)
10134 continue; // Already imported
10135 if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a light build
10136 importFilename = importFilename.replace(/\.proto$/, ".json"); // always load the JSON file
10137 var contents = ProtoBuf.Util.fetch(importFilename);
10138 if (contents === null)
10139 throw Error("failed to import '"+importFilename+"' in '"+filename+"': file not found");
10140 if (/\.json$/i.test(importFilename)) // Always possible
10141 this["import"](JSON.parse(contents+""), importFilename); // May throw
10142 else
10143 this["import"](ProtoBuf.DotProto.Parser.parse(contents), importFilename); // May throw
10144 } else // Import structure
10145 if (!filename)
10146 this["import"](json['imports'][i]);
10147 else if (/\.(\w+)$/.test(filename)) // With extension: Append _importN to the name portion to make it unique
10148 this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)$/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
10149 else // Without extension: Append _importN to make it unique
10150 this["import"](json['imports'][i], filename+"_import"+i);
10151 }
10152 if (resetRoot) // Reset import root override when all imports are done
10153 this.importRoot = null;
10154 }
10155
10156 // Import structures
10157
10158 if (json['package'])
10159 this.define(json['package']);
10160 if (json['syntax'])
10161 propagateSyntax(json);
10162 var base = this.ptr;
10163 if (json['options'])
10164 Object.keys(json['options']).forEach(function(key) {
10165 base.options[key] = json['options'][key];
10166 });
10167 if (json['messages'])
10168 this.create(json['messages']),
10169 this.ptr = base;
10170 if (json['enums'])
10171 this.create(json['enums']),
10172 this.ptr = base;
10173 if (json['services'])
10174 this.create(json['services']),
10175 this.ptr = base;
10176 if (json['extends'])
10177 this.create(json['extends']);
10178
10179 return this.reset();
10180 };
10181
10182 /**
10183 * Resolves all namespace objects.
10184 * @throws {Error} If a type cannot be resolved
10185 * @returns {!ProtoBuf.Builder} this
10186 * @expose
10187 */
10188 BuilderPrototype.resolveAll = function() {
10189 // Resolve all reflected objects
10190 var res;
10191 if (this.ptr == null || typeof this.ptr.type === 'object')
10192 return this; // Done (already resolved)
10193
10194 if (this.ptr instanceof Reflect.Namespace) { // Resolve children
10195
10196 this.ptr.children.forEach(function(child) {
10197 this.ptr = child;
10198 this.resolveAll();
10199 }, this);
10200
10201 } else if (this.ptr instanceof Reflect.Message.Field) { // Resolve type
10202
10203 if (!Lang.TYPE.test(this.ptr.type)) {
10204 if (!Lang.TYPEREF.test(this.ptr.type))
10205 throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
10206 res = (this.ptr instanceof Reflect.Message.ExtensionField ? this.ptr.extension.parent : this.ptr.parent).resolve(this.ptr.type, true);
10207 if (!res)
10208 throw Error("unresolvable type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
10209 this.ptr.resolvedType = res;
10210 if (res instanceof Reflect.Enum) {
10211 this.ptr.type = ProtoBuf.TYPES["enum"];
10212 if (this.ptr.syntax === 'proto3' && res.syntax !== 'proto3')
10213 throw Error("proto3 message cannot reference proto2 enum");
10214 }
10215 else if (res instanceof Reflect.Message)
10216 this.ptr.type = res.isGroup ? ProtoBuf.TYPES["group"] : ProtoBuf.TYPES["message"];
10217 else
10218 throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
10219 } else
10220 this.ptr.type = ProtoBuf.TYPES[this.ptr.type];
10221
10222 // If it's a map field, also resolve the key type. The key type can be only a numeric, string, or bool type
10223 // (i.e., no enums or messages), so we don't need to resolve against the current namespace.
10224 if (this.ptr.map) {
10225 if (!Lang.TYPE.test(this.ptr.keyType))
10226 throw Error("illegal key type for map field in "+this.ptr.toString(true)+": "+this.ptr.keyType);
10227 this.ptr.keyType = ProtoBuf.TYPES[this.ptr.keyType];
10228 }
10229
10230 // If it's a repeated and packable field then proto3 mandates it should be packed by
10231 // default
10232 if (
10233 this.ptr.syntax === 'proto3' &&
10234 this.ptr.repeated && this.ptr.options.packed === undefined &&
10235 ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.ptr.type.wireType) !== -1
10236 ) {
10237 this.ptr.options.packed = true;
10238 }
10239
10240 } else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
10241
10242 if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
10243 res = this.ptr.parent.resolve(this.ptr.requestName, true);
10244 if (!res || !(res instanceof ProtoBuf.Reflect.Message))
10245 throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.requestName);
10246 this.ptr.resolvedRequestType = res;
10247 res = this.ptr.parent.resolve(this.ptr.responseName, true);
10248 if (!res || !(res instanceof ProtoBuf.Reflect.Message))
10249 throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.responseName);
10250 this.ptr.resolvedResponseType = res;
10251 } else // Should not happen as nothing else is implemented
10252 throw Error("illegal service type in "+this.ptr.toString(true));
10253
10254 } else if (
10255 !(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && // Not built
10256 !(this.ptr instanceof ProtoBuf.Reflect.Extension) && // Not built
10257 !(this.ptr instanceof ProtoBuf.Reflect.Enum.Value) // Built in enum
10258 )
10259 throw Error("illegal object in namespace: "+typeof(this.ptr)+": "+this.ptr);
10260
10261 return this.reset();
10262 };
10263
10264 /**
10265 * Builds the protocol. This will first try to resolve all definitions and, if this has been successful,
10266 * return the built package.
10267 * @param {(string|Array.<string>)=} path Specifies what to return. If omitted, the entire namespace will be returned.
10268 * @returns {!ProtoBuf.Builder.Message|!Object.<string,*>}
10269 * @throws {Error} If a type could not be resolved
10270 * @expose
10271 */
10272 BuilderPrototype.build = function(path) {
10273 this.reset();
10274 if (!this.resolved)
10275 this.resolveAll(),
10276 this.resolved = true,
10277 this.result = null; // Require re-build
10278 if (this.result === null) // (Re-)Build
10279 this.result = this.ns.build();
10280 if (!path)
10281 return this.result;
10282 var part = typeof path === 'string' ? path.split(".") : path,
10283 ptr = this.result; // Build namespace pointer (no hasChild etc.)
10284 for (var i=0; i<part.length; i++)
10285 if (ptr[part[i]])
10286 ptr = ptr[part[i]];
10287 else {
10288 ptr = null;
10289 break;
10290 }
10291 return ptr;
10292 };
10293
10294 /**
10295 * Similar to {@link ProtoBuf.Builder#build}, but looks up the internal reflection descriptor.
10296 * @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
10297 * @param {boolean=} excludeNonNamespace Excludes non-namespace types like fields, defaults to `false`
10298 * @returns {?ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
10299 */
10300 BuilderPrototype.lookup = function(path, excludeNonNamespace) {
10301 return path ? this.ns.resolve(path, excludeNonNamespace) : this.ns;
10302 };
10303
10304 /**
10305 * Returns a string representation of this object.
10306 * @return {string} String representation as of "Builder"
10307 * @expose
10308 */
10309 BuilderPrototype.toString = function() {
10310 return "Builder";
10311 };
10312
10313 // ----- Base classes -----
10314 // Exist for the sole purpose of being able to "... instanceof ProtoBuf.Builder.Message" etc.
10315
10316 /**
10317 * @alias ProtoBuf.Builder.Message
10318 */
10319 Builder.Message = function() {};
10320
10321 /**
10322 * @alias ProtoBuf.Builder.Enum
10323 */
10324 Builder.Enum = function() {};
10325
10326 /**
10327 * @alias ProtoBuf.Builder.Message
10328 */
10329 Builder.Service = function() {};
10330
10331 return Builder;
10332
10333 })(ProtoBuf, ProtoBuf.Lang, ProtoBuf.Reflect);
10334
10335 /**
10336 * @alias ProtoBuf.Map
10337 * @expose
10338 */
10339 ProtoBuf.Map = (function(ProtoBuf, Reflect) {
10340
10341 /**
10342 * Constructs a new Map. A Map is a container that is used to implement map
10343 * fields on message objects. It closely follows the ES6 Map API; however,
10344 * it is distinct because we do not want to depend on external polyfills or
10345 * on ES6 itself.
10346 *
10347 * @exports ProtoBuf.Map
10348 * @param {!ProtoBuf.Reflect.Field} field Map field
10349 * @param {Object.<string,*>=} contents Initial contents
10350 * @constructor
10351 */
10352 var Map = function(field, contents) {
10353 if (!field.map)
10354 throw Error("field is not a map");
10355
10356 /**
10357 * The field corresponding to this map.
10358 * @type {!ProtoBuf.Reflect.Field}
10359 */
10360 this.field = field;
10361
10362 /**
10363 * Element instance corresponding to key type.
10364 * @type {!ProtoBuf.Reflect.Element}
10365 */
10366 this.keyElem = new Reflect.Element(field.keyType, null, true, field.syntax);
10367
10368 /**
10369 * Element instance corresponding to value type.
10370 * @type {!ProtoBuf.Reflect.Element}
10371 */
10372 this.valueElem = new Reflect.Element(field.type, field.resolvedType, false, field.syntax);
10373
10374 /**
10375 * Internal map: stores mapping of (string form of key) -> (key, value)
10376 * pair.
10377 *
10378 * We provide map semantics for arbitrary key types, but we build on top
10379 * of an Object, which has only string keys. In order to avoid the need
10380 * to convert a string key back to its native type in many situations,
10381 * we store the native key value alongside the value. Thus, we only need
10382 * a one-way mapping from a key type to its string form that guarantees
10383 * uniqueness and equality (i.e., str(K1) === str(K2) if and only if K1
10384 * === K2).
10385 *
10386 * @type {!Object<string, {key: *, value: *}>}
10387 */
10388 this.map = {};
10389
10390 /**
10391 * Returns the number of elements in the map.
10392 */
10393 Object.defineProperty(this, "size", {
10394 get: function() { return Object.keys(this.map).length; }
10395 });
10396
10397 // Fill initial contents from a raw object.
10398 if (contents) {
10399 var keys = Object.keys(contents);
10400 for (var i = 0; i < keys.length; i++) {
10401 var key = this.keyElem.valueFromString(keys[i]);
10402 var val = this.valueElem.verifyValue(contents[keys[i]]);
10403 this.map[this.keyElem.valueToString(key)] =
10404 { key: key, value: val };
10405 }
10406 }
10407 };
10408
10409 var MapPrototype = Map.prototype;
10410
10411 /**
10412 * Helper: return an iterator over an array.
10413 * @param {!Array<*>} arr the array
10414 * @returns {!Object} an iterator
10415 * @inner
10416 */
10417 function arrayIterator(arr) {
10418 var idx = 0;
10419 return {
10420 next: function() {
10421 if (idx < arr.length)
10422 return { done: false, value: arr[idx++] };
10423 return { done: true };
10424 }
10425 }
10426 }
10427
10428 /**
10429 * Clears the map.
10430 */
10431 MapPrototype.clear = function() {
10432 this.map = {};
10433 };
10434
10435 /**
10436 * Deletes a particular key from the map.
10437 * @returns {boolean} Whether any entry with this key was deleted.
10438 */
10439 MapPrototype["delete"] = function(key) {
10440 var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
10441 var hadKey = keyValue in this.map;
10442 delete this.map[keyValue];
10443 return hadKey;
10444 };
10445
10446 /**
10447 * Returns an iterator over [key, value] pairs in the map.
10448 * @returns {Object} The iterator
10449 */
10450 MapPrototype.entries = function() {
10451 var entries = [];
10452 var strKeys = Object.keys(this.map);
10453 for (var i = 0, entry; i < strKeys.length; i++)
10454 entries.push([(entry=this.map[strKeys[i]]).key, entry.value]);
10455 return arrayIterator(entries);
10456 };
10457
10458 /**
10459 * Returns an iterator over keys in the map.
10460 * @returns {Object} The iterator
10461 */
10462 MapPrototype.keys = function() {
10463 var keys = [];
10464 var strKeys = Object.keys(this.map);
10465 for (var i = 0; i < strKeys.length; i++)
10466 keys.push(this.map[strKeys[i]].key);
10467 return arrayIterator(keys);
10468 };
10469
10470 /**
10471 * Returns an iterator over values in the map.
10472 * @returns {!Object} The iterator
10473 */
10474 MapPrototype.values = function() {
10475 var values = [];
10476 var strKeys = Object.keys(this.map);
10477 for (var i = 0; i < strKeys.length; i++)
10478 values.push(this.map[strKeys[i]].value);
10479 return arrayIterator(values);
10480 };
10481
10482 /**
10483 * Iterates over entries in the map, calling a function on each.
10484 * @param {function(this:*, *, *, *)} cb The callback to invoke with value, key, and map arguments.
10485 * @param {Object=} thisArg The `this` value for the callback
10486 */
10487 MapPrototype.forEach = function(cb, thisArg) {
10488 var strKeys = Object.keys(this.map);
10489 for (var i = 0, entry; i < strKeys.length; i++)
10490 cb.call(thisArg, (entry=this.map[strKeys[i]]).value, entry.key, this);
10491 };
10492
10493 /**
10494 * Sets a key in the map to the given value.
10495 * @param {*} key The key
10496 * @param {*} value The value
10497 * @returns {!ProtoBuf.Map} The map instance
10498 */
10499 MapPrototype.set = function(key, value) {
10500 var keyValue = this.keyElem.verifyValue(key);
10501 var valValue = this.valueElem.verifyValue(value);
10502 this.map[this.keyElem.valueToString(keyValue)] =
10503 { key: keyValue, value: valValue };
10504 return this;
10505 };
10506
10507 /**
10508 * Gets the value corresponding to a key in the map.
10509 * @param {*} key The key
10510 * @returns {*|undefined} The value, or `undefined` if key not present
10511 */
10512 MapPrototype.get = function(key) {
10513 var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
10514 if (!(keyValue in this.map))
10515 return undefined;
10516 return this.map[keyValue].value;
10517 };
10518
10519 /**
10520 * Determines whether the given key is present in the map.
10521 * @param {*} key The key
10522 * @returns {boolean} `true` if the key is present
10523 */
10524 MapPrototype.has = function(key) {
10525 var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
10526 return (keyValue in this.map);
10527 };
10528
10529 return Map;
10530 })(ProtoBuf, ProtoBuf.Reflect);
10531
10532
10533 /**
10534 * Constructs a new empty Builder.
10535 * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
10536 * @return {!ProtoBuf.Builder} Builder
10537 * @expose
10538 */
10539 ProtoBuf.newBuilder = function(options) {
10540 options = options || {};
10541 if (typeof options['convertFieldsToCamelCase'] === 'undefined')
10542 options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
10543 if (typeof options['populateAccessors'] === 'undefined')
10544 options['populateAccessors'] = ProtoBuf.populateAccessors;
10545 return new ProtoBuf.Builder(options);
10546 };
10547
10548 /**
10549 * Loads a .json definition and returns the Builder.
10550 * @param {!*|string} json JSON definition
10551 * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
10552 * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
10553 * @return {ProtoBuf.Builder} Builder to create new messages
10554 * @throws {Error} If the definition cannot be parsed or built
10555 * @expose
10556 */
10557 ProtoBuf.loadJson = function(json, builder, filename) {
10558 if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
10559 filename = builder,
10560 builder = null;
10561 if (!builder || typeof builder !== 'object')
10562 builder = ProtoBuf.newBuilder();
10563 if (typeof json === 'string')
10564 json = JSON.parse(json);
10565 builder["import"](json, filename);
10566 builder.resolveAll();
10567 return builder;
10568 };
10569
10570 /**
10571 * Loads a .json file and returns the Builder.
10572 * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
10573 * an overridden 'root' path for all imported files.
10574 * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
10575 * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
10576 * file will be read synchronously and this function will return the Builder.
10577 * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
10578 * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
10579 * request has failed), else undefined
10580 * @expose
10581 */
10582 ProtoBuf.loadJsonFile = function(filename, callback, builder) {
10583 if (callback && typeof callback === 'object')
10584 builder = callback,
10585 callback = null;
10586 else if (!callback || typeof callback !== 'function')
10587 callback = null;
10588 if (callback)
10589 return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
10590 if (contents === null) {
10591 callback(Error("Failed to fetch file"));
10592 return;
10593 }
10594 try {
10595 callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
10596 } catch (e) {
10597 callback(e);
10598 }
10599 });
10600 var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
10601 return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
10602 };
10603
10604 return ProtoBuf;
10605 });
10606 });
10607
10608 var messageCompiled = protobufLight.newBuilder({})['import']({
10609 package: 'push_server.messages2',
10610 syntax: 'proto2',
10611 options: {
10612 objc_class_prefix: 'AVIM'
10613 },
10614 messages: [{
10615 name: 'JsonObjectMessage',
10616 syntax: 'proto2',
10617 fields: [{
10618 rule: 'required',
10619 type: 'string',
10620 name: 'data',
10621 id: 1
10622 }]
10623 }, {
10624 name: 'UnreadTuple',
10625 syntax: 'proto2',
10626 fields: [{
10627 rule: 'required',
10628 type: 'string',
10629 name: 'cid',
10630 id: 1
10631 }, {
10632 rule: 'required',
10633 type: 'int32',
10634 name: 'unread',
10635 id: 2
10636 }, {
10637 rule: 'optional',
10638 type: 'string',
10639 name: 'mid',
10640 id: 3
10641 }, {
10642 rule: 'optional',
10643 type: 'int64',
10644 name: 'timestamp',
10645 id: 4
10646 }, {
10647 rule: 'optional',
10648 type: 'string',
10649 name: 'from',
10650 id: 5
10651 }, {
10652 rule: 'optional',
10653 type: 'string',
10654 name: 'data',
10655 id: 6
10656 }, {
10657 rule: 'optional',
10658 type: 'int64',
10659 name: 'patchTimestamp',
10660 id: 7
10661 }, {
10662 rule: 'optional',
10663 type: 'bool',
10664 name: 'mentioned',
10665 id: 8
10666 }, {
10667 rule: 'optional',
10668 type: 'bytes',
10669 name: 'binaryMsg',
10670 id: 9
10671 }, {
10672 rule: 'optional',
10673 type: 'int32',
10674 name: 'convType',
10675 id: 10
10676 }]
10677 }, {
10678 name: 'LogItem',
10679 syntax: 'proto2',
10680 fields: [{
10681 rule: 'optional',
10682 type: 'string',
10683 name: 'from',
10684 id: 1
10685 }, {
10686 rule: 'optional',
10687 type: 'string',
10688 name: 'data',
10689 id: 2
10690 }, {
10691 rule: 'optional',
10692 type: 'int64',
10693 name: 'timestamp',
10694 id: 3
10695 }, {
10696 rule: 'optional',
10697 type: 'string',
10698 name: 'msgId',
10699 id: 4
10700 }, {
10701 rule: 'optional',
10702 type: 'int64',
10703 name: 'ackAt',
10704 id: 5
10705 }, {
10706 rule: 'optional',
10707 type: 'int64',
10708 name: 'readAt',
10709 id: 6
10710 }, {
10711 rule: 'optional',
10712 type: 'int64',
10713 name: 'patchTimestamp',
10714 id: 7
10715 }, {
10716 rule: 'optional',
10717 type: 'bool',
10718 name: 'mentionAll',
10719 id: 8
10720 }, {
10721 rule: 'repeated',
10722 type: 'string',
10723 name: 'mentionPids',
10724 id: 9
10725 }, {
10726 rule: 'optional',
10727 type: 'bool',
10728 name: 'bin',
10729 id: 10
10730 }, {
10731 rule: 'optional',
10732 type: 'int32',
10733 name: 'convType',
10734 id: 11
10735 }]
10736 }, {
10737 name: 'ConvMemberInfo',
10738 syntax: 'proto2',
10739 fields: [{
10740 rule: 'optional',
10741 type: 'string',
10742 name: 'pid',
10743 id: 1
10744 }, {
10745 rule: 'optional',
10746 type: 'string',
10747 name: 'role',
10748 id: 2
10749 }, {
10750 rule: 'optional',
10751 type: 'string',
10752 name: 'infoId',
10753 id: 3
10754 }]
10755 }, {
10756 name: 'DataCommand',
10757 syntax: 'proto2',
10758 fields: [{
10759 rule: 'repeated',
10760 type: 'string',
10761 name: 'ids',
10762 id: 1
10763 }, {
10764 rule: 'repeated',
10765 type: 'JsonObjectMessage',
10766 name: 'msg',
10767 id: 2
10768 }, {
10769 rule: 'optional',
10770 type: 'bool',
10771 name: 'offline',
10772 id: 3
10773 }]
10774 }, {
10775 name: 'SessionCommand',
10776 syntax: 'proto2',
10777 fields: [{
10778 rule: 'optional',
10779 type: 'int64',
10780 name: 't',
10781 id: 1
10782 }, {
10783 rule: 'optional',
10784 type: 'string',
10785 name: 'n',
10786 id: 2
10787 }, {
10788 rule: 'optional',
10789 type: 'string',
10790 name: 's',
10791 id: 3
10792 }, {
10793 rule: 'optional',
10794 type: 'string',
10795 name: 'ua',
10796 id: 4
10797 }, {
10798 rule: 'optional',
10799 type: 'bool',
10800 name: 'r',
10801 id: 5
10802 }, {
10803 rule: 'optional',
10804 type: 'string',
10805 name: 'tag',
10806 id: 6
10807 }, {
10808 rule: 'optional',
10809 type: 'string',
10810 name: 'deviceId',
10811 id: 7
10812 }, {
10813 rule: 'repeated',
10814 type: 'string',
10815 name: 'sessionPeerIds',
10816 id: 8
10817 }, {
10818 rule: 'repeated',
10819 type: 'string',
10820 name: 'onlineSessionPeerIds',
10821 id: 9
10822 }, {
10823 rule: 'optional',
10824 type: 'string',
10825 name: 'st',
10826 id: 10
10827 }, {
10828 rule: 'optional',
10829 type: 'int32',
10830 name: 'stTtl',
10831 id: 11
10832 }, {
10833 rule: 'optional',
10834 type: 'int32',
10835 name: 'code',
10836 id: 12
10837 }, {
10838 rule: 'optional',
10839 type: 'string',
10840 name: 'reason',
10841 id: 13
10842 }, {
10843 rule: 'optional',
10844 type: 'string',
10845 name: 'deviceToken',
10846 id: 14
10847 }, {
10848 rule: 'optional',
10849 type: 'bool',
10850 name: 'sp',
10851 id: 15
10852 }, {
10853 rule: 'optional',
10854 type: 'string',
10855 name: 'detail',
10856 id: 16
10857 }, {
10858 rule: 'optional',
10859 type: 'int64',
10860 name: 'lastUnreadNotifTime',
10861 id: 17
10862 }, {
10863 rule: 'optional',
10864 type: 'int64',
10865 name: 'lastPatchTime',
10866 id: 18
10867 }, {
10868 rule: 'optional',
10869 type: 'int64',
10870 name: 'configBitmap',
10871 id: 19
10872 }]
10873 }, {
10874 name: 'ErrorCommand',
10875 syntax: 'proto2',
10876 fields: [{
10877 rule: 'required',
10878 type: 'int32',
10879 name: 'code',
10880 id: 1
10881 }, {
10882 rule: 'required',
10883 type: 'string',
10884 name: 'reason',
10885 id: 2
10886 }, {
10887 rule: 'optional',
10888 type: 'int32',
10889 name: 'appCode',
10890 id: 3
10891 }, {
10892 rule: 'optional',
10893 type: 'string',
10894 name: 'detail',
10895 id: 4
10896 }, {
10897 rule: 'repeated',
10898 type: 'string',
10899 name: 'pids',
10900 id: 5
10901 }, {
10902 rule: 'optional',
10903 type: 'string',
10904 name: 'appMsg',
10905 id: 6
10906 }]
10907 }, {
10908 name: 'DirectCommand',
10909 syntax: 'proto2',
10910 fields: [{
10911 rule: 'optional',
10912 type: 'string',
10913 name: 'msg',
10914 id: 1
10915 }, {
10916 rule: 'optional',
10917 type: 'string',
10918 name: 'uid',
10919 id: 2
10920 }, {
10921 rule: 'optional',
10922 type: 'string',
10923 name: 'fromPeerId',
10924 id: 3
10925 }, {
10926 rule: 'optional',
10927 type: 'int64',
10928 name: 'timestamp',
10929 id: 4
10930 }, {
10931 rule: 'optional',
10932 type: 'bool',
10933 name: 'offline',
10934 id: 5
10935 }, {
10936 rule: 'optional',
10937 type: 'bool',
10938 name: 'hasMore',
10939 id: 6
10940 }, {
10941 rule: 'repeated',
10942 type: 'string',
10943 name: 'toPeerIds',
10944 id: 7
10945 }, {
10946 rule: 'optional',
10947 type: 'bool',
10948 name: 'r',
10949 id: 10
10950 }, {
10951 rule: 'optional',
10952 type: 'string',
10953 name: 'cid',
10954 id: 11
10955 }, {
10956 rule: 'optional',
10957 type: 'string',
10958 name: 'id',
10959 id: 12
10960 }, {
10961 rule: 'optional',
10962 type: 'bool',
10963 name: 'transient',
10964 id: 13
10965 }, {
10966 rule: 'optional',
10967 type: 'string',
10968 name: 'dt',
10969 id: 14
10970 }, {
10971 rule: 'optional',
10972 type: 'string',
10973 name: 'roomId',
10974 id: 15
10975 }, {
10976 rule: 'optional',
10977 type: 'string',
10978 name: 'pushData',
10979 id: 16
10980 }, {
10981 rule: 'optional',
10982 type: 'bool',
10983 name: 'will',
10984 id: 17
10985 }, {
10986 rule: 'optional',
10987 type: 'int64',
10988 name: 'patchTimestamp',
10989 id: 18
10990 }, {
10991 rule: 'optional',
10992 type: 'bytes',
10993 name: 'binaryMsg',
10994 id: 19
10995 }, {
10996 rule: 'repeated',
10997 type: 'string',
10998 name: 'mentionPids',
10999 id: 20
11000 }, {
11001 rule: 'optional',
11002 type: 'bool',
11003 name: 'mentionAll',
11004 id: 21
11005 }, {
11006 rule: 'optional',
11007 type: 'int32',
11008 name: 'convType',
11009 id: 22
11010 }]
11011 }, {
11012 name: 'AckCommand',
11013 syntax: 'proto2',
11014 fields: [{
11015 rule: 'optional',
11016 type: 'int32',
11017 name: 'code',
11018 id: 1
11019 }, {
11020 rule: 'optional',
11021 type: 'string',
11022 name: 'reason',
11023 id: 2
11024 }, {
11025 rule: 'optional',
11026 type: 'string',
11027 name: 'mid',
11028 id: 3
11029 }, {
11030 rule: 'optional',
11031 type: 'string',
11032 name: 'cid',
11033 id: 4
11034 }, {
11035 rule: 'optional',
11036 type: 'int64',
11037 name: 't',
11038 id: 5
11039 }, {
11040 rule: 'optional',
11041 type: 'string',
11042 name: 'uid',
11043 id: 6
11044 }, {
11045 rule: 'optional',
11046 type: 'int64',
11047 name: 'fromts',
11048 id: 7
11049 }, {
11050 rule: 'optional',
11051 type: 'int64',
11052 name: 'tots',
11053 id: 8
11054 }, {
11055 rule: 'optional',
11056 type: 'string',
11057 name: 'type',
11058 id: 9
11059 }, {
11060 rule: 'repeated',
11061 type: 'string',
11062 name: 'ids',
11063 id: 10
11064 }, {
11065 rule: 'optional',
11066 type: 'int32',
11067 name: 'appCode',
11068 id: 11
11069 }, {
11070 rule: 'optional',
11071 type: 'string',
11072 name: 'appMsg',
11073 id: 12
11074 }]
11075 }, {
11076 name: 'UnreadCommand',
11077 syntax: 'proto2',
11078 fields: [{
11079 rule: 'repeated',
11080 type: 'UnreadTuple',
11081 name: 'convs',
11082 id: 1
11083 }, {
11084 rule: 'optional',
11085 type: 'int64',
11086 name: 'notifTime',
11087 id: 2
11088 }]
11089 }, {
11090 name: 'ConvCommand',
11091 syntax: 'proto2',
11092 fields: [{
11093 rule: 'repeated',
11094 type: 'string',
11095 name: 'm',
11096 id: 1
11097 }, {
11098 rule: 'optional',
11099 type: 'bool',
11100 name: 'transient',
11101 id: 2
11102 }, {
11103 rule: 'optional',
11104 type: 'bool',
11105 name: 'unique',
11106 id: 3
11107 }, {
11108 rule: 'optional',
11109 type: 'string',
11110 name: 'cid',
11111 id: 4
11112 }, {
11113 rule: 'optional',
11114 type: 'string',
11115 name: 'cdate',
11116 id: 5
11117 }, {
11118 rule: 'optional',
11119 type: 'string',
11120 name: 'initBy',
11121 id: 6
11122 }, {
11123 rule: 'optional',
11124 type: 'string',
11125 name: 'sort',
11126 id: 7
11127 }, {
11128 rule: 'optional',
11129 type: 'int32',
11130 name: 'limit',
11131 id: 8
11132 }, {
11133 rule: 'optional',
11134 type: 'int32',
11135 name: 'skip',
11136 id: 9
11137 }, {
11138 rule: 'optional',
11139 type: 'int32',
11140 name: 'flag',
11141 id: 10
11142 }, {
11143 rule: 'optional',
11144 type: 'int32',
11145 name: 'count',
11146 id: 11
11147 }, {
11148 rule: 'optional',
11149 type: 'string',
11150 name: 'udate',
11151 id: 12
11152 }, {
11153 rule: 'optional',
11154 type: 'int64',
11155 name: 't',
11156 id: 13
11157 }, {
11158 rule: 'optional',
11159 type: 'string',
11160 name: 'n',
11161 id: 14
11162 }, {
11163 rule: 'optional',
11164 type: 'string',
11165 name: 's',
11166 id: 15
11167 }, {
11168 rule: 'optional',
11169 type: 'bool',
11170 name: 'statusSub',
11171 id: 16
11172 }, {
11173 rule: 'optional',
11174 type: 'bool',
11175 name: 'statusPub',
11176 id: 17
11177 }, {
11178 rule: 'optional',
11179 type: 'int32',
11180 name: 'statusTTL',
11181 id: 18
11182 }, {
11183 rule: 'optional',
11184 type: 'string',
11185 name: 'uniqueId',
11186 id: 19
11187 }, {
11188 rule: 'optional',
11189 type: 'string',
11190 name: 'targetClientId',
11191 id: 20
11192 }, {
11193 rule: 'optional',
11194 type: 'int64',
11195 name: 'maxReadTimestamp',
11196 id: 21
11197 }, {
11198 rule: 'optional',
11199 type: 'int64',
11200 name: 'maxAckTimestamp',
11201 id: 22
11202 }, {
11203 rule: 'optional',
11204 type: 'bool',
11205 name: 'queryAllMembers',
11206 id: 23
11207 }, {
11208 rule: 'repeated',
11209 type: 'MaxReadTuple',
11210 name: 'maxReadTuples',
11211 id: 24
11212 }, {
11213 rule: 'repeated',
11214 type: 'string',
11215 name: 'cids',
11216 id: 25
11217 }, {
11218 rule: 'optional',
11219 type: 'ConvMemberInfo',
11220 name: 'info',
11221 id: 26
11222 }, {
11223 rule: 'optional',
11224 type: 'bool',
11225 name: 'tempConv',
11226 id: 27
11227 }, {
11228 rule: 'optional',
11229 type: 'int32',
11230 name: 'tempConvTTL',
11231 id: 28
11232 }, {
11233 rule: 'repeated',
11234 type: 'string',
11235 name: 'tempConvIds',
11236 id: 29
11237 }, {
11238 rule: 'repeated',
11239 type: 'string',
11240 name: 'allowedPids',
11241 id: 30
11242 }, {
11243 rule: 'repeated',
11244 type: 'ErrorCommand',
11245 name: 'failedPids',
11246 id: 31
11247 }, {
11248 rule: 'optional',
11249 type: 'string',
11250 name: 'next',
11251 id: 40
11252 }, {
11253 rule: 'optional',
11254 type: 'JsonObjectMessage',
11255 name: 'results',
11256 id: 100
11257 }, {
11258 rule: 'optional',
11259 type: 'JsonObjectMessage',
11260 name: 'where',
11261 id: 101
11262 }, {
11263 rule: 'optional',
11264 type: 'JsonObjectMessage',
11265 name: 'attr',
11266 id: 103
11267 }, {
11268 rule: 'optional',
11269 type: 'JsonObjectMessage',
11270 name: 'attrModified',
11271 id: 104
11272 }]
11273 }, {
11274 name: 'RoomCommand',
11275 syntax: 'proto2',
11276 fields: [{
11277 rule: 'optional',
11278 type: 'string',
11279 name: 'roomId',
11280 id: 1
11281 }, {
11282 rule: 'optional',
11283 type: 'string',
11284 name: 's',
11285 id: 2
11286 }, {
11287 rule: 'optional',
11288 type: 'int64',
11289 name: 't',
11290 id: 3
11291 }, {
11292 rule: 'optional',
11293 type: 'string',
11294 name: 'n',
11295 id: 4
11296 }, {
11297 rule: 'optional',
11298 type: 'bool',
11299 name: 'transient',
11300 id: 5
11301 }, {
11302 rule: 'repeated',
11303 type: 'string',
11304 name: 'roomPeerIds',
11305 id: 6
11306 }, {
11307 rule: 'optional',
11308 type: 'string',
11309 name: 'byPeerId',
11310 id: 7
11311 }]
11312 }, {
11313 name: 'LogsCommand',
11314 syntax: 'proto2',
11315 fields: [{
11316 rule: 'optional',
11317 type: 'string',
11318 name: 'cid',
11319 id: 1
11320 }, {
11321 rule: 'optional',
11322 type: 'int32',
11323 name: 'l',
11324 id: 2
11325 }, {
11326 rule: 'optional',
11327 type: 'int32',
11328 name: 'limit',
11329 id: 3
11330 }, {
11331 rule: 'optional',
11332 type: 'int64',
11333 name: 't',
11334 id: 4
11335 }, {
11336 rule: 'optional',
11337 type: 'int64',
11338 name: 'tt',
11339 id: 5
11340 }, {
11341 rule: 'optional',
11342 type: 'string',
11343 name: 'tmid',
11344 id: 6
11345 }, {
11346 rule: 'optional',
11347 type: 'string',
11348 name: 'mid',
11349 id: 7
11350 }, {
11351 rule: 'optional',
11352 type: 'string',
11353 name: 'checksum',
11354 id: 8
11355 }, {
11356 rule: 'optional',
11357 type: 'bool',
11358 name: 'stored',
11359 id: 9
11360 }, {
11361 rule: 'optional',
11362 type: 'QueryDirection',
11363 name: 'direction',
11364 id: 10,
11365 options: {
11366 default: 'OLD'
11367 }
11368 }, {
11369 rule: 'optional',
11370 type: 'bool',
11371 name: 'tIncluded',
11372 id: 11
11373 }, {
11374 rule: 'optional',
11375 type: 'bool',
11376 name: 'ttIncluded',
11377 id: 12
11378 }, {
11379 rule: 'optional',
11380 type: 'int32',
11381 name: 'lctype',
11382 id: 13
11383 }, {
11384 rule: 'repeated',
11385 type: 'LogItem',
11386 name: 'logs',
11387 id: 105
11388 }],
11389 enums: [{
11390 name: 'QueryDirection',
11391 syntax: 'proto2',
11392 values: [{
11393 name: 'OLD',
11394 id: 1
11395 }, {
11396 name: 'NEW',
11397 id: 2
11398 }]
11399 }]
11400 }, {
11401 name: 'RcpCommand',
11402 syntax: 'proto2',
11403 fields: [{
11404 rule: 'optional',
11405 type: 'string',
11406 name: 'id',
11407 id: 1
11408 }, {
11409 rule: 'optional',
11410 type: 'string',
11411 name: 'cid',
11412 id: 2
11413 }, {
11414 rule: 'optional',
11415 type: 'int64',
11416 name: 't',
11417 id: 3
11418 }, {
11419 rule: 'optional',
11420 type: 'bool',
11421 name: 'read',
11422 id: 4
11423 }, {
11424 rule: 'optional',
11425 type: 'string',
11426 name: 'from',
11427 id: 5
11428 }]
11429 }, {
11430 name: 'ReadTuple',
11431 syntax: 'proto2',
11432 fields: [{
11433 rule: 'required',
11434 type: 'string',
11435 name: 'cid',
11436 id: 1
11437 }, {
11438 rule: 'optional',
11439 type: 'int64',
11440 name: 'timestamp',
11441 id: 2
11442 }, {
11443 rule: 'optional',
11444 type: 'string',
11445 name: 'mid',
11446 id: 3
11447 }]
11448 }, {
11449 name: 'MaxReadTuple',
11450 syntax: 'proto2',
11451 fields: [{
11452 rule: 'optional',
11453 type: 'string',
11454 name: 'pid',
11455 id: 1
11456 }, {
11457 rule: 'optional',
11458 type: 'int64',
11459 name: 'maxAckTimestamp',
11460 id: 2
11461 }, {
11462 rule: 'optional',
11463 type: 'int64',
11464 name: 'maxReadTimestamp',
11465 id: 3
11466 }]
11467 }, {
11468 name: 'ReadCommand',
11469 syntax: 'proto2',
11470 fields: [{
11471 rule: 'optional',
11472 type: 'string',
11473 name: 'cid',
11474 id: 1
11475 }, {
11476 rule: 'repeated',
11477 type: 'string',
11478 name: 'cids',
11479 id: 2
11480 }, {
11481 rule: 'repeated',
11482 type: 'ReadTuple',
11483 name: 'convs',
11484 id: 3
11485 }]
11486 }, {
11487 name: 'PresenceCommand',
11488 syntax: 'proto2',
11489 fields: [{
11490 rule: 'optional',
11491 type: 'StatusType',
11492 name: 'status',
11493 id: 1
11494 }, {
11495 rule: 'repeated',
11496 type: 'string',
11497 name: 'sessionPeerIds',
11498 id: 2
11499 }, {
11500 rule: 'optional',
11501 type: 'string',
11502 name: 'cid',
11503 id: 3
11504 }]
11505 }, {
11506 name: 'ReportCommand',
11507 syntax: 'proto2',
11508 fields: [{
11509 rule: 'optional',
11510 type: 'bool',
11511 name: 'initiative',
11512 id: 1
11513 }, {
11514 rule: 'optional',
11515 type: 'string',
11516 name: 'type',
11517 id: 2
11518 }, {
11519 rule: 'optional',
11520 type: 'string',
11521 name: 'data',
11522 id: 3
11523 }]
11524 }, {
11525 name: 'PatchItem',
11526 syntax: 'proto2',
11527 fields: [{
11528 rule: 'optional',
11529 type: 'string',
11530 name: 'cid',
11531 id: 1
11532 }, {
11533 rule: 'optional',
11534 type: 'string',
11535 name: 'mid',
11536 id: 2
11537 }, {
11538 rule: 'optional',
11539 type: 'int64',
11540 name: 'timestamp',
11541 id: 3
11542 }, {
11543 rule: 'optional',
11544 type: 'bool',
11545 name: 'recall',
11546 id: 4
11547 }, {
11548 rule: 'optional',
11549 type: 'string',
11550 name: 'data',
11551 id: 5
11552 }, {
11553 rule: 'optional',
11554 type: 'int64',
11555 name: 'patchTimestamp',
11556 id: 6
11557 }, {
11558 rule: 'optional',
11559 type: 'string',
11560 name: 'from',
11561 id: 7
11562 }, {
11563 rule: 'optional',
11564 type: 'bytes',
11565 name: 'binaryMsg',
11566 id: 8
11567 }, {
11568 rule: 'optional',
11569 type: 'bool',
11570 name: 'mentionAll',
11571 id: 9
11572 }, {
11573 rule: 'repeated',
11574 type: 'string',
11575 name: 'mentionPids',
11576 id: 10
11577 }, {
11578 rule: 'optional',
11579 type: 'int64',
11580 name: 'patchCode',
11581 id: 11
11582 }, {
11583 rule: 'optional',
11584 type: 'string',
11585 name: 'patchReason',
11586 id: 12
11587 }]
11588 }, {
11589 name: 'PatchCommand',
11590 syntax: 'proto2',
11591 fields: [{
11592 rule: 'repeated',
11593 type: 'PatchItem',
11594 name: 'patches',
11595 id: 1
11596 }, {
11597 rule: 'optional',
11598 type: 'int64',
11599 name: 'lastPatchTime',
11600 id: 2
11601 }]
11602 }, {
11603 name: 'PubsubCommand',
11604 syntax: 'proto2',
11605 fields: [{
11606 rule: 'optional',
11607 type: 'string',
11608 name: 'cid',
11609 id: 1
11610 }, {
11611 rule: 'repeated',
11612 type: 'string',
11613 name: 'cids',
11614 id: 2
11615 }, {
11616 rule: 'optional',
11617 type: 'string',
11618 name: 'topic',
11619 id: 3
11620 }, {
11621 rule: 'optional',
11622 type: 'string',
11623 name: 'subtopic',
11624 id: 4
11625 }, {
11626 rule: 'repeated',
11627 type: 'string',
11628 name: 'topics',
11629 id: 5
11630 }, {
11631 rule: 'repeated',
11632 type: 'string',
11633 name: 'subtopics',
11634 id: 6
11635 }, {
11636 rule: 'optional',
11637 type: 'JsonObjectMessage',
11638 name: 'results',
11639 id: 7
11640 }]
11641 }, {
11642 name: 'BlacklistCommand',
11643 syntax: 'proto2',
11644 fields: [{
11645 rule: 'optional',
11646 type: 'string',
11647 name: 'srcCid',
11648 id: 1
11649 }, {
11650 rule: 'repeated',
11651 type: 'string',
11652 name: 'toPids',
11653 id: 2
11654 }, {
11655 rule: 'optional',
11656 type: 'string',
11657 name: 'srcPid',
11658 id: 3
11659 }, {
11660 rule: 'repeated',
11661 type: 'string',
11662 name: 'toCids',
11663 id: 4
11664 }, {
11665 rule: 'optional',
11666 type: 'int32',
11667 name: 'limit',
11668 id: 5
11669 }, {
11670 rule: 'optional',
11671 type: 'string',
11672 name: 'next',
11673 id: 6
11674 }, {
11675 rule: 'repeated',
11676 type: 'string',
11677 name: 'blockedPids',
11678 id: 8
11679 }, {
11680 rule: 'repeated',
11681 type: 'string',
11682 name: 'blockedCids',
11683 id: 9
11684 }, {
11685 rule: 'repeated',
11686 type: 'string',
11687 name: 'allowedPids',
11688 id: 10
11689 }, {
11690 rule: 'repeated',
11691 type: 'ErrorCommand',
11692 name: 'failedPids',
11693 id: 11
11694 }, {
11695 rule: 'optional',
11696 type: 'int64',
11697 name: 't',
11698 id: 12
11699 }, {
11700 rule: 'optional',
11701 type: 'string',
11702 name: 'n',
11703 id: 13
11704 }, {
11705 rule: 'optional',
11706 type: 'string',
11707 name: 's',
11708 id: 14
11709 }]
11710 }, {
11711 name: 'GenericCommand',
11712 syntax: 'proto2',
11713 fields: [{
11714 rule: 'optional',
11715 type: 'CommandType',
11716 name: 'cmd',
11717 id: 1
11718 }, {
11719 rule: 'optional',
11720 type: 'OpType',
11721 name: 'op',
11722 id: 2
11723 }, {
11724 rule: 'optional',
11725 type: 'string',
11726 name: 'appId',
11727 id: 3
11728 }, {
11729 rule: 'optional',
11730 type: 'string',
11731 name: 'peerId',
11732 id: 4
11733 }, {
11734 rule: 'optional',
11735 type: 'int32',
11736 name: 'i',
11737 id: 5
11738 }, {
11739 rule: 'optional',
11740 type: 'string',
11741 name: 'installationId',
11742 id: 6
11743 }, {
11744 rule: 'optional',
11745 type: 'int32',
11746 name: 'priority',
11747 id: 7
11748 }, {
11749 rule: 'optional',
11750 type: 'int32',
11751 name: 'service',
11752 id: 8
11753 }, {
11754 rule: 'optional',
11755 type: 'int64',
11756 name: 'serverTs',
11757 id: 9
11758 }, {
11759 rule: 'optional',
11760 type: 'int64',
11761 name: 'clientTs',
11762 id: 10
11763 }, {
11764 rule: 'optional',
11765 type: 'int32',
11766 name: 'notificationType',
11767 id: 11
11768 }, {
11769 rule: 'optional',
11770 type: 'DataCommand',
11771 name: 'dataMessage',
11772 id: 101
11773 }, {
11774 rule: 'optional',
11775 type: 'SessionCommand',
11776 name: 'sessionMessage',
11777 id: 102
11778 }, {
11779 rule: 'optional',
11780 type: 'ErrorCommand',
11781 name: 'errorMessage',
11782 id: 103
11783 }, {
11784 rule: 'optional',
11785 type: 'DirectCommand',
11786 name: 'directMessage',
11787 id: 104
11788 }, {
11789 rule: 'optional',
11790 type: 'AckCommand',
11791 name: 'ackMessage',
11792 id: 105
11793 }, {
11794 rule: 'optional',
11795 type: 'UnreadCommand',
11796 name: 'unreadMessage',
11797 id: 106
11798 }, {
11799 rule: 'optional',
11800 type: 'ReadCommand',
11801 name: 'readMessage',
11802 id: 107
11803 }, {
11804 rule: 'optional',
11805 type: 'RcpCommand',
11806 name: 'rcpMessage',
11807 id: 108
11808 }, {
11809 rule: 'optional',
11810 type: 'LogsCommand',
11811 name: 'logsMessage',
11812 id: 109
11813 }, {
11814 rule: 'optional',
11815 type: 'ConvCommand',
11816 name: 'convMessage',
11817 id: 110
11818 }, {
11819 rule: 'optional',
11820 type: 'RoomCommand',
11821 name: 'roomMessage',
11822 id: 111
11823 }, {
11824 rule: 'optional',
11825 type: 'PresenceCommand',
11826 name: 'presenceMessage',
11827 id: 112
11828 }, {
11829 rule: 'optional',
11830 type: 'ReportCommand',
11831 name: 'reportMessage',
11832 id: 113
11833 }, {
11834 rule: 'optional',
11835 type: 'PatchCommand',
11836 name: 'patchMessage',
11837 id: 114
11838 }, {
11839 rule: 'optional',
11840 type: 'PubsubCommand',
11841 name: 'pubsubMessage',
11842 id: 115
11843 }, {
11844 rule: 'optional',
11845 type: 'BlacklistCommand',
11846 name: 'blacklistMessage',
11847 id: 116
11848 }]
11849 }],
11850 enums: [{
11851 name: 'CommandType',
11852 syntax: 'proto2',
11853 values: [{
11854 name: 'session',
11855 id: 0
11856 }, {
11857 name: 'conv',
11858 id: 1
11859 }, {
11860 name: 'direct',
11861 id: 2
11862 }, {
11863 name: 'ack',
11864 id: 3
11865 }, {
11866 name: 'rcp',
11867 id: 4
11868 }, {
11869 name: 'unread',
11870 id: 5
11871 }, {
11872 name: 'logs',
11873 id: 6
11874 }, {
11875 name: 'error',
11876 id: 7
11877 }, {
11878 name: 'login',
11879 id: 8
11880 }, {
11881 name: 'data',
11882 id: 9
11883 }, {
11884 name: 'room',
11885 id: 10
11886 }, {
11887 name: 'read',
11888 id: 11
11889 }, {
11890 name: 'presence',
11891 id: 12
11892 }, {
11893 name: 'report',
11894 id: 13
11895 }, {
11896 name: 'echo',
11897 id: 14
11898 }, {
11899 name: 'loggedin',
11900 id: 15
11901 }, {
11902 name: 'logout',
11903 id: 16
11904 }, {
11905 name: 'loggedout',
11906 id: 17
11907 }, {
11908 name: 'patch',
11909 id: 18
11910 }, {
11911 name: 'pubsub',
11912 id: 19
11913 }, {
11914 name: 'blacklist',
11915 id: 20
11916 }, {
11917 name: 'goaway',
11918 id: 21
11919 }]
11920 }, {
11921 name: 'OpType',
11922 syntax: 'proto2',
11923 values: [{
11924 name: 'open',
11925 id: 1
11926 }, {
11927 name: 'add',
11928 id: 2
11929 }, {
11930 name: 'remove',
11931 id: 3
11932 }, {
11933 name: 'close',
11934 id: 4
11935 }, {
11936 name: 'opened',
11937 id: 5
11938 }, {
11939 name: 'closed',
11940 id: 6
11941 }, {
11942 name: 'query',
11943 id: 7
11944 }, {
11945 name: 'query_result',
11946 id: 8
11947 }, {
11948 name: 'conflict',
11949 id: 9
11950 }, {
11951 name: 'added',
11952 id: 10
11953 }, {
11954 name: 'removed',
11955 id: 11
11956 }, {
11957 name: 'refresh',
11958 id: 12
11959 }, {
11960 name: 'refreshed',
11961 id: 13
11962 }, {
11963 name: 'start',
11964 id: 30
11965 }, {
11966 name: 'started',
11967 id: 31
11968 }, {
11969 name: 'joined',
11970 id: 32
11971 }, {
11972 name: 'members_joined',
11973 id: 33
11974 }, {
11975 name: 'left',
11976 id: 39
11977 }, {
11978 name: 'members_left',
11979 id: 40
11980 }, {
11981 name: 'results',
11982 id: 42
11983 }, {
11984 name: 'count',
11985 id: 43
11986 }, {
11987 name: 'result',
11988 id: 44
11989 }, {
11990 name: 'update',
11991 id: 45
11992 }, {
11993 name: 'updated',
11994 id: 46
11995 }, {
11996 name: 'mute',
11997 id: 47
11998 }, {
11999 name: 'unmute',
12000 id: 48
12001 }, {
12002 name: 'status',
12003 id: 49
12004 }, {
12005 name: 'members',
12006 id: 50
12007 }, {
12008 name: 'max_read',
12009 id: 51
12010 }, {
12011 name: 'is_member',
12012 id: 52
12013 }, {
12014 name: 'member_info_update',
12015 id: 53
12016 }, {
12017 name: 'member_info_updated',
12018 id: 54
12019 }, {
12020 name: 'member_info_changed',
12021 id: 55
12022 }, {
12023 name: 'join',
12024 id: 80
12025 }, {
12026 name: 'invite',
12027 id: 81
12028 }, {
12029 name: 'leave',
12030 id: 82
12031 }, {
12032 name: 'kick',
12033 id: 83
12034 }, {
12035 name: 'reject',
12036 id: 84
12037 }, {
12038 name: 'invited',
12039 id: 85
12040 }, {
12041 name: 'kicked',
12042 id: 86
12043 }, {
12044 name: 'upload',
12045 id: 100
12046 }, {
12047 name: 'uploaded',
12048 id: 101
12049 }, {
12050 name: 'subscribe',
12051 id: 120
12052 }, {
12053 name: 'subscribed',
12054 id: 121
12055 }, {
12056 name: 'unsubscribe',
12057 id: 122
12058 }, {
12059 name: 'unsubscribed',
12060 id: 123
12061 }, {
12062 name: 'is_subscribed',
12063 id: 124
12064 }, {
12065 name: 'modify',
12066 id: 150
12067 }, {
12068 name: 'modified',
12069 id: 151
12070 }, {
12071 name: 'block',
12072 id: 170
12073 }, {
12074 name: 'unblock',
12075 id: 171
12076 }, {
12077 name: 'blocked',
12078 id: 172
12079 }, {
12080 name: 'unblocked',
12081 id: 173
12082 }, {
12083 name: 'members_blocked',
12084 id: 174
12085 }, {
12086 name: 'members_unblocked',
12087 id: 175
12088 }, {
12089 name: 'check_block',
12090 id: 176
12091 }, {
12092 name: 'check_result',
12093 id: 177
12094 }, {
12095 name: 'add_shutup',
12096 id: 180
12097 }, {
12098 name: 'remove_shutup',
12099 id: 181
12100 }, {
12101 name: 'query_shutup',
12102 id: 182
12103 }, {
12104 name: 'shutup_added',
12105 id: 183
12106 }, {
12107 name: 'shutup_removed',
12108 id: 184
12109 }, {
12110 name: 'shutup_result',
12111 id: 185
12112 }, {
12113 name: 'shutuped',
12114 id: 186
12115 }, {
12116 name: 'unshutuped',
12117 id: 187
12118 }, {
12119 name: 'members_shutuped',
12120 id: 188
12121 }, {
12122 name: 'members_unshutuped',
12123 id: 189
12124 }, {
12125 name: 'check_shutup',
12126 id: 190
12127 }]
12128 }, {
12129 name: 'StatusType',
12130 syntax: 'proto2',
12131 values: [{
12132 name: 'on',
12133 id: 1
12134 }, {
12135 name: 'off',
12136 id: 2
12137 }]
12138 }],
12139 isNamespace: true
12140 }).build();
12141
12142 var _messages$push_server = messageCompiled.push_server.messages2,
12143 JsonObjectMessage = _messages$push_server.JsonObjectMessage,
12144 UnreadTuple = _messages$push_server.UnreadTuple,
12145 LogItem = _messages$push_server.LogItem,
12146 DataCommand = _messages$push_server.DataCommand,
12147 SessionCommand = _messages$push_server.SessionCommand,
12148 ErrorCommand = _messages$push_server.ErrorCommand,
12149 DirectCommand = _messages$push_server.DirectCommand,
12150 AckCommand = _messages$push_server.AckCommand,
12151 UnreadCommand = _messages$push_server.UnreadCommand,
12152 ConvCommand = _messages$push_server.ConvCommand,
12153 RoomCommand = _messages$push_server.RoomCommand,
12154 LogsCommand = _messages$push_server.LogsCommand,
12155 RcpCommand = _messages$push_server.RcpCommand,
12156 ReadTuple = _messages$push_server.ReadTuple,
12157 MaxReadTuple = _messages$push_server.MaxReadTuple,
12158 ReadCommand = _messages$push_server.ReadCommand,
12159 PresenceCommand = _messages$push_server.PresenceCommand,
12160 ReportCommand = _messages$push_server.ReportCommand,
12161 GenericCommand = _messages$push_server.GenericCommand,
12162 BlacklistCommand = _messages$push_server.BlacklistCommand,
12163 PatchCommand = _messages$push_server.PatchCommand,
12164 PatchItem = _messages$push_server.PatchItem,
12165 ConvMemberInfo = _messages$push_server.ConvMemberInfo,
12166 CommandType = _messages$push_server.CommandType,
12167 OpType = _messages$push_server.OpType,
12168 StatusType = _messages$push_server.StatusType;
12169
12170 var message = /*#__PURE__*/Object.freeze({
12171 JsonObjectMessage: JsonObjectMessage,
12172 UnreadTuple: UnreadTuple,
12173 LogItem: LogItem,
12174 DataCommand: DataCommand,
12175 SessionCommand: SessionCommand,
12176 ErrorCommand: ErrorCommand,
12177 DirectCommand: DirectCommand,
12178 AckCommand: AckCommand,
12179 UnreadCommand: UnreadCommand,
12180 ConvCommand: ConvCommand,
12181 RoomCommand: RoomCommand,
12182 LogsCommand: LogsCommand,
12183 RcpCommand: RcpCommand,
12184 ReadTuple: ReadTuple,
12185 MaxReadTuple: MaxReadTuple,
12186 ReadCommand: ReadCommand,
12187 PresenceCommand: PresenceCommand,
12188 ReportCommand: ReportCommand,
12189 GenericCommand: GenericCommand,
12190 BlacklistCommand: BlacklistCommand,
12191 PatchCommand: PatchCommand,
12192 PatchItem: PatchItem,
12193 ConvMemberInfo: ConvMemberInfo,
12194 CommandType: CommandType,
12195 OpType: OpType,
12196 StatusType: StatusType
12197 });
12198
12199 var eventemitter3 = createCommonjsModule(function (module) {
12200
12201 var has = Object.prototype.hasOwnProperty
12202 , prefix = '~';
12203
12204 /**
12205 * Constructor to create a storage for our `EE` objects.
12206 * An `Events` instance is a plain object whose properties are event names.
12207 *
12208 * @constructor
12209 * @private
12210 */
12211 function Events() {}
12212
12213 //
12214 // We try to not inherit from `Object.prototype`. In some engines creating an
12215 // instance in this way is faster than calling `Object.create(null)` directly.
12216 // If `Object.create(null)` is not supported we prefix the event names with a
12217 // character to make sure that the built-in object properties are not
12218 // overridden or used as an attack vector.
12219 //
12220 if (Object.create) {
12221 Events.prototype = Object.create(null);
12222
12223 //
12224 // This hack is needed because the `__proto__` property is still inherited in
12225 // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
12226 //
12227 if (!new Events().__proto__) prefix = false;
12228 }
12229
12230 /**
12231 * Representation of a single event listener.
12232 *
12233 * @param {Function} fn The listener function.
12234 * @param {*} context The context to invoke the listener with.
12235 * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
12236 * @constructor
12237 * @private
12238 */
12239 function EE(fn, context, once) {
12240 this.fn = fn;
12241 this.context = context;
12242 this.once = once || false;
12243 }
12244
12245 /**
12246 * Add a listener for a given event.
12247 *
12248 * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
12249 * @param {(String|Symbol)} event The event name.
12250 * @param {Function} fn The listener function.
12251 * @param {*} context The context to invoke the listener with.
12252 * @param {Boolean} once Specify if the listener is a one-time listener.
12253 * @returns {EventEmitter}
12254 * @private
12255 */
12256 function addListener(emitter, event, fn, context, once) {
12257 if (typeof fn !== 'function') {
12258 throw new TypeError('The listener must be a function');
12259 }
12260
12261 var listener = new EE(fn, context || emitter, once)
12262 , evt = prefix ? prefix + event : event;
12263
12264 if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
12265 else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
12266 else emitter._events[evt] = [emitter._events[evt], listener];
12267
12268 return emitter;
12269 }
12270
12271 /**
12272 * Clear event by name.
12273 *
12274 * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
12275 * @param {(String|Symbol)} evt The Event name.
12276 * @private
12277 */
12278 function clearEvent(emitter, evt) {
12279 if (--emitter._eventsCount === 0) emitter._events = new Events();
12280 else delete emitter._events[evt];
12281 }
12282
12283 /**
12284 * Minimal `EventEmitter` interface that is molded against the Node.js
12285 * `EventEmitter` interface.
12286 *
12287 * @constructor
12288 * @public
12289 */
12290 function EventEmitter() {
12291 this._events = new Events();
12292 this._eventsCount = 0;
12293 }
12294
12295 /**
12296 * Return an array listing the events for which the emitter has registered
12297 * listeners.
12298 *
12299 * @returns {Array}
12300 * @public
12301 */
12302 EventEmitter.prototype.eventNames = function eventNames() {
12303 var names = []
12304 , events
12305 , name;
12306
12307 if (this._eventsCount === 0) return names;
12308
12309 for (name in (events = this._events)) {
12310 if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
12311 }
12312
12313 if (Object.getOwnPropertySymbols) {
12314 return names.concat(Object.getOwnPropertySymbols(events));
12315 }
12316
12317 return names;
12318 };
12319
12320 /**
12321 * Return the listeners registered for a given event.
12322 *
12323 * @param {(String|Symbol)} event The event name.
12324 * @returns {Array} The registered listeners.
12325 * @public
12326 */
12327 EventEmitter.prototype.listeners = function listeners(event) {
12328 var evt = prefix ? prefix + event : event
12329 , handlers = this._events[evt];
12330
12331 if (!handlers) return [];
12332 if (handlers.fn) return [handlers.fn];
12333
12334 for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
12335 ee[i] = handlers[i].fn;
12336 }
12337
12338 return ee;
12339 };
12340
12341 /**
12342 * Return the number of listeners listening to a given event.
12343 *
12344 * @param {(String|Symbol)} event The event name.
12345 * @returns {Number} The number of listeners.
12346 * @public
12347 */
12348 EventEmitter.prototype.listenerCount = function listenerCount(event) {
12349 var evt = prefix ? prefix + event : event
12350 , listeners = this._events[evt];
12351
12352 if (!listeners) return 0;
12353 if (listeners.fn) return 1;
12354 return listeners.length;
12355 };
12356
12357 /**
12358 * Calls each of the listeners registered for a given event.
12359 *
12360 * @param {(String|Symbol)} event The event name.
12361 * @returns {Boolean} `true` if the event had listeners, else `false`.
12362 * @public
12363 */
12364 EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
12365 var evt = prefix ? prefix + event : event;
12366
12367 if (!this._events[evt]) return false;
12368
12369 var listeners = this._events[evt]
12370 , len = arguments.length
12371 , args
12372 , i;
12373
12374 if (listeners.fn) {
12375 if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
12376
12377 switch (len) {
12378 case 1: return listeners.fn.call(listeners.context), true;
12379 case 2: return listeners.fn.call(listeners.context, a1), true;
12380 case 3: return listeners.fn.call(listeners.context, a1, a2), true;
12381 case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
12382 case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
12383 case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
12384 }
12385
12386 for (i = 1, args = new Array(len -1); i < len; i++) {
12387 args[i - 1] = arguments[i];
12388 }
12389
12390 listeners.fn.apply(listeners.context, args);
12391 } else {
12392 var length = listeners.length
12393 , j;
12394
12395 for (i = 0; i < length; i++) {
12396 if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
12397
12398 switch (len) {
12399 case 1: listeners[i].fn.call(listeners[i].context); break;
12400 case 2: listeners[i].fn.call(listeners[i].context, a1); break;
12401 case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
12402 case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
12403 default:
12404 if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
12405 args[j - 1] = arguments[j];
12406 }
12407
12408 listeners[i].fn.apply(listeners[i].context, args);
12409 }
12410 }
12411 }
12412
12413 return true;
12414 };
12415
12416 /**
12417 * Add a listener for a given event.
12418 *
12419 * @param {(String|Symbol)} event The event name.
12420 * @param {Function} fn The listener function.
12421 * @param {*} [context=this] The context to invoke the listener with.
12422 * @returns {EventEmitter} `this`.
12423 * @public
12424 */
12425 EventEmitter.prototype.on = function on(event, fn, context) {
12426 return addListener(this, event, fn, context, false);
12427 };
12428
12429 /**
12430 * Add a one-time listener for a given event.
12431 *
12432 * @param {(String|Symbol)} event The event name.
12433 * @param {Function} fn The listener function.
12434 * @param {*} [context=this] The context to invoke the listener with.
12435 * @returns {EventEmitter} `this`.
12436 * @public
12437 */
12438 EventEmitter.prototype.once = function once(event, fn, context) {
12439 return addListener(this, event, fn, context, true);
12440 };
12441
12442 /**
12443 * Remove the listeners of a given event.
12444 *
12445 * @param {(String|Symbol)} event The event name.
12446 * @param {Function} fn Only remove the listeners that match this function.
12447 * @param {*} context Only remove the listeners that have this context.
12448 * @param {Boolean} once Only remove one-time listeners.
12449 * @returns {EventEmitter} `this`.
12450 * @public
12451 */
12452 EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
12453 var evt = prefix ? prefix + event : event;
12454
12455 if (!this._events[evt]) return this;
12456 if (!fn) {
12457 clearEvent(this, evt);
12458 return this;
12459 }
12460
12461 var listeners = this._events[evt];
12462
12463 if (listeners.fn) {
12464 if (
12465 listeners.fn === fn &&
12466 (!once || listeners.once) &&
12467 (!context || listeners.context === context)
12468 ) {
12469 clearEvent(this, evt);
12470 }
12471 } else {
12472 for (var i = 0, events = [], length = listeners.length; i < length; i++) {
12473 if (
12474 listeners[i].fn !== fn ||
12475 (once && !listeners[i].once) ||
12476 (context && listeners[i].context !== context)
12477 ) {
12478 events.push(listeners[i]);
12479 }
12480 }
12481
12482 //
12483 // Reset the array, or remove it completely if we have no more listeners.
12484 //
12485 if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
12486 else clearEvent(this, evt);
12487 }
12488
12489 return this;
12490 };
12491
12492 /**
12493 * Remove all listeners, or those of the specified event.
12494 *
12495 * @param {(String|Symbol)} [event] The event name.
12496 * @returns {EventEmitter} `this`.
12497 * @public
12498 */
12499 EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
12500 var evt;
12501
12502 if (event) {
12503 evt = prefix ? prefix + event : event;
12504 if (this._events[evt]) clearEvent(this, evt);
12505 } else {
12506 this._events = new Events();
12507 this._eventsCount = 0;
12508 }
12509
12510 return this;
12511 };
12512
12513 //
12514 // Alias methods names because people roll like that.
12515 //
12516 EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
12517 EventEmitter.prototype.addListener = EventEmitter.prototype.on;
12518
12519 //
12520 // Expose the prefix.
12521 //
12522 EventEmitter.prefixed = prefix;
12523
12524 //
12525 // Allow `EventEmitter` to be imported as module namespace.
12526 //
12527 EventEmitter.EventEmitter = EventEmitter;
12528
12529 //
12530 // Expose the module.
12531 //
12532 {
12533 module.exports = EventEmitter;
12534 }
12535 });
12536
12537 var runtime = createCommonjsModule(function (module) {
12538 /**
12539 * Copyright (c) 2014-present, Facebook, Inc.
12540 *
12541 * This source code is licensed under the MIT license found in the
12542 * LICENSE file in the root directory of this source tree.
12543 */
12544
12545 !(function(global) {
12546
12547 var Op = Object.prototype;
12548 var hasOwn = Op.hasOwnProperty;
12549 var undefined$1; // More compressible than void 0.
12550 var $Symbol = typeof Symbol === "function" ? Symbol : {};
12551 var iteratorSymbol = $Symbol.iterator || "@@iterator";
12552 var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
12553 var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
12554 var runtime = global.regeneratorRuntime;
12555 if (runtime) {
12556 {
12557 // If regeneratorRuntime is defined globally and we're in a module,
12558 // make the exports object identical to regeneratorRuntime.
12559 module.exports = runtime;
12560 }
12561 // Don't bother evaluating the rest of this file if the runtime was
12562 // already defined globally.
12563 return;
12564 }
12565
12566 // Define the runtime globally (as expected by generated code) as either
12567 // module.exports (if we're in a module) or a new, empty object.
12568 runtime = global.regeneratorRuntime = module.exports;
12569
12570 function wrap(innerFn, outerFn, self, tryLocsList) {
12571 // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
12572 var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
12573 var generator = Object.create(protoGenerator.prototype);
12574 var context = new Context(tryLocsList || []);
12575
12576 // The ._invoke method unifies the implementations of the .next,
12577 // .throw, and .return methods.
12578 generator._invoke = makeInvokeMethod(innerFn, self, context);
12579
12580 return generator;
12581 }
12582 runtime.wrap = wrap;
12583
12584 // Try/catch helper to minimize deoptimizations. Returns a completion
12585 // record like context.tryEntries[i].completion. This interface could
12586 // have been (and was previously) designed to take a closure to be
12587 // invoked without arguments, but in all the cases we care about we
12588 // already have an existing method we want to call, so there's no need
12589 // to create a new function object. We can even get away with assuming
12590 // the method takes exactly one argument, since that happens to be true
12591 // in every case, so we don't have to touch the arguments object. The
12592 // only additional allocation required is the completion record, which
12593 // has a stable shape and so hopefully should be cheap to allocate.
12594 function tryCatch(fn, obj, arg) {
12595 try {
12596 return { type: "normal", arg: fn.call(obj, arg) };
12597 } catch (err) {
12598 return { type: "throw", arg: err };
12599 }
12600 }
12601
12602 var GenStateSuspendedStart = "suspendedStart";
12603 var GenStateSuspendedYield = "suspendedYield";
12604 var GenStateExecuting = "executing";
12605 var GenStateCompleted = "completed";
12606
12607 // Returning this object from the innerFn has the same effect as
12608 // breaking out of the dispatch switch statement.
12609 var ContinueSentinel = {};
12610
12611 // Dummy constructor functions that we use as the .constructor and
12612 // .constructor.prototype properties for functions that return Generator
12613 // objects. For full spec compliance, you may wish to configure your
12614 // minifier not to mangle the names of these two functions.
12615 function Generator() {}
12616 function GeneratorFunction() {}
12617 function GeneratorFunctionPrototype() {}
12618
12619 // This is a polyfill for %IteratorPrototype% for environments that
12620 // don't natively support it.
12621 var IteratorPrototype = {};
12622 IteratorPrototype[iteratorSymbol] = function () {
12623 return this;
12624 };
12625
12626 var getProto = Object.getPrototypeOf;
12627 var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
12628 if (NativeIteratorPrototype &&
12629 NativeIteratorPrototype !== Op &&
12630 hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
12631 // This environment has a native %IteratorPrototype%; use it instead
12632 // of the polyfill.
12633 IteratorPrototype = NativeIteratorPrototype;
12634 }
12635
12636 var Gp = GeneratorFunctionPrototype.prototype =
12637 Generator.prototype = Object.create(IteratorPrototype);
12638 GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
12639 GeneratorFunctionPrototype.constructor = GeneratorFunction;
12640 GeneratorFunctionPrototype[toStringTagSymbol] =
12641 GeneratorFunction.displayName = "GeneratorFunction";
12642
12643 // Helper for defining the .next, .throw, and .return methods of the
12644 // Iterator interface in terms of a single ._invoke method.
12645 function defineIteratorMethods(prototype) {
12646 ["next", "throw", "return"].forEach(function(method) {
12647 prototype[method] = function(arg) {
12648 return this._invoke(method, arg);
12649 };
12650 });
12651 }
12652
12653 runtime.isGeneratorFunction = function(genFun) {
12654 var ctor = typeof genFun === "function" && genFun.constructor;
12655 return ctor
12656 ? ctor === GeneratorFunction ||
12657 // For the native GeneratorFunction constructor, the best we can
12658 // do is to check its .name property.
12659 (ctor.displayName || ctor.name) === "GeneratorFunction"
12660 : false;
12661 };
12662
12663 runtime.mark = function(genFun) {
12664 if (Object.setPrototypeOf) {
12665 Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
12666 } else {
12667 genFun.__proto__ = GeneratorFunctionPrototype;
12668 if (!(toStringTagSymbol in genFun)) {
12669 genFun[toStringTagSymbol] = "GeneratorFunction";
12670 }
12671 }
12672 genFun.prototype = Object.create(Gp);
12673 return genFun;
12674 };
12675
12676 // Within the body of any async function, `await x` is transformed to
12677 // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
12678 // `hasOwn.call(value, "__await")` to determine if the yielded value is
12679 // meant to be awaited.
12680 runtime.awrap = function(arg) {
12681 return { __await: arg };
12682 };
12683
12684 function AsyncIterator(generator) {
12685 function invoke(method, arg, resolve, reject) {
12686 var record = tryCatch(generator[method], generator, arg);
12687 if (record.type === "throw") {
12688 reject(record.arg);
12689 } else {
12690 var result = record.arg;
12691 var value = result.value;
12692 if (value &&
12693 typeof value === "object" &&
12694 hasOwn.call(value, "__await")) {
12695 return Promise.resolve(value.__await).then(function(value) {
12696 invoke("next", value, resolve, reject);
12697 }, function(err) {
12698 invoke("throw", err, resolve, reject);
12699 });
12700 }
12701
12702 return Promise.resolve(value).then(function(unwrapped) {
12703 // When a yielded Promise is resolved, its final value becomes
12704 // the .value of the Promise<{value,done}> result for the
12705 // current iteration.
12706 result.value = unwrapped;
12707 resolve(result);
12708 }, function(error) {
12709 // If a rejected Promise was yielded, throw the rejection back
12710 // into the async generator function so it can be handled there.
12711 return invoke("throw", error, resolve, reject);
12712 });
12713 }
12714 }
12715
12716 var previousPromise;
12717
12718 function enqueue(method, arg) {
12719 function callInvokeWithMethodAndArg() {
12720 return new Promise(function(resolve, reject) {
12721 invoke(method, arg, resolve, reject);
12722 });
12723 }
12724
12725 return previousPromise =
12726 // If enqueue has been called before, then we want to wait until
12727 // all previous Promises have been resolved before calling invoke,
12728 // so that results are always delivered in the correct order. If
12729 // enqueue has not been called before, then it is important to
12730 // call invoke immediately, without waiting on a callback to fire,
12731 // so that the async generator function has the opportunity to do
12732 // any necessary setup in a predictable way. This predictability
12733 // is why the Promise constructor synchronously invokes its
12734 // executor callback, and why async functions synchronously
12735 // execute code before the first await. Since we implement simple
12736 // async functions in terms of async generators, it is especially
12737 // important to get this right, even though it requires care.
12738 previousPromise ? previousPromise.then(
12739 callInvokeWithMethodAndArg,
12740 // Avoid propagating failures to Promises returned by later
12741 // invocations of the iterator.
12742 callInvokeWithMethodAndArg
12743 ) : callInvokeWithMethodAndArg();
12744 }
12745
12746 // Define the unified helper method that is used to implement .next,
12747 // .throw, and .return (see defineIteratorMethods).
12748 this._invoke = enqueue;
12749 }
12750
12751 defineIteratorMethods(AsyncIterator.prototype);
12752 AsyncIterator.prototype[asyncIteratorSymbol] = function () {
12753 return this;
12754 };
12755 runtime.AsyncIterator = AsyncIterator;
12756
12757 // Note that simple async functions are implemented on top of
12758 // AsyncIterator objects; they just return a Promise for the value of
12759 // the final result produced by the iterator.
12760 runtime.async = function(innerFn, outerFn, self, tryLocsList) {
12761 var iter = new AsyncIterator(
12762 wrap(innerFn, outerFn, self, tryLocsList)
12763 );
12764
12765 return runtime.isGeneratorFunction(outerFn)
12766 ? iter // If outerFn is a generator, return the full iterator.
12767 : iter.next().then(function(result) {
12768 return result.done ? result.value : iter.next();
12769 });
12770 };
12771
12772 function makeInvokeMethod(innerFn, self, context) {
12773 var state = GenStateSuspendedStart;
12774
12775 return function invoke(method, arg) {
12776 if (state === GenStateExecuting) {
12777 throw new Error("Generator is already running");
12778 }
12779
12780 if (state === GenStateCompleted) {
12781 if (method === "throw") {
12782 throw arg;
12783 }
12784
12785 // Be forgiving, per 25.3.3.3.3 of the spec:
12786 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
12787 return doneResult();
12788 }
12789
12790 context.method = method;
12791 context.arg = arg;
12792
12793 while (true) {
12794 var delegate = context.delegate;
12795 if (delegate) {
12796 var delegateResult = maybeInvokeDelegate(delegate, context);
12797 if (delegateResult) {
12798 if (delegateResult === ContinueSentinel) continue;
12799 return delegateResult;
12800 }
12801 }
12802
12803 if (context.method === "next") {
12804 // Setting context._sent for legacy support of Babel's
12805 // function.sent implementation.
12806 context.sent = context._sent = context.arg;
12807
12808 } else if (context.method === "throw") {
12809 if (state === GenStateSuspendedStart) {
12810 state = GenStateCompleted;
12811 throw context.arg;
12812 }
12813
12814 context.dispatchException(context.arg);
12815
12816 } else if (context.method === "return") {
12817 context.abrupt("return", context.arg);
12818 }
12819
12820 state = GenStateExecuting;
12821
12822 var record = tryCatch(innerFn, self, context);
12823 if (record.type === "normal") {
12824 // If an exception is thrown from innerFn, we leave state ===
12825 // GenStateExecuting and loop back for another invocation.
12826 state = context.done
12827 ? GenStateCompleted
12828 : GenStateSuspendedYield;
12829
12830 if (record.arg === ContinueSentinel) {
12831 continue;
12832 }
12833
12834 return {
12835 value: record.arg,
12836 done: context.done
12837 };
12838
12839 } else if (record.type === "throw") {
12840 state = GenStateCompleted;
12841 // Dispatch the exception by looping back around to the
12842 // context.dispatchException(context.arg) call above.
12843 context.method = "throw";
12844 context.arg = record.arg;
12845 }
12846 }
12847 };
12848 }
12849
12850 // Call delegate.iterator[context.method](context.arg) and handle the
12851 // result, either by returning a { value, done } result from the
12852 // delegate iterator, or by modifying context.method and context.arg,
12853 // setting context.delegate to null, and returning the ContinueSentinel.
12854 function maybeInvokeDelegate(delegate, context) {
12855 var method = delegate.iterator[context.method];
12856 if (method === undefined$1) {
12857 // A .throw or .return when the delegate iterator has no .throw
12858 // method always terminates the yield* loop.
12859 context.delegate = null;
12860
12861 if (context.method === "throw") {
12862 if (delegate.iterator.return) {
12863 // If the delegate iterator has a return method, give it a
12864 // chance to clean up.
12865 context.method = "return";
12866 context.arg = undefined$1;
12867 maybeInvokeDelegate(delegate, context);
12868
12869 if (context.method === "throw") {
12870 // If maybeInvokeDelegate(context) changed context.method from
12871 // "return" to "throw", let that override the TypeError below.
12872 return ContinueSentinel;
12873 }
12874 }
12875
12876 context.method = "throw";
12877 context.arg = new TypeError(
12878 "The iterator does not provide a 'throw' method");
12879 }
12880
12881 return ContinueSentinel;
12882 }
12883
12884 var record = tryCatch(method, delegate.iterator, context.arg);
12885
12886 if (record.type === "throw") {
12887 context.method = "throw";
12888 context.arg = record.arg;
12889 context.delegate = null;
12890 return ContinueSentinel;
12891 }
12892
12893 var info = record.arg;
12894
12895 if (! info) {
12896 context.method = "throw";
12897 context.arg = new TypeError("iterator result is not an object");
12898 context.delegate = null;
12899 return ContinueSentinel;
12900 }
12901
12902 if (info.done) {
12903 // Assign the result of the finished delegate to the temporary
12904 // variable specified by delegate.resultName (see delegateYield).
12905 context[delegate.resultName] = info.value;
12906
12907 // Resume execution at the desired location (see delegateYield).
12908 context.next = delegate.nextLoc;
12909
12910 // If context.method was "throw" but the delegate handled the
12911 // exception, let the outer generator proceed normally. If
12912 // context.method was "next", forget context.arg since it has been
12913 // "consumed" by the delegate iterator. If context.method was
12914 // "return", allow the original .return call to continue in the
12915 // outer generator.
12916 if (context.method !== "return") {
12917 context.method = "next";
12918 context.arg = undefined$1;
12919 }
12920
12921 } else {
12922 // Re-yield the result returned by the delegate method.
12923 return info;
12924 }
12925
12926 // The delegate iterator is finished, so forget it and continue with
12927 // the outer generator.
12928 context.delegate = null;
12929 return ContinueSentinel;
12930 }
12931
12932 // Define Generator.prototype.{next,throw,return} in terms of the
12933 // unified ._invoke helper method.
12934 defineIteratorMethods(Gp);
12935
12936 Gp[toStringTagSymbol] = "Generator";
12937
12938 // A Generator should always return itself as the iterator object when the
12939 // @@iterator function is called on it. Some browsers' implementations of the
12940 // iterator prototype chain incorrectly implement this, causing the Generator
12941 // object to not be returned from this call. This ensures that doesn't happen.
12942 // See https://github.com/facebook/regenerator/issues/274 for more details.
12943 Gp[iteratorSymbol] = function() {
12944 return this;
12945 };
12946
12947 Gp.toString = function() {
12948 return "[object Generator]";
12949 };
12950
12951 function pushTryEntry(locs) {
12952 var entry = { tryLoc: locs[0] };
12953
12954 if (1 in locs) {
12955 entry.catchLoc = locs[1];
12956 }
12957
12958 if (2 in locs) {
12959 entry.finallyLoc = locs[2];
12960 entry.afterLoc = locs[3];
12961 }
12962
12963 this.tryEntries.push(entry);
12964 }
12965
12966 function resetTryEntry(entry) {
12967 var record = entry.completion || {};
12968 record.type = "normal";
12969 delete record.arg;
12970 entry.completion = record;
12971 }
12972
12973 function Context(tryLocsList) {
12974 // The root entry object (effectively a try statement without a catch
12975 // or a finally block) gives us a place to store values thrown from
12976 // locations where there is no enclosing try statement.
12977 this.tryEntries = [{ tryLoc: "root" }];
12978 tryLocsList.forEach(pushTryEntry, this);
12979 this.reset(true);
12980 }
12981
12982 runtime.keys = function(object) {
12983 var keys = [];
12984 for (var key in object) {
12985 keys.push(key);
12986 }
12987 keys.reverse();
12988
12989 // Rather than returning an object with a next method, we keep
12990 // things simple and return the next function itself.
12991 return function next() {
12992 while (keys.length) {
12993 var key = keys.pop();
12994 if (key in object) {
12995 next.value = key;
12996 next.done = false;
12997 return next;
12998 }
12999 }
13000
13001 // To avoid creating an additional object, we just hang the .value
13002 // and .done properties off the next function object itself. This
13003 // also ensures that the minifier will not anonymize the function.
13004 next.done = true;
13005 return next;
13006 };
13007 };
13008
13009 function values(iterable) {
13010 if (iterable) {
13011 var iteratorMethod = iterable[iteratorSymbol];
13012 if (iteratorMethod) {
13013 return iteratorMethod.call(iterable);
13014 }
13015
13016 if (typeof iterable.next === "function") {
13017 return iterable;
13018 }
13019
13020 if (!isNaN(iterable.length)) {
13021 var i = -1, next = function next() {
13022 while (++i < iterable.length) {
13023 if (hasOwn.call(iterable, i)) {
13024 next.value = iterable[i];
13025 next.done = false;
13026 return next;
13027 }
13028 }
13029
13030 next.value = undefined$1;
13031 next.done = true;
13032
13033 return next;
13034 };
13035
13036 return next.next = next;
13037 }
13038 }
13039
13040 // Return an iterator with no values.
13041 return { next: doneResult };
13042 }
13043 runtime.values = values;
13044
13045 function doneResult() {
13046 return { value: undefined$1, done: true };
13047 }
13048
13049 Context.prototype = {
13050 constructor: Context,
13051
13052 reset: function(skipTempReset) {
13053 this.prev = 0;
13054 this.next = 0;
13055 // Resetting context._sent for legacy support of Babel's
13056 // function.sent implementation.
13057 this.sent = this._sent = undefined$1;
13058 this.done = false;
13059 this.delegate = null;
13060
13061 this.method = "next";
13062 this.arg = undefined$1;
13063
13064 this.tryEntries.forEach(resetTryEntry);
13065
13066 if (!skipTempReset) {
13067 for (var name in this) {
13068 // Not sure about the optimal order of these conditions:
13069 if (name.charAt(0) === "t" &&
13070 hasOwn.call(this, name) &&
13071 !isNaN(+name.slice(1))) {
13072 this[name] = undefined$1;
13073 }
13074 }
13075 }
13076 },
13077
13078 stop: function() {
13079 this.done = true;
13080
13081 var rootEntry = this.tryEntries[0];
13082 var rootRecord = rootEntry.completion;
13083 if (rootRecord.type === "throw") {
13084 throw rootRecord.arg;
13085 }
13086
13087 return this.rval;
13088 },
13089
13090 dispatchException: function(exception) {
13091 if (this.done) {
13092 throw exception;
13093 }
13094
13095 var context = this;
13096 function handle(loc, caught) {
13097 record.type = "throw";
13098 record.arg = exception;
13099 context.next = loc;
13100
13101 if (caught) {
13102 // If the dispatched exception was caught by a catch block,
13103 // then let that catch block handle the exception normally.
13104 context.method = "next";
13105 context.arg = undefined$1;
13106 }
13107
13108 return !! caught;
13109 }
13110
13111 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
13112 var entry = this.tryEntries[i];
13113 var record = entry.completion;
13114
13115 if (entry.tryLoc === "root") {
13116 // Exception thrown outside of any try block that could handle
13117 // it, so set the completion value of the entire function to
13118 // throw the exception.
13119 return handle("end");
13120 }
13121
13122 if (entry.tryLoc <= this.prev) {
13123 var hasCatch = hasOwn.call(entry, "catchLoc");
13124 var hasFinally = hasOwn.call(entry, "finallyLoc");
13125
13126 if (hasCatch && hasFinally) {
13127 if (this.prev < entry.catchLoc) {
13128 return handle(entry.catchLoc, true);
13129 } else if (this.prev < entry.finallyLoc) {
13130 return handle(entry.finallyLoc);
13131 }
13132
13133 } else if (hasCatch) {
13134 if (this.prev < entry.catchLoc) {
13135 return handle(entry.catchLoc, true);
13136 }
13137
13138 } else if (hasFinally) {
13139 if (this.prev < entry.finallyLoc) {
13140 return handle(entry.finallyLoc);
13141 }
13142
13143 } else {
13144 throw new Error("try statement without catch or finally");
13145 }
13146 }
13147 }
13148 },
13149
13150 abrupt: function(type, arg) {
13151 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
13152 var entry = this.tryEntries[i];
13153 if (entry.tryLoc <= this.prev &&
13154 hasOwn.call(entry, "finallyLoc") &&
13155 this.prev < entry.finallyLoc) {
13156 var finallyEntry = entry;
13157 break;
13158 }
13159 }
13160
13161 if (finallyEntry &&
13162 (type === "break" ||
13163 type === "continue") &&
13164 finallyEntry.tryLoc <= arg &&
13165 arg <= finallyEntry.finallyLoc) {
13166 // Ignore the finally entry if control is not jumping to a
13167 // location outside the try/catch block.
13168 finallyEntry = null;
13169 }
13170
13171 var record = finallyEntry ? finallyEntry.completion : {};
13172 record.type = type;
13173 record.arg = arg;
13174
13175 if (finallyEntry) {
13176 this.method = "next";
13177 this.next = finallyEntry.finallyLoc;
13178 return ContinueSentinel;
13179 }
13180
13181 return this.complete(record);
13182 },
13183
13184 complete: function(record, afterLoc) {
13185 if (record.type === "throw") {
13186 throw record.arg;
13187 }
13188
13189 if (record.type === "break" ||
13190 record.type === "continue") {
13191 this.next = record.arg;
13192 } else if (record.type === "return") {
13193 this.rval = this.arg = record.arg;
13194 this.method = "return";
13195 this.next = "end";
13196 } else if (record.type === "normal" && afterLoc) {
13197 this.next = afterLoc;
13198 }
13199
13200 return ContinueSentinel;
13201 },
13202
13203 finish: function(finallyLoc) {
13204 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
13205 var entry = this.tryEntries[i];
13206 if (entry.finallyLoc === finallyLoc) {
13207 this.complete(entry.completion, entry.afterLoc);
13208 resetTryEntry(entry);
13209 return ContinueSentinel;
13210 }
13211 }
13212 },
13213
13214 "catch": function(tryLoc) {
13215 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
13216 var entry = this.tryEntries[i];
13217 if (entry.tryLoc === tryLoc) {
13218 var record = entry.completion;
13219 if (record.type === "throw") {
13220 var thrown = record.arg;
13221 resetTryEntry(entry);
13222 }
13223 return thrown;
13224 }
13225 }
13226
13227 // The context.catch method must only be called with a location
13228 // argument that corresponds to a known catch block.
13229 throw new Error("illegal catch attempt");
13230 },
13231
13232 delegateYield: function(iterable, resultName, nextLoc) {
13233 this.delegate = {
13234 iterator: values(iterable),
13235 resultName: resultName,
13236 nextLoc: nextLoc
13237 };
13238
13239 if (this.method === "next") {
13240 // Deliberately forget the last sent value so that we don't
13241 // accidentally pass it on to the delegate.
13242 this.arg = undefined$1;
13243 }
13244
13245 return ContinueSentinel;
13246 }
13247 };
13248 })(
13249 // In sloppy mode, unbound `this` refers to the global object, fallback to
13250 // Function constructor if we're in global strict mode. That is sadly a form
13251 // of indirect eval which violates Content Security Policy.
13252 (function() {
13253 return this || (typeof self === "object" && self);
13254 })() || Function("return this")()
13255 );
13256 });
13257
13258 /**
13259 * Copyright (c) 2014-present, Facebook, Inc.
13260 *
13261 * This source code is licensed under the MIT license found in the
13262 * LICENSE file in the root directory of this source tree.
13263 */
13264
13265 // This method of obtaining a reference to the global object needs to be
13266 // kept identical to the way it is obtained in runtime.js
13267 var g = (function() {
13268 return this || (typeof self === "object" && self);
13269 })() || Function("return this")();
13270
13271 // Use `getOwnPropertyNames` because not all browsers support calling
13272 // `hasOwnProperty` on the global `self` object in a worker. See #183.
13273 var hadRuntime = g.regeneratorRuntime &&
13274 Object.getOwnPropertyNames(g).indexOf("regeneratorRuntime") >= 0;
13275
13276 // Save the old regeneratorRuntime in case it needs to be restored later.
13277 var oldRuntime = hadRuntime && g.regeneratorRuntime;
13278
13279 // Force reevalutation of runtime.js.
13280 g.regeneratorRuntime = undefined;
13281
13282 var runtimeModule = runtime;
13283
13284 if (hadRuntime) {
13285 // Restore the original runtime.
13286 g.regeneratorRuntime = oldRuntime;
13287 } else {
13288 // Remove the global property added by runtime.js.
13289 try {
13290 delete g.regeneratorRuntime;
13291 } catch(e) {
13292 g.regeneratorRuntime = undefined;
13293 }
13294 }
13295
13296 var regenerator = runtimeModule;
13297
13298 /**
13299 * Helpers.
13300 */
13301
13302 var s = 1000;
13303 var m = s * 60;
13304 var h = m * 60;
13305 var d = h * 24;
13306 var w = d * 7;
13307 var y = d * 365.25;
13308
13309 /**
13310 * Parse or format the given `val`.
13311 *
13312 * Options:
13313 *
13314 * - `long` verbose formatting [false]
13315 *
13316 * @param {String|Number} val
13317 * @param {Object} [options]
13318 * @throws {Error} throw an error if val is not a non-empty string or a number
13319 * @return {String|Number}
13320 * @api public
13321 */
13322
13323 var ms = function(val, options) {
13324 options = options || {};
13325 var type = typeof val;
13326 if (type === 'string' && val.length > 0) {
13327 return parse(val);
13328 } else if (type === 'number' && isNaN(val) === false) {
13329 return options.long ? fmtLong(val) : fmtShort(val);
13330 }
13331 throw new Error(
13332 'val is not a non-empty string or a valid number. val=' +
13333 JSON.stringify(val)
13334 );
13335 };
13336
13337 /**
13338 * Parse the given `str` and return milliseconds.
13339 *
13340 * @param {String} str
13341 * @return {Number}
13342 * @api private
13343 */
13344
13345 function parse(str) {
13346 str = String(str);
13347 if (str.length > 100) {
13348 return;
13349 }
13350 var match = /^((?:\d+)?\-?\d?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
13351 str
13352 );
13353 if (!match) {
13354 return;
13355 }
13356 var n = parseFloat(match[1]);
13357 var type = (match[2] || 'ms').toLowerCase();
13358 switch (type) {
13359 case 'years':
13360 case 'year':
13361 case 'yrs':
13362 case 'yr':
13363 case 'y':
13364 return n * y;
13365 case 'weeks':
13366 case 'week':
13367 case 'w':
13368 return n * w;
13369 case 'days':
13370 case 'day':
13371 case 'd':
13372 return n * d;
13373 case 'hours':
13374 case 'hour':
13375 case 'hrs':
13376 case 'hr':
13377 case 'h':
13378 return n * h;
13379 case 'minutes':
13380 case 'minute':
13381 case 'mins':
13382 case 'min':
13383 case 'm':
13384 return n * m;
13385 case 'seconds':
13386 case 'second':
13387 case 'secs':
13388 case 'sec':
13389 case 's':
13390 return n * s;
13391 case 'milliseconds':
13392 case 'millisecond':
13393 case 'msecs':
13394 case 'msec':
13395 case 'ms':
13396 return n;
13397 default:
13398 return undefined;
13399 }
13400 }
13401
13402 /**
13403 * Short format for `ms`.
13404 *
13405 * @param {Number} ms
13406 * @return {String}
13407 * @api private
13408 */
13409
13410 function fmtShort(ms) {
13411 var msAbs = Math.abs(ms);
13412 if (msAbs >= d) {
13413 return Math.round(ms / d) + 'd';
13414 }
13415 if (msAbs >= h) {
13416 return Math.round(ms / h) + 'h';
13417 }
13418 if (msAbs >= m) {
13419 return Math.round(ms / m) + 'm';
13420 }
13421 if (msAbs >= s) {
13422 return Math.round(ms / s) + 's';
13423 }
13424 return ms + 'ms';
13425 }
13426
13427 /**
13428 * Long format for `ms`.
13429 *
13430 * @param {Number} ms
13431 * @return {String}
13432 * @api private
13433 */
13434
13435 function fmtLong(ms) {
13436 var msAbs = Math.abs(ms);
13437 if (msAbs >= d) {
13438 return plural(ms, msAbs, d, 'day');
13439 }
13440 if (msAbs >= h) {
13441 return plural(ms, msAbs, h, 'hour');
13442 }
13443 if (msAbs >= m) {
13444 return plural(ms, msAbs, m, 'minute');
13445 }
13446 if (msAbs >= s) {
13447 return plural(ms, msAbs, s, 'second');
13448 }
13449 return ms + ' ms';
13450 }
13451
13452 /**
13453 * Pluralization helper.
13454 */
13455
13456 function plural(ms, msAbs, n, name) {
13457 var isPlural = msAbs >= n * 1.5;
13458 return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
13459 }
13460
13461 /**
13462 * This is the common logic for both the Node.js and web browser
13463 * implementations of `debug()`.
13464 */
13465 function setup(env) {
13466 createDebug.debug = createDebug;
13467 createDebug.default = createDebug;
13468 createDebug.coerce = coerce;
13469 createDebug.disable = disable;
13470 createDebug.enable = enable;
13471 createDebug.enabled = enabled;
13472 createDebug.humanize = ms;
13473 Object.keys(env).forEach(function (key) {
13474 createDebug[key] = env[key];
13475 });
13476 /**
13477 * Active `debug` instances.
13478 */
13479
13480 createDebug.instances = [];
13481 /**
13482 * The currently active debug mode names, and names to skip.
13483 */
13484
13485 createDebug.names = [];
13486 createDebug.skips = [];
13487 /**
13488 * Map of special "%n" handling functions, for the debug "format" argument.
13489 *
13490 * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
13491 */
13492
13493 createDebug.formatters = {};
13494 /**
13495 * Selects a color for a debug namespace
13496 * @param {String} namespace The namespace string for the for the debug instance to be colored
13497 * @return {Number|String} An ANSI color code for the given namespace
13498 * @api private
13499 */
13500
13501 function selectColor(namespace) {
13502 var hash = 0;
13503
13504 for (var i = 0; i < namespace.length; i++) {
13505 hash = (hash << 5) - hash + namespace.charCodeAt(i);
13506 hash |= 0; // Convert to 32bit integer
13507 }
13508
13509 return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
13510 }
13511
13512 createDebug.selectColor = selectColor;
13513 /**
13514 * Create a debugger with the given `namespace`.
13515 *
13516 * @param {String} namespace
13517 * @return {Function}
13518 * @api public
13519 */
13520
13521 function createDebug(namespace) {
13522 var prevTime;
13523
13524 function debug() {
13525 // Disabled?
13526 if (!debug.enabled) {
13527 return;
13528 }
13529
13530 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
13531 args[_key] = arguments[_key];
13532 }
13533
13534 var self = debug; // Set `diff` timestamp
13535
13536 var curr = Number(new Date());
13537 var ms = curr - (prevTime || curr);
13538 self.diff = ms;
13539 self.prev = prevTime;
13540 self.curr = curr;
13541 prevTime = curr;
13542 args[0] = createDebug.coerce(args[0]);
13543
13544 if (typeof args[0] !== 'string') {
13545 // Anything else let's inspect with %O
13546 args.unshift('%O');
13547 } // Apply any `formatters` transformations
13548
13549
13550 var index = 0;
13551 args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) {
13552 // If we encounter an escaped % then don't increase the array index
13553 if (match === '%%') {
13554 return match;
13555 }
13556
13557 index++;
13558 var formatter = createDebug.formatters[format];
13559
13560 if (typeof formatter === 'function') {
13561 var val = args[index];
13562 match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format`
13563
13564 args.splice(index, 1);
13565 index--;
13566 }
13567
13568 return match;
13569 }); // Apply env-specific formatting (colors, etc.)
13570
13571 createDebug.formatArgs.call(self, args);
13572 var logFn = self.log || createDebug.log;
13573 logFn.apply(self, args);
13574 }
13575
13576 debug.namespace = namespace;
13577 debug.enabled = createDebug.enabled(namespace);
13578 debug.useColors = createDebug.useColors();
13579 debug.color = selectColor(namespace);
13580 debug.destroy = destroy;
13581 debug.extend = extend; // Debug.formatArgs = formatArgs;
13582 // debug.rawLog = rawLog;
13583 // env-specific initialization logic for debug instances
13584
13585 if (typeof createDebug.init === 'function') {
13586 createDebug.init(debug);
13587 }
13588
13589 createDebug.instances.push(debug);
13590 return debug;
13591 }
13592
13593 function destroy() {
13594 var index = createDebug.instances.indexOf(this);
13595
13596 if (index !== -1) {
13597 createDebug.instances.splice(index, 1);
13598 return true;
13599 }
13600
13601 return false;
13602 }
13603
13604 function extend(namespace, delimiter) {
13605 return createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
13606 }
13607 /**
13608 * Enables a debug mode by namespaces. This can include modes
13609 * separated by a colon and wildcards.
13610 *
13611 * @param {String} namespaces
13612 * @api public
13613 */
13614
13615
13616 function enable(namespaces) {
13617 createDebug.save(namespaces);
13618 createDebug.names = [];
13619 createDebug.skips = [];
13620 var i;
13621 var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
13622 var len = split.length;
13623
13624 for (i = 0; i < len; i++) {
13625 if (!split[i]) {
13626 // ignore empty strings
13627 continue;
13628 }
13629
13630 namespaces = split[i].replace(/\*/g, '.*?');
13631
13632 if (namespaces[0] === '-') {
13633 createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
13634 } else {
13635 createDebug.names.push(new RegExp('^' + namespaces + '$'));
13636 }
13637 }
13638
13639 for (i = 0; i < createDebug.instances.length; i++) {
13640 var instance = createDebug.instances[i];
13641 instance.enabled = createDebug.enabled(instance.namespace);
13642 }
13643 }
13644 /**
13645 * Disable debug output.
13646 *
13647 * @api public
13648 */
13649
13650
13651 function disable() {
13652 createDebug.enable('');
13653 }
13654 /**
13655 * Returns true if the given mode name is enabled, false otherwise.
13656 *
13657 * @param {String} name
13658 * @return {Boolean}
13659 * @api public
13660 */
13661
13662
13663 function enabled(name) {
13664 if (name[name.length - 1] === '*') {
13665 return true;
13666 }
13667
13668 var i;
13669 var len;
13670
13671 for (i = 0, len = createDebug.skips.length; i < len; i++) {
13672 if (createDebug.skips[i].test(name)) {
13673 return false;
13674 }
13675 }
13676
13677 for (i = 0, len = createDebug.names.length; i < len; i++) {
13678 if (createDebug.names[i].test(name)) {
13679 return true;
13680 }
13681 }
13682
13683 return false;
13684 }
13685 /**
13686 * Coerce `val`.
13687 *
13688 * @param {Mixed} val
13689 * @return {Mixed}
13690 * @api private
13691 */
13692
13693
13694 function coerce(val) {
13695 if (val instanceof Error) {
13696 return val.stack || val.message;
13697 }
13698
13699 return val;
13700 }
13701
13702 createDebug.enable(createDebug.load());
13703 return createDebug;
13704 }
13705
13706 var common = setup;
13707
13708 var browser = createCommonjsModule(function (module, exports) {
13709
13710 function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
13711
13712 /* eslint-env browser */
13713
13714 /**
13715 * This is the web browser implementation of `debug()`.
13716 */
13717 exports.log = log;
13718 exports.formatArgs = formatArgs;
13719 exports.save = save;
13720 exports.load = load;
13721 exports.useColors = useColors;
13722 exports.storage = localstorage();
13723 /**
13724 * Colors.
13725 */
13726
13727 exports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'];
13728 /**
13729 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
13730 * and the Firebug extension (any Firefox version) are known
13731 * to support "%c" CSS customizations.
13732 *
13733 * TODO: add a `localStorage` variable to explicitly enable/disable colors
13734 */
13735 // eslint-disable-next-line complexity
13736
13737 function useColors() {
13738 // NB: In an Electron preload script, document will be defined but not fully
13739 // initialized. Since we know we're in Chrome, we'll just detect this case
13740 // explicitly
13741 if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
13742 return true;
13743 } // Internet Explorer and Edge do not support colors.
13744
13745
13746 if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
13747 return false;
13748 } // Is webkit? http://stackoverflow.com/a/16459606/376773
13749 // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
13750
13751
13752 return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773
13753 typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31?
13754 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
13755 typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker
13756 typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/);
13757 }
13758 /**
13759 * Colorize log arguments if enabled.
13760 *
13761 * @api public
13762 */
13763
13764
13765 function formatArgs(args) {
13766 args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff);
13767
13768 if (!this.useColors) {
13769 return;
13770 }
13771
13772 var c = 'color: ' + this.color;
13773 args.splice(1, 0, c, 'color: inherit'); // The final "%c" is somewhat tricky, because there could be other
13774 // arguments passed either before or after the %c, so we need to
13775 // figure out the correct index to insert the CSS into
13776
13777 var index = 0;
13778 var lastC = 0;
13779 args[0].replace(/%[a-zA-Z%]/g, function (match) {
13780 if (match === '%%') {
13781 return;
13782 }
13783
13784 index++;
13785
13786 if (match === '%c') {
13787 // We only are interested in the *last* %c
13788 // (the user may have provided their own)
13789 lastC = index;
13790 }
13791 });
13792 args.splice(lastC, 0, c);
13793 }
13794 /**
13795 * Invokes `console.log()` when available.
13796 * No-op when `console.log` is not a "function".
13797 *
13798 * @api public
13799 */
13800
13801
13802 function log() {
13803 var _console;
13804
13805 // This hackery is required for IE8/9, where
13806 // the `console.log` function doesn't have 'apply'
13807 return (typeof console === "undefined" ? "undefined" : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments);
13808 }
13809 /**
13810 * Save `namespaces`.
13811 *
13812 * @param {String} namespaces
13813 * @api private
13814 */
13815
13816
13817 function save(namespaces) {
13818 try {
13819 if (namespaces) {
13820 exports.storage.setItem('debug', namespaces);
13821 } else {
13822 exports.storage.removeItem('debug');
13823 }
13824 } catch (error) {// Swallow
13825 // XXX (@Qix-) should we be logging these?
13826 }
13827 }
13828 /**
13829 * Load `namespaces`.
13830 *
13831 * @return {String} returns the previously persisted debug modes
13832 * @api private
13833 */
13834
13835
13836 function load() {
13837 var r;
13838
13839 try {
13840 r = exports.storage.getItem('debug');
13841 } catch (error) {} // Swallow
13842 // XXX (@Qix-) should we be logging these?
13843 // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
13844
13845
13846 if (!r && typeof process !== 'undefined' && 'env' in process) {
13847 r = process.env.DEBUG;
13848 }
13849
13850 return r;
13851 }
13852 /**
13853 * Localstorage attempts to return the localstorage.
13854 *
13855 * This is necessary because safari throws
13856 * when a user disables cookies/localstorage
13857 * and you attempt to access it.
13858 *
13859 * @return {LocalStorage}
13860 * @api private
13861 */
13862
13863
13864 function localstorage() {
13865 try {
13866 // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
13867 // The Browser also has localStorage in the global context.
13868 return localStorage;
13869 } catch (error) {// Swallow
13870 // XXX (@Qix-) should we be logging these?
13871 }
13872 }
13873
13874 module.exports = common(exports);
13875 var formatters = module.exports.formatters;
13876 /**
13877 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
13878 */
13879
13880 formatters.j = function (v) {
13881 try {
13882 return JSON.stringify(v);
13883 } catch (error) {
13884 return '[UnexpectedJSONParseError]: ' + error.message;
13885 }
13886 };
13887 });
13888 var browser_1 = browser.log;
13889 var browser_2 = browser.formatArgs;
13890 var browser_3 = browser.save;
13891 var browser_4 = browser.load;
13892 var browser_5 = browser.useColors;
13893 var browser_6 = browser.storage;
13894 var browser_7 = browser.colors;
13895
13896 var componentEmitter = createCommonjsModule(function (module) {
13897 /**
13898 * Expose `Emitter`.
13899 */
13900 {
13901 module.exports = Emitter;
13902 }
13903 /**
13904 * Initialize a new `Emitter`.
13905 *
13906 * @api public
13907 */
13908
13909
13910 function Emitter(obj) {
13911 if (obj) return mixin(obj);
13912 }
13913 /**
13914 * Mixin the emitter properties.
13915 *
13916 * @param {Object} obj
13917 * @return {Object}
13918 * @api private
13919 */
13920
13921 function mixin(obj) {
13922 for (var key in Emitter.prototype) {
13923 obj[key] = Emitter.prototype[key];
13924 }
13925
13926 return obj;
13927 }
13928 /**
13929 * Listen on the given `event` with `fn`.
13930 *
13931 * @param {String} event
13932 * @param {Function} fn
13933 * @return {Emitter}
13934 * @api public
13935 */
13936
13937
13938 Emitter.prototype.on = Emitter.prototype.addEventListener = function (event, fn) {
13939 this._callbacks = this._callbacks || {};
13940 (this._callbacks['$' + event] = this._callbacks['$' + event] || []).push(fn);
13941 return this;
13942 };
13943 /**
13944 * Adds an `event` listener that will be invoked a single
13945 * time then automatically removed.
13946 *
13947 * @param {String} event
13948 * @param {Function} fn
13949 * @return {Emitter}
13950 * @api public
13951 */
13952
13953
13954 Emitter.prototype.once = function (event, fn) {
13955 function on() {
13956 this.off(event, on);
13957 fn.apply(this, arguments);
13958 }
13959
13960 on.fn = fn;
13961 this.on(event, on);
13962 return this;
13963 };
13964 /**
13965 * Remove the given callback for `event` or all
13966 * registered callbacks.
13967 *
13968 * @param {String} event
13969 * @param {Function} fn
13970 * @return {Emitter}
13971 * @api public
13972 */
13973
13974
13975 Emitter.prototype.off = Emitter.prototype.removeListener = Emitter.prototype.removeAllListeners = Emitter.prototype.removeEventListener = function (event, fn) {
13976 this._callbacks = this._callbacks || {}; // all
13977
13978 if (0 == arguments.length) {
13979 this._callbacks = {};
13980 return this;
13981 } // specific event
13982
13983
13984 var callbacks = this._callbacks['$' + event];
13985 if (!callbacks) return this; // remove all handlers
13986
13987 if (1 == arguments.length) {
13988 delete this._callbacks['$' + event];
13989 return this;
13990 } // remove specific handler
13991
13992
13993 var cb;
13994
13995 for (var i = 0; i < callbacks.length; i++) {
13996 cb = callbacks[i];
13997
13998 if (cb === fn || cb.fn === fn) {
13999 callbacks.splice(i, 1);
14000 break;
14001 }
14002 } // Remove event specific arrays for event types that no
14003 // one is subscribed for to avoid memory leak.
14004
14005
14006 if (callbacks.length === 0) {
14007 delete this._callbacks['$' + event];
14008 }
14009
14010 return this;
14011 };
14012 /**
14013 * Emit `event` with the given args.
14014 *
14015 * @param {String} event
14016 * @param {Mixed} ...
14017 * @return {Emitter}
14018 */
14019
14020
14021 Emitter.prototype.emit = function (event) {
14022 this._callbacks = this._callbacks || {};
14023 var args = new Array(arguments.length - 1),
14024 callbacks = this._callbacks['$' + event];
14025
14026 for (var i = 1; i < arguments.length; i++) {
14027 args[i - 1] = arguments[i];
14028 }
14029
14030 if (callbacks) {
14031 callbacks = callbacks.slice(0);
14032
14033 for (var i = 0, len = callbacks.length; i < len; ++i) {
14034 callbacks[i].apply(this, args);
14035 }
14036 }
14037
14038 return this;
14039 };
14040 /**
14041 * Return array of callbacks for `event`.
14042 *
14043 * @param {String} event
14044 * @return {Array}
14045 * @api public
14046 */
14047
14048
14049 Emitter.prototype.listeners = function (event) {
14050 this._callbacks = this._callbacks || {};
14051 return this._callbacks['$' + event] || [];
14052 };
14053 /**
14054 * Check if this emitter has `event` handlers.
14055 *
14056 * @param {String} event
14057 * @return {Boolean}
14058 * @api public
14059 */
14060
14061
14062 Emitter.prototype.hasListeners = function (event) {
14063 return !!this.listeners(event).length;
14064 };
14065 });
14066
14067 var fastSafeStringify = stringify;
14068 stringify.default = stringify;
14069 stringify.stable = deterministicStringify;
14070 stringify.stableStringify = deterministicStringify;
14071
14072 var arr = [];
14073 var replacerStack = [];
14074
14075 // Regular stringify
14076 function stringify (obj, replacer, spacer) {
14077 decirc(obj, '', [], undefined);
14078 var res;
14079 if (replacerStack.length === 0) {
14080 res = JSON.stringify(obj, replacer, spacer);
14081 } else {
14082 res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
14083 }
14084 while (arr.length !== 0) {
14085 var part = arr.pop();
14086 if (part.length === 4) {
14087 Object.defineProperty(part[0], part[1], part[3]);
14088 } else {
14089 part[0][part[1]] = part[2];
14090 }
14091 }
14092 return res
14093 }
14094 function decirc (val, k, stack, parent) {
14095 var i;
14096 if (typeof val === 'object' && val !== null) {
14097 for (i = 0; i < stack.length; i++) {
14098 if (stack[i] === val) {
14099 var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
14100 if (propertyDescriptor.get !== undefined) {
14101 if (propertyDescriptor.configurable) {
14102 Object.defineProperty(parent, k, { value: '[Circular]' });
14103 arr.push([parent, k, val, propertyDescriptor]);
14104 } else {
14105 replacerStack.push([val, k]);
14106 }
14107 } else {
14108 parent[k] = '[Circular]';
14109 arr.push([parent, k, val]);
14110 }
14111 return
14112 }
14113 }
14114 stack.push(val);
14115 // Optimize for Arrays. Big arrays could kill the performance otherwise!
14116 if (Array.isArray(val)) {
14117 for (i = 0; i < val.length; i++) {
14118 decirc(val[i], i, stack, val);
14119 }
14120 } else {
14121 var keys = Object.keys(val);
14122 for (i = 0; i < keys.length; i++) {
14123 var key = keys[i];
14124 decirc(val[key], key, stack, val);
14125 }
14126 }
14127 stack.pop();
14128 }
14129 }
14130
14131 // Stable-stringify
14132 function compareFunction (a, b) {
14133 if (a < b) {
14134 return -1
14135 }
14136 if (a > b) {
14137 return 1
14138 }
14139 return 0
14140 }
14141
14142 function deterministicStringify (obj, replacer, spacer) {
14143 var tmp = deterministicDecirc(obj, '', [], undefined) || obj;
14144 var res;
14145 if (replacerStack.length === 0) {
14146 res = JSON.stringify(tmp, replacer, spacer);
14147 } else {
14148 res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
14149 }
14150 while (arr.length !== 0) {
14151 var part = arr.pop();
14152 if (part.length === 4) {
14153 Object.defineProperty(part[0], part[1], part[3]);
14154 } else {
14155 part[0][part[1]] = part[2];
14156 }
14157 }
14158 return res
14159 }
14160
14161 function deterministicDecirc (val, k, stack, parent) {
14162 var i;
14163 if (typeof val === 'object' && val !== null) {
14164 for (i = 0; i < stack.length; i++) {
14165 if (stack[i] === val) {
14166 var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
14167 if (propertyDescriptor.get !== undefined) {
14168 if (propertyDescriptor.configurable) {
14169 Object.defineProperty(parent, k, { value: '[Circular]' });
14170 arr.push([parent, k, val, propertyDescriptor]);
14171 } else {
14172 replacerStack.push([val, k]);
14173 }
14174 } else {
14175 parent[k] = '[Circular]';
14176 arr.push([parent, k, val]);
14177 }
14178 return
14179 }
14180 }
14181 if (typeof val.toJSON === 'function') {
14182 return
14183 }
14184 stack.push(val);
14185 // Optimize for Arrays. Big arrays could kill the performance otherwise!
14186 if (Array.isArray(val)) {
14187 for (i = 0; i < val.length; i++) {
14188 deterministicDecirc(val[i], i, stack, val);
14189 }
14190 } else {
14191 // Create a temporary object in the required way
14192 var tmp = {};
14193 var keys = Object.keys(val).sort(compareFunction);
14194 for (i = 0; i < keys.length; i++) {
14195 var key = keys[i];
14196 deterministicDecirc(val[key], key, stack, val);
14197 tmp[key] = val[key];
14198 }
14199 if (parent !== undefined) {
14200 arr.push([parent, k, val]);
14201 parent[k] = tmp;
14202 } else {
14203 return tmp
14204 }
14205 }
14206 stack.pop();
14207 }
14208 }
14209
14210 // wraps replacer function to handle values we couldn't replace
14211 // and mark them as [Circular]
14212 function replaceGetterValues (replacer) {
14213 replacer = replacer !== undefined ? replacer : function (k, v) { return v };
14214 return function (key, val) {
14215 if (replacerStack.length > 0) {
14216 for (var i = 0; i < replacerStack.length; i++) {
14217 var part = replacerStack[i];
14218 if (part[1] === key && part[0] === val) {
14219 val = '[Circular]';
14220 replacerStack.splice(i, 1);
14221 break
14222 }
14223 }
14224 }
14225 return replacer.call(this, key, val)
14226 }
14227 }
14228
14229 function _typeof$1(obj) {
14230 if (typeof Symbol === "function" && _typeof(Symbol.iterator) === "symbol") {
14231 _typeof$1 = function _typeof$1(obj) {
14232 return _typeof(obj);
14233 };
14234 } else {
14235 _typeof$1 = function _typeof$1(obj) {
14236 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof(obj);
14237 };
14238 }
14239
14240 return _typeof$1(obj);
14241 }
14242 /**
14243 * Check if `obj` is an object.
14244 *
14245 * @param {Object} obj
14246 * @return {Boolean}
14247 * @api private
14248 */
14249
14250
14251 function isObject(obj) {
14252 return obj !== null && _typeof$1(obj) === 'object';
14253 }
14254
14255 var isObject_1 = isObject;
14256
14257 function _typeof$2(obj) {
14258 if (typeof Symbol === "function" && _typeof(Symbol.iterator) === "symbol") {
14259 _typeof$2 = function _typeof$1(obj) {
14260 return _typeof(obj);
14261 };
14262 } else {
14263 _typeof$2 = function _typeof$1(obj) {
14264 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof(obj);
14265 };
14266 }
14267
14268 return _typeof$2(obj);
14269 }
14270 /**
14271 * Module of mixed-in functions shared between node and client code
14272 */
14273
14274 /**
14275 * Expose `RequestBase`.
14276 */
14277
14278
14279 var requestBase = RequestBase;
14280 /**
14281 * Initialize a new `RequestBase`.
14282 *
14283 * @api public
14284 */
14285
14286 function RequestBase(obj) {
14287 if (obj) return mixin(obj);
14288 }
14289 /**
14290 * Mixin the prototype properties.
14291 *
14292 * @param {Object} obj
14293 * @return {Object}
14294 * @api private
14295 */
14296
14297
14298 function mixin(obj) {
14299 for (var key in RequestBase.prototype) {
14300 if (Object.prototype.hasOwnProperty.call(RequestBase.prototype, key)) obj[key] = RequestBase.prototype[key];
14301 }
14302
14303 return obj;
14304 }
14305 /**
14306 * Clear previous timeout.
14307 *
14308 * @return {Request} for chaining
14309 * @api public
14310 */
14311
14312
14313 RequestBase.prototype.clearTimeout = function () {
14314 clearTimeout(this._timer);
14315 clearTimeout(this._responseTimeoutTimer);
14316 clearTimeout(this._uploadTimeoutTimer);
14317 delete this._timer;
14318 delete this._responseTimeoutTimer;
14319 delete this._uploadTimeoutTimer;
14320 return this;
14321 };
14322 /**
14323 * Override default response body parser
14324 *
14325 * This function will be called to convert incoming data into request.body
14326 *
14327 * @param {Function}
14328 * @api public
14329 */
14330
14331
14332 RequestBase.prototype.parse = function (fn) {
14333 this._parser = fn;
14334 return this;
14335 };
14336 /**
14337 * Set format of binary response body.
14338 * In browser valid formats are 'blob' and 'arraybuffer',
14339 * which return Blob and ArrayBuffer, respectively.
14340 *
14341 * In Node all values result in Buffer.
14342 *
14343 * Examples:
14344 *
14345 * req.get('/')
14346 * .responseType('blob')
14347 * .end(callback);
14348 *
14349 * @param {String} val
14350 * @return {Request} for chaining
14351 * @api public
14352 */
14353
14354
14355 RequestBase.prototype.responseType = function (val) {
14356 this._responseType = val;
14357 return this;
14358 };
14359 /**
14360 * Override default request body serializer
14361 *
14362 * This function will be called to convert data set via .send or .attach into payload to send
14363 *
14364 * @param {Function}
14365 * @api public
14366 */
14367
14368
14369 RequestBase.prototype.serialize = function (fn) {
14370 this._serializer = fn;
14371 return this;
14372 };
14373 /**
14374 * Set timeouts.
14375 *
14376 * - response timeout is time between sending request and receiving the first byte of the response. Includes DNS and connection time.
14377 * - deadline is the time from start of the request to receiving response body in full. If the deadline is too short large files may not load at all on slow connections.
14378 * - upload is the time since last bit of data was sent or received. This timeout works only if deadline timeout is off
14379 *
14380 * Value of 0 or false means no timeout.
14381 *
14382 * @param {Number|Object} ms or {response, deadline}
14383 * @return {Request} for chaining
14384 * @api public
14385 */
14386
14387
14388 RequestBase.prototype.timeout = function (options) {
14389 if (!options || _typeof$2(options) !== 'object') {
14390 this._timeout = options;
14391 this._responseTimeout = 0;
14392 this._uploadTimeout = 0;
14393 return this;
14394 }
14395
14396 for (var option in options) {
14397 if (Object.prototype.hasOwnProperty.call(options, option)) {
14398 switch (option) {
14399 case 'deadline':
14400 this._timeout = options.deadline;
14401 break;
14402
14403 case 'response':
14404 this._responseTimeout = options.response;
14405 break;
14406
14407 case 'upload':
14408 this._uploadTimeout = options.upload;
14409 break;
14410
14411 default:
14412 console.warn('Unknown timeout option', option);
14413 }
14414 }
14415 }
14416
14417 return this;
14418 };
14419 /**
14420 * Set number of retry attempts on error.
14421 *
14422 * Failed requests will be retried 'count' times if timeout or err.code >= 500.
14423 *
14424 * @param {Number} count
14425 * @param {Function} [fn]
14426 * @return {Request} for chaining
14427 * @api public
14428 */
14429
14430
14431 RequestBase.prototype.retry = function (count, fn) {
14432 // Default to 1 if no count passed or true
14433 if (arguments.length === 0 || count === true) count = 1;
14434 if (count <= 0) count = 0;
14435 this._maxRetries = count;
14436 this._retries = 0;
14437 this._retryCallback = fn;
14438 return this;
14439 };
14440
14441 var ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT', 'EADDRINFO', 'ESOCKETTIMEDOUT'];
14442 /**
14443 * Determine if a request should be retried.
14444 * (Borrowed from segmentio/superagent-retry)
14445 *
14446 * @param {Error} err an error
14447 * @param {Response} [res] response
14448 * @returns {Boolean} if segment should be retried
14449 */
14450
14451 RequestBase.prototype._shouldRetry = function (err, res) {
14452 if (!this._maxRetries || this._retries++ >= this._maxRetries) {
14453 return false;
14454 }
14455
14456 if (this._retryCallback) {
14457 try {
14458 var override = this._retryCallback(err, res);
14459
14460 if (override === true) return true;
14461 if (override === false) return false; // undefined falls back to defaults
14462 } catch (err2) {
14463 console.error(err2);
14464 }
14465 }
14466
14467 if (res && res.status && res.status >= 500 && res.status !== 501) return true;
14468
14469 if (err) {
14470 if (err.code && ERROR_CODES.indexOf(err.code) !== -1) return true; // Superagent timeout
14471
14472 if (err.timeout && err.code === 'ECONNABORTED') return true;
14473 if (err.crossDomain) return true;
14474 }
14475
14476 return false;
14477 };
14478 /**
14479 * Retry request
14480 *
14481 * @return {Request} for chaining
14482 * @api private
14483 */
14484
14485
14486 RequestBase.prototype._retry = function () {
14487 this.clearTimeout(); // node
14488
14489 if (this.req) {
14490 this.req = null;
14491 this.req = this.request();
14492 }
14493
14494 this._aborted = false;
14495 this.timedout = false;
14496 return this._end();
14497 };
14498 /**
14499 * Promise support
14500 *
14501 * @param {Function} resolve
14502 * @param {Function} [reject]
14503 * @return {Request}
14504 */
14505
14506
14507 RequestBase.prototype.then = function (resolve, reject) {
14508 var _this = this;
14509
14510 if (!this._fullfilledPromise) {
14511 var self = this;
14512
14513 if (this._endCalled) {
14514 console.warn('Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises');
14515 }
14516
14517 this._fullfilledPromise = new Promise(function (resolve, reject) {
14518 self.on('abort', function () {
14519 var err = new Error('Aborted');
14520 err.code = 'ABORTED';
14521 err.status = _this.status;
14522 err.method = _this.method;
14523 err.url = _this.url;
14524 reject(err);
14525 });
14526 self.end(function (err, res) {
14527 if (err) reject(err);else resolve(res);
14528 });
14529 });
14530 }
14531
14532 return this._fullfilledPromise.then(resolve, reject);
14533 };
14534
14535 RequestBase.prototype.catch = function (cb) {
14536 return this.then(undefined, cb);
14537 };
14538 /**
14539 * Allow for extension
14540 */
14541
14542
14543 RequestBase.prototype.use = function (fn) {
14544 fn(this);
14545 return this;
14546 };
14547
14548 RequestBase.prototype.ok = function (cb) {
14549 if (typeof cb !== 'function') throw new Error('Callback required');
14550 this._okCallback = cb;
14551 return this;
14552 };
14553
14554 RequestBase.prototype._isResponseOK = function (res) {
14555 if (!res) {
14556 return false;
14557 }
14558
14559 if (this._okCallback) {
14560 return this._okCallback(res);
14561 }
14562
14563 return res.status >= 200 && res.status < 300;
14564 };
14565 /**
14566 * Get request header `field`.
14567 * Case-insensitive.
14568 *
14569 * @param {String} field
14570 * @return {String}
14571 * @api public
14572 */
14573
14574
14575 RequestBase.prototype.get = function (field) {
14576 return this._header[field.toLowerCase()];
14577 };
14578 /**
14579 * Get case-insensitive header `field` value.
14580 * This is a deprecated internal API. Use `.get(field)` instead.
14581 *
14582 * (getHeader is no longer used internally by the superagent code base)
14583 *
14584 * @param {String} field
14585 * @return {String}
14586 * @api private
14587 * @deprecated
14588 */
14589
14590
14591 RequestBase.prototype.getHeader = RequestBase.prototype.get;
14592 /**
14593 * Set header `field` to `val`, or multiple fields with one object.
14594 * Case-insensitive.
14595 *
14596 * Examples:
14597 *
14598 * req.get('/')
14599 * .set('Accept', 'application/json')
14600 * .set('X-API-Key', 'foobar')
14601 * .end(callback);
14602 *
14603 * req.get('/')
14604 * .set({ Accept: 'application/json', 'X-API-Key': 'foobar' })
14605 * .end(callback);
14606 *
14607 * @param {String|Object} field
14608 * @param {String} val
14609 * @return {Request} for chaining
14610 * @api public
14611 */
14612
14613 RequestBase.prototype.set = function (field, val) {
14614 if (isObject_1(field)) {
14615 for (var key in field) {
14616 if (Object.prototype.hasOwnProperty.call(field, key)) this.set(key, field[key]);
14617 }
14618
14619 return this;
14620 }
14621
14622 this._header[field.toLowerCase()] = val;
14623 this.header[field] = val;
14624 return this;
14625 }; // eslint-disable-next-line valid-jsdoc
14626
14627 /**
14628 * Remove header `field`.
14629 * Case-insensitive.
14630 *
14631 * Example:
14632 *
14633 * req.get('/')
14634 * .unset('User-Agent')
14635 * .end(callback);
14636 *
14637 * @param {String} field field name
14638 */
14639
14640
14641 RequestBase.prototype.unset = function (field) {
14642 delete this._header[field.toLowerCase()];
14643 delete this.header[field];
14644 return this;
14645 };
14646 /**
14647 * Write the field `name` and `val`, or multiple fields with one object
14648 * for "multipart/form-data" request bodies.
14649 *
14650 * ``` js
14651 * request.post('/upload')
14652 * .field('foo', 'bar')
14653 * .end(callback);
14654 *
14655 * request.post('/upload')
14656 * .field({ foo: 'bar', baz: 'qux' })
14657 * .end(callback);
14658 * ```
14659 *
14660 * @param {String|Object} name name of field
14661 * @param {String|Blob|File|Buffer|fs.ReadStream} val value of field
14662 * @return {Request} for chaining
14663 * @api public
14664 */
14665
14666
14667 RequestBase.prototype.field = function (name, val) {
14668 // name should be either a string or an object.
14669 if (name === null || undefined === name) {
14670 throw new Error('.field(name, val) name can not be empty');
14671 }
14672
14673 if (this._data) {
14674 throw new Error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");
14675 }
14676
14677 if (isObject_1(name)) {
14678 for (var key in name) {
14679 if (Object.prototype.hasOwnProperty.call(name, key)) this.field(key, name[key]);
14680 }
14681
14682 return this;
14683 }
14684
14685 if (Array.isArray(val)) {
14686 for (var i in val) {
14687 if (Object.prototype.hasOwnProperty.call(val, i)) this.field(name, val[i]);
14688 }
14689
14690 return this;
14691 } // val should be defined now
14692
14693
14694 if (val === null || undefined === val) {
14695 throw new Error('.field(name, val) val can not be empty');
14696 }
14697
14698 if (typeof val === 'boolean') {
14699 val = String(val);
14700 }
14701
14702 this._getFormData().append(name, val);
14703
14704 return this;
14705 };
14706 /**
14707 * Abort the request, and clear potential timeout.
14708 *
14709 * @return {Request} request
14710 * @api public
14711 */
14712
14713
14714 RequestBase.prototype.abort = function () {
14715 if (this._aborted) {
14716 return this;
14717 }
14718
14719 this._aborted = true;
14720 if (this.xhr) this.xhr.abort(); // browser
14721
14722 if (this.req) this.req.abort(); // node
14723
14724 this.clearTimeout();
14725 this.emit('abort');
14726 return this;
14727 };
14728
14729 RequestBase.prototype._auth = function (user, pass, options, base64Encoder) {
14730 switch (options.type) {
14731 case 'basic':
14732 this.set('Authorization', "Basic ".concat(base64Encoder("".concat(user, ":").concat(pass))));
14733 break;
14734
14735 case 'auto':
14736 this.username = user;
14737 this.password = pass;
14738 break;
14739
14740 case 'bearer':
14741 // usage would be .auth(accessToken, { type: 'bearer' })
14742 this.set('Authorization', "Bearer ".concat(user));
14743 break;
14744
14745 default:
14746 break;
14747 }
14748
14749 return this;
14750 };
14751 /**
14752 * Enable transmission of cookies with x-domain requests.
14753 *
14754 * Note that for this to work the origin must not be
14755 * using "Access-Control-Allow-Origin" with a wildcard,
14756 * and also must set "Access-Control-Allow-Credentials"
14757 * to "true".
14758 *
14759 * @api public
14760 */
14761
14762
14763 RequestBase.prototype.withCredentials = function (on) {
14764 // This is browser-only functionality. Node side is no-op.
14765 if (on === undefined) on = true;
14766 this._withCredentials = on;
14767 return this;
14768 };
14769 /**
14770 * Set the max redirects to `n`. Does nothing in browser XHR implementation.
14771 *
14772 * @param {Number} n
14773 * @return {Request} for chaining
14774 * @api public
14775 */
14776
14777
14778 RequestBase.prototype.redirects = function (n) {
14779 this._maxRedirects = n;
14780 return this;
14781 };
14782 /**
14783 * Maximum size of buffered response body, in bytes. Counts uncompressed size.
14784 * Default 200MB.
14785 *
14786 * @param {Number} n number of bytes
14787 * @return {Request} for chaining
14788 */
14789
14790
14791 RequestBase.prototype.maxResponseSize = function (n) {
14792 if (typeof n !== 'number') {
14793 throw new TypeError('Invalid argument');
14794 }
14795
14796 this._maxResponseSize = n;
14797 return this;
14798 };
14799 /**
14800 * Convert to a plain javascript object (not JSON string) of scalar properties.
14801 * Note as this method is designed to return a useful non-this value,
14802 * it cannot be chained.
14803 *
14804 * @return {Object} describing method, url, and data of this request
14805 * @api public
14806 */
14807
14808
14809 RequestBase.prototype.toJSON = function () {
14810 return {
14811 method: this.method,
14812 url: this.url,
14813 data: this._data,
14814 headers: this._header
14815 };
14816 };
14817 /**
14818 * Send `data` as the request body, defaulting the `.type()` to "json" when
14819 * an object is given.
14820 *
14821 * Examples:
14822 *
14823 * // manual json
14824 * request.post('/user')
14825 * .type('json')
14826 * .send('{"name":"tj"}')
14827 * .end(callback)
14828 *
14829 * // auto json
14830 * request.post('/user')
14831 * .send({ name: 'tj' })
14832 * .end(callback)
14833 *
14834 * // manual x-www-form-urlencoded
14835 * request.post('/user')
14836 * .type('form')
14837 * .send('name=tj')
14838 * .end(callback)
14839 *
14840 * // auto x-www-form-urlencoded
14841 * request.post('/user')
14842 * .type('form')
14843 * .send({ name: 'tj' })
14844 * .end(callback)
14845 *
14846 * // defaults to x-www-form-urlencoded
14847 * request.post('/user')
14848 * .send('name=tobi')
14849 * .send('species=ferret')
14850 * .end(callback)
14851 *
14852 * @param {String|Object} data
14853 * @return {Request} for chaining
14854 * @api public
14855 */
14856 // eslint-disable-next-line complexity
14857
14858
14859 RequestBase.prototype.send = function (data) {
14860 var isObj = isObject_1(data);
14861 var type = this._header['content-type'];
14862
14863 if (this._formData) {
14864 throw new Error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");
14865 }
14866
14867 if (isObj && !this._data) {
14868 if (Array.isArray(data)) {
14869 this._data = [];
14870 } else if (!this._isHost(data)) {
14871 this._data = {};
14872 }
14873 } else if (data && this._data && this._isHost(this._data)) {
14874 throw new Error("Can't merge these send calls");
14875 } // merge
14876
14877
14878 if (isObj && isObject_1(this._data)) {
14879 for (var key in data) {
14880 if (Object.prototype.hasOwnProperty.call(data, key)) this._data[key] = data[key];
14881 }
14882 } else if (typeof data === 'string') {
14883 // default to x-www-form-urlencoded
14884 if (!type) this.type('form');
14885 type = this._header['content-type'];
14886
14887 if (type === 'application/x-www-form-urlencoded') {
14888 this._data = this._data ? "".concat(this._data, "&").concat(data) : data;
14889 } else {
14890 this._data = (this._data || '') + data;
14891 }
14892 } else {
14893 this._data = data;
14894 }
14895
14896 if (!isObj || this._isHost(data)) {
14897 return this;
14898 } // default to json
14899
14900
14901 if (!type) this.type('json');
14902 return this;
14903 };
14904 /**
14905 * Sort `querystring` by the sort function
14906 *
14907 *
14908 * Examples:
14909 *
14910 * // default order
14911 * request.get('/user')
14912 * .query('name=Nick')
14913 * .query('search=Manny')
14914 * .sortQuery()
14915 * .end(callback)
14916 *
14917 * // customized sort function
14918 * request.get('/user')
14919 * .query('name=Nick')
14920 * .query('search=Manny')
14921 * .sortQuery(function(a, b){
14922 * return a.length - b.length;
14923 * })
14924 * .end(callback)
14925 *
14926 *
14927 * @param {Function} sort
14928 * @return {Request} for chaining
14929 * @api public
14930 */
14931
14932
14933 RequestBase.prototype.sortQuery = function (sort) {
14934 // _sort default to true but otherwise can be a function or boolean
14935 this._sort = typeof sort === 'undefined' ? true : sort;
14936 return this;
14937 };
14938 /**
14939 * Compose querystring to append to req.url
14940 *
14941 * @api private
14942 */
14943
14944
14945 RequestBase.prototype._finalizeQueryString = function () {
14946 var query = this._query.join('&');
14947
14948 if (query) {
14949 this.url += (this.url.indexOf('?') >= 0 ? '&' : '?') + query;
14950 }
14951
14952 this._query.length = 0; // Makes the call idempotent
14953
14954 if (this._sort) {
14955 var index = this.url.indexOf('?');
14956
14957 if (index >= 0) {
14958 var queryArr = this.url.substring(index + 1).split('&');
14959
14960 if (typeof this._sort === 'function') {
14961 queryArr.sort(this._sort);
14962 } else {
14963 queryArr.sort();
14964 }
14965
14966 this.url = this.url.substring(0, index) + '?' + queryArr.join('&');
14967 }
14968 }
14969 }; // For backwards compat only
14970
14971
14972 RequestBase.prototype._appendQueryString = function () {
14973 console.warn('Unsupported');
14974 };
14975 /**
14976 * Invoke callback with timeout error.
14977 *
14978 * @api private
14979 */
14980
14981
14982 RequestBase.prototype._timeoutError = function (reason, timeout, errno) {
14983 if (this._aborted) {
14984 return;
14985 }
14986
14987 var err = new Error("".concat(reason + timeout, "ms exceeded"));
14988 err.timeout = timeout;
14989 err.code = 'ECONNABORTED';
14990 err.errno = errno;
14991 this.timedout = true;
14992 this.abort();
14993 this.callback(err);
14994 };
14995
14996 RequestBase.prototype._setTimeouts = function () {
14997 var self = this; // deadline
14998
14999 if (this._timeout && !this._timer) {
15000 this._timer = setTimeout(function () {
15001 self._timeoutError('Timeout of ', self._timeout, 'ETIME');
15002 }, this._timeout);
15003 } // response timeout
15004
15005
15006 if (this._responseTimeout && !this._responseTimeoutTimer) {
15007 this._responseTimeoutTimer = setTimeout(function () {
15008 self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
15009 }, this._responseTimeout);
15010 }
15011 };
15012
15013 /**
15014 * Return the mime type for the given `str`.
15015 *
15016 * @param {String} str
15017 * @return {String}
15018 * @api private
15019 */
15020
15021 var type = function type(str) {
15022 return str.split(/ *; */).shift();
15023 };
15024 /**
15025 * Return header field parameters.
15026 *
15027 * @param {String} str
15028 * @return {Object}
15029 * @api private
15030 */
15031
15032
15033 var params = function params(str) {
15034 return str.split(/ *; */).reduce(function (obj, str) {
15035 var parts = str.split(/ *= */);
15036 var key = parts.shift();
15037 var val = parts.shift();
15038 if (key && val) obj[key] = val;
15039 return obj;
15040 }, {});
15041 };
15042 /**
15043 * Parse Link header fields.
15044 *
15045 * @param {String} str
15046 * @return {Object}
15047 * @api private
15048 */
15049
15050
15051 var parseLinks = function parseLinks(str) {
15052 return str.split(/ *, */).reduce(function (obj, str) {
15053 var parts = str.split(/ *; */);
15054 var url = parts[0].slice(1, -1);
15055 var rel = parts[1].split(/ *= */)[1].slice(1, -1);
15056 obj[rel] = url;
15057 return obj;
15058 }, {});
15059 };
15060 /**
15061 * Strip content related fields from `header`.
15062 *
15063 * @param {Object} header
15064 * @return {Object} header
15065 * @api private
15066 */
15067
15068
15069 var cleanHeader = function cleanHeader(header, changesOrigin) {
15070 delete header['content-type'];
15071 delete header['content-length'];
15072 delete header['transfer-encoding'];
15073 delete header.host; // secuirty
15074
15075 if (changesOrigin) {
15076 delete header.authorization;
15077 delete header.cookie;
15078 }
15079
15080 return header;
15081 };
15082
15083 var utils = {
15084 type: type,
15085 params: params,
15086 parseLinks: parseLinks,
15087 cleanHeader: cleanHeader
15088 };
15089
15090 /**
15091 * Module dependencies.
15092 */
15093
15094 /**
15095 * Expose `ResponseBase`.
15096 */
15097
15098
15099 var responseBase = ResponseBase;
15100 /**
15101 * Initialize a new `ResponseBase`.
15102 *
15103 * @api public
15104 */
15105
15106 function ResponseBase(obj) {
15107 if (obj) return mixin$1(obj);
15108 }
15109 /**
15110 * Mixin the prototype properties.
15111 *
15112 * @param {Object} obj
15113 * @return {Object}
15114 * @api private
15115 */
15116
15117
15118 function mixin$1(obj) {
15119 for (var key in ResponseBase.prototype) {
15120 if (Object.prototype.hasOwnProperty.call(ResponseBase.prototype, key)) obj[key] = ResponseBase.prototype[key];
15121 }
15122
15123 return obj;
15124 }
15125 /**
15126 * Get case-insensitive `field` value.
15127 *
15128 * @param {String} field
15129 * @return {String}
15130 * @api public
15131 */
15132
15133
15134 ResponseBase.prototype.get = function (field) {
15135 return this.header[field.toLowerCase()];
15136 };
15137 /**
15138 * Set header related properties:
15139 *
15140 * - `.type` the content type without params
15141 *
15142 * A response of "Content-Type: text/plain; charset=utf-8"
15143 * will provide you with a `.type` of "text/plain".
15144 *
15145 * @param {Object} header
15146 * @api private
15147 */
15148
15149
15150 ResponseBase.prototype._setHeaderProperties = function (header) {
15151 // TODO: moar!
15152 // TODO: make this a util
15153 // content-type
15154 var ct = header['content-type'] || '';
15155 this.type = utils.type(ct); // params
15156
15157 var params = utils.params(ct);
15158
15159 for (var key in params) {
15160 if (Object.prototype.hasOwnProperty.call(params, key)) this[key] = params[key];
15161 }
15162
15163 this.links = {}; // links
15164
15165 try {
15166 if (header.link) {
15167 this.links = utils.parseLinks(header.link);
15168 }
15169 } catch (err) {// ignore
15170 }
15171 };
15172 /**
15173 * Set flags such as `.ok` based on `status`.
15174 *
15175 * For example a 2xx response will give you a `.ok` of __true__
15176 * whereas 5xx will be __false__ and `.error` will be __true__. The
15177 * `.clientError` and `.serverError` are also available to be more
15178 * specific, and `.statusType` is the class of error ranging from 1..5
15179 * sometimes useful for mapping respond colors etc.
15180 *
15181 * "sugar" properties are also defined for common cases. Currently providing:
15182 *
15183 * - .noContent
15184 * - .badRequest
15185 * - .unauthorized
15186 * - .notAcceptable
15187 * - .notFound
15188 *
15189 * @param {Number} status
15190 * @api private
15191 */
15192
15193
15194 ResponseBase.prototype._setStatusProperties = function (status) {
15195 var type = status / 100 | 0; // status / class
15196
15197 this.statusCode = status;
15198 this.status = this.statusCode;
15199 this.statusType = type; // basics
15200
15201 this.info = type === 1;
15202 this.ok = type === 2;
15203 this.redirect = type === 3;
15204 this.clientError = type === 4;
15205 this.serverError = type === 5;
15206 this.error = type === 4 || type === 5 ? this.toError() : false; // sugar
15207
15208 this.created = status === 201;
15209 this.accepted = status === 202;
15210 this.noContent = status === 204;
15211 this.badRequest = status === 400;
15212 this.unauthorized = status === 401;
15213 this.notAcceptable = status === 406;
15214 this.forbidden = status === 403;
15215 this.notFound = status === 404;
15216 this.unprocessableEntity = status === 422;
15217 };
15218
15219 function _toConsumableArray$1(arr) {
15220 return _arrayWithoutHoles$1(arr) || _iterableToArray$1(arr) || _nonIterableSpread$1();
15221 }
15222
15223 function _nonIterableSpread$1() {
15224 throw new TypeError("Invalid attempt to spread non-iterable instance");
15225 }
15226
15227 function _iterableToArray$1(iter) {
15228 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
15229 }
15230
15231 function _arrayWithoutHoles$1(arr) {
15232 if (Array.isArray(arr)) {
15233 for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
15234 arr2[i] = arr[i];
15235 }
15236
15237 return arr2;
15238 }
15239 }
15240
15241 function Agent() {
15242 this._defaults = [];
15243 }
15244
15245 ['use', 'on', 'once', 'set', 'query', 'type', 'accept', 'auth', 'withCredentials', 'sortQuery', 'retry', 'ok', 'redirects', 'timeout', 'buffer', 'serialize', 'parse', 'ca', 'key', 'pfx', 'cert'].forEach(function (fn) {
15246 // Default setting for all requests from this agent
15247 Agent.prototype[fn] = function () {
15248 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
15249 args[_key] = arguments[_key];
15250 }
15251
15252 this._defaults.push({
15253 fn: fn,
15254 args: args
15255 });
15256
15257 return this;
15258 };
15259 });
15260
15261 Agent.prototype._setDefaults = function (req) {
15262 this._defaults.forEach(function (def) {
15263 req[def.fn].apply(req, _toConsumableArray$1(def.args));
15264 });
15265 };
15266
15267 var agentBase = Agent;
15268
15269 var client = createCommonjsModule(function (module, exports) {
15270
15271 function _typeof$1(obj) {
15272 if (typeof Symbol === "function" && _typeof(Symbol.iterator) === "symbol") {
15273 _typeof$1 = function _typeof$1(obj) {
15274 return _typeof(obj);
15275 };
15276 } else {
15277 _typeof$1 = function _typeof$1(obj) {
15278 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof(obj);
15279 };
15280 }
15281
15282 return _typeof$1(obj);
15283 }
15284 /**
15285 * Root reference for iframes.
15286 */
15287
15288
15289 var root;
15290
15291 if (typeof window !== 'undefined') {
15292 // Browser window
15293 root = window;
15294 } else if (typeof self === 'undefined') {
15295 // Other environments
15296 console.warn('Using browser-only version of superagent in non-browser environment');
15297 root = void 0;
15298 } else {
15299 // Web Worker
15300 root = self;
15301 }
15302 /**
15303 * Noop.
15304 */
15305
15306
15307 function noop() {}
15308 /**
15309 * Expose `request`.
15310 */
15311
15312
15313 module.exports = function (method, url) {
15314 // callback
15315 if (typeof url === 'function') {
15316 return new exports.Request('GET', method).end(url);
15317 } // url first
15318
15319
15320 if (arguments.length === 1) {
15321 return new exports.Request('GET', method);
15322 }
15323
15324 return new exports.Request(method, url);
15325 };
15326
15327 exports = module.exports;
15328 var request = exports;
15329 exports.Request = Request;
15330 /**
15331 * Determine XHR.
15332 */
15333
15334 request.getXHR = function () {
15335 if (root.XMLHttpRequest && (!root.location || root.location.protocol !== 'file:' || !root.ActiveXObject)) {
15336 return new XMLHttpRequest();
15337 }
15338
15339 try {
15340 return new ActiveXObject('Microsoft.XMLHTTP');
15341 } catch (err) {}
15342
15343 try {
15344 return new ActiveXObject('Msxml2.XMLHTTP.6.0');
15345 } catch (err) {}
15346
15347 try {
15348 return new ActiveXObject('Msxml2.XMLHTTP.3.0');
15349 } catch (err) {}
15350
15351 try {
15352 return new ActiveXObject('Msxml2.XMLHTTP');
15353 } catch (err) {}
15354
15355 throw new Error('Browser-only version of superagent could not find XHR');
15356 };
15357 /**
15358 * Removes leading and trailing whitespace, added to support IE.
15359 *
15360 * @param {String} s
15361 * @return {String}
15362 * @api private
15363 */
15364
15365
15366 var trim = ''.trim ? function (s) {
15367 return s.trim();
15368 } : function (s) {
15369 return s.replace(/(^\s*|\s*$)/g, '');
15370 };
15371 /**
15372 * Serialize the given `obj`.
15373 *
15374 * @param {Object} obj
15375 * @return {String}
15376 * @api private
15377 */
15378
15379 function serialize(obj) {
15380 if (!isObject_1(obj)) return obj;
15381 var pairs = [];
15382
15383 for (var key in obj) {
15384 if (Object.prototype.hasOwnProperty.call(obj, key)) pushEncodedKeyValuePair(pairs, key, obj[key]);
15385 }
15386
15387 return pairs.join('&');
15388 }
15389 /**
15390 * Helps 'serialize' with serializing arrays.
15391 * Mutates the pairs array.
15392 *
15393 * @param {Array} pairs
15394 * @param {String} key
15395 * @param {Mixed} val
15396 */
15397
15398
15399 function pushEncodedKeyValuePair(pairs, key, val) {
15400 if (val === undefined) return;
15401
15402 if (val === null) {
15403 pairs.push(encodeURIComponent(key));
15404 return;
15405 }
15406
15407 if (Array.isArray(val)) {
15408 val.forEach(function (v) {
15409 pushEncodedKeyValuePair(pairs, key, v);
15410 });
15411 } else if (isObject_1(val)) {
15412 for (var subkey in val) {
15413 if (Object.prototype.hasOwnProperty.call(val, subkey)) pushEncodedKeyValuePair(pairs, "".concat(key, "[").concat(subkey, "]"), val[subkey]);
15414 }
15415 } else {
15416 pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(val));
15417 }
15418 }
15419 /**
15420 * Expose serialization method.
15421 */
15422
15423
15424 request.serializeObject = serialize;
15425 /**
15426 * Parse the given x-www-form-urlencoded `str`.
15427 *
15428 * @param {String} str
15429 * @return {Object}
15430 * @api private
15431 */
15432
15433 function parseString(str) {
15434 var obj = {};
15435 var pairs = str.split('&');
15436 var pair;
15437 var pos;
15438
15439 for (var i = 0, len = pairs.length; i < len; ++i) {
15440 pair = pairs[i];
15441 pos = pair.indexOf('=');
15442
15443 if (pos === -1) {
15444 obj[decodeURIComponent(pair)] = '';
15445 } else {
15446 obj[decodeURIComponent(pair.slice(0, pos))] = decodeURIComponent(pair.slice(pos + 1));
15447 }
15448 }
15449
15450 return obj;
15451 }
15452 /**
15453 * Expose parser.
15454 */
15455
15456
15457 request.parseString = parseString;
15458 /**
15459 * Default MIME type map.
15460 *
15461 * superagent.types.xml = 'application/xml';
15462 *
15463 */
15464
15465 request.types = {
15466 html: 'text/html',
15467 json: 'application/json',
15468 xml: 'text/xml',
15469 urlencoded: 'application/x-www-form-urlencoded',
15470 form: 'application/x-www-form-urlencoded',
15471 'form-data': 'application/x-www-form-urlencoded'
15472 };
15473 /**
15474 * Default serialization map.
15475 *
15476 * superagent.serialize['application/xml'] = function(obj){
15477 * return 'generated xml here';
15478 * };
15479 *
15480 */
15481
15482 request.serialize = {
15483 'application/x-www-form-urlencoded': serialize,
15484 'application/json': fastSafeStringify
15485 };
15486 /**
15487 * Default parsers.
15488 *
15489 * superagent.parse['application/xml'] = function(str){
15490 * return { object parsed from str };
15491 * };
15492 *
15493 */
15494
15495 request.parse = {
15496 'application/x-www-form-urlencoded': parseString,
15497 'application/json': JSON.parse
15498 };
15499 /**
15500 * Parse the given header `str` into
15501 * an object containing the mapped fields.
15502 *
15503 * @param {String} str
15504 * @return {Object}
15505 * @api private
15506 */
15507
15508 function parseHeader(str) {
15509 var lines = str.split(/\r?\n/);
15510 var fields = {};
15511 var index;
15512 var line;
15513 var field;
15514 var val;
15515
15516 for (var i = 0, len = lines.length; i < len; ++i) {
15517 line = lines[i];
15518 index = line.indexOf(':');
15519
15520 if (index === -1) {
15521 // could be empty line, just skip it
15522 continue;
15523 }
15524
15525 field = line.slice(0, index).toLowerCase();
15526 val = trim(line.slice(index + 1));
15527 fields[field] = val;
15528 }
15529
15530 return fields;
15531 }
15532 /**
15533 * Check if `mime` is json or has +json structured syntax suffix.
15534 *
15535 * @param {String} mime
15536 * @return {Boolean}
15537 * @api private
15538 */
15539
15540
15541 function isJSON(mime) {
15542 // should match /json or +json
15543 // but not /json-seq
15544 return /[/+]json($|[^-\w])/.test(mime);
15545 }
15546 /**
15547 * Initialize a new `Response` with the given `xhr`.
15548 *
15549 * - set flags (.ok, .error, etc)
15550 * - parse header
15551 *
15552 * Examples:
15553 *
15554 * Aliasing `superagent` as `request` is nice:
15555 *
15556 * request = superagent;
15557 *
15558 * We can use the promise-like API, or pass callbacks:
15559 *
15560 * request.get('/').end(function(res){});
15561 * request.get('/', function(res){});
15562 *
15563 * Sending data can be chained:
15564 *
15565 * request
15566 * .post('/user')
15567 * .send({ name: 'tj' })
15568 * .end(function(res){});
15569 *
15570 * Or passed to `.send()`:
15571 *
15572 * request
15573 * .post('/user')
15574 * .send({ name: 'tj' }, function(res){});
15575 *
15576 * Or passed to `.post()`:
15577 *
15578 * request
15579 * .post('/user', { name: 'tj' })
15580 * .end(function(res){});
15581 *
15582 * Or further reduced to a single call for simple cases:
15583 *
15584 * request
15585 * .post('/user', { name: 'tj' }, function(res){});
15586 *
15587 * @param {XMLHTTPRequest} xhr
15588 * @param {Object} options
15589 * @api private
15590 */
15591
15592
15593 function Response(req) {
15594 this.req = req;
15595 this.xhr = this.req.xhr; // responseText is accessible only if responseType is '' or 'text' and on older browsers
15596
15597 this.text = this.req.method !== 'HEAD' && (this.xhr.responseType === '' || this.xhr.responseType === 'text') || typeof this.xhr.responseType === 'undefined' ? this.xhr.responseText : null;
15598 this.statusText = this.req.xhr.statusText;
15599 var status = this.xhr.status; // handle IE9 bug: http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
15600
15601 if (status === 1223) {
15602 status = 204;
15603 }
15604
15605 this._setStatusProperties(status);
15606
15607 this.headers = parseHeader(this.xhr.getAllResponseHeaders());
15608 this.header = this.headers; // getAllResponseHeaders sometimes falsely returns "" for CORS requests, but
15609 // getResponseHeader still works. so we get content-type even if getting
15610 // other headers fails.
15611
15612 this.header['content-type'] = this.xhr.getResponseHeader('content-type');
15613
15614 this._setHeaderProperties(this.header);
15615
15616 if (this.text === null && req._responseType) {
15617 this.body = this.xhr.response;
15618 } else {
15619 this.body = this.req.method === 'HEAD' ? null : this._parseBody(this.text ? this.text : this.xhr.response);
15620 }
15621 } // eslint-disable-next-line new-cap
15622
15623
15624 responseBase(Response.prototype);
15625 /**
15626 * Parse the given body `str`.
15627 *
15628 * Used for auto-parsing of bodies. Parsers
15629 * are defined on the `superagent.parse` object.
15630 *
15631 * @param {String} str
15632 * @return {Mixed}
15633 * @api private
15634 */
15635
15636 Response.prototype._parseBody = function (str) {
15637 var parse = request.parse[this.type];
15638
15639 if (this.req._parser) {
15640 return this.req._parser(this, str);
15641 }
15642
15643 if (!parse && isJSON(this.type)) {
15644 parse = request.parse['application/json'];
15645 }
15646
15647 return parse && str && (str.length > 0 || str instanceof Object) ? parse(str) : null;
15648 };
15649 /**
15650 * Return an `Error` representative of this response.
15651 *
15652 * @return {Error}
15653 * @api public
15654 */
15655
15656
15657 Response.prototype.toError = function () {
15658 var req = this.req;
15659 var method = req.method;
15660 var url = req.url;
15661 var msg = "cannot ".concat(method, " ").concat(url, " (").concat(this.status, ")");
15662 var err = new Error(msg);
15663 err.status = this.status;
15664 err.method = method;
15665 err.url = url;
15666 return err;
15667 };
15668 /**
15669 * Expose `Response`.
15670 */
15671
15672
15673 request.Response = Response;
15674 /**
15675 * Initialize a new `Request` with the given `method` and `url`.
15676 *
15677 * @param {String} method
15678 * @param {String} url
15679 * @api public
15680 */
15681
15682 function Request(method, url) {
15683 var self = this;
15684 this._query = this._query || [];
15685 this.method = method;
15686 this.url = url;
15687 this.header = {}; // preserves header name case
15688
15689 this._header = {}; // coerces header names to lowercase
15690
15691 this.on('end', function () {
15692 var err = null;
15693 var res = null;
15694
15695 try {
15696 res = new Response(self);
15697 } catch (err2) {
15698 err = new Error('Parser is unable to parse the response');
15699 err.parse = true;
15700 err.original = err2; // issue #675: return the raw response if the response parsing fails
15701
15702 if (self.xhr) {
15703 // ie9 doesn't have 'response' property
15704 err.rawResponse = typeof self.xhr.responseType === 'undefined' ? self.xhr.responseText : self.xhr.response; // issue #876: return the http status code if the response parsing fails
15705
15706 err.status = self.xhr.status ? self.xhr.status : null;
15707 err.statusCode = err.status; // backwards-compat only
15708 } else {
15709 err.rawResponse = null;
15710 err.status = null;
15711 }
15712
15713 return self.callback(err);
15714 }
15715
15716 self.emit('response', res);
15717 var new_err;
15718
15719 try {
15720 if (!self._isResponseOK(res)) {
15721 new_err = new Error(res.statusText || 'Unsuccessful HTTP response');
15722 }
15723 } catch (err2) {
15724 new_err = err2; // ok() callback can throw
15725 } // #1000 don't catch errors from the callback to avoid double calling it
15726
15727
15728 if (new_err) {
15729 new_err.original = err;
15730 new_err.response = res;
15731 new_err.status = res.status;
15732 self.callback(new_err, res);
15733 } else {
15734 self.callback(null, res);
15735 }
15736 });
15737 }
15738 /**
15739 * Mixin `Emitter` and `RequestBase`.
15740 */
15741 // eslint-disable-next-line new-cap
15742
15743
15744 componentEmitter(Request.prototype); // eslint-disable-next-line new-cap
15745
15746 requestBase(Request.prototype);
15747 /**
15748 * Set Content-Type to `type`, mapping values from `request.types`.
15749 *
15750 * Examples:
15751 *
15752 * superagent.types.xml = 'application/xml';
15753 *
15754 * request.post('/')
15755 * .type('xml')
15756 * .send(xmlstring)
15757 * .end(callback);
15758 *
15759 * request.post('/')
15760 * .type('application/xml')
15761 * .send(xmlstring)
15762 * .end(callback);
15763 *
15764 * @param {String} type
15765 * @return {Request} for chaining
15766 * @api public
15767 */
15768
15769 Request.prototype.type = function (type) {
15770 this.set('Content-Type', request.types[type] || type);
15771 return this;
15772 };
15773 /**
15774 * Set Accept to `type`, mapping values from `request.types`.
15775 *
15776 * Examples:
15777 *
15778 * superagent.types.json = 'application/json';
15779 *
15780 * request.get('/agent')
15781 * .accept('json')
15782 * .end(callback);
15783 *
15784 * request.get('/agent')
15785 * .accept('application/json')
15786 * .end(callback);
15787 *
15788 * @param {String} accept
15789 * @return {Request} for chaining
15790 * @api public
15791 */
15792
15793
15794 Request.prototype.accept = function (type) {
15795 this.set('Accept', request.types[type] || type);
15796 return this;
15797 };
15798 /**
15799 * Set Authorization field value with `user` and `pass`.
15800 *
15801 * @param {String} user
15802 * @param {String} [pass] optional in case of using 'bearer' as type
15803 * @param {Object} options with 'type' property 'auto', 'basic' or 'bearer' (default 'basic')
15804 * @return {Request} for chaining
15805 * @api public
15806 */
15807
15808
15809 Request.prototype.auth = function (user, pass, options) {
15810 if (arguments.length === 1) pass = '';
15811
15812 if (_typeof$1(pass) === 'object' && pass !== null) {
15813 // pass is optional and can be replaced with options
15814 options = pass;
15815 pass = '';
15816 }
15817
15818 if (!options) {
15819 options = {
15820 type: typeof btoa === 'function' ? 'basic' : 'auto'
15821 };
15822 }
15823
15824 var encoder = function encoder(string) {
15825 if (typeof btoa === 'function') {
15826 return btoa(string);
15827 }
15828
15829 throw new Error('Cannot use basic auth, btoa is not a function');
15830 };
15831
15832 return this._auth(user, pass, options, encoder);
15833 };
15834 /**
15835 * Add query-string `val`.
15836 *
15837 * Examples:
15838 *
15839 * request.get('/shoes')
15840 * .query('size=10')
15841 * .query({ color: 'blue' })
15842 *
15843 * @param {Object|String} val
15844 * @return {Request} for chaining
15845 * @api public
15846 */
15847
15848
15849 Request.prototype.query = function (val) {
15850 if (typeof val !== 'string') val = serialize(val);
15851 if (val) this._query.push(val);
15852 return this;
15853 };
15854 /**
15855 * Queue the given `file` as an attachment to the specified `field`,
15856 * with optional `options` (or filename).
15857 *
15858 * ``` js
15859 * request.post('/upload')
15860 * .attach('content', new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"}))
15861 * .end(callback);
15862 * ```
15863 *
15864 * @param {String} field
15865 * @param {Blob|File} file
15866 * @param {String|Object} options
15867 * @return {Request} for chaining
15868 * @api public
15869 */
15870
15871
15872 Request.prototype.attach = function (field, file, options) {
15873 if (file) {
15874 if (this._data) {
15875 throw new Error("superagent can't mix .send() and .attach()");
15876 }
15877
15878 this._getFormData().append(field, file, options || file.name);
15879 }
15880
15881 return this;
15882 };
15883
15884 Request.prototype._getFormData = function () {
15885 if (!this._formData) {
15886 this._formData = new root.FormData();
15887 }
15888
15889 return this._formData;
15890 };
15891 /**
15892 * Invoke the callback with `err` and `res`
15893 * and handle arity check.
15894 *
15895 * @param {Error} err
15896 * @param {Response} res
15897 * @api private
15898 */
15899
15900
15901 Request.prototype.callback = function (err, res) {
15902 if (this._shouldRetry(err, res)) {
15903 return this._retry();
15904 }
15905
15906 var fn = this._callback;
15907 this.clearTimeout();
15908
15909 if (err) {
15910 if (this._maxRetries) err.retries = this._retries - 1;
15911 this.emit('error', err);
15912 }
15913
15914 fn(err, res);
15915 };
15916 /**
15917 * Invoke callback with x-domain error.
15918 *
15919 * @api private
15920 */
15921
15922
15923 Request.prototype.crossDomainError = function () {
15924 var err = new Error('Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.');
15925 err.crossDomain = true;
15926 err.status = this.status;
15927 err.method = this.method;
15928 err.url = this.url;
15929 this.callback(err);
15930 }; // This only warns, because the request is still likely to work
15931
15932
15933 Request.prototype.agent = function () {
15934 console.warn('This is not supported in browser version of superagent');
15935 return this;
15936 };
15937
15938 Request.prototype.buffer = Request.prototype.ca;
15939 Request.prototype.ca = Request.prototype.agent; // This throws, because it can't send/receive data as expected
15940
15941 Request.prototype.write = function () {
15942 throw new Error('Streaming is not supported in browser version of superagent');
15943 };
15944
15945 Request.prototype.pipe = Request.prototype.write;
15946 /**
15947 * Check if `obj` is a host object,
15948 * we don't want to serialize these :)
15949 *
15950 * @param {Object} obj host object
15951 * @return {Boolean} is a host object
15952 * @api private
15953 */
15954
15955 Request.prototype._isHost = function (obj) {
15956 // Native objects stringify to [object File], [object Blob], [object FormData], etc.
15957 return obj && _typeof$1(obj) === 'object' && !Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]';
15958 };
15959 /**
15960 * Initiate request, invoking callback `fn(res)`
15961 * with an instanceof `Response`.
15962 *
15963 * @param {Function} fn
15964 * @return {Request} for chaining
15965 * @api public
15966 */
15967
15968
15969 Request.prototype.end = function (fn) {
15970 if (this._endCalled) {
15971 console.warn('Warning: .end() was called twice. This is not supported in superagent');
15972 }
15973
15974 this._endCalled = true; // store callback
15975
15976 this._callback = fn || noop; // querystring
15977
15978 this._finalizeQueryString();
15979
15980 this._end();
15981 };
15982
15983 Request.prototype._setUploadTimeout = function () {
15984 var self = this; // upload timeout it's wokrs only if deadline timeout is off
15985
15986 if (this._uploadTimeout && !this._uploadTimeoutTimer) {
15987 this._uploadTimeoutTimer = setTimeout(function () {
15988 self._timeoutError('Upload timeout of ', self._uploadTimeout, 'ETIMEDOUT');
15989 }, this._uploadTimeout);
15990 }
15991 }; // eslint-disable-next-line complexity
15992
15993
15994 Request.prototype._end = function () {
15995 if (this._aborted) return this.callback(new Error('The request has been aborted even before .end() was called'));
15996 var self = this;
15997 this.xhr = request.getXHR();
15998 var xhr = this.xhr;
15999 var data = this._formData || this._data;
16000
16001 this._setTimeouts(); // state change
16002
16003
16004 xhr.onreadystatechange = function () {
16005 var readyState = xhr.readyState;
16006
16007 if (readyState >= 2 && self._responseTimeoutTimer) {
16008 clearTimeout(self._responseTimeoutTimer);
16009 }
16010
16011 if (readyState !== 4) {
16012 return;
16013 } // In IE9, reads to any property (e.g. status) off of an aborted XHR will
16014 // result in the error "Could not complete the operation due to error c00c023f"
16015
16016
16017 var status;
16018
16019 try {
16020 status = xhr.status;
16021 } catch (err) {
16022 status = 0;
16023 }
16024
16025 if (!status) {
16026 if (self.timedout || self._aborted) return;
16027 return self.crossDomainError();
16028 }
16029
16030 self.emit('end');
16031 }; // progress
16032
16033
16034 var handleProgress = function handleProgress(direction, e) {
16035 if (e.total > 0) {
16036 e.percent = e.loaded / e.total * 100;
16037
16038 if (e.percent === 100) {
16039 clearTimeout(self._uploadTimeoutTimer);
16040 }
16041 }
16042
16043 e.direction = direction;
16044 self.emit('progress', e);
16045 };
16046
16047 if (this.hasListeners('progress')) {
16048 try {
16049 xhr.addEventListener('progress', handleProgress.bind(null, 'download'));
16050
16051 if (xhr.upload) {
16052 xhr.upload.addEventListener('progress', handleProgress.bind(null, 'upload'));
16053 }
16054 } catch (err) {// Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
16055 // Reported here:
16056 // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
16057 }
16058 }
16059
16060 if (xhr.upload) {
16061 this._setUploadTimeout();
16062 } // initiate request
16063
16064
16065 try {
16066 if (this.username && this.password) {
16067 xhr.open(this.method, this.url, true, this.username, this.password);
16068 } else {
16069 xhr.open(this.method, this.url, true);
16070 }
16071 } catch (err) {
16072 // see #1149
16073 return this.callback(err);
16074 } // CORS
16075
16076
16077 if (this._withCredentials) xhr.withCredentials = true; // body
16078
16079 if (!this._formData && this.method !== 'GET' && this.method !== 'HEAD' && typeof data !== 'string' && !this._isHost(data)) {
16080 // serialize stuff
16081 var contentType = this._header['content-type'];
16082
16083 var _serialize = this._serializer || request.serialize[contentType ? contentType.split(';')[0] : ''];
16084
16085 if (!_serialize && isJSON(contentType)) {
16086 _serialize = request.serialize['application/json'];
16087 }
16088
16089 if (_serialize) data = _serialize(data);
16090 } // set header fields
16091
16092
16093 for (var field in this.header) {
16094 if (this.header[field] === null) continue;
16095 if (Object.prototype.hasOwnProperty.call(this.header, field)) xhr.setRequestHeader(field, this.header[field]);
16096 }
16097
16098 if (this._responseType) {
16099 xhr.responseType = this._responseType;
16100 } // send stuff
16101
16102
16103 this.emit('request', this); // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
16104 // We need null here if data is undefined
16105
16106 xhr.send(typeof data === 'undefined' ? null : data);
16107 };
16108
16109 request.agent = function () {
16110 return new agentBase();
16111 };
16112
16113 ['GET', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE'].forEach(function (method) {
16114 agentBase.prototype[method.toLowerCase()] = function (url, fn) {
16115 var req = new request.Request(method, url);
16116
16117 this._setDefaults(req);
16118
16119 if (fn) {
16120 req.end(fn);
16121 }
16122
16123 return req;
16124 };
16125 });
16126 agentBase.prototype.del = agentBase.prototype.delete;
16127 /**
16128 * GET `url` with optional callback `fn(res)`.
16129 *
16130 * @param {String} url
16131 * @param {Mixed|Function} [data] or fn
16132 * @param {Function} [fn]
16133 * @return {Request}
16134 * @api public
16135 */
16136
16137 request.get = function (url, data, fn) {
16138 var req = request('GET', url);
16139
16140 if (typeof data === 'function') {
16141 fn = data;
16142 data = null;
16143 }
16144
16145 if (data) req.query(data);
16146 if (fn) req.end(fn);
16147 return req;
16148 };
16149 /**
16150 * HEAD `url` with optional callback `fn(res)`.
16151 *
16152 * @param {String} url
16153 * @param {Mixed|Function} [data] or fn
16154 * @param {Function} [fn]
16155 * @return {Request}
16156 * @api public
16157 */
16158
16159
16160 request.head = function (url, data, fn) {
16161 var req = request('HEAD', url);
16162
16163 if (typeof data === 'function') {
16164 fn = data;
16165 data = null;
16166 }
16167
16168 if (data) req.query(data);
16169 if (fn) req.end(fn);
16170 return req;
16171 };
16172 /**
16173 * OPTIONS query to `url` with optional callback `fn(res)`.
16174 *
16175 * @param {String} url
16176 * @param {Mixed|Function} [data] or fn
16177 * @param {Function} [fn]
16178 * @return {Request}
16179 * @api public
16180 */
16181
16182
16183 request.options = function (url, data, fn) {
16184 var req = request('OPTIONS', url);
16185
16186 if (typeof data === 'function') {
16187 fn = data;
16188 data = null;
16189 }
16190
16191 if (data) req.send(data);
16192 if (fn) req.end(fn);
16193 return req;
16194 };
16195 /**
16196 * DELETE `url` with optional `data` and callback `fn(res)`.
16197 *
16198 * @param {String} url
16199 * @param {Mixed} [data]
16200 * @param {Function} [fn]
16201 * @return {Request}
16202 * @api public
16203 */
16204
16205
16206 function del(url, data, fn) {
16207 var req = request('DELETE', url);
16208
16209 if (typeof data === 'function') {
16210 fn = data;
16211 data = null;
16212 }
16213
16214 if (data) req.send(data);
16215 if (fn) req.end(fn);
16216 return req;
16217 }
16218
16219 request.del = del;
16220 request.delete = del;
16221 /**
16222 * PATCH `url` with optional `data` and callback `fn(res)`.
16223 *
16224 * @param {String} url
16225 * @param {Mixed} [data]
16226 * @param {Function} [fn]
16227 * @return {Request}
16228 * @api public
16229 */
16230
16231 request.patch = function (url, data, fn) {
16232 var req = request('PATCH', url);
16233
16234 if (typeof data === 'function') {
16235 fn = data;
16236 data = null;
16237 }
16238
16239 if (data) req.send(data);
16240 if (fn) req.end(fn);
16241 return req;
16242 };
16243 /**
16244 * POST `url` with optional `data` and callback `fn(res)`.
16245 *
16246 * @param {String} url
16247 * @param {Mixed} [data]
16248 * @param {Function} [fn]
16249 * @return {Request}
16250 * @api public
16251 */
16252
16253
16254 request.post = function (url, data, fn) {
16255 var req = request('POST', url);
16256
16257 if (typeof data === 'function') {
16258 fn = data;
16259 data = null;
16260 }
16261
16262 if (data) req.send(data);
16263 if (fn) req.end(fn);
16264 return req;
16265 };
16266 /**
16267 * PUT `url` with optional `data` and callback `fn(res)`.
16268 *
16269 * @param {String} url
16270 * @param {Mixed|Function} [data] or fn
16271 * @param {Function} [fn]
16272 * @return {Request}
16273 * @api public
16274 */
16275
16276
16277 request.put = function (url, data, fn) {
16278 var req = request('PUT', url);
16279
16280 if (typeof data === 'function') {
16281 fn = data;
16282 data = null;
16283 }
16284
16285 if (data) req.send(data);
16286 if (fn) req.end(fn);
16287 return req;
16288 };
16289 });
16290 var client_1 = client.Request;
16291
16292 /**
16293 * Copies the values of `source` to `array`.
16294 *
16295 * @private
16296 * @param {Array} source The array to copy values from.
16297 * @param {Array} [array=[]] The array to copy values to.
16298 * @returns {Array} Returns `array`.
16299 */
16300 function copyArray(source, array) {
16301 var index = -1,
16302 length = source.length;
16303
16304 array || (array = Array(length));
16305 while (++index < length) {
16306 array[index] = source[index];
16307 }
16308 return array;
16309 }
16310
16311 var _copyArray = copyArray;
16312
16313 /* Built-in method references for those with the same name as other `lodash` methods. */
16314 var nativeFloor = Math.floor,
16315 nativeRandom = Math.random;
16316
16317 /**
16318 * The base implementation of `_.random` without support for returning
16319 * floating-point numbers.
16320 *
16321 * @private
16322 * @param {number} lower The lower bound.
16323 * @param {number} upper The upper bound.
16324 * @returns {number} Returns the random number.
16325 */
16326 function baseRandom(lower, upper) {
16327 return lower + nativeFloor(nativeRandom() * (upper - lower + 1));
16328 }
16329
16330 var _baseRandom = baseRandom;
16331
16332 /**
16333 * A specialized version of `_.shuffle` which mutates and sets the size of `array`.
16334 *
16335 * @private
16336 * @param {Array} array The array to shuffle.
16337 * @param {number} [size=array.length] The size of `array`.
16338 * @returns {Array} Returns `array`.
16339 */
16340 function shuffleSelf(array, size) {
16341 var index = -1,
16342 length = array.length,
16343 lastIndex = length - 1;
16344
16345 size = size === undefined ? length : size;
16346 while (++index < size) {
16347 var rand = _baseRandom(index, lastIndex),
16348 value = array[rand];
16349
16350 array[rand] = array[index];
16351 array[index] = value;
16352 }
16353 array.length = size;
16354 return array;
16355 }
16356
16357 var _shuffleSelf = shuffleSelf;
16358
16359 /**
16360 * A specialized version of `_.shuffle` for arrays.
16361 *
16362 * @private
16363 * @param {Array} array The array to shuffle.
16364 * @returns {Array} Returns the new shuffled array.
16365 */
16366 function arrayShuffle(array) {
16367 return _shuffleSelf(_copyArray(array));
16368 }
16369
16370 var _arrayShuffle = arrayShuffle;
16371
16372 /**
16373 * A specialized version of `_.map` for arrays without support for iteratee
16374 * shorthands.
16375 *
16376 * @private
16377 * @param {Array} [array] The array to iterate over.
16378 * @param {Function} iteratee The function invoked per iteration.
16379 * @returns {Array} Returns the new mapped array.
16380 */
16381 function arrayMap(array, iteratee) {
16382 var index = -1,
16383 length = array == null ? 0 : array.length,
16384 result = Array(length);
16385
16386 while (++index < length) {
16387 result[index] = iteratee(array[index], index, array);
16388 }
16389 return result;
16390 }
16391
16392 var _arrayMap = arrayMap;
16393
16394 /**
16395 * The base implementation of `_.values` and `_.valuesIn` which creates an
16396 * array of `object` property values corresponding to the property names
16397 * of `props`.
16398 *
16399 * @private
16400 * @param {Object} object The object to query.
16401 * @param {Array} props The property names to get values for.
16402 * @returns {Object} Returns the array of property values.
16403 */
16404 function baseValues(object, props) {
16405 return _arrayMap(props, function(key) {
16406 return object[key];
16407 });
16408 }
16409
16410 var _baseValues = baseValues;
16411
16412 /**
16413 * The base implementation of `_.times` without support for iteratee shorthands
16414 * or max array length checks.
16415 *
16416 * @private
16417 * @param {number} n The number of times to invoke `iteratee`.
16418 * @param {Function} iteratee The function invoked per iteration.
16419 * @returns {Array} Returns the array of results.
16420 */
16421 function baseTimes(n, iteratee) {
16422 var index = -1,
16423 result = Array(n);
16424
16425 while (++index < n) {
16426 result[index] = iteratee(index);
16427 }
16428 return result;
16429 }
16430
16431 var _baseTimes = baseTimes;
16432
16433 /** Detect free variable `global` from Node.js. */
16434 var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal;
16435
16436 var _freeGlobal = freeGlobal;
16437
16438 /** Detect free variable `self`. */
16439 var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
16440
16441 /** Used as a reference to the global object. */
16442 var root = _freeGlobal || freeSelf || Function('return this')();
16443
16444 var _root = root;
16445
16446 /** Built-in value references. */
16447 var Symbol$1 = _root.Symbol;
16448
16449 var _Symbol = Symbol$1;
16450
16451 /** Used for built-in method references. */
16452 var objectProto = Object.prototype;
16453
16454 /** Used to check objects for own properties. */
16455 var hasOwnProperty$1 = objectProto.hasOwnProperty;
16456
16457 /**
16458 * Used to resolve the
16459 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
16460 * of values.
16461 */
16462 var nativeObjectToString = objectProto.toString;
16463
16464 /** Built-in value references. */
16465 var symToStringTag = _Symbol ? _Symbol.toStringTag : undefined;
16466
16467 /**
16468 * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
16469 *
16470 * @private
16471 * @param {*} value The value to query.
16472 * @returns {string} Returns the raw `toStringTag`.
16473 */
16474 function getRawTag(value) {
16475 var isOwn = hasOwnProperty$1.call(value, symToStringTag),
16476 tag = value[symToStringTag];
16477
16478 try {
16479 value[symToStringTag] = undefined;
16480 var unmasked = true;
16481 } catch (e) {}
16482
16483 var result = nativeObjectToString.call(value);
16484 if (unmasked) {
16485 if (isOwn) {
16486 value[symToStringTag] = tag;
16487 } else {
16488 delete value[symToStringTag];
16489 }
16490 }
16491 return result;
16492 }
16493
16494 var _getRawTag = getRawTag;
16495
16496 /** Used for built-in method references. */
16497 var objectProto$1 = Object.prototype;
16498
16499 /**
16500 * Used to resolve the
16501 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
16502 * of values.
16503 */
16504 var nativeObjectToString$1 = objectProto$1.toString;
16505
16506 /**
16507 * Converts `value` to a string using `Object.prototype.toString`.
16508 *
16509 * @private
16510 * @param {*} value The value to convert.
16511 * @returns {string} Returns the converted string.
16512 */
16513 function objectToString(value) {
16514 return nativeObjectToString$1.call(value);
16515 }
16516
16517 var _objectToString = objectToString;
16518
16519 /** `Object#toString` result references. */
16520 var nullTag = '[object Null]',
16521 undefinedTag = '[object Undefined]';
16522
16523 /** Built-in value references. */
16524 var symToStringTag$1 = _Symbol ? _Symbol.toStringTag : undefined;
16525
16526 /**
16527 * The base implementation of `getTag` without fallbacks for buggy environments.
16528 *
16529 * @private
16530 * @param {*} value The value to query.
16531 * @returns {string} Returns the `toStringTag`.
16532 */
16533 function baseGetTag(value) {
16534 if (value == null) {
16535 return value === undefined ? undefinedTag : nullTag;
16536 }
16537 return (symToStringTag$1 && symToStringTag$1 in Object(value))
16538 ? _getRawTag(value)
16539 : _objectToString(value);
16540 }
16541
16542 var _baseGetTag = baseGetTag;
16543
16544 /**
16545 * Checks if `value` is object-like. A value is object-like if it's not `null`
16546 * and has a `typeof` result of "object".
16547 *
16548 * @static
16549 * @memberOf _
16550 * @since 4.0.0
16551 * @category Lang
16552 * @param {*} value The value to check.
16553 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
16554 * @example
16555 *
16556 * _.isObjectLike({});
16557 * // => true
16558 *
16559 * _.isObjectLike([1, 2, 3]);
16560 * // => true
16561 *
16562 * _.isObjectLike(_.noop);
16563 * // => false
16564 *
16565 * _.isObjectLike(null);
16566 * // => false
16567 */
16568 function isObjectLike(value) {
16569 return value != null && typeof value == 'object';
16570 }
16571
16572 var isObjectLike_1 = isObjectLike;
16573
16574 /** `Object#toString` result references. */
16575 var argsTag = '[object Arguments]';
16576
16577 /**
16578 * The base implementation of `_.isArguments`.
16579 *
16580 * @private
16581 * @param {*} value The value to check.
16582 * @returns {boolean} Returns `true` if `value` is an `arguments` object,
16583 */
16584 function baseIsArguments(value) {
16585 return isObjectLike_1(value) && _baseGetTag(value) == argsTag;
16586 }
16587
16588 var _baseIsArguments = baseIsArguments;
16589
16590 /** Used for built-in method references. */
16591 var objectProto$2 = Object.prototype;
16592
16593 /** Used to check objects for own properties. */
16594 var hasOwnProperty$2 = objectProto$2.hasOwnProperty;
16595
16596 /** Built-in value references. */
16597 var propertyIsEnumerable = objectProto$2.propertyIsEnumerable;
16598
16599 /**
16600 * Checks if `value` is likely an `arguments` object.
16601 *
16602 * @static
16603 * @memberOf _
16604 * @since 0.1.0
16605 * @category Lang
16606 * @param {*} value The value to check.
16607 * @returns {boolean} Returns `true` if `value` is an `arguments` object,
16608 * else `false`.
16609 * @example
16610 *
16611 * _.isArguments(function() { return arguments; }());
16612 * // => true
16613 *
16614 * _.isArguments([1, 2, 3]);
16615 * // => false
16616 */
16617 var isArguments = _baseIsArguments(function() { return arguments; }()) ? _baseIsArguments : function(value) {
16618 return isObjectLike_1(value) && hasOwnProperty$2.call(value, 'callee') &&
16619 !propertyIsEnumerable.call(value, 'callee');
16620 };
16621
16622 var isArguments_1 = isArguments;
16623
16624 /**
16625 * Checks if `value` is classified as an `Array` object.
16626 *
16627 * @static
16628 * @memberOf _
16629 * @since 0.1.0
16630 * @category Lang
16631 * @param {*} value The value to check.
16632 * @returns {boolean} Returns `true` if `value` is an array, else `false`.
16633 * @example
16634 *
16635 * _.isArray([1, 2, 3]);
16636 * // => true
16637 *
16638 * _.isArray(document.body.children);
16639 * // => false
16640 *
16641 * _.isArray('abc');
16642 * // => false
16643 *
16644 * _.isArray(_.noop);
16645 * // => false
16646 */
16647 var isArray = Array.isArray;
16648
16649 var isArray_1 = isArray;
16650
16651 /**
16652 * This method returns `false`.
16653 *
16654 * @static
16655 * @memberOf _
16656 * @since 4.13.0
16657 * @category Util
16658 * @returns {boolean} Returns `false`.
16659 * @example
16660 *
16661 * _.times(2, _.stubFalse);
16662 * // => [false, false]
16663 */
16664 function stubFalse() {
16665 return false;
16666 }
16667
16668 var stubFalse_1 = stubFalse;
16669
16670 var isBuffer_1 = createCommonjsModule(function (module, exports) {
16671 /** Detect free variable `exports`. */
16672 var freeExports = exports && !exports.nodeType && exports;
16673
16674 /** Detect free variable `module`. */
16675 var freeModule = freeExports && 'object' == 'object' && module && !module.nodeType && module;
16676
16677 /** Detect the popular CommonJS extension `module.exports`. */
16678 var moduleExports = freeModule && freeModule.exports === freeExports;
16679
16680 /** Built-in value references. */
16681 var Buffer = moduleExports ? _root.Buffer : undefined;
16682
16683 /* Built-in method references for those with the same name as other `lodash` methods. */
16684 var nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined;
16685
16686 /**
16687 * Checks if `value` is a buffer.
16688 *
16689 * @static
16690 * @memberOf _
16691 * @since 4.3.0
16692 * @category Lang
16693 * @param {*} value The value to check.
16694 * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
16695 * @example
16696 *
16697 * _.isBuffer(new Buffer(2));
16698 * // => true
16699 *
16700 * _.isBuffer(new Uint8Array(2));
16701 * // => false
16702 */
16703 var isBuffer = nativeIsBuffer || stubFalse_1;
16704
16705 module.exports = isBuffer;
16706 });
16707
16708 /** Used as references for various `Number` constants. */
16709 var MAX_SAFE_INTEGER = 9007199254740991;
16710
16711 /** Used to detect unsigned integer values. */
16712 var reIsUint = /^(?:0|[1-9]\d*)$/;
16713
16714 /**
16715 * Checks if `value` is a valid array-like index.
16716 *
16717 * @private
16718 * @param {*} value The value to check.
16719 * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
16720 * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
16721 */
16722 function isIndex(value, length) {
16723 var type = typeof value;
16724 length = length == null ? MAX_SAFE_INTEGER : length;
16725
16726 return !!length &&
16727 (type == 'number' ||
16728 (type != 'symbol' && reIsUint.test(value))) &&
16729 (value > -1 && value % 1 == 0 && value < length);
16730 }
16731
16732 var _isIndex = isIndex;
16733
16734 /** Used as references for various `Number` constants. */
16735 var MAX_SAFE_INTEGER$1 = 9007199254740991;
16736
16737 /**
16738 * Checks if `value` is a valid array-like length.
16739 *
16740 * **Note:** This method is loosely based on
16741 * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
16742 *
16743 * @static
16744 * @memberOf _
16745 * @since 4.0.0
16746 * @category Lang
16747 * @param {*} value The value to check.
16748 * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
16749 * @example
16750 *
16751 * _.isLength(3);
16752 * // => true
16753 *
16754 * _.isLength(Number.MIN_VALUE);
16755 * // => false
16756 *
16757 * _.isLength(Infinity);
16758 * // => false
16759 *
16760 * _.isLength('3');
16761 * // => false
16762 */
16763 function isLength(value) {
16764 return typeof value == 'number' &&
16765 value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER$1;
16766 }
16767
16768 var isLength_1 = isLength;
16769
16770 /** `Object#toString` result references. */
16771 var argsTag$1 = '[object Arguments]',
16772 arrayTag = '[object Array]',
16773 boolTag = '[object Boolean]',
16774 dateTag = '[object Date]',
16775 errorTag = '[object Error]',
16776 funcTag = '[object Function]',
16777 mapTag = '[object Map]',
16778 numberTag = '[object Number]',
16779 objectTag = '[object Object]',
16780 regexpTag = '[object RegExp]',
16781 setTag = '[object Set]',
16782 stringTag = '[object String]',
16783 weakMapTag = '[object WeakMap]';
16784
16785 var arrayBufferTag = '[object ArrayBuffer]',
16786 dataViewTag = '[object DataView]',
16787 float32Tag = '[object Float32Array]',
16788 float64Tag = '[object Float64Array]',
16789 int8Tag = '[object Int8Array]',
16790 int16Tag = '[object Int16Array]',
16791 int32Tag = '[object Int32Array]',
16792 uint8Tag = '[object Uint8Array]',
16793 uint8ClampedTag = '[object Uint8ClampedArray]',
16794 uint16Tag = '[object Uint16Array]',
16795 uint32Tag = '[object Uint32Array]';
16796
16797 /** Used to identify `toStringTag` values of typed arrays. */
16798 var typedArrayTags = {};
16799 typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
16800 typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
16801 typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
16802 typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
16803 typedArrayTags[uint32Tag] = true;
16804 typedArrayTags[argsTag$1] = typedArrayTags[arrayTag] =
16805 typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
16806 typedArrayTags[dataViewTag] = typedArrayTags[dateTag] =
16807 typedArrayTags[errorTag] = typedArrayTags[funcTag] =
16808 typedArrayTags[mapTag] = typedArrayTags[numberTag] =
16809 typedArrayTags[objectTag] = typedArrayTags[regexpTag] =
16810 typedArrayTags[setTag] = typedArrayTags[stringTag] =
16811 typedArrayTags[weakMapTag] = false;
16812
16813 /**
16814 * The base implementation of `_.isTypedArray` without Node.js optimizations.
16815 *
16816 * @private
16817 * @param {*} value The value to check.
16818 * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.
16819 */
16820 function baseIsTypedArray(value) {
16821 return isObjectLike_1(value) &&
16822 isLength_1(value.length) && !!typedArrayTags[_baseGetTag(value)];
16823 }
16824
16825 var _baseIsTypedArray = baseIsTypedArray;
16826
16827 /**
16828 * The base implementation of `_.unary` without support for storing metadata.
16829 *
16830 * @private
16831 * @param {Function} func The function to cap arguments for.
16832 * @returns {Function} Returns the new capped function.
16833 */
16834 function baseUnary(func) {
16835 return function(value) {
16836 return func(value);
16837 };
16838 }
16839
16840 var _baseUnary = baseUnary;
16841
16842 var _nodeUtil = createCommonjsModule(function (module, exports) {
16843 /** Detect free variable `exports`. */
16844 var freeExports = exports && !exports.nodeType && exports;
16845
16846 /** Detect free variable `module`. */
16847 var freeModule = freeExports && 'object' == 'object' && module && !module.nodeType && module;
16848
16849 /** Detect the popular CommonJS extension `module.exports`. */
16850 var moduleExports = freeModule && freeModule.exports === freeExports;
16851
16852 /** Detect free variable `process` from Node.js. */
16853 var freeProcess = moduleExports && _freeGlobal.process;
16854
16855 /** Used to access faster Node.js helpers. */
16856 var nodeUtil = (function() {
16857 try {
16858 // Use `util.types` for Node.js 10+.
16859 var types = freeModule && freeModule.require && freeModule.require('util').types;
16860
16861 if (types) {
16862 return types;
16863 }
16864
16865 // Legacy `process.binding('util')` for Node.js < 10.
16866 return freeProcess && freeProcess.binding && freeProcess.binding('util');
16867 } catch (e) {}
16868 }());
16869
16870 module.exports = nodeUtil;
16871 });
16872
16873 /* Node.js helper references. */
16874 var nodeIsTypedArray = _nodeUtil && _nodeUtil.isTypedArray;
16875
16876 /**
16877 * Checks if `value` is classified as a typed array.
16878 *
16879 * @static
16880 * @memberOf _
16881 * @since 3.0.0
16882 * @category Lang
16883 * @param {*} value The value to check.
16884 * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.
16885 * @example
16886 *
16887 * _.isTypedArray(new Uint8Array);
16888 * // => true
16889 *
16890 * _.isTypedArray([]);
16891 * // => false
16892 */
16893 var isTypedArray = nodeIsTypedArray ? _baseUnary(nodeIsTypedArray) : _baseIsTypedArray;
16894
16895 var isTypedArray_1 = isTypedArray;
16896
16897 /** Used for built-in method references. */
16898 var objectProto$3 = Object.prototype;
16899
16900 /** Used to check objects for own properties. */
16901 var hasOwnProperty$3 = objectProto$3.hasOwnProperty;
16902
16903 /**
16904 * Creates an array of the enumerable property names of the array-like `value`.
16905 *
16906 * @private
16907 * @param {*} value The value to query.
16908 * @param {boolean} inherited Specify returning inherited property names.
16909 * @returns {Array} Returns the array of property names.
16910 */
16911 function arrayLikeKeys(value, inherited) {
16912 var isArr = isArray_1(value),
16913 isArg = !isArr && isArguments_1(value),
16914 isBuff = !isArr && !isArg && isBuffer_1(value),
16915 isType = !isArr && !isArg && !isBuff && isTypedArray_1(value),
16916 skipIndexes = isArr || isArg || isBuff || isType,
16917 result = skipIndexes ? _baseTimes(value.length, String) : [],
16918 length = result.length;
16919
16920 for (var key in value) {
16921 if ((inherited || hasOwnProperty$3.call(value, key)) &&
16922 !(skipIndexes && (
16923 // Safari 9 has enumerable `arguments.length` in strict mode.
16924 key == 'length' ||
16925 // Node.js 0.10 has enumerable non-index properties on buffers.
16926 (isBuff && (key == 'offset' || key == 'parent')) ||
16927 // PhantomJS 2 has enumerable non-index properties on typed arrays.
16928 (isType && (key == 'buffer' || key == 'byteLength' || key == 'byteOffset')) ||
16929 // Skip index properties.
16930 _isIndex(key, length)
16931 ))) {
16932 result.push(key);
16933 }
16934 }
16935 return result;
16936 }
16937
16938 var _arrayLikeKeys = arrayLikeKeys;
16939
16940 /** Used for built-in method references. */
16941 var objectProto$4 = Object.prototype;
16942
16943 /**
16944 * Checks if `value` is likely a prototype object.
16945 *
16946 * @private
16947 * @param {*} value The value to check.
16948 * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
16949 */
16950 function isPrototype(value) {
16951 var Ctor = value && value.constructor,
16952 proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto$4;
16953
16954 return value === proto;
16955 }
16956
16957 var _isPrototype = isPrototype;
16958
16959 /**
16960 * Creates a unary function that invokes `func` with its argument transformed.
16961 *
16962 * @private
16963 * @param {Function} func The function to wrap.
16964 * @param {Function} transform The argument transform.
16965 * @returns {Function} Returns the new function.
16966 */
16967 function overArg(func, transform) {
16968 return function(arg) {
16969 return func(transform(arg));
16970 };
16971 }
16972
16973 var _overArg = overArg;
16974
16975 /* Built-in method references for those with the same name as other `lodash` methods. */
16976 var nativeKeys = _overArg(Object.keys, Object);
16977
16978 var _nativeKeys = nativeKeys;
16979
16980 /** Used for built-in method references. */
16981 var objectProto$5 = Object.prototype;
16982
16983 /** Used to check objects for own properties. */
16984 var hasOwnProperty$4 = objectProto$5.hasOwnProperty;
16985
16986 /**
16987 * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
16988 *
16989 * @private
16990 * @param {Object} object The object to query.
16991 * @returns {Array} Returns the array of property names.
16992 */
16993 function baseKeys(object) {
16994 if (!_isPrototype(object)) {
16995 return _nativeKeys(object);
16996 }
16997 var result = [];
16998 for (var key in Object(object)) {
16999 if (hasOwnProperty$4.call(object, key) && key != 'constructor') {
17000 result.push(key);
17001 }
17002 }
17003 return result;
17004 }
17005
17006 var _baseKeys = baseKeys;
17007
17008 /**
17009 * Checks if `value` is the
17010 * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
17011 * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
17012 *
17013 * @static
17014 * @memberOf _
17015 * @since 0.1.0
17016 * @category Lang
17017 * @param {*} value The value to check.
17018 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
17019 * @example
17020 *
17021 * _.isObject({});
17022 * // => true
17023 *
17024 * _.isObject([1, 2, 3]);
17025 * // => true
17026 *
17027 * _.isObject(_.noop);
17028 * // => true
17029 *
17030 * _.isObject(null);
17031 * // => false
17032 */
17033 function isObject$1(value) {
17034 var type = typeof value;
17035 return value != null && (type == 'object' || type == 'function');
17036 }
17037
17038 var isObject_1$1 = isObject$1;
17039
17040 /** `Object#toString` result references. */
17041 var asyncTag = '[object AsyncFunction]',
17042 funcTag$1 = '[object Function]',
17043 genTag = '[object GeneratorFunction]',
17044 proxyTag = '[object Proxy]';
17045
17046 /**
17047 * Checks if `value` is classified as a `Function` object.
17048 *
17049 * @static
17050 * @memberOf _
17051 * @since 0.1.0
17052 * @category Lang
17053 * @param {*} value The value to check.
17054 * @returns {boolean} Returns `true` if `value` is a function, else `false`.
17055 * @example
17056 *
17057 * _.isFunction(_);
17058 * // => true
17059 *
17060 * _.isFunction(/abc/);
17061 * // => false
17062 */
17063 function isFunction(value) {
17064 if (!isObject_1$1(value)) {
17065 return false;
17066 }
17067 // The use of `Object#toString` avoids issues with the `typeof` operator
17068 // in Safari 9 which returns 'object' for typed arrays and other constructors.
17069 var tag = _baseGetTag(value);
17070 return tag == funcTag$1 || tag == genTag || tag == asyncTag || tag == proxyTag;
17071 }
17072
17073 var isFunction_1 = isFunction;
17074
17075 /**
17076 * Checks if `value` is array-like. A value is considered array-like if it's
17077 * not a function and has a `value.length` that's an integer greater than or
17078 * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
17079 *
17080 * @static
17081 * @memberOf _
17082 * @since 4.0.0
17083 * @category Lang
17084 * @param {*} value The value to check.
17085 * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
17086 * @example
17087 *
17088 * _.isArrayLike([1, 2, 3]);
17089 * // => true
17090 *
17091 * _.isArrayLike(document.body.children);
17092 * // => true
17093 *
17094 * _.isArrayLike('abc');
17095 * // => true
17096 *
17097 * _.isArrayLike(_.noop);
17098 * // => false
17099 */
17100 function isArrayLike(value) {
17101 return value != null && isLength_1(value.length) && !isFunction_1(value);
17102 }
17103
17104 var isArrayLike_1 = isArrayLike;
17105
17106 /**
17107 * Creates an array of the own enumerable property names of `object`.
17108 *
17109 * **Note:** Non-object values are coerced to objects. See the
17110 * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
17111 * for more details.
17112 *
17113 * @static
17114 * @since 0.1.0
17115 * @memberOf _
17116 * @category Object
17117 * @param {Object} object The object to query.
17118 * @returns {Array} Returns the array of property names.
17119 * @example
17120 *
17121 * function Foo() {
17122 * this.a = 1;
17123 * this.b = 2;
17124 * }
17125 *
17126 * Foo.prototype.c = 3;
17127 *
17128 * _.keys(new Foo);
17129 * // => ['a', 'b'] (iteration order is not guaranteed)
17130 *
17131 * _.keys('hi');
17132 * // => ['0', '1']
17133 */
17134 function keys(object) {
17135 return isArrayLike_1(object) ? _arrayLikeKeys(object) : _baseKeys(object);
17136 }
17137
17138 var keys_1 = keys;
17139
17140 /**
17141 * Creates an array of the own enumerable string keyed property values of `object`.
17142 *
17143 * **Note:** Non-object values are coerced to objects.
17144 *
17145 * @static
17146 * @since 0.1.0
17147 * @memberOf _
17148 * @category Object
17149 * @param {Object} object The object to query.
17150 * @returns {Array} Returns the array of property values.
17151 * @example
17152 *
17153 * function Foo() {
17154 * this.a = 1;
17155 * this.b = 2;
17156 * }
17157 *
17158 * Foo.prototype.c = 3;
17159 *
17160 * _.values(new Foo);
17161 * // => [1, 2] (iteration order is not guaranteed)
17162 *
17163 * _.values('hi');
17164 * // => ['h', 'i']
17165 */
17166 function values(object) {
17167 return object == null ? [] : _baseValues(object, keys_1(object));
17168 }
17169
17170 var values_1 = values;
17171
17172 /**
17173 * The base implementation of `_.shuffle`.
17174 *
17175 * @private
17176 * @param {Array|Object} collection The collection to shuffle.
17177 * @returns {Array} Returns the new shuffled array.
17178 */
17179 function baseShuffle(collection) {
17180 return _shuffleSelf(values_1(collection));
17181 }
17182
17183 var _baseShuffle = baseShuffle;
17184
17185 /**
17186 * Creates an array of shuffled values, using a version of the
17187 * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
17188 *
17189 * @static
17190 * @memberOf _
17191 * @since 0.1.0
17192 * @category Collection
17193 * @param {Array|Object} collection The collection to shuffle.
17194 * @returns {Array} Returns the new shuffled array.
17195 * @example
17196 *
17197 * _.shuffle([1, 2, 3, 4]);
17198 * // => [4, 1, 3, 2]
17199 */
17200 function shuffle(collection) {
17201 var func = isArray_1(collection) ? _arrayShuffle : _baseShuffle;
17202 return func(collection);
17203 }
17204
17205 var shuffle_1 = shuffle;
17206
17207 var stateMachine = createCommonjsModule(function (module, exports) {
17208 /*
17209
17210 Javascript State Machine Library - https://github.com/jakesgordon/javascript-state-machine
17211
17212 Copyright (c) 2012, 2013, 2014, 2015, Jake Gordon and contributors
17213 Released under the MIT license - https://github.com/jakesgordon/javascript-state-machine/blob/master/LICENSE
17214
17215 */
17216
17217 (function () {
17218
17219 var StateMachine = {
17220
17221 //---------------------------------------------------------------------------
17222
17223 VERSION: "2.4.0",
17224
17225 //---------------------------------------------------------------------------
17226
17227 Result: {
17228 SUCCEEDED: 1, // the event transitioned successfully from one state to another
17229 NOTRANSITION: 2, // the event was successfull but no state transition was necessary
17230 CANCELLED: 3, // the event was cancelled by the caller in a beforeEvent callback
17231 PENDING: 4 // the event is asynchronous and the caller is in control of when the transition occurs
17232 },
17233
17234 Error: {
17235 INVALID_TRANSITION: 100, // caller tried to fire an event that was innapropriate in the current state
17236 PENDING_TRANSITION: 200, // caller tried to fire an event while an async transition was still pending
17237 INVALID_CALLBACK: 300 // caller provided callback function threw an exception
17238 },
17239
17240 WILDCARD: '*',
17241 ASYNC: 'async',
17242
17243 //---------------------------------------------------------------------------
17244
17245 create: function(cfg, target) {
17246
17247 var initial = (typeof cfg.initial == 'string') ? { state: cfg.initial } : cfg.initial; // allow for a simple string, or an object with { state: 'foo', event: 'setup', defer: true|false }
17248 var terminal = cfg.terminal || cfg['final'];
17249 var fsm = target || cfg.target || {};
17250 var events = cfg.events || [];
17251 var callbacks = cfg.callbacks || {};
17252 var map = {}; // track state transitions allowed for an event { event: { from: [ to ] } }
17253 var transitions = {}; // track events allowed from a state { state: [ event ] }
17254
17255 var add = function(e) {
17256 var from = Array.isArray(e.from) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]); // allow 'wildcard' transition if 'from' is not specified
17257 map[e.name] = map[e.name] || {};
17258 for (var n = 0 ; n < from.length ; n++) {
17259 transitions[from[n]] = transitions[from[n]] || [];
17260 transitions[from[n]].push(e.name);
17261
17262 map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
17263 }
17264 if (e.to)
17265 transitions[e.to] = transitions[e.to] || [];
17266 };
17267
17268 if (initial) {
17269 initial.event = initial.event || 'startup';
17270 add({ name: initial.event, from: 'none', to: initial.state });
17271 }
17272
17273 for(var n = 0 ; n < events.length ; n++)
17274 add(events[n]);
17275
17276 for(var name in map) {
17277 if (map.hasOwnProperty(name))
17278 fsm[name] = StateMachine.buildEvent(name, map[name]);
17279 }
17280
17281 for(var name in callbacks) {
17282 if (callbacks.hasOwnProperty(name))
17283 fsm[name] = callbacks[name];
17284 }
17285
17286 fsm.current = 'none';
17287 fsm.is = function(state) { return Array.isArray(state) ? (state.indexOf(this.current) >= 0) : (this.current === state); };
17288 fsm.can = function(event) { return !this.transition && (map[event] !== undefined) && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); };
17289 fsm.cannot = function(event) { return !this.can(event); };
17290 fsm.transitions = function() { return (transitions[this.current] || []).concat(transitions[StateMachine.WILDCARD] || []); };
17291 fsm.isFinished = function() { return this.is(terminal); };
17292 fsm.error = cfg.error || function(name, from, to, args, error, msg, e) { throw e || msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3 and #17)
17293 fsm.states = function() { return Object.keys(transitions).sort() };
17294
17295 if (initial && !initial.defer)
17296 fsm[initial.event]();
17297
17298 return fsm;
17299
17300 },
17301
17302 //===========================================================================
17303
17304 doCallback: function(fsm, func, name, from, to, args) {
17305 if (func) {
17306 try {
17307 return func.apply(fsm, [name, from, to].concat(args));
17308 }
17309 catch(e) {
17310 return fsm.error(name, from, to, args, StateMachine.Error.INVALID_CALLBACK, "an exception occurred in a caller-provided callback function", e);
17311 }
17312 }
17313 },
17314
17315 beforeAnyEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onbeforeevent'], name, from, to, args); },
17316 afterAnyEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onafterevent'] || fsm['onevent'], name, from, to, args); },
17317 leaveAnyState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onleavestate'], name, from, to, args); },
17318 enterAnyState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onenterstate'] || fsm['onstate'], name, from, to, args); },
17319 changeState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onchangestate'], name, from, to, args); },
17320
17321 beforeThisEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onbefore' + name], name, from, to, args); },
17322 afterThisEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onafter' + name] || fsm['on' + name], name, from, to, args); },
17323 leaveThisState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onleave' + from], name, from, to, args); },
17324 enterThisState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onenter' + to] || fsm['on' + to], name, from, to, args); },
17325
17326 beforeEvent: function(fsm, name, from, to, args) {
17327 if ((false === StateMachine.beforeThisEvent(fsm, name, from, to, args)) ||
17328 (false === StateMachine.beforeAnyEvent( fsm, name, from, to, args)))
17329 return false;
17330 },
17331
17332 afterEvent: function(fsm, name, from, to, args) {
17333 StateMachine.afterThisEvent(fsm, name, from, to, args);
17334 StateMachine.afterAnyEvent( fsm, name, from, to, args);
17335 },
17336
17337 leaveState: function(fsm, name, from, to, args) {
17338 var specific = StateMachine.leaveThisState(fsm, name, from, to, args),
17339 general = StateMachine.leaveAnyState( fsm, name, from, to, args);
17340 if ((false === specific) || (false === general))
17341 return false;
17342 else if ((StateMachine.ASYNC === specific) || (StateMachine.ASYNC === general))
17343 return StateMachine.ASYNC;
17344 },
17345
17346 enterState: function(fsm, name, from, to, args) {
17347 StateMachine.enterThisState(fsm, name, from, to, args);
17348 StateMachine.enterAnyState( fsm, name, from, to, args);
17349 },
17350
17351 //===========================================================================
17352
17353 buildEvent: function(name, map) {
17354 return function() {
17355
17356 var from = this.current;
17357 var to = map[from] || (map[StateMachine.WILDCARD] != StateMachine.WILDCARD ? map[StateMachine.WILDCARD] : from) || from;
17358 var args = Array.prototype.slice.call(arguments); // turn arguments into pure array
17359
17360 if (this.transition)
17361 return this.error(name, from, to, args, StateMachine.Error.PENDING_TRANSITION, "event " + name + " inappropriate because previous transition did not complete");
17362
17363 if (this.cannot(name))
17364 return this.error(name, from, to, args, StateMachine.Error.INVALID_TRANSITION, "event " + name + " inappropriate in current state " + this.current);
17365
17366 if (false === StateMachine.beforeEvent(this, name, from, to, args))
17367 return StateMachine.Result.CANCELLED;
17368
17369 if (from === to) {
17370 StateMachine.afterEvent(this, name, from, to, args);
17371 return StateMachine.Result.NOTRANSITION;
17372 }
17373
17374 // prepare a transition method for use EITHER lower down, or by caller if they want an async transition (indicated by an ASYNC return value from leaveState)
17375 var fsm = this;
17376 this.transition = function() {
17377 fsm.transition = null; // this method should only ever be called once
17378 fsm.current = to;
17379 StateMachine.enterState( fsm, name, from, to, args);
17380 StateMachine.changeState(fsm, name, from, to, args);
17381 StateMachine.afterEvent( fsm, name, from, to, args);
17382 return StateMachine.Result.SUCCEEDED;
17383 };
17384 this.transition.cancel = function() { // provide a way for caller to cancel async transition if desired (issue #22)
17385 fsm.transition = null;
17386 StateMachine.afterEvent(fsm, name, from, to, args);
17387 };
17388
17389 var leave = StateMachine.leaveState(this, name, from, to, args);
17390 if (false === leave) {
17391 this.transition = null;
17392 return StateMachine.Result.CANCELLED;
17393 }
17394 else if (StateMachine.ASYNC === leave) {
17395 return StateMachine.Result.PENDING;
17396 }
17397 else {
17398 if (this.transition) // need to check in case user manually called transition() but forgot to return StateMachine.ASYNC
17399 return this.transition();
17400 }
17401
17402 };
17403 }
17404
17405 }; // StateMachine
17406
17407 //===========================================================================
17408
17409 //======
17410 // NODE
17411 //======
17412 {
17413 if (module.exports) {
17414 exports = module.exports = StateMachine;
17415 }
17416 exports.StateMachine = StateMachine;
17417 }
17418
17419 }());
17420 });
17421 var stateMachine_1 = stateMachine.StateMachine;
17422
17423 /** Built-in value references. */
17424 var getPrototype = _overArg(Object.getPrototypeOf, Object);
17425
17426 var _getPrototype = getPrototype;
17427
17428 /** `Object#toString` result references. */
17429 var objectTag$1 = '[object Object]';
17430
17431 /** Used for built-in method references. */
17432 var funcProto = Function.prototype,
17433 objectProto$6 = Object.prototype;
17434
17435 /** Used to resolve the decompiled source of functions. */
17436 var funcToString = funcProto.toString;
17437
17438 /** Used to check objects for own properties. */
17439 var hasOwnProperty$5 = objectProto$6.hasOwnProperty;
17440
17441 /** Used to infer the `Object` constructor. */
17442 var objectCtorString = funcToString.call(Object);
17443
17444 /**
17445 * Checks if `value` is a plain object, that is, an object created by the
17446 * `Object` constructor or one with a `[[Prototype]]` of `null`.
17447 *
17448 * @static
17449 * @memberOf _
17450 * @since 0.8.0
17451 * @category Lang
17452 * @param {*} value The value to check.
17453 * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
17454 * @example
17455 *
17456 * function Foo() {
17457 * this.a = 1;
17458 * }
17459 *
17460 * _.isPlainObject(new Foo);
17461 * // => false
17462 *
17463 * _.isPlainObject([1, 2, 3]);
17464 * // => false
17465 *
17466 * _.isPlainObject({ 'x': 0, 'y': 0 });
17467 * // => true
17468 *
17469 * _.isPlainObject(Object.create(null));
17470 * // => true
17471 */
17472 function isPlainObject(value) {
17473 if (!isObjectLike_1(value) || _baseGetTag(value) != objectTag$1) {
17474 return false;
17475 }
17476 var proto = _getPrototype(value);
17477 if (proto === null) {
17478 return true;
17479 }
17480 var Ctor = hasOwnProperty$5.call(proto, 'constructor') && proto.constructor;
17481 return typeof Ctor == 'function' && Ctor instanceof Ctor &&
17482 funcToString.call(Ctor) == objectCtorString;
17483 }
17484
17485 var isPlainObject_1 = isPlainObject;
17486
17487 /* eslint-disable */
17488 var global$1 = typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : {};
17489
17490 var EXPIRED = Symbol('expired');
17491 var debug = browser('LC:Expirable');
17492
17493 var Expirable =
17494 /*#__PURE__*/
17495 function () {
17496 function Expirable(value, ttl) {
17497 this.originalValue = value;
17498
17499 if (typeof ttl === 'number') {
17500 this.expiredAt = Date.now() + ttl;
17501 }
17502 }
17503
17504 _createClass(Expirable, [{
17505 key: "value",
17506 get: function get() {
17507 var expired = this.expiredAt && this.expiredAt <= Date.now();
17508 if (expired) debug("expired: ".concat(this.originalValue));
17509 return expired ? EXPIRED : this.originalValue;
17510 }
17511 }]);
17512
17513 return Expirable;
17514 }();
17515 Expirable.EXPIRED = EXPIRED;
17516
17517 var debug$1 = browser('LC:Cache');
17518
17519 var Cache =
17520 /*#__PURE__*/
17521 function () {
17522 function Cache() {
17523 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'anonymous';
17524 this.name = name;
17525 this._map = {};
17526 }
17527
17528 var _proto = Cache.prototype;
17529
17530 _proto.get = function get(key) {
17531 var cache = this._map[key];
17532
17533 if (cache) {
17534 var value = cache.value;
17535
17536 if (value !== Expirable.EXPIRED) {
17537 debug$1('[%s] hit: %s', this.name, key);
17538 return value;
17539 }
17540
17541 delete this._map[key];
17542 }
17543
17544 debug$1("[".concat(this.name, "] missed: ").concat(key));
17545 return null;
17546 };
17547
17548 _proto.set = function set(key, value, ttl) {
17549 debug$1('[%s] set: %s %d', this.name, key, ttl);
17550 this._map[key] = new Expirable(value, ttl);
17551 };
17552
17553 return Cache;
17554 }();
17555
17556 var debug$2 = {
17557 enable: function enable() {
17558 var namespaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'LC*';
17559 return browser.enable(namespaces);
17560 },
17561 disable: browser.disable
17562 };
17563 var tryAll = function tryAll(promiseConstructors) {
17564 var promise = new Promise(promiseConstructors[0]);
17565
17566 if (promiseConstructors.length === 1) {
17567 return promise;
17568 }
17569
17570 return promise.catch(function () {
17571 return tryAll(promiseConstructors.slice(1));
17572 });
17573 }; // eslint-disable-next-line no-sequences
17574
17575 var tap = function tap(interceptor) {
17576 return function (value) {
17577 return interceptor(value), value;
17578 };
17579 };
17580 var finalize = function finalize(callback) {
17581 return [// eslint-disable-next-line no-sequences
17582 function (value) {
17583 return callback(), value;
17584 }, function (error) {
17585 callback();
17586 throw error;
17587 }];
17588 };
17589 /**
17590 * 将对象转换为 Date,支持 string、number、ProtoBuf Long 以及 LeanCloud 的 Date 类型,
17591 * 其他情况下(包括对象为 falsy)返回原值。
17592 * @private
17593 */
17594
17595 var decodeDate = function decodeDate(date) {
17596 if (!date) return date;
17597
17598 if (typeof date === 'string' || typeof date === 'number') {
17599 return new Date(date);
17600 }
17601
17602 if (date.__type === 'Date' && date.iso) {
17603 return new Date(date.iso);
17604 } // Long
17605
17606
17607 if (typeof date.toNumber === 'function') {
17608 return new Date(date.toNumber());
17609 }
17610
17611 return date;
17612 };
17613 /**
17614 * 获取 Date 的毫秒数,如果不是一个 Date 返回 undefined。
17615 * @private
17616 */
17617
17618 var getTime = function getTime(date) {
17619 return date && date.getTime ? date.getTime() : undefined;
17620 };
17621 /**
17622 * 解码对象中的 LeanCloud 数据结构。
17623 * 目前仅会处理 Date 类型。
17624 * @private
17625 */
17626
17627 var decode = function decode(value) {
17628 if (!value) return value;
17629
17630 if (value.__type === 'Date' && value.iso) {
17631 return new Date(value.iso);
17632 }
17633
17634 if (Array.isArray(value)) {
17635 return value.map(decode);
17636 }
17637
17638 if (isPlainObject_1(value)) {
17639 return Object.keys(value).reduce(function (result, key) {
17640 return _objectSpread({}, result, _defineProperty({}, key, decode(value[key])));
17641 }, {});
17642 }
17643
17644 return value;
17645 };
17646 /**
17647 * 将对象中的特殊类型编码为 LeanCloud 数据结构。
17648 * 目前仅会处理 Date 类型。
17649 * @private
17650 */
17651
17652 var encode = function encode(value) {
17653 if (value instanceof Date) return {
17654 __type: 'Date',
17655 iso: value.toJSON()
17656 };
17657
17658 if (Array.isArray(value)) {
17659 return value.map(encode);
17660 }
17661
17662 if (isPlainObject_1(value)) {
17663 return Object.keys(value).reduce(function (result, key) {
17664 return _objectSpread({}, result, _defineProperty({}, key, encode(value[key])));
17665 }, {});
17666 }
17667
17668 return value;
17669 };
17670 var keyRemap = function keyRemap(keymap, obj) {
17671 return Object.keys(obj).reduce(function (newObj, key) {
17672 var newKey = keymap[key] || key;
17673 return Object.assign(newObj, _defineProperty({}, newKey, obj[key]));
17674 }, {});
17675 };
17676 var isIE10 = global$1.navigator && global$1.navigator.userAgent && global$1.navigator.userAgent.indexOf('MSIE 10.') !== -1;
17677 /* eslint-disable no-proto */
17678
17679 var getStaticProperty = function getStaticProperty(klass, property) {
17680 return klass[property] || (klass.__proto__ ? getStaticProperty(klass.__proto__, property) : undefined);
17681 };
17682 /* eslint-enable no-proto */
17683
17684 var union = function union(a, b) {
17685 return Array.from(new Set([].concat(_toConsumableArray(a), _toConsumableArray(b))));
17686 };
17687 var difference = function difference(a, b) {
17688 return Array.from(function (bSet) {
17689 return new Set(a.filter(function (x) {
17690 return !bSet.has(x);
17691 }));
17692 }(new Set(b)));
17693 };
17694 var map = new WeakMap(); // protected property helper
17695
17696 var internal = function internal(object) {
17697 if (!map.has(object)) {
17698 map.set(object, {});
17699 }
17700
17701 return map.get(object);
17702 };
17703 var compact = function compact(obj, filter) {
17704 if (!isPlainObject_1(obj)) return obj;
17705 var object = Object.assign({}, obj); // eslint-disable-next-line no-restricted-syntax
17706
17707 for (var prop in object) {
17708 if ({}.hasOwnProperty.call(object, prop)) {
17709 var value = object[prop];
17710
17711 if (value === filter) {
17712 delete object[prop];
17713 } else {
17714 object[prop] = compact(value, filter);
17715 }
17716 }
17717 }
17718
17719 return object;
17720 }; // debug utility
17721
17722 var removeNull = function removeNull(obj) {
17723 return compact(obj, null);
17724 };
17725
17726 var trim = function trim(message) {
17727 return removeNull(JSON.parse(JSON.stringify(message)));
17728 };
17729 var ensureArray = function ensureArray(target) {
17730 if (Array.isArray(target)) {
17731 return target;
17732 }
17733
17734 if (target === undefined || target === null) {
17735 return [];
17736 }
17737
17738 return [target];
17739 };
17740 var setValue = function setValue(target, key, value) {
17741 // '.' is not allowed in Class keys, escaping is not in concern now.
17742 var segs = key.split('.');
17743 var lastSeg = segs.pop();
17744 var currentTarget = target;
17745 segs.forEach(function (seg) {
17746 if (currentTarget[seg] === undefined) currentTarget[seg] = {};
17747 currentTarget = currentTarget[seg];
17748 });
17749 currentTarget[lastSeg] = value;
17750 return target;
17751 };
17752 var isWeapp = // eslint-disable-next-line no-undef
17753 (typeof wx === "undefined" ? "undefined" : _typeof(wx)) === 'object' && typeof wx.connectSocket === 'function'; // throttle decorator
17754
17755 var throttle = function throttle(wait) {
17756 return function (target, property, descriptor) {
17757 var callback = descriptor.value; // very naive, internal use only
17758
17759 if (callback.length) {
17760 throw new Error('throttled function should not accept any arguments');
17761 }
17762
17763 return _objectSpread({}, descriptor, {
17764 value: function value() {
17765 var _this = this;
17766
17767 var _internal = internal(this),
17768 throttleMeta = _internal.throttleMeta;
17769
17770 if (!throttleMeta) {
17771 throttleMeta = {};
17772 internal(this).throttleMeta = throttleMeta;
17773 }
17774
17775 var _throttleMeta = throttleMeta,
17776 propertyMeta = _throttleMeta[property];
17777
17778 if (!propertyMeta) {
17779 propertyMeta = {};
17780 throttleMeta[property] = propertyMeta;
17781 }
17782
17783 var _propertyMeta = propertyMeta,
17784 _propertyMeta$previou = _propertyMeta.previouseTimestamp,
17785 previouseTimestamp = _propertyMeta$previou === void 0 ? 0 : _propertyMeta$previou,
17786 timeout = _propertyMeta.timeout;
17787 var now = Date.now();
17788 var remainingTime = wait - (now - previouseTimestamp);
17789
17790 if (remainingTime <= 0) {
17791 throttleMeta[property].previouseTimestamp = now;
17792 callback.apply(this);
17793 } else if (!timeout) {
17794 propertyMeta.timeout = setTimeout(function () {
17795 propertyMeta.previouseTimestamp = Date.now();
17796 delete propertyMeta.timeout;
17797 callback.apply(_this);
17798 }, remainingTime);
17799 }
17800 }
17801 });
17802 };
17803 };
17804 var isCNApp = function isCNApp(appId) {
17805 return appId.slice(-9) !== '-MdYXbMMI';
17806 };
17807
17808 var _class;
17809 var debug$3 = browser('LC:WebSocketPlus');
17810 var OPEN$1 = 'open';
17811 var DISCONNECT = 'disconnect';
17812 var RECONNECT = 'reconnect';
17813 var RETRY = 'retry';
17814 var SCHEDULE = 'schedule';
17815 var OFFLINE = 'offline';
17816 var ONLINE = 'online';
17817 var ERROR = 'error';
17818 var MESSAGE = 'message';
17819 var HEARTBEAT_TIME = 180000;
17820 var TIMEOUT_TIME = 380000;
17821
17822 var DEFAULT_RETRY_STRATEGY = function DEFAULT_RETRY_STRATEGY(attempt) {
17823 return Math.min(1000 * Math.pow(2, attempt), 300000);
17824 };
17825
17826 var requireConnected = function requireConnected(target, name, descriptor) {
17827 return Object.assign({}, descriptor, {
17828 value: function requireConnectedWrapper() {
17829 var _descriptor$value;
17830
17831 this.checkConnectionAvailability(name);
17832
17833 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
17834 args[_key] = arguments[_key];
17835 }
17836
17837 return (_descriptor$value = descriptor.value).call.apply(_descriptor$value, [this].concat(args));
17838 }
17839 });
17840 };
17841
17842 var WebSocketPlus = (_class =
17843 /*#__PURE__*/
17844 function (_EventEmitter) {
17845 _inheritsLoose(WebSocketPlus, _EventEmitter);
17846
17847 _createClass(WebSocketPlus, [{
17848 key: "urls",
17849 get: function get() {
17850 return this._urls;
17851 },
17852 set: function set(urls) {
17853 this._urls = ensureArray(urls);
17854 }
17855 }]);
17856
17857 function WebSocketPlus(getUrls, protocol) {
17858 var _this;
17859
17860 if (typeof websocket === 'undefined') {
17861 throw new Error('WebSocket is undefined. Polyfill is required in this runtime.');
17862 }
17863
17864 _this = _EventEmitter.call(this) || this;
17865
17866 _this.init();
17867
17868 _this._protocol = protocol;
17869 Promise.resolve(typeof getUrls === 'function' ? getUrls() : getUrls).then(ensureArray).then(function (urls) {
17870 _this._urls = urls;
17871 return _this._open();
17872 }).then(function () {
17873 _this.__postponeTimeoutTimer = _this._postponeTimeoutTimer.bind(_assertThisInitialized(_this));
17874
17875 if (global$1.addEventListener) {
17876 _this.__pause = function () {
17877 if (_this.can('pause')) _this.pause();
17878 };
17879
17880 _this.__resume = function () {
17881 if (_this.can('resume')) _this.resume();
17882 };
17883
17884 global$1.addEventListener('offline', _this.__pause);
17885 global$1.addEventListener('online', _this.__resume);
17886 }
17887
17888 _this.open();
17889 }).catch(_this.throw.bind(_assertThisInitialized(_this)));
17890 return _this;
17891 }
17892
17893 var _proto = WebSocketPlus.prototype;
17894
17895 _proto._open = function _open() {
17896 var _this2 = this;
17897
17898 return this._createWs(this._urls, this._protocol).then(function (ws) {
17899 var _this2$_urls = _toArray(_this2._urls),
17900 first = _this2$_urls[0],
17901 reset = _this2$_urls.slice(1);
17902
17903 _this2._urls = [].concat(_toConsumableArray(reset), [first]);
17904 return ws;
17905 });
17906 };
17907
17908 _proto._createWs = function _createWs(urls, protocol) {
17909 var _this3 = this;
17910
17911 return tryAll(urls.map(function (url) {
17912 return function (resolve, reject) {
17913 debug$3("connect [".concat(url, "] ").concat(protocol));
17914 var ws = protocol ? new websocket(url, protocol) : new websocket(url);
17915 ws.binaryType = _this3.binaryType || 'arraybuffer';
17916
17917 ws.onopen = function () {
17918 return resolve(ws);
17919 };
17920
17921 ws.onclose = function (error) {
17922 if (error instanceof Error) {
17923 return reject(error);
17924 } // in browser, error event is useless
17925
17926
17927 return reject(new Error("Failed to connect [".concat(url, "]")));
17928 };
17929
17930 ws.onerror = ws.onclose;
17931 };
17932 })).then(function (ws) {
17933 _this3._ws = ws;
17934 _this3._ws.onclose = _this3._handleClose.bind(_this3);
17935 _this3._ws.onmessage = _this3._handleMessage.bind(_this3);
17936 return ws;
17937 });
17938 };
17939
17940 _proto._destroyWs = function _destroyWs() {
17941 var ws = this._ws;
17942 if (!ws) return;
17943 ws.onopen = null;
17944 ws.onclose = null;
17945 ws.onerror = null;
17946 ws.onmessage = null;
17947 this._ws = null;
17948 ws.close();
17949 } // eslint-disable-next-line class-methods-use-this
17950 ;
17951
17952 _proto.onbeforeevent = function onbeforeevent(event, from, to) {
17953 for (var _len2 = arguments.length, payload = new Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) {
17954 payload[_key2 - 3] = arguments[_key2];
17955 }
17956
17957 debug$3("".concat(event, ": ").concat(from, " -> ").concat(to, " %o"), payload);
17958 };
17959
17960 _proto.onopen = function onopen() {
17961 this.emit(OPEN$1);
17962 };
17963
17964 _proto.onconnected = function onconnected() {
17965 this._startConnectionKeeper();
17966 };
17967
17968 _proto.onleaveconnected = function onleaveconnected(event, from, to) {
17969 this._stopConnectionKeeper();
17970
17971 this._destroyWs();
17972
17973 if (to === 'offline' || to === 'disconnected') {
17974 this.emit(DISCONNECT);
17975 }
17976 };
17977
17978 _proto.onpause = function onpause() {
17979 this.emit(OFFLINE);
17980 };
17981
17982 _proto.onbeforeresume = function onbeforeresume() {
17983 this.emit(ONLINE);
17984 };
17985
17986 _proto.onreconnect = function onreconnect() {
17987 this.emit(RECONNECT);
17988 };
17989
17990 _proto.ondisconnected = function ondisconnected(event, from, to) {
17991 var _this4 = this;
17992
17993 var attempt = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
17994 var delay = DEFAULT_RETRY_STRATEGY.call(null, attempt);
17995 debug$3("schedule attempt=".concat(attempt, " delay=").concat(delay));
17996 this.emit(SCHEDULE, attempt, delay);
17997
17998 if (this.__scheduledRetry) {
17999 clearTimeout(this.__scheduledRetry);
18000 }
18001
18002 this.__scheduledRetry = setTimeout(function () {
18003 if (_this4.is('disconnected')) {
18004 _this4.retry(attempt);
18005 }
18006 }, delay);
18007 };
18008
18009 _proto.onretry = function onretry(event, from, to) {
18010 var _this5 = this;
18011
18012 var attempt = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
18013 this.emit(RETRY, attempt);
18014
18015 this._open().then(function () {
18016 return _this5.can('reconnect') ? _this5.reconnect() : _this5._destroyWs();
18017 }, function () {
18018 return _this5.can('fail') && _this5.fail(attempt + 1);
18019 });
18020 };
18021
18022 _proto.onerror = function onerror(event, from, to, error) {
18023 this.emit(ERROR, error);
18024 };
18025
18026 _proto.onclose = function onclose() {
18027 if (global$1.removeEventListener) {
18028 if (this.__pause) global$1.removeEventListener('offline', this.__pause);
18029 if (this.__resume) global$1.removeEventListener('online', this.__resume);
18030 }
18031 };
18032
18033 _proto.checkConnectionAvailability = function checkConnectionAvailability() {
18034 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'API';
18035
18036 if (!this.is('connected')) {
18037 var currentState = this.current;
18038 console.warn("".concat(name, " should not be called when the connection is ").concat(currentState));
18039
18040 if (this.is('disconnected') || this.is('reconnecting')) {
18041 console.warn('disconnect and reconnect event should be handled to avoid such calls.');
18042 }
18043
18044 throw new Error('Connection unavailable');
18045 }
18046 } // jsdoc-ignore-start
18047 ;
18048
18049 _proto. // jsdoc-ignore-end
18050 _ping = function _ping() {
18051 debug$3('ping');
18052
18053 try {
18054 this.ping();
18055 } catch (error) {
18056 console.warn("websocket ping error: ".concat(error.message));
18057 }
18058 };
18059
18060 _proto.ping = function ping() {
18061 if (this._ws.ping) {
18062 this._ws.ping();
18063 } else {
18064 console.warn("The WebSocket implement does not support sending ping frame.\n Override ping method to use application defined ping/pong mechanism.");
18065 }
18066 };
18067
18068 _proto._postponeTimeoutTimer = function _postponeTimeoutTimer() {
18069 var _this6 = this;
18070
18071 debug$3('_postponeTimeoutTimer');
18072
18073 this._clearTimeoutTimers();
18074
18075 this._timeoutTimer = setTimeout(function () {
18076 debug$3('timeout');
18077
18078 _this6.disconnect();
18079 }, TIMEOUT_TIME);
18080 };
18081
18082 _proto._clearTimeoutTimers = function _clearTimeoutTimers() {
18083 if (this._timeoutTimer) {
18084 clearTimeout(this._timeoutTimer);
18085 }
18086 };
18087
18088 _proto._startConnectionKeeper = function _startConnectionKeeper() {
18089 debug$3('start connection keeper');
18090 this._heartbeatTimer = setInterval(this._ping.bind(this), HEARTBEAT_TIME);
18091 var addListener = this._ws.addListener || this._ws.addEventListener;
18092
18093 if (!addListener) {
18094 debug$3('connection keeper disabled due to the lack of #addEventListener.');
18095 return;
18096 }
18097
18098 addListener.call(this._ws, 'message', this.__postponeTimeoutTimer);
18099 addListener.call(this._ws, 'pong', this.__postponeTimeoutTimer);
18100
18101 this._postponeTimeoutTimer();
18102 };
18103
18104 _proto._stopConnectionKeeper = function _stopConnectionKeeper() {
18105 debug$3('stop connection keeper'); // websockets/ws#489
18106
18107 var removeListener = this._ws.removeListener || this._ws.removeEventListener;
18108
18109 if (removeListener) {
18110 removeListener.call(this._ws, 'message', this.__postponeTimeoutTimer);
18111 removeListener.call(this._ws, 'pong', this.__postponeTimeoutTimer);
18112
18113 this._clearTimeoutTimers();
18114 }
18115
18116 if (this._heartbeatTimer) {
18117 clearInterval(this._heartbeatTimer);
18118 }
18119 };
18120
18121 _proto._handleClose = function _handleClose(event) {
18122 debug$3("ws closed [".concat(event.code, "] ").concat(event.reason)); // socket closed manually, ignore close event.
18123
18124 if (this.isFinished()) return;
18125 this.handleClose(event);
18126 };
18127
18128 _proto.handleClose = function handleClose() {
18129 // reconnect
18130 this.disconnect();
18131 } // jsdoc-ignore-start
18132 ;
18133
18134 _proto. // jsdoc-ignore-end
18135 send = function send(data) {
18136 debug$3('send', data);
18137
18138 this._ws.send(data);
18139 };
18140
18141 _proto._handleMessage = function _handleMessage(event) {
18142 debug$3('message', event.data);
18143 this.handleMessage(event.data);
18144 };
18145
18146 _proto.handleMessage = function handleMessage(message) {
18147 this.emit(MESSAGE, message);
18148 };
18149
18150 return WebSocketPlus;
18151 }(eventemitter3), (_applyDecoratedDescriptor(_class.prototype, "_ping", [requireConnected], Object.getOwnPropertyDescriptor(_class.prototype, "_ping"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "send", [requireConnected], Object.getOwnPropertyDescriptor(_class.prototype, "send"), _class.prototype)), _class);
18152 stateMachine.create({
18153 target: WebSocketPlus.prototype,
18154 initial: {
18155 state: 'initialized',
18156 event: 'init',
18157 defer: true
18158 },
18159 terminal: 'closed',
18160 events: [{
18161 name: 'open',
18162 from: 'initialized',
18163 to: 'connected'
18164 }, {
18165 name: 'disconnect',
18166 from: 'connected',
18167 to: 'disconnected'
18168 }, {
18169 name: 'retry',
18170 from: 'disconnected',
18171 to: 'reconnecting'
18172 }, {
18173 name: 'fail',
18174 from: 'reconnecting',
18175 to: 'disconnected'
18176 }, {
18177 name: 'reconnect',
18178 from: 'reconnecting',
18179 to: 'connected'
18180 }, {
18181 name: 'pause',
18182 from: ['connected', 'disconnected', 'reconnecting'],
18183 to: 'offline'
18184 }, {}, {
18185 name: 'resume',
18186 from: 'offline',
18187 to: 'disconnected'
18188 }, {
18189 name: 'close',
18190 from: ['connected', 'disconnected', 'reconnecting', 'offline'],
18191 to: 'closed'
18192 }, {
18193 name: 'throw',
18194 from: '*',
18195 to: 'error'
18196 }]
18197 });
18198
18199 var error = Object.freeze({
18200 1000: {
18201 name: 'CLOSE_NORMAL'
18202 },
18203 1006: {
18204 name: 'CLOSE_ABNORMAL'
18205 },
18206 4100: {
18207 name: 'APP_NOT_AVAILABLE',
18208 message: 'App not exists or realtime message service is disabled.'
18209 },
18210 4102: {
18211 name: 'SIGNATURE_FAILED',
18212 message: 'Login signature mismatch.'
18213 },
18214 4103: {
18215 name: 'INVALID_LOGIN',
18216 message: 'Malformed clientId.'
18217 },
18218 4105: {
18219 name: 'SESSION_REQUIRED',
18220 message: 'Message sent before session opened.'
18221 },
18222 4107: {
18223 name: 'READ_TIMEOUT'
18224 },
18225 4108: {
18226 name: 'LOGIN_TIMEOUT'
18227 },
18228 4109: {
18229 name: 'FRAME_TOO_LONG'
18230 },
18231 4110: {
18232 name: 'INVALID_ORIGIN',
18233 message: 'Access denied by domain whitelist.'
18234 },
18235 4111: {
18236 name: 'SESSION_CONFLICT'
18237 },
18238 4112: {
18239 name: 'SESSION_TOKEN_EXPIRED'
18240 },
18241 4113: {
18242 name: 'APP_QUOTA_EXCEEDED',
18243 message: 'The daily active users limit exceeded.'
18244 },
18245 4116: {
18246 name: 'MESSAGE_SENT_QUOTA_EXCEEDED',
18247 message: 'Command sent too fast.'
18248 },
18249 4200: {
18250 name: 'INTERNAL_ERROR',
18251 message: 'Internal error, please contact LeanCloud for support.'
18252 },
18253 4301: {
18254 name: 'CONVERSATION_API_FAILED',
18255 message: 'Upstream Conversatoin API failed, see error.detail for details.'
18256 },
18257 4302: {
18258 name: 'CONVERSATION_SIGNATURE_FAILED',
18259 message: 'Conversation action signature mismatch.'
18260 },
18261 4303: {
18262 name: 'CONVERSATION_NOT_FOUND'
18263 },
18264 4304: {
18265 name: 'CONVERSATION_FULL'
18266 },
18267 4305: {
18268 name: 'CONVERSATION_REJECTED_BY_APP',
18269 message: 'Conversation action rejected by hook.'
18270 },
18271 4306: {
18272 name: 'CONVERSATION_UPDATE_FAILED'
18273 },
18274 4307: {
18275 name: 'CONVERSATION_READ_ONLY'
18276 },
18277 4308: {
18278 name: 'CONVERSATION_NOT_ALLOWED'
18279 },
18280 4309: {
18281 name: 'CONVERSATION_UPDATE_REJECTED',
18282 message: 'Conversation update rejected because the client is not a member.'
18283 },
18284 4310: {
18285 name: 'CONVERSATION_QUERY_FAILED',
18286 message: 'Conversation query failed because it is too expansive.'
18287 },
18288 4311: {
18289 name: 'CONVERSATION_LOG_FAILED'
18290 },
18291 4312: {
18292 name: 'CONVERSATION_LOG_REJECTED',
18293 message: 'Message query rejected because the client is not a member of the conversation.'
18294 },
18295 4313: {
18296 name: 'SYSTEM_CONVERSATION_REQUIRED'
18297 },
18298 4314: {
18299 name: 'NORMAL_CONVERSATION_REQUIRED'
18300 },
18301 4315: {
18302 name: 'CONVERSATION_BLACKLISTED',
18303 message: 'Blacklisted in the conversation.'
18304 },
18305 4316: {
18306 name: 'TRANSIENT_CONVERSATION_REQUIRED'
18307 },
18308 4317: {
18309 name: 'CONVERSATION_MEMBERSHIP_REQUIRED'
18310 },
18311 4318: {
18312 name: 'CONVERSATION_API_QUOTA_EXCEEDED',
18313 message: 'LeanCloud API quota exceeded. You may upgrade your plan.'
18314 },
18315 4323: {
18316 name: 'TEMPORARY_CONVERSATION_EXPIRED',
18317 message: 'Temporary conversation expired or does not exist.'
18318 },
18319 4401: {
18320 name: 'INVALID_MESSAGING_TARGET',
18321 message: 'Conversation does not exist or client is not a member.'
18322 },
18323 4402: {
18324 name: 'MESSAGE_REJECTED_BY_APP',
18325 message: 'Message rejected by hook.'
18326 },
18327 4403: {
18328 name: 'MESSAGE_OWNERSHIP_REQUIRED'
18329 },
18330 4404: {
18331 name: 'MESSAGE_NOT_FOUND'
18332 },
18333 4405: {
18334 name: 'MESSAGE_UPDATE_REJECTED_BY_APP',
18335 message: 'Message update rejected by hook.'
18336 },
18337 4406: {
18338 name: 'MESSAGE_EDIT_DISABLED'
18339 },
18340 4407: {
18341 name: 'MESSAGE_RECALL_DISABLED'
18342 },
18343 5130: {
18344 name: 'OWNER_PROMOTION_NOT_ALLOWED',
18345 message: "Updating a member's role to owner is not allowed."
18346 }
18347 });
18348 var ErrorCode = Object.freeze(Object.keys(error).reduce(function (result, code) {
18349 return Object.assign(result, _defineProperty({}, error[code].name, Number(code)));
18350 }, {}));
18351 var createError = function createError(_ref) {
18352 var code = _ref.code,
18353 reason = _ref.reason,
18354 appCode = _ref.appCode,
18355 detail = _ref.detail,
18356 errorMessage = _ref.error;
18357 var message = reason || detail || errorMessage;
18358 var name = reason;
18359
18360 if (!message && error[code]) {
18361 name = error[code].name;
18362 message = error[code].message || name;
18363 }
18364
18365 if (!message) {
18366 message = "Unknow Error: ".concat(code);
18367 }
18368
18369 var err = new Error(message);
18370 return Object.assign(err, {
18371 code: code,
18372 appCode: appCode,
18373 detail: detail,
18374 name: name
18375 });
18376 };
18377
18378 var debug$4 = browser('LC:Connection');
18379 var COMMAND_TIMEOUT = 20000;
18380 var EXPIRE = Symbol('expire');
18381
18382 var Connection =
18383 /*#__PURE__*/
18384 function (_WebSocketPlus) {
18385 _inheritsLoose(Connection, _WebSocketPlus);
18386
18387 function Connection(getUrl, _ref) {
18388 var _this;
18389
18390 var format = _ref.format,
18391 version = _ref.version;
18392 debug$4('initializing Connection');
18393 var protocolString = "lc.".concat(format, ".").concat(version);
18394
18395 if (!isWeapp) {
18396 _this = _WebSocketPlus.call(this, getUrl, protocolString) || this;
18397 } else {
18398 _this = _WebSocketPlus.call(this, getUrl().then(function (urls) {
18399 return urls.map(function (url) {
18400 return "".concat(url).concat(url.indexOf('?') === -1 ? '?' : '&', "subprotocol=").concat(encodeURIComponent(protocolString));
18401 });
18402 })) || this;
18403 }
18404
18405 _this._protocalFormat = format;
18406 _this._commands = {};
18407 _this._serialId = 0;
18408 return _assertThisInitialized(_this);
18409 }
18410
18411 var _proto = Connection.prototype;
18412
18413 _proto.send =
18414 /*#__PURE__*/
18415 function () {
18416 var _send = _asyncToGenerator(
18417 /*#__PURE__*/
18418 regenerator.mark(function _callee(command) {
18419 var _this2 = this;
18420
18421 var waitingForRespond,
18422 serialId,
18423 message,
18424 _args = arguments;
18425 return regenerator.wrap(function _callee$(_context) {
18426 while (1) {
18427 switch (_context.prev = _context.next) {
18428 case 0:
18429 waitingForRespond = _args.length > 1 && _args[1] !== undefined ? _args[1] : true;
18430
18431 if (waitingForRespond) {
18432 this._serialId += 1;
18433 serialId = this._serialId;
18434 command.i = serialId; // eslint-disable-line no-param-reassign
18435 }
18436
18437 if (debug$4.enabled) debug$4('↑ %O sent', trim(command));
18438
18439 if (this._protocalFormat === 'proto2base64') {
18440 message = command.toBase64();
18441 } else if (command.toArrayBuffer) {
18442 message = command.toArrayBuffer();
18443 }
18444
18445 if (message) {
18446 _context.next = 6;
18447 break;
18448 }
18449
18450 throw new TypeError("".concat(command, " is not a GenericCommand"));
18451
18452 case 6:
18453 _WebSocketPlus.prototype.send.call(this, message);
18454
18455 if (waitingForRespond) {
18456 _context.next = 9;
18457 break;
18458 }
18459
18460 return _context.abrupt("return", undefined);
18461
18462 case 9:
18463 return _context.abrupt("return", new Promise(function (resolve, reject) {
18464 _this2._commands[serialId] = {
18465 resolve: resolve,
18466 reject: reject,
18467 timeout: setTimeout(function () {
18468 if (_this2._commands[serialId]) {
18469 if (debug$4.enabled) debug$4('✗ %O timeout', trim(command));
18470 reject(createError({
18471 error: "Command Timeout [cmd:".concat(command.cmd, " op:").concat(command.op, "]"),
18472 name: 'COMMAND_TIMEOUT'
18473 }));
18474 delete _this2._commands[serialId];
18475 }
18476 }, COMMAND_TIMEOUT)
18477 };
18478 }));
18479
18480 case 10:
18481 case "end":
18482 return _context.stop();
18483 }
18484 }
18485 }, _callee, this);
18486 }));
18487
18488 function send(_x) {
18489 return _send.apply(this, arguments);
18490 }
18491
18492 return send;
18493 }();
18494
18495 _proto.handleMessage = function handleMessage(msg) {
18496 var message;
18497
18498 try {
18499 message = GenericCommand.decode(msg);
18500 if (debug$4.enabled) debug$4('↓ %O received', trim(message));
18501 } catch (e) {
18502 console.warn('Decode message failed:', e.message, msg);
18503 return;
18504 }
18505
18506 var serialId = message.i;
18507
18508 if (serialId) {
18509 if (this._commands[serialId]) {
18510 clearTimeout(this._commands[serialId].timeout);
18511
18512 if (message.cmd === CommandType.error) {
18513 this._commands[serialId].reject(createError(message.errorMessage));
18514 } else {
18515 this._commands[serialId].resolve(message);
18516 }
18517
18518 delete this._commands[serialId];
18519 } else {
18520 console.warn("Unexpected command received with serialId [".concat(serialId, "],\n which have timed out or never been requested."));
18521 }
18522 } else {
18523 switch (message.cmd) {
18524 case CommandType.error:
18525 {
18526 this.emit(ERROR, createError(message.errorMessage));
18527 return;
18528 }
18529
18530 case CommandType.goaway:
18531 {
18532 this.emit(EXPIRE);
18533 return;
18534 }
18535
18536 default:
18537 {
18538 this.emit(MESSAGE, message);
18539 }
18540 }
18541 }
18542 };
18543
18544 _proto.ping = function ping() {
18545 return this.send(new GenericCommand({
18546 cmd: CommandType.echo
18547 })).catch(function (error) {
18548 return debug$4('ping failed:', error);
18549 });
18550 };
18551
18552 return Connection;
18553 }(WebSocketPlus);
18554
18555 var checkType = function checkType(middleware) {
18556 return function (param) {
18557 var constructor = param.constructor;
18558 return Promise.resolve(param).then(middleware).then(tap(function (result) {
18559 if (result === undefined || result === null) {
18560 // eslint-disable-next-line max-len
18561 return console.warn("Middleware[".concat(middleware._pluginName || 'anonymous plugin', ":").concat(middleware.name || 'anonymous middleware', "] param/return types not match. It returns ").concat(result, " while a ").concat(param.constructor.name, " expected."));
18562 }
18563
18564 if (!(result instanceof constructor)) {
18565 // eslint-disable-next-line max-len
18566 return console.warn("Middleware[".concat(middleware._pluginName || 'anonymous plugin', ":").concat(middleware.name || 'anonymous middleware', "] param/return types not match. It returns a ").concat(result.constructor.name, " while a ").concat(param.constructor.name, " expected."));
18567 }
18568
18569 return 0;
18570 }));
18571 };
18572 };
18573
18574 var applyDecorators = function applyDecorators(decorators, target) {
18575 if (decorators) {
18576 decorators.forEach(function (decorator) {
18577 try {
18578 decorator(target);
18579 } catch (error) {
18580 if (decorator._pluginName) {
18581 error.message += "[".concat(decorator._pluginName, "]");
18582 }
18583
18584 throw error;
18585 }
18586 });
18587 }
18588 };
18589 var applyMiddlewares = function applyMiddlewares(middlewares) {
18590 return function (target) {
18591 return ensureArray(middlewares).reduce(function (previousPromise, middleware) {
18592 return previousPromise.then(checkType(middleware)).catch(function (error) {
18593 if (middleware._pluginName) {
18594 // eslint-disable-next-line no-param-reassign
18595 error.message += "[".concat(middleware._pluginName, "]");
18596 }
18597
18598 throw error;
18599 });
18600 }, Promise.resolve(target));
18601 };
18602 };
18603 var applyDispatcher = function applyDispatcher(dispatchers, payload) {
18604 return ensureArray(dispatchers).reduce(function (resultPromise, dispatcher) {
18605 return resultPromise.then(function (shouldDispatch) {
18606 return shouldDispatch === false ? false : dispatcher.apply(void 0, _toConsumableArray(payload));
18607 }).catch(function (error) {
18608 if (dispatcher._pluginName) {
18609 // eslint-disable-next-line no-param-reassign
18610 error.message += "[".concat(dispatcher._pluginName, "]");
18611 }
18612
18613 throw error;
18614 });
18615 }, Promise.resolve(true));
18616 };
18617
18618 var version = "5.0.0-beta.0";
18619
18620 var debug$5 = browser('LC:Realtime');
18621 var debugRequest = browser('LC:request');
18622 var routerCache = new Cache('push-router');
18623
18624 var Realtime =
18625 /*#__PURE__*/
18626 function (_EventEmitter) {
18627 _inheritsLoose(Realtime, _EventEmitter);
18628
18629 /**
18630 * @extends EventEmitter
18631 * @param {Object} options
18632 * @param {String} options.appId
18633 * @param {String} options.appKey (since 4.0.0)
18634 * @param {String|Object} [options.server] 指定服务器域名,中国节点应用此参数必填(since 4.0.0)
18635 * @param {Boolean} [options.pushOfflineMessages=false] 启用推送离线消息模式(默认为发送未读消息通知模式)
18636 * @param {Boolean} [options.noBinary=false] 设置 WebSocket 使用字符串格式收发消息(默认为二进制格式)。
18637 * 适用于 WebSocket 实现不支持二进制数据格式的情况
18638 * @param {Boolean} [options.ssl=true] 使用 wss 进行连接
18639 * @param {String|String[]} [options.RTMServers] 指定私有部署的 RTM 服务器地址(since 4.0.0)
18640 * @param {Plugin[]} [options.plugins] 加载插件(since 3.1.0)
18641 */
18642 function Realtime(_ref) {
18643 var _this2;
18644
18645 var plugins = _ref.plugins,
18646 options = _objectWithoutProperties(_ref, ["plugins"]);
18647
18648 debug$5('initializing Realtime %s %O', version, options);
18649 _this2 = _EventEmitter.call(this) || this;
18650
18651 if (typeof options.appId !== 'string') {
18652 throw new TypeError("appId [".concat(options.appId, "] is not a string"));
18653 }
18654
18655 if (typeof options.appKey !== 'string') {
18656 throw new TypeError("appKey [".concat(options.appKey, "] is not a string"));
18657 }
18658
18659 if (isCNApp(options.appId)) {
18660 if (!options.server) {
18661 throw new TypeError("server option is required for apps from CN region");
18662 }
18663 }
18664
18665 _this2._options = Object.assign({
18666 appId: undefined,
18667 appKey: undefined,
18668 pushOfflineMessages: false,
18669 noBinary: false,
18670 ssl: true,
18671 RTMServerName: process.env.RTM_SERVER_NAME // undocumented on purpose, internal use only
18672
18673 }, options);
18674 _this2._cache = new Cache('endpoints');
18675
18676 var _this = internal(_assertThisInitialized(_this2));
18677
18678 _this.clients = new Set();
18679 _this.pendingClients = new Set();
18680 var mergedPlugins = [].concat(_toConsumableArray(ensureArray(Realtime.__preRegisteredPlugins)), _toConsumableArray(ensureArray(plugins)));
18681 debug$5('Using plugins %o', mergedPlugins.map(function (plugin) {
18682 return plugin.name;
18683 }));
18684 _this2._plugins = mergedPlugins.reduce(function (result, plugin) {
18685 // eslint-disable-next-line no-restricted-syntax
18686 for (var hook in plugin) {
18687 if ({}.hasOwnProperty.call(plugin, hook) && hook !== 'name') {
18688 if (plugin.name) {
18689 ensureArray(plugin[hook]).forEach(function (value) {
18690 // eslint-disable-next-line no-param-reassign
18691 value._pluginName = plugin.name;
18692 });
18693 } // eslint-disable-next-line no-param-reassign
18694
18695
18696 result[hook] = ensureArray(result[hook]).concat(plugin[hook]);
18697 }
18698 }
18699
18700 return result;
18701 }, {}); // onRealtimeCreate hook
18702
18703 applyDecorators(_this2._plugins.onRealtimeCreate, _assertThisInitialized(_this2));
18704 return _this2;
18705 }
18706
18707 var _proto = Realtime.prototype;
18708
18709 _proto._request =
18710 /*#__PURE__*/
18711 function () {
18712 var _request2 = _asyncToGenerator(
18713 /*#__PURE__*/
18714 regenerator.mark(function _callee(_ref2) {
18715 var method, _ref2$version, version, path, query, headers, _ref2$data, data, _this$_options, appId, server, _ref3, api, url;
18716
18717 return regenerator.wrap(function _callee$(_context) {
18718 while (1) {
18719 switch (_context.prev = _context.next) {
18720 case 0:
18721 method = _ref2.method, _ref2$version = _ref2.version, version = _ref2$version === void 0 ? '1.1' : _ref2$version, path = _ref2.path, query = _ref2.query, headers = _ref2.headers, _ref2$data = _ref2.data, data = _ref2$data === void 0 ? {} : _ref2$data;
18722 _this$_options = this._options, appId = _this$_options.appId, server = _this$_options.server;
18723 _context.next = 4;
18724 return this.constructor._getServerUrls({
18725 appId: appId,
18726 server: server
18727 });
18728
18729 case 4:
18730 _ref3 = _context.sent;
18731 api = _ref3.api;
18732 url = "".concat(api, "/").concat(version).concat(path);
18733 debugRequest('Req: %O %O %O', method, url, {
18734 query: query,
18735 headers: headers,
18736 data: data
18737 });
18738 return _context.abrupt("return", client(method, url).set(_objectSpread({
18739 'X-LC-Id': this._options.appId,
18740 'X-LC-Key': this._options.appKey
18741 }, headers)).query(query).send(data).then(function (response) {
18742 debugRequest('Res: %O %O %O', url, response.status, response.body);
18743 return response.body;
18744 }, function (error) {
18745 debugRequest('Error: %O %O %O', url, error.response.status, error.response.body);
18746
18747 if (error.response && error.response.body && error.response.body.code) {
18748 throw createError(error.response.body);
18749 }
18750
18751 throw error;
18752 }));
18753
18754 case 9:
18755 case "end":
18756 return _context.stop();
18757 }
18758 }
18759 }, _callee, this);
18760 }));
18761
18762 function _request(_x) {
18763 return _request2.apply(this, arguments);
18764 }
18765
18766 return _request;
18767 }();
18768
18769 _proto._open = function _open() {
18770 var _this3 = this;
18771
18772 if (this._openPromise) return this._openPromise;
18773 var format = 'protobuf2';
18774
18775 if (this._options.noBinary) {
18776 // 不发送 binary data,fallback to base64 string
18777 format = 'proto2base64';
18778 }
18779
18780 var version = 3;
18781
18782 if (this._options.pushOfflineMessages) {
18783 // 不推送离线消息,而是发送对话的未读通知
18784 version = 1;
18785 }
18786
18787 var protocol = {
18788 format: format,
18789 version: version
18790 };
18791 this._openPromise = new Promise(function (resolve, reject) {
18792 debug$5('No connection established, create a new one.');
18793 var connection = new Connection(function () {
18794 return _this3._getRTMServers(_this3._options);
18795 }, protocol);
18796 connection.on(OPEN$1, function () {
18797 return resolve(connection);
18798 }).on(ERROR, reject).on(EXPIRE,
18799 /*#__PURE__*/
18800 _asyncToGenerator(
18801 /*#__PURE__*/
18802 regenerator.mark(function _callee2() {
18803 return regenerator.wrap(function _callee2$(_context2) {
18804 while (1) {
18805 switch (_context2.prev = _context2.next) {
18806 case 0:
18807 debug$5('Connection expired. Refresh endpoints.');
18808
18809 _this3._cache.set('endpoints', null, 0);
18810
18811 _context2.next = 4;
18812 return _this3._getRTMServers(_this3._options);
18813
18814 case 4:
18815 connection.urls = _context2.sent;
18816 connection.disconnect();
18817
18818 case 6:
18819 case "end":
18820 return _context2.stop();
18821 }
18822 }
18823 }, _callee2, this);
18824 }))).on(MESSAGE, _this3._dispatchCommand.bind(_this3));
18825 /**
18826 * 连接断开。
18827 * 连接断开可能是因为 SDK 进入了离线状态(see {@link Realtime#event:OFFLINE}),或长时间没有收到服务器心跳。
18828 * 连接断开后所有的网络操作都会失败,请在连接断开后禁用相关的 UI 元素。
18829 * @event Realtime#DISCONNECT
18830 */
18831
18832 /**
18833 * 计划在一段时间后尝试重新连接
18834 * @event Realtime#SCHEDULE
18835 * @param {Number} attempt 尝试重连的次数
18836 * @param {Number} delay 延迟的毫秒数
18837 */
18838
18839 /**
18840 * 正在尝试重新连接
18841 * @event Realtime#RETRY
18842 * @param {Number} attempt 尝试重连的次数
18843 */
18844
18845 /**
18846 * 连接恢复正常。
18847 * 请重新启用在 {@link Realtime#event:DISCONNECT} 事件中禁用的相关 UI 元素
18848 * @event Realtime#RECONNECT
18849 */
18850
18851 /**
18852 * 客户端连接断开
18853 * @event IMClient#DISCONNECT
18854 * @see Realtime#event:DISCONNECT
18855 * @since 3.2.0
18856 */
18857
18858 /**
18859 * 计划在一段时间后尝试重新连接
18860 * @event IMClient#SCHEDULE
18861 * @param {Number} attempt 尝试重连的次数
18862 * @param {Number} delay 延迟的毫秒数
18863 * @since 3.2.0
18864 */
18865
18866 /**
18867 * 正在尝试重新连接
18868 * @event IMClient#RETRY
18869 * @param {Number} attempt 尝试重连的次数
18870 * @since 3.2.0
18871 */
18872
18873 /**
18874 * 客户端进入离线状态。
18875 * 这通常意味着网络已断开,或者 {@link Realtime#pause} 被调用
18876 * @event Realtime#OFFLINE
18877 * @since 3.4.0
18878 */
18879
18880 /**
18881 * 客户端恢复在线状态
18882 * 这通常意味着网络已恢复,或者 {@link Realtime#resume} 被调用
18883 * @event Realtime#ONLINE
18884 * @since 3.4.0
18885 */
18886
18887 /**
18888 * 进入离线状态。
18889 * 这通常意味着网络已断开,或者 {@link Realtime#pause} 被调用
18890 * @event IMClient#OFFLINE
18891 * @since 3.4.0
18892 */
18893
18894 /**
18895 * 恢复在线状态
18896 * 这通常意味着网络已恢复,或者 {@link Realtime#resume} 被调用
18897 * @event IMClient#ONLINE
18898 * @since 3.4.0
18899 */
18900 // event proxy
18901
18902 [DISCONNECT, RECONNECT, RETRY, SCHEDULE, OFFLINE, ONLINE].forEach(function (event) {
18903 return connection.on(event, function () {
18904 for (var _len = arguments.length, payload = new Array(_len), _key = 0; _key < _len; _key++) {
18905 payload[_key] = arguments[_key];
18906 }
18907
18908 debug$5("".concat(event, " event emitted. %o"), payload);
18909
18910 _this3.emit.apply(_this3, [event].concat(payload));
18911
18912 if (event !== RECONNECT) {
18913 internal(_this3).clients.forEach(function (client) {
18914 client.emit.apply(client, [event].concat(payload));
18915 });
18916 }
18917 });
18918 }); // override handleClose
18919
18920 connection.handleClose = function handleClose(event) {
18921 var isFatal = [ErrorCode.APP_NOT_AVAILABLE, ErrorCode.INVALID_LOGIN, ErrorCode.INVALID_ORIGIN].some(function (errorCode) {
18922 return errorCode === event.code;
18923 });
18924
18925 if (isFatal) {
18926 // in these cases, SDK should throw.
18927 this.throw(createError(event));
18928 } else {
18929 // reconnect
18930 this.disconnect();
18931 }
18932 };
18933
18934 internal(_this3).connection = connection;
18935 });
18936 return this._openPromise;
18937 };
18938
18939 _proto._getRTMServers =
18940 /*#__PURE__*/
18941 function () {
18942 var _getRTMServers2 = _asyncToGenerator(
18943 /*#__PURE__*/
18944 regenerator.mark(function _callee3(options) {
18945 var info, cachedEndPoints;
18946 return regenerator.wrap(function _callee3$(_context3) {
18947 while (1) {
18948 switch (_context3.prev = _context3.next) {
18949 case 0:
18950 if (!options.RTMServers) {
18951 _context3.next = 2;
18952 break;
18953 }
18954
18955 return _context3.abrupt("return", shuffle_1(ensureArray(options.RTMServers)));
18956
18957 case 2:
18958 cachedEndPoints = this._cache.get('endpoints');
18959
18960 if (!cachedEndPoints) {
18961 _context3.next = 9;
18962 break;
18963 }
18964
18965 _context3.next = 6;
18966 return cachedEndPoints;
18967
18968 case 6:
18969 info = _context3.sent;
18970 _context3.next = 13;
18971 break;
18972
18973 case 9:
18974 _context3.next = 11;
18975 return this.constructor._fetchRTMServers(options);
18976
18977 case 11:
18978 info = _context3.sent;
18979
18980 this._cache.set('endpoints', info, info.ttl * 1000);
18981
18982 case 13:
18983 debug$5('endpoint info: %O', info);
18984 return _context3.abrupt("return", [info.server, info.secondary]);
18985
18986 case 15:
18987 case "end":
18988 return _context3.stop();
18989 }
18990 }
18991 }, _callee3, this);
18992 }));
18993
18994 function _getRTMServers(_x2) {
18995 return _getRTMServers2.apply(this, arguments);
18996 }
18997
18998 return _getRTMServers;
18999 }();
19000
19001 Realtime._getServerUrls =
19002 /*#__PURE__*/
19003 function () {
19004 var _getServerUrls2 = _asyncToGenerator(
19005 /*#__PURE__*/
19006 regenerator.mark(function _callee4(_ref5) {
19007 var appId, server, cachedRouter, defaultProtocol;
19008 return regenerator.wrap(function _callee4$(_context4) {
19009 while (1) {
19010 switch (_context4.prev = _context4.next) {
19011 case 0:
19012 appId = _ref5.appId, server = _ref5.server;
19013 debug$5('fetch server urls');
19014
19015 if (!server) {
19016 _context4.next = 6;
19017 break;
19018 }
19019
19020 if (!(typeof server !== 'string')) {
19021 _context4.next = 5;
19022 break;
19023 }
19024
19025 return _context4.abrupt("return", server);
19026
19027 case 5:
19028 return _context4.abrupt("return", {
19029 RTMRouter: server,
19030 api: server
19031 });
19032
19033 case 6:
19034 cachedRouter = routerCache.get(appId);
19035
19036 if (!cachedRouter) {
19037 _context4.next = 9;
19038 break;
19039 }
19040
19041 return _context4.abrupt("return", cachedRouter);
19042
19043 case 9:
19044 defaultProtocol = 'https://';
19045 return _context4.abrupt("return", client.get('https://app-router.com/2/route').query({
19046 appId: appId
19047 }).timeout(20000).then(function (res) {
19048 return res.body;
19049 }).then(tap(debug$5)).then(function (_ref6) {
19050 var RTMRouterServer = _ref6.rtm_router_server,
19051 APIServer = _ref6.api_server,
19052 _ref6$ttl = _ref6.ttl,
19053 ttl = _ref6$ttl === void 0 ? 3600 : _ref6$ttl;
19054
19055 if (!RTMRouterServer) {
19056 throw new Error('rtm router not exists');
19057 }
19058
19059 var serverUrls = {
19060 RTMRouter: "".concat(defaultProtocol).concat(RTMRouterServer),
19061 api: "".concat(defaultProtocol).concat(APIServer)
19062 };
19063 routerCache.set(appId, serverUrls, ttl * 1000);
19064 return serverUrls;
19065 }).catch(function () {
19066 var id = appId.slice(0, 8).toLowerCase();
19067 var domain = 'lncldglobal.com';
19068 return {
19069 RTMRouter: "".concat(defaultProtocol).concat(id, ".rtm.").concat(domain),
19070 api: "".concat(defaultProtocol).concat(id, ".api.").concat(domain)
19071 };
19072 }));
19073
19074 case 11:
19075 case "end":
19076 return _context4.stop();
19077 }
19078 }
19079 }, _callee4, this);
19080 }));
19081
19082 function _getServerUrls(_x3) {
19083 return _getServerUrls2.apply(this, arguments);
19084 }
19085
19086 return _getServerUrls;
19087 }();
19088
19089 Realtime._fetchRTMServers = function _fetchRTMServers(_ref7) {
19090 var appId = _ref7.appId,
19091 ssl = _ref7.ssl,
19092 server = _ref7.server,
19093 RTMServerName = _ref7.RTMServerName;
19094 debug$5('fetch endpoint info');
19095 return this._getServerUrls({
19096 appId: appId,
19097 server: server
19098 }).then(tap(debug$5)).then(function (_ref8) {
19099 var RTMRouter = _ref8.RTMRouter;
19100 return client.get("".concat(RTMRouter, "/v1/route")).query({
19101 appId: appId,
19102 secure: ssl,
19103 features: isWeapp ? 'wechat' : undefined,
19104 server: RTMServerName,
19105 _t: Date.now()
19106 }).timeout(20000).then(function (res) {
19107 return res.body;
19108 }).then(tap(debug$5));
19109 });
19110 };
19111
19112 _proto._close = function _close() {
19113 if (this._openPromise) {
19114 this._openPromise.then(function (connection) {
19115 return connection.close();
19116 });
19117 }
19118
19119 delete this._openPromise;
19120 }
19121 /**
19122 * 手动进行重连。
19123 * SDK 在网络出现异常时会自动按照一定的时间间隔尝试重连,调用该方法会立即尝试重连并重置重连尝试计数器。
19124 * 只能在 `SCHEDULE` 事件之后,`RETRY` 事件之前调用,如果当前网络正常或者正在进行重连,调用该方法会抛异常。
19125 */
19126 ;
19127
19128 _proto.retry = function retry() {
19129 var _internal = internal(this),
19130 connection = _internal.connection;
19131
19132 if (!connection) {
19133 throw new Error('no connection established');
19134 }
19135
19136 if (connection.cannot('retry')) {
19137 throw new Error("retrying not allowed when not disconnected. the connection is now ".concat(connection.current));
19138 }
19139
19140 return connection.retry();
19141 }
19142 /**
19143 * 暂停,使 SDK 进入离线状态。
19144 * 你可以在网络断开、应用进入后台等时刻调用该方法让 SDK 进入离线状态,离线状态下不会尝试重连。
19145 * 在浏览器中 SDK 会自动监听网络变化,因此无需手动调用该方法。
19146 *
19147 * @since 3.4.0
19148 * @see Realtime#event:OFFLINE
19149 */
19150 ;
19151
19152 _proto.pause = function pause() {
19153 // 这个方法常常在网络断开、进入后台时被调用,此时 connection 可能没有建立或者已经 close。
19154 // 因此不像 retry,这个方法应该尽可能 loose
19155 var _internal2 = internal(this),
19156 connection = _internal2.connection;
19157
19158 if (!connection) return;
19159 if (connection.can('pause')) connection.pause();
19160 }
19161 /**
19162 * 恢复在线状态。
19163 * 你可以在网络恢复、应用回到前台等时刻调用该方法让 SDK 恢复在线状态,恢复在线状态后 SDK 会开始尝试重连。
19164 *
19165 * @since 3.4.0
19166 * @see Realtime#event:ONLINE
19167 */
19168 ;
19169
19170 _proto.resume = function resume() {
19171 // 与 pause 一样,这个方法应该尽可能 loose
19172 var _internal3 = internal(this),
19173 connection = _internal3.connection;
19174
19175 if (!connection) return;
19176 if (connection.can('resume')) connection.resume();
19177 };
19178
19179 _proto._registerPending = function _registerPending(value) {
19180 internal(this).pendingClients.add(value);
19181 };
19182
19183 _proto._deregisterPending = function _deregisterPending(client) {
19184 internal(this).pendingClients.delete(client);
19185 };
19186
19187 _proto._register = function _register(client) {
19188 internal(this).clients.add(client);
19189 };
19190
19191 _proto._deregister = function _deregister(client) {
19192 var _this = internal(this);
19193
19194 _this.clients.delete(client);
19195
19196 if (_this.clients.size + _this.pendingClients.size === 0) {
19197 this._close();
19198 }
19199 };
19200
19201 _proto._dispatchCommand = function _dispatchCommand(command) {
19202 return applyDispatcher(this._plugins.beforeCommandDispatch, [command, this]).then(function (shouldDispatch) {
19203 // no plugin handled this command
19204 if (shouldDispatch) return debug$5('[WARN] Unexpected message received: %O', trim(command));
19205 return false;
19206 });
19207 };
19208
19209 return Realtime;
19210 }(eventemitter3);
19211
19212 var polyfilledPromise = Promise;
19213
19214 var rngBrowser = createCommonjsModule(function (module) {
19215 // Unique ID creation requires a high quality random # generator. In the
19216 // browser this is a little complicated due to unknown quality of Math.random()
19217 // and inconsistent support for the `crypto` API. We do the best we can via
19218 // feature-detection
19219
19220 // getRandomValues needs to be invoked in a context where "this" is a Crypto
19221 // implementation. Also, find the complete implementation of crypto on IE11.
19222 var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) ||
19223 (typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto));
19224
19225 if (getRandomValues) {
19226 // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
19227 var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
19228
19229 module.exports = function whatwgRNG() {
19230 getRandomValues(rnds8);
19231 return rnds8;
19232 };
19233 } else {
19234 // Math.random()-based (RNG)
19235 //
19236 // If all else fails, use Math.random(). It's fast, but is of unspecified
19237 // quality.
19238 var rnds = new Array(16);
19239
19240 module.exports = function mathRNG() {
19241 for (var i = 0, r; i < 16; i++) {
19242 if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
19243 rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
19244 }
19245
19246 return rnds;
19247 };
19248 }
19249 });
19250
19251 /**
19252 * Convert array of 16 byte values to UUID string format of the form:
19253 * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
19254 */
19255 var byteToHex = [];
19256 for (var i = 0; i < 256; ++i) {
19257 byteToHex[i] = (i + 0x100).toString(16).substr(1);
19258 }
19259
19260 function bytesToUuid(buf, offset) {
19261 var i = offset || 0;
19262 var bth = byteToHex;
19263 // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
19264 return ([bth[buf[i++]], bth[buf[i++]],
19265 bth[buf[i++]], bth[buf[i++]], '-',
19266 bth[buf[i++]], bth[buf[i++]], '-',
19267 bth[buf[i++]], bth[buf[i++]], '-',
19268 bth[buf[i++]], bth[buf[i++]], '-',
19269 bth[buf[i++]], bth[buf[i++]],
19270 bth[buf[i++]], bth[buf[i++]],
19271 bth[buf[i++]], bth[buf[i++]]]).join('');
19272 }
19273
19274 var bytesToUuid_1 = bytesToUuid;
19275
19276 function v4(options, buf, offset) {
19277 var i = buf && offset || 0;
19278
19279 if (typeof(options) == 'string') {
19280 buf = options === 'binary' ? new Array(16) : null;
19281 options = null;
19282 }
19283 options = options || {};
19284
19285 var rnds = options.random || (options.rng || rngBrowser)();
19286
19287 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
19288 rnds[6] = (rnds[6] & 0x0f) | 0x40;
19289 rnds[8] = (rnds[8] & 0x3f) | 0x80;
19290
19291 // Copy bytes to buffer, if provided
19292 if (buf) {
19293 for (var ii = 0; ii < 16; ++ii) {
19294 buf[i + ii] = rnds[ii];
19295 }
19296 }
19297
19298 return buf || bytesToUuid_1(rnds);
19299 }
19300
19301 var v4_1 = v4;
19302
19303 var base64Arraybuffer = createCommonjsModule(function (module, exports) {
19304 /*
19305 * base64-arraybuffer
19306 * https://github.com/niklasvh/base64-arraybuffer
19307 *
19308 * Copyright (c) 2012 Niklas von Hertzen
19309 * Licensed under the MIT license.
19310 */
19311 (function(){
19312
19313 var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
19314
19315 // Use a lookup table to find the index.
19316 var lookup = new Uint8Array(256);
19317 for (var i = 0; i < chars.length; i++) {
19318 lookup[chars.charCodeAt(i)] = i;
19319 }
19320
19321 exports.encode = function(arraybuffer) {
19322 var bytes = new Uint8Array(arraybuffer),
19323 i, len = bytes.length, base64 = "";
19324
19325 for (i = 0; i < len; i+=3) {
19326 base64 += chars[bytes[i] >> 2];
19327 base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
19328 base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
19329 base64 += chars[bytes[i + 2] & 63];
19330 }
19331
19332 if ((len % 3) === 2) {
19333 base64 = base64.substring(0, base64.length - 1) + "=";
19334 } else if (len % 3 === 1) {
19335 base64 = base64.substring(0, base64.length - 2) + "==";
19336 }
19337
19338 return base64;
19339 };
19340
19341 exports.decode = function(base64) {
19342 var bufferLength = base64.length * 0.75,
19343 len = base64.length, i, p = 0,
19344 encoded1, encoded2, encoded3, encoded4;
19345
19346 if (base64[base64.length - 1] === "=") {
19347 bufferLength--;
19348 if (base64[base64.length - 2] === "=") {
19349 bufferLength--;
19350 }
19351 }
19352
19353 var arraybuffer = new ArrayBuffer(bufferLength),
19354 bytes = new Uint8Array(arraybuffer);
19355
19356 for (i = 0; i < len; i+=4) {
19357 encoded1 = lookup[base64.charCodeAt(i)];
19358 encoded2 = lookup[base64.charCodeAt(i+1)];
19359 encoded3 = lookup[base64.charCodeAt(i+2)];
19360 encoded4 = lookup[base64.charCodeAt(i+3)];
19361
19362 bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
19363 bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
19364 bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
19365 }
19366
19367 return arraybuffer;
19368 };
19369 })();
19370 });
19371 var base64Arraybuffer_1 = base64Arraybuffer.encode;
19372 var base64Arraybuffer_2 = base64Arraybuffer.decode;
19373
19374 /**
19375 * Removes all key-value entries from the list cache.
19376 *
19377 * @private
19378 * @name clear
19379 * @memberOf ListCache
19380 */
19381 function listCacheClear() {
19382 this.__data__ = [];
19383 this.size = 0;
19384 }
19385
19386 var _listCacheClear = listCacheClear;
19387
19388 /**
19389 * Performs a
19390 * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
19391 * comparison between two values to determine if they are equivalent.
19392 *
19393 * @static
19394 * @memberOf _
19395 * @since 4.0.0
19396 * @category Lang
19397 * @param {*} value The value to compare.
19398 * @param {*} other The other value to compare.
19399 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
19400 * @example
19401 *
19402 * var object = { 'a': 1 };
19403 * var other = { 'a': 1 };
19404 *
19405 * _.eq(object, object);
19406 * // => true
19407 *
19408 * _.eq(object, other);
19409 * // => false
19410 *
19411 * _.eq('a', 'a');
19412 * // => true
19413 *
19414 * _.eq('a', Object('a'));
19415 * // => false
19416 *
19417 * _.eq(NaN, NaN);
19418 * // => true
19419 */
19420 function eq(value, other) {
19421 return value === other || (value !== value && other !== other);
19422 }
19423
19424 var eq_1 = eq;
19425
19426 /**
19427 * Gets the index at which the `key` is found in `array` of key-value pairs.
19428 *
19429 * @private
19430 * @param {Array} array The array to inspect.
19431 * @param {*} key The key to search for.
19432 * @returns {number} Returns the index of the matched value, else `-1`.
19433 */
19434 function assocIndexOf(array, key) {
19435 var length = array.length;
19436 while (length--) {
19437 if (eq_1(array[length][0], key)) {
19438 return length;
19439 }
19440 }
19441 return -1;
19442 }
19443
19444 var _assocIndexOf = assocIndexOf;
19445
19446 /** Used for built-in method references. */
19447 var arrayProto = Array.prototype;
19448
19449 /** Built-in value references. */
19450 var splice = arrayProto.splice;
19451
19452 /**
19453 * Removes `key` and its value from the list cache.
19454 *
19455 * @private
19456 * @name delete
19457 * @memberOf ListCache
19458 * @param {string} key The key of the value to remove.
19459 * @returns {boolean} Returns `true` if the entry was removed, else `false`.
19460 */
19461 function listCacheDelete(key) {
19462 var data = this.__data__,
19463 index = _assocIndexOf(data, key);
19464
19465 if (index < 0) {
19466 return false;
19467 }
19468 var lastIndex = data.length - 1;
19469 if (index == lastIndex) {
19470 data.pop();
19471 } else {
19472 splice.call(data, index, 1);
19473 }
19474 --this.size;
19475 return true;
19476 }
19477
19478 var _listCacheDelete = listCacheDelete;
19479
19480 /**
19481 * Gets the list cache value for `key`.
19482 *
19483 * @private
19484 * @name get
19485 * @memberOf ListCache
19486 * @param {string} key The key of the value to get.
19487 * @returns {*} Returns the entry value.
19488 */
19489 function listCacheGet(key) {
19490 var data = this.__data__,
19491 index = _assocIndexOf(data, key);
19492
19493 return index < 0 ? undefined : data[index][1];
19494 }
19495
19496 var _listCacheGet = listCacheGet;
19497
19498 /**
19499 * Checks if a list cache value for `key` exists.
19500 *
19501 * @private
19502 * @name has
19503 * @memberOf ListCache
19504 * @param {string} key The key of the entry to check.
19505 * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
19506 */
19507 function listCacheHas(key) {
19508 return _assocIndexOf(this.__data__, key) > -1;
19509 }
19510
19511 var _listCacheHas = listCacheHas;
19512
19513 /**
19514 * Sets the list cache `key` to `value`.
19515 *
19516 * @private
19517 * @name set
19518 * @memberOf ListCache
19519 * @param {string} key The key of the value to set.
19520 * @param {*} value The value to set.
19521 * @returns {Object} Returns the list cache instance.
19522 */
19523 function listCacheSet(key, value) {
19524 var data = this.__data__,
19525 index = _assocIndexOf(data, key);
19526
19527 if (index < 0) {
19528 ++this.size;
19529 data.push([key, value]);
19530 } else {
19531 data[index][1] = value;
19532 }
19533 return this;
19534 }
19535
19536 var _listCacheSet = listCacheSet;
19537
19538 /**
19539 * Creates an list cache object.
19540 *
19541 * @private
19542 * @constructor
19543 * @param {Array} [entries] The key-value pairs to cache.
19544 */
19545 function ListCache(entries) {
19546 var index = -1,
19547 length = entries == null ? 0 : entries.length;
19548
19549 this.clear();
19550 while (++index < length) {
19551 var entry = entries[index];
19552 this.set(entry[0], entry[1]);
19553 }
19554 }
19555
19556 // Add methods to `ListCache`.
19557 ListCache.prototype.clear = _listCacheClear;
19558 ListCache.prototype['delete'] = _listCacheDelete;
19559 ListCache.prototype.get = _listCacheGet;
19560 ListCache.prototype.has = _listCacheHas;
19561 ListCache.prototype.set = _listCacheSet;
19562
19563 var _ListCache = ListCache;
19564
19565 /**
19566 * Removes all key-value entries from the stack.
19567 *
19568 * @private
19569 * @name clear
19570 * @memberOf Stack
19571 */
19572 function stackClear() {
19573 this.__data__ = new _ListCache;
19574 this.size = 0;
19575 }
19576
19577 var _stackClear = stackClear;
19578
19579 /**
19580 * Removes `key` and its value from the stack.
19581 *
19582 * @private
19583 * @name delete
19584 * @memberOf Stack
19585 * @param {string} key The key of the value to remove.
19586 * @returns {boolean} Returns `true` if the entry was removed, else `false`.
19587 */
19588 function stackDelete(key) {
19589 var data = this.__data__,
19590 result = data['delete'](key);
19591
19592 this.size = data.size;
19593 return result;
19594 }
19595
19596 var _stackDelete = stackDelete;
19597
19598 /**
19599 * Gets the stack value for `key`.
19600 *
19601 * @private
19602 * @name get
19603 * @memberOf Stack
19604 * @param {string} key The key of the value to get.
19605 * @returns {*} Returns the entry value.
19606 */
19607 function stackGet(key) {
19608 return this.__data__.get(key);
19609 }
19610
19611 var _stackGet = stackGet;
19612
19613 /**
19614 * Checks if a stack value for `key` exists.
19615 *
19616 * @private
19617 * @name has
19618 * @memberOf Stack
19619 * @param {string} key The key of the entry to check.
19620 * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
19621 */
19622 function stackHas(key) {
19623 return this.__data__.has(key);
19624 }
19625
19626 var _stackHas = stackHas;
19627
19628 /** Used to detect overreaching core-js shims. */
19629 var coreJsData = _root['__core-js_shared__'];
19630
19631 var _coreJsData = coreJsData;
19632
19633 /** Used to detect methods masquerading as native. */
19634 var maskSrcKey = (function() {
19635 var uid = /[^.]+$/.exec(_coreJsData && _coreJsData.keys && _coreJsData.keys.IE_PROTO || '');
19636 return uid ? ('Symbol(src)_1.' + uid) : '';
19637 }());
19638
19639 /**
19640 * Checks if `func` has its source masked.
19641 *
19642 * @private
19643 * @param {Function} func The function to check.
19644 * @returns {boolean} Returns `true` if `func` is masked, else `false`.
19645 */
19646 function isMasked(func) {
19647 return !!maskSrcKey && (maskSrcKey in func);
19648 }
19649
19650 var _isMasked = isMasked;
19651
19652 /** Used for built-in method references. */
19653 var funcProto$1 = Function.prototype;
19654
19655 /** Used to resolve the decompiled source of functions. */
19656 var funcToString$1 = funcProto$1.toString;
19657
19658 /**
19659 * Converts `func` to its source code.
19660 *
19661 * @private
19662 * @param {Function} func The function to convert.
19663 * @returns {string} Returns the source code.
19664 */
19665 function toSource(func) {
19666 if (func != null) {
19667 try {
19668 return funcToString$1.call(func);
19669 } catch (e) {}
19670 try {
19671 return (func + '');
19672 } catch (e) {}
19673 }
19674 return '';
19675 }
19676
19677 var _toSource = toSource;
19678
19679 /**
19680 * Used to match `RegExp`
19681 * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
19682 */
19683 var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
19684
19685 /** Used to detect host constructors (Safari). */
19686 var reIsHostCtor = /^\[object .+?Constructor\]$/;
19687
19688 /** Used for built-in method references. */
19689 var funcProto$2 = Function.prototype,
19690 objectProto$7 = Object.prototype;
19691
19692 /** Used to resolve the decompiled source of functions. */
19693 var funcToString$2 = funcProto$2.toString;
19694
19695 /** Used to check objects for own properties. */
19696 var hasOwnProperty$6 = objectProto$7.hasOwnProperty;
19697
19698 /** Used to detect if a method is native. */
19699 var reIsNative = RegExp('^' +
19700 funcToString$2.call(hasOwnProperty$6).replace(reRegExpChar, '\\$&')
19701 .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
19702 );
19703
19704 /**
19705 * The base implementation of `_.isNative` without bad shim checks.
19706 *
19707 * @private
19708 * @param {*} value The value to check.
19709 * @returns {boolean} Returns `true` if `value` is a native function,
19710 * else `false`.
19711 */
19712 function baseIsNative(value) {
19713 if (!isObject_1$1(value) || _isMasked(value)) {
19714 return false;
19715 }
19716 var pattern = isFunction_1(value) ? reIsNative : reIsHostCtor;
19717 return pattern.test(_toSource(value));
19718 }
19719
19720 var _baseIsNative = baseIsNative;
19721
19722 /**
19723 * Gets the value at `key` of `object`.
19724 *
19725 * @private
19726 * @param {Object} [object] The object to query.
19727 * @param {string} key The key of the property to get.
19728 * @returns {*} Returns the property value.
19729 */
19730 function getValue(object, key) {
19731 return object == null ? undefined : object[key];
19732 }
19733
19734 var _getValue = getValue;
19735
19736 /**
19737 * Gets the native function at `key` of `object`.
19738 *
19739 * @private
19740 * @param {Object} object The object to query.
19741 * @param {string} key The key of the method to get.
19742 * @returns {*} Returns the function if it's native, else `undefined`.
19743 */
19744 function getNative(object, key) {
19745 var value = _getValue(object, key);
19746 return _baseIsNative(value) ? value : undefined;
19747 }
19748
19749 var _getNative = getNative;
19750
19751 /* Built-in method references that are verified to be native. */
19752 var Map = _getNative(_root, 'Map');
19753
19754 var _Map = Map;
19755
19756 /* Built-in method references that are verified to be native. */
19757 var nativeCreate = _getNative(Object, 'create');
19758
19759 var _nativeCreate = nativeCreate;
19760
19761 /**
19762 * Removes all key-value entries from the hash.
19763 *
19764 * @private
19765 * @name clear
19766 * @memberOf Hash
19767 */
19768 function hashClear() {
19769 this.__data__ = _nativeCreate ? _nativeCreate(null) : {};
19770 this.size = 0;
19771 }
19772
19773 var _hashClear = hashClear;
19774
19775 /**
19776 * Removes `key` and its value from the hash.
19777 *
19778 * @private
19779 * @name delete
19780 * @memberOf Hash
19781 * @param {Object} hash The hash to modify.
19782 * @param {string} key The key of the value to remove.
19783 * @returns {boolean} Returns `true` if the entry was removed, else `false`.
19784 */
19785 function hashDelete(key) {
19786 var result = this.has(key) && delete this.__data__[key];
19787 this.size -= result ? 1 : 0;
19788 return result;
19789 }
19790
19791 var _hashDelete = hashDelete;
19792
19793 /** Used to stand-in for `undefined` hash values. */
19794 var HASH_UNDEFINED = '__lodash_hash_undefined__';
19795
19796 /** Used for built-in method references. */
19797 var objectProto$8 = Object.prototype;
19798
19799 /** Used to check objects for own properties. */
19800 var hasOwnProperty$7 = objectProto$8.hasOwnProperty;
19801
19802 /**
19803 * Gets the hash value for `key`.
19804 *
19805 * @private
19806 * @name get
19807 * @memberOf Hash
19808 * @param {string} key The key of the value to get.
19809 * @returns {*} Returns the entry value.
19810 */
19811 function hashGet(key) {
19812 var data = this.__data__;
19813 if (_nativeCreate) {
19814 var result = data[key];
19815 return result === HASH_UNDEFINED ? undefined : result;
19816 }
19817 return hasOwnProperty$7.call(data, key) ? data[key] : undefined;
19818 }
19819
19820 var _hashGet = hashGet;
19821
19822 /** Used for built-in method references. */
19823 var objectProto$9 = Object.prototype;
19824
19825 /** Used to check objects for own properties. */
19826 var hasOwnProperty$8 = objectProto$9.hasOwnProperty;
19827
19828 /**
19829 * Checks if a hash value for `key` exists.
19830 *
19831 * @private
19832 * @name has
19833 * @memberOf Hash
19834 * @param {string} key The key of the entry to check.
19835 * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
19836 */
19837 function hashHas(key) {
19838 var data = this.__data__;
19839 return _nativeCreate ? (data[key] !== undefined) : hasOwnProperty$8.call(data, key);
19840 }
19841
19842 var _hashHas = hashHas;
19843
19844 /** Used to stand-in for `undefined` hash values. */
19845 var HASH_UNDEFINED$1 = '__lodash_hash_undefined__';
19846
19847 /**
19848 * Sets the hash `key` to `value`.
19849 *
19850 * @private
19851 * @name set
19852 * @memberOf Hash
19853 * @param {string} key The key of the value to set.
19854 * @param {*} value The value to set.
19855 * @returns {Object} Returns the hash instance.
19856 */
19857 function hashSet(key, value) {
19858 var data = this.__data__;
19859 this.size += this.has(key) ? 0 : 1;
19860 data[key] = (_nativeCreate && value === undefined) ? HASH_UNDEFINED$1 : value;
19861 return this;
19862 }
19863
19864 var _hashSet = hashSet;
19865
19866 /**
19867 * Creates a hash object.
19868 *
19869 * @private
19870 * @constructor
19871 * @param {Array} [entries] The key-value pairs to cache.
19872 */
19873 function Hash(entries) {
19874 var index = -1,
19875 length = entries == null ? 0 : entries.length;
19876
19877 this.clear();
19878 while (++index < length) {
19879 var entry = entries[index];
19880 this.set(entry[0], entry[1]);
19881 }
19882 }
19883
19884 // Add methods to `Hash`.
19885 Hash.prototype.clear = _hashClear;
19886 Hash.prototype['delete'] = _hashDelete;
19887 Hash.prototype.get = _hashGet;
19888 Hash.prototype.has = _hashHas;
19889 Hash.prototype.set = _hashSet;
19890
19891 var _Hash = Hash;
19892
19893 /**
19894 * Removes all key-value entries from the map.
19895 *
19896 * @private
19897 * @name clear
19898 * @memberOf MapCache
19899 */
19900 function mapCacheClear() {
19901 this.size = 0;
19902 this.__data__ = {
19903 'hash': new _Hash,
19904 'map': new (_Map || _ListCache),
19905 'string': new _Hash
19906 };
19907 }
19908
19909 var _mapCacheClear = mapCacheClear;
19910
19911 /**
19912 * Checks if `value` is suitable for use as unique object key.
19913 *
19914 * @private
19915 * @param {*} value The value to check.
19916 * @returns {boolean} Returns `true` if `value` is suitable, else `false`.
19917 */
19918 function isKeyable(value) {
19919 var type = typeof value;
19920 return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')
19921 ? (value !== '__proto__')
19922 : (value === null);
19923 }
19924
19925 var _isKeyable = isKeyable;
19926
19927 /**
19928 * Gets the data for `map`.
19929 *
19930 * @private
19931 * @param {Object} map The map to query.
19932 * @param {string} key The reference key.
19933 * @returns {*} Returns the map data.
19934 */
19935 function getMapData(map, key) {
19936 var data = map.__data__;
19937 return _isKeyable(key)
19938 ? data[typeof key == 'string' ? 'string' : 'hash']
19939 : data.map;
19940 }
19941
19942 var _getMapData = getMapData;
19943
19944 /**
19945 * Removes `key` and its value from the map.
19946 *
19947 * @private
19948 * @name delete
19949 * @memberOf MapCache
19950 * @param {string} key The key of the value to remove.
19951 * @returns {boolean} Returns `true` if the entry was removed, else `false`.
19952 */
19953 function mapCacheDelete(key) {
19954 var result = _getMapData(this, key)['delete'](key);
19955 this.size -= result ? 1 : 0;
19956 return result;
19957 }
19958
19959 var _mapCacheDelete = mapCacheDelete;
19960
19961 /**
19962 * Gets the map value for `key`.
19963 *
19964 * @private
19965 * @name get
19966 * @memberOf MapCache
19967 * @param {string} key The key of the value to get.
19968 * @returns {*} Returns the entry value.
19969 */
19970 function mapCacheGet(key) {
19971 return _getMapData(this, key).get(key);
19972 }
19973
19974 var _mapCacheGet = mapCacheGet;
19975
19976 /**
19977 * Checks if a map value for `key` exists.
19978 *
19979 * @private
19980 * @name has
19981 * @memberOf MapCache
19982 * @param {string} key The key of the entry to check.
19983 * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
19984 */
19985 function mapCacheHas(key) {
19986 return _getMapData(this, key).has(key);
19987 }
19988
19989 var _mapCacheHas = mapCacheHas;
19990
19991 /**
19992 * Sets the map `key` to `value`.
19993 *
19994 * @private
19995 * @name set
19996 * @memberOf MapCache
19997 * @param {string} key The key of the value to set.
19998 * @param {*} value The value to set.
19999 * @returns {Object} Returns the map cache instance.
20000 */
20001 function mapCacheSet(key, value) {
20002 var data = _getMapData(this, key),
20003 size = data.size;
20004
20005 data.set(key, value);
20006 this.size += data.size == size ? 0 : 1;
20007 return this;
20008 }
20009
20010 var _mapCacheSet = mapCacheSet;
20011
20012 /**
20013 * Creates a map cache object to store key-value pairs.
20014 *
20015 * @private
20016 * @constructor
20017 * @param {Array} [entries] The key-value pairs to cache.
20018 */
20019 function MapCache(entries) {
20020 var index = -1,
20021 length = entries == null ? 0 : entries.length;
20022
20023 this.clear();
20024 while (++index < length) {
20025 var entry = entries[index];
20026 this.set(entry[0], entry[1]);
20027 }
20028 }
20029
20030 // Add methods to `MapCache`.
20031 MapCache.prototype.clear = _mapCacheClear;
20032 MapCache.prototype['delete'] = _mapCacheDelete;
20033 MapCache.prototype.get = _mapCacheGet;
20034 MapCache.prototype.has = _mapCacheHas;
20035 MapCache.prototype.set = _mapCacheSet;
20036
20037 var _MapCache = MapCache;
20038
20039 /** Used as the size to enable large array optimizations. */
20040 var LARGE_ARRAY_SIZE = 200;
20041
20042 /**
20043 * Sets the stack `key` to `value`.
20044 *
20045 * @private
20046 * @name set
20047 * @memberOf Stack
20048 * @param {string} key The key of the value to set.
20049 * @param {*} value The value to set.
20050 * @returns {Object} Returns the stack cache instance.
20051 */
20052 function stackSet(key, value) {
20053 var data = this.__data__;
20054 if (data instanceof _ListCache) {
20055 var pairs = data.__data__;
20056 if (!_Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) {
20057 pairs.push([key, value]);
20058 this.size = ++data.size;
20059 return this;
20060 }
20061 data = this.__data__ = new _MapCache(pairs);
20062 }
20063 data.set(key, value);
20064 this.size = data.size;
20065 return this;
20066 }
20067
20068 var _stackSet = stackSet;
20069
20070 /**
20071 * Creates a stack cache object to store key-value pairs.
20072 *
20073 * @private
20074 * @constructor
20075 * @param {Array} [entries] The key-value pairs to cache.
20076 */
20077 function Stack(entries) {
20078 var data = this.__data__ = new _ListCache(entries);
20079 this.size = data.size;
20080 }
20081
20082 // Add methods to `Stack`.
20083 Stack.prototype.clear = _stackClear;
20084 Stack.prototype['delete'] = _stackDelete;
20085 Stack.prototype.get = _stackGet;
20086 Stack.prototype.has = _stackHas;
20087 Stack.prototype.set = _stackSet;
20088
20089 var _Stack = Stack;
20090
20091 /** Used to stand-in for `undefined` hash values. */
20092 var HASH_UNDEFINED$2 = '__lodash_hash_undefined__';
20093
20094 /**
20095 * Adds `value` to the array cache.
20096 *
20097 * @private
20098 * @name add
20099 * @memberOf SetCache
20100 * @alias push
20101 * @param {*} value The value to cache.
20102 * @returns {Object} Returns the cache instance.
20103 */
20104 function setCacheAdd(value) {
20105 this.__data__.set(value, HASH_UNDEFINED$2);
20106 return this;
20107 }
20108
20109 var _setCacheAdd = setCacheAdd;
20110
20111 /**
20112 * Checks if `value` is in the array cache.
20113 *
20114 * @private
20115 * @name has
20116 * @memberOf SetCache
20117 * @param {*} value The value to search for.
20118 * @returns {number} Returns `true` if `value` is found, else `false`.
20119 */
20120 function setCacheHas(value) {
20121 return this.__data__.has(value);
20122 }
20123
20124 var _setCacheHas = setCacheHas;
20125
20126 /**
20127 *
20128 * Creates an array cache object to store unique values.
20129 *
20130 * @private
20131 * @constructor
20132 * @param {Array} [values] The values to cache.
20133 */
20134 function SetCache(values) {
20135 var index = -1,
20136 length = values == null ? 0 : values.length;
20137
20138 this.__data__ = new _MapCache;
20139 while (++index < length) {
20140 this.add(values[index]);
20141 }
20142 }
20143
20144 // Add methods to `SetCache`.
20145 SetCache.prototype.add = SetCache.prototype.push = _setCacheAdd;
20146 SetCache.prototype.has = _setCacheHas;
20147
20148 var _SetCache = SetCache;
20149
20150 /**
20151 * A specialized version of `_.some` for arrays without support for iteratee
20152 * shorthands.
20153 *
20154 * @private
20155 * @param {Array} [array] The array to iterate over.
20156 * @param {Function} predicate The function invoked per iteration.
20157 * @returns {boolean} Returns `true` if any element passes the predicate check,
20158 * else `false`.
20159 */
20160 function arraySome(array, predicate) {
20161 var index = -1,
20162 length = array == null ? 0 : array.length;
20163
20164 while (++index < length) {
20165 if (predicate(array[index], index, array)) {
20166 return true;
20167 }
20168 }
20169 return false;
20170 }
20171
20172 var _arraySome = arraySome;
20173
20174 /**
20175 * Checks if a `cache` value for `key` exists.
20176 *
20177 * @private
20178 * @param {Object} cache The cache to query.
20179 * @param {string} key The key of the entry to check.
20180 * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
20181 */
20182 function cacheHas(cache, key) {
20183 return cache.has(key);
20184 }
20185
20186 var _cacheHas = cacheHas;
20187
20188 /** Used to compose bitmasks for value comparisons. */
20189 var COMPARE_PARTIAL_FLAG = 1,
20190 COMPARE_UNORDERED_FLAG = 2;
20191
20192 /**
20193 * A specialized version of `baseIsEqualDeep` for arrays with support for
20194 * partial deep comparisons.
20195 *
20196 * @private
20197 * @param {Array} array The array to compare.
20198 * @param {Array} other The other array to compare.
20199 * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
20200 * @param {Function} customizer The function to customize comparisons.
20201 * @param {Function} equalFunc The function to determine equivalents of values.
20202 * @param {Object} stack Tracks traversed `array` and `other` objects.
20203 * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
20204 */
20205 function equalArrays(array, other, bitmask, customizer, equalFunc, stack) {
20206 var isPartial = bitmask & COMPARE_PARTIAL_FLAG,
20207 arrLength = array.length,
20208 othLength = other.length;
20209
20210 if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
20211 return false;
20212 }
20213 // Assume cyclic values are equal.
20214 var stacked = stack.get(array);
20215 if (stacked && stack.get(other)) {
20216 return stacked == other;
20217 }
20218 var index = -1,
20219 result = true,
20220 seen = (bitmask & COMPARE_UNORDERED_FLAG) ? new _SetCache : undefined;
20221
20222 stack.set(array, other);
20223 stack.set(other, array);
20224
20225 // Ignore non-index properties.
20226 while (++index < arrLength) {
20227 var arrValue = array[index],
20228 othValue = other[index];
20229
20230 if (customizer) {
20231 var compared = isPartial
20232 ? customizer(othValue, arrValue, index, other, array, stack)
20233 : customizer(arrValue, othValue, index, array, other, stack);
20234 }
20235 if (compared !== undefined) {
20236 if (compared) {
20237 continue;
20238 }
20239 result = false;
20240 break;
20241 }
20242 // Recursively compare arrays (susceptible to call stack limits).
20243 if (seen) {
20244 if (!_arraySome(other, function(othValue, othIndex) {
20245 if (!_cacheHas(seen, othIndex) &&
20246 (arrValue === othValue || equalFunc(arrValue, othValue, bitmask, customizer, stack))) {
20247 return seen.push(othIndex);
20248 }
20249 })) {
20250 result = false;
20251 break;
20252 }
20253 } else if (!(
20254 arrValue === othValue ||
20255 equalFunc(arrValue, othValue, bitmask, customizer, stack)
20256 )) {
20257 result = false;
20258 break;
20259 }
20260 }
20261 stack['delete'](array);
20262 stack['delete'](other);
20263 return result;
20264 }
20265
20266 var _equalArrays = equalArrays;
20267
20268 /** Built-in value references. */
20269 var Uint8Array$1 = _root.Uint8Array;
20270
20271 var _Uint8Array = Uint8Array$1;
20272
20273 /**
20274 * Converts `map` to its key-value pairs.
20275 *
20276 * @private
20277 * @param {Object} map The map to convert.
20278 * @returns {Array} Returns the key-value pairs.
20279 */
20280 function mapToArray(map) {
20281 var index = -1,
20282 result = Array(map.size);
20283
20284 map.forEach(function(value, key) {
20285 result[++index] = [key, value];
20286 });
20287 return result;
20288 }
20289
20290 var _mapToArray = mapToArray;
20291
20292 /**
20293 * Converts `set` to an array of its values.
20294 *
20295 * @private
20296 * @param {Object} set The set to convert.
20297 * @returns {Array} Returns the values.
20298 */
20299 function setToArray(set) {
20300 var index = -1,
20301 result = Array(set.size);
20302
20303 set.forEach(function(value) {
20304 result[++index] = value;
20305 });
20306 return result;
20307 }
20308
20309 var _setToArray = setToArray;
20310
20311 /** Used to compose bitmasks for value comparisons. */
20312 var COMPARE_PARTIAL_FLAG$1 = 1,
20313 COMPARE_UNORDERED_FLAG$1 = 2;
20314
20315 /** `Object#toString` result references. */
20316 var boolTag$1 = '[object Boolean]',
20317 dateTag$1 = '[object Date]',
20318 errorTag$1 = '[object Error]',
20319 mapTag$1 = '[object Map]',
20320 numberTag$1 = '[object Number]',
20321 regexpTag$1 = '[object RegExp]',
20322 setTag$1 = '[object Set]',
20323 stringTag$1 = '[object String]',
20324 symbolTag = '[object Symbol]';
20325
20326 var arrayBufferTag$1 = '[object ArrayBuffer]',
20327 dataViewTag$1 = '[object DataView]';
20328
20329 /** Used to convert symbols to primitives and strings. */
20330 var symbolProto = _Symbol ? _Symbol.prototype : undefined,
20331 symbolValueOf = symbolProto ? symbolProto.valueOf : undefined;
20332
20333 /**
20334 * A specialized version of `baseIsEqualDeep` for comparing objects of
20335 * the same `toStringTag`.
20336 *
20337 * **Note:** This function only supports comparing values with tags of
20338 * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
20339 *
20340 * @private
20341 * @param {Object} object The object to compare.
20342 * @param {Object} other The other object to compare.
20343 * @param {string} tag The `toStringTag` of the objects to compare.
20344 * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
20345 * @param {Function} customizer The function to customize comparisons.
20346 * @param {Function} equalFunc The function to determine equivalents of values.
20347 * @param {Object} stack Tracks traversed `object` and `other` objects.
20348 * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
20349 */
20350 function equalByTag(object, other, tag, bitmask, customizer, equalFunc, stack) {
20351 switch (tag) {
20352 case dataViewTag$1:
20353 if ((object.byteLength != other.byteLength) ||
20354 (object.byteOffset != other.byteOffset)) {
20355 return false;
20356 }
20357 object = object.buffer;
20358 other = other.buffer;
20359
20360 case arrayBufferTag$1:
20361 if ((object.byteLength != other.byteLength) ||
20362 !equalFunc(new _Uint8Array(object), new _Uint8Array(other))) {
20363 return false;
20364 }
20365 return true;
20366
20367 case boolTag$1:
20368 case dateTag$1:
20369 case numberTag$1:
20370 // Coerce booleans to `1` or `0` and dates to milliseconds.
20371 // Invalid dates are coerced to `NaN`.
20372 return eq_1(+object, +other);
20373
20374 case errorTag$1:
20375 return object.name == other.name && object.message == other.message;
20376
20377 case regexpTag$1:
20378 case stringTag$1:
20379 // Coerce regexes to strings and treat strings, primitives and objects,
20380 // as equal. See http://www.ecma-international.org/ecma-262/7.0/#sec-regexp.prototype.tostring
20381 // for more details.
20382 return object == (other + '');
20383
20384 case mapTag$1:
20385 var convert = _mapToArray;
20386
20387 case setTag$1:
20388 var isPartial = bitmask & COMPARE_PARTIAL_FLAG$1;
20389 convert || (convert = _setToArray);
20390
20391 if (object.size != other.size && !isPartial) {
20392 return false;
20393 }
20394 // Assume cyclic values are equal.
20395 var stacked = stack.get(object);
20396 if (stacked) {
20397 return stacked == other;
20398 }
20399 bitmask |= COMPARE_UNORDERED_FLAG$1;
20400
20401 // Recursively compare objects (susceptible to call stack limits).
20402 stack.set(object, other);
20403 var result = _equalArrays(convert(object), convert(other), bitmask, customizer, equalFunc, stack);
20404 stack['delete'](object);
20405 return result;
20406
20407 case symbolTag:
20408 if (symbolValueOf) {
20409 return symbolValueOf.call(object) == symbolValueOf.call(other);
20410 }
20411 }
20412 return false;
20413 }
20414
20415 var _equalByTag = equalByTag;
20416
20417 /**
20418 * Appends the elements of `values` to `array`.
20419 *
20420 * @private
20421 * @param {Array} array The array to modify.
20422 * @param {Array} values The values to append.
20423 * @returns {Array} Returns `array`.
20424 */
20425 function arrayPush(array, values) {
20426 var index = -1,
20427 length = values.length,
20428 offset = array.length;
20429
20430 while (++index < length) {
20431 array[offset + index] = values[index];
20432 }
20433 return array;
20434 }
20435
20436 var _arrayPush = arrayPush;
20437
20438 /**
20439 * The base implementation of `getAllKeys` and `getAllKeysIn` which uses
20440 * `keysFunc` and `symbolsFunc` to get the enumerable property names and
20441 * symbols of `object`.
20442 *
20443 * @private
20444 * @param {Object} object The object to query.
20445 * @param {Function} keysFunc The function to get the keys of `object`.
20446 * @param {Function} symbolsFunc The function to get the symbols of `object`.
20447 * @returns {Array} Returns the array of property names and symbols.
20448 */
20449 function baseGetAllKeys(object, keysFunc, symbolsFunc) {
20450 var result = keysFunc(object);
20451 return isArray_1(object) ? result : _arrayPush(result, symbolsFunc(object));
20452 }
20453
20454 var _baseGetAllKeys = baseGetAllKeys;
20455
20456 /**
20457 * A specialized version of `_.filter` for arrays without support for
20458 * iteratee shorthands.
20459 *
20460 * @private
20461 * @param {Array} [array] The array to iterate over.
20462 * @param {Function} predicate The function invoked per iteration.
20463 * @returns {Array} Returns the new filtered array.
20464 */
20465 function arrayFilter(array, predicate) {
20466 var index = -1,
20467 length = array == null ? 0 : array.length,
20468 resIndex = 0,
20469 result = [];
20470
20471 while (++index < length) {
20472 var value = array[index];
20473 if (predicate(value, index, array)) {
20474 result[resIndex++] = value;
20475 }
20476 }
20477 return result;
20478 }
20479
20480 var _arrayFilter = arrayFilter;
20481
20482 /**
20483 * This method returns a new empty array.
20484 *
20485 * @static
20486 * @memberOf _
20487 * @since 4.13.0
20488 * @category Util
20489 * @returns {Array} Returns the new empty array.
20490 * @example
20491 *
20492 * var arrays = _.times(2, _.stubArray);
20493 *
20494 * console.log(arrays);
20495 * // => [[], []]
20496 *
20497 * console.log(arrays[0] === arrays[1]);
20498 * // => false
20499 */
20500 function stubArray() {
20501 return [];
20502 }
20503
20504 var stubArray_1 = stubArray;
20505
20506 /** Used for built-in method references. */
20507 var objectProto$a = Object.prototype;
20508
20509 /** Built-in value references. */
20510 var propertyIsEnumerable$1 = objectProto$a.propertyIsEnumerable;
20511
20512 /* Built-in method references for those with the same name as other `lodash` methods. */
20513 var nativeGetSymbols = Object.getOwnPropertySymbols;
20514
20515 /**
20516 * Creates an array of the own enumerable symbols of `object`.
20517 *
20518 * @private
20519 * @param {Object} object The object to query.
20520 * @returns {Array} Returns the array of symbols.
20521 */
20522 var getSymbols = !nativeGetSymbols ? stubArray_1 : function(object) {
20523 if (object == null) {
20524 return [];
20525 }
20526 object = Object(object);
20527 return _arrayFilter(nativeGetSymbols(object), function(symbol) {
20528 return propertyIsEnumerable$1.call(object, symbol);
20529 });
20530 };
20531
20532 var _getSymbols = getSymbols;
20533
20534 /**
20535 * Creates an array of own enumerable property names and symbols of `object`.
20536 *
20537 * @private
20538 * @param {Object} object The object to query.
20539 * @returns {Array} Returns the array of property names and symbols.
20540 */
20541 function getAllKeys(object) {
20542 return _baseGetAllKeys(object, keys_1, _getSymbols);
20543 }
20544
20545 var _getAllKeys = getAllKeys;
20546
20547 /** Used to compose bitmasks for value comparisons. */
20548 var COMPARE_PARTIAL_FLAG$2 = 1;
20549
20550 /** Used for built-in method references. */
20551 var objectProto$b = Object.prototype;
20552
20553 /** Used to check objects for own properties. */
20554 var hasOwnProperty$9 = objectProto$b.hasOwnProperty;
20555
20556 /**
20557 * A specialized version of `baseIsEqualDeep` for objects with support for
20558 * partial deep comparisons.
20559 *
20560 * @private
20561 * @param {Object} object The object to compare.
20562 * @param {Object} other The other object to compare.
20563 * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
20564 * @param {Function} customizer The function to customize comparisons.
20565 * @param {Function} equalFunc The function to determine equivalents of values.
20566 * @param {Object} stack Tracks traversed `object` and `other` objects.
20567 * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
20568 */
20569 function equalObjects(object, other, bitmask, customizer, equalFunc, stack) {
20570 var isPartial = bitmask & COMPARE_PARTIAL_FLAG$2,
20571 objProps = _getAllKeys(object),
20572 objLength = objProps.length,
20573 othProps = _getAllKeys(other),
20574 othLength = othProps.length;
20575
20576 if (objLength != othLength && !isPartial) {
20577 return false;
20578 }
20579 var index = objLength;
20580 while (index--) {
20581 var key = objProps[index];
20582 if (!(isPartial ? key in other : hasOwnProperty$9.call(other, key))) {
20583 return false;
20584 }
20585 }
20586 // Assume cyclic values are equal.
20587 var stacked = stack.get(object);
20588 if (stacked && stack.get(other)) {
20589 return stacked == other;
20590 }
20591 var result = true;
20592 stack.set(object, other);
20593 stack.set(other, object);
20594
20595 var skipCtor = isPartial;
20596 while (++index < objLength) {
20597 key = objProps[index];
20598 var objValue = object[key],
20599 othValue = other[key];
20600
20601 if (customizer) {
20602 var compared = isPartial
20603 ? customizer(othValue, objValue, key, other, object, stack)
20604 : customizer(objValue, othValue, key, object, other, stack);
20605 }
20606 // Recursively compare objects (susceptible to call stack limits).
20607 if (!(compared === undefined
20608 ? (objValue === othValue || equalFunc(objValue, othValue, bitmask, customizer, stack))
20609 : compared
20610 )) {
20611 result = false;
20612 break;
20613 }
20614 skipCtor || (skipCtor = key == 'constructor');
20615 }
20616 if (result && !skipCtor) {
20617 var objCtor = object.constructor,
20618 othCtor = other.constructor;
20619
20620 // Non `Object` object instances with different constructors are not equal.
20621 if (objCtor != othCtor &&
20622 ('constructor' in object && 'constructor' in other) &&
20623 !(typeof objCtor == 'function' && objCtor instanceof objCtor &&
20624 typeof othCtor == 'function' && othCtor instanceof othCtor)) {
20625 result = false;
20626 }
20627 }
20628 stack['delete'](object);
20629 stack['delete'](other);
20630 return result;
20631 }
20632
20633 var _equalObjects = equalObjects;
20634
20635 /* Built-in method references that are verified to be native. */
20636 var DataView = _getNative(_root, 'DataView');
20637
20638 var _DataView = DataView;
20639
20640 /* Built-in method references that are verified to be native. */
20641 var Promise$1 = _getNative(_root, 'Promise');
20642
20643 var _Promise = Promise$1;
20644
20645 /* Built-in method references that are verified to be native. */
20646 var Set$1 = _getNative(_root, 'Set');
20647
20648 var _Set = Set$1;
20649
20650 /* Built-in method references that are verified to be native. */
20651 var WeakMap$1 = _getNative(_root, 'WeakMap');
20652
20653 var _WeakMap = WeakMap$1;
20654
20655 /** `Object#toString` result references. */
20656 var mapTag$2 = '[object Map]',
20657 objectTag$2 = '[object Object]',
20658 promiseTag = '[object Promise]',
20659 setTag$2 = '[object Set]',
20660 weakMapTag$1 = '[object WeakMap]';
20661
20662 var dataViewTag$2 = '[object DataView]';
20663
20664 /** Used to detect maps, sets, and weakmaps. */
20665 var dataViewCtorString = _toSource(_DataView),
20666 mapCtorString = _toSource(_Map),
20667 promiseCtorString = _toSource(_Promise),
20668 setCtorString = _toSource(_Set),
20669 weakMapCtorString = _toSource(_WeakMap);
20670
20671 /**
20672 * Gets the `toStringTag` of `value`.
20673 *
20674 * @private
20675 * @param {*} value The value to query.
20676 * @returns {string} Returns the `toStringTag`.
20677 */
20678 var getTag = _baseGetTag;
20679
20680 // Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.
20681 if ((_DataView && getTag(new _DataView(new ArrayBuffer(1))) != dataViewTag$2) ||
20682 (_Map && getTag(new _Map) != mapTag$2) ||
20683 (_Promise && getTag(_Promise.resolve()) != promiseTag) ||
20684 (_Set && getTag(new _Set) != setTag$2) ||
20685 (_WeakMap && getTag(new _WeakMap) != weakMapTag$1)) {
20686 getTag = function(value) {
20687 var result = _baseGetTag(value),
20688 Ctor = result == objectTag$2 ? value.constructor : undefined,
20689 ctorString = Ctor ? _toSource(Ctor) : '';
20690
20691 if (ctorString) {
20692 switch (ctorString) {
20693 case dataViewCtorString: return dataViewTag$2;
20694 case mapCtorString: return mapTag$2;
20695 case promiseCtorString: return promiseTag;
20696 case setCtorString: return setTag$2;
20697 case weakMapCtorString: return weakMapTag$1;
20698 }
20699 }
20700 return result;
20701 };
20702 }
20703
20704 var _getTag = getTag;
20705
20706 /** Used to compose bitmasks for value comparisons. */
20707 var COMPARE_PARTIAL_FLAG$3 = 1;
20708
20709 /** `Object#toString` result references. */
20710 var argsTag$2 = '[object Arguments]',
20711 arrayTag$1 = '[object Array]',
20712 objectTag$3 = '[object Object]';
20713
20714 /** Used for built-in method references. */
20715 var objectProto$c = Object.prototype;
20716
20717 /** Used to check objects for own properties. */
20718 var hasOwnProperty$a = objectProto$c.hasOwnProperty;
20719
20720 /**
20721 * A specialized version of `baseIsEqual` for arrays and objects which performs
20722 * deep comparisons and tracks traversed objects enabling objects with circular
20723 * references to be compared.
20724 *
20725 * @private
20726 * @param {Object} object The object to compare.
20727 * @param {Object} other The other object to compare.
20728 * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
20729 * @param {Function} customizer The function to customize comparisons.
20730 * @param {Function} equalFunc The function to determine equivalents of values.
20731 * @param {Object} [stack] Tracks traversed `object` and `other` objects.
20732 * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
20733 */
20734 function baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {
20735 var objIsArr = isArray_1(object),
20736 othIsArr = isArray_1(other),
20737 objTag = objIsArr ? arrayTag$1 : _getTag(object),
20738 othTag = othIsArr ? arrayTag$1 : _getTag(other);
20739
20740 objTag = objTag == argsTag$2 ? objectTag$3 : objTag;
20741 othTag = othTag == argsTag$2 ? objectTag$3 : othTag;
20742
20743 var objIsObj = objTag == objectTag$3,
20744 othIsObj = othTag == objectTag$3,
20745 isSameTag = objTag == othTag;
20746
20747 if (isSameTag && isBuffer_1(object)) {
20748 if (!isBuffer_1(other)) {
20749 return false;
20750 }
20751 objIsArr = true;
20752 objIsObj = false;
20753 }
20754 if (isSameTag && !objIsObj) {
20755 stack || (stack = new _Stack);
20756 return (objIsArr || isTypedArray_1(object))
20757 ? _equalArrays(object, other, bitmask, customizer, equalFunc, stack)
20758 : _equalByTag(object, other, objTag, bitmask, customizer, equalFunc, stack);
20759 }
20760 if (!(bitmask & COMPARE_PARTIAL_FLAG$3)) {
20761 var objIsWrapped = objIsObj && hasOwnProperty$a.call(object, '__wrapped__'),
20762 othIsWrapped = othIsObj && hasOwnProperty$a.call(other, '__wrapped__');
20763
20764 if (objIsWrapped || othIsWrapped) {
20765 var objUnwrapped = objIsWrapped ? object.value() : object,
20766 othUnwrapped = othIsWrapped ? other.value() : other;
20767
20768 stack || (stack = new _Stack);
20769 return equalFunc(objUnwrapped, othUnwrapped, bitmask, customizer, stack);
20770 }
20771 }
20772 if (!isSameTag) {
20773 return false;
20774 }
20775 stack || (stack = new _Stack);
20776 return _equalObjects(object, other, bitmask, customizer, equalFunc, stack);
20777 }
20778
20779 var _baseIsEqualDeep = baseIsEqualDeep;
20780
20781 /**
20782 * The base implementation of `_.isEqual` which supports partial comparisons
20783 * and tracks traversed objects.
20784 *
20785 * @private
20786 * @param {*} value The value to compare.
20787 * @param {*} other The other value to compare.
20788 * @param {boolean} bitmask The bitmask flags.
20789 * 1 - Unordered comparison
20790 * 2 - Partial comparison
20791 * @param {Function} [customizer] The function to customize comparisons.
20792 * @param {Object} [stack] Tracks traversed `value` and `other` objects.
20793 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
20794 */
20795 function baseIsEqual(value, other, bitmask, customizer, stack) {
20796 if (value === other) {
20797 return true;
20798 }
20799 if (value == null || other == null || (!isObjectLike_1(value) && !isObjectLike_1(other))) {
20800 return value !== value && other !== other;
20801 }
20802 return _baseIsEqualDeep(value, other, bitmask, customizer, baseIsEqual, stack);
20803 }
20804
20805 var _baseIsEqual = baseIsEqual;
20806
20807 /** Used to compose bitmasks for value comparisons. */
20808 var COMPARE_PARTIAL_FLAG$4 = 1,
20809 COMPARE_UNORDERED_FLAG$2 = 2;
20810
20811 /**
20812 * The base implementation of `_.isMatch` without support for iteratee shorthands.
20813 *
20814 * @private
20815 * @param {Object} object The object to inspect.
20816 * @param {Object} source The object of property values to match.
20817 * @param {Array} matchData The property names, values, and compare flags to match.
20818 * @param {Function} [customizer] The function to customize comparisons.
20819 * @returns {boolean} Returns `true` if `object` is a match, else `false`.
20820 */
20821 function baseIsMatch(object, source, matchData, customizer) {
20822 var index = matchData.length,
20823 length = index,
20824 noCustomizer = !customizer;
20825
20826 if (object == null) {
20827 return !length;
20828 }
20829 object = Object(object);
20830 while (index--) {
20831 var data = matchData[index];
20832 if ((noCustomizer && data[2])
20833 ? data[1] !== object[data[0]]
20834 : !(data[0] in object)
20835 ) {
20836 return false;
20837 }
20838 }
20839 while (++index < length) {
20840 data = matchData[index];
20841 var key = data[0],
20842 objValue = object[key],
20843 srcValue = data[1];
20844
20845 if (noCustomizer && data[2]) {
20846 if (objValue === undefined && !(key in object)) {
20847 return false;
20848 }
20849 } else {
20850 var stack = new _Stack;
20851 if (customizer) {
20852 var result = customizer(objValue, srcValue, key, object, source, stack);
20853 }
20854 if (!(result === undefined
20855 ? _baseIsEqual(srcValue, objValue, COMPARE_PARTIAL_FLAG$4 | COMPARE_UNORDERED_FLAG$2, customizer, stack)
20856 : result
20857 )) {
20858 return false;
20859 }
20860 }
20861 }
20862 return true;
20863 }
20864
20865 var _baseIsMatch = baseIsMatch;
20866
20867 /**
20868 * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
20869 *
20870 * @private
20871 * @param {*} value The value to check.
20872 * @returns {boolean} Returns `true` if `value` if suitable for strict
20873 * equality comparisons, else `false`.
20874 */
20875 function isStrictComparable(value) {
20876 return value === value && !isObject_1$1(value);
20877 }
20878
20879 var _isStrictComparable = isStrictComparable;
20880
20881 /**
20882 * Gets the property names, values, and compare flags of `object`.
20883 *
20884 * @private
20885 * @param {Object} object The object to query.
20886 * @returns {Array} Returns the match data of `object`.
20887 */
20888 function getMatchData(object) {
20889 var result = keys_1(object),
20890 length = result.length;
20891
20892 while (length--) {
20893 var key = result[length],
20894 value = object[key];
20895
20896 result[length] = [key, value, _isStrictComparable(value)];
20897 }
20898 return result;
20899 }
20900
20901 var _getMatchData = getMatchData;
20902
20903 /**
20904 * A specialized version of `matchesProperty` for source values suitable
20905 * for strict equality comparisons, i.e. `===`.
20906 *
20907 * @private
20908 * @param {string} key The key of the property to get.
20909 * @param {*} srcValue The value to match.
20910 * @returns {Function} Returns the new spec function.
20911 */
20912 function matchesStrictComparable(key, srcValue) {
20913 return function(object) {
20914 if (object == null) {
20915 return false;
20916 }
20917 return object[key] === srcValue &&
20918 (srcValue !== undefined || (key in Object(object)));
20919 };
20920 }
20921
20922 var _matchesStrictComparable = matchesStrictComparable;
20923
20924 /**
20925 * The base implementation of `_.matches` which doesn't clone `source`.
20926 *
20927 * @private
20928 * @param {Object} source The object of property values to match.
20929 * @returns {Function} Returns the new spec function.
20930 */
20931 function baseMatches(source) {
20932 var matchData = _getMatchData(source);
20933 if (matchData.length == 1 && matchData[0][2]) {
20934 return _matchesStrictComparable(matchData[0][0], matchData[0][1]);
20935 }
20936 return function(object) {
20937 return object === source || _baseIsMatch(object, source, matchData);
20938 };
20939 }
20940
20941 var _baseMatches = baseMatches;
20942
20943 /** `Object#toString` result references. */
20944 var symbolTag$1 = '[object Symbol]';
20945
20946 /**
20947 * Checks if `value` is classified as a `Symbol` primitive or object.
20948 *
20949 * @static
20950 * @memberOf _
20951 * @since 4.0.0
20952 * @category Lang
20953 * @param {*} value The value to check.
20954 * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
20955 * @example
20956 *
20957 * _.isSymbol(Symbol.iterator);
20958 * // => true
20959 *
20960 * _.isSymbol('abc');
20961 * // => false
20962 */
20963 function isSymbol(value) {
20964 return typeof value == 'symbol' ||
20965 (isObjectLike_1(value) && _baseGetTag(value) == symbolTag$1);
20966 }
20967
20968 var isSymbol_1 = isSymbol;
20969
20970 /** Used to match property names within property paths. */
20971 var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
20972 reIsPlainProp = /^\w*$/;
20973
20974 /**
20975 * Checks if `value` is a property name and not a property path.
20976 *
20977 * @private
20978 * @param {*} value The value to check.
20979 * @param {Object} [object] The object to query keys on.
20980 * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
20981 */
20982 function isKey(value, object) {
20983 if (isArray_1(value)) {
20984 return false;
20985 }
20986 var type = typeof value;
20987 if (type == 'number' || type == 'symbol' || type == 'boolean' ||
20988 value == null || isSymbol_1(value)) {
20989 return true;
20990 }
20991 return reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
20992 (object != null && value in Object(object));
20993 }
20994
20995 var _isKey = isKey;
20996
20997 /** Error message constants. */
20998 var FUNC_ERROR_TEXT = 'Expected a function';
20999
21000 /**
21001 * Creates a function that memoizes the result of `func`. If `resolver` is
21002 * provided, it determines the cache key for storing the result based on the
21003 * arguments provided to the memoized function. By default, the first argument
21004 * provided to the memoized function is used as the map cache key. The `func`
21005 * is invoked with the `this` binding of the memoized function.
21006 *
21007 * **Note:** The cache is exposed as the `cache` property on the memoized
21008 * function. Its creation may be customized by replacing the `_.memoize.Cache`
21009 * constructor with one whose instances implement the
21010 * [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)
21011 * method interface of `clear`, `delete`, `get`, `has`, and `set`.
21012 *
21013 * @static
21014 * @memberOf _
21015 * @since 0.1.0
21016 * @category Function
21017 * @param {Function} func The function to have its output memoized.
21018 * @param {Function} [resolver] The function to resolve the cache key.
21019 * @returns {Function} Returns the new memoized function.
21020 * @example
21021 *
21022 * var object = { 'a': 1, 'b': 2 };
21023 * var other = { 'c': 3, 'd': 4 };
21024 *
21025 * var values = _.memoize(_.values);
21026 * values(object);
21027 * // => [1, 2]
21028 *
21029 * values(other);
21030 * // => [3, 4]
21031 *
21032 * object.a = 2;
21033 * values(object);
21034 * // => [1, 2]
21035 *
21036 * // Modify the result cache.
21037 * values.cache.set(object, ['a', 'b']);
21038 * values(object);
21039 * // => ['a', 'b']
21040 *
21041 * // Replace `_.memoize.Cache`.
21042 * _.memoize.Cache = WeakMap;
21043 */
21044 function memoize(func, resolver) {
21045 if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) {
21046 throw new TypeError(FUNC_ERROR_TEXT);
21047 }
21048 var memoized = function() {
21049 var args = arguments,
21050 key = resolver ? resolver.apply(this, args) : args[0],
21051 cache = memoized.cache;
21052
21053 if (cache.has(key)) {
21054 return cache.get(key);
21055 }
21056 var result = func.apply(this, args);
21057 memoized.cache = cache.set(key, result) || cache;
21058 return result;
21059 };
21060 memoized.cache = new (memoize.Cache || _MapCache);
21061 return memoized;
21062 }
21063
21064 // Expose `MapCache`.
21065 memoize.Cache = _MapCache;
21066
21067 var memoize_1 = memoize;
21068
21069 /** Used as the maximum memoize cache size. */
21070 var MAX_MEMOIZE_SIZE = 500;
21071
21072 /**
21073 * A specialized version of `_.memoize` which clears the memoized function's
21074 * cache when it exceeds `MAX_MEMOIZE_SIZE`.
21075 *
21076 * @private
21077 * @param {Function} func The function to have its output memoized.
21078 * @returns {Function} Returns the new memoized function.
21079 */
21080 function memoizeCapped(func) {
21081 var result = memoize_1(func, function(key) {
21082 if (cache.size === MAX_MEMOIZE_SIZE) {
21083 cache.clear();
21084 }
21085 return key;
21086 });
21087
21088 var cache = result.cache;
21089 return result;
21090 }
21091
21092 var _memoizeCapped = memoizeCapped;
21093
21094 /** Used to match property names within property paths. */
21095 var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
21096
21097 /** Used to match backslashes in property paths. */
21098 var reEscapeChar = /\\(\\)?/g;
21099
21100 /**
21101 * Converts `string` to a property path array.
21102 *
21103 * @private
21104 * @param {string} string The string to convert.
21105 * @returns {Array} Returns the property path array.
21106 */
21107 var stringToPath = _memoizeCapped(function(string) {
21108 var result = [];
21109 if (string.charCodeAt(0) === 46 /* . */) {
21110 result.push('');
21111 }
21112 string.replace(rePropName, function(match, number, quote, subString) {
21113 result.push(quote ? subString.replace(reEscapeChar, '$1') : (number || match));
21114 });
21115 return result;
21116 });
21117
21118 var _stringToPath = stringToPath;
21119
21120 /** Used as references for various `Number` constants. */
21121 var INFINITY = 1 / 0;
21122
21123 /** Used to convert symbols to primitives and strings. */
21124 var symbolProto$1 = _Symbol ? _Symbol.prototype : undefined,
21125 symbolToString = symbolProto$1 ? symbolProto$1.toString : undefined;
21126
21127 /**
21128 * The base implementation of `_.toString` which doesn't convert nullish
21129 * values to empty strings.
21130 *
21131 * @private
21132 * @param {*} value The value to process.
21133 * @returns {string} Returns the string.
21134 */
21135 function baseToString(value) {
21136 // Exit early for strings to avoid a performance hit in some environments.
21137 if (typeof value == 'string') {
21138 return value;
21139 }
21140 if (isArray_1(value)) {
21141 // Recursively convert values (susceptible to call stack limits).
21142 return _arrayMap(value, baseToString) + '';
21143 }
21144 if (isSymbol_1(value)) {
21145 return symbolToString ? symbolToString.call(value) : '';
21146 }
21147 var result = (value + '');
21148 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
21149 }
21150
21151 var _baseToString = baseToString;
21152
21153 /**
21154 * Converts `value` to a string. An empty string is returned for `null`
21155 * and `undefined` values. The sign of `-0` is preserved.
21156 *
21157 * @static
21158 * @memberOf _
21159 * @since 4.0.0
21160 * @category Lang
21161 * @param {*} value The value to convert.
21162 * @returns {string} Returns the converted string.
21163 * @example
21164 *
21165 * _.toString(null);
21166 * // => ''
21167 *
21168 * _.toString(-0);
21169 * // => '-0'
21170 *
21171 * _.toString([1, 2, 3]);
21172 * // => '1,2,3'
21173 */
21174 function toString(value) {
21175 return value == null ? '' : _baseToString(value);
21176 }
21177
21178 var toString_1 = toString;
21179
21180 /**
21181 * Casts `value` to a path array if it's not one.
21182 *
21183 * @private
21184 * @param {*} value The value to inspect.
21185 * @param {Object} [object] The object to query keys on.
21186 * @returns {Array} Returns the cast property path array.
21187 */
21188 function castPath(value, object) {
21189 if (isArray_1(value)) {
21190 return value;
21191 }
21192 return _isKey(value, object) ? [value] : _stringToPath(toString_1(value));
21193 }
21194
21195 var _castPath = castPath;
21196
21197 /** Used as references for various `Number` constants. */
21198 var INFINITY$1 = 1 / 0;
21199
21200 /**
21201 * Converts `value` to a string key if it's not a string or symbol.
21202 *
21203 * @private
21204 * @param {*} value The value to inspect.
21205 * @returns {string|symbol} Returns the key.
21206 */
21207 function toKey(value) {
21208 if (typeof value == 'string' || isSymbol_1(value)) {
21209 return value;
21210 }
21211 var result = (value + '');
21212 return (result == '0' && (1 / value) == -INFINITY$1) ? '-0' : result;
21213 }
21214
21215 var _toKey = toKey;
21216
21217 /**
21218 * The base implementation of `_.get` without support for default values.
21219 *
21220 * @private
21221 * @param {Object} object The object to query.
21222 * @param {Array|string} path The path of the property to get.
21223 * @returns {*} Returns the resolved value.
21224 */
21225 function baseGet(object, path) {
21226 path = _castPath(path, object);
21227
21228 var index = 0,
21229 length = path.length;
21230
21231 while (object != null && index < length) {
21232 object = object[_toKey(path[index++])];
21233 }
21234 return (index && index == length) ? object : undefined;
21235 }
21236
21237 var _baseGet = baseGet;
21238
21239 /**
21240 * Gets the value at `path` of `object`. If the resolved value is
21241 * `undefined`, the `defaultValue` is returned in its place.
21242 *
21243 * @static
21244 * @memberOf _
21245 * @since 3.7.0
21246 * @category Object
21247 * @param {Object} object The object to query.
21248 * @param {Array|string} path The path of the property to get.
21249 * @param {*} [defaultValue] The value returned for `undefined` resolved values.
21250 * @returns {*} Returns the resolved value.
21251 * @example
21252 *
21253 * var object = { 'a': [{ 'b': { 'c': 3 } }] };
21254 *
21255 * _.get(object, 'a[0].b.c');
21256 * // => 3
21257 *
21258 * _.get(object, ['a', '0', 'b', 'c']);
21259 * // => 3
21260 *
21261 * _.get(object, 'a.b.c', 'default');
21262 * // => 'default'
21263 */
21264 function get(object, path, defaultValue) {
21265 var result = object == null ? undefined : _baseGet(object, path);
21266 return result === undefined ? defaultValue : result;
21267 }
21268
21269 var get_1 = get;
21270
21271 /**
21272 * The base implementation of `_.hasIn` without support for deep paths.
21273 *
21274 * @private
21275 * @param {Object} [object] The object to query.
21276 * @param {Array|string} key The key to check.
21277 * @returns {boolean} Returns `true` if `key` exists, else `false`.
21278 */
21279 function baseHasIn(object, key) {
21280 return object != null && key in Object(object);
21281 }
21282
21283 var _baseHasIn = baseHasIn;
21284
21285 /**
21286 * Checks if `path` exists on `object`.
21287 *
21288 * @private
21289 * @param {Object} object The object to query.
21290 * @param {Array|string} path The path to check.
21291 * @param {Function} hasFunc The function to check properties.
21292 * @returns {boolean} Returns `true` if `path` exists, else `false`.
21293 */
21294 function hasPath(object, path, hasFunc) {
21295 path = _castPath(path, object);
21296
21297 var index = -1,
21298 length = path.length,
21299 result = false;
21300
21301 while (++index < length) {
21302 var key = _toKey(path[index]);
21303 if (!(result = object != null && hasFunc(object, key))) {
21304 break;
21305 }
21306 object = object[key];
21307 }
21308 if (result || ++index != length) {
21309 return result;
21310 }
21311 length = object == null ? 0 : object.length;
21312 return !!length && isLength_1(length) && _isIndex(key, length) &&
21313 (isArray_1(object) || isArguments_1(object));
21314 }
21315
21316 var _hasPath = hasPath;
21317
21318 /**
21319 * Checks if `path` is a direct or inherited property of `object`.
21320 *
21321 * @static
21322 * @memberOf _
21323 * @since 4.0.0
21324 * @category Object
21325 * @param {Object} object The object to query.
21326 * @param {Array|string} path The path to check.
21327 * @returns {boolean} Returns `true` if `path` exists, else `false`.
21328 * @example
21329 *
21330 * var object = _.create({ 'a': _.create({ 'b': 2 }) });
21331 *
21332 * _.hasIn(object, 'a');
21333 * // => true
21334 *
21335 * _.hasIn(object, 'a.b');
21336 * // => true
21337 *
21338 * _.hasIn(object, ['a', 'b']);
21339 * // => true
21340 *
21341 * _.hasIn(object, 'b');
21342 * // => false
21343 */
21344 function hasIn(object, path) {
21345 return object != null && _hasPath(object, path, _baseHasIn);
21346 }
21347
21348 var hasIn_1 = hasIn;
21349
21350 /** Used to compose bitmasks for value comparisons. */
21351 var COMPARE_PARTIAL_FLAG$5 = 1,
21352 COMPARE_UNORDERED_FLAG$3 = 2;
21353
21354 /**
21355 * The base implementation of `_.matchesProperty` which doesn't clone `srcValue`.
21356 *
21357 * @private
21358 * @param {string} path The path of the property to get.
21359 * @param {*} srcValue The value to match.
21360 * @returns {Function} Returns the new spec function.
21361 */
21362 function baseMatchesProperty(path, srcValue) {
21363 if (_isKey(path) && _isStrictComparable(srcValue)) {
21364 return _matchesStrictComparable(_toKey(path), srcValue);
21365 }
21366 return function(object) {
21367 var objValue = get_1(object, path);
21368 return (objValue === undefined && objValue === srcValue)
21369 ? hasIn_1(object, path)
21370 : _baseIsEqual(srcValue, objValue, COMPARE_PARTIAL_FLAG$5 | COMPARE_UNORDERED_FLAG$3);
21371 };
21372 }
21373
21374 var _baseMatchesProperty = baseMatchesProperty;
21375
21376 /**
21377 * This method returns the first argument it receives.
21378 *
21379 * @static
21380 * @since 0.1.0
21381 * @memberOf _
21382 * @category Util
21383 * @param {*} value Any value.
21384 * @returns {*} Returns `value`.
21385 * @example
21386 *
21387 * var object = { 'a': 1 };
21388 *
21389 * console.log(_.identity(object) === object);
21390 * // => true
21391 */
21392 function identity(value) {
21393 return value;
21394 }
21395
21396 var identity_1 = identity;
21397
21398 /**
21399 * The base implementation of `_.property` without support for deep paths.
21400 *
21401 * @private
21402 * @param {string} key The key of the property to get.
21403 * @returns {Function} Returns the new accessor function.
21404 */
21405 function baseProperty(key) {
21406 return function(object) {
21407 return object == null ? undefined : object[key];
21408 };
21409 }
21410
21411 var _baseProperty = baseProperty;
21412
21413 /**
21414 * A specialized version of `baseProperty` which supports deep paths.
21415 *
21416 * @private
21417 * @param {Array|string} path The path of the property to get.
21418 * @returns {Function} Returns the new accessor function.
21419 */
21420 function basePropertyDeep(path) {
21421 return function(object) {
21422 return _baseGet(object, path);
21423 };
21424 }
21425
21426 var _basePropertyDeep = basePropertyDeep;
21427
21428 /**
21429 * Creates a function that returns the value at `path` of a given object.
21430 *
21431 * @static
21432 * @memberOf _
21433 * @since 2.4.0
21434 * @category Util
21435 * @param {Array|string} path The path of the property to get.
21436 * @returns {Function} Returns the new accessor function.
21437 * @example
21438 *
21439 * var objects = [
21440 * { 'a': { 'b': 2 } },
21441 * { 'a': { 'b': 1 } }
21442 * ];
21443 *
21444 * _.map(objects, _.property('a.b'));
21445 * // => [2, 1]
21446 *
21447 * _.map(_.sortBy(objects, _.property(['a', 'b'])), 'a.b');
21448 * // => [1, 2]
21449 */
21450 function property(path) {
21451 return _isKey(path) ? _baseProperty(_toKey(path)) : _basePropertyDeep(path);
21452 }
21453
21454 var property_1 = property;
21455
21456 /**
21457 * The base implementation of `_.iteratee`.
21458 *
21459 * @private
21460 * @param {*} [value=_.identity] The value to convert to an iteratee.
21461 * @returns {Function} Returns the iteratee.
21462 */
21463 function baseIteratee(value) {
21464 // Don't store the `typeof` result in a variable to avoid a JIT bug in Safari 9.
21465 // See https://bugs.webkit.org/show_bug.cgi?id=156034 for more details.
21466 if (typeof value == 'function') {
21467 return value;
21468 }
21469 if (value == null) {
21470 return identity_1;
21471 }
21472 if (typeof value == 'object') {
21473 return isArray_1(value)
21474 ? _baseMatchesProperty(value[0], value[1])
21475 : _baseMatches(value);
21476 }
21477 return property_1(value);
21478 }
21479
21480 var _baseIteratee = baseIteratee;
21481
21482 /**
21483 * Gets the last element of `array`.
21484 *
21485 * @static
21486 * @memberOf _
21487 * @since 0.1.0
21488 * @category Array
21489 * @param {Array} array The array to query.
21490 * @returns {*} Returns the last element of `array`.
21491 * @example
21492 *
21493 * _.last([1, 2, 3]);
21494 * // => 3
21495 */
21496 function last(array) {
21497 var length = array == null ? 0 : array.length;
21498 return length ? array[length - 1] : undefined;
21499 }
21500
21501 var last_1 = last;
21502
21503 /**
21504 * The base implementation of `_.slice` without an iteratee call guard.
21505 *
21506 * @private
21507 * @param {Array} array The array to slice.
21508 * @param {number} [start=0] The start position.
21509 * @param {number} [end=array.length] The end position.
21510 * @returns {Array} Returns the slice of `array`.
21511 */
21512 function baseSlice(array, start, end) {
21513 var index = -1,
21514 length = array.length;
21515
21516 if (start < 0) {
21517 start = -start > length ? 0 : (length + start);
21518 }
21519 end = end > length ? length : end;
21520 if (end < 0) {
21521 end += length;
21522 }
21523 length = start > end ? 0 : ((end - start) >>> 0);
21524 start >>>= 0;
21525
21526 var result = Array(length);
21527 while (++index < length) {
21528 result[index] = array[index + start];
21529 }
21530 return result;
21531 }
21532
21533 var _baseSlice = baseSlice;
21534
21535 /**
21536 * Gets the parent value at `path` of `object`.
21537 *
21538 * @private
21539 * @param {Object} object The object to query.
21540 * @param {Array} path The path to get the parent value of.
21541 * @returns {*} Returns the parent value.
21542 */
21543 function parent(object, path) {
21544 return path.length < 2 ? object : _baseGet(object, _baseSlice(path, 0, -1));
21545 }
21546
21547 var _parent = parent;
21548
21549 /**
21550 * The base implementation of `_.unset`.
21551 *
21552 * @private
21553 * @param {Object} object The object to modify.
21554 * @param {Array|string} path The property path to unset.
21555 * @returns {boolean} Returns `true` if the property is deleted, else `false`.
21556 */
21557 function baseUnset(object, path) {
21558 path = _castPath(path, object);
21559 object = _parent(object, path);
21560 return object == null || delete object[_toKey(last_1(path))];
21561 }
21562
21563 var _baseUnset = baseUnset;
21564
21565 /** Used for built-in method references. */
21566 var arrayProto$1 = Array.prototype;
21567
21568 /** Built-in value references. */
21569 var splice$1 = arrayProto$1.splice;
21570
21571 /**
21572 * The base implementation of `_.pullAt` without support for individual
21573 * indexes or capturing the removed elements.
21574 *
21575 * @private
21576 * @param {Array} array The array to modify.
21577 * @param {number[]} indexes The indexes of elements to remove.
21578 * @returns {Array} Returns `array`.
21579 */
21580 function basePullAt(array, indexes) {
21581 var length = array ? indexes.length : 0,
21582 lastIndex = length - 1;
21583
21584 while (length--) {
21585 var index = indexes[length];
21586 if (length == lastIndex || index !== previous) {
21587 var previous = index;
21588 if (_isIndex(index)) {
21589 splice$1.call(array, index, 1);
21590 } else {
21591 _baseUnset(array, index);
21592 }
21593 }
21594 }
21595 return array;
21596 }
21597
21598 var _basePullAt = basePullAt;
21599
21600 /**
21601 * Removes all elements from `array` that `predicate` returns truthy for
21602 * and returns an array of the removed elements. The predicate is invoked
21603 * with three arguments: (value, index, array).
21604 *
21605 * **Note:** Unlike `_.filter`, this method mutates `array`. Use `_.pull`
21606 * to pull elements from an array by value.
21607 *
21608 * @static
21609 * @memberOf _
21610 * @since 2.0.0
21611 * @category Array
21612 * @param {Array} array The array to modify.
21613 * @param {Function} [predicate=_.identity] The function invoked per iteration.
21614 * @returns {Array} Returns the new array of removed elements.
21615 * @example
21616 *
21617 * var array = [1, 2, 3, 4];
21618 * var evens = _.remove(array, function(n) {
21619 * return n % 2 == 0;
21620 * });
21621 *
21622 * console.log(array);
21623 * // => [1, 3]
21624 *
21625 * console.log(evens);
21626 * // => [2, 4]
21627 */
21628 function remove(array, predicate) {
21629 var result = [];
21630 if (!(array && array.length)) {
21631 return result;
21632 }
21633 var index = -1,
21634 indexes = [],
21635 length = array.length;
21636
21637 predicate = _baseIteratee(predicate, 3);
21638 while (++index < length) {
21639 var value = array[index];
21640 if (predicate(value, index, array)) {
21641 result.push(value);
21642 indexes.push(index);
21643 }
21644 }
21645 _basePullAt(array, indexes);
21646 return result;
21647 }
21648
21649 var remove_1 = remove;
21650
21651 /** `Object#toString` result references. */
21652 var mapTag$3 = '[object Map]',
21653 setTag$3 = '[object Set]';
21654
21655 /** Used for built-in method references. */
21656 var objectProto$d = Object.prototype;
21657
21658 /** Used to check objects for own properties. */
21659 var hasOwnProperty$b = objectProto$d.hasOwnProperty;
21660
21661 /**
21662 * Checks if `value` is an empty object, collection, map, or set.
21663 *
21664 * Objects are considered empty if they have no own enumerable string keyed
21665 * properties.
21666 *
21667 * Array-like values such as `arguments` objects, arrays, buffers, strings, or
21668 * jQuery-like collections are considered empty if they have a `length` of `0`.
21669 * Similarly, maps and sets are considered empty if they have a `size` of `0`.
21670 *
21671 * @static
21672 * @memberOf _
21673 * @since 0.1.0
21674 * @category Lang
21675 * @param {*} value The value to check.
21676 * @returns {boolean} Returns `true` if `value` is empty, else `false`.
21677 * @example
21678 *
21679 * _.isEmpty(null);
21680 * // => true
21681 *
21682 * _.isEmpty(true);
21683 * // => true
21684 *
21685 * _.isEmpty(1);
21686 * // => true
21687 *
21688 * _.isEmpty([1, 2, 3]);
21689 * // => false
21690 *
21691 * _.isEmpty({ 'a': 1 });
21692 * // => false
21693 */
21694 function isEmpty(value) {
21695 if (value == null) {
21696 return true;
21697 }
21698 if (isArrayLike_1(value) &&
21699 (isArray_1(value) || typeof value == 'string' || typeof value.splice == 'function' ||
21700 isBuffer_1(value) || isTypedArray_1(value) || isArguments_1(value))) {
21701 return !value.length;
21702 }
21703 var tag = _getTag(value);
21704 if (tag == mapTag$3 || tag == setTag$3) {
21705 return !value.size;
21706 }
21707 if (_isPrototype(value)) {
21708 return !_baseKeys(value).length;
21709 }
21710 for (var key in value) {
21711 if (hasOwnProperty$b.call(value, key)) {
21712 return false;
21713 }
21714 }
21715 return true;
21716 }
21717
21718 var isEmpty_1 = isEmpty;
21719
21720 /**
21721 * A specialized version of `_.forEach` for arrays without support for
21722 * iteratee shorthands.
21723 *
21724 * @private
21725 * @param {Array} [array] The array to iterate over.
21726 * @param {Function} iteratee The function invoked per iteration.
21727 * @returns {Array} Returns `array`.
21728 */
21729 function arrayEach(array, iteratee) {
21730 var index = -1,
21731 length = array == null ? 0 : array.length;
21732
21733 while (++index < length) {
21734 if (iteratee(array[index], index, array) === false) {
21735 break;
21736 }
21737 }
21738 return array;
21739 }
21740
21741 var _arrayEach = arrayEach;
21742
21743 var defineProperty = (function() {
21744 try {
21745 var func = _getNative(Object, 'defineProperty');
21746 func({}, '', {});
21747 return func;
21748 } catch (e) {}
21749 }());
21750
21751 var _defineProperty$1 = defineProperty;
21752
21753 /**
21754 * The base implementation of `assignValue` and `assignMergeValue` without
21755 * value checks.
21756 *
21757 * @private
21758 * @param {Object} object The object to modify.
21759 * @param {string} key The key of the property to assign.
21760 * @param {*} value The value to assign.
21761 */
21762 function baseAssignValue(object, key, value) {
21763 if (key == '__proto__' && _defineProperty$1) {
21764 _defineProperty$1(object, key, {
21765 'configurable': true,
21766 'enumerable': true,
21767 'value': value,
21768 'writable': true
21769 });
21770 } else {
21771 object[key] = value;
21772 }
21773 }
21774
21775 var _baseAssignValue = baseAssignValue;
21776
21777 /** Used for built-in method references. */
21778 var objectProto$e = Object.prototype;
21779
21780 /** Used to check objects for own properties. */
21781 var hasOwnProperty$c = objectProto$e.hasOwnProperty;
21782
21783 /**
21784 * Assigns `value` to `key` of `object` if the existing value is not equivalent
21785 * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
21786 * for equality comparisons.
21787 *
21788 * @private
21789 * @param {Object} object The object to modify.
21790 * @param {string} key The key of the property to assign.
21791 * @param {*} value The value to assign.
21792 */
21793 function assignValue(object, key, value) {
21794 var objValue = object[key];
21795 if (!(hasOwnProperty$c.call(object, key) && eq_1(objValue, value)) ||
21796 (value === undefined && !(key in object))) {
21797 _baseAssignValue(object, key, value);
21798 }
21799 }
21800
21801 var _assignValue = assignValue;
21802
21803 /**
21804 * Copies properties of `source` to `object`.
21805 *
21806 * @private
21807 * @param {Object} source The object to copy properties from.
21808 * @param {Array} props The property identifiers to copy.
21809 * @param {Object} [object={}] The object to copy properties to.
21810 * @param {Function} [customizer] The function to customize copied values.
21811 * @returns {Object} Returns `object`.
21812 */
21813 function copyObject(source, props, object, customizer) {
21814 var isNew = !object;
21815 object || (object = {});
21816
21817 var index = -1,
21818 length = props.length;
21819
21820 while (++index < length) {
21821 var key = props[index];
21822
21823 var newValue = customizer
21824 ? customizer(object[key], source[key], key, object, source)
21825 : undefined;
21826
21827 if (newValue === undefined) {
21828 newValue = source[key];
21829 }
21830 if (isNew) {
21831 _baseAssignValue(object, key, newValue);
21832 } else {
21833 _assignValue(object, key, newValue);
21834 }
21835 }
21836 return object;
21837 }
21838
21839 var _copyObject = copyObject;
21840
21841 /**
21842 * The base implementation of `_.assign` without support for multiple sources
21843 * or `customizer` functions.
21844 *
21845 * @private
21846 * @param {Object} object The destination object.
21847 * @param {Object} source The source object.
21848 * @returns {Object} Returns `object`.
21849 */
21850 function baseAssign(object, source) {
21851 return object && _copyObject(source, keys_1(source), object);
21852 }
21853
21854 var _baseAssign = baseAssign;
21855
21856 /**
21857 * This function is like
21858 * [`Object.keys`](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
21859 * except that it includes inherited enumerable properties.
21860 *
21861 * @private
21862 * @param {Object} object The object to query.
21863 * @returns {Array} Returns the array of property names.
21864 */
21865 function nativeKeysIn(object) {
21866 var result = [];
21867 if (object != null) {
21868 for (var key in Object(object)) {
21869 result.push(key);
21870 }
21871 }
21872 return result;
21873 }
21874
21875 var _nativeKeysIn = nativeKeysIn;
21876
21877 /** Used for built-in method references. */
21878 var objectProto$f = Object.prototype;
21879
21880 /** Used to check objects for own properties. */
21881 var hasOwnProperty$d = objectProto$f.hasOwnProperty;
21882
21883 /**
21884 * The base implementation of `_.keysIn` which doesn't treat sparse arrays as dense.
21885 *
21886 * @private
21887 * @param {Object} object The object to query.
21888 * @returns {Array} Returns the array of property names.
21889 */
21890 function baseKeysIn(object) {
21891 if (!isObject_1$1(object)) {
21892 return _nativeKeysIn(object);
21893 }
21894 var isProto = _isPrototype(object),
21895 result = [];
21896
21897 for (var key in object) {
21898 if (!(key == 'constructor' && (isProto || !hasOwnProperty$d.call(object, key)))) {
21899 result.push(key);
21900 }
21901 }
21902 return result;
21903 }
21904
21905 var _baseKeysIn = baseKeysIn;
21906
21907 /**
21908 * Creates an array of the own and inherited enumerable property names of `object`.
21909 *
21910 * **Note:** Non-object values are coerced to objects.
21911 *
21912 * @static
21913 * @memberOf _
21914 * @since 3.0.0
21915 * @category Object
21916 * @param {Object} object The object to query.
21917 * @returns {Array} Returns the array of property names.
21918 * @example
21919 *
21920 * function Foo() {
21921 * this.a = 1;
21922 * this.b = 2;
21923 * }
21924 *
21925 * Foo.prototype.c = 3;
21926 *
21927 * _.keysIn(new Foo);
21928 * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
21929 */
21930 function keysIn$1(object) {
21931 return isArrayLike_1(object) ? _arrayLikeKeys(object, true) : _baseKeysIn(object);
21932 }
21933
21934 var keysIn_1 = keysIn$1;
21935
21936 /**
21937 * The base implementation of `_.assignIn` without support for multiple sources
21938 * or `customizer` functions.
21939 *
21940 * @private
21941 * @param {Object} object The destination object.
21942 * @param {Object} source The source object.
21943 * @returns {Object} Returns `object`.
21944 */
21945 function baseAssignIn(object, source) {
21946 return object && _copyObject(source, keysIn_1(source), object);
21947 }
21948
21949 var _baseAssignIn = baseAssignIn;
21950
21951 var _cloneBuffer = createCommonjsModule(function (module, exports) {
21952 /** Detect free variable `exports`. */
21953 var freeExports = exports && !exports.nodeType && exports;
21954
21955 /** Detect free variable `module`. */
21956 var freeModule = freeExports && 'object' == 'object' && module && !module.nodeType && module;
21957
21958 /** Detect the popular CommonJS extension `module.exports`. */
21959 var moduleExports = freeModule && freeModule.exports === freeExports;
21960
21961 /** Built-in value references. */
21962 var Buffer = moduleExports ? _root.Buffer : undefined,
21963 allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined;
21964
21965 /**
21966 * Creates a clone of `buffer`.
21967 *
21968 * @private
21969 * @param {Buffer} buffer The buffer to clone.
21970 * @param {boolean} [isDeep] Specify a deep clone.
21971 * @returns {Buffer} Returns the cloned buffer.
21972 */
21973 function cloneBuffer(buffer, isDeep) {
21974 if (isDeep) {
21975 return buffer.slice();
21976 }
21977 var length = buffer.length,
21978 result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length);
21979
21980 buffer.copy(result);
21981 return result;
21982 }
21983
21984 module.exports = cloneBuffer;
21985 });
21986
21987 /**
21988 * Copies own symbols of `source` to `object`.
21989 *
21990 * @private
21991 * @param {Object} source The object to copy symbols from.
21992 * @param {Object} [object={}] The object to copy symbols to.
21993 * @returns {Object} Returns `object`.
21994 */
21995 function copySymbols(source, object) {
21996 return _copyObject(source, _getSymbols(source), object);
21997 }
21998
21999 var _copySymbols = copySymbols;
22000
22001 /* Built-in method references for those with the same name as other `lodash` methods. */
22002 var nativeGetSymbols$1 = Object.getOwnPropertySymbols;
22003
22004 /**
22005 * Creates an array of the own and inherited enumerable symbols of `object`.
22006 *
22007 * @private
22008 * @param {Object} object The object to query.
22009 * @returns {Array} Returns the array of symbols.
22010 */
22011 var getSymbolsIn = !nativeGetSymbols$1 ? stubArray_1 : function(object) {
22012 var result = [];
22013 while (object) {
22014 _arrayPush(result, _getSymbols(object));
22015 object = _getPrototype(object);
22016 }
22017 return result;
22018 };
22019
22020 var _getSymbolsIn = getSymbolsIn;
22021
22022 /**
22023 * Copies own and inherited symbols of `source` to `object`.
22024 *
22025 * @private
22026 * @param {Object} source The object to copy symbols from.
22027 * @param {Object} [object={}] The object to copy symbols to.
22028 * @returns {Object} Returns `object`.
22029 */
22030 function copySymbolsIn(source, object) {
22031 return _copyObject(source, _getSymbolsIn(source), object);
22032 }
22033
22034 var _copySymbolsIn = copySymbolsIn;
22035
22036 /**
22037 * Creates an array of own and inherited enumerable property names and
22038 * symbols of `object`.
22039 *
22040 * @private
22041 * @param {Object} object The object to query.
22042 * @returns {Array} Returns the array of property names and symbols.
22043 */
22044 function getAllKeysIn(object) {
22045 return _baseGetAllKeys(object, keysIn_1, _getSymbolsIn);
22046 }
22047
22048 var _getAllKeysIn = getAllKeysIn;
22049
22050 /** Used for built-in method references. */
22051 var objectProto$g = Object.prototype;
22052
22053 /** Used to check objects for own properties. */
22054 var hasOwnProperty$e = objectProto$g.hasOwnProperty;
22055
22056 /**
22057 * Initializes an array clone.
22058 *
22059 * @private
22060 * @param {Array} array The array to clone.
22061 * @returns {Array} Returns the initialized clone.
22062 */
22063 function initCloneArray(array) {
22064 var length = array.length,
22065 result = new array.constructor(length);
22066
22067 // Add properties assigned by `RegExp#exec`.
22068 if (length && typeof array[0] == 'string' && hasOwnProperty$e.call(array, 'index')) {
22069 result.index = array.index;
22070 result.input = array.input;
22071 }
22072 return result;
22073 }
22074
22075 var _initCloneArray = initCloneArray;
22076
22077 /**
22078 * Creates a clone of `arrayBuffer`.
22079 *
22080 * @private
22081 * @param {ArrayBuffer} arrayBuffer The array buffer to clone.
22082 * @returns {ArrayBuffer} Returns the cloned array buffer.
22083 */
22084 function cloneArrayBuffer(arrayBuffer) {
22085 var result = new arrayBuffer.constructor(arrayBuffer.byteLength);
22086 new _Uint8Array(result).set(new _Uint8Array(arrayBuffer));
22087 return result;
22088 }
22089
22090 var _cloneArrayBuffer = cloneArrayBuffer;
22091
22092 /**
22093 * Creates a clone of `dataView`.
22094 *
22095 * @private
22096 * @param {Object} dataView The data view to clone.
22097 * @param {boolean} [isDeep] Specify a deep clone.
22098 * @returns {Object} Returns the cloned data view.
22099 */
22100 function cloneDataView(dataView, isDeep) {
22101 var buffer = isDeep ? _cloneArrayBuffer(dataView.buffer) : dataView.buffer;
22102 return new dataView.constructor(buffer, dataView.byteOffset, dataView.byteLength);
22103 }
22104
22105 var _cloneDataView = cloneDataView;
22106
22107 /** Used to match `RegExp` flags from their coerced string values. */
22108 var reFlags = /\w*$/;
22109
22110 /**
22111 * Creates a clone of `regexp`.
22112 *
22113 * @private
22114 * @param {Object} regexp The regexp to clone.
22115 * @returns {Object} Returns the cloned regexp.
22116 */
22117 function cloneRegExp(regexp) {
22118 var result = new regexp.constructor(regexp.source, reFlags.exec(regexp));
22119 result.lastIndex = regexp.lastIndex;
22120 return result;
22121 }
22122
22123 var _cloneRegExp = cloneRegExp;
22124
22125 /** Used to convert symbols to primitives and strings. */
22126 var symbolProto$2 = _Symbol ? _Symbol.prototype : undefined,
22127 symbolValueOf$1 = symbolProto$2 ? symbolProto$2.valueOf : undefined;
22128
22129 /**
22130 * Creates a clone of the `symbol` object.
22131 *
22132 * @private
22133 * @param {Object} symbol The symbol object to clone.
22134 * @returns {Object} Returns the cloned symbol object.
22135 */
22136 function cloneSymbol(symbol) {
22137 return symbolValueOf$1 ? Object(symbolValueOf$1.call(symbol)) : {};
22138 }
22139
22140 var _cloneSymbol = cloneSymbol;
22141
22142 /**
22143 * Creates a clone of `typedArray`.
22144 *
22145 * @private
22146 * @param {Object} typedArray The typed array to clone.
22147 * @param {boolean} [isDeep] Specify a deep clone.
22148 * @returns {Object} Returns the cloned typed array.
22149 */
22150 function cloneTypedArray(typedArray, isDeep) {
22151 var buffer = isDeep ? _cloneArrayBuffer(typedArray.buffer) : typedArray.buffer;
22152 return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length);
22153 }
22154
22155 var _cloneTypedArray = cloneTypedArray;
22156
22157 /** `Object#toString` result references. */
22158 var boolTag$2 = '[object Boolean]',
22159 dateTag$2 = '[object Date]',
22160 mapTag$4 = '[object Map]',
22161 numberTag$2 = '[object Number]',
22162 regexpTag$2 = '[object RegExp]',
22163 setTag$4 = '[object Set]',
22164 stringTag$2 = '[object String]',
22165 symbolTag$2 = '[object Symbol]';
22166
22167 var arrayBufferTag$2 = '[object ArrayBuffer]',
22168 dataViewTag$3 = '[object DataView]',
22169 float32Tag$1 = '[object Float32Array]',
22170 float64Tag$1 = '[object Float64Array]',
22171 int8Tag$1 = '[object Int8Array]',
22172 int16Tag$1 = '[object Int16Array]',
22173 int32Tag$1 = '[object Int32Array]',
22174 uint8Tag$1 = '[object Uint8Array]',
22175 uint8ClampedTag$1 = '[object Uint8ClampedArray]',
22176 uint16Tag$1 = '[object Uint16Array]',
22177 uint32Tag$1 = '[object Uint32Array]';
22178
22179 /**
22180 * Initializes an object clone based on its `toStringTag`.
22181 *
22182 * **Note:** This function only supports cloning values with tags of
22183 * `Boolean`, `Date`, `Error`, `Map`, `Number`, `RegExp`, `Set`, or `String`.
22184 *
22185 * @private
22186 * @param {Object} object The object to clone.
22187 * @param {string} tag The `toStringTag` of the object to clone.
22188 * @param {boolean} [isDeep] Specify a deep clone.
22189 * @returns {Object} Returns the initialized clone.
22190 */
22191 function initCloneByTag(object, tag, isDeep) {
22192 var Ctor = object.constructor;
22193 switch (tag) {
22194 case arrayBufferTag$2:
22195 return _cloneArrayBuffer(object);
22196
22197 case boolTag$2:
22198 case dateTag$2:
22199 return new Ctor(+object);
22200
22201 case dataViewTag$3:
22202 return _cloneDataView(object, isDeep);
22203
22204 case float32Tag$1: case float64Tag$1:
22205 case int8Tag$1: case int16Tag$1: case int32Tag$1:
22206 case uint8Tag$1: case uint8ClampedTag$1: case uint16Tag$1: case uint32Tag$1:
22207 return _cloneTypedArray(object, isDeep);
22208
22209 case mapTag$4:
22210 return new Ctor;
22211
22212 case numberTag$2:
22213 case stringTag$2:
22214 return new Ctor(object);
22215
22216 case regexpTag$2:
22217 return _cloneRegExp(object);
22218
22219 case setTag$4:
22220 return new Ctor;
22221
22222 case symbolTag$2:
22223 return _cloneSymbol(object);
22224 }
22225 }
22226
22227 var _initCloneByTag = initCloneByTag;
22228
22229 /** Built-in value references. */
22230 var objectCreate = Object.create;
22231
22232 /**
22233 * The base implementation of `_.create` without support for assigning
22234 * properties to the created object.
22235 *
22236 * @private
22237 * @param {Object} proto The object to inherit from.
22238 * @returns {Object} Returns the new object.
22239 */
22240 var baseCreate = (function() {
22241 function object() {}
22242 return function(proto) {
22243 if (!isObject_1$1(proto)) {
22244 return {};
22245 }
22246 if (objectCreate) {
22247 return objectCreate(proto);
22248 }
22249 object.prototype = proto;
22250 var result = new object;
22251 object.prototype = undefined;
22252 return result;
22253 };
22254 }());
22255
22256 var _baseCreate = baseCreate;
22257
22258 /**
22259 * Initializes an object clone.
22260 *
22261 * @private
22262 * @param {Object} object The object to clone.
22263 * @returns {Object} Returns the initialized clone.
22264 */
22265 function initCloneObject(object) {
22266 return (typeof object.constructor == 'function' && !_isPrototype(object))
22267 ? _baseCreate(_getPrototype(object))
22268 : {};
22269 }
22270
22271 var _initCloneObject = initCloneObject;
22272
22273 /** `Object#toString` result references. */
22274 var mapTag$5 = '[object Map]';
22275
22276 /**
22277 * The base implementation of `_.isMap` without Node.js optimizations.
22278 *
22279 * @private
22280 * @param {*} value The value to check.
22281 * @returns {boolean} Returns `true` if `value` is a map, else `false`.
22282 */
22283 function baseIsMap(value) {
22284 return isObjectLike_1(value) && _getTag(value) == mapTag$5;
22285 }
22286
22287 var _baseIsMap = baseIsMap;
22288
22289 /* Node.js helper references. */
22290 var nodeIsMap = _nodeUtil && _nodeUtil.isMap;
22291
22292 /**
22293 * Checks if `value` is classified as a `Map` object.
22294 *
22295 * @static
22296 * @memberOf _
22297 * @since 4.3.0
22298 * @category Lang
22299 * @param {*} value The value to check.
22300 * @returns {boolean} Returns `true` if `value` is a map, else `false`.
22301 * @example
22302 *
22303 * _.isMap(new Map);
22304 * // => true
22305 *
22306 * _.isMap(new WeakMap);
22307 * // => false
22308 */
22309 var isMap = nodeIsMap ? _baseUnary(nodeIsMap) : _baseIsMap;
22310
22311 var isMap_1 = isMap;
22312
22313 /** `Object#toString` result references. */
22314 var setTag$5 = '[object Set]';
22315
22316 /**
22317 * The base implementation of `_.isSet` without Node.js optimizations.
22318 *
22319 * @private
22320 * @param {*} value The value to check.
22321 * @returns {boolean} Returns `true` if `value` is a set, else `false`.
22322 */
22323 function baseIsSet(value) {
22324 return isObjectLike_1(value) && _getTag(value) == setTag$5;
22325 }
22326
22327 var _baseIsSet = baseIsSet;
22328
22329 /* Node.js helper references. */
22330 var nodeIsSet = _nodeUtil && _nodeUtil.isSet;
22331
22332 /**
22333 * Checks if `value` is classified as a `Set` object.
22334 *
22335 * @static
22336 * @memberOf _
22337 * @since 4.3.0
22338 * @category Lang
22339 * @param {*} value The value to check.
22340 * @returns {boolean} Returns `true` if `value` is a set, else `false`.
22341 * @example
22342 *
22343 * _.isSet(new Set);
22344 * // => true
22345 *
22346 * _.isSet(new WeakSet);
22347 * // => false
22348 */
22349 var isSet = nodeIsSet ? _baseUnary(nodeIsSet) : _baseIsSet;
22350
22351 var isSet_1 = isSet;
22352
22353 /** Used to compose bitmasks for cloning. */
22354 var CLONE_DEEP_FLAG = 1,
22355 CLONE_FLAT_FLAG = 2,
22356 CLONE_SYMBOLS_FLAG = 4;
22357
22358 /** `Object#toString` result references. */
22359 var argsTag$3 = '[object Arguments]',
22360 arrayTag$2 = '[object Array]',
22361 boolTag$3 = '[object Boolean]',
22362 dateTag$3 = '[object Date]',
22363 errorTag$2 = '[object Error]',
22364 funcTag$2 = '[object Function]',
22365 genTag$1 = '[object GeneratorFunction]',
22366 mapTag$6 = '[object Map]',
22367 numberTag$3 = '[object Number]',
22368 objectTag$4 = '[object Object]',
22369 regexpTag$3 = '[object RegExp]',
22370 setTag$6 = '[object Set]',
22371 stringTag$3 = '[object String]',
22372 symbolTag$3 = '[object Symbol]',
22373 weakMapTag$2 = '[object WeakMap]';
22374
22375 var arrayBufferTag$3 = '[object ArrayBuffer]',
22376 dataViewTag$4 = '[object DataView]',
22377 float32Tag$2 = '[object Float32Array]',
22378 float64Tag$2 = '[object Float64Array]',
22379 int8Tag$2 = '[object Int8Array]',
22380 int16Tag$2 = '[object Int16Array]',
22381 int32Tag$2 = '[object Int32Array]',
22382 uint8Tag$2 = '[object Uint8Array]',
22383 uint8ClampedTag$2 = '[object Uint8ClampedArray]',
22384 uint16Tag$2 = '[object Uint16Array]',
22385 uint32Tag$2 = '[object Uint32Array]';
22386
22387 /** Used to identify `toStringTag` values supported by `_.clone`. */
22388 var cloneableTags = {};
22389 cloneableTags[argsTag$3] = cloneableTags[arrayTag$2] =
22390 cloneableTags[arrayBufferTag$3] = cloneableTags[dataViewTag$4] =
22391 cloneableTags[boolTag$3] = cloneableTags[dateTag$3] =
22392 cloneableTags[float32Tag$2] = cloneableTags[float64Tag$2] =
22393 cloneableTags[int8Tag$2] = cloneableTags[int16Tag$2] =
22394 cloneableTags[int32Tag$2] = cloneableTags[mapTag$6] =
22395 cloneableTags[numberTag$3] = cloneableTags[objectTag$4] =
22396 cloneableTags[regexpTag$3] = cloneableTags[setTag$6] =
22397 cloneableTags[stringTag$3] = cloneableTags[symbolTag$3] =
22398 cloneableTags[uint8Tag$2] = cloneableTags[uint8ClampedTag$2] =
22399 cloneableTags[uint16Tag$2] = cloneableTags[uint32Tag$2] = true;
22400 cloneableTags[errorTag$2] = cloneableTags[funcTag$2] =
22401 cloneableTags[weakMapTag$2] = false;
22402
22403 /**
22404 * The base implementation of `_.clone` and `_.cloneDeep` which tracks
22405 * traversed objects.
22406 *
22407 * @private
22408 * @param {*} value The value to clone.
22409 * @param {boolean} bitmask The bitmask flags.
22410 * 1 - Deep clone
22411 * 2 - Flatten inherited properties
22412 * 4 - Clone symbols
22413 * @param {Function} [customizer] The function to customize cloning.
22414 * @param {string} [key] The key of `value`.
22415 * @param {Object} [object] The parent object of `value`.
22416 * @param {Object} [stack] Tracks traversed objects and their clone counterparts.
22417 * @returns {*} Returns the cloned value.
22418 */
22419 function baseClone(value, bitmask, customizer, key, object, stack) {
22420 var result,
22421 isDeep = bitmask & CLONE_DEEP_FLAG,
22422 isFlat = bitmask & CLONE_FLAT_FLAG,
22423 isFull = bitmask & CLONE_SYMBOLS_FLAG;
22424
22425 if (customizer) {
22426 result = object ? customizer(value, key, object, stack) : customizer(value);
22427 }
22428 if (result !== undefined) {
22429 return result;
22430 }
22431 if (!isObject_1$1(value)) {
22432 return value;
22433 }
22434 var isArr = isArray_1(value);
22435 if (isArr) {
22436 result = _initCloneArray(value);
22437 if (!isDeep) {
22438 return _copyArray(value, result);
22439 }
22440 } else {
22441 var tag = _getTag(value),
22442 isFunc = tag == funcTag$2 || tag == genTag$1;
22443
22444 if (isBuffer_1(value)) {
22445 return _cloneBuffer(value, isDeep);
22446 }
22447 if (tag == objectTag$4 || tag == argsTag$3 || (isFunc && !object)) {
22448 result = (isFlat || isFunc) ? {} : _initCloneObject(value);
22449 if (!isDeep) {
22450 return isFlat
22451 ? _copySymbolsIn(value, _baseAssignIn(result, value))
22452 : _copySymbols(value, _baseAssign(result, value));
22453 }
22454 } else {
22455 if (!cloneableTags[tag]) {
22456 return object ? value : {};
22457 }
22458 result = _initCloneByTag(value, tag, isDeep);
22459 }
22460 }
22461 // Check for circular references and return its corresponding clone.
22462 stack || (stack = new _Stack);
22463 var stacked = stack.get(value);
22464 if (stacked) {
22465 return stacked;
22466 }
22467 stack.set(value, result);
22468
22469 if (isSet_1(value)) {
22470 value.forEach(function(subValue) {
22471 result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));
22472 });
22473
22474 return result;
22475 }
22476
22477 if (isMap_1(value)) {
22478 value.forEach(function(subValue, key) {
22479 result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));
22480 });
22481
22482 return result;
22483 }
22484
22485 var keysFunc = isFull
22486 ? (isFlat ? _getAllKeysIn : _getAllKeys)
22487 : (isFlat ? keysIn : keys_1);
22488
22489 var props = isArr ? undefined : keysFunc(value);
22490 _arrayEach(props || value, function(subValue, key) {
22491 if (props) {
22492 key = subValue;
22493 subValue = value[key];
22494 }
22495 // Recursively populate clone (susceptible to call stack limits).
22496 _assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));
22497 });
22498 return result;
22499 }
22500
22501 var _baseClone = baseClone;
22502
22503 /** Used to compose bitmasks for cloning. */
22504 var CLONE_DEEP_FLAG$1 = 1,
22505 CLONE_SYMBOLS_FLAG$1 = 4;
22506
22507 /**
22508 * This method is like `_.clone` except that it recursively clones `value`.
22509 *
22510 * @static
22511 * @memberOf _
22512 * @since 1.0.0
22513 * @category Lang
22514 * @param {*} value The value to recursively clone.
22515 * @returns {*} Returns the deep cloned value.
22516 * @see _.clone
22517 * @example
22518 *
22519 * var objects = [{ 'a': 1 }, { 'b': 2 }];
22520 *
22521 * var deep = _.cloneDeep(objects);
22522 * console.log(deep[0] === objects[0]);
22523 * // => false
22524 */
22525 function cloneDeep(value) {
22526 return _baseClone(value, CLONE_DEEP_FLAG$1 | CLONE_SYMBOLS_FLAG$1);
22527 }
22528
22529 var cloneDeep_1 = cloneDeep;
22530
22531 /**
22532 * Creates a `_.find` or `_.findLast` function.
22533 *
22534 * @private
22535 * @param {Function} findIndexFunc The function to find the collection index.
22536 * @returns {Function} Returns the new find function.
22537 */
22538 function createFind(findIndexFunc) {
22539 return function(collection, predicate, fromIndex) {
22540 var iterable = Object(collection);
22541 if (!isArrayLike_1(collection)) {
22542 var iteratee = _baseIteratee(predicate, 3);
22543 collection = keys_1(collection);
22544 predicate = function(key) { return iteratee(iterable[key], key, iterable); };
22545 }
22546 var index = findIndexFunc(collection, predicate, fromIndex);
22547 return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined;
22548 };
22549 }
22550
22551 var _createFind = createFind;
22552
22553 /**
22554 * The base implementation of `_.findIndex` and `_.findLastIndex` without
22555 * support for iteratee shorthands.
22556 *
22557 * @private
22558 * @param {Array} array The array to inspect.
22559 * @param {Function} predicate The function invoked per iteration.
22560 * @param {number} fromIndex The index to search from.
22561 * @param {boolean} [fromRight] Specify iterating from right to left.
22562 * @returns {number} Returns the index of the matched value, else `-1`.
22563 */
22564 function baseFindIndex(array, predicate, fromIndex, fromRight) {
22565 var length = array.length,
22566 index = fromIndex + (fromRight ? 1 : -1);
22567
22568 while ((fromRight ? index-- : ++index < length)) {
22569 if (predicate(array[index], index, array)) {
22570 return index;
22571 }
22572 }
22573 return -1;
22574 }
22575
22576 var _baseFindIndex = baseFindIndex;
22577
22578 /** Used as references for various `Number` constants. */
22579 var NAN = 0 / 0;
22580
22581 /** Used to match leading and trailing whitespace. */
22582 var reTrim = /^\s+|\s+$/g;
22583
22584 /** Used to detect bad signed hexadecimal string values. */
22585 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
22586
22587 /** Used to detect binary string values. */
22588 var reIsBinary = /^0b[01]+$/i;
22589
22590 /** Used to detect octal string values. */
22591 var reIsOctal = /^0o[0-7]+$/i;
22592
22593 /** Built-in method references without a dependency on `root`. */
22594 var freeParseInt = parseInt;
22595
22596 /**
22597 * Converts `value` to a number.
22598 *
22599 * @static
22600 * @memberOf _
22601 * @since 4.0.0
22602 * @category Lang
22603 * @param {*} value The value to process.
22604 * @returns {number} Returns the number.
22605 * @example
22606 *
22607 * _.toNumber(3.2);
22608 * // => 3.2
22609 *
22610 * _.toNumber(Number.MIN_VALUE);
22611 * // => 5e-324
22612 *
22613 * _.toNumber(Infinity);
22614 * // => Infinity
22615 *
22616 * _.toNumber('3.2');
22617 * // => 3.2
22618 */
22619 function toNumber(value) {
22620 if (typeof value == 'number') {
22621 return value;
22622 }
22623 if (isSymbol_1(value)) {
22624 return NAN;
22625 }
22626 if (isObject_1$1(value)) {
22627 var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
22628 value = isObject_1$1(other) ? (other + '') : other;
22629 }
22630 if (typeof value != 'string') {
22631 return value === 0 ? value : +value;
22632 }
22633 value = value.replace(reTrim, '');
22634 var isBinary = reIsBinary.test(value);
22635 return (isBinary || reIsOctal.test(value))
22636 ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
22637 : (reIsBadHex.test(value) ? NAN : +value);
22638 }
22639
22640 var toNumber_1 = toNumber;
22641
22642 /** Used as references for various `Number` constants. */
22643 var INFINITY$2 = 1 / 0,
22644 MAX_INTEGER = 1.7976931348623157e+308;
22645
22646 /**
22647 * Converts `value` to a finite number.
22648 *
22649 * @static
22650 * @memberOf _
22651 * @since 4.12.0
22652 * @category Lang
22653 * @param {*} value The value to convert.
22654 * @returns {number} Returns the converted number.
22655 * @example
22656 *
22657 * _.toFinite(3.2);
22658 * // => 3.2
22659 *
22660 * _.toFinite(Number.MIN_VALUE);
22661 * // => 5e-324
22662 *
22663 * _.toFinite(Infinity);
22664 * // => 1.7976931348623157e+308
22665 *
22666 * _.toFinite('3.2');
22667 * // => 3.2
22668 */
22669 function toFinite(value) {
22670 if (!value) {
22671 return value === 0 ? value : 0;
22672 }
22673 value = toNumber_1(value);
22674 if (value === INFINITY$2 || value === -INFINITY$2) {
22675 var sign = (value < 0 ? -1 : 1);
22676 return sign * MAX_INTEGER;
22677 }
22678 return value === value ? value : 0;
22679 }
22680
22681 var toFinite_1 = toFinite;
22682
22683 /**
22684 * Converts `value` to an integer.
22685 *
22686 * **Note:** This method is loosely based on
22687 * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
22688 *
22689 * @static
22690 * @memberOf _
22691 * @since 4.0.0
22692 * @category Lang
22693 * @param {*} value The value to convert.
22694 * @returns {number} Returns the converted integer.
22695 * @example
22696 *
22697 * _.toInteger(3.2);
22698 * // => 3
22699 *
22700 * _.toInteger(Number.MIN_VALUE);
22701 * // => 0
22702 *
22703 * _.toInteger(Infinity);
22704 * // => 1.7976931348623157e+308
22705 *
22706 * _.toInteger('3.2');
22707 * // => 3
22708 */
22709 function toInteger(value) {
22710 var result = toFinite_1(value),
22711 remainder = result % 1;
22712
22713 return result === result ? (remainder ? result - remainder : result) : 0;
22714 }
22715
22716 var toInteger_1 = toInteger;
22717
22718 /* Built-in method references for those with the same name as other `lodash` methods. */
22719 var nativeMax = Math.max;
22720
22721 /**
22722 * This method is like `_.find` except that it returns the index of the first
22723 * element `predicate` returns truthy for instead of the element itself.
22724 *
22725 * @static
22726 * @memberOf _
22727 * @since 1.1.0
22728 * @category Array
22729 * @param {Array} array The array to inspect.
22730 * @param {Function} [predicate=_.identity] The function invoked per iteration.
22731 * @param {number} [fromIndex=0] The index to search from.
22732 * @returns {number} Returns the index of the found element, else `-1`.
22733 * @example
22734 *
22735 * var users = [
22736 * { 'user': 'barney', 'active': false },
22737 * { 'user': 'fred', 'active': false },
22738 * { 'user': 'pebbles', 'active': true }
22739 * ];
22740 *
22741 * _.findIndex(users, function(o) { return o.user == 'barney'; });
22742 * // => 0
22743 *
22744 * // The `_.matches` iteratee shorthand.
22745 * _.findIndex(users, { 'user': 'fred', 'active': false });
22746 * // => 1
22747 *
22748 * // The `_.matchesProperty` iteratee shorthand.
22749 * _.findIndex(users, ['active', false]);
22750 * // => 0
22751 *
22752 * // The `_.property` iteratee shorthand.
22753 * _.findIndex(users, 'active');
22754 * // => 2
22755 */
22756 function findIndex(array, predicate, fromIndex) {
22757 var length = array == null ? 0 : array.length;
22758 if (!length) {
22759 return -1;
22760 }
22761 var index = fromIndex == null ? 0 : toInteger_1(fromIndex);
22762 if (index < 0) {
22763 index = nativeMax(length + index, 0);
22764 }
22765 return _baseFindIndex(array, _baseIteratee(predicate, 3), index);
22766 }
22767
22768 var findIndex_1 = findIndex;
22769
22770 /**
22771 * Iterates over elements of `collection`, returning the first element
22772 * `predicate` returns truthy for. The predicate is invoked with three
22773 * arguments: (value, index|key, collection).
22774 *
22775 * @static
22776 * @memberOf _
22777 * @since 0.1.0
22778 * @category Collection
22779 * @param {Array|Object} collection The collection to inspect.
22780 * @param {Function} [predicate=_.identity] The function invoked per iteration.
22781 * @param {number} [fromIndex=0] The index to search from.
22782 * @returns {*} Returns the matched element, else `undefined`.
22783 * @example
22784 *
22785 * var users = [
22786 * { 'user': 'barney', 'age': 36, 'active': true },
22787 * { 'user': 'fred', 'age': 40, 'active': false },
22788 * { 'user': 'pebbles', 'age': 1, 'active': true }
22789 * ];
22790 *
22791 * _.find(users, function(o) { return o.age < 40; });
22792 * // => object for 'barney'
22793 *
22794 * // The `_.matches` iteratee shorthand.
22795 * _.find(users, { 'age': 1, 'active': true });
22796 * // => object for 'pebbles'
22797 *
22798 * // The `_.matchesProperty` iteratee shorthand.
22799 * _.find(users, ['active', false]);
22800 * // => object for 'fred'
22801 *
22802 * // The `_.property` iteratee shorthand.
22803 * _.find(users, 'active');
22804 * // => object for 'barney'
22805 */
22806 var find = _createFind(findIndex_1);
22807
22808 var find_1 = find;
22809
22810 // IMClient
22811 var UNREAD_MESSAGES_COUNT_UPDATE = 'unreadmessagescountupdate';
22812 var CLOSE = 'close';
22813 var CONFLICT = 'conflict';
22814 var CONVERSATION_INFO_UPDATED = 'conversationinfoupdated';
22815 var UNHANDLED_MESSAGE = 'unhandledmessage'; // shared
22816
22817 var INVITED = 'invited';
22818 var KICKED = 'kicked';
22819 var MEMBERS_JOINED = 'membersjoined';
22820 var MEMBERS_LEFT = 'membersleft';
22821 var MEMBER_INFO_UPDATED = 'memberinfoupdated';
22822 var BLOCKED = 'blocked';
22823 var UNBLOCKED = 'unblocked';
22824 var MEMBERS_BLOCKED = 'membersblocked';
22825 var MEMBERS_UNBLOCKED = 'membersunblocked';
22826 var MUTED = 'muted';
22827 var UNMUTED = 'unmuted';
22828 var MEMBERS_MUTED = 'membersmuted';
22829 var MEMBERS_UNMUTED = 'membersunmuted';
22830 var MESSAGE$1 = 'message';
22831 var MESSAGE_RECALL = 'messagerecall';
22832 var MESSAGE_UPDATE = 'messageupdate'; // Conversation
22833
22834 var LAST_DELIVERED_AT_UPDATE = 'lastdeliveredatupdate';
22835 var LAST_READ_AT_UPDATE = 'lastreadatupdate';
22836 var INFO_UPDATED = 'infoupdated';
22837
22838 var Event = /*#__PURE__*/Object.freeze({
22839 UNREAD_MESSAGES_COUNT_UPDATE: UNREAD_MESSAGES_COUNT_UPDATE,
22840 CLOSE: CLOSE,
22841 CONFLICT: CONFLICT,
22842 CONVERSATION_INFO_UPDATED: CONVERSATION_INFO_UPDATED,
22843 UNHANDLED_MESSAGE: UNHANDLED_MESSAGE,
22844 INVITED: INVITED,
22845 KICKED: KICKED,
22846 MEMBERS_JOINED: MEMBERS_JOINED,
22847 MEMBERS_LEFT: MEMBERS_LEFT,
22848 MEMBER_INFO_UPDATED: MEMBER_INFO_UPDATED,
22849 BLOCKED: BLOCKED,
22850 UNBLOCKED: UNBLOCKED,
22851 MEMBERS_BLOCKED: MEMBERS_BLOCKED,
22852 MEMBERS_UNBLOCKED: MEMBERS_UNBLOCKED,
22853 MUTED: MUTED,
22854 UNMUTED: UNMUTED,
22855 MEMBERS_MUTED: MEMBERS_MUTED,
22856 MEMBERS_UNMUTED: MEMBERS_UNMUTED,
22857 MESSAGE: MESSAGE$1,
22858 MESSAGE_RECALL: MESSAGE_RECALL,
22859 MESSAGE_UPDATE: MESSAGE_UPDATE,
22860 LAST_DELIVERED_AT_UPDATE: LAST_DELIVERED_AT_UPDATE,
22861 LAST_READ_AT_UPDATE: LAST_READ_AT_UPDATE,
22862 INFO_UPDATED: INFO_UPDATED
22863 });
22864
22865 var _rMessageStatus;
22866 /**
22867 * 消息状态枚举
22868 * @enum {Symbol}
22869 * @since 3.2.0
22870 * @memberof module:leancloud-realtime
22871 */
22872
22873 var MessageStatus = {
22874 /** 初始状态、未知状态 */
22875 NONE: Symbol('none'),
22876
22877 /** 正在发送 */
22878 SENDING: Symbol('sending'),
22879
22880 /** 已发送 */
22881 SENT: Symbol('sent'),
22882
22883 /** 已送达 */
22884 DELIVERED: Symbol('delivered'),
22885
22886 /** 发送失败 */
22887 FAILED: Symbol('failed')
22888 };
22889 Object.freeze(MessageStatus);
22890 var rMessageStatus = (_rMessageStatus = {}, _defineProperty(_rMessageStatus, MessageStatus.NONE, true), _defineProperty(_rMessageStatus, MessageStatus.SENDING, true), _defineProperty(_rMessageStatus, MessageStatus.SENT, true), _defineProperty(_rMessageStatus, MessageStatus.DELIVERED, true), _defineProperty(_rMessageStatus, MessageStatus.READ, true), _defineProperty(_rMessageStatus, MessageStatus.FAILED, true), _rMessageStatus);
22891
22892 var Message =
22893 /*#__PURE__*/
22894 function () {
22895 /**
22896 * @implements AVMessage
22897 * @param {Object|String|ArrayBuffer} content 消息内容
22898 */
22899 function Message(content) {
22900 Object.assign(this, {
22901 content: content
22902 }, {
22903 /**
22904 * @type {String}
22905 * @memberof Message#
22906 */
22907 id: v4_1(),
22908
22909 /**
22910 * 消息所在的 conversation id
22911 * @memberof Message#
22912 * @type {String?}
22913 */
22914 cid: null,
22915
22916 /**
22917 * 消息发送时间
22918 * @memberof Message#
22919 * @type {Date}
22920 */
22921 timestamp: new Date(),
22922
22923 /**
22924 * 消息发送者
22925 * @memberof Message#
22926 * @type {String}
22927 */
22928 from: undefined,
22929
22930 /**
22931 * 消息提及的用户
22932 * @since 4.0.0
22933 * @memberof Message#
22934 * @type {String[]}
22935 */
22936 mentionList: [],
22937
22938 /**
22939 * 消息是否提及了所有人
22940 * @since 4.0.0
22941 * @memberof Message#
22942 * @type {Boolean}
22943 */
22944 mentionedAll: false,
22945 _mentioned: false
22946 });
22947
22948 this._setStatus(MessageStatus.NONE);
22949 }
22950 /**
22951 * 将当前消息的内容序列化为 JSON 对象
22952 * @private
22953 * @return {Object}
22954 */
22955
22956
22957 var _proto = Message.prototype;
22958
22959 _proto.getPayload = function getPayload() {
22960 return this.content;
22961 };
22962
22963 _proto._toJSON = function _toJSON() {
22964 var id = this.id,
22965 cid = this.cid,
22966 from = this.from,
22967 timestamp = this.timestamp,
22968 deliveredAt = this.deliveredAt,
22969 updatedAt = this.updatedAt,
22970 mentionList = this.mentionList,
22971 mentionedAll = this.mentionedAll,
22972 mentioned = this.mentioned;
22973 return {
22974 id: id,
22975 cid: cid,
22976 from: from,
22977 timestamp: timestamp,
22978 deliveredAt: deliveredAt,
22979 updatedAt: updatedAt,
22980 mentionList: mentionList,
22981 mentionedAll: mentionedAll,
22982 mentioned: mentioned
22983 };
22984 }
22985 /**
22986 * 返回 JSON 格式的消息
22987 * @return {Object} 返回值是一个 plain Object
22988 */
22989 ;
22990
22991 _proto.toJSON = function toJSON() {
22992 return _objectSpread({}, this._toJSON(), {
22993 data: this.content
22994 });
22995 }
22996 /**
22997 * 返回 JSON 格式的消息,与 toJSON 不同的是,该对象包含了完整的信息,可以通过 {@link IMClient#parseMessage} 反序列化。
22998 * @return {Object} 返回值是一个 plain Object
22999 * @since 4.0.0
23000 */
23001 ;
23002
23003 _proto.toFullJSON = function toFullJSON() {
23004 var content = this.content,
23005 id = this.id,
23006 cid = this.cid,
23007 from = this.from,
23008 timestamp = this.timestamp,
23009 deliveredAt = this.deliveredAt,
23010 _updatedAt = this._updatedAt,
23011 mentionList = this.mentionList,
23012 mentionedAll = this.mentionedAll;
23013 return {
23014 data: content,
23015 id: id,
23016 cid: cid,
23017 from: from,
23018 timestamp: getTime(timestamp),
23019 deliveredAt: getTime(deliveredAt),
23020 updatedAt: getTime(_updatedAt),
23021 mentionList: mentionList,
23022 mentionedAll: mentionedAll
23023 };
23024 }
23025 /**
23026 * 消息状态,值为 {@link module:leancloud-realtime.MessageStatus} 之一
23027 * @type {Symbol}
23028 * @readonly
23029 * @since 3.2.0
23030 */
23031 ;
23032
23033 _proto._setStatus = function _setStatus(status) {
23034 if (!rMessageStatus[status]) {
23035 throw new Error('Invalid message status');
23036 }
23037
23038 this._status = status;
23039 };
23040
23041 _proto._updateMentioned = function _updateMentioned(client) {
23042 this._mentioned = this.from !== client && (this.mentionedAll || this.mentionList.indexOf(client) > -1);
23043 }
23044 /**
23045 * 获取提及用户列表
23046 * @since 4.0.0
23047 * @return {String[]} 提及用户的 id 列表
23048 */
23049 ;
23050
23051 _proto.getMentionList = function getMentionList() {
23052 return this.mentionList;
23053 }
23054 /**
23055 * 设置提及用户列表
23056 * @since 4.0.0
23057 * @param {String[]} clients 提及用户的 id 列表
23058 * @return {this} self
23059 */
23060 ;
23061
23062 _proto.setMentionList = function setMentionList(clients) {
23063 this.mentionList = ensureArray(clients);
23064 return this;
23065 }
23066 /**
23067 * 设置是否提及所有人
23068 * @since 4.0.0
23069 * @param {Boolean} [value=true]
23070 * @return {this} self
23071 */
23072 ;
23073
23074 _proto.mentionAll = function mentionAll() {
23075 var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
23076 this.mentionedAll = Boolean(value);
23077 return this;
23078 }
23079 /**
23080 * 判断给定的内容是否是有效的 Message,
23081 * 该方法始终返回 true
23082 * @private
23083 * @returns {Boolean}
23084 * @implements AVMessage.validate
23085 */
23086 ;
23087
23088 Message.validate = function validate() {
23089 return true;
23090 }
23091 /**
23092 * 解析处理消息内容
23093 * <pre>
23094 * 如果子类提供了 message,返回该 message
23095 * 如果没有提供,将 json 作为 content 实例化一个 Message
23096 * @private
23097 * @param {Object} json json 格式的消息内容
23098 * @param {Message} message 子类提供的 message
23099 * @return {Message}
23100 * @implements AVMessage.parse
23101 */
23102 ;
23103
23104 Message.parse = function parse(json, message) {
23105 return message || new this(json);
23106 };
23107
23108 _createClass(Message, [{
23109 key: "status",
23110 get: function get() {
23111 return this._status;
23112 }
23113 }, {
23114 key: "timestamp",
23115 get: function get() {
23116 return this._timestamp;
23117 },
23118 set: function set(value) {
23119 this._timestamp = decodeDate(value);
23120 }
23121 /**
23122 * 消息送达时间
23123 * @type {?Date}
23124 */
23125
23126 }, {
23127 key: "deliveredAt",
23128 get: function get() {
23129 return this._deliveredAt;
23130 },
23131 set: function set(value) {
23132 this._deliveredAt = decodeDate(value);
23133 }
23134 /**
23135 * 消息修改或撤回时间,可以通过比较其与消息的 timestamp 是否相等判断消息是否被修改过或撤回过。
23136 * @type {Date}
23137 * @since 3.5.0
23138 */
23139
23140 }, {
23141 key: "updatedAt",
23142 get: function get() {
23143 return this._updatedAt || this.timestamp;
23144 },
23145 set: function set(value) {
23146 this._updatedAt = decodeDate(value);
23147 }
23148 /**
23149 * 当前用户是否在该消息中被提及
23150 * @type {Boolean}
23151 * @readonly
23152 * @since 4.0.0
23153 */
23154
23155 }, {
23156 key: "mentioned",
23157 get: function get() {
23158 return this._mentioned;
23159 }
23160 }]);
23161
23162 return Message;
23163 }();
23164
23165 /* eslint-disable no-param-reassign */
23166
23167 var messageType = function messageType(type) {
23168 if (typeof type !== 'number') {
23169 throw new TypeError("".concat(type, " is not a Number"));
23170 }
23171
23172 return function (target) {
23173 target.TYPE = type;
23174
23175 target.validate = function (json) {
23176 return json._lctype === type;
23177 };
23178
23179 target.prototype._getType = function () {
23180 return {
23181 _lctype: type
23182 };
23183 };
23184 };
23185 }; // documented in ../index.js
23186
23187 var messageField = function messageField(fields) {
23188 if (typeof fields !== 'string') {
23189 if (!Array.isArray(fields)) {
23190 throw new TypeError("".concat(fields, " is not an Array"));
23191 } else if (fields.some(function (value) {
23192 return typeof value !== 'string';
23193 })) {
23194 throw new TypeError('fields contains non-string typed member');
23195 }
23196 }
23197
23198 return function (target) {
23199 // IE10 Hack:
23200 // static properties in IE10 will not be inherited from super
23201 // search for parse method and assign it manually
23202 var originalCustomFields = isIE10 ? getStaticProperty(target, '_customFields') : target._customFields;
23203 originalCustomFields = Array.isArray(originalCustomFields) ? originalCustomFields : [];
23204 target._customFields = originalCustomFields.concat(fields);
23205 };
23206 }; // IE10 Hack:
23207 // static properties in IE10 will not be inherited from super
23208 // search for parse method and assign it manually
23209
23210 var IE10Compatible = function IE10Compatible(target) {
23211 if (isIE10) {
23212 target.parse = getStaticProperty(target, 'parse');
23213 }
23214 };
23215
23216 var _dec, _class$1;
23217
23218 var // jsdoc-ignore-end
23219
23220 /**
23221 * 所有内置的富媒体消息均继承自本类
23222 * @extends Message
23223 */
23224 TypedMessage = (_dec = messageField(['_lctext', '_lcattrs']), _dec(_class$1 =
23225 /*#__PURE__*/
23226 function (_Message) {
23227 _inheritsLoose(TypedMessage, _Message);
23228
23229 function TypedMessage() {
23230 return _Message.apply(this, arguments) || this;
23231 }
23232
23233 var _proto = TypedMessage.prototype;
23234
23235 /**
23236 * @param {String} text
23237 * @return {this} self
23238 */
23239 _proto.setText = function setText(text) {
23240 this._lctext = text;
23241 return this;
23242 }
23243 /**
23244 * @return {String}
23245 */
23246 ;
23247
23248 _proto.getText = function getText() {
23249 return this._lctext;
23250 }
23251 /**
23252 * @param {Object} attributes
23253 * @return {this} self
23254 */
23255 ;
23256
23257 _proto.setAttributes = function setAttributes(attributes) {
23258 this._lcattrs = attributes;
23259 return this;
23260 }
23261 /**
23262 * @return {Object}
23263 */
23264 ;
23265
23266 _proto.getAttributes = function getAttributes() {
23267 return this._lcattrs;
23268 };
23269
23270 _proto._getCustomFields = function _getCustomFields() {
23271 var _this = this;
23272
23273 var fields = Array.isArray(this.constructor._customFields) ? this.constructor._customFields : [];
23274 return fields.reduce(function (result, field) {
23275 if (typeof field !== 'string') return result;
23276 result[field] = _this[field]; // eslint-disable-line no-param-reassign
23277
23278 return result;
23279 }, {});
23280 }
23281 /* eslint-disable class-methods-use-this */
23282 ;
23283
23284 _proto._getType = function _getType() {
23285 throw new Error('not implemented');
23286 }
23287 /* eslint-enable class-methods-use-this */
23288 ;
23289
23290 _proto.getPayload = function getPayload() {
23291 return compact(Object.assign({
23292 _lctext: this.getText(),
23293 _lcattrs: this.getAttributes()
23294 }, this._getCustomFields(), this._getType()));
23295 };
23296
23297 _proto.toJSON = function toJSON() {
23298 var type = this.type,
23299 text = this.text,
23300 attributes = this.attributes,
23301 summary = this.summary;
23302 return _objectSpread({}, _Message.prototype._toJSON.call(this), {
23303 type: type,
23304 text: text,
23305 attributes: attributes,
23306 summary: summary
23307 });
23308 };
23309
23310 _proto.toFullJSON = function toFullJSON() {
23311 return _objectSpread({}, _Message.prototype.toFullJSON.call(this), {
23312 data: this.getPayload()
23313 });
23314 }
23315 /**
23316 * 解析处理消息内容
23317 * <pre>
23318 * 为给定的 message 设置 text 与 attributes 属性,返回该 message
23319 * 如果子类没有提供 message,new this()
23320 * @protected
23321 * @param {Object} json json 格式的消息内容
23322 * @param {TypedMessage} message 子类提供的 message
23323 * @return {TypedMessage}
23324 * @implements AVMessage.parse
23325 */
23326 ;
23327
23328 TypedMessage.parse = function parse(json) {
23329 var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new this();
23330 message.content = json; // eslint-disable-line no-param-reassign
23331
23332 var customFields = isIE10 ? getStaticProperty(message.constructor, '_customFields') : message.constructor._customFields;
23333 var fields = Array.isArray(customFields) ? customFields : [];
23334 fields = fields.reduce(function (result, field) {
23335 if (typeof field !== 'string') return result;
23336 result[field] = json[field]; // eslint-disable-line no-param-reassign
23337
23338 return result;
23339 }, {});
23340 Object.assign(message, fields);
23341 return _Message.parse.call(this, json, message);
23342 };
23343
23344 _createClass(TypedMessage, [{
23345 key: "type",
23346
23347 /**
23348 * @type {Number}
23349 * @readonly
23350 */
23351 get: function get() {
23352 return this.constructor.TYPE;
23353 }
23354 /** @type {String} */
23355
23356 }, {
23357 key: "text",
23358 set: function set(text) {
23359 return this.setText(text);
23360 },
23361 get: function get() {
23362 return this.getText();
23363 }
23364 /** @type {Object} */
23365
23366 }, {
23367 key: "attributes",
23368 set: function set(attributes) {
23369 return this.setAttributes(attributes);
23370 },
23371 get: function get() {
23372 return this.getAttributes();
23373 }
23374 /**
23375 * 在客户端需要以文本形式展示该消息时显示的文案,
23376 * 如 <code>[红包] 新春快乐</code>。
23377 * 默认值为消息的 text。
23378 * @type {String}
23379 * @readonly
23380 */
23381
23382 }, {
23383 key: "summary",
23384 get: function get() {
23385 return this.text;
23386 }
23387 }]);
23388
23389 return TypedMessage;
23390 }(Message)) || _class$1);
23391
23392 var _dec$1, _class$2;
23393
23394 var // jsdoc-ignore-end
23395
23396 /**
23397 * 已撤回类型消息,当消息被撤回时,SDK 会使用该类型的消息替代原始消息
23398 * @extends TypedMessage
23399 */
23400 RecalledMessage = (_dec$1 = messageType(-127), _dec$1(_class$2 = IE10Compatible(_class$2 =
23401 /*#__PURE__*/
23402 function (_TypedMessage) {
23403 _inheritsLoose(RecalledMessage, _TypedMessage);
23404
23405 function RecalledMessage() {
23406 return _TypedMessage.apply(this, arguments) || this;
23407 }
23408
23409 _createClass(RecalledMessage, [{
23410 key: "summary",
23411
23412 /**
23413 * 在客户端需要以文本形式展示该消息时显示的文案,值为 <code>[该消息已撤回]</code>
23414 * @type {String}
23415 * @readonly
23416 */
23417 // eslint-disable-next-line class-methods-use-this
23418 get: function get() {
23419 return '[该消息已撤回]';
23420 }
23421 }]);
23422
23423 return RecalledMessage;
23424 }(TypedMessage)) || _class$2) || _class$2);
23425
23426 var debug$6 = browser('LC:Conversation');
23427
23428 var serializeMessage = function serializeMessage(message) {
23429 var content = message.getPayload();
23430 var msg;
23431 var binaryMsg;
23432
23433 if (content instanceof ArrayBuffer) {
23434 binaryMsg = content;
23435 } else if (typeof content !== 'string') {
23436 msg = JSON.stringify(content);
23437 } else {
23438 msg = content;
23439 }
23440
23441 return {
23442 msg: msg,
23443 binaryMsg: binaryMsg
23444 };
23445 };
23446
23447 var _LogsCommand$QueryDir = LogsCommand.QueryDirection,
23448 NEW = _LogsCommand$QueryDir.NEW,
23449 OLD = _LogsCommand$QueryDir.OLD;
23450 /**
23451 * 历史消息查询方向枚举
23452 * @enum {Number}
23453 * @since 4.0.0
23454 * @memberof module:leancloud-realtime
23455 */
23456
23457 var MessageQueryDirection = {
23458 /** 从后向前 */
23459 NEW_TO_OLD: OLD,
23460
23461 /** 从前向后 */
23462 OLD_TO_NEW: NEW
23463 };
23464 Object.freeze(MessageQueryDirection);
23465
23466 var ConversationBase =
23467 /*#__PURE__*/
23468 function (_EventEmitter) {
23469 _inheritsLoose(ConversationBase, _EventEmitter);
23470
23471 /**
23472 * @extends EventEmitter
23473 * @private
23474 * @abstract
23475 */
23476 function ConversationBase(_ref, client) {
23477 var _this;
23478
23479 var id = _ref.id,
23480 lastMessageAt = _ref.lastMessageAt,
23481 lastMessage = _ref.lastMessage,
23482 lastDeliveredAt = _ref.lastDeliveredAt,
23483 lastReadAt = _ref.lastReadAt,
23484 _ref$unreadMessagesCo = _ref.unreadMessagesCount,
23485 unreadMessagesCount = _ref$unreadMessagesCo === void 0 ? 0 : _ref$unreadMessagesCo,
23486 _ref$members = _ref.members,
23487 members = _ref$members === void 0 ? [] : _ref$members,
23488 _ref$mentioned = _ref.mentioned,
23489 mentioned = _ref$mentioned === void 0 ? false : _ref$mentioned,
23490 properties = _objectWithoutProperties(_ref, ["id", "lastMessageAt", "lastMessage", "lastDeliveredAt", "lastReadAt", "unreadMessagesCount", "members", "mentioned"]);
23491
23492 _this = _EventEmitter.call(this) || this;
23493 Object.assign(_assertThisInitialized(_this), _objectSpread({
23494 /**
23495 * 对话 id,对应 _Conversation 表中的 objectId
23496 * @memberof ConversationBase#
23497 * @type {String}
23498 */
23499 id: id,
23500
23501 /**
23502 * 最后一条消息时间
23503 * @memberof ConversationBase#
23504 * @type {?Date}
23505 */
23506 lastMessageAt: lastMessageAt,
23507
23508 /**
23509 * 最后一条消息
23510 * @memberof ConversationBase#
23511 * @type {?Message}
23512 */
23513 lastMessage: lastMessage,
23514
23515 /**
23516 * 参与该对话的用户列表
23517 * @memberof ConversationBase#
23518 * @type {String[]}
23519 */
23520 members: members
23521 }, properties));
23522 _this.members = Array.from(new Set(_this.members));
23523 Object.assign(internal(_assertThisInitialized(_this)), {
23524 messagesWaitingForReceipt: {},
23525 lastDeliveredAt: lastDeliveredAt,
23526 lastReadAt: lastReadAt,
23527 unreadMessagesCount: unreadMessagesCount,
23528 mentioned: mentioned
23529 });
23530 _this._client = client;
23531
23532 if (debug$6.enabled) {
23533 values_1(Event).forEach(function (event) {
23534 return _this.on(event, function () {
23535 for (var _len = arguments.length, payload = new Array(_len), _key = 0; _key < _len; _key++) {
23536 payload[_key] = arguments[_key];
23537 }
23538
23539 return _this._debug("".concat(event, " event emitted. %o"), payload);
23540 });
23541 });
23542 } // onConversationCreate hook
23543
23544
23545 applyDecorators(_this._client._plugins.onConversationCreate, _assertThisInitialized(_this));
23546 return _this;
23547 }
23548 /**
23549 * 当前用户是否在该对话的未读消息中被提及
23550 * @type {Boolean}
23551 * @since 4.0.0
23552 */
23553
23554
23555 var _proto = ConversationBase.prototype;
23556
23557 _proto._setUnreadMessagesMentioned = function _setUnreadMessagesMentioned(value) {
23558 internal(this).unreadMessagesMentioned = Boolean(value);
23559 };
23560
23561 _proto._setLastDeliveredAt = function _setLastDeliveredAt(value) {
23562 var date = decodeDate(value);
23563
23564 if (!(date < internal(this).lastDeliveredAt)) {
23565 internal(this).lastDeliveredAt = date;
23566 /**
23567 * 最后消息送达时间更新
23568 * @event ConversationBase#LAST_DELIVERED_AT_UPDATE
23569 * @since 3.4.0
23570 */
23571
23572 this.emit(LAST_DELIVERED_AT_UPDATE);
23573 }
23574 }
23575 /**
23576 * 最后消息被阅读时间,常用来实现发送消息的「已读」标记,可通过 {@link Conversation#fetchReceiptTimestamps} 获取或更新该属性
23577 * @type {?Date}
23578 * @since 3.4.0
23579 */
23580 ;
23581
23582 _proto._setLastReadAt = function _setLastReadAt(value) {
23583 var date = decodeDate(value);
23584
23585 if (!(date < internal(this).lastReadAt)) {
23586 internal(this).lastReadAt = date;
23587 /**
23588 * 最后消息被阅读时间更新
23589 * @event ConversationBase#LAST_READ_AT_UPDATE
23590 * @since 3.4.0
23591 */
23592
23593 this.emit(LAST_READ_AT_UPDATE);
23594 }
23595 }
23596 /**
23597 * 返回 JSON 格式的对话,与 toJSON 不同的是,该对象包含了完整的信息,可以通过 {@link IMClient#parseConversation} 反序列化。
23598 * @return {Object} 返回值是一个 plain Object
23599 * @since 4.0.0
23600 */
23601 ;
23602
23603 _proto.toFullJSON = function toFullJSON() {
23604 var id = this.id,
23605 members = this.members,
23606 lastMessageAt = this.lastMessageAt,
23607 lastDeliveredAt = this.lastDeliveredAt,
23608 lastReadAt = this.lastReadAt,
23609 lastMessage = this.lastMessage,
23610 unreadMessagesCount = this.unreadMessagesCount;
23611 return {
23612 id: id,
23613 members: members,
23614 lastMessageAt: getTime(lastMessageAt),
23615 lastDeliveredAt: getTime(lastDeliveredAt),
23616 lastReadAt: getTime(lastReadAt),
23617 lastMessage: lastMessage ? lastMessage.toFullJSON() : undefined,
23618 unreadMessagesCount: unreadMessagesCount
23619 };
23620 }
23621 /**
23622 * 返回 JSON 格式的对话
23623 * @return {Object} 返回值是一个 plain Object
23624 * @since 4.0.0
23625 */
23626 ;
23627
23628 _proto.toJSON = function toJSON() {
23629 var id = this.id,
23630 members = this.members,
23631 lastMessageAt = this.lastMessageAt,
23632 lastDeliveredAt = this.lastDeliveredAt,
23633 lastReadAt = this.lastReadAt,
23634 lastMessage = this.lastMessage,
23635 unreadMessagesCount = this.unreadMessagesCount,
23636 unreadMessagesMentioned = this.unreadMessagesMentioned;
23637 return {
23638 id: id,
23639 members: members,
23640 lastMessageAt: lastMessageAt,
23641 lastDeliveredAt: lastDeliveredAt,
23642 lastReadAt: lastReadAt,
23643 lastMessage: lastMessage ? lastMessage.toJSON() : undefined,
23644 unreadMessagesCount: unreadMessagesCount,
23645 unreadMessagesMentioned: unreadMessagesMentioned
23646 };
23647 };
23648
23649 _proto._debug = function _debug() {
23650 for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
23651 params[_key2] = arguments[_key2];
23652 }
23653
23654 debug$6.apply(void 0, params.concat(["[".concat(this.id, "]")]));
23655 };
23656
23657 _proto._send = function _send(command) {
23658 var _this$_client;
23659
23660 /* eslint-disable no-param-reassign */
23661 if (command.cmd === null) {
23662 command.cmd = 'conv';
23663 }
23664
23665 if (command.cmd === 'conv' && command.convMessage === null) {
23666 command.convMessage = new ConvCommand();
23667 }
23668
23669 if (command.convMessage && command.convMessage.cid === null) {
23670 command.convMessage.cid = this.id;
23671 }
23672 /* eslint-enable no-param-reassign */
23673
23674
23675 for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
23676 args[_key3 - 1] = arguments[_key3];
23677 }
23678
23679 return (_this$_client = this._client)._send.apply(_this$_client, [command].concat(args));
23680 }
23681 /**
23682 * 发送消息
23683 * @param {Message} message 消息,Message 及其子类的实例
23684 * @param {Object} [options] since v3.3.0,发送选项
23685 * @param {Boolean} [options.transient] since v3.3.1,是否作为暂态消息发送
23686 * @param {Boolean} [options.receipt] 是否需要回执,仅在普通对话中有效
23687 * @param {Boolean} [options.will] since v3.4.0,是否指定该消息作为「掉线消息」发送,
23688 * 「掉线消息」会延迟到当前用户掉线后发送,常用来实现「下线通知」功能
23689 * @param {MessagePriority} [options.priority] 消息优先级,仅在暂态对话中有效,
23690 * see: {@link module:leancloud-realtime.MessagePriority MessagePriority}
23691 * @param {Object} [options.pushData] 消息对应的离线推送内容,如果消息接收方不在线,会推送指定的内容。其结构说明参见: {@link https://url.leanapp.cn/pushData 推送消息内容}
23692 * @return {Promise.<Message>} 发送的消息
23693 */
23694 ;
23695
23696 _proto.send =
23697 /*#__PURE__*/
23698 function () {
23699 var _send2 = _asyncToGenerator(
23700 /*#__PURE__*/
23701 regenerator.mark(function _callee(message, options) {
23702 var _Object$assign, transient, receipt, priority, pushData, will, _serializeMessage, msg, binaryMsg, command, resCommand, _resCommand$ackMessag, uid, t, code, reason, appCode;
23703
23704 return regenerator.wrap(function _callee$(_context) {
23705 while (1) {
23706 switch (_context.prev = _context.next) {
23707 case 0:
23708 this._debug(message, 'send');
23709
23710 if (message instanceof Message) {
23711 _context.next = 3;
23712 break;
23713 }
23714
23715 throw new TypeError("".concat(message, " is not a Message"));
23716
23717 case 3:
23718 _Object$assign = Object.assign({}, // support Message static property: sendOptions
23719 message.constructor.sendOptions, // support Message static property: getSendOptions
23720 typeof message.constructor.getSendOptions === 'function' ? message.constructor.getSendOptions(message) : {}, options), transient = _Object$assign.transient, receipt = _Object$assign.receipt, priority = _Object$assign.priority, pushData = _Object$assign.pushData, will = _Object$assign.will;
23721
23722 if (receipt) {
23723 if (this.transient) {
23724 console.warn('receipt option is ignored as the conversation is transient.');
23725 } else if (transient) {
23726 console.warn('receipt option is ignored as the message is sent transiently.');
23727 } else if (this.members.length > 2) {
23728 console.warn('receipt option is recommended to be used in one-on-one conversation.'); // eslint-disable-line max-len
23729 }
23730 }
23731
23732 if (priority && !this.transient) {
23733 console.warn('priority option is ignored as the conversation is not transient.');
23734 }
23735
23736 Object.assign(message, {
23737 cid: this.id,
23738 from: this._client.id
23739 });
23740
23741 message._setStatus(MessageStatus.SENDING);
23742
23743 _serializeMessage = serializeMessage(message), msg = _serializeMessage.msg, binaryMsg = _serializeMessage.binaryMsg;
23744 command = new GenericCommand({
23745 cmd: 'direct',
23746 directMessage: new DirectCommand({
23747 msg: msg,
23748 binaryMsg: binaryMsg,
23749 cid: this.id,
23750 r: receipt,
23751 transient: transient,
23752 dt: message.id,
23753 pushData: JSON.stringify(pushData),
23754 will: will,
23755 mentionPids: message.mentionList,
23756 mentionAll: message.mentionedAll
23757 }),
23758 priority: priority
23759 });
23760 _context.prev = 10;
23761 _context.next = 13;
23762 return this._send(command);
23763
23764 case 13:
23765 resCommand = _context.sent;
23766 _resCommand$ackMessag = resCommand.ackMessage, uid = _resCommand$ackMessag.uid, t = _resCommand$ackMessag.t, code = _resCommand$ackMessag.code, reason = _resCommand$ackMessag.reason, appCode = _resCommand$ackMessag.appCode;
23767
23768 if (!(code !== null)) {
23769 _context.next = 17;
23770 break;
23771 }
23772
23773 throw createError({
23774 code: code,
23775 reason: reason,
23776 appCode: appCode
23777 });
23778
23779 case 17:
23780 Object.assign(message, {
23781 id: uid,
23782 timestamp: t
23783 });
23784
23785 if (!transient) {
23786 this.lastMessage = message;
23787 this.lastMessageAt = message.timestamp;
23788 }
23789
23790 message._setStatus(MessageStatus.SENT);
23791
23792 if (receipt) {
23793 internal(this).messagesWaitingForReceipt[message.id] = message;
23794 }
23795
23796 return _context.abrupt("return", message);
23797
23798 case 24:
23799 _context.prev = 24;
23800 _context.t0 = _context["catch"](10);
23801
23802 message._setStatus(MessageStatus.FAILED);
23803
23804 throw _context.t0;
23805
23806 case 28:
23807 case "end":
23808 return _context.stop();
23809 }
23810 }
23811 }, _callee, this, [[10, 24]]);
23812 }));
23813
23814 function send(_x, _x2) {
23815 return _send2.apply(this, arguments);
23816 }
23817
23818 return send;
23819 }();
23820
23821 _proto._update =
23822 /*#__PURE__*/
23823 function () {
23824 var _update2 = _asyncToGenerator(
23825 /*#__PURE__*/
23826 regenerator.mark(function _callee2(message, newMessage, recall) {
23827 var msg, binaryMsg, content, id, cid, timestamp, from, _status;
23828
23829 return regenerator.wrap(function _callee2$(_context2) {
23830 while (1) {
23831 switch (_context2.prev = _context2.next) {
23832 case 0:
23833 this._debug('patch %O %O %O', message, newMessage, recall);
23834
23835 if (!(message instanceof Message)) {
23836 _context2.next = 8;
23837 break;
23838 }
23839
23840 if (!(message.from !== this._client.id)) {
23841 _context2.next = 4;
23842 break;
23843 }
23844
23845 throw new Error('Updating message from others is not allowed');
23846
23847 case 4:
23848 if (!(message.status !== MessageStatus.SENT && message.status !== MessageStatus.DELIVERED)) {
23849 _context2.next = 6;
23850 break;
23851 }
23852
23853 throw new Error('Message is not sent');
23854
23855 case 6:
23856 _context2.next = 10;
23857 break;
23858
23859 case 8:
23860 if (message.id && message.timestamp) {
23861 _context2.next = 10;
23862 break;
23863 }
23864
23865 throw new TypeError("".concat(message, " is not a Message"));
23866
23867 case 10:
23868 if (!recall) {
23869 content = serializeMessage(newMessage);
23870 msg = content.msg;
23871 binaryMsg = content.binaryMsg;
23872 }
23873
23874 _context2.next = 13;
23875 return this._send(new GenericCommand({
23876 cmd: CommandType.patch,
23877 op: OpType.modify,
23878 patchMessage: new PatchCommand({
23879 patches: [new PatchItem({
23880 cid: this.id,
23881 mid: message.id,
23882 timestamp: Number(message.timestamp),
23883 recall: recall,
23884 data: msg,
23885 binaryMsg: binaryMsg,
23886 mentionPids: newMessage.mentionList,
23887 mentionAll: newMessage.mentionedAll
23888 })],
23889 lastPatchTime: this._client._lastPatchTime
23890 })
23891 }));
23892
23893 case 13:
23894 id = message.id, cid = message.cid, timestamp = message.timestamp, from = message.from, _status = message._status;
23895 Object.assign(newMessage, {
23896 id: id,
23897 cid: cid,
23898 timestamp: timestamp,
23899 from: from,
23900 _status: _status
23901 });
23902
23903 if (this.lastMessage.id === newMessage.id) {
23904 this.lastMessage = newMessage;
23905 }
23906
23907 return _context2.abrupt("return", newMessage);
23908
23909 case 17:
23910 case "end":
23911 return _context2.stop();
23912 }
23913 }
23914 }, _callee2, this);
23915 }));
23916
23917 function _update(_x3, _x4, _x5) {
23918 return _update2.apply(this, arguments);
23919 }
23920
23921 return _update;
23922 }()
23923 /**
23924 * 获取对话人数,或暂态对话的在线人数
23925 * @return {Promise.<Number>}
23926 */
23927 ;
23928
23929 _proto.count =
23930 /*#__PURE__*/
23931 function () {
23932 var _count = _asyncToGenerator(
23933 /*#__PURE__*/
23934 regenerator.mark(function _callee3() {
23935 var resCommand;
23936 return regenerator.wrap(function _callee3$(_context3) {
23937 while (1) {
23938 switch (_context3.prev = _context3.next) {
23939 case 0:
23940 this._debug('count');
23941
23942 _context3.next = 3;
23943 return this._send(new GenericCommand({
23944 op: 'count'
23945 }));
23946
23947 case 3:
23948 resCommand = _context3.sent;
23949 return _context3.abrupt("return", resCommand.convMessage.count);
23950
23951 case 5:
23952 case "end":
23953 return _context3.stop();
23954 }
23955 }
23956 }, _callee3, this);
23957 }));
23958
23959 function count() {
23960 return _count.apply(this, arguments);
23961 }
23962
23963 return count;
23964 }()
23965 /**
23966 * 应用增加成员的操作,产生副作用
23967 * @param {string[]} members
23968 * @abstract
23969 * @private
23970 */
23971 ;
23972
23973 _proto._addMembers = function _addMembers() {}
23974 /**
23975 * 应用减少成员的操作,产生副作用
23976 * @param {string[]} members
23977 * @abstract
23978 * @private
23979 */
23980 ;
23981
23982 _proto._removeMembers = function _removeMembers() {}
23983 /**
23984 * 修改已发送的消息
23985 * @param {AVMessage} message 要修改的消息,该消息必须是由当前用户发送的。也可以提供一个包含消息 {id, timestamp} 的对象
23986 * @param {AVMessage} newMessage 新的消息
23987 * @return {Promise.<AVMessage>} 更新后的消息
23988 */
23989 ;
23990
23991 _proto.update =
23992 /*#__PURE__*/
23993 function () {
23994 var _update3 = _asyncToGenerator(
23995 /*#__PURE__*/
23996 regenerator.mark(function _callee4(message, newMessage) {
23997 return regenerator.wrap(function _callee4$(_context4) {
23998 while (1) {
23999 switch (_context4.prev = _context4.next) {
24000 case 0:
24001 if (newMessage instanceof Message) {
24002 _context4.next = 2;
24003 break;
24004 }
24005
24006 throw new TypeError("".concat(newMessage, " is not a Message"));
24007
24008 case 2:
24009 return _context4.abrupt("return", this._update(message, newMessage, false));
24010
24011 case 3:
24012 case "end":
24013 return _context4.stop();
24014 }
24015 }
24016 }, _callee4, this);
24017 }));
24018
24019 function update(_x6, _x7) {
24020 return _update3.apply(this, arguments);
24021 }
24022
24023 return update;
24024 }()
24025 /**
24026 * 撤回已发送的消息
24027 * @param {AVMessage} message 要撤回的消息,该消息必须是由当前用户发送的。也可以提供一个包含消息 {id, timestamp} 的对象
24028 * @return {Promise.<RecalledMessage>} 一条已撤回的消息
24029 */
24030 ;
24031
24032 _proto.recall =
24033 /*#__PURE__*/
24034 function () {
24035 var _recall = _asyncToGenerator(
24036 /*#__PURE__*/
24037 regenerator.mark(function _callee5(message) {
24038 return regenerator.wrap(function _callee5$(_context5) {
24039 while (1) {
24040 switch (_context5.prev = _context5.next) {
24041 case 0:
24042 return _context5.abrupt("return", this._update(message, new RecalledMessage(), true));
24043
24044 case 1:
24045 case "end":
24046 return _context5.stop();
24047 }
24048 }
24049 }, _callee5, this);
24050 }));
24051
24052 function recall(_x8) {
24053 return _recall.apply(this, arguments);
24054 }
24055
24056 return recall;
24057 }()
24058 /**
24059 * 查询消息记录
24060 * 如果仅需实现消息向前记录翻页查询需求,建议使用 {@link Conversation#createMessagesIterator}。
24061 * 不论何种方向,获得的消息都是按照时间升序排列的。
24062 * startClosed 与 endClosed 用于指定查询区间的开闭。
24063 *
24064 * @param {Object} [options]
24065 * @param {Number} [options.limit] 限制查询结果的数量,目前服务端默认为 20
24066 * @param {Number} [options.type] 指定查询的富媒体消息类型,不指定则查询所有消息。
24067 * @param {MessageQueryDirection} [options.direction] 查询的方向。
24068 * 在不指定的情况下如果 startTime 大于 endTime,则为从新到旧查询,可以实现加载聊天记录等场景。
24069 * 如果 startTime 小于 endTime,则为从旧到新查询,可以实现弹幕等场景。
24070 * @param {Date} [options.startTime] 从该时间开始查询,不传则从当前时间开始查询
24071 * @param {String} [options.startMessageId] 从该消息之前开始查询,需要与 startTime 同时使用,为防止某时刻有重复消息
24072 * @param {Boolean}[options.startClosed] 指定查询范围是否包括开始的时间点,默认不包括
24073 * @param {Date} [options.endTime] 查询到该时间为止,不传则查询最早消息为止
24074 * @param {String} [options.endMessageId] 查询到该消息为止,需要与 endTime 同时使用,为防止某时刻有重复消息
24075 * @param {Boolean}[options.endClosed] 指定查询范围是否包括结束的时间点,默认不包括
24076 *
24077 * @param {Date} [options.beforeTime] DEPRECATED: 使用 startTime 代替。限制查询结果为小于该时间之前的消息,不传则为当前时间
24078 * @param {String} [options.beforeMessageId] DEPRECATED: 使用 startMessageId 代替。
24079 * 限制查询结果为该消息之前的消息,需要与 beforeTime 同时使用,为防止某时刻有重复消息
24080 * @param {Date} [options.afterTime] DEPRECATED: 使用 endTime 代替。限制查询结果为大于该时间之前的消息
24081 * @param {String} [options.afterMessageId] DEPRECATED: 使用 endMessageId 代替。
24082 * 限制查询结果为该消息之后的消息,需要与 afterTime 同时使用,为防止某时刻有重复消息
24083 * @return {Promise.<Message[]>} 消息列表
24084 */
24085 ;
24086
24087 _proto.queryMessages =
24088 /*#__PURE__*/
24089 function () {
24090 var _queryMessages = _asyncToGenerator(
24091 /*#__PURE__*/
24092 regenerator.mark(function _callee7() {
24093 var _this2 = this;
24094
24095 var options,
24096 beforeTime,
24097 beforeMessageId,
24098 afterTime,
24099 afterMessageId,
24100 limit,
24101 direction,
24102 type,
24103 startTime,
24104 startMessageId,
24105 startClosed,
24106 endTime,
24107 endMessageId,
24108 endClosed,
24109 conditions,
24110 resCommand,
24111 _args7 = arguments;
24112 return regenerator.wrap(function _callee7$(_context7) {
24113 while (1) {
24114 switch (_context7.prev = _context7.next) {
24115 case 0:
24116 options = _args7.length > 0 && _args7[0] !== undefined ? _args7[0] : {};
24117
24118 this._debug('query messages %O', options);
24119
24120 beforeTime = options.beforeTime, beforeMessageId = options.beforeMessageId, afterTime = options.afterTime, afterMessageId = options.afterMessageId, limit = options.limit, direction = options.direction, type = options.type, startTime = options.startTime, startMessageId = options.startMessageId, startClosed = options.startClosed, endTime = options.endTime, endMessageId = options.endMessageId, endClosed = options.endClosed;
24121
24122 if (!(beforeMessageId || beforeTime || afterMessageId || afterTime)) {
24123 _context7.next = 6;
24124 break;
24125 }
24126
24127 console.warn('DEPRECATION: queryMessages options beforeTime, beforeMessageId, afterTime and afterMessageId are deprecated in favor of startTime, startMessageId, endTime and endMessageId.');
24128 return _context7.abrupt("return", this.queryMessages({
24129 startTime: beforeTime,
24130 startMessageId: beforeMessageId,
24131 endTime: afterTime,
24132 endMessageId: afterMessageId,
24133 limit: limit
24134 }));
24135
24136 case 6:
24137 if (!(startMessageId && !startTime)) {
24138 _context7.next = 8;
24139 break;
24140 }
24141
24142 throw new Error('query option startMessageId must be used with option startTime');
24143
24144 case 8:
24145 if (!(endMessageId && !endTime)) {
24146 _context7.next = 10;
24147 break;
24148 }
24149
24150 throw new Error('query option endMessageId must be used with option endTime');
24151
24152 case 10:
24153 conditions = {
24154 t: startTime,
24155 mid: startMessageId,
24156 tIncluded: startClosed,
24157 tt: endTime,
24158 tmid: endMessageId,
24159 ttIncluded: endClosed,
24160 l: limit,
24161 lctype: type
24162 };
24163
24164 if (conditions.t instanceof Date) {
24165 conditions.t = conditions.t.getTime();
24166 }
24167
24168 if (conditions.tt instanceof Date) {
24169 conditions.tt = conditions.tt.getTime();
24170 }
24171
24172 if (direction !== undefined) {
24173 conditions.direction = direction;
24174 } else if (conditions.tt > conditions.t) {
24175 conditions.direction = MessageQueryDirection.OLD_TO_NEW;
24176 }
24177
24178 _context7.next = 16;
24179 return this._send(new GenericCommand({
24180 cmd: 'logs',
24181 logsMessage: new LogsCommand(Object.assign(conditions, {
24182 cid: this.id
24183 }))
24184 }));
24185
24186 case 16:
24187 resCommand = _context7.sent;
24188 return _context7.abrupt("return", Promise.all(resCommand.logsMessage.logs.map(
24189 /*#__PURE__*/
24190 function () {
24191 var _ref3 = _asyncToGenerator(
24192 /*#__PURE__*/
24193 regenerator.mark(function _callee6(_ref2) {
24194 var msgId, timestamp, patchTimestamp, from, ackAt, readAt, data, mentionAll, mentionPids, bin, messageData, message, status;
24195 return regenerator.wrap(function _callee6$(_context6) {
24196 while (1) {
24197 switch (_context6.prev = _context6.next) {
24198 case 0:
24199 msgId = _ref2.msgId, timestamp = _ref2.timestamp, patchTimestamp = _ref2.patchTimestamp, from = _ref2.from, ackAt = _ref2.ackAt, readAt = _ref2.readAt, data = _ref2.data, mentionAll = _ref2.mentionAll, mentionPids = _ref2.mentionPids, bin = _ref2.bin;
24200 messageData = {
24201 data: data,
24202 bin: bin,
24203 id: msgId,
24204 cid: _this2.id,
24205 timestamp: timestamp,
24206 from: from,
24207 deliveredAt: ackAt,
24208 updatedAt: patchTimestamp,
24209 mentionList: mentionPids,
24210 mentionedAll: mentionAll
24211 };
24212 _context6.next = 4;
24213 return _this2._client.parseMessage(messageData);
24214
24215 case 4:
24216 message = _context6.sent;
24217 status = MessageStatus.SENT;
24218
24219 if (_this2.members.length === 2) {
24220 if (ackAt) status = MessageStatus.DELIVERED;
24221 if (ackAt) _this2._setLastDeliveredAt(ackAt);
24222 if (readAt) _this2._setLastReadAt(readAt);
24223 }
24224
24225 message._setStatus(status);
24226
24227 return _context6.abrupt("return", message);
24228
24229 case 9:
24230 case "end":
24231 return _context6.stop();
24232 }
24233 }
24234 }, _callee6, this);
24235 }));
24236
24237 return function (_x9) {
24238 return _ref3.apply(this, arguments);
24239 };
24240 }())));
24241
24242 case 18:
24243 case "end":
24244 return _context7.stop();
24245 }
24246 }
24247 }, _callee7, this);
24248 }));
24249
24250 function queryMessages() {
24251 return _queryMessages.apply(this, arguments);
24252 }
24253
24254 return queryMessages;
24255 }()
24256 /**
24257 * 获取消息翻页迭代器
24258 * @param {Object} [options]
24259 * @param {Date} [options.beforeTime] 限制起始查询结果为小于该时间之前的消息,不传则为当前时间
24260 * @param {String} [options.beforeMessageId] 限制起始查询结果为该消息之前的消息,需要与 beforeTime 同时使用,为防止某时刻有重复消息
24261 * @param {Number} [options.limit] 限制每页查询结果的数量,目前服务端默认为 20
24262 * @return {AsyncIterater.<Promise.<IteratorResult<Message[]>>>} [AsyncIterator]{@link https://github.com/tc39/proposal-async-iteration},调用其 next 方法返回获取下一页消息的 Promise
24263 * @example
24264 * var messageIterator = conversation.createMessagesIterator({ limit: 10 });
24265 * messageIterator.next().then(function(result) {
24266 * // result: {
24267 * // value: [message1, ..., message10],
24268 * // done: false,
24269 * // }
24270 * });
24271 * messageIterator.next().then(function(result) {
24272 * // result: {
24273 * // value: [message11, ..., message20],
24274 * // done: false,
24275 * // }
24276 * });
24277 * messageIterator.next().then(function(result) {
24278 * // No more messages
24279 * // result: { value: [], done: true }
24280 * });
24281 */
24282 ;
24283
24284 _proto.createMessagesIterator = function createMessagesIterator() {
24285 var _this3 = this;
24286
24287 var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
24288 beforeTime = _ref4.beforeTime,
24289 beforeMessageId = _ref4.beforeMessageId,
24290 limit = _ref4.limit;
24291
24292 var promise;
24293 return {
24294 next: function next() {
24295 if (promise === undefined) {
24296 // first call
24297 promise = _this3.queryMessages({
24298 limit: limit,
24299 startTime: beforeTime,
24300 startMessageId: beforeMessageId
24301 });
24302 } else {
24303 promise = promise.then(function (prevMessages) {
24304 if (prevMessages.length === 0 || prevMessages.length < limit) {
24305 // no more messages
24306 return [];
24307 }
24308
24309 return _this3.queryMessages({
24310 startTime: prevMessages[0].timestamp,
24311 startMessageId: prevMessages[0].id,
24312 limit: limit
24313 });
24314 });
24315 }
24316
24317 return promise.then(function (value) {
24318 return {
24319 value: Array.from(value),
24320 done: value.length === 0 || value.length < limit
24321 };
24322 });
24323 }
24324 };
24325 }
24326 /**
24327 * 将该会话标记为已读
24328 * @return {Promise.<this>} self
24329 */
24330 ;
24331
24332 _proto.read =
24333 /*#__PURE__*/
24334 function () {
24335 var _read = _asyncToGenerator(
24336 /*#__PURE__*/
24337 regenerator.mark(function _callee8() {
24338 var client;
24339 return regenerator.wrap(function _callee8$(_context8) {
24340 while (1) {
24341 switch (_context8.prev = _context8.next) {
24342 case 0:
24343 this.unreadMessagesCount = 0;
24344
24345 this._setUnreadMessagesMentioned(false); // 跳过暂态会话
24346
24347
24348 if (!this.transient) {
24349 _context8.next = 4;
24350 break;
24351 }
24352
24353 return _context8.abrupt("return", this);
24354
24355 case 4:
24356 client = this._client;
24357
24358 if (!internal(client).readConversationsBuffer) {
24359 internal(client).readConversationsBuffer = new Set();
24360 }
24361
24362 internal(client).readConversationsBuffer.add(this);
24363
24364 client._doSendRead();
24365
24366 return _context8.abrupt("return", this);
24367
24368 case 9:
24369 case "end":
24370 return _context8.stop();
24371 }
24372 }
24373 }, _callee8, this);
24374 }));
24375
24376 function read() {
24377 return _read.apply(this, arguments);
24378 }
24379
24380 return read;
24381 }();
24382
24383 _proto._handleReceipt = function _handleReceipt(_ref5) {
24384 var messageId = _ref5.messageId,
24385 timestamp = _ref5.timestamp,
24386 read = _ref5.read;
24387
24388 if (read) {
24389 this._setLastReadAt(timestamp);
24390 } else {
24391 this._setLastDeliveredAt(timestamp);
24392 }
24393
24394 var _internal = internal(this),
24395 messagesWaitingForReceipt = _internal.messagesWaitingForReceipt;
24396
24397 var message = messagesWaitingForReceipt[messageId];
24398 if (!message) return;
24399
24400 message._setStatus(MessageStatus.DELIVERED);
24401
24402 message.deliveredAt = timestamp;
24403 delete messagesWaitingForReceipt[messageId];
24404 }
24405 /**
24406 * 更新对话的最新回执时间戳(lastDeliveredAt、lastReadAt)
24407 * @since 3.4.0
24408 * @return {Promise.<this>} this
24409 */
24410 ;
24411
24412 _proto.fetchReceiptTimestamps =
24413 /*#__PURE__*/
24414 function () {
24415 var _fetchReceiptTimestamps = _asyncToGenerator(
24416 /*#__PURE__*/
24417 regenerator.mark(function _callee9() {
24418 var _ref6, _ref6$convMessage, maxReadTimestamp, maxAckTimestamp;
24419
24420 return regenerator.wrap(function _callee9$(_context9) {
24421 while (1) {
24422 switch (_context9.prev = _context9.next) {
24423 case 0:
24424 _context9.next = 2;
24425 return this._send(new GenericCommand({
24426 op: 'max_read'
24427 }));
24428
24429 case 2:
24430 _ref6 = _context9.sent;
24431 _ref6$convMessage = _ref6.convMessage;
24432 maxReadTimestamp = _ref6$convMessage.maxReadTimestamp;
24433 maxAckTimestamp = _ref6$convMessage.maxAckTimestamp;
24434
24435 this._setLastDeliveredAt(maxAckTimestamp);
24436
24437 this._setLastReadAt(maxReadTimestamp);
24438
24439 return _context9.abrupt("return", this);
24440
24441 case 9:
24442 case "end":
24443 return _context9.stop();
24444 }
24445 }
24446 }, _callee9, this);
24447 }));
24448
24449 function fetchReceiptTimestamps() {
24450 return _fetchReceiptTimestamps.apply(this, arguments);
24451 }
24452
24453 return fetchReceiptTimestamps;
24454 }();
24455
24456 _proto._fetchAllReceiptTimestamps = function _fetchAllReceiptTimestamps() {
24457 var convMessage = new ConvCommand({
24458 queryAllMembers: true
24459 });
24460 return this._send(new GenericCommand({
24461 op: 'max_read',
24462 convMessage: convMessage
24463 })).then(function (_ref7) {
24464 var maxReadTuples = _ref7.convMessage.maxReadTuples;
24465 return maxReadTuples.filter(function (maxReadTuple) {
24466 return maxReadTuple.maxAckTimestamp || maxReadTuple.maxReadTimestamp;
24467 }).map(function (_ref8) {
24468 var pid = _ref8.pid,
24469 maxAckTimestamp = _ref8.maxAckTimestamp,
24470 maxReadTimestamp = _ref8.maxReadTimestamp;
24471 return {
24472 pid: pid,
24473 lastDeliveredAt: decodeDate(maxAckTimestamp),
24474 lastReadAt: decodeDate(maxReadTimestamp)
24475 };
24476 });
24477 });
24478 };
24479
24480 _createClass(ConversationBase, [{
24481 key: "unreadMessagesMentioned",
24482 get: function get() {
24483 return internal(this).unreadMessagesMentioned;
24484 }
24485 }, {
24486 key: "unreadMessagesCount",
24487 set: function set(value) {
24488 if (value !== this.unreadMessagesCount) {
24489 internal(this).unreadMessagesCount = value;
24490
24491 this._client.emit(UNREAD_MESSAGES_COUNT_UPDATE, [this]);
24492 }
24493 }
24494 /**
24495 * 当前用户在该对话的未读消息数
24496 * @type {Number}
24497 */
24498 ,
24499 get: function get() {
24500 return internal(this).unreadMessagesCount;
24501 }
24502 }, {
24503 key: "lastMessageAt",
24504 set: function set(value) {
24505 var time = decodeDate(value);
24506 if (time <= this._lastMessageAt) return;
24507 this._lastMessageAt = time;
24508 },
24509 get: function get() {
24510 return this._lastMessageAt;
24511 }
24512 /**
24513 * 最后消息送达时间,常用来实现消息的「已送达」标记,可通过 {@link Conversation#fetchReceiptTimestamps} 获取或更新该属性
24514 * @type {?Date}
24515 * @since 3.4.0
24516 */
24517
24518 }, {
24519 key: "lastDeliveredAt",
24520 get: function get() {
24521 if (this.members.length !== 2) return null;
24522 return internal(this).lastDeliveredAt;
24523 }
24524 }, {
24525 key: "lastReadAt",
24526 get: function get() {
24527 if (this.members.length !== 2) return null;
24528 return internal(this).lastReadAt;
24529 }
24530 }]);
24531
24532 return ConversationBase;
24533 }(eventemitter3);
24534
24535 var debug$7 = browser('LC:SignatureFactoryRunner');
24536
24537 function _validateSignature() {
24538 var signatureResult = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
24539 var signature = signatureResult.signature,
24540 timestamp = signatureResult.timestamp,
24541 nonce = signatureResult.nonce;
24542
24543 if (typeof signature !== 'string' || typeof timestamp !== 'number' || typeof nonce !== 'string') {
24544 throw new Error('malformed signature');
24545 }
24546
24547 return {
24548 signature: signature,
24549 timestamp: timestamp,
24550 nonce: nonce
24551 };
24552 }
24553
24554 var runSignatureFactory = (function (signatureFactory, params) {
24555 return Promise.resolve().then(function () {
24556 debug$7('call signatureFactory with %O', params);
24557 return signatureFactory.apply(void 0, _toConsumableArray(params));
24558 }).then(tap(function (signatureResult) {
24559 return debug$7('sign result %O', signatureResult);
24560 }), function (error) {
24561 // eslint-disable-next-line no-param-reassign
24562 error.message = "sign error: ".concat(error.message);
24563 debug$7(error);
24564 throw error;
24565 }).then(_validateSignature);
24566 });
24567
24568 /**
24569 * 部分失败异常
24570 * @typedef OperationFailureError
24571 * @type {Error}
24572 * @property {string} message 异常信息
24573 * @property {string[]} clientIds 因为该原因失败的 client id 列表
24574 * @property {number} [code] 错误码
24575 * @property {string} [detail] 详细信息
24576 */
24577
24578 /**
24579 * 部分成功的结果
24580 * @typedef PartiallySuccess
24581 * @type {Object}
24582 * @property {string[]} successfulClientIds 成功的 client id 列表
24583 * @property {OperationFailureError[]} failures 失败的异常列表
24584 */
24585
24586 /**
24587 * 分页查询结果
24588 * @typedef PagedResults
24589 * @type {Object}
24590 * @property {T[]} results 查询结果
24591 * @property {string} [next] 存在表示还有更多结果,在下次查询中带上可实现翻页。
24592 */
24593
24594 var createPartiallySuccess = function createPartiallySuccess(_ref) {
24595 var allowedPids = _ref.allowedPids,
24596 failedPids = _ref.failedPids;
24597 return {
24598 successfulClientIds: allowedPids,
24599 failures: failedPids.map(function (_ref2) {
24600 var pids = _ref2.pids,
24601 error = _objectWithoutProperties(_ref2, ["pids"]);
24602
24603 return Object.assign(createError(error), {
24604 clientIds: pids
24605 });
24606 })
24607 };
24608 };
24609 /**
24610 * @extends ConversationBase
24611 * @private
24612 * @abstract
24613 */
24614
24615
24616 var PersistentConversation =
24617 /*#__PURE__*/
24618 function (_ConversationBase) {
24619 _inheritsLoose(PersistentConversation, _ConversationBase);
24620
24621 function PersistentConversation(data, _ref3, client) {
24622 var _this;
24623
24624 var creator = _ref3.creator,
24625 createdAt = _ref3.createdAt,
24626 updatedAt = _ref3.updatedAt,
24627 _ref3$transient = _ref3.transient,
24628 transient = _ref3$transient === void 0 ? false : _ref3$transient,
24629 _ref3$system = _ref3.system,
24630 system = _ref3$system === void 0 ? false : _ref3$system,
24631 _ref3$muted = _ref3.muted,
24632 muted = _ref3$muted === void 0 ? false : _ref3$muted,
24633 _ref3$mutedMembers = _ref3.mutedMembers,
24634 mutedMembers = _ref3$mutedMembers === void 0 ? [] : _ref3$mutedMembers,
24635 attributes = _objectWithoutProperties(_ref3, ["creator", "createdAt", "updatedAt", "transient", "system", "muted", "mutedMembers"]);
24636
24637 _this = _ConversationBase.call(this, _objectSpread({}, data, {
24638 /**
24639 * 对话创建者
24640 * @memberof PersistentConversation#
24641 * @type {String}
24642 */
24643 creator: creator,
24644
24645 /**
24646 * 对话创建时间
24647 * @memberof PersistentConversation#
24648 * @type {Date}
24649 */
24650 createdAt: createdAt,
24651
24652 /**
24653 * 对话更新时间
24654 * @memberof PersistentConversation#
24655 * @type {Date}
24656 */
24657 updatedAt: updatedAt,
24658
24659 /**
24660 * 对该对话设置了静音的用户列表
24661 * @memberof PersistentConversation#
24662 * @type {?String[]}
24663 */
24664 mutedMembers: mutedMembers,
24665
24666 /**
24667 * 暂态对话标记
24668 * @memberof PersistentConversation#
24669 * @type {Boolean}
24670 */
24671 transient: transient,
24672
24673 /**
24674 * 系统对话标记
24675 * @memberof PersistentConversation#
24676 * @type {Boolean}
24677 * @since 3.3.0
24678 */
24679 system: system,
24680
24681 /**
24682 * 当前用户静音该对话标记
24683 * @memberof PersistentConversation#
24684 * @type {Boolean}
24685 */
24686 muted: muted,
24687 _attributes: attributes
24688 }), client) || this;
24689
24690 _this._reset();
24691
24692 return _this;
24693 }
24694
24695 var _proto = PersistentConversation.prototype;
24696
24697 /**
24698 * 获取对话的自定义属性
24699 * @since 3.2.0
24700 * @param {String} key key 属性的键名,'x' 对应 Conversation 表中的 x 列
24701 * @return {Any} 属性的值
24702 */
24703 _proto.get = function get(key) {
24704 return internal(this).currentAttributes[key];
24705 }
24706 /**
24707 * 设置对话的自定义属性
24708 * @since 3.2.0
24709 * @param {String} key 属性的键名,'x' 对应 Conversation 表中的 x 列,支持使用 'x.y.z' 来修改对象的部分字段。
24710 * @param {Any} value 属性的值
24711 * @return {this} self
24712 * @example
24713 *
24714 * // 设置对话的 color 属性
24715 * conversation.set('color', {
24716 * text: '#000',
24717 * background: '#DDD',
24718 * });
24719 * // 设置对话的 color.text 属性
24720 * conversation.set('color.text', '#333');
24721 */
24722 ;
24723
24724 _proto.set = function set(key, value) {
24725 this._debug("set [".concat(key, "]: ").concat(value));
24726
24727 var _internal = internal(this),
24728 pendingAttributes = _internal.pendingAttributes;
24729
24730 var pendingKeys = Object.keys(pendingAttributes); // suppose pendingAttributes = { 'a.b': {} }
24731 // set 'a' or 'a.b': delete 'a.b'
24732
24733 var re = new RegExp("^".concat(key));
24734 var childKeys = pendingKeys.filter(re.test.bind(re));
24735 childKeys.forEach(function (k) {
24736 delete pendingAttributes[k];
24737 });
24738
24739 if (childKeys.length) {
24740 pendingAttributes[key] = value;
24741 } else {
24742 // set 'a.c': nothing to do
24743 // set 'a.b.c.d': assign c: { d: {} } to 'a.b'
24744 var parentKey = find_1(pendingKeys, function (k) {
24745 return key.indexOf(k) === 0;
24746 }); // 'a.b'
24747
24748 if (parentKey) {
24749 setValue(pendingAttributes[parentKey], key.slice(parentKey.length + 1), value);
24750 } else {
24751 pendingAttributes[key] = value;
24752 }
24753 }
24754
24755 this._buildCurrentAttributes();
24756
24757 return this;
24758 };
24759
24760 _proto._buildCurrentAttributes = function _buildCurrentAttributes() {
24761 var _internal2 = internal(this),
24762 pendingAttributes = _internal2.pendingAttributes;
24763
24764 internal(this).currentAttributes = Object.keys(pendingAttributes).reduce(function (target, k) {
24765 return setValue(target, k, pendingAttributes[k]);
24766 }, cloneDeep_1(this._attributes));
24767 };
24768
24769 _proto._updateServerAttributes = function _updateServerAttributes(attributes) {
24770 var _this2 = this;
24771
24772 Object.keys(attributes).forEach(function (key) {
24773 return setValue(_this2._attributes, key, attributes[key]);
24774 });
24775
24776 this._buildCurrentAttributes();
24777 };
24778
24779 _proto._reset = function _reset() {
24780 Object.assign(internal(this), {
24781 pendingAttributes: {},
24782 currentAttributes: this._attributes
24783 });
24784 }
24785 /**
24786 * 保存当前对话的属性至服务器
24787 * @return {Promise.<this>} self
24788 */
24789 ;
24790
24791 _proto.save =
24792 /*#__PURE__*/
24793 function () {
24794 var _save = _asyncToGenerator(
24795 /*#__PURE__*/
24796 regenerator.mark(function _callee() {
24797 var attr, convMessage, resCommand;
24798 return regenerator.wrap(function _callee$(_context) {
24799 while (1) {
24800 switch (_context.prev = _context.next) {
24801 case 0:
24802 this._debug('save');
24803
24804 attr = internal(this).pendingAttributes;
24805
24806 if (!isEmpty_1(attr)) {
24807 _context.next = 5;
24808 break;
24809 }
24810
24811 this._debug('nothing touched, resolve with self');
24812
24813 return _context.abrupt("return", this);
24814
24815 case 5:
24816 this._debug('attr: %O', attr);
24817
24818 convMessage = new ConvCommand({
24819 attr: new JsonObjectMessage({
24820 data: JSON.stringify(encode(attr))
24821 })
24822 });
24823 _context.next = 9;
24824 return this._send(new GenericCommand({
24825 op: 'update',
24826 convMessage: convMessage
24827 }));
24828
24829 case 9:
24830 resCommand = _context.sent;
24831 this.updatedAt = resCommand.convMessage.udate;
24832 this._attributes = internal(this).currentAttributes;
24833 internal(this).pendingAttributes = {};
24834 return _context.abrupt("return", this);
24835
24836 case 14:
24837 case "end":
24838 return _context.stop();
24839 }
24840 }
24841 }, _callee, this);
24842 }));
24843
24844 function save() {
24845 return _save.apply(this, arguments);
24846 }
24847
24848 return save;
24849 }()
24850 /**
24851 * 从服务器更新对话的属性
24852 * @return {Promise.<this>} self
24853 */
24854 ;
24855
24856 _proto.fetch =
24857 /*#__PURE__*/
24858 function () {
24859 var _fetch = _asyncToGenerator(
24860 /*#__PURE__*/
24861 regenerator.mark(function _callee2() {
24862 var query;
24863 return regenerator.wrap(function _callee2$(_context2) {
24864 while (1) {
24865 switch (_context2.prev = _context2.next) {
24866 case 0:
24867 query = this._client.getQuery().equalTo('objectId', this.id);
24868 _context2.next = 3;
24869 return query.find();
24870
24871 case 3:
24872 return _context2.abrupt("return", this);
24873
24874 case 4:
24875 case "end":
24876 return _context2.stop();
24877 }
24878 }
24879 }, _callee2, this);
24880 }));
24881
24882 function fetch() {
24883 return _fetch.apply(this, arguments);
24884 }
24885
24886 return fetch;
24887 }()
24888 /**
24889 * 静音,客户端拒绝收到服务器端的离线推送通知
24890 * @return {Promise.<this>} self
24891 */
24892 ;
24893
24894 _proto.mute =
24895 /*#__PURE__*/
24896 function () {
24897 var _mute = _asyncToGenerator(
24898 /*#__PURE__*/
24899 regenerator.mark(function _callee3() {
24900 return regenerator.wrap(function _callee3$(_context3) {
24901 while (1) {
24902 switch (_context3.prev = _context3.next) {
24903 case 0:
24904 this._debug('mute');
24905
24906 _context3.next = 3;
24907 return this._send(new GenericCommand({
24908 op: 'mute'
24909 }));
24910
24911 case 3:
24912 if (!this.transient) {
24913 this.muted = true;
24914 this.mutedMembers = union(this.mutedMembers, [this._client.id]);
24915 }
24916
24917 return _context3.abrupt("return", this);
24918
24919 case 5:
24920 case "end":
24921 return _context3.stop();
24922 }
24923 }
24924 }, _callee3, this);
24925 }));
24926
24927 function mute() {
24928 return _mute.apply(this, arguments);
24929 }
24930
24931 return mute;
24932 }()
24933 /**
24934 * 取消静音
24935 * @return {Promise.<this>} self
24936 */
24937 ;
24938
24939 _proto.unmute =
24940 /*#__PURE__*/
24941 function () {
24942 var _unmute = _asyncToGenerator(
24943 /*#__PURE__*/
24944 regenerator.mark(function _callee4() {
24945 return regenerator.wrap(function _callee4$(_context4) {
24946 while (1) {
24947 switch (_context4.prev = _context4.next) {
24948 case 0:
24949 this._debug('unmute');
24950
24951 _context4.next = 3;
24952 return this._send(new GenericCommand({
24953 op: 'unmute'
24954 }));
24955
24956 case 3:
24957 if (!this.transient) {
24958 this.muted = false;
24959 this.mutedMembers = difference(this.mutedMembers, [this._client.id]);
24960 }
24961
24962 return _context4.abrupt("return", this);
24963
24964 case 5:
24965 case "end":
24966 return _context4.stop();
24967 }
24968 }
24969 }, _callee4, this);
24970 }));
24971
24972 function unmute() {
24973 return _unmute.apply(this, arguments);
24974 }
24975
24976 return unmute;
24977 }();
24978
24979 _proto._appendConversationSignature =
24980 /*#__PURE__*/
24981 function () {
24982 var _appendConversationSignature2 = _asyncToGenerator(
24983 /*#__PURE__*/
24984 regenerator.mark(function _callee5(command, action, clientIds) {
24985 var params, signatureResult;
24986 return regenerator.wrap(function _callee5$(_context5) {
24987 while (1) {
24988 switch (_context5.prev = _context5.next) {
24989 case 0:
24990 if (!this._client.options.conversationSignatureFactory) {
24991 _context5.next = 6;
24992 break;
24993 }
24994
24995 params = [this.id, this._client.id, clientIds.sort(), action];
24996 _context5.next = 4;
24997 return runSignatureFactory(this._client.options.conversationSignatureFactory, params);
24998
24999 case 4:
25000 signatureResult = _context5.sent;
25001 Object.assign(command.convMessage, keyRemap({
25002 signature: 's',
25003 timestamp: 't',
25004 nonce: 'n'
25005 }, signatureResult));
25006
25007 case 6:
25008 case "end":
25009 return _context5.stop();
25010 }
25011 }
25012 }, _callee5, this);
25013 }));
25014
25015 function _appendConversationSignature(_x, _x2, _x3) {
25016 return _appendConversationSignature2.apply(this, arguments);
25017 }
25018
25019 return _appendConversationSignature;
25020 }();
25021
25022 _proto._appendBlacklistSignature =
25023 /*#__PURE__*/
25024 function () {
25025 var _appendBlacklistSignature2 = _asyncToGenerator(
25026 /*#__PURE__*/
25027 regenerator.mark(function _callee6(command, action, clientIds) {
25028 var params, signatureResult;
25029 return regenerator.wrap(function _callee6$(_context6) {
25030 while (1) {
25031 switch (_context6.prev = _context6.next) {
25032 case 0:
25033 if (!this._client.options.blacklistSignatureFactory) {
25034 _context6.next = 6;
25035 break;
25036 }
25037
25038 params = [this._client.id, this.id, clientIds.sort(), action];
25039 _context6.next = 4;
25040 return runSignatureFactory(this._client.options.blacklistSignatureFactory, params);
25041
25042 case 4:
25043 signatureResult = _context6.sent;
25044 Object.assign(command.blacklistMessage, keyRemap({
25045 signature: 's',
25046 timestamp: 't',
25047 nonce: 'n'
25048 }, signatureResult));
25049
25050 case 6:
25051 case "end":
25052 return _context6.stop();
25053 }
25054 }
25055 }, _callee6, this);
25056 }));
25057
25058 function _appendBlacklistSignature(_x4, _x5, _x6) {
25059 return _appendBlacklistSignature2.apply(this, arguments);
25060 }
25061
25062 return _appendBlacklistSignature;
25063 }()
25064 /**
25065 * 增加成员
25066 * @param {String|String[]} clientIds 新增成员 client id
25067 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25068 */
25069 ;
25070
25071 _proto.add =
25072 /*#__PURE__*/
25073 function () {
25074 var _add = _asyncToGenerator(
25075 /*#__PURE__*/
25076 regenerator.mark(function _callee7(clientIds) {
25077 var command, _ref4, convMessage, allowedPids;
25078
25079 return regenerator.wrap(function _callee7$(_context7) {
25080 while (1) {
25081 switch (_context7.prev = _context7.next) {
25082 case 0:
25083 this._debug('add', clientIds);
25084
25085 if (typeof clientIds === 'string') {
25086 clientIds = [clientIds]; // eslint-disable-line no-param-reassign
25087 }
25088
25089 command = new GenericCommand({
25090 op: 'add',
25091 convMessage: new ConvCommand({
25092 m: clientIds
25093 })
25094 });
25095 _context7.next = 5;
25096 return this._appendConversationSignature(command, 'add', clientIds);
25097
25098 case 5:
25099 _context7.next = 7;
25100 return this._send(command);
25101
25102 case 7:
25103 _ref4 = _context7.sent;
25104 convMessage = _ref4.convMessage;
25105 allowedPids = _ref4.convMessage.allowedPids;
25106
25107 this._addMembers(allowedPids);
25108
25109 return _context7.abrupt("return", createPartiallySuccess(convMessage));
25110
25111 case 12:
25112 case "end":
25113 return _context7.stop();
25114 }
25115 }
25116 }, _callee7, this);
25117 }));
25118
25119 function add(_x7) {
25120 return _add.apply(this, arguments);
25121 }
25122
25123 return add;
25124 }()
25125 /**
25126 * 剔除成员
25127 * @param {String|String[]} clientIds 成员 client id
25128 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25129 */
25130 ;
25131
25132 _proto.remove =
25133 /*#__PURE__*/
25134 function () {
25135 var _remove = _asyncToGenerator(
25136 /*#__PURE__*/
25137 regenerator.mark(function _callee8(clientIds) {
25138 var command, _ref5, convMessage, allowedPids;
25139
25140 return regenerator.wrap(function _callee8$(_context8) {
25141 while (1) {
25142 switch (_context8.prev = _context8.next) {
25143 case 0:
25144 this._debug('remove', clientIds);
25145
25146 if (typeof clientIds === 'string') {
25147 clientIds = [clientIds]; // eslint-disable-line no-param-reassign
25148 }
25149
25150 command = new GenericCommand({
25151 op: 'remove',
25152 convMessage: new ConvCommand({
25153 m: clientIds
25154 })
25155 });
25156 _context8.next = 5;
25157 return this._appendConversationSignature(command, 'remove', clientIds);
25158
25159 case 5:
25160 _context8.next = 7;
25161 return this._send(command);
25162
25163 case 7:
25164 _ref5 = _context8.sent;
25165 convMessage = _ref5.convMessage;
25166 allowedPids = _ref5.convMessage.allowedPids;
25167
25168 this._removeMembers(allowedPids);
25169
25170 return _context8.abrupt("return", createPartiallySuccess(convMessage));
25171
25172 case 12:
25173 case "end":
25174 return _context8.stop();
25175 }
25176 }
25177 }, _callee8, this);
25178 }));
25179
25180 function remove(_x8) {
25181 return _remove.apply(this, arguments);
25182 }
25183
25184 return remove;
25185 }()
25186 /**
25187 * (当前用户)加入该对话
25188 * @return {Promise.<this>} self
25189 */
25190 ;
25191
25192 _proto.join =
25193 /*#__PURE__*/
25194 function () {
25195 var _join = _asyncToGenerator(
25196 /*#__PURE__*/
25197 regenerator.mark(function _callee9() {
25198 var _this3 = this;
25199
25200 return regenerator.wrap(function _callee9$(_context9) {
25201 while (1) {
25202 switch (_context9.prev = _context9.next) {
25203 case 0:
25204 this._debug('join');
25205
25206 return _context9.abrupt("return", this.add(this._client.id).then(function (_ref6) {
25207 var failures = _ref6.failures;
25208 if (failures[0]) throw failures[0];
25209 return _this3;
25210 }));
25211
25212 case 2:
25213 case "end":
25214 return _context9.stop();
25215 }
25216 }
25217 }, _callee9, this);
25218 }));
25219
25220 function join() {
25221 return _join.apply(this, arguments);
25222 }
25223
25224 return join;
25225 }()
25226 /**
25227 * (当前用户)退出该对话
25228 * @return {Promise.<this>} self
25229 */
25230 ;
25231
25232 _proto.quit =
25233 /*#__PURE__*/
25234 function () {
25235 var _quit = _asyncToGenerator(
25236 /*#__PURE__*/
25237 regenerator.mark(function _callee10() {
25238 var _this4 = this;
25239
25240 return regenerator.wrap(function _callee10$(_context10) {
25241 while (1) {
25242 switch (_context10.prev = _context10.next) {
25243 case 0:
25244 this._debug('quit');
25245
25246 return _context10.abrupt("return", this.remove(this._client.id).then(function (_ref7) {
25247 var failures = _ref7.failures;
25248 if (failures[0]) throw failures[0];
25249 return _this4;
25250 }));
25251
25252 case 2:
25253 case "end":
25254 return _context10.stop();
25255 }
25256 }
25257 }, _callee10, this);
25258 }));
25259
25260 function quit() {
25261 return _quit.apply(this, arguments);
25262 }
25263
25264 return quit;
25265 }()
25266 /**
25267 * 在该对话中禁言成员
25268 * @param {String|String[]} clientIds 成员 client id
25269 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25270 */
25271 ;
25272
25273 _proto.muteMembers =
25274 /*#__PURE__*/
25275 function () {
25276 var _muteMembers = _asyncToGenerator(
25277 /*#__PURE__*/
25278 regenerator.mark(function _callee11(clientIds) {
25279 var command, _ref8, convMessage;
25280
25281 return regenerator.wrap(function _callee11$(_context11) {
25282 while (1) {
25283 switch (_context11.prev = _context11.next) {
25284 case 0:
25285 this._debug('mute', clientIds);
25286
25287 clientIds = ensureArray(clientIds); // eslint-disable-line no-param-reassign
25288
25289 command = new GenericCommand({
25290 op: OpType.add_shutup,
25291 convMessage: new ConvCommand({
25292 m: clientIds
25293 })
25294 });
25295 _context11.next = 5;
25296 return this._send(command);
25297
25298 case 5:
25299 _ref8 = _context11.sent;
25300 convMessage = _ref8.convMessage;
25301 return _context11.abrupt("return", createPartiallySuccess(convMessage));
25302
25303 case 8:
25304 case "end":
25305 return _context11.stop();
25306 }
25307 }
25308 }, _callee11, this);
25309 }));
25310
25311 function muteMembers(_x9) {
25312 return _muteMembers.apply(this, arguments);
25313 }
25314
25315 return muteMembers;
25316 }()
25317 /**
25318 * 在该对话中解除成员禁言
25319 * @param {String|String[]} clientIds 成员 client id
25320 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25321 */
25322 ;
25323
25324 _proto.unmuteMembers =
25325 /*#__PURE__*/
25326 function () {
25327 var _unmuteMembers = _asyncToGenerator(
25328 /*#__PURE__*/
25329 regenerator.mark(function _callee12(clientIds) {
25330 var command, _ref9, convMessage;
25331
25332 return regenerator.wrap(function _callee12$(_context12) {
25333 while (1) {
25334 switch (_context12.prev = _context12.next) {
25335 case 0:
25336 this._debug('unmute', clientIds);
25337
25338 clientIds = ensureArray(clientIds); // eslint-disable-line no-param-reassign
25339
25340 command = new GenericCommand({
25341 op: OpType.remove_shutup,
25342 convMessage: new ConvCommand({
25343 m: clientIds
25344 })
25345 });
25346 _context12.next = 5;
25347 return this._send(command);
25348
25349 case 5:
25350 _ref9 = _context12.sent;
25351 convMessage = _ref9.convMessage;
25352 return _context12.abrupt("return", createPartiallySuccess(convMessage));
25353
25354 case 8:
25355 case "end":
25356 return _context12.stop();
25357 }
25358 }
25359 }, _callee12, this);
25360 }));
25361
25362 function unmuteMembers(_x10) {
25363 return _unmuteMembers.apply(this, arguments);
25364 }
25365
25366 return unmuteMembers;
25367 }()
25368 /**
25369 * 查询该对话禁言成员列表
25370 * @param {Object} [options]
25371 * @param {Number} [options.limit] 返回的成员数量,服务器默认值 10
25372 * @param {String} [options.next] 从指定 next 开始查询,与 limit 一起使用可以完成翻页。
25373 * @return {PagedResults.<string>} 查询结果。其中的 cureser 存在表示还有更多结果。
25374 */
25375 ;
25376
25377 _proto.queryMutedMembers =
25378 /*#__PURE__*/
25379 function () {
25380 var _queryMutedMembers = _asyncToGenerator(
25381 /*#__PURE__*/
25382 regenerator.mark(function _callee13() {
25383 var _ref10,
25384 limit,
25385 next,
25386 command,
25387 _ref11,
25388 _ref11$convMessage,
25389 m,
25390 newNext,
25391 _args13 = arguments;
25392
25393 return regenerator.wrap(function _callee13$(_context13) {
25394 while (1) {
25395 switch (_context13.prev = _context13.next) {
25396 case 0:
25397 _ref10 = _args13.length > 0 && _args13[0] !== undefined ? _args13[0] : {}, limit = _ref10.limit, next = _ref10.next;
25398
25399 this._debug('query muted: limit %O, next: %O', limit, next);
25400
25401 command = new GenericCommand({
25402 op: OpType.query_shutup,
25403 convMessage: new ConvCommand({
25404 limit: limit,
25405 next: next
25406 })
25407 });
25408 _context13.next = 5;
25409 return this._send(command);
25410
25411 case 5:
25412 _ref11 = _context13.sent;
25413 _ref11$convMessage = _ref11.convMessage;
25414 m = _ref11$convMessage.m;
25415 newNext = _ref11$convMessage.next;
25416 return _context13.abrupt("return", {
25417 results: m,
25418 next: newNext
25419 });
25420
25421 case 10:
25422 case "end":
25423 return _context13.stop();
25424 }
25425 }
25426 }, _callee13, this);
25427 }));
25428
25429 function queryMutedMembers() {
25430 return _queryMutedMembers.apply(this, arguments);
25431 }
25432
25433 return queryMutedMembers;
25434 }()
25435 /**
25436 * 将用户加入该对话黑名单
25437 * @param {String|String[]} clientIds 成员 client id
25438 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25439 */
25440 ;
25441
25442 _proto.blockMembers =
25443 /*#__PURE__*/
25444 function () {
25445 var _blockMembers = _asyncToGenerator(
25446 /*#__PURE__*/
25447 regenerator.mark(function _callee14(clientIds) {
25448 var command, _ref12, blacklistMessage;
25449
25450 return regenerator.wrap(function _callee14$(_context14) {
25451 while (1) {
25452 switch (_context14.prev = _context14.next) {
25453 case 0:
25454 this._debug('block', clientIds);
25455
25456 clientIds = ensureArray(clientIds); // eslint-disable-line no-param-reassign
25457
25458 command = new GenericCommand({
25459 cmd: 'blacklist',
25460 op: OpType.block,
25461 blacklistMessage: new BlacklistCommand({
25462 srcCid: this.id,
25463 toPids: clientIds
25464 })
25465 });
25466 _context14.next = 5;
25467 return this._appendBlacklistSignature(command, 'conversation-block-clients', clientIds);
25468
25469 case 5:
25470 _context14.next = 7;
25471 return this._send(command);
25472
25473 case 7:
25474 _ref12 = _context14.sent;
25475 blacklistMessage = _ref12.blacklistMessage;
25476 return _context14.abrupt("return", createPartiallySuccess(blacklistMessage));
25477
25478 case 10:
25479 case "end":
25480 return _context14.stop();
25481 }
25482 }
25483 }, _callee14, this);
25484 }));
25485
25486 function blockMembers(_x11) {
25487 return _blockMembers.apply(this, arguments);
25488 }
25489
25490 return blockMembers;
25491 }()
25492 /**
25493 * 将用户移出该对话黑名单
25494 * @param {String|String[]} clientIds 成员 client id
25495 * @return {Promise.<PartiallySuccess>} 部分成功结果,包含了成功的 id 列表、失败原因与对应的 id 列表
25496 */
25497 ;
25498
25499 _proto.unblockMembers =
25500 /*#__PURE__*/
25501 function () {
25502 var _unblockMembers = _asyncToGenerator(
25503 /*#__PURE__*/
25504 regenerator.mark(function _callee15(clientIds) {
25505 var command, _ref13, blacklistMessage;
25506
25507 return regenerator.wrap(function _callee15$(_context15) {
25508 while (1) {
25509 switch (_context15.prev = _context15.next) {
25510 case 0:
25511 this._debug('unblock', clientIds);
25512
25513 clientIds = ensureArray(clientIds); // eslint-disable-line no-param-reassign
25514
25515 command = new GenericCommand({
25516 cmd: 'blacklist',
25517 op: OpType.unblock,
25518 blacklistMessage: new BlacklistCommand({
25519 srcCid: this.id,
25520 toPids: clientIds
25521 })
25522 });
25523 _context15.next = 5;
25524 return this._appendBlacklistSignature(command, 'conversation-unblock-clients', clientIds);
25525
25526 case 5:
25527 _context15.next = 7;
25528 return this._send(command);
25529
25530 case 7:
25531 _ref13 = _context15.sent;
25532 blacklistMessage = _ref13.blacklistMessage;
25533 return _context15.abrupt("return", createPartiallySuccess(blacklistMessage));
25534
25535 case 10:
25536 case "end":
25537 return _context15.stop();
25538 }
25539 }
25540 }, _callee15, this);
25541 }));
25542
25543 function unblockMembers(_x12) {
25544 return _unblockMembers.apply(this, arguments);
25545 }
25546
25547 return unblockMembers;
25548 }()
25549 /**
25550 * 查询该对话黑名单
25551 * @param {Object} [options]
25552 * @param {Number} [options.limit] 返回的成员数量,服务器默认值 10
25553 * @param {String} [options.next] 从指定 next 开始查询,与 limit 一起使用可以完成翻页
25554 * @return {PagedResults.<string>} 查询结果。其中的 cureser 存在表示还有更多结果。
25555 */
25556 ;
25557
25558 _proto.queryBlockedMembers =
25559 /*#__PURE__*/
25560 function () {
25561 var _queryBlockedMembers = _asyncToGenerator(
25562 /*#__PURE__*/
25563 regenerator.mark(function _callee16() {
25564 var _ref14,
25565 limit,
25566 next,
25567 command,
25568 _ref15,
25569 _ref15$blacklistMessa,
25570 blockedPids,
25571 newNext,
25572 _args16 = arguments;
25573
25574 return regenerator.wrap(function _callee16$(_context16) {
25575 while (1) {
25576 switch (_context16.prev = _context16.next) {
25577 case 0:
25578 _ref14 = _args16.length > 0 && _args16[0] !== undefined ? _args16[0] : {}, limit = _ref14.limit, next = _ref14.next;
25579
25580 this._debug('query blocked: limit %O, next: %O', limit, next);
25581
25582 command = new GenericCommand({
25583 cmd: 'blacklist',
25584 op: OpType.query,
25585 blacklistMessage: new BlacklistCommand({
25586 srcCid: this.id,
25587 limit: limit,
25588 next: next
25589 })
25590 });
25591 _context16.next = 5;
25592 return this._send(command);
25593
25594 case 5:
25595 _ref15 = _context16.sent;
25596 _ref15$blacklistMessa = _ref15.blacklistMessage;
25597 blockedPids = _ref15$blacklistMessa.blockedPids;
25598 newNext = _ref15$blacklistMessa.next;
25599 return _context16.abrupt("return", {
25600 results: blockedPids,
25601 next: newNext
25602 });
25603
25604 case 10:
25605 case "end":
25606 return _context16.stop();
25607 }
25608 }
25609 }, _callee16, this);
25610 }));
25611
25612 function queryBlockedMembers() {
25613 return _queryBlockedMembers.apply(this, arguments);
25614 }
25615
25616 return queryBlockedMembers;
25617 }();
25618
25619 _proto.toFullJSON = function toFullJSON() {
25620 var creator = this.creator,
25621 system = this.system,
25622 transient = this.transient,
25623 createdAt = this.createdAt,
25624 updatedAt = this.updatedAt,
25625 _attributes = this._attributes;
25626 return _objectSpread({}, _ConversationBase.prototype.toFullJSON.call(this), {
25627 creator: creator,
25628 system: system,
25629 transient: transient,
25630 createdAt: getTime(createdAt),
25631 updatedAt: getTime(updatedAt)
25632 }, _attributes);
25633 };
25634
25635 _proto.toJSON = function toJSON() {
25636 var creator = this.creator,
25637 system = this.system,
25638 transient = this.transient,
25639 muted = this.muted,
25640 mutedMembers = this.mutedMembers,
25641 createdAt = this.createdAt,
25642 updatedAt = this.updatedAt,
25643 _attributes = this._attributes;
25644 return _objectSpread({}, _ConversationBase.prototype.toJSON.call(this), {
25645 creator: creator,
25646 system: system,
25647 transient: transient,
25648 muted: muted,
25649 mutedMembers: mutedMembers,
25650 createdAt: createdAt,
25651 updatedAt: updatedAt
25652 }, _attributes);
25653 };
25654
25655 _createClass(PersistentConversation, [{
25656 key: "createdAt",
25657 set: function set(value) {
25658 this._createdAt = decodeDate(value);
25659 },
25660 get: function get() {
25661 return this._createdAt;
25662 }
25663 }, {
25664 key: "updatedAt",
25665 set: function set(value) {
25666 this._updatedAt = decodeDate(value);
25667 },
25668 get: function get() {
25669 return this._updatedAt;
25670 }
25671 /**
25672 * 对话名字,对应 _Conversation 表中的 name
25673 * @type {String}
25674 */
25675
25676 }, {
25677 key: "name",
25678 get: function get() {
25679 return this.get('name');
25680 },
25681 set: function set(value) {
25682 this.set('name', value);
25683 }
25684 }]);
25685
25686 return PersistentConversation;
25687 }(ConversationBase);
25688
25689 /**
25690 * 对话成员角色枚举
25691 * @enum {String}
25692 * @since 4.0.0
25693 * @memberof module:leancloud-realtime
25694 */
25695
25696 var ConversationMemberRole = {
25697 /** 所有者 */
25698 OWNER: 'Owner',
25699
25700 /** 管理员 */
25701 MANAGER: 'Manager',
25702
25703 /** 成员 */
25704 MEMBER: 'Member'
25705 };
25706 Object.freeze(ConversationMemberRole);
25707
25708 var ConversationMemberInfo =
25709 /*#__PURE__*/
25710 function () {
25711 /**
25712 * 对话成员属性,保存了成员与某个对话相关的属性,对应 _ConversationMemberInfo 表
25713 * @since 4.0.0
25714 */
25715 function ConversationMemberInfo(_ref) {
25716 var conversation = _ref.conversation,
25717 memberId = _ref.memberId,
25718 role = _ref.role;
25719 if (!conversation) throw new Error('conversation requried');
25720 if (!memberId) throw new Error('memberId requried');
25721 Object.assign(internal(this), {
25722 conversation: conversation,
25723 memberId: memberId,
25724 role: role
25725 });
25726 }
25727 /**
25728 * 对话 Id
25729 * @type {String}
25730 * @readonly
25731 */
25732
25733
25734 var _proto = ConversationMemberInfo.prototype;
25735
25736 _proto.toJSON = function toJSON() {
25737 var conversationId = this.conversationId,
25738 memberId = this.memberId,
25739 role = this.role,
25740 isOwner = this.isOwner;
25741 return {
25742 conversationId: conversationId,
25743 memberId: memberId,
25744 role: role,
25745 isOwner: isOwner
25746 };
25747 };
25748
25749 _createClass(ConversationMemberInfo, [{
25750 key: "conversationId",
25751 get: function get() {
25752 return internal(this).conversation.id;
25753 }
25754 /**
25755 * 成员 Id
25756 * @type {String}
25757 * @readonly
25758 */
25759
25760 }, {
25761 key: "memberId",
25762 get: function get() {
25763 return internal(this).memberId;
25764 }
25765 /**
25766 * 角色
25767 * @type {module:leancloud-realtime.ConversationMemberRole | String}
25768 * @readonly
25769 */
25770
25771 }, {
25772 key: "role",
25773 get: function get() {
25774 if (this.isOwner) return ConversationMemberRole.OWNER;
25775 return internal(this).role;
25776 }
25777 /**
25778 * 是否是管理员
25779 * @type {Boolean}
25780 * @readonly
25781 */
25782
25783 }, {
25784 key: "isOwner",
25785 get: function get() {
25786 return this.memberId === internal(this).conversation.creator;
25787 }
25788 }]);
25789
25790 return ConversationMemberInfo;
25791 }();
25792
25793 /**
25794 * 普通对话
25795 *
25796 * 无法直接实例化,请使用 {@link IMClient#createConversation} 创建新的普通对话。
25797 * @extends PersistentConversation
25798 * @public
25799 */
25800
25801 var Conversation =
25802 /*#__PURE__*/
25803 function (_PersistentConversati) {
25804 _inheritsLoose(Conversation, _PersistentConversati);
25805
25806 function Conversation() {
25807 return _PersistentConversati.apply(this, arguments) || this;
25808 }
25809
25810 var _proto = Conversation.prototype;
25811
25812 _proto._addMembers = function _addMembers(members) {
25813 var _this = this;
25814
25815 _PersistentConversati.prototype._addMembers.call(this, members);
25816
25817 this.members = union(this.members, members);
25818
25819 var _internal = internal(this),
25820 memberInfoMap = _internal.memberInfoMap;
25821
25822 if (!memberInfoMap) return;
25823 members.forEach(function (memberId) {
25824 memberInfoMap[memberId] = memberInfoMap[memberId] || new ConversationMemberInfo({
25825 conversation: _this,
25826 memberId: memberId,
25827 role: ConversationMemberRole.MEMBER
25828 });
25829 });
25830 };
25831
25832 _proto._removeMembers = function _removeMembers(members) {
25833 _PersistentConversati.prototype._removeMembers.call(this, members);
25834
25835 this.members = difference(this.members, members);
25836
25837 var _internal2 = internal(this),
25838 memberInfoMap = _internal2.memberInfoMap;
25839
25840 if (!memberInfoMap) return;
25841 members.forEach(function (memberId) {
25842 delete memberInfoMap[memberId];
25843 });
25844 };
25845
25846 _proto._fetchAllMemberInfo =
25847 /*#__PURE__*/
25848 function () {
25849 var _fetchAllMemberInfo2 = _asyncToGenerator(
25850 /*#__PURE__*/
25851 regenerator.mark(function _callee() {
25852 var _this2 = this;
25853
25854 var response, memberInfos, memberInfoMap;
25855 return regenerator.wrap(function _callee$(_context) {
25856 while (1) {
25857 switch (_context.prev = _context.next) {
25858 case 0:
25859 _context.next = 2;
25860 return this._client._requestWithSessionToken({
25861 method: 'GET',
25862 path: '/classes/_ConversationMemberInfo',
25863 query: {
25864 where: {
25865 cid: this.id
25866 }
25867 }
25868 });
25869
25870 case 2:
25871 response = _context.sent;
25872 memberInfos = response.results.map(function (info) {
25873 return new ConversationMemberInfo({
25874 conversation: _this2,
25875 memberId: info.clientId,
25876 role: info.role
25877 });
25878 });
25879 memberInfoMap = {};
25880 memberInfos.forEach(function (memberInfo) {
25881 memberInfoMap[memberInfo.memberId] = memberInfo;
25882 });
25883 this.members.forEach(function (memberId) {
25884 memberInfoMap[memberId] = memberInfoMap[memberId] || new ConversationMemberInfo({
25885 conversation: _this2,
25886 memberId: memberId,
25887 role: ConversationMemberRole.MEMBER
25888 });
25889 });
25890 internal(this).memberInfoMap = memberInfoMap;
25891 return _context.abrupt("return", memberInfoMap);
25892
25893 case 9:
25894 case "end":
25895 return _context.stop();
25896 }
25897 }
25898 }, _callee, this);
25899 }));
25900
25901 function _fetchAllMemberInfo() {
25902 return _fetchAllMemberInfo2.apply(this, arguments);
25903 }
25904
25905 return _fetchAllMemberInfo;
25906 }()
25907 /**
25908 * 获取所有成员的对话属性
25909 * @since 4.0.0
25910 * @return {Promise.<ConversationMemberInfo[]>} 所有成员的对话属性列表
25911 */
25912 ;
25913
25914 _proto.getAllMemberInfo =
25915 /*#__PURE__*/
25916 function () {
25917 var _getAllMemberInfo = _asyncToGenerator(
25918 /*#__PURE__*/
25919 regenerator.mark(function _callee2() {
25920 var _ref,
25921 _ref$noCache,
25922 noCache,
25923 _internal3,
25924 memberInfoMap,
25925 _args2 = arguments;
25926
25927 return regenerator.wrap(function _callee2$(_context2) {
25928 while (1) {
25929 switch (_context2.prev = _context2.next) {
25930 case 0:
25931 _ref = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {}, _ref$noCache = _ref.noCache, noCache = _ref$noCache === void 0 ? false : _ref$noCache;
25932 _internal3 = internal(this), memberInfoMap = _internal3.memberInfoMap;
25933
25934 if (!(!memberInfoMap || noCache)) {
25935 _context2.next = 6;
25936 break;
25937 }
25938
25939 _context2.next = 5;
25940 return this._fetchAllMemberInfo();
25941
25942 case 5:
25943 memberInfoMap = _context2.sent;
25944
25945 case 6:
25946 return _context2.abrupt("return", this.members.map(function (memberId) {
25947 return memberInfoMap[memberId];
25948 }));
25949
25950 case 7:
25951 case "end":
25952 return _context2.stop();
25953 }
25954 }
25955 }, _callee2, this);
25956 }));
25957
25958 function getAllMemberInfo() {
25959 return _getAllMemberInfo.apply(this, arguments);
25960 }
25961
25962 return getAllMemberInfo;
25963 }()
25964 /**
25965 * 获取指定成员的对话属性
25966 * @since 4.0.0
25967 * @param {String} memberId 成员 Id
25968 * @return {Promise.<ConversationMemberInfo>} 指定成员的对话属性
25969 */
25970 ;
25971
25972 _proto.getMemberInfo =
25973 /*#__PURE__*/
25974 function () {
25975 var _getMemberInfo = _asyncToGenerator(
25976 /*#__PURE__*/
25977 regenerator.mark(function _callee3(memberId) {
25978 var _internal4, memberInfoMap;
25979
25980 return regenerator.wrap(function _callee3$(_context3) {
25981 while (1) {
25982 switch (_context3.prev = _context3.next) {
25983 case 0:
25984 if (!(this.members.indexOf(memberId) === -1)) {
25985 _context3.next = 2;
25986 break;
25987 }
25988
25989 throw new Error("".concat(memberId, " is not the mumber of conversation[").concat(this.id, "]"));
25990
25991 case 2:
25992 _internal4 = internal(this), memberInfoMap = _internal4.memberInfoMap;
25993
25994 if (memberInfoMap && memberInfoMap[memberId]) {
25995 _context3.next = 6;
25996 break;
25997 }
25998
25999 _context3.next = 6;
26000 return this.getAllMemberInfo();
26001
26002 case 6:
26003 return _context3.abrupt("return", internal(this).memberInfoMap[memberId]);
26004
26005 case 7:
26006 case "end":
26007 return _context3.stop();
26008 }
26009 }
26010 }, _callee3, this);
26011 }));
26012
26013 function getMemberInfo(_x) {
26014 return _getMemberInfo.apply(this, arguments);
26015 }
26016
26017 return getMemberInfo;
26018 }()
26019 /**
26020 * 更新指定用户的角色
26021 * @since 4.0.0
26022 * @param {String} memberId 成员 Id
26023 * @param {module:leancloud-realtime.ConversationMemberRole | String} role 角色
26024 * @return {Promise.<this>} self
26025 */
26026 ;
26027
26028 _proto.updateMemberRole =
26029 /*#__PURE__*/
26030 function () {
26031 var _updateMemberRole = _asyncToGenerator(
26032 /*#__PURE__*/
26033 regenerator.mark(function _callee4(memberId, role) {
26034 var _internal5, memberInfos;
26035
26036 return regenerator.wrap(function _callee4$(_context4) {
26037 while (1) {
26038 switch (_context4.prev = _context4.next) {
26039 case 0:
26040 this._debug('update member role');
26041
26042 if (!(role === ConversationMemberRole.OWNER)) {
26043 _context4.next = 3;
26044 break;
26045 }
26046
26047 throw createError({
26048 code: ErrorCode.OWNER_PROMOTION_NOT_ALLOWED
26049 });
26050
26051 case 3:
26052 _context4.next = 5;
26053 return this._send(new GenericCommand({
26054 op: OpType.member_info_update,
26055 convMessage: new ConvCommand({
26056 targetClientId: memberId,
26057 info: new ConvMemberInfo({
26058 pid: memberId,
26059 role: role
26060 })
26061 })
26062 }));
26063
26064 case 5:
26065 _internal5 = internal(this), memberInfos = _internal5.memberInfos;
26066
26067 if (memberInfos && memberInfos[memberId]) {
26068 internal(memberInfos[memberId]).role = role;
26069 }
26070
26071 return _context4.abrupt("return", this);
26072
26073 case 8:
26074 case "end":
26075 return _context4.stop();
26076 }
26077 }
26078 }, _callee4, this);
26079 }));
26080
26081 function updateMemberRole(_x2, _x3) {
26082 return _updateMemberRole.apply(this, arguments);
26083 }
26084
26085 return updateMemberRole;
26086 }();
26087
26088 return Conversation;
26089 }(PersistentConversation);
26090
26091 /**
26092 * 聊天室。
26093 *
26094 * 无法直接实例化,请使用 {@link IMClient#createChatRoom} 创建新的聊天室。
26095 * @since 4.0.0
26096 * @extends PersistentConversation
26097 * @public
26098 */
26099
26100 var ChatRoom =
26101 /*#__PURE__*/
26102 function (_PersistentConversati) {
26103 _inheritsLoose(ChatRoom, _PersistentConversati);
26104
26105 function ChatRoom() {
26106 return _PersistentConversati.apply(this, arguments) || this;
26107 }
26108
26109 return ChatRoom;
26110 }(PersistentConversation);
26111
26112 /**
26113 * 服务号。
26114 *
26115 * 服务号不支持在客户端创建。
26116 * @since 4.0.0
26117 * @extends PersistentConversation
26118 * @public
26119 */
26120
26121 var ServiceConversation =
26122 /*#__PURE__*/
26123 function (_PersistentConversati) {
26124 _inheritsLoose(ServiceConversation, _PersistentConversati);
26125
26126 function ServiceConversation() {
26127 return _PersistentConversati.apply(this, arguments) || this;
26128 }
26129
26130 var _proto = ServiceConversation.prototype;
26131
26132 /**
26133 * 订阅该服务号
26134 * @return {Promise.<this>} self
26135 */
26136 _proto.subscribe =
26137 /*#__PURE__*/
26138 function () {
26139 var _subscribe = _asyncToGenerator(
26140 /*#__PURE__*/
26141 regenerator.mark(function _callee() {
26142 return regenerator.wrap(function _callee$(_context) {
26143 while (1) {
26144 switch (_context.prev = _context.next) {
26145 case 0:
26146 return _context.abrupt("return", this.join());
26147
26148 case 1:
26149 case "end":
26150 return _context.stop();
26151 }
26152 }
26153 }, _callee, this);
26154 }));
26155
26156 function subscribe() {
26157 return _subscribe.apply(this, arguments);
26158 }
26159
26160 return subscribe;
26161 }()
26162 /**
26163 * 退订该服务号
26164 * @return {Promise.<this>} self
26165 */
26166 ;
26167
26168 _proto.unsubscribe =
26169 /*#__PURE__*/
26170 function () {
26171 var _unsubscribe = _asyncToGenerator(
26172 /*#__PURE__*/
26173 regenerator.mark(function _callee2() {
26174 return regenerator.wrap(function _callee2$(_context2) {
26175 while (1) {
26176 switch (_context2.prev = _context2.next) {
26177 case 0:
26178 return _context2.abrupt("return", this.quit());
26179
26180 case 1:
26181 case "end":
26182 return _context2.stop();
26183 }
26184 }
26185 }, _callee2, this);
26186 }));
26187
26188 function unsubscribe() {
26189 return _unsubscribe.apply(this, arguments);
26190 }
26191
26192 return unsubscribe;
26193 }();
26194
26195 return ServiceConversation;
26196 }(PersistentConversation);
26197
26198 var transformNotFoundError = function transformNotFoundError(error) {
26199 return error.code === ErrorCode.CONVERSATION_NOT_FOUND ? createError({
26200 code: ErrorCode.TEMPORARY_CONVERSATION_EXPIRED
26201 }) : error;
26202 };
26203 /**
26204 * 临时对话
26205 * @since 4.0.0
26206 * @extends ConversationBase
26207 * @public
26208 */
26209
26210
26211 var TemporaryConversation =
26212 /*#__PURE__*/
26213 function (_ConversationBase) {
26214 _inheritsLoose(TemporaryConversation, _ConversationBase);
26215
26216 /**
26217 * 无法直接实例化,请使用 {@link IMClient#createTemporaryConversation} 创建新的临时对话。
26218 */
26219 function TemporaryConversation(data, _ref, client) {
26220 var expiredAt = _ref.expiredAt;
26221 return _ConversationBase.call(this, _objectSpread({}, data, {
26222 expiredAt: expiredAt
26223 }), client) || this;
26224 }
26225 /**
26226 * 对话失效时间
26227 * @type {Date}
26228 */
26229
26230
26231 var _proto = TemporaryConversation.prototype;
26232
26233 _proto._send =
26234 /*#__PURE__*/
26235 function () {
26236 var _send2 = _asyncToGenerator(
26237 /*#__PURE__*/
26238 regenerator.mark(function _callee() {
26239 var _ConversationBase$pro,
26240 _len,
26241 args,
26242 _key,
26243 _args = arguments;
26244
26245 return regenerator.wrap(function _callee$(_context) {
26246 while (1) {
26247 switch (_context.prev = _context.next) {
26248 case 0:
26249 if (!this.expired) {
26250 _context.next = 2;
26251 break;
26252 }
26253
26254 throw createError({
26255 code: ErrorCode.TEMPORARY_CONVERSATION_EXPIRED
26256 });
26257
26258 case 2:
26259 _context.prev = 2;
26260
26261 for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26262 args[_key] = _args[_key];
26263 }
26264
26265 _context.next = 6;
26266 return (_ConversationBase$pro = _ConversationBase.prototype._send).call.apply(_ConversationBase$pro, [this].concat(args));
26267
26268 case 6:
26269 return _context.abrupt("return", _context.sent);
26270
26271 case 9:
26272 _context.prev = 9;
26273 _context.t0 = _context["catch"](2);
26274 throw transformNotFoundError(_context.t0);
26275
26276 case 12:
26277 case "end":
26278 return _context.stop();
26279 }
26280 }
26281 }, _callee, this, [[2, 9]]);
26282 }));
26283
26284 function _send() {
26285 return _send2.apply(this, arguments);
26286 }
26287
26288 return _send;
26289 }();
26290
26291 _proto.send =
26292 /*#__PURE__*/
26293 function () {
26294 var _send3 = _asyncToGenerator(
26295 /*#__PURE__*/
26296 regenerator.mark(function _callee2() {
26297 var _ConversationBase$pro2,
26298 _len2,
26299 args,
26300 _key2,
26301 _args2 = arguments;
26302
26303 return regenerator.wrap(function _callee2$(_context2) {
26304 while (1) {
26305 switch (_context2.prev = _context2.next) {
26306 case 0:
26307 _context2.prev = 0;
26308
26309 for (_len2 = _args2.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
26310 args[_key2] = _args2[_key2];
26311 }
26312
26313 _context2.next = 4;
26314 return (_ConversationBase$pro2 = _ConversationBase.prototype.send).call.apply(_ConversationBase$pro2, [this].concat(args));
26315
26316 case 4:
26317 return _context2.abrupt("return", _context2.sent);
26318
26319 case 7:
26320 _context2.prev = 7;
26321 _context2.t0 = _context2["catch"](0);
26322 throw transformNotFoundError(_context2.t0);
26323
26324 case 10:
26325 case "end":
26326 return _context2.stop();
26327 }
26328 }
26329 }, _callee2, this, [[0, 7]]);
26330 }));
26331
26332 function send() {
26333 return _send3.apply(this, arguments);
26334 }
26335
26336 return send;
26337 }();
26338
26339 _proto.toFullJSON = function toFullJSON() {
26340 var expiredAt = this.expiredAt;
26341 return _objectSpread({}, _ConversationBase.prototype.toFullJSON.call(this), {
26342 expiredAt: getTime(expiredAt)
26343 });
26344 };
26345
26346 _proto.toJSON = function toJSON() {
26347 var expiredAt = this.expiredAt,
26348 expired = this.expired;
26349 return _objectSpread({}, _ConversationBase.prototype.toJSON.call(this), {
26350 expiredAt: expiredAt,
26351 expired: expired
26352 });
26353 };
26354
26355 _createClass(TemporaryConversation, [{
26356 key: "expiredAt",
26357 set: function set(value) {
26358 this._expiredAt = decodeDate(value);
26359 },
26360 get: function get() {
26361 return this._expiredAt;
26362 }
26363 /**
26364 * 对话是否已失效
26365 * @type {Boolean}
26366 */
26367
26368 }, {
26369 key: "expired",
26370 get: function get() {
26371 return this.expiredAt < new Date();
26372 }
26373 }]);
26374
26375 return TemporaryConversation;
26376 }(ConversationBase);
26377
26378 var debug$8 = browser('LC:ConversationQuery');
26379
26380 var ConversationQuery =
26381 /*#__PURE__*/
26382 function () {
26383 ConversationQuery._encode = function _encode(value) {
26384 if (value instanceof Date) {
26385 return {
26386 __type: 'Date',
26387 iso: value.toJSON()
26388 };
26389 }
26390
26391 if (value instanceof RegExp) {
26392 return value.source;
26393 }
26394
26395 return value;
26396 };
26397
26398 ConversationQuery._quote = function _quote(s) {
26399 return "\\Q".concat(s.replace('\\E', '\\E\\\\E\\Q'), "\\E");
26400 };
26401
26402 ConversationQuery._calculateFlag = function _calculateFlag(options) {
26403 return ['withLastMessagesRefreshed', 'compact'].reduce( // eslint-disable-next-line no-bitwise
26404 function (prev, key) {
26405 return (prev << 1) + Boolean(options[key]);
26406 }, 0);
26407 }
26408 /**
26409 * Create a ConversationQuery
26410 * @param {IMClient} client
26411 */
26412 ;
26413
26414 function ConversationQuery(client) {
26415 this._client = client;
26416 this._where = {};
26417 this._extraOptions = {};
26418 }
26419
26420 var _proto = ConversationQuery.prototype;
26421
26422 _proto._addCondition = function _addCondition(key, condition, value) {
26423 // Check if we already have a condition
26424 if (!this._where[key]) {
26425 this._where[key] = {};
26426 }
26427
26428 this._where[key][condition] = this.constructor._encode(value);
26429 return this;
26430 };
26431
26432 _proto.toJSON = function toJSON() {
26433 var json = {
26434 where: this._where,
26435 flag: this.constructor._calculateFlag(this._extraOptions)
26436 };
26437 if (typeof this._skip !== 'undefined') json.skip = this._skip;
26438 if (typeof this._limit !== 'undefined') json.limit = this._limit;
26439 if (typeof this._order !== 'undefined') json.sort = this._order;
26440 debug$8(json);
26441 return json;
26442 }
26443 /**
26444 * 增加查询条件,指定聊天室的组员包含某些成员即可返回
26445 * @param {string[]} peerIds - 成员 ID 列表
26446 * @return {ConversationQuery} self
26447 */
26448 ;
26449
26450 _proto.containsMembers = function containsMembers(peerIds) {
26451 return this.containsAll('m', peerIds);
26452 }
26453 /**
26454 * 增加查询条件,指定聊天室的组员条件满足条件的才返回
26455 *
26456 * @param {string[]} - 成员 ID 列表
26457 * @param {Boolean} includeSelf - 是否包含自己
26458 * @return {ConversationQuery} self
26459 */
26460 ;
26461
26462 _proto.withMembers = function withMembers(peerIds, includeSelf) {
26463 var peerIdsSet = new Set(peerIds);
26464
26465 if (includeSelf) {
26466 peerIdsSet.add(this._client.id);
26467 }
26468
26469 this.sizeEqualTo('m', peerIdsSet.size);
26470 return this.containsMembers(Array.from(peerIdsSet));
26471 }
26472 /**
26473 * 增加查询条件,当 conversation 的属性中对应的字段满足等于条件时即可返回
26474 *
26475 * @param {string} key
26476 * @param value
26477 * @return {ConversationQuery} self
26478 */
26479 ;
26480
26481 _proto.equalTo = function equalTo(key, value) {
26482 this._where[key] = this.constructor._encode(value);
26483 return this;
26484 }
26485 /**
26486 * 增加查询条件,当 conversation 的属性中对应的字段满足小于条件时即可返回
26487 * @param {string} key
26488 * @param value
26489 * @return {ConversationQuery} self
26490 */
26491 ;
26492
26493 _proto.lessThan = function lessThan(key, value) {
26494 return this._addCondition(key, '$lt', value);
26495 }
26496 /**
26497 * 增加查询条件,当 conversation 的属性中对应的字段满足小于等于条件时即可返回
26498 * @param {string} key
26499 * @param value
26500 * @return {ConversationQuery} self
26501 */
26502 ;
26503
26504 _proto.lessThanOrEqualTo = function lessThanOrEqualTo(key, value) {
26505 return this._addCondition(key, '$lte', value);
26506 }
26507 /**
26508 * 增加查询条件,当 conversation 的属性中对应的字段满足大于条件时即可返回
26509 *
26510 * @param {string} key
26511 * @param value
26512 * @return {ConversationQuery} self
26513 */
26514 ;
26515
26516 _proto.greaterThan = function greaterThan(key, value) {
26517 return this._addCondition(key, '$gt', value);
26518 }
26519 /**
26520 * 增加查询条件,当 conversation 的属性中对应的字段满足大于等于条件时即可返回
26521 *
26522 * @param {string} key
26523 * @param value
26524 * @return {ConversationQuery} self
26525 */
26526 ;
26527
26528 _proto.greaterThanOrEqualTo = function greaterThanOrEqualTo(key, value) {
26529 return this._addCondition(key, '$gte', value);
26530 }
26531 /**
26532 * 增加查询条件,当 conversation 的属性中对应的字段满足不等于条件时即可返回
26533 *
26534 * @param {string} key
26535 * @param value
26536 * @return {ConversationQuery} self
26537 */
26538 ;
26539
26540 _proto.notEqualTo = function notEqualTo(key, value) {
26541 return this._addCondition(key, '$ne', value);
26542 }
26543 /**
26544 * 增加查询条件,当 conversation 存在指定的字段时即可返回
26545 *
26546 * @since 3.5.0
26547 * @param {string} key
26548 * @return {ConversationQuery} self
26549 */
26550 ;
26551
26552 _proto.exists = function exists(key) {
26553 return this._addCondition(key, '$exists', true);
26554 }
26555 /**
26556 * 增加查询条件,当 conversation 不存在指定的字段时即可返回
26557 *
26558 * @since 3.5.0
26559 * @param {string} key
26560 * @return {ConversationQuery} self
26561 */
26562 ;
26563
26564 _proto.doesNotExist = function doesNotExist(key) {
26565 return this._addCondition(key, '$exists', false);
26566 }
26567 /**
26568 * 增加查询条件,当 conversation 的属性中对应的字段对应的值包含在指定值中时即可返回
26569 *
26570 * @param {string} key
26571 * @param values
26572 * @return {ConversationQuery} self
26573 */
26574 ;
26575
26576 _proto.containedIn = function containedIn(key, values) {
26577 return this._addCondition(key, '$in', values);
26578 }
26579 /**
26580 * 增加查询条件,当 conversation 的属性中对应的字段对应的值不包含在指定值中时即可返回
26581 *
26582 * @param {string} key
26583 * @param values
26584 * @return {ConversationQuery} self
26585 */
26586 ;
26587
26588 _proto.notContainsIn = function notContainsIn(key, values) {
26589 return this._addCondition(key, '$nin', values);
26590 }
26591 /**
26592 * 增加查询条件,当conversation的属性中对应的字段中的元素包含所有的值才可返回
26593 *
26594 * @param {string} key
26595 * @param values
26596 * @return {ConversationQuery} self
26597 */
26598 ;
26599
26600 _proto.containsAll = function containsAll(key, values) {
26601 return this._addCondition(key, '$all', values);
26602 }
26603 /**
26604 * 增加查询条件,当 conversation 的属性中对应的字段对应的值包含此字符串即可返回
26605 *
26606 * @param {string} key
26607 * @param {string} subString
26608 * @return {ConversationQuery} self
26609 */
26610 ;
26611
26612 _proto.contains = function contains(key, subString) {
26613 return this._addCondition(key, '$regex', ConversationQuery._quote(subString));
26614 }
26615 /**
26616 * 增加查询条件,当 conversation 的属性中对应的字段对应的值以此字符串起始即可返回
26617 *
26618 * @param {string} key
26619 * @param {string} prefix
26620 * @return {ConversationQuery} self
26621 */
26622 ;
26623
26624 _proto.startsWith = function startsWith(key, prefix) {
26625 return this._addCondition(key, '$regex', "^".concat(ConversationQuery._quote(prefix)));
26626 }
26627 /**
26628 * 增加查询条件,当 conversation 的属性中对应的字段对应的值以此字符串结束即可返回
26629 *
26630 * @param {string} key
26631 * @param {string} suffix
26632 * @return {ConversationQuery} self
26633 */
26634 ;
26635
26636 _proto.endsWith = function endsWith(key, suffix) {
26637 return this._addCondition(key, '$regex', "".concat(ConversationQuery._quote(suffix), "$"));
26638 }
26639 /**
26640 * 增加查询条件,当 conversation 的属性中对应的字段对应的值满足提供的正则表达式即可返回
26641 *
26642 * @param {string} key
26643 * @param {RegExp} regex
26644 * @return {ConversationQuery} self
26645 */
26646 ;
26647
26648 _proto.matches = function matches(key, regex) {
26649 this._addCondition(key, '$regex', regex); // Javascript regex options support mig as inline options but store them
26650 // as properties of the object. We support mi & should migrate them to
26651 // modifiers
26652
26653
26654 var _modifiers = '';
26655
26656 if (regex.ignoreCase) {
26657 _modifiers += 'i';
26658 }
26659
26660 if (regex.multiline) {
26661 _modifiers += 'm';
26662 }
26663
26664 if (_modifiers && _modifiers.length) {
26665 this._addCondition(key, '$options', _modifiers);
26666 }
26667
26668 return this;
26669 }
26670 /**
26671 * 添加查询约束条件,查找 key 类型是数组,该数组的长度匹配提供的数值
26672 *
26673 * @param {string} key
26674 * @param {Number} length
26675 * @return {ConversationQuery} self
26676 */
26677 ;
26678
26679 _proto.sizeEqualTo = function sizeEqualTo(key, length) {
26680 return this._addCondition(key, '$size', length);
26681 }
26682 /**
26683 * 设置返回集合的大小上限
26684 *
26685 * @param {Number} limit - 上限
26686 * @return {ConversationQuery} self
26687 */
26688 ;
26689
26690 _proto.limit = function limit(_limit) {
26691 this._limit = _limit;
26692 return this;
26693 }
26694 /**
26695 * 设置返回集合的起始位置,一般用于分页
26696 *
26697 * @param {Number} skip - 起始位置跳过几个对象
26698 * @return {ConversationQuery} self
26699 */
26700 ;
26701
26702 _proto.skip = function skip(_skip) {
26703 this._skip = _skip;
26704 return this;
26705 }
26706 /**
26707 * 设置返回集合按照指定key进行增序排列
26708 *
26709 * @param {string} key
26710 * @return {ConversationQuery} self
26711 */
26712 ;
26713
26714 _proto.ascending = function ascending(key) {
26715 this._order = key;
26716 return this;
26717 }
26718 /**
26719 * 设置返回集合按照指定key进行增序排列,如果已设置其他排序,原排序的优先级较高
26720 *
26721 * @param {string} key
26722 * @return {ConversationQuery} self
26723 */
26724 ;
26725
26726 _proto.addAscending = function addAscending(key) {
26727 if (this._order) {
26728 this._order += ",".concat(key);
26729 } else {
26730 this._order = key;
26731 }
26732
26733 return this;
26734 }
26735 /**
26736 * 设置返回集合按照指定 key 进行降序排列
26737 *
26738 * @param {string} key
26739 * @return {ConversationQuery} self
26740 */
26741 ;
26742
26743 _proto.descending = function descending(key) {
26744 this._order = "-".concat(key);
26745 return this;
26746 }
26747 /**
26748 * 设置返回集合按照指定 key 进行降序排列,如果已设置其他排序,原排序的优先级较高
26749 *
26750 * @param {string} key
26751 * @return {ConversationQuery} self
26752 */
26753 ;
26754
26755 _proto.addDescending = function addDescending(key) {
26756 if (this._order) {
26757 this._order += ",-".concat(key);
26758 } else {
26759 this._order = "-".concat(key);
26760 }
26761
26762 return this;
26763 }
26764 /**
26765 * 设置返回的 conversations 刷新最后一条消息
26766 * @param {Boolean} [enabled=true]
26767 * @return {ConversationQuery} self
26768 */
26769 ;
26770
26771 _proto.withLastMessagesRefreshed = function withLastMessagesRefreshed() {
26772 var enabled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
26773 this._extraOptions.withLastMessagesRefreshed = enabled;
26774 return this;
26775 }
26776 /**
26777 * 设置返回的 conversations 为精简模式,即不含成员列表
26778 * @param {Boolean} [enabled=true]
26779 * @return {ConversationQuery} self
26780 */
26781 ;
26782
26783 _proto.compact = function compact() {
26784 var enabled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
26785 this._extraOptions.compact = enabled;
26786 return this;
26787 }
26788 /**
26789 * 执行查询
26790 * @return {Promise.<ConversationBase[]>}
26791 */
26792 ;
26793
26794 _proto.find =
26795 /*#__PURE__*/
26796 function () {
26797 var _find = _asyncToGenerator(
26798 /*#__PURE__*/
26799 regenerator.mark(function _callee() {
26800 return regenerator.wrap(function _callee$(_context) {
26801 while (1) {
26802 switch (_context.prev = _context.next) {
26803 case 0:
26804 return _context.abrupt("return", this._client._executeQuery(this));
26805
26806 case 1:
26807 case "end":
26808 return _context.stop();
26809 }
26810 }
26811 }, _callee, this);
26812 }));
26813
26814 function find() {
26815 return _find.apply(this, arguments);
26816 }
26817
26818 return find;
26819 }();
26820
26821 return ConversationQuery;
26822 }();
26823
26824 var debug$9 = browser('LC:SessionManager');
26825
26826 var SessionManager =
26827 /*#__PURE__*/
26828 function () {
26829 function SessionManager() {
26830 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
26831 refresh = _ref.refresh,
26832 onBeforeGetSessionToken = _ref.onBeforeGetSessionToken;
26833
26834 this.refresh = refresh;
26835 this._onBeforeGetSessionToken = onBeforeGetSessionToken;
26836 this.setSessionToken(null, 0);
26837 }
26838
26839 var _proto = SessionManager.prototype;
26840
26841 _proto.setSessionToken = function setSessionToken(token, ttl) {
26842 debug$9('set session token', token, ttl);
26843 var sessionToken = new Expirable(token, ttl * 1000);
26844 this._sessionToken = sessionToken;
26845 delete this._pendingSessionTokenPromise;
26846 return sessionToken;
26847 };
26848
26849 _proto.setSessionTokenAsync =
26850 /*#__PURE__*/
26851 function () {
26852 var _setSessionTokenAsync = _asyncToGenerator(
26853 /*#__PURE__*/
26854 regenerator.mark(function _callee(promise) {
26855 var _this = this;
26856
26857 var currentSessionToken;
26858 return regenerator.wrap(function _callee$(_context) {
26859 while (1) {
26860 switch (_context.prev = _context.next) {
26861 case 0:
26862 currentSessionToken = this._sessionToken;
26863 this._pendingSessionTokenPromise = promise.catch(function (error) {
26864 // revert, otherwise the following getSessionToken calls
26865 // will all be rejected
26866 _this._sessionToken = currentSessionToken;
26867 throw error;
26868 });
26869 _context.t0 = this.setSessionToken;
26870 _context.t1 = this;
26871 _context.t2 = _toConsumableArray;
26872 _context.next = 7;
26873 return this._pendingSessionTokenPromise;
26874
26875 case 7:
26876 _context.t3 = _context.sent;
26877 _context.t4 = (0, _context.t2)(_context.t3);
26878 return _context.abrupt("return", _context.t0.apply.call(_context.t0, _context.t1, _context.t4));
26879
26880 case 10:
26881 case "end":
26882 return _context.stop();
26883 }
26884 }
26885 }, _callee, this);
26886 }));
26887
26888 function setSessionTokenAsync(_x) {
26889 return _setSessionTokenAsync.apply(this, arguments);
26890 }
26891
26892 return setSessionTokenAsync;
26893 }();
26894
26895 _proto.getSessionToken =
26896 /*#__PURE__*/
26897 function () {
26898 var _getSessionToken = _asyncToGenerator(
26899 /*#__PURE__*/
26900 regenerator.mark(function _callee2() {
26901 var _ref2,
26902 _ref2$autoRefresh,
26903 autoRefresh,
26904 _ref3,
26905 value,
26906 originalValue,
26907 _ref4,
26908 newValue,
26909 _args2 = arguments;
26910
26911 return regenerator.wrap(function _callee2$(_context2) {
26912 while (1) {
26913 switch (_context2.prev = _context2.next) {
26914 case 0:
26915 _ref2 = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {}, _ref2$autoRefresh = _ref2.autoRefresh, autoRefresh = _ref2$autoRefresh === void 0 ? true : _ref2$autoRefresh;
26916 debug$9('get session token');
26917
26918 if (this._onBeforeGetSessionToken) {
26919 this._onBeforeGetSessionToken(this);
26920 }
26921
26922 _context2.t0 = this._sessionToken;
26923
26924 if (_context2.t0) {
26925 _context2.next = 8;
26926 break;
26927 }
26928
26929 _context2.next = 7;
26930 return this._pendingSessionTokenPromise;
26931
26932 case 7:
26933 _context2.t0 = _context2.sent;
26934
26935 case 8:
26936 _ref3 = _context2.t0;
26937 value = _ref3.value;
26938 originalValue = _ref3.originalValue;
26939
26940 if (!(value === Expirable.EXPIRED && autoRefresh && this.refresh)) {
26941 _context2.next = 19;
26942 break;
26943 }
26944
26945 debug$9('refresh expired session token');
26946 _context2.next = 15;
26947 return this.setSessionTokenAsync(this.refresh(this, originalValue));
26948
26949 case 15:
26950 _ref4 = _context2.sent;
26951 newValue = _ref4.value;
26952 debug$9('session token', newValue);
26953 return _context2.abrupt("return", newValue);
26954
26955 case 19:
26956 debug$9('session token', value);
26957 return _context2.abrupt("return", value);
26958
26959 case 21:
26960 case "end":
26961 return _context2.stop();
26962 }
26963 }
26964 }, _callee2, this);
26965 }));
26966
26967 function getSessionToken() {
26968 return _getSessionToken.apply(this, arguments);
26969 }
26970
26971 return getSessionToken;
26972 }();
26973
26974 _proto.revoke = function revoke() {
26975 if (this._sessionToken) this._sessionToken.expiredAt = -1;
26976 };
26977
26978 return SessionManager;
26979 }();
26980
26981 var _dec$2, _dec2, _class$3;
26982 var debug$a = browser('LC:IMClient');
26983 var INVITED$1 = INVITED,
26984 KICKED$1 = KICKED,
26985 MEMBERS_JOINED$1 = MEMBERS_JOINED,
26986 MEMBERS_LEFT$1 = MEMBERS_LEFT,
26987 MEMBER_INFO_UPDATED$1 = MEMBER_INFO_UPDATED,
26988 BLOCKED$1 = BLOCKED,
26989 UNBLOCKED$1 = UNBLOCKED,
26990 MEMBERS_BLOCKED$1 = MEMBERS_BLOCKED,
26991 MEMBERS_UNBLOCKED$1 = MEMBERS_UNBLOCKED,
26992 MUTED$1 = MUTED,
26993 UNMUTED$1 = UNMUTED,
26994 MEMBERS_MUTED$1 = MEMBERS_MUTED,
26995 MEMBERS_UNMUTED$1 = MEMBERS_UNMUTED,
26996 MESSAGE$2 = MESSAGE$1,
26997 UNREAD_MESSAGES_COUNT_UPDATE$1 = UNREAD_MESSAGES_COUNT_UPDATE,
26998 CLOSE$1 = CLOSE,
26999 CONFLICT$1 = CONFLICT,
27000 UNHANDLED_MESSAGE$1 = UNHANDLED_MESSAGE,
27001 CONVERSATION_INFO_UPDATED$1 = CONVERSATION_INFO_UPDATED,
27002 MESSAGE_RECALL$1 = MESSAGE_RECALL,
27003 MESSAGE_UPDATE$1 = MESSAGE_UPDATE,
27004 INFO_UPDATED$1 = INFO_UPDATED;
27005
27006 var isTemporaryConversatrionId = function isTemporaryConversatrionId(id) {
27007 return /^_tmp:/.test(id);
27008 };
27009 /**
27010 * 1 patch-msg
27011 * 1 temp-conv-msg
27012 * 0 auto-bind-deviceid-and-installation
27013 * 1 transient-msg-ack
27014 * 1 keep-notification
27015 * 1 partial-failed-msg
27016 * @ignore
27017 */
27018
27019
27020 var configBitmap = 59;
27021 var IMClient = (_dec$2 = throttle(1000), _dec2 = throttle(1000), (_class$3 =
27022 /*#__PURE__*/
27023 function (_EventEmitter) {
27024 _inheritsLoose(IMClient, _EventEmitter);
27025
27026 /**
27027 * 无法直接实例化,请使用 {@link Realtime#createIMClient} 创建新的 IMClient。
27028 *
27029 * @extends EventEmitter
27030 */
27031 function IMClient(id) {
27032 var _this;
27033
27034 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
27035 var props = arguments.length > 2 ? arguments[2] : undefined;
27036
27037 if (!(id === undefined || typeof id === 'string')) {
27038 throw new TypeError("Client id [".concat(id, "] is not a String"));
27039 }
27040
27041 _this = _EventEmitter.call(this) || this;
27042 Object.assign(_assertThisInitialized(_this), {
27043 /**
27044 * @var id {String} 客户端 id
27045 * @memberof IMClient#
27046 */
27047 id: id,
27048 options: options
27049 }, props);
27050
27051 if (!_this._messageParser) {
27052 throw new Error('IMClient must be initialized with a MessageParser');
27053 }
27054
27055 _this._conversationCache = new Cache("client:".concat(_this.id));
27056 _this._ackMessageBuffer = {};
27057 internal(_assertThisInitialized(_this)).lastPatchTime = Date.now();
27058 internal(_assertThisInitialized(_this)).lastNotificationTime = undefined;
27059 internal(_assertThisInitialized(_this))._eventemitter = new eventemitter3();
27060
27061 if (debug$a.enabled) {
27062 values_1(Event).forEach(function (event) {
27063 return _this.on(event, function () {
27064 for (var _len = arguments.length, payload = new Array(_len), _key = 0; _key < _len; _key++) {
27065 payload[_key] = arguments[_key];
27066 }
27067
27068 return _this._debug("".concat(event, " event emitted. %o"), payload);
27069 });
27070 });
27071 } // onIMClientCreate hook
27072
27073
27074 applyDecorators(_this._plugins.onIMClientCreate, _assertThisInitialized(_this));
27075 return _this;
27076 }
27077
27078 var _proto = IMClient.prototype;
27079
27080 _proto._debug = function _debug() {
27081 for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
27082 params[_key2] = arguments[_key2];
27083 }
27084
27085 debug$a.apply(void 0, params.concat(["[".concat(this.id, "]")]));
27086 }
27087 /**
27088 * @override
27089 * @private
27090 */
27091 ;
27092
27093 _proto._dispatchCommand =
27094 /*#__PURE__*/
27095 function () {
27096 var _dispatchCommand2 = _asyncToGenerator(
27097 /*#__PURE__*/
27098 regenerator.mark(function _callee(command) {
27099 return regenerator.wrap(function _callee$(_context) {
27100 while (1) {
27101 switch (_context.prev = _context.next) {
27102 case 0:
27103 this._debug(trim(command), 'received');
27104
27105 if (command.serverTs && command.notificationType === 1) {
27106 internal(this).lastNotificationTime = getTime(decodeDate(command.serverTs));
27107 }
27108
27109 _context.t0 = command.cmd;
27110 _context.next = _context.t0 === CommandType.conv ? 5 : _context.t0 === CommandType.direct ? 6 : _context.t0 === CommandType.session ? 7 : _context.t0 === CommandType.unread ? 8 : _context.t0 === CommandType.rcp ? 9 : _context.t0 === CommandType.patch ? 10 : 11;
27111 break;
27112
27113 case 5:
27114 return _context.abrupt("return", this._dispatchConvMessage(command));
27115
27116 case 6:
27117 return _context.abrupt("return", this._dispatchDirectMessage(command));
27118
27119 case 7:
27120 return _context.abrupt("return", this._dispatchSessionMessage(command));
27121
27122 case 8:
27123 return _context.abrupt("return", this._dispatchUnreadMessage(command));
27124
27125 case 9:
27126 return _context.abrupt("return", this._dispatchRcpMessage(command));
27127
27128 case 10:
27129 return _context.abrupt("return", this._dispatchPatchMessage(command));
27130
27131 case 11:
27132 return _context.abrupt("return", this.emit(UNHANDLED_MESSAGE$1, command));
27133
27134 case 12:
27135 case "end":
27136 return _context.stop();
27137 }
27138 }
27139 }, _callee, this);
27140 }));
27141
27142 function _dispatchCommand(_x) {
27143 return _dispatchCommand2.apply(this, arguments);
27144 }
27145
27146 return _dispatchCommand;
27147 }();
27148
27149 _proto._dispatchSessionMessage =
27150 /*#__PURE__*/
27151 function () {
27152 var _dispatchSessionMessage2 = _asyncToGenerator(
27153 /*#__PURE__*/
27154 regenerator.mark(function _callee2(message) {
27155 var _message$sessionMessa, code, reason;
27156
27157 return regenerator.wrap(function _callee2$(_context2) {
27158 while (1) {
27159 switch (_context2.prev = _context2.next) {
27160 case 0:
27161 _message$sessionMessa = message.sessionMessage, code = _message$sessionMessa.code, reason = _message$sessionMessa.reason;
27162 _context2.t0 = message.op;
27163 _context2.next = _context2.t0 === OpType.closed ? 4 : 8;
27164 break;
27165
27166 case 4:
27167 internal(this)._eventemitter.emit('close');
27168
27169 if (!(code === ErrorCode.SESSION_CONFLICT)) {
27170 _context2.next = 7;
27171 break;
27172 }
27173
27174 return _context2.abrupt("return", this.emit(CONFLICT$1, {
27175 reason: reason
27176 }));
27177
27178 case 7:
27179 return _context2.abrupt("return", this.emit(CLOSE$1, {
27180 code: code,
27181 reason: reason
27182 }));
27183
27184 case 8:
27185 this.emit(UNHANDLED_MESSAGE$1, message);
27186 throw new Error('Unrecognized session command');
27187
27188 case 10:
27189 case "end":
27190 return _context2.stop();
27191 }
27192 }
27193 }, _callee2, this);
27194 }));
27195
27196 function _dispatchSessionMessage(_x2) {
27197 return _dispatchSessionMessage2.apply(this, arguments);
27198 }
27199
27200 return _dispatchSessionMessage;
27201 }();
27202
27203 _proto._dispatchUnreadMessage = function _dispatchUnreadMessage(_ref) {
27204 var _this2 = this;
27205
27206 var _ref$unreadMessage = _ref.unreadMessage,
27207 convs = _ref$unreadMessage.convs,
27208 notifTime = _ref$unreadMessage.notifTime;
27209 internal(this).lastUnreadNotifTime = notifTime; // ensure all converstions are cached
27210
27211 return this.getConversations(convs.map(function (conv) {
27212 return conv.cid;
27213 })).then(function () {
27214 return (// update conversations data
27215 Promise.all(convs.map(function (_ref2) {
27216 var cid = _ref2.cid,
27217 unread = _ref2.unread,
27218 mid = _ref2.mid,
27219 ts = _ref2.timestamp,
27220 from = _ref2.from,
27221 data = _ref2.data,
27222 binaryMsg = _ref2.binaryMsg,
27223 patchTimestamp = _ref2.patchTimestamp,
27224 mentioned = _ref2.mentioned;
27225
27226 var conversation = _this2._conversationCache.get(cid); // deleted conversation
27227
27228
27229 if (!conversation) return null;
27230 var timestamp;
27231
27232 if (ts) {
27233 timestamp = decodeDate(ts);
27234 conversation.lastMessageAt = timestamp; // eslint-disable-line no-param-reassign
27235 }
27236
27237 return (mid ? _this2._messageParser.parse(binaryMsg || data).then(function (message) {
27238 var messageProps = {
27239 id: mid,
27240 cid: cid,
27241 timestamp: timestamp,
27242 updatedAt: patchTimestamp,
27243 from: from
27244 };
27245 Object.assign(message, messageProps);
27246 conversation.lastMessage = message; // eslint-disable-line no-param-reassign
27247 }) : Promise.resolve()).then(function () {
27248 conversation._setUnreadMessagesMentioned(mentioned);
27249
27250 var countNotUpdated = unread === internal(conversation).unreadMessagesCount;
27251 if (countNotUpdated) return null; // to be filtered
27252 // manipulate internal property directly to skip unreadmessagescountupdate event
27253
27254 internal(conversation).unreadMessagesCount = unread;
27255 return conversation;
27256 }); // filter conversations without unread count update
27257 })).then(function (conversations) {
27258 return conversations.filter(function (conversation) {
27259 return conversation;
27260 });
27261 })
27262 );
27263 }).then(function (conversations) {
27264 if (conversations.length) {
27265 /**
27266 * 未读消息数目更新
27267 * @event IMClient#UNREAD_MESSAGES_COUNT_UPDATE
27268 * @since 3.4.0
27269 * @param {Conversation[]} conversations 未读消息数目有更新的对话列表
27270 */
27271 _this2.emit(UNREAD_MESSAGES_COUNT_UPDATE$1, conversations);
27272 }
27273 });
27274 };
27275
27276 _proto._dispatchRcpMessage =
27277 /*#__PURE__*/
27278 function () {
27279 var _dispatchRcpMessage2 = _asyncToGenerator(
27280 /*#__PURE__*/
27281 regenerator.mark(function _callee3(message) {
27282 var rcpMessage, read, conversationId, messageId, timestamp, conversation;
27283 return regenerator.wrap(function _callee3$(_context3) {
27284 while (1) {
27285 switch (_context3.prev = _context3.next) {
27286 case 0:
27287 rcpMessage = message.rcpMessage, read = message.rcpMessage.read;
27288 conversationId = rcpMessage.cid;
27289 messageId = rcpMessage.id;
27290 timestamp = decodeDate(rcpMessage.t);
27291 conversation = this._conversationCache.get(conversationId); // conversation not cached means the client does not send the message
27292 // during this session
27293
27294 if (conversation) {
27295 _context3.next = 7;
27296 break;
27297 }
27298
27299 return _context3.abrupt("return");
27300
27301 case 7:
27302 conversation._handleReceipt({
27303 messageId: messageId,
27304 timestamp: timestamp,
27305 read: read
27306 });
27307
27308 case 8:
27309 case "end":
27310 return _context3.stop();
27311 }
27312 }
27313 }, _callee3, this);
27314 }));
27315
27316 function _dispatchRcpMessage(_x3) {
27317 return _dispatchRcpMessage2.apply(this, arguments);
27318 }
27319
27320 return _dispatchRcpMessage;
27321 }();
27322
27323 _proto._dispatchPatchMessage = function _dispatchPatchMessage(_ref3) {
27324 var _this3 = this;
27325
27326 var patches = _ref3.patchMessage.patches;
27327 // ensure all converstions are cached
27328 return this.getConversations(patches.map(function (patch) {
27329 return patch.cid;
27330 })).then(function () {
27331 return Promise.all(patches.map(function (_ref4) {
27332 var cid = _ref4.cid,
27333 mid = _ref4.mid,
27334 timestamp = _ref4.timestamp,
27335 recall = _ref4.recall,
27336 data = _ref4.data,
27337 patchTimestamp = _ref4.patchTimestamp,
27338 from = _ref4.from,
27339 binaryMsg = _ref4.binaryMsg,
27340 mentionAll = _ref4.mentionAll,
27341 mentionPids = _ref4.mentionPids,
27342 patchCode = _ref4.patchCode,
27343 patchReason = _ref4.patchReason;
27344
27345 var conversation = _this3._conversationCache.get(cid); // deleted conversation
27346
27347
27348 if (!conversation) return null;
27349 return _this3._messageParser.parse(binaryMsg || data).then(function (message) {
27350 var patchTime = getTime(decodeDate(patchTimestamp));
27351 var messageProps = {
27352 id: mid,
27353 cid: cid,
27354 timestamp: timestamp,
27355 updatedAt: patchTime,
27356 from: from,
27357 mentionList: mentionPids,
27358 mentionedAll: mentionAll
27359 };
27360 Object.assign(message, messageProps);
27361
27362 message._setStatus(MessageStatus.SENT);
27363
27364 message._updateMentioned(_this3.id);
27365
27366 if (internal(_this3).lastPatchTime < patchTime) {
27367 internal(_this3).lastPatchTime = patchTime;
27368 } // update conversation lastMessage
27369
27370
27371 if (conversation.lastMessage && conversation.lastMessage.id === mid) {
27372 conversation.lastMessage = message; // eslint-disable-line no-param-reassign
27373 }
27374
27375 var reason;
27376
27377 if (patchCode) {
27378 reason = {
27379 code: patchCode.toNumber(),
27380 detail: patchReason
27381 };
27382 }
27383
27384 if (recall) {
27385 /**
27386 * 消息被撤回
27387 * @event IMClient#MESSAGE_RECALL
27388 * @param {AVMessage} message 被撤回的消息
27389 * @param {ConversationBase} conversation 消息所在的会话
27390 * @param {PatchReason} [reason] 撤回的原因,不存在代表是发送者主动撤回
27391 */
27392 _this3.emit(MESSAGE_RECALL$1, message, conversation, reason);
27393 /**
27394 * 消息被撤回
27395 * @event ConversationBase#MESSAGE_RECALL
27396 * @param {AVMessage} message 被撤回的消息
27397 * @param {PatchReason} [reason] 撤回的原因,不存在代表是发送者主动撤回
27398 */
27399
27400
27401 conversation.emit(MESSAGE_RECALL$1, message, reason);
27402 } else {
27403 /**
27404 * 消息被修改
27405 * @event IMClient#MESSAGE_UPDATE
27406 * @param {AVMessage} message 被修改的消息
27407 * @param {ConversationBase} conversation 消息所在的会话
27408 * @param {PatchReason} [reason] 修改的原因,不存在代表是发送者主动修改
27409 */
27410 _this3.emit(MESSAGE_UPDATE$1, message, conversation, reason);
27411 /**
27412 * 消息被修改
27413 * @event ConversationBase#MESSAGE_UPDATE
27414 * @param {AVMessage} message 被修改的消息
27415 * @param {PatchReason} [reason] 修改的原因,不存在代表是发送者主动修改
27416 */
27417
27418
27419 conversation.emit(MESSAGE_UPDATE$1, message, reason);
27420 }
27421 });
27422 }));
27423 });
27424 };
27425
27426 _proto._dispatchConvMessage =
27427 /*#__PURE__*/
27428 function () {
27429 var _dispatchConvMessage2 = _asyncToGenerator(
27430 /*#__PURE__*/
27431 regenerator.mark(function _callee4(message) {
27432 var convMessage, _message$convMessage, initBy, m, info, attr, conversation, payload, _payload, _payload2, _payload3, _payload4, _payload5, _payload6, _payload7, _payload8, _payload9, _payload10, _payload11, pid, role, _internal, memberInfoMap, memberInfo, _payload12, attributes, _payload13;
27433
27434 return regenerator.wrap(function _callee4$(_context4) {
27435 while (1) {
27436 switch (_context4.prev = _context4.next) {
27437 case 0:
27438 convMessage = message.convMessage, _message$convMessage = message.convMessage, initBy = _message$convMessage.initBy, m = _message$convMessage.m, info = _message$convMessage.info, attr = _message$convMessage.attr;
27439 _context4.next = 3;
27440 return this.getConversation(convMessage.cid);
27441
27442 case 3:
27443 conversation = _context4.sent;
27444 _context4.t0 = message.op;
27445 _context4.next = _context4.t0 === OpType.joined ? 7 : _context4.t0 === OpType.left ? 12 : _context4.t0 === OpType.members_joined ? 17 : _context4.t0 === OpType.members_left ? 22 : _context4.t0 === OpType.members_blocked ? 27 : _context4.t0 === OpType.members_unblocked ? 31 : _context4.t0 === OpType.blocked ? 35 : _context4.t0 === OpType.unblocked ? 39 : _context4.t0 === OpType.members_shutuped ? 43 : _context4.t0 === OpType.members_unshutuped ? 47 : _context4.t0 === OpType.shutuped ? 51 : _context4.t0 === OpType.unshutuped ? 55 : _context4.t0 === OpType.member_info_changed ? 59 : _context4.t0 === OpType.updated ? 71 : 77;
27446 break;
27447
27448 case 7:
27449 conversation._addMembers([this.id]);
27450
27451 payload = {
27452 invitedBy: initBy
27453 };
27454 /**
27455 * 当前用户被添加至某个对话
27456 * @event IMClient#INVITED
27457 * @param {Object} payload
27458 * @param {String} payload.invitedBy 邀请者 id
27459 * @param {ConversationBase} conversation
27460 */
27461
27462 this.emit(INVITED$1, payload, conversation);
27463 /**
27464 * 当前用户被添加至当前对话
27465 * @event ConversationBase#INVITED
27466 * @param {Object} payload
27467 * @param {String} payload.invitedBy 该移除操作的发起者 id
27468 */
27469
27470 conversation.emit(INVITED$1, payload);
27471 return _context4.abrupt("return");
27472
27473 case 12:
27474 conversation._removeMembers([this.id]);
27475
27476 _payload = {
27477 kickedBy: initBy
27478 };
27479 /**
27480 * 当前用户被从某个对话中移除
27481 * @event IMClient#KICKED
27482 * @param {Object} payload
27483 * @param {String} payload.kickedBy 该移除操作的发起者 id
27484 * @param {ConversationBase} conversation
27485 */
27486
27487 this.emit(KICKED$1, _payload, conversation);
27488 /**
27489 * 当前用户被从当前对话中移除
27490 * @event ConversationBase#KICKED
27491 * @param {Object} payload
27492 * @param {String} payload.kickedBy 该移除操作的发起者 id
27493 */
27494
27495 conversation.emit(KICKED$1, _payload);
27496 return _context4.abrupt("return");
27497
27498 case 17:
27499 conversation._addMembers(m);
27500
27501 _payload2 = {
27502 invitedBy: initBy,
27503 members: m
27504 };
27505 /**
27506 * 有用户被添加至某个对话
27507 * @event IMClient#MEMBERS_JOINED
27508 * @param {Object} payload
27509 * @param {String[]} payload.members 被添加的用户 id 列表
27510 * @param {String} payload.invitedBy 邀请者 id
27511 * @param {ConversationBase} conversation
27512 */
27513
27514 this.emit(MEMBERS_JOINED$1, _payload2, conversation);
27515 /**
27516 * 有成员被添加至当前对话
27517 * @event ConversationBase#MEMBERS_JOINED
27518 * @param {Object} payload
27519 * @param {String[]} payload.members 被添加的成员 id 列表
27520 * @param {String} payload.invitedBy 邀请者 id
27521 */
27522
27523 conversation.emit(MEMBERS_JOINED$1, _payload2);
27524 return _context4.abrupt("return");
27525
27526 case 22:
27527 conversation._removeMembers(m);
27528
27529 _payload3 = {
27530 kickedBy: initBy,
27531 members: m
27532 };
27533 /**
27534 * 有成员被从某个对话中移除
27535 * @event IMClient#MEMBERS_LEFT
27536 * @param {Object} payload
27537 * @param {String[]} payload.members 被移除的成员 id 列表
27538 * @param {String} payload.kickedBy 该移除操作的发起者 id
27539 * @param {ConversationBase} conversation
27540 */
27541
27542 this.emit(MEMBERS_LEFT$1, _payload3, conversation);
27543 /**
27544 * 有成员被从当前对话中移除
27545 * @event ConversationBase#MEMBERS_LEFT
27546 * @param {Object} payload
27547 * @param {String[]} payload.members 被移除的成员 id 列表
27548 * @param {String} payload.kickedBy 该移除操作的发起者 id
27549 */
27550
27551 conversation.emit(MEMBERS_LEFT$1, _payload3);
27552 return _context4.abrupt("return");
27553
27554 case 27:
27555 _payload4 = {
27556 blockedBy: initBy,
27557 members: m
27558 };
27559 /**
27560 * 有成员被加入某个对话的黑名单
27561 * @event IMClient#MEMBERS_BLOCKED
27562 * @param {Object} payload
27563 * @param {String[]} payload.members 成员 id 列表
27564 * @param {String} payload.blockedBy 该操作的发起者 id
27565 * @param {ConversationBase} conversation
27566 */
27567
27568 this.emit(MEMBERS_BLOCKED$1, _payload4, conversation);
27569 /**
27570 * 有成员被加入当前对话的黑名单
27571 * @event ConversationBase#MEMBERS_BLOCKED
27572 * @param {Object} payload
27573 * @param {String[]} payload.members 成员 id 列表
27574 * @param {String} payload.blockedBy 该操作的发起者 id
27575 */
27576
27577 conversation.emit(MEMBERS_BLOCKED$1, _payload4);
27578 return _context4.abrupt("return");
27579
27580 case 31:
27581 _payload5 = {
27582 unblockedBy: initBy,
27583 members: m
27584 };
27585 /**
27586 * 有成员被移出某个对话的黑名单
27587 * @event IMClient#MEMBERS_UNBLOCKED
27588 * @param {Object} payload
27589 * @param {String[]} payload.members 成员 id 列表
27590 * @param {String} payload.unblockedBy 该操作的发起者 id
27591 * @param {ConversationBase} conversation
27592 */
27593
27594 this.emit(MEMBERS_UNBLOCKED$1, _payload5, conversation);
27595 /**
27596 * 有成员被移出当前对话的黑名单
27597 * @event ConversationBase#MEMBERS_UNBLOCKED
27598 * @param {Object} payload
27599 * @param {String[]} payload.members 成员 id 列表
27600 * @param {String} payload.unblockedBy 该操作的发起者 id
27601 */
27602
27603 conversation.emit(MEMBERS_UNBLOCKED$1, _payload5);
27604 return _context4.abrupt("return");
27605
27606 case 35:
27607 _payload6 = {
27608 blockedBy: initBy
27609 };
27610 /**
27611 * 当前用户被加入某个对话的黑名单
27612 * @event IMClient#BLOCKED
27613 * @param {Object} payload
27614 * @param {String} payload.blockedBy 该操作的发起者 id
27615 * @param {ConversationBase} conversation
27616 */
27617
27618 this.emit(BLOCKED$1, _payload6, conversation);
27619 /**
27620 * 当前用户被加入当前对话的黑名单
27621 * @event ConversationBase#BLOCKED
27622 * @param {Object} payload
27623 * @param {String} payload.blockedBy 该操作的发起者 id
27624 */
27625
27626 conversation.emit(BLOCKED$1, _payload6);
27627 return _context4.abrupt("return");
27628
27629 case 39:
27630 _payload7 = {
27631 unblockedBy: initBy
27632 };
27633 /**
27634 * 当前用户被移出某个对话的黑名单
27635 * @event IMClient#UNBLOCKED
27636 * @param {Object} payload
27637 * @param {String} payload.unblockedBy 该操作的发起者 id
27638 * @param {ConversationBase} conversation
27639 */
27640
27641 this.emit(UNBLOCKED$1, _payload7, conversation);
27642 /**
27643 * 当前用户被移出当前对话的黑名单
27644 * @event ConversationBase#UNBLOCKED
27645 * @param {Object} payload
27646 * @param {String} payload.unblockedBy 该操作的发起者 id
27647 */
27648
27649 conversation.emit(UNBLOCKED$1, _payload7);
27650 return _context4.abrupt("return");
27651
27652 case 43:
27653 _payload8 = {
27654 mutedBy: initBy,
27655 members: m
27656 };
27657 /**
27658 * 有成员在某个对话中被禁言
27659 * @event IMClient#MEMBERS_MUTED
27660 * @param {Object} payload
27661 * @param {String[]} payload.members 成员 id 列表
27662 * @param {String} payload.mutedBy 该操作的发起者 id
27663 * @param {ConversationBase} conversation
27664 */
27665
27666 this.emit(MEMBERS_MUTED$1, _payload8, conversation);
27667 /**
27668 * 有成员在当前对话中被禁言
27669 * @event ConversationBase#MEMBERS_MUTED
27670 * @param {Object} payload
27671 * @param {String[]} payload.members 成员 id 列表
27672 * @param {String} payload.mutedBy 该操作的发起者 id
27673 */
27674
27675 conversation.emit(MEMBERS_MUTED$1, _payload8);
27676 return _context4.abrupt("return");
27677
27678 case 47:
27679 _payload9 = {
27680 unmutedBy: initBy,
27681 members: m
27682 };
27683 /**
27684 * 有成员在某个对话中被解除禁言
27685 * @event IMClient#MEMBERS_UNMUTED
27686 * @param {Object} payload
27687 * @param {String[]} payload.members 成员 id 列表
27688 * @param {String} payload.unmutedBy 该操作的发起者 id
27689 * @param {ConversationBase} conversation
27690 */
27691
27692 this.emit(MEMBERS_UNMUTED$1, _payload9, conversation);
27693 /**
27694 * 有成员在当前对话中被解除禁言
27695 * @event ConversationBase#MEMBERS_UNMUTED
27696 * @param {Object} payload
27697 * @param {String[]} payload.members 成员 id 列表
27698 * @param {String} payload.unmutedBy 该操作的发起者 id
27699 */
27700
27701 conversation.emit(MEMBERS_UNMUTED$1, _payload9);
27702 return _context4.abrupt("return");
27703
27704 case 51:
27705 _payload10 = {
27706 mutedBy: initBy
27707 };
27708 /**
27709 * 有成员在某个对话中被禁言
27710 * @event IMClient#MUTED
27711 * @param {Object} payload
27712 * @param {String} payload.mutedBy 该操作的发起者 id
27713 * @param {ConversationBase} conversation
27714 */
27715
27716 this.emit(MUTED$1, _payload10, conversation);
27717 /**
27718 * 有成员在当前对话中被禁言
27719 * @event ConversationBase#MUTED
27720 * @param {Object} payload
27721 * @param {String} payload.mutedBy 该操作的发起者 id
27722 */
27723
27724 conversation.emit(MUTED$1, _payload10);
27725 return _context4.abrupt("return");
27726
27727 case 55:
27728 _payload11 = {
27729 unmutedBy: initBy
27730 };
27731 /**
27732 * 有成员在某个对话中被解除禁言
27733 * @event IMClient#UNMUTED
27734 * @param {Object} payload
27735 * @param {String} payload.unmutedBy 该操作的发起者 id
27736 * @param {ConversationBase} conversation
27737 */
27738
27739 this.emit(UNMUTED$1, _payload11, conversation);
27740 /**
27741 * 有成员在当前对话中被解除禁言
27742 * @event ConversationBase#UNMUTED
27743 * @param {Object} payload
27744 * @param {String} payload.unmutedBy 该操作的发起者 id
27745 */
27746
27747 conversation.emit(UNMUTED$1, _payload11);
27748 return _context4.abrupt("return");
27749
27750 case 59:
27751 pid = info.pid, role = info.role;
27752 _internal = internal(conversation), memberInfoMap = _internal.memberInfoMap; // 如果不存在缓存,且不是 role 的更新,则不通知
27753
27754 if (!(!memberInfoMap && !role)) {
27755 _context4.next = 63;
27756 break;
27757 }
27758
27759 return _context4.abrupt("return");
27760
27761 case 63:
27762 _context4.next = 65;
27763 return conversation.getMemberInfo(pid);
27764
27765 case 65:
27766 memberInfo = _context4.sent;
27767 internal(memberInfo).role = role;
27768 _payload12 = {
27769 member: pid,
27770 memberInfo: memberInfo,
27771 updatedBy: initBy
27772 };
27773 /**
27774 * 有成员的对话信息被更新
27775 * @event IMClient#MEMBER_INFO_UPDATED
27776 * @param {Object} payload
27777 * @param {String} payload.member 被更新对话信息的成员 id
27778 * @param {ConversationMumberInfo} payload.memberInfo 被更新的成员对话信息
27779 * @param {String} payload.updatedBy 该操作的发起者 id
27780 * @param {ConversationBase} conversation
27781 */
27782
27783 this.emit(MEMBER_INFO_UPDATED$1, _payload12, conversation);
27784 /**
27785 * 有成员的对话信息被更新
27786 * @event ConversationBase#MEMBER_INFO_UPDATED
27787 * @param {Object} payload
27788 * @param {String} payload.member 被更新对话信息的成员 id
27789 * @param {ConversationMumberInfo} payload.memberInfo 被更新的成员对话信息
27790 * @param {String} payload.updatedBy 该操作的发起者 id
27791 */
27792
27793 conversation.emit(MEMBER_INFO_UPDATED$1, _payload12);
27794 return _context4.abrupt("return");
27795
27796 case 71:
27797 attributes = decode(JSON.parse(attr.data));
27798
27799 conversation._updateServerAttributes(attributes);
27800
27801 _payload13 = {
27802 attributes: attributes,
27803 updatedBy: initBy
27804 };
27805 /**
27806 * 该对话信息被更新
27807 * @event IMClient#CONVERSATION_INFO_UPDATED
27808 * @param {Object} payload
27809 * @param {Object} payload.attributes 被更新的属性
27810 * @param {String} payload.updatedBy 该操作的发起者 id
27811 * @param {ConversationBase} conversation
27812 */
27813
27814 this.emit(CONVERSATION_INFO_UPDATED$1, _payload13, conversation);
27815 /**
27816 * 有对话信息被更新
27817 * @event ConversationBase#INFO_UPDATED
27818 * @param {Object} payload
27819 * @param {Object} payload.attributes 被更新的属性
27820 * @param {String} payload.updatedBy 该操作的发起者 id
27821 */
27822
27823 conversation.emit(INFO_UPDATED$1, _payload13);
27824 return _context4.abrupt("return");
27825
27826 case 77:
27827 this.emit(UNHANDLED_MESSAGE$1, message);
27828 throw new Error('Unrecognized conversation command');
27829
27830 case 79:
27831 case "end":
27832 return _context4.stop();
27833 }
27834 }
27835 }, _callee4, this);
27836 }));
27837
27838 function _dispatchConvMessage(_x4) {
27839 return _dispatchConvMessage2.apply(this, arguments);
27840 }
27841
27842 return _dispatchConvMessage;
27843 }();
27844
27845 _proto._dispatchDirectMessage = function _dispatchDirectMessage(originalMessage) {
27846 var _this4 = this;
27847
27848 var directMessage = originalMessage.directMessage,
27849 _originalMessage$dire = originalMessage.directMessage,
27850 id = _originalMessage$dire.id,
27851 cid = _originalMessage$dire.cid,
27852 fromPeerId = _originalMessage$dire.fromPeerId,
27853 timestamp = _originalMessage$dire.timestamp,
27854 transient = _originalMessage$dire.transient,
27855 patchTimestamp = _originalMessage$dire.patchTimestamp,
27856 mentionPids = _originalMessage$dire.mentionPids,
27857 mentionAll = _originalMessage$dire.mentionAll,
27858 binaryMsg = _originalMessage$dire.binaryMsg,
27859 msg = _originalMessage$dire.msg;
27860 var content = binaryMsg ? binaryMsg.toArrayBuffer() : msg;
27861 return Promise.all([this.getConversation(directMessage.cid), this._messageParser.parse(content)]).then(function (_ref5) {
27862 var _ref6 = _slicedToArray(_ref5, 2),
27863 conversation = _ref6[0],
27864 message = _ref6[1];
27865
27866 // deleted conversation
27867 if (!conversation) return undefined;
27868 var messageProps = {
27869 id: id,
27870 cid: cid,
27871 timestamp: timestamp,
27872 updatedAt: patchTimestamp,
27873 from: fromPeerId,
27874 mentionList: mentionPids,
27875 mentionedAll: mentionAll
27876 };
27877 Object.assign(message, messageProps);
27878
27879 message._updateMentioned(_this4.id);
27880
27881 message._setStatus(MessageStatus.SENT); // filter outgoing message sent from another device
27882
27883
27884 if (message.from !== _this4.id) {
27885 if (!(transient || conversation.transient)) {
27886 _this4._sendAck(message);
27887 }
27888 }
27889
27890 return _this4._dispatchParsedMessage(message, conversation);
27891 });
27892 };
27893
27894 _proto._dispatchParsedMessage = function _dispatchParsedMessage(message, conversation) {
27895 var _this5 = this;
27896
27897 // beforeMessageDispatch hook
27898 return applyDispatcher(this._plugins.beforeMessageDispatch, [message, conversation]).then(function (shouldDispatch) {
27899 if (shouldDispatch === false) return;
27900 conversation.lastMessage = message; // eslint-disable-line no-param-reassign
27901
27902 conversation.lastMessageAt = message.timestamp; // eslint-disable-line no-param-reassign
27903 // filter outgoing message sent from another device
27904
27905 if (message.from !== _this5.id) {
27906 conversation.unreadMessagesCount += 1; // eslint-disable-line no-param-reassign
27907
27908 if (message.mentioned) conversation._setUnreadMessagesMentioned(true);
27909 }
27910 /**
27911 * 当前用户收到消息
27912 * @event IMClient#MESSAGE
27913 * @param {Message} message
27914 * @param {ConversationBase} conversation 收到消息的对话
27915 */
27916
27917
27918 _this5.emit(MESSAGE$2, message, conversation);
27919 /**
27920 * 当前对话收到消息
27921 * @event ConversationBase#MESSAGE
27922 * @param {Message} message
27923 */
27924
27925
27926 conversation.emit(MESSAGE$2, message);
27927 });
27928 };
27929
27930 _proto._sendAck = function _sendAck(message) {
27931 this._debug('send ack for %O', message);
27932
27933 var cid = message.cid;
27934
27935 if (!cid) {
27936 throw new Error('missing cid');
27937 }
27938
27939 if (!this._ackMessageBuffer[cid]) {
27940 this._ackMessageBuffer[cid] = [];
27941 }
27942
27943 this._ackMessageBuffer[cid].push(message);
27944
27945 return this._doSendAck();
27946 } // jsdoc-ignore-start
27947 ;
27948
27949 _proto. // jsdoc-ignore-end
27950 _doSendAck = function _doSendAck() {
27951 var _this6 = this;
27952
27953 // if not connected, just skip everything
27954 if (!this._connection.is('connected')) return;
27955
27956 this._debug('do send ack %O', this._ackMessageBuffer);
27957
27958 Promise.all(Object.keys(this._ackMessageBuffer).map(function (cid) {
27959 var convAckMessages = _this6._ackMessageBuffer[cid];
27960 var timestamps = convAckMessages.map(function (message) {
27961 return message.timestamp;
27962 });
27963 var command = new GenericCommand({
27964 cmd: 'ack',
27965 peerId: _this6.id,
27966 ackMessage: new AckCommand({
27967 cid: cid,
27968 fromts: Math.min.apply(null, timestamps),
27969 tots: Math.max.apply(null, timestamps)
27970 })
27971 });
27972 delete _this6._ackMessageBuffer[cid];
27973 return _this6._send(command, false).catch(function (error) {
27974 _this6._debug('send ack failed: %O', error);
27975
27976 _this6._ackMessageBuffer[cid] = convAckMessages;
27977 });
27978 }));
27979 };
27980
27981 _proto._omitPeerId = function _omitPeerId(value) {
27982 internal(this).peerIdOmittable = value;
27983 };
27984
27985 _proto._send = function _send(cmd) {
27986 var _this$_connection;
27987
27988 var command = cmd;
27989
27990 if (!internal(this).peerIdOmittable && this.id) {
27991 command.peerId = this.id;
27992 }
27993
27994 for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
27995 args[_key3 - 1] = arguments[_key3];
27996 }
27997
27998 return (_this$_connection = this._connection).send.apply(_this$_connection, [command].concat(args));
27999 };
28000
28001 _proto._open =
28002 /*#__PURE__*/
28003 function () {
28004 var _open2 = _asyncToGenerator(
28005 /*#__PURE__*/
28006 regenerator.mark(function _callee5(appId, tag, deviceId) {
28007 var isReconnect,
28008 _internal2,
28009 lastUnreadNotifTime,
28010 lastPatchTime,
28011 lastNotificationTime,
28012 command,
28013 signatureResult,
28014 sessionToken,
28015 resCommand,
28016 _resCommand,
28017 peerId,
28018 sessionMessage,
28019 _resCommand$sessionMe,
28020 token,
28021 tokenTTL,
28022 code,
28023 serverTs,
28024 serverTime,
28025 _args5 = arguments;
28026
28027 return regenerator.wrap(function _callee5$(_context5) {
28028 while (1) {
28029 switch (_context5.prev = _context5.next) {
28030 case 0:
28031 isReconnect = _args5.length > 3 && _args5[3] !== undefined ? _args5[3] : false;
28032
28033 this._debug('open session');
28034
28035 _internal2 = internal(this), lastUnreadNotifTime = _internal2.lastUnreadNotifTime, lastPatchTime = _internal2.lastPatchTime, lastNotificationTime = _internal2.lastNotificationTime;
28036 command = new GenericCommand({
28037 cmd: 'session',
28038 op: 'open',
28039 appId: appId,
28040 peerId: this.id,
28041 sessionMessage: new SessionCommand({
28042 ua: "js/".concat(version),
28043 r: isReconnect,
28044 lastUnreadNotifTime: lastUnreadNotifTime,
28045 lastPatchTime: lastPatchTime,
28046 configBitmap: configBitmap
28047 })
28048 });
28049
28050 if (isReconnect) {
28051 _context5.next = 13;
28052 break;
28053 }
28054
28055 Object.assign(command.sessionMessage, trim({
28056 tag: tag,
28057 deviceId: deviceId
28058 }));
28059
28060 if (!this.options.signatureFactory) {
28061 _context5.next = 11;
28062 break;
28063 }
28064
28065 _context5.next = 9;
28066 return runSignatureFactory(this.options.signatureFactory, [this._identity]);
28067
28068 case 9:
28069 signatureResult = _context5.sent;
28070 Object.assign(command.sessionMessage, keyRemap({
28071 signature: 's',
28072 timestamp: 't',
28073 nonce: 'n'
28074 }, signatureResult));
28075
28076 case 11:
28077 _context5.next = 17;
28078 break;
28079
28080 case 13:
28081 _context5.next = 15;
28082 return this._sessionManager.getSessionToken({
28083 autoRefresh: false
28084 });
28085
28086 case 15:
28087 sessionToken = _context5.sent;
28088
28089 if (sessionToken && sessionToken !== Expirable.EXPIRED) {
28090 Object.assign(command.sessionMessage, {
28091 st: sessionToken
28092 });
28093 }
28094
28095 case 17:
28096 _context5.prev = 17;
28097 _context5.next = 20;
28098 return this._send(command);
28099
28100 case 20:
28101 resCommand = _context5.sent;
28102 _context5.next = 32;
28103 break;
28104
28105 case 23:
28106 _context5.prev = 23;
28107 _context5.t0 = _context5["catch"](17);
28108
28109 if (!(_context5.t0.code === ErrorCode.SESSION_TOKEN_EXPIRED)) {
28110 _context5.next = 31;
28111 break;
28112 }
28113
28114 if (this._sessionManager) {
28115 _context5.next = 28;
28116 break;
28117 }
28118
28119 throw new Error('Unexpected session expiration');
28120
28121 case 28:
28122 debug$a('Session token expired, reopening');
28123
28124 this._sessionManager.revoke();
28125
28126 return _context5.abrupt("return", this._open(appId, tag, deviceId, isReconnect));
28127
28128 case 31:
28129 throw _context5.t0;
28130
28131 case 32:
28132 _resCommand = resCommand, peerId = _resCommand.peerId, sessionMessage = _resCommand.sessionMessage, _resCommand$sessionMe = _resCommand.sessionMessage, token = _resCommand$sessionMe.st, tokenTTL = _resCommand$sessionMe.stTtl, code = _resCommand$sessionMe.code, serverTs = _resCommand.serverTs;
28133
28134 if (!code) {
28135 _context5.next = 35;
28136 break;
28137 }
28138
28139 throw createError(sessionMessage);
28140
28141 case 35:
28142 if (peerId) {
28143 this.id = peerId;
28144 if (!this._identity) this._identity = peerId;
28145
28146 if (token) {
28147 this._sessionManager = this._sessionManager || this._createSessionManager();
28148
28149 this._sessionManager.setSessionToken(token, tokenTTL);
28150 }
28151
28152 serverTime = getTime(decodeDate(serverTs));
28153
28154 if (serverTs) {
28155 internal(this).lastPatchTime = serverTime;
28156 }
28157
28158 if (lastNotificationTime) {
28159 // Do not await for it as this is failable
28160 this._syncNotifications(lastNotificationTime).catch(function (error) {
28161 return console.warn('Syncing notifications failed:', error);
28162 });
28163 } else {
28164 // Set timestamp to now for next reconnection
28165 internal(this).lastNotificationTime = serverTime;
28166 }
28167 } else {
28168 console.warn('Unexpected session opened without peerId.');
28169 }
28170
28171 return _context5.abrupt("return", undefined);
28172
28173 case 37:
28174 case "end":
28175 return _context5.stop();
28176 }
28177 }
28178 }, _callee5, this, [[17, 23]]);
28179 }));
28180
28181 function _open(_x5, _x6, _x7) {
28182 return _open2.apply(this, arguments);
28183 }
28184
28185 return _open;
28186 }();
28187
28188 _proto._syncNotifications =
28189 /*#__PURE__*/
28190 function () {
28191 var _syncNotifications2 = _asyncToGenerator(
28192 /*#__PURE__*/
28193 regenerator.mark(function _callee6(timestamp) {
28194 var _this7 = this;
28195
28196 var _ref7, hasMore, notifications;
28197
28198 return regenerator.wrap(function _callee6$(_context6) {
28199 while (1) {
28200 switch (_context6.prev = _context6.next) {
28201 case 0:
28202 _context6.next = 2;
28203 return this._fetchNotifications(timestamp);
28204
28205 case 2:
28206 _ref7 = _context6.sent;
28207 hasMore = _ref7.hasMore;
28208 notifications = _ref7.notifications;
28209 notifications.forEach(function (notification) {
28210 var cmd = notification.cmd,
28211 op = notification.op,
28212 serverTs = notification.serverTs,
28213 notificationType = notification.notificationType,
28214 payload = _objectWithoutProperties(notification, ["cmd", "op", "serverTs", "notificationType"]);
28215
28216 _this7._dispatchCommand(_defineProperty({
28217 cmd: CommandType[cmd],
28218 op: OpType[op],
28219 serverTs: serverTs,
28220 notificationType: notificationType
28221 }, "".concat(cmd, "Message"), payload));
28222 });
28223
28224 if (!hasMore) {
28225 _context6.next = 8;
28226 break;
28227 }
28228
28229 return _context6.abrupt("return", this._syncNotifications(internal(this).lastNotificationTime));
28230
28231 case 8:
28232 return _context6.abrupt("return", undefined);
28233
28234 case 9:
28235 case "end":
28236 return _context6.stop();
28237 }
28238 }
28239 }, _callee6, this);
28240 }));
28241
28242 function _syncNotifications(_x8) {
28243 return _syncNotifications2.apply(this, arguments);
28244 }
28245
28246 return _syncNotifications;
28247 }();
28248
28249 _proto._fetchNotifications =
28250 /*#__PURE__*/
28251 function () {
28252 var _fetchNotifications2 = _asyncToGenerator(
28253 /*#__PURE__*/
28254 regenerator.mark(function _callee7(timestamp) {
28255 return regenerator.wrap(function _callee7$(_context7) {
28256 while (1) {
28257 switch (_context7.prev = _context7.next) {
28258 case 0:
28259 return _context7.abrupt("return", this._requestWithSessionToken({
28260 method: 'GET',
28261 path: '/rtm/notifications',
28262 query: {
28263 start_ts: timestamp,
28264 notification_type: 'permanent'
28265 }
28266 }));
28267
28268 case 1:
28269 case "end":
28270 return _context7.stop();
28271 }
28272 }
28273 }, _callee7, this);
28274 }));
28275
28276 function _fetchNotifications(_x9) {
28277 return _fetchNotifications2.apply(this, arguments);
28278 }
28279
28280 return _fetchNotifications;
28281 }();
28282
28283 _proto._createSessionManager = function _createSessionManager() {
28284 var _this8 = this;
28285
28286 debug$a('create SessionManager');
28287 return new SessionManager({
28288 onBeforeGetSessionToken: this._connection.checkConnectionAvailability.bind(this._connection),
28289 refresh: function refresh(manager, expiredSessionToken) {
28290 return manager.setSessionTokenAsync(Promise.resolve(new GenericCommand({
28291 cmd: 'session',
28292 op: 'refresh',
28293 sessionMessage: new SessionCommand({
28294 ua: "js/".concat(version),
28295 st: expiredSessionToken
28296 })
28297 })).then(
28298 /*#__PURE__*/
28299 function () {
28300 var _ref8 = _asyncToGenerator(
28301 /*#__PURE__*/
28302 regenerator.mark(function _callee8(command) {
28303 var signatureResult;
28304 return regenerator.wrap(function _callee8$(_context8) {
28305 while (1) {
28306 switch (_context8.prev = _context8.next) {
28307 case 0:
28308 if (!_this8.options.signatureFactory) {
28309 _context8.next = 5;
28310 break;
28311 }
28312
28313 _context8.next = 3;
28314 return runSignatureFactory(_this8.options.signatureFactory, [_this8._identity]);
28315
28316 case 3:
28317 signatureResult = _context8.sent;
28318 Object.assign(command.sessionMessage, keyRemap({
28319 signature: 's',
28320 timestamp: 't',
28321 nonce: 'n'
28322 }, signatureResult));
28323
28324 case 5:
28325 return _context8.abrupt("return", command);
28326
28327 case 6:
28328 case "end":
28329 return _context8.stop();
28330 }
28331 }
28332 }, _callee8, this);
28333 }));
28334
28335 return function (_x10) {
28336 return _ref8.apply(this, arguments);
28337 };
28338 }()).then(_this8._send.bind(_this8)).then(function (_ref9) {
28339 var _ref9$sessionMessage = _ref9.sessionMessage,
28340 token = _ref9$sessionMessage.st,
28341 ttl = _ref9$sessionMessage.stTtl;
28342 return [token, ttl];
28343 }));
28344 }
28345 });
28346 };
28347
28348 _proto._requestWithSessionToken =
28349 /*#__PURE__*/
28350 function () {
28351 var _requestWithSessionToken2 = _asyncToGenerator(
28352 /*#__PURE__*/
28353 regenerator.mark(function _callee9(_ref10) {
28354 var headers, query, params, sessionToken;
28355 return regenerator.wrap(function _callee9$(_context9) {
28356 while (1) {
28357 switch (_context9.prev = _context9.next) {
28358 case 0:
28359 headers = _ref10.headers, query = _ref10.query, params = _objectWithoutProperties(_ref10, ["headers", "query"]);
28360 _context9.next = 3;
28361 return this._sessionManager.getSessionToken();
28362
28363 case 3:
28364 sessionToken = _context9.sent;
28365 return _context9.abrupt("return", this._request(_objectSpread({
28366 headers: _objectSpread({
28367 'X-LC-IM-Session-Token': sessionToken
28368 }, headers),
28369 query: _objectSpread({
28370 client_id: this.id
28371 }, query)
28372 }, params)));
28373
28374 case 5:
28375 case "end":
28376 return _context9.stop();
28377 }
28378 }
28379 }, _callee9, this);
28380 }));
28381
28382 function _requestWithSessionToken(_x11) {
28383 return _requestWithSessionToken2.apply(this, arguments);
28384 }
28385
28386 return _requestWithSessionToken;
28387 }()
28388 /**
28389 * 关闭客户端
28390 * @return {Promise}
28391 */
28392 ;
28393
28394 _proto.close =
28395 /*#__PURE__*/
28396 function () {
28397 var _close = _asyncToGenerator(
28398 /*#__PURE__*/
28399 regenerator.mark(function _callee10() {
28400 var _ee, command;
28401
28402 return regenerator.wrap(function _callee10$(_context10) {
28403 while (1) {
28404 switch (_context10.prev = _context10.next) {
28405 case 0:
28406 this._debug('close session');
28407
28408 _ee = internal(this)._eventemitter;
28409
28410 _ee.emit('beforeclose');
28411
28412 if (!this._connection.is('connected')) {
28413 _context10.next = 7;
28414 break;
28415 }
28416
28417 command = new GenericCommand({
28418 cmd: 'session',
28419 op: 'close'
28420 });
28421 _context10.next = 7;
28422 return this._send(command);
28423
28424 case 7:
28425 _ee.emit('close');
28426
28427 this.emit(CLOSE$1, {
28428 code: 0
28429 });
28430
28431 case 9:
28432 case "end":
28433 return _context10.stop();
28434 }
28435 }
28436 }, _callee10, this);
28437 }));
28438
28439 function close() {
28440 return _close.apply(this, arguments);
28441 }
28442
28443 return close;
28444 }()
28445 /**
28446 * 获取 client 列表中在线的 client,每次查询最多 20 个 clientId,超出部分会被忽略
28447 * @param {String[]} clientIds 要查询的 client ids
28448 * @return {Primse.<String[]>} 在线的 client ids
28449 */
28450 ;
28451
28452 _proto.ping =
28453 /*#__PURE__*/
28454 function () {
28455 var _ping = _asyncToGenerator(
28456 /*#__PURE__*/
28457 regenerator.mark(function _callee11(clientIds) {
28458 var command, resCommand;
28459 return regenerator.wrap(function _callee11$(_context11) {
28460 while (1) {
28461 switch (_context11.prev = _context11.next) {
28462 case 0:
28463 this._debug('ping');
28464
28465 if (clientIds instanceof Array) {
28466 _context11.next = 3;
28467 break;
28468 }
28469
28470 throw new TypeError("clientIds ".concat(clientIds, " is not an Array"));
28471
28472 case 3:
28473 if (clientIds.length) {
28474 _context11.next = 5;
28475 break;
28476 }
28477
28478 return _context11.abrupt("return", Promise.resolve([]));
28479
28480 case 5:
28481 command = new GenericCommand({
28482 cmd: 'session',
28483 op: 'query',
28484 sessionMessage: new SessionCommand({
28485 sessionPeerIds: clientIds
28486 })
28487 });
28488 _context11.next = 8;
28489 return this._send(command);
28490
28491 case 8:
28492 resCommand = _context11.sent;
28493 return _context11.abrupt("return", resCommand.sessionMessage.onlineSessionPeerIds);
28494
28495 case 10:
28496 case "end":
28497 return _context11.stop();
28498 }
28499 }
28500 }, _callee11, this);
28501 }));
28502
28503 function ping(_x12) {
28504 return _ping.apply(this, arguments);
28505 }
28506
28507 return ping;
28508 }()
28509 /**
28510 * 获取某个特定的对话
28511 * @param {String} id 对话 id,对应 _Conversation 表中的 objectId
28512 * @param {Boolean} [noCache=false] 强制不从缓存中获取
28513 * @return {Promise.<ConversationBase>} 如果 id 对应的对话不存在则返回 null
28514 */
28515 ;
28516
28517 _proto.getConversation =
28518 /*#__PURE__*/
28519 function () {
28520 var _getConversation = _asyncToGenerator(
28521 /*#__PURE__*/
28522 regenerator.mark(function _callee12(id) {
28523 var noCache,
28524 cachedConversation,
28525 _args12 = arguments;
28526 return regenerator.wrap(function _callee12$(_context12) {
28527 while (1) {
28528 switch (_context12.prev = _context12.next) {
28529 case 0:
28530 noCache = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : false;
28531
28532 if (!(typeof id !== 'string')) {
28533 _context12.next = 3;
28534 break;
28535 }
28536
28537 throw new TypeError("".concat(id, " is not a String"));
28538
28539 case 3:
28540 if (noCache) {
28541 _context12.next = 7;
28542 break;
28543 }
28544
28545 cachedConversation = this._conversationCache.get(id);
28546
28547 if (!cachedConversation) {
28548 _context12.next = 7;
28549 break;
28550 }
28551
28552 return _context12.abrupt("return", cachedConversation);
28553
28554 case 7:
28555 if (!isTemporaryConversatrionId(id)) {
28556 _context12.next = 14;
28557 break;
28558 }
28559
28560 _context12.next = 10;
28561 return this._getTemporaryConversations([id]);
28562
28563 case 10:
28564 _context12.t0 = _context12.sent[0];
28565
28566 if (_context12.t0) {
28567 _context12.next = 13;
28568 break;
28569 }
28570
28571 _context12.t0 = null;
28572
28573 case 13:
28574 return _context12.abrupt("return", _context12.t0);
28575
28576 case 14:
28577 return _context12.abrupt("return", this.getQuery().equalTo('objectId', id).find().then(function (conversations) {
28578 return conversations[0] || null;
28579 }));
28580
28581 case 15:
28582 case "end":
28583 return _context12.stop();
28584 }
28585 }
28586 }, _callee12, this);
28587 }));
28588
28589 function getConversation(_x13) {
28590 return _getConversation.apply(this, arguments);
28591 }
28592
28593 return getConversation;
28594 }()
28595 /**
28596 * 通过 id 批量获取某个特定的对话
28597 * @since 3.4.0
28598 * @param {String[]} ids 对话 id 列表,对应 _Conversation 表中的 objectId
28599 * @param {Boolean} [noCache=false] 强制不从缓存中获取
28600 * @return {Promise.<ConversationBase[]>} 如果 id 对应的对话不存在则返回 null
28601 */
28602 ;
28603
28604 _proto.getConversations =
28605 /*#__PURE__*/
28606 function () {
28607 var _getConversations = _asyncToGenerator(
28608 /*#__PURE__*/
28609 regenerator.mark(function _callee13(ids) {
28610 var _this9 = this;
28611
28612 var noCache,
28613 remoteConversationIds,
28614 remoteTemporaryConversationIds,
28615 query,
28616 remoteTemporaryConversationsPromise,
28617 _args13 = arguments;
28618 return regenerator.wrap(function _callee13$(_context13) {
28619 while (1) {
28620 switch (_context13.prev = _context13.next) {
28621 case 0:
28622 noCache = _args13.length > 1 && _args13[1] !== undefined ? _args13[1] : false;
28623 remoteConversationIds = noCache ? ids : ids.filter(function (id) {
28624 return _this9._conversationCache.get(id) === null;
28625 });
28626
28627 if (!remoteConversationIds.length) {
28628 _context13.next = 9;
28629 break;
28630 }
28631
28632 remoteTemporaryConversationIds = remove_1(remoteConversationIds, isTemporaryConversatrionId);
28633 query = [];
28634
28635 if (remoteConversationIds.length) {
28636 query.push(this.getQuery().containedIn('objectId', remoteConversationIds).limit(999).find());
28637 }
28638
28639 if (remoteTemporaryConversationIds.length) {
28640 remoteTemporaryConversationsPromise = remoteTemporaryConversationIds.map(this._getTemporaryConversations.bind(this));
28641 query.push.apply(query, _toConsumableArray(remoteTemporaryConversationsPromise));
28642 }
28643
28644 _context13.next = 9;
28645 return Promise.all(query);
28646
28647 case 9:
28648 return _context13.abrupt("return", ids.map(function (id) {
28649 return _this9._conversationCache.get(id);
28650 }));
28651
28652 case 10:
28653 case "end":
28654 return _context13.stop();
28655 }
28656 }
28657 }, _callee13, this);
28658 }));
28659
28660 function getConversations(_x14) {
28661 return _getConversations.apply(this, arguments);
28662 }
28663
28664 return getConversations;
28665 }();
28666
28667 _proto._getTemporaryConversations =
28668 /*#__PURE__*/
28669 function () {
28670 var _getTemporaryConversations2 = _asyncToGenerator(
28671 /*#__PURE__*/
28672 regenerator.mark(function _callee14(ids) {
28673 var command, resCommand;
28674 return regenerator.wrap(function _callee14$(_context14) {
28675 while (1) {
28676 switch (_context14.prev = _context14.next) {
28677 case 0:
28678 command = new GenericCommand({
28679 cmd: 'conv',
28680 op: 'query',
28681 convMessage: new ConvCommand({
28682 tempConvIds: ids
28683 })
28684 });
28685 _context14.next = 3;
28686 return this._send(command);
28687
28688 case 3:
28689 resCommand = _context14.sent;
28690 return _context14.abrupt("return", this._handleQueryResults(resCommand));
28691
28692 case 5:
28693 case "end":
28694 return _context14.stop();
28695 }
28696 }
28697 }, _callee14, this);
28698 }));
28699
28700 function _getTemporaryConversations(_x15) {
28701 return _getTemporaryConversations2.apply(this, arguments);
28702 }
28703
28704 return _getTemporaryConversations;
28705 }()
28706 /**
28707 * 构造一个 ConversationQuery 来查询对话
28708 * @return {ConversationQuery.<PersistentConversation>}
28709 */
28710 ;
28711
28712 _proto.getQuery = function getQuery() {
28713 return new ConversationQuery(this);
28714 }
28715 /**
28716 * 构造一个 ConversationQuery 来查询聊天室
28717 * @return {ConversationQuery.<ChatRoom>}
28718 */
28719 ;
28720
28721 _proto.getChatRoomQuery = function getChatRoomQuery() {
28722 return this.getQuery().equalTo('tr', true);
28723 }
28724 /**
28725 * 构造一个 ConversationQuery 来查询服务号
28726 * @return {ConversationQuery.<ServiceConversation>}
28727 */
28728 ;
28729
28730 _proto.getServiceConversationQuery = function getServiceConversationQuery() {
28731 return this.getQuery().equalTo('sys', true);
28732 };
28733
28734 _proto._executeQuery =
28735 /*#__PURE__*/
28736 function () {
28737 var _executeQuery2 = _asyncToGenerator(
28738 /*#__PURE__*/
28739 regenerator.mark(function _callee15(query) {
28740 var queryJSON, command, resCommand;
28741 return regenerator.wrap(function _callee15$(_context15) {
28742 while (1) {
28743 switch (_context15.prev = _context15.next) {
28744 case 0:
28745 queryJSON = query.toJSON();
28746 queryJSON.where = new JsonObjectMessage({
28747 data: JSON.stringify(encode(queryJSON.where))
28748 });
28749 command = new GenericCommand({
28750 cmd: 'conv',
28751 op: 'query',
28752 convMessage: new ConvCommand(queryJSON)
28753 });
28754 _context15.next = 5;
28755 return this._send(command);
28756
28757 case 5:
28758 resCommand = _context15.sent;
28759 return _context15.abrupt("return", this._handleQueryResults(resCommand));
28760
28761 case 7:
28762 case "end":
28763 return _context15.stop();
28764 }
28765 }
28766 }, _callee15, this);
28767 }));
28768
28769 function _executeQuery(_x16) {
28770 return _executeQuery2.apply(this, arguments);
28771 }
28772
28773 return _executeQuery;
28774 }();
28775
28776 _proto._handleQueryResults =
28777 /*#__PURE__*/
28778 function () {
28779 var _handleQueryResults2 = _asyncToGenerator(
28780 /*#__PURE__*/
28781 regenerator.mark(function _callee16(resCommand) {
28782 var conversations, commandString;
28783 return regenerator.wrap(function _callee16$(_context16) {
28784 while (1) {
28785 switch (_context16.prev = _context16.next) {
28786 case 0:
28787 _context16.prev = 0;
28788 conversations = decode(JSON.parse(resCommand.convMessage.results.data));
28789 _context16.next = 8;
28790 break;
28791
28792 case 4:
28793 _context16.prev = 4;
28794 _context16.t0 = _context16["catch"](0);
28795 commandString = JSON.stringify(trim(resCommand));
28796 throw new Error("Parse query result failed: ".concat(_context16.t0.message, ". Command: ").concat(commandString));
28797
28798 case 8:
28799 _context16.next = 10;
28800 return Promise.all(conversations.map(this._parseConversationFromRawData.bind(this)));
28801
28802 case 10:
28803 conversations = _context16.sent;
28804 return _context16.abrupt("return", conversations.map(this._upsertConversationToCache.bind(this)));
28805
28806 case 12:
28807 case "end":
28808 return _context16.stop();
28809 }
28810 }
28811 }, _callee16, this, [[0, 4]]);
28812 }));
28813
28814 function _handleQueryResults(_x17) {
28815 return _handleQueryResults2.apply(this, arguments);
28816 }
28817
28818 return _handleQueryResults;
28819 }();
28820
28821 _proto._upsertConversationToCache = function _upsertConversationToCache(fetchedConversation) {
28822 var conversation = this._conversationCache.get(fetchedConversation.id);
28823
28824 if (!conversation) {
28825 conversation = fetchedConversation;
28826
28827 this._debug('no match, set cache');
28828
28829 this._conversationCache.set(fetchedConversation.id, fetchedConversation);
28830 } else {
28831 this._debug('update cached conversation');
28832
28833 ['creator', 'createdAt', 'updatedAt', 'lastMessageAt', 'lastMessage', 'mutedMembers', 'members', '_attributes', 'transient', 'muted'].forEach(function (key) {
28834 var value = fetchedConversation[key];
28835 if (value !== undefined) conversation[key] = value;
28836 });
28837 if (conversation._reset) conversation._reset();
28838 }
28839
28840 return conversation;
28841 }
28842 /**
28843 * 反序列化消息,与 {@link Message#toFullJSON} 相对。
28844 * @param {Object}
28845 * @return {AVMessage} 解析后的消息
28846 * @since 4.0.0
28847 */
28848 ;
28849
28850 _proto.parseMessage =
28851 /*#__PURE__*/
28852 function () {
28853 var _parseMessage = _asyncToGenerator(
28854 /*#__PURE__*/
28855 regenerator.mark(function _callee17(_ref11) {
28856 var data, _ref11$bin, bin, properties, content, message;
28857
28858 return regenerator.wrap(function _callee17$(_context17) {
28859 while (1) {
28860 switch (_context17.prev = _context17.next) {
28861 case 0:
28862 data = _ref11.data, _ref11$bin = _ref11.bin, bin = _ref11$bin === void 0 ? false : _ref11$bin, properties = _objectWithoutProperties(_ref11, ["data", "bin"]);
28863 content = bin ? base64Arraybuffer_2(data) : data;
28864 _context17.next = 4;
28865 return this._messageParser.parse(content);
28866
28867 case 4:
28868 message = _context17.sent;
28869 Object.assign(message, properties);
28870
28871 message._updateMentioned(this.id);
28872
28873 return _context17.abrupt("return", message);
28874
28875 case 8:
28876 case "end":
28877 return _context17.stop();
28878 }
28879 }
28880 }, _callee17, this);
28881 }));
28882
28883 function parseMessage(_x18) {
28884 return _parseMessage.apply(this, arguments);
28885 }
28886
28887 return parseMessage;
28888 }()
28889 /**
28890 * 反序列化对话,与 {@link Conversation#toFullJSON} 相对。
28891 * @param {Object}
28892 * @return {ConversationBase} 解析后的对话
28893 * @since 4.0.0
28894 */
28895 ;
28896
28897 _proto.parseConversation =
28898 /*#__PURE__*/
28899 function () {
28900 var _parseConversation = _asyncToGenerator(
28901 /*#__PURE__*/
28902 regenerator.mark(function _callee18(_ref12) {
28903 var id, lastMessageAt, lastMessage, lastDeliveredAt, lastReadAt, unreadMessagesCount, members, mentioned, properties, conversationData, transient, system, expiredAt;
28904 return regenerator.wrap(function _callee18$(_context18) {
28905 while (1) {
28906 switch (_context18.prev = _context18.next) {
28907 case 0:
28908 id = _ref12.id, lastMessageAt = _ref12.lastMessageAt, lastMessage = _ref12.lastMessage, lastDeliveredAt = _ref12.lastDeliveredAt, lastReadAt = _ref12.lastReadAt, unreadMessagesCount = _ref12.unreadMessagesCount, members = _ref12.members, mentioned = _ref12.mentioned, properties = _objectWithoutProperties(_ref12, ["id", "lastMessageAt", "lastMessage", "lastDeliveredAt", "lastReadAt", "unreadMessagesCount", "members", "mentioned"]);
28909 conversationData = {
28910 id: id,
28911 lastMessageAt: lastMessageAt,
28912 lastMessage: lastMessage,
28913 lastDeliveredAt: lastDeliveredAt,
28914 lastReadAt: lastReadAt,
28915 unreadMessagesCount: unreadMessagesCount,
28916 members: members,
28917 mentioned: mentioned
28918 };
28919
28920 if (!lastMessage) {
28921 _context18.next = 7;
28922 break;
28923 }
28924
28925 _context18.next = 5;
28926 return this.parseMessage(lastMessage);
28927
28928 case 5:
28929 conversationData.lastMessage = _context18.sent;
28930
28931 conversationData.lastMessage._setStatus(MessageStatus.SENT);
28932
28933 case 7:
28934 transient = properties.transient, system = properties.system, expiredAt = properties.expiredAt;
28935
28936 if (!transient) {
28937 _context18.next = 10;
28938 break;
28939 }
28940
28941 return _context18.abrupt("return", new ChatRoom(conversationData, properties, this));
28942
28943 case 10:
28944 if (!system) {
28945 _context18.next = 12;
28946 break;
28947 }
28948
28949 return _context18.abrupt("return", new ServiceConversation(conversationData, properties, this));
28950
28951 case 12:
28952 if (!(expiredAt || isTemporaryConversatrionId(id))) {
28953 _context18.next = 14;
28954 break;
28955 }
28956
28957 return _context18.abrupt("return", new TemporaryConversation(conversationData, {
28958 expiredAt: expiredAt
28959 }, this));
28960
28961 case 14:
28962 return _context18.abrupt("return", new Conversation(conversationData, properties, this));
28963
28964 case 15:
28965 case "end":
28966 return _context18.stop();
28967 }
28968 }
28969 }, _callee18, this);
28970 }));
28971
28972 function parseConversation(_x19) {
28973 return _parseConversation.apply(this, arguments);
28974 }
28975
28976 return parseConversation;
28977 }();
28978
28979 _proto._parseConversationFromRawData =
28980 /*#__PURE__*/
28981 function () {
28982 var _parseConversationFromRawData2 = _asyncToGenerator(
28983 /*#__PURE__*/
28984 regenerator.mark(function _callee19(rawData) {
28985 var data, ttl;
28986 return regenerator.wrap(function _callee19$(_context19) {
28987 while (1) {
28988 switch (_context19.prev = _context19.next) {
28989 case 0:
28990 data = keyRemap({
28991 objectId: 'id',
28992 lm: 'lastMessageAt',
28993 m: 'members',
28994 tr: 'transient',
28995 sys: 'system',
28996 c: 'creator',
28997 mu: 'mutedMembers'
28998 }, rawData);
28999
29000 if (data.msg) {
29001 data.lastMessage = {
29002 data: data.msg,
29003 bin: data.bin,
29004 from: data.msg_from,
29005 id: data.msg_mid,
29006 timestamp: data.msg_timestamp,
29007 updatedAt: data.patch_timestamp
29008 };
29009 delete data.lastMessageFrom;
29010 delete data.lastMessageId;
29011 delete data.lastMessageTimestamp;
29012 delete data.lastMessagePatchTimestamp;
29013 }
29014
29015 ttl = data.ttl;
29016 if (ttl) data.expiredAt = Date.now() + ttl * 1000;
29017 return _context19.abrupt("return", this.parseConversation(data));
29018
29019 case 5:
29020 case "end":
29021 return _context19.stop();
29022 }
29023 }
29024 }, _callee19, this);
29025 }));
29026
29027 function _parseConversationFromRawData(_x20) {
29028 return _parseConversationFromRawData2.apply(this, arguments);
29029 }
29030
29031 return _parseConversationFromRawData;
29032 }()
29033 /**
29034 * 创建一个对话
29035 * @param {Object} options 除了下列字段外的其他字段将被视为对话的自定义属性
29036 * @param {String[]} options.members 对话的初始成员列表,默认包含当前 client
29037 * @param {String} [options.name] 对话的名字
29038 * @param {Boolean} [options.unique=true] 唯一对话,当其为 true 时,如果当前已经有相同成员的对话存在则返回该对话,否则会创建新的对话
29039 * @return {Promise.<Conversation>}
29040 */
29041 ;
29042
29043 _proto.createConversation =
29044 /*#__PURE__*/
29045 function () {
29046 var _createConversation = _asyncToGenerator(
29047 /*#__PURE__*/
29048 regenerator.mark(function _callee20() {
29049 var _ref13,
29050 m,
29051 name,
29052 transient,
29053 _ref13$unique,
29054 unique,
29055 tempConv,
29056 tempConvTTL,
29057 properties,
29058 members,
29059 attr,
29060 startCommandJson,
29061 command,
29062 params,
29063 signatureResult,
29064 _ref14,
29065 _ref14$convMessage,
29066 cid,
29067 cdate,
29068 ttl,
29069 data,
29070 conversation,
29071 _args20 = arguments;
29072
29073 return regenerator.wrap(function _callee20$(_context20) {
29074 while (1) {
29075 switch (_context20.prev = _context20.next) {
29076 case 0:
29077 _ref13 = _args20.length > 0 && _args20[0] !== undefined ? _args20[0] : {}, m = _ref13.members, name = _ref13.name, transient = _ref13.transient, _ref13$unique = _ref13.unique, unique = _ref13$unique === void 0 ? true : _ref13$unique, tempConv = _ref13._tempConv, tempConvTTL = _ref13._tempConvTTL, properties = _objectWithoutProperties(_ref13, ["members", "name", "transient", "unique", "_tempConv", "_tempConvTTL"]);
29078
29079 if (transient || Array.isArray(m)) {
29080 _context20.next = 3;
29081 break;
29082 }
29083
29084 throw new TypeError("conversation members ".concat(m, " is not an array"));
29085
29086 case 3:
29087 members = new Set(m);
29088 members.add(this.id);
29089 members = Array.from(members).sort();
29090 attr = properties || {};
29091
29092 if (!name) {
29093 _context20.next = 11;
29094 break;
29095 }
29096
29097 if (!(typeof name !== 'string')) {
29098 _context20.next = 10;
29099 break;
29100 }
29101
29102 throw new TypeError("conversation name ".concat(name, " is not a string"));
29103
29104 case 10:
29105 attr.name = name;
29106
29107 case 11:
29108 attr = new JsonObjectMessage({
29109 data: JSON.stringify(encode(attr))
29110 });
29111 startCommandJson = {
29112 m: members,
29113 attr: attr,
29114 transient: transient,
29115 unique: unique,
29116 tempConv: tempConv,
29117 tempConvTTL: tempConvTTL
29118 };
29119 command = new GenericCommand({
29120 cmd: 'conv',
29121 op: 'start',
29122 convMessage: new ConvCommand(startCommandJson)
29123 });
29124
29125 if (!this.options.conversationSignatureFactory) {
29126 _context20.next = 20;
29127 break;
29128 }
29129
29130 params = [null, this._identity, members, 'create'];
29131 _context20.next = 18;
29132 return runSignatureFactory(this.options.conversationSignatureFactory, params);
29133
29134 case 18:
29135 signatureResult = _context20.sent;
29136 Object.assign(command.convMessage, keyRemap({
29137 signature: 's',
29138 timestamp: 't',
29139 nonce: 'n'
29140 }, signatureResult));
29141
29142 case 20:
29143 _context20.next = 22;
29144 return this._send(command);
29145
29146 case 22:
29147 _ref14 = _context20.sent;
29148 _ref14$convMessage = _ref14.convMessage;
29149 cid = _ref14$convMessage.cid;
29150 cdate = _ref14$convMessage.cdate;
29151 ttl = _ref14$convMessage.tempConvTTL;
29152 data = _objectSpread({
29153 name: name,
29154 transient: transient,
29155 unique: unique,
29156 id: cid,
29157 createdAt: cdate,
29158 updatedAt: cdate,
29159 lastMessageAt: null,
29160 creator: this.id,
29161 members: transient ? [] : members
29162 }, properties);
29163 if (ttl) data.expiredAt = Date.now() + ttl * 1000;
29164 _context20.next = 31;
29165 return this.parseConversation(data);
29166
29167 case 31:
29168 conversation = _context20.sent;
29169 return _context20.abrupt("return", this._upsertConversationToCache(conversation));
29170
29171 case 33:
29172 case "end":
29173 return _context20.stop();
29174 }
29175 }
29176 }, _callee20, this);
29177 }));
29178
29179 function createConversation() {
29180 return _createConversation.apply(this, arguments);
29181 }
29182
29183 return createConversation;
29184 }()
29185 /**
29186 * 创建一个聊天室
29187 * @since 4.0.0
29188 * @param {Object} options 除了下列字段外的其他字段将被视为对话的自定义属性
29189 * @param {String} [options.name] 对话的名字
29190 * @return {Promise.<ChatRoom>}
29191 */
29192 ;
29193
29194 _proto.createChatRoom =
29195 /*#__PURE__*/
29196 function () {
29197 var _createChatRoom = _asyncToGenerator(
29198 /*#__PURE__*/
29199 regenerator.mark(function _callee21(param) {
29200 return regenerator.wrap(function _callee21$(_context21) {
29201 while (1) {
29202 switch (_context21.prev = _context21.next) {
29203 case 0:
29204 return _context21.abrupt("return", this.createConversation(_objectSpread({}, param, {
29205 transient: true,
29206 members: null,
29207 unique: false,
29208 _tempConv: false
29209 })));
29210
29211 case 1:
29212 case "end":
29213 return _context21.stop();
29214 }
29215 }
29216 }, _callee21, this);
29217 }));
29218
29219 function createChatRoom(_x21) {
29220 return _createChatRoom.apply(this, arguments);
29221 }
29222
29223 return createChatRoom;
29224 }()
29225 /**
29226 * 创建一个临时对话
29227 * @since 4.0.0
29228 * @param {Object} options
29229 * @param {String[]} options.members 对话的初始成员列表,默认包含当前 client
29230 * @param {String} [options.ttl] 对话存在时间,单位为秒,最大值与默认值均为 86400(一天),过期后该对话不再可用。
29231 * @return {Promise.<TemporaryConversation>}
29232 */
29233 ;
29234
29235 _proto.createTemporaryConversation =
29236 /*#__PURE__*/
29237 function () {
29238 var _createTemporaryConversation = _asyncToGenerator(
29239 /*#__PURE__*/
29240 regenerator.mark(function _callee22(_ref15) {
29241 var _tempConvTTL, param;
29242
29243 return regenerator.wrap(function _callee22$(_context22) {
29244 while (1) {
29245 switch (_context22.prev = _context22.next) {
29246 case 0:
29247 _tempConvTTL = _ref15.ttl, param = _objectWithoutProperties(_ref15, ["ttl"]);
29248 return _context22.abrupt("return", this.createConversation(_objectSpread({}, param, {
29249 transient: false,
29250 unique: false,
29251 _tempConv: true,
29252 _tempConvTTL: _tempConvTTL
29253 })));
29254
29255 case 2:
29256 case "end":
29257 return _context22.stop();
29258 }
29259 }
29260 }, _callee22, this);
29261 }));
29262
29263 function createTemporaryConversation(_x22) {
29264 return _createTemporaryConversation.apply(this, arguments);
29265 }
29266
29267 return createTemporaryConversation;
29268 }() // jsdoc-ignore-start
29269 ;
29270
29271 _proto. // jsdoc-ignore-end
29272 _doSendRead = function _doSendRead() {
29273 var _this10 = this;
29274
29275 // if not connected, just skip everything
29276 if (!this._connection.is('connected')) return;
29277 var buffer = internal(this).readConversationsBuffer;
29278 var conversations = Array.from(buffer);
29279 if (!conversations.length) return;
29280 var ids = conversations.map(function (conversation) {
29281 if (!(conversation instanceof ConversationBase)) {
29282 throw new TypeError("".concat(conversation, " is not a Conversation"));
29283 }
29284
29285 return conversation.id;
29286 });
29287
29288 this._debug("mark [".concat(ids, "] as read"));
29289
29290 buffer.clear();
29291
29292 this._sendReadCommand(conversations).catch(function (error) {
29293 _this10._debug('send read failed: %O', error);
29294
29295 conversations.forEach(buffer.add.bind(buffer));
29296 });
29297 };
29298
29299 _proto._sendReadCommand = function _sendReadCommand(conversations) {
29300 var _this11 = this;
29301
29302 return this._send(new GenericCommand({
29303 cmd: 'read',
29304 readMessage: new ReadCommand({
29305 convs: conversations.map(function (conversation) {
29306 return new ReadTuple({
29307 cid: conversation.id,
29308 mid: conversation.lastMessage && conversation.lastMessage.from !== _this11.id ? conversation.lastMessage.id : undefined,
29309 timestamp: (conversation.lastMessageAt || new Date()).getTime()
29310 });
29311 })
29312 })
29313 }), false);
29314 };
29315
29316 return IMClient;
29317 }(eventemitter3), (_applyDecoratedDescriptor(_class$3.prototype, "_doSendAck", [_dec$2], Object.getOwnPropertyDescriptor(_class$3.prototype, "_doSendAck"), _class$3.prototype), _applyDecoratedDescriptor(_class$3.prototype, "_doSendRead", [_dec2], Object.getOwnPropertyDescriptor(_class$3.prototype, "_doSendRead"), _class$3.prototype)), _class$3));
29318 /**
29319 * 修改、撤回消息的原因
29320 * @typedef PatchReason
29321 * @type {Object}
29322 * @property {number} code 负数为内置 code,正数为开发者在 hook 中自定义的 code。比如因为敏感词过滤被修改的 code 为 -4408。
29323 * @property {string} [detail] 具体的原因说明。
29324 */
29325
29326 var RECONNECT_ERROR = 'reconnecterror';
29327
29328 var CoreEvent = /*#__PURE__*/Object.freeze({
29329 RECONNECT_ERROR: RECONNECT_ERROR,
29330 DISCONNECT: DISCONNECT,
29331 RECONNECT: RECONNECT,
29332 RETRY: RETRY,
29333 SCHEDULE: SCHEDULE,
29334 OFFLINE: OFFLINE,
29335 ONLINE: ONLINE
29336 });
29337
29338 var _class$4;
29339
29340 var // jsdoc-ignore-end
29341 BinaryMessage = IE10Compatible(_class$4 =
29342 /*#__PURE__*/
29343 function (_Message) {
29344 _inheritsLoose(BinaryMessage, _Message);
29345
29346 /**
29347 * 二进制消息
29348 * @extends Message
29349 * @param {ArrayBuffer} buffer
29350 * @since 4.0.0
29351 */
29352 function BinaryMessage(buffer) {
29353 if (!(buffer instanceof ArrayBuffer)) {
29354 throw new TypeError("".concat(buffer, " is not an ArrayBuffer"));
29355 }
29356
29357 return _Message.call(this, buffer) || this;
29358 }
29359 /**
29360 * @type ArrayBuffer
29361 */
29362
29363
29364 BinaryMessage.validate = function validate(target) {
29365 return target instanceof ArrayBuffer;
29366 };
29367
29368 var _proto = BinaryMessage.prototype;
29369
29370 _proto.toJSON = function toJSON() {
29371 return _objectSpread({}, _Message.prototype._toJSON.call(this), {
29372 data: base64Arraybuffer_1(this.content)
29373 });
29374 };
29375
29376 _proto.toFullJSON = function toFullJSON() {
29377 return _objectSpread({}, _Message.prototype.toFullJSON.call(this), {
29378 bin: true,
29379 data: base64Arraybuffer_1(this.content)
29380 });
29381 };
29382
29383 _createClass(BinaryMessage, [{
29384 key: "buffer",
29385 get: function get() {
29386 return this.content;
29387 },
29388 set: function set(buffer) {
29389 this.content = buffer;
29390 }
29391 }]);
29392
29393 return BinaryMessage;
29394 }(Message)) || _class$4;
29395
29396 var _dec$3, _class$5;
29397
29398 var // jsdoc-ignore-end
29399 TextMessage = (_dec$3 = messageType(-1), _dec$3(_class$5 = IE10Compatible(_class$5 =
29400 /*#__PURE__*/
29401 function (_TypedMessage) {
29402 _inheritsLoose(TextMessage, _TypedMessage);
29403
29404 /**
29405 * 文类类型消息
29406 * @extends TypedMessage
29407 * @param {String} [text='']
29408 * @throws {TypeError} text 不是 String 类型
29409 */
29410 function TextMessage() {
29411 var _this;
29412
29413 var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
29414
29415 if (typeof text !== 'string') {
29416 throw new TypeError("".concat(text, " is not a string"));
29417 }
29418
29419 _this = _TypedMessage.call(this) || this;
29420
29421 _this.setText(text);
29422
29423 return _this;
29424 }
29425
29426 return TextMessage;
29427 }(TypedMessage)) || _class$5) || _class$5);
29428 /**
29429 * @name TYPE
29430 * @memberof TextMessage
29431 * @type Number
29432 * @static
29433 * @const
29434 */
29435
29436 var _class$6;
29437 var debug$b = browser('LC:MessageParser');
29438
29439 var tryParseJson = function tryParseJson(target, key, descriptor) {
29440 var fn = descriptor.value; // eslint-disable-next-line no-param-reassign
29441
29442 descriptor.value = function wrapper(param) {
29443 var content;
29444
29445 if (typeof param !== 'string') {
29446 content = param;
29447 } else {
29448 try {
29449 content = JSON.parse(param);
29450 } catch (error) {
29451 content = param;
29452 }
29453 }
29454
29455 return fn.call(this, content);
29456 };
29457 };
29458
29459 var applyPlugins = function applyPlugins(target, key, descriptor) {
29460 var fn = descriptor.value; // eslint-disable-next-line no-param-reassign
29461
29462 descriptor.value = function wrapper(json) {
29463 var _this = this;
29464
29465 return Promise.resolve(json).then(applyMiddlewares(this._plugins.beforeMessageParse)).then(function (decoratedJson) {
29466 return fn.call(_this, decoratedJson);
29467 }).then(applyMiddlewares(this._plugins.afterMessageParse));
29468 };
29469 };
29470
29471 var MessageParser = (_class$6 =
29472 /*#__PURE__*/
29473 function () {
29474 function MessageParser() {
29475 var plugins = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
29476 this._plugins = plugins;
29477 this._messageClasses = [];
29478 }
29479
29480 var _proto = MessageParser.prototype;
29481
29482 _proto.register = function register(messageClass) {
29483 if (messageClass && messageClass.parse && messageClass.prototype && messageClass.prototype.getPayload) {
29484 this._messageClasses.unshift(messageClass);
29485 } else {
29486 throw new TypeError('Invalid messageClass');
29487 }
29488 } // jsdoc-ignore-start
29489 ;
29490
29491 _proto. // jsdoc-ignore-end
29492 parse = function parse(content) {
29493 debug$b('parsing message: %O', content); // eslint-disable-next-line no-restricted-syntax
29494
29495 var _iteratorNormalCompletion = true;
29496 var _didIteratorError = false;
29497 var _iteratorError = undefined;
29498
29499 try {
29500 for (var _iterator = this._messageClasses[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
29501 var Klass = _step.value;
29502 var contentCopy = isPlainObject_1(content) ? Object.assign({}, content) : content;
29503 var valid = void 0;
29504 var result = void 0;
29505
29506 try {
29507 valid = Klass.validate(contentCopy);
29508 } catch (error) {// eslint-disable-line no-empty
29509 }
29510
29511 if (valid) {
29512 try {
29513 result = Klass.parse(contentCopy);
29514 } catch (error) {
29515 console.warn('parsing a valid message content error', {
29516 error: error,
29517 Klass: Klass,
29518 content: contentCopy
29519 });
29520 }
29521
29522 if (result !== undefined) {
29523 debug$b('parse result: %O', result);
29524 return result;
29525 }
29526 }
29527 }
29528 } catch (err) {
29529 _didIteratorError = true;
29530 _iteratorError = err;
29531 } finally {
29532 try {
29533 if (!_iteratorNormalCompletion && _iterator.return != null) {
29534 _iterator.return();
29535 }
29536 } finally {
29537 if (_didIteratorError) {
29538 throw _iteratorError;
29539 }
29540 }
29541 }
29542
29543 throw new Error('No Message Class matched');
29544 };
29545
29546 return MessageParser;
29547 }(), (_applyDecoratedDescriptor(_class$6.prototype, "parse", [tryParseJson, applyPlugins], Object.getOwnPropertyDescriptor(_class$6.prototype, "parse"), _class$6.prototype)), _class$6);
29548
29549 var debug$c = browser('LC:IMPlugin');
29550 /**
29551 * 消息优先级枚举
29552 * @enum {Number}
29553 * @since 3.3.0
29554 */
29555
29556 var MessagePriority = {
29557 /** 高 */
29558 HIGH: 1,
29559
29560 /** 普通 */
29561 NORMAL: 2,
29562
29563 /** 低 */
29564 LOW: 3
29565 };
29566 Object.freeze(MessagePriority);
29567 /**
29568 * 为 Conversation 定义一个新属性
29569 * @param {String} prop 属性名
29570 * @param {Object} [descriptor] 属性的描述符,参见 {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description getOwnPropertyDescriptor#Description - MDN},默认为该属性名对应的 Conversation 自定义属性的 getter/setter
29571 * @returns void
29572 * @example
29573 *
29574 * conversation.get('type');
29575 * conversation.set('type', 1);
29576 *
29577 * // equals to
29578 * defineConversationProperty('type');
29579 * conversation.type;
29580 * conversation.type = 1;
29581 */
29582
29583 var defineConversationProperty = function defineConversationProperty(prop) {
29584 var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
29585 get: function get() {
29586 return this.get(prop);
29587 },
29588 set: function set(value) {
29589 this.set(prop, value);
29590 }
29591 };
29592 Object.defineProperty(Conversation.prototype, prop, descriptor);
29593 };
29594
29595 var onRealtimeCreate = function onRealtimeCreate(realtime) {
29596 /* eslint-disable no-param-reassign */
29597 var deviceId = v4_1();
29598 realtime._IMClients = {};
29599 realtime._IMClientsCreationCount = 0;
29600 var messageParser = new MessageParser(realtime._plugins);
29601 realtime._messageParser = messageParser;
29602
29603 var signAVUser =
29604 /*#__PURE__*/
29605 function () {
29606 var _ref = _asyncToGenerator(
29607 /*#__PURE__*/
29608 regenerator.mark(function _callee(user) {
29609 return regenerator.wrap(function _callee$(_context) {
29610 while (1) {
29611 switch (_context.prev = _context.next) {
29612 case 0:
29613 return _context.abrupt("return", realtime._request({
29614 method: 'POST',
29615 path: '/rtm/sign',
29616 data: {
29617 session_token: user.getSessionToken()
29618 }
29619 }));
29620
29621 case 1:
29622 case "end":
29623 return _context.stop();
29624 }
29625 }
29626 }, _callee, this);
29627 }));
29628
29629 return function signAVUser(_x) {
29630 return _ref.apply(this, arguments);
29631 };
29632 }();
29633 /**
29634 * 注册消息类
29635 *
29636 * 在接收消息、查询消息时,会按照消息类注册顺序的逆序依次尝试解析消息内容
29637 *
29638 * @memberof Realtime
29639 * @instance
29640 * @param {Function | Function[]} messageClass 消息类,需要实现 {@link AVMessage} 接口,
29641 * 建议继承自 {@link TypedMessage}
29642 * @throws {TypeError} 如果 messageClass 没有实现 {@link AVMessage} 接口则抛出异常
29643 */
29644
29645
29646 var register = function register(messageClass) {
29647 return ensureArray(messageClass).map(messageParser.register.bind(messageParser));
29648 };
29649
29650 register(ensureArray(realtime._plugins.messageClasses));
29651 /**
29652 * 创建一个即时通讯客户端,多次创建相同 id 的客户端会返回同一个实例
29653 * @memberof Realtime
29654 * @instance
29655 * @param {String|AV.User} [identity] 客户端 identity,如果不指定该参数,服务端会随机生成一个字符串作为 identity,
29656 * 如果传入一个已登录的 AV.User,则会使用该用户的 id 作为客户端 identity 登录。
29657 * @param {Object} [options]
29658 * @param {Function} [options.signatureFactory] open session 时的签名方法 // TODO need details
29659 * @param {Function} [options.conversationSignatureFactory] 对话创建、增减成员操作时的签名方法
29660 * @param {Function} [options.blacklistSignatureFactory] 黑名单操作时的签名方法
29661 * @param {String} [options.tag] 客户端类型标记,以支持单点登录功能
29662 * @param {String} [options.isReconnect=false] 单点登录时标记该次登录是不是应用启动时自动重新登录
29663 * @return {Promise.<IMClient>}
29664 */
29665
29666 var createIMClient =
29667 /*#__PURE__*/
29668 function () {
29669 var _ref2 = _asyncToGenerator(
29670 /*#__PURE__*/
29671 regenerator.mark(function _callee2(identity) {
29672 var _realtime$_open$then;
29673
29674 var _ref3,
29675 tag,
29676 isReconnect,
29677 clientOptions,
29678 lagecyTag,
29679 id,
29680 buildinOptions,
29681 sessionToken,
29682 _tag,
29683 promise,
29684 _args2 = arguments;
29685
29686 return regenerator.wrap(function _callee2$(_context2) {
29687 while (1) {
29688 switch (_context2.prev = _context2.next) {
29689 case 0:
29690 _ref3 = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}, tag = _ref3.tag, isReconnect = _ref3.isReconnect, clientOptions = _objectWithoutProperties(_ref3, ["tag", "isReconnect"]);
29691 lagecyTag = _args2.length > 2 ? _args2[2] : undefined;
29692 buildinOptions = {};
29693
29694 if (!identity) {
29695 _context2.next = 19;
29696 break;
29697 }
29698
29699 if (!(typeof identity === 'string')) {
29700 _context2.next = 8;
29701 break;
29702 }
29703
29704 id = identity;
29705 _context2.next = 17;
29706 break;
29707
29708 case 8:
29709 if (!(identity.id && identity.getSessionToken)) {
29710 _context2.next = 16;
29711 break;
29712 }
29713
29714 id = identity.id;
29715 sessionToken = identity.getSessionToken();
29716
29717 if (sessionToken) {
29718 _context2.next = 13;
29719 break;
29720 }
29721
29722 throw new Error('User must be authenticated');
29723
29724 case 13:
29725 buildinOptions.signatureFactory = signAVUser;
29726 _context2.next = 17;
29727 break;
29728
29729 case 16:
29730 throw new TypeError('Identity must be a String or an AV.User');
29731
29732 case 17:
29733 if (!(realtime._IMClients[id] !== undefined)) {
29734 _context2.next = 19;
29735 break;
29736 }
29737
29738 return _context2.abrupt("return", realtime._IMClients[id]);
29739
29740 case 19:
29741 if (lagecyTag) {
29742 console.warn('DEPRECATION createIMClient tag param: Use options.tag instead.');
29743 }
29744
29745 _tag = tag || lagecyTag;
29746 promise = (_realtime$_open$then = realtime._open().then(function (connection) {
29747 var client = new IMClient(id, _objectSpread({}, buildinOptions, clientOptions), {
29748 _connection: connection,
29749 _request: realtime._request.bind(realtime),
29750 _messageParser: messageParser,
29751 _plugins: realtime._plugins,
29752 _identity: identity
29753 });
29754 connection.on(RECONNECT, function () {
29755 return client._open(realtime._options.appId, _tag, deviceId, true)
29756 /**
29757 * 客户端连接恢复正常,该事件通常在 {@link Realtime#event:RECONNECT} 之后发生
29758 * @event IMClient#RECONNECT
29759 * @see Realtime#event:RECONNECT
29760 * @since 3.2.0
29761 */
29762
29763 /**
29764 * 客户端重新登录发生错误(网络连接已恢复,但重新登录错误)
29765 * @event IMClient#RECONNECT_ERROR
29766 * @since 3.2.0
29767 */
29768 .then(function () {
29769 return client.emit(RECONNECT);
29770 }, function (error) {
29771 return client.emit(RECONNECT_ERROR, error);
29772 });
29773 });
29774
29775 internal(client)._eventemitter.on('beforeclose', function () {
29776 delete realtime._IMClients[client.id];
29777
29778 if (realtime._firstIMClient === client) {
29779 delete realtime._firstIMClient;
29780 }
29781 }, realtime);
29782
29783 internal(client)._eventemitter.on('close', function () {
29784 realtime._deregister(client);
29785 }, realtime);
29786
29787 return client._open(realtime._options.appId, _tag, deviceId, isReconnect).then(function () {
29788 realtime._IMClients[client.id] = client;
29789 realtime._IMClientsCreationCount += 1;
29790
29791 if (realtime._IMClientsCreationCount === 1) {
29792 client._omitPeerId(true);
29793
29794 realtime._firstIMClient = client;
29795 } else if (realtime._IMClientsCreationCount > 1 && realtime._firstIMClient) {
29796 realtime._firstIMClient._omitPeerId(false);
29797 }
29798
29799 realtime._register(client);
29800
29801 return client;
29802 }).catch(function (error) {
29803 delete realtime._IMClients[client.id];
29804 throw error;
29805 });
29806 })).then.apply(_realtime$_open$then, _toConsumableArray(finalize(function () {
29807 realtime._deregisterPending(promise);
29808 })));
29809
29810 if (identity) {
29811 realtime._IMClients[id] = promise;
29812 }
29813
29814 realtime._registerPending(promise);
29815
29816 return _context2.abrupt("return", promise);
29817
29818 case 25:
29819 case "end":
29820 return _context2.stop();
29821 }
29822 }
29823 }, _callee2, this);
29824 }));
29825
29826 return function createIMClient(_x2) {
29827 return _ref2.apply(this, arguments);
29828 };
29829 }();
29830
29831 Object.assign(realtime, {
29832 register: register,
29833 createIMClient: createIMClient
29834 });
29835 /* eslint-enable no-param-reassign */
29836 };
29837
29838 var beforeCommandDispatch = function beforeCommandDispatch(command, realtime) {
29839 var isIMCommand = command.service === null || command.service === 2;
29840 if (!isIMCommand) return true;
29841 var targetClient = command.peerId ? realtime._IMClients[command.peerId] : realtime._firstIMClient;
29842
29843 if (targetClient) {
29844 Promise.resolve(targetClient).then(function (client) {
29845 return client._dispatchCommand(command);
29846 }).catch(debug$c);
29847 } else {
29848 debug$c('[WARN] Unexpected message received without any live client match: %O', trim(command));
29849 }
29850
29851 return false;
29852 };
29853
29854 var IMPlugin = {
29855 name: 'leancloud-realtime-plugin-im',
29856 onRealtimeCreate: onRealtimeCreate,
29857 beforeCommandDispatch: beforeCommandDispatch,
29858 messageClasses: [Message, BinaryMessage, RecalledMessage, TextMessage]
29859 };
29860
29861 Realtime.defineConversationProperty = defineConversationProperty;
29862 Realtime.__preRegisteredPlugins = [IMPlugin];
29863
29864 var Event$1 = _objectSpread({}, CoreEvent, Event);
29865
29866 exports.Event = Event$1;
29867 exports.ErrorCode = ErrorCode;
29868 exports.Protocals = message;
29869 exports.Promise = polyfilledPromise;
29870 exports.EventEmitter = eventemitter3;
29871 exports.Realtime = Realtime;
29872 exports.debug = debug$2;
29873 exports.Message = Message;
29874 exports.BinaryMessage = BinaryMessage;
29875 exports.TypedMessage = TypedMessage;
29876 exports.TextMessage = TextMessage;
29877 exports.RecalledMessage = RecalledMessage;
29878 exports.MessagePriority = MessagePriority;
29879 exports.MessageStatus = MessageStatus;
29880 exports.MessageQueryDirection = MessageQueryDirection;
29881 exports.defineConversationProperty = defineConversationProperty;
29882 exports.IMPlugin = IMPlugin;
29883 exports.messageType = messageType;
29884 exports.messageField = messageField;
29885 exports.IE10Compatible = IE10Compatible;
29886 exports.ConversationMemberRole = ConversationMemberRole;
29887 exports.Conversation = Conversation;
29888 exports.ChatRoom = ChatRoom;
29889 exports.ServiceConversation = ServiceConversation;
29890 exports.TemporaryConversation = TemporaryConversation;
29891
29892 Object.defineProperty(exports, '__esModule', { value: true });
29893
29894}));
29895//# sourceMappingURL=realtime-weapp.js.map