1
1
import type { LoaderFunctionArgs } from "@remix-run/node" ;
2
2
import { env } from "~/env.server" ;
3
+ import { logger } from "~/services/logger.server" ;
4
+ import { getUserId , requireUserId } from "~/services/session.server" ;
5
+ import { longPollingFetch } from "~/utils/longPollingFetch" ;
3
6
4
7
export async function loader ( { params, request } : LoaderFunctionArgs ) {
8
+ const userId = await getUserId ( request ) ;
9
+
10
+ logger . log ( `/sync/traces/${ params . traceId } ` , { userId } ) ;
11
+
12
+ if ( ! userId ) {
13
+ return new Response ( "authorization header not found" , { status : 401 } ) ;
14
+ }
15
+
16
+ //todo check the user has access to this trace
17
+
5
18
const url = new URL ( request . url ) ;
6
19
const originUrl = new URL ( `${ env . ELECTRIC_ORIGIN } /v1/shape/public."TaskEvent"` ) ;
7
20
url . searchParams . forEach ( ( value , key ) => {
@@ -10,21 +23,5 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
10
23
11
24
originUrl . searchParams . set ( "where" , `"traceId"='${ params . traceId } '` ) ;
12
25
13
- // When proxying long-polling requests, content-encoding & content-length are added
14
- // erroneously (saying the body is gzipped when it's not) so we'll just remove
15
- // them to avoid content decoding errors in the browser.
16
- //
17
- // Similar-ish problem to https://github.com/wintercg/fetch/issues/23
18
- let response = await fetch ( originUrl . toString ( ) ) ;
19
- if ( response . headers . get ( `content-encoding` ) ) {
20
- const headers = new Headers ( response . headers ) ;
21
- headers . delete ( `content-encoding` ) ;
22
- headers . delete ( `content-length` ) ;
23
- response = new Response ( response . body , {
24
- status : response . status ,
25
- statusText : response . statusText ,
26
- headers,
27
- } ) ;
28
- }
29
- return response ;
26
+ return longPollingFetch ( originUrl . toString ( ) ) ;
30
27
}
0 commit comments