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] Add type side of global Blob, along with tests #62654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions types/node/buffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ declare module 'buffer' {
export import atob = globalThis.atob;
export import btoa = globalThis.btoa;

import { Blob as _Blob } from 'buffer';
import { Blob as NodeBlob } from 'buffer';
// This conditional type will be the existing global Blob in a browser, or
// the copy below in a Node environment.
type __Blob = typeof globalThis extends { onmessage: any, Blob: infer T }
? T : NodeBlob;
global {
// Buffer class
type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
Expand Down Expand Up @@ -2235,6 +2239,7 @@ declare module 'buffer' {
*/
function btoa(data: string): string;

interface Blob extends __Blob {}
/**
* `Blob` class is a global reference for `require('node:buffer').Blob`
* https://nodejs.org/api/buffer.html#class-blob
Expand All @@ -2245,7 +2250,7 @@ declare module 'buffer' {
Blob: infer T;
}
? T
: typeof _Blob;
: typeof NodeBlob;
}
}
declare module 'node:buffer' {
Expand Down
4 changes: 2 additions & 2 deletions types/node/stream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
declare module 'stream' {
import { EventEmitter, Abortable } from 'node:events';
import { Blob } from "node:buffer";
import { Blob as NodeBlob } from "node:buffer";
import * as streamPromises from 'node:stream/promises';
import * as streamConsumers from 'node:stream/consumers';
import * as streamWeb from 'node:stream/web';
Expand Down Expand Up @@ -893,7 +893,7 @@ declare module 'stream' {
*
* @since v16.8.0
*/
static from(src: Stream | Blob | ArrayBuffer | string | Iterable<any> | AsyncIterable<any> | AsyncGeneratorFunction | Promise<any> | Object): Duplex;
static from(src: Stream | NodeBlob | ArrayBuffer | string | Iterable<any> | AsyncIterable<any> | AsyncGeneratorFunction | Promise<any> | Object): Duplex;
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
_writev?(
chunks: Array<{
Expand Down
5 changes: 4 additions & 1 deletion types/node/test/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Specifically test buffer module regression.
import {
Blob as NodeBlob,
Blob,
Buffer as ImportedBuffer,
constants,
kMaxLength,
Expand Down Expand Up @@ -309,6 +308,10 @@ async () => {
blob2.stream().locked; // $ExpectType boolean
};

// Ensure type-side of global Blob exists
declare const blob3: Blob;
blob3.stream();

{
atob(btoa('test')); // $ExpectType string
}
Expand Down
10 changes: 8 additions & 2 deletions types/node/ts4.8/buffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ declare module 'buffer' {
export import atob = globalThis.atob;
export import btoa = globalThis.btoa;

import { Blob as _Blob } from 'buffer';
import { Blob as NodeBlob } from 'buffer';
// This conditional type will be the existing global Blob in a browser, or
// the copy below in a Node environment.
type __Blob = typeof globalThis extends { onmessage: any, Blob: infer T }
? T : NodeBlob;

global {
// Buffer class
type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
Expand Down Expand Up @@ -2235,6 +2240,7 @@ declare module 'buffer' {
*/
function btoa(data: string): string;

interface Blob extends __Blob {}
/**
* `Blob` class is a global reference for `require('node:buffer').Blob`
* https://nodejs.org/api/buffer.html#class-blob
Expand All @@ -2245,7 +2251,7 @@ declare module 'buffer' {
Blob: infer T;
}
? T
: typeof _Blob;
: typeof NodeBlob;
}
}
declare module 'node:buffer' {
Expand Down
4 changes: 2 additions & 2 deletions types/node/ts4.8/stream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
declare module 'stream' {
import { EventEmitter, Abortable } from 'node:events';
import { Blob } from "node:buffer";
import { Blob as NodeBlob } from "node:buffer";
import * as streamPromises from 'node:stream/promises';
import * as streamConsumers from 'node:stream/consumers';
import * as streamWeb from 'node:stream/web';
Expand Down Expand Up @@ -893,7 +893,7 @@ declare module 'stream' {
*
* @since v16.8.0
*/
static from(src: Stream | Blob | ArrayBuffer | string | Iterable<any> | AsyncIterable<any> | AsyncGeneratorFunction | Promise<any> | Object): Duplex;
static from(src: Stream | NodeBlob | ArrayBuffer | string | Iterable<any> | AsyncIterable<any> | AsyncGeneratorFunction | Promise<any> | Object): Duplex;
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
_writev?(
chunks: Array<{
Expand Down
5 changes: 4 additions & 1 deletion types/node/ts4.8/test/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Specifically test buffer module regression.
import {
Blob as NodeBlob,
Blob,
Buffer as ImportedBuffer,
constants,
kMaxLength,
Expand Down Expand Up @@ -309,6 +308,10 @@ async () => {
blob2.stream().locked; // $ExpectType boolean
};

// Ensure type-side of global Blob exists
declare const blob3: Blob;
blob3.stream();

{
atob(btoa('test')); // $ExpectType string
}
Expand Down