Skip to content

Commit 289a7cd

Browse files
committed
Fix issue with RPC style calls to instrumented Durable Objects
1 parent d207b47 commit 289a7cd

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/instrumentation/do.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Env = Record<string, unknown>
2222

2323
function instrumentBindingStub(stub: DurableObjectStub, nsName: string): DurableObjectStub {
2424
const stubHandler: ProxyHandler<typeof stub> = {
25-
get(target, prop) {
25+
get(target, prop, receiver) {
2626
if (prop === 'fetch') {
2727
const fetcher = Reflect.get(target, prop)
2828
const attrs = {
@@ -33,7 +33,7 @@ function instrumentBindingStub(stub: DurableObjectStub, nsName: string): Durable
3333
}
3434
return instrumentClientFetch(fetcher, () => ({ includeTraceContext: true }), attrs)
3535
} else {
36-
return passthroughGet(target, prop)
36+
return passthroughGet(target, prop, receiver)
3737
}
3838
},
3939
}
@@ -52,12 +52,12 @@ function instrumentBindingGet(getFn: DurableObjectNamespace['get'], nsName: stri
5252

5353
export function instrumentDOBinding(ns: DurableObjectNamespace, nsName: string) {
5454
const nsHandler: ProxyHandler<typeof ns> = {
55-
get(target, prop) {
55+
get(target, prop, receiver) {
5656
if (prop === 'get') {
57-
const fn = Reflect.get(ns, prop)
57+
const fn = Reflect.get(ns, prop, receiver)
5858
return instrumentBindingGet(fn, nsName)
5959
} else {
60-
return passthroughGet(target, prop)
60+
return passthroughGet(target, prop, receiver)
6161
}
6262
},
6363
}
@@ -176,22 +176,23 @@ function instrumentAlarmFn(alarmFn: AlarmFn, initialiser: Initialiser, env: Env,
176176
return wrap(alarmFn, alarmHandler)
177177
}
178178

179-
function instrumentAnyFn(fn: () => any, initialiser: Initialiser, env: Env, id: DurableObjectId) {
179+
function instrumentAnyFn(fn: () => any, initialiser: Initialiser, env: Env, _id: DurableObjectId) {
180180
if (!fn) return undefined
181181

182-
const alarmHandler: ProxyHandler<() => any> = {
183-
async apply(target, thisArg) {
182+
const fnHandler: ProxyHandler<() => any> = {
183+
async apply(target, thisArg, argArray: []) {
184+
thisArg = unwrap(thisArg)
184185
const config = initialiser(env, 'do-alarm')
185186
const context = setConfig(config)
186187
try {
187188
const bound = target.bind(unwrap(thisArg))
188-
return await api_context.with(context, executeDOAlarm, undefined, bound, id)
189+
return await api_context.with(context, () => bound.apply(thisArg, argArray), undefined)
189190
} catch (error) {
190191
throw error
191192
}
192193
},
193194
}
194-
return wrap(fn, alarmHandler)
195+
return wrap(fn, fnHandler)
195196
}
196197

197198
function instrumentDurableObject(

src/wrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ export function unwrap<T extends object>(item: T): T {
4646

4747
export function passthroughGet(target: any, prop: string | symbol, thisArg?: any) {
4848
const unwrappedTarget = unwrap(target)
49+
thisArg = unwrap(thisArg) || unwrappedTarget
4950
const value = Reflect.get(unwrappedTarget, prop)
5051
if (typeof value === 'function') {
5152
if (value.constructor.name === 'RpcProperty') {
5253
return (...args: unknown[]) => unwrappedTarget[prop](...args)
5354
}
54-
thisArg = thisArg || unwrappedTarget
5555
return value.bind(thisArg)
5656
} else {
5757
return value

0 commit comments

Comments
 (0)