This module defines all Avro data types and their serialization logic.
This module defines all Avro data types and their serialization logic.
Customizable long.
This allows support of arbitrarily large long (e.g. larger than
Number.MAX_SAFE_INTEGER
). See LongType.__with
method above. Note that we
can't use a logical type because we need a "lower-level" hook here: passing
through through the standard long would cause a loss of precision.
Avro array. Represented as vanilla arrays.
Booleans.
Bytes.
These are represented in memory as Buffer
s rather than binary-encoded
strings. This is more efficient (when decoding/encoding from bytes, the
common use-case), idiomatic, and convenient.
Note the coercion in _copy
.
Doubles.
Avro enum type.
Represented as strings (with allowed values from the set of symbols). Using integers would be a reasonable option, but the performance boost is arguably offset by the legibility cost and the extra deviation from the JSON encoding convention.
An integer representation can still be used (e.g. for compatibility with
TypeScript enum
s) by overriding the EnumType
with a LongType
(e.g. via
parse
's registry).
A record field.
Avro fixed type. Represented simply as a Buffer
.
Floats.
Mutable hash container.
Integers.
Derived type abstract class.
Longs.
We can't capture all the range unfortunately since JavaScript represents all
numbers internally as double
s, so the default implementation plays safe
and throws rather than potentially silently change the data. See __with
or
AbstractLongType
below for a way to implement a custom long type.
Avro map. Represented as vanilla objects.
Nulls.
Base primitive Avro type.
Most of the primitive types share the same cloning and resolution
mechanisms, provided by this class. This class also lets us conveniently
check whether a type is a primitive using instanceof
.
Avro record.
Values are represented as instances of a programmatically generated
constructor (similar to a "specific record"), available via the
getRecordConstructor
method. This "specific record class" gives
significant speedups over using generics objects.
Note that vanilla objects are still accepted as valid as long as their fields match (this makes it much more convenient to do simple things like update nested records).
This type is also used for errors (similar, except for the extra Error
constructor call) and for messages (see comment below).
Resolver to read a writer's schema as a new schema.
The type to convert to.
Strings.
"Abstract" base Avro type.
This class' constructor will register any named types to support recursive schemas. All type values are represented in memory similarly to their JSON representation, except for:
bytes
and fixed
which are represented as Buffer
s.union
s which will be "unwrapped" unless the wrapUnions
option is set.
See individual subclasses for details.
Base "abstract" Avro union type.
"Natural" union type.
This representation doesn't require a wrapping object and is therefore simpler and generally closer to what users expect. However it cannot be used to represent all Avro unions since some lead to ambiguities (e.g. if two number types are in the union).
Currently, this union supports at most one type in each of the categories below:
null
boolean
int
, long
, float
, double
string
, enum
bytes
, fixed
array
map
, record
Compatible union type.
Values of this type are represented in memory similarly to their JSON representation (i.e. inside an object with single key the name of the contained type).
This is not ideal, but is the most efficient way to unambiguously support all unions. Here are a few reasons why the wrapping object is necessary:
Combine bytes and fixed.
This function is optimized to avoid creating new types when possible: in case of a size mismatch between fixed types, it will continue looking through the array to find an existing bytes type (rather than exit early by creating one eagerly).
Combine number types.
Note that never have to create a new type here, we are guaranteed to be able to reuse one of the input types as super-type.
Combine maps and records.
Field defaults are kept when possible (i.e. when no coercion to a map happens), with later definitions overriding previous ones.
Combine enums and strings.
The order of the returned symbols is undefined and the returned enum is
Get all aliases for a type (including its name).
Typically a type or a field. Its aliases property must exist and be an array.
Return a type's class name from its Avro type name.
We can't simply use constructor.name
since it isn't supported in all
browsers.
Type name.
Get a type's bucket when included inside an unwrapped union.
Any type.
Infer a value's bucket (see unwrapped unions for more details).
Any value.
Check whether a collection of types leads to an ambiguous union.
Array of types.
Check whether an object is the JSON representation of a buffer.
Check whether a type's name is a primitive.
Check whether a long can be represented without precision loss.
The number.
Two things to note:
Number
constants for compatibility with older
browsers.Check whether a string is a valid Avro identifier.
Verify and return fully qualified name.
Full or short name. It can be prefixed with a dot to force global namespace.
Optional namespace.
Get the number of elements in an array block.
A tap positioned at the beginning of an array block.
Throw a somewhat helpful error on invalid object.
The object to reject.
The type to check against.
This method is mostly used from _write
to signal an invalid object for a
given type. Note that this provides less information than calling isValid
with a hook since the path is not propagated (for efficiency reasons).
Remove namespace from a name.
Full or short name.
Generated using TypeDoc
This module defines all Avro data types and their serialization logic.