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

node: process.stdin is a stream.Readable and process.stdout is a stream.Writable #20493

Merged
5 commits merged into from
Oct 17, 2017

Conversation

dex4er
Copy link
Contributor

@dex4er dex4er commented Oct 11, 2017

New interfaces NodeJS.Readable and NodeJS.Writable have the same properties
as in stream.Readable and stream.Writable classes.

Interface NodeJS.Socket is just a NodeJS.ReadWriteStream. But new
interface NodeJS.TtyStream (TtyWriteStream and TtyReadStream) has
extra properties as in tty.ReadStream and tty.WriteStream.

process.stdin implements NodeJS.TtyReadStream and
process.stdout implements NodeJS.TtyWriteStream.

net.Socket, dgram.Socket, fs.Stats, fs.ReadStream and fs.WriteStream
are classes.

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 adding a new definition:

  • The package does not provide its own types, and you can not add them.
  • If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
  • Create it with dts-gen --dt, not by basing it on an existing project.
  • tslint.json should be present, and tsconfig.json should have noImplicitAny, noImplicitThis, and strictNullChecks set to true.

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: <>
  • Increase the version number in the header if appropriate.
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }.

If removing a declaration:

  • If a package was never on DefinitelyTyped, you don't need to do anything. (If you wrote a package and provided types, you don't need to register it with us.)
  • Delete the package's directory.
  • Add it to notNeededPackages.json.

@dex4er dex4er requested a review from vvakame as a code owner October 11, 2017 14:46
@dt-bot
Copy link
Member

dt-bot commented Oct 11, 2017

types/hexo-fs/index.d.ts

to author (@segayuu). Could you review this PR?
👍 or 👎?


types/node/index.d.ts

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

@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). The Travis CI build failed labels Oct 11, 2017
@typescript-bot
Copy link
Contributor

@dex4er Please fix the failures indicated in the Travis CI log.

@dex4er
Copy link
Contributor Author

dex4er commented Oct 11, 2017

Travis died because of current problems in master branch.

This pull request should fix #20496 and #20487

