Skip to content

[node] net.Server, http.Server, http.ServerRequest, http.ServerResponse, http.ClientRequest, http.IncomingMessage, http.ClientResponse fix from interface to class and add constructors. - #18281

Merged
11 commits merged into
DefinitelyTyped:masterfrom
Canner:master
Jul 24, 2017

Conversation

@wwwy3y3

@wwwy3y3 wwwy3y3 commented Jul 21, 2017

Copy link
Copy Markdown
Contributor

Fix

  • several interface in http and net should be class instead (net.Server, http.Server, http.ServerRequest, http.ServerResponse, http.ClientRequest, http.IncomingMessage, http.ClientResponse)
  • add constructors all according to nodejs source code

Template

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:
If changing an existing definition:

wwwy3y3 added 3 commits July 21, 2017 16:04
…se, http.ClientRequest, http.IncomingMessage, http.ClientResponse, http.Server fix from interface to class and add constructors.
@dt-bot

dt-bot commented Jul 21, 2017

Copy link
Copy Markdown
Member

types/node/index.d.ts

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

Checklist

  • pass the Travis CI test?

Comment thread types/node/index.d.ts Outdated
agent?: Agent | boolean;
_defaultAgent?: Agent,
timeout?: number;
rejectUnauthorized?: boolean;

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 rejectUnauthorized is only available for https.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/nodejs/node/blob/master/lib/_http_client.js#L251

rejectUnauthorized is used here. it will be passed to underlying agent if socketpath provided

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.

Yes, because ClientRequest class is reused by HTTPS. Setting this flag for plain http doesn't make much sense as it is related to tls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

you're right. I removed it.

Comment thread types/node/index.d.ts Outdated
_defaultAgent?: Agent,
timeout?: number;
rejectUnauthorized?: boolean;
createConnection?: Function;

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.

would be nice to have a more concrete typing of this function here. Maybe there is a reuse with agent.createConnection().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

judging from code here
https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278

I think the type should be

createConnection?: (options: ClientRequestArgs, oncreate: (err: Error, socket: net.Socket) => void) => net.Socket;

Comment thread types/node/index.d.ts Outdated

