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

Commit

Permalink
feat(pin): spec compliant pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 16, 2016
1 parent 776cccd commit ed07921
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 134 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"chai": "^3.5.0",
"gulp": "^3.9.1",
"hapi": "^14.1.0",
"interface-ipfs-core": "^0.8.0",
"interface-ipfs-core": "^0.9.0",
"ipfsd-ctl": "^0.14.0",
"passthrough-counter": "^1.0.0",
"pre-commit": "^1.1.3",
Expand Down
65 changes: 42 additions & 23 deletions src/api/pin.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,68 @@
'use strict'

const promisify = require('promisify-es6')

module.exports = (send) => {
return {
add (hash, opts, callback) {
add: promisify((hash, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = null
}
return send({
send({
path: 'pin/add',
args: hash,
qs: opts
}, callback)
},
remove (hash, opts, callback) {
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, res.Pins)
})
}),
rm: promisify((hash, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = null
}
return send({
send({
path: 'pin/rm',
args: hash,
qs: opts
}, callback)
},
list (type, callback) {
if (typeof type === 'function') {
callback = type
type = null
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, res.Pins)
})
}),
ls: promisify((hash, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
let opts = null
let hash = null

if (typeof type === 'string') {
opts = { type: type }
} else if (type && type.hash) {
hash = type.hash
type.hash = null
opts = type
if (typeof hash === 'object') {
opts = hash
hash = undefined
}
return send({

if (typeof hash === 'function') {
callback = hash
hash = undefined
opts = {}
}

send({
path: 'pin/ls',
args: hash,
qs: opts
}, callback)
}
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, res.Keys)
})
})
}
}
123 changes: 13 additions & 110 deletions test/interface-ipfs-core/pin.spec.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,20 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const expect = require('chai').expect
const test = require('interface-ipfs-core')
const FactoryClient = require('../factory/factory-client')
const fs = require('fs')
const path = require('path')

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

describe('.pin', () => {
let ipfs
let fc

before(function (done) {
this.timeout(20 * 1000) // slow CI
const common = {
setup: function (callback) {
fc = new FactoryClient()
fc.spawnNode((err, node) => {
expect(err).to.not.exist
ipfs = node
done()
})
})

after((done) => {
fc.dismantle(done)
})

it('add file for testing', (done) => {
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()
})
})

it('.pin.remove', (done) => {
ipfs.pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: true}, (err, res) => {
expect(err).to.not.exist
expect(res).to.exist
ipfs.pin.list('direct', (err, res) => {
expect(err).to.not.exist
expect(res).to.exist
expect(res.Keys).to.be.empty
done()
})
})
})

it('.pin.add', (done) => {
ipfs.pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => {
expect(err).to.not.exist
expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
done()
})
})

it('.pin.list', (done) => {
ipfs.pin.list((err, res) => {
expect(err).to.not.exist
expect(res).to.exist
done()
})
})

it('.pin.list hash', (done) => {
ipfs.pin.list({hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'}, (err, res) => {
expect(err).to.not.exist
expect(res).to.exist
done()
})
})

describe('promise', () => {
it('.pin.add', () => {
return ipfs.pin
.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false})
.then((res) => {
expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
})
})

it('.pin.list', () => {
return ipfs.pin.list()
.then((res) => {
expect(res).to.exist
})
})

it('.pin.list hash', () => {
return ipfs.pin.list({
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
})
.then((res) => {
expect(res).to.exist
})
})

it('.pin.remove', () => {
return ipfs.pin
.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false})
.then((res) => {
expect(res).to.exist
return ipfs.pin.list('direct')
})
.then((res) => {
expect(res).to.exist
expect(res.Keys).to.be.empty
})
})
})
})
callback(null, fc)
},
teardown: function (callback) {
fc.dismantle(callback)
}
}

test.pin(common)

0 comments on commit ed07921

Please sign in to comment.