From 2d0bb6615ecb8f9ef1019bc0737aab7f6449641f Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 26 Feb 2020 14:30:20 -0800 Subject: [PATCH] fix: Do not attempt to use url.URL when unavailable Fix #61 This should not be ported to the latest branch, as Node.js v6 support was dropped there anyway. PR-URL: https://github.com/npm/hosted-git-info/pull/62 Credit: @isaacs Close: #62 Reviewed-by: @isaacs --- index.js | 4 +++- test/auth.js | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b57880c..08fa329 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/auth.js b/test/auth.js index 0e5c752..8393ab8 100644 --- a/test/auth.js +++ b/test/auth.js @@ -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')