[READ-ONLY] a fast, modern browser for the npm registry

refactor: share `encodePackageName` utility function (#1066)

authored by

Bjorn Lu and committed by
GitHub
e74a201a f7d99207

+5 -17
+2 -5
app/utils/package-name.ts
··· 1 1 import validatePackageName from 'validate-npm-package-name' 2 + import { encodePackageName } from '#shared/utils/npm' 2 3 3 4 /** 4 5 * Normalize a package name for comparison by removing common variations. ··· 77 78 options: Parameters<typeof $fetch>[1] = {}, 78 79 ): Promise<boolean> { 79 80 try { 80 - const encodedName = name.startsWith('@') 81 - ? `@${encodeURIComponent(name.slice(1))}` 82 - : encodeURIComponent(name) 83 - 84 - await $fetch(`${NPM_REGISTRY}/${encodedName}`, { 81 + await $fetch(`${NPM_REGISTRY}/${encodePackageName(name)}`, { 85 82 ...options, 86 83 method: 'HEAD', 87 84 })
+1 -7
server/api/registry/analysis/[...pkg].get.ts
··· 18 18 ERROR_PACKAGE_ANALYSIS_FAILED, 19 19 } from '#shared/utils/constants' 20 20 import { parseRepoUrl } from '#shared/utils/git-providers' 21 + import { encodePackageName } from '#shared/utils/npm' 21 22 import { getLatestVersion, getLatestVersionBatch } from 'fast-npm-meta' 22 23 23 24 export default defineCachedEventHandler( ··· 75 76 }, 76 77 }, 77 78 ) 78 - 79 - function encodePackageName(name: string): string { 80 - if (name.startsWith('@')) { 81 - return `@${encodeURIComponent(name.slice(1))}` 82 - } 83 - return encodeURIComponent(name) 84 - } 85 79 86 80 /** 87 81 * Fetch @types package info including deprecation status using fast-npm-meta.
+2 -5
server/utils/dependency-resolver.ts
··· 1 1 import type { Packument, PackumentVersion, DependencyDepth } from '#shared/types' 2 2 import { mapWithConcurrency } from '#shared/utils/async' 3 + import { encodePackageName } from '#shared/utils/npm' 3 4 import { maxSatisfying } from 'semver' 4 5 5 6 /** Concurrency limit for fetching packuments during dependency resolution */ ··· 21 22 export const fetchPackument = defineCachedFunction( 22 23 async (name: string): Promise<Packument | null> => { 23 24 try { 24 - const encodedName = name.startsWith('@') 25 - ? `@${encodeURIComponent(name.slice(1))}` 26 - : encodeURIComponent(name) 27 - 28 - return await $fetch<Packument>(`https://registry.npmjs.org/${encodedName}`) 25 + return await $fetch<Packument>(`https://registry.npmjs.org/${encodePackageName(name)}`) 29 26 } catch (error) { 30 27 if (import.meta.dev) { 31 28 // oxlint-disable-next-line no-console -- log npm registry failures for debugging