1- import { HttpsProxyAgent } from 'hpagent'
1+ import { HttpProxyAgent , HttpsProxyAgent } from 'hpagent'
22
33import isInteractive from '@socketregistry/is-interactive/index.cjs'
44import { password } from '@socketsecurity/registry/lib/prompts'
@@ -10,8 +10,6 @@ import constants from '../constants.mts'
1010
1111import type { CResult } from '../types.mts'
1212
13- const { SOCKET_PUBLIC_API_TOKEN } = constants
14-
1513const TOKEN_PREFIX = 'sktsec_'
1614
1715const { length : TOKEN_PREFIX_LENGTH } = TOKEN_PREFIX
@@ -21,15 +19,26 @@ function getDefaultApiBaseUrl(): string | undefined {
2119 const baseUrl =
2220 // Lazily access constants.ENV.SOCKET_CLI_API_BASE_URL.
2321 constants . ENV . SOCKET_CLI_API_BASE_URL || getConfigValueOrUndef ( 'apiBaseUrl' )
24- return isNonEmptyString ( baseUrl ) ? baseUrl : undefined
22+ return isUrl ( baseUrl ) ? baseUrl : undefined
2523}
2624
2725// The API server that should be used for operations.
28- function getDefaultHttpProxy ( ) : string | undefined {
26+ function getDefaultProxyUrl ( ) : string | undefined {
2927 const apiProxy =
3028 // Lazily access constants.ENV.SOCKET_CLI_API_PROXY.
3129 constants . ENV . SOCKET_CLI_API_PROXY || getConfigValueOrUndef ( 'apiProxy' )
32- return isNonEmptyString ( apiProxy ) ? apiProxy : undefined
30+ return isUrl ( apiProxy ) ? apiProxy : undefined
31+ }
32+
33+ function isUrl ( value : any ) : value is string {
34+ if ( isNonEmptyString ( value ) ) {
35+ try {
36+ // eslint-disable-next-line no-new
37+ new URL ( value )
38+ return true
39+ } catch { }
40+ }
41+ return false
3342}
3443
3544// This API key should be stored globally for the duration of the CLI execution.
@@ -64,14 +73,15 @@ export function getPublicToken(): string {
6473 return (
6574 // Lazily access constants.ENV.SOCKET_CLI_API_TOKEN.
6675 ( constants . ENV . SOCKET_CLI_API_TOKEN || getDefaultToken ( ) ) ??
67- SOCKET_PUBLIC_API_TOKEN
76+ // Lazily access constants.SOCKET_PUBLIC_API_TOKEN.
77+ constants . SOCKET_PUBLIC_API_TOKEN
6878 )
6979}
7080
7181export async function setupSdk (
7282 apiToken : string | undefined = getDefaultToken ( ) ,
7383 apiBaseUrl : string | undefined = getDefaultApiBaseUrl ( ) ,
74- proxy : string | undefined = getDefaultHttpProxy ( ) ,
84+ proxy : string | undefined ,
7585) : Promise < CResult < SocketSdk > > {
7686 if ( typeof apiToken !== 'string' && isInteractive ( ) ) {
7787 apiToken = await password ( {
@@ -87,10 +97,22 @@ export async function setupSdk(
8797 cause : 'You need to provide an API Token. Run `socket login` first.' ,
8898 }
8999 }
100+ if ( ! isUrl ( proxy ) ) {
101+ proxy = getDefaultProxyUrl ( )
102+ }
103+
104+ const ProxyAgent = proxy ?. startsWith ( 'http:' )
105+ ? HttpProxyAgent
106+ : HttpsProxyAgent
107+
90108 return {
91109 ok : true ,
92110 data : new SocketSdk ( apiToken , {
93- agent : proxy ? new HttpsProxyAgent ( { proxy } ) : undefined ,
111+ agent : proxy
112+ ? new ProxyAgent ( {
113+ proxy,
114+ } )
115+ : undefined ,
94116 baseUrl : apiBaseUrl ,
95117 userAgent : createUserAgentFromPkgJson ( {
96118 // Lazily access constants.ENV.INLINED_SOCKET_CLI_NAME.
0 commit comments