From 1f79f08eaa897663908ea18d32f86c0980011057 Mon Sep 17 00:00:00 2001 From: hezhengxu Date: Mon, 3 Jul 2023 13:48:15 +0800 Subject: [PATCH] fix: revert npmRegistry. --- app/common/adapter/NPMRegistry.ts | 80 ++++++++-------------------- app/core/service/ProxyModeService.ts | 6 ++- 2 files changed, 25 insertions(+), 61 deletions(-) diff --git a/app/common/adapter/NPMRegistry.ts b/app/common/adapter/NPMRegistry.ts index 2b5cb910..1745b172 100644 --- a/app/common/adapter/NPMRegistry.ts +++ b/app/common/adapter/NPMRegistry.ts @@ -40,51 +40,32 @@ export class NPMRegistry { this.registryHost = registryHost; } - public async getFullManifests(fullname: string, retries = 3): Promise { + public async getFullManifests(fullname: string, optionalConfig?: {retries?:number, remoteAuthToken?:string}): Promise { + let retries = optionalConfig?.retries || 3; // set query t=timestamp, make sure CDN cache disable // cache=0 is sync worker request flag const url = `${this.registry}/${encodeURIComponent(fullname)}?t=${Date.now()}&cache=0`; - return await this.getManifest(url, {}, retries); - } - - public async getAbbreviatedManifests(fullname: string, retries = 3): Promise { - const url = `${this.registry}/${encodeURIComponent(fullname)}?t=${Date.now()}&cache=0`; - const headers = { Accept: 'application/vnd.npm.install-v1+json' }; - return await this.getManifest(url, headers, retries); - } - - public async getPackageVersionManifest(fullname: string, versionOrTag: string, retries = 3) { - const url = `${this.registry}/${encodeURIComponent(fullname)}/${versionOrTag}`; - return await this.getManifest(url, {}, retries); + let lastError: any; + while (retries > 0) { + try { + // large package: https://r.cnpmjs.org/%40procore%2Fcore-icons + // https://r.cnpmjs.org/intraactive-sdk-ui 44s + const authorization = this.genAuthorizationHeader(optionalConfig?.remoteAuthToken); + return await this.request('GET', url, undefined, { timeout: 120000, headers: { authorization } }); + } catch (err: any) { + if (err.name === 'ResponseTimeoutError') throw err; + lastError = err; + } + retries--; + if (retries > 0) { + // sleep 1s ~ 4s in random + const delay = process.env.NODE_ENV === 'test' ? 1 : 1000 + Math.random() * 4000; + await setTimeout(delay); + } + } + throw lastError; } - - // public async getFullManifests(fullname: string, optionalConfig?: {retries?:number, remoteAuthToken?:string}): Promise { - // let retries = optionalConfig?.retries || 3; - // // set query t=timestamp, make sure CDN cache disable - // // cache=0 is sync worker request flag - // const url = `${this.registry}/${encodeURIComponent(fullname)}?t=${Date.now()}&cache=0`; - // let lastError: any; - // while (retries > 0) { - // try { - // // large package: https://r.cnpmjs.org/%40procore%2Fcore-icons - // // https://r.cnpmjs.org/intraactive-sdk-ui 44s - // const authorization = this.genAuthorizationHeader(optionalConfig?.remoteAuthToken); - // return await this.request('GET', url, undefined, { timeout: 120000, headers: { authorization } }); - // } catch (err: any) { - // if (err.name === 'ResponseTimeoutError') throw err; - // lastError = err; - // } - // retries--; - // if (retries > 0) { - // // sleep 1s ~ 4s in random - // const delay = process.env.NODE_ENV === 'test' ? 1 : 1000 + Math.random() * 4000; - // await setTimeout(delay); - // } - // } - // throw lastError; - // } - // app.put('/:name/sync', sync.sync); public async createSyncTask(fullname: string, optionalConfig?: { remoteAuthToken?:string}): Promise { const authorization = this.genAuthorizationHeader(optionalConfig?.remoteAuthToken); @@ -131,23 +112,4 @@ export class NPMRegistry { private genAuthorizationHeader(remoteAuthToken?:string) { return remoteAuthToken ? `Bearer ${remoteAuthToken}` : ''; } - - private async getManifest(url: string, headers = {}, retries = 3) { - let lastError: any; - while (retries > 0) { - try { - return await this.request('GET', url, undefined, { timeout: 120000, headers }); - } catch (err: any) { - if (err.name === 'ResponseTimeoutError') throw err; - lastError = err; - } - retries--; - if (retries > 0) { - // sleep 1s ~ 4s in random - const delay = process.env.NODE_ENV === 'test' ? 1 : 1000 + Math.random() * 4000; - await setTimeout(delay); - } - } - throw lastError; - } } diff --git a/app/core/service/ProxyModeService.ts b/app/core/service/ProxyModeService.ts index c654c342..0fd18de7 100644 --- a/app/core/service/ProxyModeService.ts +++ b/app/core/service/ProxyModeService.ts @@ -62,7 +62,8 @@ export class ProxyModeService extends AbstractService { } // not in NFS - const responseResult = await this.npmRegistry.getPackageVersionManifest(fullname, version); + let responseResult; + // const responseResult = await this.npmRegistry.getPackageVersionManifest(fullname, version); if (responseResult.status !== 200) { throw new HttpError({ status: responseResult.status, @@ -114,7 +115,8 @@ export class ProxyModeService extends AbstractService { if (isFullManifests) { responseResult = await this.npmRegistry.getFullManifests(fullname); } else { - responseResult = await this.npmRegistry.getAbbreviatedManifests(fullname); + responseResult = await this.npmRegistry.getFullManifests(fullname); + // responseResult = await this.npmRegistry.getAbbreviatedManifests(fullname); } if (responseResult.status !== 200) { throw new HttpError({