All files / json-pack/src/avro AvroSchemaDecoder.ts

81.48% Statements 88/108
78.72% Branches 37/47
73.07% Functions 19/26
80.39% Lines 82/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 2871x 1x 1x                                 1x     28x   28x 28x 28x 28x             27x 27x     27x 1x     26x 26x             79x   79x 51x   3x   3x   17x   4x   2x   2x   2x   18x           28x 4x     24x   9x   3x   7x   4x   1x                   9x   9x 26x 26x 26x           9x             3x   3x 1x     2x             17x             6x             9x 4x 4x             1x                             2x 1x                                                                                                               2x   2x           2x 1x         81x 53x 53x   28x       59x 39x     20x 20x   7x 7x 7x 24x 7x     3x 3x 3x 3x     1x 1x 1x 1x     5x 5x   4x 4x           11x      
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {AvroDecoder} from './AvroDecoder';
import {AvroSchemaValidator} from './AvroSchemaValidator';
import type {
  AvroSchema,
  AvroRecordSchema,
  AvroEnumSchema,
  AvroArraySchema,
  AvroMapSchema,
  AvroUnionSchema,
  AvroFixedSchema,
  AvroNamedSchema,
} from './types';
 
/**
 * Apache Avro binary decoder with schema validation and decoding.
 * Decodes values according to provided Avro schemas with proper validation.
 * Based on https://avro.apache.org/docs/1.12.0/specification/
 */
export class AvroSchemaDecoder {
  private decoder: AvroDecoder;
  private validator: AvroSchemaValidator;
  private namedSchemas = new Map<string, AvroNamedSchema>();
 
  constructor(public readonly reader: Reader = new Reader()) {
    this.decoder = new AvroDecoder();
    this.decoder.reader = reader;
    this.validator = new AvroSchemaValidator();
  }
 
  /**
   * Decodes a value according to the provided schema.
   */
  public decode(data: Uint8Array, schema: AvroSchema): unknown {
    this.reader.reset(data);
    this.namedSchemas.clear();
 
    // Validate schema first
    if (!this.validator.validateSchema(schema)) {
      throw new Error('Invalid Avro schema');
    }
 
    this.collectNamedSchemas(schema);
    return this.readValue(schema);
  }
 
  /**
   * Reads a value according to its schema.
   */
  private readValue(schema: AvroSchema): unknown {
    const resolvedSchema = this.resolveSchema(schema);
 
    if (typeof resolvedSchema === 'string') {
      switch (resolvedSchema) {
        case 'null':
          return this.decoder.readNull();
        case 'boolean':
          return this.decoder.readBoolean();
        case 'int':
          return this.decoder.readInt();
        case 'long':
          return this.decoder.readLong();
        case 'float':
          return this.decoder.readFloat();
        case 'double':
          return this.decoder.readDouble();
        case 'bytes':
          return this.decoder.readBytes();
        case 'string':
          return this.decoder.readString();
        default:
          throw new Error(`Unknown primitive type: ${resolvedSchema}`);
      }
    }
 
    if (Array.isArray(resolvedSchema)) {
      return this.readUnion(resolvedSchema);
    }
 
    switch (resolvedSchema.type) {
      case 'record':
        return this.readRecord(resolvedSchema);
      case 'enum':
        return this.readEnum(resolvedSchema);
      case 'array':
        return this.readArray(resolvedSchema);
      case 'map':
        return this.readMap(resolvedSchema);
      case 'fixed':
        return this.readFixed(resolvedSchema);
      default:
        throw new Error(`Unknown schema type: ${(resolvedSchema as any).type}`);
    }
  }
 
  /**
   * Reads a record value according to the record schema.
   */
  private readRecord(schema: AvroRecordSchema): Record<string, unknown> {
    const result: Record<string, unknown> = {};
 
    for (let i = 0; i < schema.fields.length; i++) {
      const field = schema.fields[i];
      try {
        result[field.name] = this.readValue(field.type);
      } catch (error) {
        throw new Error(`Error reading field '${field.name}': ${(error as Error).message}`);
      }
    }
 
    return result;
  }
 
  /**
   * Reads an enum value according to the enum schema.
   */
  private readEnum(schema: AvroEnumSchema): string {
    const index = this.decoder.readEnum();
 
    if (index < 0 || index >= schema.symbols.length) {
      throw new Error(`Invalid enum index ${index} for enum with ${schema.symbols.length} symbols`);
    }
 
    return schema.symbols[index];
  }
 
  /**
   * Reads an array value according to the array schema.
   */
  private readArray(schema: AvroArraySchema): unknown[] {
    return this.decoder.readArray(() => this.readValue(schema.items));
  }
 
