This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathstat.js
142 lines (116 loc) · 4.05 KB
/
stat.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
/* eslint-env mocha */
'use strict'
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { asDAGLink } = require('./utils')
/** @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.stat', function () {
this.timeout(80 * 1000)
let ipfs
before(async () => {
ipfs = (await common.spawn()).api
})
after(() => common.clean())
it('should get stats by multihash', async () => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}
const cid = await ipfs.object.put(testObj)
const stats = await ipfs.object.stat(cid)
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
NumLinks: 0,
BlockSize: 17,
LinksSize: 2,
DataSize: 15,
CumulativeSize: 17
}
expect(expected).to.deep.equal(stats)
})
it('should respect timeout option', async () => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}
await ipfs.object.put(testObj)
const timeout = 2
const startTime = new Date()
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'
const err = await expect(ipfs.object.stat(badCid, { timeout: `${timeout}s` })).to.be.rejected()
const timeForRequest = (new Date() - startTime) / 1000
if (err.code) {
expect(err.code).to.equal('ERR_TIMEOUT')
} else {
expect(err.message).to.equal('failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded')
}
expect(timeForRequest).to.not.lessThan(timeout - 0.1)
expect(timeForRequest).to.not.greaterThan(timeout + 1)
})
it('should get stats for object with links by multihash', 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)
const stats = await ipfs.object.stat(node1bCid)
const expected = {
Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC',
NumLinks: 1,
BlockSize: 64,
LinksSize: 53,
DataSize: 11,
CumulativeSize: 77
}
expect(expected).to.eql(stats)
})
it('should get stats by base58 encoded multihash', async () => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}
const cid = await ipfs.object.put(testObj)
const stats = await ipfs.object.stat(cid.buffer)
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
NumLinks: 0,
BlockSize: 17,
LinksSize: 2,
DataSize: 15,
CumulativeSize: 17
}
expect(expected).to.deep.equal(stats)
})
it('should get stats by base58 encoded multihash string', async () => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}
const cid = await ipfs.object.put(testObj)
const stats = await ipfs.object.stat(cid.toBaseEncodedString())
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
NumLinks: 0,
BlockSize: 17,
LinksSize: 2,
DataSize: 15,
CumulativeSize: 17
}
expect(expected).to.deep.equal(stats)
})
it('returns error for request without argument', () => {
return expect(ipfs.object.stat(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
it('returns error for request with invalid argument', () => {
return expect(ipfs.object.stat('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}