# pull-stream-to-net-socket

> Converts a pull-stream duplex into a net.Socket

Latest version **1.0.0** (published 2017-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install pull-stream-to-net-socket
pnpm add pull-stream-to-net-socket
yarn add pull-stream-to-net-socket
bun add pull-stream-to-net-socket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-10-26 |
| First published | 2017-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Maciej Krüger |
| Maintainers | mkg20001 |
| Keywords | pull-stream, net-socket, stream, convert |

## Links

- npm: https://www.npmjs.com/package/pull-stream-to-net-socket
- Repository: https://github.com/mkg20001/pull-stream-to-net-socket
- Homepage: https://github.com/mkg20001/pull-stream-to-net-socket#readme
- Issues: https://github.com/mkg20001/pull-stream-to-net-socket/issues
- npm.io page: https://npm.io/package/pull-stream-to-net-socket

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^3.1.0
- [pull-stream-to-stream](https://npm.io/package/pull-stream-to-stream.md) ^1.3.4

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-10-26
- 0.0.1 — 2017-10-26

## README

# pull-stream-to-net-socket
[![Build Status](https://travis-ci.org/mkg20001/pull-stream-to-net-socket.svg?branch=master)](https://travis-ci.org/mkg20001/pull-stream-to-net-socket) [![codecov](https://codecov.io/gh/mkg20001/pull-stream-to-net-socket/branch/master/graph/badge.svg)](https://codecov.io/gh/mkg20001/pull-stream-to-net-socket)

Converts a pull-stream duplex into a net.Socket
```js
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.TLSSocket`s or `tls.Server`s) 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
```js
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
```js
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()
  )
})
```

---
_Source: https://npm.io/package/pull-stream-to-net-socket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
