File tree Expand file tree Collapse file tree 1 file changed +34
-9
lines changed
packages/common/src/utils Expand file tree Collapse file tree 1 file changed +34
-9
lines changed Original file line number Diff line number Diff line change 1- const host = process . env . CODESANDBOX_HOST ;
1+ async function fetchWithRetries ( url : string ) {
2+ let err : Error ;
3+ for ( let i = 0 ; i < 3 ; i ++ ) {
4+ try {
5+ // eslint-disable-next-line
6+ return await fetch ( url ) . then ( x => {
7+ if ( x . ok ) {
8+ return x . json ( ) ;
9+ }
10+
11+ throw new Error ( 'Could not fetch ' + url ) ;
12+ } ) ;
13+ } catch ( e ) {
14+ err = e ;
15+ }
16+ }
17+
18+ throw err ;
19+ }
20+
21+ async function fetchPackageJSON ( dep : string , version : string ) {
22+ try {
23+ return await fetchWithRetries (
24+ `https://unpkg.com/${ dep } @${ encodeURIComponent ( version ) } /package.json`
25+ ) ;
26+ } catch ( e ) {
27+ return fetchWithRetries (
28+ `https://cdn.jsdelivr.net/npm/${ dep } @${ encodeURIComponent (
29+ version
30+ ) } /package.json`
31+ ) ;
32+ }
33+ }
234
335export async function getAbsoluteDependencies ( dependencies : Object ) {
436 const nonAbsoluteDependencies = Object . keys ( dependencies ) . filter ( dep => {
@@ -14,14 +46,7 @@ export async function getAbsoluteDependencies(dependencies: Object) {
1446 await Promise . all (
1547 nonAbsoluteDependencies . map ( async dep => {
1648 try {
17- const data = await window
18- . fetch (
19- `${ host } /api/v1/dependencies/${ dep } @${ encodeURIComponent (
20- dependencies [ dep ]
21- ) } `
22- )
23- . then ( x => x . json ( ) )
24- . then ( x => x . data ) ;
49+ const data = await fetchPackageJSON ( dep , dependencies [ dep ] ) ;
2550
2651 newDependencies [ dep ] = data . version ;
2752 } catch ( e ) {
You can’t perform that action at this time.
0 commit comments