@@ -43,9 +43,14 @@ import {
4343import ENV from '../../constants/env.mts'
4444import { TOKEN_PREFIX_LENGTH } from '../../constants/socket.mts'
4545import { getConfigValueOrUndef } from '../config.mts'
46+ import { debugApiRequest , debugApiResponse } from '../debug.mts'
4647
4748import type { CResult } from '../../types.mts'
48- import type { FileValidationResult } from '@socketsecurity/sdk'
49+ import type {
50+ FileValidationResult ,
51+ RequestInfo ,
52+ ResponseInfo ,
53+ } from '@socketsecurity/sdk'
4954const logger = getDefaultLogger ( )
5055
5156const TOKEN_VISIBLE_LENGTH = 5
@@ -148,40 +153,68 @@ export async function setupSdk(
148153
149154 const timeout = ENV . SOCKET_CLI_API_TIMEOUT || undefined
150155
151- return {
152- ok : true ,
153- data : new SocketSdk ( apiToken , {
154- ...( apiProxy ? { agent : new ProxyAgent ( { proxy : apiProxy } ) } : { } ) ,
155- ...( apiBaseUrl ? { baseUrl : apiBaseUrl } : { } ) ,
156- ...( timeout ? { timeout } : { } ) ,
157- onFileValidation : (
158- _validPaths : string [ ] ,
159- invalidPaths : string [ ] ,
160- _context : {
161- operation :
162- | 'createDependenciesSnapshot'
163- | 'createFullScan'
164- | 'uploadManifestFiles'
165- orgSlug ?: string | undefined
166- [ key : string ] : unknown
167- } ,
168- ) : FileValidationResult => {
169- if ( invalidPaths . length > 0 ) {
170- logger . warn (
171- `Skipped ${ invalidPaths . length } ${ pluralize ( 'file' , { count : invalidPaths . length } ) } that could not be read` ,
172- )
173- logger . substep (
174- 'This may occur with Yarn Berry PnP virtual filesystem or pnpm symlinks' ,
175- )
156+ const sdkOptions = {
157+ ...( apiProxy ? { agent : new ProxyAgent ( { proxy : apiProxy } ) } : { } ) ,
158+ ...( apiBaseUrl ? { baseUrl : apiBaseUrl } : { } ) ,
159+ ...( timeout ? { timeout } : { } ) ,
160+ // Add HTTP request hooks for debugging if SOCKET_CLI_DEBUG is enabled.
161+ ...( ENV . SOCKET_CLI_DEBUG
162+ ? {
163+ hooks : {
164+ onRequest : ( info : RequestInfo ) => {
165+ debugApiRequest ( info . method , info . url , info . timeout )
166+ } ,
167+ onResponse : ( info : ResponseInfo ) => {
168+ debugApiResponse ( info . url , info . status , info . error , {
169+ method : info . method ,
170+ url : info . url ,
171+ durationMs : info . duration ,
172+ headers : info . headers ,
173+ } )
174+ } ,
175+ } ,
176176 }
177- // Continue with valid files.
178- return { shouldContinue : true }
177+ : { } ) ,
178+ onFileValidation : (
179+ _validPaths : string [ ] ,
180+ invalidPaths : string [ ] ,
181+ _context : {
182+ operation :
183+ | 'createDependenciesSnapshot'
184+ | 'createFullScan'
185+ | 'uploadManifestFiles'
186+ orgSlug ?: string | undefined
187+ [ key : string ] : unknown
179188 } ,
180- userAgent : createUserAgentFromPkgJson ( {
181- name : ENV . INLINED_SOCKET_CLI_NAME || 'socket' ,
182- version : ENV . INLINED_SOCKET_CLI_VERSION || '0.0.0' ,
183- homepage : ENV . INLINED_SOCKET_CLI_HOMEPAGE || 'https://socket.dev/cli' ,
184- } ) ,
189+ ) : FileValidationResult => {
190+ if ( invalidPaths . length > 0 ) {
191+ logger . warn (
192+ `Skipped ${ invalidPaths . length } ${ pluralize ( 'file' , { count : invalidPaths . length } ) } that could not be read` ,
193+ )
194+ logger . substep (
195+ 'This may occur with Yarn Berry PnP virtual filesystem or pnpm symlinks' ,
196+ )
197+ }
198+ // Continue with valid files.
199+ return { shouldContinue : true }
200+ } ,
201+ userAgent : createUserAgentFromPkgJson ( {
202+ name : ENV . INLINED_SOCKET_CLI_NAME || 'socket' ,
203+ version : ENV . INLINED_SOCKET_CLI_VERSION || '0.0.0' ,
204+ homepage : ENV . INLINED_SOCKET_CLI_HOMEPAGE || 'https://socket.dev/cli' ,
185205 } ) ,
186206 }
207+
208+ if ( ENV . SOCKET_CLI_DEBUG ) {
209+ logger . info (
210+ `[DEBUG] ${ new Date ( ) . toISOString ( ) } SDK options: ${ JSON . stringify ( sdkOptions ) } ` ,
211+ )
212+ }
213+
214+ const sdk = new SocketSdk ( apiToken , sdkOptions )
215+
216+ return {
217+ ok : true ,
218+ data : sdk ,
219+ }
187220}
0 commit comments