Code coverage report for bridge.ts

# All files bridge.ts

71.42% Statements 85/119
75% Branches 21/28
95% Functions 19/20
71.42% Lines 85/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230

    1x

    1x

    1x

    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    2x
    2x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    2x
    2x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1x
    18x
    1x
    1x
    18x
    18x

    1x
    1x
    1x
     /**
     * AuroraView SDK Bridge Client
     *
     * Provides a type-safe wrapper around the native bridge API.
     */

    import { EventEmitter, getGlobalEmitter } from './events';
    import type {
      EventHandler,
      Unsubscribe,
      AuroraViewBridge,
      FileSystemAPI,
      DialogAPI,
      ClipboardAPI,
      ShellAPI,
      AuroraViewState,
    } from './types';

    /**
     * AuroraView client interface
     */
    export interface AuroraViewClient {
      /** Call a Python method (RPC-style) */
      call<T = unknown>(method: string, params?: unknown): Promise<T>;

      /** Invoke a plugin command */
      invoke<T = unknown>(cmd: string, args?: Record<string, unknown>): Promise<T>;

      /** Send an event to Python (fire-and-forget) */
      emit(event: string, data?: unknown): void;

      /** Subscribe to an event from Python */
      on<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe;

      /** Subscribe to an event once */
      once<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe;

      /** Unsubscribe from an event */
      off(event: string, handler?: EventHandler): void;

      /** Check if bridge is ready */
      isReady(): boolean;

      /** Wait for bridge to be ready */
      whenReady(): Promise<AuroraViewClient>;

      /** Get the raw bridge object */
      getRawBridge(): AuroraViewBridge | undefined;

      /** File system API */
      readonly fs: FileSystemAPI | undefined;

      /** Dialog API */
      readonly dialog: DialogAPI | undefined;

      /** Clipboard API */
      readonly clipboard: ClipboardAPI | undefined;

      /** Shell API */
      readonly shell: ShellAPI | undefined;

      /** Shared state */
      readonly state: AuroraViewState | undefined;
    }

    /**
     * Internal client implementation
     */
    class AuroraViewClientImpl implements AuroraViewClient {
      private events: EventEmitter;
      private interceptInstalled = false;

      constructor() {
        this.events = getGlobalEmitter();
        this.installTriggerIntercept();
      }

      /**
       * Install intercept on window.auroraview.trigger to forward events
       */
      private installTriggerIntercept(): void {
        if (this.interceptInstalled) return;
        if (typeof window === 'undefined') return;

        const install = () => {
          const bridge = window.auroraview;
          if (!bridge) return;

          const originalTrigger = bridge.trigger;
          bridge.trigger = (event: string, detail?: unknown) => {
            // Call original trigger first
            originalTrigger?.call(bridge, event, detail);
            // Forward to our event system
            this.events.emit(event, detail);
          };

          this.interceptInstalled = true;
        };

        // Try to install immediately
        if (window.auroraview) {
          install();
        } else {
          // Wait for bridge to be available
          const checkInterval = setInterval(() => {
            if (window.auroraview) {
              clearInterval(checkInterval);
              install();
            }
          }, 10);

          // Stop checking after 10 seconds
          setTimeout(() => clearInterval(checkInterval), 10000);
        }
      }

      call<T = unknown>(method: string, params?: unknown): Promise<T> {
        const bridge = window.auroraview;
        if (!bridge) {
          return Promise.reject(new Error('AuroraView bridge not available'));
        }
        return bridge.call<T>(method, params);
      }

      invoke<T = unknown>(cmd: string, args?: Record<string, unknown>): Promise<T> {
        const bridge = window.auroraview;
        if (!bridge) {
          return Promise.reject(new Error('AuroraView bridge not available'));
        }
        return bridge.invoke<T>(cmd, args);
      }

      emit(event: string, data?: unknown): void {
        const bridge = window.auroraview;
        if (bridge) {
          bridge.send_event(event, data);
        }
      }

      on<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe {
        return this.events.on(event, handler);
      }

      once<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe {
        return this.events.once(event, handler);
      }

      off(event: string, handler?: EventHandler): void {
        this.events.off(event, handler);
      }

      isReady(): boolean {
        return window.auroraview?._ready === true;
      }

      whenReady(): Promise<AuroraViewClient> {
        return new Promise((resolve) => {
          if (this.isReady()) {
            resolve(this);
          } else if (window.auroraview) {
            window.auroraview.whenReady().then(() => {
              this.installTriggerIntercept();
              resolve(this);
            });
          } else {
            // Wait for bridge to appear
            const checkInterval = setInterval(() => {
              if (window.auroraview) {
                clearInterval(checkInterval);
                window.auroraview.whenReady().then(() => {
                  this.installTriggerIntercept();
                  resolve(this);
                });
              }
            }, 10);

            // Timeout after 30 seconds
            setTimeout(() => {
              clearInterval(checkInterval);
              resolve(this);
            }, 30000);
          }
        });
      }

      getRawBridge(): AuroraViewBridge | undefined {
        return window.auroraview;
      }

      get fs(): FileSystemAPI | undefined {
        return window.auroraview?.fs;
      }

      get dialog(): DialogAPI | undefined {
        return window.auroraview?.dialog;
      }

      get clipboard(): ClipboardAPI | undefined {
        return window.auroraview?.clipboard;
      }

      get shell(): ShellAPI | undefined {
        return window.auroraview?.shell;
      }

      get state(): AuroraViewState | undefined {
        return window.auroraview?.state;
      }
    }

    /** Singleton client instance */
    let clientInstance: AuroraViewClient | null = null;

    /**
     * Create or get the AuroraView client instance
     */
    export function createAuroraView(): AuroraViewClient {
      if (!clientInstance) {
        clientInstance = new AuroraViewClientImpl();
      }
      return clientInstance;
    }

    /**
     * Get the AuroraView client instance (alias for createAuroraView)
     */
    export function getAuroraView(): AuroraViewClient {
      return createAuroraView();
    }