Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app store ui not updating to show installing plugins; cache npm response for one minute #1871

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 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 @@ -211,45 +211,50 @@ function isTheServerModule(moduleName: string, config: Config) {
return moduleName === config.name
}

function findModulesWithKeyword(keyword: string) {
return new Promise((resolve, reject) => {
let errorCount = 0
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
)
resolve(_.values(result))
})
.catch((e) => {
if (errorCount++) {
reject(e)
}
})
const modulesByKeyword: { [key: string]: any } = {}

function findModulesWithKeyword(keyword: string): Promise<NpmModuleData[]> {
return new Promise<NpmModuleData[]>((resolve, reject) => {
if (
modulesByKeyword[keyword] &&
Date.now() - modulesByKeyword[keyword].time < 60 * 1000
) {
resolve(modulesByKeyword[keyword].packages)
return
}
;[
fetch(
'http://registry.npmjs.org/-/v1/search?size=250&text=keywords:' +
keyword
)
].forEach(handleResultWithTimeout)

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