This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathget.js
155 lines (122 loc) · 4.74 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
149
150
151
152
153
154
155
/* eslint-env mocha */
'use strict'
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const UnixFs = require('ipfs-unixfs')
const crypto = require('crypto')
const { asDAGLink } = require('./utils')
const all = require('it-all')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.object.get', function () {
this.timeout(80 * 1000)
let ipfs
before(async () => {
ipfs = (await common.spawn()).api
})
after(() => common.clean())
it('should get object by multihash', async () => {
const obj = {
Data: Buffer.from(hat()),
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 Buffer or String
if (typeof node2.Data === 'string') {
node2 = new DAGNode(Buffer.from(node2.Data), node2.Links, node2.size)
}
expect(node1.Data).to.eql(node2.Data)
expect(node1.Links).to.eql(node2.Links)
})
it('should get object by multihash string', async () => {
const obj = {
Data: Buffer.from(hat()),
Links: []
}
const node1Cid = await ipfs.object.put(obj)
const node1 = await ipfs.object.get(node1Cid)
let node2 = await ipfs.object.get(node1Cid.toBaseEncodedString())
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node2.Data === 'string') {
node2 = new DAGNode(Buffer.from(node2.Data), node2.Links, node2.size)
}
expect(node1.Data).to.deep.equal(node2.Data)
expect(node1.Links).to.deep.equal(node2.Links)
})
it('should get object with links by multihash string', async () => {
const node1a = new DAGNode(Buffer.from('Some data 1'))
const node2 = new DAGNode(Buffer.from('Some data 2'))
const link = await asDAGLink(node2, 'some-link')
const node1b = new DAGNode(node1a.Data, node1a.Links.concat(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 Buffer or String
if (typeof node1c.Data === 'string') {
node1c = new DAGNode(Buffer.from(node1c.Data), node1c.Links, node1c.size)
}
expect(node1a.Data).to.eql(node1c.Data)
})
it('should get object by base58 encoded multihash', async () => {
const obj = {
Data: Buffer.from(hat()),
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 Buffer or String
if (typeof node1b.Data === 'string') {
node1b = new DAGNode(Buffer.from(node1b.Data), node1b.Links, node1b.size)
}
expect(node1a.Data).to.eql(node1b.Data)
expect(node1a.Links).to.eql(node1b.Links)
})
it('should get object by base58 encoded multihash string', async () => {
const obj = {
Data: Buffer.from(hat()),
Links: []
}
const node1aCid = await ipfs.object.put(obj)
const node1a = await ipfs.object.get(node1aCid)
let node1b = await ipfs.object.get(node1aCid.toBaseEncodedString(), { enc: 'base58' })
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node1b.Data === 'string') {
node1b = new DAGNode(Buffer.from(node1b.Data), node1b.Links, node1b.size)
}
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 = crypto.randomBytes(1024 * 3000)
const result = await all(ipfs.add({
path: '',
content: data
}))
const node = await ipfs.object.get(result[0].cid)
const meta = UnixFs.unmarshal(node.Data)
expect(meta.fileSize()).to.equal(data.length)
})
it('should error for request without argument', () => {
return expect(ipfs.object.get(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
it('returns error for request with invalid argument', () => {
return expect(ipfs.object.get('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}