Skip to content

Commit 9a25781

Browse files
committed
feat(debug): add API request/response logging via SDK hooks
Add HTTP request/response logging when SOCKET_CLI_DEBUG is enabled. Logs method, URL, status, duration, and headers (with auth redacted). - Add debugApiRequest() for logging request start - Update debugApiResponse() signature to accept detailed request info - Add SDK hooks for onRequest and onResponse callbacks - Log SDK options when debug is enabled Based on #895 Ported from v1.x commit 0e8e165
1 parent 541cd0e commit 9a25781

2 files changed

Lines changed: 84 additions & 33 deletions

File tree

packages/cli/src/utils/debug.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ function sanitizeHeaders(
5858
return sanitized
5959
}
6060

61+
/**
62+
* Debug an API request start.
63+
* Logs essential info without exposing sensitive data.
64+
*/
65+
export function debugApiRequest(
66+
method: string,
67+
endpoint: string,
68+
timeout?: number | undefined,
69+
): void {
70+
if (isDebugNs('silly')) {
71+
const timeoutStr = timeout !== undefined ? ` (timeout: ${timeout}ms)` : ''
72+
debugNs(
73+
'silly',
74+
`[${new Date().toISOString()}] request started: ${method} ${endpoint}${timeoutStr}`,
75+
)
76+
}
77+
}
78+
6179
/**
6280
* Debug an API response with detailed request information.
6381
* Logs essential info without exposing sensitive data.

packages/cli/src/utils/socket/sdk.mts

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ import {
4343
import ENV from '../../constants/env.mts'
4444
import { TOKEN_PREFIX_LENGTH } from '../../constants/socket.mts'
4545
import { getConfigValueOrUndef } from '../config.mts'
46+
import { debugApiRequest, debugApiResponse } from '../debug.mts'
4647

4748
import 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'
4954
const logger = getDefaultLogger()
5055

5156
const 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

Comments
 (0)