@@ -12,6 +12,7 @@ import {
1212 SandboxFs ,
1313} from '@codesandbox/common/lib/types' ;
1414import { getGlobal } from '@codesandbox/common/lib/utils/global' ;
15+ import delay from '@codesandbox/common/lib/utils/delay' ;
1516import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator' ;
1617import { getSavedCode } from 'app/overmind/utils/sandbox' ;
1718import { json } from 'overmind' ;
@@ -22,6 +23,49 @@ import { appendFile, mkdir, rename, rmdir, unlink, writeFile } from './utils';
2223const global = getGlobal ( ) as Window & { BrowserFS : any } ;
2324
2425const 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+ }
2569
2670declare global {
2771 interface Window {
@@ -396,13 +440,18 @@ class SandboxFsSync {
396440
397441 private async fetchDependencyTypingFiles ( name : string , version : string ) {
398442 const dependencyQuery = encodeURIComponent ( `${ name } @${ version } ` ) ;
399- const fetchRequest = await fetch ( `${ SERVICE_URL } /${ dependencyQuery } .json` ) ;
400443
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
403449 }
404450
405- const { files } = await fetchRequest . json ( ) ;
451+ const { files } = await requestPackager (
452+ `${ SERVICE_URL } /${ dependencyQuery } .json` ,
453+ 3
454+ ) ;
406455
407456 return files ;
408457 }
0 commit comments