import type { BlipClient } from '../client.ts' import { type Identity, Node } from '../types/index.ts' import { uri } from '../utils/uri.ts' import { type ConsumeOptions, Namespace, type SendCommandOptions } from './namespace.ts' export class TunnelNamespace extends Namespace { constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) { super(blipClient, 'tunnel', defaultOptions) } /** * Retrieves metadata information about the tunnel origin and destination * This must only be called by a subbot * @returns An object containing: * - `owner` - The identity of the subbot owner (router) * - `originator` - The identity of the originator on the router (user) * - `destination` - The identity of the destination child (subbot) */ public getTunnel( tunnelIdentityOrName: Identity | string, opts?: ConsumeOptions, ): Promise<{ owner: Identity originator: Identity destination: Identity }> { const node = typeof tunnelIdentityOrName === 'string' ? new Node(tunnelIdentityOrName, 'tunnel.msging.net') : Node.from(tunnelIdentityOrName) if (node.getSource() !== 'Tunnel') { throw new Error('Invalid tunnel identity or name') } return this.sendCommand( { method: 'get', uri: uri`/tunnels/${node.name}`, }, opts, ) } /** * Retrieves the tunnel ID for a given originator and destination * Must only be called by a router * @param originator - The identity of the originator on the router (user) * @param destination - The identity of the destination child (subbot) * @returns The tunnel ID */ public getTunnelId(originator: Identity, destination: Identity) { return this.sendCommand({ method: 'get', uri: uri`/tunnels/${originator}/${destination}`, }) } }