@libp2p/fetch
TypeScript icon, indicating that this package has built-in type declarations

1.0.18 • Public • Published

@libp2p/fetch

libp2p.io Discuss codecov CI

Implementation of the Fetch Protocol

About

An implementation of the Fetch protocol as described here: https://github.com/libp2p/specs/tree/master/fetch

The fetch protocol is a simple protocol for requesting a value corresponding to a key from a peer.

Example

import { createLibp2p } from 'libp2p'
import { fetch } from '@libp2p/fetch'
import { peerIdFromString } from '@libp2p/peer-id'

const libp2p = await createLibp2p({
  services: {
    fetch: fetch()
  }
})

// Given a key (as a string) returns a value (as a Uint8Array), or undefined
// if the key isn't found.
// All keys must be prefixed my the same prefix, which will be used to find
// the appropriate key lookup function.
async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> {
  // app specific callback to lookup key-value pairs.
  return Uint8Array.from([0, 1, 2, 3, 4])
}

// Enable this peer to respond to fetch requests for keys that begin with
// '/my_subsystem_key_prefix/'
libp2p.services.fetch.registerLookupFunction('/my_subsystem_key_prefix/', my_subsystem_key_lookup)

const key = '/my_subsystem_key_prefix/{...}'
const peerDst = peerIdFromString('Qmfoo...')

// Load the value from the remote peer, timing out after 10s
const value = await libp2p.services.fetch.fetch(peerDst, key, {
  signal: AbortSignal.timeout(10_000)
})

Install

$ npm i @libp2p/fetch

Browser <script> tag

Loading this module through a script tag will make it's exports available as Libp2pFetch in the global namespace.

<script src="https://unpkg.com/@libp2p/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.

Readme

Keywords

none

Package Sidebar

Install

npm i @libp2p/fetch

Weekly Downloads

568

Version

1.0.18

License

Apache-2.0 OR MIT

Unpacked Size

102 kB

Total Files

26

Last publish

Collaborators

  • jacobheun
  • npm-service-account-libp2p
  • alanshaw
  • vascosantos
  • achingbrain
  • daviddias