Skip to content

Commit

Permalink
fix(remix): Patch request to have duplex='half' property
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Jun 12, 2024
1 parent 4955a65 commit 58ca606
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 2 additions & 6 deletions packages/remix/src/ssr/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AuthenticateRequestOptions, SignedInState, SignedOutState } from '
import { AuthStatus } from '@clerk/backend/internal';

import type { LoaderFunctionArgs } from './types';
import { patchRequest } from './utils';

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

// RequestInit#duplex option is required when sending a body.
if (request?.body) {
(request as unknown as { duplex: 'half' }).duplex = 'half';
}

const requestState = await createClerkClient({
apiUrl,
secretKey,
Expand All @@ -28,7 +24,7 @@ export async function authenticateRequest(
domain,
publishableKey,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
}).authenticateRequest(request, {
}).authenticateRequest(patchRequest(request), {
audience,
authorizedParties,
signInUrl,
Expand Down
3 changes: 2 additions & 1 deletion packages/remix/src/ssr/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { isTruthy } from '@clerk/shared/underscore';
import { noSecretKeyError, satelliteAndMissingProxyUrlAndDomain, satelliteAndMissingSignInUrl } from '../utils/errors';
import { getEnvVariable } from '../utils/utils';
import type { LoaderFunctionArgs, RootAuthLoaderOptions } from './types';
import { patchRequest } from './utils';

export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderOptions = {}) => {
const { request, context } = args;
const clerkRequest = createClerkRequest(request);
const clerkRequest = createClerkRequest(patchRequest(request));

// Fetch environment variables across Remix runtime.
// 1. First check if the user passed the key in the getAuth function or the rootAuthLoader.
Expand Down
15 changes: 15 additions & 0 deletions packages/remix/src/ssr/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,18 @@ export function getResponseClerkState(requestState: RequestStateWithRedirectUrls
export const wrapWithClerkState = (data: any) => {
return { clerkState: { __internal_clerk_state: { ...data } } };
};

/**
* Patches request to avoid duplex issues with unidici
* For more information, see:
* https://github.com/nodejs/node/issues/46221
* https://github.com/whatwg/fetch/pull/1457
* @internal
*/
export const patchRequest = (request: Request) => {
const clonedRequest = request.clone();
if (clonedRequest.method !== 'GET' && clonedRequest.body !== null) {
(clonedRequest as unknown as { duplex: 'half' }).duplex = 'half';
}
return clonedRequest;
};

0 comments on commit 58ca606

Please sign in to comment.