johannschopplich / kirbyup

🆙 Official bundler for Kirby Panel plugins
https://kirbyup.getkirby.com
MIT License
51 stars 3 forks source link

Invalid Url Error when using kirbyup serve #22

Closed ovenum closed 2 years ago

ovenum commented 2 years ago

Thanks for this great tool @johannschopplich!

Currently running into an error when trying to start kirbyup with the new HMR feature.

Starting kirbyup from within a plugin with kirbyup serve src/index.js will result in this error message. There is no custom config applied. This is on macOS 12.3.1 and using node v18.6.0.

kirbyup v2.0.0                                                                                                                                                                                                                                                                                11:40:37
ℹ Starting development server...                                                                                                                                                                                                                                                              11:40:37
node:internal/errors:477
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:387:5)
    at URL.onParseError (node:internal/url:564:9)
    at new URL (node:internal/url:644:5)
    at new URL (node:internal/url:641:22)
    at Server.<anonymous> (file:///project_path/node_modules/kirbyup/dist/shared/kirbyup.1b72ea0c.mjs:3429:26)
    at Object.onceWrapper (node:events:627:28)
    at Server.emit (node:events:525:35)
    at emitListeningNT (node:net:1465:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
  input: 'http://::1:5177/',
  code: 'ERR_INVALID_URL'
}

Node.js v18.6.0
johannschopplich commented 2 years ago

Could you provide a minimal reproducible example with your code, please?

A repo would be just fine. Thanks in advance!

ovenum commented 2 years ago

Sure it’s pretty bare-bones: https://github.com/basics09/kirbyup-issue-22/ : )

Looking at line 3429 in node_modules/kirbyup/dist/shared/kirbyup.1b72ea0c.mjs the issue is emitted from inside configureServer().

The address property of server.httpServer.address() is ::1 when i try to run the script. Editing the file and replacing it with the IPv4 loopback address will run the script but of course HMR will not work since the server was configured with the IPv6 loopback address.

This probably boils down to node URL in v18.6.0 not handling IPv6 correctly?

ovenum commented 2 years ago

After upgrading to node v18.8.0 i found the issue.

IPv6 addresses inside a URL have to be enclosed in square brackets [ ] as by the RFC Spec

configureServer should maybe try to detect if a IPv4 or IPv6 address is used. Not that deep into node, maybe httpServer.address() does this somewhere already? After that baseUrl can be build with IPv6 addresses working correctly.

https://github.com/johannschopplich/kirbyup/blob/1236007ade40fb8964ec4be1e7c460600aefee9b/src/node/plugins/hmr.ts#L58-L62

Currently baseUrl will be something like this http://::1:5177/ but should be http://[::1]:5177 for IPv6 Urls.

johannschopplich commented 2 years ago

@basics09 fixed by @jonaskuske. Thank you both!

Fix will land in kirbyup v2.0.1.