AvroDecoder.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AvroDecoder = void 0;
  4. const Reader_1 = require("@jsonjoy.com/buffers/lib/Reader");
  5. class AvroDecoder {
  6. constructor() {
  7. this.reader = new Reader_1.Reader();
  8. }
  9. read(uint8) {
  10. this.reader.reset(uint8);
  11. return this.readAny();
  12. }
  13. decode(uint8) {
  14. this.reader.reset(uint8);
  15. return this.readAny();
  16. }
  17. readAny() {
  18. throw new Error('readAny() requires schema information. Use readNull, readBoolean, etc. directly.');
  19. }
  20. readNull() {
  21. return null;
  22. }
  23. readBoolean() {
  24. return this.reader.u8() === 1;
  25. }
  26. readInt() {
  27. const zigzag = this.readVarIntUnsigned();
  28. return this.decodeZigZag32(zigzag);
  29. }
  30. readLong() {
  31. const zigzag = this.readVarLong();
  32. const decoded = this.decodeZigZag64(zigzag);
  33. if (decoded >= BigInt(Number.MIN_SAFE_INTEGER) && decoded <= BigInt(Number.MAX_SAFE_INTEGER)) {
  34. return Number(decoded);
  35. }
  36. return decoded;
  37. }
  38. readFloat() {
  39. const reader = this.reader;
  40. const value = reader.view.getFloat32(reader.x, true);
  41. reader.x += 4;
  42. return value;
  43. }
  44. readDouble() {
  45. const reader = this.reader;
  46. const value = reader.view.getFloat64(reader.x, true);
  47. reader.x += 8;
  48. return value;
  49. }
  50. readBytes() {
  51. const length = this.readVarIntUnsigned();
  52. return this.reader.buf(length);
  53. }
  54. readString() {
  55. const length = this.readVarIntUnsigned();
  56. const bytes = this.reader.buf(length);
  57. return new TextDecoder().decode(bytes);
  58. }
  59. readArray(itemReader) {
  60. const result = [];
  61. while (true) {
  62. const count = this.readVarIntUnsigned();
  63. if (count === 0)
  64. break;
  65. for (let i = 0; i < count; i++) {
  66. result.push(itemReader());
  67. }
  68. }
  69. return result;
  70. }
  71. readMap(valueReader) {
  72. const result = {};
  73. while (true) {
  74. const count = this.readVarIntUnsigned();
  75. if (count === 0)
  76. break;
  77. for (let i = 0; i < count; i++) {
  78. const key = this.readString();
  79. if (key === '__proto__')
  80. throw new Error('INVALID_KEY');
  81. result[key] = valueReader();
  82. }
  83. }
  84. return result;
  85. }
  86. readUnion(schemaReaders) {
  87. const index = this.decodeZigZag32(this.readVarIntUnsigned());
  88. if (index < 0 || index >= schemaReaders.length) {
  89. throw new Error(`Invalid union index: ${index}`);
  90. }
  91. const value = schemaReaders[index]();
  92. return { index, value };
  93. }
  94. readEnum() {
  95. return this.decodeZigZag32(this.readVarIntUnsigned());
  96. }
  97. readFixed(size) {
  98. return this.reader.buf(size);
  99. }
  100. readRecord(fieldReaders) {
  101. const result = {};
  102. for (let i = 0; i < fieldReaders.length; i++) {
  103. const fieldValue = fieldReaders[i]();
  104. result[`field${i}`] = fieldValue;
  105. }
  106. return result;
  107. }
  108. readVarIntUnsigned() {
  109. const reader = this.reader;
  110. let result = 0;
  111. let shift = 0;
  112. while (true) {
  113. const byte = reader.u8();
  114. result |= (byte & 0x7f) << shift;
  115. if ((byte & 0x80) === 0)
  116. break;
  117. shift += 7;
  118. if (shift >= 32) {
  119. throw new Error('Variable-length integer is too long');
  120. }
  121. }
  122. return result >>> 0;
  123. }
  124. readVarLong() {
  125. const reader = this.reader;
  126. let result = BigInt(0);
  127. let shift = BigInt(0);
  128. while (true) {
  129. const byte = BigInt(reader.u8());
  130. result |= (byte & BigInt(0x7f)) << shift;
  131. if ((byte & BigInt(0x80)) === BigInt(0))
  132. break;
  133. shift += BigInt(7);
  134. if (shift >= BigInt(64)) {
  135. throw new Error('Variable-length long is too long');
  136. }
  137. }
  138. return result;
  139. }
  140. decodeZigZag32(value) {
  141. return (value >>> 1) ^ -(value & 1);
  142. }
  143. decodeZigZag64(value) {
  144. return (value >> BigInt(1)) ^ -(value & BigInt(1));
  145. }
  146. }
  147. exports.AvroDecoder = AvroDecoder;
  148. //# sourceMappingURL=AvroDecoder.js.map