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 pathlinks.js
116 lines (94 loc) · 3.24 KB
/
links.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
/* eslint-env mocha */
'use strict'
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const dagPB = require('@ipld/dag-pb')
const { nanoid } = require('nanoid')
const { CID } = require('multiformats/cid')
const { sha256 } = require('multiformats/hashes/sha2')
const { getDescribe, getIt, expect } = require('../utils/mocha')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.object.links', function () {
this.timeout(80 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => {
ipfs = (await factory.spawn()).api
})
after(() => factory.clean())
it('should get empty links by multihash', async () => {
const testObj = {
Data: uint8ArrayFromString(nanoid()),
Links: []
}
const cid = await ipfs.object.put(testObj)
const node = await ipfs.object.get(cid)
const links = await ipfs.object.links(cid)
expect(node.Links).to.eql(links)
})
it('should get links by multihash', async () => {
const node1a = {
Data: uint8ArrayFromString('Some data 1'),
Links: []
}
const node2 = {
Data: uint8ArrayFromString('Some data 2'),
Links: []
}
const node2Buf = dagPB.encode(node2)
const link = {
Name: 'some-link',
Tsize: node2Buf.length,
Hash: CID.createV0(await sha256.digest(node2Buf))
}
const node1b = {
Data: node1a.Data,
Links: [link]
}
const node1bCid = await ipfs.object.put(node1b)
const links = await ipfs.object.links(node1bCid)
expect(links).to.have.lengthOf(1)
expect(node1b.Links).to.deep.equal(links)
})
it('should get links from CBOR object', async () => {
const hashes = []
const res1 = await ipfs.add(uint8ArrayFromString('test data'))
hashes.push(res1.cid)
const res2 = await ipfs.add(uint8ArrayFromString('more test data'))
hashes.push(res2.cid)
const obj = {
some: 'data',
mylink: hashes[0],
myobj: {
anotherLink: hashes[1]
}
}
const cid = await ipfs.dag.put(obj)
const links = await ipfs.object.links(cid)
expect(links.length).to.eql(2)
// TODO: js-ipfs succeeds but go returns empty strings for link name
// const names = [links[0].name, links[1].name]
// expect(names).includes('mylink')
// expect(names).includes('myobj/anotherLink')
const cids = [links[0].Hash.toString(), links[1].Hash.toString()]
expect(cids).includes(hashes[0].toString())
expect(cids).includes(hashes[1].toString())
})
it('returns error for request without argument', () => {
// @ts-expect-error invalid arg
return expect(ipfs.object.links(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
it('returns error for request with invalid argument', () => {
// @ts-expect-error invalid arg
return expect(ipfs.object.links('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}