Added MessagePack operations

This commit is contained in:
Matt C 2018-08-09 11:09:28 +01:00
parent 2b6c280858
commit 0c06e64051
5 changed files with 218 additions and 43 deletions

View file

@ -0,0 +1,45 @@
/**
* @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 = [];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
try {
return notepack.decode(input);
} catch (err) {
throw new OperationError(`Could not decode MessagePack to JSON:MessagePack ${err}`);
}
}
}
export default FromMessagePack;

View file

@ -0,0 +1,45 @@
/**
* @author Matt C [matt@artemisbot.uk]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError.mjs";
import notepack from "notepack.io";
/**
* To MessagePack operation
*/
class ToMessagePack extends Operation {
/**
* ToMessagePack constructor
*/
constructor() {
super();
this.name = "To MessagePack";
this.module = "Code";
this.description = "Converts JSON to MessagePack encoded byte buffer";
this.inputType = "JSON";
this.outputType = "ArrayBuffer";
this.args = [];
}
/**
* @param {JSON} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
try {
return notepack.encode(input);
} catch (err) {
throw new OperationError(`Could not encode JSON to MessagePack: ${err}`);
}
}
}
export default ToMessagePack;