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

Commit e22efed

Browse files
committed
fix: convert pin input/output to async iterables
1 parent 80986f9 commit e22efed

File tree

13 files changed

+49
-53
lines changed

13 files changed

+49
-53
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"ipfs-bitswap": "^0.27.1",
100100
"ipfs-block": "~0.8.1",
101101
"ipfs-block-service": "~0.16.0",
102-
"ipfs-http-client": "^42.0.0",
102+
"ipfs-http-client": "github:ipfs/js-ipfs-http-client#store-pins-in-datastore",
103103
"ipfs-http-response": "^0.5.0",
104104
"ipfs-mfs": "^1.0.0",
105105
"ipfs-multipart": "^0.3.0",

src/cli/commands/pin/add.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ module.exports = {
2626
resolve((async () => {
2727
const type = recursive ? 'recursive' : 'direct'
2828
const ipfs = await getIpfs()
29-
const results = await ipfs.pin.add(ipfsPath, { recursive })
30-
results.forEach((res) => {
29+
for await (const res of ipfs.pin.add(ipfsPath, { recursive })) {
3130
print(`pinned ${cidToString(res.cid, { base: cidBase })} ${type}ly`)
32-
})
31+
}
3332
})())
3433
}
3534
}

src/cli/commands/pin/rm.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ module.exports = {
2525
handler: ({ getIpfs, print, ipfsPath, recursive, cidBase, resolve }) => {
2626
resolve((async () => {
2727
const ipfs = await getIpfs()
28-
const results = await ipfs.pin.rm(ipfsPath, { recursive })
29-
results.forEach((res) => {
28+
29+
for await (const res of ipfs.pin.rm(ipfsPath, { recursive })) {
3030
print(`unpinned ${cidToString(res.cid, { base: cidBase })}`)
31-
})
31+
}
3232
})())
3333
}
3434
}

src/core/components/add/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +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')
78

