Skip to content

Commit

Permalink
fix: gracefully handle cases where content URIs are invalid
Browse files Browse the repository at this point in the history
Prior to this commit, all content URIs of application versions are
expected to follow the format `<provider>:<identifier>`. In case a
content URI doesn't follow this format, an error is thrown.

We now ensure that in such cases, the code still executes but resolves
early with an error or warning respectively.

It also fixes #27 where parsing content URIs that contain more than
one split character (e.g. `:` in `http://localhost:8080/foo/bar`) splits
the `identifier` as well (which we don't want).

Fixes #20, #27
  • Loading branch information
0x-r4bbit committed Aug 17, 2018
1 parent 9e3c52f commit d53212f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/index.js
Expand Up @@ -62,14 +62,30 @@ module.exports = (web3, options = {}) => {
version.split('.').map((part) => parseInt(part))

const getApplicationInfo = (contentURI) => {
const [provider, location] = contentURI.split(/:(.+)/)
let error;

if (!provider || !location) {
error = `contentURI: ${contentURI} is invalid.`
} else if (!providers[provider]) {
error = `Provider: ${provider} is not supported`
}

if (error) {
return Promise.resolve({
error,
contentURI
})
}

return Promise.all([
readFileFromApplication(contentURI, 'manifest.json'),
readFileFromApplication(contentURI, 'artifact.json')
])
.then((files) => files.map(JSON.parse))
.then(
([ manifest, module ]) => {
const [provider, location] = contentURI.split(':')
const [provider, location] = contentURI.split(/:(.+)/)

return Object.assign(
manifest,
Expand All @@ -79,7 +95,7 @@ module.exports = (web3, options = {}) => {
}
)
.catch(() => {
const [provider, location] = contentURI.split(':')
const [provider, location] = contentURI.split(/:(.+)/)
return {
content: { provider, location }
}
Expand Down

0 comments on commit d53212f

Please sign in to comment.