@@ -117,6 +117,10 @@ function _getBodyNonDestructively(body: ReadableStream) {
117117 } ;
118118}
119119
120+ function isDocument ( value : unknown ) : value is Document {
121+ return typeof Document !== 'undefined' && value instanceof Document ;
122+ }
123+
120124/**
121125 * Helper function to determine payload content length for XHR requests
122126 * @param body
@@ -125,17 +129,17 @@ function _getBodyNonDestructively(body: ReadableStream) {
125129export function getXHRBodyLength (
126130 body : Document | XMLHttpRequestBodyInit
127131) : number | undefined {
128- if ( typeof Document !== 'undefined' && body instanceof Document ) {
132+ if ( isDocument ( body ) ) {
129133 return new XMLSerializer ( ) . serializeToString ( document ) . length ;
130134 }
135+
131136 // XMLHttpRequestBodyInit expands to the following:
132- if ( body instanceof Blob ) {
133- return body . size ;
137+ if ( typeof body === 'string' ) {
138+ return getByteLength ( body ) ;
134139 }
135140
136- // ArrayBuffer | ArrayBufferView
137- if ( ( body as any ) . byteLength !== undefined ) {
138- return ( body as any ) . byteLength as number ;
141+ if ( body instanceof Blob ) {
142+ return body . size ;
139143 }
140144
141145 if ( body instanceof FormData ) {
@@ -146,8 +150,9 @@ export function getXHRBodyLength(
146150 return getByteLength ( body . toString ( ) ) ;
147151 }
148152
149- if ( typeof body === 'string' ) {
150- return getByteLength ( body ) ;
153+ // ArrayBuffer | ArrayBufferView
154+ if ( body . byteLength !== undefined ) {
155+ return body . byteLength ;
151156 }
152157
153158 DIAG_LOGGER . warn ( 'unknown body type' ) ;
0 commit comments