Skip to content

Driver

Defined in: src/driver/driver.ts:148

Backend abstraction: turns the composed frame into something a device shows (ANSI for a terminal, a cell grid for the canvas) and emits normalized input events. Subclass to target a new backend — see BunDriver, WebDriver, MockDriver.

  • EventEmitter

new Driver(options?): Driver

Defined in: node_modules/@types/node/events.d.ts:54

EventEmitterOptions

Driver

abstract readonly capabilities: TerminalCapabilities

Defined in: src/driver/driver.ts:204

What this backend supports (see TerminalCapabilities).


capabilitiesResolved: boolean = false

Defined in: src/driver/driver.ts:195

True once capability probing has completed.


abstract readonly clipboard: Clipboard

Defined in: src/driver/driver.ts:206

Clipboard access for this backend.


readonly consumesFrameBytes: boolean = true

Defined in: src/driver/driver.ts:202

Whether this backend consumes the ANSI frame bytes from writeFrame. Terminals do; the web canvas re-presents the cell grid via presentBuffer and ignores the bytes, so the App skips building the (discarded) ANSI diff for it and only detects whether the frame changed.

get enforcesRuntimeHoverMode(): boolean

Defined in: src/driver/driver.ts:237

Whether passive hover move suppression should be enforced by the app.

boolean

optional [captureRejectionSymbol](error, event, …args): void

Defined in: node_modules/@types/node/events.d.ts:87

The Symbol.for('nodejs.rejection') method is called in case a promise rejection happens when emitting an event and captureRejections is enabled on the emitter. It is possible to use events.captureRejectionSymbol in place of Symbol.for('nodejs.rejection').

import { EventEmitter, captureRejectionSymbol } from 'node:events';
class MyClass extends EventEmitter {
constructor() {
super({ captureRejections: true });
}
[captureRejectionSymbol](err, event, ...args) {
console.log('rejection happened for', event, 'with', err, ...args);
this.destroy(err);
}
destroy(err) {
// Tear the resource down here.
}
}

Error

string | symbol

any[]

void

v13.4.0, v12.16.0


addListener<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:92

Alias for emitter.on(eventName, listener).

E extends string | symbol

string | symbol

(…args) => void

this

v0.1.26


clearScreen(): void

Defined in: src/driver/driver.ts:259

Clear the whole screen and scrollback.

void


emit(event, size): boolean

Defined in: src/driver/driver.ts:166

Emit a resize (drivers call this).

"resize"

Size

boolean

emit(event, ev): boolean

Defined in: src/driver/driver.ts:168

Emit a key event.

"key"

KeyEvent

boolean

emit(event, ev): boolean

Defined in: src/driver/driver.ts:170

Emit a mouse event.

"mouse"

MouseEvent

boolean

emit(event, text): boolean

Defined in: src/driver/driver.ts:172

Emit pasted text.

"paste"

string

boolean

emit(event): boolean

Defined in: src/driver/driver.ts:174

Emit the capabilities-resolved signal.

"capabilities_resolved"

boolean

emit(event, signal): boolean

Defined in: src/driver/driver.ts:176

Emit a received OS signal (drivers call this when exitOnSignal is false).

"signal"

"SIGINT" | "SIGTERM"

boolean


eventNames(): (string | symbol)[]

Defined in: node_modules/@types/node/events.d.ts:154

Returns an array listing the events for which the emitter has registered listeners.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]

(string | symbol)[]

v6.0.0


getGraphicClearSequence(_bgColor?): string

Defined in: src/driver/driver.ts:269

Sequence emitted before redrawing a cell whose graphic/icon changed or was removed, to erase the stale image. bgColor is the cell’s new background, used by protocols (e.g. sixel) that clear by painting an opaque rectangle. Returns "" for backends/protocols where no explicit clear is needed. Keeps graphics-protocol specifics out of the render/app layers.

string

string


getGraphicResetSequence(): string

