@@ -46,20 +46,35 @@ const ASSUME_HTTP_CODES = [
46
46
47
47
interface Interpreter { ( value : string , ma : StringTuple [ ] ) : string }
48
48
49
- function extractSNI ( ma : StringTuple [ ] ) : string | null {
50
- let sniProtoCode : number
49
+ function extractSNI ( ma : StringTuple [ ] ) : string | undefined {
50
+ return extractTuple ( 'sni' , ma ) ?. [ 1 ]
51
+ }
52
+
53
+ function extractPort ( ma : StringTuple [ ] ) : string {
54
+ const port = extractTuple ( 'tcp' , ma ) ?. [ 1 ]
55
+
56
+ if ( port == null ) {
57
+ return ''
58
+ }
59
+
60
+ return `:${ port } `
61
+ }
62
+
63
+ function extractTuple ( name : string , ma : StringTuple [ ] ) : StringTuple | undefined {
64
+ let code : number
65
+
51
66
try {
52
- sniProtoCode = protocols ( 'sni' ) . code
67
+ code = protocols ( name ) . code
53
68
} catch ( e ) {
54
- // No SNI protocol in multiaddr
55
- return null
69
+ // No support for protocol in multiaddr
70
+ return
56
71
}
72
+
57
73
for ( const [ proto , value ] of ma ) {
58
- if ( proto === sniProtoCode && value !== undefined ) {
59
- return value
74
+ if ( proto === code && value != null ) {
75
+ return [ proto , value ]
60
76
}
61
77
}
62
- return null
63
78
}
64
79
65
80
function hasTLS ( ma : StringTuple [ ] ) : boolean {
@@ -68,7 +83,7 @@ function hasTLS (ma: StringTuple[]): boolean {
68
83
69
84
function interpretNext ( headProtoCode : number , headProtoVal : string , restMa : StringTuple [ ] ) : string {
70
85
const interpreter = interpreters [ protocols ( headProtoCode ) . name ]
71
- if ( interpreter === undefined ) {
86
+ if ( interpreter == null ) {
72
87
throw new Error ( `Can't interpret protocol ${ protocols ( headProtoCode ) . name } ` )
73
88
}
74
89
const restVal = interpreter ( headProtoVal , restMa )
@@ -88,14 +103,14 @@ const interpreters: Record<string, Interpreter> = {
88
103
} ,
89
104
tcp : ( value : string , restMa : StringTuple [ ] ) => {
90
105
const tailProto = restMa . pop ( )
91
- if ( tailProto === undefined ) {
106
+ if ( tailProto == null ) {
92
107
throw new Error ( 'Unexpected end of multiaddr' )
93
108
}
94
109
return `tcp://${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } :${ value } `
95
110
} ,
96
111
udp : ( value : string , restMa : StringTuple [ ] ) => {
97
112
const tailProto = restMa . pop ( )
98
- if ( tailProto === undefined ) {
113
+ if ( tailProto == null ) {
99
114
throw new Error ( 'Unexpected end of multiaddr' )
100
115
}
101
116
return `udp://${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } :${ value } `
@@ -106,27 +121,28 @@ const interpreters: Record<string, Interpreter> = {
106
121
dns : ( value : string , restMa : StringTuple [ ] ) => value ,
107
122
ipfs : ( value : string , restMa : StringTuple [ ] ) => {
108
123
const tailProto = restMa . pop ( )
109
- if ( tailProto === undefined ) {
124
+ if ( tailProto == null ) {
110
125
throw new Error ( 'Unexpected end of multiaddr' )
111
126
}
112
127
return `${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } /ipfs/${ value } `
113
128
} ,
114
129
p2p : ( value : string , restMa : StringTuple [ ] ) => {
115
130
const tailProto = restMa . pop ( )
116
- if ( tailProto === undefined ) {
131
+ if ( tailProto == null ) {
117
132
throw new Error ( 'Unexpected end of multiaddr' )
118
133
}
119
134
return `${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } /p2p/${ value } `
120
135
} ,
121
136
http : ( value : string , restMa : StringTuple [ ] ) => {
122
137
const maHasTLS = hasTLS ( restMa )
123
138
const sni = extractSNI ( restMa )
124
- if ( maHasTLS && sni !== null ) {
125
- return `https://${ sni } `
139
+ const port = extractPort ( restMa )
140
+ if ( maHasTLS && sni != null ) {
141
+ return `https://${ sni } ${ port } `
126
142
}
127
143
const protocol = maHasTLS ? 'https://' : 'http://'
128
144
const tailProto = restMa . pop ( )
129
- if ( tailProto === undefined ) {
145
+ if ( tailProto == null ) {
130
146
throw new Error ( 'Unexpected end of multiaddr' )
131
147
}
132
148
let baseVal = interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -136,7 +152,7 @@ const interpreters: Record<string, Interpreter> = {
136
152
} ,
137
153
'http-path' : ( value : string , restMa : StringTuple [ ] ) => {
138
154
const tailProto = restMa . pop ( )
139
- if ( tailProto === undefined ) {
155
+ if ( tailProto == null ) {
140
156
throw new Error ( 'Unexpected end of multiaddr' )
141
157
}
142
158
const baseVal = interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -147,7 +163,7 @@ const interpreters: Record<string, Interpreter> = {
147
163
// Noop, the parent context knows that it's tls. We don't need to do
148
164
// anything here
149
165
const tailProto = restMa . pop ( )
150
- if ( tailProto === undefined ) {
166
+ if ( tailProto == null ) {
151
167
throw new Error ( 'Unexpected end of multiaddr' )
152
168
}
153
169
return interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -156,14 +172,14 @@ const interpreters: Record<string, Interpreter> = {
156
172
// Noop, the parent context uses the sni information, we don't need to do
157
173
// anything here
158
174
const tailProto = restMa . pop ( )
159
- if ( tailProto === undefined ) {
175
+ if ( tailProto == null ) {
160
176
throw new Error ( 'Unexpected end of multiaddr' )
161
177
}
162
178
return interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
163
179
} ,
164
180
https : ( value : string , restMa : StringTuple [ ] ) => {
165
181
const tailProto = restMa . pop ( )
166
- if ( tailProto === undefined ) {
182
+ if ( tailProto == null ) {
167
183
throw new Error ( 'Unexpected end of multiaddr' )
168
184
}
169
185
let baseVal = interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -174,12 +190,13 @@ const interpreters: Record<string, Interpreter> = {
174
190
ws : ( value : string , restMa : StringTuple [ ] ) => {
175
191
const maHasTLS = hasTLS ( restMa )
176
192
const sni = extractSNI ( restMa )
177
- if ( maHasTLS && sni !== null ) {
178
- return `wss://${ sni } `
193
+ const port = extractPort ( restMa )
194
+ if ( maHasTLS && sni != null ) {
195
+ return `wss://${ sni } ${ port } `
179
196
}
180
197
const protocol = maHasTLS ? 'wss://' : 'ws://'
181
198
const tailProto = restMa . pop ( )
182
- if ( tailProto === undefined ) {
199
+ if ( tailProto == null ) {
183
200
throw new Error ( 'Unexpected end of multiaddr' )
184
201
}
185
202
let baseVal = interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -189,7 +206,7 @@ const interpreters: Record<string, Interpreter> = {
189
206
} ,
190
207
wss : ( value : string , restMa : StringTuple [ ] ) => {
191
208
const tailProto = restMa . pop ( )
192
- if ( tailProto === undefined ) {
209
+ if ( tailProto == null ) {
193
210
throw new Error ( 'Unexpected end of multiaddr' )
194
211
}
195
212
let baseVal = interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa )
@@ -199,21 +216,21 @@ const interpreters: Record<string, Interpreter> = {
199
216
} ,
200
217
'p2p-websocket-star' : ( value : string , restMa : StringTuple [ ] ) => {
201
218
const tailProto = restMa . pop ( )
202
- if ( tailProto === undefined ) {
219
+ if ( tailProto == null ) {
203
220
throw new Error ( 'Unexpected end of multiaddr' )
204
221
}
205
222
return `${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } /p2p-websocket-star`
206
223
} ,
207
224
'p2p-webrtc-star' : ( value : string , restMa : StringTuple [ ] ) => {
208
225
const tailProto = restMa . pop ( )
209
- if ( tailProto === undefined ) {
226
+ if ( tailProto == null ) {
210
227
throw new Error ( 'Unexpected end of multiaddr' )
211
228
}
212
229
return `${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } /p2p-webrtc-star`
213
230
} ,
214
231
'p2p-webrtc-direct' : ( value : string , restMa : StringTuple [ ] ) => {
215
232
const tailProto = restMa . pop ( )
216
- if ( tailProto === undefined ) {
233
+ if ( tailProto == null ) {
217
234
throw new Error ( 'Unexpected end of multiaddr' )
218
235
}
219
236
return `${ interpretNext ( tailProto [ 0 ] , tailProto [ 1 ] ?? '' , restMa ) } /p2p-webrtc-direct`
@@ -224,7 +241,7 @@ export function multiaddrToUri (input: Multiaddr | string | Uint8Array, opts?: M
224
241
const ma = multiaddr ( input )
225
242
const parts = ma . stringTuples ( )
226
243
const head = parts . pop ( )
227
- if ( head === undefined ) {
244
+ if ( head == null ) {
228
245
throw new Error ( 'Unexpected end of multiaddr' )
229
246
}
230
247
@@ -248,7 +265,7 @@ export function multiaddrToUri (input: Multiaddr | string | Uint8Array, opts?: M
248
265
}
249
266
}
250
267
251
- if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) {
268
+ if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) || uri . startsWith ( 'ws://' ) || uri . startsWith ( 'wss://' ) ) {
252
269
// this will strip default ports while keeping paths intact
253
270
uri = new URL ( uri ) . toString ( )
254
271
0 commit comments