Skip to main content

interface Deno.Conn

extends Disposable

A generic stream-oriented network connection that can be read from and written to.

Type Parameters #

#A extends Addr = Addr

Properties #

#localAddr: A
readonly

The local address of the connection.

#remoteAddr: A
readonly

The remote address of the connection.

#readable: ReadableStream<Uint8Array<ArrayBuffer>>
readonly

A ReadableStream of the data received over the connection.

#writable: WritableStream<Uint8Array<ArrayBufferLike>>
readonly

A WritableStream for sending data over the connection.

Methods #

#read(p: Uint8Array): Promise<number | null>

Read the incoming data from the connection into an array buffer (p).

Resolves to either the number of bytes read during the operation or EOF (null) if there was nothing more to read.

It is possible for a read to successfully return with 0 bytes. This does not indicate EOF.

It is not guaranteed that the full buffer will be read in a single call.

// If the text "hello world" is received by the client:
const conn = await Deno.connect({ hostname: "example.com", port: 80 });
const buf = new Uint8Array(100);
const numberOfBytesRead = await conn.read(buf); // 11 bytes
const text = new TextDecoder().decode(buf);  // "hello world"
#write(p: Uint8Array): Promise<number>

Write the contents of the array buffer (p) to the connection.

Resolves to the number of bytes written.

It is not guaranteed that the full buffer will be written in a single call.

const conn = await Deno.connect({ hostname: "example.com", port: 80 });
const encoder = new TextEncoder();
const data = encoder.encode("Hello world");
const bytesWritten = await conn.write(data); // 11
#close(): void

Closes the connection, freeing the resource.

const conn = await Deno.connect({ hostname: "example.com", port: 80 });

// ...

conn.close();
#closeWrite(): Promise<void>

Shuts down (shutdown(2)) the write side of the connection. Most callers should just use close().

#ref(): void

Make the connection block the event loop from finishing.

Note: the connection blocks the event loop from finishing by default. This method is only meaningful after .unref() is called.

#unref(): void

Make the connection not block the event loop from finishing.

Did you find what you needed?

Privacy policy