Skip to content

Commit

Permalink
nit
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Nov 1, 2024
1 parent 17c4964 commit 28a7114
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/data/socket-api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,4 @@ export function init(disposables?: vscode.Disposable[]) {
}

const changeAPIConf = new vscode.EventEmitter<void>();
export const onAPIConfChange = changeAPIConf.event;
export const onAPIConfChange = changeAPIConf.event;
28 changes: 19 additions & 9 deletions src/ui/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<PackageScore>((f, r) => {
const req = https.get(`https://socket.dev/api/${eco}/package-info/score?name=${pkgName}`);
const score = new Promise<PackageScore>(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();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/parse-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const SUPPORTED_LANGUAGES: Record<string, string> = {
typescript: 'npm',
typescriptreact: 'npm',
python: 'pypi',
go: 'go'
go: 'golang'
}

function getJSPackageNameFromSpecifier(name: string): string {
Expand Down Expand Up @@ -301,7 +301,7 @@ export async function parseExternals(doc: Pick<vscode.TextDocument, 'getText' |
results.push({ name: name.split('.')[0], range });
}
}
} else if (SUPPORTED_LANGUAGES[doc.languageId] === 'go') {
} else if (SUPPORTED_LANGUAGES[doc.languageId] === 'golang') {
const goExecutable = await getGoExecutable()
if (goExecutable) {
const [childProcess, importFinderBin] = await Promise.all([
Expand Down

0 comments on commit 28a7114

Please sign in to comment.