Skip to content

Commit

Permalink
Merge pull request #1 from Rookout/use-urijs
Browse files Browse the repository at this point in the history
use urijs to align URL parsing between Node and Chrome
  • Loading branch information
ElDuderinos authored Oct 25, 2022
2 parents c53b17e + 01b06a7 commit d12af22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
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

0 comments on commit d12af22

Please sign in to comment.