successful async/await implementation of IPFS handler; update suggestions

pull/7/head
0mkar 6 years ago
parent febb64511b
commit 42f7c500bf
  1. 57
      remix-resolve/src/resolve.ts
  2. 25
      remix-resolve/tests/test.js

@ -5,7 +5,7 @@ interface Imported {
type: string; type: string;
} }
interface PrvHandld { interface PreviouslyHandledImports {
[filePath: string]: Imported [filePath: string]: Imported
} }
@ -16,46 +16,53 @@ interface Handler {
} }
export class ImportResolver { export class ImportResolver {
previouslyHandled: PrvHandld[] previouslyHandled: PreviouslyHandledImports
constructor() { constructor() {
this.previouslyHandled = [] this.previouslyHandled = {}
} }
/**
* Handle an import statement based on github
* @params root The root of the github import statement
* @params filePath path of the file in github
*/
handleGithubCall(root: string, filePath: string) { handleGithubCall(root: string, filePath: string) {
return return
} }
/**
* Handle an import statement based on http
* @params url The url of the import statement
* @params cleanURL
*/
handleHttp(url: string, cleanURL: string) { handleHttp(url: string, cleanURL: string) {
return return
} }
/**
* Handle an import statement based on https
* @params url The url of the import statement
* @params cleanURL
*/
handleHttps(url: string, cleanURL: string) { handleHttps(url: string, cleanURL: string) {
return return
} }
handleSwarm(url: string, cleanURL: string) { handleSwarm(url: string, cleanURL: string) {
return return
} }
/**
* Handle an import statement based on IPFS
* @params url The url of the IPFS import statement
*/
async handleIPFS(url: string) { async handleIPFS(url: string) {
// replace ipfs:// with /ipfs/ // replace ipfs:// with /ipfs/
url = url.replace(/^ipfs:\/\/?/, 'ipfs/') url = url.replace(/^ipfs:\/\/?/, 'ipfs/')
console.log(url)
try { try {
const response = await axios.get('http://localhost:8080/' + url) const req = 'https://gateway.ipfs.io/' + url
// If you don't find greeter.sol on ipfs gateway use local
// const req = 'http://localhost:8080/' + url
const response = await axios.get(req)
return response.data return response.data
} catch (e) { } catch (e) {
throw e throw e
} }
/*axios.get('http://localhost:8080/' + url)
.then(function (response) {
// handle success
console.log(response);
return response.data
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
*/
} }
handleLocal(root: string, filePath: string) { handleLocal(root: string, filePath: string) {
return return
@ -65,27 +72,27 @@ export class ImportResolver {
{ {
type: 'github', type: 'github',
match: (url) => { return /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/.exec(url) }, match: (url) => { return /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/.exec(url) },
handle: (match) => { this.handleGithubCall(match[3], match[4]) } handle: (match) => this.handleGithubCall(match[3], match[4])
}, },
{ {
type: 'http', type: 'http',
match: (url) => { return /^(http?:\/\/?(.*))$/.exec(url) }, match: (url) => { return /^(http?:\/\/?(.*))$/.exec(url) },
handle: (match) => { this.handleHttp(match[1], match[2]) } handle: (match) => this.handleHttp(match[1], match[2])
}, },
{ {
type: 'https', type: 'https',
match: (url) => { return /^(https?:\/\/?(.*))$/.exec(url) }, match: (url) => { return /^(https?:\/\/?(.*))$/.exec(url) },
handle: (match) => { this.handleHttps(match[1], match[2]) } handle: (match) => this.handleHttps(match[1], match[2])
}, },
{ {
type: 'swarm', type: 'swarm',
match: (url) => { return /^(bzz-raw?:\/\/?(.*))$/.exec(url) }, match: (url) => { return /^(bzz-raw?:\/\/?(.*))$/.exec(url) },
handle: (match) => { this.handleSwarm(match[1], match[2]) } handle: (match) => this.handleSwarm(match[1], match[2])
}, },
{ {
type: 'ipfs', type: 'ipfs',
match: (url) => { return /^(ipfs:\/\/?.+)/.exec(url) }, match: (url) => { return /^(ipfs:\/\/?.+)/.exec(url) },
handle: (match) => { this.handleIPFS(match[1]) } handle: (match) => this.handleIPFS(match[1])
} }
] ]
} }
@ -99,7 +106,7 @@ export class ImportResolver {
const matchedHandler = handlers.filter(handler => handler.match(filePath)) const matchedHandler = handlers.filter(handler => handler.match(filePath))
const handler: Handler = matchedHandler[0] const handler: Handler = matchedHandler[0]
const match = handler.match(filePath) const match = handler.match(filePath)
const content: any = handler.handle(match) const content: string = await handler.handle(match)
imported = { imported = {
content, content,
cleanURL: filePath, cleanURL: filePath,

@ -55,26 +55,31 @@ describe('testRunner', function () {
}) })
// test IPFShandle // test IPFShandle
describe('test getting IPFS files', function() { describe('test getting IPFS files', function() {
const fileName = 'ipfs://' + 'QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn' const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn'
let results = [] let results = []
before(function(done) { before(async function() {
try {
const resolver = new rr.ImportResolver() const resolver = new rr.ImportResolver()
var sources = [] var sources = await resolver.resolve(fileName)
resolver.resolve(fileName)
.then(sources => {
console.log(sources)
results = sources results = sources
done() return
}) } catch (e) {
.catch(e => {
throw e throw e
}) }
}) })
it('should have 3 items', function () { it('should have 3 items', function () {
assert.equal(Object.keys(results).length, 3) assert.equal(Object.keys(results).length, 3)
}) })
it('should returns contract content of given local path', function () {
const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn',
type: 'ipfs'
}
assert.deepEqual(results, expt)
})
}) })
}) })
}) })

Loading…
Cancel
Save