protodef
Version:
A simple yet powerful way to define binary protocols
61 lines (53 loc) • 1.46 kB
JavaScript
const ProtoDef = require("protodef").ProtoDef;
const proto = new ProtoDef();
const testData=[
{
"kind":"conditional",
"data":require("../../ProtoDef/test/conditional.json")
},
{
"kind":"numeric",
"data":require("../../ProtoDef/test/numeric.json")
},
{
"kind":"structures",
"data":require("../../ProtoDef/test/structures.json")
},
{
"kind":"utils",
"data":require("../../ProtoDef/test/utils.json")
}
];
function arrayToBuffer(arr)
{
return new Buffer(arr.map(e => parseInt(e)));
}
function transformValues(type,values)
{
return values.map(value => ({
buffer: arrayToBuffer(value.buffer),
value: type.indexOf("buffer") == 0 ? arrayToBuffer(value.value) : value.value,
description: value.description
}));
}
testData.forEach(tests => {
tests.originalData=JSON.parse(JSON.stringify(tests.data));
tests.data.forEach(test => {
const subTypes = [];
if (test.subtypes)
test.subtypes.forEach((subtype, i) => {
const type = test.type + "_" + i;
proto.addType(type, subtype.type);
subtype.values = transformValues(test.type, subtype.values);
subtype.type = type;
subTypes.push(subtype);
});
else {
test.values = transformValues(test.type, test.values);
subTypes.push({type: test.type, values: test.values});
}
test.subtypes=subTypes;
});
});
module.exports.testData=testData;
module.exports.proto=proto;