Hyperlinkv0.8.0-beta.28

Node

Node.withProtocolconstsrc/internal/nodeCore.ts:837
<const T extends ShorthandTarget>(transports: T): <
  Self,
  ROut,
  K extends ProtocolKind
>(
  node: NodeTagClass<
    Self,
    ROut,
    {
      readonly kind: K
      readonly url?: string
      readonly path?: string
      readonly endpoints?: Endpoints
    }
  >
) => NodeTagClass<Self, ROut, MultiAddress<K | KindsOf<T>>>

Add transports to a node — piped on to derive a same-identity handle that speaks more protocols: class DropletWs extends Droplet.pipe(Node.withProtocol({ ws: "/rpc" })) {}. Takes the same { http, ws, ipc } record as the declaration; keeps the node's key and served resources, merges the transports into its endpoints, and widens its ProtocolKind set in the type. Accepts single- or multi-protocol nodes alike.

export const withProtocol =
  <const T extends ShorthandTarget>(transports: T) =>
  <Self, ROut, K extends ProtocolKind>(
    node: NodeTagClass<
      Self,
      ROut,
      {
        readonly kind: K;
        readonly url?: string;
        readonly path?: string;
        readonly endpoints?: Endpoints;
      }
    >,
  ): NodeTagClass<Self, ROut, MultiAddress<K | KindsOf<T>>> => {
    const add: Endpoints = {
      ...(transports.http !== undefined ? { Http: { url: transports.http } } : {}),
      ...(transports.ws !== undefined ? { WebSocket: { url: transports.ws } } : {}),
      ...(transports.ipc !== undefined
        ? { IpcSocket: { path: transports.ipc } }
        : {}),
    };
    const endpoints: Endpoints = { ...node.endpoints, ...add };
    // Keep the base node's primary if it had one, else the first of the merged set (http > ws > ipc).
    const kind: ProtocolKind | undefined =
      node.kind ??
      (endpoints.Http !== undefined
        ? "Http"
        : endpoints.WebSocket !== undefined
          ? "WebSocket"
          : endpoints.IpcSocket !== undefined
            ? "IpcSocket"
            : undefined);
    return assembleNode<Self, ROut, MultiAddress<K | KindsOf<T>>>(node.key, {
      url: node.url ?? endpoints.Http?.url ?? endpoints.WebSocket?.url,
      path: node.path ?? endpoints.IpcSocket?.path,
      kind,
      endpoints,
      // Same-identity derived handle keeps the base node's advertise policy.
      onConflict: node.onConflict,
    });
  };