|
| 1 | +'use strict' |
| 2 | +/* eslint-env browser */ |
| 3 | + |
| 4 | +const { TimeoutError, AbortError } = require('./error') |
| 5 | +const { Request, Response, Headers, fetch } = require('./fetch.polyfill') |
| 6 | + |
| 7 | +/** |
| 8 | + * @typedef {RequestInit & ExtraFetchOptions} FetchOptions |
| 9 | + * @typedef {Object} ExtraFetchOptions |
| 10 | + * @property {number} [timeout] |
| 11 | + * @property {URLSearchParams} [searchParams] |
| 12 | + * @property {function({total:number, loaded:number, lengthComputable:boolean}):void} [onUploadProgress] |
| 13 | + * @property {string} [overrideMimeType] |
| 14 | + * @returns {Promise<Response>} |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {string|URL} url |
| 19 | + * @param {FetchOptions} [options] |
| 20 | + * @returns {Promise<Response>} |
| 21 | + */ |
| 22 | +const fetchWithProgress = (url, options = {}) => { |
| 23 | + const request = new XMLHttpRequest() |
| 24 | + request.open(options.method || 'GET', url.toString(), true) |
| 25 | + |
| 26 | + const { timeout } = options |
| 27 | + if (timeout > 0 && timeout < Infinity) { |
| 28 | + request.timeout = options.timeout |
| 29 | + } |
| 30 | + |
| 31 | + if (options.overrideMimeType != null) { |
| 32 | + request.overrideMimeType(options.overrideMimeType) |
| 33 | + } |
| 34 | + |
| 35 | + if (options.headers) { |
| 36 | + for (const [name, value] of options.headers.entries()) { |
| 37 | + request.setRequestHeader(name, value) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (options.signal) { |
| 42 | + options.signal.onabort = () => request.abort() |
| 43 | + } |
| 44 | + |
| 45 | + if (options.onUploadProgress) { |
| 46 | + request.upload.onprogress = options.onUploadProgress |
| 47 | + } |
| 48 | + |
| 49 | + // Note: Need to use `arraybuffer` here instead of `blob` because `Blob` |
| 50 | + // instances coming from JSDOM are not compatible with `Response` from |
| 51 | + // node-fetch (which is the setup we get when testing with jest because |
| 52 | + // it uses JSDOM which does not provide a global fetch |
| 53 | + // https://github.com/jsdom/jsdom/issues/1724) |
| 54 | + request.responseType = 'arraybuffer' |
| 55 | + |
| 56 | + return new Promise((resolve, reject) => { |
| 57 | + /** |
| 58 | + * @param {Event} event |
| 59 | + */ |
| 60 | + const handleEvent = (event) => { |
| 61 | + switch (event.type) { |
| 62 | + case 'error': { |
| 63 | + resolve(Response.error()) |
| 64 | + break |
| 65 | + } |
| 66 | + case 'load': { |
| 67 | + resolve( |
| 68 | + new ResponseWithURL(request.responseURL, request.response, { |
| 69 | + status: request.status, |
| 70 | + statusText: request.statusText, |
| 71 | + headers: parseHeaders(request.getAllResponseHeaders()) |
| 72 | + }) |
| 73 | + ) |
| 74 | + break |
| 75 | + } |
| 76 | + case 'timeout': { |
| 77 | + reject(new TimeoutError()) |
| 78 | + break |
| 79 | + } |
| 80 | + case 'abort': { |
| 81 | + reject(new AbortError()) |
| 82 | + break |
| 83 | + } |
| 84 | + default: { |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + request.onerror = handleEvent |
| 90 | + request.onload = handleEvent |
| 91 | + request.ontimeout = handleEvent |
| 92 | + request.onabort = handleEvent |
| 93 | + |
| 94 | + request.send(options.body) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +const fetchWithStreaming = fetch |
| 99 | + |
| 100 | +const fetchWith = (url, options = {}) => |
| 101 | + (options.onUploadProgress != null) |
| 102 | + ? fetchWithProgress(url, options) |
| 103 | + : fetchWithStreaming(url, options) |
| 104 | + |
| 105 | +exports.fetch = fetchWith |
| 106 | +exports.Request = Request |
| 107 | +exports.Headers = Headers |
| 108 | + |
| 109 | +/** |
| 110 | + * @param {string} input |
| 111 | + * @returns {Headers} |
| 112 | + */ |
| 113 | +const parseHeaders = (input) => { |
| 114 | + const headers = new Headers() |
| 115 | + for (const line of input.trim().split(/[\r\n]+/)) { |
| 116 | + const index = line.indexOf(': ') |
| 117 | + if (index > 0) { |
| 118 | + headers.set(line.slice(0, index), line.slice(index + 1)) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return headers |
| 123 | +} |
| 124 | + |
| 125 | +class ResponseWithURL extends Response { |
| 126 | + /** |
| 127 | + * @param {string} url |
| 128 | + * @param {string|Blob|ArrayBufferView|ArrayBuffer|FormData|ReadableStream<Uint8Array>} body |
| 129 | + * @param {ResponseInit} options |
| 130 | + */ |
| 131 | + constructor (url, body, options) { |
| 132 | + super(body, options) |
| 133 | + Object.defineProperty(this, 'url', { value: url }) |
| 134 | + } |
| 135 | +} |
0 commit comments