This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathresolve.js
143 lines (117 loc) · 4.35 KB
/
resolve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* eslint-env mocha */
import * as dagPB from '@ipld/dag-pb'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import testTimeout from '../utils/test-timeout.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testResolve (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dag.resolve', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('should respect timeout option when resolving a path within a DAG node', async () => {
const cid = await ipfs.dag.put({}, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
return testTimeout(() => ipfs.dag.resolve(cid, {
timeout: 1
}))
})
it('should resolve a path inside a cbor node', async () => {
const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(`${cid}/c/cb`)
expect(result).to.have.deep.property('cid', cid)
expect(result).to.have.property('remainderPath', 'c/cb')
})
it('should resolve a path inside a cbor node by CID', async () => {
const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(cid, { path: '/c/cb' })
expect(result).to.have.deep.property('cid', cid)
expect(result).to.have.property('remainderPath', 'c/cb')
})
it('should resolve a multi-node path inside a cbor node', async () => {
const obj0 = {
ca: [5, 6, 7],
cb: 'foo'
}
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const obj1 = {
a: 1,
b: [1, 2, 3],
c: cid0
}
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(`/ipfs/${cid1}/c/cb`)
expect(result).to.have.deep.property('cid', cid0)
expect(result).to.have.property('remainderPath', 'cb')
})
it('should resolve a multi-node path inside a cbor node by CID', async () => {
const obj0 = {
ca: [5, 6, 7],
cb: 'foo'
}
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const obj1 = {
a: 1,
b: [1, 2, 3],
c: cid0
}
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(cid1, { path: '/c/cb' })
expect(result).to.have.deep.property('cid', cid0)
expect(result).to.have.property('remainderPath', 'cb')
})
it('should resolve a raw node', async () => {
const node = uint8ArrayFromString('hello world')
const cid = await ipfs.dag.put(node, { storeCodec: 'raw', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(cid, { path: '/' })
expect(result).to.have.deep.property('cid', cid)
expect(result).to.have.property('remainderPath', '')
})
it('should resolve a path inside a dag-pb node linked to from another dag-pb node', async () => {
const someData = uint8ArrayFromString('some other data')
const childNode = {
Data: someData,
Links: []
}
const childCid = await ipfs.dag.put(childNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
const linkToChildNode = {
Name: 'foo',
Tsize: dagPB.encode(childNode).length,
Hash: childCid
}
const parentNode = {
Data: uint8ArrayFromString('derp'),
Links: [linkToChildNode]
}
const parentCid = await ipfs.dag.put(parentNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
const result = await ipfs.dag.resolve(parentCid, { path: '/foo' })
expect(result).to.have.deep.property('cid', childCid)
expect(result).to.have.property('remainderPath', '')
})
})
}