Skip to content

Commit 41c46ac

Browse files
committed
Use parseUrl
1 parent 223cd3f commit 41c46ac

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/shadow/npm/arborist-helpers.mts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { debugFn } from '@socketsecurity/registry/lib/debug'
22
import { getOwn } from '@socketsecurity/registry/lib/objects'
3+
import { parseUrl } from '@socketsecurity/registry/lib/url'
34

45
import constants from '../../constants.mts'
56
import { DiffAction } from './arborist/types.mts'
@@ -15,13 +16,10 @@ import type {
1516
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
1617

1718
function getUrlOrigin(input: string): string {
18-
try {
19-
// TODO: URL.parse is available in Node 22.1.0. We can use it when we drop Node 18.
20-
// https://nodejs.org/docs/latest-v22.x/api/url.html#urlparseinput-base
21-
// return URL.parse(input)?.origin ?? ''
22-
return new URL(input).origin ?? ''
23-
} catch {}
24-
return ''
19+
// TODO: URL.parse is available in Node 22.1.0. We can use it when we drop Node 18.
20+
// https://nodejs.org/docs/latest-v22.x/api/url.html#urlparseinput-base
21+
// return URL.parse(input)?.origin ?? ''
22+
return parseUrl(input)?.origin ?? ''
2523
}
2624

2725
export type GetAlertsMapFromArboristOptions = {

src/utils/github.mts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
writeJson,
1515
} from '@socketsecurity/registry/lib/fs'
1616
import { spawn } from '@socketsecurity/registry/lib/spawn'
17+
import { parseUrl } from '@socketsecurity/registry/lib/url'
1718

1819
import constants, { UNKNOWN_ERROR } from '../constants.mts'
1920

@@ -251,7 +252,14 @@ export async function setGitRemoteGithubRepoUrl(
251252
token: string,
252253
cwd = process.cwd(),
253254
): Promise<boolean> {
254-
const { host } = new URL(constants.ENV.GITHUB_SERVER_URL)
255+
const { GITHUB_SERVER_URL } = constants.ENV
256+
const urlObj = parseUrl(GITHUB_SERVER_URL)
257+
const host = urlObj?.host
258+
if (!host) {
259+
debugFn('error', 'invalid: GITHUB_SERVER_URL env var')
260+
debugDir('inspect', { GITHUB_SERVER_URL })
261+
return false
262+
}
255263
const url = `https://x-access-token:${token}@${host}/${owner}/${repo}`
256264
const stdioIgnoreOptions: SpawnOptions = {
257265
cwd,

0 commit comments

Comments
 (0)