try async ipfs request

pull/7/head
0mkar 6 years ago
parent a98c8ac38f
commit e07f3b903e
  1. 1
      remix-resolve/package.json
  2. 31
      remix-resolve/src/resolve.ts
  3. 27
      remix-resolve/tests/test.js

@ -30,7 +30,6 @@
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"request": "^2.88.0",
"solc": "^0.5.0", "solc": "^0.5.0",
"url": "^0.11.0", "url": "^0.11.0",
"valid-url": "^1.0.9" "valid-url": "^1.0.9"

@ -1,3 +1,4 @@
import axios from 'axios'
interface Imported { interface Imported {
content: string; content: string;
cleanURL: string; cleanURL: string;
@ -31,8 +32,30 @@ export class ImportResolver {
handleSwarm(url: string, cleanURL: string) { handleSwarm(url: string, cleanURL: string) {
return return
} }
handleIPFS(url: string) { async handleIPFS(url: string) {
return // 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) { handleLocal(root: string, filePath: string) {
return 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] var imported: Imported = this.previouslyHandled[filePath]
if(imported) { if(imported) {
return imported return imported
} }
const builtinHandlers: Handler[] = this.getHandlers() const builtinHandlers: Handler[] = this.getHandlers()
const handlers: Handler[] = [...builtinHandlers, ...customHandlers] const handlers: Handler[] = customHandlers ? [...builtinHandlers, ...customHandlers] : [...builtinHandlers]
handlers.forEach(handler => { handlers.forEach(handler => {
const match = handler.match(filePath) const match = handler.match(filePath)
if(match) { if(match) {

@ -6,7 +6,7 @@ const path = require('path')
describe('testRunner', function () { describe('testRunner', function () {
describe('#resolve', function() { describe('#resolve', function() {
describe('test example_1 [local imports]]', 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 = {} let results = {}
before(function (done) { before(function (done) {
@ -30,7 +30,7 @@ describe('testRunner', function () {
handle: (match) => { return handleLocal(match[2], match[3]) } handle: (match) => { return handleLocal(match[2], match[3]) }
} }
] ]
resolver.resolve(filename, localFSHandler) resolver.resolve(fileName, localFSHandler)
.then(sources => { .then(sources => {
results = sources results = sources
done() done()
@ -53,5 +53,28 @@ describe('testRunner', function () {
assert.deepEqual(results, expt) 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)
})
})
}) })
}) })

Loading…
Cancel
Save