@@ -5033,7 +5084,7 @@ declare module "stream" {
isPaused(): boolean;
unpipe<T extends NodeJS.WritableStream>(destination?: T): this;
unshift(chunk: any): void;
wrap(oldStream: NodeJS.ReadableStream): Readable;
wrap(oldStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be not correct. warp method returns a Readable class or even better this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so maybe better is wrap<T extends NodeJS.ReadableStream>(oldStream: NodeJS.ReadableStream): T

It will break some modules which define wrap method, but it is very easy to fix (maybe 3 modules in DefinitelyTyped repo)

Copy link
Contributor

@Flarna Flarna Oct 12, 2017

Choose a reason for hiding this comment

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

I think it should be wrap(oldStream: NodeJS.ReadableStream): this as this is how it is implemented (and documented).

openStdin(): Socket;
stdout: StdWriteStream;
stderr: StdWriteStream;
stdin: StdReadStream;
Copy link
Contributor

Choose a reason for hiding this comment

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

The StdReadStream you defined has a setRawMode() and isRaw like tty.ReadStream.
If you pipe in a file to node process.stdin doesn't have them.

@Flarna
Copy link
Contributor

Flarna commented Oct 11, 2017

I wonder why there are new types added as NodeJs docu tells that process.stdin is either a net.Socket or a stream.Readable. Can't we use them here instead of creating new interfaces?

@dex4er
Copy link
Contributor Author

dex4er commented Oct 12, 2017

@Flarna It would be better to use

stdin: fs.ReadStream | tty.Socket;

but NodeJS are built with interfaces only. So:

stdin: NodeJS.ReadStream | NodeJS.Socket;

but... it is very inconvenient. It would mean that I had to check the instanceof or use duck typing:

console.log(process.stdin.isTTY); // fails because not defined in fs.ReadStream
console.log(process.stdin.path); // fails because not defined in net.Socket

So that was idea behind TtyReadStream: it has properties from both fs.ReadStream and net.Socket, so it is perfectly legal:

console.log(process.stdin.isTTY); // undefined when stdin is fs.ReadStream
console.log(process.stdin.path); // undefined when stdin is net.Socket

@Flarna
Copy link
Contributor

Flarna commented Oct 12, 2017

With your proposed solution it's valid to write process.stdin.setRawMode(false) but it may crash depending on the type of stream stdin points to.
Hopefully I find some time soon to play around a little and provide some more concrete proposals.

@dex4er
Copy link
Contributor Author

dex4er commented Oct 12, 2017

@Flarna Contre-proposition is to really make NodeJS.Process.stdin as fs.ReadStream | tty.ReadStream alternative and then you couldn't do:

if (process.stdin.isTTY) console('this is tty');

and you should replace it with:

if (process.stdin instanceof tty.ReadStream) console('this is tty');

Maybe this is the right way in Typescript and we should not take all patterns from Javascript blindly.

@Flarna
Copy link
Contributor

Flarna commented Oct 12, 2017

I did a closer look into the various definition of streams. Hopefully I understand your problem and proposal better now.

One issue is that it seems we can't use the "correct" types net.Socket, ... in namespace NodeJS as they are not visible and can't be imported.
Workaround is to define interfaces in NodeJS which look like the classes in stream/fs/net but it seems there got out of sync over time.
I think instead of creating new interfaces Readable and Writeable extending the existing ones (e.a. ReadableStream and WriteableStream it would be better to simply updated the existing ones with the missing properties.
In current definition properties isTTY, columns, rows, isRaw and setRawMode were alredy present but optional. I think they should be kept optional as they are in fact not always there.

Maybe an alternative to keep interfaces in NodeJS and classes in stream/... in sync would be to define the classes already in NodeJS and re-export the type and constructor in stream/... But I'm not sure which side effects this may have.

@dex4er
Copy link
Contributor Author

dex4er commented Oct 13, 2017

@Flarna ok so this is another try, after rebase to the newest master:

  • stream, fs, net and dgram have classes, not interfaces, so instanceof fs.ReadStream is possible.
  • NodeJS.ReadableStream and WritableStream are simple interfaces so it is possible to write own ReadableStream without extra methods (_read or _write).
  • NodeJS.ReadStream and WriteStream were used only for process.stdin and process.stdout and now have defined all missing methods so the interface is compatible with stream.readable.

Now most of problems are fixed and changed don't break other modules, except firebird and some of them that reimplemented net.Socket class because of bug in @types/node.

@Flarna
Copy link
Contributor

Flarna commented Oct 13, 2017

Thanks! Hope this still solves the problems you have seen.
Changes in node look fine to me but I think the changes in firebird should be reviewed by an firebird expert (@karak).

@karak
Copy link
Contributor

karak commented Oct 13, 2017

@Flarna I'm sorry to say I never used the stream and blob APIs. Actually, I just followed the reference document of node-firebird( https://github.com/xdenser/node-firebird-libfbclient#stream-object) about them.

It's author @xdenser could help about the relation between Firebird Streams and NodeJS Streams.

APPEND:
I just walked through the .js and compare it with the new .ts, and the function overrides seem fine, as you may know.

@dex4er
Copy link
Contributor Author

dex4er commented Oct 13, 2017

@karak @Flarna I made the changes for firebird based on the current code https://github.com/xdenser/node-firebird-libfbclient/blob/master/firebird.js#L170

fb.Stream extends stream.Stream and implements few methods. Exactly the same as in the typings from this patch.

@@ -426,11 +426,6 @@ export function writeFile(
*/
export function writeFileSync(path: string, data: any, options?: string | { encoding?: string | null; mode?: string | number; flag?: string }): void;

// Static classes
export let Stats: Stats;
Copy link

Choose a reason for hiding this comment

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

This appears to remove an exported variable from an unrelated module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can reexport class from fs to get the same behaviour as before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is now rebased and fixed

@dex4er dex4er closed this Oct 17, 2017
@dex4er dex4er deleted the node_streams branch October 17, 2017 12:51
@dex4er dex4er restored the node_streams branch October 17, 2017 12:52
@dex4er dex4er reopened this Oct 17, 2017
@ghost
Copy link

ghost commented Oct 17, 2017

Thanks!

@ghost ghost merged commit 8ef0c39 into DefinitelyTyped:master Oct 17, 2017
@dex4er dex4er deleted the node_streams branch October 23, 2017 12:18
KSXGitHub pushed a commit to KSXGitHub/DefinitelyTyped that referenced this pull request May 12, 2018
…am.Writable (DefinitelyTyped#20493)

* Readable.wrap returns this

* Add missing properties to interface ReadableStream and WritableStream; change interfaces in fs, dgram and net into classes

* Move all additional properties from ReadableStream interface to ReadStream interface (and Write...)

* hexo-fs should re-export classes from 'fs'

* process.stdin._destroy is a function
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Popular package This PR affects a popular package (as counted by NPM download counts).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants