@@ -14,6 +14,7 @@ import { captureError, StatusError } from "@stackframe/stack-shared/dist/utils/e
1414import { wait } from "@stackframe/stack-shared/dist/utils/promises" ;
1515
1616const DEFAULT_MAX_DURATION_MS = 3 * 60 * 1000 ;
17+ const DIRECT_SYNC_ENV = "STACK_EXTERNAL_DB_SYNC_DIRECT" ;
1718
1819function parseMaxDurationMs ( value : string | undefined ) : number {
1920 if ( ! value ) return DEFAULT_MAX_DURATION_MS ;
@@ -24,6 +25,15 @@ function parseMaxDurationMs(value: string | undefined): number {
2425 return parsed ;
2526}
2627
28+ function directSyncEnabled ( ) : boolean {
29+ return getEnvVariable ( DIRECT_SYNC_ENV , "" ) === "true" ;
30+ }
31+
32+ function getLocalApiBaseUrl ( ) : string {
33+ const prefix = getEnvVariable ( "NEXT_PUBLIC_STACK_PORT_PREFIX" , "81" ) ;
34+ return `http://localhost:${ prefix } 02` ;
35+ }
36+
2737export const GET = createSmartRouteHandler ( {
2838 metadata : {
2939 summary : "Poll outgoing requests and push to QStash" ,
@@ -113,11 +123,25 @@ export const GET = createSmartRouteHandler({
113123 }
114124 }
115125
116-
117- await upstash . publishJSON ( {
118- url : fullUrl ,
119- body : options . body ,
120- } ) ;
126+ if ( directSyncEnabled ( ) ) {
127+ const directUrl = new URL ( options . url , getLocalApiBaseUrl ( ) ) . toString ( ) ;
128+ const res = await fetch ( directUrl , {
129+ method : "POST" ,
130+ headers : {
131+ "content-type" : "application/json" ,
132+ "upstash-signature" : "test-bypass" ,
133+ } ,
134+ body : JSON . stringify ( options . body ) ,
135+ } ) ;
136+ if ( ! res . ok ) {
137+ throw new StatusError ( res . status , `Direct sync failed: ${ res . status } ${ res . statusText } ` ) ;
138+ }
139+ } else {
140+ await upstash . publishJSON ( {
141+ url : fullUrl ,
142+ body : options . body ,
143+ } ) ;
144+ }
121145
122146 await deleteOutgoingRequest ( request . id ) ;
123147 } ) ,
0 commit comments