  /**
   * Reads a map value according to the map schema.
   */
  private readMap(schema: AvroMapSchema): Record<string, unknown> {
    return this.decoder.readMap(() => this.readValue(schema.values));
  }
 
  /**
   * Reads a union value according to the union schema.
   */
  private readUnion(schema: AvroUnionSchema): unknown {
    const schemaReaders = schema.map((subSchema) => () => this.readValue(subSchema));
    const result = this.decoder.readUnion(schemaReaders);
    return result.value;
  }
 
  /**
   * Reads a fixed value according to the fixed schema.
   */
  private readFixed(schema: AvroFixedSchema): Uint8Array {
    return this.decoder.readFixed(schema.size);
  }
 
  /**
   * Reads a null value with schema validation.
   */
  public readNull(schema: AvroSchema): null {
    this.validateSchemaType(schema, 'null');
    return this.decoder.readNull();
  }
 
  /**
   * Reads a boolean value with schema validation.
   */
  public readBoolean(schema: AvroSchema): boolean {
    this.validateSchemaType(schema, 'boolean');
    return this.decoder.readBoolean();
  }
 
  /**
   * Reads an int value with schema validation.
   */
  public readInt(schema: AvroSchema): number {
    this.validateSchemaType(schema, 'int');
    const value = this.decoder.readInt();
    Iif (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
      throw new Error('Decoded value is not a valid 32-bit integer');
    }
    return value;
  }
 
  /**
   * Reads a long value with schema validation.
   */
  public readLong(schema: AvroSchema): number | bigint {
    this.validateSchemaType(schema, 'long');
    return this.decoder.readLong();
  }
 
  /**
   * Reads a float value with schema validation.
   */
  public readFloat(schema: AvroSchema): number {
    this.validateSchemaType(schema, 'float');
    return this.decoder.readFloat();
  }
 
  /**
   * Reads a double value with schema validation.
   */
  public readDouble(schema: AvroSchema): number {
    this.validateSchemaType(schema, 'double');
    return this.decoder.readDouble();
  }
 
  /**
   * Reads a bytes value with schema validation.
   */
  public readBytes(schema: AvroSchema): Uint8Array {
    this.validateSchemaType(schema, 'bytes');
    return this.decoder.readBytes();
  }
 
  /**
   * Reads a string value with schema validation.
   */
  public readString(schema: AvroSchema): string {
    this.validateSchemaType(schema, 'string');
    return this.decoder.readString();
  }
 
  private validateSchemaType(schema: AvroSchema, expectedType: string): void {
    const resolvedSchema = this.resolveSchema(schema);
    const actualType =
      typeof resolvedSchema === 'string'
        ? resolvedSchema
        : Array.isArray(resolvedSchema)
          ? 'union'
          : resolvedSchema.type;
 
    if (actualType !== expectedType) {
      throw new Error(`Expected schema type ${expectedType}, got ${actualType}`);
    }
  }
 
  private resolveSchema(schema: AvroSchema): AvroSchema {
    if (typeof schema === 'string') {
      const namedSchema = this.namedSchemas.get(schema);
      return namedSchema || schema;
    }
    return schema;
  }
 
  private collectNamedSchemas(schema: AvroSchema): void {
    if (typeof schema === 'string' || Array.isArray(schema)) {
      return;
    }
 
    if (typeof schema === 'object' && schema !== null) {
      switch (schema.type) {
        case 'record': {
          const recordSchema = schema as AvroRecordSchema;
          const recordFullName = this.getFullName(recordSchema.name, recordSchema.namespace);
          this.namedSchemas.set(recordFullName, recordSchema);
          recordSchema.fields.forEach((field) => this.collectNamedSchemas(field.type));
          break;
        }
        case 'enum': {
          const enumSchema = schema as AvroEnumSchema;
          const enumFullName = this.getFullName(enumSchema.name, enumSchema.namespace);
          this.namedSchemas.set(enumFullName, enumSchema);
          break;
        }
        case 'fixed': {
          const fixedSchema = schema as AvroFixedSchema;
          const fixedFullName = this.getFullName(fixedSchema.name, fixedSchema.namespace);
          this.namedSchemas.set(fixedFullName, fixedSchema);
          break;
        }
        case 'array':
          this.collectNamedSchemas((schema as AvroArraySchema).items);
          break;
        case 'map':
          this.collectNamedSchemas((schema as AvroMapSchema).values);
          break;
      }
    }
  }
 
  private getFullName(name: string, namespace?: string): string {
    return namespace ? `${namespace}.${name}` : name;
  }
}