Skip to content

armgit / fx0

Public Imported from GitHub
0

A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.

TypeScript

Count every embedded copy of an image against the size budget

Esteban Suarez00832ed38624
diff --git a/lib/design-artifact.ts b/lib/design-artifact.ts
index 0328015..394d0e2 100644
--- a/lib/design-artifact.ts
+++ b/lib/design-artifact.ts
@@ -352,9 +352,15 @@ export function rewriteRemoteImages(html: string, embed: (url: string) => string
for (const edit of cssUrlEdits(html, tag.content, false)) edits.push({ ...edit, text: `url(${image(edit.url)})` })
}
}
- let result = html
- for (const edit of edits.sort((a, b) => b.start - a.start)) result = result.slice(0, edit.start) + edit.text + result.slice(edit.end)
- return result
+ // One pass, so the cost grows with the output, not with the number of edits.
+ const parts: string[] = []
+ let cursor = 0
+ for (const edit of edits.sort((a, b) => a.start - b.start)) {
+ parts.push(html.slice(cursor, edit.start), edit.text)
+ cursor = edit.end
+ }
+ parts.push(html.slice(cursor))
+ return parts.join('')
}
// The embedded value lands unquoted in CSS url() and quoted in attributes, so it must
diff --git a/lib/remote-image.ts b/lib/remote-image.ts
index 66af4a6..160e650 100644
--- a/lib/remote-image.ts
+++ b/lib/remote-image.ts
@@ -11,6 +11,8 @@ import { fetchPublicImage, MAX_IMAGE_FETCH_BYTES } from './public-fetch.ts'
// What is fetched depends only on the stored design, never on the viewer.
export const MAX_REMOTE_IMAGES = 12
+// Counted per place an image is written. With the 512 KiB design limit this bounds a
+// prepared preview to about 6 MiB, and the artifact-frame response to about 8 MiB.
export const MAX_REMOTE_IMAGES_TOTAL_BYTES = 4 * 1024 * 1024
/** One remote image as a data: URL. Throws when it is unreachable, too large or not an image. */
@@ -22,9 +24,11 @@ export async function loadRemoteImage(url: string): Promise<string> {
}
/**
- * The design with its remote images embedded, in document order, within the count
- * and size budgets; any other one becomes a blank image. `complete` is false when an
- * image failed, so the caller does not let a browser keep that copy.
+ * The design with its remote images embedded, within the count and size budgets; any
+ * other one becomes a blank image. The size budget counts every place an image is
+ * written, since each one carries the whole image, so a repeated image cannot expand
+ * the preview past it. `complete` is false when an image failed to load, so the
+ * caller does not let a browser keep that copy.
*/
export async function embedRemoteImages(html: string, load: (url: string) => Promise<string>): Promise<{ html: string; complete: boolean }> {
const urls: string[] = []
@@ -36,14 +40,19 @@ export async function embedRemoteImages(html: string, load: (url: string) => Pro
const wanted = urls.slice(0, MAX_REMOTE_IMAGES)
const loaded = await Promise.all(wanted.map((url) => load(url).catch(() => null)))
const embedded = new Map<string, string>()
- let total = 0
for (const [index, image] of loaded.entries()) {
- if (!image) continue
- const bytes = Math.floor((image.length - image.indexOf(',') - 1) * 3 / 4)
- if (bytes > MAX_IMAGE_FETCH_BYTES || total + bytes > MAX_REMOTE_IMAGES_TOTAL_BYTES) continue
- total += bytes
- embedded.set(wanted[index], image)
+ if (image && imageBytes(image) <= MAX_IMAGE_FETCH_BYTES) embedded.set(wanted[index], image)
}
- const complete = loaded.every(Boolean)
- return { html: rewriteRemoteImages(html, (url: string) => embedded.get(url) ?? BLANK_IMAGE), complete }
+ let total = 0
+ const rewritten = rewriteRemoteImages(html, (url: string) => {
+ const image = embedded.get(url)
+ if (!image || total + imageBytes(image) > MAX_REMOTE_IMAGES_TOTAL_BYTES) return BLANK_IMAGE
+ total += imageBytes(image)
+ return image
+ })
+ return { html: rewritten, complete: loaded.every(Boolean) }
+}
+
+function imageBytes(image: string): number {
+ return Math.floor((image.length - image.indexOf(',') - 1) * 3 / 4)
}
diff --git a/tests/remote-image.test.mjs b/tests/remote-image.test.mjs
index f65f7a7..bdf2dab 100644
--- a/tests/remote-image.test.mjs
+++ b/tests/remote-image.test.mjs
@@ -5,7 +5,7 @@ import dns from 'node:dns/promises'
import { Readable } from 'node:stream'
import { EventEmitter } from 'node:events'
import { BLANK_IMAGE, DESIGN_ARTIFACT_EMBEDDED_CSP, remoteImageUrl, rewriteRemoteImages, validateDesignArtifactHtml } from '../lib/design-artifact.ts'
-import { embedRemoteImages, loadRemoteImage, MAX_REMOTE_IMAGES } from '../lib/remote-image.ts'
+import { embedRemoteImages, loadRemoteImage, MAX_REMOTE_IMAGES, MAX_REMOTE_IMAGES_TOTAL_BYTES } from '../lib/remote-image.ts'
import { sniffImageType } from '../lib/image-sniff.ts'
import { MAX_IMAGE_FETCH_BYTES } from '../lib/public-fetch.ts'
@@ -95,6 +95,20 @@ test('embedding fetches each image once, in document order, within the count and
assert.equal(sized.html.split(big).length - 1, 3, 'the total budget stops the fourth large image')
})
+test('the size budget counts every place an image is written, so repeating one cannot expand the preview', async () => {
+ const big = `data:image/png;base64,${'A'.repeat(Math.ceil(1024 * 1024 * 4 / 3))}`
+ const source = doc('<img src="https://example.com/big.png">'.repeat(20) + '<div style="background:url(https://example.com/big.png)"></div>'.repeat(20))
+ const result = await embedRemoteImages(source, async () => big)
+ assert.equal(result.html.split(big).length - 1, 4, 'four 1 MiB copies fill the 4 MiB budget')
+ assert.ok(result.html.length < source.length + MAX_REMOTE_IMAGES_TOTAL_BYTES * 4 / 3 + 40 * BLANK_IMAGE.length)
+
+ const many = doc('<img src="https://example.com/tiny.png">'.repeat(20_000))
+ const started = performance.now()
+ const tiny = await embedRemoteImages(many, async () => PNG_URL)
+ assert.equal(tiny.html.split(PNG_URL).length - 1, 20_000)
+ assert.ok(performance.now() - started < 2_000, 'rewriting grows with the output, not with the number of edits')
+})
+
test('a failed image is blank and the copy is marked incomplete; a design without remote images is untouched', async () => {
const html = doc('<img src="https://example.com/ok.png"><img src="https://example.com/down.png">')
const result = await embedRemoteImages(html, async (url) => { if (url.endsWith('down.png')) throw new Error('down'); return PNG_URL })