AvroSchemaEncoder.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AvroSchemaEncoder = void 0;
  4. const AvroEncoder_1 = require("./AvroEncoder");
  5. const AvroSchemaValidator_1 = require("./AvroSchemaValidator");
  6. class AvroSchemaEncoder {
  7. constructor(writer) {
  8. this.writer = writer;
  9. this.namedSchemas = new Map();
  10. this.encoder = new AvroEncoder_1.AvroEncoder(writer);
  11. this.validator = new AvroSchemaValidator_1.AvroSchemaValidator();
  12. }
  13. encode(value, schema, selectedIndex) {
  14. this.writer.reset();
  15. this.namedSchemas.clear();
  16. if (!this.validator.validateSchema(schema)) {
  17. throw new Error('Invalid Avro schema');
  18. }
  19. if (!this.validator.validateValue(value, schema)) {
  20. throw new Error('Value does not conform to schema');
  21. }
  22. this.collectNamedSchemas(schema);
  23. if (Array.isArray(schema) && selectedIndex !== undefined) {
  24. this.writeUnion(value, schema, selectedIndex);
  25. }
  26. else {
  27. this.writeValue(value, schema);
  28. }
  29. return this.writer.flush();
  30. }
  31. writeNull(schema) {
  32. this.validateSchemaType(schema, 'null');
  33. this.encoder.writeNull();
  34. }
  35. writeBoolean(value, schema) {
  36. this.validateSchemaType(schema, 'boolean');
  37. this.encoder.writeBoolean(value);
  38. }
  39. writeInt(value, schema) {
  40. this.validateSchemaType(schema, 'int');
  41. if (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
  42. throw new Error('Value is not a valid 32-bit integer');
  43. }
  44. this.encoder.writeInt(value);
  45. }
  46. writeLong(value, schema) {
  47. this.validateSchemaType(schema, 'long');
  48. this.encoder.writeLong(value);
  49. }
  50. writeFloat(value, schema) {
  51. this.validateSchemaType(schema, 'float');
  52. this.encoder.writeFloat(value);
  53. }
  54. writeDouble(value, schema) {
  55. this.validateSchemaType(schema, 'double');
  56. this.encoder.writeDouble(value);
  57. }
  58. writeBytes(value, schema) {
  59. this.validateSchemaType(schema, 'bytes');
  60. this.encoder.writeBin(value);
  61. }
  62. writeString(value, schema) {
  63. this.validateSchemaType(schema, 'string');
  64. this.encoder.writeStr(value);
  65. }
  66. writeRecord(value, schema) {
  67. if (typeof schema === 'object' && schema.type !== 'record') {
  68. throw new Error('Schema is not a record schema');
  69. }
  70. const recordSchema = this.resolveSchema(schema);
  71. if (recordSchema.type !== 'record') {
  72. throw new Error('Schema is not a record schema');
  73. }
  74. for (let i = 0; i < recordSchema.fields.length; i++) {
  75. const field = recordSchema.fields[i];
  76. const fieldValue = value[field.name];
  77. if (fieldValue !== undefined) {
  78. this.writeValue(fieldValue, field.type);
  79. }
  80. else if (field.default !== undefined) {
  81. this.writeValue(field.default, field.type);
  82. }
  83. else {
  84. throw new Error(`Missing required field: ${field.name}`);
  85. }
  86. }
  87. }
  88. writeEnum(value, schema) {
  89. if (typeof schema === 'object' && schema.type !== 'enum') {
  90. throw new Error('Schema is not an enum schema');
  91. }
  92. const enumSchema = this.resolveSchema(schema);
  93. if (enumSchema.type !== 'enum') {
  94. throw new Error('Schema is not an enum schema');
  95. }
  96. const index = enumSchema.symbols.indexOf(value);
  97. if (index === -1) {
  98. throw new Error(`Invalid enum value: ${value}`);
  99. }
  100. this.writeVarIntSigned(this.encodeZigZag32(index));
  101. }
  102. writeArray(value, schema) {
  103. if (typeof schema === 'object' && schema.type !== 'array') {
  104. throw new Error('Schema is not an array schema');
  105. }
  106. const arraySchema = this.resolveSchema(schema);
  107. if (arraySchema.type !== 'array') {
  108. throw new Error('Schema is not an array schema');
  109. }
  110. this.writeVarIntUnsigned(value.length);
  111. const length = value.length;
  112. for (let i = 0; i < length; i++) {
  113. this.writeValue(value[i], arraySchema.items);
  114. }
  115. this.writeVarIntUnsigned(0);
  116. }
  117. writeMap(value, schema) {
  118. if (typeof schema === 'object' && schema.type !== 'map') {
  119. throw new Error('Schema is not a map schema');
  120. }
  121. const mapSchema = this.resolveSchema(schema);
  122. if (mapSchema.type !== 'map') {
  123. throw new Error('Schema is not a map schema');
  124. }
  125. const entries = Object.entries(value);
  126. this.writeVarIntUnsigned(entries.length);
  127. const length = entries.length;
  128. for (let i = 0; i < length; i++) {
  129. const entry = entries[i];
  130. this.encoder.writeStr(entry[0]);
  131. this.writeValue(entry[1], mapSchema.values);
  132. }
  133. this.writeVarIntUnsigned(0);
  134. }
  135. writeUnion(value, schema, selectedIndex) {
  136. if (!Array.isArray(schema)) {
  137. throw new Error('Schema is not a union schema');
  138. }
  139. let index = selectedIndex;
  140. if (index === undefined) {
  141. index = schema.findIndex((subSchema) => this.validator.validateValue(value, subSchema));
  142. if (index === -1) {
  143. throw new Error('Value does not match any schema in the union');
  144. }
  145. }
  146. if (index < 0 || index >= schema.length) {
  147. throw new Error('Invalid union index');
  148. }
  149. this.writeVarIntSigned(this.encodeZigZag32(index));
  150. this.writeValue(value, schema[index]);
  151. }
  152. writeFixed(value, schema) {
  153. if (typeof schema === 'object' && schema.type !== 'fixed') {
  154. throw new Error('Schema is not a fixed schema');
  155. }
  156. const fixedSchema = this.resolveSchema(schema);
  157. if (fixedSchema.type !== 'fixed') {
  158. throw new Error('Schema is not a fixed schema');
  159. }
  160. if (value.length !== fixedSchema.size) {
  161. throw new Error(`Fixed value length ${value.length} does not match schema size ${fixedSchema.size}`);
  162. }
  163. this.writer.buf(value, value.length);
  164. }
  165. writeNumber(value, schema) {
  166. const resolvedSchema = this.resolveSchema(schema);
  167. const schemaType = typeof resolvedSchema === 'string'
  168. ? resolvedSchema
  169. : Array.isArray(resolvedSchema)
  170. ? 'union'
  171. : resolvedSchema.type;
  172. switch (schemaType) {
  173. case 'int':
  174. this.writeInt(value, schema);
  175. break;
  176. case 'long':
  177. this.writeLong(value, schema);
  178. break;
  179. case 'float':
  180. this.writeFloat(value, schema);
  181. break;
  182. case 'double':
  183. this.writeDouble(value, schema);
  184. break;
  185. default:
  186. throw new Error(`Schema type ${schemaType} is not a numeric type`);
  187. }
  188. }
  189. writeValue(value, schema) {
  190. const resolvedSchema = this.resolveSchema(schema);
  191. if (typeof resolvedSchema === 'string') {
  192. switch (resolvedSchema) {
  193. case 'null':
  194. this.encoder.writeNull();
  195. break;
  196. case 'boolean':
  197. this.encoder.writeBoolean(value);
  198. break;
  199. case 'int':
  200. this.encoder.writeInt(value);
  201. break;
  202. case 'long':
  203. this.encoder.writeLong(value);
  204. break;
  205. case 'float':
  206. this.encoder.writeFloat(value);
  207. break;
  208. case 'double':
  209. this.encoder.writeDouble(value);
  210. break;
  211. case 'bytes':
  212. this.encoder.writeBin(value);
  213. break;
  214. case 'string':
  215. this.encoder.writeStr(value);
  216. break;
  217. default:
  218. throw new Error(`Unknown primitive type: ${resolvedSchema}`);
  219. }
  220. return;
  221. }
  222. if (Array.isArray(resolvedSchema)) {
  223. this.writeUnion(value, resolvedSchema);
  224. return;
  225. }
  226. switch (resolvedSchema.type) {
  227. case 'record':
  228. this.writeRecord(value, resolvedSchema);
  229. break;
  230. case 'enum':
  231. this.writeEnum(value, resolvedSchema);
  232. break;
  233. case 'array':
  234. this.writeArray(value, resolvedSchema);
  235. break;
  236. case 'map':
  237. this.writeMap(value, resolvedSchema);
  238. break;
  239. case 'fixed':
  240. this.writeFixed(value, resolvedSchema);
  241. break;
  242. default:
  243. throw new Error(`Unknown schema type: ${resolvedSchema.type}`);
  244. }
  245. }
  246. validateSchemaType(schema, expectedType) {
  247. const resolvedSchema = this.resolveSchema(schema);
  248. const actualType = typeof resolvedSchema === 'string'
  249. ? resolvedSchema
  250. : Array.isArray(resolvedSchema)
  251. ? 'union'
  252. : resolvedSchema.type;
  253. if (actualType !== expectedType) {
  254. throw new Error(`Expected schema type ${expectedType}, got ${actualType}`);
  255. }
  256. }
  257. resolveSchema(schema) {
  258. if (typeof schema === 'string') {
  259. const namedSchema = this.namedSchemas.get(schema);
  260. return namedSchema || schema;
  261. }
  262. return schema;
  263. }
  264. collectNamedSchemas(schema) {
  265. if (typeof schema === 'string' || Array.isArray(schema)) {
  266. return;
  267. }
  268. if (typeof schema === 'object' && schema !== null) {
  269. switch (schema.type) {
  270. case 'record':
  271. const recordSchema = schema;
  272. const recordFullName = this.getFullName(recordSchema.name, recordSchema.namespace);
  273. this.namedSchemas.set(recordFullName, recordSchema);
  274. recordSchema.fields.forEach((field) => this.collectNamedSchemas(field.type));
  275. break;
  276. case 'enum':
  277. const enumSchema = schema;
  278. const enumFullName = this.getFullName(enumSchema.name, enumSchema.namespace);
  279. this.namedSchemas.set(enumFullName, enumSchema);
  280. break;
  281. case 'fixed':
  282. const fixedSchema = schema;
  283. const fixedFullName = this.getFullName(fixedSchema.name, fixedSchema.namespace);
  284. this.namedSchemas.set(fixedFullName, fixedSchema);
  285. break;
  286. case 'array':
  287. this.collectNamedSchemas(schema.items);
  288. break;
  289. case 'map':
  290. this.collectNamedSchemas(schema.values);
  291. break;
  292. }
  293. }
  294. }
  295. getFullName(name, namespace) {
  296. return namespace ? `${namespace}.${name}` : name;
  297. }
  298. writeVarIntUnsigned(value) {
  299. const writer = this.writer;
  300. let n = value >>> 0;
  301. while (n >= 0x80) {
  302. writer.u8((n & 0x7f) | 0x80);
  303. n >>>= 7;
  304. }
  305. writer.u8(n & 0x7f);
  306. }
  307. writeVarIntSigned(value) {
  308. const writer = this.writer;
  309. let n = value >>> 0;
  310. while (n >= 0x80) {
  311. writer.u8((n & 0x7f) | 0x80);
  312. n >>>= 7;
  313. }
  314. writer.u8(n & 0x7f);
  315. }
  316. encodeZigZag32(value) {
  317. return (value << 1) ^ (value >> 31);
  318. }
  319. }
  320. exports.AvroSchemaEncoder = AvroSchemaEncoder;
  321. //# sourceMappingURL=AvroSchemaEncoder.js.map