@@ -3,7 +3,7 @@ function isDataView(obj) {
33}
44
55function consumed ( body ) {
6- if ( body . _noBody ) {
6+ if ( body . _bodySize === 0 ) {
77 return ;
88 }
99
@@ -26,36 +26,6 @@ function fileReaderReady(reader) {
2626 } ) ;
2727}
2828
29- function readBlobAsArrayBuffer ( blob ) {
30- var reader = new FileReader ( ) ;
31- var promise = fileReaderReady ( reader ) ;
32-
33- reader . readAsArrayBuffer ( blob ) ;
34-
35- return promise ;
36- }
37-
38- function readBlobAsText ( blob ) {
39- var reader = new FileReader ( ) ;
40- var promise = fileReaderReady ( reader ) ;
41- var match = / c h a r s e t = ( [ A - Z a - z 0 - 9 _ - ] + ) / . exec ( blob . type ) ;
42- var encoding = match ? match [ 1 ] : 'utf-8' ;
43-
44- reader . readAsText ( blob , encoding ) ;
45-
46- return promise ;
47- }
48-
49- function readArrayBufferAsText ( buf ) {
50- var view = new Uint8Array ( buf ) ;
51- var chars = new Array ( view . length ) ;
52-
53- for ( var i = 0 ; i < view . length ; i ++ ) {
54- chars [ i ] = String . fromCharCode ( view [ i ] ) ;
55- }
56-
57- return chars . join ( '' ) ;
58- }
5929
6030function bufferClone ( buf ) {
6131 if ( buf . slice ) {
@@ -80,24 +50,43 @@ export const BodyMixin = {
8050 this . _bodyInit = body ;
8151
8252 if ( ! body ) {
83- this . _noBody = true ;
84- this . _bodyText = '' ;
53+ this . _bodySize = 0 ;
54+ this . _bodyReadable = ReadableStream . from ( [ new Uint8Array ( 0 ) ] ) ;
8555 } else if ( typeof body === 'string' ) {
86- this . _bodyText = body ;
56+ const bodyBuffer = new TextEncoder ( ) . encode ( body ) ;
57+ this . _bodySize = bodyBuffer . byteLength ;
58+ this . _bodyReadable = ReadableStream . from ( [ bodyBuffer ] ) ;
8759 } else if ( isPrototypeOf ( Blob . prototype , body ) ) {
88- this . _bodyBlob = body ;
60+ this . _bodySize = body . size ;
61+ this . _bodyReadable = body . stream ;
8962 } else if ( isPrototypeOf ( FormData . prototype , body ) ) {
90- this . _bodyFormData = body ;
63+ //formdata polyfill
64+ const bodyBuffer = body [ '_blob' ] ( ) ;
65+ this . _bodySize = bodyBuffer . byteLength ;
66+ this . _bodyReadable = ReadableStream . from ( [ bodyBuffer ] ) ;
9167 } else if ( isPrototypeOf ( URLSearchParams . prototype , body ) ) {
92- this . _bodyText = body . toString ( ) ;
68+ const bodyBuffer = new TextEncoder ( ) . encode ( body ) ;
69+ this . _bodySize = bodyBuffer . byteLength ;
70+ this . _bodyReadable = ReadableStream . from ( [ new TextEncoder ( ) . encode ( body . toString ( ) ) ] ) ;
9371 } else if ( isDataView ( body ) ) {
94- this . _bodyArrayBuffer = bufferClone ( body . buffer ) ;
72+ const bodyBuffer = new Uint8Array ( bufferClone ( body . buffer ) ) ;
73+ this . _bodySize = bodyBuffer . byteLength ;
74+ this . _bodyReadable = ReadableStream . from ( [ bodyBuffer ] ) ;
9575 } else if ( isPrototypeOf ( ArrayBuffer . prototype , body ) || ArrayBuffer . isView ( body ) ) {
96- this . _bodyArrayBuffer = bufferClone ( body ) ;
76+ const bodyBuffer = new Uint8Array ( bufferClone ( body . buffer ) ) ;
77+ this . _bodySize = bodyBuffer . byteLength ;
78+ this . _bodyReadable = ReadableStream . from ( [ bodyBuffer ] ) ;
79+ } else if ( isPrototypeOf ( ReadableStream . prototype , body ) ) {
80+ this . _bodySize = - 1 ;
81+ this . _bodyReadable = body ;
9782 } else {
98- this . _bodyText = body = Object . prototype . toString . call ( body ) ;
83+ const bodyBuffer = new TextEncoder ( ) . encode ( body . toString ( ) ) ;
84+ this . _bodySize = bodyBuffer . byteLength ;
85+ this . _bodyReadable = ReadableStream . from ( [ bodyBuffer ] ) ;
9986 }
10087
88+ this . body = this . _bodyReadable
89+
10190 if ( ! this . headers . get ( 'content-type' ) ) {
10291 if ( typeof body === 'string' ) {
10392 this . headers . set ( 'content-type' , 'text/plain;charset=UTF-8' ) ;
@@ -109,70 +98,46 @@ export const BodyMixin = {
10998 }
11099 } ,
111100
112- blob ( ) {
101+ async blob ( ) {
113102 const rejected = consumed ( this ) ;
114103
115104 if ( rejected ) {
116- return rejected ;
105+ await rejected ;
117106 }
118107
119- if ( this . _bodyBlob ) {
120- return Promise . resolve ( this . _bodyBlob ) ;
121- } else if ( this . _bodyArrayBuffer ) {
122- return Promise . resolve ( new Blob ( [ this . _bodyArrayBuffer ] ) ) ;
123- } else if ( this . _bodyFormData ) {
124- throw new Error ( 'could not read FormData body as blob' ) ;
125- } else {
126- return Promise . resolve ( new Blob ( [ this . _bodyText ] ) ) ;
127- }
128- } ,
129-
130- arrayBuffer ( ) {
131- if ( this . _bodyArrayBuffer ) {
132- var isConsumed = consumed ( this ) ;
133-
134- if ( isConsumed ) {
135- return isConsumed ;
136- } else if ( ArrayBuffer . isView ( this . _bodyArrayBuffer ) ) {
137- return Promise . resolve (
138- this . _bodyArrayBuffer . buffer . slice (
139- this . _bodyArrayBuffer . byteOffset ,
140- this . _bodyArrayBuffer . byteOffset + this . _bodyArrayBuffer . byteLength
141- )
142- ) ;
143- } else {
144- return Promise . resolve ( this . _bodyArrayBuffer ) ;
108+ if ( this . _bodyReadable ) {
109+ const parts = [ ] ;
110+ const reader = this . _bodyReadable . getReader ( ) ;
111+ while ( true ) {
112+ const next = await reader . read ( ) ;
113+ if ( next . done ) {
114+ break ;
115+ }
116+ parts . push ( next . value ) ;
145117 }
118+ return new Blob ( parts ) ;
146119 } else {
147- return this . blob ( ) . then ( readBlobAsArrayBuffer ) ;
120+ throw new Error ( 'Unknown body type' ) ;
148121 }
149122 } ,
150123
151- text ( ) {
152- const rejected = consumed ( this ) ;
153-
154- if ( rejected ) {
155- return rejected ;
156- }
124+ async arrayBuffer ( ) {
125+ //TODO: expose Blob.parts to reduce memeory copy?
126+ return await ( await this . blob ( ) ) . arrayBuffer ( ) ;
127+ } ,
157128
158- if ( this . _bodyBlob ) {
159- return readBlobAsText ( this . _bodyBlob ) ;
160- } else if ( this . _bodyArrayBuffer ) {
161- return Promise . resolve ( readArrayBufferAsText ( this . _bodyArrayBuffer ) ) ;
162- } else if ( this . _bodyFormData ) {
163- throw new Error ( 'could not read FormData body as text' ) ;
164- } else {
165- return Promise . resolve ( this . _bodyText ) ;
166- }
129+ async text ( ) {
130+ return new TextDecoder ( ) . decode ( await this . arrayBuffer ( ) ) ;
167131 } ,
168132
169- formData ( ) {
170- return this . text ( ) . then ( decode ) ;
133+ async formData ( ) {
134+ return decode ( await this . text ( ) ) ;
171135 } ,
172136
173- json ( ) {
174- return this . text ( ) . then ( JSON . parse ) ;
137+ async json ( ) {
138+ return JSON . parse ( await this . text ( ) ) ;
175139 } ,
140+
176141} ;
177142
178143function decode ( body ) {
0 commit comments