Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'avsc' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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++)
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()
}
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(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('.'));
}
});
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
// 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')) {
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('.'));
}
});