Defined in: src/driver/driver.ts:279

Sequence that deletes all inline graphics from the terminal at once. Used on a full redraw after a transition (resize, screen change, invalidated frame) to wipe any placements that were orphaned — e.g. a Kitty image that scrolled or whose owning screen was replaced — before the frame re-places the current graphics. Returns "" where not applicable.

string


getIconSequence(name, _color?, _bgColor?): string

Defined in: src/driver/driver.ts:241

Escape sequence drawing a registered icon by name (text fallback by default; protocol drivers override).

string

string

string

string


getImageSequence(_pixelBuffer, _pixelWidth, _pixelHeight, _cellWidth, _cellHeight, _pngBase64?, _bgColor?, _zIndex?): string

Defined in: src/driver/driver.ts:246

Escape sequence drawing an inline image (empty by default; graphics-protocol drivers override).

Uint8Array

number

number

number

number

string

string

number

string


getMaxListeners(): number

Defined in: node_modules/@types/node/events.d.ts:161

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to events.defaultMaxListeners.

number

v1.0.0


getScreenClearSequence(): string

Defined in: src/driver/driver.ts:289

The screen-blanking sequence (SGR reset + cursor home + erase display) as a string, so the App can prepend it to a frame and write both atomically. Pairs with getGraphicResetSequence on a post-graphics-change full wipe. The leading SGR reset makes the erase clear to the default background. Returns "" on backends that don’t consume ANSI (e.g. the web canvas).

string


abstract getSize(): Size

Defined in: src/driver/driver.ts:214

Current viewport size in cells.

Size


listenerCount<E>(eventName, listener?): number

Defined in: node_modules/@types/node/events.d.ts:170

Returns the number of listeners listening for the event named eventName. If listener is provided, it will return how many times the listener is found in the list of the listeners of the event.

E extends string | symbol

string | symbol

The name of the event being listened for

(…args) => void

The event handler function

number

v3.2.0


listeners<E>(eventName): (…args) => void[]

Defined in: node_modules/@types/node/events.d.ts:186

Returns a copy of the array of listeners for the event named eventName.

server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]

E extends string | symbol

string | symbol

(…args) => void[]

v0.1.26


off<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:191

Alias for emitter.removeListener().

E extends string | symbol

string | symbol

(…args) => void

this

v10.0.0


on(event, listener): this

Defined in: src/driver/driver.ts:150

Subscribe to viewport resize.

"resize"

(size) => void

this

on(event, listener): this

Defined in: src/driver/driver.ts:152

Subscribe to key events.

"key"

(ev) => void

this

on(event, listener): this

Defined in: src/driver/driver.ts:154

Subscribe to mouse events.

"mouse"

(ev) => void

this

on(event, listener): this

Defined in: src/driver/driver.ts:156

Subscribe to bracketed-paste text.

"paste"

(text) => void

this

on(event, listener): this

Defined in: src/driver/driver.ts:158

Fires once capability probing completes.

"capabilities_resolved"

() => void

this

on(event, listener): this

Defined in: src/driver/driver.ts:164

Subscribe to a received OS signal (SIGINT/SIGTERM). Only emitted by drivers constructed with exitOnSignal: false; otherwise the driver restores the terminal and exits before any listener could run.

"signal"

(signal) => void

this


once<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:256

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a

E extends string | symbol

string | symbol

The name of the event.

(…args) => void

The callback function

this

v0.3.0


prependListener<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:275

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

