or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

browser.mdfiles.mdindex.mdschemas.mdservices.mdstreams.mdtypes.md
tile.json

index.mddocs/

AVSC

AVSC 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.

Package Information

  • Package Name: avsc
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install avsc
  • Compatibility: Node.js 0.11+ and browsers (via browserify)

Core Imports

const 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');

Basic Usage

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}); // true

Architecture

AVSC is built around several key components:

  • Type System: Core serialization engine with support for all Avro primitive and complex types
  • Schema Parsing: IDL and JSON schema parsing with support for schema evolution
  • Container Files: Streaming support for reading/writing Avro container files
  • RPC Services: Complete implementation of Avro's Remote Procedure Call protocol
  • Browser Compatibility: Optimized builds for different browser usage patterns
  • Logical Types: Extension system for custom data type handling

Capabilities

Core Type System

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;
}

Type System

File Operations

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);

File Operations

Streaming Support

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;
}

Streaming

RPC Services

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);

RPC Services

Schema Processing

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);

Schema Processing

Browser Support

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);

Browser Support

Types

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;
}