Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from ipfs/feat/export-resolver
Browse files Browse the repository at this point in the history
feat: export resolver
  • Loading branch information
vasco-santos authored Jun 1, 2018
2 parents a02a518 + d9e56b8 commit da7bccf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,30 @@
This project consists on creating a HTTP response from an IPFS Hash. This response can be a file, a directory list view or the entry point of a web page.

```js
const ipfsHttpResponse = require('ipfs-http-response')
const { getResponse } = require('ipfs-http-response')

ipfsHttpResponse(ipfsNode, ipfsPath)
.then((response) => {
getResponse(ipfsNode, ipfsPath)
.then((result) => {
...
})
```

This module also exports the used ipfs resolver, which should be used when the response needs to be customized.

```js
const { resolver } = require('ipfs-http-response')

resolver.multihash(ipfsNode, ipfsPath)
.then((result) => {
...
})
```

```js
const { resolver } = require('ipfs-http-response')

resolver.directory(node, path, multihash)
.then((result) => {
...
})
```
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const header = (status = 200, statusText = 'OK', headers = {}) => ({
headers
})

module.exports = (ipfsNode, ipfsPath) => {
const response = (ipfsNode, ipfsPath) => {
// handle hash resolve error (simple hash, test for directory now)
const handleResolveError = (node, path, error) => {
if (error) {
Expand Down Expand Up @@ -100,3 +100,8 @@ module.exports = (ipfsNode, ipfsPath) => {
})
})
}

module.exports = {
getResponse: response,
resolver: resolver
}
8 changes: 4 additions & 4 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const loadFixture = require('aegir/fixtures')
const ipfs = require('ipfs')
const DaemonFactory = require('ipfsd-ctl')

const ipfsHttpResponse = require('../src')
const { getResponse } = require('../src')
const makeWebResponseEnv = require('./utils/web-response-env')

const df = DaemonFactory.create({ type: 'proc', exec: ipfs })
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('resolve file', function () {
})

it('should resolve a multihash', async () => {
const res = await ipfsHttpResponse(ipfs, `/ipfs/${file.cid}`)
const res = await getResponse(ipfs, `/ipfs/${file.cid}`)

expect(res).to.exist()
expect(res).to.deep.include({
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('resolve directory', function () {
})

it('should return the list of files of a directory', async () => {
const res = await ipfsHttpResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)

expect(res).to.exist()
})
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('resolve web page', function () {
})

it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => {
const res = await ipfsHttpResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)

expect(res).to.exist()
expect(res).to.equal(`/ipfs/${webpage.cid}/index.html`)
Expand Down

0 comments on commit da7bccf

Please sign in to comment.