File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,28 @@ exports.getCodec = (prefixedData) => {
65
65
return codecName
66
66
}
67
67
68
+ /**
69
+ * Get the name of the codec.
70
+ * @param {number } codec
71
+ * @returns {string }
72
+ */
73
+ exports . getName = ( codec ) => {
74
+ return codeToCodecName [ codec . toString ( 16 ) ]
75
+ }
76
+
77
+ /**
78
+ * Get the code of the codec
79
+ * @param {string } name
80
+ * @returns {number }
81
+ */
82
+ exports . getNumber = ( name ) => {
83
+ const code = codecNameToCodeVarint [ name ]
84
+ if ( code === undefined ) {
85
+ throw new Error ( 'Codec `' + name + '` not found' )
86
+ }
87
+ return util . varintBufferDecode ( code ) [ 0 ]
88
+ }
89
+
68
90
/**
69
91
* Get the code of the prefixed data.
70
92
* @param {Buffer } prefixedData
Original file line number Diff line number Diff line change @@ -46,6 +46,16 @@ describe('multicodec', () => {
46
46
expect ( code ) . to . eql ( [ 0x1b ] )
47
47
} )
48
48
49
+ it ( 'returns the codec name from code' , ( ) => {
50
+ expect ( multicodec . getName ( 144 ) ) . to . eql ( 'eth-block' )
51
+ expect ( multicodec . getName ( 112 ) ) . to . eql ( 'dag-pb' )
52
+ } )
53
+
54
+ it ( 'returns the codec number from name' , ( ) => {
55
+ expect ( multicodec . getNumber ( 'eth-block' ) ) . to . eql ( 144 )
56
+ expect ( multicodec . getNumber ( 'dag-pb' ) ) . to . eql ( 112 )
57
+ } )
58
+
49
59
it ( 'throws error on unknown codec name when getting the code' , ( ) => {
50
60
expect ( ( ) => {
51
61
multicodec . getCodeVarint ( 'this-codec-doesnt-exist' )
You can’t perform that action at this time.
0 commit comments