Skip to content

Commit

Permalink
feat: Filebase support (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsmedley authored Sep 1, 2022
1 parent a7c9ef9 commit 0f0d353
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 3 deletions.
5 changes: 5 additions & 0 deletions md/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const cid = await deploy({
host: argv.ipfsCluster && argv.ipfsCluster.host,
username: argv.ipfsCluster && argv.ipfsCluster.username,
password: argv.ipfsCluster && argv.ipfsCluster.password
},
filebase: {
apiKey: argv.filebase && argv.filebase.apiKey,
secretApiKey: argv.filebase && argv.filebase.secretApiKey,
bucket: argv.filebase && argv.filebase.bucket
}
}
})
Expand Down
15 changes: 14 additions & 1 deletion md/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [IPFS Cluster](#ipfs-cluster)
- [Pinata](#pinata)
- [C4REX](#c4rex)
- [Filebase](#filebase)
- [DNS Providers Configuration](#dns-providers-configuration)
- [Cloudflare](#cloudflare)
- [DNSimple](#dnsimple)
Expand Down Expand Up @@ -69,7 +70,7 @@ Options:
-p, --pinner Pin to this services. If -u is set, these services are
only used to pin and not upload. Defaults to "infura" if
neither -u or -p are set.
[choices: "dappnode", "infura", "ipfs-cluster", "pinata"]
[choices: "dappnode", "infura", "ipfs-cluster", "pinata", "filebase"]
-d, --dns DNS provider whose dnslink TXT field will be updated
[choices: "cloudflare", "dnsimple", "dreamhost"]
-c, --cid Pin this CID instead of uploading
Expand Down Expand Up @@ -187,6 +188,18 @@ C4REX is a free to use upload and pinning service provided by [C4REX nearshore s

- Usage: `-u c4rex -p c4rex`

### [Filebase](https://filebase.com)

Filebase is another freemium pinning service. It gives you more control over
what's uploaded. You can delete, label and add custom metadata. This service
requires #.

- Usage: `-p filebase`
- Environment variables
- `IPFS_DEPLOY_FILEBASE__API_KEY=<api key>`
- `IPFS_DEPLOY_FILEBASE__SECRET_API_KEY=<secret api key>`
- `IPFS_DEPLOY_FILEBASE__BUCKET=<bucket>`

## DNS Providers Configuration

### [Cloudflare](https://cloudflare.com)
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ipfs-deploy",
"version": "11.2.2",
"version": "11.2.3",
"description": "Zero-Config CLI to Deploy Static Websites to IPFS",
"keywords": [
"ipfs",
Expand Down Expand Up @@ -67,6 +67,7 @@
],
"dependencies": {
"@aws-sdk/client-route-53": "^3.53.0",
"@filebase/client": "^0.0.2",
"axios": "^0.26.0",
"byte-size": "^8.1.0",
"chalk": "^4.1.1",
Expand All @@ -75,6 +76,7 @@
"dnslink-dnsimple": "^1.0.1",
"dotenv": "^16.0.0",
"dreamhost": "^1.0.5",
"files-from-path": "^0.2.6",
"form-data": "^4.0.0",
"ipfs-http-client": "^50.1.0",
"it-all": "^1.0.6",
Expand Down
5 changes: 5 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const options = {
host: argv.ipfsCluster && argv.ipfsCluster.host,
username: argv.ipfsCluster && argv.ipfsCluster.username,
password: argv.ipfsCluster && argv.ipfsCluster.password
},
filebase: {
apiKey: argv.filebase && argv.filebase.apiKey,
secretApiKey: argv.filebase && argv.filebase.secretApiKey,
bucket: argv.filebase && argv.filebase.bucket
}
},

Expand Down
95 changes: 95 additions & 0 deletions src/pinners/filebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict'

const isEmpty = require('lodash.isempty')
const { FilebaseClient } = require('@filebase/client')
const { filesFromPath } = require('files-from-path')
const { default: axios } = require('axios')

/**
* @typedef {import('./types').FilebaseOptions} FilebaseOptions
* @typedef {import('./types').PinDirOptions} PinDirOptions
*/

const PIN_HASH_URL = 'https://api.filebase.io/v1/ipfs/pins'

class Filebase {
/**
* @param {FilebaseOptions} options
*/
constructor ({ apiKey, secretApiKey, bucket }) {
if ([apiKey, secretApiKey, bucket].some(isEmpty)) {
throw new Error('apiKey and secretApiKey are required for Pinata')
}

this.auth = {
api_key: apiKey,
secret_api_key: secretApiKey,
bucket: bucket
}

this.tokenString = btoa(`${this.auth.api_key}:${this.auth.secret_api_key}:${this.auth.bucket}`)
}

/**
* @param {string} dir
* @param {PinDirOptions|undefined} options
* @returns {Promise<string>}
*/
async pinDir (dir, { tag, hidden = false } = {}) {
const files = []
for await (const file of filesFromPath(dir, { pathPrefix: dir })) {
files.push(file)
}

const cid = await FilebaseClient.storeDirectory(
{ endpoint: 'https://s3.filebase.com', token: this.tokenString },
files,
tag
)

return cid
}

/**
* @param {string} cid
* @param {string|undefined} tag
* @returns {Promise<void>}
*/
async pinCid (cid, tag) {
const body = JSON.stringify({
cid: cid,
name: tag
})

const config = {
headers: {
Authorization: `Bearer ${this.tokenString}`,
'Content-Type': 'application/json'
}
}

await axios.post(PIN_HASH_URL, body, config)
}

/**
* @param {string} cid
* @returns string
*/
gatewayUrl (cid) {
return `https://ipfs.filebase.io/ipfs/${cid}`
}

static get displayName () {
return 'Filebase'
}

get displayName () {
return Filebase.displayName
}

static get slug () {
return 'filebase'
}
}

module.exports = Filebase
4 changes: 3 additions & 1 deletion src/pinners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const Infura = require('./infura')
const IpfsCluster = require('./ipfs-cluster')
const Pinata = require('./pinata')
const c4rex = require('./c4rex')
const Filebase = require('./filebase')

const pinners = [
DAppNode,
Infura,
IpfsCluster,
Pinata,
c4rex
c4rex,
Filebase
]

const pinnersMap = pinners.reduce((map, pinner) => {
Expand Down
6 changes: 6 additions & 0 deletions src/pinners/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export interface PinataOptions {
apiKey: string
secretApiKey: string
}

export interface FilebaseOptions {
apiKey: string
secretApiKey: string
bucket: string
}

0 comments on commit 0f0d353

Please # to comment.