Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ovotech/avro-stream

Package Overview
Dependencies
Maintainers
78
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ovotech/avro-stream

Serialize/deserialize kafka-node streams with avro data, using confluent schema-registry to hold the schemas

  • 1.1.0
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
78
Weekly downloads
 
Created
Source

Avro Stream

Serialize/deserialize kafka-node streams with avro data, using confluent schema-registry to hold the schemas.

Using

yarn add @ovotech/avro-stream

Where sourceStream is a node readble stream, producing kafka-node produce objects. With an additional "schema" key holding the avro schema.

If the schema for the topic does not exist inside the schema registry, it would be created. Unless the auto create topic has been set for kafka, it would not create the topic automatically. You'll need to create it yourself.

import { AvroSerializer, AvroProduceRequest } from '@ovotech/avro-stream';
import { ReadableMock } from 'stream-mock';
import { ProducerStream } from 'kafka-node';

const data: AvroProduceRequest[] = [
  {
    topic: 'migration-completed',
    partition: 0,
    key: 'some-key',
    schema: {
      type: 'record',
      name: 'TestSchema',
      fields: [{ name: 'accountId', type: 'string' }],
    },
    messages: [{ accountId: '6709629' }, { accountId: '5709629' }],
  },
];

const sourceStream = new ReadableMock(data, { objectMode: true });
const producerStream = new ProducerStream({ kafkaClient: { kafkaHost: 'localhost:29092' } });
const serializer = new AvroSerializer('http://localhost:8081');

sourceStream.pipe(serializer).pipe(producerStream);

For deserializing avro kafka events:

import { AvroDeserializer } from '@ovotech/avro-stream';
import { WritableMock } from 'stream-mock';
import { ConsumerGroupStream } from 'kafka-node';

const consumerStream = new ConsumerGroupStream(
  {
    kafkaHost: 'localhost:29092',
    groupId: 'my-group',
    encoding: 'buffer',
    fromOffset: 'earliest',
  },
  ['migration-completed'],
);
const deserializer = new AvroDeserializer('http://localhost:8081');
const sinkStream = new WritableMock({ objectMode: true });

consumerStream.pipe(deserializer).pipe(sinkStream);

Custom schema registry implementations

The way avro serialization for kafka works is to embed the schema id as the first 5 bytes of the buffer so the buffer becomes <id><avro serialized buffer>. For that to work we need a resolver service that can do id->schema for deserializing and schema->id to serializing kafka events.

The default provided resolver is SchemaRegistryresolver using confluent schema-registry but you can write your own:

import { AvroSerializer, AvroDeserializer, SchemaResolver } from '@ovotech/avro-stream';

class MyResolver implements SchemaResolver {
  async toId(topic: string, schema: Schema) {
    return ...
  }

  async fromId(id: number) {
    return ...
  }
}

const resolver = new MyResolver();
const serializer = new AvroSerializer(resolver);
const deserializer = new AvroDeserializer(resolver);

Passing avro schema options

Sometimes you'll want to pass some options to the creation of the avro type from the schema, for example to pass in logical type resolvers. You can do that with the second argument to the constructors.

import { AvroSerializer, AvroDeserializer } from '@ovotech/avro-stream';

const serializer new AvroSerializer('...', { logicalTypes: ... });
const deserializer new AvroDeserializer('...', { logicalTypes: ... });

Errors

AvroSerializer can emit an AvroSerializerError, and subsequently AvroDeserializer - AvroDeserializerError. They are as follows:

AvroSerializerError

PropertyDescription
messageOriginal error message
chunkThe event sent from the previous stream to be serialized (AvroProduceRequest)
encodingThe buffer encoding
originalErrorThe original error object that was triggered

AvroDeserializer

PropertyDescription
messageOriginal error message
chunkThe event sent from the previous stream to be deserialized from kafka-node
encodingThe buffer encoding
originalErrorThe original error object that was triggered

Example error handling:

import { AvroSerializer, AvroSerializerError } from '@ovotech/avro-stream';

const serializer new AvroSerializer('...');

serializer.on('error', (error: AvroSerializerError) => {
  console.log(error.chunk);
})

Gotchas

A thing to be aware of is that node streams unpipe in an event of an error, which means that you'll need to provide your own error handling and repipe the streams if you want it to be resilient to errors.

Running the tests

The tests require a running schema registry service, kafka and zookeeper. This is setup easily with a docker-compose:

docker-compose up

Then you can run the tests with:

yarn test

Coding style (linting, etc) tests

Style is maintained with prettier and tslint

yarn lint

Deployment

To deploy a new version, push to master and then create a new release. CircleCI will automatically build and deploy a the version to the npm registry. All package versions are synchronized, but it will only publish the versions of the packages that have changed.

Contributing

Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see test/integration.spec.ts).

Responsible Team

  • OVO Energy's Boost Internal Tools (BIT)

License

This project is licensed under Apache 2 - see the LICENSE file for details

FAQs

Package last updated on 14 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc