upstash / qstash-js

Message queue for serverless
https://docs.upstash.com/qstash
MIT License
158 stars 15 forks source link

Using verifySignatureAppRouter uses the body and renders it unreadable #100

Closed grantsingleton closed 4 months ago

grantsingleton commented 4 months ago

Replicate by using the example from the docs:

import { NextRequest, NextResponse } from "next/server";
import { verifySignatureAppRouter } from "@upstash/qstash/dist/nextjs";

async function handler(_req: NextRequest) {
    await new Promise((r) => setTimeout(r, 1000));

    console.log("Success");
    return NextResponse.json({ name: "John Doe Serverless" });
}

export const POST = verifySignatureAppRouter(handler);

That works fine, but if you try to read the body, it fails:

import { NextRequest, NextResponse } from "next/server";
import { verifySignatureAppRouter } from "@upstash/qstash/dist/nextjs";

async function handler(_req: NextRequest) {
    const body = await _req.json() //  ** this fails since the body was already read in verifySignatureAppRouter 
    await new Promise((r) => setTimeout(r, 1000));

    console.log("Success");
    return NextResponse.json({ name: "John Doe Serverless" });
}

export const POST = verifySignatureAppRouter(handler);

The error log in vercel:

TypeError: Body is unusable at specConsumeBody (node:internal/deps/undici/undici:5294:15) at tt.json (node:internal/deps/undici/undici:5196:18) at el (/var/task/.next/server/app/(app)/api/queue/task/route.js:1:18015) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:34754 at async eS.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:25901) at async eS.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36008) at async ei (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25461) at async es.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026) at async r3.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:508)

Xexr commented 4 months ago

I now get the same error having updated qstash.

This was working in 2.5.0, but is broken from 2.5.1+

Reverting back to 2.5.0 has resolved it for me for now.

fahreddinozcan commented 4 months ago

Just confirmed this issue. We're working on this

fahreddinozcan commented 4 months ago

This should be solved by #101 .

grantsingleton commented 4 months ago

This should be solved by #101 .

I verified that it is fixed in v2.5.4