AvroEncoder.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AvroEncoder = void 0;
  4. class AvroEncoder {
  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. writeUnknown(value) {
  15. this.writeNull();
  16. }
  17. writeAny(value) {
  18. switch (typeof value) {
  19. case 'boolean':
  20. return this.writeBoolean(value);
  21. case 'number':
  22. return this.writeNumber(value);
  23. case 'string':
  24. return this.writeStr(value);
  25. case 'object': {
  26. if (value === null)
  27. return this.writeNull();
  28. const constructor = value.constructor;
  29. switch (constructor) {
  30. case Object:
  31. return this.writeObj(value);
  32. case Array:
  33. return this.writeArr(value);
  34. case Uint8Array:
  35. return this.writeBin(value);
  36. default:
  37. return this.writeUnknown(value);
  38. }
  39. }
  40. case 'bigint':
  41. return this.writeLong(value);
  42. case 'undefined':
  43. return this.writeNull();
  44. default:
  45. return this.writeUnknown(value);
  46. }
  47. }
  48. writeNull() {
  49. }
  50. writeBoolean(bool) {
  51. this.writer.u8(bool ? 1 : 0);
  52. }
  53. writeInt(int) {
  54. this.writeVarIntSigned(this.encodeZigZag32(Math.trunc(int)));
  55. }
  56. writeLong(long) {
  57. if (typeof long === 'bigint') {
  58. this.writeVarLong(this.encodeZigZag64(long));
  59. }
  60. else {
  61. this.writeVarLong(this.encodeZigZag64(BigInt(Math.trunc(long))));
  62. }
  63. }
  64. writeFloatAvro(float) {
  65. const writer = this.writer;
  66. writer.ensureCapacity(4);
  67. writer.view.setFloat32(writer.x, float, true);
  68. writer.move(4);
  69. }
  70. writeDouble(double) {
  71. const writer = this.writer;
  72. writer.ensureCapacity(8);
  73. writer.view.setFloat64(writer.x, double, true);
  74. writer.move(8);
  75. }
  76. writeBin(bytes) {
  77. this.writeVarIntUnsigned(bytes.length);
  78. this.writer.buf(bytes, bytes.length);
  79. }
  80. writeStr(str) {
  81. const writer = this.writer;
  82. const maxSize = str.length * 4;
  83. writer.ensureCapacity(5 + maxSize);
  84. const lengthOffset = writer.x;
  85. writer.x += 5;
  86. const bytesWritten = writer.utf8(str);
  87. const endPos = writer.x;
  88. writer.x = lengthOffset;
  89. this.writeVarIntUnsigned(bytesWritten);
  90. const actualLengthSize = writer.x - lengthOffset;
  91. if (actualLengthSize < 5) {
  92. const stringStart = lengthOffset + 5;
  93. const stringData = writer.uint8.slice(stringStart, endPos);
  94. writer.x = lengthOffset + actualLengthSize;
  95. writer.buf(stringData, stringData.length);
  96. }
  97. else {
  98. writer.x = endPos;
  99. }
  100. }
  101. writeArr(arr) {
  102. this.writeVarIntUnsigned(arr.length);
  103. const length = arr.length;
  104. for (let i = 0; i < length; i++) {
  105. this.writeAny(arr[i]);
  106. }
  107. this.writeVarIntUnsigned(0);
  108. }
  109. writeObj(obj) {
  110. const entries = Object.entries(obj);
  111. const length = entries.length;
  112. this.writeVarIntUnsigned(length);
  113. for (let i = 0; i < length; i++) {
  114. const entry = entries[i];
  115. this.writeStr(entry[0]);
  116. this.writeAny(entry[1]);
  117. }
  118. this.writeVarIntUnsigned(0);
  119. }
  120. writeNumber(num) {
  121. if (Number.isInteger(num)) {
  122. if (num >= -2147483648 && num <= 2147483647) {
  123. this.writeInt(num);
  124. }
  125. else {
  126. this.writeLong(num);
  127. }
  128. }
  129. else {
  130. this.writeDouble(num);
  131. }
  132. }
  133. writeInteger(int) {
  134. this.writeInt(int);
  135. }
  136. writeUInteger(uint) {
  137. this.writeInt(uint);
  138. }
  139. writeFloat(float) {
  140. this.writeFloatValue(float);
  141. }
  142. writeFloatValue(float) {
  143. const writer = this.writer;
  144. writer.ensureCapacity(4);
  145. writer.view.setFloat32(writer.x, float, true);
  146. writer.move(4);
  147. }
  148. writeAsciiStr(str) {
  149. const writer = this.writer;
  150. this.writeVarIntUnsigned(str.length);
  151. writer.ascii(str);
  152. }
  153. writeVarIntSigned(value) {
  154. const writer = this.writer;
  155. let n = value >>> 0;
  156. while (n >= 0x80) {
  157. writer.u8((n & 0x7f) | 0x80);
  158. n >>>= 7;
  159. }
  160. writer.u8(n & 0x7f);
  161. }
  162. writeVarIntUnsigned(value) {
  163. const writer = this.writer;
  164. let n = value >>> 0;
  165. while (n >= 0x80) {
  166. writer.u8((n & 0x7f) | 0x80);
  167. n >>>= 7;
  168. }
  169. writer.u8(n & 0x7f);
  170. }
  171. writeVarLong(value) {
  172. const writer = this.writer;
  173. let n = value;
  174. const mask = BigInt(0x7f);
  175. const shift = BigInt(7);
  176. while (n >= BigInt(0x80)) {
  177. writer.u8(Number((n & mask) | BigInt(0x80)));
  178. n >>= shift;
  179. }
  180. writer.u8(Number(n & mask));
  181. }
  182. encodeZigZag32(value) {
  183. return (value << 1) ^ (value >> 31);
  184. }
  185. encodeZigZag64(value) {
  186. return (value << BigInt(1)) ^ (value >> BigInt(63));
  187. }
  188. }
  189. exports.AvroEncoder = AvroEncoder;
  190. //# sourceMappingURL=AvroEncoder.js.map