How to use avsc - 10 common examples

To help you get started, we’ve selected a few avsc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mattheworiordan / nodejs-encoding-benchmarks / benchmark.js View on Github external
msgpack5  = require('msgpack5')(),
    PSON      = require('pson'),
    pson      = new PSON.StaticPair(),
    avsc      = require('avsc')

// Avro type declarations for avsc.
var typeWithoutBytes = avsc.parse({
  name: 'Record',
  type: 'record',
  fields: [
    {name: 'abcdef', type: 'int'},
    {name: 'qqq', type: 'int'},
    {name: 'a19', type: {type: 'array', items: 'int'}},
  ]
})
var typeWithBytes = avsc.parse({
  name: 'Record',
  type: 'record',
  fields: [
    {name: 'abcdef', type: 'int'},
    {name: 'qqq', type: 'int'},
    {name: 'a19', type: {type: 'array', items: 'int'}},
    {name: 'buf', type: {name: 'Buf', type: 'fixed', size: 256}}
  ]
})

function benchmark(name, data) {
  var testCount = 1e5,
      packed

  console.time(`${name} msgpack.pack`)
  for (var i = 0; i < testCount; i++)
github kafkajs / confluent-schema-registry / src / utils / avdlToAVSC.spec.ts View on Github external
const compareWithJavaImplementation = (avdlPath: string, name: string) => async () => {
  const absolutePathToAvdlToAVSC = absolutePath('./bin/avdlToAVSC.sh')
  const execaArgs = [`./fixtures/avdl/${avdlPath}`, name]

  let expectedAVSC
  try {
    const { stdout: result } = await execa(absolutePathToAvdlToAVSC, execaArgs)
    expectedAVSC = JSON.parse(result)
  } catch (error) {
    console.error(`Error when running ${absolutePathToAvdlToAVSC}`, error) // eslint-disable-line no-console
    throw error
  }

  const avsc = avdlToAVSC(absolutePath('./fixtures/avdl', avdlPath))
  expect(avsc).toEqual(expectedAVSC)
  expect(avro.Type.forSchema(avsc)).toBeTruthy()
  expect(await registry.register(avsc)).toBeTruthy()
}
github ivosequeros / kafkajs-avro / lib / Registry.ts View on Github external
if (url == undefined)
      throw new Error(
        "In order to fetch a schema, an object with format {id} or {subject, version} must be provided"
      );

    const response = await fetch(url);
    if (response.status != 200)
      throw new Error(
        `${
          response.status
        } response code from registry when trying to fetch ${JSON.stringify(
          filter
        )}\n${url}\n${response.statusText}`
      );
    const { id, schema } = await response.json();
    const parsedSchema = avsc.parse(schema, this.parseOptions);

    /* Result */
    this.cache.set(key, { id: filter.id || id, schema });
    return { id: filter.id || id, schema: parsedSchema };
  }
  async encode(subject, version, originalMessage) {
github ivosequeros / kafkajs-avro / lib / Registry.ts View on Github external
async getSchema(filter) {
    const key = filter.id ? filter.id : `${filter.subject}:${filter.version}`;
    /* Check if schema is in cache: */
    if (this.cache.has(key)) {
      const { id, schema } = this.cache.get(key);
      return {
        id,
        schema: avsc.parse(schema, this.parseOptions)
      };
    }

    /* Schema is not in cache, download it: */
    let url;
    if (filter.id) url = `${this.url}/schemas/ids/${filter.id}`;
    if (filter.subject && filter.version)
      url = `${this.url}/subjects/${filter.subject}/versions/${filter.version}`;
    if (url == undefined)
      throw new Error(
        "In order to fetch a schema, an object with format {id} or {subject, version} must be provided"
      );

    const response = await fetch(url);
    if (response.status != 200)
      throw new Error(
github mtth / avsc / js / assemble.js View on Github external
avro.assemble('', opts, function (err, attrs) {
    if (err) {
      cb(err);
      return;
    }
    // Make sure the protocol is valid.
    try {
      window.PROTOCOL = avro.parse(attrs, {wrapUnions: true});
    } catch (parseErr) {
      cb(parseErr);
      return;
    }
    cb(null, attrs);
  });
github dmtrs / introduction-to-avro / registry.js View on Github external
var avro = require('avsc');
var options = { namespace: "com.shop" };

// Version 1
var brand  = avro.parse('./schemas/brand.avsc', options);
var color  = avro.parse('./schemas/color.avsc', options);
var shoe_1 = avro.parse('./schemas/shoe.v1.avsc', options);

// Version 2
var system = avro.parse('./schemas/size.system.avsc', options);
var size   = avro.parse('./schemas/size.avsc', options);
var shoe_2 = avro.parse('./schemas/shoe.v2.avsc', options);

module.exports = {
  v1: { shoe: shoe_1 },
  v2: { shoe: shoe_2 }
};
github mtth / avsc / js / index.js View on Github external
function(path, any, type) {
              if (
                typeof any == 'string' &&
                ( 
                  type instanceof avsc.types.BytesType ||
                  (
                    type instanceof avsc.types.FixedType &&
                    any.length === type.getSize()
                  )
                )
              ) {
                // This is a string-encoded buffer.
                return;
              }
              throw new Error('invalid ' + type + ' at ' + path.join('.'));
            }
          });
github eHealthAfrica / aether / aether-ui / aether / ui / assets / apps / utils / avro-utils.jsx View on Github external
avro.types.UnwrappedUnionType.prototype.random = function () {
  const types = this.types.filter(({ typeName }) => typeName !== NULL)
  if (types.length === 0) {
    return null
  }
  const index = Math.floor(Math.random() * types.length)
  return types[index].random()
}

const randomBytes = (len = 8) => Math.floor((1 + Math.random()) * 16 ** len).toString(16).slice(1)

// In-house workaround to the avsc library to avoid buffer serialization
avro.types.FixedType.prototype.random = function () {
  return randomBytes(this.size)
}
avro.types.BytesType.prototype.random = function () {
  return randomBytes()
}

export const parseSchema = (schema) => (
  avro.parse(schema, { noAnonymousTypes: true, wrapUnions: false })
)

export const randomInput = (schema) => {
  const type = parseSchema(schema)
  const input = type.random()

  // check if there is a string "id" field
  if (schema.fields.find(field => field.name === FIELD_ID && field.type === 'string')) {
    input[FIELD_ID] = generateGUID() // make it more UUID
  }
  return input
github eHealthAfrica / aether / aether-ui / aether / ui / assets / apps / utils / avro-utils.jsx View on Github external
// In-house workaround to the avsc library to avoid null values
// in case of union types
avro.types.UnwrappedUnionType.prototype.random = function () {
  const types = this.types.filter(({ typeName }) => typeName !== NULL)
  if (types.length === 0) {
    return null
  }
  const index = Math.floor(Math.random() * types.length)
  return types[index].random()
}

const randomBytes = (len = 8) => Math.floor((1 + Math.random()) * 16 ** len).toString(16).slice(1)

// In-house workaround to the avsc library to avoid buffer serialization
avro.types.FixedType.prototype.random = function () {
  return randomBytes(this.size)
}
avro.types.BytesType.prototype.random = function () {
  return randomBytes()
}

export const parseSchema = (schema) => (
  avro.parse(schema, { noAnonymousTypes: true, wrapUnions: false })
)

export const randomInput = (schema) => {
  const type = parseSchema(schema)
  const input = type.random()

  // check if there is a string "id" field
  if (schema.fields.find(field => field.name === FIELD_ID && field.type === 'string')) {
github mtth / avsc / js / index.js View on Github external
function(path, any, type) {
              if (
                typeof any == 'string' &&
                ( 
                  type instanceof avsc.types.BytesType ||
                  (
                    type instanceof avsc.types.FixedType &&
                    any.length === type.getSize()
                  )
                )
              ) {
                // This is a string-encoded buffer.
                return;
              }
              throw new Error('invalid ' + type + ' at ' + path.join('.'));
            }
          });

avsc

Avro for JavaScript

MIT
Latest version published 2 years ago

Package Health Score

76 / 100
Full package analysis