Socket
Socket
Sign inDemoInstall

defer-to-connect

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

defer-to-connect

The safe way to handle the `connect` socket event

    2.0.1latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
12,647,614
increased by3.24%

Weekly downloads

Package description

What is defer-to-connect?

The defer-to-connect npm package allows you to defer actions until a socket connection is established. It provides hooks for different stages of a connection, such as 'lookup', 'connect', 'secureConnect', and 'close'. This can be useful for debugging, logging, or modifying the socket during its lifecycle.

What are defer-to-connect's main functionalities?

Defer actions until the socket connects

This feature allows you to execute code when the socket connects. In the provided code sample, a message is logged to the console once the socket connection is established.

const net = require('net');
const deferToConnect = require('defer-to-connect');

const socket = net.createConnection({ port: 80, host: 'example.com' });
deferToConnect(socket, (event) => {
  if (event === 'connect') {
    console.log('Socket connected!');
  }
});

Defer actions until the socket is secured

This feature allows you to execute code when a TLS socket is secured. In the provided code sample, a message is logged to the console once the TLS socket connection is secured.

const tls = require('tls');
const deferToConnect = require('defer-to-connect');

const socket = tls.connect({ port: 443, host: 'example.com' });
deferToConnect(socket, (event) => {
  if (event === 'secureConnect') {
    console.log('Socket secured!');
  }
});

Defer actions until the socket closes

This feature allows you to execute code when the socket closes. In the provided code sample, a message is logged to the console once the socket connection is closed.

const net = require('net');
const deferToConnect = require('defer-to-connect');

const socket = net.createConnection({ port: 80, host: 'example.com' });
deferToConnect(socket, (event) => {
  if (event === 'close') {
    console.log('Socket closed!');
  }
});

Other packages similar to defer-to-connect

Readme

Source

defer-to-connect

The safe way to handle the connect socket event

Coverage Status

Once you receive the socket, it may be already connected (or disconnected).
To avoid checking that, use defer-to-connect. It'll do that for you.

Usage

const deferToConnect = require('defer-to-connect');

deferToConnect(socket, () => {
    console.log('Connected!');
});

API

deferToConnect(socket, connectListener)

Calls connectListener() when connected.

deferToConnect(socket, listeners)

listeners

An object representing connect, secureConnect and close properties.

Calls connect() when the socket is connected.
Calls secureConnect() when the socket is securely connected.
Calls close() when the socket is destroyed.

License

MIT

Keywords

FAQs

Last updated on 24 Feb 2021

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc