@@ -12,6 +12,7 @@ const mh = require('../src')
12
12
const constants = require ( '../src/constants' )
13
13
const validCases = require ( './fixtures/valid' )
14
14
const invalidCases = require ( './fixtures/invalid' )
15
+ const { TextEncoder } = require ( 'web-encoding' )
15
16
16
17
function sample ( code , size , hex ) {
17
18
const toHex = ( i ) => {
@@ -24,12 +25,28 @@ function sample (code, size, hex) {
24
25
return Buffer . from ( `${ toHex ( code ) } ${ toHex ( size ) } ${ hex } ` , 'hex' )
25
26
}
26
27
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
+
27
44
describe ( 'multihash' , ( ) => {
28
45
describe ( 'toHexString' , ( ) => {
29
- it ( 'valid' , ( ) => {
46
+ they ( 'valid' , ( { encodeHex } ) => {
30
47
validCases . forEach ( ( test ) => {
31
48
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 )
33
50
expect (
34
51
mh . toHexString ( buf )
35
52
) . to . be . eql (
@@ -42,16 +59,16 @@ describe('multihash', () => {
42
59
expect (
43
60
( ) => mh . toHexString ( 'hello world' )
44
61
) . 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 /
46
63
)
47
64
} )
48
65
} )
49
66
50
67
describe ( 'fromHexString' , ( ) => {
51
- it ( 'valid' , ( ) => {
68
+ they ( 'valid' , ( { encodeHex } ) => {
52
69
validCases . forEach ( ( test ) => {
53
70
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 )
55
72
expect (
56
73
mh . fromHexString ( buf . toString ( 'hex' ) ) . toString ( 'hex' )
57
74
) . to . be . eql (
@@ -62,10 +79,10 @@ describe('multihash', () => {
62
79
} )
63
80
64
81
describe ( 'toB58String' , ( ) => {
65
- it ( 'valid' , ( ) => {
82
+ they ( 'valid' , ( { encodeHex } ) => {
66
83
validCases . forEach ( ( test ) => {
67
84
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 )
69
86
expect (
70
87
mh . toB58String ( buf )
71
88
) . to . be . eql (
@@ -78,15 +95,15 @@ describe('multihash', () => {
78
95
expect (
79
96
( ) => mh . toB58String ( 'hello world' )
80
97
) . 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 /
82
99
)
83
100
} )
84
101
} )
85
102
86
103
describe ( 'fromB58String' , ( ) => {
87
- it ( 'valid' , ( ) => {
104
+ they ( 'valid' , ( { encodeHex , encodeText } ) => {
88
105
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
89
- const expected = Buffer . from ( '122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461' , 'hex ')
106
+ const expected = encodeHex ( '122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461' )
90
107
91
108
expect (
92
109
mh . fromB58String ( src )
@@ -95,7 +112,7 @@ describe('multihash', () => {
95
112
)
96
113
97
114
expect (
98
- mh . fromB58String ( Buffer . from ( src ) )
115
+ mh . fromB58String ( encodeText ( src ) )
99
116
) . to . be . eql (
100
117
expected
101
118
)
@@ -125,20 +142,20 @@ describe('multihash', () => {
125
142
expect (
126
143
( ) => mh . decode ( 'hello' )
127
144
) . 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 /
129
146
)
130
147
} )
131
148
} )
132
149
133
150
describe ( 'encode' , ( ) => {
134
- it ( 'valid' , ( ) => {
151
+ they ( 'valid' , ( { encodeHex } ) => {
135
152
validCases . forEach ( ( test ) => {
136
153
const code = test . encoding . code
137
154
const name = test . encoding . name
138
155
const buf = sample ( test . encoding . varint || code , test . size , test . hex )
139
156
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 )
142
159
]
143
160
144
161
results . forEach ( ( res ) => {
@@ -151,7 +168,7 @@ describe('multihash', () => {
151
168
} )
152
169
} )
153
170
154
- it ( 'invalid' , ( ) => {
171
+ they ( 'invalid' , ( { encodeText } ) => {
155
172
expect (
156
173
( ) => mh . encode ( )
157
174
) . to . throw (
@@ -161,11 +178,11 @@ describe('multihash', () => {
161
178
expect (
162
179
( ) => mh . encode ( 'hello' , 0x11 )
163
180
) . 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 /
165
182
)
166
183
167
184
expect (
168
- ( ) => mh . encode ( Buffer . from ( 'hello' ) , 0x11 , 2 )
185
+ ( ) => mh . encode ( encodeText ( 'hello' ) , 0x11 , 2 )
169
186
) . to . throw (
170
187
/ l e n g t h s h o u l d b e e q u a l /
171
188
)
@@ -188,7 +205,7 @@ describe('multihash', () => {
188
205
) . to . throw ( )
189
206
} )
190
207
191
- const longBuffer = Buffer . alloc ( 150 , 'a' )
208
+ const longBuffer = Uint8Array . from ( Buffer . alloc ( 150 , 'a' ) )
192
209
expect (
193
210
( ) => mh . validate ( longBuffer )
194
211
) . to . throw ( )
@@ -277,7 +294,7 @@ describe('multihash', () => {
277
294
} )
278
295
} )
279
296
280
- it ( 'invalid' , ( ) => {
297
+ they ( 'invalid' , ( { encodeText } ) => {
281
298
const invalidNames = [
282
299
'sha256' ,
283
300
'sha9' ,
@@ -293,7 +310,7 @@ describe('multihash', () => {
293
310
} )
294
311
295
312
expect (
296
- ( ) => mh . coerceCode ( Buffer . from ( 'hello' ) )
313
+ ( ) => mh . coerceCode ( encodeText ( 'hello' ) )
297
314
) . to . throw (
298
315
/ s h o u l d b e a n u m b e r /
299
316
)
@@ -306,14 +323,14 @@ describe('multihash', () => {
306
323
} )
307
324
} )
308
325
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 )
311
328
const prefix = mh . prefix ( multihash )
312
329
expect ( prefix . toString ( 'hex' ) ) . to . eql ( '1103' )
313
330
} )
314
331
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' )
317
334
318
335
expect ( ( ) => mh . prefix ( multihash ) ) . to . throw ( )
319
336
} )
0 commit comments