npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ipfs-gateway-fetch

v1.0.2

Published

This library provides a service worker for a website deployed on IPFS to fetch other IPFS assets. If accessed from IPFS, it will use the current gateway or subdomain where the page is being accessed from as the provider for the requested assets.

Downloads

8

Readme

IPFS Assets Service Worker

This library provides a service worker for a website deployed on IPFS to fetch other IPFS assets. If accessed from IPFS, it will use the current gateway or subdomain where the page is being accessed from as the provider for the requested assets.

Features

  • Get a fetch URL from a CID using a gateway or subdomain.
  • Get a fetch URL for a user's first owned asset in an ERC721 NFT collection.

Use

Install and import

Install the package using

npm install ipfs-gateway-fetch

Import the package into your project using

import { Gateway } from 'ipfs-gateway-fetch'

Create

new Gateway(rootURL, accountAddress?, ethURI?)

create a gateway object

Parameters

| Name | Type | Required | Description | | -------- | -------------------------------------------------------------------- | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | | location | Location or String | Yes | The location where the website is being accessed from. If the app is being hosted on IPFS, it should be document.location or window.location. The gateway will attempt to use the same host when requesting other IPFS content. If the app is not being hosted on IPFS, it should be a URL resolving to a IPFS gateway. (https://ipfs.io for example) | | accountAddress | String | No | The public wallet address of the user accessing the website. Only required if fetching NFT assets. | | ethURI | String or Provider | No | A URL that resolves to a node on a blockchain network or an instance of an EIP-1193 Provider (window.ethereum for example). Only required if fetching NFT assets. | |

Methods

urlFromCid(cid)

Returns a url to an IPFS asset given by cid. If the root location is accessed by a subdomain, then an absolute url using the same subdomain is returned. Otherwise, a relative URL is returned as recommended by the IPFS documentation.

Example: Subdomain

Assume the website is being accessed from http://<CID>.ipfs.localhost:8080 (a subdomain link)

const gateway = new Gateway(document.location)
gateway.urlFromCid('bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq')

Returns http://bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq.ipfs.localhost:8080

Example: Gateway

Assume the website is being accessed from http://localhost:8080/ipfs/<CID> (a gateway link)

const gateway = new Gateway(document.location)
const url = gateway.urlFromCid('bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq')

Returns /ipfs/bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq

Example: Not being accessed from IPFS

Assume the website is not being accessed from IPFS and instead from a dedicated hosting website. An IPFS gateway would then be explicitly required.

const gateway = new Gateway("https://ipfs.io")
const url = gateway.urlFromCid('bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq')

Returns https://ipfs.io/ipfs/bafybeicaubxlzbr4sgc3tfwakfn7ganskxlgxmx25pdrcsojchgs3xpfqq

urlFromJsonEntry(json, key)

Returns a url to the IPFS object in json given by key. Requires accountAddress and ethURI to be given to the constructor.

json must be in the format generated by the IPFS Upload Extension. If the type of the json entry is ownable, then the function will query the blockchain and IPFS for the first asset owned by accountAddress

Example

{
  "src/assets/birds": {
    "baseUri": "ipfs://ba...",
    "deployAddress": "0x...",
    "type": "ownable"
  }
}
import json from './assets.json'
const gateway = new Gateway(document.location, "0x...", window.ethereum)
const url = await gateway.urlFromJsonEntry(json, "src/assets/birds")

Returns a url to the first asset owned by accountAddress in the given smart contract. The url format is the same as returned by the urlFromCid(cid) method.