question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make SvelteKit compatible with server middlewares

See original GitHub issue

Is your feature request related to a problem? Please describe. There are a lot of issues that either ask for server side features like SSE, websocket, etc or directly for server side middlewares.

I think the root issue is that the handle hook exposes its own types for request and response, which are not compatible with express middlewares that is the de facto standard in Node.

I get the rationale behind this design, as @Rich-Harris said in #334

it means that the answer to various problems becomes ‘use adapter-node, because you can use arbitrary middleware’

However, the current design feels more like making adapter-node a second-class citizen in order to treat every adapter the same.

I recently came across https://github.com/vendia/serverless-express, which seems to contain a solution to this problem.

Describe the solution you’d like

This code converts a request-like object into http.IncomingMessage (there is similar code for http.ServerResponse) https://github.com/vendia/serverless-express/blob/5cd87c8e7ca3a5e971d26d471cb70a6ac15cc61f/src/request.js#L7-L16

And it’s the cornerstone for using middlewares in a serverless environment.

I wonder if it’s a good idea to make the handle hook (maybe also endpoints) expose http.IncomingMessage and http.ServerResponse instead? For adaptor-node, the corresponding objects from polka are directly returned, for other adaptors, the aforementioned polyfill is applied.

This should open Svelte Kit to the vast amount of server middlewares that are available.

Describe alternatives you’ve considered

How important is this feature to you? Very, not being able use existing middlewares is a deal breaker for many people from the referenced issues. But it’s actually possible to polyfill in userland to use the middlewares.

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
benmccanncommented, Jun 1, 2021

if they’re convertible, I wonder what’s the appeal of exposing the custom types in the first place

We didn’t want to use Express types because not all adapters provide an Express request / response the way adapter-node does.

@benmccann Is that suggestion tracked in this repo somewhere?

I think this is the best issue tracking it

1reaction
Conduitrycommented, Jun 1, 2021

This is a retread of #334.

When this was being discussed internally in March (rather heatedly!), where I believe we landed was that, if someone cared enough about supporting the Express middleware API, there could be a community-maintained interop library between that API and what the handle hook provides. Rich wanted a certain amount of friction in place for using things that needed the middleware API, and forcing this to happen with a third-party library in userland was approximately the amount of friction he said he wanted to have.

The outcome of the conversation at the time was summarized in https://github.com/sveltejs/kit/issues/334#issuecomment-806289525 and I don’t know how much of the thinking has changed since then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQ • SvelteKit
How do I use middleware?permalink. adapter-node builds a middleware that you can use with your own server for production mode. In dev, you...
Read more >
Use adapter-node (SvelteKit) to create middleware for ...
1 Answer 1 · Now I am getting this error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ...\build\middlewares. · To add,...
Read more >
Learn How SvelteKit Works As a Framework - Joy of Code
SvelteKit is a machine that takes your code as input and outputs code as you make a change and you can find the...
Read more >
Sveltekit Authentication - Hyper.io
Tutorial: What you will learn * How to create an OAuth Application using Github * How to redirect requests using SvelteKit
Read more >
Using PostGraphile as a Library
If your server does not support the preferred middleware approach of mounting the PostGraphile library, ... First we create the PostGraphile middleware:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found