Skip to content

Commit a238328

Browse files
committed
fix(remix): Patch request to have duplex='half' property
1 parent c362904 commit a238328

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

packages/remix/src/ssr/authenticateRequest.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AuthenticateRequestOptions, SignedInState, SignedOutState } from '
33
import { AuthStatus } from '@clerk/backend/internal';
44

55
import type { LoaderFunctionArgs } from './types';
6+
import { patchRequest } from './utils';
67

78
export async function authenticateRequest(
89
args: LoaderFunctionArgs,
@@ -14,11 +15,6 @@ export async function authenticateRequest(
1415
const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey } = opts;
1516
const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = opts;
1617

17-
// RequestInit#duplex option is required when sending a body.
18-
if (request?.body) {
19-
(request as unknown as { duplex: 'half' }).duplex = 'half';
20-
}
21-
2218
const requestState = await createClerkClient({
2319
apiUrl,
2420
secretKey,
@@ -28,7 +24,7 @@ export async function authenticateRequest(
2824
domain,
2925
publishableKey,
3026
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
31-
}).authenticateRequest(request, {
27+
}).authenticateRequest(patchRequest(request), {
3228
audience,
3329
authorizedParties,
3430
signInUrl,

packages/remix/src/ssr/loadOptions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { isTruthy } from '@clerk/shared/underscore';
88
import { noSecretKeyError, satelliteAndMissingProxyUrlAndDomain, satelliteAndMissingSignInUrl } from '../utils/errors';
99
import { getEnvVariable } from '../utils/utils';
1010
import type { LoaderFunctionArgs, RootAuthLoaderOptions } from './types';
11+
import { patchRequest } from './utils';
1112

1213
export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderOptions = {}) => {
1314
const { request, context } = args;
14-
const clerkRequest = createClerkRequest(request);
15+
const clerkRequest = createClerkRequest(patchRequest(request));
1516

1617
// Fetch environment variables across Remix runtime.
1718
// 1. First check if the user passed the key in the getAuth function or the rootAuthLoader.

packages/remix/src/ssr/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,18 @@ export function getResponseClerkState(requestState: RequestStateWithRedirectUrls
116116
export const wrapWithClerkState = (data: any) => {
117117
return { clerkState: { __internal_clerk_state: { ...data } } };
118118
};
119+
120+
/**
121+
* Patches request to avoid duplex issues with unidici
122+
* For more information, see:
123+
* https://github.com/nodejs/node/issues/46221
124+
* https://github.com/whatwg/fetch/pull/1457
125+
* @internal
126+
*/
127+
export const patchRequest = (request: Request) => {
128+
const clonedRequest = request.clone();
129+
if (clonedRequest.method !== 'GET' && clonedRequest.body !== null) {
130+
(clonedRequest as unknown as { duplex: 'half' }).duplex = 'half';
131+
}
132+
return clonedRequest;
133+
};

0 commit comments

Comments
 (0)