jstack icon indicating copy to clipboard operation
jstack copied to clipboard

Cannot use ClerkMiddleware and input together

Open Shivam-002 opened this issue 10 months ago • 4 comments

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. :)

Shivam-002 avatar Feb 07 '25 14:02 Shivam-002