@@ -16,6 +16,7 @@ import {FormData as FormDataNode} from 'formdata-polyfill/esm.min.js';
1616import delay from 'delay' ;
1717import AbortControllerMysticatea from 'abort-controller' ;
1818import abortControllerPolyfill from 'abortcontroller-polyfill/dist/abortcontroller.js' ;
19+ import { text } from 'stream-consumers' ;
1920
2021// Test subjects
2122import Blob from 'fetch-blob' ;
@@ -36,6 +37,7 @@ import TestServer from './utils/server.js';
3637import chaiTimeout from './utils/chai-timeout.js' ;
3738
3839const AbortControllerPolyfill = abortControllerPolyfill . AbortController ;
40+ const encoder = new TextEncoder ( ) ;
3941
4042function isNodeLowerThan ( version ) {
4143 return ! ~ process . version . localeCompare ( version , undefined , { numeric : true } ) ;
@@ -51,18 +53,6 @@ chai.use(chaiString);
5153chai . use ( chaiTimeout ) ;
5254const { expect} = chai ;
5355
54- function streamToPromise ( stream , dataHandler ) {
55- return new Promise ( ( resolve , reject ) => {
56- stream . on ( 'data' , ( ...args ) => {
57- Promise . resolve ( )
58- . then ( ( ) => dataHandler ( ...args ) )
59- . catch ( reject ) ;
60- } ) ;
61- stream . on ( 'end' , resolve ) ;
62- stream . on ( 'error' , reject ) ;
63- } ) ;
64- }
65-
6656describe ( 'node-fetch' , ( ) => {
6757 const local = new TestServer ( ) ;
6858 let base ;
@@ -1314,25 +1304,7 @@ describe('node-fetch', () => {
13141304 } ) ;
13151305 } ) ;
13161306
1317- it ( 'should allow POST request with buffer body' , ( ) => {
1318- const url = `${ base } inspect` ;
1319- const options = {
1320- method : 'POST' ,
1321- body : Buffer . from ( 'a=1' , 'utf-8' )
1322- } ;
1323- return fetch ( url , options ) . then ( res => {
1324- return res . json ( ) ;
1325- } ) . then ( res => {
1326- expect ( res . method ) . to . equal ( 'POST' ) ;
1327- expect ( res . body ) . to . equal ( 'a=1' ) ;
1328- expect ( res . headers [ 'transfer-encoding' ] ) . to . be . undefined ;
1329- expect ( res . headers [ 'content-type' ] ) . to . be . undefined ;
1330- expect ( res . headers [ 'content-length' ] ) . to . equal ( '3' ) ;
1331- } ) ;
1332- } ) ;
1333-
13341307 it ( 'should allow POST request with ArrayBuffer body' , ( ) => {
1335- const encoder = new TextEncoder ( ) ;
13361308 const url = `${ base } inspect` ;
13371309 const options = {
13381310 method : 'POST' ,
@@ -1351,7 +1323,7 @@ describe('node-fetch', () => {
13511323 const url = `${ base } inspect` ;
13521324 const options = {
13531325 method : 'POST' ,
1354- body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) ) . buffer
1326+ body : new VMUint8Array ( encoder . encode ( 'Hello, world!\n' ) ) . buffer
13551327 } ;
13561328 return fetch ( url , options ) . then ( res => res . json ( ) ) . then ( res => {
13571329 expect ( res . method ) . to . equal ( 'POST' ) ;
@@ -1363,7 +1335,6 @@ describe('node-fetch', () => {
13631335 } ) ;
13641336
13651337 it ( 'should allow POST request with ArrayBufferView (Uint8Array) body' , ( ) => {
1366- const encoder = new TextEncoder ( ) ;
13671338 const url = `${ base } inspect` ;
13681339 const options = {
13691340 method : 'POST' ,
@@ -1379,7 +1350,6 @@ describe('node-fetch', () => {
13791350 } ) ;
13801351
13811352 it ( 'should allow POST request with ArrayBufferView (DataView) body' , ( ) => {
1382- const encoder = new TextEncoder ( ) ;
13831353 const url = `${ base } inspect` ;
13841354 const options = {
13851355 method : 'POST' ,
@@ -1398,7 +1368,7 @@ describe('node-fetch', () => {
13981368 const url = `${ base } inspect` ;
13991369 const options = {
14001370 method : 'POST' ,
1401- body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) )
1371+ body : new VMUint8Array ( encoder . encode ( 'Hello, world!\n' ) )
14021372 } ;
14031373 return fetch ( url , options ) . then ( res => res . json ( ) ) . then ( res => {
14041374 expect ( res . method ) . to . equal ( 'POST' ) ;
@@ -1410,7 +1380,6 @@ describe('node-fetch', () => {
14101380 } ) ;
14111381
14121382 it ( 'should allow POST request with ArrayBufferView (Uint8Array, offset, length) body' , ( ) => {
1413- const encoder = new TextEncoder ( ) ;
14141383 const url = `${ base } inspect` ;
14151384 const options = {
14161385 method : 'POST' ,
@@ -1846,39 +1815,28 @@ describe('node-fetch', () => {
18461815 } ) ;
18471816 } ) ;
18481817
1849- it ( 'should allow piping response body as stream' , ( ) => {
1818+ it ( 'should allow piping response body as stream' , async ( ) => {
18501819 const url = `${ base } hello` ;
1851- return fetch ( url ) . then ( res => {
1852- expect ( res . body ) . to . be . an . instanceof ( stream . Transform ) ;
1853- return streamToPromise ( res . body , chunk => {
1854- if ( chunk === null ) {
1855- return ;
1856- }
1857-
1858- expect ( chunk . toString ( ) ) . to . equal ( 'world' ) ;
1859- } ) ;
1860- } ) ;
1820+ const res = await fetch ( url ) ;
1821+ expect ( res . body ) . to . be . an . instanceof ( stream . Transform ) ;
1822+ const body = await text ( res . body ) ;
1823+ expect ( body ) . to . equal ( 'world' ) ;
18611824 } ) ;
18621825
1863- it ( 'should allow cloning a response, and use both as stream' , ( ) => {
1826+ it ( 'should allow cloning a response, and use both as stream' , async ( ) => {
18641827 const url = `${ base } hello` ;
1865- return fetch ( url ) . then ( res => {
1866- const r1 = res . clone ( ) ;
1867- expect ( res . body ) . to . be . an . instanceof ( stream . Transform ) ;
1868- expect ( r1 . body ) . to . be . an . instanceof ( stream . Transform ) ;
1869- const dataHandler = chunk => {
1870- if ( chunk === null ) {
1871- return ;
1872- }
1828+ const res = await fetch ( url ) ;
1829+ const r1 = res . clone ( ) ;
1830+ expect ( res . body ) . to . be . an . instanceof ( stream . Transform ) ;
1831+ expect ( r1 . body ) . to . be . an . instanceof ( stream . Transform ) ;
18731832
1874- expect ( chunk . toString ( ) ) . to . equal ( 'world' ) ;
1875- } ;
1833+ const [ t1 , t2 ] = await Promise . all ( [
1834+ text ( res . body ) ,
1835+ text ( r1 . body )
1836+ ] ) ;
18761837
1877- return Promise . all ( [
1878- streamToPromise ( res . body , dataHandler ) ,
1879- streamToPromise ( r1 . body , dataHandler )
1880- ] ) ;
1881- } ) ;
1838+ expect ( t1 ) . to . equal ( 'world' ) ;
1839+ expect ( t2 ) . to . equal ( 'world' ) ;
18821840 } ) ;
18831841
18841842 it ( 'should allow cloning a json response and log it as text response' , ( ) => {
@@ -2141,13 +2099,10 @@ describe('node-fetch', () => {
21412099 } ) ;
21422100 } ) ;
21432101
2144- it ( 'should support reading blob as stream' , ( ) => {
2145- return new Response ( 'hello' )
2146- . blob ( )
2147- . then ( blob => streamToPromise ( stream . Readable . from ( blob . stream ( ) ) , data => {
2148- const string = Buffer . from ( data ) . toString ( ) ;
2149- expect ( string ) . to . equal ( 'hello' ) ;
2150- } ) ) ;
2102+ it ( 'should support reading blob as stream' , async ( ) => {
2103+ const blob = await new Response ( 'hello' ) . blob ( ) ;
2104+ const str = await text ( blob . stream ( ) ) ;
2105+ expect ( str ) . to . equal ( 'hello' ) ;
21512106 } ) ;
21522107
21532108 it ( 'should support blob round-trip' , ( ) => {
@@ -2233,7 +2188,7 @@ describe('node-fetch', () => {
22332188 // Issue #414
22342189 it ( 'should reject if attempt to accumulate body stream throws' , ( ) => {
22352190 const res = new Response ( stream . Readable . from ( ( async function * ( ) {
2236- yield Buffer . from ( 'tada' ) ;
2191+ yield encoder . encode ( 'tada' ) ;
22372192 await new Promise ( resolve => {
22382193 setTimeout ( resolve , 200 ) ;
22392194 } ) ;
@@ -2329,7 +2284,7 @@ describe('node-fetch', () => {
23292284 size : 1024
23302285 } ) ;
23312286
2332- const bufferBody = Buffer . from ( bodyContent ) ;
2287+ const bufferBody = encoder . encode ( bodyContent ) ;
23332288 const bufferRequest = new Request ( url , {
23342289 method : 'POST' ,
23352290 body : bufferBody ,
0 commit comments