noflo / noflo-runtime-base

Base library for building NoFlo runtimes
11 stars 12 forks source link

Typescript defination #355

Open linonetwo opened 1 year ago

linonetwo commented 1 year ago

This is guessed using ChatGPT, and fix some any by me.

/* eslint-disable unicorn/prevent-abbreviations */
/* eslint-disable unicorn/prefer-event-target */

declare module 'noflo-runtime-base' {
  // noflo-runtime-base/blob/master/src/protocol/Component.js
  import { EventEmitter } from 'events';
  import { ComponentLoader } from 'noflo';
  import { Graph } from 'fbp-graph';

  interface PortDef {
    addressable?: boolean;
    default?: any;
    description?: string;
    id: string;
    required?: boolean;
    schema?: string;
    type: string;
    values?: any[];
  }

  interface Instance {
    description: string;
    getIcon?: () => string;
    inPorts: Record<string, any>;
    isSubgraph: () => boolean;
    outPorts: Record<string, any>;
  }

  interface ComponentProtocol extends EventEmitter {
    getLoader: (baseDir: string, options?: Record<string, any>) => ComponentLoader;

    getSource: (payload: any, context: any) => void;

    listComponents: (payload: any, context: any) => void;

    loaders: Record<string, ComponentLoader>;

    processComponent: (loader: ComponentLoader, component: string, context: any) => Promise<any>;

    processPort: (portName: string, port: any) => PortDef;

    receive: (topic: string, payload: any, context: any) => any;

    registerGraph: (id: string, graph: Graph, context: any) => void;

    send: (topic: string, payload: any, context: any) => any;

    sendComponent: (component: string, instance: Instance, context: any) => void;

    setSource: (payload: any, context: any) => void;

    transport: BaseTransport;
  }

  const ComponentProtocol: {
    new(transport: BaseTransport): ComponentProtocol;
    initClass: () => void;
  };

  // noflo-runtime-base/blob/master/src/protocol/Graph.js

  import { EventEmitter } from 'events';
  export interface Node {
    component?: string;
    id: string;
    metadata?: any;
  }

  export interface Edge {
    metadata?: any;
    src: {
      index?: number;
      node: string;
      port: string;
    };
    tgt: {
      index?: number;
      node: string;
      port: string;
    };
  }

  export interface Initial {
    metadata?: any;
    src: {
      data: any;
    };
    tgt: {
      index?: number;
      node: string;
      port: string;
    };
  }

  export interface Group {
    metadata?: any;
    name: string;
    nodes: string[];
  }

  export interface Port {
    metadata?: any;
    port: string;
    process: string;
  }

  export interface Payload {
    description?: string;
    from?: string;
    graph: string;
    icon?: string;
    id?: string;
    library?: string;
    main?: boolean;
    metadata?: any;
    name?: string;
    node?: string;
    nodes?: string[];
    port?: string;
    public?: string;
  }

  enum Topic {
    AddEdge = 'addedge',
    AddGroup = 'addgroup',
    AddInitial = 'addinitial',
    AddInport = 'addinport',
    AddNode = 'addnode',
    AddOutport = 'addoutport',
    ChangeEdge = 'changeedge',
    ChangeGroup = 'changegroup',
    ChangeNode = 'changenode',
    Clear = 'clear',
    Error = 'error',
    RemoveEdge = 'removeedge',
    RemoveGroup = 'removegroup',
    RemoveInitial = 'removeinitial',
    RemoveInport = 'removeinport',
    RemoveNode = 'removenode',
    RemoveOutport = 'removeoutport',
    RenameGroup = 'renamegroup',
    RenameInport = 'renameinport',
    RenameNode = 'renamenode',
    RenameOutport = 'renameoutport',
  }

  class GraphProtocol extends EventEmitter {
    transport: BaseTransport;
    graphs: Record<string, Graph>;

