UbjsonEncoder.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.UbjsonEncoder = void 0;
  4. class UbjsonEncoder {
  5. constructor(writer) {
  6. this.writer = writer;
  7. }
  8. encode(value) {
  9. const writer = this.writer;
  10. writer.reset();
  11. this.writeAny(value);
  12. return writer.flush();
  13. }
  14. writeAny(value) {
  15. switch (typeof value) {
  16. case 'boolean':
  17. return this.writeBoolean(value);
  18. case 'number':
  19. return this.writeNumber(value);
  20. case 'string':
  21. return this.writeStr(value);
  22. case 'object': {
  23. if (value === null)
  24. return this.writeNull();
  25. const constructor = value.constructor;
  26. switch (constructor) {
  27. case Array:
  28. return this.writeArr(value);
  29. case Uint8Array:
  30. return this.writeBin(value);
  31. default:
  32. return this.writeObj(value);
  33. }
  34. }
  35. case 'bigint':
  36. return this.writeBigInt(value);
  37. case 'undefined':
  38. return this.writeUndef();
  39. default:
  40. return this.writeNull();
  41. }
  42. }
  43. writeNull() {
  44. this.writer.u8(0x5a);
  45. }
  46. writeUndef() {
  47. this.writer.u8(0x4e);
  48. }
  49. writeBoolean(bool) {
  50. this.writer.u8(bool ? 0x54 : 0x46);
  51. }
  52. writeNumber(num) {
  53. if (num >> 0 === num)
  54. return this.writeInteger(num);
  55. this.writeFloat(num);
  56. }
  57. writeInteger(int) {
  58. const writer = this.writer;
  59. if (int <= 0xff && 0 <= int)
  60. writer.u16(0x5500 | int);
  61. else if (int <= 127 && -128 <= int) {
  62. writer.u16(0x6900);
  63. writer.view.setInt8(writer.x - 1, int);
  64. }
  65. else if (int <= 32767 && -32768 <= int) {
  66. writer.ensureCapacity(3);
  67. writer.u8(0x49);
  68. writer.view.setInt16(writer.x, int, false);
  69. writer.x += 2;
  70. }
  71. else if (int <= 2147483647 && -2147483648 <= int) {
  72. writer.ensureCapacity(5);
  73. writer.u8(0x6c);
  74. writer.view.setInt32(writer.x, int, false);
  75. writer.x += 4;
  76. }
  77. }
  78. writeUInteger(uint) {
  79. const writer = this.writer;
  80. if (uint < 0xff)
  81. writer.u16(0x5500 + uint);
  82. }
  83. writeFloat(float) {
  84. const writer = this.writer;
  85. writer.ensureCapacity(9);
  86. const view = writer.view;
  87. const x = writer.x;
  88. view.setUint8(x, 0x44);
  89. view.setFloat64(x + 1, float, false);
  90. writer.x = x + 9;
  91. }
  92. writeBigInt(int) {
  93. const writer = this.writer;
  94. writer.ensureCapacity(9);
  95. const view = writer.view;
  96. const x = writer.x;
  97. view.setUint8(x, 0x4c);
  98. view.setBigInt64(x + 1, int, false);
  99. writer.x = x + 9;
  100. }
  101. writeBin(buf) {
  102. const writer = this.writer;
  103. const length = buf.length;
  104. writer.u32(1529107747);
  105. this.writeInteger(length);
  106. writer.buf(buf, length);
  107. }
  108. writeStr(str) {
  109. const length = str.length;
  110. const maxLength = length * 4;
  111. const capacity = maxLength + 1 + 5;
  112. const writer = this.writer;
  113. writer.ensureCapacity(capacity);
  114. const uint8 = writer.uint8;
  115. uint8[writer.x++] = 0x53;
  116. const x = writer.x;
  117. const oneByteLength = maxLength < 0xff;
  118. if (oneByteLength) {
  119. uint8[writer.x++] = 0x55;
  120. writer.x++;
  121. }
  122. else {
  123. uint8[writer.x++] = 0x6c;
  124. writer.x += 4;
  125. }
  126. const size = writer.utf8(str);
  127. if (oneByteLength)
  128. uint8[x + 1] = size;
  129. else
  130. writer.view.setUint32(x + 1, size);
  131. }
  132. writeAsciiStr(str) {
  133. this.writeStr(str);
  134. }
  135. writeArr(arr) {
  136. const writer = this.writer;
  137. writer.u8(0x5b);
  138. const length = arr.length;
  139. for (let i = 0; i < length; i++)
  140. this.writeAny(arr[i]);
  141. writer.u8(0x5d);
  142. }
  143. writeObj(obj) {
  144. const writer = this.writer;
  145. const keys = Object.keys(obj);
  146. const length = keys.length;
  147. writer.u8(0x7b);
  148. for (let i = 0; i < length; i++) {
  149. const key = keys[i];
  150. const value = obj[key];
  151. this.writeKey(key);
  152. this.writeAny(value);
  153. }
  154. writer.u8(0x7d);
  155. }
  156. writeKey(str) {
  157. const length = str.length;
  158. const maxLength = length * 4;
  159. const capacity = maxLength + 5;
  160. const writer = this.writer;
  161. writer.ensureCapacity(capacity);
  162. const uint8 = writer.uint8;
  163. const x = writer.x;
  164. const oneByteLength = maxLength < 0xff;
  165. if (oneByteLength) {
  166. uint8[writer.x++] = 0x55;
  167. writer.x++;
  168. }
  169. else {
  170. uint8[writer.x++] = 0x6c;
  171. writer.x += 4;
  172. }
  173. const size = writer.utf8(str);
  174. if (oneByteLength)
  175. uint8[x + 1] = size;
  176. else
  177. writer.view.setUint32(x + 1, size);
  178. }
  179. writeStartStr() {
  180. throw new Error('Method not implemented.');
  181. }
  182. writeStrChunk(str) {
  183. throw new Error('Method not implemented.');
  184. }
  185. writeEndStr() {
  186. throw new Error('Method not implemented.');
  187. }
  188. writeStartBin() {
  189. throw new Error('Method not implemented.');
  190. }
  191. writeBinChunk(buf) {
  192. throw new Error('Method not implemented.');
  193. }
  194. writeEndBin() {
  195. throw new Error('Method not implemented.');
  196. }
  197. writeStartArr() {
  198. this.writer.u8(0x5b);
  199. }
  200. writeArrChunk(item) {
  201. this.writeAny(item);
  202. }
  203. writeEndArr() {
  204. this.writer.u8(0x5d);
  205. }
  206. writeStartObj() {
  207. this.writer.u8(0x7b);
  208. }
  209. writeObjChunk(key, value) {
  210. this.writeKey(key);
  211. this.writeAny(value);
  212. }
  213. writeEndObj() {
  214. this.writer.u8(0x7d);
  215. }
  216. }
  217. exports.UbjsonEncoder = UbjsonEncoder;
  218. //# sourceMappingURL=UbjsonEncoder.js.map