-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 991: Add CBOR Encode operation
- Loading branch information
Showing
6 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,8 @@ | |
"Parse TLV", | ||
"CSV to JSON", | ||
"JSON to CSV", | ||
"Avro to JSON" | ||
"Avro to JSON", | ||
"CBOR Encode" | ||
] | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @author Danh4 [dan.h4@ncsc.gov.uk] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import Cbor from "cbor"; | ||
|
||
/** | ||
* CBOR Encode operation | ||
*/ | ||
class CBOREncode extends Operation { | ||
|
||
/** | ||
* CBOREncode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "CBOR Encode"; | ||
this.module = "Default"; | ||
this.description = "2"; | ||
this.infoURL = "https://cbor.io"; | ||
this.inputType = "JSON"; | ||
this.outputType = "ArrayBuffer"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {JSON} input | ||
* @param {Object[]} args | ||
* @returns {ArrayBuffer} | ||
*/ | ||
run(input, args) { | ||
return new Uint8Array(Cbor.encodeCanonical(input)).buffer; | ||
} | ||
|
||
} | ||
|
||
export default CBOREncode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* CBOR Encode Tests. | ||
* | ||
* @author Danh4 [dan.h4@ncsc.gov.uk] | ||
* | ||
* @copyright Crown Copyright 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "CBOR Encode: Can encode integer", | ||
input: "15", | ||
expectedOutput: "0f", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Decode: Can encode decimal", | ||
input: "1.5", | ||
expectedOutput: "f9 3e 00", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Encode: Can encode text", | ||
input: "\"Text\"", | ||
expectedOutput: "64 54 65 78 74", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Encode: Can encode boolean true", | ||
input: "true", | ||
expectedOutput: "f5", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Encode: Can encode boolean false", | ||
input: "false", | ||
expectedOutput: "f4", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Encode: Can encode map", | ||
input: JSON.stringify({a: 1, b: 2, c: 3}), | ||
expectedOutput: "a3 61 61 01 61 62 02 61 63 03", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "CBOR Encode: Can encode list", | ||
input: "[0,1,2]", | ||
expectedOutput: "83 00 01 02", | ||
recipeConfig: [ | ||
{ | ||
op: "CBOR Encode", | ||
args: [] | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: [] | ||
} | ||
] | ||
} | ||
]); |