
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
promise-socket
Advanced tools
This module allows to convert
net.Socket
stream into
its promisified version, which returns
Promise
object fulfilled when stream"s events occurred.
This module requires Node >= 6.
npm install promise-socket
const {PromiseSocket, TimeoutError} = require("promise-socket")
Typescript:
import {PromiseSocket, TimeoutError} from "promise-socket"
// or
import PromiseSocket from "promise-socket"
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)
const socket = promiseSocket.stream
Original socket object.
Example:
console.log(promiseSocket.stream.localAddress)
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"})
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()
const chunk = await promiseSocket.read(chunkSize)
Check
PromiseReadable.read
for details.
const content = await promiseSocket.readAll()
Check
PromiseReadable.readAll
for details.
for await (const chunk of promiseDuplex.iterate(chunkSize)) {}
Check
PromiseReadable.iterate
for details.
for await (const chunk of promiseDuplex.iterate(chunkSize)) {}
Check
PromiseReadable[Symbol.asyncIterator]
for details.
await promiseSocket.write(chunk)
Check
PromiseWritable.write
for details.
await promiseSocket.writeAll(content, chunkSize)
Check
PromiseWritable.writeAll
for details.
await promiseSocket.end()
Check
PromiseWritable.once
for details.
const result = await promiseSocket.once(event)
Check
PromiseReadable.once
and
PromiseWritable.once
for details.
promiseSocket.destroy()
This method calls destroy method on stream and cleans up all own handlers.
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.
PromiseReadable
,
PromiseWritable
,
PromiseDuplex
,
PromisePiping
.
Copyright (c) 2017-2019 Piotr Roszatycki mailto:piotr.roszatycki@gmail.com
FAQs
Return promise for socket stream
The npm package promise-socket receives a total of 4,243 weekly downloads. As such, promise-socket popularity was classified as popular.
We found that promise-socket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.