Skip to content
VaelJS Blog

vael:stdio

Bounded synchronous byte readers, writers, and standard output streams.

Available in the vjs (vaeljs) runtime only; absent in vjs-lite. Provides memory-backed Readable / Writable byte streams bounded by MAX_BYTES (8 MiB) and synchronous host stdout / stderr writers.

import ... from "vael:stdio"
Since v0.0.0-alpha.0 vjs (V8)
Cargo feature: Always enabled in vaeljs-core (absent in vaeljs-lite-core)
import { Readable, Writable, stdout } from "vael:stdio";
const reader = new Readable(new Uint8Array([104, 105]));
const out = new Uint8Array(2);
reader.readSync(out);
reader.close();
const writer = new Writable();
writer.writeSync("hello\n");
writer.end();
stdout.writeSync(writer.toUint8Array());
writer.close();
classSince v0.0.0-alpha.0vjs only
export class Readable {
  constructor(bytes: Uint8Array);
  readonly remaining: number;
  readonly closed: boolean;
  readSync(target: Uint8Array): number;
  close(): void;
}

Synchronous reader over an owned snapshot copy of a Uint8Array.

Constructor
constructor(bytes: Uint8Array);

Copy bytes (attached view, at most MAX_BYTES) into reader-owned storage.

Parameters
NameTypeDescription
bytesUint8ArrayAttached source byte view (at most MAX_BYTES).
Exceptions
  • TypeError — when bytes is not a Uint8Array or arity is wrong.
  • Error — (name = "StdioError") with code "VAEL_STDIO_DETACHED" or "VAEL_STDIO_LIMIT".
Properties
PropertyTypeDescriptionSince
remaining readonlynumberRemaining unread bytes (0 at EOF and after close()).v0.0.0-alpha.0
closed readonlybooleanWhether close() has been called.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
readSync(target: Uint8Array): number;

Copy up to target.byteLength remaining bytes into target, returning the number of bytes copied (0 at EOF).

Parameters
NameTypeDescription
targetUint8ArrayAttached, non-empty Uint8Array.
Returns
number — Number of bytes copied into target (0 at EOF).
Exceptions
  • TypeError — when target is not a Uint8Array or arity is wrong.
  • Error — (name = "StdioError") with code "VAEL_STDIO_CLOSED", "VAEL_STDIO_DETACHED", or "VAEL_STDIO_ARGUMENT" (empty target).
import { Readable } from "vael:stdio";
const r = new Readable(new Uint8Array([1, 2, 3]));
const buf = new Uint8Array(2);
console.log(r.readSync(buf), r.remaining); // 2 1
r.close();
methodSince v0.0.0-alpha.0
close(): void;

Release the owned byte snapshot. Idempotent.

Returns
void
classSince v0.0.0-alpha.0vjs only
export class Writable {
  constructor();
  readonly kind: "memory" | "stdout" | "stderr";
  readonly length: number;
  readonly closed: boolean;
  readonly ended: boolean;
  writeSync(chunk: Uint8Array | string): number;
  end(): void;
  toUint8Array(): Uint8Array;
  close(): void;
}

Synchronous byte writer (either a bounded in-memory buffer or a shared host stream stdout/stderr).

Constructor
constructor();

Create an empty memory-backed writer (capacity bounded by MAX_BYTES).

Exceptions
  • TypeError — when any argument is supplied.
Properties
PropertyTypeDescriptionSince
kind readonly"memory" | "stdout" | "stderr"Stream backing kind.v0.0.0-alpha.0
length readonlynumberBytes retained by a memory writer (0 for host streams or after close()).v0.0.0-alpha.0
closed readonlybooleanWhether close() has been called.v0.0.0-alpha.0
ended readonlybooleanWhether end() or close() has been called.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
writeSync(chunk: Uint8Array | string): number;

Synchronously append chunk (or write it to the host stream), returning the number of bytes accepted.

Parameters
NameTypeDescription
chunkUint8Array | stringUint8Array or UTF-8 string (total memory capacity max MAX_BYTES).
Returns
number — Number of bytes written.
Exceptions
  • TypeError — on invalid argument type or arity.
  • Error — (name = "StdioError") with code "VAEL_STDIO_CLOSED", "VAEL_STDIO_ENDED", "VAEL_STDIO_DETACHED", "VAEL_STDIO_LIMIT", or "VAEL_STDIO_WRITE".
import { Writable } from "vael:stdio";
const w = new Writable();
console.log(w.writeSync("hello")); // 5
w.close();
methodSince v0.0.0-alpha.0
end(): void;

Stop accepting writes while keeping memory-backed content readable. Idempotent.

Returns
void
methodSince v0.0.0-alpha.0
toUint8Array(): Uint8Array;

Return a fresh Uint8Array copy of a memory-backed writer’s contents.

Returns
Uint8Array — Fresh Uint8Array copy of accumulated bytes.
Exceptions
  • Error — (name = "StdioError") with code "VAEL_STDIO_CLOSED" or "VAEL_STDIO_NOT_MEMORY" (when called on stdout or stderr).
methodSince v0.0.0-alpha.0
close(): void;

Release memory-backed content and retire the writer. Idempotent. Does not close the underlying OS file descriptor for stdout/stderr.

Returns
void
typeSince v0.0.0-alpha.0vjs only
export type StdioErrorCode =
  | "VAEL_STDIO_DETACHED"
  | "VAEL_STDIO_LIMIT"
  | "VAEL_STDIO_ARGUMENT"
  | "VAEL_STDIO_WRITE"
  | "VAEL_STDIO_CLOSED"
  | "VAEL_STDIO_ENDED"
  | "VAEL_STDIO_NOT_MEMORY";

Error codes reported on StdioError.code (name = "StdioError").

constSince v0.0.0-alpha.0vjs only
export const MAX_BYTES: 8388608;

Maximum byte capacity of one memory-backed stream or single writeSync chunk (8 MiB / 8_388_608 bytes).

constSince v0.0.0-alpha.0vjs only
export const stdout: Writable;

Shared host writer targeting the current runtime’s standard output.

constSince v0.0.0-alpha.0vjs only
export const stderr: Writable;

Shared host writer targeting the current runtime’s standard error.