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

Commit 38c01be

Browse files
authored
feat: update DAG API to match [email protected] changes (#3917)
Co-authored-by: Marcin Rataj <[email protected]> Co-authored-by: achingbrain <[email protected]> BREAKING CHANGE: `ipfs.dag.put` no longer accepts a `format` arg, it is now `storeCodec` and `inputCodec`. `'json'` has become `'dag-json'`, `'cbor'` has become `'dag-cbor'` and so on
1 parent 0c269cf commit 38c01be

File tree

36 files changed

+584
-384
lines changed

36 files changed

+584
-384
lines changed

.github/workflows/test.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ jobs:
366366
- name: ipfs custom ipfs repo
367367
repo: https://github.com/ipfs-examples/js-ipfs-custom-ipfs-repo.git
368368
deps: ipfs-core@$PWD/packages/ipfs-core/dist
369-
- name: ipfs custom ipld formats
370-
repo: https://github.com/ipfs-examples/js-ipfs-custom-ipld-formats.git
371-
deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-daemon@$PWD/packages/ipfs-daemon/dist,ipfs-http-client@$PWD/packages/ipfs-http-client/dist
369+
#- name: ipfs custom ipld formats
370+
# repo: https://github.com/ipfs-examples/js-ipfs-custom-ipld-formats.git
371+
# deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-daemon@$PWD/packages/ipfs-daemon/dist,ipfs-http-client@$PWD/packages/ipfs-http-client/dist
372372
- name: ipfs custom libp2p
373373
repo: https://github.com/ipfs-examples/js-ipfs-custom-libp2p.git
374374
deps: ipfs-core@$PWD/packages/ipfs-core/dist
@@ -396,9 +396,9 @@ jobs:
396396
- name: ipfs running multiple nodes
397397
repo: https://github.com/ipfs-examples/js-ipfs-running-multiple-nodes.git
398398
deps: ipfs@$PWD/packages/ipfs/dist
399-
- name: ipfs traverse ipld graphs
400-
repo: https://github.com/ipfs-examples/js-ipfs-traverse-ipld-graphs.git
401-
deps: ipfs-core@$PWD/packages/ipfs-core/dist
399+
#- name: ipfs traverse ipld graphs
400+
# repo: https://github.com/ipfs-examples/js-ipfs-traverse-ipld-graphs.git
401+
# deps: ipfs-core@$PWD/packages/ipfs-core/dist
402402
- name: types with typescript
403403
repo: https://github.com/ipfs-examples/js-ipfs-types-use-ipfs-from-ts.git
404404
deps: ipfs-core@$PWD/packages/ipfs-core/dist

docs/core-api/DAG.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ An optional object which may have the following keys:
8888

8989
| Name | Type | Default | Description |
9090
| ---- | ---- | ------- | ----------- |
91-
| format | `String` | `'dag-cbor'` | The IPLD format multicodec |
91+
| storeCodec | `String` | `'dag-cbor'` | The codec that the stored object will be encoded with |
92+
| inputCodec | `String` | `undefined` | If an already encoded object is provided (as a `Uint8Array`), the codec that the object is encoded with, otherwise it is assumed the `dagNode` argument is an object to be encoded |
9293
| hashAlg | `String` | `'sha2-256'` | The hash algorithm to be used over the serialized DAG node |
9394
| cid | [CID][] | `'dag-cbor'` | The IPLD format multicodec |
9495
| pin | `boolean` | `false` | Pin this node when adding to the blockstore |
9596
| timeout | `Number` | `undefined` | A timeout in ms |
96-
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
97+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
9798

9899
**Note**: You should pass `cid` or the `format` & `hashAlg` pair but _not both_.
99100

@@ -107,7 +108,7 @@ An optional object which may have the following keys:
107108

108109
```JavaScript
109110
const obj = { simple: 'object' }
110-
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-512' })
111+
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-512' })
111112

112113
console.log(cid.toString())
113114
// zBwWX9ecx5F4X54WAjmFLErnBT6ByfNxStr5ovowTL7AhaUR98RWvXPS1V3HqV1qs3r5Ec5ocv7eCdbqYQREXNUfYNuKG
@@ -160,7 +161,7 @@ const obj = {
160161
}
161162
}
162163

163-
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
164+
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
164165
console.log(cid.toString())
165166
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5
166167

@@ -272,7 +273,7 @@ const obj = {
272273
}
273274
}
274275

275-
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
276+
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
276277
console.log(cid.toString())
277278
// bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq
278279

packages/interface-ipfs-core/src/block/rm.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function testRm (factory, options) {
3939

4040
it('should remove by CID object', async () => {
4141
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
42-
format: 'raw',
42+
storeCodec: 'raw',
4343
hashAlg: 'sha2-256'
4444
})
4545

@@ -61,15 +61,15 @@ export function testRm (factory, options) {
6161
it('should remove multiple CIDs', async () => {
6262
const cids = await Promise.all([
6363
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
64-
format: 'raw',
64+
storeCodec: 'raw',
6565
hashAlg: 'sha2-256'
6666
}),
6767
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
68-
format: 'raw',
68+
storeCodec: 'raw',
6969
hashAlg: 'sha2-256'
7070
}),
7171
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
72-
format: 'raw',
72+
storeCodec: 'raw',
7373
hashAlg: 'sha2-256'
7474
})
7575
])
@@ -86,7 +86,7 @@ export function testRm (factory, options) {
8686

8787
it('should error when removing non-existent blocks', async () => {
8888
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
89-
format: 'raw',
89+
storeCodec: 'raw',
9090
hashAlg: 'sha2-256'
9191
})
9292

@@ -102,7 +102,7 @@ export function testRm (factory, options) {
102102

103103
it('should not error when force removing non-existent blocks', async () => {
104104
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
105-
format: 'raw',
105+
storeCodec: 'raw',
106106
hashAlg: 'sha2-256'
107107
})
108108

@@ -119,7 +119,7 @@ export function testRm (factory, options) {
119119

120120
it('should return empty output when removing blocks quietly', async () => {
121121
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
122-
format: 'raw',
122+
storeCodec: 'raw',
123123
hashAlg: 'sha2-256'
124124
})
125125
const result = await all(ipfs.block.rm(cid, { quiet: true }))
@@ -129,7 +129,7 @@ export function testRm (factory, options) {
129129

130130
it('should error when removing pinned blocks', async () => {
131131
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
132-
format: 'raw',
132+
storeCodec: 'raw',
133133
hashAlg: 'sha2-256'
134134
})
135135
await ipfs.pin.add(cid)

packages/interface-ipfs-core/src/dag/get.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ export function testGet (factory, options) {
7373
Data: uint8ArrayFromString('I am inside a Protobuf'),
7474
Links: []
7575
}
76-
cidPb = CID.createV0(await sha256.digest(dagPB.encode(nodePb)))
76+
cidPb = CID.createV1(dagPB.code, await sha256.digest(dagPB.encode(nodePb)))
7777
nodeCbor = {
7878
someData: 'I am inside a Cbor object',
7979
pb: cidPb
8080
}
8181

8282
cidCbor = CID.createV1(dagCBOR.code, await sha256.digest(dagCBOR.encode(nodeCbor)))
8383

84-
await ipfs.dag.put(nodePb, { format: 'dag-pb', hashAlg: 'sha2-256' })
85-
await ipfs.dag.put(nodeCbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
84+
await ipfs.dag.put(nodePb, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
85+
await ipfs.dag.put(nodeCbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
8686
})
8787

8888
it('should respect timeout option when getting a DAG node', () => {
@@ -93,7 +93,7 @@ export function testGet (factory, options) {
9393

9494
it('should get a dag-pb node', async () => {
9595
const cid = await ipfs.dag.put(pbNode, {
96-
format: 'dag-pb',
96+
storeCodec: 'dag-pb',
9797
hashAlg: 'sha2-256'
9898
})
9999

@@ -105,7 +105,7 @@ export function testGet (factory, options) {
105105

106106
it('should get a dag-cbor node', async () => {
107107
const cid = await ipfs.dag.put(cborNode, {
108-
format: 'dag-cbor',
108+
storeCodec: 'dag-cbor',
109109
hashAlg: 'sha2-256'
110110
})
111111

@@ -122,7 +122,7 @@ export function testGet (factory, options) {
122122

123123
const node = result.value
124124

125-
const cid = CID.createV0(await sha256.digest(dagPB.encode(node)))
125+
const cid = CID.createV1(dagPB.code, await sha256.digest(dagPB.encode(node)))
126126
expect(cid.equals(cidPb)).to.be.true()
127127
})
128128

@@ -192,7 +192,7 @@ export function testGet (factory, options) {
192192
}
193193

194194
const cid = await ipfs.dag.put(node, {
195-
format: 'dag-pb',
195+
storeCodec: 'dag-pb',
196196
hashAlg: 'sha2-256',
197197
version: 0
198198
})
@@ -226,7 +226,7 @@ export function testGet (factory, options) {
226226
foo: 'dag-cbor-bar'
227227
}
228228

229-
const cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
229+
const cid = await ipfs.dag.put(cbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
230230
expect(cid.code).to.equal(dagCBOR.code)
231231
expect(cid.toString(base32)).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
232232

@@ -241,10 +241,10 @@ export function testGet (factory, options) {
241241
foo: 'dag-cbor-bar'
242242
}
243243

244-
const cid1 = await ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
244+
const cid1 = await ipfs.dag.put(cbor1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
245245
const cbor2 = { other: cid1 }
246246

247-
const cid2 = await ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' })
247+
const cid2 = await ipfs.dag.put(cbor2, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
248248

249249
const result = await ipfs.dag.get(cid2, {
250250
path: 'other/foo'
@@ -256,7 +256,7 @@ export function testGet (factory, options) {
256256
const buf = Uint8Array.from([0, 1, 2, 3])
257257

258258
const cid = await ipfs.dag.put(buf, {
259-
format: 'raw',
259+
storeCodec: 'raw',
260260
hashAlg: 'sha2-256'
261261
})
262262

packages/interface-ipfs-core/src/dag/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import { testGet } from './get.js'
55
import { testPut } from './put.js'
66
import { testImport } from './import.js'
77
import { testResolve } from './resolve.js'
8+
import { testDagSharnessT0053 } from './sharness-t0053-dag.js'
89

910
const tests = {
1011
export: testExport,
1112
get: testGet,
1213
put: testPut,
1314
import: testImport,
14-
resolve: testResolve
15+
resolve: testResolve,
16+
dagSharnessT0053: testDagSharnessT0053
1517
}
1618

1719
export default createSuite(tests)

packages/interface-ipfs-core/src/dag/put.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,35 @@ export function testPut (factory, options) {
3737

3838
it('should put dag-pb with default hash func (sha2-256)', () => {
3939
return ipfs.dag.put(pbNode, {
40-
format: 'dag-pb',
40+
storeCodec: 'dag-pb',
4141
hashAlg: 'sha2-256'
4242
})
4343
})
4444

4545
it('should put dag-pb with non-default hash func (sha2-512)', () => {
4646
return ipfs.dag.put(pbNode, {
47-
format: 'dag-pb',
47+
storeCodec: 'dag-pb',
4848
hashAlg: 'sha2-512'
4949
})
5050
})
5151

5252
it('should put dag-cbor with default hash func (sha2-256)', () => {
5353
return ipfs.dag.put(cborNode, {
54-
format: 'dag-cbor',
54+
storeCodec: 'dag-cbor',
5555
hashAlg: 'sha2-256'
5656
})
5757
})
5858

5959
it('should put dag-cbor with non-default hash func (sha2-512)', () => {
6060
return ipfs.dag.put(cborNode, {
61-
format: 'dag-cbor',
61+
storeCodec: 'dag-cbor',
6262
hashAlg: 'sha2-512'
6363
})
6464
})
6565

6666
it('should return the cid', async () => {
6767
const cid = await ipfs.dag.put(cborNode, {
68-
format: 'dag-cbor',
68+
storeCodec: 'dag-cbor',
6969
hashAlg: 'sha2-256'
7070
})
7171
expect(cid).to.exist()
@@ -90,7 +90,7 @@ export function testPut (factory, options) {
9090

9191
it('should override hash algorithm default and resolve with it', async () => {
9292
const cid = await ipfs.dag.put(cborNode, {
93-
format: 'dag-cbor',
93+
storeCodec: 'dag-cbor',
9494
hashAlg: 'sha2-512'
9595
})
9696
expect(cid.code).to.equal(dagCBOR.code)

packages/interface-ipfs-core/src/dag/resolve.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function testResolve (factory, options) {
2626
after(() => factory.clean())
2727

2828
it('should respect timeout option when resolving a path within a DAG node', async () => {
29-
const cid = await ipfs.dag.put({}, { format: 'dag-cbor', hashAlg: 'sha2-256' })
29+
const cid = await ipfs.dag.put({}, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
3030

3131
return testTimeout(() => ipfs.dag.resolve(cid, {
3232
timeout: 1
@@ -43,7 +43,7 @@ export function testResolve (factory, options) {
4343
}
4444
}
4545

46-
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
46+
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
4747

4848
const result = await ipfs.dag.resolve(`${cid}/c/cb`)
4949
expect(result).to.have.deep.property('cid', cid)
@@ -60,7 +60,7 @@ export function testResolve (factory, options) {
6060
}
6161
}
6262

63-
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
63+
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
6464

6565
const result = await ipfs.dag.resolve(cid, { path: '/c/cb' })
6666
expect(result).to.have.deep.property('cid', cid)
@@ -72,15 +72,15 @@ export function testResolve (factory, options) {
7272
ca: [5, 6, 7],
7373
cb: 'foo'
7474
}
75-
const cid0 = await ipfs.dag.put(obj0, { format: 'dag-cbor', hashAlg: 'sha2-256' })
75+
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
7676

7777
const obj1 = {
7878
a: 1,
7979
b: [1, 2, 3],
8080
c: cid0
8181
}
8282

83-
const cid1 = await ipfs.dag.put(obj1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
83+
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
8484

8585
const result = await ipfs.dag.resolve(`/ipfs/${cid1}/c/cb`)
8686
expect(result).to.have.deep.property('cid', cid0)
@@ -92,15 +92,15 @@ export function testResolve (factory, options) {
9292
ca: [5, 6, 7],
9393
cb: 'foo'
9494
}
95-
const cid0 = await ipfs.dag.put(obj0, { format: 'dag-cbor', hashAlg: 'sha2-256' })
95+
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
9696

9797
const obj1 = {
9898
a: 1,
9999
b: [1, 2, 3],
100100
c: cid0
101101
}
102102

103-
const cid1 = await ipfs.dag.put(obj1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
103+
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
104104

105105
const result = await ipfs.dag.resolve(cid1, { path: '/c/cb' })
106106
expect(result).to.have.deep.property('cid', cid0)
@@ -109,7 +109,7 @@ export function testResolve (factory, options) {
109109

110110
it('should resolve a raw node', async () => {
111111
const node = uint8ArrayFromString('hello world')
112-
const cid = await ipfs.dag.put(node, { format: 'raw', hashAlg: 'sha2-256' })
112+
const cid = await ipfs.dag.put(node, { storeCodec: 'raw', hashAlg: 'sha2-256' })
113113

114114
const result = await ipfs.dag.resolve(cid, { path: '/' })
115115
expect(result).to.have.deep.property('cid', cid)
@@ -122,7 +122,7 @@ export function testResolve (factory, options) {
122122
Data: someData,
123123
Links: []
124124
}
125-
const childCid = await ipfs.dag.put(childNode, { format: 'dag-pb', hashAlg: 'sha2-256' })
125+
const childCid = await ipfs.dag.put(childNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
126126

127127
const linkToChildNode = {
128128
Name: 'foo',
@@ -133,7 +133,7 @@ export function testResolve (factory, options) {
133133
Data: uint8ArrayFromString('derp'),
134134
Links: [linkToChildNode]
135135
}
136-
const parentCid = await ipfs.dag.put(parentNode, { format: 'dag-pb', hashAlg: 'sha2-256' })
136+
const parentCid = await ipfs.dag.put(parentNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
137137

138138
const result = await ipfs.dag.resolve(parentCid, { path: '/foo' })
139139
expect(result).to.have.deep.property('cid', childCid)

0 commit comments

Comments
 (0)