Skip to content

Commit

Permalink
refactor: remove handleResultWithTimeout
Browse files Browse the repository at this point in the history
The timeout mechanim was removed already earlier, but
we had left the function call to obfuscate the logic.

Remove the unnecessary construct and use a simple
promise chain to handle fetch result.
  • Loading branch information
tkurki committed Feb 1, 2025
1 parent deb97bf commit 16ea7a0
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { spawn } from 'child_process'
import fs from 'fs'
import _ from 'lodash'
import fetch, { Response } from 'node-fetch'
import fetch from 'node-fetch'
import path from 'path'
import semver, { SemVer } from 'semver'
import { Config } from './config/config'
Expand Down Expand Up @@ -213,41 +213,8 @@ function isTheServerModule(moduleName: string, config: Config) {

const modulesByKeyword: { [key: string]: any } = {}

function findModulesWithKeyword(keyword: string) {
return new Promise((resolve, reject) => {
const result = {}
const handleResultWithTimeout = (fetchResult: Promise<Response>): void => {
fetchResult
.then((r) => r.json())
.then((parsed) => {
const data = parsed.results || parsed.objects || []
data.reduce(
(
acc: { [packageName: string]: NpmModuleData },
module: NpmModuleData
) => {
const name = module.package.name
if (
!acc[name] ||
semver.gt(module.package.version, acc[name].package.version)
) {
acc[name] = module
}
return acc
},
result
)
const packages = _.values(result)
modulesByKeyword[keyword] = {
time: Date.now(),
packages
}
resolve(packages)
})
.catch((e) => {
reject(e)
})
}
function findModulesWithKeyword(keyword: string): Promise<NpmModuleData[]> {
return new Promise<NpmModuleData[]>((resolve, reject) => {
if (
modulesByKeyword[keyword] &&
Date.now() - modulesByKeyword[keyword].time < 60 * 1000
Expand All @@ -256,12 +223,38 @@ function findModulesWithKeyword(keyword: string) {
return
}

handleResultWithTimeout(
fetch(
'http://registry.npmjs.org/-/v1/search?size=250&text=keywords:' +
keyword
)
fetch(
'http://registry.npmjs.org/-/v1/search?size=250&text=keywords:' + keyword
)
.then((r) => r.json())
.then((parsed) => {
const data = parsed.results || parsed.objects || []
const result = data.reduce(
(
acc: { [packageName: string]: NpmModuleData },
module: NpmModuleData
) => {
const name = module.package.name
if (
!acc[name] ||
semver.gt(module.package.version, acc[name].package.version)
) {
acc[name] = module
}
return acc
},
{}
)
const packages = _.values(result)
modulesByKeyword[keyword] = {
time: Date.now(),
packages
}
resolve(packages)
})
.catch((e) => {
reject(e)
})
})
}

Expand Down

0 comments on commit 16ea7a0

Please sign in to comment.