11'use strict'
22
33const util = require ( '../core/util' )
4- const { kBodyUsed } = require ( '../core/symbols' )
54const assert = require ( 'node:assert' )
65const { InvalidArgumentError } = require ( '../core/errors' )
7- const EE = require ( 'node:events' )
86
97const redirectableStatusCodes = [ 300 , 301 , 302 , 303 , 307 , 308 ]
108
11- const kBody = Symbol ( 'body' )
12-
139const noop = ( ) => { }
1410
15- class BodyAsyncIterable {
16- constructor ( body ) {
17- this [ kBody ] = body
18- this [ kBodyUsed ] = false
19- }
20-
21- async * [ Symbol . asyncIterator ] ( ) {
22- assert ( ! this [ kBodyUsed ] , 'disturbed' )
23- this [ kBodyUsed ] = true
24- yield * this [ kBody ]
25- }
26- }
27-
2811class RedirectHandler {
2912 static buildDispatch ( dispatcher , maxRedirections ) {
3013 if ( maxRedirections != null && ( ! Number . isInteger ( maxRedirections ) || maxRedirections < 0 ) ) {
@@ -44,43 +27,10 @@ class RedirectHandler {
4427 this . location = null
4528 const { maxRedirections : _ , ...cleanOpts } = opts
4629 this . opts = cleanOpts // opts must be a copy, exclude maxRedirections
30+ this . opts . body = util . wrapRequestBody ( this . opts . body )
4731 this . maxRedirections = maxRedirections
4832 this . handler = handler
4933 this . history = [ ]
50-
51- if ( util . isStream ( this . opts . body ) ) {
52- // TODO (fix): Provide some way for the user to cache the file to e.g. /tmp
53- // so that it can be dispatched again?
54- // TODO (fix): Do we need 100-expect support to provide a way to do this properly?
55- if ( util . bodyLength ( this . opts . body ) === 0 ) {
56- this . opts . body
57- . on ( 'data' , function ( ) {
58- assert ( false )
59- } )
60- }
61-
62- if ( typeof this . opts . body . readableDidRead !== 'boolean' ) {
63- this . opts . body [ kBodyUsed ] = false
64- EE . prototype . on . call ( this . opts . body , 'data' , function ( ) {
65- this [ kBodyUsed ] = true
66- } )
67- }
68- } else if ( this . opts . body && typeof this . opts . body . pipeTo === 'function' ) {
69- // TODO (fix): We can't access ReadableStream internal state
70- // to determine whether or not it has been disturbed. This is just
71- // a workaround.
72- this . opts . body = new BodyAsyncIterable ( this . opts . body )
73- } else if (
74- this . opts . body &&
75- typeof this . opts . body !== 'string' &&
76- ! ArrayBuffer . isView ( this . opts . body ) &&
77- util . isIterable ( this . opts . body ) &&
78- ! util . isFormDataLike ( this . opts . body )
79- ) {
80- // TODO: Should we allow re-using iterable if !this.opts.idempotent
81- // or through some other flag?
82- this . opts . body = new BodyAsyncIterable ( this . opts . body )
83- }
8434 }
8535
8636 onRequestStart ( controller , context ) {
0 commit comments