Skip to content

Cannot redirect with partial URLs #25

Open
@eliellis

Description

@eliellis

What version of Elysia is running?

1.2.10

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Run the following:

import { Elysia } from "elysia";
import { node } from "@elysiajs/node";

const app = new Elysia({
  adapter: typeof Bun === "undefined" ? node() : undefined,
})
  .get("/home", () => {
    return "Home";
  })
  .onError(({ error }) => {
    console.log("Error", error);
  })
  .onRequest(({ request, redirect }) => {
    if (new URL(request.url).pathname !== "/home") {
      return redirect("/home");
    }
  })
  .listen(3000);

const res = await fetch("http://localhost:3000/redirect-me", {
  redirect: "manual",
});

console.log("status", res.status);

await app.stop(true);

What is the expected behavior?

The above should print status 302. Running the same sample with bun run does print status 302.

What do you see instead?

Error TypeError: Failed to parse URL from /home
    at Response.redirect (node:internal/deps/undici/undici:9100:17)
    at redirect (file:///Users/eli/code/elysia-node-redirect-issue/node_modules/elysia/dist/utils.mjs:1037:50)
    ... 4 lines matching cause stack trace ...
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17) {
  [cause]: TypeError: Invalid URL
      at new URL (node:internal/url:818:25)
      at Response.redirect (node:internal/deps/undici/undici:9098:23)
      at redirect (file:///Users/eli/code/elysia-node-redirect-issue/node_modules/elysia/dist/utils.mjs:1037:50)
      at Array.<anonymous> (/Users/eli/code/elysia-node-redirect-issue/tests/redirect-invalid-status-code.ts:15:14)
      at Server.map (eval at composeGeneralHandler (file:///Users/eli/code/elysia-node-redirect-issue/node_modules/elysia/dist/compose.mjs:1330:10), <anonymous>:22:43)
      at Server.emit (node:events:507:28)
      at parserOnIncoming (node:_http_server:1153:12)
      at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17) {
    code: 'ERR_INVALID_URL',
    input: '/home'
  }
}
Waiting for the debugger to disconnect...
node:_http_server:358
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
          ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
    at ServerResponse.writeHead (node:_http_server:358:11)
    at Elysia.eval [as handleError] (eval at composeErrorHandler (file:///Users/eli/code/elysia-node-redirect-issue/node_modules/elysia/dist/compose.mjs:1435:10), <anonymous>:24:5)
    at Server.map (eval at composeGeneralHandler (file:///Users/eli/code/elysia-node-redirect-issue/node_modules/elysia/dist/compose.mjs:1330:10), <anonymous>:24:26)
    at Server.emit (node:events:507:28)
    at parserOnIncoming (node:_http_server:1153:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17) {
  code: 'ERR_HTTP_INVALID_STATUS_CODE'
}

Node.js v23.6.1

Additional information

Additionally, running the following simply hangs:

import { Elysia } from "elysia";
import { node } from "@elysiajs/node";

const app = new Elysia({
  adapter: typeof Bun === "undefined" ? node() : undefined,
})
  .get("/home", () => {
    return "Home";
  })
  .onError(() => {
    console.log("Error");
  })
  .onRequest(({ request, redirect }) => {
    if (new URL(request.url).pathname !== "/home") {
      return redirect("http://localhost:3000/home");
    }
  })
  .listen(3000);

const res = await fetch("http://localhost:3000/redirect-me", {
  redirect: "manual",
});

console.log("status", res.status);

await app.stop(true);

Running the above using bun run prints status 302.

Have you try removing the node_modules and bun.lockb and try again yet?

yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions