Skip to content

Commit b5b4079

Browse files
fix: do not pass signal into server function (#5470)
* fix: do not pass signal into server function * fix test * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6f64ae2 commit b5b4079

14 files changed

Lines changed: 68 additions & 90 deletions

File tree

e2e/react-start/server-functions/src/routes/abort-signal/$method.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createFileRoute } from '@tanstack/react-router'
2-
import { createServerFn } from '@tanstack/react-start'
2+
import { createServerFn, createServerOnlyFn } from '@tanstack/react-start'
3+
import { getRequest } from '@tanstack/react-start/server'
34
import React from 'react'
45
import z from 'zod'
56

@@ -10,7 +11,9 @@ export const Route = createFileRoute('/abort-signal/$method')({
1011
component: RouteComponent,
1112
})
1213

13-
function serverFnImpl(signal: AbortSignal) {
14+
const serverFnImpl = createServerOnlyFn(async () => {
15+
const request = getRequest()
16+
const signal = request.signal
1417
console.log('server function started', { signal })
1518
return new Promise<string>((resolve, reject) => {
1619
if (signal.aborted) {
@@ -27,13 +30,12 @@ function serverFnImpl(signal: AbortSignal) {
2730
}
2831
signal.addEventListener('abort', onAbort, { once: true })
2932
})
30-
}
31-
const abortableServerFnGET = createServerFn().handler(async ({ signal }) =>
32-
serverFnImpl(signal),
33-
)
33+
})
34+
35+
const abortableServerFnGET = createServerFn().handler(serverFnImpl)
3436

3537
const abortableServerFnPOST = createServerFn({ method: 'POST' }).handler(
36-
async ({ signal }) => serverFnImpl(signal),
38+
serverFnImpl,
3739
)
3840

3941
function RouteComponent() {

e2e/solid-start/server-functions/src/routes/abort-signal/$method.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createFileRoute } from '@tanstack/solid-router'
2-
import { createServerFn } from '@tanstack/solid-start'
2+
import { createServerFn, createServerOnlyFn } from '@tanstack/solid-start'
3+
import { getRequest } from '@tanstack/solid-start/server'
34
import * as Solid from 'solid-js'
45
import z from 'zod'
56

@@ -10,7 +11,9 @@ export const Route = createFileRoute('/abort-signal/$method')({
1011
component: RouteComponent,
1112
})
1213

13-
function serverFnImpl(signal: AbortSignal) {
14+
const serverFnImpl = createServerOnlyFn(async () => {
15+
const request = getRequest()
16+
const signal = request.signal
1417
console.log('server function started', { signal })
1518
return new Promise<string>((resolve, reject) => {
1619
if (signal.aborted) {
@@ -27,13 +30,11 @@ function serverFnImpl(signal: AbortSignal) {
2730
}
2831
signal.addEventListener('abort', onAbort, { once: true })
2932
})
30-
}
31-
const abortableServerFnGET = createServerFn().handler(async ({ signal }) =>
32-
serverFnImpl(signal),
33-
)
33+
})
34+
const abortableServerFnGET = createServerFn().handler(serverFnImpl)
3435

3536
const abortableServerFnPOST = createServerFn({ method: 'POST' }).handler(
36-
async ({ signal }) => serverFnImpl(signal),
37+
serverFnImpl,
3738
)
3839

3940
function RouteComponent() {

e2e/vue-start/server-functions/src/routes/abort-signal.tsx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { createFileRoute } from '@tanstack/vue-router'
22
import { createServerFn } from '@tanstack/vue-start'
3+
import { getRequest } from '@tanstack/vue-start/server'
34
import { defineComponent, ref } from 'vue'
45

5-
const abortableServerFn = createServerFn().handler(
6-
async ({ context, signal }) => {
7-
console.log('server function started', { context, signal })
8-
return new Promise<string>((resolve, reject) => {
9-
if (signal.aborted) {
10-
return reject(new Error('Aborted before start'))
11-
}
12-
const timerId = setTimeout(() => {
13-
console.log('server function finished')
14-
resolve('server function result')
15-
}, 1000)
16-
const onAbort = () => {
17-
clearTimeout(timerId)
18-
console.log('server function aborted')
19-
reject(new Error('Aborted'))
20-
}
21-
signal.addEventListener('abort', onAbort, { once: true })
22-
})
23-
},
24-
)
6+
const abortableServerFn = createServerFn().handler(async ({ context }) => {
7+
console.log('server function started', { context })
8+
const signal = getRequest().signal
9+
return new Promise<string>((resolve, reject) => {
10+
if (signal.aborted) {
11+
return reject(new Error('Aborted before start'))
12+
}
13+
const timerId = setTimeout(() => {
14+
console.log('server function finished')
15+
resolve('server function result')
16+
}, 1000)
17+
const onAbort = () => {
18+
clearTimeout(timerId)
19+
console.log('server function aborted')
20+
reject(new Error('Aborted'))
21+
}
22+
signal.addEventListener('abort', onAbort, { once: true })
23+
})
24+
})
2525

2626
const RouteComponent = defineComponent({
2727
setup() {

packages/start-client-core/src/createServerFn.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export const createServerFn: CreateServerFn<Register> = (options, __opts) => {
140140
...extractedFn,
141141
// The extracted function on the server-side calls
142142
// this function
143-
__executeServer: async (opts: any, signal: AbortSignal) => {
143+
__executeServer: async (opts: any) => {
144144
const startContext = getStartContextServerOnly()
145145
const serverContextAfterGlobalMiddlewares =
146146
startContext.contextAfterGlobalMiddlewares
@@ -157,7 +157,6 @@ export const createServerFn: CreateServerFn<Register> = (options, __opts) => {
157157
serverContextAfterGlobalMiddlewares,
158158
opts.context,
159159
),
160-
signal,
161160
request: startContext.request,
162161
}
163162

@@ -350,7 +349,6 @@ export interface FetcherBase {
350349
data: unknown
351350
headers?: HeadersInit
352351
context?: any
353-
signal: AbortSignal
354352
}) => Promise<unknown>
355353
}
356354

@@ -429,7 +427,6 @@ export interface ServerFnCtx<
429427
serverFnMeta: ServerFnMeta
430428
context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>
431429
method: TMethod
432-
signal: AbortSignal
433430
}
434431

435432
export type CompiledFetcherFn<TRegister, TResponse> = {

packages/start-client-core/src/tests/createServerFn.test-d.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ test('createServerFn without middleware', () => {
2626
context: undefined
2727
data: undefined
2828
method: 'GET'
29-
signal: AbortSignal
3029
serverFnMeta: ServerFnMeta
3130
}>()
3231
})
@@ -50,7 +49,6 @@ test('createServerFn with validator function', () => {
5049
a: string
5150
}
5251
method: 'GET'
53-
signal: AbortSignal
5452
serverFnMeta: ServerFnMeta
5553
}>()
5654
})
@@ -79,7 +77,6 @@ test('createServerFn with async validator function', () => {
7977
context: undefined
8078
data: string
8179
method: 'GET'
82-
signal: AbortSignal
8380
serverFnMeta: ServerFnMeta
8481
}>()
8582
})
@@ -110,7 +107,6 @@ test('createServerFn with validator with parse method', () => {
110107
context: undefined
111108
data: string
112109
method: 'GET'
113-
signal: AbortSignal
114110
serverFnMeta: ServerFnMeta
115111
}>()
116112
})
@@ -141,7 +137,6 @@ test('createServerFn with async validator with parse method', () => {
141137
context: undefined
142138
data: string
143139
method: 'GET'
144-
signal: AbortSignal
145140
serverFnMeta: ServerFnMeta
146141
}>()
147142
})
@@ -189,7 +184,6 @@ test('createServerFn with standard validator', () => {
189184
context: undefined
190185
data: string
191186
method: 'GET'
192-
signal: AbortSignal
193187
serverFnMeta: ServerFnMeta
194188
}>()
195189
})
@@ -238,7 +232,6 @@ test('createServerFn with async standard validator', () => {
238232
context: undefined
239233
data: string
240234
method: 'GET'
241-
signal: AbortSignal
242235
serverFnMeta: ServerFnMeta
243236
}>()
244237
})
@@ -303,7 +296,6 @@ test('createServerFn with middleware and context', () => {
303296
}
304297
data: undefined
305298
method: 'GET'
306-
signal: AbortSignal
307299
serverFnMeta: ServerFnMeta
308300
}>()
309301
})
@@ -347,7 +339,6 @@ describe('createServerFn with middleware and validator', () => {
347339
readonly outputC: 'outputC'
348340
}
349341
method: 'GET'
350-
signal: AbortSignal
351342
serverFnMeta: ServerFnMeta
352343
}>()
353344

@@ -446,7 +437,6 @@ test('createServerFn where validator is a primitive', () => {
446437
context: undefined
447438
data: 'c'
448439
method: 'GET'
449-
signal: AbortSignal
450440
serverFnMeta: ServerFnMeta
451441
}>()
452442
})
@@ -460,7 +450,6 @@ test('createServerFn where validator is optional if object is optional', () => {
460450
context: undefined
461451
data: 'c' | undefined
462452
method: 'GET'
463-
signal: AbortSignal
464453
serverFnMeta: ServerFnMeta
465454
}>()
466455
})
@@ -484,7 +473,6 @@ test('createServerFn where data is optional if there is no validator', () => {
484473
context: undefined
485474
data: undefined
486475
method: 'GET'
487-
signal: AbortSignal
488476
serverFnMeta: ServerFnMeta
489477
}>()
490478
})
@@ -679,7 +667,6 @@ test('incrementally building createServerFn with multiple middleware calls', ()
679667
}
680668
data: undefined
681669
method: 'GET'
682-
signal: AbortSignal
683670
serverFnMeta: ServerFnMeta
684671
}>()
685672
})
@@ -701,7 +688,6 @@ test('incrementally building createServerFn with multiple middleware calls', ()
701688
}
702689
data: undefined
703690
method: 'POST'
704-
signal: AbortSignal
705691
serverFnMeta: ServerFnMeta
706692
}>()
707693
})
@@ -724,7 +710,7 @@ test('incrementally building createServerFn with multiple middleware calls', ()
724710
}
725711
data: undefined
726712
method: 'GET'
727-
signal: AbortSignal
713+
728714
serverFnMeta: ServerFnMeta
729715
}>()
730716
})
@@ -758,7 +744,6 @@ test('compose middlewares and server function factories', () => {
758744
}
759745
data: undefined
760746
method: 'GET'
761-
signal: AbortSignal
762747
serverFnMeta: ServerFnMeta
763748
}>()
764749
})

