MsgPackEncoderFast.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MsgPackEncoderFast = void 0;
  4. const Writer_1 = require("@jsonjoy.com/util/lib/buffers/Writer");
  5. class MsgPackEncoderFast {
  6. constructor(writer = new Writer_1.Writer()) {
  7. this.writer = writer;
  8. }
  9. encode(json) {
  10. this.writer.reset();
  11. this.writeAny(json);
  12. return this.writer.flush();
  13. }
  14. encodeAny(json) {
  15. this.writeAny(json);
  16. }
  17. writeAny(value) {
  18. switch (value) {
  19. case null:
  20. return this.writer.u8(0xc0);
  21. case false:
  22. return this.writer.u8(0xc2);
  23. case true:
  24. return this.writer.u8(0xc3);
  25. }
  26. if (value instanceof Array)
  27. return this.writeArr(value);
  28. switch (typeof value) {
  29. case 'number':
  30. return this.writeNumber(value);
  31. case 'string':
  32. return this.writeStr(value);
  33. case 'object':
  34. return this.writeObj(value);
  35. }
  36. }
  37. encodeFloat64(num) {
  38. this.writeFloat(num);
  39. }
  40. writeNull() {
  41. return this.writer.u8(0xc0);
  42. }
  43. writeFloat(float) {
  44. this.writer.u8f64(0xcb, float);
  45. }
  46. u32(num) {
  47. const writer = this.writer;
  48. this.writer.ensureCapacity(5);
  49. const uint8 = writer.uint8;
  50. if (num <= 0b1111111) {
  51. uint8[writer.x++] = num;
  52. }
  53. else if (num <= 0xffff) {
  54. uint8[writer.x++] = 0xcd;
  55. writer.view.setUint16(writer.x, num);
  56. writer.x += 2;
  57. }
  58. else if (num <= 0xffffffff) {
  59. uint8[writer.x++] = 0xce;
  60. writer.view.setUint32(writer.x, num);
  61. writer.x += 4;
  62. }
  63. else
  64. this.writeFloat(num);
  65. }
  66. n32(num) {
  67. const writer = this.writer;
  68. this.writer.ensureCapacity(5);
  69. const uint8 = writer.uint8;
  70. if (num >= -0x20) {
  71. uint8[writer.x++] = 0x100 + num;
  72. }
  73. else if (num >= -0x8000) {
  74. uint8[writer.x++] = 0xd1;
  75. writer.view.setInt16(writer.x, num);
  76. writer.x += 2;
  77. }
  78. else if (num >= -0x80000000) {
  79. uint8[writer.x++] = 0xd2;
  80. writer.view.setInt32(writer.x, num);
  81. writer.x += 4;
  82. }
  83. else
  84. this.writeFloat(num);
  85. }
  86. encodeNumber(num) {
  87. this.writeNumber(num);
  88. }
  89. writeNumber(num) {
  90. if (num >>> 0 === num)
  91. return this.u32(num);
  92. if (num >> 0 === num)
  93. return this.n32(num);
  94. this.writeFloat(num);
  95. }
  96. writeInteger(int) {
  97. if (int >= 0)
  98. if (int <= 0xffffffff)
  99. return this.u32(int);
  100. else if (int > -0x80000000)
  101. return this.n32(int);
  102. this.writeFloat(int);
  103. }
  104. writeUInteger(uint) {
  105. if (uint <= 0xffffffff)
  106. return this.u32(uint);
  107. this.writeFloat(uint);
  108. }
  109. encodeNull() {
  110. this.writer.u8(0xc0);
  111. }
  112. encodeTrue() {
  113. this.writer.u8(0xc3);
  114. }
  115. encodeFalse() {
  116. this.writer.u8(0xc2);
  117. }
  118. encodeBoolean(bool) {
  119. this.writeBoolean(bool);
  120. }
  121. writeBoolean(bool) {
  122. if (bool)
  123. this.writer.u8(0xc3);
  124. else
  125. this.writer.u8(0xc2);
  126. }
  127. encodeStringHeader(length) {
  128. this.writeStrHdr(length);
  129. }
  130. writeStrHdr(length) {
  131. if (length <= 0b11111)
  132. this.writer.u8(0b10100000 | length);
  133. else if (length <= 0xff)
  134. this.writer.u16(0xd900 + length);
  135. else if (length <= 0xffff)
  136. this.writer.u8u16(0xda, length);
  137. else
  138. this.writer.u8u32(0xdb, length);
  139. }
  140. encodeString(str) {
  141. this.writeStr(str);
  142. }
  143. writeStr(str) {
  144. const writer = this.writer;
  145. const length = str.length;
  146. const maxSize = length * 4;
  147. writer.ensureCapacity(5 + maxSize);
  148. const uint8 = writer.uint8;
  149. let lengthOffset = writer.x;
  150. if (maxSize <= 0b11111)
  151. writer.x++;
  152. else if (maxSize <= 0xff) {
  153. uint8[writer.x++] = 0xd9;
  154. lengthOffset = writer.x;
  155. writer.x++;
  156. }
  157. else if (maxSize <= 0xffff) {
  158. uint8[writer.x++] = 0xda;
  159. lengthOffset = writer.x;
  160. writer.x += 2;
  161. }
  162. else {
  163. uint8[writer.x++] = 0xdb;
  164. lengthOffset = writer.x;
  165. writer.x += 4;
  166. }
  167. const bytesWritten = this.writer.utf8(str);
  168. if (maxSize <= 0b11111)
  169. uint8[lengthOffset] = 0b10100000 | bytesWritten;
  170. else if (maxSize <= 0xff)
  171. uint8[lengthOffset] = bytesWritten;
  172. else if (maxSize <= 0xffff)
  173. writer.view.setUint16(lengthOffset, bytesWritten);
  174. else
  175. writer.view.setUint32(lengthOffset, bytesWritten);
  176. }
  177. encodeAsciiString(str) {
  178. this.writeAsciiStr(str);
  179. }
  180. writeAsciiStr(str) {
  181. this.writeStrHdr(str.length);
  182. this.writer.ascii(str);
  183. }
  184. encodeArrayHeader(length) {
  185. this.writeArrHdr(length);
  186. }
  187. encodeArray(arr) {
  188. this.writeArr(arr);
  189. }
  190. writeArrHdr(length) {
  191. if (length <= 0b1111)
  192. this.writer.u8(0b10010000 | length);
  193. else if (length <= 0xffff)
  194. this.writer.u8u16(0xdc, length);
  195. else if (length <= 0xffffffff)
  196. this.writer.u8u32(0xdd, length);
  197. }
  198. writeArr(arr) {
  199. const length = arr.length;
  200. if (length <= 0b1111)
  201. this.writer.u8(0b10010000 | length);
  202. else if (length <= 0xffff)
  203. this.writer.u8u16(0xdc, length);
  204. else if (length <= 0xffffffff)
  205. this.writer.u8u32(0xdd, length);
  206. for (let i = 0; i < length; i++)
  207. this.writeAny(arr[i]);
  208. }
  209. encodeObjectHeader(length) {
  210. this.writeObjHdr(length);
  211. }
  212. encodeObject(obj) {
  213. this.writeObj(obj);
  214. }
  215. writeObjHdr(length) {
  216. if (length <= 0b1111)
  217. this.writer.u8(0b10000000 | length);
  218. else if (length <= 0xffff) {
  219. this.writer.u8u16(0xde, length);
  220. }
  221. else if (length <= 0xffffffff) {
  222. this.writer.u8u32(0xdf, length);
  223. }
  224. }
  225. writeObj(obj) {
  226. const keys = Object.keys(obj);
  227. const length = keys.length;
  228. this.writeObjHdr(length);
  229. for (let i = 0; i < length; i++) {
  230. const key = keys[i];
  231. this.writeStr(key);
  232. this.writeAny(obj[key]);
  233. }
  234. }
  235. encodeExtHeader(type, length) {
  236. switch (length) {
  237. case 1:
  238. this.writer.u16((0xd4 << 8) | type);
  239. break;
  240. case 2:
  241. this.writer.u16((0xd5 << 8) | type);
  242. break;
  243. case 4:
  244. this.writer.u16((0xd6 << 8) | type);
  245. break;
  246. case 8:
  247. this.writer.u16((0xd7 << 8) | type);
  248. break;
  249. case 16:
  250. this.writer.u16((0xd8 << 8) | type);
  251. break;
  252. default:
  253. if (length <= 0xff) {
  254. this.writer.u16((0xc7 << 8) | length);
  255. this.writer.u8(type);
  256. }
  257. else if (length <= 0xffff) {
  258. this.writer.u8u16(0xc8, length);
  259. this.writer.u8(type);
  260. }
  261. else if (length <= 0xffffffff) {
  262. this.writer.u8u32(0xc9, length);
  263. this.writer.u8(type);
  264. }
  265. }
  266. }
  267. encodeExt(ext) {
  268. const { tag: type, val: buf } = ext;
  269. const length = buf.length;
  270. this.encodeExtHeader(type, length);
  271. this.writer.buf(buf, length);
  272. }
  273. encodeBinaryHeader(length) {
  274. this.writeBinHdr(length);
  275. }
  276. encodeBinary(buf) {
  277. this.writeBin(buf);
  278. }
  279. writeBinHdr(length) {
  280. if (length <= 0xff)
  281. this.writer.u16((0xc4 << 8) | length);
  282. else if (length <= 0xffff) {
  283. this.writer.u8u16(0xc5, length);
  284. }
  285. else if (length <= 0xffffffff) {
  286. this.writer.u8u32(0xc6, length);
  287. }
  288. }
  289. writeBin(buf) {
  290. const length = buf.length;
  291. this.writeBinHdr(length);
  292. this.writer.buf(buf, length);
  293. }
  294. }
  295. exports.MsgPackEncoderFast = MsgPackEncoderFast;
  296. //# sourceMappingURL=MsgPackEncoderFast.js.map