Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nftstorage.link

Package Overview
Dependencies
Maintainers
5
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nftstorage.link

Utilities for working with the NFT.Storage IPFS Edge Gateway

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-80%
Maintainers
5
Weekly downloads
 
Created
Source

Utilities for working with the NFT.Storage IPFS Edge Gateway.

Install

npm install nftstorage.link

Usage

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)
Examples
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
React Components

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
}
Node.js usage

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 }
)

Contributing

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.

License

Dual-licensed under MIT + Apache 2.0

Keywords

FAQs

Package last updated on 27 Apr 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc