Skip to content

Commit 6f615bb

Browse files
authored
feat(server): make CORS plugin origin options sync (#1839)
The `origin` and `timingOrigin` options of `CORSHandlerPlugin` are now synchronous. They no longer accept promises, so resolving the allowed origin never suspends the response path. This is a breaking change for anyone passing an async function to either option. ## Changes - `origin` and `timingOrigin` drop `Promisable` from their value types. - The two `value(...)` calls in the response interceptor are no longer awaited. ## Testing `pnpm vitest run packages/server/src/plugins/cors.test.ts` passes (15 tests), `pnpm type:check` is clean across all packages, and lint is clean. No test changes were needed: every existing case already used sync origin functions. ## Notes `BatchHandlerPlugin`'s `maxSize`, `successStatus`, and `headers` options still accept promises and were left unchanged.
1 parent b32e9aa commit 6f615bb

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

packages/server/src/plugins/cors.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Promisable, Value } from '@orpc/shared'
1+
import type { Value } from '@orpc/shared'
22
import type { StandardHeaders } from '@standardserver/core'
33
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptor, StandardHandlerRoutingInterceptorOptions } from '../adapters/standard'
44
import type { Context } from '../context'
@@ -12,15 +12,15 @@ export interface CORSHandlerPluginOptions<T extends Context> {
1212
*
1313
* @default (origin) => origin
1414
*/
15-
origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>
15+
origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>
1616

1717
/**
1818
* Configures the `Timing-Allow-Origin` header.
1919
* Can be a string, an array of allowed origins, or a function that returns the allowed origin(s).
2020
*
2121
* @default undefined
2222
*/
23-
timingOrigin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>
23+
timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>
2424

2525
/**
2626
* Configures the `Access-Control-Allow-Methods` header for preflight requests.
@@ -101,7 +101,7 @@ export class CORSHandlerPlugin<T extends Context> implements StandardHandlerPlug
101101

102102
const origin = flattenStandardHeader(interceptorOptions.request.headers.origin) ?? ''
103103

104-
const allowedOrigins = toArray(await value(this.options.origin, origin, interceptorOptions))
104+
const allowedOrigins = toArray(value(this.options.origin, origin, interceptorOptions))
105105

106106
if (allowedOrigins.includes('*')) {
107107
resHeaders['access-control-allow-origin'] = '*'
@@ -117,7 +117,7 @@ export class CORSHandlerPlugin<T extends Context> implements StandardHandlerPlug
117117
}
118118
}
119119

120-
const allowedTimingOrigins = toArray(await value(this.options.timingOrigin, origin, interceptorOptions))
120+
const allowedTimingOrigins = toArray(value(this.options.timingOrigin, origin, interceptorOptions))
121121

122122
if (allowedTimingOrigins.includes('*')) {
123123
resHeaders['timing-allow-origin'] = '*'

0 commit comments

Comments
 (0)