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

dir-list already sent with trailing slash #497

Open
2 tasks done
jessekrubin opened this issue Jan 9, 2025 · 1 comment
Open
2 tasks done

dir-list already sent with trailing slash #497

jessekrubin opened this issue Jan 9, 2025 · 1 comment

Comments

@jessekrubin
Copy link

jessekrubin commented Jan 9, 2025

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.x.x

Plugin version

8.x.x

Node.js version

20

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

rhel8 and ms windows

Description

When using the default json list for directories and fastify static trailing slashes trigger a reply-already sent error internally.

repro: https://github.com/jessekrubin/fastify-static-reply-sent

// fastify

import path from "node:path";
import Fastify from "fastify";

import FastifyStatic from "@fastify/static";
const fastify = Fastify({ logger: { level: "trace" } });

const pwd = process.cwd();
const root = path.join(pwd, "/node_modules");
console.log(root);
fastify
  .register(FastifyStatic, {
    root,
    redirect: false, // redirect true makes problem always happen
    prefix: "/node_modules",
    index: false,
    list: true,
  })
  .listen({ port: 3000 }, (err) => {
    if (err) throw err;
  });

// dir routes give me a 'already sent' error msg!

// NO PROBLEMO:
//   http://localhost:3000/node_modules/@fastify   <-- no trailing slash
// HAS PROBLEM:
//   http://localhost:3000/node_modules/           <-- trailing slash
//   http://localhost:3000/node_modules/@fastify/  <-- trailing slash

Link to code that reproduces the bug

https://github.com/jessekrubin/fastify-static-reply-sent

Expected Behavior

not throw an error or document how I am supposed to configure this?

@jessekrubin
Copy link
Author

jessekrubin commented Jan 9, 2025

adding the trailing slash causes problem-o-s

jesse@corbett[[fastify-static-bug]]$ node --watch thingy.js | pino-pretty &
[16:13:24.557] INFO (149519): Server listening at http://127.0.0.1:3000

jesse@corbett[[fastify-static-bug]]$ curl -s http://localhost:3000/node_modules/@fastify/ > outputshit.json && sleep 1 && echo -------prob-------- && cat outputshit.json | jq
[16:13:48.497] INFO (149519): incoming request
    reqId: "req-1"
    req: {
      "method": "GET",
      "url": "/node_modules/@fastify/",
      "host": "localhost:3000",
      "remoteAddress": "127.0.0.1",
      "remotePort": 49692
    }
[16:13:48.502] WARN (149519): Reply was already sent, did you forget to "return reply" in "/node_modules/@fastify/" (GET)?
    reqId: "req-1"
    err: {
      "type": "FastifyError",
      "message": "Reply was already sent, did you forget to \"return reply\" in \"/node_modules/@fastify/\" (GET)?",
      "stack":
          FastifyError: Reply was already sent, did you forget to "return reply" in "/node_modules/@fastify/" (GET)?
              at Reply.send (/home/jesse/fastify-static-bug/node_modules/.pnpm/[email protected]/node_modules/fastify/lib/reply.js:132:26)
              at pumpSendToReply (/home/jesse/fastify-static-bug/node_modules/.pnpm/@[email protected]/node_modules/@fastify/static/index.js:350:21)
      "code": "FST_ERR_REP_ALREADY_SENT",
      "name": "FastifyError",
      "statusCode": 500
    }
[16:13:48.503] INFO (149519): request completed
    reqId: "req-1"
    res: {
      "statusCode": 200
    }
    responseTime: 5.25226900074631
-------prob--------
{
  "dirs": [
    "static"
  ],
  "files": []
}
jesse@corbett[[fastify-static-bug]]$ curl -s http://localhost:3000/node_modules/@fastify > outputshit.json && sleep 1 && echo -------NO-prob-no-trailing-slash-------- && cat outputshit.json | jq
[16:14:07.158] INFO (149519): incoming request
    reqId: "req-2"
    req: {
      "method": "GET",
      "url": "/node_modules/@fastify",
      "host": "localhost:3000",
      "remoteAddress": "127.0.0.1",
      "remotePort": 56780
    }
[16:14:07.159] INFO (149519): request completed
    reqId: "req-2"
    res: {
      "statusCode": 200
    }
    responseTime: 0.9850010005757213
-------NO-prob-no-trailing-slash--------
{
  "dirs": [
    "static"
  ],
  "files": []
}

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

1 participant