function assertConfigReq (t, req) { const url = new URL(req.url, 'relative:///') t.equal(req.method, 'GET', 'should make a GET request') t.equal(url.pathname, '/config/v1/agents', 'should send request to /config/v1/agents') t.equal(url.search, '?service.name=my-service-name&service.environment=development', 'should encode query in query params') t.equal(req.headers.authorization, 'Bearer secret', 'should add secret token') t.equal(req.headers['user-agent'], `my-user-agent ${pkg.name}/${pkg.version} ${process.release.name}/${process.versions.node}`, 'should add proper User-Agent') }
function getSanitizedPath(path) { // using the whatwg URL api, get a sanitized pathname from given path // this is because url.parse's `path` may not always have a slash // in front of it return new _url.URL(path, WEBAPP_ENDPOINT).pathname; }
var getHandleFromURL = function getHandleFromURL(url) { var customRegex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; try { var path = new URL(url).pathname; var regex = customRegex ? new RegExp(customRegex, 'i') : new RegExp('/([\\w|@|-]+)/?$', 'i'); var match = regex.exec(path); return customRegex ? match.find(function (match, index) { return index > 1 && match != undefined; }) : match[1]; } catch (error) { //Unable to parse handle, return empty value return ''; } }
const processItem = async (item, outputPath) => { const [ itemUrl, newName ] = item; const url = new URL(itemUrl); const itemName = newName ? `${newName}${path.extname(url.pathname)}` : path.basename(url.pathname); const response = await fetch(itemUrl); if (response.ok) { const dest = createWriteStream(path.join(outputPath, itemName)); response.body.pipe(dest); } else { throw { status: response.status, statusText: response.statusText, itemInfo: url.href } } }
const urlParser = ( req, res, next ) => { if ( !req.isRoutable ) return next( false ); const urlObject = new URL( req.headers.url ); /** * url query variables convert to object * ?a=b&c=d --> {a: b, c: d} */ urlObject.searchParams.forEach( ( value, name ) => { req.query[ name ] = value; } ); req.pathname = urlObject.pathname; req.hostname = urlObject.hostname; req.href = urlObject.href; return next( true ); }
/** * @param {URL} url * @param request * @param response * @param body * @private */ _handleRoute (url, request, response, body) { for (let route of this.routes) { if (route.path === url.pathname) { route.callback(url, request, response, body); return true; } } return false; }
app.post('/api/surveys/webhooks', (req, res) => { const p = new Path('/api/surveys/:surveyId/:choice'); _.chain(req.body) .map(({url, email}) => { const match = p.test(new URL(url).pathname); if (match) { return {email, ...match}; } }) .compact() .uniqBy('email', 'surveyId') .each(({surveyId, email, choice}) => { Survey.updateOne({ _id: surveyId, recipients: { $elemMatch: {email, responded: false}, } }, { $inc: {[choice]: 1}, $set: {'recipients.$.responded': true}, lastResponded: new Date(), }).exec(); }) .value(); res.send({}); });
cache.wrap('Verify', new URL(args.url).pathname + getHash({ jar: jar }), function () { return getVerification(jar, args.url, args.getBody) })
_isMatch(consumer, urlStr) { const url = new URL(urlStr); const providerInfo = url.searchParams || {}; const interfaceName = providerInfo.get('interface') || url.pathname.slice(1); if (interfaceName && consumer.interfaceName !== interfaceName) { return false; } const category = providerInfo.get('category'); if (category && category !== 'providers') { return false; } const enabled = providerInfo.get('enabled'); if (enabled && enabled !== 'true') { return false; } const consumerGroup = consumer.group; const consumerVersion = consumer.version; const providerGroup = providerInfo.get('group') || providerInfo.get('default.group'); const providerVersion = providerInfo.get('version') || providerInfo.get('default.version'); if (consumerGroup && providerGroup && consumerGroup !== providerGroup) { return false; } if (consumerVersion && providerVersion && consumerVersion !== providerVersion) { return false; } return true; }
var path = urlObj.pathname var url = host + path span.name = method + ' ' + url
options.quiet || process.stderr.write(`${currentUrl} \n`) outputProgress() if (urlParse.pathname.endsWith('.pdf') || urlParse.pathname.endsWith('.zip') || urlParse.pathname.endsWith('.mp3')) { urlsToDownload.add(urlParse.href) return void resolve() if (!href) return let hrefParse = new URL(href.split('#')[0], urlParse) if (hrefParse.pathname.startsWith(urlParse.pathname) && hrefParse.pathname.length > urlParse.pathname.length) { pageQueue.push(hrefParse.href)
hostname: _url.hostname, port: _url.port, path: `${_url.pathname}${_url.search}`, method: 'GET', headers: {
hostname: _url.hostname, port: _url.port, path: `${_url.pathname}${_url.search}`, method: 'POST', headers: {
const getHandleFromURL = (url, customRegex = null) => { try { const path = new URL(url).pathname const regex = customRegex ? new RegExp(customRegex, 'i') : new RegExp(`/([\\w|@|-]+)/?$`, 'i') const match = regex.exec(path) return customRegex ? match.find((match, index) => index > 1 && match != undefined) : match[1] } catch (error) { //Unable to parse handle, return empty value return '' } }
function getSanitizedPath(path) { // using the whatwg URL api, get a sanitized pathname from given path // this is because url.parse's `path` may not always have a slash // in front of it return new _url.URL(path, WEBAPP_ENDPOINT).pathname; }