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

Commit

Permalink
feat: support remote pinning services in ipfs-http-client (#3293)
Browse files Browse the repository at this point in the history
Implement [remote pinning service API](https://github.com/ipfs/pinning-services-api-spec) in ipfs-http-client.
  • Loading branch information
lidel authored Jan 30, 2021
1 parent 65dc161 commit ba240fd
Show file tree
Hide file tree
Showing 25 changed files with 2,214 additions and 22 deletions.
424 changes: 413 additions & 11 deletions docs/core-api/PIN.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/browser-ipns-publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"delay": "^4.4.0",
"execa": "^4.0.3",
"ipfsd-ctl": "^7.2.0",
"go-ipfs": "^0.7.0",
"go-ipfs": "0.8.0-rc2",
"parcel-bundler": "^1.12.4",
"path": "^0.12.7",
"test-ipfs-example": "^2.0.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client-browser-pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"devDependencies": {
"execa": "^4.0.3",
"go-ipfs": "^0.7.0",
"go-ipfs": "0.8.0-rc2",
"ipfs": "^0.53.2",
"ipfsd-ctl": "^7.2.0",
"parcel-bundler": "^1.12.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client-name-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"devDependencies": {
"execa": "^4.0.3",
"go-ipfs": "^0.7.0",
"go-ipfs": "0.8.0-rc2",
"ipfsd-ctl": "^7.2.0",
"parcel-bundler": "^1.12.4",
"rimraf": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/interface-ipfs-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports.block = require('./block')
exports.dag = require('./dag')
exports.object = require('./object')
exports.pin = require('./pin')
exports.pin.remote = require('./pin/remote')

exports.bootstrap = require('./bootstrap')
exports.dht = require('./dht')
Expand Down
149 changes: 149 additions & 0 deletions packages/interface-ipfs-core/src/pin/remote/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-env mocha */
'use strict'

const { fixtures, clearRemotePins, clearServices } = require('../utils')
const { getDescribe, getIt, expect } = require('../../utils/mocha')
const testTimeout = require('../../utils/test-timeout')
const CID = require('cids')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)

const ENDPOINT = new URL(process.env.PINNING_SERVICE_ENDPOINT || '')
const KEY = process.env.PINNING_SERVIEC_KEY
const SERVICE = 'pinbot'

describe('.pin.remote.add', function () {
this.timeout(50 * 1000)

let ipfs
before(async () => {
ipfs = (await common.spawn()).api
await ipfs.pin.remote.service.add(SERVICE, {
endpoint: ENDPOINT,
key: KEY
})
})
after(async () => {
await clearServices(ipfs)
await common.clean()
})

beforeEach(async () => {
await clearRemotePins(ipfs)
})

it('should add a CID and return the added CID', async () => {
const pin = await ipfs.pin.remote.add(fixtures.files[0].cid, {
name: 'fixtures-files-0',
background: true,
service: SERVICE
})

expect(pin).to.deep.equal({
status: 'queued',
cid: fixtures.files[0].cid,
name: 'fixtures-files-0'
})
})

it('should fail if service is not provided', async () => {
const result = ipfs.pin.remote.add(fixtures.files[0].cid, {
name: 'fixtures-files-0',
background: true
})

await expect(result).to.eventually.be.rejectedWith(/service name must be passed/)
})

it('if name is not provided defaults to ""', async () => {
const pin = await ipfs.pin.remote.add(fixtures.files[0].cid, {
background: true,
service: SERVICE
})

expect(pin).to.deep.equal({
cid: fixtures.files[0].cid,
name: '',
status: 'queued'
})
})

it('should default to blocking pin', async () => {
const { cid } = fixtures.files[0]
const result = ipfs.pin.remote.add(cid, {
service: SERVICE
})

const timeout = {}

const winner = await Promise.race([
result,
new Promise(resolve => setTimeout(resolve, 100, timeout))
])

expect(winner).to.equal(timeout)

// trigger status change on the mock service
ipfs.pin.remote.add(cid, {
service: SERVICE,
name: 'pinned-block'
})

expect(await result).to.deep.equal({
cid,
status: 'pinned',
name: ''
})
})
it('should pin dag-cbor', async () => {
const cid = await ipfs.dag.put({}, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
})

const pin = await ipfs.pin.remote.add(cid, {
service: SERVICE,
name: 'cbor-pin',
background: true
})

expect(pin).to.deep.equal({
cid,
name: 'cbor-pin',
status: 'queued'
})
})

it('should pin raw', async () => {
const cid = await ipfs.dag.put(new Uint8Array(0), {
format: 'raw',
hashAlg: 'sha2-256'
})

const pin = await ipfs.pin.remote.add(cid, {
service: SERVICE,
background: true
})

expect(pin).to.deep.equal({
cid,
status: 'queued',
name: ''
})
})

it('should respect timeout option when pinning a block', () => {
return testTimeout(() => ipfs.pin.remote.add(new CID('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ'), {
timeout: 1,
service: SERVICE
}))
})
})
}
12 changes: 12 additions & 0 deletions packages/interface-ipfs-core/src/pin/remote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
const { createSuite } = require('../../utils/suite')

const tests = {
service: require('./service'),
add: require('./add'),
ls: require('./ls'),
rm: require('./rm'),
rmAll: require('./rm-all')
}

module.exports = createSuite(tests)
Loading

0 comments on commit ba240fd

Please sign in to comment.