JSPM

  • Created
  • Published
  • Downloads 916527
  • Score
    100M100P100Q188230F
  • License MIT

A serialization API to make you smile

Package Exports

  • avsc
  • avsc/etc/browser/avsc
  • avsc/etc/browser/avsc.js
  • avsc/etc/browser/crypto.js
  • avsc/lib
  • avsc/lib/types
  • avsc/lib/utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (avsc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Avsc NPM version Build status Coverage status

Pure JavaScript implementation of the Avro specification.

Features

Performance

Representative decoding throughput rates (higher is better):

Throughput rate chart

Libraries compared:

  • node-avsc, this package.
  • node-json, built-in JSON serializer.
  • node-pson, an alternative to JSON.
  • node-etp-avro, existing Avro implementation.
  • node-avro-io, other popular Avro implementation.

These rates are for decoding a realistic record schema, modeled after a popular open-source API. Encoding rates are slightly lower but ratios across libraries are similar. You can find the raw numbers and more details on the benchmarks page.

Installation

$ npm install avsc

avsc is compatible with all versions of node.js since 0.11 and major browsers via browserify.

Documentation

Examples

Inside a node.js module, or using browserify:

var avsc = require('avsc');
  • Encode and decode objects:

    // We can declare a schema inline:
    var type = avsc.parse({
      name: 'Pet',
      type: 'record',
      fields: [
        {name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
        {name: 'name', type: 'string'}
      ]
    });
    var pet = {kind: 'CAT', name: 'Albert'};
    var buf = type.toBuffer(pet); // Serialized object.
    var obj = type.fromBuffer(buf); // {kind: 'CAT', name: 'Albert'}
  • Generate random instances of a schema:

    // We can also parse a JSON-stringified schema:
    var type = avsc.parse('{"type": "fixed", "name": "Id", "size": 4}');
    var id = type.random(); // E.g. Buffer([48, 152, 2, 123])
  • Check whether an object fits a given schema:

    // Or we can specify a path to a schema file (not in the browser):
    var type = avsc.parse('./Person.avsc');
    var person = {name: 'Bob', address: {city: 'Cambridge', zip: '02139'}};
    var status = type.isValid(person); // Boolean status.
  • Get a readable stream of decoded records from an Avro container file (not in the browser):

    avsc.createFileDecoder('./records.avro')
      .on('metadata', function (type) { /* `type` is the writer's type. */ })
      .on('data', function (record) { /* Do something with the record. */ });

Limitations

  • Protocols aren't yet implemented.