Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
// No need to carry callback around. This is what happens in original `end`.
if (typeof callback === 'function') {
req.once('finish', callback);
}
const headers =
typeof req.getHeaders === 'function'
? req.getHeaders()
: req.headers || req._headers || {};
const host = headers.host;
const [hostname, port = '80'] = host.split(':');
const { method, path } = req;
const parsedUrl = new URL('');
parsedUrl.set('protocol', req.agent.protocol);
parsedUrl.set('pathname', path);
parsedUrl.set('hostname', hostname);
parsedUrl.set('port', port !== '80' ? port : '');
adapter
.handleRequest({
method,
headers,
url: parsedUrl.href,
body: chunks,
requestArguments: [wrapper, req, args]
})
.catch(e => {
// This allows the consumer to handle the error gracefully
request: async request => {
if (requestResourceTypes.includes(request.resourceType())) {
const url = request.url();
const method = request.method();
const headers = request.headers();
// A CORS preflight request is a CORS request that checks to see
// if the CORS protocol is understood.
const isPreFlightReq =
method === 'OPTIONS' &&
!!headers['origin'] &&
!!headers['access-control-request-method'];
// Do not intercept requests with the Polly passthrough QP
if (url.includes(PASSTHROUGH_REQ_ID_QP)) {
const parsedUrl = new URL(url, true);
// If this is a polly passthrough request
// Get the associated promise object for the request id and set it
// on the request.
if (!isPreFlightReq) {
this._requestsMapping.passthroughs.set(
request,
this[PASSTHROUGH_PROMISES].get(
parsedUrl.query[PASSTHROUGH_REQ_ID_QP]
)
);
}
// Delete the query param to remove any pollyjs footprint
delete parsedUrl.query[PASSTHROUGH_REQ_ID_QP];
async passthroughRequest(pollyRequest) {
const { page } = this.options;
const { id, order, url, method, headers, body } = pollyRequest;
const requestId = `${this.polly.recordingId}:${id}:${order}`;
const parsedUrl = new URL(url, true);
parsedUrl.query[PASSTHROUGH_REQ_ID_QP] = requestId;
try {
const response = await new Promise((resolve, reject) => {
this[PASSTHROUGH_PROMISES].set(requestId, { resolve, reject });
// This gets evaluated within the browser's context, meaning that
// this fetch call executes from within the browser.
page.evaluate(
new Function(
'url',
'method',
'headers',
'body',
'return fetch(url, { method, headers, body });'
function parseUrl(url) {
const parsedUrl = new URL(url);
/*
Use the full origin (http://hostname:port) if the host exists. If there
is no host, URL.origin returns "null" (null as a string) so set host to '/'
*/
const host = parsedUrl.host ? parsedUrl.origin : CHARS.SLASH;
const path = parsedUrl.pathname || CHARS.SLASH;
return { host, path };
}
get absoluteUrl() {
const { url } = this;
return isAbsoluteUrl(url) ? url : new URL(url).href;
}
export default function parseUrl(url, ...args) {
const parsedUrl = new URL(url, ...args);
if (!isAbsoluteUrl(url)) {
if (url.startsWith('//')) {
/*
If the url is protocol-relative, strip out the protocol
*/
parsedUrl.set('protocol', '');
} else {
/*
If the url is relative, setup the parsed url to reflect just that
by removing the host. By default URL sets the host via window.location if
it does not exist.
*/
removeHostFromUrl(parsedUrl);
}
}