-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
feat(@types/node): add stream/consumers
#55311
Merged
typescript-bot
merged 1 commit into
DefinitelyTyped:master
from
favna:feat/add-stream-consumers-to-node
Aug 27, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Duplicates of interface in lib.dom.ts. | ||
// Duplicated here rather than referencing lib.dom.ts because doing so causes lib.dom.ts to be loaded for "test-all" | ||
// Which in turn causes tests to pass that shouldn't pass. | ||
// | ||
// This interface is not, and should not be, exported. | ||
interface Blob { | ||
readonly size: number; | ||
readonly type: string; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
slice(start?: number, end?: number, contentType?: string): Blob; | ||
stream(): NodeJS.ReadableStream; | ||
text(): Promise<string>; | ||
} | ||
|
||
declare module 'stream/consumers' { | ||
import { Readable } from 'node:stream'; | ||
function buffer(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<Buffer>; | ||
function text(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<string>; | ||
function arrayBuffer(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<ArrayBuffer>; | ||
function blob(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<Blob>; | ||
function json(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<unknown>; | ||
} | ||
declare module 'node:stream/consumers' { | ||
export * from 'stream/consumers'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this comment is accurate? It looks like this interface is leaking into global definitions:
I can't remember where I read it (maybe in a linter rule or something?), but I'm pretty sure all interfaces in a
.d.ts
file are exported, so that consumers have access to references to them. Very happy to be proven wrong on this point, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not only is that comment (about not being exported) inaccurate, the comment above it isn't accurate either. The interface is not a "duplicate" of Blob from
lib.dom.ts
, because the DOMBlob#stream
method returns a W3C ReadableStream, which is not the same thing as aNodeJS.ReadableStream
.As you point out, simply having
@types/node
in yournode_modules
pollutes frontend code with this Node-specific global type definition, which is merged with the real (DOM) Blob definition. As a result, I've had to any-cast the return value whenever I call.stream()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good news, the correct types have been added in the interim so this erroneous "duplicate" is no longer needed. I just opened #59905 which I think will resolve this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thw0rted With the benefit of the doubt I will think you didn't mean it as such but I feel called out here anyway, and so I feel a need to defend myself.
I specifically did not add myself to the contributors list for node types because I have no interest in being pinged for reviews and I have hardly any interest in Node's types to begin with as long as they work, and this has for many months.
Furthermore, even though yes it might be wrong that I made this change, that change was made in a pull request just like your own. That means it was reviewed and it was merged by a DT maintainer. Simply denotsting that I did bad here is therefore coming over as shallow because clearly other people were involved as well.
Lastly, as I have also stated elsewhere, this review comment was made after the pull request was already merged. It was therefore quite obviously never taken into full consideration and mostly completely dismissed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right that I didn't mean it as a personal attack -- reading back through my comment I can definitely see how you'd take it that way, and I'm sorry. (I can sympathize about not signing yourself up as a contributor, the scope is unlike anything else on DT!)
The situation is very confusing, and I don't know that anybody can really be blamed directly -- Node chose to add this partial implementation of some web APIs, and worse they're reusing web class names for incomplete implementations that aren't available at the same scope. When you made your PR you didn't even have a "Node Blob" to reference, so your approach was totally reasonable; bleeding into global was an honest mistake.
To make matters worse, I don't see any way to write a test (or I guess a "meta" test?) that would prevent this. Maybe the package.json or tsconfig file could have a field with a list of globals introduced by the package, and if anything else bleeds out, the test would fail? I don't really know enough about TS internals to suggest how to implement something like that but it might be worth kicking the idea around in Discussions -- I'll take a look.