How to use the url.URL function in url

To help you get started, we’ve selected a few url examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sindresorhus / got / source / utils / merge.ts View on Github external
export default function merge(target: Target, ...sources: Source[]): Merge {
	for (const source of sources) {
		for (const [key, sourceValue] of Object.entries(source)) {
			const targetValue = target[key];

			if (is.urlInstance(targetValue) && is.string(sourceValue)) {
				// @ts-ignore TS doesn't recognise Target accepts string keys
				target[key] = new URL(sourceValue, targetValue);
			} else if (is.plainObject(sourceValue)) {
				if (is.plainObject(targetValue)) {
					// @ts-ignore TS doesn't recognise Target accepts string keys
					target[key] = merge({}, targetValue, sourceValue);
				} else {
					// @ts-ignore TS doesn't recognise Target accepts string keys
					target[key] = merge({}, sourceValue);
				}
			} else if (is.array(sourceValue)) {
				// @ts-ignore TS doesn't recognise Target accepts string keys
				target[key] = sourceValue.slice();
			} else {
				// @ts-ignore TS doesn't recognise Target accepts string keys
				target[key] = sourceValue;
			}
		}
github commonshost / playdoh / test / helpers / fetch.js View on Github external
async function fetch (url, options) {
  const { origin, pathname, search } = new URL(url)
  const session = http2.connect(origin)
  const reqHeaders = {
    ...options.headers,
    ...{ ':path': pathname + search }
  }
  const request = session.request(reqHeaders)
  request.end(options.body)
  const headers = await eventToPromise(request, 'response')
  const chunks = []
  await readAll(request, chunks)
  await promisify(session.close).call(session)
  const body = Buffer.concat(chunks)
  chunks.length = 0
  return {
    ok: headers[':status'] < 400,
    get headers () { return new Map(Object.entries(headers)) },
github emberjs / data / bin / -tarball-info.js View on Github external
function generatePackageReference(version, tarballName) {
  if (options.referenceViaVersion === true) {
    return version;
  }
  if (options.hostPath.indexOf('file:') === 0) {
    return path.join(options.hostPath, tarballName);
  }
  return new url.URL(tarballName, options.hostPath);
}
github mj1618 / serverless-offline-sns / src / sns-server.ts View on Github external
private publishSqs(event, sub) {
        const subEndpointUrl = new URL(sub.Endpoint);
        const sqsEndpoint = `${subEndpointUrl.protocol}//${subEndpointUrl.host}/`;
        const sqs = new SQS({ endpoint: sqsEndpoint, region: this.region });

        if (sub["Attributes"]["RawMessageDelivery"] === "true") {
            return sqs.sendMessage({
                QueueUrl: sub.Endpoint,
                MessageBody: event,
            }).promise();
        } else {
            const records = JSON.parse(event).Records;
            const messagePromises = records.map(record => {
                return sqs
                    .sendMessage({
                        QueueUrl: sub.Endpoint,
                        MessageBody: JSON.stringify(record.Sns),
                    })
github brave-experiments / brave-abp-measurer / lib / db.js View on Github external
async function recordUrlIfHttp (client, url, baseUrl = undefined) {
    let parsedUrl;
    try {
        parsedUrl = baseUrl ? new urlLib.URL(url, baseUrl) : new urlLib.URL(url);
    } catch (_) {
        return false;
    }

    if (parsedUrl.protocol !== "http") {
        return false;
    }

    const insertQuery = `
        INSERT INTO
            http_domains(domain, last_seen)
        VALUES
            ($1, now())
        ON CONFLICT
            (domain)
        DO UPDATE SET
github cardstack / cardstack / packages / jsonapi / middleware.js View on Github external
_urlWithUpdatedParams(params) {
    let p = merge({}, this.query, params);
    let origin = this.ctxt.request.origin;
    if (this.prefix) {
      origin = origin + '/' + this.prefix;
    }
    let u = new URL(origin + (this.ctxt.req.originalUrl || this.ctxt.req.url));
    u.search = '?' + qs.stringify(p, { encode: false });
    return u.href;
  }
}
github graphql / swapi-graphql / scripts / download.js View on Github external
function normalizeUrl(url) {
  return new URL(url).toString();
}
github elastic / elasticsearch-js / index.js View on Github external
function getUsernameAndPassword (node) {
    if (typeof node === 'string') {
      const { username, password } = new URL(node)
      return {
        username: decodeURIComponent(username),
        password: decodeURIComponent(password)
      }
    } else if (node.url instanceof URL) {
      return {
        username: decodeURIComponent(node.url.username),
        password: decodeURIComponent(node.url.password)
      }
    }
  }
}
github x-orpheus / elint / src / notifier / get-preset-info.js View on Github external
function getRegistryUrl (resolved) {
  if (!resolved) {
    return ''
  }

  let resolvedUrl = ''

  try {
    const urlObj = new url.URL(resolved)
    resolvedUrl = `${urlObj.protocol}//${urlObj.host}`
  } catch (error) {
    debug('url parse error: %o', error)
  }

  return resolvedUrl
}
github qwazwsx / c4-cams / index.js View on Github external
return new Promise(function(resolve,reject){

	//setup options
	var options = new URL(url);
	options.method = 'HEAD'

	//make the request
	var beforeTime = time;
	var req = http.request(options, function(r) {
			//return the response code on no errors
			if (r.statusCode == 200){
				var totalTime = time - beforeTime
				
				resolve({success: true, code: r.statusCode, time: totalTime });
			}else{
				resolve({ error: true, message: 'request succeeded but response code was non-200, response code:'+r.statusCode });
			}
		});
	req.on('error', function(err) {
		//on error return error flag