try async ipfs request

pull/5370/head
0mkar 6 years ago
parent 8b2c6e7f3b
commit 4cfcd8d61d
  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": {
"axios": "^0.18.0",
"request": "^2.88.0",
"solc": "^0.5.0",
"url": "^0.11.0",
"valid-url": "^1.0.9"

@ -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) {

@ -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)
})
})
})
})

Loading…
Cancel
Save