Skip to content

Commit 763bb46

Browse files
author
Ives van Hoorne
committed
Use jsdelivr temporarily for type definitions
1 parent 25a953a commit 763bb46

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

packages/app/src/app/components/CodeEditor/Monaco/workers/fetch-dependency-typings.js

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
import path from 'path';
33

44
self.importScripts([
5-
'https://cdnjs.cloudflare.com/ajax/libs/typescript/2.4.2/typescript.min.js',
5+
'https://cdnjs.cloudflare.com/ajax/libs/typescript/2.7.2/typescript.min.js',
66
]);
77

8-
const ROOT_URL = `https://unpkg.com/`;
8+
const ROOT_URL = `https://cdn.jsdelivr.net/`;
99

1010
const loadedTypings = [];
1111

12-
const getVersion = version => {
13-
if (/^\d/.test(version)) {
14-
return `^${version}`;
15-
}
16-
17-
return version;
18-
};
19-
2012
/**
2113
* Send the typings library to the editor, the editor can then add them to the
2214
* registry
@@ -52,7 +44,11 @@ const doFetch = url => {
5244
};
5345

5446
const fetchFromDefinitelyTyped = (dependency, version, fetchedPaths) =>
55-
doFetch(`${ROOT_URL}@types/${dependency}/index.d.ts`).then(typings => {
47+
doFetch(
48+
`${ROOT_URL}npm/@types/${dependency
49+
.replace('@', '')
50+
.replace(/\//g, '__')}/index.d.ts`
51+
).then(typings => {
5652
addLib(
5753
`node_modules/@types/${dependency}/index.d.ts`,
5854
typings,
@@ -93,6 +89,16 @@ const getRequireStatements = (title: string, code: string) => {
9389
return requires;
9490
};
9591

92+
const tempTransformFiles = files => {
93+
const finalObj = {};
94+
95+
files.forEach(d => {
96+
finalObj[d.name] = d;
97+
});
98+
99+
return finalObj;
100+
};
101+
96102
const transformFiles = dir =>
97103
dir.files
98104
? dir.files.reduce((prev, next) => {
@@ -104,10 +110,13 @@ const transformFiles = dir =>
104110
}, {})
105111
: {};
106112

107-
const getFileMetaData = (depUrl, depPath) =>
108-
doFetch(`${depUrl}/${path.dirname(depPath)}/?meta`)
113+
const getFileMetaData = (dependency, version, depPath) =>
114+
doFetch(
115+
`https://data.jsdelivr.com/v1/package/npm/${dependency}@${version}/flat`
116+
)
109117
.then(response => JSON.parse(response))
110-
.then(transformFiles);
118+
.then(response => response.files.filter(f => f.name.startsWith(depPath)))
119+
.then(tempTransformFiles);
111120

112121
const resolveAppropiateFile = (fileMetaData, relativePath) => {
113122
const absolutePath = `/${relativePath}`;
@@ -160,17 +169,14 @@ const getFileTypes = (
160169
};
161170

162171
function fetchFromMeta(dependency, version, fetchedPaths) {
163-
const depUrl = `${ROOT_URL}${dependency}@${version}`;
164-
return doFetch(`${depUrl}/?meta`)
172+
const depUrl = `https://data.jsdelivr.com/v1/package/npm/${dependency}@${version}/flat`;
173+
return doFetch(depUrl)
165174
.then(response => JSON.parse(response))
166175
.then(meta => {
167176
const filterAndFlatten = files =>
168177
files.reduce((paths, file) => {
169-
if (file.type === 'directory') {
170-
return paths.concat(filterAndFlatten(file.files));
171-
}
172-
if (/\.d\.ts$/.test(file.path)) {
173-
paths.push(file.path);
178+
if (/\.d\.ts$/.test(file.name)) {
179+
paths.push(file.name);
174180
}
175181
return paths;
176182
}, []);
@@ -182,7 +188,7 @@ function fetchFromMeta(dependency, version, fetchedPaths) {
182188
}
183189

184190
dtsFiles.forEach(file => {
185-
doFetch(`${depUrl}/${file}`)
191+
doFetch(`https://cdn.jsdelivr.net/npm/${dependency}@${version}${file}`)
186192
.then(dtsFile =>
187193
addLib(`node_modules/${dependency}${file}`, dtsFile, fetchedPaths)
188194
)
@@ -192,7 +198,7 @@ function fetchFromMeta(dependency, version, fetchedPaths) {
192198
}
193199

194200
function fetchFromTypings(dependency, version, fetchedPaths) {
195-
const depUrl = `${ROOT_URL}${dependency}@${version}`;
201+
const depUrl = `${ROOT_URL}npm/${dependency}@${version}`;
196202
return doFetch(`${depUrl}/package.json`)
197203
.then(response => JSON.parse(response))
198204
.then(packageJSON => {
@@ -206,7 +212,7 @@ function fetchFromTypings(dependency, version, fetchedPaths) {
206212
);
207213

208214
// get all files in the specified directory
209-
return getFileMetaData(depUrl, types).then(fileData =>
215+
return getFileMetaData(dependency, version, types).then(fileData =>
210216
getFileTypes(
211217
depUrl,
212218
dependency,
@@ -231,20 +237,20 @@ async function fetchAndAddDependencies(dependencies) {
231237
try {
232238
if (loadedTypings.indexOf(dep) === -1) {
233239
loadedTypings.push(dep);
240+
241+
const depVersion = await doFetch(
242+
`https://data.jsdelivr.com/v1/package/resolve/npm/${dep}@${
243+
dependencies[dep]
244+
}`
245+
)
246+
.then(x => JSON.parse(x))
247+
.then(x => x.version);
234248
// eslint-disable-next-line no-await-in-loop
235-
await fetchFromTypings(
236-
dep,
237-
getVersion(dependencies[dep]),
238-
fetchedPaths
239-
).catch(() =>
249+
await fetchFromTypings(dep, depVersion, fetchedPaths).catch(() =>
240250
// not available in package.json, try checking meta for inline .d.ts files
241-
fetchFromMeta(
242-
dep,
243-
getVersion(dependencies[dep]),
244-
fetchedPaths
245-
).catch(() =>
251+
fetchFromMeta(dep, depVersion, fetchedPaths).catch(() =>
246252
// Not available in package.json or inline from meta, try checking in @types/
247-
fetchFromDefinitelyTyped(dep, dependencies[dep], fetchedPaths)
253+
fetchFromDefinitelyTyped(dep, depVersion, fetchedPaths)
248254
)
249255
);
250256
}

0 commit comments

Comments
 (0)