File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,15 @@ exports.getCodec = (prefixedData) => {
65
65
return codecName
66
66
}
67
67
68
+ /**
69
+ * Get the code of the prefixed data.
70
+ * @param {Buffer } prefixedData
71
+ * @returns {number }
72
+ */
73
+ exports . getCode = ( prefixedData ) => {
74
+ return varint . decode ( prefixedData )
75
+ }
76
+
68
77
/**
69
78
* Get the code as varint of a codec name.
70
79
* @param {string } codecName
@@ -78,6 +87,15 @@ exports.getCodeVarint = (codecName) => {
78
87
return code
79
88
}
80
89
90
+ /**
91
+ * Get the varint of a code.
92
+ * @param {Number } code
93
+ * @returns {Array.<number> }
94
+ */
95
+ exports . getVarint = ( code ) => {
96
+ return varint . encode ( code )
97
+ }
98
+
81
99
// Make the constants top-level constants
82
100
const constants = require ( './constants' )
83
101
Object . assign ( exports , constants )
Original file line number Diff line number Diff line change @@ -34,6 +34,18 @@ describe('multicodec', () => {
34
34
expect ( code ) . to . eql ( Buffer . from ( '1b' , 'hex' ) )
35
35
} )
36
36
37
+ it ( 'returns code from prefixed data' , ( ) => {
38
+ const buf = Buffer . from ( 'hey' )
39
+ const prefixedBuf = multicodec . addPrefix ( 'dag-cbor' , buf )
40
+ const code = multicodec . getCode ( prefixedBuf )
41
+ expect ( code ) . to . eql ( multicodec . DAG_CBOR )
42
+ } )
43
+
44
+ it ( 'returns varint from code' , ( ) => {
45
+ const code = multicodec . getVarint ( multicodec . KECCAK_256 )
46
+ expect ( code ) . to . eql ( [ 0x1b ] )
47
+ } )
48
+
37
49
it ( 'throws error on unknown codec name when getting the code' , ( ) => {
38
50
expect ( ( ) => {
39
51
multicodec . getCodeVarint ( 'this-codec-doesnt-exist' )
You can’t perform that action at this time.
0 commit comments