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

Commit 61d0981

Browse files
authored
fix: only use public api in http api server (#3660)
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
1 parent 06bd4f3 commit 61d0981

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

packages/ipfs-http-server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"ipfs-core-utils": "^0.7.2",
4444
"ipfs-http-gateway": "^0.3.2",
4545
"ipfs-unixfs": "^4.0.3",
46+
"ipld-block": "^0.11.1",
4647
"ipld-dag-pb": "^0.22.1",
4748
"it-all": "^1.0.4",
4849
"it-drain": "^1.0.3",

packages/ipfs-http-server/src/api/resources/dag.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict'
22

33
const multipart = require('../../utils/multipart-request-parser')
4-
const mh = require('multihashing-async').multihash
4+
const mha = require('multihashing-async')
5+
const mh = mha.multihash
56
const Joi = require('../../utils/joi')
67
const Boom = require('@hapi/boom')
78
const {
89
cidToString
910
} = require('ipfs-core-utils/src/cid')
1011
const all = require('it-all')
1112
const uint8ArrayToString = require('uint8arrays/to-string')
13+
const Block = require('ipld-block')
14+
const CID = require('cids')
1215

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

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

179-
node = await ipldFormat.util.deserialize(data)
182+
const {
183+
value
184+
} = await request.server.app.ipfs.dag.get(cid)
185+
node = value
180186
}
181187

182188
return {
@@ -197,6 +203,7 @@ exports.put = {
197203
pin: Joi.boolean().default(false),
198204
hash: Joi.string().valid(...Object.keys(mh.names)).default('sha2-256'),
199205
cidBase: Joi.cidBase(),
206+
cidVersion: Joi.number().integer().valid(0, 1).default(1),
200207
timeout: Joi.timeout()
201208
})
202209
.rename('input-enc', 'inputEncoding', {
@@ -207,6 +214,10 @@ exports.put = {
207214
override: true,
208215
ignoreUndefined: true
209216
})
217+
.rename('cid-version', 'cidVersion', {
218+
override: true,
219+
ignoreUndefined: true
220+
})
210221
}
211222
},
212223

packages/ipfs-http-server/src/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const LOG_ERROR = 'ipfs:http-api:error'
1717
* @typedef {import('./types').Server} Server
1818
* @typedef {import('ipld')} IPLD
1919
* @typedef {import('libp2p')} libp2p
20-
* @typedef {IPFS & { ipld: IPLD, libp2p: libp2p }} JSIPFS
2120
*/
2221

2322
/**
@@ -38,8 +37,8 @@ function hapiInfoToMultiaddr (info) {
3837

3938
/**
4039
* @param {string | string[]} serverAddrs
41-
* @param {(host: string, port: string, ipfs: JSIPFS, cors: Record<string, any>) => Promise<Server>} createServer
42-
* @param {JSIPFS} ipfs
40+
* @param {(host: string, port: string, ipfs: IPFS, cors: Record<string, any>) => Promise<Server>} createServer
41+
* @param {IPFS} ipfs
4342
* @param {Record<string, any>} cors
4443
*/
4544
async function serverCreator (serverAddrs, createServer, ipfs, cors) {
@@ -61,7 +60,7 @@ async function serverCreator (serverAddrs, createServer, ipfs, cors) {
6160

6261
class HttpApi {
6362
/**
64-
* @param {JSIPFS} ipfs
63+
* @param {IPFS} ipfs
6564
*/
6665
constructor (ipfs) {
6766
this._ipfs = ipfs
@@ -98,7 +97,7 @@ class HttpApi {
9897
/**
9998
* @param {string} host
10099
* @param {string} port
101-
* @param {JSIPFS} ipfs
100+
* @param {IPFS} ipfs
102101
* @param {Record<string, any>} cors
103102
*/
104103
async _createApiServer (host, port, ipfs, cors) {

packages/ipfs-http-server/src/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import libp2p from 'libp2p'
77

88
declare module '@hapi/hapi' {
99
interface ServerApplicationState {
10-
ipfs: IPFS & { ipld: IPLD, libp2p: libp2p }
10+
ipfs: IPFS
1111
}
1212
interface RequestApplicationState {
1313
signal: AbortSignal

packages/ipfs-http-server/test/inject/dag.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ describe('/dag', () => {
3838
put: sinon.stub(),
3939
resolve: sinon.stub()
4040
},
41-
ipld: {
42-
getFormat: sinon.stub()
41+
block: {
42+
put: sinon.stub()
4343
}
4444
}
4545
})
@@ -295,16 +295,14 @@ describe('/dag', () => {
295295
expect(res).to.have.deep.nested.property('result.Cid', { '/': cid.toString() })
296296
})
297297

298-
it('should attempt to load an unsupported format', async () => {
298+
it('adds a node with an esoteric format', async () => {
299+
const cid = new CID('baf4beiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y')
299300
const data = Buffer.from('some data')
300301
const codec = 'git-raw'
301-
const format = {
302-
util: {
303-
deserialize: (buf) => buf
304-
}
305-
}
306-
ipfs.ipld.getFormat.withArgs(codec).returns(format)
307302

303+
ipfs.dag.get.withArgs(cid).returns({
304+
value: data
305+
})
308306
ipfs.dag.put.withArgs(data, {
309307
...defaultOptions,
310308
format: codec
@@ -316,7 +314,7 @@ describe('/dag', () => {
316314
...await toHeadersAndPayload(data)
317315
}, { ipfs })
318316

319-
expect(ipfs.ipld.getFormat.calledWith(codec)).to.be.true()
317+
expect(ipfs.block.put.called).to.be.true()
320318
expect(res).to.have.property('statusCode', 200)
321319
expect(res).to.have.deep.nested.property('result.Cid', { '/': cid.toString() })
322320
})

0 commit comments

Comments
 (0)