    constructor(transport: BaseTransport);
    send(topic: string, payload: Payload, context: Context): any;
    sendAll(topic: string, payload: Payload): any;
    receive(topic: GraphProtocolCommands, payload: Payload, context: Context): void;
    resolveGraph(payload: Payload, context: Context): any;
    getLoader(baseDirectory: string): any;
    sendGraph(id: string, graph: Graph, context: Context): any;
    initGraph(payload: Payload, context: Context): void;
    registerGraph(id: string, graph: Graph, context?: Context, propagate?: boolean): Promise<any>;
    subscribeGraph(id: string, graph: Graph, context: Context): void;
    addNode(graph: Graph, node: Node, context: Context): void;
    removeNode(graph: Graph, payload: Payload, context: Context): void;
    renameNode(graph: Graph, payload: Payload, context: Context): void;
    changeNode(graph: Graph, payload: Payload, context: Context): void;
    addEdge(graph: Graph, edge: Edge, context: Context): void;
    removeEdge(graph: Graph, edge: Edge, context: Context): void;
    changeEdge(graph: Graph, edge: Edge, context: Context): void;
    addInitial(graph: Graph, payload: Initial, context: Context): void;
    removeInitial(graph: Graph, payload: Initial, context: Context): void;
    addInport(graph: Graph, payload: Payload, context: Context): void;
    removeInport(graph: Graph, payload: Payload, context: Context): void;
    renameInport(graph: Graph, payload: Payload, context: Context): void;
    addOutport(graph: Graph, payload: Payload, context: Context): void;
    removeOutport(graph: Graph, payload: Payload, context: Context): void;
    renameOutport(graph: Graph, payload: Payload, context: Context): void;
    addGroup(graph: Graph, payload: Group, context: Context): void;
    removeGroup(graph: Graph, payload: Payload, context: Context): void;
    renameGroup(graph: Graph, payload: Payload, context: Context): void;
    changeGroup(graph: Graph, payload: Group, context: Context): void;
  }

  // noflo-runtime-base/blob/master/src/protocol/Runtime.js

  declare function sendToInport(port: any, event: string, payload: any): void;
  declare function findPort(network: Network | null, name: string, inPort: boolean): any | null;

  interface PortPayload {
    addressable: boolean;
    description: string | undefined;
    id: string;
    required: boolean;
    type: string;
  }

  declare function portToPayload(pub: string, internal: any, network: Network | null, inPort: boolean): PortPayload;
  declare function portsPayload(name: string, network: Network | null): any;

  type OutputSockets = Record<string, InternalSocket>;

  interface RuntimeProtocolOptions {
    capabilities?: string[];
    id?: string;
    label?: string;
    namespace?: string;
    repository?: string;
    repositoryVersion?: string;
  }

  type RuntimeProtocolConstructor = new(transport: BaseTransport) => RuntimeProtocol;

  export interface RuntimeProtocol extends EventEmitter {
    getRuntime(request: any, context: any): any[];
    getRuntimeDefinition(): RuntimeProtocolOptions;
    mainGraph: string | null;

    outputSockets: OutputSockets;
    receive(topic: string, payload: any, context: any): any;
    registerNetwork(name: string, network: Network): void;
    send(topic: string, payload: any, context: any): any;
    sendAll(topic: string, payload: any): any;
    sendError(err: Error, context: any): any;
    sendPacket(payload: any): Promise<void>;
    sendPorts(name: string, network: Network | null, context: any): any;
    setMainGraph(id: string): void;
    subscribeExportedPorts(name: string, network: Network, add: boolean): void;
    subscribeOutPorts(name: string, network: Network, add?: boolean): void;
    subscribeOutdata(graphName: string, network: Network, add: boolean): void;
    transport: BaseTransport;
  }

  /// noflo-runtime-base/blob/master/src/protocol/Network.js

  interface SocketEvent {
    data: any;
    datatype?: string;
    group?: string;
    id: string;
    // Replace with a more accurate type if known
    metadata?: any;
    schema?: string;
    socket: any;
    // Replace with a more accurate type if known
    subgraph?: string; // Replace with a more accurate type if known
  }

  interface Edge {
    src: any; // Replace with a more accurate type if known
    tgt: any; // Replace with a more accurate type if known
  }

  interface Connection {
    port: string;
    process: { id: string };
  }

  interface Network {
    // Replace with a more accurate type if known
    isRunning: () => boolean;
    isStarted: () => boolean;
    on: (event: string, callback: (event: any) => void) => void;
    setDebug: (enable: boolean) => void;
    start: () => Promise<any>;
    // Replace with a more accurate type if known
    stop: () => Promise<any>; // Replace with a more accurate type if known
  }

  export class NetworkProtocol extends EventEmitter {
    constructor(transport: BaseTransport);
    send: (topic: string, payload: any, context: any) => void; // Replace with a more accurate type if known
    sendAll: (topic: string, payload: any) => void; // Replace with a more accurate type if known
    receive: (topic: string, payload: any, context: any) => void; // Replace with a more accurate type if known
    resolveGraph: (payload: any, context: any) => any; // Replace with a more accurate type if known
    getNetwork: (graphName: string) => Network | null;
    updateEdgesFilter: (graph: Graph, payload: any, context: any) => void; // Replace with a more accurate type if known
    eventFiltered: (graph: string, event: SocketEvent) => boolean;
    initNetwork: (graph: Graph, graphName: string, context: any) => Promise<Network>; // Replace with a more accurate type if known
    subscribeNetwork: (network: Network, graphName: string, context: any) => void; // Replace with a more accurate type if known
    startNetwork: (graph: Graph, payload: any, context: any) => void; // Replace with a more accurate type if known
    stopNetwork: (graph: Graph, payload: any, context: any) => void; // Replace with a more accurate type if known
    debugNetwork: (graph: Graph, payload: any, context: any) => void; // Replace with a more accurate type if known
    getStatus: (graph: Graph, payload: any, context: any) => void; // Replace with a more accurate type if known
  }

