Skip to content

Commit eada1b8

Browse files
committed
Clarify that there are two distinct types of cf object
Fixes cloudflare#15. There are two distinct cf objects serves associated with a Request: 1. On an eyeball Request attached to a FetchEvent, the cf property contains metadata about the request, provided by Cloudflare's edge. 2. As part of a dictionary of configuration passed to the Request constructor, the cf property allows a user to control certain Cloudflare functionality that can be applied to said Request, like image resizing. While those two things are both objects in a dictionary with the key "cf", they are otherwise distinct. This change makes these two types explicitly different, and specifies that a Request constructor's RequestInit dict can have a "cf" property that is either (1) or (2). Previously, passing a Request object as the `init` arg to the Request constructor failed, because an eyeball request's cf prop did not have the same shape as (2).
1 parent 5163f0e commit eada1b8

File tree

1 file changed

+142
-133
lines changed

1 file changed

+142
-133
lines changed

index.d.ts

+142-133
Original file line numberDiff line numberDiff line change
@@ -2,157 +2,166 @@ interface FetchEvent {
22
passThroughOnException: () => void
33
}
44

5-
interface RequestInit {
6-
cf?: {
7-
cacheEverything?: boolean
8-
scrapeShield?: boolean
9-
apps?: boolean
10-
image?: {
11-
/**
12-
* Maximum width in image pixels. The value must be an integer.
13-
*/
14-
width?: number
15-
/**
16-
* Maximum height in image pixels.
17-
*/
18-
height?: number
19-
/**
20-
* Resizing mode as a string. It affects interpretation of width and height
21-
* options:
22-
* - scale-down: Similar to contain, but the image is never enlarged. If
23-
* the image is larger than given width or height, it will be resized.
24-
* Otherwise its original size will be kept.
25-
* - contain: Resizes to maximum size that fits within the given width and
26-
* height. If only a single dimension is given (e.g. only width), the
27-
* image will be shrunk or enlarged to exactly match that dimension.
28-
* Aspect ratio is always preserved.
29-
* - cover: Resizes (shrinks or enlarges) to fill the entire area of width
30-
* and height. If the image has an aspect ratio different from the ratio
31-
* of width and height, it will be cropped to fit.
32-
*/
33-
fit?: 'scale-down' | 'contain' | 'cover'
34-
/**
35-
* When cropping with fit: "cover", this defines the side or point that should
36-
* be left uncropped. The value is either a string
37-
* "left", "right", "top", "bottom" or "center" (the default),
38-
* or an object {x, y} containing focal point coordinates in the original
39-
* image expressed as fractions ranging from 0.0 (top or left) to 1.0
40-
* (bottom or right), 0.5 being the center. {fit: "cover", gravity: "top"} will
41-
* crop bottom or left and right sides as necessary, but won’t crop anything
42-
* from the top. {fit: "cover", gravity: {x:0.5, y:0.2}} will crop each side to
43-
* preserve as much as possible around a point at 20% of the height of the
44-
* source image.
45-
*/
46-
gravity?: 'left' | 'right' | 'top' | 'bottom' | 'center' | { x: number; y: number }
47-
/**
48-
* Quality setting from 1-100 (useful values are in 60-90 range). Lower values
49-
* make images look worse, but load faster. The default is 85. It applies only
50-
* to JPEG and WebP images. It doesn’t have any effect on PNG.
51-
*/
52-
quality?: number
53-
/**
54-
* Output format to generate. It can be:
55-
* - webp: generate images in Google WebP format. Set quality to 100 to get
56-
* the WebP-lossles format.
57-
* - json: instead of generating an image, outputs information about the
58-
* image, in JSON format. The JSON object will contain image size
59-
* (before and after resizing), source image’s MIME type, file size, etc.
60-
*/
61-
format?: 'webp' | 'json'
62-
}
63-
minify?: {
64-
javascript?: boolean
65-
css?: boolean
66-
html?: boolean
67-
}
68-
mirage?: boolean
69-
/**
70-
* Redirects the request to an alternate origin server. You can use this,
71-
* for example, to implement load balancing across several origins.
72-
* (e.g.us-east.example.com)
73-
*
74-
* Note - For security reasons, the hostname set in resolveOverride must
75-
* be proxied on the same Cloudflare zone of the incoming request.
76-
* Otherwise, the setting is ignored. CNAME hosts are allowed, so to
77-
* resolve to a host under a different domain or a DNS only domain first
78-
* declare a CNAME record within your own zone’s DNS mapping to the
79-
* external hostname, set proxy on Cloudflare, then set resolveOverride
80-
* to point to that CNAME record.
81-
*/
82-
resolveOverride?: string
83-
}
84-
}
85-
86-
declare function addEventListener(
87-
type: 'fetch',
88-
handler: (event: FetchEvent) => void,
89-
): void
90-
91-
interface Request {
92-
/**
93-
* In addition to the properties on the standard Request object,
94-
* you can use a request.cf object to control how Cloudflare
95-
* features are applied as well as other custom information provided
96-
* by Cloudflare.
5+
interface CfRequestInit {
6+
/**
7+
* In addition to the properties you can set in the RequestInit dict
8+
* that you pass as an argument to the Request constructor, you can
9+
* set certain properties of a `cf` object to control how Cloudflare
10+
* features are applied to that new Request.
9711
*
98-
* Note: Currently, settings in the cf object cannot be tested in the
12+
* Note: Currently, these properties cannot be tested in the
9913
* playground.
10014
*/
101-
cf: {
102-
/**
103-
* (e.g. 395747)
104-
*/
105-
asn: string
106-
city: string
107-
clientTrustScore: number
15+
cacheEverything?: boolean
16+
scrapeShield?: boolean
17+
apps?: boolean
18+
image?: {
10819
/**
109-
* The three-letter airport code of the data center that the request
110-
* hit. (e.g. "DFW")
20+
* Maximum width in image pixels. The value must be an integer.
11121
*/
112-
colo: string
113-
continent: string
22+
width?: number
11423
/**
115-
* The two-letter country code in the request. This is the same value
116-
* as that provided in the CF-IPCountry header. (e.g. "US")
24+
* Maximum height in image pixels.
11725
*/
118-
country: string
119-
httpProtocol: string
120-
latitude: number
121-
longitude: number
122-
postalCode: string
26+
height?: number
12327
/**
124-
* e.g. "Texas"
28+
* Resizing mode as a string. It affects interpretation of width and height
29+
* options:
30+
* - scale-down: Similar to contain, but the image is never enlarged. If
31+
* the image is larger than given width or height, it will be resized.
32+
* Otherwise its original size will be kept.
33+
* - contain: Resizes to maximum size that fits within the given width and
34+
* height. If only a single dimension is given (e.g. only width), the
35+
* image will be shrunk or enlarged to exactly match that dimension.
36+
* Aspect ratio is always preserved.
37+
* - cover: Resizes (shrinks or enlarges) to fill the entire area of width
38+
* and height. If the image has an aspect ratio different from the ratio
39+
* of width and height, it will be cropped to fit.
12540
*/
126-
region: string
41+
fit?: 'scale-down' | 'contain' | 'cover'
12742
/**
128-
* e.g. "TX"
43+
* When cropping with fit: "cover", this defines the side or point that should
44+
* be left uncropped. The value is either a string
45+
* "left", "right", "top", "bottom" or "center" (the default),
46+
* or an object {x, y} containing focal point coordinates in the original
47+
* image expressed as fractions ranging from 0.0 (top or left) to 1.0
48+
* (bottom or right), 0.5 being the center. {fit: "cover", gravity: "top"} will
49+
* crop bottom or left and right sides as necessary, but won’t crop anything
50+
* from the top. {fit: "cover", gravity: {x:0.5, y:0.2}} will crop each side to
51+
* preserve as much as possible around a point at 20% of the height of the
52+
* source image.
12953
*/
130-
regionCode: string
54+
gravity?: 'left' | 'right' | 'top' | 'bottom' | 'center' | { x: number; y: number }
13155
/**
132-
* e.g. "weight=256;exclusive=1"
56+
* Quality setting from 1-100 (useful values are in 60-90 range). Lower values
57+
* make images look worse, but load faster. The default is 85. It applies only
58+
* to JPEG and WebP images. It doesn’t have any effect on PNG.
13359
*/
134-
requestPriority: string
60+
quality?: number
13561
/**
136-
* e.g. "America/Chicago"
62+
* Output format to generate. It can be:
63+
* - webp: generate images in Google WebP format. Set quality to 100 to get
64+
* the WebP-lossles format.
65+
* - json: instead of generating an image, outputs information about the
66+
* image, in JSON format. The JSON object will contain image size
67+
* (before and after resizing), source image’s MIME type, file size, etc.
13768
*/
138-
timezone: string
139-
tlsVersion: string
140-
tlsCipher: string
141-
tlsClientAuth: {
142-
certIssuerDNLegacy: string
143-
certIssuerDN: string
144-
certPresented: '0' | '1'
145-
certSubjectDNLegacy: string
146-
certSubjectDN: string
147-
certNotBefore: string // In format "Dec 22 19:39:00 2018 GMT"
148-
certNotAfter: string // In format "Dec 22 19:39:00 2018 GMT"
149-
certSerial: string
150-
certFingerprintSHA1: string
151-
certVerified: string // “SUCCESS”, “FAILED:reason”, “NONE”
152-
}
69+
format?: 'webp' | 'json'
70+
}
71+
minify?: {
72+
javascript?: boolean
73+
css?: boolean
74+
html?: boolean
75+
}
76+
mirage?: boolean
77+
/**
78+
* Redirects the request to an alternate origin server. You can use this,
79+
* for example, to implement load balancing across several origins.
80+
* (e.g.us-east.example.com)
81+
*
82+
* Note - For security reasons, the hostname set in resolveOverride must
83+
* be proxied on the same Cloudflare zone of the incoming request.
84+
* Otherwise, the setting is ignored. CNAME hosts are allowed, so to
85+
* resolve to a host under a different domain or a DNS only domain first
86+
* declare a CNAME record within your own zone’s DNS mapping to the
87+
* external hostname, set proxy on Cloudflare, then set resolveOverride
88+
* to point to that CNAME record.
89+
*/
90+
resolveOverride?: string
91+
}
92+
93+
interface CfRequestProperties {
94+
/**
95+
* In addition to the properties on the standard Request object,
96+
* the cf object contains extra information about the request provided
97+
* by Cloudflare's edge.
98+
*
99+
* Note: Currently, settings in the cf object cannot be accessed in the
100+
* playground.
101+
*/
102+
asn: string
103+
city: string
104+
clientTrustScore: number
105+
/**
106+
* The three-letter airport code of the data center that the request
107+
* hit. (e.g. "DFW")
108+
*/
109+
colo: string
110+
continent: string
111+
/**
112+
* The two-letter country code in the request. This is the same value
113+
* as that provided in the CF-IPCountry header. (e.g. "US")
114+
*/
115+
country: string
116+
httpProtocol: string
117+
latitude: number
118+
longitude: number
119+
postalCode: string
120+
/**
121+
* e.g. "Texas"
122+
*/
123+
region: string
124+
/**
125+
* e.g. "TX"
126+
*/
127+
regionCode: string
128+
/**
129+
* e.g. "weight=256;exclusive=1"
130+
*/
131+
requestPriority: string
132+
/**
133+
* e.g. "America/Chicago"
134+
*/
135+
timezone: string
136+
tlsVersion: string
137+
tlsCipher: string
138+
tlsClientAuth: {
139+
certIssuerDNLegacy: string
140+
certIssuerDN: string
141+
certPresented: '0' | '1'
142+
certSubjectDNLegacy: string
143+
certSubjectDN: string
144+
certNotBefore: string // In format "Dec 22 19:39:00 2018 GMT"
145+
certNotAfter: string // In format "Dec 22 19:39:00 2018 GMT"
146+
certSerial: string
147+
certFingerprintSHA1: string
148+
certVerified: string // “SUCCESS”, “FAILED:reason”, “NONE”
153149
}
154150
}
155151

152+
interface RequestInit {
153+
cf?: CfRequestInit|CfRequestProperties
154+
}
155+
156+
declare function addEventListener(
157+
type: 'fetch',
158+
handler: (event: FetchEvent) => void,
159+
): void
160+
161+
interface Request {
162+
cf: CfRequestProperties
163+
}
164+
156165
interface ContentOptions {
157166
/**
158167
* Controls the way the HTMLRewriter treats inserted content.

0 commit comments

Comments
 (0)