Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update getPath to use WHATWG URL API #28354

Merged
merged 9 commits into from
Nov 20, 2023
4 changes: 3 additions & 1 deletion packages/network/lib/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export function addDefaultPort (urlToCheck: any) {
}

export function getPath (urlToCheck: string) {
return url.parse(urlToCheck).path
const url = new URL(urlToCheck)
mschile marked this conversation as resolved.
Show resolved Hide resolved

return `${url.pathname}${url.search}`
}

const localhostIPRegex = /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
Expand Down
14 changes: 14 additions & 0 deletions packages/network/test/unit/uri_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { URL } from 'url'
import { uri } from '../../lib'

describe('lib/uri', () => {
context('.getPath', () => {
it('returns the pathname and search', () => {
expect(uri.getPath('http://localhost:9999/foo/bar?baz=quux#/index.html')).to.eq('/foo/bar?baz=quux')
})

it('supports encoded characters', () => {
expect(uri.getPath('http://localhost:9999?foo=0%3C1')).to.eq('/?foo=0%3C1')
})

it('does not encode the "|" character', () => {
expect(uri.getPath('http://localhost:9999?foo=bar|baz')).to.eq('/?foo=bar|baz')
})
})

context('.isLocalhost', () => {
it('http://localhost is localhost', () => {
expect(uri.isLocalhost(new URL('http://localhost'))).to.be.true
Expand Down
Loading