11import { deepStrictEqual } from "assert" ;
22import { decodeAsync , encode , decodeArrayStream } from "@msgpack/msgpack" ;
3-
3+ import { ReadableStream as PonyReadableStream } from "web-streams-polyfill/ponyfill" ;
44const isReadableStreamConstructorAvailable : boolean = ( ( ) => {
55 try {
66 // Edge <= 18 has ReadableStream but its constructor is not available
@@ -13,24 +13,28 @@ const isReadableStreamConstructorAvailable: boolean = (() => {
1313 }
1414} ) ( ) ;
1515
16- describe ( "whatwg streams" , ( ) => {
17- before ( function ( ) {
18- if ( ! isReadableStreamConstructorAvailable ) {
19- this . skip ( ) ;
20- }
21- } ) ;
16+ const MyReadableStream : typeof ReadableStream = isReadableStreamConstructorAvailable
17+ ? ReadableStream
18+ : PonyReadableStream ;
2219
23- it ( "decodeAsync" , async ( ) => {
20+ // Downgrade stream not to implement AsycIterable<T>
21+ function downgradeReadableStream ( stream : ReadableStream ) {
22+ ( stream as any ) [ Symbol . asyncIterator ] = undefined ;
23+ }
24+
25+ describe ( "whatwg streams" , ( ) => {
26+ it ( "decodeArrayStream" , async ( ) => {
2427 const data = [ 1 , 2 , 3 ] ;
2528 const encoded = encode ( data ) ;
26- const stream = new ReadableStream ( {
29+ const stream = new MyReadableStream ( {
2730 start ( controller ) {
2831 for ( const byte of encoded ) {
2932 controller . enqueue ( [ byte ] ) ;
3033 }
3134 controller . close ( ) ;
3235 } ,
3336 } ) ;
37+ downgradeReadableStream ( stream ) ;
3438
3539 const items : Array < unknown > = [ ] ;
3640 for await ( const item of decodeArrayStream ( stream ) ) {
@@ -39,17 +43,18 @@ describe("whatwg streams", () => {
3943 deepStrictEqual ( items , data ) ;
4044 } ) ;
4145
42- it ( "reads from stream " , async ( ) => {
46+ it ( "decodeAsync " , async ( ) => {
4347 const data = [ 1 , 2 , 3 ] ;
4448 const encoded = encode ( data ) ;
45- const stream = new ReadableStream ( {
49+ const stream = new MyReadableStream ( {
4650 start ( controller ) {
4751 for ( const byte of encoded ) {
4852 controller . enqueue ( [ byte ] ) ;
4953 }
5054 controller . close ( ) ;
5155 } ,
5256 } ) ;
57+ downgradeReadableStream ( stream ) ;
5358
5459 deepStrictEqual ( await decodeAsync ( stream ) , data ) ;
5560 } ) ;
0 commit comments