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

Commit ed07921

Browse files
committed
feat(pin): spec compliant pinning
1 parent 776cccd commit ed07921

File tree

3 files changed

+56
-134
lines changed

3 files changed

+56
-134
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"chai": "^3.5.0",
4848
"gulp": "^3.9.1",
4949
"hapi": "^14.1.0",
50-
"interface-ipfs-core": "^0.8.0",
50+
"interface-ipfs-core": "^0.9.0",
5151
"ipfsd-ctl": "^0.14.0",
5252
"passthrough-counter": "^1.0.0",
5353
"pre-commit": "^1.1.3",

src/api/pin.js

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,68 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = (send) => {
46
return {
5-
add (hash, opts, callback) {
7+
add: promisify((hash, opts, callback) => {
68
if (typeof opts === 'function') {
79
callback = opts
810
opts = null
911
}
10-
return send({
12+
send({
1113
path: 'pin/add',
1214
args: hash,
1315
qs: opts
14-
}, callback)
15-
},
16-
remove (hash, opts, callback) {
16+
}, (err, res) => {
17+
if (err) {
18+
return callback(err)
19+
}
20+
callback(null, res.Pins)
21+
})
22+
}),
23+
rm: promisify((hash, opts, callback) => {
1724
if (typeof opts === 'function') {
1825
callback = opts
1926
opts = null
2027
}
21-
return send({
28+
send({
2229
path: 'pin/rm',
2330
args: hash,
2431
qs: opts
25-
}, callback)
26-
},
27-
list (type, callback) {
28-
if (typeof type === 'function') {
29-
callback = type
30-
type = null
32+
}, (err, res) => {
33+
if (err) {
34+
return callback(err)
35+
}
36+
callback(null, res.Pins)
37+
})
38+
}),
39+
ls: promisify((hash, opts, callback) => {
40+
if (typeof opts === 'function') {
41+
callback = opts
42+
opts = {}
3143
}
32-
let opts = null
33-
let hash = null
3444

35-
if (typeof type === 'string') {
36-
opts = { type: type }
37-
} else if (type && type.hash) {
38-
hash = type.hash
39-
type.hash = null
40-
opts = type
45+
if (typeof hash === 'object') {
46+
opts = hash
47+
hash = undefined
4148
}
42-
return send({
49+
50+
if (typeof hash === 'function') {
51+
callback = hash
52+
hash = undefined
53+
opts = {}
54+
}
55+
56+
send({
4357
path: 'pin/ls',
4458
args: hash,
4559
qs: opts
46-
}, callback)
47-
}
60+
}, (err, res) => {
61+
if (err) {
62+
return callback(err)
63+
}
64+
callback(null, res.Keys)
65+
})
66+
})
4867
}
4968
}

test/interface-ipfs-core/pin.spec.js

+13-110
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,20 @@
11
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
23
'use strict'
34

4-
const expect = require('chai').expect
5+
const test = require('interface-ipfs-core')
56
const FactoryClient = require('../factory/factory-client')
6-
const fs = require('fs')
7-
const path = require('path')
87

9-
const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt'))
8+
let fc
109

11-
describe('.pin', () => {
12-
let ipfs
13-
let fc
14-
15-
before(function (done) {
16-
this.timeout(20 * 1000) // slow CI
10+
const common = {
11+
setup: function (callback) {
1712
fc = new FactoryClient()
18-
fc.spawnNode((err, node) => {
19-
expect(err).to.not.exist
20-
ipfs = node
21-
done()
22-
})
23-
})
24-
25-
after((done) => {
26-
fc.dismantle(done)
27-
})
28-
29-
it('add file for testing', (done) => {
30-
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
31-
32-
ipfs.files.add(testfile, (err, res) => {
33-
expect(err).to.not.exist
34-
35-
expect(res).to.have.length(1)
36-
expect(res[0].hash).to.equal(expectedMultihash)
37-
expect(res[0].path).to.equal(expectedMultihash)
38-
done()
39-
})
40-
})
41-
42-
it('.pin.remove', (done) => {
43-
ipfs.pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: true}, (err, res) => {
44-
expect(err).to.not.exist
45-
expect(res).to.exist
46-
ipfs.pin.list('direct', (err, res) => {
47-
expect(err).to.not.exist
48-
expect(res).to.exist
49-
expect(res.Keys).to.be.empty
50-
done()
51-
})
52-
})
53-
})
54-
55-
it('.pin.add', (done) => {
56-
ipfs.pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => {
57-
expect(err).to.not.exist
58-
expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
59-
done()
60-
})
61-
})
62-
63-
it('.pin.list', (done) => {
64-
ipfs.pin.list((err, res) => {
65-
expect(err).to.not.exist
66-
expect(res).to.exist
67-
done()
68-
})
69-
})
70-
71-
it('.pin.list hash', (done) => {
72-
ipfs.pin.list({hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'}, (err, res) => {
73-
expect(err).to.not.exist
74-
expect(res).to.exist
75-
done()
76-
})
77-
})
78-
79-
describe('promise', () => {
80-
it('.pin.add', () => {
81-
return ipfs.pin
82-
.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false})
83-
.then((res) => {
84-
expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
85-
})
86-
})
87-
88-
it('.pin.list', () => {
89-
return ipfs.pin.list()
90-
.then((res) => {
91-
expect(res).to.exist
92-
})
93-
})
94-
95-
it('.pin.list hash', () => {
96-
return ipfs.pin.list({
97-
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
98-
})
99-
.then((res) => {
100-
expect(res).to.exist
101-
})
102-
})
103-
104-
it('.pin.remove', () => {
105-
return ipfs.pin
106-
.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false})
107-
.then((res) => {
108-
expect(res).to.exist
109-
return ipfs.pin.list('direct')
110-
})
111-
.then((res) => {
112-
expect(res).to.exist
113-
expect(res.Keys).to.be.empty
114-
})
115-
})
116-
})
117-
})
13+
callback(null, fc)
14+
},
15+
teardown: function (callback) {
16+
fc.dismantle(callback)
17+
}
18+
}
19+
20+
test.pin(common)

0 commit comments

Comments
 (0)