UNPKG

crosis

Version:
166 lines (165 loc) 5.74 kB
/// <reference types="node" /> /// <reference types="node" /> import _protocol from "@replit/protocol"; import protocol = _protocol.api; import type { CrosisOptions, Adapter } from "./types"; import { Channel } from "./channel.js"; import { EventEmitter } from "events"; declare interface Crosis { on(event: "connect", listener: () => void): this; on(event: "disconnect", listener: () => void): this; on(event: "message", listener: (message: protocol.Command) => void): this; on(event: "messageSent", listener: (message: protocol.Command) => void): this; on(event: "openChannel", listener: (channel: Channel) => void): this; on(event: "closeChannel", listener: (closeChanRes: protocol.CloseChannelRes) => void): this; on(event: "toast", listener: (message: string) => void): this; on(event: string, listener: Function): this; } declare class Crosis extends EventEmitter { private url; private adapter; private ws; private debug; private refHandlers; private channels; private channelsByName; private utilFuncsChannels; private otStatuses; private execUtilResolve; private execUtilReject; private execUtilOutput; bootStatus: protocol.BootStatus.Stage | null; containerState: protocol.ContainerState.State | null; constructor(options: CrosisOptions); /** * Sets the adapter to use. * * Usually you'll want to specify the adapter * when instantiating Crosis, but you can also * set it later on. */ setAdapter(adapter: Adapter | null): void; /** * Sets the Goval URL to use when connecting. * Note that this will be overridden if an * adapter is specified. */ setUrl(url: string | null): void; /** * Returns the WebSocket ready state. */ get wsReadyState(): 0 | 2 | 1 | 3; /** * Connects to the WebSocket server, waits for * the WebSocket to be ready, and sets up all * required event listeners. * * When a connection is successful, the "connect" * event is emitted, which can be listened to * with `crosis.on("connect", () => { ... })`. */ connect(): Promise<void>; /** * Generates a random message ref. */ private generateRef; /** * Encodes the message, sends it, and returns a promise * that resolves to the response message. * * The response message is determined by the ref field. * It will have the same ref as the original sent message. * * If the message does not have a ref, one will be generated, * unless autoRef is set to false. */ send(message: any, autoRef?: boolean, throwErrors?: boolean): Promise<protocol.Command>; /** * Requests opening a channel for a specified service, * with an optional unique channel name. * * Returns a promise that resolves to a Channel object * if the channel was successfully opened. Otherwise, * an error is thrown. */ openChannel(service: string, name?: string, action?: protocol.OpenChannel.Action): Promise<Channel>; /** * Requests closing a channel with the specified ID. * * Returns a promise that resolves to a CloseChannelRes * object, no matter the outcome. */ closeChannel(id: number, action?: protocol.CloseChannel.Action): Promise<_protocol.api.CloseChannelRes>; /** * Disconnects the WebSocket, and closes all * previously opened channels. * * If we're already disconnected, this will return * `false` and do nothing. * * @param autoClose Whether to automatically close * all the channels that were opened * * @returns Whether the WebSocket was disconnected */ disconnect(autoClose?: boolean): Promise<boolean>; /** * Returns the ID of the channel with the * specified name. */ getChannelIdByName(name: string): number; /** * Requests opening a channel for util functions to use. * If a channel for the specified service already exists, * it will be returned instead. */ private startUtil; /** * Reads a file using GCSFiles. */ readFile(path: string): Promise<Uint8Array>; /** * Writes a file using GCSFiles. */ writeFile(path: string, data: string | Buffer | Uint8Array): Promise<_protocol.api.Command>; /** * Stat a file using GCSFiles. */ statFile(path: string): Promise<_protocol.api.StatResult>; /** * Lists files in a directory using GCSFiles. */ readDir(path: string): Promise<_protocol.api.File[]>; /** * Creates a directory using GCSFiles. */ createDir(path: string): Promise<_protocol.api.Command>; /** * Executes a shell command. * * Note that this is blocking, meaning that only * one command can be executed at a time. */ exec(args: string[], env?: Record<string, string>): Promise<unknown>; /** * Gets the edit history of a file. * @param path The path of the file * @param from From which version to start * @param to Until which version to get * @returns An array of OT packets */ getFileHistory(path: string, from?: number, to?: number): Promise<_protocol.api.OTPacket[]>; /** * Gets the latest version of a file * that this client knows about. Meaning * that if the file was edited by another * client, this will not return the latest * version until we've received the OT * packets from the other client. * * Will return null if the client hasn't * received any OT packets for the file. */ getLatestFileVersion(path: string): number | null; } export { Crosis };