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

Commit cc821fe

Browse files
committed
refactor: do not expose pinTypes
They're simple enough, documented elsewhere, and not used by any exposed functionality.
1 parent 8b3fc26 commit cc821fe

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

src/core/components/pin.js

+26-27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ module.exports = function pin (self) {
2323
const repo = self._repo
2424
const dag = self.dag
2525
const pinset = createPinSet(dag)
26+
const types = {
27+
direct: 'direct',
28+
recursive: 'recursive',
29+
indirect: 'indirect',
30+
all: 'all'
31+
}
2632

2733
let directPins = new Set()
2834
let recursivePins = new Set()
@@ -61,14 +67,14 @@ module.exports = function pin (self) {
6167
// create a DAGLink to the node with direct pins
6268
cb => async.waterfall([
6369
cb => pinset.storeSet(directKeys(), cb),
64-
(node, cb) => DAGLink.create(pin.types.direct, node.size, node.multihash, cb),
70+
(node, cb) => DAGLink.create(types.direct, node.size, node.multihash, cb),
6571
(link, cb) => { dLink = link; cb(null) }
6672
], cb),
6773

6874
// create a DAGLink to the node with recursive pins
6975
cb => async.waterfall([
7076
cb => pinset.storeSet(recursiveKeys(), cb),
71-
(node, cb) => DAGLink.create(pin.types.recursive, node.size, node.multihash, cb),
77+
(node, cb) => DAGLink.create(types.recursive, node.size, node.multihash, cb),
7278
(link, cb) => { rLink = link; cb(null) }
7379
], cb),
7480

@@ -98,13 +104,6 @@ module.exports = function pin (self) {
98104
}
99105

100106
const pin = {
101-
types: {
102-
direct: 'direct',
103-
recursive: 'recursive',
104-
indirect: 'indirect',
105-
all: 'all'
106-
},
107-
108107
add: promisify((paths, options, callback) => {
109108
if (typeof options === 'function') {
110109
callback = options
@@ -177,7 +176,7 @@ module.exports = function pin (self) {
177176

178177
// verify that each hash can be unpinned
179178
async.map(mhs, (multihash, cb) => {
180-
pin._isPinnedWithType(multihash, pin.types.all, (err, res) => {
179+
pin._isPinnedWithType(multihash, types.all, (err, res) => {
181180
if (err) { return cb(err) }
182181
const { pinned, reason } = res
183182
const key = toB58String(multihash)
@@ -186,13 +185,13 @@ module.exports = function pin (self) {
186185
}
187186

188187
switch (reason) {
189-
case (pin.types.recursive):
188+
case (types.recursive):
190189
if (recursive) {
191190
return cb(null, key)
192191
} else {
193192
return cb(new Error(`${key} is pinned recursively`))
194193
}
195-
case (pin.types.direct):
194+
case (types.direct):
196195
return cb(null, key)
197196
default:
198197
return cb(new Error(
@@ -223,7 +222,7 @@ module.exports = function pin (self) {
223222
}),
224223

225224
ls: promisify((paths, options, callback) => {
226-
let type = pin.types.all
225+
let type = types.all
227226
if (typeof paths === 'function') {
228227
callback = paths
229228
options = null
@@ -239,7 +238,7 @@ module.exports = function pin (self) {
239238
if (options && options.type) {
240239
type = options.type.toLowerCase()
241240
}
242-
if (!pin.types[type]) {
241+
if (!types[type]) {
243242
return callback(new Error(
244243
`Invalid type '${type}', must be one of {direct, indirect, recursive, all}`
245244
))
@@ -251,7 +250,7 @@ module.exports = function pin (self) {
251250
if (err) { return callback(err) }
252251

253252
async.mapSeries(mhs, (multihash, cb) => {
254-
pin._isPinnedWithType(multihash, pin.types.all, (err, res) => {
253+
pin._isPinnedWithType(multihash, types.all, (err, res) => {
255254
if (err) { return cb(err) }
256255
const { pinned, reason } = res
257256
const key = toB58String(multihash)
@@ -260,16 +259,16 @@ module.exports = function pin (self) {
260259
}
261260

262261
switch (reason) {
263-
case pin.types.direct:
264-
case pin.types.recursive:
262+
case types.direct:
263+
case types.recursive:
265264
return cb(null, {
266265
hash: key,
267266
type: reason
268267
})
269268
default:
270269
return cb(null, {
271270
hash: key,
272-
type: `${pin.types.indirect} through ${reason}`
271+
type: `${types.indirect} through ${reason}`
273272
})
274273
}
275274
})
@@ -278,23 +277,23 @@ module.exports = function pin (self) {
278277
} else {
279278
// show all pinned items of type
280279
let pins = []
281-
if (type === pin.types.direct || type === pin.types.all) {
280+
if (type === types.direct || type === types.all) {
282281
pins = pins.concat(
283282
Array.from(directPins).map(hash => ({
284-
type: pin.types.direct,
283+
type: types.direct,
285284
hash
286285
}))
287286
)
288287
}
289-
if (type === pin.types.recursive || type === pin.types.all) {
288+
if (type === types.recursive || type === types.all) {
290289
pins = pins.concat(
291290
Array.from(recursivePins).map(hash => ({
292-
type: pin.types.recursive,
291+
type: types.recursive,
293292
hash
294293
}))
295294
)
296295
}
297-
if (type === pin.types.indirect || type === pin.types.all) {
296+
if (type === types.indirect || type === types.all) {
298297
getIndirectKeys((err, indirects) => {
299298
if (err) { return callback(err) }
300299
pins = pins
@@ -305,7 +304,7 @@ module.exports = function pin (self) {
305304
(indirects.includes(hash) && !directPins.has(hash))
306305
)
307306
.concat(indirects.map(hash => ({
308-
type: pin.types.indirect,
307+
type: types.indirect,
309308
hash
310309
})))
311310
return callback(null, pins)
@@ -318,7 +317,7 @@ module.exports = function pin (self) {
318317

319318
_isPinnedWithType: promisify((multihash, type, callback) => {
320319
const key = toB58String(multihash)
321-
const { recursive, direct, all } = pin.types
320+
const { recursive, direct, all } = types
322321
// recursive
323322
if ((type === recursive || type === all) && recursivePins.has(key)) {
324323
return callback(null, {pinned: true, reason: recursive})
@@ -374,8 +373,8 @@ module.exports = function pin (self) {
374373
}
375374

376375
async.parallel([
377-
cb => pinset.loadSet(pinRoot.value, pin.types.recursive, cb),
378-
cb => pinset.loadSet(pinRoot.value, pin.types.direct, cb)
376+
cb => pinset.loadSet(pinRoot.value, types.recursive, cb),
377+
cb => pinset.loadSet(pinRoot.value, types.direct, cb)
379378
], (err, [rKeys, dKeys]) => {
380379
if (err) { return callback(err) }
381380

src/core/utils.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const CID = require('cids')
43
const multihashes = require('multihashes')
54
const promisify = require('promisify-es6')
65
const map = require('async/map')

src/http/api/resources/pin.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function parseArgs (request, reply) {
2525

2626
exports.ls = {
2727
parseArgs: (request, reply) => {
28-
const ipfs = request.server.app.ipfs
29-
const type = request.query.type || ipfs.pin.types.all
28+
const type = request.query.type || 'all'
3029

3130
return reply({
3231
path: request.query.arg,

test/core/pin.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const pins = {
2424
mercuryDir: 'QmbJCNKXJqVK8CzbjpNFz2YekHwh3CSHpBA86uqYg3sJ8q',
2525
mercuryWiki: 'QmVgSHAdMxFAuMP2JiMAYkB8pCWP1tcB9djqvq8GKAFiHi'
2626
}
27+
const pinTypes = {
28+
direct: 'direct',
29+
recursive: 'recursive',
30+
indirect: 'indirect',
31+
all: 'all'
32+
}
2733

2834
describe('pin', function () {
2935
const fixtures = [
@@ -44,22 +50,22 @@ describe('pin', function () {
4450
type = undefined
4551
}
4652

47-
return pin._isPinnedWithType(hash, type || pin.types.all)
53+
return pin._isPinnedWithType(hash, type || pinTypes.all)
4854
.then(result => expect(result.pinned).to.eql(pinned))
4955
}
5056

5157
function clearPins () {
5258
return pin.ls()
5359
.then(ls => {
5460
const pinsToRemove = ls
55-
.filter(out => out.type === pin.types.recursive)
61+
.filter(out => out.type === pinTypes.recursive)
5662
.map(out => pin.rm(out.hash))
5763
return Promise.all(pinsToRemove)
5864
})
5965
.then(() => pin.ls())
6066
.then(ls => {
6167
const pinsToRemove = ls
62-
.filter(out => out.type === pin.types.direct)
68+
.filter(out => out.type === pinTypes.direct)
6369
.map(out => pin.rm(out.hash))
6470
return Promise.all(pinsToRemove)
6571
})
@@ -85,13 +91,13 @@ describe('pin', function () {
8591

8692
it('when node is pinned', function () {
8793
return pin.add(pins.solarWiki)
88-
.then(() => pin._isPinnedWithType(pins.solarWiki, pin.types.all))
94+
.then(() => pin._isPinnedWithType(pins.solarWiki, pinTypes.all))
8995
.then(pinned => expect(pinned.pinned).to.eql(true))
9096
})
9197

9298
it('when node is not in datastore', function () {
9399
const falseHash = `${pins.root.slice(0, -2)}ss`
94-
return pin._isPinnedWithType(falseHash, pin.types.all)
100+
return pin._isPinnedWithType(falseHash, pinTypes.all)
95101
.then(pinned => {
96102
expect(pinned.pinned).to.eql(false)
97103
expect(pinned.reason).to.eql(undefined)
@@ -104,15 +110,15 @@ describe('pin', function () {
104110
})
105111

106112
it('when pinned recursively', function () {
107-
return pin._isPinnedWithType(pins.root, pin.types.recursive)
113+
return pin._isPinnedWithType(pins.root, pinTypes.recursive)
108114
.then(result => {
109115
expect(result.pinned).to.eql(true)
110-
expect(result.reason).to.eql(pin.types.recursive)
116+
expect(result.reason).to.eql(pinTypes.recursive)
111117
})
112118
})
113119

114120
it('when pinned indirectly', function () {
115-
return pin._isPinnedWithType(pins.mercuryWiki, pin.types.indirect)
121+
return pin._isPinnedWithType(pins.mercuryWiki, pinTypes.indirect)
116122
.then(result => {
117123
expect(result.pinned).to.eql(true)
118124
expect(result.reason).to.eql(pins.root)
@@ -122,17 +128,17 @@ describe('pin', function () {
122128
it('when pinned directly', function () {
123129
return pin.add(pins.mercuryDir, { recursive: false })
124130
.then(() => {
125-
return pin._isPinnedWithType(pins.mercuryDir, pin.types.direct)
131+
return pin._isPinnedWithType(pins.mercuryDir, pinTypes.direct)
126132
.then(result => {
127133
expect(result.pinned).to.eql(true)
128-
expect(result.reason).to.eql(pin.types.direct)
134+
expect(result.reason).to.eql(pinTypes.direct)
129135
})
130136
})
131137
})
132138

133139
it('when not pinned', function () {
134140
return clearPins()
135-
.then(() => pin._isPinnedWithType(pins.mercuryDir, pin.types.direct))
141+
.then(() => pin._isPinnedWithType(pins.mercuryDir, pinTypes.direct))
136142
.then(pin => expect(pin.pinned).to.eql(false))
137143
})
138144
})
@@ -165,8 +171,8 @@ describe('pin', function () {
165171
.then(() => pin.add(pins.root))
166172
.then(() => Promise.all([
167173
// solarWiki is pinned both directly and indirectly o.O
168-
expectPinned(pins.solarWiki, pin.types.direct),
169-
expectPinned(pins.solarWiki, pin.types.indirect)
174+
expectPinned(pins.solarWiki, pinTypes.direct),
175+
expectPinned(pins.solarWiki, pinTypes.indirect)
170176
]))
171177
})
172178

@@ -208,7 +214,7 @@ describe('pin', function () {
208214
return pin.ls()
209215
.then(ls => {
210216
const pinType = ls.find(out => out.hash === pins.mercuryDir).type
211-
expect(pinType).to.eql(pin.types.indirect)
217+
expect(pinType).to.eql(pinTypes.indirect)
212218
})
213219
})
214220

test/core/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ describe('utils', () => {
6161
// indicator. Used go-ipfs@0.4.13 `add --hash=keccak-512` to generate
6262
const keccak512 = 'zB7S6ZdcqsTqvNhBpx3SbFTocRpAUHj1w9WQXQGyWBVEsLStNfaaNtsdFUQbRk4tYPZvnpGbtDN5gEH4uVzUwsFyJh9Ei'
6363
expect(utils.parseIpfsPath(keccak512))
64-
.to.deep.equal({
65-
hash: keccak512,
66-
links: []
67-
})
64+
.to.deep.equal({
65+
hash: keccak512,
66+
links: []
67+
})
6868
})
6969

7070
it('returns error for malformed path', function () {

0 commit comments

Comments
 (0)