-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,10 +145,20 @@ export class PackageVersionFileController extends AbstractController { | |
} | ||
|
||
const file = await this.packageVersionFileService.showPackageVersionFile(packageVersion, path); | ||
const hasMeta = typeof meta === 'string'; | ||
|
||
if (!file) { | ||
const possibleFile = await this.#searchPossibleEntries(packageVersion, path); | ||
|
||
if (possibleFile) { | ||
const route = `/${fullname}/${versionSpec}/files${possibleFile.path}${hasMeta ? '?meta' : ''}`; | ||
ctx.redirect(route); | ||
return; | ||
} | ||
|
||
throw new NotFoundError(`File ${fullname}@${versionSpec}${path} not found`); | ||
} | ||
const hasMeta = typeof meta === 'string'; | ||
|
||
if (hasMeta) { | ||
ctx.set('cache-control', META_CACHE_CONTROL); | ||
return formatFileItem(file); | ||
|
@@ -161,6 +171,28 @@ export class PackageVersionFileController extends AbstractController { | |
return await this.distRepository.getDistStream(file.dist); | ||
} | ||
|
||
/** | ||
* compatibility with unpkg | ||
* 1. try to match alias entry. e.g. accessing `index.js` or `index.json` using /index | ||
* 2. if given path is directory and has `index.js` file, redirect to it. e.g. using `lib` alias to access `lib/index.js` | ||
* @param packageVersion | ||
Check warning on line 178 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)
Check warning on line 178 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)
Check warning on line 178 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (21, ubuntu-latest)
|
||
* @param path | ||
Check warning on line 179 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)
Check warning on line 179 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)
Check warning on line 179 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (21, ubuntu-latest)
|
||
* @return | ||
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (21, ubuntu-latest)
Check warning on line 180 in app/port/controller/PackageVersionFileController.ts GitHub Actions / test-mysql57-fs-nfs (21, ubuntu-latest)
|
||
*/ | ||
async #searchPossibleEntries(packageVersion: any, path: string) { | ||
const possiblePath = [ `${path}.js`, `${path}.json`, `${path}/index.js` ]; | ||
|
||
const possibleFiles = possiblePath.map(pathItem => this.packageVersionFileService.showPackageVersionFile(packageVersion, pathItem)); | ||
|
||
const files = await Promise.allSettled(possibleFiles); | ||
|
||
for (const file of files) { | ||
if (file.status === 'fulfilled' && file.value) { | ||
return file.value; | ||
} | ||
} | ||
} | ||
|
||
async #getPackageVersion(ctx: EggContext, fullname: string, scope: string, name: string, versionSpec: string) { | ||
const { blockReason, packageVersion } = await this.packageManagerService.showPackageVersionByVersionOrTag( | ||
scope, name, versionSpec); | ||
|