2019-10-31 06:09:42 +01:00
|
|
|
/**
|
|
|
|
* @author jarrodconnolly [jarrod@nestedquotes.ca]
|
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
|
|
|
import avro from "avsc";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avro to JSON operation
|
|
|
|
*/
|
|
|
|
class AvroToJSON extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AvroToJSON constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Avro to JSON";
|
2019-10-31 15:17:07 +01:00
|
|
|
this.module = "Serialise";
|
2019-10-31 06:09:42 +01:00
|
|
|
this.description = "Converts Avro encoded data into JSON.";
|
2019-10-31 15:17:07 +01:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Apache_Avro";
|
2019-10-31 06:09:42 +01:00
|
|
|
this.inputType = "ArrayBuffer";
|
2019-10-31 15:17:07 +01:00
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Force Valid JSON",
|
|
|
|
type: "boolean",
|
|
|
|
value: true
|
|
|
|
}
|
|
|
|
];
|
2019-10-31 06:09:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ArrayBuffer} input
|
|
|
|
* @param {Object[]} args
|
2019-10-31 15:17:07 +01:00
|
|
|
* @returns {string}
|
2019-10-31 06:09:42 +01:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
if (input.byteLength <= 0) {
|
|
|
|
throw new OperationError("Please provide an input.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const forceJSON = args[0];
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const result = [];
|
|
|
|
const inpArray = new Uint8Array(input);
|
|
|
|
const decoder = new avro.streams.BlockDecoder();
|
|
|
|
|
|
|
|
decoder
|
|
|
|
.on("data", function (obj) {
|
|
|
|
result.push(obj);
|
|
|
|
})
|
|
|
|
.on("error", function () {
|
|
|
|
reject(new OperationError("Error parsing Avro file."));
|
|
|
|
})
|
|
|
|
.on("end", function () {
|
|
|
|
if (forceJSON) {
|
2019-10-31 15:17:07 +01:00
|
|
|
resolve(result.length === 1 ? JSON.stringify(result[0], null, 4) : JSON.stringify(result, null, 4));
|
2019-10-31 06:09:42 +01:00
|
|
|
} else {
|
|
|
|
const data = result.reduce((result, current) => result + JSON.stringify(current) + "\n", "");
|
|
|
|
resolve(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
decoder.write(inpArray);
|
|
|
|
decoder.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AvroToJSON;
|