Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong typings for connection address in http request #5466

Closed
PatrykMiszczak opened this issue May 15, 2020 · 3 comments
Closed

Wrong typings for connection address in http request #5466

PatrykMiszczak opened this issue May 15, 2020 · 3 comments

Comments

@PatrykMiszczak
Copy link

Steps to reproduce

For the example code:

import { serve } from "https://deno.land/std@0.50.0/http/server.ts";

const server = serve({ port: 8000 });

console.log("Started");

for await (const req of server) {
    const peerHost: string = req.conn.remoteAddr.hostname;

    console.log(`Host ${peerHost} connected`);

    req.respond({});
}

I get:

error: TS2339 [ERROR]: Property 'hostname' does not exist on type 'Addr'.
  Property 'hostname' does not exist on type 'UnixAddr'.
    const peerHost: string = req.conn.remoteAddr.hostname;

When I replace req.conn.remoteAddr.hostname with (req.conn.remoteAddr as Deno.NetAddr).hostname everything works as expected:

Started
Host 127.0.0.1 connected

The full remoteAddr looks similar to { hostname: "127.0.0.1", port: 64859, transport: "tcp" }

Version

$ deno --version
deno 1.0.0
v8 8.4.300
typescript 3.9.2
@xReveres
Copy link

I also solved it by doing like this:

const remote = JSON.parse(JSON.stringify(req.conn.remoteAddr));
console.log(remote.hostname, remote.port, remote.transport);

It is bugged like groups in regex match I guess

@SyrupThinker
Copy link
Contributor

Your error is a duplicate of #5102, check this comment; it applies to the hostname field aswell.

Your cast (req.conn.remoteAddr as Deno.NetAddr).hostname is an expected solution because you know contextually that it is a Deno.NetAddr.

@bartlomieju
Copy link
Member

@SyrupThinker's response is a proper solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants