pull/3404/head
filip mertens 2 years ago committed by Aniket
parent 90b9ba7293
commit 9a8aa12041
  1. 45
      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<HandlerResponse> {
async handleGithubCall(root: string, filePath: string): Promise<HandlerResponse> {
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<HandlerResponse> {
async handleHttp(url: string, cleanUrl: string): Promise<HandlerResponse> {
// 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<HandlerResponse> {
async handleHttps(url: string, cleanUrl: string): Promise<HandlerResponse> {
// 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<HandlerResponse> {
async handleSwarm(url: string, cleanUrl: string): Promise<HandlerResponse> {
// 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<HandlerResponse> {
async handleIPFS(url: string): Promise<HandlerResponse> {
// 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<HandlerResponse> {
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<HandlerResponse> {
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<Imported> {
public async resolve(filePath: string, customHandlers?: Handler[]): Promise<Imported> {
let imported: Imported = this.previouslyHandled[filePath]
if (imported) {
return imported

Loading…
Cancel
Save