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 pathrm.js
163 lines (132 loc) · 5.17 KB
/
rm.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
156
157
158
159
160
161
162
163
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { nanoid } = require('nanoid')
const all = require('it-all')
const drain = require('it-drain')
const CID = require('cids')
const testTimeout = require('../utils/test-timeout')
/** @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('.block.rm', () => {
let ipfs
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
it('should respect timeout option when removing a block', () => {
return testTimeout(() => drain(ipfs.block.rm(new CID('QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn'), {
timeout: 1
})))
})
it('should remove by CID object', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
// block should be present in the local store
const localRefs = await all(ipfs.refs.local())
expect(localRefs).to.have.property('length').that.is.greaterThan(0)
expect(localRefs.find(ref => ref.ref === new CID(1, 'raw', cid.multihash).toString())).to.be.ok()
const result = await all(ipfs.block.rm(cid))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0].cid.toString()).equal(cid.toString())
expect(result[0]).to.not.have.property('error')
// did we actually remove the block?
const localRefsAfterRemove = await all(ipfs.refs.local())
expect(localRefsAfterRemove.find(ref => ref.ref === new CID(1, 'raw', cid.multihash).toString())).to.not.be.ok()
})
it('should remove by CID in string', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
const result = await all(ipfs.block.rm(cid.toString()))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0].cid.toString()).to.equal(cid.toString())
expect(result[0]).to.not.have.property('error')
})
it('should remove by CID in buffer', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
const result = await all(ipfs.block.rm(cid.buffer))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0].cid.toString()).to.equal(cid.toString())
expect(result[0]).to.not.have.property('error')
})
it('should remove multiple CIDs', async () => {
const cids = [
await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
}),
await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
}),
await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
]
const result = await all(ipfs.block.rm(cids))
expect(result).to.be.an('array').and.to.have.lengthOf(3)
result.forEach((res, index) => {
expect(res.cid.toString()).to.equal(cids[index].toString())
expect(res).to.not.have.property('error')
})
})
it('should error when removing non-existent blocks', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
// remove it
await all(ipfs.block.rm(cid))
// remove it again
const result = await all(ipfs.block.rm(cid))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0]).to.have.property('error')
expect(result[0].error.message).to.include('block not found')
})
it('should not error when force removing non-existent blocks', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
// remove it
await all(ipfs.block.rm(cid))
// remove it again
const result = await all(ipfs.block.rm(cid, { force: true }))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0].cid.toString()).to.equal(cid.toString())
expect(result[0]).to.not.have.property('error')
})
it('should return empty output when removing blocks quietly', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
const result = await all(ipfs.block.rm(cid, { quiet: true }))
expect(result).to.be.an('array').and.to.have.lengthOf(0)
})
it('should error when removing pinned blocks', async () => {
const cid = await ipfs.dag.put(Buffer.from(nanoid()), {
format: 'raw',
hashAlg: 'sha2-256'
})
await ipfs.pin.add(cid.toString())
const result = await all(ipfs.block.rm(cid))
expect(result).to.be.an('array').and.to.have.lengthOf(1)
expect(result[0]).to.have.property('error')
expect(result[0].error.message).to.include('pinned')
})
})
}