MsgPackToJsonConverter.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MsgPackToJsonConverter = void 0;
  4. const asString_1 = require("@jsonjoy.com/util/lib/strings/asString");
  5. const toDataUri_1 = require("../util/buffers/toDataUri");
  6. class MsgPackToJsonConverter {
  7. constructor() {
  8. this.uint8 = new Uint8Array([]);
  9. this.view = new DataView(this.uint8.buffer);
  10. this.x = 0;
  11. }
  12. reset(uint8) {
  13. this.x = 0;
  14. this.uint8 = uint8;
  15. this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length);
  16. }
  17. convert(uint8) {
  18. this.reset(uint8);
  19. return this.val();
  20. }
  21. val() {
  22. const byte = this.u8();
  23. if (byte >= 0xe0)
  24. return (byte - 0x100).toString();
  25. if (byte <= 0xbf) {
  26. if (byte < 0x90) {
  27. if (byte <= 0b1111111)
  28. return byte.toString();
  29. return this.obj(byte & 0b1111);
  30. }
  31. else {
  32. if (byte < 0xa0)
  33. return this.arr(byte & 0b1111);
  34. else
  35. return this.str(byte & 0b11111);
  36. }
  37. }
  38. if (byte <= 0xd0) {
  39. if (byte <= 0xc8) {
  40. if (byte <= 0xc4) {
  41. if (byte <= 0xc2)
  42. return byte === 0xc2 ? 'false' : 'null';
  43. else
  44. return byte === 0xc4 ? this.bin(this.u8()) : 'true';
  45. }
  46. else {
  47. if (byte <= 0xc6)
  48. return byte === 0xc6 ? this.bin(this.u32()) : this.bin(this.u16());
  49. else
  50. return byte === 0xc8 ? this.ext(this.u16()) : this.ext(this.u8());
  51. }
  52. }
  53. else {
  54. return byte <= 0xcc
  55. ? byte <= 0xca
  56. ? byte === 0xca
  57. ? this.f32().toString()
  58. : this.ext(this.u32())
  59. : byte === 0xcc
  60. ? this.u8().toString()
  61. : this.f64().toString()
  62. : byte <= 0xce
  63. ? byte === 0xce
  64. ? this.u32().toString()
  65. : this.u16().toString()
  66. : byte === 0xd0
  67. ? this.i8().toString()
  68. : (this.u32() * 4294967296 + this.u32()).toString();
  69. }
  70. }
  71. else if (byte <= 0xd8) {
  72. return byte <= 0xd4
  73. ? byte <= 0xd2
  74. ? byte === 0xd2
  75. ? this.i32().toString()
  76. : this.i16().toString()
  77. : byte === 0xd4
  78. ? this.ext(1)
  79. : (this.i32() * 4294967296 + this.i32()).toString()
  80. : byte <= 0xd6
  81. ? byte === 0xd6
  82. ? this.ext(4)
  83. : this.ext(2)
  84. : byte === 0xd8
  85. ? this.ext(16)
  86. : this.ext(8);
  87. }
  88. else {
  89. switch (byte) {
  90. case 0xd9:
  91. return this.str(this.u8());
  92. case 0xda:
  93. return this.str(this.u16());
  94. case 0xdb:
  95. return this.str(this.u32());
  96. case 0xdc:
  97. return this.arr(this.u16());
  98. case 0xdd:
  99. return this.arr(this.u32());
  100. case 0xde:
  101. return this.obj(this.u16());
  102. case 0xdf:
  103. return this.obj(this.u32());
  104. }
  105. }
  106. return '';
  107. }
  108. str(size) {
  109. const uint8 = this.uint8;
  110. const end = this.x + size;
  111. let x = this.x;
  112. let str = '';
  113. while (x < end) {
  114. const b1 = uint8[x++];
  115. if ((b1 & 0x80) === 0) {
  116. str += String.fromCharCode(b1);
  117. continue;
  118. }
  119. else if ((b1 & 0xe0) === 0xc0) {
  120. str += String.fromCharCode(((b1 & 0x1f) << 6) | (uint8[x++] & 0x3f));
  121. }
  122. else if ((b1 & 0xf0) === 0xe0) {
  123. str += String.fromCharCode(((b1 & 0x1f) << 12) | ((uint8[x++] & 0x3f) << 6) | (uint8[x++] & 0x3f));
  124. }
  125. else if ((b1 & 0xf8) === 0xf0) {
  126. const b2 = uint8[x++] & 0x3f;
  127. const b3 = uint8[x++] & 0x3f;
  128. const b4 = uint8[x++] & 0x3f;
  129. let code = ((b1 & 0x07) << 0x12) | (b2 << 0x0c) | (b3 << 0x06) | b4;
  130. if (code > 0xffff) {
  131. code -= 0x10000;
  132. str += String.fromCharCode(((code >>> 10) & 0x3ff) | 0xd800);
  133. code = 0xdc00 | (code & 0x3ff);
  134. }
  135. str += String.fromCharCode(code);
  136. }
  137. else {
  138. str += String.fromCharCode(b1);
  139. }
  140. }
  141. this.x = end;
  142. return (0, asString_1.asString)(str);
  143. }
  144. obj(size) {
  145. let str = '{';
  146. for (let i = 0; i < size; i++) {
  147. if (i > 0)
  148. str += ',';
  149. str += this.key();
  150. str += ':';
  151. str += this.val();
  152. }
  153. return (str + '}');
  154. }
  155. key() {
  156. return this.val();
  157. }
  158. arr(size) {
  159. let str = '[';
  160. for (let i = 0; i < size; i++) {
  161. if (i > 0)
  162. str += ',';
  163. str += this.val();
  164. }
  165. return (str + ']');
  166. }
  167. bin(size) {
  168. const end = this.x + size;
  169. const buf = this.uint8.subarray(this.x, end);
  170. this.x = end;
  171. return '"' + (0, toDataUri_1.toDataUri)(buf) + '"';
  172. }
  173. ext(size) {
  174. const ext = this.u8();
  175. const end = this.x + size;
  176. const buf = this.uint8.subarray(this.x, end);
  177. this.x = end;
  178. return '"' + (0, toDataUri_1.toDataUri)(buf, { ext }) + '"';
  179. }
  180. u8() {
  181. return this.view.getUint8(this.x++);
  182. }
  183. u16() {
  184. const num = this.view.getUint16(this.x);
  185. this.x += 2;
  186. return num;
  187. }
  188. u32() {
  189. const num = this.view.getUint32(this.x);
  190. this.x += 4;
  191. return num;
  192. }
  193. i8() {
  194. return this.view.getInt8(this.x++);
  195. }
  196. i16() {
  197. const num = this.view.getInt16(this.x);
  198. this.x += 2;
  199. return num;
  200. }
  201. i32() {
  202. const num = this.view.getInt32(this.x);
  203. this.x += 4;
  204. return num;
  205. }
  206. f32() {
  207. const pos = this.x;
  208. this.x += 4;
  209. return this.view.getFloat32(pos);
  210. }
  211. f64() {
  212. const pos = this.x;
  213. this.x += 8;
  214. return this.view.getFloat64(pos);
  215. }
  216. }
  217. exports.MsgPackToJsonConverter = MsgPackToJsonConverter;
  218. //# sourceMappingURL=MsgPackToJsonConverter.js.map