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

@sondremare/avro-schema-registry

v3.1.0

Published

Confluent Schema Registry implementation in javascript to easily serialize and deserialize kafka messages

Downloads

7

Readme

avro-schema-registry

Confluent Schema Registry implementation to easily serialize and deserialize kafka messages with only one peer depencency.

Quickstart

const registry = require('avro-schema-registry')('https://host.com:8081');

const schema = {type: 'string'};
const message = 'test message';

registry.encodeMessage('topic', schema, message)
  .then((msg) => {
    console.log(msg);   // <Buffer 00 00 00 00 01 18 74 65 73 74 20 6d 65 73 73 61 67 65>

    return registry.decode(msg);
  })
  .then((msg) => {
    console.log(msg);  // test message
  });

registry.encodeById(1, message)
  .then((msg) => {
    console.log(msg);   // <Buffer 00 00 00 00 01 18 74 65 73 74 20 6d 65 73 73 61 67 65>

    return registry.decode(msg);
  })

Install

npm install avsc // if not already installed
npm install avro-schema-registry

Doc

The module exports one function only, which expects a url parameter, which is a Confluent Schema Registry endpoint and an optional auth object. The function returns an object .

Every method returns a Promise. Every method uses an internal cache to store already retrieved schemas and if the same id or schema is used again it won't perform another network call. Schemas are cached with their parsing options.

Authentication with the Schema Registry

You can set username and password in the url object:

require('avro-schema-registry')('https://username:[email protected]:8081');

You can pass in an optional second parameter for the registry, with the username and password:

require('avro-schema-registry')('https://host.com:8081', {username: 'username', password: 'password'});

If both the url contains the authencation information and there's an authentication object parameter then the object takes precedence.

decode

Parameters:

  • msg: object to decode
  • parseOptions: parsiong options to pass to avsc.parse, default: null

Decodes an avro encoded buffer into a javascript object.

decodeMessage

Same as decode, only exists for backward compatibility reason.

encodeKey

Parameters:

  • topic: the topic to register the schema, if it doesn't exist already in the registry. The schema will be put under the subject ${topic}-key
  • schema: object representing an avro schema
  • msg: message object to be encoded
  • parseOptions: parsiong options to pass to avsc.parse, default: null

Encodes an object into an avro encoded buffer.

encodeMessage

Parameters:

  • topic: the topic to register the schema, if it doesn't exist already in the registry. The schema will be put under the subject ${topic}-value
  • schema: object representing an avro schema
  • msg: message object to be encoded
  • parseOptions: parsiong options to pass to avsc.parse, default: null

Encodes a message object into an avro encoded buffer.

encodeById

Parameters:

  • id: schema id in the registry
  • msg: message object to be encoded
  • parseOptions: parsiong options to pass to avsc.parse, default: null

Encodes a message object into an avro encoded buffer by fetching the schema from the registry.

encodeMessageByTopicName

Try to get already existing schema from the schema registry and encode message with obtained schema. Please note, that the latest schema will be obtained.

This may be useful when topic consumer is on duty of providing the schema for the topic's messages.

Parameters:

  • topic: topic name to fetch schema for
  • msg: message object to be encoded
  • parseOptions: parsiong options to pass to avsc.parse, default: null

getSchemaByTopicName

This method tries to get already existing schema from the schema registry. Please note, that the latest schema will be obtained.

Parameters:

  • topic: topic name to fetch schema for
  • parseOptions: parsiong options to pass to avsc.parse, default: null

Peer dependency

The module has no dependency, only one peer dependency: avsc