Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

pinning API #34

Merged
merged 4 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions API/pin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Pin API
=======

#### `add`

> Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).

##### `Go` **WIP**

##### `JavaScript` - ipfs.pin.add(hash, [options, callback])

Where:

- `hash` is an IPFS multihash.
- `options` is an object that can contain the following keys
- 'recursive' - Recursively pin the object linked. Type: bool. Default: `false`

`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an array of objects that represent the files that were pinned. Example:

```JavaScript
{
hash: 'QmHash'
}
```

If no `callback` is passed, a promise is returned.

Example:

```JavaScript
ipfs.pin.add(hash, function (err) {})
```

#### `ls`

> List all the objects pinned to local storage or under a specific hash.

##### `Go` **WIP**

##### `JavaScript` - ipfs.pin.ls([hash, options, callback])

Where:

- `hash` is an IPFS multihash.
- `options` is an object that can contain the following keys:
- 'type' - Return also the type of pin (direct, indirect or recursive)

`callback` must follow `function (err, pinset) {}` signature, where `err` is an error if the operation was not successful. `pinset` is an array of objects with keys `hash` and `type`.

If no `callback` is passed, a promise is returned.

Example:

```JavaScript
ipfs.pin.ls(function (err, pinset) {
if (err) {
throw err
}
console.log(pinset)
})
```


#### `rm`

> Remove a hash from the pinset

##### `Go` **WIP**

##### `JavaScript` - ipfs.pin.rm(hash, [options, callback])

Where:
- `hash` is a multihash.
- `options` is an object that can contain the following keys
- 'recursive' - Recursively unpin the object linked. Type: bool. Default: `false`

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no `callback` is passed, a promise is returned.

Example:

```JavaScript
ipfs.pin.rm(hash, function (err, pinset) {
if (err) {
throw err
}
console.log(pinset) prints the hashes that were unpinned
})
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
"greenkeeperio-bot <[email protected]>",
"nginnever <[email protected]>"
]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated. Pull out, put in own PR.

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
exports.object = require('./object')
exports.files = require('./files')
exports.config = require('./config')
exports.pin = require('./pin')
158 changes: 158 additions & 0 deletions src/pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */

'use strict'

const expect = require('chai').expect
const fs = require('fs')
const path = require('path')

const testfile = fs.readFileSync(path.join(__dirname, './data/testfile.txt'))

module.exports = (common) => {
describe('.pin', () => {
let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon,
// so we need to increase the timeout for the
// before step
this.timeout(20 * 1000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor this out, make it a global thing for all tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just the step of creating the node. If put globally, then we always have to wait 20 seconds for tests to timeout.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not say that, instead of "CI is slow?"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!


common.setup((err, factory) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does common come from? Curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the 'interface test suite pattern', see: https://github.com/ipfs/interface-ipfs-core#usage

expect(err).to.not.exist
factory.spawnNode((err, node) => {
expect(err).to.not.exist
ipfs = node
populate()
})
})

function populate () {
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

ipfs.files.add(testfile, (err, res) => {
expect(err).to.not.exist

expect(res).to.have.length(1)
expect(res[0].hash).to.equal(expectedMultihash)
expect(res[0].path).to.equal(expectedMultihash)
done()
})
}
})

after((done) => {
common.teardown(done)
})

describe('callback API', () => {
// 1st, because ipfs.files.add pins automatically
it('.rm', (done) => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this const declared above, not sure why you declare it so many times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of like of having tests that are really self descriptive, it makes it easier to read and to change.


ipfs.pin.rm(hash, { recursive: true }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.exist
ipfs.pin.ls({ type: 'direct' }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.be.empty
done()
})
})
})

it('.add', (done) => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

ipfs.pin.add(hash, { recursive: false }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset[0]).to.be.equal(hash)
done()
})
})

it('.ls', (done) => {
ipfs.pin.ls((err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.not.be.empty
done()
})
})

it('.ls type direct', (done) => {
ipfs.pin.ls({ type: 'direct' }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.not.be.empty
done()
})
})

it('.ls type indirect', (done) => {
ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.not.be.empty
done()
})
})

it('.ls type recursive', (done) => {
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.not.be.empty
done()
})
})
Copy link
Contributor

@dignifiedquire dignifiedquire Aug 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three tests above could be simplified to

function testLs (type) {
  return it(`.ls type ${type}`, (done) => {
    ipfs.pin.ls({type}, (err, pinset) => {
      expect(err).to.not.exist
      expect(Object.keys(pinset)).to.be.not.empty
      done()
    })
  })
}

testLs('recursive')
testLs('indirect')
testLs('direct')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified, but not simpler for changes, reading, etc :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, but please use .not.empty rather than .length > 0).equal(true)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you got it :)


it('.ls for a specific hash', (done) => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

ipfs.pin.ls(hash, (err, pinset) => {
expect(err).to.not.exist
expect(pinset).to.exist
done()
})
})
})

describe('promise API', () => {
it('.add', () => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

return ipfs.pin.add(hash, { recursive: false })
.then((pinset) => {
expect(pinset[0]).to.be.equal(hash)
})
})

it('.ls', () => {
return ipfs.pin.ls()
.then((pinset) => {
expect(pinset).to.exist
expect(pinset).to.not.be.empty
})
})

it('.ls hash', () => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

return ipfs.pin.ls(hash)
.then((pinset) => {
expect(pinset).to.exist
})
})

it('.rm', () => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

return ipfs.pin.rm(hash, { recursive: false })
.then((pinset) => {
return ipfs.pin.ls({ type: 'direct' })
})
.then((pinset) => {
expect(pinset).to.be.empty
})
})
})
})
}