Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use ClerkMiddleware and input together #38

Open
Shivam-002 opened this issue Feb 7, 2025 · 2 comments
Open

Cannot use ClerkMiddleware and input together #38

Shivam-002 opened this issue Feb 7, 2025 · 2 comments

Comments

@Shivam-002
Copy link

I encountered following issue when using Clerk Middleware and jstack's input validation.

X [ERROR] [API Error] TypeError: This ReadableStream is disturbed (has already been read from), and cannot be used as a body.

at new ClerkRequest
(file:///E:/intonix/application-backup-v3/node_modules/@clerk/backend/src/tokens/clerkRequest.ts:29:5)

The main cause to the problem is clerk reading the stream after it's already been read.

When I debug it I found that this code trying to read it.

node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts
var drainBody = /* @__PURE__ */ __name(async (request, env4, _ctx, middlewareCtx) => {
  try {
    return await middlewareCtx.next(request, env4);
  } finally {
    try {
      if (request.body !== null && !request.bodyUsed && request) {
        const reader = request.body.getReader();
        while (!(await reader.read()).done) {
        }
      }
    } catch (e) {
      console.error("Failed to drain the unused request body.", e);
    }
  }
}, "drainBody");
var middleware_ensure_req_body_drained_default = drainBody;

Just reporting the bug. As I am now directly using the await c.req.json() and manually parsing it. :)

@joschan21
Copy link
Collaborator

Please lmk these things to help:

  • jstack version
  • which clerk middleware exactly youve used
  • your middleware implementation you expect to work

thanks for raising the issue

@Shivam-002
Copy link
Author

Jstack Version : [email protected]
Clerk Version : @hono/[email protected]

Here is the middleware implementation.

const authMiddleware = j.middleware(async ({ c, next }) => {
  const { NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY } = env(c) as {
    NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: string;
    CLERK_SECRET_KEY: string;
  };

  await honoClerkMiddleware({
    publishableKey: NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
    secretKey: CLERK_SECRET_KEY,
  })(c, async () => {
    const auth = getAuth(c);

    if (!auth?.userId) {
      c.json({ message: "You are not logged in." }, 401);
      return;
    }

    await next({ auth });
  });
});

When in a procedure i have input with a schema it throws this error. But as I remove the input and directly use the procedure it works. The error stack trace says it's clerk fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants