Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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++)
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) {
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(
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);
});
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 }
};
function instrument(schema) {
if (schema instanceof avsc.Type) {
schema = schema.getSchema();
}
var refs = [];
return avsc.parse(schema, {typeHook: hook,
wrapUnions: true});
function hook(schema, opts) {
if (~refs.indexOf(schema)) {
return;
}
refs.push(schema);
if (schema.type === 'record') {
schema.fields.forEach(function (f) {
f['default'] = undefined;
});
}
var name = schema.name;
if (name) {
res.on('end', () => {
if (res.statusCode !== 200) {
const error = JSON.parse(data);
return reject(new Error(`Schema registry error: ${error.error_code} - ${error.message}`));
}
const schema = JSON.parse(data).schema;
try {
resolve(avsc.parse(schema, parseOptions));
} catch (e) {
reject(e)
}
});
}).on('error', (e) => {
}
return {value: obj};
}
var primitiveSymbols = [
"boolean",
"bytes",
"int",
"long",
"double",
"float",
"null",
"string"
];
var metaType = avro.parse({
"logicalType": "meta",
"type": "record",
"name": "Meta",
"fields": [
{
"type": [
{
"type": "enum",
"name": "PrimitiveType",
"symbols": primitiveSymbols,
},
{
"type": "record",
"name": "Array",
"fields": [
{
function typeFromSchemaResponse(schema, parseOptions) {
const schemaType = avro.parse(schema);
//check if the schema has been previously parsed and added to the registry
if (typeof parseOptions.registry === 'object' && typeof parseOptions.registry[schemaType.name] !== 'undefined') {
return parseOptions.registry[schemaType.name];
}
return avro.parse(schema, parseOptions);
}
function typeFromSchemaResponse(schema, parseOptions) {
const schemaType = avro.parse(schema);
//check if the schema has been previously parsed and added to the registry
if (typeof parseOptions.registry === 'object' && typeof parseOptions.registry[schemaType.name] !== 'undefined') {
return parseOptions.registry[schemaType.name];
}
return avro.parse(schema, parseOptions);
}