@@ -12,6 +12,7 @@ import {
12
12
SandboxFs ,
13
13
} from '@codesandbox/common/lib/types' ;
14
14
import { getGlobal } from '@codesandbox/common/lib/utils/global' ;
15
+ import delay from '@codesandbox/common/lib/utils/delay' ;
15
16
import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator' ;
16
17
import { getSavedCode } from 'app/overmind/utils/sandbox' ;
17
18
import { json } from 'overmind' ;
@@ -22,6 +23,49 @@ import { appendFile, mkdir, rename, rmdir, unlink, writeFile } from './utils';
22
23
const global = getGlobal ( ) as Window & { BrowserFS : any } ;
23
24
24
25
const SERVICE_URL = 'https://ata.codesandbox.io/api/v8' ;
26
+ const BUCKET_URL = 'https://prod-packager-packages.codesandbox.io/v1/typings' ;
27
+
28
+ async function callApi ( url : string , method = 'GET' ) {
29
+ const response = await fetch ( url , {
30
+ method,
31
+ } ) ;
32
+
33
+ if ( ! response . ok ) {
34
+ const error = new Error ( response . statusText || '' + response . status ) ;
35
+ const message = await response . json ( ) ;
36
+ // @ts -ignore
37
+ error . response = message ;
38
+ // @ts -ignore
39
+ error . statusCode = response . status ;
40
+ throw error ;
41
+ }
42
+
43
+ return response . json ( ) ;
44
+ }
45
+
46
+ async function requestPackager ( url : string , retryCount = 0 , method = 'GET' ) {
47
+ let retries = 0 ;
48
+
49
+ // eslint-disable-next-line no-constant-condition
50
+ while ( true ) {
51
+ try {
52
+ const manifest = await callApi ( url , method ) ; // eslint-disable-line no-await-in-loop
53
+
54
+ return manifest ;
55
+ } catch ( e ) {
56
+ if ( e . response && e . statusCode !== 504 ) {
57
+ throw new Error ( e . response . error ) ;
58
+ }
59
+ // 403 status code means the bundler is still bundling
60
+ if ( retries < retryCount ) {
61
+ retries += 1 ;
62
+ await delay ( 1000 * 2 ) ; // eslint-disable-line no-await-in-loop
63
+ } else {
64
+ throw e ;
65
+ }
66
+ }
67
+ }
68
+ }
25
69
26
70
declare global {
27
71
interface Window {
@@ -396,13 +440,18 @@ class SandboxFsSync {
396
440
397
441
private async fetchDependencyTypingFiles ( name : string , version : string ) {
398
442
const dependencyQuery = encodeURIComponent ( `${ name } @${ version } ` ) ;
399
- const fetchRequest = await fetch ( `${ SERVICE_URL } /${ dependencyQuery } .json` ) ;
400
443
401
- if ( ! fetchRequest . ok ) {
402
- throw new Error ( 'Fetch error' ) ;
444
+ try {
445
+ const url = `${ BUCKET_URL } /${ name } /${ version } .json` ;
446
+ return await requestPackager ( url , 0 ) . then ( x => x . files ) ;
447
+ } catch ( e ) {
448
+ // Hasn't been generated
403
449
}
404
450
405
- const { files } = await fetchRequest . json ( ) ;
451
+ const { files } = await requestPackager (
452
+ `${ SERVICE_URL } /${ dependencyQuery } .json` ,
453
+ 3
454
+ ) ;
406
455
407
456
return files ;
408
457
}
0 commit comments