-
-
Notifications
You must be signed in to change notification settings - Fork 272
fix: use consistent package parsing on social/likes #1180
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
Merged
danielroe
merged 2 commits into
npmx-dev:main
from
BoxenOfDonuts:fix/consistent-package-parsing
Feb 7, 2026
+28
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,33 @@ | ||
| import * as v from 'valibot' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
|
|
||
| /** | ||
| * GET /api/social/likes/:name | ||
| * | ||
| * Gets the likes for a npm package on npmx | ||
| */ | ||
| export default eventHandlerWithOAuthSession(async (event, oAuthSession, _) => { | ||
| const packageName = getRouterParam(event, 'pkg') | ||
| if (!packageName) { | ||
| const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] | ||
| const { rawPackageName } = parsePackageParams(pkgParamSegments) | ||
|
|
||
| if (!rawPackageName) { | ||
| throw createError({ | ||
| status: 400, | ||
| message: 'package name not provided', | ||
| }) | ||
| } | ||
|
|
||
| const likesUtil = new PackageLikesUtils() | ||
| return await likesUtil.getLikes(packageName, oAuthSession?.did.toString()) | ||
| try { | ||
| const { packageName } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: decodeURIComponent(rawPackageName), | ||
| }) | ||
|
|
||
| const likesUtil = new PackageLikesUtils() | ||
| return await likesUtil.getLikes(packageName, oAuthSession?.did.toString()) | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: 'Failed to get likes', | ||
| }) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
cat -n server/api/social/likes/[...pkg].get.ts | head -50Repository: npmx-dev/npmx.dev
Length of output: 1211
🏁 Script executed:
rg -n "decodeURIComponent\(" server/api -C 2Repository: npmx-dev/npmx.dev
Length of output: 886
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 43
🏁 Script executed:
rg -n "import.*createError\|import.*handleApiError" server/apiRepository: npmx-dev/npmx.dev
Length of output: 43
🏁 Script executed:
cat -n server/api/registry/vulnerabilities/[...pkg].get.ts | head -40Repository: npmx-dev/npmx.dev
Length of output: 1826
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 88
🏁 Script executed:
rg -n "createError\|handleApiError" server/api/social/likes/[...pkg].get.tsRepository: npmx-dev/npmx.dev
Length of output: 43
🏁 Script executed:
rg -n "export.*handleApiError\|function handleApiError" server/Repository: npmx-dev/npmx.dev
Length of output: 43
🏁 Script executed:
fd "utils" server/ -type dRepository: npmx-dev/npmx.dev
Length of output: 231
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 231
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 909
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 174
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 1414
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 1532
🏁 Script executed:
rg -n "URIError" server/Repository: npmx-dev/npmx.dev
Length of output: 43
🏁 Script executed:
rg -n "import.*createError" server/api/social/likes/[...pkg].get.tsRepository: npmx-dev/npmx.dev
Length of output: 43
Catch
URIErrorfrom malformed percent-encoding to return 400 instead of 502.decodeURIComponentthrowsURIErrorwhen given invalid percent-encoding. The current implementation funnels this tohandleApiError's generic fallback, which returns 502, misclassifying a client error as a server error. Wrap the call to catchURIErrorseparately and return a 400 status.This pattern repeats across multiple endpoints (e.g.
server/api/registry/vulnerabilities/[...pkg].get.ts), so consider applying the fix consistently.Suggested approach
Extract
decodeURIComponentoutside thev.parsecall to isolate the error:try { + const decodedPackageName = decodeURIComponent(rawPackageName) const { packageName } = v.parse(PackageRouteParamsSchema, { - packageName: decodeURIComponent(rawPackageName), + packageName: decodedPackageName, }) const likesUtil = new PackageLikesUtils() return await likesUtil.getLikes(packageName, oAuthSession?.did.toString()) } catch (error: unknown) { + if (error instanceof URIError) { + throw createError({ + status: 400, + message: 'invalid package name encoding', + }) + } handleApiError(error, { statusCode: 502, message: 'Failed to get likes', }) }