Skip to content

Bug: input zod validation done before middleware causes readable stream error #58

@jcodog

Description

@jcodog

When using jStack with clerk, and using wrangler for this, you have to create an auth middleware and a clerk middleware. The clerk middleware however, likes to be picky about the ReadableStream not being read from or mutated in any way once sent for security, clearly.

This is where an issue starts to occur. When you run .input() on a mutation, it reads the c.req.json() apparently, to validate any and all data sent to the input against the zod schema, this is internal to Zod and unavoidable. But the issue is, the middleware such as clerkMiddleware, needs to run BEFORE the input validation is run. This would negate and resolve this issue.

Image

This is the error that is shown when .input() is used and calls the zod validation of the schema.

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

      at new ClerkRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@clerk+backend@1.24.2_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/clerkRequest.ts:29:5)
      at createClerkRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@clerk+backend@1.24.2_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/clerkRequest.ts:80:54)
      at authenticateRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@clerk+backend@1.24.2_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/request.ts:97:63)
      at Object.authenticateRequest2 [as authenticateRequest]
  (file:///E:/projects/jobs/node_modules/.pnpm/@clerk+backend@1.24.2_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/factory.ts:52:12)
      at null.<anonymous>
  (file:///E:/projects/jobs/node_modules/.pnpm/@hono+clerk-auth@2.0.0_@cle_8683ebbd4f39a51ba677898a90870e39/node_modules/@hono/clerk-auth/dist/index.js:29:44)
      at null.<anonymous> (file:///E:/projects/jobs/src/server/jstack.ts:67:3)
      at middlewareHandler
  (file:///E:/projects/jobs/node_modules/.pnpm/jstack@1.0.6_hono@4.7.1_react@19.0.0_zod@3.24.2/node_modules/jstack/dist/server/index.js:5191:29)
      at dispatch
  (file:///E:/projects/jobs/node_modules/.pnpm/hono@4.7.1/node_modules/hono/dist/compose.js:30:23)
      at null.<anonymous>
  (file:///E:/projects/jobs/node_modules/.pnpm/hono@4.7.1/node_modules/hono/dist/compose.js:31:20)
      at middlewareHandler
  (file:///E:/projects/jobs/node_modules/.pnpm/jstack@1.0.6_hono@4.7.1_react@19.0.0_zod@3.24.2/node_modules/jstack/dist/server/index.js:5200:17)


[wrangler:inf] POST /api/auth/updatePersonalInformation 500 Internal Server Error (196ms)

Any research into fixes for this and ways around it to make .input and validation work without breaking the middleware is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions