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

Commit f95fbfc

Browse files
committed
feat!: match [email protected] dag put options and semantics
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 09c7bf7 commit f95fbfc

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

packages/ipfs-http-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"devDependencies": {
7777
"aegir": "^35.1.1",
7878
"delay": "^5.0.0",
79-
"go-ipfs": "0.9.1",
79+
"go-ipfs": "0.10.0",
8080
"ipfsd-ctl": "^10.0.4",
8181
"it-all": "^1.0.4",
8282
"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/test/dag.spec.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
55
import { expect } from 'aegir/utils/chai.js'
6+
import { CID } from 'multiformats/cid'
67
import * as dagPB from '@ipld/dag-pb'
78
import * as dagCBOR from '@ipld/dag-cbor'
89
import * as raw from 'multiformats/codecs/raw'
@@ -14,33 +15,33 @@ const f = factory()
1415

1516
let ipfs
1617

17-
describe('.dag', function () {
18+
describe.only('.dag', function () {
1819
this.timeout(20 * 1000)
1920
before(async function () {
2021
ipfs = (await f.spawn()).api
2122
})
2223

2324
after(() => f.clean())
2425

25-
it('should be able to put and get a DAG node with format dag-pb', async () => {
26+
it('should be able to put and get a DAG node with dag-pb codec', async () => {
2627
const data = uint8ArrayFromString('some data')
2728
const node = {
2829
Data: data,
2930
Links: []
3031
}
3132

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

3637
const result = await ipfs.dag.get(cid)
3738

3839
expect(result.value.Data).to.deep.equal(data)
3940
})
4041

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

4546
expect(cid.code).to.equal(dagCBOR.code)
4647
expect(cid.toString(base32)).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
@@ -50,9 +51,9 @@ describe('.dag', function () {
5051
expect(result.value).to.deep.equal(cbor)
5152
})
5253

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

5758
expect(cid.code).to.equal(raw.code)
5859
expect(cid.toString(base32)).to.equal('bafkreiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y')
@@ -70,19 +71,28 @@ describe('.dag', function () {
7071
await expect(ipfs.dag.get(cid)).to.eventually.be.rejectedWith(/No codec found/)
7172
})
7273

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

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

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

95105
// 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"/)
106+
await expect(ipfs2.dag.put(node, { storeCodec: 'boop', hashAlg: 'sha2-256' })).to.eventually.be.rejectedWith(/unknown multicodec: "boop"/)
97107

98-
expect(askedToLoadFormat).to.be.true()
108+
expect(askedToLoadCodec).to.be.true()
99109
})
100110

101111
it('should allow formats to be specified without overwriting others', async () => {
@@ -115,7 +125,7 @@ describe('.dag', function () {
115125
hello: 'world'
116126
}
117127
const cid1 = await ipfs2.dag.put(dagCborNode, {
118-
format: 'dag-cbor',
128+
storeCodec: 'dag-cbor',
119129
hashAlg: 'sha2-256'
120130
})
121131

@@ -124,7 +134,7 @@ describe('.dag', function () {
124134
Links: []
125135
}
126136
const cid2 = await ipfs2.dag.put(dagPbNode, {
127-
format: 'dag-pb',
137+
storeCodec: 'dag-pb',
128138
hashAlg: 'sha2-256'
129139
})
130140

0 commit comments

Comments
 (0)