99
1010/* global Bun */
1111
12+ import type { Writable } from 'stream' ;
13+
1214type BunReadableStreamController = ReadableStreamController & {
1315end ( ) : mixed ,
1416write ( data : Chunk | BinaryChunk ) : void ,
1517error ( error : Error ) : void ,
1618flush ?: ( ) => void ,
1719} ;
18- export type Destination = BunReadableStreamController ;
20+
21+ interface MightBeFlushable {
22+ flush ?: ( ) => void ;
23+ }
24+
25+ export type Destination =
26+ | BunReadableStreamController
27+ | ( Writable & MightBeFlushable ) ;
1928
2029export type PrecomputedChunk = string ;
2130export opaque type Chunk = string ;
@@ -46,13 +55,15 @@ export function writeChunk(
4655return ;
4756}
4857
58+ // $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
4959destination . write ( chunk ) ;
5060}
5161
5262export function writeChunkAndReturn (
5363destination : Destination ,
5464chunk : PrecomputedChunk | Chunk | BinaryChunk ,
5565) : boolean {
66+ // $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
5667return ! ! destination . write ( chunk ) ;
5768}
5869
@@ -86,11 +97,21 @@ export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
8697}
8798
8899export function closeWithError ( destination : Destination , error : mixed ) : void {
100+ // $FlowFixMe[incompatible-use]
89101// $FlowFixMe[method-unbinding]
90102if ( typeof destination . error === 'function' ) {
91103// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
92104destination . error ( error ) ;
93- } else {
105+
106+ // $FlowFixMe[incompatible-use]
107+ // $FlowFixMe[method-unbinding]
108+ } else if ( typeof destination . destroy === 'function' ) {
109+ // $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
110+ destination . destroy ( error ) ;
111+
112+ // $FlowFixMe[incompatible-use]
113+ // $FlowFixMe[method-unbinding]
114+ } else if ( typeof destination . close === 'function' ) {
94115// Earlier implementations doesn't support this method. In that environment you're
95116// supposed to throw from a promise returned but we don't return a promise in our
96117// approach. We could fork this implementation but this is environment is an edge
@@ -101,7 +122,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
101122}
102123}
103124
104- export function createFastHash ( input : string ) : string | number {
125+ export function createFastHash ( input : string ) : number {
105126return Bun . hash ( input ) ;
106127}
107128
0 commit comments