@@ -1124,63 +1124,61 @@ describe("base path", () => {
11241124 const text = await response . text ( ) ;
11251125 expect ( text ) . toBe ( "hello world" ) ;
11261126 } ) ;
1127+ } ) ;
11271128
1128- /**
1129- * @see https://github.com/better-auth/better-call/issues/12
1130- */
1131- describe ( "streaming response" , ( ) => {
1132- it ( "returns a Response with a ReadableStream body unchanged from the handler" , async ( ) => {
1133- const encoder = new TextEncoder ( ) ;
1134- const messages = Array . from ( { length : 5 } , ( _ , i ) => `Message ${ i } \n` ) ;
1135- const endpoint = createEndpoint (
1136- "/ai/realtime" ,
1137- { method : "POST" } ,
1138- async ( ) => {
1139- const queue = [ ...messages ] ;
1140- const stream = new ReadableStream ( {
1141- pull ( controller ) {
1142- const next = queue . shift ( ) ;
1143- if ( next === undefined ) {
1144- controller . close ( ) ;
1145- return ;
1146- }
1147- controller . enqueue ( encoder . encode ( next ) ) ;
1148- } ,
1149- } ) ;
1150- return new Response ( stream , {
1151- status : 200 ,
1152- headers : {
1153- "Content-Type" : "application/x-ndjson" ,
1154- "Transfer-Encoding" : "chunked" ,
1155- "Cache-Control" : "no-cache" ,
1156- Connection : "keep-alive" ,
1157- } ,
1158- } ) ;
1159- } ,
1160- ) ;
1161- const router = createRouter ( { endpoint } ) ;
1129+ /**
1130+ * @see https://github.com/better-auth/better-call/issues/12
1131+ */
1132+ describe ( "streaming response" , ( ) => {
1133+ it ( "returns a Response with a ReadableStream body unchanged from the handler" , async ( ) => {
1134+ const encoder = new TextEncoder ( ) ;
1135+ const messages = [ "Message 0\n" , "Message 1\n" , "Message 2\n" ] ;
1136+ const endpoint = createEndpoint (
1137+ "/ai/realtime" ,
1138+ { method : "POST" } ,
1139+ async ( ) => {
1140+ const queue = [ ...messages ] ;
1141+ const stream = new ReadableStream ( {
1142+ pull ( controller ) {
1143+ const next = queue . shift ( ) ;
1144+ if ( next === undefined ) {
1145+ controller . close ( ) ;
1146+ return ;
1147+ }
1148+ controller . enqueue ( encoder . encode ( next ) ) ;
1149+ } ,
1150+ } ) ;
1151+ return new Response ( stream , {
1152+ status : 200 ,
1153+ headers : {
1154+ "Content-Type" : "application/x-ndjson" ,
1155+ "Cache-Control" : "no-cache" ,
1156+ } ,
1157+ } ) ;
1158+ } ,
1159+ ) ;
1160+ const router = createRouter ( { endpoint } ) ;
11621161
1163- const res = await router . handler (
1164- new Request ( "http://localhost/ai/realtime" , { method : "POST" } ) ,
1165- ) ;
1162+ const res = await router . handler (
1163+ new Request ( "http://localhost/ai/realtime" , { method : "POST" } ) ,
1164+ ) ;
11661165
1167- expect ( res . status ) . toBe ( 200 ) ;
1168- expect ( res . headers . get ( "Content-Type" ) ) . toBe ( "application/x-ndjson" ) ;
1169- expect ( res . headers . get ( "Transfer-Encoding" ) ) . toBe ( "chunked" ) ;
1170- expect ( res . headers . get ( "Cache-Control" ) ) . toBe ( "no-cache" ) ;
1171- expect ( res . headers . get ( "Connection" ) ) . toBe ( "keep-alive" ) ;
1172-
1173- if ( ! res . body ) {
1174- throw new Error ( "Body is null" ) ;
1175- }
1176- const reader = res . body . getReader ( ) ;
1177- const decoder = new TextDecoder ( ) ;
1178- for ( let i = 0 ; i < messages . length ; i ++ ) {
1179- const { value } = await reader . read ( ) ;
1180- expect ( decoder . decode ( value ) ) . toBe ( messages [ i ] ) ;
1181- }
1182- const tail = await reader . read ( ) ;
1183- expect ( tail . done ) . toBe ( true ) ;
1184- } ) ;
1166+ expect ( res . status ) . toBe ( 200 ) ;
1167+ expect ( res . headers . get ( "Content-Type" ) ) . toBe ( "application/x-ndjson" ) ;
1168+ expect ( res . headers . get ( "Cache-Control" ) ) . toBe ( "no-cache" ) ;
1169+
1170+ if ( ! res . body ) {
1171+ throw new Error ( "Body is null" ) ;
1172+ }
1173+ const reader = res . body . getReader ( ) ;
1174+ const decoder = new TextDecoder ( ) ;
1175+ let combined = "" ;
1176+ while ( true ) {
1177+ const { done, value } = await reader . read ( ) ;
1178+ if ( done ) break ;
1179+ combined += decoder . decode ( value , { stream : true } ) ;
1180+ }
1181+ combined += decoder . decode ( ) ;
1182+ expect ( combined ) . toBe ( messages . join ( "" ) ) ;
11851183 } ) ;
11861184} ) ;
0 commit comments