@@ -16,6 +16,7 @@ import {FormData as FormDataNode} from 'formdata-polyfill/esm.min.js';
16
16
import delay from 'delay' ;
17
17
import AbortControllerMysticatea from 'abort-controller' ;
18
18
import abortControllerPolyfill from 'abortcontroller-polyfill/dist/abortcontroller.js' ;
19
+ import { text } from 'stream-consumers' ;
19
20
20
21
// Test subjects
21
22
import Blob from 'fetch-blob' ;
@@ -36,6 +37,7 @@ import TestServer from './utils/server.js';
36
37
import chaiTimeout from './utils/chai-timeout.js' ;
37
38
38
39
const AbortControllerPolyfill = abortControllerPolyfill . AbortController ;
40
+ const encoder = new TextEncoder ( ) ;
39
41
40
42
function isNodeLowerThan ( version ) {
41
43
return ! ~ process . version . localeCompare ( version , undefined , { numeric : true } ) ;
@@ -51,18 +53,6 @@ chai.use(chaiString);
51
53
chai . use ( chaiTimeout ) ;
52
54
const { expect} = chai ;
53
55
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
-
66
56
describe ( 'node-fetch' , ( ) => {
67
57
const local = new TestServer ( ) ;
68
58
let base ;
@@ -1314,25 +1304,7 @@ describe('node-fetch', () => {
1314
1304
} ) ;
1315
1305
} ) ;
1316
1306
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
-
1334
1307
it ( 'should allow POST request with ArrayBuffer body' , ( ) => {
1335
- const encoder = new TextEncoder ( ) ;
1336
1308
const url = `${ base } inspect` ;
1337
1309
const options = {
1338
1310
method : 'POST' ,
@@ -1351,7 +1323,7 @@ describe('node-fetch', () => {
1351
1323
const url = `${ base } inspect` ;
1352
1324
const options = {
1353
1325
method : 'POST' ,
1354
- body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) ) . buffer
1326
+ body : new VMUint8Array ( encoder . encode ( 'Hello, world!\n' ) ) . buffer
1355
1327
} ;
1356
1328
return fetch ( url , options ) . then ( res => res . json ( ) ) . then ( res => {
1357
1329
expect ( res . method ) . to . equal ( 'POST' ) ;
@@ -1363,7 +1335,6 @@ describe('node-fetch', () => {
1363
1335
} ) ;
1364
1336
1365
1337
it ( 'should allow POST request with ArrayBufferView (Uint8Array) body' , ( ) => {
1366
- const encoder = new TextEncoder ( ) ;
1367
1338
const url = `${ base } inspect` ;
1368
1339
const options = {
1369
1340
method : 'POST' ,
@@ -1379,7 +1350,6 @@ describe('node-fetch', () => {
1379
1350
} ) ;
1380
1351
1381
1352
it ( 'should allow POST request with ArrayBufferView (DataView) body' , ( ) => {
1382
- const encoder = new TextEncoder ( ) ;
1383
1353
const url = `${ base } inspect` ;
1384
1354
const options = {
1385
1355
method : 'POST' ,
@@ -1398,7 +1368,7 @@ describe('node-fetch', () => {
1398
1368
const url = `${ base } inspect` ;
1399
1369
const options = {
1400
1370
method : 'POST' ,
1401
- body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) )
1371
+ body : new VMUint8Array ( encoder . encode ( 'Hello, world!\n' ) )
1402
1372
} ;
1403
1373
return fetch ( url , options ) . then ( res => res . json ( ) ) . then ( res => {
1404
1374
expect ( res . method ) . to . equal ( 'POST' ) ;
@@ -1410,7 +1380,6 @@ describe('node-fetch', () => {
1410
1380
} ) ;
1411
1381
1412
1382
it ( 'should allow POST request with ArrayBufferView (Uint8Array, offset, length) body' , ( ) => {
1413
- const encoder = new TextEncoder ( ) ;
1414
1383
const url = `${ base } inspect` ;
1415
1384
const options = {
1416
1385
method : 'POST' ,
@@ -1846,39 +1815,28 @@ describe('node-fetch', () => {
1846
1815
} ) ;
1847
1816
} ) ;
1848
1817
1849
- it ( 'should allow piping response body as stream' , ( ) => {
1818
+ it ( 'should allow piping response body as stream' , async ( ) => {
1850
1819
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' ) ;
1861
1824
} ) ;
1862
1825
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 ( ) => {
1864
1827
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 ) ;
1873
1832
1874
- expect ( chunk . toString ( ) ) . to . equal ( 'world' ) ;
1875
- } ;
1833
+ const [ t1 , t2 ] = await Promise . all ( [
1834
+ text ( res . body ) ,
1835
+ text ( r1 . body )
1836
+ ] ) ;
1876
1837
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' ) ;
1882
1840
} ) ;
1883
1841
1884
1842
it ( 'should allow cloning a json response and log it as text response' , ( ) => {
@@ -2141,13 +2099,10 @@ describe('node-fetch', () => {
2141
2099
} ) ;
2142
2100
} ) ;
2143
2101
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' ) ;
2151
2106
} ) ;
2152
2107
2153
2108
it ( 'should support blob round-trip' , ( ) => {
@@ -2233,7 +2188,7 @@ describe('node-fetch', () => {
2233
2188
// Issue #414
2234
2189
it ( 'should reject if attempt to accumulate body stream throws' , ( ) => {
2235
2190
const res = new Response ( stream . Readable . from ( ( async function * ( ) {
2236
- yield Buffer . from ( 'tada' ) ;
2191
+ yield encoder . encode ( 'tada' ) ;
2237
2192
await new Promise ( resolve => {
2238
2193
setTimeout ( resolve , 200 ) ;
2239
2194
} ) ;
@@ -2329,7 +2284,7 @@ describe('node-fetch', () => {
2329
2284
size : 1024
2330
2285
} ) ;
2331
2286
2332
- const bufferBody = Buffer . from ( bodyContent ) ;
2287
+ const bufferBody = encoder . encode ( bodyContent ) ;
2333
2288
const bufferRequest = new Request ( url , {
2334
2289
method : 'POST' ,
2335
2290
body : bufferBody ,
0 commit comments