File tree 2 files changed +26
-8
lines changed
2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -119,17 +119,14 @@ function dataURLProcessor (dataURL) {
119
119
* @param {boolean } excludeFragment
120
120
*/
121
121
function URLSerializer ( url , excludeFragment = false ) {
122
- const href = url . href
123
-
124
122
if ( ! excludeFragment ) {
125
- return href
123
+ return url . href
126
124
}
127
125
128
- const hash = href . lastIndexOf ( '#' )
129
- if ( hash === - 1 ) {
130
- return href
131
- }
132
- return href . slice ( 0 , hash )
126
+ const href = url . href
127
+ const hashLength = url . hash . length
128
+
129
+ return hashLength === 0 ? href : href . substring ( 0 , href . length - hashLength )
133
130
}
134
131
135
132
// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
Original file line number Diff line number Diff line change @@ -191,3 +191,24 @@ test('https://domain.com/?', (t) => {
191
191
const serialized = URLSerializer ( new URL ( domain ) )
192
192
t . equal ( serialized , domain )
193
193
} )
194
+
195
+ // https://github.com/nodejs/undici/issues/2474
196
+ test ( 'hash url' , ( t ) => {
197
+ t . plan ( 1 )
198
+ const domain = 'https://domain.com/#a#b'
199
+ const url = new URL ( domain )
200
+ const serialized = URLSerializer ( url , true )
201
+ t . equal ( serialized , url . href . substring ( 0 , url . href . length - url . hash . length ) )
202
+ } )
203
+
204
+ // https://github.com/nodejs/undici/issues/2474
205
+ test ( 'data url that includes the hash' , async ( t ) => {
206
+ t . plan ( 1 )
207
+ const dataURL = 'data:,node#js#'
208
+ try {
209
+ const res = await fetch ( dataURL )
210
+ t . equal ( await res . text ( ) , 'node' )
211
+ } catch ( error ) {
212
+ t . fail ( `failed to fetch ${ dataURL } ` )
213
+ }
214
+ } )
You can’t perform that action at this time.
0 commit comments