Remove unneeded req params related to the URL
This commit is contained in:
26
modules/utils/parsePackagePathname.js
Normal file
26
modules/utils/parsePackagePathname.js
Normal file
@ -0,0 +1,26 @@
|
||||
const packagePathnameFormat = /^\/((?:@[^/@]+\/)?[^/@]+)(?:@([^/]+))?(\/.*)?$/;
|
||||
|
||||
export default function parsePackagePathname(pathname) {
|
||||
try {
|
||||
pathname = decodeURIComponent(pathname);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = packagePathnameFormat.exec(pathname);
|
||||
|
||||
// Disallow invalid pathnames.
|
||||
if (match == null) return null;
|
||||
|
||||
const packageName = match[1];
|
||||
const packageVersion = match[2] || 'latest';
|
||||
const filename = (match[3] || '').replace(/\/\/+/g, '/');
|
||||
|
||||
return {
|
||||
// If the pathname is /@scope/name@version/file.js:
|
||||
packageName, // @scope/name
|
||||
packageVersion, // version
|
||||
packageSpec: `${packageName}@${packageVersion}`, // @scope/name@version
|
||||
filename // /file.js
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user