@effing/ffs 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -16
- package/dist/{chunk-RNE6TKMF.js → chunk-J64HSZNQ.js} +276 -207
- package/dist/chunk-J64HSZNQ.js.map +1 -0
- package/dist/chunk-XSCNUWZJ.js +935 -0
- package/dist/chunk-XSCNUWZJ.js.map +1 -0
- package/dist/handlers/index.d.ts +40 -7
- package/dist/handlers/index.js +10 -2
- package/dist/index.d.ts +23 -15
- package/dist/index.js +3 -9
- package/dist/proxy-qTA69nOV.d.ts +72 -0
- package/dist/server.js +853 -283
- package/dist/server.js.map +1 -1
- package/package.json +2 -2
- package/dist/cache-BUVFfGZF.d.ts +0 -25
- package/dist/chunk-LK5K4SQV.js +0 -439
- package/dist/chunk-LK5K4SQV.js.map +0 -1
- package/dist/chunk-RNE6TKMF.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/handlers/shared.ts","../src/proxy.ts","../src/handlers/caching.ts","../src/handlers/orchestrating.ts","../src/handlers/rendering.ts"],"sourcesContent":["import express from \"express\";\nimport type { TransientStore } from \"../storage\";\nimport { createTransientStore } from \"../storage\";\nimport { HttpProxy } from \"../proxy\";\nimport type {\n EffieData,\n EffieSources,\n EffieSourceWithType,\n} from \"@effing/effie\";\nimport { effieDataSchema } from \"@effing/effie\";\n\nexport type UploadOptions = {\n videoUrl: string;\n coverUrl?: string;\n};\n\nexport type WarmupJob = {\n sources: EffieSourceWithType[];\n};\n\nexport type RenderJob = {\n effie: EffieData<EffieSources>;\n scale: number;\n upload?: UploadOptions;\n createdAt: number;\n};\n\nexport type WarmupAndRenderJob = {\n effie: EffieData<EffieSources>;\n sources: EffieSourceWithType[];\n scale: number;\n upload?: UploadOptions;\n warmupJobId: string;\n renderJobId: string;\n createdAt: number;\n};\n\nexport type ServerContext = {\n transientStore: TransientStore;\n httpProxy: HttpProxy;\n baseUrl: string;\n skipValidation: boolean;\n warmupConcurrency: number;\n warmupBackendBaseUrl?: string;\n renderBackendBaseUrl?: string;\n};\n\nexport type SSEEventSender = (event: string, data: object) => void;\n\nexport type ParseEffieResult =\n | { effie: EffieData<EffieSources> }\n | { error: string; issues?: object[] };\n\n/**\n * Create the server context with configuration from environment variables\n */\nexport async function createServerContext(): Promise<ServerContext> {\n const port = process.env.FFS_PORT || 2000;\n const httpProxy = new HttpProxy();\n await httpProxy.start();\n return {\n transientStore: createTransientStore(),\n httpProxy,\n baseUrl: process.env.FFS_BASE_URL || `http://localhost:${port}`,\n skipValidation:\n !!process.env.FFS_SKIP_VALIDATION &&\n process.env.FFS_SKIP_VALIDATION !== \"false\",\n warmupConcurrency: parseInt(process.env.FFS_WARMUP_CONCURRENCY || \"4\", 10),\n warmupBackendBaseUrl: process.env.FFS_WARMUP_BACKEND_BASE_URL,\n renderBackendBaseUrl: process.env.FFS_RENDER_BACKEND_BASE_URL,\n };\n}\n\n/**\n * Parse and validate Effie data from request body\n */\nexport function parseEffieData(\n body: unknown,\n skipValidation: boolean,\n): ParseEffieResult {\n // Wrapped format has `effie` property\n const isWrapped =\n typeof body === \"object\" && body !== null && \"effie\" in body;\n const rawEffieData = isWrapped ? (body as { effie: unknown }).effie : body;\n\n if (!skipValidation) {\n const result = effieDataSchema.safeParse(rawEffieData);\n if (!result.success) {\n return {\n error: \"Invalid effie data\",\n issues: result.error.issues.map((issue) => ({\n path: issue.path.join(\".\"),\n message: issue.message,\n })),\n };\n }\n return { effie: result.data };\n } else {\n const effie = rawEffieData as EffieData<EffieSources>;\n if (!effie?.segments) {\n return { error: \"Invalid effie data: missing segments\" };\n }\n return { effie };\n }\n}\n\n/**\n * Set up CORS headers for public endpoints\n */\nexport function setupCORSHeaders(res: express.Response): void {\n res.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n res.setHeader(\"Access-Control-Allow-Methods\", \"GET\");\n}\n\n/**\n * Set up SSE response headers\n */\nexport function setupSSEResponse(res: express.Response): void {\n res.setHeader(\"Content-Type\", \"text/event-stream\");\n res.setHeader(\"Cache-Control\", \"no-cache\");\n res.setHeader(\"Connection\", \"keep-alive\");\n res.flushHeaders();\n}\n\n/**\n * Create an SSE event sender function for a response\n */\nexport function createSSEEventSender(res: express.Response): SSEEventSender {\n return (event: string, data: object) => {\n res.write(`event: ${event}\\ndata: ${JSON.stringify(data)}\\n\\n`);\n };\n}\n","import http from \"http\";\nimport type { AddressInfo, Server } from \"net\";\nimport { Readable } from \"stream\";\nimport { ffsFetch } from \"./fetch\";\n\n/**\n * HTTP proxy for FFmpeg URL handling.\n *\n * Static FFmpeg binaries can have DNS resolution issues on Alpine Linux (musl libc).\n * This proxy lets Node.js handle DNS lookups instead of FFmpeg by proxying HTTP\n * requests through localhost.\n *\n * URL scheme (M3U8-compatible):\n * - Original: https://cdn.example.com/path/to/stream.m3u8\n * - Proxy: http://127.0.0.1:{port}/https://cdn.example.com/path/to/stream.m3u8\n * - Relative: segment-0.ts → http://127.0.0.1:{port}/https://cdn.example.com/path/to/segment-0.ts\n */\nexport class HttpProxy {\n private server: Server | null = null;\n private _port: number | null = null;\n private startPromise: Promise<void> | null = null;\n\n get port(): number | null {\n return this._port;\n }\n\n /**\n * Transform a URL to go through the proxy.\n * @throws Error if proxy not started\n */\n transformUrl(url: string): string {\n if (this._port === null) throw new Error(\"Proxy not started\");\n return `http://127.0.0.1:${this._port}/${url}`;\n }\n\n /**\n * Start the proxy server. Safe to call multiple times.\n */\n async start(): Promise<void> {\n if (this._port !== null) return;\n if (this.startPromise) {\n await this.startPromise;\n return;\n }\n this.startPromise = this.doStart();\n await this.startPromise;\n }\n\n private async doStart(): Promise<void> {\n this.server = http.createServer(async (req, res) => {\n try {\n const originalUrl = this.parseProxyPath(req.url || \"\");\n if (!originalUrl) {\n res.writeHead(400, { \"Content-Type\": \"text/plain\" });\n res.end(\"Bad Request: invalid proxy path\");\n return;\n }\n\n const response = await ffsFetch(originalUrl, {\n method: req.method as \"GET\" | \"HEAD\" | undefined,\n headers: this.filterHeaders(req.headers),\n bodyTimeout: 0, // No timeout for streaming\n });\n\n // Convert response headers to plain object\n const headers: Record<string, string> = {};\n response.headers.forEach((value, key) => {\n headers[key] = value;\n });\n\n res.writeHead(response.status, headers);\n\n if (response.body) {\n const nodeStream = Readable.fromWeb(response.body);\n nodeStream.pipe(res);\n nodeStream.on(\"error\", (err) => {\n console.error(\"Proxy stream error:\", err);\n res.destroy();\n });\n } else {\n res.end();\n }\n } catch (err) {\n console.error(\"Proxy request error:\", err);\n if (!res.headersSent) {\n res.writeHead(502, { \"Content-Type\": \"text/plain\" });\n res.end(\"Bad Gateway\");\n } else {\n res.destroy();\n }\n }\n });\n\n await new Promise<void>((resolve) => {\n this.server!.listen(0, \"127.0.0.1\", () => {\n this._port = (this.server!.address() as AddressInfo).port;\n resolve();\n });\n });\n }\n\n /**\n * Parse the proxy path to extract the original URL.\n * Path format: /{originalUrl}\n */\n private parseProxyPath(path: string): string | null {\n if (!path.startsWith(\"/http://\") && !path.startsWith(\"/https://\")) {\n return null;\n }\n return path.slice(1); // Remove leading /\n }\n\n /**\n * Filter headers to forward to the upstream server.\n * Removes hop-by-hop headers that shouldn't be forwarded.\n */\n private filterHeaders(\n headers: http.IncomingHttpHeaders,\n ): Record<string, string> {\n const skip = new Set([\n \"host\",\n \"connection\",\n \"keep-alive\",\n \"transfer-encoding\",\n \"te\",\n \"trailer\",\n \"upgrade\",\n \"proxy-authorization\",\n \"proxy-authenticate\",\n ]);\n\n const result: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n if (!skip.has(key.toLowerCase()) && typeof value === \"string\") {\n result[key] = value;\n }\n }\n return result;\n }\n\n /**\n * Close the proxy server and reset state.\n */\n close(): void {\n this.server?.close();\n this.server = null;\n this._port = null;\n this.startPromise = null;\n }\n}\n","import express from \"express\";\nimport { Readable, Transform } from \"stream\";\nimport { randomUUID } from \"crypto\";\nimport { storeKeys } from \"../storage\";\nimport { ffsFetch } from \"../fetch\";\nimport {\n extractEffieSources,\n extractEffieSourcesWithTypes,\n} from \"@effing/effie\";\nimport type { EffieSourceWithType } from \"@effing/effie\";\nimport type { ServerContext, SSEEventSender, WarmupJob } from \"./shared\";\nimport {\n parseEffieData,\n setupCORSHeaders,\n setupSSEResponse,\n createSSEEventSender,\n} from \"./shared\";\nimport { proxyRemoteSSE } from \"./orchestrating\";\n\n/**\n * Check if a source should be skipped during warmup.\n * Video/audio sources are passed directly to FFmpeg and don't need caching.\n */\nfunction shouldSkipWarmup(source: EffieSourceWithType): boolean {\n return source.type === \"video\" || source.type === \"audio\";\n}\n\n// Track in-flight fetches to avoid duplicate fetches within the same instance\nconst inFlightFetches = new Map<string, Promise<void>>();\n\n/**\n * POST /warmup - Create a warmup job\n * Stores the source list in cache and returns a job ID for SSE streaming\n */\nexport async function createWarmupJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n const parseResult = parseEffieData(req.body, ctx.skipValidation);\n if (\"error\" in parseResult) {\n res.status(400).json(parseResult);\n return;\n }\n\n const sources = extractEffieSourcesWithTypes(parseResult.effie);\n const jobId = randomUUID();\n\n // Store job in cache with job metadata TTL\n await ctx.transientStore.putJson(\n storeKeys.warmupJob(jobId),\n { sources },\n ctx.transientStore.jobMetadataTtlMs,\n );\n\n res.json({\n id: jobId,\n url: `${ctx.baseUrl}/warmup/${jobId}`,\n });\n } catch (error) {\n console.error(\"Error creating warmup job:\", error);\n res.status(500).json({ error: \"Failed to create warmup job\" });\n }\n}\n\n/**\n * GET /warmup/:id - Stream warmup progress via SSE\n * Fetches and caches sources, emitting progress events\n */\nexport async function streamWarmupJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n setupCORSHeaders(res);\n\n const jobId = req.params.id;\n\n // Proxy to warmup backend if configured\n if (ctx.warmupBackendBaseUrl) {\n setupSSEResponse(res);\n const sendEvent = createSSEEventSender(res);\n try {\n await proxyRemoteSSE(\n `${ctx.warmupBackendBaseUrl}/warmup/${jobId}`,\n sendEvent,\n \"\",\n res,\n );\n } finally {\n res.end();\n }\n return;\n }\n\n const jobCacheKey = storeKeys.warmupJob(jobId);\n const job = await ctx.transientStore.getJson<WarmupJob>(jobCacheKey);\n // only allow the warmup job to run once\n ctx.transientStore.delete(jobCacheKey);\n\n if (!job) {\n res.status(404).json({ error: \"Job not found\" });\n return;\n }\n\n setupSSEResponse(res);\n const sendEvent = createSSEEventSender(res);\n\n try {\n await warmupSources(job.sources, sendEvent, ctx);\n sendEvent(\"complete\", { status: \"ready\" });\n } catch (error) {\n sendEvent(\"error\", { message: String(error) });\n } finally {\n res.end();\n }\n } catch (error) {\n console.error(\"Error in warmup streaming:\", error);\n if (!res.headersSent) {\n res.status(500).json({ error: \"Warmup streaming failed\" });\n } else {\n res.end();\n }\n }\n}\n\n/**\n * POST /purge - Purge cached sources for an Effie composition\n */\nexport async function purgeCache(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n const parseResult = parseEffieData(req.body, ctx.skipValidation);\n if (\"error\" in parseResult) {\n res.status(400).json(parseResult);\n return;\n }\n\n const sources = extractEffieSources(parseResult.effie);\n\n let purged = 0;\n for (const url of sources) {\n const ck = storeKeys.source(url);\n if (await ctx.transientStore.exists(ck)) {\n await ctx.transientStore.delete(ck);\n purged++;\n }\n }\n\n res.json({ purged, total: sources.length });\n } catch (error) {\n console.error(\"Error purging cache:\", error);\n res.status(500).json({ error: \"Failed to purge cache\" });\n }\n}\n\n/**\n * Warm up sources by fetching and caching them.\n * HTTP(S) video/audio sources are skipped as they are passed directly to FFmpeg.\n */\nexport async function warmupSources(\n sources: EffieSourceWithType[],\n sendEvent: SSEEventSender,\n ctx: ServerContext,\n): Promise<void> {\n const total = sources.length;\n\n sendEvent(\"start\", { total });\n\n let cached = 0;\n let failed = 0;\n let skipped = 0;\n\n // Separate sources that need caching from those that should be skipped\n const sourcesToCache: EffieSourceWithType[] = [];\n for (const source of sources) {\n if (shouldSkipWarmup(source)) {\n skipped++;\n sendEvent(\"progress\", {\n url: source.url,\n status: \"skipped\",\n reason: \"http-video-audio-passthrough\",\n cached,\n failed,\n skipped,\n total,\n });\n } else {\n sourcesToCache.push(source);\n }\n }\n\n // Check what's already cached\n const sourceCacheKeys = sourcesToCache.map((s) => storeKeys.source(s.url));\n const existsMap = await ctx.transientStore.existsMany(sourceCacheKeys);\n\n // Report hits immediately\n for (let i = 0; i < sourcesToCache.length; i++) {\n if (existsMap.get(sourceCacheKeys[i])) {\n cached++;\n sendEvent(\"progress\", {\n url: sourcesToCache[i].url,\n status: \"hit\",\n cached,\n failed,\n skipped,\n total,\n });\n }\n }\n\n // Filter to uncached sources\n const uncached = sourcesToCache.filter(\n (_, i) => !existsMap.get(sourceCacheKeys[i]),\n );\n\n if (uncached.length === 0) {\n sendEvent(\"summary\", { cached, failed, skipped, total });\n return;\n }\n\n // Keepalive interval for long-running fetches\n const keepalive = setInterval(() => {\n sendEvent(\"keepalive\", { cached, failed, skipped, total });\n }, 25_000);\n\n // Fetch uncached sources with concurrency limit\n const queue = [...uncached];\n const workers = Array.from(\n { length: Math.min(ctx.warmupConcurrency, queue.length) },\n async () => {\n while (queue.length > 0) {\n const source = queue.shift()!;\n const cacheKey = storeKeys.source(source.url);\n const startTime = Date.now();\n\n try {\n // Check if another worker is already fetching this\n let fetchPromise = inFlightFetches.get(cacheKey);\n if (!fetchPromise) {\n fetchPromise = fetchAndCache(source.url, cacheKey, sendEvent, ctx);\n inFlightFetches.set(cacheKey, fetchPromise);\n }\n\n await fetchPromise;\n inFlightFetches.delete(cacheKey);\n\n cached++;\n sendEvent(\"progress\", {\n url: source.url,\n status: \"cached\",\n cached,\n failed,\n skipped,\n total,\n ms: Date.now() - startTime,\n });\n } catch (error) {\n inFlightFetches.delete(cacheKey);\n failed++;\n sendEvent(\"progress\", {\n url: source.url,\n status: \"error\",\n error: String(error),\n cached,\n failed,\n skipped,\n total,\n ms: Date.now() - startTime,\n });\n }\n }\n },\n );\n\n await Promise.all(workers);\n clearInterval(keepalive);\n\n sendEvent(\"summary\", { cached, failed, skipped, total });\n}\n\n/**\n * Fetch a source and cache it, with streaming progress events\n */\nexport async function fetchAndCache(\n url: string,\n cacheKey: string,\n sendEvent: SSEEventSender,\n ctx: ServerContext,\n): Promise<void> {\n const response = await ffsFetch(url, {\n headersTimeout: 10 * 60 * 1000, // 10 minutes\n bodyTimeout: 20 * 60 * 1000, // 20 minutes\n });\n\n if (!response.ok) {\n throw new Error(`${response.status} ${response.statusText}`);\n }\n\n sendEvent(\"downloading\", { url, status: \"started\", bytesReceived: 0 });\n\n // Stream through a progress tracker\n const sourceStream = Readable.fromWeb(\n response.body as import(\"stream/web\").ReadableStream,\n );\n\n let totalBytes = 0;\n let lastEventTime = Date.now();\n const PROGRESS_INTERVAL = 10_000; // 10 seconds\n\n const progressStream = new Transform({\n transform(chunk, _encoding, callback) {\n totalBytes += chunk.length;\n const now = Date.now();\n if (now - lastEventTime >= PROGRESS_INTERVAL) {\n sendEvent(\"downloading\", {\n url,\n status: \"downloading\",\n bytesReceived: totalBytes,\n });\n lastEventTime = now;\n }\n callback(null, chunk);\n },\n });\n\n // Pipe through progress tracker to cache storage with source TTL\n const trackedStream = sourceStream.pipe(progressStream);\n await ctx.transientStore.put(\n cacheKey,\n trackedStream,\n ctx.transientStore.sourceTtlMs,\n );\n}\n","import express from \"express\";\nimport { randomUUID } from \"crypto\";\nimport type { Response as UndiciResponse } from \"undici\";\nimport { storeKeys } from \"../storage\";\nimport { ffsFetch } from \"../fetch\";\nimport { extractEffieSourcesWithTypes, effieDataSchema } from \"@effing/effie\";\nimport type { EffieData, EffieSources } from \"@effing/effie\";\nimport type {\n ServerContext,\n SSEEventSender,\n WarmupAndRenderJob,\n RenderJob,\n UploadOptions,\n} from \"./shared\";\nimport {\n setupCORSHeaders,\n setupSSEResponse,\n createSSEEventSender,\n} from \"./shared\";\nimport { warmupSources } from \"./caching\";\nimport { renderAndUploadInternal } from \"./rendering\";\n\n/**\n * POST /warmup-and-render - Create a combined warmup and render job\n * Returns a job ID and URL for SSE streaming\n */\nexport async function createWarmupAndRenderJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n // Parse request body\n const options = req.body as {\n effie: unknown;\n scale?: number;\n upload?: UploadOptions;\n };\n\n let rawEffieData: unknown;\n if (typeof options.effie === \"string\") {\n // Effie is a URL to fetch the EffieData from\n const response = await ffsFetch(options.effie);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch Effie data: ${response.status} ${response.statusText}`,\n );\n }\n rawEffieData = await response.json();\n } else {\n rawEffieData = options.effie;\n }\n\n // Validate/parse effie data\n let effie: EffieData<EffieSources>;\n if (!ctx.skipValidation) {\n const result = effieDataSchema.safeParse(rawEffieData);\n if (!result.success) {\n res.status(400).json({\n error: \"Invalid effie data\",\n issues: result.error.issues.map((issue) => ({\n path: issue.path.join(\".\"),\n message: issue.message,\n })),\n });\n return;\n }\n effie = result.data;\n } else {\n const data = rawEffieData as EffieData<EffieSources>;\n if (!data?.segments) {\n res.status(400).json({ error: \"Invalid effie data: missing segments\" });\n return;\n }\n effie = data;\n }\n\n const sources = extractEffieSourcesWithTypes(effie);\n const scale = options.scale ?? 1;\n const upload = options.upload;\n\n // Create IDs for warmup and render sub-jobs\n const jobId = randomUUID();\n const warmupJobId = randomUUID();\n const renderJobId = randomUUID();\n\n // Store the combined job\n const job: WarmupAndRenderJob = {\n effie,\n sources,\n scale,\n upload,\n warmupJobId,\n renderJobId,\n createdAt: Date.now(),\n };\n\n await ctx.transientStore.putJson(\n storeKeys.warmupAndRenderJob(jobId),\n job,\n ctx.transientStore.jobMetadataTtlMs,\n );\n\n // Also store sub-jobs for backend execution\n await ctx.transientStore.putJson(\n storeKeys.warmupJob(warmupJobId),\n { sources },\n ctx.transientStore.jobMetadataTtlMs,\n );\n await ctx.transientStore.putJson(\n storeKeys.renderJob(renderJobId),\n {\n effie,\n scale,\n upload,\n createdAt: Date.now(),\n } satisfies RenderJob,\n ctx.transientStore.jobMetadataTtlMs,\n );\n\n res.json({\n id: jobId,\n url: `${ctx.baseUrl}/warmup-and-render/${jobId}`,\n });\n } catch (error) {\n console.error(\"Error creating warmup-and-render job:\", error);\n res.status(500).json({ error: \"Failed to create warmup-and-render job\" });\n }\n}\n\n/**\n * GET /warmup-and-render/:id - Stream warmup and render progress via SSE\n * Orchestrates warmup (local or remote) followed by render (local or remote)\n */\nexport async function streamWarmupAndRenderJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n setupCORSHeaders(res);\n\n const jobId = req.params.id;\n const jobCacheKey = storeKeys.warmupAndRenderJob(jobId);\n const job =\n await ctx.transientStore.getJson<WarmupAndRenderJob>(jobCacheKey);\n // Only allow the job to run once\n ctx.transientStore.delete(jobCacheKey);\n\n if (!job) {\n res.status(404).json({ error: \"Job not found\" });\n return;\n }\n\n setupSSEResponse(res);\n const sendEvent = createSSEEventSender(res);\n\n // Keepalive interval for long-running operations\n let keepalivePhase: \"warmup\" | \"render\" = \"warmup\";\n const keepalive = setInterval(() => {\n sendEvent(\"keepalive\", { phase: keepalivePhase });\n }, 25_000);\n\n try {\n // Phase 1: Warmup\n if (ctx.warmupBackendBaseUrl) {\n // Proxy warmup from remote backend\n await proxyRemoteSSE(\n `${ctx.warmupBackendBaseUrl}/warmup/${job.warmupJobId}`,\n sendEvent,\n \"warmup:\",\n res,\n );\n } else {\n // Local warmup execution\n const warmupSender = prefixEventSender(sendEvent, \"warmup:\");\n await warmupSources(job.sources, warmupSender, ctx);\n warmupSender(\"complete\", { status: \"ready\" });\n }\n\n // Phase 2: Render\n keepalivePhase = \"render\";\n\n if (ctx.renderBackendBaseUrl) {\n // Proxy render from remote backend\n await proxyRemoteSSE(\n `${ctx.renderBackendBaseUrl}/render/${job.renderJobId}`,\n sendEvent,\n \"render:\",\n res,\n );\n } else {\n // Local render execution\n const renderSender = prefixEventSender(sendEvent, \"render:\");\n\n if (job.upload) {\n // Upload mode: render and upload, emit SSE events\n renderSender(\"started\", { status: \"rendering\" });\n const timings = await renderAndUploadInternal(\n job.effie,\n job.scale,\n job.upload,\n renderSender,\n ctx,\n );\n renderSender(\"complete\", { status: \"uploaded\", timings });\n } else {\n // Non-upload mode: return URL to existing render job\n const videoUrl = `${ctx.baseUrl}/render/${job.renderJobId}`;\n sendEvent(\"complete\", { status: \"ready\", videoUrl });\n }\n }\n\n // Final complete event (only for upload mode, non-upload already sent complete)\n if (job.upload && !ctx.renderBackendBaseUrl) {\n sendEvent(\"complete\", { status: \"done\" });\n }\n } catch (error) {\n sendEvent(\"error\", {\n phase: keepalivePhase,\n message: String(error),\n });\n } finally {\n clearInterval(keepalive);\n res.end();\n }\n } catch (error) {\n console.error(\"Error in warmup-and-render streaming:\", error);\n if (!res.headersSent) {\n res.status(500).json({ error: \"Warmup-and-render streaming failed\" });\n } else {\n res.end();\n }\n }\n}\n\n/**\n * Create a prefixed event sender that adds a prefix to event names\n */\nexport function prefixEventSender(\n sendEvent: SSEEventSender,\n prefix: string,\n): SSEEventSender {\n return (event: string, data: object) => {\n sendEvent(`${prefix}${event}`, data);\n };\n}\n\n/**\n * Proxy SSE events from a remote backend, prefixing event names\n */\nexport async function proxyRemoteSSE(\n url: string,\n sendEvent: SSEEventSender,\n prefix: string,\n res: express.Response,\n): Promise<void> {\n const response = await ffsFetch(url, {\n headers: {\n Accept: \"text/event-stream\",\n },\n });\n\n if (!response.ok) {\n throw new Error(`Remote backend error: ${response.status}`);\n }\n\n const reader = response.body?.getReader();\n if (!reader) {\n throw new Error(\"No response body from remote backend\");\n }\n\n const decoder = new TextDecoder();\n let buffer = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n // Check if client disconnected\n if (res.destroyed) {\n reader.cancel();\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n // Parse SSE events from buffer\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop() || \"\"; // Keep incomplete line in buffer\n\n let currentEvent = \"\";\n let currentData = \"\";\n\n for (const line of lines) {\n if (line.startsWith(\"event: \")) {\n currentEvent = line.slice(7);\n } else if (line.startsWith(\"data: \")) {\n currentData = line.slice(6);\n } else if (line === \"\" && currentEvent && currentData) {\n // End of event, forward it with prefix\n try {\n const data = JSON.parse(currentData);\n sendEvent(`${prefix}${currentEvent}`, data);\n } catch {\n // Skip malformed JSON\n }\n currentEvent = \"\";\n currentData = \"\";\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n}\n\n/**\n * Proxy a binary stream (e.g., video) from a fetch Response to an Express response.\n * Forwards Content-Type and Content-Length headers.\n */\nexport async function proxyBinaryStream(\n response: UndiciResponse,\n res: express.Response,\n): Promise<void> {\n const contentType = response.headers.get(\"content-type\");\n if (contentType) res.set(\"Content-Type\", contentType);\n\n const contentLength = response.headers.get(\"content-length\");\n if (contentLength) res.set(\"Content-Length\", contentLength);\n\n const reader = response.body?.getReader();\n if (!reader) {\n throw new Error(\"No response body\");\n }\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n if (res.destroyed) {\n reader.cancel();\n break;\n }\n\n res.write(value);\n }\n } finally {\n reader.releaseLock();\n res.end();\n }\n}\n","import express from \"express\";\nimport { randomUUID } from \"crypto\";\nimport { storeKeys } from \"../storage\";\nimport { ffsFetch } from \"../fetch\";\nimport { EffieRenderer } from \"../render\";\nimport { effieDataSchema } from \"@effing/effie\";\nimport type { EffieData, EffieSources } from \"@effing/effie\";\nimport type {\n ServerContext,\n SSEEventSender,\n RenderJob,\n UploadOptions,\n} from \"./shared\";\nimport {\n setupCORSHeaders,\n setupSSEResponse,\n createSSEEventSender,\n} from \"./shared\";\nimport { proxyBinaryStream } from \"./orchestrating\";\n\n/**\n * POST /render - Create a render job\n * Returns a job ID and URL for streaming the rendered video\n */\nexport async function createRenderJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n // Wrapped format has `effie` property,\n // otherwise it's just raw EffieData (which doesn't have an `effie` property)\n const isWrapped = \"effie\" in req.body;\n\n let rawEffieData: unknown;\n let scale: number;\n let upload: UploadOptions | undefined;\n\n if (isWrapped) {\n // Wrapped format: { effie: EffieData | string, scale?, upload? }\n const options = req.body as {\n effie: unknown;\n scale?: number;\n upload?: UploadOptions;\n };\n\n if (typeof options.effie === \"string\") {\n // Effie is a string, so it's a URL to fetch the EffieData from\n const response = await ffsFetch(options.effie);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch Effie data: ${response.status} ${response.statusText}`,\n );\n }\n rawEffieData = await response.json();\n } else {\n // Effie is an EffieData object\n rawEffieData = options.effie;\n }\n\n scale = options.scale ?? 1;\n upload = options.upload;\n } else {\n // Body is the EffieData, options in query params\n rawEffieData = req.body;\n scale = parseFloat(req.query.scale?.toString() || \"1\");\n }\n\n // Validate/parse effie data (validation can be disabled by setting FFS_SKIP_VALIDATION)\n let effie: EffieData<EffieSources>;\n if (!ctx.skipValidation) {\n const result = effieDataSchema.safeParse(rawEffieData);\n if (!result.success) {\n res.status(400).json({\n error: \"Invalid effie data\",\n issues: result.error.issues.map((issue) => ({\n path: issue.path.join(\".\"),\n message: issue.message,\n })),\n });\n return;\n }\n effie = result.data;\n } else {\n // Minimal validation when schema validation is disabled\n const data = rawEffieData as EffieData<EffieSources>;\n if (!data?.segments) {\n res.status(400).json({ error: \"Invalid effie data: missing segments\" });\n return;\n }\n effie = data;\n }\n\n // Create render job\n const jobId = randomUUID();\n const job: RenderJob = {\n effie,\n scale,\n upload,\n createdAt: Date.now(),\n };\n\n await ctx.transientStore.putJson(\n storeKeys.renderJob(jobId),\n job,\n ctx.transientStore.jobMetadataTtlMs,\n );\n\n res.json({\n id: jobId,\n url: `${ctx.baseUrl}/render/${jobId}`,\n });\n } catch (error) {\n console.error(\"Error creating render job:\", error);\n res.status(500).json({ error: \"Failed to create render job\" });\n }\n}\n\n/**\n * GET /render/:id - Execute render job\n * Streams video directly (no upload) or SSE progress events (with upload)\n */\nexport async function streamRenderJob(\n req: express.Request,\n res: express.Response,\n ctx: ServerContext,\n): Promise<void> {\n try {\n setupCORSHeaders(res);\n\n const jobId = req.params.id;\n\n // Proxy to render backend if configured\n if (ctx.renderBackendBaseUrl) {\n await proxyRenderFromBackend(res, jobId, ctx);\n return;\n }\n\n const jobCacheKey = storeKeys.renderJob(jobId);\n const job = await ctx.transientStore.getJson<RenderJob>(jobCacheKey);\n // only allow the render job to run once\n ctx.transientStore.delete(jobCacheKey);\n\n if (!job) {\n res.status(404).json({ error: \"Job not found or expired\" });\n return;\n }\n\n // Dispatch based on upload mode\n if (job.upload) {\n await streamRenderWithUpload(res, job, ctx);\n } else {\n await streamRenderDirect(res, job, ctx);\n }\n } catch (error) {\n console.error(\"Error in render:\", error);\n if (!res.headersSent) {\n res.status(500).json({ error: \"Rendering failed\" });\n } else {\n res.end();\n }\n }\n}\n\n/**\n * Stream video directly to the response (no upload)\n */\nexport async function streamRenderDirect(\n res: express.Response,\n job: RenderJob,\n ctx: ServerContext,\n): Promise<void> {\n const renderer = new EffieRenderer(job.effie, {\n transientStore: ctx.transientStore,\n httpProxy: ctx.httpProxy,\n });\n const videoStream = await renderer.render(job.scale);\n\n res.on(\"close\", () => {\n videoStream.destroy();\n renderer.close();\n });\n\n res.set(\"Content-Type\", \"video/mp4\");\n videoStream.pipe(res);\n}\n\n/**\n * Render and upload, streaming SSE progress events\n */\nexport async function streamRenderWithUpload(\n res: express.Response,\n job: RenderJob,\n ctx: ServerContext,\n): Promise<void> {\n setupSSEResponse(res);\n const sendEvent = createSSEEventSender(res);\n\n // Keepalive interval for long-running renders\n const keepalive = setInterval(() => {\n sendEvent(\"keepalive\", { status: \"rendering\" });\n }, 25_000);\n\n try {\n sendEvent(\"started\", { status: \"rendering\" });\n\n const timings = await renderAndUploadInternal(\n job.effie,\n job.scale,\n job.upload!,\n sendEvent,\n ctx,\n );\n\n sendEvent(\"complete\", { status: \"uploaded\", timings });\n } catch (error) {\n sendEvent(\"error\", { message: String(error) });\n } finally {\n clearInterval(keepalive);\n res.end();\n }\n}\n\n/**\n * Internal render and upload logic\n * Returns timings for the SSE complete event\n */\nexport async function renderAndUploadInternal(\n effie: EffieData<EffieSources>,\n scale: number,\n upload: UploadOptions,\n sendEvent: SSEEventSender,\n ctx: ServerContext,\n): Promise<Record<string, number>> {\n const timings: Record<string, number> = {};\n\n // Fetch and upload cover if coverUrl provided\n if (upload.coverUrl) {\n const fetchCoverStartTime = Date.now();\n const coverFetchResponse = await ffsFetch(effie.cover);\n if (!coverFetchResponse.ok) {\n throw new Error(\n `Failed to fetch cover image: ${coverFetchResponse.status} ${coverFetchResponse.statusText}`,\n );\n }\n const coverBuffer = Buffer.from(await coverFetchResponse.arrayBuffer());\n timings.fetchCoverTime = Date.now() - fetchCoverStartTime;\n\n const uploadCoverStartTime = Date.now();\n const uploadCoverResponse = await ffsFetch(upload.coverUrl, {\n method: \"PUT\",\n body: coverBuffer,\n headers: {\n \"Content-Type\": \"image/png\",\n \"Content-Length\": coverBuffer.length.toString(),\n },\n });\n if (!uploadCoverResponse.ok) {\n throw new Error(\n `Failed to upload cover: ${uploadCoverResponse.status} ${uploadCoverResponse.statusText}`,\n );\n }\n timings.uploadCoverTime = Date.now() - uploadCoverStartTime;\n }\n\n // Render effie data to video\n const renderStartTime = Date.now();\n const renderer = new EffieRenderer(effie, {\n transientStore: ctx.transientStore,\n httpProxy: ctx.httpProxy,\n });\n const videoStream = await renderer.render(scale);\n const chunks: Buffer[] = [];\n for await (const chunk of videoStream) {\n chunks.push(Buffer.from(chunk));\n }\n const videoBuffer = Buffer.concat(chunks);\n timings.renderTime = Date.now() - renderStartTime;\n\n // Update keepalive status for upload phase\n sendEvent(\"keepalive\", { status: \"uploading\" });\n\n // Upload rendered video\n const uploadStartTime = Date.now();\n const uploadResponse = await ffsFetch(upload.videoUrl, {\n method: \"PUT\",\n body: videoBuffer,\n headers: {\n \"Content-Type\": \"video/mp4\",\n \"Content-Length\": videoBuffer.length.toString(),\n },\n });\n if (!uploadResponse.ok) {\n throw new Error(\n `Failed to upload rendered video: ${uploadResponse.status} ${uploadResponse.statusText}`,\n );\n }\n timings.uploadTime = Date.now() - uploadStartTime;\n\n return timings;\n}\n\n/**\n * Proxy render from backend based on Content-Type.\n * SSE (upload mode) uses proxyRemoteSSE, video stream uses proxyBinaryStream.\n */\nasync function proxyRenderFromBackend(\n res: express.Response,\n jobId: string,\n ctx: ServerContext,\n): Promise<void> {\n const backendUrl = `${ctx.renderBackendBaseUrl}/render/${jobId}`;\n const response = await ffsFetch(backendUrl);\n\n if (!response.ok) {\n res.status(response.status).json({ error: \"Backend render failed\" });\n return;\n }\n\n const contentType = response.headers.get(\"content-type\") || \"\";\n\n if (contentType.includes(\"text/event-stream\")) {\n // Upload mode: proxy SSE events\n setupSSEResponse(res);\n const sendEvent = createSSEEventSender(res);\n\n const reader = response.body?.getReader();\n if (!reader) {\n sendEvent(\"error\", { message: \"No response body from backend\" });\n res.end();\n return;\n }\n\n const decoder = new TextDecoder();\n let buffer = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n if (res.destroyed) {\n reader.cancel();\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop() || \"\";\n\n let currentEvent = \"\";\n let currentData = \"\";\n\n for (const line of lines) {\n if (line.startsWith(\"event: \")) {\n currentEvent = line.slice(7);\n } else if (line.startsWith(\"data: \")) {\n currentData = line.slice(6);\n } else if (line === \"\" && currentEvent && currentData) {\n try {\n const data = JSON.parse(currentData);\n sendEvent(currentEvent, data);\n } catch {\n // Skip malformed JSON\n }\n currentEvent = \"\";\n currentData = \"\";\n }\n }\n }\n } finally {\n reader.releaseLock();\n res.end();\n }\n } else {\n // Non-upload mode: proxy binary video stream\n await proxyBinaryStream(response, res);\n }\n}\n"],"mappings":";;;;;;;;AAAA,OAAoB;;;ACApB,OAAO,UAAU;AAEjB,SAAS,gBAAgB;AAel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AA,EACX;AAEA,MAAI,aAAa;AACjB,MAAI,gBAAgB,KAAK,IAAI;AAC7B,QAAM,oBAAoB;AAE1B,QAAM,iBAAiB,IAAI,UAAU;AAAA,IACnC,UAAU,OAAO,WAAW,UAAU;AACpC,oBAAc,MAAM;AACpB,YAAM,MAAM,KAAK,IAAI;AACrB,UAAI,MAAM,iBAAiB,mBAAmB;AAC5C,kBAAU,eAAe;AAAA,UACvB;AAAA,UACA,QAAQ;AAAA,UACR,eAAe;AAAA,QACjB,CAAC;AACD,wBAAgB;AAAA,MAClB;AACA,eAAS,MAAM,KAAK;AAAA,IACtB;AAAA,EACF,CAAC;AAGD,QAAM,gBAAgB,aAAa,KAAK,cAAc;AACtD,QAAM,IAAI,eAAe;AAAA,IACvB;AAAA,IACA;AAAA,IACA,IAAI,eAAe;AAAA,EACrB;AACF;","names":["Readable","randomUUID","extractEffieSourcesWithTypes","randomUUID","effieDataSchema","effieDataSchema","effieDataSchema","effieDataSchema","randomUUID","extractEffieSourcesWithTypes","randomUUID","sendEvent","Readable"]}
|
package/dist/handlers/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EffieData, EffieSources } from '@effing/effie';
|
|
1
|
+
import { T as TransientStore, H as HttpProxy } from '../proxy-qTA69nOV.js';
|
|
2
|
+
import { EffieSourceWithType, EffieData, EffieSources } from '@effing/effie';
|
|
3
3
|
import express from 'express';
|
|
4
|
+
import { Response } from 'undici';
|
|
4
5
|
import 'stream';
|
|
5
6
|
|
|
6
7
|
type UploadOptions = {
|
|
@@ -8,7 +9,7 @@ type UploadOptions = {
|
|
|
8
9
|
coverUrl?: string;
|
|
9
10
|
};
|
|
10
11
|
type WarmupJob = {
|
|
11
|
-
sources:
|
|
12
|
+
sources: EffieSourceWithType[];
|
|
12
13
|
};
|
|
13
14
|
type RenderJob = {
|
|
14
15
|
effie: EffieData<EffieSources>;
|
|
@@ -16,17 +17,29 @@ type RenderJob = {
|
|
|
16
17
|
upload?: UploadOptions;
|
|
17
18
|
createdAt: number;
|
|
18
19
|
};
|
|
20
|
+
type WarmupAndRenderJob = {
|
|
21
|
+
effie: EffieData<EffieSources>;
|
|
22
|
+
sources: EffieSourceWithType[];
|
|
23
|
+
scale: number;
|
|
24
|
+
upload?: UploadOptions;
|
|
25
|
+
warmupJobId: string;
|
|
26
|
+
renderJobId: string;
|
|
27
|
+
createdAt: number;
|
|
28
|
+
};
|
|
19
29
|
type ServerContext = {
|
|
20
|
-
|
|
30
|
+
transientStore: TransientStore;
|
|
31
|
+
httpProxy: HttpProxy;
|
|
21
32
|
baseUrl: string;
|
|
22
33
|
skipValidation: boolean;
|
|
23
|
-
|
|
34
|
+
warmupConcurrency: number;
|
|
35
|
+
warmupBackendBaseUrl?: string;
|
|
36
|
+
renderBackendBaseUrl?: string;
|
|
24
37
|
};
|
|
25
38
|
type SSEEventSender = (event: string, data: object) => void;
|
|
26
39
|
/**
|
|
27
40
|
* Create the server context with configuration from environment variables
|
|
28
41
|
*/
|
|
29
|
-
declare function createServerContext(): ServerContext
|
|
42
|
+
declare function createServerContext(): Promise<ServerContext>;
|
|
30
43
|
|
|
31
44
|
/**
|
|
32
45
|
* POST /warmup - Create a warmup job
|
|
@@ -54,4 +67,24 @@ declare function createRenderJob(req: express.Request, res: express.Response, ct
|
|
|
54
67
|
*/
|
|
55
68
|
declare function streamRenderJob(req: express.Request, res: express.Response, ctx: ServerContext): Promise<void>;
|
|
56
69
|
|
|
57
|
-
|
|
70
|
+
/**
|
|
71
|
+
* POST /warmup-and-render - Create a combined warmup and render job
|
|
72
|
+
* Returns a job ID and URL for SSE streaming
|
|
73
|
+
*/
|
|
74
|
+
declare function createWarmupAndRenderJob(req: express.Request, res: express.Response, ctx: ServerContext): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* GET /warmup-and-render/:id - Stream warmup and render progress via SSE
|
|
77
|
+
* Orchestrates warmup (local or remote) followed by render (local or remote)
|
|
78
|
+
*/
|
|
79
|
+
declare function streamWarmupAndRenderJob(req: express.Request, res: express.Response, ctx: ServerContext): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Proxy SSE events from a remote backend, prefixing event names
|
|
82
|
+
*/
|
|
83
|
+
declare function proxyRemoteSSE(url: string, sendEvent: SSEEventSender, prefix: string, res: express.Response): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Proxy a binary stream (e.g., video) from a fetch Response to an Express response.
|
|
86
|
+
* Forwards Content-Type and Content-Length headers.
|
|
87
|
+
*/
|
|
88
|
+
declare function proxyBinaryStream(response: Response, res: express.Response): Promise<void>;
|
|
89
|
+
|
|
90
|
+
export { type RenderJob, type SSEEventSender, type ServerContext, type UploadOptions, type WarmupAndRenderJob, type WarmupJob, createRenderJob, createServerContext, createWarmupAndRenderJob, createWarmupJob, proxyBinaryStream, proxyRemoteSSE, purgeCache, streamRenderJob, streamWarmupAndRenderJob, streamWarmupJob };
|
package/dist/handlers/index.js
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createRenderJob,
|
|
3
3
|
createServerContext,
|
|
4
|
+
createWarmupAndRenderJob,
|
|
4
5
|
createWarmupJob,
|
|
6
|
+
proxyBinaryStream,
|
|
7
|
+
proxyRemoteSSE,
|
|
5
8
|
purgeCache,
|
|
6
9
|
streamRenderJob,
|
|
10
|
+
streamWarmupAndRenderJob,
|
|
7
11
|
streamWarmupJob
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-
|
|
12
|
+
} from "../chunk-XSCNUWZJ.js";
|
|
13
|
+
import "../chunk-J64HSZNQ.js";
|
|
10
14
|
export {
|
|
11
15
|
createRenderJob,
|
|
12
16
|
createServerContext,
|
|
17
|
+
createWarmupAndRenderJob,
|
|
13
18
|
createWarmupJob,
|
|
19
|
+
proxyBinaryStream,
|
|
20
|
+
proxyRemoteSSE,
|
|
14
21
|
purgeCache,
|
|
15
22
|
streamRenderJob,
|
|
23
|
+
streamWarmupAndRenderJob,
|
|
16
24
|
streamWarmupJob
|
|
17
25
|
};
|
|
18
26
|
//# sourceMappingURL=index.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Readable } from 'stream';
|
|
2
|
-
import { EffieWebUrl, EffieData, EffieSources
|
|
3
|
-
import {
|
|
2
|
+
import { EffieWebUrl, EffieData, EffieSources } from '@effing/effie';
|
|
3
|
+
import { T as TransientStore, H as HttpProxy } from './proxy-qTA69nOV.js';
|
|
4
4
|
|
|
5
5
|
type EffieRendererOptions = {
|
|
6
6
|
/**
|
|
@@ -11,16 +11,24 @@ type EffieRendererOptions = {
|
|
|
11
11
|
*/
|
|
12
12
|
allowLocalFiles?: boolean;
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
* If not provided,
|
|
14
|
+
* Transient store instance for source lookups.
|
|
15
|
+
* If not provided, sources will be fetched directly from network.
|
|
16
16
|
*/
|
|
17
|
-
|
|
17
|
+
transientStore?: TransientStore;
|
|
18
|
+
/**
|
|
19
|
+
* HTTP proxy for video/audio URLs.
|
|
20
|
+
* When provided, HTTP(S) URLs for video/audio inputs will be routed
|
|
21
|
+
* through this proxy, allowing Node.js to handle DNS resolution
|
|
22
|
+
* instead of FFmpeg (useful for Alpine Linux with musl libc).
|
|
23
|
+
*/
|
|
24
|
+
httpProxy?: HttpProxy;
|
|
18
25
|
};
|
|
19
26
|
declare class EffieRenderer<U extends string = EffieWebUrl> {
|
|
20
27
|
private effieData;
|
|
21
28
|
private ffmpegRunner?;
|
|
22
29
|
private allowLocalFiles;
|
|
23
|
-
private
|
|
30
|
+
private transientStore?;
|
|
31
|
+
private httpProxy?;
|
|
24
32
|
constructor(effieData: EffieData<EffieSources<U>, U>, options?: EffieRendererOptions);
|
|
25
33
|
private fetchSource;
|
|
26
34
|
private buildAudioFilter;
|
|
@@ -60,6 +68,12 @@ declare class EffieRenderer<U extends string = EffieWebUrl> {
|
|
|
60
68
|
private applyGeneralAudio;
|
|
61
69
|
private buildFFmpegCommand;
|
|
62
70
|
private createImageTransformer;
|
|
71
|
+
/**
|
|
72
|
+
* Resolves a source reference to its actual URL.
|
|
73
|
+
* If the source is a #reference, returns the resolved URL.
|
|
74
|
+
* Otherwise, returns the source as-is.
|
|
75
|
+
*/
|
|
76
|
+
private resolveReference;
|
|
63
77
|
/**
|
|
64
78
|
* Renders the effie data to a video stream.
|
|
65
79
|
* @param scaleFactor - Scale factor for output dimensions
|
|
@@ -90,17 +104,11 @@ declare class FFmpegRunner {
|
|
|
90
104
|
private command;
|
|
91
105
|
private ffmpegProc?;
|
|
92
106
|
constructor(command: FFmpegCommand);
|
|
93
|
-
run(
|
|
107
|
+
run(sourceFetcher: (input: {
|
|
94
108
|
type: FFmpegInput["type"];
|
|
95
109
|
src: string;
|
|
96
|
-
}) => Promise<Readable>, imageTransformer?: (imageStream: Readable) => Promise<Readable
|
|
110
|
+
}) => Promise<Readable>, imageTransformer?: (imageStream: Readable) => Promise<Readable>, referenceResolver?: (src: string) => string, urlTransformer?: (url: string) => string): Promise<Readable>;
|
|
97
111
|
close(): void;
|
|
98
112
|
}
|
|
99
113
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
declare function processEffects(effects: EffieEffect[] | undefined, frameRate: number, frameWidth: number, frameHeight: number): string;
|
|
103
|
-
|
|
104
|
-
declare function processTransition(transition: EffieTransition): string;
|
|
105
|
-
|
|
106
|
-
export { EffieRenderer, type EffieRendererOptions, FFmpegCommand, type FFmpegInput, FFmpegRunner, processEffects, processMotion, processTransition };
|
|
114
|
+
export { EffieRenderer, type EffieRendererOptions, FFmpegCommand, type FFmpegInput, FFmpegRunner };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EffieRenderer,
|
|
3
3
|
FFmpegCommand,
|
|
4
|
-
FFmpegRunner
|
|
5
|
-
|
|
6
|
-
processMotion,
|
|
7
|
-
processTransition
|
|
8
|
-
} from "./chunk-RNE6TKMF.js";
|
|
4
|
+
FFmpegRunner
|
|
5
|
+
} from "./chunk-J64HSZNQ.js";
|
|
9
6
|
export {
|
|
10
7
|
EffieRenderer,
|
|
11
8
|
FFmpegCommand,
|
|
12
|
-
FFmpegRunner
|
|
13
|
-
processEffects,
|
|
14
|
-
processMotion,
|
|
15
|
-
processTransition
|
|
9
|
+
FFmpegRunner
|
|
16
10
|
};
|
|
17
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transient store interface for caching sources and storing ephemeral job metadata.
|
|
5
|
+
*/
|
|
6
|
+
interface TransientStore {
|
|
7
|
+
/** TTL for cached sources in milliseconds */
|
|
8
|
+
readonly sourceTtlMs: number;
|
|
9
|
+
/** TTL for job metadata in milliseconds */
|
|
10
|
+
readonly jobMetadataTtlMs: number;
|
|
11
|
+
/** Store a stream with the given key and optional TTL override */
|
|
12
|
+
put(key: string, stream: Readable, ttlMs?: number): Promise<void>;
|
|
13
|
+
/** Get a stream for the given key, or null if not found */
|
|
14
|
+
getStream(key: string): Promise<Readable | null>;
|
|
15
|
+
/** Check if a key exists */
|
|
16
|
+
exists(key: string): Promise<boolean>;
|
|
17
|
+
/** Check if multiple keys exist (batch operation) */
|
|
18
|
+
existsMany(keys: string[]): Promise<Map<string, boolean>>;
|
|
19
|
+
/** Delete a key */
|
|
20
|
+
delete(key: string): Promise<void>;
|
|
21
|
+
/** Store JSON data with optional TTL override */
|
|
22
|
+
putJson(key: string, data: object, ttlMs?: number): Promise<void>;
|
|
23
|
+
/** Get JSON data, or null if not found */
|
|
24
|
+
getJson<T>(key: string): Promise<T | null>;
|
|
25
|
+
/** Close and cleanup resources */
|
|
26
|
+
close(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* HTTP proxy for FFmpeg URL handling.
|
|
31
|
+
*
|
|
32
|
+
* Static FFmpeg binaries can have DNS resolution issues on Alpine Linux (musl libc).
|
|
33
|
+
* This proxy lets Node.js handle DNS lookups instead of FFmpeg by proxying HTTP
|
|
34
|
+
* requests through localhost.
|
|
35
|
+
*
|
|
36
|
+
* URL scheme (M3U8-compatible):
|
|
37
|
+
* - Original: https://cdn.example.com/path/to/stream.m3u8
|
|
38
|
+
* - Proxy: http://127.0.0.1:{port}/https://cdn.example.com/path/to/stream.m3u8
|
|
39
|
+
* - Relative: segment-0.ts → http://127.0.0.1:{port}/https://cdn.example.com/path/to/segment-0.ts
|
|
40
|
+
*/
|
|
41
|
+
declare class HttpProxy {
|
|
42
|
+
private server;
|
|
43
|
+
private _port;
|
|
44
|
+
private startPromise;
|
|
45
|
+
get port(): number | null;
|
|
46
|
+
/**
|
|
47
|
+
* Transform a URL to go through the proxy.
|
|
48
|
+
* @throws Error if proxy not started
|
|
49
|
+
*/
|
|
50
|
+
transformUrl(url: string): string;
|
|
51
|
+
/**
|
|
52
|
+
* Start the proxy server. Safe to call multiple times.
|
|
53
|
+
*/
|
|
54
|
+
start(): Promise<void>;
|
|
55
|
+
private doStart;
|
|
56
|
+
/**
|
|
57
|
+
* Parse the proxy path to extract the original URL.
|
|
58
|
+
* Path format: /{originalUrl}
|
|
59
|
+
*/
|
|
60
|
+
private parseProxyPath;
|
|
61
|
+
/**
|
|
62
|
+
* Filter headers to forward to the upstream server.
|
|
63
|
+
* Removes hop-by-hop headers that shouldn't be forwarded.
|
|
64
|
+
*/
|
|
65
|
+
private filterHeaders;
|
|
66
|
+
/**
|
|
67
|
+
* Close the proxy server and reset state.
|
|
68
|
+
*/
|
|
69
|
+
close(): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { HttpProxy as H, type TransientStore as T };
|