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. 31
      remix-resolve/tests/test.js

@ -5,7 +5,7 @@ interface Imported {
type: string;
}
interface PrvHandld {
interface PreviouslyHandledImports {
[filePath: string]: Imported
}
@ -16,46 +16,53 @@ interface Handler {
}
export class ImportResolver {
previouslyHandled: PrvHandld[]
previouslyHandled: PreviouslyHandledImports
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) {
return
}
/**
* Handle an import statement based on http
* @params url The url of the import statement
* @params cleanURL
*/
handleHttp(url: string, cleanURL: string) {
return
}
/**
* Handle an import statement based on https
* @params url The url of the import statement
* @params cleanURL
*/
handleHttps(url: string, cleanURL: string) {
return
}
handleSwarm(url: string, cleanURL: string) {
return
}
/**
* Handle an import statement based on IPFS
* @params url The url of the IPFS import statement
*/
async handleIPFS(url: string) {
// replace ipfs:// with /ipfs/
url = url.replace(/^ipfs:\/\/?/, 'ipfs/')
console.log(url)
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
} catch (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) {
return
@ -65,27 +72,27 @@ export class ImportResolver {
{
type: 'github',
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',
match: (url) => { return /^(http?:\/\/?(.*))$/.exec(url) },
handle: (match) => { this.handleHttp(match[1], match[2]) }
handle: (match) => this.handleHttp(match[1], match[2])
},
{
type: 'https',
match: (url) => { return /^(https?:\/\/?(.*))$/.exec(url) },
handle: (match) => { this.handleHttps(match[1], match[2]) }
handle: (match) => this.handleHttps(match[1], match[2])
},
{
type: 'swarm',
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',
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 handler: Handler = matchedHandler[0]
const match = handler.match(filePath)
const content: any = handler.handle(match)
const content: string = await handler.handle(match)
imported = {
content,
cleanURL: filePath,

@ -55,26 +55,31 @@ describe('testRunner', function () {
})
// test IPFShandle
describe('test getting IPFS files', function() {
const fileName = 'ipfs://' + 'QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn'
const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn'
let results = []
before(function(done) {
const resolver = new rr.ImportResolver()
var sources = []
resolver.resolve(fileName)
.then(sources => {
console.log(sources)
results = sources
done()
})
.catch(e => {
throw e
})
before(async function() {
try {
const resolver = new rr.ImportResolver()
var sources = await resolver.resolve(fileName)
results = sources
return
} catch (e) {
throw e
}
})
it('should have 3 items', function () {
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