1
- import type { ExecutionContext } from '@cloudflare/workers-types' ;
1
+ import type { ExecutionContext , IncomingRequestCfProperties } from '@cloudflare/workers-types' ;
2
2
import type { CloudflareOptions } from '@sentry/cloudflare' ;
3
3
import { setAsyncLocalStorageAsyncContextStrategy , wrapRequestHandler } from '@sentry/cloudflare' ;
4
4
import { getDefaultIsolationScope , getIsolationScope , getTraceData , logger } from '@sentry/core' ;
@@ -11,28 +11,44 @@ import { addSentryTracingMetaTags } from '../utils';
11
11
interface CfEventType {
12
12
protocol : string ;
13
13
host : string ;
14
+ method : string ;
15
+ headers : Record < string , string > ;
14
16
context : {
17
+ cf : {
18
+ httpProtocol ?: string ;
19
+ country ?: string ;
20
+ // ...other CF properties
21
+ } ;
15
22
cloudflare : {
16
23
context : ExecutionContext ;
24
+ request ?: Record < string , unknown > ;
25
+ env ?: Record < string , unknown > ;
17
26
} ;
18
27
} ;
19
28
}
20
29
21
30
function isEventType ( event : unknown ) : event is CfEventType {
31
+ if ( event === null || typeof event !== 'object' ) return false ;
32
+
22
33
return (
23
- event !== null &&
24
- typeof event === 'object' &&
34
+ // basic properties
25
35
'protocol' in event &&
26
36
'host' in event &&
27
- 'context' in event &&
28
37
typeof event . protocol === 'string' &&
29
38
typeof event . host === 'string' &&
39
+ // context property
40
+ 'context' in event &&
30
41
typeof event . context === 'object' &&
31
- event ?. context !== null &&
42
+ event . context !== null &&
43
+ // context.cf properties
44
+ 'cf' in event . context &&
45
+ typeof event . context . cf === 'object' &&
46
+ event . context . cf !== null &&
47
+ // context.cloudflare properties
32
48
'cloudflare' in event . context &&
33
49
typeof event . context . cloudflare === 'object' &&
34
- event ? .context . cloudflare !== null &&
35
- 'context' in event ? .context ? .cloudflare
50
+ event . context . cloudflare !== null &&
51
+ 'context' in event . context . cloudflare
36
52
) ;
37
53
}
38
54
@@ -85,9 +101,16 @@ export const sentryCloudflareNitroPlugin =
85
101
logger . log ( "Nitro Cloudflare plugin did not detect a Cloudflare event type. Won't patch Cloudflare handler." ) ;
86
102
return handlerTarget . apply ( handlerThisArg , handlerArgs ) ;
87
103
} else {
104
+ const url = `${ event . protocol } //${ event . host } ${ pathname } ` ;
105
+ const request = new Request ( url , {
106
+ method : event . method ,
107
+ headers : event . headers ,
108
+ cf : event . context . cf ,
109
+ } ) as Request < unknown , IncomingRequestCfProperties < unknown > > ;
110
+
88
111
const requestHandlerOptions = {
89
112
options : cloudflareOptions ,
90
- request : { ... event , url : ` ${ event . protocol } // ${ event . host } ${ pathname } ` } ,
113
+ request,
91
114
context : event . context . cloudflare . context ,
92
115
} ;
93
116
0 commit comments