Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Pure JavaScript implementation of the Avro specification.
avro-js
even runs in the browser.$ npm install avro-js
avro-js
is compatible with all versions of node.js since 0.11
and major
browsers via browserify.
See doc/
folder.
Inside a node.js module, or using browserify:
var avro = require('avro-js');
Encode and decode objects:
// We can declare a schema inline:
var type = avro.parse({
name: 'Pet',
type: 'record',
fields: [
{name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
{name: 'name', type: 'string'}
]
});
var pet = {kind: 'CAT', name: 'Albert'};
var buf = type.toBuffer(pet); // Serialized object.
var obj = type.fromBuffer(buf); // {kind: 'CAT', name: 'Albert'}
Generate random instances of a schema:
// We can also parse a JSON-stringified schema:
var type = avro.parse('{"type": "fixed", "name": "Id", "size": 4}');
var id = type.random(); // E.g. Buffer([48, 152, 2, 123])
Check whether an object fits a given schema:
// Or we can specify a path to a schema file (not in the browser):
var type = avro.parse('./Person.avsc');
var person = {name: 'Bob', address: {city: 'Cambridge', zip: '02139'}};
var status = type.isValid(person); // Boolean status.
Get a readable stream of decoded records from an Avro container file (not in the browser):
avro.createFileDecoder('./records.avro')
.on('metadata', function (type) { /* `type` is the writer's type. */ })
.on('data', function (record) { /* Do something with the record. */ });
Implement recursive schemata (due to lack of duck-typing):
// example type: linked list with one long-int as element value
const recursiveRecordType = avro.parse({
"type": "record",
"name": "LongList",
"fields" : [
{"name": "value", "type": "long"},
{"name": "next", "type": ["null", "LongList"]} // optional next element via recursion
]
});
// will work
const validRecursiveRecordDTO = {
value: 1,
next: {
// no duck-typing support: from first nested level on the
// recursive type has to be explicitly specified.
LongList: {
value: 2,
next: null
}
}
};
const serializedValid = recursiveRecordType.parse(validRecursiveRecordDTO);
// will throw error
const invalidRecursiveRecordDTO = {
value: 1,
next: {
value: 2,
next: null
}
};
const serializedInvalid = recursiveRecordType.parse(invalidRecursiveRecordDTO);
FAQs
JavaScript Avro implementation
We found that avro-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.