Skip to content

Add abstraction of requests and responses in Relic #2495

Closed

Description

@vlidholt

Relic is Serverpod's built-in web server. Adding an abstraction layer on top of dart:io could be beneficial for a number of reasons. It would make it easier to work with Relic and make the interface future-proof if we want to move away from dart:io.

The shelf package has done a pretty good job with this, but it has some shortcomings. We could create a new package based on shelf (to preserve its licence, and give them proper credit). We can then tighten up some of its shortcomings. The new package could be named serverpod_relic_helpers or if we come up with a better name. From the shelf package we can copy and modify the following classes: Message, Request, Response, and Body.

The most significant issues with the types from shelf are that they are largely untyped (my guess because of legacy), and they are using List<int> to represent data (which is much less efficient than using Uint8List). Adding type-safety and using Uint8List will improve performance and memory usage (especially for large requests).

Modifications to the shelf Body class

Instead of having a generic constructor that takes any type of object, we could have three distinct, typed constructors:

Body.fromString(String body, {Encoding? encoding});

Body.fromDataStream(Stream<Uint8List> body);

Body.fromData(Uint8List body);

The underlying stream can be accessed through either the read method, which should return a Stream<Uint8List> (instead of the Stream<List<int>>) or readAsString, which decodes the full stream into a String.

Modifications to the shelf Request class

We can take the opportunity to add the parameters that are parsed by the new Router (#2489) to the Request class, making them easy to access:

Map<String, String> pathParameters;

Modifications to Relic's Route class

To support the new abstractions, the handleRequest method of the Route class can be structured like this:

Future<Response> handleRequest(Session session, Request request);

Activity

  1. added
    enhancement
    on Jul 18, 2024
  2. SandPod commented on Jul 22, 2024

    @SandPod
    Contributor

    My understanding is that the biggest shortcoming of shelf is that it does not provide type-safety and uses List<int>´ instead of Uint8List`.

    How big of an undertaking is it to introduce these changes? Is there any possibility for us to add these changes as contributions directly to shelf instead of creating a fork?

    We kind of end up needing to copy or mimic the classes regardless if we want an abstraction layer, but it could help us make it smaller.

  3. added
    area: web server
    on Aug 29, 2024
  4. moved this to Later 🥳 in Serverpod Frameworkson Mar 5, 2025
  5. moved this to Later 🥳 in Serverpod Frameworkson Jun 16, 2025
  6. moved this from Later 🥳 to Next 🌟 in Serverpod Frameworkson Jun 16, 2025
  7. moved this from Next 🌟 to Now 🔥 in Serverpod Frameworkson Jun 16, 2025
  8. SandPod commented on Jul 31, 2025

    @SandPod
    Contributor

    This issue is implemented as the new Relic library, a standalone web server that will be used to drive the web server in Serverpod: https://github.com/serverpod/relic

  9. SandPod commented on Jul 31, 2025

    @SandPod
    Contributor

    @nielsenko - Should we close this issue as this essentially is the relase of the Relic library?

  10. SandPod commented on Jul 31, 2025

    @SandPod
    Contributor

    Perhaps this PR actually closed this: #3625

  11. nielsenko commented on Aug 4, 2025

    @nielsenko
    Collaborator

    Closed by #3625

    Note that the handleRequest signature is slightly different.

  12. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: web serverenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions