Skip to content

Commit 209fc07

Browse files
committed
Issue 991: Add CBOR Decode operation
1 parent ae70cb8 commit 209fc07

File tree

4 files changed

+190
-1
lines changed

4 files changed

+190
-1
lines changed

src/core/config/Categories.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"CSV to JSON",
6363
"JSON to CSV",
6464
"Avro to JSON",
65-
"CBOR Encode"
65+
"CBOR Encode",
66+
"CBOR Decode"
6667
]
6768
},
6869
{

src/core/operations/CBORDecode.mjs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author Danh4 [[email protected]]
3+
* @copyright Crown Copyright 2020
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Cbor from "cbor";
9+
10+
/**
11+
* CBOR Decode operation
12+
*/
13+
class CBORDecode extends Operation {
14+
15+
/**
16+
* CBORDecode constructor
17+
*/
18+
constructor() {
19+
super();
20+
21+
this.name = "CBOR Decode";
22+
this.module = "Default";
23+
this.description = "Decode Concise Binary Object Representation (CBOR) data format (RFC7049) to JSON.";
24+
this.infoURL = "https://cbor.io";
25+
this.inputType = "ArrayBuffer";
26+
this.outputType = "JSON";
27+
this.args = [
28+
];
29+
}
30+
31+
/**
32+
* @param {ArrayBuffer} input
33+
* @param {Object[]} args
34+
* @returns {JSON}
35+
*/
36+
run(input, args) {
37+
return Cbor.decodeFirstSync(Buffer.from(input).toString("hex"));
38+
}
39+
40+
}
41+
42+
export default CBORDecode;

tests/operations/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import "./tests/CipherSaber2.mjs";
102102
import "./tests/Colossus.mjs";
103103
import "./tests/ParseObjectIDTimestamp.mjs";
104104
import "./tests/CBOREncode.mjs";
105+
import "./tests/CBORDecode.mjs";
105106

106107

107108
// Cannot test operations that use the File type yet

tests/operations/tests/CBORDecode.mjs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* From Hex Tests.
3+
*
4+
* @author Danh4 [[email protected]]
5+
*
6+
* @copyright Crown Copyright 2019
7+
* @license Apache-2.0
8+
*/
9+
10+
import TestRegister from "../../lib/TestRegister.mjs";
11+
12+
TestRegister.addTests([
13+
{
14+
name: "CBOR Decode: Can decode integer",
15+
input: "0f",
16+
expectedOutput: "15",
17+
recipeConfig: [
18+
{
19+
op: "From Hex",
20+
args: []
21+
},
22+
{
23+
op: "CBOR Decode",
24+
args: []
25+
}
26+
]
27+
},
28+
{
29+
name: "CBOR Decode: Can decode decimal",
30+
input: "f9 3e 00",
31+
expectedOutput: "1.5",
32+
recipeConfig: [
33+
{
34+
op: "From Hex",
35+
args: []
36+
},
37+
{
38+
op: "CBOR Decode",
39+
args: []
40+
}
41+
]
42+
},
43+
{
44+
name: "From Hex: Can decode text",
45+
input: "64 54 65 78 74",
46+
expectedOutput: "\"Text\"",
47+
recipeConfig: [
48+
{
49+
op: "From Hex",
50+
args: []
51+
},
52+
{
53+
op: "CBOR Decode",
54+
args: []
55+
}
56+
]
57+
},
58+
{
59+
name: "From Hex: Can decode boolean true",
60+
input: "f5",
61+
expectedOutput: "true",
62+
recipeConfig: [
63+
{
64+
op: "From Hex",
65+
args: []
66+
},
67+
{
68+
op: "CBOR Decode",
69+
args: []
70+
}
71+
]
72+
},
73+
{
74+
name: "From Hex: Can decode boolean false",
75+
input: "f4",
76+
expectedOutput: "false",
77+
recipeConfig: [
78+
{
79+
op: "From Hex",
80+
args: []
81+
},
82+
{
83+
op: "CBOR Decode",
84+
args: []
85+
}
86+
]
87+
},
88+
{
89+
name: "From Hex: Can decode map",
90+
input: "a3 61 61 01 61 62 02 61 63 03",
91+
expectedOutput: JSON.stringify({a: 1, b: 2, c: 3}),
92+
recipeConfig: [
93+
{
94+
op: "From Hex",
95+
args: []
96+
},
97+
{
98+
op: "CBOR Decode",
99+
args: []
100+
},
101+
{
102+
op: "JSON Minify",
103+
args: []
104+
}
105+
]
106+
},
107+
{
108+
name: "From Hex: Can decode list",
109+
input: "83 00 01 02",
110+
expectedOutput: "[0,1,2]",
111+
recipeConfig: [
112+
{
113+
op: "From Hex",
114+
args: []
115+
},
116+
{
117+
op: "CBOR Decode",
118+
args: []
119+
},
120+
{
121+
op: "JSON Minify",
122+
args: []
123+
}
124+
]
125+
},
126+
{
127+
name: "From Hex: Can round trip with encode",
128+
input: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}),
129+
expectedOutput: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}),
130+
recipeConfig: [
131+
{
132+
op: "CBOR Encode",
133+
args: []
134+
},
135+
{
136+
op: "CBOR Decode",
137+
args: []
138+
},
139+
{
140+
op: "JSON Minify",
141+
args: []
142+
}
143+
]
144+
}
145+
]);

0 commit comments

Comments
 (0)