helper.sub.js (2592B)
1 'use strict'; 2 3 /** 4 * Construct a URL which, when followed, will trigger redirection through zero 5 * or more specified origins and ultimately resolve in the Python handler 6 * `record-headers.py`. 7 * 8 * @param {string} key - the WPT server "stash" name where the request's 9 * headers should be stored 10 * @param {string[]} [origins] - zero or more origin names through which the 11 * request should pass; see the function 12 * implementation for a completel list of names 13 * and corresponding origins; If specified, the 14 * final origin will be used to access the 15 * `record-headers.py` hander. 16 * @param {object} [params] - a collection of key-value pairs to include as 17 * URL "search" parameters in the final request to 18 * `record-headers.py` 19 * 20 * @returns {string} an absolute URL 21 */ 22 function makeRequestURL(key, origins, params) { 23 const byName = { 24 httpOrigin: 'http://{{host}}:{{ports[http][0]}}', 25 httpSameSite: 'http://{{hosts[][www]}}:{{ports[http][0]}}', 26 httpCrossSite: 'http://{{hosts[alt][]}}:{{ports[http][0]}}', 27 httpsOrigin: 'https://{{host}}:{{ports[https][0]}}', 28 httpsSameSite: 'https://{{hosts[][www]}}:{{ports[https][0]}}', 29 httpsCrossSite: 'https://{{hosts[alt][]}}:{{ports[https][0]}}' 30 }; 31 const redirectPath = '/fetch/api/resources/redirect.py?location='; 32 const path = '/fetch/metadata/resources/record-headers.py?key=' + key; 33 34 let requestUrl = path; 35 if (params) { 36 requestUrl += '&' + new URLSearchParams(params).toString(); 37 } 38 39 if (origins && origins.length) { 40 requestUrl = byName[origins.pop()] + requestUrl; 41 42 while (origins.length) { 43 requestUrl = byName[origins.pop()] + redirectPath + 44 encodeURIComponent(requestUrl); 45 } 46 } else { 47 requestUrl = byName.httpsOrigin + requestUrl; 48 } 49 50 return requestUrl; 51 } 52 53 function retrieve(key, options) { 54 return fetch('/fetch/metadata/resources/record-headers.py?retrieve&key=' + key) 55 .then((response) => { 56 if (response.status === 204 && options && options.poll) { 57 return new Promise((resolve) => setTimeout(resolve, 300)) 58 .then(() => retrieve(key, options)); 59 } 60 61 if (response.status !== 200) { 62 throw new Error('Failed to query for recorded headers.'); 63 } 64 65 return response.text().then((text) => JSON.parse(text)); 66 }); 67 }