Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@nftstorage/ipfs-cluster
Advanced tools
A zero-dependency client to the IPFS Cluster HTTP API, built for the browser.
A zero-dependency client to the IPFS Cluster HTTP API, built for the browser.
Import it from your favourite CDN (e.g. skypack.dev, unpkg.com, jsdelivr.com) or install directly from npm:
npm i @nftstorage/ipfs-cluster
Example:
import { Cluster } from 'https://cdn.skypack.dev/@nftstorage/ipfs-cluster'
const cluster = new Cluster('https://your-cluster-domain.com')
const file = new File(['foo'], 'foo.txt')
const { cid } = await cluster.add(file)
console.log(cid) // bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu
This library is designed to run in the browser or in web workers but it can be run in Node.js if required web APIs are added to the global environment. For exmaple:
import fetch from '@web-std/fetch'
import { FormData } from '@web-std/form-data'
import { File, Blob } from '@web-std/file'
Object.assign(global, { fetch, File, Blob, FormData })
This library is WIP and not all cluster HTTP API methods are available yet (PR's welcome!). Please see the typescript types for full parameter and return types.
Note: all methods take an options object with a signal
property - an AbortSignal
from an AbortController
allowing the request to be aborted by the caller.
add
addCAR
addDirectory
alerts
allocations
id
metrics
metricNames
peerList
peerAdd
peerRemove
pin
pinPath
recover
recoverAll
repoGC
status
statusAll
version
unpin
unpinPath
Create a new instance of the cluster client.
import { Cluster } from '@nftstorage/ipfs-cluster'
const cluster = new Cluster('https://your-cluster-domain.com', {
// optional custom headers for e.g. auth
headers: { Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' }
})
add
Import a file to the cluster. First argument must be a File
or Blob
.
const file = new File(['foo'], 'foo.txt')
const { cid } = await cluster.add(file)
Note: by default this module uses v1 CIDs and raw leaves enabled.
addCAR
Alternatively you can import data from a CAR (Content Addressable aRchive) file:
const car = new Blob(carFileData, { type: 'application/car' })
const { cid } = await cluster.addCAR(car)
More information on reading and writing CAR files in JS.
addDirectory
Imports multiple files to the cluster. First argument must be an array of File
or Blob
.
const files = [new File(['foo'], 'foo.txt'), new File(['bar'], 'bar.txt')]
const dir = await cluster.addDirectory(file)
for (const entry of dir) {
console.log(entry.cid)
// bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu
// bafybeidsnna57jpm2ttwaydwak25qpkxafrg4cnrjsfqipbcqxlsfobjje
}
Note: by default this module uses v1 CIDs and raw leaves enabled.
allocation
Returns the current allocation for a given CID.
const cid = 'bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu'
const allocation = await cluster.allocation(cid)
metricNames
Get a list of metric types known to the peer.
const names = await cluster.metricNames()
console.log(names) // [ 'ping', 'freespace' ]
peerList
Get a list of Cluster peer info.
const peers = await cluster.peerList()
peers.forEach((peer) => {
console.log(`${peer.id} | ${peer.peerName}`)
console.log(' > Addresses:')
peer.addresses.forEach((addr) => console.log(` - ${addr}`))
console.log(` > IPFS: ${peer.ipfs.id}`)
peer.ipfs.addresses.forEach((addr) => console.log(` - ${addr}`))
})
pin
Tracks a CID with the given replication factor and a name for human-friendliness.
const cid = 'bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu'
const { cid } = await cluster.pin(cid)
recover
Re-triggers pin or unpin IPFS operations for a CID in error state.
const cid = 'bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu'
const { cid } = await cluster.recover(cid)
status
Returns the current IPFS state for a given CID.
const cid = 'bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu'
const status = await cluster.status(cid)
for (const [clusterPeerID, pinInfo] of Object.entries(status.peerMap)) {
console.log(`${clusterPeerID}: ${pinInfo.status}`)
// e.g.
// 12D3KooWAjKw14hMUo7wdyEu9KwogrUFCCMiQZApgZ4zMcvtcacj: pinned
// 12D3KooWKiebn7GqPvjqjKARnm47Xoez6f1civBEWxef3u5G6UdM: pinned
// 12D3KooWLKdPdFx5UpPNwoVmMXsLULCDegAqXZ7RAgpKuPSMKoSS: pinned
}
statusAll
Status of all tracked CIDs. Note: this is an expensive operation. Use the optional filters when possible.
const statuses = await cluster.statusAll()
for (const status of statuses) {
console.log(`${status.cid}:`)
for (const [clusterPeerID, pinInfo] of Object.entries(status.peerMap)) {
console.log(` ${clusterPeerID}: ${pinInfo.status}`)
}
}
// e.g.
// bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu:
// 12D3KooWAjKw14hMUo7wdyEu9KwogrUFCCMiQZApgZ4zMcvtcacj: pinned
// 12D3KooWKiebn7GqPvjqjKARnm47Xoez6f1civBEWxef3u5G6UdM: pinned
// 12D3KooWLKdPdFx5UpPNwoVmMXsLULCDegAqXZ7RAgpKuPSMKoSS: pinned
// bafkreibme22gw2h7y2h7tg2fhqotaqjucnbc24deqo72b6mkl2egezxhvy:
// 12D3KooWAjKw14hMUo7wdyEu9KwogrUFCCMiQZApgZ4zMcvtcacj: queued
// 12D3KooWKiebn7GqPvjqjKARnm47Xoez6f1civBEWxef3u5G6UdM: queued
// 12D3KooWLKdPdFx5UpPNwoVmMXsLULCDegAqXZ7RAgpKuPSMKoSS: queued
// ...etc.
Note: The method takes an options object that allows filtering by status or cid e.g.
// retrieve only pinning and pinned items
await cluster.statusAll({ filter: ['pinning', 'pinned'] })
// retrieve status for the passed list of CIDs (requires Cluster version >= 0.14.5-rc1)
await cluster.statusAll({
cids: ['bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu']
})
unpin
Untracks a CID from cluster.
const cid = 'bafybeigpsl667todjswabhelaxvwmk7amgg3txsv5tkcpbpj5rtrf6g7mu'
await cluster.unpin(cid)
Feel free to dive in! Open an issue or submit PRs.
APACHE-2.0 AND MIT
FAQs
A zero-dependency client to the IPFS Cluster HTTP API, built for the browser.
The npm package @nftstorage/ipfs-cluster receives a total of 61 weekly downloads. As such, @nftstorage/ipfs-cluster popularity was classified as not popular.
We found that @nftstorage/ipfs-cluster demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.