server.prependListener('connection', (stream) => {
console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

E extends string | symbol

string | symbol

The name of the event.

(…args) => void

The callback function

this

v6.0.0


prependOnceListener<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:292

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

server.prependOnceListener('connection', (stream) => {
console.log('Ah, we have our first user!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

E extends string | symbol

string | symbol

The name of the event.

(…args) => void

The callback function

this

v6.0.0


presentBuffer(_buffer): void

Defined in: src/driver/driver.ts:298

Hand the composed cell grid to the backend after each changed frame. The portable alternative to consuming the ANSI diff: non-terminal backends (web DOM/canvas) override this and may ignore write/writeFrame entirely. The buffer is the live frame — consume it synchronously or copy.

ScreenBuffer

void


rawListeners<E>(eventName): (…args) => void[]

Defined in: node_modules/@types/node/events.d.ts:326

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));
// Returns a new Array with a function `onceWrapper` which has a property
// `listener` which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];
// Logs "log once" to the console and does not unbind the `once` event
logFnWrapper.listener();
// Logs "log once" to the console and removes the listener
logFnWrapper();
emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by `.on()` above
const newListeners = emitter.rawListeners('log');
// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');

E extends string | symbol

string | symbol

(…args) => void[]

v9.4.0


removeAllListeners<E>(eventName?): this

Defined in: node_modules/@types/node/events.d.ts:338

Removes all listeners, or those of the specified eventName.

It is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other component or module (e.g. sockets or file streams).

Returns a reference to the EventEmitter, so that calls can be chained.

E extends string | symbol

string | symbol

this

v0.1.26


removeListener<E>(eventName, listener): this

Defined in: node_modules/@types/node/events.d.ts:425

Removes the specified listener from the listener array for the event named eventName.

const callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

import { EventEmitter } from 'node:events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
const callbackA = () => {
console.log('A');
myEmitter.removeListener('event', callbackB);
};
const callbackB = () => {
console.log('B');
};
myEmitter.on('event', callbackA);
myEmitter.on('event', callbackB);
// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
// A
// B
// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
// A

Because listeners are managed using an internal array, calling this will change the position indexes of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

import { EventEmitter } from 'node:events';
const ee = new EventEmitter();
function pong() {
console.log('pong');
}
ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);
ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

E extends string | symbol

string | symbol

(…args) => void

this

v0.1.26


setMaxListeners(n): this

Defined in: node_modules/@types/node/events.d.ts:436

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

Returns a reference to the EventEmitter, so that calls can be chained.

number

this

v0.3.5


setMouseHover(_enabled): void

Defined in: src/driver/driver.ts:218

Enable/disable passive hover (any-motion) reporting at runtime.

boolean

void


setPointerShape(shape): void

Defined in: src/driver/driver.ts:229

Set the mouse-pointer shape via OSC 22 (ESC ] 22 ; <name> ST). Pass null to reset to the terminal default (ESC ] 22 ; ST). A no-op when the backend lacks TerminalCapabilities.pointerShapes or when the shape is already active. Unknown names are coerced to the default so a stray value can never wedge the cursor on an unsupported shape. Backends that don’t speak ANSI (e.g. the web canvas) override this.

"default" | "move" | "pointer" | "text" | "vertical-text" | "wait" | "progress" | "help" | "crosshair" | "cell" | "grab" | "grabbing" | "alias" | "copy" | "no-drop" | "not-allowed" | "zoom-in" | "zoom-out" | "n-resize" | "e-resize" | "s-resize" | "w-resize" | "ne-resize" | "nw-resize" | "se-resize" | "sw-resize" | "ew-resize" | "ns-resize" | "nesw-resize" | "nwse-resize" | null

void


abstract showNotification(title, body): void

Defined in: src/driver/driver.ts:216

Show a desktop notification, where supported.

string

string

void


abstract start(): void

Defined in: src/driver/driver.ts:208

Begin: set up the device, enter raw mode / bind listeners, probe capabilities.

void


abstract stop(): void

Defined in: src/driver/driver.ts:210

Tear down: restore the device and release listeners.

void


abstract write(data): void

Defined in: src/driver/driver.ts:212

Write raw output to the device (ANSI for a terminal).

string

void


writeFrame(data): void

Defined in: src/driver/driver.ts:300

Write a full frame, wrapping it in synchronized-update markers when supported.

string

void