diff --git a/libs/remix-url-resolver/src/resolve.ts b/libs/remix-url-resolver/src/resolve.ts index ffeaf3417c..665eb2f3dd 100644 --- a/libs/remix-url-resolver/src/resolve.ts +++ b/libs/remix-url-resolver/src/resolve.ts @@ -28,12 +28,12 @@ export class RemixURLResolver { gistAccessToken: string protocol: string - constructor (gistToken?: string, protocol = 'http:') { + constructor(gistToken?: string, protocol = 'http:') { this.previouslyHandled = {} this.setGistToken(gistToken, protocol) } - async setGistToken (gistToken?: string, protocol = 'http:') { + async setGistToken(gistToken?: string, protocol = 'http:') { this.gistAccessToken = gistToken || '' this.protocol = protocol } @@ -43,7 +43,7 @@ export class RemixURLResolver { * @param root The root of the github import statement * @param filePath path of the file in github */ - async handleGithubCall (root: string, filePath: string): Promise { + async handleGithubCall(root: string, filePath: string): Promise { const regex = filePath.match(/blob\/([^/]+)\/(.*)/) let reference = 'master' if (regex) { @@ -67,7 +67,7 @@ export class RemixURLResolver { * @param url The url of the import statement * @param cleanUrl */ - async handleHttp (url: string, cleanUrl: string): Promise { + async handleHttp(url: string, cleanUrl: string): Promise { // eslint-disable-next-line no-useless-catch try { const response: AxiosResponse = await axios.get(url, { transformResponse: [] }) @@ -82,7 +82,7 @@ export class RemixURLResolver { * @param url The url of the import statement * @param cleanUrl */ - async handleHttps (url: string, cleanUrl: string): Promise { + async handleHttps(url: string, cleanUrl: string): Promise { // eslint-disable-next-line no-useless-catch try { const response: AxiosResponse = await axios.get(url, { transformResponse: [] }) @@ -92,7 +92,7 @@ export class RemixURLResolver { } } - async handleSwarm (url: string, cleanUrl: string): Promise { + async handleSwarm(url: string, cleanUrl: string): Promise { // eslint-disable-next-line no-useless-catch try { const bzz = new Bzz({ url: this.protocol + '//swarm-gateways.net' }) @@ -108,7 +108,7 @@ export class RemixURLResolver { * Handle an import statement based on IPFS * @param url The url of the IPFS import statement */ - async handleIPFS (url: string): Promise { + async handleIPFS(url: string): Promise { // replace ipfs:// with /ipfs/ url = url.replace(/^ipfs:\/\/?/, 'ipfs/') // eslint-disable-next-line no-useless-catch @@ -127,19 +127,28 @@ export class RemixURLResolver { * Handle an import statement based on NPM * @param url The url of the NPM import statement */ - async handleNpmImport (url: string): Promise { - const NX_NPM_URL: string = process && process.env && process.env['NX_NPM_URL'] ? process.env['NX_NPM_URL'] : 'https://unpkg.com/' - // eslint-disable-next-line no-useless-catch - try { - const req = NX_NPM_URL + url - const response: AxiosResponse = await axios.get(req, { transformResponse: [] }) - return { content: response.data, cleanUrl: url } - } catch (e) { - throw e + async handleNpmImport(url: string): Promise { + const urls = ["https://cdn.jsdelivr.net/npm/", "https://unpkg.com/"] + process && process.env && process.env['NX_NPM_URL'] && urls.unshift(process.env['NX_NPM_URL']) + let content = null + // get response from all urls + for (let i = 0; i < urls.length; i++) { + try { + const req = urls[i] + url + const response: AxiosResponse = await axios.get(req, { transformResponse: [] }) + content = response.data + break + } catch (e) { + // try next url + } } + + + return { content: content, cleanUrl: url } + } - getHandlers (): Handler[] { + getHandlers(): Handler[] { return [ { type: 'github', @@ -174,7 +183,7 @@ export class RemixURLResolver { ] } - public async resolve (filePath: string, customHandlers?: Handler[]): Promise { + public async resolve(filePath: string, customHandlers?: Handler[]): Promise { let imported: Imported = this.previouslyHandled[filePath] if (imported) { return imported