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

Commit b7b0b3f

Browse files
committed
feat!: update http-client DAG API to match [email protected]
Fixes: #3914 Ref: https://github.com/ipfs/go-ipfs/blob/master/CHANGELOG.md#v0100-2021-09-30 --format and --input-enc have been replaced with --input-codec and --store-codec and mean something a little different. You now supply raw input and instruct the server which --input-codec that data is in which it will decode, then re-encode with --store-codec before storing it and providing you with the CID. We accept plain JavaScript objects to encode with --store-codec via the API here, defaulting to dag-cbor, and send that to the server as encoded bytes using that codec, to be stored using that codec. If you supply an --input-codec then we assume you're supplying raw, encoded bytes using that codec and we pass that directly on to the server to handle.
1 parent 605c83b commit b7b0b3f

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

packages/ipfs-core-types/src/dag/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ export interface GetResult {
132132

133133
export interface PutOptions extends AbortOptions, PreloadOptions {
134134
/**
135-
* The codec to use to create the CID (defaults to 'dag-cbor')
135+
* The codec that the input object is encoded with (defaults to 'dag-json')
136+
*/
137+
inputCodec?: string
138+
139+
/**
140+
* The codec that the stored object will be encoded with (defaults to 'dag-cbor')
136141
*/
137142
format?: string
138143

packages/ipfs-http-client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
},
5656
"dependencies": {
5757
"@ipld/dag-cbor": "^6.0.5",
58+
"@ipld/dag-json": "^8.0.1",
5859
"@ipld/dag-pb": "^2.1.3",
5960
"abort-controller": "^3.0.0",
6061
"any-signal": "^2.1.2",
@@ -76,7 +77,7 @@
7677
"devDependencies": {
7778
"aegir": "^35.1.1",
7879
"delay": "^5.0.0",
79-
"go-ipfs": "0.9.1",
80+
"go-ipfs": "0.10.0",
8081
"ipfsd-ctl": "^10.0.4",
8182
"it-all": "^1.0.4",
8283
"it-first": "^1.0.4",

packages/ipfs-http-client/src/dag/put.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,31 @@ export const createPut = (codecs, options) => {
2121
*/
2222
const put = async (dagNode, options = {}) => {
2323
const settings = {
24-
format: 'dag-cbor',
24+
storeCodec: 'dag-cbor',
2525
hashAlg: 'sha2-256',
26-
inputEnc: 'raw',
2726
...options
2827
}
2928

30-
const codec = await codecs.getCodec(settings.format)
31-
const serialized = codec.encode(dagNode)
29+
let serialized
30+
31+
if (settings.inputCodec) {
32+
// if you supply an inputCodec, we assume you're passing in a raw, encoded
33+
// block using that codec, so we'll just pass that on to the server and let
34+
// it deal with the decode/encode/store cycle
35+
if (!(dagNode instanceof Uint8Array)) {
36+
throw new Error('Can only inputCodec on raw bytes that can be decoded')
37+
}
38+
serialized = dagNode
39+
} else {
40+
// if you don't supply an inputCodec, we assume you've passed in a JavaScript
41+
// object you want to have encoded using storeCodec, so we'll prepare it for
42+
// you if we have the codec
43+
const storeCodec = await codecs.getCodec(settings.storeCodec)
44+
serialized = storeCodec.encode(dagNode)
45+
// now we have a serialized form, the server should be told to receive it
46+
// in that format
47+
settings.inputCodec = settings.storeCodec
48+
}
3249

3350
// allow aborting requests on body errors
3451
const controller = new AbortController()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Multicodecs } from 'ipfs-core-utils/multicodecs'
55
import { Multihashes } from 'ipfs-core-utils/multihashes'
66
import * as dagPB from '@ipld/dag-pb'
77
import * as dagCBOR from '@ipld/dag-cbor'
8+
import * as dagJSON from '@ipld/dag-json'
89
import { identity } from 'multiformats/hashes/identity'
910
import { bases, hashes, codecs } from 'multiformats/basics'
1011
import { createBitswap } from './bitswap/index.js'
@@ -79,7 +80,7 @@ export function create (options = {}) {
7980
/** @type {BlockCodec[]} */
8081
const blockCodecs = Object.values(codecs);
8182

82-
[dagPB, dagCBOR, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
83+
[dagPB, dagCBOR, dagJSON, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
8384

8485
const multicodecs = new Multicodecs({
8586
codecs: blockCodecs,

packages/ipfs-http-client/test/dag.spec.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { expect } from 'aegir/utils/chai.js'
66
import * as dagPB from '@ipld/dag-pb'
77
import * as dagCBOR from '@ipld/dag-cbor'
88
import * as raw from 'multiformats/codecs/raw'
9-
import { base58btc } from 'multiformats/bases/base58'
109
import { base32 } from 'multiformats/bases/base32'
1110
import { create as httpClient } from '../src/index.js'
1211
import { factory } from './utils/factory.js'
@@ -22,25 +21,25 @@ describe('.dag', function () {
2221

2322
after(() => f.clean())
2423

25-
it('should be able to put and get a DAG node with format dag-pb', async () => {
24+
it('should be able to put and get a DAG node with dag-pb codec', async () => {
2625
const data = uint8ArrayFromString('some data')
2726
const node = {
2827
Data: data,
2928
Links: []
3029
}
3130

32-
const cid = await ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256', cidVersion: 0 })
31+
const cid = await ipfs.dag.put(node, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
3332
expect(cid.code).to.equal(dagPB.code)
34-
expect(cid.toString(base58btc)).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
33+
expect(cid.toV0().toString()).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
3534

3635
const result = await ipfs.dag.get(cid)
3736

3837
expect(result.value.Data).to.deep.equal(data)
3938
})
4039

41-
it('should be able to put and get a DAG node with format dag-cbor', async () => {
40+
it('should be able to put and get a DAG node with dag-cbor codec', async () => {
4241
const cbor = { foo: 'dag-cbor-bar' }
43-
const cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
42+
const cid = await ipfs.dag.put(cbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
4443

4544
expect(cid.code).to.equal(dagCBOR.code)
4645
expect(cid.toString(base32)).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
@@ -50,9 +49,9 @@ describe('.dag', function () {
5049
expect(result.value).to.deep.equal(cbor)
5150
})
5251

53-
it('should be able to put and get a DAG node with format raw', async () => {
52+
it('should be able to put and get a DAG node with raw codec', async () => {
5453
const node = uint8ArrayFromString('some data')
55-
const cid = await ipfs.dag.put(node, { format: 'raw', hashAlg: 'sha2-256' })
54+
const cid = await ipfs.dag.put(node, { storeCodec: 'raw', hashAlg: 'sha2-256' })
5655

5756
expect(cid.code).to.equal(raw.code)
5857
expect(cid.toString(base32)).to.equal('bafkreiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y')
@@ -70,19 +69,28 @@ describe('.dag', function () {
7069
await expect(ipfs.dag.get(cid)).to.eventually.be.rejectedWith(/No codec found/)
7170
})
7271

73-
it('should error when putting node with esoteric format', () => {
72+
it('should error when putting node with esoteric codec', () => {
7473
const node = uint8ArrayFromString('some data')
7574

76-
return expect(ipfs.dag.put(node, { format: 'git-raw', hashAlg: 'sha2-256' })).to.eventually.be.rejectedWith(/No codec found/)
75+
return expect(ipfs.dag.put(node, { storeCodec: 'git-raw', hashAlg: 'sha2-256' })).to.eventually.be.rejectedWith(/No codec found/)
7776
})
7877

79-
it('should attempt to load an unsupported format', async () => {
80-
let askedToLoadFormat
78+
it('should pass through raw bytes with inputCodec', async () => {
79+
const node = uint8ArrayFromString('blob 9\0some data')
80+
// we don't support git-raw in the HTTP client, but inputCodec and a Uint8Array should make
81+
// the raw data pass through to go-ipfs, which does talk git-raw
82+
const cid = await ipfs.dag.put(node, { inputCodec: 'git-raw', storeCodec: 'git-raw', hashAlg: 'sha1' })
83+
expect(cid.code).to.equal(0x78)
84+
expect(cid.toString(base32)).to.equal('baf4bcfd4azdl7vj4d4hnix75qfld6mabo4l4uwa')
85+
})
86+
87+
it('should attempt to load an unsupported codec', async () => {
88+
let askedToLoadCodec
8189
const ipfs2 = httpClient({
8290
url: `http://${ipfs.apiHost}:${ipfs.apiPort}`,
8391
ipld: {
84-
loadCodec: (format) => {
85-
askedToLoadFormat = format === 'git-raw'
92+
loadCodec: (codec) => {
93+
askedToLoadCodec = codec === 'boop'
8694
return {
8795
encode: (buf) => buf
8896
}
@@ -93,9 +101,9 @@ describe('.dag', function () {
93101
const node = uint8ArrayFromString('some data')
94102

95103
// error is from go-ipfs, this means the client serialized it ok
96-
await expect(ipfs2.dag.put(node, { format: 'git-raw', hashAlg: 'sha2-256' })).to.eventually.be.rejectedWith(/no parser for format "git-raw"/)
104+
await expect(ipfs2.dag.put(node, { storeCodec: 'boop', hashAlg: 'sha2-256' })).to.eventually.be.rejectedWith(/unknown multicodec: "boop"/)
97105

98-
expect(askedToLoadFormat).to.be.true()
106+
expect(askedToLoadCodec).to.be.true()
99107
})
100108

101109
it('should allow formats to be specified without overwriting others', async () => {
@@ -115,7 +123,7 @@ describe('.dag', function () {
115123
hello: 'world'
116124
}
117125
const cid1 = await ipfs2.dag.put(dagCborNode, {
118-
format: 'dag-cbor',
126+
storeCodec: 'dag-cbor',
119127
hashAlg: 'sha2-256'
120128
})
121129

@@ -124,7 +132,7 @@ describe('.dag', function () {
124132
Links: []
125133
}
126134
const cid2 = await ipfs2.dag.put(dagPbNode, {
127-
format: 'dag-pb',
135+
storeCodec: 'dag-pb',
128136
hashAlg: 'sha2-256'
129137
})
130138

0 commit comments

Comments
 (0)