diff --git a/navigator/types/cockpit/Problem.d.ts b/navigator/types/cockpit/Problem.d.ts new file mode 100644 index 0000000..c6dd0be --- /dev/null +++ b/navigator/types/cockpit/Problem.d.ts @@ -0,0 +1,16 @@ +export type ProblemCode = + 'access-denied' | + 'authentication-failed' | + 'internal-error' | + 'no-cockpit' | + 'no-session' | + 'not-found' | + 'terminated' | + 'timeout' | + 'unknown-hostkey' | + 'no-forwarding'; + +export interface Problem { + message: string; + problem: ProblemCode | null; +} diff --git a/navigator/types/cockpit/Spawn.d.ts b/navigator/types/cockpit/Spawn.d.ts new file mode 100644 index 0000000..dccb834 --- /dev/null +++ b/navigator/types/cockpit/Spawn.d.ts @@ -0,0 +1,55 @@ +import { Problem } from "./Problem"; + +export interface SpawnProblem extends Problem { + exit_status: number | null; + exit_signal: string | null; +} + +export type SpawnOptionsErr = "out" | "ignore" | "message" + +export interface SpawnOptions { + /** + * If set to `true` then handle the input and output of the process as + * arrays of binary bytes. + */ + binary?: boolean; + /** + * The directory to spawn the process in. + */ + directory?: string; + /** + * Controls where the standard error is sent. By default it is logged to the + * journal. + * If set to `"out"` it is included in with the output data. + * If set to `"ignore"` then the error output is discarded. + * If set to `"message"`, then it will be returned as the error message. + * When the {@link SpawnOptions.pty} `"pty"` field is set, this field has no effect. + */ + err?: "out" | "ignore" | "message"; + /** + * The remote host to spawn the process on. If an alternate user or port is + * required it can be specified as `"user@myhost:port"`. If no host is + * specified then the correct one will be automatically selected based on + * the page calling this function. + */ + host?: string; + /** + * An optional array that contains strings to be used as additional + * environment variables for the new process. These are "NAME=VALUE" + * strings. + */ + environ?: string[]; + /** + * Launch the process in its own PTY terminal, and send/receive terminal + * input and output. + */ + pty?: boolean; + /** + * Batch data coming from the process in blocks of at least this size. This + * is not a guarantee. After a short timeout the data will be sent even if + * the data doesn't match the batch size. Defaults to zero. + */ + batch?: number; +} + +export type Spawn = () => void; diff --git a/navigator/types/cockpit/index.d.ts b/navigator/types/cockpit/index.d.ts new file mode 100644 index 0000000..09b6990 --- /dev/null +++ b/navigator/types/cockpit/index.d.ts @@ -0,0 +1,167 @@ +export import { Problem, ProblemCode } from './Problem'; +export import { Spawn, SpawnOptions, SpawnOptionsErr, SpawnProblem } from './Spawn'; + +export interface Cockpit { + spawn: Spawn; +} + +declare global { + declare var cockpit: Cockpit; +} +// interface Func1 { +// (arg: T): R; +// } + +// interface Func2 { +// (arg1: T, arg2: K): R; +// } + +// interface Func3 { +// (arg1: T, arg2: K, arg3: V): R; +// } + +// type GUID = string; + +// type Fail = { +// message: string; +// problem?: string; +// }; + +// type SpawnFail = Fail & { +// exit_status?: number; +// exit_signal?: number; +// }; + +// type ErrorConfig = 'message' | 'out' | 'ignore' | 'pty'; +// type Superuser = 'require' | 'try'; +// type ProblemCodes = 'access-denied' | 'authentication-failed' | 'internal-error' | 'no-cockpit' | 'no-session' | 'not-found' | 'terminated' | 'timeout' | 'unknown-hostkey' | 'no-forwarding'; + +// type SpawnConfig = { +// err?: ErrorConfig; +// binary?: boolean; +// directory?: string; +// host?: string; +// environ?: string[]; +// pty?: boolean; +// batch?: boolean; +// latency?: number; +// superuser?: Superuser; +// }; + +// interface SyntaxParser { +// parse: Func1; +// stringify: Func1; +// } + +// type FileConfig = { +// syntax?: SyntaxParser; +// binary?: boolean; +// max_read_size?: number; +// superuser?: Superuser; +// host?: string; +// }; + +// interface FileOperationsPromise extends JQuery.Promise {} + +// interface ClosableWithProblem { close(problem?: ProblemCodes): void; } + +// interface FileOperations extends Closable { +// read(): FileOperationsPromise; +// replace(content: string | null, tag?: string): FileOperationsPromise; +// modify(): FileOperationsPromise; +// watch(callback: Func3 | Func2): void; +// /** +// * A string containing the path that was passed to the `cockpit.file()` method. +// */ +// path: string; +// } + +// interface SpawnPromise extends JQuery.Promise, ClosableWithProblem { +// stream(callback: Func1): SpawnPromise; +// input(data?: string | Uint8Array, stream?: boolean): SpawnPromise; +// } + +// interface Closable { close(): void; } + +// function CacheProvider(provide: Func1, key: any): Closable | null; + +// interface UserInfo { +// id: number; +// name: string; +// full_name: string; +// groups: string[]; +// home: string; +// shell: string; +// } + +// interface EventHandler { +// addEventListener(type: T, handler: Func1>); +// removeEventListener(type: T, handler: Func1>); +// dispatchEvent(event: Event); +// } + +// interface UserInfoPromise extends JQuery.Promise {} + +// type PermissionOptions = { group: string }; + +// type PermissionEvents = 'changed'; + +// interface PermissionInfo extends EventHandler, Closable { +// allowed: boolean; +// user: UserInfo; +// }; + +// interface PermissionInfoPromise extends JQuery.Promise {}; + +// type HttpHeaders = any; + +// interface HttpOptions { +// address: string; +// connection: string; +// superuser: Superuser; +// } + +// type HttpData = string | Uint8Array; + +// declare const enum HttpMethod { +// Get = 'GET', +// Post = 'POST', +// Head = 'HEAD' +// } + +// interface HttpRequestOptions { +// body?: HttpData; +// headers?: HttpHeaders; +// method?: HttpMethod; +// params?: any; +// path?: string; +// } + +// interface HttpOperations extends ClosableWithProblem { +// get(path: string, params: any, headers: HttpHeaders): HttpOperationsPromise; +// post(path: string, body: string | any, headers: HttpHeaders): HttpOperationsPromise; +// request(options: HttpRequestOptions): HttpOperationsPromise; +// } + +// interface HttpOperationsPromise extends JQuery.Promise, ClosableWithProblem { +// response(handler: Func2): HttpOperationsPromise; +// stream(handler: Func1): HttpOperationsPromise; +// input(handler: HttpData, stream?: boolean): HttpOperationsPromise; +// } + +// interface CockpitAPI { +// spawn(path: string[], config?: SpawnConfig): SpawnPromise; +// script(path: string, args?: string[], config?: SpawnConfig): SpawnPromise; +// file(path: string): FileOperations; +// cache(key: GUID, provider: CacheProvider, consumer: Func2): Closable; +// logout(reload: boolean): void; +// user(): UserInfoPromise; +// permission(options?: PermissionOptions): PermissionInfoPromise; +// http(endpoint: string | number, options: HttpOptions): HttpOperations; +// } + +// declare var cockpit : CockpitAPI; +// declare module 'cockpit' { +// export default cockpit; +// export { Superuser, ErrorConfig, ProblemCodes }; +// }