1- import type { ExecutionContext } from '@cloudflare/workers-types' ;
1+ import type { ExecutionContext , IncomingRequestCfProperties } from '@cloudflare/workers-types' ;
22import type { CloudflareOptions } from '@sentry/cloudflare' ;
33import { setAsyncLocalStorageAsyncContextStrategy , wrapRequestHandler } from '@sentry/cloudflare' ;
44import { getDefaultIsolationScope , getIsolationScope , getTraceData , logger } from '@sentry/core' ;
@@ -11,28 +11,44 @@ import { addSentryTracingMetaTags } from '../utils';
1111interface CfEventType {
1212 protocol : string ;
1313 host : string ;
14+ method : string ;
15+ headers : Record < string , string > ;
1416 context : {
17+ cf : {
18+ httpProtocol ?: string ;
19+ country ?: string ;
20+ // ...other CF properties
21+ } ;
1522 cloudflare : {
1623 context : ExecutionContext ;
24+ request ?: Record < string , unknown > ;
25+ env ?: Record < string , unknown > ;
1726 } ;
1827 } ;
1928}
2029
2130function isEventType ( event : unknown ) : event is CfEventType {
31+ if ( event === null || typeof event !== 'object' ) return false ;
32+
2233 return (
23- event !== null &&
24- typeof event === 'object' &&
34+ // basic properties
2535 'protocol' in event &&
2636 'host' in event &&
27- 'context' in event &&
2837 typeof event . protocol === 'string' &&
2938 typeof event . host === 'string' &&
39+ // context property
40+ 'context' in event &&
3041 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
3248 'cloudflare' in event . context &&
3349 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
3652 ) ;
3753}
3854
@@ -85,9 +101,16 @@ export const sentryCloudflareNitroPlugin =
85101 logger . log ( "Nitro Cloudflare plugin did not detect a Cloudflare event type. Won't patch Cloudflare handler." ) ;
86102 return handlerTarget . apply ( handlerThisArg , handlerArgs ) ;
87103 } 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+
88111 const requestHandlerOptions = {
89112 options : cloudflareOptions ,
90- request : { ... event , url : ` ${ event . protocol } // ${ event . host } ${ pathname } ` } ,
113+ request,
91114 context : event . context . cloudflare . context ,
92115 } ;
93116
0 commit comments