  // noflo-runtime-base/blob/master/src/protocol/Trace.js

  type TopicType = 'start' | 'stop' | 'dump' | 'clear' | string;

  interface Payload {
    buffersize?: number;
    flowtrace?: any;
    graph: string;
    type?: string;
  }

  type Context = Record<string, any>;

  interface Network {
    getNetwork(graph: string): Network | null;
    setFlowtrace(flowtrace: Flowtrace | null, graphName?: string, flag?: boolean): any;
  }

  interface Runtime {
    getRuntimeDefinition(): any;
    network: Network;
  }

  export class TraceProtocol {
    transport: BaseTransport;
    traces: Record<string, Flowtrace>;

    constructor(transport: BaseTransport);
    send(topic: TopicType, payload: Payload, context: Context): Promise<void>;
    sendAll(topic: TopicType, payload: Payload): Promise<void>;
    receive(topic: TopicType, payload: Payload, context: Context): void;
    resolveGraph(payload: Payload, context: Context): Flowtrace | null;
    startTrace(graphName: string, network: Network, buffersize: number): Flowtrace;
    start(payload: Payload, context: Context): void;
    stop(payload: Payload, context: Context): void;
    dump(payload: Payload, context: Context): void;
    clear(payload: Payload, context: Context): void;
  }

  // noflo-runtime-base/blob/master/src/Base.js
  export interface BaseTransportOptions {
    capabilities?: string[];
    captureOutput?: boolean;
    defaultGraph?: Graph;
    defaultPermissions?: string[];
    namespace?: string;
    permissions?: Record<string, string[]>;
  }

  enum Protocol {
    Component = 'component',
    Graph = 'graph',
    Network = 'network',
    Runtime = 'runtime',
    Trace = 'trace',
  }

  enum CanInputTopic {
    debug = 'debug',
    edges = 'edges',
    getruntime = 'getruntime',
    getsource = 'getsource',
    getstatus = 'getstatus',
    list = 'list',
    packet = 'packet',
    source = 'source',
    start = 'start',
    stop = 'stop',
  }

  export default class BaseTransport extends EventEmitter {
    constructor(options: BaseTransportOptions);
    version: string;
    component: Component;
    graph: Graph;
    network: Network;
    runtime: Runtime;
    trace: Trace;
    context: any;
    options: BaseTransportOptions;

    /**
     * Generate a name for the main graph
     */
    getGraphName(graph: Graph): string;
    /**
     * Check if a given user is authorized for a given capability

    @param [Array] Capabilities to check
    @param [String] Secret provided by user */
    canDo(capability: string | string[], secret: string): boolean;
    /**
     * Check if a given user is authorized to send a given message
     */
    canInput(protocol: Protocol, topic: CanInputTopic, secret: string): boolean;
    /**
     * Get enabled capabilities for a user

    @param [String] Secret provided by user */
    getPermitted(secret: string): string[];
    /**
     * Send a message back to the user via the transport protocol.

    Each transport implementation should provide their own implementation
    of this method.

    The context is usually the context originally received from the
    transport with the request. This could be an iframe origin or a
    specific WebSocket connection.

    @param [String] Name of the protocol
    @param [String] Topic of the message
    @param [Object] Message payload
    @param [Object] Message context, dependent on the transport */
    send(protocol: string, topic: string, payload: any, context?: any): void;
    /*
    *all users*  via the transport protocol

    The transport should verify that the recipients are authorized to receive
    the message by using the `canDo` method.

    Like send() only it sends to all.

    @param [String] Name of the protocol
    @param [String] Topic of the message
    @param [Object] Message payload
    @param [Object] Message context, dependent on the transport */
    sendAll(protocol?: string, topic?: string, payload?: any, context?: any): void;
    /**
     * This is the entry-point to actual protocol handlers. When receiving
    a message, the runtime should call this to make the requested actions
    happen

    The context is originally received from the transport. This could be
    an iframe origin or a specific WebSocket connection. The context will
    be utilized when sending messages back to the requester.

    @param [String] Name of the protocol
    @param [String] Topic of the message
    @param [Object] Message payload
    @param [Object] Message context, dependent on the transport */
    receive(protocol: string, topic: string, payload?: any, context?: any): void;
  }

  export function direct(options: object): void;
}