@@ -17,6 +17,8 @@ import { calculateIntegrity } from '../../common/PackageUtil';
17
17
import { ABBREVIATED_META_TYPE , PROXY_CACHE_DIR_NAME } from '../../common/constants' ;
18
18
import { DIST_NAMES } from '../entity/Package' ;
19
19
import type { AbbreviatedPackageManifestType , AbbreviatedPackageJSONType , PackageManifestType , PackageJSONType } from '../../repository/PackageRepository' ;
20
+ import { PackageManagerService } from './PackageManagerService' ;
21
+ import { getScopeAndName } from '../../common/PackageUtil' ;
20
22
21
23
function isoNow ( ) {
22
24
return new Date ( ) . toISOString ( ) ;
@@ -50,6 +52,8 @@ export class ProxyCacheService extends AbstractService {
50
52
private readonly cacheService : CacheService ;
51
53
@Inject ( )
52
54
private readonly backgroundTaskHelper :BackgroundTaskHelper ;
55
+ @Inject ( )
56
+ private readonly packageManagerService : PackageManagerService ;
53
57
54
58
async getPackageVersionTarResponse ( fullname : string , ctx : EggContext ) : Promise < HttpClientResponse > {
55
59
if ( this . config . cnpmcore . syncPackageBlockList . includes ( fullname ) ) {
@@ -59,12 +63,17 @@ export class ProxyCacheService extends AbstractService {
59
63
}
60
64
61
65
async getPackageManifest ( fullname : string , fileType : DIST_NAMES . FULL_MANIFESTS | DIST_NAMES . ABBREVIATED_MANIFESTS ) : Promise < AbbreviatedPackageManifestType | PackageManifestType > {
62
- const cachedStoreKey = ( await this . proxyCacheRepository . findProxyCache ( fullname , fileType ) ) ?. filePath ;
63
- if ( cachedStoreKey ) {
64
- const nfsBytes = await this . nfsAdapter . getBytes ( cachedStoreKey ) ;
65
- const nfsString = Buffer . from ( nfsBytes ! ) . toString ( ) ;
66
- const nfsPkgManifgest = JSON . parse ( nfsString ) ;
67
- return nfsPkgManifgest ;
66
+ try {
67
+ const cachedStoreKey = ( await this . proxyCacheRepository . findProxyCache ( fullname , fileType ) ) ?. filePath ;
68
+ if ( cachedStoreKey ) {
69
+ const nfsBytes = await this . nfsAdapter . getBytes ( cachedStoreKey ) ;
70
+ const nfsString = Buffer . from ( nfsBytes ! ) . toString ( ) ;
71
+ const nfsPkgManifest = JSON . parse ( nfsString ) ;
72
+ return nfsPkgManifest ;
73
+ }
74
+ } catch ( e ) {
75
+ this . logger . error ( e ) ;
76
+ this . logger . error ( '[ProxyCacheService.getPackageManifest:error] get cache error, ignore' ) ;
68
77
}
69
78
70
79
const manifest = await this . getRewrittenManifest < typeof fileType > ( fullname , fileType ) ;
@@ -152,6 +161,7 @@ export class ProxyCacheService extends AbstractService {
152
161
}
153
162
154
163
async getRewrittenManifest < T extends DIST_NAMES > ( fullname :string , fileType : T , versionOrTag ?:string ) : Promise < GetSourceManifestAndCacheReturnType < T > > {
164
+ const [ scope , name ] = getScopeAndName ( fullname ) ;
155
165
let responseResult ;
156
166
switch ( fileType ) {
157
167
case DIST_NAMES . FULL_MANIFESTS :
@@ -171,9 +181,24 @@ export class ProxyCacheService extends AbstractService {
171
181
}
172
182
173
183
// replace tarball url
174
- const manifest = responseResult . data ;
184
+ const { status, data : manifest } = responseResult ;
185
+ // sourceRegistry not found, check private package
186
+ if ( status === 404 ) {
187
+ const { etag, data : manifest , blockReason } = fileType === DIST_NAMES . FULL_MANIFESTS ?
188
+ await this . packageManagerService . listPackageFullManifests ( scope , name , false ) :
189
+ await this . packageManagerService . listPackageAbbreviatedManifests ( scope , name , false ) ;
190
+ // found in private package
191
+ if ( etag && ! blockReason ) {
192
+ return manifest as any ;
193
+ }
194
+ }
175
195
const { sourceRegistry, registry } = this . config . cnpmcore ;
176
196
if ( isPkgManifest ( fileType ) ) {
197
+ const { etag, data, blockReason } = fileType === DIST_NAMES . FULL_MANIFESTS ?
198
+ await this . packageManagerService . listPackageFullManifests ( scope , name , false ) :
199
+ await this . packageManagerService . listPackageAbbreviatedManifests ( scope , name , false ) ;
200
+ const hasPrivatePackage = etag && ! blockReason ;
201
+
177
202
// pkg manifest
178
203
const versionMap = manifest . versions || { } ;
179
204
for ( const key in versionMap ) {
@@ -182,9 +207,26 @@ export class ProxyCacheService extends AbstractService {
182
207
versionItem . dist . tarball = versionItem . dist . tarball . replace ( sourceRegistry , registry ) ;
183
208
}
184
209
}
210
+ // private manifest
211
+ if ( hasPrivatePackage ) {
212
+ const privateVersionMap = data ?. versions || { } ;
213
+ for ( const key in privateVersionMap ) {
214
+ if ( ! versionMap [ key ] ) {
215
+ versionMap [ key ] = privateVersionMap [ key ] ;
216
+ }
217
+ }
218
+ if ( manifest . time ) {
219
+ const privateTimeMap = data ?. time || { } ;
220
+ for ( const key in privateTimeMap ) {
221
+ if ( ! manifest . time [ key ] ) {
222
+ manifest . time [ key ] = privateTimeMap [ key ] ;
223
+ }
224
+ }
225
+ }
226
+ }
185
227
} else {
186
228
// pkg version manifest
187
- const distItem = manifest . dist || { } ;
229
+ const distItem = manifest ? .dist || { } ;
188
230
if ( distItem . tarball ) {
189
231
distItem . tarball = distItem . tarball . replace ( sourceRegistry , registry ) ;
190
232
}
0 commit comments