Skip to content

RFC: drop mutation of IncomingMessage owned fields, write only to new, augmented fields #4854

@cdaringe

Description

@cdaringe

Problem

express mutates req.url, and saves the un-mutated version onto originalUrl, making URL driven generic middleware hard to design.

Ref: chimurai/http-proxy-middleware#723

Apologies if this topic has already been discussed, I did execute a cursory search and didn't see it, but I miss things time to time.

Context

http.IncomingMessage is the http module's owned memory, and thus could be considered memory not open for writing. The writing to this memory prevents me from fixing problematic typescript issues in http-proxy-middleware. To make http-proxy-middleware generic is to decouple it from all frameworks that it supports--express, connect, raw http, koa, fastify, ...the works!

However, because express mutates req.url, I cannot trust http.IncomingMessage::url, and either have to:

  1. burden users with passing to HPM the same prefix they use in app.use(prefix, ...), then additionally track that prefix internally to the HPM lib, or
  2. have express conditionals within HPM that sniff specifically for indicators of this framework, and use alternate logic.
    1. this is the current state of http-proxy-middleware, but to make it play nicely with other server providers, we're now trying to avoid this

Solutions

  1. write to new, owned memory only:
// GET /api/foo/bar
req.url // untouched, /api/foo/bar

app.use('/api', ...)
req.express.url // /foo/bar
req.express.base // /api
  1. don't mutate URL at all. offer functions for users to derive relative path to the base/prefix path

  2. offer a .relativePathname, yielding req.url - sum(...nestedPrefixes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions