Skip to content

Commit

Permalink
fix: Do not attempt to use url.URL when unavailable
Browse files Browse the repository at this point in the history
Fix #61

This should not be ported to the latest branch, as Node.js v6 support
was dropped there anyway.

PR-URL: #62
Credit: @isaacs
Close: #62
Reviewed-by: @isaacs
  • Loading branch information
isaacs committed Feb 26, 2020
1 parent f2cdfcf commit 2d0bb66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ function parseGitUrl (giturl) {
var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
if (!matched) {
var legacy = url.parse(giturl)
if (legacy.auth) {
// If we don't have url.URL, then sorry, this is just not fixable.
// This affects Node <= 6.12.
if (legacy.auth && typeof url.URL === 'function') {
// git urls can be in the form of scp-style/ssh-connect strings, like
// git+ssh://user@host.com:some/path, which the legacy url parser
// supports, but WhatWG url.URL class does not. However, the legacy
Expand Down
6 changes: 6 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ tap.equal(parsedUrl.hostname, 'github.com')
// For full backwards-compatibility; support auth where only username or only password is provided
tap.equal(HostedGitInfo.fromUrl('https://user%3An%40me@github.com/npm/hosted-git-info.git').auth, 'user%3An%40me')
tap.equal(HostedGitInfo.fromUrl('https://:p%40ss%3Aword@github.com/npm/hosted-git-info.git').auth, ':p%40ss%3Aword')

// don't try to url.URL parse it if url.URL is not available
// ie, node <6.13. This is broken, but at least it doesn't throw.
url.URL = null
var parsedInfoNoURL = HostedGitInfo.fromUrl('https://user%3An%40me:p%40ss%3Aword@github.com/npm/xyz.git')
tap.equal(parsedInfoNoURL.auth, 'user:n@me:p@ss:word')

0 comments on commit 2d0bb66

Please sign in to comment.