Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@network-stackify/libp2p-net

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@network-stackify/libp2p-net

A port of node's net module for the browser using libp2p

  • 0.0.10
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

@network-stackify/libp2p-net

Drop in replacement for net module

Usage

This module is inspired by go-ipfs experimental p2p streams.

Generally, you will need a go-ipfs node to expose a port through libp2p

ipfs p2p listen /x/<host> /dns4/<host-fqdn>/tcp/80

In nodejs, use like the native net module:

HTTP

const net = require("@network-stackify/libp2p-net");
const http = require("http"); //require('@network-stackify/http')

function readResponse(response) {
  var str = "";
  response.on("data", (chunk) => {
    str += chunk;
  });
  response.on("end", () => {
    console.log(str);
  });
}

const exampleMultiaddr = "/dns4/localhost/tcp/4003/ws/p2p/" + peerId;
const libp2p = await createLibp2pInstance(opts);

//The following options are required but can be omitted when using an agent
// results in EPERM (piping) or TLS errors if not strictly correct
const tlsOptions = {
  path: undefined,
  port: 443,
  servername: "google.com",
};

//go-ipfs: ipfs p2p listen /x/httpGoogle /dns4/google.com/tcp/80
const httpOptions = {
  createConnection: net.createConnection,
  libp2p,
  multiaddr: exampleMultiaddr,
  hops: [],
  proto: "/x/httpGoogle",
};

http.get("http://google.com", httpOptions, readResponse);

HTTPS

const net = require("@network-stackify/libp2p-net");
const https = require("https"); //require('@network-stackify/https')
const tls = require("tls"); //require('@network-stackify/tls')

function readResponse(response) {
  var str = "";
  response.on("data", (chunk) => {
    str += chunk;
  });
  response.on("end", () => {
    console.log(str);
  });
}

const exampleMultiaddr = "/dns4/localhost/tcp/4003/ws/p2p/" + peerId;
const libp2p = await createLibp2pInstance(opts);

//go-ipfs: ipfs p2p listen /x/httpsGoogle /dns4/google.com/tcp/443
const httpsOptions = {
  createConnection: (opts) => {
    const socket = net.createConnection(opts);
    //Override net in tls module
    return tls.connect({ ...opts, ...tlsOptions, socket });
  },
  libp2p,
  multiaddr: exampleMultiaddr,
  hops: [],
  proto: "/x/httpsGoogle",

  //The following options are required but can be omitted when using an agent
  // results in EPERM (piping) or TLS errors if not strictly correct
  path: undefined,
  port: 443,
  servername: "google.com",
};

https.get("https://google.com", httpsOptions, readResponse);

Browser

module.export = {
  resolve: {
    fallback: {
      net: require.resolve("@network-stackify/libp2p-net"),

      //for https using net bundle
      tls: require.resolve("@network-stackify/tls"),
      https: require.resolve("@network-stackify/https"),

      //for http using libp2p-net
      http: require.resolve("@network-stackify/http"),
    },
  },
};
const ws = require("ws");
const libp2p = createInstance();
let client = new ws("wss://endpoint.tld", {
  //Options for libp2p-net
  libp2p,
  multiaddr,
  proto,
  hops,
});

Cross-platform

For easy front-end bundling, network-stackify uses native modules by default. This way, modules can be replaced selectively.

Other network-stackify modules like http, https, tls will require shimming the native net module or overriding defaults by passing opts.connect/createConnection or opts.socket.

Contents

Class: net.Socket

net.connect()

net.createConnection()

net.isIP()

net.isIPv4()

net.isIPv6()

net._normalizeArgs()

FAQs

Package last updated on 13 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc