UNPKG

@globalping/n8n-nodes-globalping

Version:

The Globalping n8n node allows you to perform network measurements such as ping, traceroute, mtr, http and DNS lookups from thousands of locations around the world.

76 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.globalpingApiRequest = globalpingApiRequest; exports.parseMeasurementTarget = parseMeasurementTarget; const url_1 = require("url"); const package_json_1 = require("../../package.json"); async function globalpingApiRequest(method, endpoint, body, query, uri, option = {}) { const useToken = this.getNodeParameter('useToken', 0); const options = { headers: { 'User-Agent': `n8n-community-node (v${package_json_1.version})`, }, method, body, qs: query, url: uri || `https://api.globalping.io${endpoint}`, json: true, }; if (Object.keys(option).length !== 0) { Object.assign(options, option); } if (Object.keys(body).length === 0) { delete options.body; } if (useToken) { return await this.helpers.requestWithAuthentication.call(this, 'globalpingApi', options); } else { return await this.helpers.request.call(this, options); } } function parseMeasurementTarget(target) { let withoutProtocol = false; if (!target.includes('://')) { target = `https://${target}`; withoutProtocol = true; } let parsedUrl; try { parsedUrl = new url_1.URL(target); } catch (error) { throw new Error(`Invalid target format: ${target}`); } let type; const host = parsedUrl.hostname; if (host.includes(':') && host.includes('[')) { type = 'ipv6'; } else if (/^(\d{1,3}\.){3}\d{1,3}$/.test(host)) { type = 'ipv4'; } else { type = 'url'; } let protocol = parsedUrl.protocol.replace(':', '').toUpperCase(); if (['ipv6', 'ipv4'].includes(type) && withoutProtocol) { protocol = 'HTTP'; } let port = parsedUrl.port || ''; if (protocol === 'HTTP') { port = parsedUrl.port || '80'; } else if (protocol === 'HTTPS') { port = parsedUrl.port || '443'; } return { type, protocol, host: parsedUrl.hostname, port: parseInt(port, 10), path: parsedUrl.pathname, query: parsedUrl.search.replace('?', ''), }; } //# sourceMappingURL=GenericFunctions.js.map