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

Commit

Permalink
refactor: do not expose pinTypes
Browse files Browse the repository at this point in the history
They're simple enough, documented elsewhere, and not used by any exposed functionality.
  • Loading branch information
JonKrone committed Jun 14, 2018
1 parent 8b3fc26 commit cc821fe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 48 deletions.
53 changes: 26 additions & 27 deletions src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = function pin (self) {
const repo = self._repo
const dag = self.dag
const pinset = createPinSet(dag)
const types = {
direct: 'direct',
recursive: 'recursive',
indirect: 'indirect',
all: 'all'
}

let directPins = new Set()
let recursivePins = new Set()
Expand Down Expand Up @@ -61,14 +67,14 @@ module.exports = function pin (self) {
// create a DAGLink to the node with direct pins
cb => async.waterfall([
cb => pinset.storeSet(directKeys(), cb),
(node, cb) => DAGLink.create(pin.types.direct, node.size, node.multihash, cb),
(node, cb) => DAGLink.create(types.direct, node.size, node.multihash, cb),
(link, cb) => { dLink = link; cb(null) }
], cb),

// create a DAGLink to the node with recursive pins
cb => async.waterfall([
cb => pinset.storeSet(recursiveKeys(), cb),
(node, cb) => DAGLink.create(pin.types.recursive, node.size, node.multihash, cb),
(node, cb) => DAGLink.create(types.recursive, node.size, node.multihash, cb),
(link, cb) => { rLink = link; cb(null) }
], cb),

Expand Down Expand Up @@ -98,13 +104,6 @@ module.exports = function pin (self) {
}

const pin = {
types: {
direct: 'direct',
recursive: 'recursive',
indirect: 'indirect',
all: 'all'
},

add: promisify((paths, options, callback) => {
if (typeof options === 'function') {
callback = options
Expand Down Expand Up @@ -177,7 +176,7 @@ module.exports = function pin (self) {

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

switch (reason) {
case (pin.types.recursive):
case (types.recursive):
if (recursive) {
return cb(null, key)
} else {
return cb(new Error(`${key} is pinned recursively`))
}
case (pin.types.direct):
case (types.direct):
return cb(null, key)
default:
return cb(new Error(
Expand Down Expand Up @@ -223,7 +222,7 @@ module.exports = function pin (self) {
}),

ls: promisify((paths, options, callback) => {
let type = pin.types.all
let type = types.all
if (typeof paths === 'function') {
callback = paths
options = null
Expand All @@ -239,7 +238,7 @@ module.exports = function pin (self) {
if (options && options.type) {
type = options.type.toLowerCase()
}
if (!pin.types[type]) {
if (!types[type]) {
return callback(new Error(
`Invalid type '${type}', must be one of {direct, indirect, recursive, all}`
))
Expand All @@ -251,7 +250,7 @@ module.exports = function pin (self) {
if (err) { return callback(err) }

async.mapSeries(mhs, (multihash, cb) => {
pin._isPinnedWithType(multihash, pin.types.all, (err, res) => {
pin._isPinnedWithType(multihash, types.all, (err, res) => {
if (err) { return cb(err) }
const { pinned, reason } = res
const key = toB58String(multihash)
Expand All @@ -260,16 +259,16 @@ module.exports = function pin (self) {
}

switch (reason) {
case pin.types.direct:
case pin.types.recursive:
case types.direct:
case types.recursive:
return cb(null, {
hash: key,
type: reason
})
default:
return cb(null, {
hash: key,
type: `${pin.types.indirect} through ${reason}`
type: `${types.indirect} through ${reason}`
})
}
})
Expand All @@ -278,23 +277,23 @@ module.exports = function pin (self) {
} else {
// show all pinned items of type
let pins = []
if (type === pin.types.direct || type === pin.types.all) {
if (type === types.direct || type === types.all) {
pins = pins.concat(
Array.from(directPins).map(hash => ({
type: pin.types.direct,
type: types.direct,
hash
}))
)
}
if (type === pin.types.recursive || type === pin.types.all) {
if (type === types.recursive || type === types.all) {
pins = pins.concat(
Array.from(recursivePins).map(hash => ({
type: pin.types.recursive,
type: types.recursive,
hash
}))
)
}
if (type === pin.types.indirect || type === pin.types.all) {
if (type === types.indirect || type === types.all) {
getIndirectKeys((err, indirects) => {
if (err) { return callback(err) }
pins = pins
Expand All @@ -305,7 +304,7 @@ module.exports = function pin (self) {
(indirects.includes(hash) && !directPins.has(hash))
)
.concat(indirects.map(hash => ({
type: pin.types.indirect,
type: types.indirect,
hash
})))
return callback(null, pins)
Expand All @@ -318,7 +317,7 @@ module.exports = function pin (self) {

_isPinnedWithType: promisify((multihash, type, callback) => {
const key = toB58String(multihash)
const { recursive, direct, all } = pin.types
const { recursive, direct, all } = types
// recursive
if ((type === recursive || type === all) && recursivePins.has(key)) {
return callback(null, {pinned: true, reason: recursive})
Expand Down Expand Up @@ -374,8 +373,8 @@ module.exports = function pin (self) {
}

async.parallel([
cb => pinset.loadSet(pinRoot.value, pin.types.recursive, cb),
cb => pinset.loadSet(pinRoot.value, pin.types.direct, cb)
cb => pinset.loadSet(pinRoot.value, types.recursive, cb),
cb => pinset.loadSet(pinRoot.value, types.direct, cb)
], (err, [rKeys, dKeys]) => {
if (err) { return callback(err) }

Expand Down
1 change: 0 additions & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const CID = require('cids')
const multihashes = require('multihashes')
const promisify = require('promisify-es6')
const map = require('async/map')
Expand Down
3 changes: 1 addition & 2 deletions src/http/api/resources/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ function parseArgs (request, reply) {

exports.ls = {
parseArgs: (request, reply) => {
const ipfs = request.server.app.ipfs
const type = request.query.type || ipfs.pin.types.all
const type = request.query.type || 'all'

return reply({
path: request.query.arg,
Expand Down
34 changes: 20 additions & 14 deletions test/core/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const pins = {
mercuryDir: 'QmbJCNKXJqVK8CzbjpNFz2YekHwh3CSHpBA86uqYg3sJ8q',
mercuryWiki: 'QmVgSHAdMxFAuMP2JiMAYkB8pCWP1tcB9djqvq8GKAFiHi'
}
const pinTypes = {
direct: 'direct',
recursive: 'recursive',
indirect: 'indirect',
all: 'all'
}

describe('pin', function () {
const fixtures = [
Expand All @@ -44,22 +50,22 @@ describe('pin', function () {
type = undefined
}

return pin._isPinnedWithType(hash, type || pin.types.all)
return pin._isPinnedWithType(hash, type || pinTypes.all)
.then(result => expect(result.pinned).to.eql(pinned))
}

function clearPins () {
return pin.ls()
.then(ls => {
const pinsToRemove = ls
.filter(out => out.type === pin.types.recursive)
.filter(out => out.type === pinTypes.recursive)
.map(out => pin.rm(out.hash))
return Promise.all(pinsToRemove)
})
.then(() => pin.ls())
.then(ls => {
const pinsToRemove = ls
.filter(out => out.type === pin.types.direct)
.filter(out => out.type === pinTypes.direct)
.map(out => pin.rm(out.hash))
return Promise.all(pinsToRemove)
})
Expand All @@ -85,13 +91,13 @@ describe('pin', function () {

it('when node is pinned', function () {
return pin.add(pins.solarWiki)
.then(() => pin._isPinnedWithType(pins.solarWiki, pin.types.all))
.then(() => pin._isPinnedWithType(pins.solarWiki, pinTypes.all))
.then(pinned => expect(pinned.pinned).to.eql(true))
})

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

it('when pinned recursively', function () {
return pin._isPinnedWithType(pins.root, pin.types.recursive)
return pin._isPinnedWithType(pins.root, pinTypes.recursive)
.then(result => {
expect(result.pinned).to.eql(true)
expect(result.reason).to.eql(pin.types.recursive)
expect(result.reason).to.eql(pinTypes.recursive)
})
})

it('when pinned indirectly', function () {
return pin._isPinnedWithType(pins.mercuryWiki, pin.types.indirect)
return pin._isPinnedWithType(pins.mercuryWiki, pinTypes.indirect)
.then(result => {
expect(result.pinned).to.eql(true)
expect(result.reason).to.eql(pins.root)
Expand All @@ -122,17 +128,17 @@ describe('pin', function () {
it('when pinned directly', function () {
return pin.add(pins.mercuryDir, { recursive: false })
.then(() => {
return pin._isPinnedWithType(pins.mercuryDir, pin.types.direct)
return pin._isPinnedWithType(pins.mercuryDir, pinTypes.direct)
.then(result => {
expect(result.pinned).to.eql(true)
expect(result.reason).to.eql(pin.types.direct)
expect(result.reason).to.eql(pinTypes.direct)
})
})
})

it('when not pinned', function () {
return clearPins()
.then(() => pin._isPinnedWithType(pins.mercuryDir, pin.types.direct))
.then(() => pin._isPinnedWithType(pins.mercuryDir, pinTypes.direct))
.then(pin => expect(pin.pinned).to.eql(false))
})
})
Expand Down Expand Up @@ -165,8 +171,8 @@ describe('pin', function () {
.then(() => pin.add(pins.root))
.then(() => Promise.all([
// solarWiki is pinned both directly and indirectly o.O
expectPinned(pins.solarWiki, pin.types.direct),
expectPinned(pins.solarWiki, pin.types.indirect)
expectPinned(pins.solarWiki, pinTypes.direct),
expectPinned(pins.solarWiki, pinTypes.indirect)
]))
})

Expand Down Expand Up @@ -208,7 +214,7 @@ describe('pin', function () {
return pin.ls()
.then(ls => {
const pinType = ls.find(out => out.hash === pins.mercuryDir).type
expect(pinType).to.eql(pin.types.indirect)
expect(pinType).to.eql(pinTypes.indirect)
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ describe('utils', () => {
// indicator. Used go-ipfs@0.4.13 `add --hash=keccak-512` to generate
const keccak512 = 'zB7S6ZdcqsTqvNhBpx3SbFTocRpAUHj1w9WQXQGyWBVEsLStNfaaNtsdFUQbRk4tYPZvnpGbtDN5gEH4uVzUwsFyJh9Ei'
expect(utils.parseIpfsPath(keccak512))
.to.deep.equal({
hash: keccak512,
links: []
})
.to.deep.equal({
hash: keccak512,
links: []
})
})

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

0 comments on commit cc821fe

Please sign in to comment.