msgpack.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var msgpack = msgpack || {};
  2. // https://github.com/msgpack/msgpack-javascript/blob/master/src/Decoder.ts
  3. msgpack.BinaryReader = class {
  4. static open(data, callback) {
  5. return new msgpack.BinaryReader(data, callback);
  6. }
  7. constructor(buffer, callback) {
  8. this._buffer = buffer;
  9. this._callback = callback;
  10. this._position = 0;
  11. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  12. }
  13. read() {
  14. const value = this.value();
  15. return value;
  16. }
  17. value() {
  18. const c = this.byte();
  19. if (c >= 0xe0) {
  20. return c - 0x100;
  21. }
  22. if (c < 0xC0) {
  23. if (c < 0x80) {
  24. return c;
  25. }
  26. if (c < 0x90) {
  27. return this.map(c - 0x80);
  28. }
  29. if (c < 0xa0) {
  30. return this.array(c - 0x90);
  31. }
  32. return this.string(c - 0xa0);
  33. }
  34. switch (c) {
  35. case 0xC0: return null;
  36. case 0xC2: return false;
  37. case 0xC3: return true;
  38. case 0xC4: return this.bytes(this.byte());
  39. case 0xC5: return this.bytes(this.uint16());
  40. case 0xC6: return this.bytes(this.uint32());
  41. case 0xC7: return this.extension(this.byte());
  42. case 0xC8: return this.extension(this.uint16());
  43. case 0xC9: return this.extension(this.uint32());
  44. case 0xCA: return this.float32();
  45. case 0xCB: return this.float64();
  46. case 0xCC: return this.byte();
  47. case 0xCD: return this.uint16();
  48. case 0xCE: return this.uint32();
  49. case 0xCF: return this.uint64();
  50. case 0xD0: return this.int8();
  51. case 0xD1: return this.int16();
  52. case 0xD2: return this.int32();
  53. case 0xD3: return this.int64();
  54. case 0xD4: return this.extension(1);
  55. case 0xD5: return this.extension(2);
  56. case 0xD6: return this.extension(4);
  57. case 0xD7: return this.extension(8);
  58. case 0xD8: return this.extension(16);
  59. case 0xD9: return this.string(this.byte());
  60. case 0xDA: return this.string(this.uint16());
  61. case 0xDB: return this.string(this.uint32());
  62. case 0xDC: return this.array(this.uint16());
  63. case 0xDD: return this.array(this.uint32());
  64. case 0xDE: return this.map(this.uint16());
  65. case 0xDF: return this.map(this.uint32());
  66. default: throw new msgpack.Error("Invalid code '" + c + "'.");
  67. }
  68. }
  69. map(size) {
  70. const map = {};
  71. for (let i = 0; i < size; i++) {
  72. const key = this.value();
  73. const value = this.value();
  74. map[key] = value;
  75. }
  76. return map;
  77. }
  78. array(size) {
  79. const array = new Array(size);
  80. for (let i = 0; i < size; i++) {
  81. array[i] = this.value();
  82. }
  83. return array;
  84. }
  85. extension(size) {
  86. const code = this.byte();
  87. const data = this.bytes(size);
  88. return this._callback(code, data);
  89. }
  90. seek(position) {
  91. this._position = position;
  92. }
  93. skip(offset) {
  94. this._position += offset;
  95. if (this._position > this._buffer.length) {
  96. throw new msgpack.Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  97. }
  98. }
  99. bytes(size) {
  100. const data = this._buffer.subarray(this._position, this._position + size);
  101. this._position += size;
  102. return data;
  103. }
  104. byte() {
  105. const position = this._position;
  106. this.skip(1);
  107. return this._buffer[position];
  108. }
  109. uint16() {
  110. const position = this._position;
  111. this.skip(2);
  112. return this._view.getUint16(position);
  113. }
  114. uint32() {
  115. const position = this._position;
  116. this.skip(4);
  117. return this._view.getUint32(position);
  118. }
  119. uint64() {
  120. const position = this._position;
  121. this.skip(8);
  122. return this._view.getUint64(position);
  123. }
  124. int8() {
  125. const position = this._position;
  126. this.skip(1);
  127. return this._view.getInt8(position);
  128. }
  129. int16() {
  130. const position = this._position;
  131. this.skip(2);
  132. return this._view.getInt16(position);
  133. }
  134. int32() {
  135. const position = this._position;
  136. this.skip(4);
  137. return this._view.getInt32(position);
  138. }
  139. int64() {
  140. const position = this._position;
  141. this.skip(8);
  142. return this._view.getInt64(position);
  143. }
  144. float32() {
  145. const position = this._position;
  146. this.skip(4);
  147. return this._view.getFloat32(position);
  148. }
  149. float64() {
  150. const position = this._position;
  151. this.skip(8);
  152. return this._view.getFloat64(position);
  153. }
  154. string(size) {
  155. const buffer = this.bytes(size);
  156. this._decoder = this._decoder || new TextDecoder('utf8');
  157. return this._decoder.decode(buffer);
  158. }
  159. };
  160. msgpack.Error = class extends Error {
  161. constructor(message) {
  162. super(message);
  163. this.name = 'MessagePack Error';
  164. }
  165. };
  166. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  167. module.exports.BinaryReader = msgpack.BinaryReader;
  168. }