@serban.marti/promise-socket
v1.0.0
Published
Return promise for socket stream
Downloads
29
Readme
promise-socket
This is a fork from promise-socket by Piotr Roszatycki!
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 >= 18.
Installation
npm install @serban.marti/promise-socket
Usage
const {PromiseSocket, TimeoutError} = require("@serban.marti/promise-socket")
Typescript:
import {PromiseSocket, TimeoutError} from "@serban.marti/promise-socket"
// or
import PromiseSocket from "@serban.marti/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("@serban.marti/promise-socket")
const socket = new net.Socket()
const promiseSocket = new PromiseSocket(socket)
Typescript:
import net from "net"
import PromiseSocket from "@serban.marti/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
.