avro-js is a pure JavaScript implementation of the Apache Avro specification, providing efficient data serialization and deserialization. It is currently stable at version 1.12.1, with the Apache Avro project demonstrating an active release cadence, including regular minor and patch updates across its language SDKs. Key differentiators include its reported speed (often twice as fast as JSON with significantly smaller encodings), comprehensive Avro feature support (including recursive schemas, sort order, and schema evolution), and the ability to serialize arbitrary JavaScript objects through logical types. Notably, it boasts zero runtime dependencies and is designed to run both in Node.js environments and modern web browsers. While the core project evolves the Avro specification and multi-language SDKs, avro-js focuses solely on the JavaScript ecosystem.
npm install avro-jsVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic Avro schema parsing, object serialization to a buffer, deserialization back to an object, random instance generation, and schema validation.
For a union type like `["null", "MyRecord"]` where `MyRecord` is recursive, always structure nested data as `{ MyRecord: { field1: value1, ... } }` instead of directly `{ field1: value1, ... }`.In browser environments, load schemas as JSON strings or JavaScript objects using `avro.parse(jsonString)` or `avro.parse(jsonObject)`. Avoid all file I/O methods.
To preserve precision for `long` types, configure `avro-js` to use `BigInt` or a library like `long.js` for `long` schema types. This often involves customizing logical types or type hooks during schema parsing. Refer to the `avro-js` documentation for specific configuration options related to 64-bit integers.
Always validate Avro schemas carefully using online tools or a schema linter. Pay close attention to JSON syntax rules and Avro's complex type definitions, especially for `record`, `enum`, `array`, `map`, and `union` types. Ensure all required fields like `name` and `type` are correctly defined.
When dealing with recursive or optional record types within a union, explicitly wrap the nested object with its Avro record name, e.g., `{ next: { LongList: { value: 2, next: null } } }` instead of `{ next: { value: 2, next: null } }`.For modern JavaScript environments, use `import * as avro from 'avro-js';`. If running in Node.js ES Modules, ensure your `package.json` specifies `"type": "module"` and adjust imports accordingly. If `avro-js` is primarily CJS, use `import avro from 'avro-js';` with caution, as it implies a default export which may not exist, or `import * as avro from 'avro-js';`.
Ensure that `avro-js` file-based operations are only executed in Node.js. For browser applications, load schemas as pre-fetched JSON strings or JavaScript objects and use `avro.parse(jsonString)` or `avro.parse(jsonObject)`.
Ensure that the JavaScript object being passed for serialization contains all fields that are defined as required in the corresponding Avro schema. Provide a value for the missing field or update the schema to make the field optional or provide a default.
No dependency data recorded yet.