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 pathget.js
148 lines (121 loc) · 4.06 KB
/
get.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
144
145
146
147
148
/* eslint-env mocha */
import * as dagPB from '@ipld/dag-pb'
import { nanoid } from 'nanoid'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { UnixFS } from 'ipfs-unixfs'
import { randomBytes } from 'iso-random-stream'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testGet (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.object.get', 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 object by multihash', async () => {
const obj = {
Data: uint8ArrayFromString(nanoid()),
Links: []
}
const node1Cid = await ipfs.object.put(obj)
const node1 = await ipfs.object.get(node1Cid)
let node2 = await ipfs.object.get(node1Cid)
// because js-ipfs-api can't infer if the
// returned Data is Uint8Array or String
if (typeof node2.Data === 'string') {
node2 = {
Data: uint8ArrayFromString(node2.Data),
Links: node2.Links
}
}
expect(node1.Data).to.eql(node2.Data)
expect(node1.Links).to.eql(node2.Links)
})
it('should get object with links by multihash string', 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)
let node1c = await ipfs.object.get(node1bCid)
// because js-ipfs-api can't infer if the
// returned Data is Uint8Array or String
if (typeof node1c.Data === 'string') {
node1c = {
Data: uint8ArrayFromString(node1c.Data),
Links: node1c.Links
}
}
expect(node1a.Data).to.eql(node1c.Data)
})
it('should get object by base58 encoded multihash', async () => {
const obj = {
Data: uint8ArrayFromString(nanoid()),
Links: []
}
const node1aCid = await ipfs.object.put(obj)
const node1a = await ipfs.object.get(node1aCid)
let node1b = await ipfs.object.get(node1aCid, { enc: 'base58' })
// because js-ipfs-api can't infer if the
// returned Data is Uint8Array or String
if (typeof node1b.Data === 'string') {
node1b = {
Data: uint8ArrayFromString(node1b.Data),
Links: node1b.Links
}
}
expect(node1a.Data).to.eql(node1b.Data)
expect(node1a.Links).to.eql(node1b.Links)
})
it('should supply unaltered data', async () => {
// has to be big enough to span several DAGNodes
const data = randomBytes(1024 * 3000)
const result = await ipfs.add({
path: '',
content: data
})
const node = await ipfs.object.get(result.cid)
if (!node.Data) {
throw new Error('Node did not have data')
}
const meta = UnixFS.unmarshal(node.Data)
expect(meta.fileSize()).to.equal(data.length)
})
it('should error for request without argument', () => {
// @ts-expect-error invalid arg
return expect(ipfs.object.get(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.get('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}