diff --git a/remix-resolve/package.json b/remix-resolve/package.json index 24ff61dfd0..1e2a6544b0 100644 --- a/remix-resolve/package.json +++ b/remix-resolve/package.json @@ -30,7 +30,6 @@ }, "dependencies": { "axios": "^0.18.0", - "request": "^2.88.0", "solc": "^0.5.0", "url": "^0.11.0", "valid-url": "^1.0.9" diff --git a/remix-resolve/src/resolve.ts b/remix-resolve/src/resolve.ts index 45c18189c5..74e6ee0f6d 100644 --- a/remix-resolve/src/resolve.ts +++ b/remix-resolve/src/resolve.ts @@ -1,3 +1,4 @@ +import axios from 'axios' interface Imported { content: string; cleanURL: string; @@ -31,8 +32,30 @@ export class ImportResolver { handleSwarm(url: string, cleanURL: string) { return } - handleIPFS(url: string) { - return + 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) + 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 @@ -66,13 +89,13 @@ export class ImportResolver { } ] } - async resolve(filePath: string, customHandlers: Handler[]) { + async resolve(filePath: string, customHandlers?: Handler[]) { var imported: Imported = this.previouslyHandled[filePath] if(imported) { return imported } const builtinHandlers: Handler[] = this.getHandlers() - const handlers: Handler[] = [...builtinHandlers, ...customHandlers] + const handlers: Handler[] = customHandlers ? [...builtinHandlers, ...customHandlers] : [...builtinHandlers] handlers.forEach(handler => { const match = handler.match(filePath) if(match) { diff --git a/remix-resolve/tests/test.js b/remix-resolve/tests/test.js index 74354b3a4d..6b6b84f82c 100644 --- a/remix-resolve/tests/test.js +++ b/remix-resolve/tests/test.js @@ -6,7 +6,7 @@ const path = require('path') describe('testRunner', function () { describe('#resolve', function() { describe('test example_1 [local imports]]', function () { - let filename = '../remix-resolve/tests/example_1/greeter.sol' + const fileName = '../remix-resolve/tests/example_1/greeter.sol' let results = {} before(function (done) { @@ -30,7 +30,7 @@ describe('testRunner', function () { handle: (match) => { return handleLocal(match[2], match[3]) } } ] - resolver.resolve(filename, localFSHandler) + resolver.resolve(fileName, localFSHandler) .then(sources => { results = sources done() @@ -53,5 +53,28 @@ describe('testRunner', function () { assert.deepEqual(results, expt) }) }) + // test IPFShandle + describe('test getting IPFS files', function() { + 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 + }) + }) + + it('should have 3 items', function () { + assert.equal(Object.keys(results).length, 3) + }) + }) }) })