11import type { DurableObjectStorage } from '@cloudflare/workers-types' ;
2- import type { SerializedTraceData } from '@sentry/core' ;
2+ import type { SerializedTraceData , Span } from '@sentry/core' ;
33import {
44 captureException ,
55 continueTrace ,
@@ -18,13 +18,40 @@ import { flushAndDispose } from './flush';
1818import { ensureInstrumented } from './instrument' ;
1919import { init } from './sdk' ;
2020import { extractRpcMeta } from './utils/rpcMeta' ;
21- import { buildSpanLinks , getStoredSpanContext , storeSpanContext } from './utils/traceLinks' ;
21+ import {
22+ buildSpanLinks ,
23+ getStoredSpanContext ,
24+ storeSpanContext ,
25+ } from './utils/traceLinks' ;
2226
2327/** Extended DurableObjectState with originalStorage exposed by instrumentContext */
2428interface InstrumentedDurableObjectState extends DurableObjectState {
2529 originalStorage ?: DurableObjectStorage ;
2630}
2731
32+ /**
33+ * Resolves uninstrumented DO storage for the current invocation.
34+ * Prefer `thisArg.ctx` (the live Durable Object instance) over the context captured at
35+ * construction time to avoid cross-DO I/O errors in the same isolate.
36+ */
37+ function resolveOriginalStorage (
38+ context : ExecutionContext | InstrumentedDurableObjectState | undefined ,
39+ thisArg : unknown ,
40+ ) : DurableObjectStorage | undefined {
41+ if ( thisArg && typeof thisArg === 'object' && 'ctx' in thisArg ) {
42+ const doCtx = ( thisArg as { ctx : InstrumentedDurableObjectState } ) . ctx ;
43+ if ( doCtx ?. originalStorage ) {
44+ return doCtx . originalStorage ;
45+ }
46+ }
47+
48+ if ( context && 'originalStorage' in context && context . originalStorage ) {
49+ return context . originalStorage ;
50+ }
51+
52+ return undefined ;
53+ }
54+
2855type MethodWrapperOptions = {
2956 spanName ?: string ;
3057 spanOp ?: string ;
@@ -94,7 +121,7 @@ export function wrapMethodWithSentry<T extends OriginalMethod>(
94121 const context : typeof wrapperOptions . context | undefined = wrapperOptions . context ;
95122
96123 const waitUntil = context ?. waitUntil ?. bind ?.( context ) ;
97- const storage = context && 'originalStorage' in context ? context . originalStorage : undefined ;
124+ const storage = resolveOriginalStorage ( context , thisArg ) ;
98125
99126 let scopeClient = scope . getClient ( ) ;
100127 // Check if client exists AND is still usable (transport not disposed)
0 commit comments