Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

799 B
Raw Blame
/**
 * Controllable fake upstream ReadableStream for integration tests.
 * Allows pushing chunks, closing, erroring, and observing cancel.
 */
export function fakeUpstreamStream() {
  let controllerRef: ReadableStreamDefaultController<Uint8Array> | null = null;
  let cancelCb: ((reason?: unknown) => void) | null = null;
  const enc = new TextEncoder();

  const stream = new ReadableStream<Uint8Array>({
    start(controller) {
      controllerRef = controller;
    },
    cancel(reason) {
      cancelCb?.(reason);
    },
  });

  return {
    stream,
    push: (s: string) => controllerRef?.enqueue(enc.encode(s)),
    close: () => controllerRef?.close(),
    error: (e: unknown) => controllerRef?.error(e),
    onCancel: (cb: (reason?: unknown) => void) => {
      cancelCb = cb;
    },
  };
}