Pure JavaScript implementation of the Apache Avro specification for data serialization with blazingly fast and compact binary serialization, comprehensive schema support, and RPC capabilities.
npx @tessl/cli install tessl/npm-avsc@5.7.0AVSC is a pure JavaScript implementation of the Apache Avro specification for data serialization. It provides blazingly fast and compact binary serialization that is typically faster than JSON with much smaller encodings, comprehensive Avro functionality including type inference and schema evolution, support for serializing arbitrary JavaScript objects through logical types, and Remote Procedure Call (RPC) capabilities for distributed systems.
npm install avscconst avsc = require('avsc');For ES modules:
import avsc from 'avsc';Browser specific imports:
// Full browser build with blob support
const avsc = require('avsc/etc/browser/avsc');
// Types only for smaller bundle
const avsc = require('avsc/etc/browser/avsc-types');
// With services (RPC) support
const avsc = require('avsc/etc/browser/avsc-services');const avsc = require('avsc');
// Parse a schema and create a type
const type = avsc.parse({
type: 'record',
name: 'User',
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'}
]
});
// Serialize data to buffer
const user = {name: 'Alice', age: 30};
const buf = type.toBuffer(user);
// Deserialize data from buffer
const decoded = type.fromBuffer(buf);
console.log(decoded); // {name: 'Alice', age: 30}
// Validate data against schema
const isValid = type.isValid({name: 'Bob', age: 25}); // trueAVSC is built around several key components:
The foundation of AVSC providing schema parsing, type creation, and data serialization/deserialization with full Avro specification compliance.
/**
* Parse a schema or protocol and return the corresponding type or service
* @param schema - Avro schema or protocol object, JSON string, or IDL string
* @param opts - Parsing options
* @returns Type or Service instance
*/
function parse(schema, opts);
class Type {
static forSchema(schema, opts);
static forValue(value, opts);
static forTypes(types, opts);
static isType(obj, ...prefixes);
toBuffer(value);
fromBuffer(buffer, resolver, noCheck);
isValid(value, opts);
clone(value, opts);
compare(val1, val2);
equals(type);
fingerprint(algorithm);
schema(opts);
}
/** Built-in Avro type constructors */
namespace types {
class NullType extends Type;
class BooleanType extends Type;
class IntType extends Type;
class LongType extends Type;
class FloatType extends Type;
class DoubleType extends Type;
class BytesType extends Type;
class StringType extends Type;
class RecordType extends Type;
class EnumType extends Type;
class ArrayType extends Type;
class MapType extends Type;
class UnionType extends Type;
class FixedType extends Type;
class LogicalType extends Type;
}Node.js specific functionality for reading and writing Avro container files from the filesystem with streaming support and header extraction.
function createFileDecoder(path, opts);
function createFileEncoder(path, schema, opts);
function extractFileHeader(path, opts);High-performance streaming interfaces for processing large datasets with support for both container files and raw data streams.
namespace streams {
class BlockDecoder extends stream.Duplex;
class BlockEncoder extends stream.Duplex;
class RawDecoder extends stream.Duplex;
class RawEncoder extends stream.Duplex;
class FrameDecoder extends stream.Duplex;
class FrameEncoder extends stream.Duplex;
class NettyDecoder extends stream.Duplex;
class NettyEncoder extends stream.Duplex;
// Container constants
const MAGIC_BYTES: Buffer;
const HEADER_TYPE: Type;
const BLOCK_TYPE: Type;
}Complete Avro RPC implementation with client-server communication, protocol discovery, and transport abstraction for building distributed systems.
class Service {
static forProtocol(protocol, opts);
static compatible(client, server);
createClient(opts);
createServer(opts);
message(name);
type(name);
}
function discoverProtocol(transport, opts, callback);IDL parsing and protocol assembly functionality for working with Avro Interface Definition Language and complex schema compositions.
function assembleProtocol(filePath, opts, callback);
function readProtocol(protocolIdl, opts);
function readSchema(schemaIdl, opts);Browser-optimized builds with blob support and different bundle sizes for various use cases from types-only to full RPC functionality.
function createBlobDecoder(blob, opts);
function createBlobEncoder(schema, opts);interface ForSchemaOptions {
assertLogicalTypes?: boolean;
logicalTypes?: {[type: string]: LogicalTypeConstructor};
namespace?: string;
noAnonymousTypes?: boolean;
omitRecordMethods?: boolean;
registry?: {[name: string]: Type};
typeHook?: (schema, opts) => Type | undefined;
wrapUnions?: boolean | 'auto' | 'always' | 'never';
}
interface CloneOptions {
coerceBuffers?: boolean;
fieldHook?: (field, value, type) => any;
qualifyNames?: boolean;
skipMissingFields?: boolean;
wrapUnions?: boolean;
}
interface IsValidOptions {
noUndeclaredFields?: boolean;
errorHook?: (path, val, type) => void;
}
interface SchemaOptions {
exportAttrs?: boolean;
noDeref?: boolean;
}