fastify / fastify-url-data

A plugin to provide access to the raw URL components
MIT License
57 stars 15 forks source link

urlData().path starts with "null/..." on Vercel or Railway with Fastify 5 #153

Closed jablonski closed 1 month ago

jablonski commented 1 month ago

Prerequisites

Fastify version

5.0.0

Plugin version

6.0.0

Node.js version

20

Operating system

Linux

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

Unknown

Description

When running Fastify with fastify-url-data on Vercel or Railway with Fastify 4, all works fine.

Running with Fastify 5, the "path" contains a leading "null/...", so the application breaks.

I suspect the following change for the problem:

Former plugin code was:

const urlData = fastUri.parse(scheme + '://' + host + path)`

The current code of the plugin was changed to:

const urlData = parse(`${scheme}://${host}:${port}${path}`)

I suspect that "port" is "null" when deploying on Vercel or Railway, therefere the fastUri-parser "fails".

The following example shows the problem:

import fastUri from "fast-uri";
console.log(fastUri.parse(`https://example.com:null/todos`).path);

It prints "null/todos".

Link to code that reproduces the bug

https://github.com/jablonski/fastify-serverless-function

Deployed on Vercel:

https://fastify-serverless-function-gules-ten.vercel.app/foo/bar

Here's the version deployed with fastify@4.28.1 and fastify-url-data@5.4.0, which works fine:

https://fastify-serverless-function-git-fastify-4-jeasx.vercel.app/foo/bar

Expected Behavior

When no port is set, the code should omit the port definition, so the path-Attribute doesn't starts with "null/".

johnyb commented 1 month ago

One addition to this: another typical use-case that fails now is when doing unit testing. At least I haven't found a useful way to specify a port when running in unit test environments using app.inject to query certain endpoints. This also has the same effect as described above, the path is not parsed correctly.