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

pull-stream-to-net-socket

v1.0.0

Published

Converts a pull-stream duplex into a net.Socket

Downloads

9

Readme

pull-stream-to-net-socket

Build Status codecov

Converts a pull-stream duplex into a net.Socket

const toSocket = require('pull-stream-to-net-socket')

Why

Ever tried to use native TLS or any other nodeJS thing that requires net.Sockets or a net.Server with pull-streams?

It's way too complicated...

That's why I created pull-stream-to-net-socket

API

toSocket(duplex[, options, callback])

  • duplex: A pull-stream duplex stream

  • options: An object containing the options

    • createServer: Custom function used instead of net.createServer()
    • createClient: Custom function used instead of net.connect(server.address())

    Both createServer and createClient listen for secureConnect(ion) instead of the connect(ion) event if the return value is a tls.TLSSocket or tls.Server instance

    • prefire: Start sending data from the duplex immediatly instead of waiting for .run() to be called
    • inverse: Reverse the stream-flow direction

    This will make the server socket the primary and the client socket the secondary

  • callback: Function which will be called with (err, primary, secondary)

    • err is the error that occured anywhere in the process
    • primary is the socket that can be used to send data through. By default no data is sent unless primary.run() is called.
    • secondary the socket used to establish a connection. Not intended to be used to sent data with.

Flow

First a server is created using createServer(). That server is then listening on a random port.

A client that connects to the server is created using createClient(server.address()). When the client successfully connects and prefire is enabled the circuit is established (duplex -> client -> duplex OR if inverse is true duplex -> server-side client -> duplex).

After that it is being listened for the connect(ion) event (or secureConnect(ion) for tls.TLSSockets or tls.Servers) on both sides (prefire prevents deadlocks if one side is tls and another plain net as secureConnect wouldn't be called).

If no error occured the callback is called with cb(null, primary, secondary) otherwise cb(err).

Examples

It's very complicated to understand the purpose without examples

TLS client

duplex is a pull-stream connection to a TLS server

toSocket(duplex, {
  createClient: dest => tls.connect(dest), //use tls instead of net
  prefire: true //prevent said deadlock
}, (err, client) => {
  if (err) throw err //possibly some cert error. try setting "rejectUnauthorized: false"
  //client is a tls.TLSSocket instance that can be converted to a pull-stream
  client.run() //call this after async io or just before you convert the stream to a pull-stream
  pull(
    pull.values(["hello world"]),
    toPull.duplex(client),
    pull.log()
  )
})

TLS server

duplex is a pull-stream connection to a TLS client

toSocket(duplex, {
  createServer: () => tls.createServer({ //for this to work you also need a self-signed/x509 cert.
    cert,
    key
  }),
  prefire: true, //prevent said deadlock
  inverse: true //make the server the primary target / "connect to client" mode
}, (err, client) => {
  if (err) throw err //most likely a crypto error
  //client is a tls.TLSSocket instance that can be converted to a pull-stream
  client.run() //call this after async io or just before you convert the stream to a pull-stream
  pull(
    pull.values(["hello world"]),
    toPull.duplex(client),
    pull.log()
  )
})