@socketsecurity/cli-with-sentry 0.14.155 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/.config/tsconfig.dts.tsbuildinfo +1 -1
  2. package/dist/cli.js +279 -325
  3. package/dist/cli.js.map +1 -1
  4. package/dist/constants.js +11 -9
  5. package/dist/constants.js.map +1 -1
  6. package/dist/types/commands/analytics/fetch-org-analytics.d.mts.map +1 -1
  7. package/dist/types/commands/analytics/fetch-repo-analytics.d.mts.map +1 -1
  8. package/dist/types/commands/audit-log/fetch-audit-log.d.mts.map +1 -1
  9. package/dist/types/commands/ci/fetch-default-org-slug.d.mts.map +1 -1
  10. package/dist/types/commands/dependencies/fetch-dependencies.d.mts.map +1 -1
  11. package/dist/types/commands/diff-scan/fetch-diff-scan.d.mts.map +1 -1
  12. package/dist/types/commands/info/fetch-package-info.d.mts.map +1 -1
  13. package/dist/types/commands/login/attempt-login.d.mts.map +1 -1
  14. package/dist/types/commands/oops/cmd-oops.d.mts.map +1 -1
  15. package/dist/types/commands/organization/fetch-license-policy.d.mts.map +1 -1
  16. package/dist/types/commands/organization/fetch-organization-list.d.mts.map +1 -1
  17. package/dist/types/commands/organization/fetch-quota.d.mts.map +1 -1
  18. package/dist/types/commands/organization/fetch-security-policy.d.mts.map +1 -1
  19. package/dist/types/commands/organization/output-organization-list.d.mts.map +1 -1
  20. package/dist/types/commands/package/cmd-package-score.d.mts.map +1 -1
  21. package/dist/types/commands/package/fetch-purl-deep-score.d.mts.map +1 -1
  22. package/dist/types/commands/package/fetch-purls-shallow-score.d.mts.map +1 -1
  23. package/dist/types/commands/repos/fetch-create-repo.d.mts.map +1 -1
  24. package/dist/types/commands/repos/fetch-delete-repo.d.mts.map +1 -1
  25. package/dist/types/commands/repos/fetch-list-repos.d.mts.map +1 -1
  26. package/dist/types/commands/repos/fetch-update-repo.d.mts.map +1 -1
  27. package/dist/types/commands/repos/fetch-view-repo.d.mts.map +1 -1
  28. package/dist/types/commands/scan/fetch-create-org-full-scan.d.mts.map +1 -1
  29. package/dist/types/commands/scan/fetch-delete-org-full-scan.d.mts.map +1 -1
  30. package/dist/types/commands/scan/fetch-diff-scan.d.mts.map +1 -1
  31. package/dist/types/commands/scan/fetch-list-scans.d.mts.map +1 -1
  32. package/dist/types/commands/scan/fetch-report-data.d.mts.map +1 -1
  33. package/dist/types/commands/scan/fetch-scan-metadata.d.mts.map +1 -1
  34. package/dist/types/commands/scan/fetch-scan.d.mts.map +1 -1
  35. package/dist/types/commands/scan/fetch-supported-scan-file-names.d.mts.map +1 -1
  36. package/dist/types/commands/scan/generate-report.d.mts.map +1 -1
  37. package/dist/types/commands/scan/stream-scan.d.mts.map +1 -1
  38. package/dist/types/commands/scan/suggest-org-slug.d.mts.map +1 -1
  39. package/dist/types/commands/scan/suggest-repo-slug.d.mts.map +1 -1
  40. package/dist/types/commands/threat-feed/fetch-threat-feed.d.mts.map +1 -1
  41. package/dist/types/constants.d.mts.map +1 -1
  42. package/dist/types/utils/alerts-map.d.mts.map +1 -1
  43. package/dist/types/utils/api.d.mts +11 -11
  44. package/dist/types/utils/api.d.mts.map +1 -1
  45. package/dist/types/utils/meow-with-subcommands.d.mts.map +1 -1
  46. package/dist/types/utils/sdk.d.mts +4 -1
  47. package/dist/types/utils/sdk.d.mts.map +1 -1
  48. package/dist/types/utils/socket-url.d.mts +10 -2
  49. package/dist/types/utils/socket-url.d.mts.map +1 -1
  50. package/dist/utils.js +331 -177
  51. package/dist/utils.js.map +1 -1
  52. package/external/@socketsecurity/registry/lib/constants/maintained-node-versions.js +5 -10
  53. package/external/@socketsecurity/registry/package.json +3 -3
  54. package/package.json +6 -6
package/dist/cli.js CHANGED
@@ -31,7 +31,11 @@ const promises = require('../external/@socketsecurity/registry/lib/promises')
31
31
  const _documentCurrentScript =
32
32
  typeof document !== 'undefined' ? document.currentScript : null
