forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-dependencies.ts
More file actions
189 lines (163 loc) · 4.9 KB
/
fetch-dependencies.ts
File metadata and controls
189 lines (163 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import _debug from '@codesandbox/common/lib/utils/debug';
import { getAbsoluteDependencies } from '@codesandbox/common/lib/utils/dependencies';
import { actions, dispatch } from 'codesandbox-api';
import setScreen from '../status-screen';
import delay from '../utils/delay';
import dependenciesToQuery from './dependencies-to-query';
type Dependencies = {
[dependency: string]: string;
};
const RETRY_COUNT = 60;
const debug = _debug('cs:sandbox:packager');
const VERSION = 1;
// eslint-disable-next-line
const DEV_URLS = {
packager:
'https://xi5p9f7czk.execute-api.eu-west-1.amazonaws.com/dev/packages',
bucket: 'https://dev-packager-packages.codesandbox.io',
};
// eslint-disable-next-line
const PROD_URLS = {
packager:
'https://aiwi8rnkp5.execute-api.eu-west-1.amazonaws.com/prod/packages',
bucket: 'https://prod-packager-packages.codesandbox.io',
};
const URLS = PROD_URLS;
const BUCKET_URL = URLS.bucket;
const PACKAGER_URL = URLS.packager;
function callApi(url: string, method = 'GET') {
return fetch(url, {
method,
})
.then(async response => {
if (!response.ok) {
const error = new Error(response.statusText || '' + response.status);
const message = await response.json();
// @ts-ignore
error.response = message;
// @ts-ignore
error.statusCode = response.status;
throw error;
}
return response;
})
.then(response => response.json());
}
/**
* Request the packager, if retries > RETRY_COUNT it will throw if something goes wrong
* otherwise it will retry again with an incremented retry
*
* @param {string} query The dependencies to call
*/
async function requestPackager(url, method = 'GET') {
let retries = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
debug(`Trying to call packager for ${retries} time`);
try {
const manifest = await callApi(url, method); // eslint-disable-line no-await-in-loop
return manifest;
} catch (e) {
if (e.response && e.statusCode !== 504) {
throw new Error(e.response.error);
}
// 403 status code means the bundler is still bundling
if (retries < RETRY_COUNT) {
retries += 1;
await delay(1000 * 2); // eslint-disable-line no-await-in-loop
} else {
throw e;
}
}
}
}
function dependenciesToBucketPath(dependencies: Object) {
return `v${VERSION}/combinations/${Object.keys(dependencies)
.sort()
.map(
// Paths starting with slashes don't work with cloudfront, even escaped. So we remove the slashes
dep =>
`${encodeURIComponent(dep.replace('/', '-').replace('@', ''))}@${
dependencies[dep]
}`
)
.join('%2B')}.json`;
}
/**
* Some dependencies have a space in their version for some reason, this is invalid and we
* ignore them. This is what yarn does as well.
*/
function removeSpacesFromDependencies(dependencies: Object) {
const newDeps = {};
Object.keys(dependencies).forEach(depName => {
const [version] = dependencies[depName].split(' ');
newDeps[depName] = version;
});
return newDeps;
}
async function getDependencies(
dependencies: Object,
showLoadingFullScreen: boolean
) {
const absoluteDependencies = await getAbsoluteDependencies(
removeSpacesFromDependencies(dependencies)
);
const dependencyUrl = dependenciesToQuery(absoluteDependencies);
const bucketDependencyUrl = dependenciesToBucketPath(absoluteDependencies);
setScreen({
type: 'loading',
text: 'Downloading Dependencies...',
showFullScreen: showLoadingFullScreen,
});
try {
const bucketManifest = await callApi(
`${BUCKET_URL}/${bucketDependencyUrl}`
);
return bucketManifest;
} catch (e) {
setScreen({
type: 'loading',
text: 'Resolving Dependencies...',
showFullScreen: showLoadingFullScreen,
});
// The dep has not been generated yet...
const { url } = await requestPackager(
`${PACKAGER_URL}/${dependencyUrl}`,
'POST'
);
setScreen({
type: 'loading',
text: 'Downloading Dependencies...',
showFullScreen: showLoadingFullScreen,
});
return requestPackager(`${BUCKET_URL}/${url}`);
}
}
export async function fetchDependencies(
npmDependencies: Dependencies,
_: any,
showLoaderFullScreen: boolean
) {
if (Object.keys(npmDependencies).length !== 0) {
// New Packager flow
try {
const result = await getDependencies(
npmDependencies,
showLoaderFullScreen
);
if (showLoaderFullScreen) {
setScreen({
type: 'loading',
text: 'Transpiling Modules...',
showFullScreen: showLoaderFullScreen,
});
}
return result;
} catch (e) {
e.message = `Could not fetch dependencies, please try again in a couple seconds: ${e.message}`;
dispatch(actions.notifications.show(e.message, 'error'));
throw e;
}
}
return false;
}