@libp2p/http-fetch

@libp2p/http-fetch

libp2p.io Discuss codecov CI

Implementation of the WHATWG Fetch API on libp2p streams

About

http implements the WHATWG Fetch api. It can be used as a drop in replacement for the browser's fetch function. It supports http, https, and multiaddr URIs. Use HTTP in p2p networks.

Example

See the examples/ for full examples of how to use the HTTP service.

import { createLibp2p } from 'libp2p'
import { http } from '@libp2p/http-fetch'

const node = await createLibp2p({
// other options ...
services: {
http: http()
}
})

await node.start()

// Make an http request to a libp2p peer
let resp = await node.services.http.fetch('multiaddr:/dns4/localhost/tcp/1234')
// Or a traditional HTTP request
resp = await node.services.http.fetch('multiaddr:/dns4/example.com/tcp/443/tls/http')
// And of course, you can use the fetch API as you normally would
resp = await node.services.http.fetch('https://example.com')

// This gives you the accessibility of the fetch API with the flexibility of using a p2p network.

Install

$ npm i @libp2p/http-fetch

Browser <script> tag

Loading this module through a script tag will make its exports available as Libp2pHttpFetch in the global namespace.

<script src="https://unpkg.com/@libp2p/http-fetch/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.