-
Notifications
You must be signed in to change notification settings - Fork 94
feat(server): add autoFormats option to limit f_auto formats
#141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { type RequestListener, createServer } from "node:http"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { resolve } from "node:path"; | ||
| import { | ||
| type IPX, | ||
| type IPXAutoFormat, | ||
| type IPXURLParser, | ||
| createIPX, | ||
| createIPXFetchHandler, | ||
| createIPXNodeHandler, | ||
| ipxFSStorage, | ||
| parseIPXURL, | ||
| serveIPX, | ||
| } from "../src/index.ts"; | ||
|
|
||
| describe("server", () => { | ||
|
|
@@ -381,6 +386,180 @@ describe("server", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("f_auto", () => { | ||
| const chrome = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8"; | ||
|
|
||
| const detect = async ( | ||
| url: string, | ||
| accept: string, | ||
| opts?: Parameters<typeof createIPXFetchHandler>[1], | ||
| ) => { | ||
| const handler = createIPXFetchHandler(ipx, opts); | ||
| const res = await handler(new Request(url, { headers: { accept } })); | ||
| return { format: lastRequest.modifiers?.format, res }; | ||
| }; | ||
|
|
||
| it("negotiates the best format by default", async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Isolate If the test process starts with 🤖 Prompt for AI Agents |
||
| const { format, res } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| chrome, | ||
| ); | ||
| expect(format).toEqual("avif"); | ||
| expect(res.headers.get("vary")).toEqual("Accept"); | ||
| expect(lastRequest.modifiers).not.toHaveProperty("f"); | ||
| }); | ||
|
|
||
| it("falls back to jpeg (or gif when animated)", async () => { | ||
| expect( | ||
| (await detect("http://example.com/f_auto/test.jpg", "text/html")) | ||
| .format, | ||
| ).toEqual("jpeg"); | ||
| expect( | ||
| (await detect("http://example.com/f_auto,a/test.gif", "text/html")) | ||
| .format, | ||
| ).toEqual("gif"); | ||
| }); | ||
|
|
||
| it("autoFormats limits and orders the candidates", async () => { | ||
| const opts = { autoFormats: ["webp", "jpg"] as IPXAutoFormat[] }; | ||
| expect( | ||
| (await detect("http://example.com/f_auto/test.jpg", chrome, opts)) | ||
| .format, | ||
| ).toEqual("webp"); | ||
| expect( | ||
| ( | ||
| await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| "image/avif,image/jpeg", | ||
| opts, | ||
| ) | ||
| ).format, | ||
| ).toEqual("jpeg"); | ||
| }); | ||
|
|
||
| it("autoFormats only uses animated capable formats when animated", async () => { | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto,animated/test.gif", | ||
| chrome, | ||
| { autoFormats: ["avif", "gif"] }, | ||
| ); | ||
| expect(format).toEqual("gif"); | ||
| }); | ||
|
|
||
| it("prefers the highest q-value over the list order", async () => { | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| "image/webp;q=0.5,image/avif", | ||
| { autoFormats: ["webp", "avif"] }, | ||
| ); | ||
| expect(format).toEqual("avif"); | ||
| }); | ||
|
|
||
| it("does not match wildcards", async () => { | ||
| for (const accept of ["image/*", "*/*"]) { | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| accept, | ||
| { autoFormats: ["png"] }, | ||
| ); | ||
| expect(format).toEqual("jpeg"); | ||
| } | ||
| }); | ||
|
|
||
| it("falls back to jpeg even when autoFormats excludes it", async () => { | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| "", | ||
| { autoFormats: ["webp", "png"] }, | ||
| ); | ||
| expect(format).toEqual("jpeg"); | ||
| }); | ||
|
|
||
| it("throws on unsupported autoFormats", () => { | ||
| for (const autoFormats of [["image/webp"], ["svg"], ["foo", "webp"]]) { | ||
| expect(() => | ||
| createIPXFetchHandler(ipx, { autoFormats: autoFormats as any }), | ||
| ).toThrow(/Unsupported `autoFormats`/); | ||
| } | ||
| }); | ||
|
|
||
| describe("IPX_AUTO_FORMATS", () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it.each(["webp, jpeg", '["webp", "jpeg"]'])("reads %s", async (env) => { | ||
| vi.stubEnv("IPX_AUTO_FORMATS", env); | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| chrome, | ||
| ); | ||
| expect(format).toEqual("webp"); | ||
| }); | ||
|
|
||
| it.each([",", " ", "5", '{"a":1}'])( | ||
| "uses the defaults for %j", | ||
| async (env) => { | ||
| vi.stubEnv("IPX_AUTO_FORMATS", env); | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| chrome, | ||
| ); | ||
| expect(format).toEqual("avif"); | ||
| }, | ||
| ); | ||
|
|
||
| it("throws on unsupported formats", () => { | ||
| vi.stubEnv("IPX_AUTO_FORMATS", "webp,foo"); | ||
| expect(() => createIPXFetchHandler(ipx)).toThrow(/foo/); | ||
| }); | ||
|
|
||
| it("is overridden by the option", async () => { | ||
| vi.stubEnv("IPX_AUTO_FORMATS", "avif"); | ||
| const { format } = await detect( | ||
| "http://example.com/f_auto/test.jpg", | ||
| chrome, | ||
| { autoFormats: ["webp"] }, | ||
| ); | ||
| expect(format).toEqual("webp"); | ||
| }); | ||
| }); | ||
|
|
||
| it("createIPXNodeHandler and serveIPX pass autoFormats through", async () => { | ||
| const accept = { accept: chrome }; | ||
|
|
||
| const nodeServer = createServer( | ||
| createIPXNodeHandler(ipx, { autoFormats: ["webp"] }) as RequestListener, | ||
| ); | ||
| await new Promise<void>((r) => nodeServer.listen(0, "127.0.0.1", r)); | ||
| try { | ||
| const { port } = nodeServer.address() as AddressInfo; | ||
| await fetch(`http://127.0.0.1:${port}/f_auto/test.jpg`, { | ||
| headers: accept, | ||
| }); | ||
| expect(lastRequest.modifiers?.format).toEqual("webp"); | ||
| } finally { | ||
| nodeServer.close(); | ||
| } | ||
|
|
||
| const server = serveIPX(ipx, { | ||
| port: 0, | ||
| hostname: "127.0.0.1", | ||
| silent: true, | ||
| autoFormats: ["png"], | ||
| }); | ||
| try { | ||
| await server.ready(); | ||
| await fetch(new URL("/f_auto/test.jpg", server.url), { | ||
| headers: { accept: "image/avif,image/png" }, | ||
| }); | ||
| expect(lastRequest.modifiers?.format).toEqual("png"); | ||
| } finally { | ||
| await server.close(true); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("error handling", () => { | ||
| it("IPX_MISSING_MODIFIERS", async () => { | ||
| const handler = createIPXFetchHandler(ipx); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.