export interface Server extends net.Server {
export class Server extends net.Server {
constructor(requestListener?: Function);

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.

Please consider to use a more specific type for requestListener, e.a. (req: IncomingMessage, res: ServerResponse) => void

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

after checking source code here
https://github.com/nodejs/node/blob/master/lib/_http_server.js#L563-L593

you're right. the type should be

constructor(requestListener?: (req: IncomingMessage, res: ServerResponse) => void);

Comment thread types/node/index.d.ts Outdated

export interface ServerResponse extends stream.Writable {
// https://github.com/nodejs/node/blob/master/lib/_http_server.js#L108
export class ServerResponse 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.

ServerResponse inherits from OutgoingMessage (see https://github.com/nodejs/node/blob/master/lib/_http_server.js#L122).
I think a new export OutgoingMessage should be added here and used as base for ServerResponse and ClientRequest. see also #17720

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Flarna fix! please take a look. thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also add more tests for http

Comment thread types/node/index.d.ts Outdated

// https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
export class ClientRequest extends stream.Writable {
constructor(url: string, cb?: Function);

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.

Consider to combine the constructors by using union types.
Consider also to use a stricter typing for the cb (e.a. (res: IncomingMessage) => void).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Flarna fixed in the new commit. please take a look. thanks.

wwwy3y3 added 3 commits July 21, 2017 20:36
createConnection callback make more detailed,
http.Server requestListener more detailed,
add export OutgoingMessage, ServerResponse and ClientRequest extends OutgoingMessage.
combine the constructors in ClientRequest
@typescript-bot

Copy link
Copy Markdown
Contributor

@Flarna - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

Comment thread types/node/index.d.ts
ciphers?: string;
honorCipherOrder?: boolean;
requestCert?: boolean;
rejectUnauthorized?: boolean;

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 rejectUnauthorized should be kept here as this are https server options. It should be removed in http.ClientRequestArgs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

maybe add another httpsClientArgs extends ClientRequestArgs with own prop rejectUnauthorized ?

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

omg. totally mistake. should remove from ClientRequestArgs. Must be tired. lol

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

export interface RequestOptions {
export interface HttpHeaders {

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.

Please note that incoming headers will never contain numbers but setting response headers allows nubmers (as they are converted internally to strings). Therefore using HttpHeaders in incoming message forces everyone to check if type is string or number.
I think we should keep two distinct types for incoming and outgoing headers.

Comment thread types/node/index.d.ts Outdated
}
// https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
export class OutgoingMessage extends stream.Writable {
output: any[];

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.

Not sure if it is a good idea to add non documented properties here which were not yet here before as they may change even on patch version of NodeJs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

since OutgoingMessage is not documented on Nodejs official document, I had hard time determine which should be kept in props.
I think we should keep props below for sure.

        upgrading: boolean;
        chunkedEncoding: boolean;
        shouldKeepAlive: boolean;
        useChunkedEncodingByDefault: boolean;
        sendDate: boolean;
        finished: boolean;
        headersSent: boolean;
        connection: net.Socket;

and remove output, outputEncodings, outputCallbacks.
@Flarna what to you think?

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 we should remove upgrading. chunkedEncoding. shouldKeepAlive, and useChunkedEncodingByDefault as they are undocumented and implementation details.
And socket (currently in ClientRequest) should be here.

Comment thread types/node/index.d.ts Outdated
detachSocket(socket: net.Socket): void;
// https://github.com/nodejs/node/blob/master/test/parallel/test-http-write-callbacks.js#L53
// no args in writeContinue callback
writeContinue(callback?: Function): void;

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.

Use () => voidfor callback.

Comment thread types/node/index.d.ts Outdated
/**
* @deprecated Docs-only deprecated: DEP0063
*/
writeHeader(statusCode: number, reasonPhrase?: string, headers?: HttpHeaders): void;

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 add deprecated APIs here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it's still work in current version of node.
so I think it would be better we still support it

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 would not add deprecated APIs (in special as they are no longer in the docs) as this "invites" to us it. Same is valid for other internal/private properties.
But this is just my vote.

@wwwy3y3 wwwy3y3 Jul 21, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

make sense. going to remove deprecated

Comment thread types/node/index.d.ts Outdated

// https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
export class ClientRequest extends OutgoingMessage {
agent: Agent;

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.

Not sure if adding non document properties here is a good idea as they may change without notice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

maybe just props I found on document?

request.aborted
request.connection
request.socket

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 so yes. Undocumented stuff should be anyway not used therefore no need to provide typings.

@Flarna

Flarna commented Jul 21, 2017

Copy link
Copy Markdown
Contributor

Nice work! I reviewed once more and had a few more comments.
Please note that I'm on vacation for a week therefore most likely no more comments from my side.

…aders to incoming and outgoing, fix rejectUnauthorized prop misplaced, use () => void for callback
@wwwy3y3

wwwy3y3 commented Jul 21, 2017

Copy link
Copy Markdown
Contributor Author

@Flarna all fixed!

@typescript-bot typescript-bot added The Travis CI build failed Revision needed This PR needs code changes before it can be merged. labels Jul 21, 2017
@typescript-bot

typescript-bot commented Jul 21, 2017 •

Copy link
Copy Markdown
Contributor

@wwwy3y3 Please address the merge conflict.

@typescript-bot typescript-bot added Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. Revision needed This PR needs code changes before it can be merged. and removed The Travis CI build failed Revision needed This PR needs code changes before it can be merged. labels Jul 21, 2017
@typescript-bot typescript-bot removed Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. Revision needed This PR needs code changes before it can be merged. labels Jul 21, 2017
Comment thread types/node/index.d.ts Outdated
export class Server extends net.Server {
constructor(requestListener?: (req: IncomingMessage, res: ServerResponse) => void);

setTimeout(msecs: number, callback: () => void): NodeJS.Timer;

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.

setTimeout is returning this.

Comment thread types/node/index.d.ts Outdated
trailers: { [key: string]: string };
rawTrailers: string[];
setTimeout(msecs: number, callback: Function): NodeJS.Timer;
setTimeout(msecs: number, callback: () => void): NodeJS.Timer;

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.

setTimeout() returns this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Flarna fix

@typescript-bot

Copy link
Copy Markdown
Contributor

Approved by a listed owner. PR appears ready to merge pending express review by a maintainer.

@ghost

ghost commented Jul 24, 2017

Copy link
Copy Markdown

Thanks!

@ghost
ghost merged commit dfda34b into DefinitelyTyped:master Jul 24, 2017
milosdanilov pushed a commit to milosdanilov/DefinitelyTyped that referenced this pull request Aug 5, 2017
…se, http.ClientRequest, http.IncomingMessage, http.ClientResponse fix from interface to class and add constructors. (DefinitelyTyped#18281)

* [node] net.Server, http.Server, http.ServerRequest, http.ServerResponse, http.ClientRequest, http.IncomingMessage, http.ClientResponse, http.Server fix from interface to class and add constructors.

* add http constructor tests

* fix lint

* add author

* fix: remove rejectUnauthorized,
createConnection callback make more detailed,
http.Server requestListener more detailed,
add export OutgoingMessage, ServerResponse and ClientRequest extends OutgoingMessage.
combine the constructors in ClientRequest

* add more http tests

* remove trailing space

* remove deprecated apis, private props not documented, seperate httpHeaders to incoming and outgoing, fix rejectUnauthorized prop misplaced, use () => void for callback

* typo

* settimeout return this
@ghost ghost mentioned this pull request Oct 18, 2017
8 tasks done
KSXGitHub pushed a commit to KSXGitHub/DefinitelyTyped that referenced this pull request May 12, 2018
…se, http.ClientRequest, http.IncomingMessage, http.ClientResponse fix from interface to class and add constructors. (DefinitelyTyped#18281)

* [node] net.Server, http.Server, http.ServerRequest, http.ServerResponse, http.ClientRequest, http.IncomingMessage, http.ClientResponse, http.Server fix from interface to class and add constructors.

* add http constructor tests

* fix lint

* add author

* fix: remove rejectUnauthorized,
createConnection callback make more detailed,
http.Server requestListener more detailed,
add export OutgoingMessage, ServerResponse and ClientRequest extends OutgoingMessage.
combine the constructors in ClientRequest

* add more http tests

* remove trailing space

* remove deprecated apis, private props not documented, seperate httpHeaders to incoming and outgoing, fix rejectUnauthorized prop misplaced, use () => void for callback

* typo

* settimeout return this
This pull request was closed.
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

Successfully merging this pull request may close these issues.

4 participants