Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 24e6ad3

Browse files
committed
fix: fix tests and use drain where we drain
1 parent 2c276ec commit 24e6ad3

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
"is-ipfs": "~0.6.1",
122122
"it-all": "^1.0.1",
123123
"it-concat": "^1.0.0",
124+
"it-drain": "^1.0.0",
124125
"it-first": "^1.0.1",
125126
"it-glob": "0.0.7",
126127
"it-last": "^1.0.1",

src/core/components/add/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const importer = require('ipfs-unixfs-importer')
44
const normaliseAddInput = require('ipfs-utils/src/files/normalise-input')
55
const { parseChunkerString } = require('./utils')
66
const pipe = require('it-pipe')
7-
const last = require('it-last')
7+
const drain = require('it-drain')
88

99
module.exports = ({ ipld, gcLock, preload, pin, options: constructorOptions }) => {
1010
const isShardingEnabled = constructorOptions.EXPERIMENTAL && constructorOptions.EXPERIMENTAL.sharding
@@ -112,7 +112,7 @@ function pinFile (pin, opts) {
112112
if (shouldPin) {
113113
// Note: addAsyncIterator() has already taken a GC lock, so tell
114114
// pin.add() not to take a (second) GC lock
115-
await last(pin.add(file.cid, {
115+
await drain(pin.add(file.cid, {
116116
preload: false,
117117
lock: false
118118
}))

src/core/components/dag/put.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const multicodec = require('multicodec')
44
const nameToCodec = name => multicodec[name.toUpperCase().replace(/-/g, '_')]
5-
const last = require('it-last')
5+
const drain = require('it-drain')
66

77
module.exports = ({ ipld, pin, gcLock, preload }) => {
88
return async function put (dagNode, options) {
@@ -52,7 +52,7 @@ module.exports = ({ ipld, pin, gcLock, preload }) => {
5252
})
5353

5454
if (options.pin) {
55-
await last(pin.add(cid, {
55+
await drain(pin.add(cid, {
5656
lock: false
5757
}))
5858
}

src/core/components/pin/add.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ module.exports = ({ pinManager, gcLock, dag }) => {
1414
const pinAdd = async function * () {
1515
// verify that each hash can be pinned
1616
for (const cid of cids) {
17-
const isPinned = await pinManager.isPinnedWithType(cid, [PinTypes.recursive, PinTypes.direct])
18-
const pinned = isPinned.pinned
17+
const { reason } = await pinManager.isPinnedWithType(cid, [PinTypes.recursive, PinTypes.direct])
1918

20-
if (pinned) {
21-
throw new Error(`${cid} already pinned with type ${isPinned.reason}`)
19+
if (reason === 'recursive' && !recursive) {
20+
// only disallow trying to override recursive pins
21+
throw new Error(`${cid} already pinned recursively`)
2222
}
2323

24-
if (!pinned) {
25-
if (recursive) {
26-
await pinManager.pinRecursively(cid)
27-
} else {
28-
await pinManager.pinDirectly(cid)
29-
}
24+
if (recursive) {
25+
await pinManager.pinRecursively(cid)
26+
} else {
27+
await pinManager.pinDirectly(cid)
3028
}
3129

3230
yield { cid }

src/http/api/resources/dag.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
const all = require('it-all')
1515
const log = debug('ipfs:http-api:dag')
1616
log.error = debug('ipfs:http-api:dag:error')
17-
const last = require('it-last')
17+
const drain = require('it-drain')
1818

1919
const IpldFormats = {
2020
get [multicodec.RAW] () {
@@ -253,7 +253,7 @@ exports.put = {
253253
}
254254

255255
if (request.query.pin) {
256-
await last(ipfs.pin.add(cid))
256+
await drain(ipfs.pin.add(cid))
257257
}
258258

259259
return h.response({

test/core/gc.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const { expect } = require('interface-ipfs-core/src/utils/mocha')
66
const last = require('it-last')
7+
const drain = require('it-drain')
78
const factory = require('../utils/factory')
89
const pEvent = require('p-event')
910

@@ -230,8 +231,8 @@ describe.skip('gc', function () {
230231
const cid2 = (await ipfs.block.put(Buffer.from('block to pin rm 2'), null)).cid
231232

232233
// Pin blocks
233-
await last(ipfs.pin.add(cid1))
234-
await last(ipfs.pin.add(cid2))
234+
await drain(ipfs.pin.add(cid1))
235+
await drain(ipfs.pin.add(cid2))
235236

236237
// Unpin first block
237238
// Note: pin rm will take a read lock

test/http-api/inject/pin.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ module.exports = (http) => {
5252
})
5353

5454
it('unpins recursive pins', async () => {
55-
const res = await api.inject({
55+
let res = await api.inject({
56+
method: 'POST',
57+
url: `/api/v0/pin/add?arg=${pins.root1}`
58+
})
59+
60+
expect(res.statusCode).to.equal(200)
61+
62+
res = await api.inject({
5663
method: 'POST',
5764
url: `/api/v0/pin/rm?arg=${pins.root1}`
5865
})
@@ -71,7 +78,7 @@ module.exports = (http) => {
7178

7279
res = await api.inject({
7380
method: 'POST',
74-
url: `/api/v0/pin/rm?arg=${pins.root1}&recursive=false`
81+
url: `/api/v0/pin/rm?arg=${pins.root1}`
7582
})
7683

7784
expect(res.statusCode).to.equal(200)

0 commit comments

Comments
 (0)