Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
nftstorage.link
Advanced tools
Utilities for working with the NFT.Storage IPFS Edge Gateway.
npm install nftstorage.link
Import the library in your client application like:
import { getGatewayURL } from 'nftstorage.link'
getGatewayURL
Get a gateway URL, given a CID, CID+path, IPFS path or an IPFS gateway URL. If the status of the nftstorage.link
gateway is known to be good (according to the status checker) then return a URL that uses nftstorage.link
, otherwise return an URL that uses dweb.link
(or the optional passed fallback gateway URL). Status result is cached for 60 seconds by default.
Note: the fallback gateway is not guaranteed to be operational and this library makes no attempt to verify this.
getGatewayURL (cid: string|URL): Promise<string>
// (typical usage - also takes options object as second param)
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
// https://nftstorage.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
// or
// https://dweb.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
Also works with CID + path:
const url = await getGatewayURL(
'bafyreihwsoxxcxfsisghlc22xzc6datssd7n52wonpdgrhu3lwyqqagzye/metadata.json'
)
console.log(url)
// https://nftstorage.link/ipfs/bafyreihwsoxxcxfsisghlc22xzc6datssd7n52wonpdgrhu3lwyqqagzye/metadata.json
// or
// https://dweb.link/ipfs/bafyreihwsoxxcxfsisghlc22xzc6datssd7n52wonpdgrhu3lwyqqagzye/metadata.json
Also works with ipfs://
URL:
const url = await getGatewayURL(
'ipfs://bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
// https://nftstorage.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
// or
// https://dweb.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
Also works with any IPFS gateway URL:
const url = await getGatewayURL(
'https://ipfs.io/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
// https://nftstorage.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
// or
// https://dweb.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
...including subdomain gateway URL:
const url = await getGatewayURL(
'https://bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui.ipfs.dweb.link'
)
console.log(url)
// https://nftstorage.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
// or
// https://dweb.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
You can also specify a different fallback gateway URL:
const fallbackGatewayURL = 'https://ipfs.io' // default is 'https://dweb.link'
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui',
{ fallbackGatewayURL }
)
console.log(url)
// https://nftstorage.link/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
// or
// https://ipfs.io/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui
Example of how this function could be used in a React project:
import { useState, useEffect } from 'react'
import { getGatewayURL } from 'nftstorage.link'
function GatewayLink({ cid, title }) {
const [url, setUrl] = useState('')
useEffect(async () => setUrl(await getGatewayURL(cid)), [cid])
return url ? <a href={url}>{title}</a> : null
}
function GatewayImage({ cid, alt }) {
const [url, setUrl] = useState('')
useEffect(async () => setUrl(await getGatewayURL(cid)), [cid])
return url ? <img src={url} alt={alt} /> : null
}
This library uses the fetch
API. In Node.js there are two options to enable usage:
Assign to global:
import fetch from '@web-std/fetch' // npm install @web-std/fetch
globalThis.fetch = fetch
// use getGatewayURL etc. as usual
Pass to GatewayStatusChecker
:
import { getGatewayURL, GatewayStatusChecker } from 'nftstorage.link'
import fetch from '@web-std/fetch' // npm install @web-std/fetch
const statusChecker = new GatewayStatusChecker({ fetch })
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui',
{ statusChecker }
)
Feel free to join in. All welcome. Open an issue!
If you're opening a pull request, please see the guidelines in DEVELOPMENT.md on structuring your commit messages so that your PR will be compatible with our release process.
Dual-licensed under MIT + Apache 2.0
FAQs
Utilities for working with the NFT.Storage IPFS Edge Gateway
The npm package nftstorage.link receives a total of 1 weekly downloads. As such, nftstorage.link popularity was classified as not popular.
We found that nftstorage.link demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.