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

Commit

Permalink
fix: only use public api in http api server (#3660)
Browse files Browse the repository at this point in the history
We needed to reach into ipld in order to get at the formats so we can deserialize the data.

Instead, put the data as a block and then read it out using ipfs.dag.get.

Slightly inefficient but better to use the public API.

Fixes #3639
  • Loading branch information
achingbrain committed Apr 30, 2021
1 parent 06bd4f3 commit 61d0981
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/ipfs-http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ipfs-core-utils": "^0.7.2",
"ipfs-http-gateway": "^0.3.2",
"ipfs-unixfs": "^4.0.3",
"ipld-block": "^0.11.1",
"ipld-dag-pb": "^0.22.1",
"it-all": "^1.0.4",
"it-drain": "^1.0.3",
Expand Down
25 changes: 18 additions & 7 deletions packages/ipfs-http-server/src/api/resources/dag.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict'

const multipart = require('../../utils/multipart-request-parser')
const mh = require('multihashing-async').multihash
const mha = require('multihashing-async')
const mh = mha.multihash
const Joi = require('../../utils/joi')
const Boom = require('@hapi/boom')
const {
cidToString
} = require('ipfs-core-utils/src/cid')
const all = require('it-all')
const uint8ArrayToString = require('uint8arrays/to-string')
const Block = require('ipld-block')
const CID = require('cids')

/**
* @param {undefined | Uint8Array | Record<string, any>} obj
Expand Down Expand Up @@ -169,14 +172,17 @@ exports.put = {
}
} else {
// the node is an uncommon format which the client should have
// serialized so deserialize it before continuing
const ipldFormat = await request.server.app.ipfs.ipld.getFormat(format)
// serialized so add it to the block store and fetch it deserialized
// before continuing
const hash = await mha(data, request.query.hash)
const cid = new CID(request.query.cidVersion, format, hash)

if (!ipldFormat) {
throw new Error(`Missing IPLD format "${format}"`)
}
await request.server.app.ipfs.block.put(new Block(data, cid))

node = await ipldFormat.util.deserialize(data)
const {
value
} = await request.server.app.ipfs.dag.get(cid)
node = value
}

return {
Expand All @@ -197,6 +203,7 @@ exports.put = {
pin: Joi.boolean().default(false),
hash: Joi.string().valid(...Object.keys(mh.names)).default('sha2-256'),
cidBase: Joi.cidBase(),
cidVersion: Joi.number().integer().valid(0, 1).default(1),
timeout: Joi.timeout()
})
.rename('input-enc', 'inputEncoding', {
Expand All @@ -207,6 +214,10 @@ exports.put = {
override: true,
ignoreUndefined: true
})
.rename('cid-version', 'cidVersion', {
override: true,
ignoreUndefined: true
})
}
},

Expand Down
9 changes: 4 additions & 5 deletions packages/ipfs-http-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const LOG_ERROR = 'ipfs:http-api:error'
* @typedef {import('./types').Server} Server
* @typedef {import('ipld')} IPLD
* @typedef {import('libp2p')} libp2p
* @typedef {IPFS & { ipld: IPLD, libp2p: libp2p }} JSIPFS
*/

/**
Expand All @@ -38,8 +37,8 @@ function hapiInfoToMultiaddr (info) {

/**
* @param {string | string[]} serverAddrs
* @param {(host: string, port: string, ipfs: JSIPFS, cors: Record<string, any>) => Promise<Server>} createServer
* @param {JSIPFS} ipfs
* @param {(host: string, port: string, ipfs: IPFS, cors: Record<string, any>) => Promise<Server>} createServer
* @param {IPFS} ipfs
* @param {Record<string, any>} cors
*/
async function serverCreator (serverAddrs, createServer, ipfs, cors) {
Expand All @@ -61,7 +60,7 @@ async function serverCreator (serverAddrs, createServer, ipfs, cors) {

class HttpApi {
/**
* @param {JSIPFS} ipfs
* @param {IPFS} ipfs
*/
constructor (ipfs) {
this._ipfs = ipfs
Expand Down Expand Up @@ -98,7 +97,7 @@ class HttpApi {
/**
* @param {string} host
* @param {string} port
* @param {JSIPFS} ipfs
* @param {IPFS} ipfs
* @param {Record<string, any>} cors
*/
async _createApiServer (host, port, ipfs, cors) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-server/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import libp2p from 'libp2p'

declare module '@hapi/hapi' {
interface ServerApplicationState {
ipfs: IPFS & { ipld: IPLD, libp2p: libp2p }
ipfs: IPFS
}
interface RequestApplicationState {
signal: AbortSignal
Expand Down
18 changes: 8 additions & 10 deletions packages/ipfs-http-server/test/inject/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('/dag', () => {
put: sinon.stub(),
resolve: sinon.stub()
},
ipld: {
getFormat: sinon.stub()
block: {
put: sinon.stub()
}
}
})
Expand Down Expand Up @@ -295,16 +295,14 @@ describe('/dag', () => {
expect(res).to.have.deep.nested.property('result.Cid', { '/': cid.toString() })
})

it('should attempt to load an unsupported format', async () => {
it('adds a node with an esoteric format', async () => {
const cid = new CID('baf4beiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y')
const data = Buffer.from('some data')
const codec = 'git-raw'
const format = {
util: {
deserialize: (buf) => buf
}
}
ipfs.ipld.getFormat.withArgs(codec).returns(format)

ipfs.dag.get.withArgs(cid).returns({
value: data
})
ipfs.dag.put.withArgs(data, {
...defaultOptions,
format: codec
Expand All @@ -316,7 +314,7 @@ describe('/dag', () => {
...await toHeadersAndPayload(data)
}, { ipfs })

expect(ipfs.ipld.getFormat.calledWith(codec)).to.be.true()
expect(ipfs.block.put.called).to.be.true()
expect(res).to.have.property('statusCode', 200)
expect(res).to.have.deep.nested.property('result.Cid', { '/': cid.toString() })
})
Expand Down

0 comments on commit 61d0981

Please sign in to comment.