forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unneeded req params related to the URL
- Loading branch information
Showing
11 changed files
with
131 additions
and
165 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import parsePackagePathname from '../utils/parsePackagePathname.js'; | ||
|
||
/** | ||
* Parse the pathname in the URL. Reject invalid URLs. | ||
*/ | ||
export default function validatePackagePathname(req, res, next) { | ||
const parsed = parsePackagePathname(req.path); | ||
|
||
if (parsed == null) { | ||
return res.status(403).send({ error: `Invalid URL: ${req.path}` }); | ||
} | ||
|
||
req.packageName = parsed.packageName; | ||
req.packageVersion = parsed.packageVersion; | ||
req.packageSpec = parsed.packageSpec; | ||
req.filename = parsed.filename; | ||
|
||
next(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import parsePackagePathname from '../parsePackagePathname.js'; | ||
|
||
describe('parsePackagePathname', () => { | ||
it('parses plain packages', () => { | ||
expect(parsePackagePathname('/[email protected]/umd/history.min.js')).toEqual({ | ||
packageName: 'history', | ||
packageVersion: '1.0.0', | ||
packageSpec: '[email protected]', | ||
filename: '/umd/history.min.js' | ||
}); | ||
}); | ||
|
||
it('parses plain packages with a hyphen in the name', () => { | ||
expect(parsePackagePathname('/[email protected]/index.js')).toEqual({ | ||
packageName: 'query-string', | ||
packageVersion: '5.0.0', | ||
packageSpec: '[email protected]', | ||
filename: '/index.js' | ||
}); | ||
}); | ||
|
||
it('parses plain packages with no version specified', () => { | ||
expect(parsePackagePathname('/query-string/index.js')).toEqual({ | ||
packageName: 'query-string', | ||
packageVersion: 'latest', | ||
packageSpec: 'query-string@latest', | ||
filename: '/index.js' | ||
}); | ||
}); | ||
|
||
it('parses plain packages with version spec', () => { | ||
expect(parsePackagePathname('/query-string@>=4.0.0/index.js')).toEqual({ | ||
packageName: 'query-string', | ||
packageVersion: '>=4.0.0', | ||
packageSpec: 'query-string@>=4.0.0', | ||
filename: '/index.js' | ||
}); | ||
}); | ||
|
||
it('parses scoped packages', () => { | ||
expect( | ||
parsePackagePathname('/@angular/[email protected]/src/index.d.ts') | ||
).toEqual({ | ||
packageName: '@angular/router', | ||
packageVersion: '4.3.3', | ||
packageSpec: '@angular/[email protected]', | ||
filename: '/src/index.d.ts' | ||
}); | ||
}); | ||
|
||
it('parses package names with a period in them', () => { | ||
expect(parsePackagePathname('/index.js')).toEqual({ | ||
packageName: 'index.js', | ||
packageVersion: 'latest', | ||
packageSpec: 'index.js@latest', | ||
filename: '' | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains 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,14 +1,16 @@ | ||
import createSearch from './createSearch.js'; | ||
|
||
export default function createPackageURL( | ||
packageName, | ||
version, | ||
pathname, | ||
search | ||
packageVersion, | ||
filename, | ||
query | ||
) { | ||
let url = `/${packageName}`; | ||
|
||
if (version != null) url += `@${version}`; | ||
if (pathname) url += pathname; | ||
if (search) url += search; | ||
if (packageVersion) url += `@${packageVersion}`; | ||
if (filename) url += filename; | ||
if (query) url += createSearch(query); | ||
|
||
return url; | ||
} |
This file contains 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 |
---|---|---|
@@ -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 | ||
}; | ||
} |
Oops, something went wrong.