File tree 3 files changed +34
-21
lines changed
packages/gitbook-v2/src/lib/images/resizer
3 files changed +34
-21
lines changed Original file line number Diff line number Diff line change 1
1
import { GITBOOK_IMAGE_RESIZE_URL } from '@v2/lib/env' ;
2
2
import type { CloudflareImageOptions } from './types' ;
3
+ import { copyImageResponse } from './utils' ;
3
4
4
5
/**
5
6
* Resize an image by doing a request to a /cdn/cgi/ endpoint.
@@ -26,16 +27,18 @@ export async function resizeImageWithCDNCgi(
26
27
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
27
28
console . log ( `resize image using cdn-cgi: ${ resizeURL } ` ) ;
28
29
29
- return await fetch ( resizeURL , {
30
- headers : {
31
- // Pass the `Accept` header, as Cloudflare uses this to validate the format.
32
- Accept :
33
- resizeOptions . format === 'json'
34
- ? 'application/json'
35
- : `image/${ resizeOptions . format || 'jpeg' } ` ,
36
- } ,
37
- signal,
38
- } ) ;
30
+ return copyImageResponse (
31
+ await fetch ( resizeURL , {
32
+ headers : {
33
+ // Pass the `Accept` header, as Cloudflare uses this to validate the format.
34
+ Accept :
35
+ resizeOptions . format === 'json'
36
+ ? 'application/json'
37
+ : `image/${ resizeOptions . format || 'jpeg' } ` ,
38
+ } ,
39
+ signal,
40
+ } )
41
+ ) ;
39
42
}
40
43
41
44
function stringifyOptions ( options : CloudflareImageOptions ) : string {
Original file line number Diff line number Diff line change 1
1
import type { CloudflareImageOptions } from './types' ;
2
+ import { copyImageResponse } from './utils' ;
2
3
3
4
/**
4
5
* Resize an image by doing a request to the image itself using the Cloudflare fetch.
@@ -17,15 +18,17 @@ export async function resizeImageWithCFFetch(
17
18
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
18
19
console . log ( `resize image using cf-fetch: ${ input } ` ) ;
19
20
20
- return await fetch ( input , {
21
- headers : {
22
- // Pass the `Accept` header, as Cloudflare uses this to validate the format.
23
- Accept :
24
- resizeOptions . format === 'json'
25
- ? 'application/json'
26
- : `image/${ resizeOptions . format || 'jpeg' } ` ,
27
- } ,
28
- signal,
29
- cf : { image : resizeOptions } ,
30
- } ) ;
21
+ return copyImageResponse (
22
+ await fetch ( input , {
23
+ headers : {
24
+ // Pass the `Accept` header, as Cloudflare uses this to validate the format.
25
+ Accept :
26
+ resizeOptions . format === 'json'
27
+ ? 'application/json'
28
+ : `image/${ resizeOptions . format || 'jpeg' } ` ,
29
+ } ,
30
+ signal,
31
+ cf : { image : resizeOptions } ,
32
+ } )
33
+ ) ;
31
34
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copy a response to make sure it can be mutated by the rest of the middleware.
3
+ * To avoid errors "Can't modify immutable headers".
4
+ */
5
+ export function copyImageResponse ( response : Response ) {
6
+ return new Response ( response . body , response ) ;
7
+ }
You can’t perform that action at this time.
0 commit comments