Skip to content
velt/io exports

velt/io

API reference

The error type the I/O modules share: files (velt/fs), sockets (velt/net), child processes and WebSockets all throw IoError. The module also holds the streams (ReadableStream and its reader and controller), which are globals and need no import. For a walkthrough, read the guide.

import { IoError } from "velt/io";

Exports

Re-exported from velt/io/error

class IoError extends Error

A failed I/O operation (Error is the prelude base class). code is the Node-style name: "ENOENT", "EACCES", "EEXIST", "EINVAL", "EILSEQ", "ETIMEDOUT", "ECONNREFUSED", "ECONNRESET", "EADDRINUSE", "EPIPE", "EOF", "ENOTSUP", "ENOTDIR", "ENOTEMPTY", "EISDIR", "EBADF" (a closed socket, file stream, child process or WebSocket handle) or "UNKNOWN".

code: stringNo doc comment yet
constructor(code: string, message: string)No doc comment yet
class

ReadableStreamDefaultController

#std/io/streams/controller.vlt:42

Re-exported from velt/io/streams/controller

class ReadableStreamDefaultController<R>

The handle a source gets to its stream: enqueue(chunk), close(), error(e) and desiredSize (how many more chunks the queue wants). Streams make it; JS has no public constructor either.

get desiredSize(): number | null

How many more chunks the queue wants: the high-water mark minus the chunks queued, 0 once closed, null once errored.

enqueue(chunk: R)

Adds chunk to the stream: the oldest waiting read gets it, or it is queued.

close()

Closes the stream: reads get the queued chunks, then done.

error(e: unknown)

Errors the stream with e: queued chunks are dropped and reads reject with e.

[ [INSPECT](): string

How console.log shows the controller, as Node does.

class

ReadableStreamDefaultReader

#std/io/streams/reader.vlt:12

Re-exported from velt/io/streams/reader

class ReadableStreamDefaultReader<R>

Reads a stream it holds (JS ReadableStreamDefaultReader): read() the next chunk, releaseLock() to let go, cancel(reason), and closed.

constructor(stream: ReadableStream<R>)

A reader holding stream; a TypeError when another reader holds it.

read(): Promise<ReadableStreamReadResult<R>, unknown>

The next chunk ({ value, done: false }), or { done: true } once the stream is closed.

releaseLock()

Lets go of the stream: another reader may take it, and waiting reads reject.

cancel(reason: CancelReason = null): Promise<void, unknown>

Cancels the stream (see ReadableStream.cancel).

get closed(): Promise<void, unknown>

Settles when the stream closes (or rejects when it errors); rejects with TypeError: Invalid state: Reader released once the reader let go of the stream.

[ [INSPECT](): string

How console.log shows the reader, as Node does.

Re-exported from velt/io/streams/streams

class ReadableStream<R> extends ByteSource

A stream of chunks (JS ReadableStream): made from a source (new ReadableStream({ start, pull, cancel })) or an async iterable (ReadableStream.from), and read with a reader (getReader()), with for await, or as a Response body.

constructor(source: UnderlyingSource<R> = {}, strategy: QueuingStrategy = {}) throws unknown

A stream reading from source; strategy.highWaterMark is how many chunks it queues ahead of the reads (default 1).

static from<T, E>(src: AsyncIterable<T, E>): ReadableStream<T>

A stream of the values src yields (an async generator, any async iterable).

get locked(): bool

Whether a reader holds the stream (getReader() until releaseLock()).

cancel(reason: CancelReason = null): Promise<void, unknown>

Cancels the stream: queued chunks are dropped, the source's cancel(reason) runs, and reads get done. Rejects with a TypeError while a reader holds the stream (cancel through the reader then).

getReader(): ReadableStreamDefaultReader<R>

A reader that holds the stream; a TypeError when another one does.

[Symbol.asyncIterator](): AsyncIterator<R, Reason>

for await (const chunk of stream): reads to the end; leaving the loop early cancels the stream.

[ [BYTE_CHUNKS](): AsyncIterator<u8[], IoError | AbortError>

The chunks as a body's bytes (std only: new Response(stream), a request body): each one must be a Uint8Array (copied, as a body's bytes are), else the read fails as Node's does.

[ [INSPECT](): string

How console.log shows the stream, as Node does.

interface

QueuingStrategy

#std/io/streams/types.vlt:25

Re-exported from velt/io/streams/types

How many chunks a stream queues before it stops pulling (JS's count queuing strategy).

highWaterMark?: number

The queue length the stream pulls up to (default 1; 0 pulls only for a waiting read).

interface

UnderlyingSource

#std/io/streams/types.vlt:18

Re-exported from velt/io/streams/types

interface UnderlyingSource<R>

What a ReadableStream reads from (JS's underlying source). start(controller) runs when the stream is made, pull(controller) whenever the stream wants more chunks (it enqueues them, or closes the stream), and cancel(reason) when a consumer cancels it. Each may be async, and an error it throws errors the stream.

start?: ((controller: ReadableStreamDefaultController<R>) => void throws unknown) | ((controller: ReadableStreamDefaultController<R>) => Promise<void, unknown>)No doc comment yet
pull?: ((controller: ReadableStreamDefaultController<R>) => void throws unknown) | ((controller: ReadableStreamDefaultController<R>) => Promise<void, unknown>)No doc comment yet
cancel?: ((reason: CancelReason) => void throws unknown) | ((reason: CancelReason) => Promise<void, unknown>)No doc comment yet

Re-exported from velt/io/streams/types

type CancelReason = Error | string | null

Why a stream was cancelled, as given to cancel(reason) (JS takes any value).

type

ReadableStreamReadResult

#std/io/streams/types.vlt:10

Re-exported from velt/io/streams/types

type ReadableStreamReadResult<R> = IteratorResult<R>

One step of reader.read(): { value, done: false }, or { done: true } once closed.