|
|
|
@ -1,3 +1,4 @@ |
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
|
import axios, { AxiosResponse } from 'axios' |
|
|
|
|
import { BzzNode as Bzz } from '@erebos/bzz-node' |
|
|
|
|
|
|
|
|
@ -29,7 +30,7 @@ export class RemixURLResolver { |
|
|
|
|
|
|
|
|
|
constructor (gistToken?: string, protocol = 'http:') { |
|
|
|
|
this.previouslyHandled = {} |
|
|
|
|
this.gistAccessToken = gistToken ? gistToken : '' |
|
|
|
|
this.gistAccessToken = gistToken || '' |
|
|
|
|
this.protocol = protocol |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -39,15 +40,12 @@ export class RemixURLResolver { |
|
|
|
|
* @param filePath path of the file in github |
|
|
|
|
*/ |
|
|
|
|
async handleGithubCall (root: string, filePath: string): Promise<HandlerResponse> { |
|
|
|
|
let param = '?' |
|
|
|
|
param += this.gistAccessToken ? 'access_token=' + this.gistAccessToken : '' |
|
|
|
|
const regex = filePath.match(/blob\/([^/]+)\/(.*)/) |
|
|
|
|
let reference = 'master' |
|
|
|
|
if (regex) { |
|
|
|
|
// if we have /blob/master/+path we extract the branch name "master" and add it as a parameter to the github api
|
|
|
|
|
// the ref can be branch name, tag, commit id
|
|
|
|
|
reference = regex[1] |
|
|
|
|
param += '&ref=' + reference |
|
|
|
|
filePath = filePath.replace(`blob/${reference}/`, '') |
|
|
|
|
} |
|
|
|
|
// eslint-disable-next-line no-useless-catch
|
|
|
|
@ -93,7 +91,7 @@ export class RemixURLResolver { |
|
|
|
|
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'}); |
|
|
|
|
const bzz = new Bzz({ url: this.protocol + '//swarm-gateways.net' }) |
|
|
|
|
const url = bzz.getDownloadURL(cleanUrl, { mode: 'raw' }) |
|
|
|
|
const response: AxiosResponse = await axios.get(url) |
|
|
|
|
return { content: response.data, cleanUrl } |
|
|
|
@ -184,7 +182,7 @@ export class RemixURLResolver { |
|
|
|
|
const { content, cleanUrl } = await handler.handle(match) |
|
|
|
|
imported = { |
|
|
|
|
content, |
|
|
|
|
cleanUrl : cleanUrl ? cleanUrl : filePath, |
|
|
|
|
cleanUrl: cleanUrl || filePath, |
|
|
|
|
type: handler.type |
|
|
|
|
} |
|
|
|
|
this.previouslyHandled[filePath] = imported |
|
|
|
|