ps

promise-socket

Return promise for socket stream

Showing:

Popularity

Downloads/wk

3.8K

GitHub Stars

40

Maintenance

Last Commit

1yr ago

Contributors

1

Package

Dependencies

2

Size (min+gzip)

5.6KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

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 piotr.roszatycki@gmail.com

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial