Skip to content

Commit

Permalink
fix: revert npmRegistry.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jul 3, 2023
1 parent 7481536 commit 1f79f08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 61 deletions.
80 changes: 21 additions & 59 deletions app/common/adapter/NPMRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,32 @@ export class NPMRegistry {
this.registryHost = registryHost;
}

public async getFullManifests(fullname: string, retries = 3): Promise<RegistryResponse> {
public async getFullManifests(fullname: string, optionalConfig?: {retries?:number, remoteAuthToken?:string}): Promise<RegistryResponse> {
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<RegistryResponse> {
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<RegistryResponse> {
// 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<RegistryResponse> {
const authorization = this.genAuthorizationHeader(optionalConfig?.remoteAuthToken);
Expand Down Expand Up @@ -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;
}
}
6 changes: 4 additions & 2 deletions app/core/service/ProxyModeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 1f79f08

Please sign in to comment.