33
33
  async function fetchOrgAnalyticsData(time) {
34
- const sockSdk = await utils.setupSdk()
34
+ const sockSdkResult = await utils.setupSdk()
35
+ if (!sockSdkResult.ok) {
36
+ return sockSdkResult
37
+ }
38
+ const sockSdk = sockSdkResult.data
35
39
  return await utils.handleApiCall(
36
40
  sockSdk.getOrgAnalytics(time.toString()),
37
41
  'analytics data'
@@ -39,7 +43,11 @@ async function fetchOrgAnalyticsData(time) {
39
43
  }
40
44
 
41
45
  async function fetchRepoAnalyticsData(repo, time) {
42
- const sockSdk = await utils.setupSdk()
46
+ const sockSdkResult = await utils.setupSdk()
47
+ if (!sockSdkResult.ok) {
48
+ return sockSdkResult
49
+ }
50
+ const sockSdk = sockSdkResult.data
43
51
  return await utils.handleApiCall(
44
52
  sockSdk.getRepoAnalytics(repo, time.toString()),
45
53
  'analytics data'
@@ -534,7 +542,7 @@ async function run$M(argv, importMeta, { parentName }) {
534
542
  time = Number(cli.flags['time'] || 30)
535
543
  }
536
544
  }
537
- const apiToken = utils.getDefaultToken()
545
+ const hasApiToken = utils.hasDefaultToken()
538
546
  const wasValidInput = utils.checkCommandInput(
539
547
  outputKind,
540
548
  {
@@ -600,7 +608,7 @@ async function run$M(argv, importMeta, { parentName }) {
600
608
  },
601
609
  {
602
610
  nook: true,
603
- test: !!apiToken,
611
+ test: hasApiToken,
604
612
  message:
605
613
  'You need to be logged in to use this command. See `socket login`.',
606
614
  pass: 'ok',
@@ -625,7 +633,11 @@ async function run$M(argv, importMeta, { parentName }) {
625
633
  }
626
634
 
627
635
  async function fetchAuditLog({ logType, orgSlug, outputKind, page, perPage }) {
628
- const sockSdk = await utils.setupSdk()
636
+ const sockSdkResult = await utils.setupSdk()
637
+ if (!sockSdkResult.ok) {
638
+ return sockSdkResult
639
+ }
640
+ const sockSdk = sockSdkResult.data
629
641
  return await utils.handleApiCall(
630
642
  sockSdk.getAuditLogEvents(orgSlug, {
631
643
  // I'm not sure this is used at all.
@@ -878,7 +890,7 @@ async function run$L(argv, importMeta, { parentName }) {
878
890
  !!interactive,
879
891
  !!dryRun
880
892
  )
881
- const apiToken = utils.getDefaultToken()
893
+ const hasApiToken = utils.hasDefaultToken()
882
894
  const wasValidInput = utils.checkCommandInput(
883
895
  outputKind,
884
896
  {
@@ -892,7 +904,7 @@ async function run$L(argv, importMeta, { parentName }) {
892
904
  },
893
905
  {
894
906
  nook: true,
895
- test: !!apiToken,
907
+ test: hasApiToken,
896
908
  message:
897
909
  'You need to be logged in to use this command. See `socket login`.',
898
910
  pass: 'ok',
@@ -1330,7 +1342,11 @@ async function getDefaultOrgSlug() {
1330
1342
  data: defaultOrgResult
1331
1343
  }
1332
1344
  }
1333
- const sockSdk = await utils.setupSdk()
1345
+ const sockSdkResult = await utils.setupSdk()
1346
+ if (!sockSdkResult.ok) {
1347
+ return sockSdkResult
1348
+ }
1349
+ const sockSdk = sockSdkResult.data
1334
1350
  const result = await utils.handleApiCall(
1335
1351
  sockSdk.getOrganizations(),
1336
1352
  'list of organizations'
@@ -1372,7 +1388,11 @@ async function fetchCreateOrgFullScan(
1372
1388
  cwd,
1373
1389
  { branchName, commitHash, commitMessage, committers, pullRequest, repoName }
1374
1390
  ) {
1375
- const sockSdk = await utils.setupSdk()
1391
+ const sockSdkResult = await utils.setupSdk()
1392
+ if (!sockSdkResult.ok) {
1393
+ return sockSdkResult
1394
+ }
1395
+ const sockSdk = sockSdkResult.data
1376
1396
  return await utils.handleApiCall(
1377
1397
  sockSdk.createOrgFullScan(
1378
1398
  orgSlug,
@@ -1416,7 +1436,11 @@ async function fetchCreateOrgFullScan(
1416
1436
  }
1417
1437
 
1418
1438
  async function fetchSupportedScanFileNames() {
1419
- const sockSdk = await utils.setupSdk()
1439
+ const sockSdkResult = await utils.setupSdk()
1440
+ if (!sockSdkResult.ok) {
1441
+ return sockSdkResult
1442
+ }
1443
+ const sockSdk = sockSdkResult.data
1420
1444
  return await utils.handleApiCall(
1421
1445
  sockSdk.getReportSupportedFiles(),
1422
1446
  'supported scan file types'
@@ -1428,16 +1452,11 @@ async function fetchSupportedScanFileNames() {
1428
1452
  * full scan ID.
1429
1453
  */
1430
1454
  async function fetchReportData(orgSlug, scanId, includeLicensePolicy) {
1431
- const apiToken = utils.getDefaultToken()
1432
- if (!apiToken) {
1433
- return {
1434
- ok: false,
1435
- message: 'Authentication Error',
1436
- cause:
1437
- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
1438
- }
1455
+ const sockSdkResult = await utils.setupSdk()
1456
+ if (!sockSdkResult.ok) {
1457
+ return sockSdkResult
1439
1458
  }
1440
- const sockSdk = await utils.setupSdk(apiToken)
1459
+ const sockSdk = sockSdkResult.data
1441
1460
  let scanStatus = 'requested..'
1442
1461
  let policyStatus = 'requested..'
1443
1462
  let finishedFetching = false
@@ -1464,45 +1483,42 @@ async function fetchReportData(orgSlug, scanId, includeLicensePolicy) {
1464
1483
  )
1465
1484
  }
1466
1485
  }
1467
- async function fetchScanResult(apiToken) {
1468
- const response = await utils.tmpHandleApiCall(
1469
- utils.queryApi(
1470
- `orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}${includeLicensePolicy ? '?include_license_details=true' : ''}`,
1471
- apiToken
1472
- ),
1473
- 'fetchScanResult'
1486
+ async function fetchScanResult() {
1487
+ const result = await utils.queryApiSafeText(
1488
+ `orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}${includeLicensePolicy ? '?include_license_details=true' : ''}`
1474
1489
  )
1475
- updateScan('received response')
1476
- if (!response.ok) {
1477
- const cause = await utils.handleApiError(response.status)
1478
- updateScan(`request resulted in status code ${response.status}`)
1479
- return {
1480
- ok: false,
1481
- message: 'Socket API returned an error',
1482
- cause: `${response.statusText}${cause ? ` (cause: ${cause})` : ''}`
1483
- }
1490
+ updateScan(`response received`)
1491
+ if (!result.ok) {
1492
+ return result
1484
1493
  }
1485
- updateScan(`ok, downloading response..`)
1486
- const jsons = await response.text()
1487
- updateScan(`received policy`)
1488
- const lines = jsons.split('\n').filter(Boolean)
1494
+ const jsonsString = result.data
1495
+
1496
+ // This is nd-json; each line is a json object
1497
+ const lines = jsonsString.split('\n').filter(Boolean)
1498
+ let ok = true
1489
1499
  const data = lines.map(line => {
1490
1500
  try {
1491
1501
  return JSON.parse(line)
1492
1502
  } catch {
1493
- scanStatus = `received invalid JSON response`
1494
- spinner.stop()
1495
- logger.logger.error(
1496
- 'Response was not valid JSON but it ought to be (please report if this persists)'
1497
- )
1503
+ ok = false
1504
+ debug.debugLog('ndjson failed to parse the following line:')
1498
1505
  debug.debugLog(line)
1499
- updateProgress()
1500
1506
  return
1501
1507
  }
1502
1508
  })
1509
+ if (ok) {
1510
+ updateScan(`success`)
1511
+ return {
1512
+ ok: true,
1513
+ data
1514
+ }
1515
+ }
1516
+ updateScan(`received invalid JSON response`)
1503
1517
  return {
1504
- ok: true,
1505
- data
1518
+ ok: false,
1519
+ message: 'Invalid API response',
1520
+ cause:
1521
+ 'The API responded with at least one line that was not valid JSON. Please report if this persists.'
1506
1522
  }
1507
1523
  }
1508
1524
  async function fetchSecurityPolicy() {
@@ -1515,12 +1531,12 @@ async function fetchReportData(orgSlug, scanId, includeLicensePolicy) {
1515
1531
  }
1516
1532
  updateProgress()
1517
1533
  const [scan, securityPolicy] = await Promise.all([
1518
- fetchScanResult(apiToken).catch(e => {
1534
+ fetchScanResult().catch(e => {
1519
1535
  updateScan(`failure; unknown blocking problem occurred`)
1520
1536
  return {
1521
1537
  ok: false,
1522
1538
  message: 'Unexpected API problem',
1523
- cause: `We encountered an unexpected problem while requesting the Scan from the API: ${e?.message || '(no error message found)'}${e?.cause ? ` (cause: ${e.cause})` : ''}'}`
1539
+ cause: `We encountered an unexpected problem while requesting the Scan from the API: ${e?.message || '(no error message found)'}${e?.cause ? ` (cause: ${e.cause})` : ''}`
1524
1540
  }
1525
1541
  }),
1526
1542
  fetchSecurityPolicy().catch(e => {
@@ -1528,7 +1544,7 @@ async function fetchReportData(orgSlug, scanId, includeLicensePolicy) {
1528
1544
  return {
1529
1545
  ok: false,
1530
1546
  message: 'Unexpected API problem',
1531
- cause: `We encountered an unexpected problem while requesting the policy from the API: ${e?.message || '(no error message found)'}${e?.cause ? ` (cause: ${e.cause})` : ''}'}`
1547
+ cause: `We encountered an unexpected problem while requesting the policy from the API: ${e?.message || '(no error message found)'}${e?.cause ? ` (cause: ${e.cause})` : ''}`
1532
1548
  }
1533
1549
  })
1534
1550
  ]).finally(() => {
@@ -1732,7 +1748,7 @@ function createLeaf(art, alert, policyAction) {
1732
1748
  const leaf = {
1733
1749
  type: alert.type,
1734
1750
  policy: policyAction,
1735
- url: `https://socket.dev/${art.type}/package/${art.name}/${art.version}`,
1751
+ url: utils.getSocketDevPackageOverviewUrlFromPurl(art),
1736
1752
  manifest: art.manifestFiles?.map(obj => obj.file) ?? []
1737
1753
  }
1738
1754
  return leaf
@@ -1972,7 +1988,7 @@ Configuration used to generate this report:
1972
1988
 
1973
1989
  ## Alerts
1974
1990
 
1975
- ${report.alerts.size ? `All the alerts from the scan with a policy set to at least "${report.options.reportLevel}"}.` : `The scan contained no alerts for with a policy set to at least "${report.options.reportLevel}".`}
1991
+ ${report.alerts.size ? `All the alerts from the scan with a policy set to at least "${report.options.reportLevel}".` : `The scan contained no alerts with a policy set to at least "${report.options.reportLevel}".`}
1976
1992
 
1977
1993
  ${!report.alerts.size ? '' : utils.mdTable(flatData, ['Policy', 'Alert Type', 'Package', 'Introduced by', 'url', 'Manifest file'])}
1978
1994
  `.trim() + '\n'
@@ -2251,8 +2267,8 @@ async function discoverConfigValue(key) {
2251
2267
  }
2252
2268
  }
2253
2269
  if (key === 'defaultOrg') {
2254
- const apiToken = utils.getDefaultToken()
2255
- if (!apiToken) {
2270
+ const hasApiToken = utils.hasDefaultToken()
2271
+ if (!hasApiToken) {
2256
2272
  return {
2257
2273
  ok: false,
2258
2274
  message: 'Auto discover failed',
@@ -2281,8 +2297,8 @@ async function discoverConfigValue(key) {
2281
2297
  }
2282
2298
  }
2283
2299
  if (key === 'enforcedOrgs') {
2284
- const apiToken = utils.getDefaultToken()
2285
- if (!apiToken) {
2300
+ const hasApiToken = utils.hasDefaultToken()
2301
+ if (!hasApiToken) {
2286
2302
  return {
2287
2303
  ok: false,
2288
2304
  message: 'Auto discover failed',
@@ -2320,7 +2336,11 @@ async function discoverConfigValue(key) {
2320
2336
  }
2321
2337
  }
2322
2338
  async function getDefaultOrgFromToken() {
2323
- const sockSdk = await utils.setupSdk()
2339
+ const sockSdkResult = await utils.setupSdk()
2340
+ if (!sockSdkResult.ok) {
2341
+ return undefined
2342
+ }
2343
+ const sockSdk = sockSdkResult.data
2324
2344
  const result = await utils.handleApiCall(
2325
2345
  sockSdk.getOrganizations(),
2326
2346
  'list of organizations'
@@ -2340,7 +2360,11 @@ async function getDefaultOrgFromToken() {
2340
2360
  return undefined
2341
2361
  }
2342
2362
  async function getEnforceableOrgsFromToken() {
2343
- const sockSdk = await utils.setupSdk()
2363
+ const sockSdkResult = await utils.setupSdk()
2364
+ if (!sockSdkResult.ok) {
2365
+ return undefined
2366
+ }
2367
+ const sockSdk = sockSdkResult.data
2344
2368
  const result = await utils.handleApiCall(
2345
2369
  sockSdk.getOrganizations(),
2346
2370
  'list of organizations'
@@ -3047,7 +3071,11 @@ const cmdConfig = {
3047
3071
  }
3048
3072
 
3049
3073
  async function fetchDependencies({ limit, offset }) {
3050
- const sockSdk = await utils.setupSdk()
3074
+ const sockSdkResult = await utils.setupSdk()
3075
+ if (!sockSdkResult.ok) {
3076
+ return sockSdkResult
3077
+ }
3078
+ const sockSdk = sockSdkResult.data
3051
3079
  return await utils.handleApiCall(
3052
3080
  sockSdk.searchDependencies({
3053
3081
  limit,
@@ -3176,7 +3204,7 @@ async function run$D(argv, importMeta, { parentName }) {
3176
3204
  })
3177
3205
  const { json, limit, markdown, offset } = cli.flags
3178
3206
  const outputKind = utils.getOutputKind(json, markdown)
3179
- const apiToken = utils.getDefaultToken()
3207
+ const hasApiToken = utils.hasDefaultToken()
3180
3208
  const wasValidInput = utils.checkCommandInput(
3181
3209
  outputKind,
3182
3210
  {
@@ -3189,7 +3217,7 @@ async function run$D(argv, importMeta, { parentName }) {
3189
3217
  },
3190
3218
  {
3191
3219
  nook: true,
3192
- test: !!apiToken,
3220
+ test: hasApiToken,
3193
3221
  message:
3194
3222
  'You need to be logged in to use this command. See `socket login`.',
3195
3223
  pass: 'ok',
@@ -3211,29 +3239,10 @@ async function run$D(argv, importMeta, { parentName }) {
3211
3239
  }
3212
3240
 
3213
3241
  async function fetchDiffScan$1({ after, before, orgSlug }) {
3214
- const apiToken = utils.getDefaultToken()
3215
-
3216
- // Lazily access constants.spinner.
3217
- const { spinner } = constants
3218
- spinner.start('Fetching diff-scan...')
3219
- const response = await utils.queryApi(
3242
+ return await utils.queryApiSafeJson(
3220
3243
  `orgs/${orgSlug}/full-scans/diff?before=${encodeURIComponent(before)}&after=${encodeURIComponent(after)}`,
3221
- apiToken || ''
3244
+ 'a scan diff'
3222
3245
  )
3223
- spinner.successAndStop('Received diff-scan response')
3224
- if (!response.ok) {
3225
- const err = await utils.handleApiError(response.status)
3226
- return {
3227
- ok: false,
3228
- message: 'Socket API returned an error',
3229
- cause: `${response.statusText}${err ? ` ( Reason: ${err} )` : ''}`
3230
- }
3231
- }
3232
- const result = await response.json()
3233
- return {
3234
- ok: true,
3235
- data: result
3236
- }
3237
3246
  }
3238
3247
 
3239
3248
  async function outputDiffScan$1(result, { depth, file, outputKind }) {
@@ -3397,7 +3406,7 @@ async function run$C(argv, importMeta, { parentName }) {
3397
3406
  const outputKind = utils.getOutputKind(json, markdown)
3398
3407
  const defaultOrgSlugResult = utils.getConfigValueOrUndef('defaultOrg')
3399
3408
  const orgSlug = defaultOrgSlugResult || cli.input[0] || ''
3400
- const apiToken = utils.getDefaultToken()
3409
+ const hasApiToken = utils.hasDefaultToken()
3401
3410
  const wasValidInput = utils.checkCommandInput(
3402
3411
  outputKind,
3403
3412
  {
@@ -3429,7 +3438,7 @@ async function run$C(argv, importMeta, { parentName }) {
3429
3438
  },
3430
3439
  {
3431
3440
  nook: true,
3432
- test: !!apiToken,
3441
+ test: hasApiToken,
3433
3442
  message:
3434
3443
  'You need to be logged in to use this command. See `socket login`.',
3435
3444
  pass: 'ok',
@@ -3485,9 +3494,6 @@ function formatBranchName(str) {
3485
3494
  .replace(/[^-a-zA-Z0-9]+/g, '')
3486
3495
  .replace(/^-+|-+$/g, '')
3487
3496
  }
3488
- function getPkgNameFromPurlObj(purlObj) {
3489
- return `${purlObj.namespace ? `${purlObj.namespace}/` : ''}${purlObj.name}`
3490
- }
3491
3497
  function getBaseGitBranch() {
3492
3498
  // Lazily access constants.ENV.GITHUB_REF_NAME.
3493
3499
  return (
@@ -3510,31 +3516,31 @@ function getSocketBranchName(purl, newVersion, workspaceName) {
3510
3516
  }
3511
3517
  function getSocketPrTitlePattern(purl, workspaceName) {
3512
3518
  const purlObj = vendor.packageurlJsExports.PackageURL.fromString(purl)
3513
- const pkgName = getPkgNameFromPurlObj(purlObj)
3519
+ const pkgFullName = utils.getPkgFullNameFromPurlObj(purlObj)
3514
3520
  const workspaceDetails = workspaceName
3515
3521
  ? ` in ${regexps.escapeRegExp(workspaceName)}`
3516
3522
  : ''
3517
3523
  return new RegExp(
3518
- `Bump ${regexps.escapeRegExp(pkgName)} from ${regexps.escapeRegExp(purlObj.version)} to \\S+${workspaceDetails}`
3524
+ `Bump ${regexps.escapeRegExp(pkgFullName)} from ${regexps.escapeRegExp(purlObj.version)} to \\S+${workspaceDetails}`
3519
3525
  )
3520
3526
  }
3521
3527
  function getSocketPullRequestTitle(purl, newVersion, workspaceName) {
3522
3528
  const purlObj = vendor.packageurlJsExports.PackageURL.fromString(purl)
3523
- const pkgName = getPkgNameFromPurlObj(purlObj)
3529
+ const pkgFullName = utils.getPkgFullNameFromPurlObj(purlObj)
3524
3530
  const workspaceDetails = workspaceName ? ` in ${workspaceName}` : ''
3525
- return `Bump ${pkgName} from ${purlObj.version} to ${newVersion}${workspaceDetails}`
3531
+ return `Bump ${pkgFullName} from ${purlObj.version} to ${newVersion}${workspaceDetails}`
3526
3532
  }
3527
3533
  function getSocketPullRequestBody(purl, newVersion, workspaceName) {
3528
3534
  const purlObj = vendor.packageurlJsExports.PackageURL.fromString(purl)
3529
- const pkgName = getPkgNameFromPurlObj(purlObj)
3535
+ const pkgFullName = utils.getPkgFullNameFromPurlObj(purlObj)
3530
3536
  const workspaceDetails = workspaceName ? ` in ${workspaceName}` : ''
3531
- return `Bump [${pkgName}](https://socket.dev/${purlObj.type}/package/${pkgName}) from ${purlObj.version} to ${newVersion}${workspaceDetails}.`
3537
+ return `Bump [${pkgFullName}](${utils.getSocketDevPackageOverviewUrlFromPurl(purlObj)}) from ${purlObj.version} to ${newVersion}${workspaceDetails}.`
3532
3538
  }
3533
3539
  function getSocketCommitMessage(purl, newVersion, workspaceName) {
3534
3540
  const purlObj = vendor.packageurlJsExports.PackageURL.fromString(purl)
3535
- const pkgName = getPkgNameFromPurlObj(purlObj)
3541
+ const pkgFullName = utils.getPkgFullNameFromPurlObj(purlObj)
3536
3542
  const workspaceDetails = workspaceName ? ` in ${workspaceName}` : ''
3537
- return `socket: Bump ${pkgName} from ${purlObj.version} to ${newVersion}${workspaceDetails}`
3543
+ return `socket: Bump ${pkgFullName} from ${purlObj.version} to ${newVersion}${workspaceDetails}`
3538
3544
  }
3539
3545
  async function gitCreateAndPushBranchIfNeeded(
3540
3546
  branch,
@@ -4824,7 +4830,11 @@ async function run$B(argv, importMeta, { parentName }) {
4824
4830
  }
4825
4831
 
4826
4832
  async function fetchPackageInfo(pkgName, pkgVersion, includeAllIssues) {
4827
- const sockSdk = await utils.setupSdk(utils.getPublicToken())
4833
+ const sockSdkResult = await utils.setupSdk(utils.getPublicToken())
4834
+ if (!sockSdkResult.ok) {
4835
+ throw new Error('Was unable to setup sdk. Run `socket login` first.')
4836
+ }
4837
+ const sockSdk = sockSdkResult.data
4828
4838
  const result = await utils.handleApiCall(
4829
4839
  sockSdk.getIssuesByNPMPackage(pkgName, pkgVersion),
4830
4840
  'package issues'
@@ -5111,11 +5121,17 @@ async function attemptLogin(apiBaseUrl, apiProxy) {
5111
5121
  message: `Enter your ${vendor.terminalLinkExports('Socket.dev API key', 'https://docs.socket.dev/docs/api-keys')} (leave blank for a public key)`
5112
5122
  })) || SOCKET_PUBLIC_API_TOKEN
5113
5123
  const sdk = await utils.setupSdk(apiToken, apiBaseUrl, apiProxy)
5124
+ if (!sdk.ok) {
5125
+ process.exitCode = 1
5126
+ logger.logger.fail(utils.failMsgWithBadge(sdk.message, sdk.cause))
5127
+ return
5128
+ }
5114
5129
  const result = await utils.handleApiCall(
5115
- sdk.getOrganizations(),
5130
+ sdk.data.getOrganizations(),
5116
5131
  'token verification'
5117
5132
  )
5118
5133
  if (!result.ok) {
5134
+ process.exitCode = 1
5119
5135
  logger.logger.fail(utils.failMsgWithBadge(result.message, result.cause))
5120
5136
  return
5121
5137
  }
@@ -5167,6 +5183,7 @@ async function attemptLogin(apiBaseUrl, apiProxy) {
5167
5183
  )
5168
5184
  }
5169
5185
  } catch {
5186
+ process.exitCode = 1
5170
5187
  logger.logger.fail(`API login failed`)
5171
5188
  }
5172
5189
  }
@@ -6522,7 +6539,8 @@ const config$p = {
6522
6539
  description: 'Trigger an intentional error (for development)',
6523
6540
  hidden: true,
6524
6541
  flags: {
6525
- ...utils.commonFlags
6542
+ ...utils.commonFlags,
6543
+ ...utils.outputFlags
6526
6544
  },
6527
6545
  help: (parentName, config) => `
6528
6546
  Usage
@@ -6543,13 +6561,28 @@ async function run$p(argv, importMeta, { parentName }) {
6543
6561
  importMeta,
6544
6562
  parentName
6545
6563
  })
6546
-
6547
- // TODO: impl json/md
6548
-
6564
+ const { json, markdown } = cli.flags
6549
6565
  if (cli.flags['dryRun']) {
6550
6566
  logger.logger.log(DRY_RUN_BAILING_NOW$n)
6551
6567
  return
6552
6568
  }
6569
+ if (json) {
6570
+ process.exitCode = 1
6571
+ logger.logger.log(
6572
+ utils.serializeResultJson({
6573
+ ok: false,
6574
+ message: 'Oops',
6575
+ cause: 'This error was intentionally left blank'
6576
+ })
6577
+ )
6578
+ }
6579
+ if (markdown) {
6580
+ process.exitCode = 1
6581
+ logger.logger.fail(
6582
+ utils.failMsgWithBadge('Oops', 'This error was intentionally left blank')
6583
+ )
6584
+ return
6585
+ }
6553
6586
  throw new Error('This error was intentionally left blank')
6554
6587
  }
6555
6588
 
@@ -7447,7 +7480,11 @@ async function run$o(argv, importMeta, { parentName }) {
7447
7480
  }
7448
7481
 
7449
7482
  async function fetchOrganization() {
7450
- const sockSdk = await utils.setupSdk()
7483
+ const sockSdkResult = await utils.setupSdk()
7484
+ if (!sockSdkResult.ok) {
7485
+ return sockSdkResult
7486
+ }
7487
+ const sockSdk = sockSdkResult.data
7451
7488
  return await utils.handleApiCall(
7452
7489
  sockSdk.getOrganizations(),
7453
7490
  'organization list'
@@ -7467,8 +7504,7 @@ async function outputOrganizationList(result, outputKind = 'text') {
7467
7504
  return
7468
7505
  }
7469
7506
  const organizations = Object.values(result.data.organizations)
7470
- const apiToken = utils.getDefaultToken()
7471
- const lastFiveOfApiToken = utils.getLastFiveOfApiToken(apiToken ?? '?????')
7507
+ const visibleTokenPrefix = utils.getVisibleTokenPrefix()
7472
7508
  switch (outputKind) {
7473
7509
  case 'markdown': {
7474
7510
  // | Syntax | Description |
@@ -7485,7 +7521,7 @@ async function outputOrganizationList(result, outputKind = 'text') {
7485
7521
  }
7486
7522
  logger.logger.log('# Organizations\n')
7487
7523
  logger.logger.log(
7488
- `List of organizations associated with your API key, ending with: ${vendor.yoctocolorsCjsExports.italic(lastFiveOfApiToken)}\n`
7524
+ `List of organizations associated with your API key, starting with: ${vendor.yoctocolorsCjsExports.italic(visibleTokenPrefix)}\n`
7489
7525
  )
7490
7526
  logger.logger.log(
7491
7527
  `| Name${' '.repeat(mw1 - 4)} | ID${' '.repeat(mw2 - 2)} | Plan${' '.repeat(mw3 - 4)} |`
@@ -7505,7 +7541,7 @@ async function outputOrganizationList(result, outputKind = 'text') {
7505
7541
  }
7506
7542
  default: {
7507
7543
  logger.logger.log(
7508
- `List of organizations associated with your API key, ending with: ${vendor.yoctocolorsCjsExports.italic(lastFiveOfApiToken)}\n`
7544
+ `List of organizations associated with your API key, starting with: ${vendor.yoctocolorsCjsExports.italic(visibleTokenPrefix)}\n`
7509
7545
  )
7510
7546
  // Just dump
7511
7547
  for (const o of organizations) {
@@ -7557,7 +7593,7 @@ async function run$n(argv, importMeta, { parentName }) {
7557
7593
  })
7558
7594
  const { json, markdown } = cli.flags
7559
7595
  const outputKind = utils.getOutputKind(json, markdown)
7560
- const apiToken = utils.getDefaultToken()
7596
+ const hasApiToken = utils.hasDefaultToken()
7561
7597
  const wasValidInput = utils.checkCommandInput(
7562
7598
  outputKind,
7563
7599
  {
@@ -7570,7 +7606,7 @@ async function run$n(argv, importMeta, { parentName }) {
7570
7606
  },
7571
7607
  {
7572
7608
  nook: true,
7573
- test: !!apiToken,
7609
+ test: hasApiToken,
7574
7610
  message:
7575
7611
  'You need to be logged in to use this command. See `socket login`.',
7576
7612
  pass: 'ok',
@@ -7588,7 +7624,11 @@ async function run$n(argv, importMeta, { parentName }) {
7588
7624
  }
7589
7625
 
7590
7626
  async function fetchLicensePolicy(orgSlug) {
7591
- const sockSdk = await utils.setupSdk()
7627
+ const sockSdkResult = await utils.setupSdk()
7628
+ if (!sockSdkResult.ok) {
7629
+ return sockSdkResult
7630
+ }
7631
+ const sockSdk = sockSdkResult.data
7592
7632
  return await utils.handleApiCall(
7593
7633
  sockSdk.getOrgLicensePolicy(orgSlug),
7594
7634
  'organization license policy'
@@ -7689,7 +7729,7 @@ async function run$m(argv, importMeta, { parentName }) {
7689
7729
  !!interactive,
7690
7730
  !!dryRun
7691
7731
  )
7692
- const apiToken = utils.getDefaultToken()
7732
+ const hasApiToken = utils.hasDefaultToken()
7693
7733
  const wasValidInput = utils.checkCommandInput(
7694
7734
  outputKind,
7695
7735
  {
@@ -7710,7 +7750,7 @@ async function run$m(argv, importMeta, { parentName }) {
7710
7750
  },
7711
7751
  {
7712
7752
  nook: true,
7713
- test: !!apiToken,
7753
+ test: hasApiToken,
7714
7754
  message:
7715
7755
  'You need to be logged in to use this command. See `socket login`.',
7716
7756
  pass: 'ok',
@@ -7728,7 +7768,11 @@ async function run$m(argv, importMeta, { parentName }) {
7728
7768
  }
7729
7769
 
7730
7770
  async function fetchSecurityPolicy(orgSlug) {
7731
- const sockSdk = await utils.setupSdk()
7771
+ const sockSdkResult = await utils.setupSdk()
7772
+ if (!sockSdkResult.ok) {
7773
+ return sockSdkResult
7774
+ }
7775
+ const sockSdk = sockSdkResult.data
7732
7776
  return await utils.handleApiCall(
7733
7777
  sockSdk.getOrgSecurityPolicy(orgSlug),
7734
7778
  'organization security policy'
@@ -7831,7 +7875,7 @@ async function run$l(argv, importMeta, { parentName }) {
7831
7875
  !!interactive,
7832
7876
  !!dryRun
7833
7877
  )
7834
- const apiToken = utils.getDefaultToken()
7878
+ const hasApiToken = utils.hasDefaultToken()
7835
7879
  const wasValidInput = utils.checkCommandInput(
7836
7880
  outputKind,
7837
7881
  {
@@ -7850,7 +7894,7 @@ async function run$l(argv, importMeta, { parentName }) {
7850
7894
  },
7851
7895
  {
7852
7896
  nook: true,
7853
- test: !!apiToken,
7897
+ test: hasApiToken,
7854
7898
  message:
7855
7899
  'You need to be logged in to use this command. See `socket login`.',
7856
7900
  pass: 'ok',
@@ -7894,7 +7938,11 @@ const cmdOrganizationPolicy = {
7894
7938
  }
7895
7939
 
7896
7940
  async function fetchQuota() {
7897
- const sockSdk = await utils.setupSdk()
7941
+ const sockSdkResult = await utils.setupSdk()
7942
+ if (!sockSdkResult.ok) {
7943
+ return sockSdkResult
7944
+ }
7945
+ const sockSdk = sockSdkResult.data
7898
7946
  return await utils.handleApiCall(sockSdk.getQuota(), 'token quota')
7899
7947
  }
7900
7948
 
@@ -7960,7 +8008,7 @@ async function run$k(argv, importMeta, { parentName }) {
7960
8008
  const json = Boolean(cli.flags['json'])
7961
8009
  const markdown = Boolean(cli.flags['markdown'])
7962
8010
  const outputKind = utils.getOutputKind(json, markdown)
7963
- const apiToken = utils.getDefaultToken()
8011
+ const hasApiToken = utils.hasDefaultToken()
7964
8012
  const wasValidInput = utils.checkCommandInput(
7965
8013
  outputKind,
7966
8014
  {
@@ -7972,7 +8020,7 @@ async function run$k(argv, importMeta, { parentName }) {
7972
8020
  },
7973
8021
  {
7974
8022
  nook: true,
7975
- test: !!apiToken,
8023
+ test: hasApiToken,
7976
8024
  message:
7977
8025
  'You need to be logged in to use this command. See `socket login`.',
7978
8026
  pass: 'ok',
@@ -8018,60 +8066,10 @@ const cmdOrganization = {
8018
8066
 
8019
8067
  async function fetchPurlDeepScore(purl) {
8020
8068
  logger.logger.error(`Requesting deep score data for this purl: ${purl}`)
8021
- const apiToken = utils.getDefaultToken()
8022
- if (!apiToken) {
8023
- return {
8024
- ok: false,
8025
- message: 'Authentication Error',
8026
- cause:
8027
- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
8028
- }
8029
- }
8030
-
8031
- // Lazily access constants.spinner.
8032
- const { spinner } = constants
8033
- spinner.start('Getting deep package score...')
8034
- let result
8035
- try {
8036
- result = await utils.queryApi(
8037
- `purl/score/${encodeURIComponent(purl)}`,
8038
- apiToken
8039
- )
8040
- } catch (e) {
8041
- spinner.failAndStop('The request was unsuccessful.')
8042
- const msg = e?.message
8043
- return {
8044
- ok: false,
8045
- message: 'API Request failed to complete',
8046
- ...(msg
8047
- ? {
8048
- cause: msg
8049
- }
8050
- : {})
8051
- }
8052
- }
8053
- spinner.successAndStop('Received deep package score response.')
8054
- if (!result.ok) {
8055
- const cause = await utils.handleApiError(result.status)
8056
- return {
8057
- ok: false,
8058
- message: 'Socket API returned an error',
8059
- cause: `${result.statusText}${cause ? ` (cause: ${cause})` : ''}`
8060
- }
8061
- }
8062
- const data = await result.text()
8063
- try {
8064
- return {
8065
- ok: true,
8066
- data: JSON.parse(data) // as PurlDataResponse
8067
- }
8068
- } catch (e) {
8069
- return {
8070
- ok: false,
8071
- message: 'Server returned invalid JSON',
8072
- cause: `Please report this. JSON.parse threw an error over the following response: \`${data}\``
8073
- }
8074
- }
8069
+ return await utils.queryApiSafeJson(
8070
+ `purl/score/${encodeURIComponent(purl)}`,
8071
+ 'the deep package scores'
8072
+ )
8075
8073
  }
8076
8074
 
8077
8075
  async function outputPurlScore(purl, result, outputKind) {
@@ -8388,7 +8386,7 @@ async function run$j(argv, importMeta, { parentName }) {
8388
8386
  const { json, markdown } = cli.flags
8389
8387
  const outputKind = utils.getOutputKind(json, markdown)
8390
8388
  const [ecosystem = '', purl] = cli.input
8391
- const apiToken = utils.getDefaultToken()
8389
+ const hasApiToken = utils.hasDefaultToken()
8392
8390
  const { purls, valid } = parsePackageSpecifiers(ecosystem, purl ? [purl] : [])
8393
8391
  const wasValidInput = utils.checkCommandInput(
8394
8392
  outputKind,
@@ -8413,7 +8411,7 @@ async function run$j(argv, importMeta, { parentName }) {
8413
8411
  },
8414
8412
  {
8415
8413
  nook: true,
8416
- test: !!apiToken,
8414
+ test: hasApiToken,
8417
8415
  message:
8418
8416
  'You need to be logged in to use this command. See `socket login`.',
8419
8417
  pass: 'ok',
@@ -8434,12 +8432,12 @@ async function fetchPurlsShallowScore(purls) {
8434
8432
  logger.logger.error(
8435
8433
  `Requesting shallow score data for ${purls.length} package urls (purl): ${purls.join(', ')}`
8436
8434
  )
8437
- const sockSdk = await utils.setupSdk(utils.getPublicToken())
8438
-
8439
- // Lazily access constants.spinner.
8440
- const { spinner } = constants
8441
- spinner.start(`Requesting data ...`)
8442
- const result = await utils.tmpHandleApiCall(
8435
+ const sockSdkResult = await utils.setupSdk()
8436
+ if (!sockSdkResult.ok) {
8437
+ return sockSdkResult
8438
+ }
8439
+ const sockSdk = sockSdkResult.data
8440
+ const result = await utils.handleApiCall(
8443
8441
  sockSdk.batchPackageFetch(
8444
8442
  {
8445
8443
  alerts: 'true'
@@ -8452,9 +8450,8 @@ async function fetchPurlsShallowScore(purls) {
8452
8450
  ),
8453
8451
  'looking up package'
8454
8452
  )
8455
- spinner.successAndStop('Request completed')
8456
- if (!result.success) {
8457
- return utils.handleFailedApiResponse('batchPackageFetch', result)
8453
+ if (!result.ok) {
8454
+ return result
8458
8455
  }
8459
8456
 
8460
8457
  // TODO: seems like there's a bug in the typing since we absolutely have to return the .data here
@@ -8958,7 +8955,11 @@ async function fetchCreateRepo({
8958
8955
  repoName,
8959
8956
  visibility
8960
8957
  }) {
8961
- const sockSdk = await utils.setupSdk()
8958
+ const sockSdkResult = await utils.setupSdk()
8959
+ if (!sockSdkResult.ok) {
8960
+ return sockSdkResult
8961
+ }
8962
+ const sockSdk = sockSdkResult.data
8962
8963
  return await utils.handleApiCall(
8963
8964
  sockSdk.createOrgRepo(orgSlug, {
8964
8965
  name: repoName,
@@ -9098,7 +9099,7 @@ async function run$d(argv, importMeta, { parentName }) {
9098
9099
  !!dryRun
9099
9100
  )
9100
9101
  const repoName = (utils.isTestingV1() ? cli.input[0] : repoNameFlag) || ''
9101
- const apiToken = utils.getDefaultToken()
9102
+ const hasApiToken = utils.hasDefaultToken()
9102
9103
  const wasValidInput = utils.checkCommandInput(
9103
9104
  outputKind,
9104
9105
  {
@@ -9120,7 +9121,7 @@ async function run$d(argv, importMeta, { parentName }) {
9120
9121
  },
9121
9122
  {
9122
9123
  nook: true,
9123
- test: !!apiToken,
9124
+ test: hasApiToken,
9124
9125
  message:
9125
9126
  'You need to be logged in to use this command. See `socket login`.',
9126
9127
  pass: 'ok',
@@ -9155,7 +9156,11 @@ async function run$d(argv, importMeta, { parentName }) {
9155
9156
  }
9156
9157
 
9157
9158
  async function fetchDeleteRepo(orgSlug, repoName) {
9158
- const sockSdk = await utils.setupSdk()
9159
+ const sockSdkResult = await utils.setupSdk()
9160
+ if (!sockSdkResult.ok) {
9161
+ return sockSdkResult
9162
+ }
9163
+ const sockSdk = sockSdkResult.data
9159
9164
  return await utils.handleApiCall(
9160
9165
  sockSdk.deleteOrgRepo(orgSlug, repoName),
9161
9166
  'to delete a repository'
@@ -9239,7 +9244,7 @@ async function run$c(argv, importMeta, { parentName }) {
9239
9244
  )
9240
9245
  const repoName =
9241
9246
  (defaultOrgSlug || utils.isTestingV1() ? cli.input[0] : cli.input[1]) || ''
9242
- const apiToken = utils.getDefaultToken()
9247
+ const hasApiToken = utils.hasDefaultToken()
9243
9248
  const wasValidInput = utils.checkCommandInput(
9244
9249
  outputKind,
9245
9250
  {
@@ -9261,7 +9266,7 @@ async function run$c(argv, importMeta, { parentName }) {
9261
9266
  },
9262
9267
  {
9263
9268
  nook: true,
9264
- test: !!apiToken,
9269
+ test: hasApiToken,
9265
9270
  message:
9266
9271
  'You need to be logged in to use this command. See `socket login`.',
9267
9272
  pass: 'ok',
@@ -9279,7 +9284,11 @@ async function run$c(argv, importMeta, { parentName }) {
9279
9284
  }
9280
9285
 
9281
9286
  async function fetchListRepos({ direction, orgSlug, page, per_page, sort }) {
9282
- const sockSdk = await utils.setupSdk()
9287
+ const sockSdkResult = await utils.setupSdk()
9288
+ if (!sockSdkResult.ok) {
9289
+ return sockSdkResult
9290
+ }
9291
+ const sockSdk = sockSdkResult.data
9283
9292
  return await utils.handleApiCall(
9284
9293
  sockSdk.getOrgRepoList(orgSlug, {
9285
9294
  sort,
@@ -9428,7 +9437,7 @@ async function run$b(argv, importMeta, { parentName }) {
9428
9437
  !!interactive,
9429
9438
  !!dryRun
9430
9439
  )
9431
- const apiToken = utils.getDefaultToken()
9440
+ const hasApiToken = utils.hasDefaultToken()
9432
9441
  const wasValidInput = utils.checkCommandInput(
9433
9442
  outputKind,
9434
9443
  {
@@ -9450,7 +9459,7 @@ async function run$b(argv, importMeta, { parentName }) {
9450
9459
  },
9451
9460
  {
9452
9461
  nook: true,
9453
- test: !!apiToken,
9462
+ test: hasApiToken,
9454
9463
  message:
9455
9464
  'You need to be logged in to use this command. See `socket login`.',
9456
9465
  pass: 'ok',
@@ -9482,7 +9491,11 @@ async function fetchUpdateRepo({
9482
9491
  repoName,
9483
9492
  visibility
9484
9493
  }) {
9485
- const sockSdk = await utils.setupSdk()
9494
+ const sockSdkResult = await utils.setupSdk()
9495
+ if (!sockSdkResult.ok) {
9496
+ return sockSdkResult
9497
+ }
9498
+ const sockSdk = sockSdkResult.data
9486
9499
  return await utils.handleApiCall(
9487
9500
  sockSdk.updateOrgRepo(orgSlug, repoName, {
9488
9501
  orgSlug,
@@ -9614,7 +9627,7 @@ async function run$a(argv, importMeta, { parentName }) {
9614
9627
  )
9615
9628
  const repoNameFlag = cli.flags['repoName']
9616
9629
  const repoName = (utils.isTestingV1() ? cli.input[0] : repoNameFlag) || ''
9617
- const apiToken = utils.getDefaultToken()
9630
+ const hasApiToken = utils.hasDefaultToken()
9618
9631
  const wasValidInput = utils.checkCommandInput(
9619
9632
  outputKind,
9620
9633
  {
@@ -9636,7 +9649,7 @@ async function run$a(argv, importMeta, { parentName }) {
9636
9649
  },
9637
9650
  {
9638
9651
  nook: true,
9639
- test: !!apiToken,
9652
+ test: hasApiToken,
9640
9653
  message:
9641
9654
  'You need to be logged in to use this command. See `socket login`.',
9642
9655
  pass: 'ok',
@@ -9671,7 +9684,11 @@ async function run$a(argv, importMeta, { parentName }) {
9671
9684
  }
9672
9685
 
9673
9686
  async function fetchViewRepo(orgSlug, repoName) {
9674
- const sockSdk = await utils.setupSdk()
9687
+ const sockSdkResult = await utils.setupSdk()
9688
+ if (!sockSdkResult.ok) {
9689
+ return sockSdkResult
9690
+ }
9691
+ const sockSdk = sockSdkResult.data
9675
9692
  return await utils.handleApiCall(
9676
9693
  sockSdk.getOrgRepo(orgSlug, repoName),
9677
9694
  'repository data'
@@ -9799,7 +9816,7 @@ async function run$9(argv, importMeta, { parentName }) {
9799
9816
  !!dryRun
9800
9817
  )
9801
9818
  const repoName = (utils.isTestingV1() ? cli.input[0] : repoNameFlag) || ''
9802
- const apiToken = utils.getDefaultToken()
9819
+ const hasApiToken = utils.hasDefaultToken()
9803
9820
  const wasValidInput = utils.checkCommandInput(
9804
9821
  outputKind,
9805
9822
  {
@@ -9829,7 +9846,7 @@ async function run$9(argv, importMeta, { parentName }) {
9829
9846
  },
9830
9847
  {
9831
9848
  nook: true,
9832
- test: !!apiToken,
9849
+ test: hasApiToken,
9833
9850
  message:
9834
9851
  'You need to be logged in to use this command. See `socket login`.',
9835
9852
  pass: 'ok',
@@ -10080,7 +10097,7 @@ async function run$8(argv, importMeta, { parentName }) {
10080
10097
  // We're going to need an api token to suggest data because those suggestions
10081
10098
  // must come from data we already know. Don't error on missing api token yet.
10082
10099
  // If the api-token is not set, ignore it for the sake of suggestions.
10083
- const apiToken = utils.getDefaultToken()
10100
+ const hasApiToken = utils.hasDefaultToken()
10084
10101
 
10085
10102
  // If we updated any inputs then we should print the command line to repeat
10086
10103
  // the command without requiring user input, as a suggestion.
@@ -10094,7 +10111,7 @@ async function run$8(argv, importMeta, { parentName }) {
10094
10111
  // If the current cwd is unknown and is used as a repo slug anyways, we will
10095
10112
  // first need to register the slug before we can use it.
10096
10113
  // Only do suggestions with an apiToken and when not in dryRun mode
10097
- if (apiToken && !dryRun && interactive) {
10114
+ if (hasApiToken && !dryRun && interactive) {
10098
10115
  if (!orgSlug) {
10099
10116
  const suggestion = await utils.suggestOrgSlug()
10100
10117
  if (suggestion) {
@@ -10142,7 +10159,7 @@ async function run$8(argv, importMeta, { parentName }) {
10142
10159
  },
10143
10160
  {
10144
10161
  nook: true,
10145
- test: !!apiToken,
10162
+ test: hasApiToken,
10146
10163
  message: 'This command requires an API token for access',
10147
10164
  pass: 'ok',
10148
10165
  fail: 'missing (try `socket login`)'
@@ -10199,7 +10216,11 @@ async function run$8(argv, importMeta, { parentName }) {
10199
10216
  }
10200
10217
 
10201
10218
  async function fetchDeleteOrgFullScan(orgSlug, scanId) {
10202
- const sockSdk = await utils.setupSdk()
10219
+ const sockSdkResult = await utils.setupSdk()
10220
+ if (!sockSdkResult.ok) {
10221
+ return sockSdkResult
10222
+ }
10223
+ const sockSdk = sockSdkResult.data
10203
10224
  return await utils.handleApiCall(
10204
10225
  sockSdk.deleteOrgFullScan(orgSlug, scanId),
10205
10226
  'to delete a scan'
@@ -10283,7 +10304,7 @@ async function run$7(argv, importMeta, { parentName }) {
10283
10304
  )
10284
10305
  const scanId =
10285
10306
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[0] : cli.input[1]) || ''
10286
- const apiToken = utils.getDefaultToken()
10307
+ const hasApiToken = utils.hasDefaultToken()
10287
10308
  const wasValidInput = utils.checkCommandInput(
10288
10309
  outputKind,
10289
10310
  {
@@ -10306,7 +10327,7 @@ async function run$7(argv, importMeta, { parentName }) {
10306
10327
  },
10307
10328
  {
10308
10329
  nook: true,
10309
- test: !!apiToken,
10330
+ test: hasApiToken,
10310
10331
  message:
10311
10332
  'You need to be logged in to use this command. See `socket login`.',
10312
10333
  pass: 'ok',
@@ -10324,31 +10345,15 @@ async function run$7(argv, importMeta, { parentName }) {
10324
10345
  }
10325
10346
 
10326
10347
  async function fetchDiffScan({ id1, id2, orgSlug }) {
10327
- const apiToken = utils.getDefaultToken()
10328
-
10329
- // Lazily access constants.spinner.
10330
- const { spinner } = constants
10331
10348
  logger.logger.error('Scan ID 1:', id1)
10332
10349
  logger.logger.error('Scan ID 2:', id2)
10333
- spinner.start('Fetching scan diff... (this may take a while)')
10334
- const response = await utils.queryApi(
10350
+ logger.logger.error(
10351
+ 'Note: this request may take some time if the scans are big'
10352
+ )
10353
+ return await utils.queryApiSafeJson(
10335
10354
  `orgs/${orgSlug}/full-scans/diff?before=${encodeURIComponent(id1)}&after=${encodeURIComponent(id2)}`,
10336
- apiToken || ''
10355
+ 'a scan diff'
10337
10356
  )
10338
- spinner.successAndStop('Received scan diff response')
10339
- if (!response.ok) {
10340
- const cause = await utils.handleApiError(response.status)
10341
- return {
10342
- ok: false,
10343
- message: 'Socket API returned an error',
10344
- cause: `${response.statusText}${cause ? ` (cause: ${cause})` : ''}`
10345
- }
10346
- }
10347
- const fullScan = await response.json()
10348
- return {
10349
- ok: true,
10350
- data: fullScan
10351
- }
10352
10357
  }
10353
10358
 
10354
10359
  const SOCKET_SBOM_URL_PREFIX$1 =
@@ -10644,7 +10649,7 @@ async function run$6(argv, importMeta, { parentName }) {
10644
10649
  if (id2.startsWith(SOCKET_SBOM_URL_PREFIX)) {
10645
10650
  id2 = id2.slice(SOCKET_SBOM_URL_PREFIX.length)
10646
10651
  }
10647
- const apiToken = utils.getDefaultToken()
10652
+ const hasApiToken = utils.hasDefaultToken()
10648
10653
  const wasValidInput = utils.checkCommandInput(
10649
10654
  outputKind,
10650
10655
  {
@@ -10678,7 +10683,7 @@ async function run$6(argv, importMeta, { parentName }) {
10678
10683
  },
10679
10684
  {
10680
10685
  nook: true,
10681
- test: !!apiToken,
10686
+ test: hasApiToken,
10682
10687
  message:
10683
10688
  'You need to be logged in to use this command. See `socket login`.',
10684
10689
  pass: 'ok',
@@ -10712,7 +10717,11 @@ async function fetchListScans({
10712
10717
  repo,
10713
10718
  sort
10714
10719
  }) {
10715
- const sockSdk = await utils.setupSdk()
10720
+ const sockSdkResult = await utils.setupSdk()
10721
+ if (!sockSdkResult.ok) {
10722
+ return sockSdkResult
10723
+ }
10724
+ const sockSdk = sockSdkResult.data
10716
10725
  return await utils.handleApiCall(
10717
10726
  sockSdk.getOrgFullScanList(orgSlug, {
10718
10727
  ...(branch
@@ -10924,7 +10933,7 @@ async function run$5(argv, importMeta, { parentName }) {
10924
10933
  !!interactive,
10925
10934
  !!dryRun
10926
10935
  )
10927
- const apiToken = utils.getDefaultToken()
10936
+ const hasApiToken = utils.hasDefaultToken()
10928
10937
  const wasValidInput = utils.checkCommandInput(
10929
10938
  outputKind,
10930
10939
  {
@@ -10948,7 +10957,7 @@ async function run$5(argv, importMeta, { parentName }) {
10948
10957
  },
10949
10958
  {
10950
10959
  nook: true,
10951
- test: !!apiToken,
10960
+ test: hasApiToken,
10952
10961
  message:
10953
10962
  'You need to be logged in to use this command. See `socket login`.',
10954
10963
  pass: 'ok',
@@ -10976,7 +10985,11 @@ async function run$5(argv, importMeta, { parentName }) {
10976
10985
  }
10977
10986
 
10978
10987
  async function fetchScanMetadata(orgSlug, scanId) {
10979
- const sockSdk = await utils.setupSdk()
10988
+ const sockSdkResult = await utils.setupSdk()
10989
+ if (!sockSdkResult.ok) {
10990
+ return sockSdkResult
10991
+ }
10992
+ const sockSdk = sockSdkResult.data
10980
10993
  return await utils.handleApiCall(
10981
10994
  sockSdk.getOrgFullScanMetadata(orgSlug, scanId),
10982
10995
  'meta data for a full scan'
@@ -11087,7 +11100,7 @@ async function run$4(argv, importMeta, { parentName }) {
11087
11100
  )
11088
11101
  const scanId =
11089
11102
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[0] : cli.input[1]) || ''
11090
- const apiToken = utils.getDefaultToken()
11103
+ const hasApiToken = utils.hasDefaultToken()
11091
11104
  const wasValidInput = utils.checkCommandInput(
11092
11105
  outputKind,
11093
11106
  {
@@ -11117,7 +11130,7 @@ async function run$4(argv, importMeta, { parentName }) {
11117
11130
  },
11118
11131
  {
11119
11132
  nook: true,
11120
- test: !!apiToken,
11133
+ test: hasApiToken,
11121
11134
  message:
11122
11135
  'You need to be logged in to use this command. See `socket login`.',
11123
11136
  pass: 'ok',
@@ -11231,7 +11244,7 @@ async function run$3(argv, importMeta, { parentName }) {
11231
11244
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[0] : cli.input[1]) || ''
11232
11245
  const file =
11233
11246
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[1] : cli.input[2]) || '-'
11234
- const apiToken = utils.getDefaultToken()
11247
+ const hasApiToken = utils.hasDefaultToken()
11235
11248
  const wasValidInput = utils.checkCommandInput(
11236
11249
  outputKind,
11237
11250
  {
@@ -11261,7 +11274,7 @@ async function run$3(argv, importMeta, { parentName }) {
11261
11274
  },
11262
11275
  {
11263
11276
  nook: true,
11264
- test: !!apiToken,
11277
+ test: hasApiToken,
11265
11278
  message:
11266
11279
  'You need to be logged in to use this command. See `socket login`.',
11267
11280
  pass: 'ok',
@@ -11288,56 +11301,39 @@ async function run$3(argv, importMeta, { parentName }) {
11288
11301
  }
11289
11302
 
11290
11303
  async function fetchScan(orgSlug, scanId) {
11291
- const apiToken = utils.getDefaultToken()
11292
- if (!apiToken) {
11293
- return {
11294
- ok: false,
11295
- message: 'Authentication Error',
11296
- cause:
11297
- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
11298
- }
11299
- }
11300
-
11301
- // Lazily access constants.spinner.
11302
- const { spinner } = constants
11303
- spinner.start('Fetching scan data...')
11304
- const response = await utils.queryApi(
11304
+ const result = await utils.queryApiSafeText(
11305
11305
  `orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}`,
11306
- apiToken
11306
+ 'a scan'
11307
11307
  )
11308
- spinner.successAndStop('Received response while fetching scan data.')
11309
- if (!response.ok) {
11310
- const cause = await utils.handleApiError(response.status)
11311
- return {
11312
- ok: false,
11313
- message: 'Socket API returned an error',
11314
- cause: `${response.statusText}${cause ? ` (cause: ${cause})` : ''}`
11315
- }
11308
+ if (!result.ok) {
11309
+ return result
11316
11310
  }
11311
+ const jsonsString = result.data
11317
11312
 
11318
11313
  // This is nd-json; each line is a json object
11319
- const jsons = await response.text()
11320
- const lines = jsons.split('\n').filter(Boolean)
11321
- let failed = false
11314
+ const lines = jsonsString.split('\n').filter(Boolean)
11315
+ let ok = true
11322
11316
  const data = lines.map(line => {
11323
11317
  try {
11324
11318
  return JSON.parse(line)
11325
11319
  } catch {
11326
- failed = true
11327
- return {}
11320
+ ok = false
11321
+ debug.debugLog('ndjson failed to parse the following line:')
11322
+ debug.debugLog(line)
11323
+ return null
11328
11324
  }
11329
11325
  })
11330
- if (failed) {
11326
+ if (ok) {
11331
11327
  return {
11332
- ok: false,
11333
- message: 'API response was invalid',
11334
- cause:
11335
- 'At least one line item was returned that could not be parsed as JSON... Please report.'
11328
+ ok: true,
11329
+ data
11336
11330
  }
11337
11331
  }
11338
11332
  return {
11339
- ok: true,
11340
- data
11333
+ ok: false,
11334
+ message: 'Invalid API response',
11335
+ cause:
11336
+ 'The API responded with at least one line that was not valid JSON. Please report if this persists.'
11341
11337
  }
11342
11338
  }
11343
11339
 
@@ -11435,7 +11431,11 @@ async function handleScanView(orgSlug, scanId, filePath, outputKind) {
11435
11431
  }
11436
11432
 
11437
11433
  async function streamScan(orgSlug, scanId, file) {
11438
- const sockSdk = await utils.setupSdk()
11434
+ const sockSdkResult = await utils.setupSdk()
11435
+ if (!sockSdkResult.ok) {
11436
+ return sockSdkResult
11437
+ }
11438
+ const sockSdk = sockSdkResult.data
11439
11439
  logger.logger.error('Requesting data from API...')
11440
11440
 
11441
11441
  // Note: this will write to stdout or target file. It's not a noop
@@ -11519,7 +11519,7 @@ async function run$2(argv, importMeta, { parentName }) {
11519
11519
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[0] : cli.input[1]) || ''
11520
11520
  const file =
11521
11521
  (utils.isTestingV1() || defaultOrgSlug ? cli.input[1] : cli.input[2]) || '-'
11522
- const apiToken = utils.getDefaultToken()
11522
+ const hasApiToken = utils.hasDefaultToken()
11523
11523
  const wasValidInput = utils.checkCommandInput(
11524
11524
  outputKind,
11525
11525
  {
@@ -11550,7 +11550,7 @@ async function run$2(argv, importMeta, { parentName }) {
11550
11550
  },
11551
11551
  {
11552
11552
  nook: true,
11553
- test: !!apiToken,
11553
+ test: hasApiToken,
11554
11554
  message:
11555
11555
  'You need to be logged in to use this command. See `socket login`.',
11556
11556
  pass: 'ok',
@@ -11624,56 +11624,10 @@ async function fetchThreatFeed({
11624
11624
  ['page', page],
11625
11625
  ['per_page', String(perPage)]
11626
11626
  ])
11627
- const apiToken = utils.getDefaultToken()
11628
- if (!apiToken) {
11629
- return {
11630
- ok: false,
11631
- message: 'Authentication Error',
11632
- cause:
11633
- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
11634
- }
11635
- }
11636
-
11637
- // Lazily access constants.spinner.
11638
- const { spinner } = constants
11639
- spinner.start('Fetching Threat Feed data...')
11640
- let result
11641
- try {
11642
- result = await utils.queryApi(`threat-feed?${queryParams}`, apiToken)
11643
- } catch (e) {
11644
- spinner.failAndStop('The request was unsuccessful.')
11645
- const msg = e?.message
11646
- return {
11647
- ok: false,
11648
- message: 'API Request failed to complete',
11649
- ...(msg
11650
- ? {
11651
- cause: msg
11652
- }
11653
- : {})
11654
- }
11655
- }
11656
- spinner.successAndStop('Received response while fetching Threat Feed data.')
11657
- if (!result.ok) {
11658
- const cause = await utils.handleApiError(result.status)
11659
- return {
11660
- ok: false,
11661
- message: 'Socket API returned an error',
11662
- cause: `${result.statusText}${cause ? ` (cause: ${cause})` : ''}`
11663
- }
11664
- }
11665
- const data = await result.json()
11666
- if ('error' in data && data.error) {
11667
- return {
11668
- ok: false,
11669
- message: 'Socket API returned an error',
11670
- cause: data.error.message
11671
- }
11672
- }
11673
- return {
11674
- ok: true,
11675
- data: data
11676
- }
11627
+ return await utils.queryApiSafeJson(
11628
+ `threat-feed?${queryParams}`,
11629
+ 'the Threat Feed data'
11630
+ )
11677
11631
  }
11678
11632
 
11679
11633
  const require$1 = Module.createRequire(
@@ -11966,7 +11920,7 @@ async function run$1(argv, importMeta, { parentName }) {
11966
11920
  !!interactive,
11967
11921
  !!dryRun
11968
11922
  )
11969
- const apiToken = utils.getDefaultToken()
11923
+ const hasApiToken = utils.hasDefaultToken()
11970
11924
  const wasValidInput = utils.checkCommandInput(
11971
11925
  outputKind,
11972
11926
  {
@@ -11985,7 +11939,7 @@ async function run$1(argv, importMeta, { parentName }) {
11985
11939
  },
11986
11940
  {
11987
11941
  nook: true,
11988
- test: !!apiToken,
11942
+ test: hasApiToken,
11989
11943
  message:
11990
11944
  'You need to be logged in to use this command. See `socket login`.',
11991
11945
  pass: 'ok',
@@ -12302,5 +12256,5 @@ void (async () => {
12302
12256
  await utils.captureException(e)
12303
12257
  }
12304
12258
  })()
12305
- //# debugId=1c1e33e0-0ac3-4ee9-a611-1b6a04c9c662
12259
+ //# debugId=a4b81e94-18e0-4900-a5c2-a2b66c92bf6c
12306
12260
  //# sourceMappingURL=cli.js.map