All files client.ts

95.91% Statements 47/49
75% Branches 15/20
90.47% Functions 19/21
95.65% Lines 44/46

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                                                      6x   6x   6x 6x                 17x 17x       17x 13x 4x                           18x   18x               18x 18x               17x 17x 16x   17x               12x 12x       12x   3x 3x   9x   12x                   5x 5x         5x 1x 1x       4x   5x                   3x       1x                 4x                 2x                       1x                                       5x   4x 5x 5x       5x 5x   4x               1x              
/**
 * HTTP client for the BioPB Tensor FastAPI sidecar.
 *
 * Communicates with the Python FastAPI sidecar (default :8816) over plain
 * HTTP/JSON + binary.  All protected endpoints require a token passed as
 * ``Authorization: Bearer <token>`` or ``X-Biopb-Token``.
 *
 * Usage:
 *   const client = new TensorHttpClient("http://localhost:8816", token);
 *   const sources = await client.listSources();
 *   const arr = await client.slice({ source_id: "...", tensor_id: "...", ... });
 */
 
import type {
  DataSourceDescriptor,
  DiagnosticsSnapshot,
  ReadyzSnapshot,
  SliceRequest,
  TypedNdArray,
} from "./types.js";
 
// ---------------------------------------------------------------------------
// Error types
// ---------------------------------------------------------------------------
 
export class TensorApiError extends Error {
  constructor(
    public readonly status: number,
    message: string,
    public readonly detail?: unknown,
  ) {
    super(`TensorApi ${status}: ${message}`);
    this.name = "TensorApiError";
  }
}
 
// ---------------------------------------------------------------------------
// Timeout helpers
// ---------------------------------------------------------------------------
 
function withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
  return new Promise<T>((resolve, reject) => {
    const id = setTimeout(
      () => reject(new TensorApiError(408, `Timeout after ${ms}ms (${label})`)),
      ms,
    );
    promise.then(
      (v) => { clearTimeout(id); resolve(v); },
      (e) => { clearTimeout(id); reject(e); },
    );
  });
}
 
// ---------------------------------------------------------------------------
// Client
// ---------------------------------------------------------------------------
 
export class TensorHttpClient {
  private readonly base: string;
  private readonly token: string | null;
 
  /** Timeout for metadata / listing requests (ms). */
  metadataTimeoutMs = 3_000;
  /** Timeout for binary chunk/slice requests (ms). */
  chunkTimeoutMs = 8_000;
 
  /**
   * @param apiBase   Base URL of the FastAPI sidecar, e.g. "http://localhost:8816".
   * @param token     Website token.  Pass null or "" to skip auth header
   *                  (dev-mode bypass on the server side).
   */
  constructor(apiBase: string, token: string | null) {
    this.base = apiBase.replace(/\/$/, "");
    this.token = token || null;
  }
 
  // -------------------------------------------------------------------------
  // Internal helpers
  // -------------------------------------------------------------------------
 
  private headers(extra?: Record<string, string>): Record<string, string> {
    const h: Record<string, string> = { "Content-Type": "application/json" };
    if (this.token) {
      h["Authorization"] = `Bearer ${this.token}`;
    }
    return { ...h, ...extra };
  }
 
  private async fetchJson<T>(
    path: string,
    options?: RequestInit,
    timeoutMs?: number,
  ): Promise<T> {
    const url = `${this.base}${path}`;
    const promise = fetch(url, {
      ...options,
      headers: { ...this.headers(), ...(options?.headers as Record<string, string> ?? {}) },
    }).then(async (res) => {
      if (!res.ok) {
        let detail: unknown;
        try { detail = await res.json(); } catch { /* ignore */ }
        throw new TensorApiError(res.status, res.statusText, detail);
      }
      return res.json() as Promise<T>;
    });
    return timeoutMs != null
      ? withTimeout(promise, timeoutMs, path)
      : promise;
  }
 
  private async fetchBinary(
    path: string,
    body: unknown,
    timeoutMs?: number,
  ): Promise<Response> {
    const url = `${this.base}${path}`;
    const promise = fetch(url, {
      method: "POST",
      headers: this.headers(),
      body: JSON.stringify(body),
    }).then((res) => {
      if (!res.ok) {
        return res.json().then(
          (detail) => { throw new TensorApiError(res.status, res.statusText, detail); },
          () => { throw new TensorApiError(res.status, res.statusText); },
        );
      }
      return res;
    });
    return timeoutMs != null
      ? withTimeout(promise, timeoutMs, path)
      : promise;
  }
 
  // -------------------------------------------------------------------------
  // Health (no auth required)
  // -------------------------------------------------------------------------
 
  async livez(): Promise<{ status: string; timestamp: string }> {
    return this.fetchJson("/livez", undefined, this.metadataTimeoutMs);
  }
 
  async readyz(): Promise<ReadyzSnapshot> {
    return this.fetchJson("/readyz", undefined, this.metadataTimeoutMs);
  }
 
  // -------------------------------------------------------------------------
  // Sources
  // -------------------------------------------------------------------------
 
  /** List all data sources registered with the server. */
  async listSources(): Promise<DataSourceDescriptor[]> {
    return this.fetchJson<DataSourceDescriptor[]>(
      "/api/sources",
      undefined,
      this.metadataTimeoutMs,
    );
  }
 
  /** Get a single DataSourceDescriptor by source_id. */
  async getSource(sourceId: string): Promise<DataSourceDescriptor> {
    return this.fetchJson<DataSourceDescriptor>(
      `/api/sources/${encodeURIComponent(sourceId)}`,
      undefined,
      this.metadataTimeoutMs,
    );
  }
 
  /**
   * Get the parsed OME-NGFF metadata for a source.
   * Returns an empty object if the source has no metadata.
   */
  async getSourceMetadata(sourceId: string): Promise<Record<string, unknown>> {
    return this.fetchJson<Record<string, unknown>>(
      `/api/sources/${encodeURIComponent(sourceId)}/metadata`,
      undefined,
      this.metadataTimeoutMs,
    );
  }
 
  // -------------------------------------------------------------------------
  // Slice
  // -------------------------------------------------------------------------
 
  /**
   * Fetch a sub-region of a tensor as raw bytes.
   *
   * The server returns C-contiguous numpy bytes; shape, dtype, and dim labels
   * are in response headers ``X-Shape``, ``X-Dtype``, ``X-Dim-Labels``.
   *
   * @throws {TensorApiError} on HTTP error or timeout.
   */
  async slice(req: SliceRequest): Promise<TypedNdArray> {
    const res = await this.fetchBinary("/api/slice", req, this.chunkTimeoutMs);
 
    const shapeHeader = res.headers.get("X-Shape") ?? "";
    const dtype = res.headers.get("X-Dtype") ?? "uint8";
    const dimLabels = (res.headers.get("X-Dim-Labels") ?? "")
      .split(",")
      .filter(Boolean);
 
    const shape = shapeHeader.split(",").filter(Boolean).map(Number);
    const buffer = await res.arrayBuffer();
 
    return { buffer, shape, dtype, dimLabels };
  }
 
  // -------------------------------------------------------------------------
  // Diagnostics
  // -------------------------------------------------------------------------
 
  async diagnostics(): Promise<DiagnosticsSnapshot> {
    return this.fetchJson<DiagnosticsSnapshot>(
      "/api/diagnostics",
      undefined,
      this.metadataTimeoutMs,
    );
  }
}