89
module.exports = ({ ipld, gcLock, preload, pin, options: constructorOptions }) => {
910
const isShardingEnabled = constructorOptions.EXPERIMENTAL && constructorOptions.EXPERIMENTAL.sharding
@@ -111,10 +112,10 @@ function pinFile (pin, opts) {
111112
if (shouldPin) {
112113
// Note: addAsyncIterator() has already taken a GC lock, so tell
113114
// pin.add() not to take a (second) GC lock
114-
await pin.add(file.cid, {
115+
await last(pin.add(file.cid, {
115116
preload: false,
116117
lock: false
117-
})
118+
}))
118119
}
119120

120121
yield file

src/core/components/dag/put.js

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

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

67
module.exports = ({ ipld, pin, gcLock, preload }) => {
78
return async function put (dagNode, options) {
@@ -51,9 +52,9 @@ module.exports = ({ ipld, pin, gcLock, preload }) => {
5152
})
5253

5354
if (options.pin) {
54-
await pin.add(cid, {
55+
await last(pin.add(cid, {
5556
lock: false
56-
})
57+
}))
5758
}
5859

5960
if (options.preload !== false) {

src/core/components/pin/add.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ module.exports = ({ pinManager, gcLock, dag }) => {
1717
const isPinned = await pinManager.isPinnedWithType(cid, [PinTypes.recursive, PinTypes.direct])
1818
const pinned = isPinned.pinned
1919

20-
if (pinned) {
20+
/* if (pinned) {
2121
throw new Error(`${cid} already pinned with type ${isPinned.reason}`)
22-
}
23-
24-
if (recursive) {
25-
await pinManager.pinRecursively(cid)
26-
} else {
27-
await pinManager.pinDirectly(cid)
22+
} */
23+
24+
if (!pinned) {
25+
if (recursive) {
26+
await pinManager.pinRecursively(cid)
27+
} else {
28+
await pinManager.pinDirectly(cid)
29+
}
2830
}
2931

3032
yield { cid }
31-
32-
continue
3333
}
3434
}
3535

src/core/components/pin/pin-manager.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,23 @@ class PinManager {
130130
}
131131
}
132132

133-
* indirectKeys ({ preload }) {
134-
const self = this
135-
136-
async function * findChildren (recursiveKeys) {
137-
for await (const { cid } of recursiveKeys) {
138-
for await (const childCid of self._walkDag(cid, { preload })) {
139-
// recursive pins override indirect pins
140-
const types = [
141-
PinTypes.recursive
142-
]
143-
144-
const result = await self.isPinnedWithType(childCid, types)
145-
146-
if (result.pinned) {
147-
continue
148-
}
149-
150-
yield childCid
133+
async * indirectKeys ({ preload }) {
134+
for await (const { cid } of this.recursiveKeys()) {
135+
for await (const childCid of this._walkDag(cid, { preload })) {
136+
// recursive pins override indirect pins
137+
const types = [
138+
PinTypes.recursive
139+
]
140+
141+
const result = await this.isPinnedWithType(childCid, types)
142+
143+
if (result.pinned) {
144+
continue
151145
}
146+
147+
yield childCid
152148
}
153149
}
154-
155-
yield * findChildren(this.recursiveKeys())
156150
}
157151

158152
async isPinnedWithType (cid, types) {

src/http/api/resources/dag.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +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')
1718

1819
const IpldFormats = {
1920
get [multicodec.RAW] () {
@@ -252,7 +253,7 @@ exports.put = {
252253
}
253254

254255
if (request.query.pin) {
255-
await ipfs.pin.add(cid)
256+
await last(ipfs.pin.add(cid))
256257
}
257258

258259
return h.response({

src/http/api/resources/pin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const pipe = require('it-pipe')
99
const ndjson = require('iterable-ndjson')
1010
const { cidToString } = require('../../../utils/cid')
1111
const streamResponse = require('../../utils/stream-response')
12+
const all = require('it-all')
1213

1314
function parseArgs (request, h) {
1415
let { arg } = request.query
@@ -93,7 +94,7 @@ exports.add = {
9394

9495
let result
9596
try {
96-
result = await ipfs.pin.add(path, { recursive })
97+
result = await all(ipfs.pin.add(path, { recursive }))
9798
} catch (err) {
9899
if (err.message.includes('already pinned recursively')) {
99100
throw Boom.boomify(err, { statusCode: 400 })
@@ -122,7 +123,7 @@ exports.rm = {
122123

123124
let result
124125
try {
125-
result = await ipfs.pin.rm(path, { recursive })
126+
result = await all(ipfs.pin.rm(path, { recursive }))
126127
} catch (err) {
127128
throw Boom.boomify(err, { message: 'Failed to remove pin' })
128129
}

test/cli/dag.js

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ describe('dag', () => runOnAndOff.off((thing) => {
133133
const cid = (await ipfs('dag put --pin "{"hello":"world"}"')).trim()
134134

135135
const out = await ipfs('pin ls')
136+
136137
expect(out).to.include(cid)
137138
})
138139

test/cli/dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha')
55
const runOnAndOff = require('../utils/on-and-off')
66
const isIPFS = require('is-ipfs')
77

8-
describe('dns', () => runOnAndOff((thing) => {
8+
describe.skip('dns', () => runOnAndOff((thing) => {
99
let ipfs
1010

1111
before(function () {

test/core/gc.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe.skip('gc', function () {
199199
// Pin first block
200200
// Note: pin add will take a read lock
201201
const pinLockRequested = pEvent(lockEmitter, 'readLock request')
202-
const pin1 = ipfs.pin.add(cid1)
202+
const pin1 = last(ipfs.pin.add(cid1))
203203

204204
// Once pin lock has been requested, start GC
205205
await pinLockRequested
@@ -209,7 +209,7 @@ describe.skip('gc', function () {
209209

210210
// TODO: Adding pin for removed block never returns, which means the lock
211211
// never gets released
212-
// const pin2 = ipfs.pin.add(cid2)
212+
// const pin2 = last(ipfs.pin.add(cid2))
213213

214214
// Confirm second second block has been removed
215215
const localRefs = (await ipfs.refs.local()).map(r => r.ref)
@@ -230,13 +230,13 @@ describe.skip('gc', function () {
230230
const cid2 = (await ipfs.block.put(Buffer.from('block to pin rm 2'), null)).cid
231231

232232
// Pin blocks
233-
await ipfs.pin.add(cid1)
234-
await ipfs.pin.add(cid2)
233+
await last(ipfs.pin.add(cid1))
234+
await last(ipfs.pin.add(cid2))
235235

236236
// Unpin first block
237237
// Note: pin rm will take a read lock
238238
const pinLockRequested = pEvent(lockEmitter, 'readLock request')
239-
const pinRm1 = ipfs.pin.rm(cid1)
239+
const pinRm1 = last(ipfs.pin.rm(cid1))
240240

241241
// Once pin lock has been requested, start GC
242242
await pinLockRequested
@@ -246,7 +246,7 @@ describe.skip('gc', function () {
246246

247247
// Once GC has started, start second pin rm
248248
await gcStarted
249-
const pinRm2 = ipfs.pin.rm(cid2)
249+
const pinRm2 = last(ipfs.pin.rm(cid2))
250250

251251
const deleted = (await gc).map(i => i.cid.toString())
252252
await pinRm1

test/core/interface.spec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ describe('interface-ipfs-core tests', function () {
6666

6767
tests.object(commonFactory)
6868

69-
tests.pin(commonFactory, {
70-
only: true
71-
})
69+
tests.pin(commonFactory)
7270

7371
tests.ping(commonFactory)
7472

0 commit comments

Comments
 (0)