|
20 | 20 | arrayBuffer: 'ArrayBuffer' in self |
21 | 21 | } |
22 | 22 |
|
| 23 | + if (support.arrayBuffer) { |
| 24 | + var viewClasses = [ |
| 25 | + '[object Int8Array]', |
| 26 | + '[object Uint8Array]', |
| 27 | + '[object Uint8ClampedArray]', |
| 28 | + '[object Int16Array]', |
| 29 | + '[object Uint16Array]', |
| 30 | + '[object Int32Array]', |
| 31 | + '[object Uint32Array]', |
| 32 | + '[object Float32Array]', |
| 33 | + '[object Float64Array]' |
| 34 | + ] |
| 35 | + |
| 36 | + var isDataView = function(obj) { |
| 37 | + return obj && DataView.prototype.isPrototypeOf(obj) |
| 38 | + } |
| 39 | + |
| 40 | + var isArrayBufferView = ArrayBuffer.isView || function(obj) { |
| 41 | + return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1 |
| 42 | + } |
| 43 | + } |
| 44 | + |
23 | 45 | function normalizeName(name) { |
24 | 46 | if (typeof name !== 'string') { |
25 | 47 | name = String(name) |
|
152 | 174 |
|
153 | 175 | function readBlobAsArrayBuffer(blob) { |
154 | 176 | var reader = new FileReader() |
| 177 | + var promise = fileReaderReady(reader) |
155 | 178 | reader.readAsArrayBuffer(blob) |
156 | | - return fileReaderReady(reader) |
| 179 | + return promise |
157 | 180 | } |
158 | 181 |
|
159 | 182 | function readBlobAsText(blob) { |
160 | 183 | var reader = new FileReader() |
| 184 | + var promise = fileReaderReady(reader) |
161 | 185 | reader.readAsText(blob) |
162 | | - return fileReaderReady(reader) |
| 186 | + return promise |
| 187 | + } |
| 188 | + |
| 189 | + function readArrayBufferAsText(buf) { |
| 190 | + var view = new Uint8Array(buf) |
| 191 | + var chars = new Array(view.length) |
| 192 | + |
| 193 | + for (var i = 0; i < view.length; i++) { |
| 194 | + chars[i] = String.fromCharCode(view[i]) |
| 195 | + } |
| 196 | + return chars.join('') |
| 197 | + } |
| 198 | + |
| 199 | + function bufferClone(buf) { |
| 200 | + if (buf.slice) { |
| 201 | + return buf.slice(0) |
| 202 | + } else { |
| 203 | + var view = new Uint8Array(buf.byteLength) |
| 204 | + view.set(new Uint8Array(buf)) |
| 205 | + return view.buffer |
| 206 | + } |
163 | 207 | } |
164 | 208 |
|
165 | 209 | function Body() { |
166 | 210 | this.bodyUsed = false |
167 | 211 |
|
168 | 212 | this._initBody = function(body) { |
169 | 213 | this._bodyInit = body |
170 | | - if (typeof body === 'string') { |
| 214 | + if (!body) { |
| 215 | + this._bodyText = '' |
| 216 | + } else if (typeof body === 'string') { |
171 | 217 | this._bodyText = body |
172 | 218 | } else if (support.blob && Blob.prototype.isPrototypeOf(body)) { |
173 | 219 | this._bodyBlob = body |
174 | 220 | } else if (support.formData && FormData.prototype.isPrototypeOf(body)) { |
175 | 221 | this._bodyFormData = body |
176 | 222 | } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { |
177 | 223 | this._bodyText = body.toString() |
178 | | - } else if (!body) { |
179 | | - this._bodyText = '' |
180 | | - } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) { |
181 | | - // Only support ArrayBuffers for POST method. |
182 | | - // Receiving ArrayBuffers happens via Blobs, instead. |
| 224 | + } else if (support.arrayBuffer && support.blob && isDataView(body)) { |
| 225 | + this._bodyArrayBuffer = bufferClone(body.buffer) |
| 226 | + // IE 10-11 can't handle a DataView body. |
| 227 | + this._bodyInit = new Blob([this._bodyArrayBuffer]) |
| 228 | + } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) { |
| 229 | + this._bodyArrayBuffer = bufferClone(body) |
183 | 230 | } else { |
184 | 231 | throw new Error('unsupported BodyInit type') |
185 | 232 | } |
|
204 | 251 |
|
205 | 252 | if (this._bodyBlob) { |
206 | 253 | return Promise.resolve(this._bodyBlob) |
| 254 | + } else if (this._bodyArrayBuffer) { |
| 255 | + return Promise.resolve(new Blob([this._bodyArrayBuffer])) |
207 | 256 | } else if (this._bodyFormData) { |
208 | 257 | throw new Error('could not read FormData body as blob') |
209 | 258 | } else { |
|
212 | 261 | } |
213 | 262 |
|
214 | 263 | this.arrayBuffer = function() { |
215 | | - return this.blob().then(readBlobAsArrayBuffer) |
216 | | - } |
217 | | - |
218 | | - this.text = function() { |
219 | | - var rejected = consumed(this) |
220 | | - if (rejected) { |
221 | | - return rejected |
222 | | - } |
223 | | - |
224 | | - if (this._bodyBlob) { |
225 | | - return readBlobAsText(this._bodyBlob) |
226 | | - } else if (this._bodyFormData) { |
227 | | - throw new Error('could not read FormData body as text') |
| 264 | + if (this._bodyArrayBuffer) { |
| 265 | + return consumed(this) || Promise.resolve(this._bodyArrayBuffer) |
228 | 266 | } else { |
229 | | - return Promise.resolve(this._bodyText) |
| 267 | + return this.blob().then(readBlobAsArrayBuffer) |
230 | 268 | } |
231 | 269 | } |
232 | | - } else { |
233 | | - this.text = function() { |
234 | | - var rejected = consumed(this) |
235 | | - return rejected ? rejected : Promise.resolve(this._bodyText) |
| 270 | + } |
| 271 | + |
| 272 | + this.text = function() { |
| 273 | + var rejected = consumed(this) |
| 274 | + if (rejected) { |
| 275 | + return rejected |
| 276 | + } |
| 277 | + |
| 278 | + if (this._bodyBlob) { |
| 279 | + return readBlobAsText(this._bodyBlob) |
| 280 | + } else if (this._bodyArrayBuffer) { |
| 281 | + return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer)) |
| 282 | + } else if (this._bodyFormData) { |
| 283 | + throw new Error('could not read FormData body as text') |
| 284 | + } else { |
| 285 | + return Promise.resolve(this._bodyText) |
236 | 286 | } |
237 | 287 | } |
238 | 288 |
|
|
260 | 310 | function Request(input, options) { |
261 | 311 | options = options || {} |
262 | 312 | var body = options.body |
263 | | - if (Request.prototype.isPrototypeOf(input)) { |
| 313 | + |
| 314 | + if (typeof input === 'string') { |
| 315 | + this.url = input |
| 316 | + } else { |
264 | 317 | if (input.bodyUsed) { |
265 | 318 | throw new TypeError('Already read') |
266 | 319 | } |
|
271 | 324 | } |
272 | 325 | this.method = input.method |
273 | 326 | this.mode = input.mode |
274 | | - if (!body) { |
| 327 | + if (!body && input._bodyInit != null) { |
275 | 328 | body = input._bodyInit |
276 | 329 | input.bodyUsed = true |
277 | 330 | } |
278 | | - } else { |
279 | | - this.url = input |
280 | 331 | } |
281 | 332 |
|
282 | 333 | this.credentials = options.credentials || this.credentials || 'omit' |
|
294 | 345 | } |
295 | 346 |
|
296 | 347 | Request.prototype.clone = function() { |
297 | | - return new Request(this) |
| 348 | + return new Request(this, { body: this._bodyInit }) |
298 | 349 | } |
299 | 350 |
|
300 | 351 | function decode(body) { |
|
310 | 361 | return form |
311 | 362 | } |
312 | 363 |
|
313 | | - function headers(xhr) { |
314 | | - var head = new Headers() |
315 | | - var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n') |
316 | | - pairs.forEach(function(header) { |
317 | | - var split = header.trim().split(':') |
318 | | - var key = split.shift().trim() |
319 | | - var value = split.join(':').trim() |
320 | | - head.append(key, value) |
| 364 | + function parseHeaders(rawHeaders) { |
| 365 | + var headers = new Headers() |
| 366 | + rawHeaders.split('\r\n').forEach(function(line) { |
| 367 | + var parts = line.split(':') |
| 368 | + var key = parts.shift().trim() |
| 369 | + if (key) { |
| 370 | + var value = parts.join(':').trim() |
| 371 | + headers.append(key, value) |
| 372 | + } |
321 | 373 | }) |
322 | | - return head |
| 374 | + return headers |
323 | 375 | } |
324 | 376 |
|
325 | 377 | Body.call(Request.prototype) |
|
330 | 382 | } |
331 | 383 |
|
332 | 384 | this.type = 'default' |
333 | | - this.status = options.status |
| 385 | + this.status = 'status' in options ? options.status : 200 |
334 | 386 | this.ok = this.status >= 200 && this.status < 300 |
335 | | - this.statusText = options.statusText |
336 | | - this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers) |
| 387 | + this.statusText = 'statusText' in options ? options.statusText : 'OK' |
| 388 | + this.headers = new Headers(options.headers) |
337 | 389 | this.url = options.url || '' |
338 | 390 | this._initBody(bodyInit) |
339 | 391 | } |
|
371 | 423 |
|
372 | 424 | self.fetch = function(input, init) { |
373 | 425 | return new Promise(function(resolve, reject) { |
374 | | - var request |
375 | | - if (Request.prototype.isPrototypeOf(input) && !init) { |
376 | | - request = input |
377 | | - } else { |
378 | | - request = new Request(input, init) |
379 | | - } |
380 | | - |
| 426 | + var request = new Request(input, init) |
381 | 427 | var xhr = new XMLHttpRequest() |
382 | 428 |
|
383 | | - function responseURL() { |
384 | | - if ('responseURL' in xhr) { |
385 | | - return xhr.responseURL |
386 | | - } |
387 | | - |
388 | | - // Avoid security warnings on getResponseHeader when not allowed by CORS |
389 | | - if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) { |
390 | | - return xhr.getResponseHeader('X-Request-URL') |
391 | | - } |
392 | | - |
393 | | - return |
394 | | - } |
395 | | - |
396 | 429 | xhr.onload = function() { |
397 | 430 | var options = { |
398 | 431 | status: xhr.status, |
399 | 432 | statusText: xhr.statusText, |
400 | | - headers: headers(xhr), |
401 | | - url: responseURL() |
| 433 | + headers: parseHeaders(xhr.getAllResponseHeaders() || '') |
402 | 434 | } |
| 435 | + options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL') |
403 | 436 | var body = 'response' in xhr ? xhr.response : xhr.responseText |
404 | 437 | resolve(new Response(body, options)) |
405 | 438 | } |
|
0 commit comments