Debian Codegraph @

pkg/lib/cockpit.d.ts

raw
1 /*
2 * Copyright (C) 2024 Red Hat, Inc.
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 import '_internal/common'; // side-effecting import (`window` augmentations)
7 import type { Info } from './cockpit/_internal/info';
8
9 declare module 'cockpit' {
10 export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };
11 export type JsonObject = Record<string, JsonValue>;
12
13 export class BasicError {
14 problem: string;
15 message: string;
16 toString(): string;
17 }
18
19 export type SuperuserMode = "require" | "try" | null | undefined;
20
21 export function init(): Promise<void>;
22
23 export function assert(predicate: unknown, message?: string): asserts predicate;
24
25 export const manifests: { [package in string]?: JsonObject };
26 export const info: Info;
27
28 export let language: string;
29 export let language_direction: string;
30
31 export interface Transport {
32 csrf_token: string;
33 origin: string;
34 host: string;
35 options: JsonObject;
36 uri(suffix?: string): string;
37 wait(callback: (transport: Transport) => void): void;
38 close(problem?: string): void;
39 application(): string;
40 control(command: string, options: JsonObject): void;
41 }
42
43 export const transport: Transport;
44
45 /* === jQuery compatible promise ============== */
46
47 export interface DeferredPromise<T> extends Promise<T> {
48 /* jQuery Promise compatibility */
49 done(callback: (data: T) => void): DeferredPromise<T>
50 fail(callback: (exc: Error) => void): DeferredPromise<T>
51 always(callback: () => void): DeferredPromise<T>
52 progress(callback: (message: T, cancel: () => void) => void): DeferredPromise<T>
53 }
54
55 export interface Deferred<T> {
56 resolve(): Deferred<T>;
57 reject(): Deferred<T>;
58 notify(): Deferred<T>;
59 promise: DeferredPromise<T>
60 }
61
62 export function defer<T>(): Deferred<T>;
63
64 /* === Events mix-in ========================= */
65
66 export interface EventMap {
67 // eslint-disable-next-line @typescript-eslint/no-explicit-any
68 [_: string]: (...args: any[]) => void;
69 }
70
71 export type EventListener<E extends (...args: unknown[]) => void> =
72 (event: CustomEvent<Parameters<E>>, ...args: Parameters<E>) => void;
73
74 export interface EventSource<EM extends EventMap> {
75 addEventListener<E extends keyof EM>(event: E, listener: EventListener<EM[E]>): void;
76 removeEventListener<E extends keyof EM>(event: E, listener: EventListener<EM[E]>): void;
77 dispatchEvent<E extends keyof EM>(event: E, ...args: Parameters<EM[E]>): void;
78 }
79
80 export interface CockpitEvents extends EventMap {
81 locationchanged(): void;
82 visibilitychange(): void;
83 }
84
85 export function addEventListener<E extends keyof CockpitEvents>(
86 event: E, listener: EventListener<CockpitEvents[E]>
87 ): void;
88 export function removeEventListener<E extends keyof CockpitEvents>(
89 event: E, listener: EventListener<CockpitEvents[E]>
90 ): void;
91
92 export interface ChangedEvents {
93 changed(): void;
94 }
95
96 export function event_target<T, EM extends EventMap>(obj: T): T & EventSource<EM>;
97
98 /* === Channel =============================== */
99
100 export interface ControlMessage extends JsonObject {
101 command: string;
102 }
103
104 export interface ChannelEvents<T> extends EventMap {
105 control(options: JsonObject): void;
106 ready(options: JsonObject): void;
107 close(options: JsonObject): void;
108 message(data: T): void;
109 }
110
111 export interface Channel<T> extends EventSource<ChannelEvents<T>> {
112 id: string | null;
113 binary: boolean;
114 options: JsonObject;
115 ready: boolean;
116 valid: boolean;
117 send(data: T): void;
118 control(options: ControlMessage): void;
119 wait(callback?: (data: T) => void): Promise<T>;
120 close(options?: string | JsonObject): void;
121 }
122
123 // these apply to all channels
124 export interface ChannelOptions {
125 superuser?: SuperuserMode;
126 [_: string]: JsonValue | undefined;
127 binary?: boolean,
128
129 // for remote channels
130 host?: string;
131 user?: string;
132 password?: string;
133 session?: "shared" | "private";
134 }
135
136 // this applies to opening a generic channel() with explicit payload
137 export interface ChannelOpenOptions extends ChannelOptions {
138 payload: string;
139 }
140
141 export function channel(options: ChannelOpenOptions & { binary?: false; }): Channel<string>;
142 export function channel(options: ChannelOpenOptions & { binary: true; }): Channel<Uint8Array>;
143
144 /* === cockpit.{spawn,script} ============================= */
145
146 export class ProcessError {
147 problem: string | null;
148 exit_status: number | null;
149 exit_signal: number | null;
150 message: string;
151 }
152
153 export interface Spawn<T> extends DeferredPromise<T> {
154 input(message?: T | null, stream?: boolean): Spawn<T>;
155 stream(callback: (data: T) => number | null | void): Spawn<T>;
156 done(callback: (data: T, message?: string) => void): Spawn<T>;
157 fail(callback: (exc: Error, data?: string) => void): Spawn<T>;
158 state(): "pending" | "resolved" | "rejected";
159 close(options?: string | JsonObject): void;
160 }
161
162 export interface SpawnOptions extends ChannelOptions {
163 directory?: string;
164 err?: "out" | "ignore" | "message";
165 environ?: string[];
166 pty?: boolean;
167 window?: { rows: number; cols: number };
168 }
169
170 export function spawn(
171 args: string[],
172 options?: SpawnOptions & { binary?: false }
173 ): Spawn<string>;
174 export function spawn(
175 args: string[],
176 options: SpawnOptions & { binary: true }
177 ): Spawn<Uint8Array>;
178
179 export function script(
180 script: string,
181 options?: SpawnOptions & { binary?: false }
182 ): Spawn<string>;
183 export function script(
184 script: string,
185 options: SpawnOptions & { binary: true }
186 ): Spawn<Uint8Array>;
187 export function script(
188 script: string,
189 args: string[],
190 options?: SpawnOptions & { binary?: false }
191 ): Spawn<string>;
192 export function script(
193 script: string,
194 args: string[],
195 options: SpawnOptions & { binary: true }
196 ): Spawn<Uint8Array>;
197
198 /* === cockpit.location ========================== */
199
200 export interface Location {
201 url_root: string;
202 options: { [name: string]: string | Array<string> };
203 path: Array<string>;
204 href: string;
205 go(path: Location | string[] | string, options?: { [key: string]: string }): void;
206 replace(path: Location | string[] | string, options?: { [key: string]: string }): void;
207
208 encode(path: string[], options?: { [key: string]: string }, with_root?: boolean): string;
209 decode(string: string, options?: { [key: string]: string }): string[];
210 }
211
212 export let location: Location;
213
214 /* === cockpit.jump ========================== */
215
216 export function jump(path: string | string[], host?: string): void;
217
218 /* === cockpit page visibility =============== */
219
220 export let hidden: boolean;
221
222 /* === cockpit.dbus ========================== */
223
224 export interface DBusProxyEvents extends EventMap {
225 changed(changes: { [property: string]: unknown }): void;
226 }
227
228 export interface DBusProxiesEvents extends EventMap {
229 added(proxy: DBusProxy): void;
230 changed(proxy: DBusProxy): void;
231 removed(proxy: DBusProxy): void;
232 }
233
234 export interface DBusProxy extends EventSource<DBusProxyEvents> {
235 valid: boolean;
236 [property: string]: unknown;
237 }
238
239 export interface DBusClientEvents extends EventMap {
240 notify(data: unknown): void;
241 meta(data: unknown): void;
242 owner(owner: string): void;
243 }
244
245 export interface DBusOptions {
246 bus?: string;
247 address?: string;
248 host?: string;
249 superuser?: SuperuserMode;
250 track?: boolean;
251 }
252
253 export type DBusCallOptions = {
254 flags?: "" | "i",
255 type?: string,
256 timeout?: number,
257 };
258
259 export interface DBusProxies extends EventSource<DBusProxiesEvents> {
260 client: DBusClient;
261 iface: string;
262 path_namespace: string;
263 wait(callback?: () => void): Promise<void>;
264 }
265
266 export interface DBusClient extends EventSource<DBusClientEvents> {
267 readonly unique_name: string;
268 readonly options: DBusOptions;
269 proxy(interface?: string, path?: string, options?: { watch?: boolean }): DBusProxy;
270 proxies(interface?: string, path_namespace?: string, options?: { watch?: boolean }): DBusProxies;
271 call(path: string, iface: string, method: string, args?: unknown[] | null, options?: DBusCallOptions): Promise<unknown[]>;
272 watch(path: string): DeferredPromise<void>,
273 subscribe: (
274 match: {
275 path?: string,
276 path_namespace?: string,
277 interface?: string,
278 member?: string,
279 arg0?: string
280 },
281 callback: (path: string, iface: string, signal: string, args: unknown[]) => void,
282 rule?: boolean,
283 ) => {
284 remove: () => void;
285 },
286 close(): void;
287 }
288
289 export type VariantType = string | Uint8Array | number | boolean | VariantType[];
290 export interface Variant {
291 t: string;
292 v: VariantType;
293 }
294
295 export function dbus(name: string | null, options?: DBusOptions): DBusClient;
296
297 export function variant(type: string, value: VariantType): Variant;
298 export function byte_array(string: string): string;
299
300 /* === cockpit.file ========================== */
301
302 export interface FileSyntaxObject<T, B> {
303 parse(content: B): T;
304 stringify?(content: T): B;
305 }
306
307 export type FileTag = string;
308
309 export type FileWatchCallback<T> = (data: T | null, tag: FileTag | null, error: BasicError | null) => void;
310 export interface FileWatchHandle {
311 remove(): void;
312 }
313
314 export interface FileHandle<T> {
315 // BUG: This should be Promise<T, FileTag>, but this isn't representable (it's a cockpit.defer underneath)
316 read(): Promise<T>;
317 replace(new_content: T | null, expected_tag?: FileTag): Promise<FileTag>;
318 watch(callback: FileWatchCallback<T>, options?: { read?: boolean }): FileWatchHandle;
319 // BUG: same as read
320 modify(callback: (data: T | null) => T | null, initial_content?: string, initial_tag?: FileTag): Promise<T>;
321 close(): void;
322 path: string;
323 }
324
325 export type FileOpenOptions = {
326 max_read_size?: number;
327 superuser?: SuperuserMode;
328 };
329
330 export function file(
331 path: string,
332 options?: FileOpenOptions & { binary?: false; syntax?: never; }
333 ): FileHandle<string>;
334 export function file(
335 path: string,
336 options: FileOpenOptions & { binary: true; syntax?: never; }
337 ): FileHandle<Uint8Array>;
338 export function file<T>(
339 path: string,
340 options: FileOpenOptions & { binary?: false; syntax: FileSyntaxObject<T, string>; }
341 ): FileHandle<T>;
342 export function file<T>(
343 path: string,
344 options: FileOpenOptions & { binary: true; syntax: FileSyntaxObject<T, Uint8Array>; }
345 ): FileHandle<T>;
346
347 /* === cockpit.user ========================== */
348
349 export type UserInfo = {
350 id: number;
351 gid: number;
352 name: string;
353 full_name: string;
354 groups: Array<string>;
355 home: string;
356 shell: string;
357 };
358 /** @deprecated */ export function user(): Promise<Readonly<UserInfo>>;
359
360 /* === cockpit.http ====================== */
361
362 export interface TlsCert {
363 file?: string;
364 data?: string;
365 }
366
367 export interface HttpOptions {
368 // target address; if omitted, the endpoint string must include the host
369 address?: string;
370 port?: number;
371 tls?: {
372 authority?: TlsCert;
373 certificate?: TlsCert;
374 key?: TlsCert;
375 validate?: boolean;
376 };
377 superuser?: SuperuserMode;
378 binary?: boolean;
379 // default HTTP headers to send with every request
380 headers?: HttpHeaders;
381 // Default query parameters to include with every request
382 params?: { [key: string]: string | number };
383 }
384
385 export type HttpHeaders = { [key: string]: string };
386
387 export interface HttpRequestOptions {
388 path?: string;
389 method?: string;
390 headers?: HttpHeaders;
391 params?: { [key: string]: string | number };
392 body?: string | Uint8Array | null;
393 }
394
395 // Cockpit HTTP client instance
396 // The generic parameter TResponse controls the type returned by the request methods.
397 export interface HttpInstance<TResponse = string> {
398 request(options: HttpRequestOptions): Promise<TResponse>;
399 get(path: string, options?: Omit<HttpRequestOptions, "method" | "path" | "body">): Promise<TResponse>;
400 post(path: string,
401 // JSON stringification is only implemented in post()
402 body?: string | Uint8Array | JsonObject | null,
403 options?: Omit<HttpRequestOptions, "method" | "path" | "body">
404 ): Promise<TResponse>;
405 close(): void;
406 }
407
408 export function http(endpoint: string): HttpInstance<string>;
409 export function http(endpoint: string, options: HttpOptions & { binary?: false | undefined }): HttpInstance<string>;
410 export function http(endpoint: string, options: HttpOptions & { binary: true }): HttpInstance<Uint8Array>;
411
412 /* === String helpers ======================== */
413
414 export function message(problem: string | JsonObject): string;
415
416 export function format(format_string: string, ...args: unknown[]): string;
417
418 /* === i18n ===================== */
419
420 export function gettext(message: string): string;
421 export function gettext(context: string, message?: string): string;
422 export function ngettext(message1: string, messageN: string, n: number): string;
423 export function ngettext(context: string, message1: string, messageN: string, n: number): string;
424
425 export function translate(): void;
426
427 /* === Number formatting ===================== */
428
429 export type FormatOptions = {
430 precision?: number;
431 base2?: boolean;
432 };
433 export type MaybeNumber = number | null | undefined;
434
435 export function format_number(n: MaybeNumber, precision?: number): string
436 export function format_bytes(n: MaybeNumber, options?: FormatOptions): string;
437 export function format_bytes_per_sec(n: MaybeNumber, options?: FormatOptions): string;
438 export function format_bits_per_sec(n: MaybeNumber, options?: FormatOptions & { base2?: false }): string;
439
440 /** @deprecated */ export function format_bytes(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[];
441 /** @deprecated */ export function format_bytes_per_sec(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[];
442 /** @deprecated */ export function format_bits_per_sec(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[];
443
444 /* === Session ====================== */
445 export function logout(reload: boolean, reason?: string): void;
446
447 export let localStorage: Storage;
448 export let sessionStorage: Storage;
449 }