Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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('.'));
}
});
var util = require('util'),
avro = require('avsc');
function MetaType(attr, opts) {
avro.types.LogicalType.call(this, attr, opts, [avro.types.RecordType]);
}
util.inherits(MetaType, avro.types.LogicalType);
MetaType.prototype._fromValue = function(val) {
var obj = val.value;
return obj[Object.keys(obj)[0]];
}
MetaType.prototype._toValue = function(any) {
var obj;
if(typeof any == 'string') {
if (~primitiveSymbols.indexOf(any)) {
// Handling primitive names separately from references lets us save a
// significant amount of space (1 byte per type name instead of 5-8).
obj = { PrimitiveType: any};
} else {
obj = {string: any};
}
'float',
'double',
'bytes',
'string',
// these ones are not primitives but work the same
// {"type": {"type": "enum", "name": "e", "symbols": ["A", "B", "C", "D"]}}
'enum',
// {"type": {"type": "fixed", "size": 16, "name": "f"}}
'fixed'
]
export const FIELD_ID = 'id'
// 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()
function MetaType(attr, opts) {
avro.types.LogicalType.call(this, attr, opts, [avro.types.RecordType]);
}