I/O Operations

The io module provides object-oriented access to UNIX file descriptors.

Functions can be individually imported and directly accessed using the named import syntax:

import { open, O_RDWR } from 'io';

let handle = open('/tmp/test.txt', O_RDWR);
handle.write('Hello World\n');
handle.close();

Alternatively, the module namespace can be imported using a wildcard import statement:

import * as io from 'io';

let handle = io.open('/tmp/test.txt', io.O_RDWR);
handle.write('Hello World\n');
handle.close();

Additionally, the io module namespace may also be imported by invoking the ucode interpreter with the -lio switch.

Classes

io.handle

Represents a handle for interacting with a file descriptor.

Methods

error() → {string}nullable

Query error information.

Returns a string containing a description of the last occurred error or null if there is no error information.

Returns: string
Example
// Trigger an error
io.open('/path/does/not/exist');

// Print error (should yield "No such file or directory")
print(io.error(), "\n");

from(value) → {handle}nullable

Creates an io.handle from various value types.

Creates an io.handle by extracting the file descriptor from the given value. The value can be:

  • An integer file descriptor number
  • An fs.file, fs.proc, or socket resource
  • Any object/array/resource with a fileno() method

Returns an io.handle object.

Returns null if an error occurred or the value cannot be converted.

Parameters:
NameTypeDescription
value*

The value to convert.

Returns: handle
Example
import { open as fsopen } from 'fs';
const fp = fsopen('/tmp/test.txt', 'r');
const handle = io.from(fp);
const data = handle.read(100);

new(fd) → {handle}nullable

Creates an io.handle from a file descriptor number.

Wraps the given file descriptor number in an io.handle object.

Returns an io.handle object.

Returns null if an error occurred.

Parameters:
NameTypeDescription
fdnumber

The file descriptor number.

Returns: handle
Example
// Wrap stdin
const stdin = io.new(0);
const data = stdin.read(100);

open(path, flagsopt, modeopt) → {handle}nullable

Opens a file and returns an io.handle.

Opens the specified file with the given flags and mode, returning an io.handle wrapping the resulting file descriptor.

Returns an io.handle object.

Returns null if an error occurred.

Parameters:
NameTypeDescription
pathstring

The path to the file.

flagsnumber(optional, default: O_RDONLY)

The open flags (O_RDONLY, O_WRONLY, O_RDWR, etc.).

modenumber(optional, default: 0o666)

The file creation mode (used with O_CREAT).

Returns: handle
Example
const handle = io.open('/tmp/test.txt', O_RDWR | O_CREAT, 0o644);
handle.write('Hello World\n');
handle.close();

pipe() → {handle[]}nullable

Creates a pipe.

Creates a unidirectional data channel (pipe) that can be used for inter-process communication. Returns an array containing two io.handle objects: the first is the read end of the pipe, the second is the write end.

Data written to the write end can be read from the read end.

Returns an array [read_handle, write_handle] on success.

Returns null if an error occurred.

Returns: handle[]
Example
const [reader, writer] = io.pipe();
writer.write('Hello from pipe!');
const data = reader.read(100);
print(data, "\n");  // Prints: Hello from pipe!