@@ -15,6 +15,7 @@ import { getFinalOptions } from './options';
1515import { wrapRequestHandler } from './request' ;
1616import { addCloudResourceContext } from './scope-utils' ;
1717import { init } from './sdk' ;
18+ import { copyExecutionContext } from './utils/copyExecutionContext' ;
1819
1920/**
2021 * Wrapper for Cloudflare handlers.
@@ -38,7 +39,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
3839 if ( 'fetch' in handler && typeof handler . fetch === 'function' && ! isInstrumented ( handler . fetch ) ) {
3940 handler . fetch = new Proxy ( handler . fetch , {
4041 apply ( target , thisArg , args : Parameters < ExportedHandlerFetchHandler < Env , CfHostMetadata > > ) {
41- const [ request , env , context ] = args ;
42+ const [ request , env , ctx ] = args ;
43+ const context = copyExecutionContext ( ctx ) ;
4244
4345 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
4446
@@ -72,7 +74,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
7274 if ( 'scheduled' in handler && typeof handler . scheduled === 'function' && ! isInstrumented ( handler . scheduled ) ) {
7375 handler . scheduled = new Proxy ( handler . scheduled , {
7476 apply ( target , thisArg , args : Parameters < ExportedHandlerScheduledHandler < Env > > ) {
75- const [ event , env , context ] = args ;
77+ const [ event , env , ctx ] = args ;
78+ const context = copyExecutionContext ( ctx ) ;
79+
7680 return withIsolationScope ( isolationScope => {
7781 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
7882 const waitUntil = context . waitUntil . bind ( context ) ;
@@ -115,7 +119,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
115119 if ( 'email' in handler && typeof handler . email === 'function' && ! isInstrumented ( handler . email ) ) {
116120 handler . email = new Proxy ( handler . email , {
117121 apply ( target , thisArg , args : Parameters < EmailExportedHandler < Env > > ) {
118- const [ emailMessage , env , context ] = args ;
122+ const [ emailMessage , env , ctx ] = args ;
123+ const context = copyExecutionContext ( ctx ) ;
124+
119125 return withIsolationScope ( isolationScope => {
120126 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
121127 const waitUntil = context . waitUntil . bind ( context ) ;
@@ -156,7 +162,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
156162 if ( 'queue' in handler && typeof handler . queue === 'function' && ! isInstrumented ( handler . queue ) ) {
157163 handler . queue = new Proxy ( handler . queue , {
158164 apply ( target , thisArg , args : Parameters < ExportedHandlerQueueHandler < Env , QueueHandlerMessage > > ) {
159- const [ batch , env , context ] = args ;
165+ const [ batch , env , ctx ] = args ;
166+ const context = copyExecutionContext ( ctx ) ;
160167
161168 return withIsolationScope ( isolationScope => {
162169 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
@@ -206,7 +213,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
206213 if ( 'tail' in handler && typeof handler . tail === 'function' && ! isInstrumented ( handler . tail ) ) {
207214 handler . tail = new Proxy ( handler . tail , {
208215 apply ( target , thisArg , args : Parameters < ExportedHandlerTailHandler < Env > > ) {
209- const [ , env , context ] = args ;
216+ const [ , env , ctx ] = args ;
217+ const context = copyExecutionContext ( ctx ) ;
210218
211219 return withIsolationScope ( async isolationScope => {
212220 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
0 commit comments