@@ -12,6 +12,7 @@ const mh = require('../src')
1212const constants = require ( '../src/constants' )
1313const validCases = require ( './fixtures/valid' )
1414const invalidCases = require ( './fixtures/invalid' )
15+ const { TextEncoder } = require ( 'web-encoding' )
1516
1617function sample ( code , size , hex ) {
1718 const toHex = ( i ) => {
@@ -24,12 +25,28 @@ function sample (code, size, hex) {
2425 return Buffer . from ( `${ toHex ( code ) } ${ toHex ( size ) } ${ hex } ` , 'hex' )
2526}
2627
28+ const they = ( description , test ) => {
29+ it ( `${ description } (Buffer)` , ( ) => test ( {
30+ encodeText : Buffer . from ,
31+ encodeHex : ( text ) => Buffer . from ( text , 'hex' )
32+ } ) )
33+
34+ const textEncoder = new TextEncoder ( )
35+ it ( `${ description } (Uint8Array)` , ( ) => test ( {
36+ encodeText : ( text ) => textEncoder . encode ( text ) ,
37+ encodeHex : ( text ) => {
38+ const { buffer, byteOffset, byteLength } = Buffer . from ( text , 'hex' )
39+ return new Uint8Array ( buffer , byteOffset , byteLength )
40+ }
41+ } ) )
42+ }
43+
2744describe ( 'multihash' , ( ) => {
2845 describe ( 'toHexString' , ( ) => {
29- it ( 'valid' , ( ) => {
46+ they ( 'valid' , ( { encodeHex } ) => {
3047 validCases . forEach ( ( test ) => {
3148 const code = test . encoding . code
32- const buf = mh . encode ( Buffer . from ( test . hex , 'hex' ) , code )
49+ const buf = mh . encode ( encodeHex ( test . hex ) , code )
3350 expect (
3451 mh . toHexString ( buf )
3552 ) . to . be . eql (
@@ -42,16 +59,16 @@ describe('multihash', () => {
4259 expect (
4360 ( ) => mh . toHexString ( 'hello world' )
4461 ) . to . throw (
45- / m u s t b e p a s s e d a b u f f e r /
62+ / m u s t b e p a s s e d a U i n t 8 A r r a y /
4663 )
4764 } )
4865 } )
4966
5067 describe ( 'fromHexString' , ( ) => {
51- it ( 'valid' , ( ) => {
68+ they ( 'valid' , ( { encodeHex } ) => {
5269 validCases . forEach ( ( test ) => {
5370 const code = test . encoding . code
54- const buf = mh . encode ( Buffer . from ( test . hex , 'hex' ) , code )
71+ const buf = mh . encode ( encodeHex ( test . hex ) , code )
5572 expect (
5673 mh . fromHexString ( buf . toString ( 'hex' ) ) . toString ( 'hex' )
5774 ) . to . be . eql (
@@ -62,10 +79,10 @@ describe('multihash', () => {
6279 } )
6380
6481 describe ( 'toB58String' , ( ) => {
65- it ( 'valid' , ( ) => {
82+ they ( 'valid' , ( { encodeHex } ) => {
6683 validCases . forEach ( ( test ) => {
6784 const code = test . encoding . code
68- const buf = mh . encode ( Buffer . from ( test . hex , 'hex' ) , code )
85+ const buf = mh . encode ( encodeHex ( test . hex ) , code )
6986 expect (
7087 mh . toB58String ( buf )
7188 ) . to . be . eql (
@@ -78,15 +95,15 @@ describe('multihash', () => {
7895 expect (
7996 ( ) => mh . toB58String ( 'hello world' )
8097 ) . to . throw (
81- / m u s t b e p a s s e d a b u f f e r /
98+ / m u s t b e p a s s e d a U i n t 8 A r r a y /
8299 )
83100 } )
84101 } )
85102
86103 describe ( 'fromB58String' , ( ) => {
87- it ( 'valid' , ( ) => {
104+ they ( 'valid' , ( { encodeHex , encodeText } ) => {
88105 const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
89- const expected = Buffer . from ( '122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461' , 'hex ')
106+ const expected = encodeHex ( '122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461' )
90107
91108 expect (
92109 mh . fromB58String ( src )
@@ -95,7 +112,7 @@ describe('multihash', () => {
95112 )
96113
97114 expect (
98- mh . fromB58String ( Buffer . from ( src ) )
115+ mh . fromB58String ( encodeText ( src ) )
99116 ) . to . be . eql (
100117 expected
101118 )
@@ -125,20 +142,20 @@ describe('multihash', () => {
125142 expect (
126143 ( ) => mh . decode ( 'hello' )
127144 ) . to . throw (
128- / m u l t i h a s h m u s t b e a B u f f e r /
145+ / m u l t i h a s h m u s t b e a U i n t 8 A r r a y /
129146 )
130147 } )
131148 } )
132149
133150 describe ( 'encode' , ( ) => {
134- it ( 'valid' , ( ) => {
151+ they ( 'valid' , ( { encodeHex } ) => {
135152 validCases . forEach ( ( test ) => {
136153 const code = test . encoding . code
137154 const name = test . encoding . name
138155 const buf = sample ( test . encoding . varint || code , test . size , test . hex )
139156 const results = [
140- mh . encode ( Buffer . from ( test . hex , 'hex' ) , code ) ,
141- mh . encode ( Buffer . from ( test . hex , 'hex' ) , name )
157+ mh . encode ( encodeHex ( test . hex ) , code ) ,
158+ mh . encode ( encodeHex ( test . hex ) , name )
142159 ]
143160
144161 results . forEach ( ( res ) => {
@@ -151,7 +168,7 @@ describe('multihash', () => {
151168 } )
152169 } )
153170
154- it ( 'invalid' , ( ) => {
171+ they ( 'invalid' , ( { encodeText } ) => {
155172 expect (
156173 ( ) => mh . encode ( )
157174 ) . to . throw (
@@ -161,11 +178,11 @@ describe('multihash', () => {
161178 expect (
162179 ( ) => mh . encode ( 'hello' , 0x11 )
163180 ) . to . throw (
164- / d i g e s t s h o u l d b e a B u f f e r /
181+ / d i g e s t s h o u l d b e a U i n t 8 A r r a y /
165182 )
166183
167184 expect (
168- ( ) => mh . encode ( Buffer . from ( 'hello' ) , 0x11 , 2 )
185+ ( ) => mh . encode ( encodeText ( 'hello' ) , 0x11 , 2 )
169186 ) . to . throw (
170187 / l e n g t h s h o u l d b e e q u a l /
171188 )
@@ -188,7 +205,7 @@ describe('multihash', () => {
188205 ) . to . throw ( )
189206 } )
190207
191- const longBuffer = Buffer . alloc ( 150 , 'a' )
208+ const longBuffer = Uint8Array . from ( Buffer . alloc ( 150 , 'a' ) )
192209 expect (
193210 ( ) => mh . validate ( longBuffer )
194211 ) . to . throw ( )
@@ -277,7 +294,7 @@ describe('multihash', () => {
277294 } )
278295 } )
279296
280- it ( 'invalid' , ( ) => {
297+ they ( 'invalid' , ( { encodeText } ) => {
281298 const invalidNames = [
282299 'sha256' ,
283300 'sha9' ,
@@ -293,7 +310,7 @@ describe('multihash', () => {
293310 } )
294311
295312 expect (
296- ( ) => mh . coerceCode ( Buffer . from ( 'hello' ) )
313+ ( ) => mh . coerceCode ( encodeText ( 'hello' ) )
297314 ) . to . throw (
298315 / s h o u l d b e a n u m b e r /
299316 )
@@ -306,14 +323,14 @@ describe('multihash', () => {
306323 } )
307324 } )
308325
309- it ( 'prefix' , ( ) => {
310- const multihash = mh . encode ( Buffer . from ( 'hey' ) , 0x11 , 3 )
326+ they ( 'prefix' , ( { encodeText } ) => {
327+ const multihash = mh . encode ( encodeText ( 'hey' ) , 0x11 , 3 )
311328 const prefix = mh . prefix ( multihash )
312329 expect ( prefix . toString ( 'hex' ) ) . to . eql ( '1103' )
313330 } )
314331
315- it ( 'prefix throws on invalid multihash' , ( ) => {
316- const multihash = Buffer . from ( 'definitely not valid' )
332+ they ( 'prefix throws on invalid multihash' , ( { encodeText } ) => {
333+ const multihash = encodeText ( 'definitely not valid' )
317334
318335 expect ( ( ) => mh . prefix ( multihash ) ) . to . throw ( )
319336 } )
0 commit comments