Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xsline OpenChatCut FetchGeneratedResult

From Leeroopedia
Knowledge Sources
Domains AI_Generation, Download, Security
Last Updated 2026-10-22 14:00 GMT

Overview

Concrete function that downloads a generated media result from a provider URL through the SSRF-safe fetcher, with per-media-type byte limits, header and body idle deadlines, and retry with exponential backoff.

Description

The fetchGeneratedResult function retrieves the finished product after an AI generation job completes. It:

  • Uses SSRF-safe fetch: Defaults to `safePublicFetch` which blocks private/internal IP ranges.
  • Enforces byte limits: Per media type:
    • Audio: 1 GB
    • Video: 10 GB
    • Image: 128 MB
    • Subtitle: 5 MB
  • Header deadline: 30s (time to receive response headers).
  • Body idle deadline: 60s (max gap between body chunks).
  • Retries: 3 attempts with exponential backoff.
    • Retryable: HTTP 429, 5xx.
    • Non-retryable: HTTP 4xx (except 429), SSRF rejections.
  • Validates content-type: Checks the response content-type against the expected media type.
  • Bounded body stream: Returns a `Response` with a validated, bounded body stream.
  • Error reporting: Throws `ResultDownloadError` with the URL and reason after exhaustion.

Usage

  • Generation job lifecycle: Called by the `registerDownload` callback during job execution to fetch provider results.
  • Result download plugin: Exposed as an HTTP endpoint for on-demand downloads.

Code Reference

Source Location

  • Repository: OpenChatCut
  • File:*** server/plugins/result-download.ts

Signature

export type GeneratedResultKind = 'audio' | 'video' | 'image' | 'subtitle';

export class ResultDownloadError extends Error {
  constructor(url: string, message: string, retryable?: boolean);
}

export interface FetchResultDeps {
  fetchImpl?: typeof safePublicFetch;
  maxBytes?: number;
  headerDeadlineMs?: number;
  bodyIdleDeadlineMs?: number;
  sleep?: (ms: number) => Promise<void>;
}

export async function fetchGeneratedResult(
  url: string,
  label: GeneratedResultKind,
  deps?: FetchResultDeps,
): Promise<Response>;

Import

import { fetchGeneratedResult, type GeneratedResultKind } from '../server/plugins/result-download.ts';

I/O Contract

Inputs

Name Type Required Description
url string Yes Provider result URL
label GeneratedResultKind Yes Media type: audio, video, image, subtitle
deps.maxBytes number No Override max bytes (default per type)
deps.headerDeadlineMs number No Header deadline (default 30s)
deps.bodyIdleDeadlineMs number No Body idle deadline (default 60s)

Outputs

Name Type Description
fetchGeneratedResult() returns Response Validated response with bounded body stream
throws ResultDownloadError After 3 failed attempts, includes URL for retry

Usage Examples

Download a Generated Image

import { fetchGeneratedResult } from '../server/plugins/result-download.ts';

const response = await fetchGeneratedResult(
  'https://fal.media/results/abc123.png',
  'image',
);

// Read the body stream to save the file
const buffer = await response.arrayBuffer();
await writeFile('/data/uploads/generated-image.png', Buffer.from(buffer));

Download a Video with Custom Limits

const response = await fetchGeneratedResult(
  'https://fal.media/results/abc123.mp4',
  'video',
  { maxBytes: 500 * 1024 * 1024, headerDeadlineMs: 60_000 },  // 500 MB, 60s header
);

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment