diff --git a/app/port/controller/AbstractController.ts b/app/port/controller/AbstractController.ts index 26fe8d9c..9d16713c 100644 --- a/app/port/controller/AbstractController.ts +++ b/app/port/controller/AbstractController.ts @@ -106,9 +106,11 @@ export abstract class AbstractController extends MiddlewareController { if (!this.isPrivateScope(scope)) { // syncMode = none, redirect public package to source registry if (!this.enableSync) { - err.redirectToSourceRegistry = this.sourceRegistry; - // syncMode = all/exist + if (this.redirectNotFound) { + err.redirectToSourceRegistry = this.sourceRegistry; + } } else { + // syncMode = all/exist if (allowSync && this.syncNotFound) { // ErrorHandler will use syncPackage to create sync task err.syncPackage = { diff --git a/config/config.default.ts b/config/config.default.ts index 18e3d64d..24806a82 100644 --- a/config/config.default.ts +++ b/config/config.default.ts @@ -81,7 +81,7 @@ export default (appInfo: EggAppConfig) => { enableNpmClientAndVersionCheck: true, // sync when package not found, only effect when syncMode = all/exist syncNotFound: false, - // redirect to source registry when package not found, only effect when syncMode = all/exist + // redirect to source registry when package not found redirectNotFound: true, }; diff --git a/test/port/controller/package/ShowPackageController.test.ts b/test/port/controller/package/ShowPackageController.test.ts index 39810b24..60f1d502 100644 --- a/test/port/controller/package/ShowPackageController.test.ts +++ b/test/port/controller/package/ShowPackageController.test.ts @@ -718,6 +718,15 @@ describe('test/port/controller/package/ShowPackageController.test.ts', () => { assert(res.headers.location === 'https://registry.npmjs.org/@eggjs/tegg-metadata?t=0123123&foo=bar'); }); + it('should not redirect to source registry when redirectNotFound is false and sync mode is none', async () => { + mock(app.config.cnpmcore, 'syncMode', 'none'); + mock(app.config.cnpmcore, 'redirectNotFound', false); + const res = await app.httpRequest() + .get('/@eggjs/tegg-metadata') + .set('Accept', 'application/vnd.npm.install-v1+json'); + assert(res.status === 404); + }); + it('should redirect public non-scope package to source registry if package not exists when syncMode=none', async () => { mock(app.config.cnpmcore, 'syncMode', 'none'); let res = await app.httpRequest()