Sync snapshot (2026-08-05) (#34)
- Add a user-selectable update channel (Stable/Beta) with a manual promote-to-stable release flow - PDF: image signatures and WYSIWYG typed signatures with better placement, plus a status bar, keyboard navigation, and two-page spread reading upgrades - Docs: Word-parity ribbon font controls and style gallery, an AI Format one-click preset, and restored headers/footers and TOC formatting for Pages-style docx - Add GenOffice device-code sign-in for AI features - Refresh UI: AI panel and ribbon styling aligned with the design system, new icons, save toasts, and content-width status bars beside a full-height AI dock - AI reliability: retry transient image-download failures, detect empty gateway streams without killing long generations, and forward the resolved proxy to spawned CLI children - Slides: bullet list gallery, type-ahead font filtering, and shorter clarify buttons across locales - Add a Linux (AppImage) packaging target Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
3a440f7944
commit
d1de6ac44b
@@ -20,3 +20,4 @@ export {
|
||||
isSafeRemoteUrl,
|
||||
type FetchWithSsrfGuardOptions,
|
||||
} from './safe-remote-url'
|
||||
export { fetchRemoteImage, remoteImageHeaders } from './remote-image'
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/// Downloader for AI-inserted images. Image-search results largely live on the
|
||||
/// Genspark CDN (sspark.genspark.ai), which intermittently refuses bare
|
||||
/// requests; browser-like headers plus a Referer on genspark hosts and a couple
|
||||
/// of retries turn most of those transient failures into successful inserts.
|
||||
|
||||
import { fetchWithSsrfGuard, type FetchWithSsrfGuardOptions } from './safe-remote-url'
|
||||
|
||||
const RETRY_DELAYS_MS: readonly number[] = [500, 1500]
|
||||
|
||||
export function remoteImageHeaders(rawUrl: string): Record<string, string> {
|
||||
const headers: Record<string, string> = {
|
||||
'User-Agent': 'Mozilla/5.0',
|
||||
// Only advertise formats the insert pipelines can label correctly: callers
|
||||
// map non-png/gif responses to JPEG, so preferring avif/webp would invite
|
||||
// content-negotiating CDNs to send bytes that end up mislabeled.
|
||||
Accept: 'image/png,image/jpeg,image/gif,image/*;q=0.8,*/*;q=0.5',
|
||||
}
|
||||
try {
|
||||
const host = new URL(rawUrl).hostname.toLowerCase()
|
||||
if (host === 'genspark.ai' || host.endsWith('.genspark.ai')) {
|
||||
headers.Referer = 'https://www.genspark.ai/'
|
||||
}
|
||||
} catch {
|
||||
/* fetchWithSsrfGuard rejects unparseable URLs on its own */
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
/**
|
||||
* fetchWithSsrfGuard specialized for image downloads: browser-like headers
|
||||
* (with a Referer for the Genspark CDN) and retries on transient failures
|
||||
* (network errors, 403/408/429, 5xx). An SSRF-blocked URL still returns null
|
||||
* immediately — that outcome never changes on retry.
|
||||
*/
|
||||
export async function fetchRemoteImage(
|
||||
rawUrl: string,
|
||||
options: Pick<FetchWithSsrfGuardOptions, 'fetchImpl'> & {
|
||||
retryDelaysMs?: readonly number[]
|
||||
} = {},
|
||||
): Promise<Response | null> {
|
||||
const { retryDelaysMs = RETRY_DELAYS_MS, ...guardOptions } = options
|
||||
const headers = remoteImageHeaders(rawUrl)
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
let resp: Response | null = null
|
||||
let threw = false
|
||||
try {
|
||||
resp = await fetchWithSsrfGuard(rawUrl, { ...guardOptions, headers })
|
||||
} catch {
|
||||
threw = true
|
||||
}
|
||||
if (resp?.ok) return resp
|
||||
if (resp === null && !threw) return null // blocked by the SSRF guard: permanent
|
||||
const transient =
|
||||
threw ||
|
||||
(resp !== null &&
|
||||
(resp.status === 403 || resp.status === 408 || resp.status === 429 || resp.status >= 500))
|
||||
const delay = retryDelaysMs[attempt]
|
||||
if (!transient || delay === undefined) return resp
|
||||
await new Promise((r) => setTimeout(r, delay))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { fetchRemoteImage, remoteImageHeaders } from '../src/remote-image'
|
||||
|
||||
const png = () => new Response('img', { status: 200 })
|
||||
|
||||
describe('remoteImageHeaders', () => {
|
||||
it('sends a Referer for genspark hosts', () => {
|
||||
expect(remoteImageHeaders('https://sspark.genspark.ai/a.png').Referer).toBe(
|
||||
'https://www.genspark.ai/',
|
||||
)
|
||||
expect(remoteImageHeaders('https://genspark.ai/a.png').Referer).toBe('https://www.genspark.ai/')
|
||||
})
|
||||
|
||||
it('sends no Referer for other hosts (including lookalikes)', () => {
|
||||
expect(remoteImageHeaders('https://example.com/a.png').Referer).toBeUndefined()
|
||||
expect(remoteImageHeaders('https://evilgenspark.ai/a.png').Referer).toBeUndefined()
|
||||
})
|
||||
|
||||
it('always sends a browser-like User-Agent and image Accept', () => {
|
||||
const headers = remoteImageHeaders('https://example.com/a.png')
|
||||
expect(headers['User-Agent']).toBeTruthy()
|
||||
expect(headers.Accept).toContain('image/')
|
||||
})
|
||||
|
||||
it('does not advertise formats the insert pipelines mislabel (avif/webp)', () => {
|
||||
const accept = remoteImageHeaders('https://example.com/a.png').Accept!
|
||||
expect(accept).not.toContain('avif')
|
||||
expect(accept).not.toContain('webp')
|
||||
})
|
||||
})
|
||||
|
||||
describe('fetchRemoteImage', () => {
|
||||
it('returns the response on first success', async () => {
|
||||
const fetchImpl = vi.fn().mockResolvedValue(png())
|
||||
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
|
||||
fetchImpl,
|
||||
retryDelaysMs: [0, 0],
|
||||
})
|
||||
expect(resp?.ok).toBe(true)
|
||||
expect(fetchImpl).toHaveBeenCalledTimes(1)
|
||||
const headers = fetchImpl.mock.calls[0]![1].headers as Record<string, string>
|
||||
expect(headers.Referer).toBe('https://www.genspark.ai/')
|
||||
})
|
||||
|
||||
it('retries transient statuses until success', async () => {
|
||||
const fetchImpl = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(new Response('nope', { status: 503 }))
|
||||
.mockResolvedValueOnce(new Response('nope', { status: 403 }))
|
||||
.mockResolvedValueOnce(png())
|
||||
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
|
||||
fetchImpl,
|
||||
retryDelaysMs: [0, 0],
|
||||
})
|
||||
expect(resp?.ok).toBe(true)
|
||||
expect(fetchImpl).toHaveBeenCalledTimes(3)
|
||||
})
|
||||
|
||||
it('retries network errors and returns null when the budget is exhausted', async () => {
|
||||
const fetchImpl = vi.fn().mockRejectedValue(new Error('ECONNRESET'))
|
||||
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
|
||||
fetchImpl,
|
||||
retryDelaysMs: [0],
|
||||
})
|
||||
expect(resp).toBeNull()
|
||||
expect(fetchImpl).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('does not retry permanent statuses like 404', async () => {
|
||||
const fetchImpl = vi.fn().mockResolvedValue(new Response('gone', { status: 404 }))
|
||||
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
|
||||
fetchImpl,
|
||||
retryDelaysMs: [0, 0],
|
||||
})
|
||||
expect(resp?.status).toBe(404)
|
||||
expect(fetchImpl).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('does not retry SSRF-blocked URLs', async () => {
|
||||
const fetchImpl = vi.fn()
|
||||
const resp = await fetchRemoteImage('https://127.0.0.1/a.png', {
|
||||
fetchImpl,
|
||||
retryDelaysMs: [0, 0],
|
||||
})
|
||||
expect(resp).toBeNull()
|
||||
expect(fetchImpl).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user