Skip to content

Commit ec682ea

Browse files
committed
Guard against non-function exports in wrapServerFunction
1 parent 3de65ee commit ec682ea

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

packages/react-router/src/server/rsc/wrapServerFunction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export function wrapServerFunction<T extends (...args: any[]) => Promise<any>>(
4141
serverFunction: T,
4242
options: WrapServerFunctionOptions = {},
4343
): T {
44+
// Auto-instrumentation wraps all exports from "use server" files, including non-function values.
45+
if (typeof serverFunction !== 'function') {
46+
DEBUG_BUILD && debug.warn(`[RSC] Not wrapping non-function export: ${functionName}`);
47+
return serverFunction;
48+
}
49+
4450
DEBUG_BUILD && debug.log(`[RSC] Wrapping server function: ${functionName}`);
4551

4652
return new Proxy(serverFunction, {

packages/react-router/test/server/rsc/wrapServerFunction.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,16 @@ describe('wrapServerFunction', () => {
186186

187187
await expect(wrappedFn()).rejects.toBe(mockError);
188188
});
189+
190+
describe('non-function exports', () => {
191+
it('should return non-function values unchanged', () => {
192+
expect(wrapServerFunction('foo', 3 as any)).toBe(3);
193+
expect(wrapServerFunction('foo', null as any)).toBe(null);
194+
});
195+
196+
it('should return object as-is', () => {
197+
const obj = { key: 'value' };
198+
expect(wrapServerFunction('foo', obj as any)).toBe(obj);
199+
});
200+
});
189201
});

0 commit comments

Comments
 (0)