npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

avro-js

v1.12.0

Published

JavaScript Avro implementation

Downloads

113,229

Readme

Avro-js

Pure JavaScript implementation of the Avro specification.

Features

  • Fast! Typically twice as fast as JSON with much smaller encodings.
  • Full Avro support, including recursive schemas, sort order, and evolution.
  • Serialization of arbitrary JavaScript objects via logical types.
  • Unopinionated 64-bit integer compatibility.
  • No dependencies, avro-js even runs in the browser.

Installation

$ npm install avro-js

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

Documentation

See doc/ folder.

Examples

Inside a node.js module, or using browserify:

var avro = require('avro-js');
  • Encode and decode objects:

    // We can declare a schema inline:
    var type = avro.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 = avro.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 = avro.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):

    avro.createFileDecoder('./records.avro')
      .on('metadata', function (type) { /* `type` is the writer's type. */ })
      .on('data', function (record) { /* Do something with the record. */ });
  • Implement recursive schemata (due to lack of duck-typing):

      // example type: linked list with one long-int as element value
      const recursiveRecordType =  avro.parse({
        "type": "record",
        "name": "LongList",
        "fields" : [
          {"name": "value", "type": "long"},             
          {"name": "next", "type": ["null", "LongList"]} // optional next element via recursion
        ]
      });
    
      // will work
      const validRecursiveRecordDTO = {
        value: 1,
        next: {
          // no duck-typing support: from first nested level on the 
          // recursive type has to be explicitly specified.
          LongList: {
            value: 2,
            next: null
          }
        }
      };
      const serializedValid = recursiveRecordType.parse(validRecursiveRecordDTO);
        
    
      // will throw error
      const invalidRecursiveRecordDTO = {
        value: 1,
        next: {
            value: 2,
            next: null
        }
      };
      const serializedInvalid = recursiveRecordType.parse(invalidRecursiveRecordDTO);