This project provides a JavaScript SDK for interacting with a libp2p-based network. It includes an HTTP-like client for sending requests over the libp2p protocol.
- Manage libp2p connections.
- Perform HTTP-like requests to peers using the
Libp2pHttpClient
. - Cache HTTP clients to avoid redundant connections.
- Automatically handle stream closures and cache cleanup.
Install the required dependencies using npm or yarn:
npm install
# or
yarn install
To start using the SDK, first create a P2PClient
instance:
import { P2PClient } from "./src/http";
async function main() {
const p2pClient = await P2PClient.create();
console.log("P2P Client initialized");
}
main();
Use the getHttpClient
method to create or retrieve a cached Libp2pHttpClient
for a specific peer, app ID, and port:
async function main() {
const p2pClient = await P2PClient.create();
const peerId = "/ip4/127.0.0.1/tcp/4001/p2p/QmPeerId";
const appId = "1";
const port = 80;
const httpClient = await p2pClient.getHttpClient(peerId, appId, port);
console.log("HTTP Client connected");
}
main();
Once you have a Libp2pHttpClient
instance, you can send HTTP-like requests using the fetch
method:
async function main() {
const p2pClient = await P2PClient.create();
const peerId = "/ip4/127.0.0.1/tcp/4001/p2p/QmPeerId";
const appId = "1";
const port = 80;
const httpClient = await p2pClient.getHttpClient(peerId, appId, port);
const response = await httpClient.fetch("/", {
method: "GET",
headers: {
"Custom-Header": "ExampleValue",
},
});
console.log("Response status:", response.status);
console.log("Response body:", await response.text());
}
main();
The SDK automatically removes cached HTTP clients when their streams are closed. You don't need to manually manage the cache.
To run tests, use the following command:
npm test
To lint the code, use:
npm run lint
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.