Complete schema parsing functionality that converts Avro schemas into executable Type instances. Supports the full Avro specification including complex types, logical types, and schema evolution.
Main entry point for parsing Avro schemas and protocols.
/**
* Parse a schema and return the corresponding type or protocol
* @param {Object|String} schema - Schema definition, JSON string, or file path
* @param {Object} opts - Optional parsing options
* @returns {Type|Protocol} Type instance or Protocol instance
*/
function parse(schema, opts);Usage Examples:
const avro = require('avro-js');
// Parse from object
const type = avro.parse({
type: 'record',
name: 'User',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
]
});
// Parse from JSON string
const jsonSchema = '{"type": "string"}';
const stringType = avro.parse(jsonSchema);
// Parse with options
const typeWithNamespace = avro.parse(schema, {
namespace: 'com.example',
registry: {}
});Abstract base class for all Avro types providing core serialization and validation methods.
/**
* Abstract base class for all Avro types
*/
class Type {
/**
* Create resolver for schema evolution
* @param {Type} type - Target type for resolution
* @param {Object} opts - Resolution options
* @returns {Object} Resolver object
*/
createResolver(type, opts);
/**
* Encode value to buffer at position
* @param {*} val - Value to encode
* @param {Buffer} buf - Target buffer
* @param {Number} pos - Position in buffer
* @returns {Number} New position after encoding
*/
encode(val, buf, pos);
/**
* Decode value from buffer at position
* @param {Buffer} buf - Source buffer
* @param {Number} pos - Position in buffer
* @param {Object} resolver - Optional resolver
* @returns {*} Decoded value
*/
decode(buf, pos, resolver);
/**
* Serialize value to buffer
* @param {*} val - Value to serialize
* @returns {Buffer} Serialized data
*/
toBuffer(val);
/**
* Deserialize from buffer
* @param {Buffer} buf - Buffer to deserialize
* @param {Object} resolver - Optional resolver for schema evolution
* @param {Boolean} noCheck - Skip validation
* @returns {*} Deserialized value
*/
fromBuffer(buf, resolver, noCheck);
/**
* Parse from JSON string
* @param {String} str - JSON string to parse
* @returns {*} Parsed value
*/
fromString(str);
/**
* Convert to JSON string
* @param {*} val - Value to convert
* @returns {String} JSON representation
*/
toString(val);
/**
* Deep clone value
* @param {*} val - Value to clone
* @param {Object} opts - Clone options
* @returns {*} Cloned value
*/
clone(val, opts);
/**
* Validate value against schema
* @param {*} val - Value to validate
* @param {Object} opts - Validation options
* @returns {Boolean} True if valid
*/
isValid(val, opts);
/**
* Compare two encoded values
* @param {Buffer} buf1 - First buffer
* @param {Buffer} buf2 - Second buffer
* @returns {Number} Comparison result (-1, 0, 1)
*/
compareBuffers(buf1, buf2);
/**
* Get type name
* @param {Boolean} noRef - Don't use references
* @returns {String} Type name
*/
getName(noRef);
/**
* Get schema representation
* @param {Boolean} noDeref - Don't dereference
* @returns {Object} Schema object
*/
getSchema(noDeref);
/**
* Get schema fingerprint
* @param {String} algorithm - Hash algorithm (default: MD5)
* @returns {Buffer} Fingerprint hash
*/
getFingerprint(algorithm);
/**
* Compare two values
* @param {*} val1 - First value
* @param {*} val2 - Second value
* @returns {Number} Comparison result (-1, 0, 1)
*/
compare(val1, val2);
/**
* Generate random valid value
* @returns {*} Random value matching schema
*/
random();
}Built-in primitive type implementations.
/**
* Avro null type - represents null values
*/
class NullType extends Type {}
/**
* Avro boolean type - represents true/false values
*/
class BooleanType extends Type {}
/**
* Avro int type - 32-bit signed integer
*/
class IntType extends Type {}
/**
* Avro long type - 64-bit signed integer
*/
class LongType extends Type {
/**
* Create custom long type implementation
* @param {Object} methods - Required methods object
* @param {Boolean} noUnpack - Skip unpacking optimization
* @returns {LongType} Custom long type
*/
static using(methods, noUnpack);
}
/**
* Avro float type - 32-bit IEEE 754 floating point
*/
class FloatType extends Type {}
/**
* Avro double type - 64-bit IEEE 754 floating point
*/
class DoubleType extends Type {}
/**
* Avro string type - UTF-8 encoded strings
*/
class StringType extends Type {}
/**
* Avro bytes type - arbitrary byte sequences (Node.js Buffer)
*/
class BytesType extends Type {}Complex type implementations for structured data.
/**
* Avro array type - ordered collection of items
*/
class ArrayType extends Type {
/**
* Get array item type
* @returns {Type} Item type
*/
getItemsType();
}
/**
* Avro map type - key-value pairs with string keys
*/
class MapType extends Type {
/**
* Get map value type
* @returns {Type} Value type
*/
getValuesType();
}
/**
* Avro union type - choice between multiple types
*/
class UnionType extends Type {
/**
* Get array of union member types
* @returns {Type[]} Array of member types
*/
getTypes();
}
/**
* Avro enum type - named set of symbols
*/
class EnumType extends Type {
/**
* Get array of enum symbols
* @returns {String[]} Array of symbols
*/
getSymbols();
/**
* Get array of aliases
* @returns {String[]} Array of aliases
*/
getAliases();
}
/**
* Avro fixed type - fixed-length byte sequence
*/
class FixedType extends Type {
/**
* Get fixed size in bytes
* @returns {Number} Size in bytes
*/
getSize();
/**
* Get array of aliases
* @returns {String[]} Array of aliases
*/
getAliases();
}
/**
* Avro record type - structured data with named fields
*/
class RecordType extends Type {
/**
* Get array of Field instances
* @returns {Field[]} Array of fields
*/
getFields();
/**
* Get constructor function for record instances
* @returns {Function} Constructor function
*/
getRecordConstructor();
/**
* Get array of aliases
* @returns {String[]} Array of aliases
*/
getAliases();
}Advanced type system features including logical types and field definitions.
/**
* Abstract base class for logical types
*/
class LogicalType extends Type {
/**
* Constructor for logical types
* @param {Object} attrs - Type attributes
* @param {Object} opts - Options
* @param {Function[]} Types - Type constructors
*/
constructor(attrs, opts, Types);
/**
* Get underlying Avro type
* @returns {Type} Underlying type
*/
getUnderlyingType();
/**
* Convert from underlying type (abstract method)
* @param {*} val - Value from underlying type
* @returns {*} Converted value
*/
_fromValue(val);
/**
* Convert to underlying type (abstract method)
* @param {*} val - Value to convert
* @returns {*} Value for underlying type
*/
_toValue(val);
/**
* Schema resolution logic (abstract method)
* @param {Type} type - Target type
* @param {Object} opts - Resolution options
* @returns {Object} Resolution result
*/
_resolve(type, opts);
}
/**
* Represents a record field
*/
class Field {
/**
* Constructor for record fields
* @param {Object} attrs - Field attributes
* @param {Object} opts - Options
*/
constructor(attrs, opts);
/**
* Get field name
* @returns {String} Field name
*/
getName();
/**
* Get field type
* @returns {Type} Field type
*/
getType();
/**
* Get default value
* @returns {*} Default value or undefined
*/
getDefault();
/**
* Get field aliases
* @returns {String[]} Array of aliases
*/
getAliases();
/**
* Get sort order
* @returns {String} Sort order ('ascending', 'descending', 'ignore')
*/
getOrder();
}All type classes are available through the types export:
const types = {
Type,
NullType,
BooleanType,
IntType,
LongType,
FloatType,
DoubleType,
StringType,
BytesType,
ArrayType,
MapType,
UnionType,
EnumType,
FixedType,
RecordType,
LogicalType,
Field
};Usage Example:
const avro = require('avro-js');
const { LongType } = avro.types;
// Create custom long type
const customLong = LongType.using({
toBuffer: (val) => Buffer.from(val.toString()),
fromBuffer: (buf) => parseInt(buf.toString()),
// ... other methods
});interface ParseOptions {
/** Parent namespace for type resolution */
namespace?: string;
/** Registry of predefined types */
registry?: Object;
/** Dictionary of LogicalType classes */
logicalTypes?: Object;
/** Throw error on invalid logical types */
assertLogicalTypes?: boolean;
/** Custom type creation hook */
typeHook?: (attrs: Object, opts: ParseOptions) => Type;
}
interface ValidationOptions {
/** Error handling mode */
errorHook?: Function;
/** Skip type checking */
noTypeCheck?: boolean;
}
interface LongMethods {
/** Convert long to buffer */
toBuffer: (val: any) => Buffer;
/** Convert buffer to long */
fromBuffer: (buf: Buffer) => any;
/** Convert JSON to long */
fromJSON: (obj: any) => any;
/** Convert long to JSON */
toJSON: (val: any) => any;
/** Validate long value */
isValid: (val: any) => boolean;
/** Compare two long values */
compare: (val1: any, val2: any) => number;
}