Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 22 additions & 12 deletions server/api/opensearch/suggestions.get.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import type { NpmSearchResponse } from '#shared/types'
import { NPM_REGISTRY } from '#shared/utils/constants'
import * as v from 'valibot'
import { SearchQuerySchema } from '#shared/schemas/package'
import { CACHE_MAX_AGE_ONE_MINUTE, NPM_REGISTRY } from '#shared/utils/constants'

export default defineCachedEventHandler(
async event => {
const query = getQuery(event)
const q = String(query.q || '').trim()

if (!q) {
return [q, []]
}
try {
const q = v.parse(SearchQuerySchema, query.q)

if (!q) {
return [q, []]
}

const params = new URLSearchParams({ text: q, size: '10' })
const response = await $fetch<NpmSearchResponse>(`${NPM_REGISTRY}/-/v1/search?${params}`)
const params = new URLSearchParams({ text: q, size: '10' })
const response = await $fetch<NpmSearchResponse>(`${NPM_REGISTRY}/-/v1/search?${params}`)

const suggestions = response.objects.map(obj => obj.package.name)
return [q, suggestions]
const suggestions = response.objects.map(obj => obj.package.name)
return [q, suggestions]
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: ERROR_SUGGESTIONS_FETCH_FAILED,
})
}
},
{
maxAge: 60,
maxAge: CACHE_MAX_AGE_ONE_MINUTE,
swr: true,
getKey: event => {
const query = getQuery(event)
return `opensearch-suggestions:${query.q || ''}`
const q = String(query.q || '').trim()
return `opensearch-suggestions:${q}`
},
},
)
9 changes: 9 additions & 0 deletions shared/schemas/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export const FilePathSchema = v.pipe(
v.check(input => !input.startsWith('/'), 'Invalid path: must be relative to package root'),
)

/**
* Schema for search queries, limits length to guard against DoS attacks
*/
export const SearchQuerySchema = v.pipe(
v.string(),
v.trim(),
v.maxLength(100, 'Search query is too long'),
)

/**
* Schema for package fetching where version is not required
*/
Expand Down
2 changes: 2 additions & 0 deletions shared/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Duration
export const CACHE_MAX_AGE_ONE_MINUTE = 60
export const CACHE_MAX_AGE_ONE_HOUR = 60 * 60
export const CACHE_MAX_AGE_ONE_DAY = 60 * 60 * 24
export const CACHE_MAX_AGE_ONE_YEAR = 60 * 60 * 24 * 365
Expand All @@ -14,3 +15,4 @@ export const ERROR_CALC_INSTALL_SIZE_FAILED = 'Failed to calculate install size.
export const NPM_MISSING_README_SENTINEL = 'ERROR: No README data found!'
export const ERROR_JSR_FETCH_FAILED = 'Failed to fetch package from JSR registry.'
export const ERROR_NPM_FETCH_FAILED = 'Failed to fetch package from npm registry.'
export const ERROR_SUGGESTIONS_FETCH_FAILED = 'Failed to fetch suggestions.'