Skip to content

[node] Fix https? classes - #17720

Closed
blakeembrey wants to merge 1 commit into
DefinitelyTyped:masterfrom
blakeembrey:improve-node-http
Closed

blakeembrey wants to merge 1 commit into
DefinitelyTyped:masterfrom
blakeembrey:improve-node-http

Conversation

@blakeembrey

Copy link
Copy Markdown
Member

Changes the http interfaces to correctly be classes. Some of it is directly copied from @types.

I was hoping to be able to copy more across, especially from a few of the modules I typed from scratch in https://github.com/types/env-node (fs, path, tls, crypto, child_process etc), but things have diverged too much for me to take the time to verify the conflicts. One day I might get to them. The initial work took me around 12 hours in @types, so copying it across for someone else might be a bit quicker in the future (plus there's the specific commits you can grab, e.g. typed-typings/env-node@f3dc512).

@dt-bot

dt-bot commented Jul 3, 2017

Copy link
Copy Markdown
Member

types/node/index.d.ts

to authors (@DefinitelyTyped/DefinitelyTyped @parambirs @robdesideri @tellnes @WilcoBakker @octo-sniffle @smac89 @Flarna Microsoft TypeScript (account can't be detected)). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

types/node/v4/index.d.ts

to authors (@DefinitelyTyped/DefinitelyTyped Microsoft TypeScript (account can't be detected)). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

types/node/v6/index.d.ts

to authors (@DefinitelyTyped/DefinitelyTyped @WilcoBakker Microsoft TypeScript (account can't be detected)). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

types/node/v7/index.d.ts

to authors (@DefinitelyTyped/DefinitelyTyped @parambirs @robdesideri @tellnes @WilcoBakker Microsoft TypeScript (account can't be detected)). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@blakeembrey

Copy link
Copy Markdown
Member Author

Note: This did delete the deprecated and non-existing stuff in the http definition.

Comment thread types/node/index.d.ts
export function createClient(port?: number, host?: string): any;
export function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
export function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
export function request(options: string | RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you removed URL from interface? Even typescript accepts this currently I would prefer to add it to make clear that url.URL is supported here (see https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_request_options_callback).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's wrong? It just happens that URL is assignable to RequestOptions, so this still works fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make sure there's a test case for this, but adding URL should be redundant (if the typings are correct).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it's redundant as it is enough that at least one property from URL matches RequestOptions. But URL would be also allowed if there would be no match as this is a separate path in code. Was added somewhere in NodeJS 7.x

Comment thread types/node/index.d.ts
[header: string]: number | string | string[];
}

export interface IncomingHeaders {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rename IncomingMessageHeaders to IncomingHeaders?
Please consider also to add | undefined or even split it up to two interfaces (one for string | undefined one for "set-cookie": string[]). See #17157 for details (in special last comments)

Comment thread types/node/index.d.ts
import * as stream from "stream";
import { URL } from "url";

export interface OutgoingHeaders {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you replace ServerResponseHeaders by OutgoingHeaders?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency, but I can change it back since there's no semver in DT.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I don't care that much as I would get hit just at one or two places. but others may see more impact.

Comment thread types/node/index.d.ts

export interface Server extends net.Server {
export class Server extends net.Server {
constructor(req: IncomingMessage, res: ServerResponse);

@Flarna Flarna Jul 4, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Server constructor gets a request listener (requestListener: (req: IncomingMessage, res: ServerResponse) => void) as optional parameter, not an IncomingMessage and ServerResponse object.

Comment thread types/node/index.d.ts
write(str: string, encoding?: string, fd?: string): boolean;

addTrailers(headers: ServerResponseHeaders): void;
export class OutgoingMessage extends stream.Writable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation (https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_class_http_serverresponse) Outgoing message does not inherit from stream.Writeable but it is an EventEmitter

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it inherits from stream internally but all the types match up with a writable stream.

image

Comment thread types/node/index.d.ts
cert?: any;
ca?: any;
cert?: string | Buffer;
ca?: string | Buffer | string[] | Buffer[];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blakeembrey blakeembrey Jul 4, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what it says, matches what you just commented on 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ca matches but cert does not. Seems I was interrupted too often during my review...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Good catch!

Comment thread types/node/index.d.ts
/**
* Private key to use for SSL. Default `null`.
*/
key?: string | Buffer | string[] | Buffer[];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also Object[] is allowed

Comment thread types/node/index.d.ts

export class Server extends tls.Server { }

export function createServer(options: ServerOptions, requestListener?: Function): Server;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better typing of requestListener got lost.

Comment thread types/node/index.d.ts
export class Server extends tls.Server { }

export function createServer(options: ServerOptions, requestListener?: Function): Server;
export function request(options: string | RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variant to use URL as option got lost.

Comment thread types/node/index.d.ts
/**
* Returns the current number of concurrent connections on the server.
*/
connections: number;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think connections can be removed here has it should be visible from net.Server

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. It's really only here because of the docblock, but I can move that to net.

@Flarna

Flarna commented Jul 4, 2017

Copy link
Copy Markdown
Contributor

Nice work! Seems some dependent packages use the deprecated types you removed but it should be simple to adapt them.

@blakeembrey

Copy link
Copy Markdown
Member Author

Thanks for the review @Flarna. I'll try to come back to these, but it'll take some time.

@RyanCavanaugh RyanCavanaugh added The Travis CI build failed Revision needed This PR needs code changes before it can be merged. Abandoned This PR had no activity for a long time, and is considered abandoned labels Jul 10, 2017
@typescript-bot

Copy link
Copy Markdown
Contributor

@blakeembrey Please fix the failures indicated in the Travis CI log.
This message was generated by a script

@blakeembrey
blakeembrey deleted the improve-node-http branch July 12, 2017 19:32
@blakeembrey

blakeembrey commented Jul 12, 2017 •

Copy link
Copy Markdown
Member Author

Thanks for your review @Flarna. I think I'll just keep the changes in my own repo for now since I can't do the work myself and I don't want the bot heckling me. I might try to go the other way when I can - it'll probably be easier to pull all the changes into @types and if I get them back in sync with DT, then I'll reverse the PR. Of course, there's some deviations made in @types for consistency or deleting deprecated code that I'll consider when reversing it, but for now it's more than I can do myself. Thanks again for your thoughtful review ❤️

@Flarna

Flarna commented Jul 12, 2017

Copy link
Copy Markdown
Contributor

Our new best friends the bots...
I will keep in mind that I should look into types during future PRs.

@leizongmin

Copy link
Copy Markdown
Contributor

nice work!

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

Labels

Abandoned This PR had no activity for a long time, and is considered abandoned Revision needed This PR needs code changes before it can be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants