MooMoo.io Official Msgpack

The MooMoo.io Official Msgpack

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/523347/1518120/MooMooio%20Official%20Msgpack.js

  1. var process = window.process || {};
  2. var globalThis = window.globalThis || {};
  3. var maxUint32 = 4294967295;
  4. var POW32 = 4294967296;
  5. var split64 = {
  6. mode_1: (buffer, offset, value) => {
  7. buffer.setUint32(offset, value / POW32);
  8. buffer.setUint32(offset + 4, value);
  9. },
  10. mode_2: (buffer, offset, value) => {
  11. buffer.setUint32(offset, Math.floor(value / POW32));
  12. buffer.setUint32(offset + 4, value);
  13. },
  14. }
  15. function combine64(buffer, offset) {
  16. return buffer.getInt32(offset) * POW32 + buffer.getUint32(offset + 4);
  17. }
  18. var isTextEncodingSupported = (typeof process === "undefined" || (process == null ? undefined : process.env)?.TEXT_ENCODING !== "never") && typeof TextEncoder !== "undefined" && typeof TextDecoder !== "undefined";
  19. function byteLengthUTF8(str) {
  20. let len = str.length, byteCount = 0, i = 0;
  21. while (i < len) {
  22. let code = str.charCodeAt(i++);
  23. if (code >= 0xD800 && code <= 0xDBFF && i < len) {
  24. let nextCode = str.charCodeAt(i);
  25. if (nextCode >= 0xDC00 && nextCode <= 0xDFFF) {
  26. code = (code - 0xD800 << 10) + (nextCode - 0xDC00) + 0x10000;
  27. i++;
  28. }
  29. }
  30. byteCount += (code <= 0x7F) ? 1 : (code <= 0x7FF) ? 2 : (code <= 0xFFFF) ? 3 : 4;
  31. }
  32. return byteCount;
  33. }
  34. function encodeUTF8(str, arr, index) {
  35. let len = str.length;
  36. for (let i = 0; i < len; i++) {
  37. let code = str.charCodeAt(i);
  38. if (code >= 0xD800 && code <= 0xDBFF && i + 1 < len) {
  39. let nextCode = str.charCodeAt(i + 1);
  40. if (nextCode >= 0xDC00 && nextCode <= 0xDFFF) {
  41. code = ((code - 0xD800) << 10) + (nextCode - 0xDC00) + 0x10000;
  42. i++;
  43. }
  44. }
  45. if (code <= 0x7F) {
  46. arr[index++] = code;
  47. } else if (code <= 0x7FF) {
  48. arr[index++] = (code >> 6) | 0xC0;
  49. arr[index++] = (code & 0x3F) | 0x80;
  50. } else if (code <= 0xFFFF) {
  51. arr[index++] = (code >> 12) | 0xE0;
  52. arr[index++] = ((code >> 6) & 0x3F) | 0x80;
  53. arr[index++] = (code & 0x3F) | 0x80;
  54. } else {
  55. arr[index++] = (code >> 18) | 0xF0;
  56. arr[index++] = ((code >> 12) & 0x3F) | 0x80;
  57. arr[index++] = ((code >> 6) & 0x3F) | 0x80;
  58. arr[index++] = (code & 0x3F) | 0x80;
  59. }
  60. if (code > 0x7F) arr[index++] = (code & 0x3F) | 0x80;
  61. }
  62. }
  63. var textEncoder = isTextEncodingSupported ? new TextEncoder() : undefined;
  64. var encodingLimit = isTextEncodingSupported && (!process?.env?.TEXT_ENCODING === "force") ? 200 : maxUint32;
  65. function encodeTextToArray(str, arr, idx) {
  66. arr.set(textEncoder.encode(str), idx);
  67. }
  68. function encodeTextToSubarray(str, subarr, idx) {
  69. textEncoder.encodeInto(str, subarr.subarray(idx));
  70. }
  71. var encodeFunc = textEncoder?.encodeInto ? encodeTextToSubarray : encodeTextToArray;
  72. var bufferSize = 4096;
  73. function decodeBytesToString(bytes, startIdx, length) {
  74. let endIdx = startIdx + length, buffer = [], result = '';
  75. while (startIdx < endIdx) {
  76. let byte = bytes[startIdx++];
  77. if (byte < 128) {
  78. buffer.push(byte);
  79. } else if ((byte & 224) === 192) {
  80. buffer.push(((byte & 31) << 6) | (bytes[startIdx++] & 63));
  81. } else if ((byte & 240) === 224) {
  82. buffer.push(((byte & 31) << 12) | ((bytes[startIdx++] & 63) << 6) | (bytes[startIdx++] & 63));
  83. } else if ((byte & 248) === 240) {
  84. let codePoint = ((byte & 7) << 18) | ((bytes[startIdx++] & 63) << 12) | ((bytes[startIdx++] & 63) << 6) | (bytes[startIdx++] & 63);
  85. if (codePoint > 65535) {
  86. codePoint -= 65536;
  87. buffer.push((codePoint >>> 10) & 1023 | 55296);
  88. codePoint = (codePoint & 1023) | 56320;
  89. }
  90. buffer.push(codePoint);
  91. } else {
  92. buffer.push(byte);
  93. }
  94. if (buffer.length >= bufferSize) {
  95. result += String.fromCharCode.apply(String, buffer);
  96. buffer.length = 0;
  97. }
  98. }
  99. if (buffer.length) result += String.fromCharCode.apply(String, buffer);
  100. return result;
  101. }
  102. var textDecoder = isTextEncodingSupported ? new TextDecoder() : null;
  103. var decodingLimit = isTextEncodingSupported ? typeof process !== "undefined" && (process == null ? undefined : process.env)?.TEXT_DECODER !== "force" ? 200 : 0 : maxUint32;
  104. function decodeSubarray(array, start, length) {
  105. var subArray = array.subarray(start, start + length);
  106. return textDecoder.decode(subArray);
  107. }
  108. var TypeDataPair = function () {
  109. function pair(type, data) {
  110. this.type = type;
  111. this.data = data;
  112. }
  113. return pair;
  114. }();
  115. var createInheritance = globalThis && globalThis.__extends || function () {
  116. function setPrototype(child, parent) {
  117. setPrototype = Object.setPrototypeOf || {
  118. __proto__: []
  119. } instanceof Array && function (child, parent) {
  120. child.__proto__ = parent;
  121. } || function (child, parent) {
  122. for (var key in parent) {
  123. if (Object.prototype.hasOwnProperty.call(parent, key)) {
  124. child[key] = parent[key];
  125. }
  126. }
  127. };
  128. return setPrototype(child, parent);
  129. }
  130. return function (ChildClass, ParentClass) {
  131. if (typeof ParentClass !== "function" && ParentClass !== null) {
  132. throw new TypeError("Class extends value " + String(ParentClass) + " is not a constructor or null");
  133. }
  134. setPrototype(ChildClass, ParentClass);
  135. function TemporaryConstructor() {
  136. this.constructor = ChildClass;
  137. }
  138. ChildClass.prototype = ParentClass === null ? Object.create(ParentClass) : (TemporaryConstructor.prototype = ParentClass.prototype, new TemporaryConstructor());
  139. };
  140. }();
  141. var createCustomError = function (BaseClass) {
  142. createInheritance(CustomError, BaseClass);
  143. function CustomError(message) {
  144. var instance = BaseClass.call(this, message) || this;
  145. var prototype = Object.create(CustomError.prototype);
  146. Object.setPrototypeOf(instance, prototype);
  147. Object.defineProperty(instance, "name", {
  148. configurable: true,
  149. enumerable: false,
  150. value: CustomError.name
  151. });
  152. return instance;
  153. }
  154.  
  155. return CustomError;
  156. }(Error);
  157. var type_data = -1;
  158. var MAX_UINT32 = 4294967295;
  159. var MAX_SECONDS_64BIT = 17179869183;
  160. function encodeTime(timeObj) {
  161. let seconds = timeObj.sec;
  162. let nanoseconds = timeObj.nsec;
  163. if (seconds >= 0 && nanoseconds >= 0 && seconds <= MAX_SECONDS_64BIT) {
  164. if (nanoseconds === 0 && seconds <= MAX_UINT32) {
  165. let result = new Uint8Array(4);
  166. let view = new DataView(result.buffer);
  167. view.setUint32(0, seconds);
  168. return result;
  169. } else {
  170. let highBits = seconds / POW32;
  171. let lowBits = seconds & 4294967295;
  172. let result = new Uint8Array(8);
  173. let view = new DataView(result.buffer);
  174. view.setUint32(0, nanoseconds << 2 | highBits & 3);
  175. view.setUint32(4, lowBits);
  176. return result;
  177. }
  178. } else {
  179. let result = new Uint8Array(12);
  180. let view = new DataView(result.buffer);
  181. view.setUint32(0, nanoseconds);
  182. split64.mode_2(view, 4, seconds);
  183. return result;
  184. }
  185. }
  186. function dateToTimeObject(date) {
  187. let milliseconds = date.getTime();
  188. let seconds = Math.floor(milliseconds / 1000);
  189. let nanoseconds = (milliseconds - seconds * 1000) * 1000000;
  190.  
  191. let overflowSeconds = Math.floor(nanoseconds / 1000000000);
  192. return {
  193. sec: seconds + overflowSeconds,
  194. nsec: nanoseconds - overflowSeconds * 1000000000
  195. };
  196. }
  197. function encodeDate(date) {
  198. if (date instanceof Date) {
  199. const timeObject = dateToTimeObject(date);
  200. return encodeTime(timeObject);
  201. } else {
  202. return null;
  203. }
  204. }
  205. function decodeTime(buffer) {
  206. let view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  207. let seconds, nanoseconds;
  208.  
  209. if (buffer.byteLength === 4) {
  210. seconds = view.getUint32(0);
  211. nanoseconds = 0;
  212. } else if (buffer.byteLength === 8) {
  213. let firstPart = view.getUint32(0);
  214. let secondPart = view.getUint32(4);
  215. seconds = (firstPart & 3) * 4294967296 + secondPart;
  216. nanoseconds = firstPart >>> 2;
  217. } else if (buffer.byteLength === 12) {
  218. seconds = combine64(view, 4);
  219. nanoseconds = view.getUint32(0);
  220. } else {
  221. throw new createCustomError("Unrecognized data size for timestamp (expected 4, 8, or 12): " + buffer.length);
  222. }
  223.  
  224. return { sec: seconds, nsec: nanoseconds };
  225. }
  226. function decodeToDate(buffer) {
  227. let timeObject = decodeTime(buffer);
  228. return new Date(timeObject.sec * 1000 + timeObject.nsec / 1000000);
  229. }
  230. let timestampCodec = {
  231. type: type_data,
  232. encode: encodeDate,
  233. decode: decodeToDate
  234. };
  235. var CodecRegistry = function () {
  236. function Codec() {
  237. this.builtInEncoders = [];
  238. this.builtInDecoders = [];
  239. this.encoders = [];
  240. this.decoders = [];
  241. this.register(timestampCodec);
  242. }
  243.  
  244. Codec.prototype.register = function (codec) {
  245. var type = codec.type;
  246. var encode = codec.encode;
  247. var decode = codec.decode;
  248.  
  249. if (type >= 0) {
  250. this.encoders[type] = encode;
  251. this.decoders[type] = decode;
  252. } else {
  253. var adjustedType = 1 + type;
  254. this.builtInEncoders[adjustedType] = encode;
  255. this.builtInDecoders[adjustedType] = decode;
  256. }
  257. };
  258.  
  259. Codec.prototype.tryToEncode = function (data, type) {
  260. for (var i = 0; i < this.builtInEncoders.length; i++) {
  261. var encoder = this.builtInEncoders[i];
  262. if (encoder != null) {
  263. var encodedData = encoder(data, type);
  264. if (encodedData != null) {
  265. var encodedType = -1 - i;
  266. return new TypeDataPair(encodedType, encodedData);
  267. }
  268. }
  269. }
  270. for (var o = 0; i < this.encoders.length; i++) {
  271. var encoder2 = this.encoders[i];
  272. if (encoder2 != null) {
  273. var encodedData2 = encoder2(data, type);
  274. if (encodedData2 != null) {
  275. return new TypeDataPair(i, encodedData2);
  276. }
  277. }
  278. }
  279. if (data instanceof TypeDataPair) {
  280. return data;
  281. } else {
  282. return null;
  283. }
  284. };
  285.  
  286. Codec.prototype.decode = function (data, type, additionalData) {
  287. var decoder = type < 0 ? this.builtInDecoders[-1 - type] : this.decoders[type];
  288. if (decoder) {
  289. return decoder(data, type, additionalData);
  290. } else {
  291. return new TypeDataPair(type, data);
  292. }
  293. };
  294.  
  295. Codec.defaultCodec = new Codec();
  296. return Codec;
  297. }();
  298. function toUint8Array(input) {
  299. if (input instanceof Uint8Array) {
  300. return input;
  301. } else if (ArrayBuffer.isView(input)) {
  302. return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
  303. } else if (input instanceof ArrayBuffer) {
  304. return new Uint8Array(input);
  305. } else {
  306. return Uint8Array.from(input);
  307. }
  308. }
  309. function toDataView(input) {
  310. if (input instanceof ArrayBuffer) {
  311. return new DataView(input);
  312. }
  313. var uint8Array = toUint8Array(input);
  314. return new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
  315. }
  316. var maxDepth = 100;
  317. var initialBufferSize = 2048;
  318. function formatHex(input) {
  319. return `${input < 0 ? "-" : ""}0x${Math.abs(input).toString(16).padStart(2, "0")}`;
  320. }
  321. var maxKeyL = 16;
  322. var maxKeyP = 16;
  323. var CacheSystem = function () {
  324. function Cache(maxKeyLength = maxKeyL, maxLengthPerKey = maxKeyP) {
  325. this.maxKeyLength = maxKeyLength;
  326. this.maxLengthPerKey = maxLengthPerKey;
  327. this.hitCount = 0;
  328. this.missCount = 0;
  329. this.cacheStorage = [];
  330. for (let i = 0; i < this.maxKeyLength; i++) {
  331. this.cacheStorage.push([]);
  332. }
  333. }
  334. Cache.prototype.canBeCached = function (keyLength) {
  335. return keyLength > 0 && keyLength <= this.maxKeyLength;
  336. };
  337. Cache.prototype.find = function (data, startIndex, length) {
  338. let cacheList = this.cacheStorage[length - 1];
  339. for (let i = 0; i < cacheList.length; i++) {
  340. let cachedItem = cacheList[i];
  341. let cachedBytes = cachedItem.bytes;
  342. let isMatch = true;
  343.  
  344. for (let j = 0; j < length; j++) {
  345. if (cachedBytes[j] !== data[startIndex + j]) {
  346. isMatch = false;
  347. break;
  348. }
  349. }
  350.  
  351. if (isMatch) {
  352. return cachedItem.str;
  353. }
  354. }
  355.  
  356. return null;
  357. };
  358. Cache.prototype.store = function (dataBytes, decodedString) {
  359. let cacheList = this.cacheStorage[dataBytes.length - 1];
  360. let cachedItem = {
  361. bytes: dataBytes,
  362. str: decodedString
  363. };
  364.  
  365. if (cacheList.length >= this.maxLengthPerKey) {
  366. cacheList[Math.random() * cacheList.length | 0] = cachedItem;
  367. } else {
  368. cacheList.push(cachedItem);
  369. }
  370. };
  371. Cache.prototype.decode = function (data, startIndex, length) {
  372. let foundString = this.find(data, startIndex, length);
  373.  
  374. if (foundString !== null) {
  375. this.hitCount++;
  376. return foundString;
  377. }
  378.  
  379. this.missCount++;
  380. let decodedString = decodeBytesToString(data, startIndex, length);
  381. let dataBytes = Uint8Array.prototype.slice.call(data, startIndex, startIndex + length);
  382. this.store(dataBytes, decodedString);
  383. return decodedString;
  384. };
  385.  
  386. return Cache;
  387. }();
  388. var asyncWrapper = globalThis && globalThis.__awaiter || function (context, args, PromiseConstructor, generator) {
  389. function ensurePromise(value) {
  390. if (value instanceof PromiseConstructor) {
  391. return value;
  392. } else {
  393. return new PromiseConstructor(function (resolve) {
  394. resolve(value);
  395. });
  396. }
  397. }
  398. return new (PromiseConstructor ||= Promise)(function (resolve, reject) {
  399. function handleNext(value) {
  400. try {
  401. processResult(generator.next(value));
  402. } catch (error) {
  403. reject(error);
  404. }
  405. }
  406. function handleThrow(error) {
  407. try {
  408. processResult(generator.throw(error));
  409. } catch (error) {
  410. reject(error);
  411. }
  412. }
  413. function processResult(result) {
  414. if (result.done) {
  415. resolve(result.value);
  416. } else {
  417. ensurePromise(result.value).then(handleNext, handleThrow);
  418. }
  419. }
  420. processResult((generator = generator.apply(context, args || [])).next());
  421. });
  422. };
  423. var generatorFunction = globalThis && globalThis.__generator || function (context, generatorFn) {
  424. var state = {
  425. label: 0,
  426. sent: function () {
  427. if (executionState[0] & 1) {
  428. throw executionState[1];
  429. }
  430. return executionState[1];
  431. },
  432. tryBlocks: [],
  433. operations: []
  434. };
  435.  
  436. var generator;
  437. var currentValue;
  438. var executionState;
  439. var resultHandler;
  440.  
  441. resultHandler = {
  442. next: step(0),
  443. throw: step(1),
  444. return: step(2)
  445. };
  446.  
  447. if (typeof Symbol === "function") {
  448. resultHandler[Symbol.iterator] = function () {
  449. return this;
  450. };
  451. }
  452.  
  453. return resultHandler;
  454.  
  455. function step(type) {
  456. return function (value) {
  457. return execute([type, value]);
  458. };
  459. }
  460.  
  461. function execute([type, value]) {
  462. if (generator) {
  463. throw new TypeError("Generator is already executing.");
  464. }
  465. while (state) {
  466. try {
  467. generator = 1;
  468. if (currentValue && (executionState = type & 2 ? currentValue.return : type ? currentValue.throw || ((executionState = currentValue.return) && executionState.call(currentValue), 0) : currentValue.next) && !(executionState = executionState.call(currentValue, value)).done) {
  469. return executionState;
  470. }
  471. currentValue = 0;
  472. if (executionState) {
  473. value = [type & 2, executionState.value];
  474. }
  475. switch (value[0]) {
  476. case 0:
  477. case 1:
  478. executionState = value;
  479. break;
  480. case 4:
  481. state.label++;
  482. return {
  483. value: value[1],
  484. done: false
  485. };
  486. case 5:
  487. state.label++;
  488. currentValue = value[1];
  489. value = [0];
  490. continue;
  491. case 7:
  492. value = state.operations.pop();
  493. state.tryBlocks.pop();
  494. continue;
  495. default:
  496. executionState = state.tryBlocks;
  497. if (!(executionState = executionState.length > 0 && executionState[executionState.length - 1]) && (value[0] === 6 || value[0] === 2)) {
  498. state = 0;
  499. continue;
  500. }
  501.  
  502. if (value[0] === 3 && (!executionState || value[1] > executionState[0] && value[1] < executionState[3])) {
  503. state.label = value[1];
  504. break;
  505. }
  506.  
  507. if (value[0] === 6 && state.label < executionState[1]) {
  508. state.label = executionState[1];
  509. executionState = value;
  510. break;
  511. }
  512.  
  513. if (executionState && state.label < executionState[2]) {
  514. state.label = executionState[2];
  515. state.operations.push(value);
  516. break;
  517. }
  518.  
  519. if (executionState[2]) {
  520. state.operations.pop();
  521. }
  522.  
  523. state.tryBlocks.pop();
  524. continue;
  525. }
  526.  
  527. value = generatorFn.call(context, state);
  528. } catch (error) {
  529. value = [6, error];
  530. currentValue = 0;
  531. } finally {
  532. generator = executionState = 0;
  533. }
  534. }
  535.  
  536. if (value[0] & 5) {
  537. throw value[1];
  538. }
  539.  
  540. return {
  541. value: value[0] ? value[1] : undefined,
  542. done: true
  543. };
  544. }
  545. };
  546. var asyncValues = globalThis && globalThis.__asyncValues || function (iterable) {
  547. if (!Symbol.asyncIterator) {
  548. throw new TypeError("Symbol.asyncIterator is not defined.");
  549. }
  550. var asyncIterator = iterable[Symbol.asyncIterator];
  551. var asyncIteratorWrapper;
  552. if (asyncIterator) {
  553. return asyncIterator.call(iterable);
  554. } else {
  555. iterable = typeof __values === "function" ? __values(iterable) : iterable[Symbol.iterator]();
  556. asyncIteratorWrapper = {};
  557. defineAsyncIteratorMethod("next");
  558. defineAsyncIteratorMethod("throw");
  559. defineAsyncIteratorMethod("return");
  560.  
  561. asyncIteratorWrapper[Symbol.asyncIterator] = function () {
  562. return this;
  563. };
  564.  
  565. return asyncIteratorWrapper;
  566. }
  567. function defineAsyncIteratorMethod(method) {
  568. asyncIteratorWrapper[method] = iterable[method] && function (argument) {
  569. return new Promise(function (resolve, reject) {
  570. var result = iterable[method](argument);
  571. handlePromise(resolve, reject, result.done, result.value);
  572. });
  573. };
  574. }
  575. function handlePromise(resolve, reject, done, value) {
  576. Promise.resolve(value).then(function (resolvedValue) {
  577. resolve({
  578. value: resolvedValue,
  579. done: done
  580. });
  581. }, reject);
  582. }
  583. };
  584. var Await = globalThis && globalThis.__await || function (value) {
  585. if (this instanceof Await) {
  586. this.value = value;
  587. return this;
  588. } else {
  589. return new Await(value);
  590. }
  591. };
  592. var asyncGenerator = globalThis && globalThis.__asyncGenerator || function (context, args, generatorFunction) {
  593. if (!Symbol.asyncIterator) {
  594. throw new TypeError("Symbol.asyncIterator is not defined.");
  595. }
  596. var generator = generatorFunction.apply(context, args || []);
  597. var asyncIteratorWrapper;
  598. var taskQueue = [];
  599. asyncIteratorWrapper = {};
  600. defineAsyncIteratorMethod("next");
  601. defineAsyncIteratorMethod("throw");
  602. defineAsyncIteratorMethod("return");
  603. asyncIteratorWrapper[Symbol.asyncIterator] = function () {
  604. return this;
  605. };
  606. return asyncIteratorWrapper;
  607. function defineAsyncIteratorMethod(method) {
  608. if (generator[method]) {
  609. asyncIteratorWrapper[method] = function (arg) {
  610. return new Promise(function (resolve, reject) {
  611. if (!(taskQueue.push([method, arg, resolve, reject]) > 1)) {
  612. processTask(method, arg);
  613. }
  614. });
  615. };
  616. }
  617. }
  618. function processTask(method, arg) {
  619. try {
  620. handleGeneratorResult(generator[method](arg));
  621. } catch (error) {
  622. handleError(taskQueue[0][3], error);
  623. }
  624. }
  625. function handleGeneratorResult(result) {
  626. if (result.value instanceof Await) {
  627. Promise.resolve(result.value.v).then(handleNext, handleThrow);
  628. } else {
  629. handleError(taskQueue[0][2], result);
  630. }
  631. }
  632.  
  633. function handleNext(value) {
  634. processTask("next", value);
  635. }
  636.  
  637. function handleThrow(error) {
  638. processTask("throw", error);
  639. }
  640.  
  641. function handleError(resolve, error) {
  642. resolve(error);
  643. taskQueue.shift();
  644. if (taskQueue.length) {
  645. processTask(taskQueue[0][0], taskQueue[0][1]);
  646. }
  647. }
  648. };
  649. function isStringOrNumber(input) {
  650. var inputType = typeof input;
  651. return inputType === "string" || inputType === "number";
  652. }
  653. var headbyte = -1;
  654. var emptyBufferView = new DataView(new ArrayBuffer(0));
  655. var byteArray = new Uint8Array(emptyBufferView.buffer);
  656. var errorCon = function () {
  657. try {
  658. emptyBufferView.getInt8(0);
  659. } catch (e) {
  660. return e.constructor;
  661. }
  662. throw new Error("never reached");
  663. }();
  664. var errorIns = new errorCon("Insufficient data");
  665. var cache_sys = new CacheSystem();
  666. var lite_encoder = function () {
  667. function init(extensionCodec = CodecRegistry.defaultCodec, content = undefined, maxDepths = maxDepth, initialBufferSizes = initialBufferSize, sortKeys = false, forceFloat32 = false, ignoreUndefined = false, forceIntergerToFloat = false) {
  668. this.extensionCodec = extensionCodec;
  669. this.context = content;
  670. this.maxDepth = maxDepths;
  671. this.initialBufferSize = initialBufferSizes;
  672. this.sortKeys = sortKeys;
  673. this.forceFloat32 = forceFloat32;
  674. this.ignoreUndefined = ignoreUndefined;
  675. this.forceIntegerToFloat = forceIntergerToFloat;
  676. this.pos = 0;
  677. this.view = new DataView(new ArrayBuffer(this.initialBufferSize));
  678. this.bytes = new Uint8Array(this.view.buffer);
  679. }
  680. init.prototype.reinitializeState = function () {
  681. this.pos = 0;
  682. };
  683. init.prototype.encodeSharedRef = function (obj) {
  684. this.reinitializeState();
  685. this.doEncode(obj, 1);
  686. return this.bytes.subarray(0, this.pos);
  687. };
  688. init.prototype.encode = function (obj) {
  689. this.reinitializeState();
  690. this.doEncode(obj, 1);
  691. return this.bytes.slice(0, this.pos);
  692. };
  693. init.prototype.doEncode = function (obj, depth) {
  694. if (depth > this.maxDepth) {
  695. throw new Error(`Object exceeds max depth of ${this.maxDepth}`);
  696. }
  697. if (obj == null) {
  698. this.encodeNil();
  699. } else if (typeof obj === "boolean") {
  700. this.encodeBoolean(obj);
  701. } else if (typeof obj === "number") {
  702. this.encodeNumber(obj);
  703. } else if (typeof obj === "string") {
  704. this.encodeString(obj);
  705. } else {
  706. this.encodeObject(obj, depth);
  707. }
  708. };
  709. init.prototype.ensureBufferSizeToWrite = function (size) {
  710. var req = this.pos + size;
  711. if (this.view.byteLength < req) {
  712. this.resizeBuffer(req * 2);
  713. }
  714. };
  715. init.prototype.resizeBuffer = function (newSize) {
  716. var newBuffer = new ArrayBuffer(newSize);
  717. var newBytes = new Uint8Array(newBuffer);
  718. var newView = new DataView(newBuffer);
  719. newBytes.set(this.bytes);
  720. this.view = newView;
  721. this.bytes = newBytes;
  722. };
  723. init.prototype.encodeNil = function () {
  724. this.writeU8(192);
  725. };
  726. init.prototype.encodeBoolean = function (value) {
  727. if (value === false) {
  728. this.writeU8(194);
  729. } else {
  730. this.writeU8(195);
  731. }
  732. };
  733. init.prototype.encodeNumber = function (value) {
  734. if (Number.isSafeInteger(value) && !this.forceIntegerToFloat) {
  735. if (value >= 0) {
  736. if (value < 128) {
  737. this.writeU8(value);
  738. } else if (value < 256) {
  739. this.writeU8(204);
  740. this.writeU8(value);
  741. } else if (value < 65536) {
  742. this.writeU8(205);
  743. this.writeU16(value);
  744. } else if (value < POW32) {
  745. this.writeU8(206);
  746. this.writeU32(value);
  747. } else {
  748. this.writeU8(207);
  749. this.writeU64(value);
  750. }
  751. } else if (value >= -32) {
  752. this.writeU8(value + 32 | 224);
  753. } else if (value >= -128) {
  754. this.writeU8(208);
  755. this.writeI8(value);
  756. } else if (value >= -32768) {
  757. this.writeU8(209);
  758. this.writeI16(value);
  759. } else if (value >= -2147483648) {
  760. this.writeU8(210);
  761. this.writeI32(value);
  762. } else {
  763. this.writeU8(211);
  764. this.writeI64(value);
  765. }
  766. } else if (this.forceFloat32) {
  767. this.writeU8(202);
  768. this.writeF32(value);
  769. } else {
  770. this.writeU8(203);
  771. this.writeF64(value);
  772. }
  773. };
  774. init.prototype.writeStringHeader = function (value) {
  775. if (value < 32) {
  776. this.writeU8(160 + value);
  777. } else if (value < 256) {
  778. this.writeU8(217);
  779. this.writeU8(value);
  780. } else if (value < 65536) {
  781. this.writeU8(218);
  782. this.writeU16(value);
  783. } else if (value < POW32) {
  784. this.writeU8(219);
  785. this.writeU32(value);
  786. } else {
  787. throw new Error(`Too long string: ${value} bytes in UTF-8`);
  788. }
  789. };
  790. init.prototype.encodeString = function (value) {
  791. var numZ = 5;
  792. var cope = value.length;
  793. if (cope > encodingLimit) {
  794. let resul = byteLengthUTF8(value);
  795. this.ensureBufferSizeToWrite(numZ + resul);
  796. this.writeStringHeader(resul);
  797. encodeFunc(value, this.bytes, this.pos);
  798. this.pos += resul;
  799. } else {
  800. let resul = byteLengthUTF8(value);
  801. this.ensureBufferSizeToWrite(numZ + resul);
  802. this.writeStringHeader(resul);
  803. encodeUTF8(value, this.bytes, this.pos);
  804. this.pos += resul;
  805. }
  806. };
  807. init.prototype.encodeObject = function (obj, depth) {
  808. var data = this.extensionCodec.tryToEncode(obj, this.context);
  809. if (data != null) {
  810. this.encodeExtension(data);
  811. } else if (Array.isArray(obj)) {
  812. this.encodeArray(obj, depth);
  813. } else if (ArrayBuffer.isView(obj)) {
  814. this.encodeBinary(obj);
  815. } else if (typeof obj == "object") {
  816. this.encodeMap(obj, depth);
  817. } else {
  818. throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(obj)}`);
  819. }
  820. };
  821. init.prototype.encodeBinary = function (binaryData) {
  822. var dataLength = binaryData.byteLength;
  823. if (dataLength < 256) {
  824. this.writeU8(196);
  825. this.writeU8(dataLength);
  826. } else if (dataLength < 65536) {
  827. this.writeU8(197);
  828. this.writeU16(dataLength);
  829. } else if (dataLength < POW32) {
  830. this.writeU8(198);
  831. this.writeU32(dataLength);
  832. } else {
  833. throw new Error(`Too large binary: ${dataLength}`);
  834. }
  835. var byteArray = toUint8Array(binaryData);
  836. this.writeU8a(byteArray);
  837. };
  838.  
  839. init.prototype.encodeArray = function (arrayData, depth) {
  840. var arrayLength = arrayData.length;
  841. if (arrayLength < 16) {
  842. this.writeU8(144 + arrayLength);
  843. } else if (arrayLength < 65536) {
  844. this.writeU8(220);
  845. this.writeU16(arrayLength);
  846. } else if (arrayLength < POW32) {
  847. this.writeU8(221);
  848. this.writeU32(arrayLength);
  849. } else {
  850. throw new Error(`Too large array: ${arrayLength}`);
  851. }
  852. for (var index = 0, array = arrayData; index < array.length; index++) {
  853. var item = array[index];
  854. this.doEncode(item, depth + 1);
  855. }
  856. };
  857.  
  858. init.prototype.countWithoutUndefined = function (obj, keys) {
  859. var count = 0;
  860. for (var index = 0, keyArray = keys; index < keyArray.length; index++) {
  861. var key = keyArray[index];
  862. if (obj[key] !== undefined) {
  863. count++;
  864. }
  865. }
  866. return count;
  867. };
  868.  
  869. init.prototype.encodeMap = function (mapData, depth) {
  870. var keys = Object.keys(mapData);
  871. if (this.sortKeys) {
  872. keys.sort();
  873. }
  874. var keyCount = this.ignoreUndefined ? this.countWithoutUndefined(mapData, keys) : keys.length;
  875. if (keyCount < 16) {
  876. this.writeU8(128 + keyCount);
  877. } else if (keyCount < 65536) {
  878. this.writeU8(222);
  879. this.writeU16(keyCount);
  880. } else if (keyCount < POW32) {
  881. this.writeU8(223);
  882. this.writeU32(keyCount);
  883. } else {
  884. throw new Error(`Too large map object: ${keyCount}`);
  885. }
  886. for (var index = 0, keyArray = keys; index < keyArray.length; index++) {
  887. var key = keyArray[index];
  888. var value = mapData[key];
  889. if (!this.ignoreUndefined || value !== undefined) {
  890. this.encodeString(key);
  891. this.doEncode(value, depth + 1);
  892. }
  893. }
  894. };
  895.  
  896. init.prototype.encodeExtension = function (value) {
  897. var data = value.data.length;
  898. if (data === 1) {
  899. this.writeU8(212);
  900. } else if (data === 2) {
  901. this.writeU8(213);
  902. } else if (data === 4) {
  903. this.writeU8(214);
  904. } else if (data === 8) {
  905. this.writeU8(215);
  906. } else if (data === 16) {
  907. this.writeU8(216);
  908. } else if (data < 256) {
  909. this.writeU8(199);
  910. this.writeU8(data);
  911. } else if (data < 65536) {
  912. this.writeU8(200);
  913. this.writeU16(data);
  914. } else if (data < POW32) {
  915. this.writeU8(201);
  916. this.writeU32(data);
  917. } else {
  918. throw new Error(`Too large extension object: ${data}`);
  919. }
  920. this.writeI8(value.type);
  921. this.writeU8a(value.data);
  922. };
  923. init.prototype.writeU8 = function (value) {
  924. this.ensureBufferSizeToWrite(1);
  925. this.view.setUint8(this.pos, value);
  926. this.pos++;
  927. };
  928. init.prototype.writeU8a = function (value) {
  929. var data = value.length;
  930. this.ensureBufferSizeToWrite(data);
  931. this.bytes.set(value, this.pos);
  932. this.pos += data;
  933. };
  934. init.prototype.writeI8 = function (value) {
  935. this.ensureBufferSizeToWrite(1);
  936. this.view.setInt8(this.pos, value);
  937. this.pos++;
  938. };
  939. init.prototype.writeU16 = function (value) {
  940. this.ensureBufferSizeToWrite(2);
  941. this.view.setUint16(this.pos, value);
  942. this.pos += 2;
  943. };
  944. init.prototype.writeI16 = function (value) {
  945. this.ensureBufferSizeToWrite(2);
  946. this.view.setInt16(this.pos, value);
  947. this.pos += 2;
  948. };
  949. init.prototype.writeU32 = function (value) {
  950. this.ensureBufferSizeToWrite(4);
  951. this.view.setUint32(this.pos, value);
  952. this.pos += 4;
  953. };
  954. init.prototype.writeI32 = function (value) {
  955. this.ensureBufferSizeToWrite(4);
  956. this.view.setInt32(this.pos, value);
  957. this.pos += 4;
  958. };
  959. init.prototype.writeF32 = function (value) {
  960. this.ensureBufferSizeToWrite(4);
  961. this.view.setFloat32(this.pos, value);
  962. this.pos += 4;
  963. };
  964. init.prototype.writeF64 = function (value) {
  965. this.ensureBufferSizeToWrite(8);
  966. this.view.setFloat64(this.pos, value);
  967. this.pos += 8;
  968. };
  969. init.prototype.writeU64 = function (value) {
  970. this.ensureBufferSizeToWrite(8);
  971. split64.mode_1(this.view, this.pos, value);
  972. this.pos += 8;
  973. };
  974. init.prototype.writeI64 = function (value) {
  975. this.ensureBufferSizeToWrite(8);
  976. split64.mode_2(this.view, this.pos, value);
  977. this.pos += 8;
  978. };
  979. return init;
  980. }();
  981. var lite_decoder = function () {
  982. function init(codec = CodecRegistry.defaultCodec, context = undefined, maxStrLen = maxUint32, maxBinLen = maxUint32, maxArrayLen = maxUint32, maxMapLen = maxUint32, maxExtLen = maxUint32, keyDecoder = cache_sys) {
  983. this.extensionCodec = codec;
  984. this.context = context;
  985. this.maxStrLength = maxStrLen;
  986. this.maxBinLength = maxBinLen;
  987. this.maxArrayLength = maxArrayLen;
  988. this.maxMapLength = maxMapLen;
  989. this.maxExtLength = maxExtLen;
  990. this.keyDecoder = keyDecoder;
  991. this.totalPos = 0;
  992. this.pos = 0;
  993. this.view = emptyBufferView;
  994. this.bytes = byteArray;
  995. this.headByte = headbyte;
  996. this.stack = [];
  997. }
  998. init.prototype.reinitializeState = function () {
  999. this.totalPos = 0;
  1000. this.headByte = headbyte;
  1001. this.stack.length = 0;
  1002. };
  1003. init.prototype.setBuffer = function (buffer) {
  1004. this.bytes = toUint8Array(buffer);
  1005. this.view = toDataView(this.bytes);
  1006. this.pos = 0;
  1007. };
  1008. init.prototype.appendBuffer = function (buffer) {
  1009. if (this.headByte === headbyte && !this.hasRemaining(1)) {
  1010. this.setBuffer(buffer);
  1011. } else {
  1012. var remainingBytes = this.bytes.subarray(this.pos);
  1013. var newBytes = toUint8Array(buffer);
  1014. var combinedBytes = new Uint8Array(remainingBytes.length + newBytes.length);
  1015. combinedBytes.set(remainingBytes);
  1016. combinedBytes.set(newBytes, remainingBytes.length);
  1017. this.setBuffer(combinedBytes);
  1018. }
  1019. };
  1020. init.prototype.hasRemaining = function (lengthsc) {
  1021. return this.view.byteLength - this.pos >= lengthsc;
  1022. };
  1023. init.prototype.createExtraByteError = function (pos) {
  1024. var vThis = this;
  1025. var currV = vThis.view;
  1026. var currP = vThis.pos;
  1027. return new RangeError(`Extra ${currV.byteLength - currP} of ${currV.byteLength} byte(s) found at buffer[${pos}]`);
  1028. };
  1029. init.prototype.decode = function (array) {
  1030. this.reinitializeState();
  1031. this.setBuffer(array);
  1032. var decodedData = this.doDecodeSync();
  1033. if (this.hasRemaining(1)) {
  1034. throw this.createExtraByteError(this.pos);
  1035. }
  1036. return decodedData;
  1037. };
  1038. init.prototype.decodeMulti = function (data) {
  1039. return generatorFunction(this, function (step) {
  1040. if (step.label === 0) {
  1041. this.reinitializeState();
  1042. this.setBuffer(data);
  1043. step.label = 1;
  1044. } else if (step.label === 1) {
  1045. if (this.hasRemaining(1)) {
  1046. return [4, this.doDecodeSync()];
  1047. } else {
  1048. return [3, 3];
  1049. }
  1050. } else if (step.label === 2) {
  1051. step.sent();
  1052. return [3, 1];
  1053. } else if (step.label === 3) {
  1054. return [2];
  1055. }
  1056. });
  1057. };
  1058. init.prototype.decodeAsync = function (input) {
  1059. var asyncIterator;
  1060. var currentChunk;
  1061. var decodedValue;
  1062. var decodeResult;
  1063. var decodingFailed;
  1064. var error;
  1065. var result;
  1066. var bufferChunk;
  1067. var finalResult;
  1068. var nextChunk;
  1069. return asyncWrapper(this, undefined, undefined, function () {
  1070. return generatorFunction(this, function (step) {
  1071. if (step.label === 0) {
  1072. decodingFailed = false;
  1073. step.label = 1;
  1074. } else if (step.label === 1) {
  1075. step.trys.push([1, 6, 7, 12]);
  1076. asyncIterator = asyncValues(input);
  1077. step.label = 2;
  1078. } else if (step.label === 2) {
  1079. return [4, asyncIterator.next()];
  1080. } else if (step.label === 3) {
  1081. currentChunk = step.sent();
  1082. if (currentChunk.done) {
  1083. return [3, 5];
  1084. }
  1085. bufferChunk = currentChunk.value;
  1086. if (decodingFailed) {
  1087. throw this.createExtraByteError(this.totalPos);
  1088. }
  1089. this.appendBuffer(bufferChunk);
  1090. try {
  1091. decodedValue = this.doDecodeSync();
  1092. decodingFailed = true;
  1093. } catch (exception) {
  1094. if (!(exception instanceof errorCon)) {
  1095. throw exception;
  1096. }
  1097. }
  1098. this.totalPos += this.pos;
  1099. step.label = 4;
  1100. } else if (step.label === 4) {
  1101. return [3, 2];
  1102. } else if (step.label === 5) {
  1103. return [3, 12];
  1104. } else if (step.label === 6) {
  1105. error = step.sent();
  1106. result = {
  1107. error: error
  1108. };
  1109. return [3, 12];
  1110. } else if (step.label === 7) {
  1111. step.trys.push([7,, 10, 11]);
  1112. if (currentChunk && !currentChunk.done && (finalResult = asyncIterator.return)) {
  1113. return [4, finalResult.call(asyncIterator)];
  1114. } else {
  1115. return [3, 9];
  1116. }
  1117. } else if (step.label === 8) {
  1118. step.sent();
  1119. step.label = 9;
  1120. } else if (step.label === 9) {
  1121. return [3, 11];
  1122. } else if (step.label === 10) {
  1123. if (result) {
  1124. throw result.error;
  1125. }
  1126. return [7];
  1127. } else if (step.label === 11) {
  1128. return [7];
  1129. } else if (step.label === 12) {
  1130. if (decodingFailed) {
  1131. if (this.hasRemaining(1)) {
  1132. throw this.createExtraByteError(this.totalPos);
  1133. }
  1134. return [2, decodedValue];
  1135. }
  1136. nextChunk = this;
  1137. throw new RangeError(`Insufficient data in parsing ${formatHex(nextChunk.headByte)} at ${nextChunk.totalPos} (${nextChunk.pos} in the current buffer)`);
  1138. }
  1139. });
  1140. });
  1141. };
  1142.  
  1143. init.prototype.decodeArrayStream = function (array) {
  1144. return this.decodeMultiAsync(array, true);
  1145. };
  1146. init.prototype.decodeStream = function (stream) {
  1147. return this.decodeMultiAsync(stream, false);
  1148. };
  1149. init.prototype.decodeMultiAsync = function (inputData, hasArraySize) {
  1150. return asyncGenerator(this, arguments, function () {
  1151. var arraySize;
  1152. var currentPos;
  1153. var currentValue;
  1154. var chunk;
  1155. var decodedValue;
  1156. var errorResult;
  1157. var finalResult;
  1158. var bufferChunk;
  1159. return generatorFunction(this, function (step) {
  1160. if (step.label === 0) {
  1161. arraySize = hasArraySize;
  1162. currentPos = -1;
  1163. step.label = 1;
  1164. } else if (step.label === 1) {
  1165. step.trys.push([1, 13, 14, 19]);
  1166. currentValue = asyncValues(inputData);
  1167. step.label = 2;
  1168. } else if (step.label === 2) {
  1169. return [4, Await(currentValue.next())];
  1170. } else if (step.label === 3) {
  1171. chunk = step.sent();
  1172. if (chunk.done) {
  1173. return [3, 12];
  1174. }
  1175. bufferChunk = chunk.value;
  1176. if (hasArraySize && currentPos === 0) {
  1177. throw this.createExtraByteError(this.totalPos);
  1178. }
  1179. this.appendBuffer(bufferChunk);
  1180. if (arraySize) {
  1181. currentPos = this.readArraySize();
  1182. arraySize = false;
  1183. this.complete();
  1184. }
  1185. step.label = 4;
  1186. } else if (step.label === 4) {
  1187. step.trys.push([4, 9,, 10]);
  1188. step.label = 5;
  1189. } else if (step.label === 5) {
  1190. return [4, Await(this.doDecodeSync())];
  1191. } else if (step.label === 6) {
  1192. return [4, step.sent()];
  1193. } else if (step.label === 7) {
  1194. step.sent();
  1195. if (--currentPos === 0) {
  1196. return [3, 8];
  1197. } else {
  1198. return [3, 5];
  1199. }
  1200. } else if (step.label === 8) {
  1201. return [3, 10];
  1202. } else if (step.label === 9) {
  1203. decodedValue = step.sent();
  1204. if (!(decodedValue instanceof errorCon)) {
  1205. throw decodedValue;
  1206. }
  1207. return [3, 10];
  1208. } else if (step.label === 10) {
  1209. this.totalPos += this.pos;
  1210. step.label = 11;
  1211. } else if (step.label === 11) {
  1212. return [3, 2];
  1213. } else if (step.label === 12) {
  1214. return [3, 19];
  1215. } else if (step.label === 13) {
  1216. errorResult = step.sent();
  1217. finalResult = {
  1218. error: errorResult
  1219. };
  1220. return [3, 19];
  1221. } else if (step.label === 14) {
  1222. step.trys.push([14,, 17, 18]);
  1223. if (chunk && !chunk.done && (bufferChunk = currentValue.return)) {
  1224. return [4, Await(bufferChunk.call(currentValue))];
  1225. } else {
  1226. return [3, 16];
  1227. }
  1228. } else if (step.label === 15) {
  1229. step.sent();
  1230. step.label = 16;
  1231. } else if (step.label === 16) {
  1232. return [3, 18];
  1233. } else if (step.label === 17) {
  1234. if (finalResult) {
  1235. throw finalResult.error;
  1236. }
  1237. return [7];
  1238. } else if (step.label === 18) {
  1239. return [7];
  1240. } else if (step.label === 19) {
  1241. return [2];
  1242. }
  1243. });
  1244. });
  1245. };
  1246.  
  1247. init.prototype.doDecodeSync = function () {
  1248. e: while (true) {
  1249. var headByte = this.readHeadByte();
  1250. var decodedValue = undefined;
  1251. if (headByte >= 224) {
  1252. decodedValue = headByte - 256;
  1253. } else if (headByte < 192) {
  1254. if (headByte < 128) {
  1255. decodedValue = headByte;
  1256. } else if (headByte < 144) {
  1257. var arraySize = headByte - 128;
  1258. if (arraySize !== 0) {
  1259. this.pushMapState(arraySize);
  1260. this.complete();
  1261. continue e;
  1262. } else {
  1263. decodedValue = {};
  1264. }
  1265. } else if (headByte < 160) {
  1266. let arraySize = headByte - 144;
  1267. if (arraySize !== 0) {
  1268. this.pushArrayState(arraySize);
  1269. this.complete();
  1270. continue e;
  1271. } else {
  1272. decodedValue = [];
  1273. }
  1274. } else {
  1275. var utf8StringLength = headByte - 160;
  1276. decodedValue = this.decodeUtf8String(utf8StringLength, 0);
  1277. }
  1278. } else if (headByte === 192) {
  1279. decodedValue = null;
  1280. } else if (headByte === 194) {
  1281. decodedValue = false;
  1282. } else if (headByte === 195) {
  1283. decodedValue = true;
  1284. } else if (headByte === 202) {
  1285. decodedValue = this.readF32();
  1286. } else if (headByte === 203) {
  1287. decodedValue = this.readF64();
  1288. } else if (headByte === 204) {
  1289. decodedValue = this.readU8();
  1290. } else if (headByte === 205) {
  1291. decodedValue = this.readU16();
  1292. } else if (headByte === 206) {
  1293. decodedValue = this.readU32();
  1294. } else if (headByte === 207) {
  1295. decodedValue = this.readU64();
  1296. } else if (headByte === 208) {
  1297. decodedValue = this.readI8();
  1298. } else if (headByte === 209) {
  1299. decodedValue = this.readI16();
  1300. } else if (headByte === 210) {
  1301. decodedValue = this.readI32();
  1302. } else if (headByte === 211) {
  1303. decodedValue = this.readI64();
  1304. } else if (headByte === 217) {
  1305. let utf8Length = this.lookU8();
  1306. decodedValue = this.decodeUtf8String(utf8Length, 1);
  1307. } else if (headByte === 218) {
  1308. let utf8Length = this.lookU16();
  1309. decodedValue = this.decodeUtf8String(utf8Length, 2);
  1310. } else if (headByte === 219) {
  1311. var utf8Length = this.lookU32();
  1312. decodedValue = this.decodeUtf8String(utf8Length, 4);
  1313. } else if (headByte === 220) {
  1314. let arraySize = this.readU16();
  1315. if (arraySize !== 0) {
  1316. this.pushArrayState(arraySize);
  1317. this.complete();
  1318. continue e;
  1319. } else {
  1320. decodedValue = [];
  1321. }
  1322. } else if (headByte === 221) {
  1323. let arraySize = this.readU32();
  1324. if (arraySize !== 0) {
  1325. this.pushArrayState(arraySize);
  1326. this.complete();
  1327. continue e;
  1328. } else {
  1329. decodedValue = [];
  1330. }
  1331. } else if (headByte === 222) {
  1332. var mapSize = this.readU16();
  1333. if (mapSize !== 0) {
  1334. this.pushMapState(mapSize);
  1335. this.complete();
  1336. continue e;
  1337. } else {
  1338. decodedValue = {};
  1339. }
  1340. } else if (headByte === 223) {
  1341. let mapSize = this.readU32();
  1342. if (mapSize !== 0) {
  1343. this.pushMapState(mapSize);
  1344. this.complete();
  1345. continue e;
  1346. } else {
  1347. decodedValue = {};
  1348. }
  1349. } else if (headByte === 196) {
  1350. var binaryLength = this.lookU8();
  1351. decodedValue = this.decodeBinary(binaryLength, 1);
  1352. } else if (headByte === 197) {
  1353. let binaryLength = this.lookU16();
  1354. decodedValue = this.decodeBinary(binaryLength, 2);
  1355. } else if (headByte === 198) {
  1356. let binaryLength = this.lookU32();
  1357. decodedValue = this.decodeBinary(binaryLength, 4);
  1358. } else if (headByte === 212) {
  1359. decodedValue = this.decodeExtension(1, 0);
  1360. } else if (headByte === 213) {
  1361. decodedValue = this.decodeExtension(2, 0);
  1362. } else if (headByte === 214) {
  1363. decodedValue = this.decodeExtension(4, 0);
  1364. } else if (headByte === 215) {
  1365. decodedValue = this.decodeExtension(8, 0);
  1366. } else if (headByte === 216) {
  1367. decodedValue = this.decodeExtension(16, 0);
  1368. } else if (headByte === 199) {
  1369. var extensionLength = this.lookU8();
  1370. decodedValue = this.decodeExtension(extensionLength, 1);
  1371. } else if (headByte === 200) {
  1372. let extensionLength = this.lookU16();
  1373. decodedValue = this.decodeExtension(extensionLength, 2);
  1374. } else if (headByte === 201) {
  1375. let extensionLength = this.lookU32();
  1376. decodedValue = this.decodeExtension(extensionLength, 4);
  1377. } else {
  1378. throw new createCustomError(`Unrecognized type byte: ${formatHex(headByte)}`);
  1379. }
  1380. this.complete();
  1381. for (var state = this.stack; state.length > 0;) {
  1382. var stackState = state[state.length - 1];
  1383. if (stackState.type === 0) {
  1384. stackState.array[stackState.position] = decodedValue;
  1385. stackState.position++;
  1386. if (stackState.position === stackState.size) {
  1387. state.pop();
  1388. decodedValue = stackState.array;
  1389. } else {
  1390. continue e;
  1391. }
  1392. } else if (stackState.type === 1) {
  1393. if (!isStringOrNumber(decodedValue)) {
  1394. throw new createCustomError("The type of key must be string or number but " + typeof decodedValue);
  1395. }
  1396. if (decodedValue === "__proto__") {
  1397. throw new createCustomError("The key __proto__ is not allowed");
  1398. }
  1399. stackState.key = decodedValue;
  1400. stackState.type = 2;
  1401. continue e;
  1402. } else {
  1403. stackState.map[stackState.key] = decodedValue;
  1404. stackState.readCount++;
  1405. if (stackState.readCount === stackState.size) {
  1406. state.pop();
  1407. decodedValue = stackState.map;
  1408. } else {
  1409. stackState.key = null;
  1410. stackState.type = 1;
  1411. continue e;
  1412. }
  1413. }
  1414. }
  1415. return decodedValue;
  1416. }
  1417. };
  1418.  
  1419. init.prototype.readHeadByte = function () {
  1420. if (this.headByte === headbyte) {
  1421. this.headByte = this.readU8();
  1422. }
  1423. return this.headByte;
  1424. };
  1425. init.prototype.complete = function () {
  1426. this.headByte = headbyte;
  1427. };
  1428. init.prototype.readArraySize = function () {
  1429. var byte = this.readHeadByte();
  1430. switch (byte) {
  1431. case 220:
  1432. return this.readU16();
  1433. case 221:
  1434. return this.readU32();
  1435. default:
  1436. {
  1437. if (byte < 160) {
  1438. return byte - 144;
  1439. }
  1440. throw new createCustomError(`Unrecognized array type byte: ${formatHex(byte)}`);
  1441. }
  1442. }
  1443. };
  1444. init.prototype.pushMapState = function (leap) {
  1445. if (leap > this.maxMapLength) {
  1446. throw new createCustomError(`Max length exceeded: map length (${leap}) > maxMapLengthLength (${this.maxMapLength})`);
  1447. }
  1448. this.stack.push({
  1449. type: 1,
  1450. size: leap,
  1451. key: null,
  1452. readCount: 0,
  1453. map: {}
  1454. });
  1455. };
  1456. init.prototype.pushArrayState = function (arr) {
  1457. if (arr > this.maxArrayLength) {
  1458. throw new createCustomError(`Max length exceeded: array length (${arr}) > maxArrayLength (${this.maxArrayLength})`);
  1459. }
  1460. this.stack.push({
  1461. type: 0,
  1462. size: arr,
  1463. array: new Array(arr),
  1464. position: 0
  1465. });
  1466. };
  1467. init.prototype.decodeUtf8String = function (byteLength, offset) {
  1468. var keyDecoder;
  1469. if (byteLength > this.maxStrLength) {
  1470. throw new createCustomError(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
  1471. }
  1472. if (this.bytes.byteLength < this.pos + offset + byteLength) {
  1473. throw errorIns;
  1474. }
  1475. var startPosition = this.pos + offset;
  1476. var decodedValue;
  1477. if (this.stateIsMapKey() && (keyDecoder = this.keyDecoder) !== null && keyDecoder !== undefined && keyDecoder.canBeCached(byteLength)) {
  1478. decodedValue = keyDecoder.decode(this.bytes, startPosition, byteLength);
  1479. } else if (byteLength > decodingLimit) {
  1480. decodedValue = decodeSubarray(this.bytes, startPosition, byteLength);
  1481. } else {
  1482. decodedValue = decodeBytesToString(this.bytes, startPosition, byteLength);
  1483. }
  1484. this.pos += offset + byteLength;
  1485. return decodedValue;
  1486. };
  1487.  
  1488. init.prototype.stateIsMapKey = function () {
  1489. if (this.stack.length > 0) {
  1490. var satc = this.stack[this.stack.length - 1];
  1491. return satc.type === 1;
  1492. }
  1493. return false;
  1494. };
  1495. init.prototype.decodeBinary = function (bin, offset) {
  1496. if (bin > this.maxBinLength) {
  1497. throw new createCustomError(`Max length exceeded: bin length (${bin}) > maxBinLength (${this.maxBinLength})`);
  1498. }
  1499. if (!this.hasRemaining(bin + offset)) {
  1500. throw errorIns;
  1501. }
  1502. var comb = this.pos + offset;
  1503. var result = this.bytes.subarray(comb, comb + bin);
  1504. this.pos += offset + bin;
  1505. return result;
  1506. };
  1507. init.prototype.decodeExtension = function (ext, offset) {
  1508. if (ext > this.maxExtLength) {
  1509. throw new createCustomError(`Max length exceeded: ext length (${ext}) > maxExtLength (${this.maxExtLength})`);
  1510. }
  1511. var getInt8 = this.view.getInt8(this.pos + offset);
  1512. var decodeBinary = this.decodeBinary(ext, offset + 1);
  1513. return this.extensionCodec.decode(decodeBinary, getInt8, this.context);
  1514. };
  1515. init.prototype.lookU8 = function () {
  1516. return this.view.getUint8(this.pos);
  1517. };
  1518. init.prototype.lookU16 = function () {
  1519. return this.view.getUint16(this.pos);
  1520. };
  1521. init.prototype.lookU32 = function () {
  1522. return this.view.getUint32(this.pos);
  1523. };
  1524. init.prototype.readU8 = function () {
  1525. let data = this.view.getUint8(this.pos);
  1526. this.pos++;
  1527. return data;
  1528. };
  1529. init.prototype.readI8 = function () {
  1530. let data = this.view.getInt8(this.pos);
  1531. this.pos++;
  1532. return data;
  1533. };
  1534. init.prototype.readU16 = function () {
  1535. let data = this.view.getUint16(this.pos);
  1536. this.pos += 2;
  1537. return data;
  1538. };
  1539. init.prototype.readI16 = function () {
  1540. let data = this.view.getInt16(this.pos);;
  1541. this.pos += 2;
  1542. return data;
  1543. };
  1544. init.prototype.readU32 = function () {
  1545. let data = this.view.getUint32(this.pos);
  1546. this.pos += 4;
  1547. return data;
  1548. };
  1549. init.prototype.readI32 = function () {
  1550. let data = this.view.getInt32(this.pos);
  1551. this.pos += 4;
  1552. return data;
  1553. };
  1554. init.prototype.readU64 = function () {
  1555. let data = combine64(this.view, this.pos);
  1556. this.pos += 8;
  1557. return data;
  1558. };
  1559. init.prototype.readI64 = function () {
  1560. let data = combine64(this.view, this.pos);
  1561. this.pos += 8;
  1562. return data;
  1563. };
  1564. init.prototype.readF32 = function () {
  1565. let data = this.view.getFloat32(this.pos);
  1566. this.pos += 4;
  1567. return data;
  1568. };
  1569. init.prototype.readF64 = function () {
  1570. let data = this.view.getFloat64(this.pos);
  1571. this.pos += 8;
  1572. return data;
  1573. };
  1574. return init;
  1575. }();
  1576. var msgpack = window.msgpack = {
  1577. decode: (array) => new lite_decoder().decode(array),
  1578. encode: (data) => new lite_encoder().encode(data),
  1579. }