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

use urijs to align URL parsing between Node and Chrome #1

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const protocols = require("protocols")
var URI = require('urijs');

/**
* parsePath
Expand Down Expand Up @@ -41,21 +41,24 @@ function parsePath(url) {
, parse_failed: false
}

try {
const parsed = new URL(url)
output.protocols = protocols(parsed)
try {
const parsed = URI(url.trim())
output.protocols = parsed.protocol().split(/\:|\+/).filter(Boolean)
if (output.protocols.length === 0){
throw new TypeError("Invalid URL " + url)
}
output.protocol = output.protocols[0]
output.port = parsed.port
output.resource = parsed.hostname
output.host = parsed.host
output.user = parsed.username || ""
output.password = parsed.password || ""
output.pathname = parsed.pathname
output.hash = parsed.hash.slice(1)
output.search = parsed.search.slice(1)
output.href = parsed.href
output.query = Object.fromEntries(parsed.searchParams)
} catch (e) {
output.port = parsed.port()
output.resource = parsed.hostname()
output.host = parsed.host()
output.user = parsed.username() || ""
output.password = parsed.password() || ""
output.pathname = parsed.pathname()
output.hash = parsed.hash()?.slice(1) || ""
output.search = parsed.search()?.slice(1) || ""
output.href = parsed.href()
output.query = URI.parseQuery(output.search)
} catch (e) {
// TODO Maybe check if it is a valid local file path
// In any case, these will be parsed by higher
// level parsers such as parse-url, git-url-parse, git-up
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tester": "^1.4.5"
},
"dependencies": {
"protocols": "^2.0.0"
"urijs": "^1.19.11"
},
"files": [
"bin/",
Expand All @@ -49,4 +49,4 @@
"bloggify.json",
"bloggify/"
]
}
}
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ const INPUTS = [
, resource: "%0aalert(1)"
, host: "%0aalert(1)"
, user: ""
, pathname: ""
, pathname: "/"
, hash: ""
, href: "javascript://%0aalert(1)"
, href: "javascript://%0aalert(1)/"
, query: {}
, parse_failed: false
, search: ""
Expand Down