From 28a711436e8315909e61a8bac98cd948299609d2 Mon Sep 17 00:00:00 2001 From: Bradley Meck Farias Date: Fri, 1 Nov 2024 14:17:48 -0500 Subject: [PATCH] nit --- src/data/socket-api-config.ts | 2 +- src/ui/file.ts | 28 +++++++++++++++++++--------- src/ui/parse-externals.ts | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/data/socket-api-config.ts b/src/data/socket-api-config.ts index aa11790..9be6465 100644 --- a/src/data/socket-api-config.ts +++ b/src/data/socket-api-config.ts @@ -303,4 +303,4 @@ export function init(disposables?: vscode.Disposable[]) { } const changeAPIConf = new vscode.EventEmitter(); -export const onAPIConfChange = changeAPIConf.event; \ No newline at end of file +export const onAPIConfChange = changeAPIConf.event; diff --git a/src/ui/file.ts b/src/ui/file.ts index c20bcfa..6ab6176 100644 --- a/src/ui/file.ts +++ b/src/ui/file.ts @@ -8,7 +8,7 @@ import * as module from 'module' import { parseExternals, SUPPORTED_LANGUAGES } from './parse-externals'; import { isPythonBuiltin } from '../data/python/builtins'; import { isGoBuiltin } from '../data/go/builtins'; -import { getExistingAPIConfig } from '../data/socket-api-config'; +import { getExistingAPIConfig, getAPIConfig, toAuthHeader } from '../data/socket-api-config'; import { sniffForGithubOrgOrUser } from '../data/github'; // @ts-expect-error missing module.isBuiltin @@ -114,22 +114,28 @@ export function activate( if (signal.aborted) { return Promise.reject('Aborted'); } - if (eco === 'pypi') { + if (['go', 'golang', 'pypi'].includes(eco)) { // TODO: implement PyPI depscores in backend return Promise.reject('Python depscores unavailable'); } - if (eco === 'go') { - // TODO: implement Go depscores in backend - return Promise.reject('Go depscores unavailable'); - } const cacheKey = `${eco}.${pkgName}` const existing = depscoreCache.get(cacheKey) const time = Date.now(); if (existing && time < existing.expires) { return existing.score; } - const score = new Promise((f, r) => { - const req = https.get(`https://socket.dev/api/${eco}/package-info/score?name=${pkgName}`); + const score = new Promise(async (f, r) => { + const apiConfig = await getAPIConfig() + if (!apiConfig) { + return + } + const req = https.request(`https://socket.dev/api/${eco}/package-info/score?name=${pkgName}`, { + method: 'POST', + headers: { + 'content-type': 'json', + 'authorization': toAuthHeader(apiConfig.apiKey) + } + }); function cleanupReq() { try { req.destroy(); @@ -138,7 +144,11 @@ export function activate( r(Promise.reject('Aborted')); } signal.addEventListener('abort', cleanupReq); - req.end(); + req.end(JSON.stringify({ + components: [ + purl: `pkg:${eco}/${pkgName}` + ] + })); req.on('error', r); req.on('response', (res) => { signal.removeEventListener('abort', cleanupReq); diff --git a/src/ui/parse-externals.ts b/src/ui/parse-externals.ts index ff89644..0e2892f 100644 --- a/src/ui/parse-externals.ts +++ b/src/ui/parse-externals.ts @@ -26,7 +26,7 @@ export const SUPPORTED_LANGUAGES: Record = { typescript: 'npm', typescriptreact: 'npm', python: 'pypi', - go: 'go' + go: 'golang' } function getJSPackageNameFromSpecifier(name: string): string { @@ -301,7 +301,7 @@ export async function parseExternals(doc: Pick