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

promise-socket

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-socket

Return promise for socket stream

  • 7.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.4K
increased by5.42%
Maintainers
1
Weekly downloads
 
Created
Source

promise-socket

Build Status Coverage Status npm

This module allows to convert net.Socket stream into its promisified version, which returns Promise object fulfilled when stream"s events occurred.

Requirements

This module requires Node >= 6.

Installation

npm install promise-socket

Usage

const {PromiseSocket, TimeoutError} = require("promise-socket")

Typescript:

import {PromiseSocket, TimeoutError} from "promise-socket"
// or
import PromiseSocket from "promise-socket"

constructor

const promiseSocket = new PromiseSocket(socket)

PromiseSocket object requires socket object to work. New net.Socket object is created if socket argument is missing.

Example:

const net = require("net")
const {PromiseSocket} = require("promise-socket")

const socket = new net.Socket()

const promiseSocket = new PromiseSocket(socket)

Typescript:

import net from "net"
import PromiseSocket from "promise-socket"

const socket = new net.Socket()

const promiseSocket = new PromiseSocket(socket)

stream

const socket = promiseSocket.stream

Original socket object.

Example:

console.log(promiseSocket.stream.localAddress)

connect

await connect(port, host)
await connect(path)
await connect(options)

Initiate a connection on a given socket. Promise if fulfilled when connect event is emitted. Check socket.connect for arguments.

Example:

await connect(80, "localhost")
// or
await connect({port: 80, host: "localhost"})

setTimeout

socket.setTimeout(ms)

Set the timeout for idle socket and after this timeout the socket will be destroyed with a TimeoutError. It means that socket methods (connect, read, write, etc.) will be rejected.

Example:

socket.setTimeout(1000)
await socket.readAll()

read

const chunk = await promiseSocket.read(chunkSize)

Check PromiseReadable.read for details.

readAll

const content = await promiseSocket.readAll()

Check PromiseReadable.readAll for details.

iterate

for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}

Check PromiseReadable.iterate for details.

Symbol.asyncIterator

for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}

Check PromiseReadable[Symbol.asyncIterator] for details.

write

await promiseSocket.write(chunk)

Check PromiseWritable.write for details.

writeAll

await promiseSocket.writeAll(content, chunkSize)

Check PromiseWritable.writeAll for details.

end

await promiseSocket.end()

Check PromiseWritable.once for details.

once

const result = await promiseSocket.once(event)

Check PromiseReadable.once and PromiseWritable.once for details.

destroy

promiseSocket.destroy()

This method calls destroy method on stream and cleans up all own handlers.

TimeoutError

socket.setTimeout(5000)
try {
  socket.connect({port, host})
} catch (e) {
  if (e instanceof TimeoutError) {
    console.error("Socket timeout")
  }
}

This is an error class that is used when timeout occured after using setTimeout method.

See also

PromiseReadable, PromiseWritable, PromiseDuplex, PromisePiping.

License

Copyright (c) 2017-2019 Piotr Roszatycki mailto:piotr.roszatycki@gmail.com

MIT

Keywords

FAQs

Package last updated on 11 Aug 2020

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