packages/start-plugin-core/src/start-compiler-plugin/handleCreateServerFn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,16 @@ export function handleCreateServerFn(
328328
// const extractedFn = createServerRpc({id, name, filename}, (opts) => varName.__executeServer(opts));
329329
// const varName = createServerFn().handler(extractedFn, originalHandler);
330330

331-
// Build the arrow function: (opts, signal) => varName.__executeServer(opts, signal)
331+
// Build the arrow function: (opts) => varName.__executeServer(opts)
332332
// The signal parameter is passed through to allow abort signal propagation
333333
const executeServerArrowFn = t.arrowFunctionExpression(
334-
[t.identifier('opts'), t.identifier('signal')],
334+
[t.identifier('opts')],
335335
t.callExpression(
336336
t.memberExpression(
337337
t.identifier(existingVariableName),
338338
t.identifier('__executeServer'),
339339
),
340-
[t.identifier('opts'), t.identifier('signal')],
340+
[t.identifier('opts')],
341341
),
342342
)
343343

packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('createServerFn compiles correctly', async () => {
143143
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJteVNlcnZlckZuX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
144144
name: "myServerFn",
145145
filename: "src/test.ts"
146-
}, (opts, signal) => myServerFn.__executeServer(opts, signal));
146+
}, opts => myServerFn.__executeServer(opts));
147147
const myServerFn = createServerFn().handler(myServerFn_createServerFn_handler, myFunc);
148148
export { myServerFn_createServerFn_handler };"
149149
`)
@@ -204,7 +204,7 @@ describe('createServerFn compiles correctly', async () => {
204204
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJleHBvcnRlZEZuX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
205205
name: "exportedFn",
206206
filename: "src/test.ts"
207-
}, (opts, signal) => exportedFn.__executeServer(opts, signal));
207+
}, opts => exportedFn.__executeServer(opts));
208208
const exportedFn = createServerFn().handler(exportedFn_createServerFn_handler, async () => {
209209
return exportedVar;
210210
});
@@ -213,7 +213,7 @@ describe('createServerFn compiles correctly', async () => {
213213
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJub25FeHBvcnRlZEZuX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
214214
name: "nonExportedFn",
215215
filename: "src/test.ts"
216-
}, (opts, signal) => nonExportedFn.__executeServer(opts, signal));
216+
}, opts => nonExportedFn.__executeServer(opts));
217217
const nonExportedFn = createServerFn().handler(nonExportedFn_createServerFn_handler, async () => {
218218
return nonExportedVar;
219219
});

packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const withUseServer_createServerFn_handler = createServerRpc({
55
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoVXNlU2VydmVyX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
66
name: "withUseServer",
77
filename: "src/test.ts"
8-
}, (opts, signal) => withUseServer.__executeServer(opts, signal));
8+
}, opts => withUseServer.__executeServer(opts));
99
const withUseServer = createServerFn({
1010
method: 'GET'
1111
}).handler(withUseServer_createServerFn_handler, async function () {
@@ -17,23 +17,23 @@ const withArrowFunction_createServerFn_handler = createServerRpc({
1717
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoQXJyb3dGdW5jdGlvbl9jcmVhdGVTZXJ2ZXJGbl9oYW5kbGVyIn0",
1818
name: "withArrowFunction",
1919
filename: "src/test.ts"
20-
}, (opts, signal) => withArrowFunction.__executeServer(opts, signal));
20+
}, opts => withArrowFunction.__executeServer(opts));
2121
const withArrowFunction = createServerFn({
2222
method: 'GET'
2323
}).handler(withArrowFunction_createServerFn_handler, async () => null);
2424
const withArrowFunctionAndFunction_createServerFn_handler = createServerRpc({
2525
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoQXJyb3dGdW5jdGlvbkFuZEZ1bmN0aW9uX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
2626
name: "withArrowFunctionAndFunction",
2727
filename: "src/test.ts"
28-
}, (opts, signal) => withArrowFunctionAndFunction.__executeServer(opts, signal));
28+
}, opts => withArrowFunctionAndFunction.__executeServer(opts));
2929
const withArrowFunctionAndFunction = createServerFn({
3030
method: 'GET'
3131
}).handler(withArrowFunctionAndFunction_createServerFn_handler, async () => test());
3232
const withoutUseServer_createServerFn_handler = createServerRpc({
3333
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRob3V0VXNlU2VydmVyX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
3434
name: "withoutUseServer",
3535
filename: "src/test.ts"
36-
}, (opts, signal) => withoutUseServer.__executeServer(opts, signal));
36+
}, opts => withoutUseServer.__executeServer(opts));
3737
const withoutUseServer = createServerFn({
3838
method: 'GET'
3939
}).handler(withoutUseServer_createServerFn_handler, async () => {
@@ -45,7 +45,7 @@ const withVariable_createServerFn_handler = createServerRpc({
4545
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoVmFyaWFibGVfY3JlYXRlU2VydmVyRm5faGFuZGxlciJ9",
4646
name: "withVariable",
4747
filename: "src/test.ts"
48-
}, (opts, signal) => withVariable.__executeServer(opts, signal));
48+
}, opts => withVariable.__executeServer(opts));
4949
const withVariable = createServerFn({
5050
method: 'GET'
5151
}).handler(withVariable_createServerFn_handler, abstractedFunction);
@@ -63,7 +63,7 @@ const withZodValidator_createServerFn_handler = createServerRpc({
6363
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoWm9kVmFsaWRhdG9yX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
6464
name: "withZodValidator",
6565
filename: "src/test.ts"
66-
}, (opts, signal) => withZodValidator.__executeServer(opts, signal));
66+
}, opts => withZodValidator.__executeServer(opts));
6767
const withZodValidator = createServerFn({
6868
method: 'GET'
6969
}).handler(withZodValidator_createServerFn_handler, zodValidator(z.number(), input => {
@@ -75,7 +75,7 @@ const withValidatorFn_createServerFn_handler = createServerRpc({
7575
id: "eyJmaWxlIjoiL0BpZC9zcmMvdGVzdC50cz90c3Mtc2VydmVyZm4tc3BsaXQiLCJleHBvcnQiOiJ3aXRoVmFsaWRhdG9yRm5fY3JlYXRlU2VydmVyRm5faGFuZGxlciJ9",
7676
name: "withValidatorFn",
7777
filename: "src/test.ts"
78-
}, (opts, signal) => withValidatorFn.__executeServer(opts, signal));
78+
}, opts => withValidatorFn.__executeServer(opts));
7979
const withValidatorFn = createServerFn({
8080
method: 'GET'
8181
}).inputValidator(z.number()).handler(withValidatorFn_createServerFn_handler, async ({

0 commit comments

Comments
 (0)