Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/gitbook-v2/src/lib/images/resizer/cdn-cgi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GITBOOK_IMAGE_RESIZE_URL } from '@v2/lib/env';
import type { CloudflareImageOptions } from './types';
import { copyImageResponse } from './utils';

/**
* Resize an image by doing a request to a /cdn/cgi/ endpoint.
Expand All @@ -26,16 +27,18 @@ export async function resizeImageWithCDNCgi(
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
console.log(`resize image using cdn-cgi: ${resizeURL}`);

return await fetch(resizeURL, {
headers: {
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
Accept:
resizeOptions.format === 'json'
? 'application/json'
: `image/${resizeOptions.format || 'jpeg'}`,
},
signal,
});
return copyImageResponse(
await fetch(resizeURL, {
headers: {
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
Accept:
resizeOptions.format === 'json'
? 'application/json'
: `image/${resizeOptions.format || 'jpeg'}`,
},
signal,
})
);
}

function stringifyOptions(options: CloudflareImageOptions): string {
Expand Down
25 changes: 14 additions & 11 deletions packages/gitbook-v2/src/lib/images/resizer/cf-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CloudflareImageOptions } from './types';
import { copyImageResponse } from './utils';

/**
* Resize an image by doing a request to the image itself using the Cloudflare fetch.
Expand All @@ -17,15 +18,17 @@ export async function resizeImageWithCFFetch(
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
console.log(`resize image using cf-fetch: ${input}`);

return await fetch(input, {
headers: {
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
Accept:
resizeOptions.format === 'json'
? 'application/json'
: `image/${resizeOptions.format || 'jpeg'}`,
},
signal,
cf: { image: resizeOptions },
});
return copyImageResponse(
await fetch(input, {
headers: {
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
Accept:
resizeOptions.format === 'json'
? 'application/json'
: `image/${resizeOptions.format || 'jpeg'}`,
},
signal,
cf: { image: resizeOptions },
})
);
}
7 changes: 7 additions & 0 deletions packages/gitbook-v2/src/lib/images/resizer/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Copy a response to make sure it can be mutated by the rest of the middleware.
* To avoid errors "Can't modify immutable headers".
*/
export function copyImageResponse(response: Response) {
return new Response(response.body, response);
}
Loading