Skip to content

ERROR: Body is unusable: Body has already been read #4031

@cjnoname

Description

@cjnoname

What version of Hono are you using?

4.7.5

What runtime/platform is your app running on? (with version if possible)

NodeJS on AWS Lambda

What steps can reproduce the bug?

The code below randomly throws this error with a probability of 1 in 100 to 1 in 1000.

{
    "errorType": "TypeError",
    "errorMessage": "Body is unusable: Body has already been read",
    "stack": [
        "TypeError: Body is unusable: Body has already been read",
        "    at consumeBody (node:internal/deps/undici/undici:5712:15)",
        "    at _Response.text (node:internal/deps/undici/undici:5662:18)",
        "    at s.createResult (file:///var/task/lambda.mjs:595:10175)",
        "    at Runtime.handler (file:///var/task/lambda.mjs:595:9620)"
    ]
}
import { Hono } from "hono";
import { secureHeaders } from "hono/secure-headers";
import { renderPage } from "./render";
import { randomBytes } from "crypto";
import { serveStatic } from "@hono/node-server/serve-static";
import { etag } from "hono/etag";

const app = new Hono();

app.use(etag({ weak: true }));

app.use(serveStatic({ root: "./static" }));

app.use((c, next) => {
  const nonce = randomBytes(16).toString("base64");
  c.set("secureHeadersNonce", nonce);

  return secureHeaders({
    contentSecurityPolicy: {
      scriptSrc: [`'self'`, `'strict-dynamic'`, `'nonce-${nonce}'`]
    }
  })(c, next);
});

app.get("*", async c => {
  const nonce = c.get("secureHeadersNonce");
  return c.html(await renderPage(c.req.path, nonce));
});

export default app;

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions