From 42f7c500bf42f58352dfa73f06d61d7f44476970 Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Wed, 12 Dec 2018 15:16:07 +0530 Subject: [PATCH] successful async/await implementation of IPFS handler; update suggestions --- remix-resolve/src/resolve.ts | 57 ++++++++++++++++++++---------------- remix-resolve/tests/test.js | 31 ++++++++++++-------- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/remix-resolve/src/resolve.ts b/remix-resolve/src/resolve.ts index 16549fc299..ca67d272be 100644 --- a/remix-resolve/src/resolve.ts +++ b/remix-resolve/src/resolve.ts @@ -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, diff --git a/remix-resolve/tests/test.js b/remix-resolve/tests/test.js index 6b6b84f82c..8d31ab0de7 100644 --- a/remix-resolve/tests/test.js +++ b/remix-resolve/tests/test.js @@ -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) + }) }) }) })