mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
- imageHelper.encodeDataUri(mime, base64) replaces 5 inline `data:${m};base64,${d}` templates
- Applied to gemini/claude/antigravity request + gemini response translators
- Golden tests pass, identical output
Co-authored-by: Cursor <cursoragent@cursor.com>
40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
// Build a base64 data URI from mime + base64 payload
|
|
export function encodeDataUri(mimeType, base64) {
|
|
return `data:${mimeType};base64,${base64}`;
|
|
}
|
|
|
|
/**
|
|
* Fetch a remote image URL and return it as a base64 data URI.
|
|
* Used when upstream providers (Codex, etc.) require inline base64 images
|
|
* instead of remote URLs they cannot fetch.
|
|
* Returns null if fetch fails.
|
|
*
|
|
* @param {string} imageUrl - HTTP(S) URL of the image
|
|
* @param {object} options - { signal, timeoutMs }
|
|
* @returns {Promise<{url: string, mimeType: string}|null>}
|
|
*/
|
|
export async function fetchImageAsBase64(imageUrl, options = {}) {
|
|
const { signal, timeoutMs = 10000 } = options;
|
|
if (!imageUrl || (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))) {
|
|
return null;
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
const timeout = signal ? null : setTimeout(() => controller.abort(), timeoutMs);
|
|
const fetchSignal = signal || controller.signal;
|
|
|
|
try {
|
|
const response = await fetch(imageUrl, { signal: fetchSignal });
|
|
if (!response.ok) return null;
|
|
|
|
const mimeType = response.headers.get("Content-Type") || "image/jpeg";
|
|
const arrayBuffer = await response.arrayBuffer();
|
|
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
|
return { url: `data:${mimeType};base64,${base64}`, mimeType };
|
|
} catch {
|
|
return null;
|
|
} finally {
|
|
if (timeout) clearTimeout(timeout);
|
|
}
|
|
}
|