Skip to content

Commit

Permalink
🤖 Merge PR #62654 [node] Add type side of global Blob, along with tes…
Browse files Browse the repository at this point in the history
…ts by @thw0rted
  • Loading branch information
thw0rted committed Oct 12, 2022
1 parent 5b031a4 commit 3123ee3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
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

0 comments on commit 3123ee3

Please sign in to comment.