2018-08-09 12:09:28 +02:00
|
|
|
/**
|
|
|
|
* @author Matt C [matt@artemisbot.uk]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
import OperationError from "../errors/OperationError";
|
|
|
|
import notepack from "notepack.io";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From MessagePack operation
|
|
|
|
*/
|
|
|
|
class FromMessagePack extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FromMessagePack constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "From MessagePack";
|
|
|
|
this.module = "Code";
|
|
|
|
this.description = "Converts MessagePack encoded data to JSON";
|
|
|
|
this.inputType = "ArrayBuffer";
|
|
|
|
this.outputType = "JSON";
|
|
|
|
this.args = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-20 20:08:01 +02:00
|
|
|
* @param {ArrayBuffer} input
|
2018-08-09 12:09:28 +02:00
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {JSON}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
try {
|
2018-08-20 20:08:01 +02:00
|
|
|
const buf = Buffer.from(new Uint8Array(input));
|
|
|
|
return notepack.decode(buf);
|
2018-08-09 12:09:28 +02:00
|
|
|
} catch (err) {
|
2018-08-20 01:20:04 +02:00
|
|
|
throw new OperationError(`Could not decode MessagePack to JSON: ${err}`);
|
2018-08-09 12:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FromMessagePack;
|