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

@polywrap/ipfs-http-client-lite

v0.3.0

Published

A client library for the IPFS HTTP API

Downloads

3

Readme

An alternative client library for the IPFS HTTP API, aiming to be as lightweight as possible (<20KB) in the browser.

Lead Maintainer

Alan Shaw.

Table of Contents

Install

This module requires Node.js and npm to install:

npm install --save ipfs-http-client-lite

We support both the Current and Active LTS versions of Node.js. Please see nodejs.org for what these currently are.

Usage

Running the daemon with the right port

To interact with the API, you need to have a local daemon running. It needs to be open on the right port. 5001 is the default, and is used in the examples below, but it can be set to whatever you need.

# Show the ipfs config API port to check it is correct
> ipfs config Addresses.API
/ip4/127.0.0.1/tcp/5001
# Set it if it does not match the above output
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
# Restart the daemon after changing the config

# Run the daemon
> ipfs daemon

CORS

In a web browser IPFS HTTP client (either browserified or CDN-based) might encounter an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure: IPFS servers are designed to reject requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your ipfs config like this:

$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin  '["http://example.com"]'
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST", "GET"]'

Custom HTTP Headers

If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:

const ipfs = IpfsHttpClientLite({
  apiUrl: 'http://localhost:5001',
  headers: {
    Authorization: 'Bearer ' + TOKEN
  }
})

Importing

const IpfsHttpClientLite = require('ipfs-http-client-lite')

// Connect to ipfs daemon HTTP API server
const ipfs = IpfsHttpClientLite('http://localhost:5001')
// Note: leaving out the argument will default to this value

For ultra small bundle size, import just the methods you need. e.g.

const cat = require('ipfs-http-client-lite/src/cat')('http://localhost:5001')
const data = await cat('QmQeEyDPA47GqnduyVVWNdnj6UBPXYPVWogAQoqmAcLx6y')

In a web browser

Bundling

This module can be bundled with webpack and browserify and should be compatible with most other bundlers.

CDN

Instead of a local installation (and bundling) you may request a remote copy of the IPFS HTTP API client from unpkg CDN.

To always request the latest version, use the following:

<script src="https://unpkg.com/ipfs-http-client-lite/dist/index.min.js"></script>

You can also use the un-minified version, just remove ".min" from the URL.

For maximum security you may also decide to:

  • Reference a specific version of the IPFS HTTP API client (to prevent unexpected breaking changes when a newer latest version is published)
  • Generate a SRI hash of that version and use it to ensure integrity
  • Set the CORS settings attribute to make anonymous requests to CDN

Example:

<script src="https://unpkg.com/[email protected]/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>

The CDN-based IPFS HTTP API provides the IpfsHttpClientLite constructor as a property of the global window object. Example:

const ipfs = window.IpfsHttpClientLite('http://localhost:5001')

If you omit the URL, the client will parse window.location.host, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:

const ipfs = window.IpfsHttpClientLite()

API

This module is in heavy development, not all API methods are available (or documented) yet!

Note: All API methods are documented using Promises/async/await but they also accept a callback as their last parameter.

Testing

We run tests by executing npm test in a terminal window. This will run both Node.js and Browser tests, both in Chrome and PhantomJS. To ensure that the module conforms with the interface-ipfs-core spec, we run the batch of tests provided by the interface module, which can be found here.

Contribute

Feel free to dive in! Open an issue or submit PRs.

Want to hack on IPFS?

License

Dual-licensed under Apache 2.0 and MIT terms:

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)