Replace `req.connection` with `req.socket` · Issue #316 · tinyhttp/tinyhttp · GitHub
Skip to content

Replace req.connection with req.socket #316

Closed

Description

@aldy505

Not really a bug or feature request per se, just a deprecation notice. Regarding these:

req.connection is deprecated. https://nodejs.org/api/http.html#http_request_connection

The solution for the first one is simple:

import { Socket } from 'net';

const trustRemoteAddress = ({ remoteAddress}: Pick<Socket, 'remoteAddress'>) => {
  // Or might be picked from IncomingMessage as well, so it'll be `req.socket.remoteAddress`
  const val = connection.remoteAddress

I haven't found the solution for the second one, and I don't have the time to work on this yet. So if anyone's up for this, they're absolutely welcome.

Activity

  1. changed the title [-]Deprecation notice for the usage of `req.connection`[/-] [+]Replace `req.connection` with `req.socket`[/+] on Sep 17, 2021
  2. diivi commented on Oct 1, 2021

    @diivi

    I tried working on the second one and found this https://nodejs.org/api/tls.html#tls_tlssocket_encrypted but the req.socket here is a normal net.Socket instance so I could'nt figure out how to use the tlsSocket.encrypted function

  3. younes-io commented on Oct 8, 2021

    @younes-io
    Contributor

    This is my proposal :

    import { TLSSocket } from 'tls'
    
    export interface Request extends IncomingMessage {
      originalUrl: string
      path: string
      url: string
      query: ParsedUrlQuery
      params: URLParams
      connection: Connection
      socket: TLSSocket | Socket     // I added this
    ...
    }
    
    
    const trustRemoteAddress = ({ socket }: Pick<IncomingMessage, 'socket'>) => {
      const val = socket.remoteAddress
    
      if (typeof val === 'function') return val
      if (typeof val === 'boolean' && val === true) return () => true
      if (typeof val === 'number') return (_: unknown, i: number) => (val ? i < val : undefined)
      if (typeof val === 'string') return compile(val.split(',').map((x) => x.trim()))
    
      return compile(val || [])
    }
    
    
    export const getProtocol = (req: Request): Protocol => {
      const proto = req.socket instanceof TLSSocket && req.socket.encrypted ? 'https' : 'http'
    
      if (!trustRemoteAddress(req)) return proto
      const header = (req.headers['X-Forwarded-Proto'] as string) || proto
      const index = header.indexOf(',')
    
      return index !== -1 ? header.substring(0, index).trim() : header.trim()
    }
    
    // Lines 60 and 63 are also to be changed the same way

    What do you think?

  4. v1rtl commented on Oct 23, 2021

    @v1rtl
    Member

    @younes-io does it pass tinyhttp tests?

  5. younes-io commented on Oct 23, 2021

    @younes-io
    Contributor

    @talentlessguy when I pull the repo, and run the tests I get this:
    image

    After my updates, I run the tests again, and I get exactly the same results.

    Is the contributing section up-to-date?

  6. v1rtl commented on Oct 23, 2021

    @v1rtl
    Member

    @younes-io try running pnpm i && pnpm build -r

  7. younes-io commented on Oct 23, 2021

    @younes-io
    Contributor

    @talentlessguy I get exactly the same outcome. I also tried pnpm prerelease, but in vain. By the way, I tried with node 14.18.0 and 16.6.2

  8. v1rtl commented on Oct 23, 2021

    @v1rtl
    Member

    @younes-io delete and clone the repo again, check twice if branch is master, then try the same

  9. younes-io commented on Oct 23, 2021

    @younes-io
    Contributor

    @talentlessguy Indeed, it worked after deleting and re-pulling the repo. Thank you!
    I created a PR #328

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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