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

Commit ba240fd

Browse files
authored
feat: support remote pinning services in ipfs-http-client (#3293)
Implement [remote pinning service API](https://github.com/ipfs/pinning-services-api-spec) in ipfs-http-client.
1 parent 65dc161 commit ba240fd

File tree

25 files changed

+2214
-22
lines changed

25 files changed

+2214
-22
lines changed

docs/core-api/PIN.md

Lines changed: 413 additions & 11 deletions
Large diffs are not rendered by default.

examples/browser-ipns-publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"delay": "^4.4.0",
2929
"execa": "^4.0.3",
3030
"ipfsd-ctl": "^7.2.0",
31-
"go-ipfs": "^0.7.0",
31+
"go-ipfs": "0.8.0-rc2",
3232
"parcel-bundler": "^1.12.4",
3333
"path": "^0.12.7",
3434
"test-ipfs-example": "^2.0.3"

examples/http-client-browser-pubsub/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"devDependencies": {
2121
"execa": "^4.0.3",
22-
"go-ipfs": "^0.7.0",
22+
"go-ipfs": "0.8.0-rc2",
2323
"ipfs": "^0.53.2",
2424
"ipfsd-ctl": "^7.2.0",
2525
"parcel-bundler": "^1.12.4",

examples/http-client-name-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"devDependencies": {
1919
"execa": "^4.0.3",
20-
"go-ipfs": "^0.7.0",
20+
"go-ipfs": "0.8.0-rc2",
2121
"ipfsd-ctl": "^7.2.0",
2222
"parcel-bundler": "^1.12.4",
2323
"rimraf": "^3.0.2",

packages/interface-ipfs-core/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports.block = require('./block')
2020
exports.dag = require('./dag')
2121
exports.object = require('./object')
2222
exports.pin = require('./pin')
23+
exports.pin.remote = require('./pin/remote')
2324

2425
exports.bootstrap = require('./bootstrap')
2526
exports.dht = require('./dht')
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { fixtures, clearRemotePins, clearServices } = require('../utils')
5+
const { getDescribe, getIt, expect } = require('../../utils/mocha')
6+
const testTimeout = require('../../utils/test-timeout')
7+
const CID = require('cids')
8+
9+
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
10+
/**
11+
* @param {Factory} common
12+
* @param {Object} options
13+
*/
14+
module.exports = (common, options) => {
15+
const describe = getDescribe(options)
16+
const it = getIt(options)
17+
18+
const ENDPOINT = new URL(process.env.PINNING_SERVICE_ENDPOINT || '')
19+
const KEY = process.env.PINNING_SERVIEC_KEY
20+
const SERVICE = 'pinbot'
21+
22+
describe('.pin.remote.add', function () {
23+
this.timeout(50 * 1000)
24+
25+
let ipfs
26+
before(async () => {
27+
ipfs = (await common.spawn()).api
28+
await ipfs.pin.remote.service.add(SERVICE, {
29+
endpoint: ENDPOINT,
30+
key: KEY
31+
})
32+
})
33+
after(async () => {
34+
await clearServices(ipfs)
35+
await common.clean()
36+
})
37+
38+
beforeEach(async () => {
39+
await clearRemotePins(ipfs)
40+
})
41+
42+
it('should add a CID and return the added CID', async () => {
43+
const pin = await ipfs.pin.remote.add(fixtures.files[0].cid, {
44+
name: 'fixtures-files-0',
45+
background: true,
46+
service: SERVICE
47+
})
48+
49+
expect(pin).to.deep.equal({
50+
status: 'queued',
51+
cid: fixtures.files[0].cid,
52+
name: 'fixtures-files-0'
53+
})
54+
})
55+
56+
it('should fail if service is not provided', async () => {
57+
const result = ipfs.pin.remote.add(fixtures.files[0].cid, {
58+
name: 'fixtures-files-0',
59+
background: true
60+
})
61+
62+
await expect(result).to.eventually.be.rejectedWith(/service name must be passed/)
63+
})
64+
65+
it('if name is not provided defaults to ""', async () => {
66+
const pin = await ipfs.pin.remote.add(fixtures.files[0].cid, {
67+
background: true,
68+
service: SERVICE
69+
})
70+
71+
expect(pin).to.deep.equal({
72+
cid: fixtures.files[0].cid,
73+
name: '',
74+
status: 'queued'
75+
})
76+
})
77+
78+
it('should default to blocking pin', async () => {
79+
const { cid } = fixtures.files[0]
80+
const result = ipfs.pin.remote.add(cid, {
81+
service: SERVICE
82+
})
83+
84+
const timeout = {}
85+
86+
const winner = await Promise.race([
87+
result,
88+
new Promise(resolve => setTimeout(resolve, 100, timeout))
89+
])
90+
91+
expect(winner).to.equal(timeout)
92+
93+
// trigger status change on the mock service
94+
ipfs.pin.remote.add(cid, {
95+
service: SERVICE,
96+
name: 'pinned-block'
97+
})
98+
99+
expect(await result).to.deep.equal({
100+
cid,
101+
status: 'pinned',
102+
name: ''
103+
})
104+
})
105+
it('should pin dag-cbor', async () => {
106+
const cid = await ipfs.dag.put({}, {
107+
format: 'dag-cbor',
108+
hashAlg: 'sha2-256'
109+
})
110+
111+
const pin = await ipfs.pin.remote.add(cid, {
112+
service: SERVICE,
113+
name: 'cbor-pin',
114+
background: true
115+
})
116+
117+
expect(pin).to.deep.equal({
118+
cid,
119+
name: 'cbor-pin',
120+
status: 'queued'
121+
})
122+
})
123+
124+
it('should pin raw', async () => {
125+
const cid = await ipfs.dag.put(new Uint8Array(0), {
126+
format: 'raw',
127+
hashAlg: 'sha2-256'
128+
})
129+
130+
const pin = await ipfs.pin.remote.add(cid, {
131+
service: SERVICE,
132+
background: true
133+
})
134+
135+
expect(pin).to.deep.equal({
136+
cid,
137+
status: 'queued',
138+
name: ''
139+
})
140+
})
141+
142+
it('should respect timeout option when pinning a block', () => {
143+
return testTimeout(() => ipfs.pin.remote.add(new CID('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ'), {
144+
timeout: 1,
145+
service: SERVICE
146+
}))
147+
})
148+
})
149+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
const { createSuite } = require('../../utils/suite')
3+
4+
const tests = {
5+
service: require('./service'),
6+
add: require('./add'),
7+
ls: require('./ls'),
8+
rm: require('./rm'),
9+
rmAll: require('./rm-all')
10+
}
11+
12+
module.exports = createSuite(tests)

0 commit comments

Comments
 (0)