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.

253 lines 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlobalpingV1 = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ProbesDescription_1 = require("./ProbesDescription"); const LimitsDescription_1 = require("./LimitsDescription"); const GenericFunctions_1 = require("../GenericFunctions"); const MeasurementsDescription_1 = require("./MeasurementsDescription"); const versionDescription = { displayName: 'Globalping', name: 'globalping', icon: 'file:globalping.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume the Globalping API', defaults: { name: 'Globalping', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'globalpingApi', required: false, }, ], properties: [ { displayName: 'Authentication', name: 'useToken', type: 'boolean', description: 'Whether to use a token for requests. If disabled, requests will not include a token.', default: false, }, { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Measurement', value: 'measurements', }, { name: 'Probe', value: 'probes', }, { name: 'Limit', value: 'limits', }, ], default: 'measurements', }, ...ProbesDescription_1.probesOperations, ...ProbesDescription_1.probesFields, ...LimitsDescription_1.limitsOperations, ...LimitsDescription_1.limitsFields, ...MeasurementsDescription_1.measurementsOperations, ...MeasurementsDescription_1.measurementsFields, ], }; class GlobalpingV1 { constructor(baseDescription) { this.description = { ...baseDescription, ...versionDescription, }; } async execute() { const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); let method = 'GET'; let body = {}; let qs = {}; let endpoint = ''; let responseData; for (let i = 0; i < items.length; i++) { try { if (resource === 'probes') { if (operation === 'list') { method = 'GET'; endpoint = '/v1/probes'; body = {}; responseData = await GenericFunctions_1.globalpingApiRequest.call(this, method, endpoint, body, qs); } } if (resource === 'limits') { if (operation === 'list') { method = 'GET'; endpoint = '/v1/limits'; body = {}; responseData = await GenericFunctions_1.globalpingApiRequest.call(this, method, endpoint, body, qs); } } if (resource === 'measurements') { const executeData = this.getExecuteData(); const params = executeData.node.parameters; let requestBody = {}; const parsedTarget = (0, GenericFunctions_1.parseMeasurementTarget)(this.getNodeParameter('measurementTarget', i)); requestBody.target = parsedTarget.host; let measurementLocationsUi = this.getNodeParameter('measurementLocationsUi', i); if (measurementLocationsUi) { if (measurementLocationsUi.measurementLocations) { requestBody.locations = []; for (let location of measurementLocationsUi.measurementLocations) { let locationObject = {}; if (location.magic && location.magic !== '') { locationObject.magic = location.magic; } if (Object.keys(locationObject).length > 0) { requestBody.locations.push(locationObject); } } } } let measurementLimit = this.getNodeParameter('measurementLimit', i); if (measurementLimit && measurementLimit !== 1) { requestBody.limit = measurementLimit; } if (operation === 'measurementPing') { requestBody.type = 'ping'; let measurementOptions = {}; if (params.packets && params.packets !== 3) { measurementOptions.packets = this.getNodeParameter('packets', i); } if (Object.keys(measurementOptions).length > 0) { requestBody.measurementOptions = measurementOptions; } } if (operation === 'measurementTraceroute') { requestBody.type = 'traceroute'; let measurementOptions = {}; let moValues = this.getNodeParameter('measurementOptions', i); if (moValues.port && moValues.port !== 80) { measurementOptions.port = moValues.port; } if (moValues.protocol && moValues.protocol !== 'ICMP') { measurementOptions.protocol = moValues.protocol; } if (Object.keys(measurementOptions).length > 0) { requestBody.measurementOptions = measurementOptions; } } if (operation === 'measurementDns') { requestBody.type = 'dns'; let measurementOptions = {}; let moValues = this.getNodeParameter('measurementOptions', i); if (moValues.queryType && moValues.queryType !== 'A') { measurementOptions.query = {}; measurementOptions.query.type = moValues.queryType; } if (moValues.resolver && moValues.resolver !== '') { measurementOptions.resolver = moValues.resolver; } if (Object.keys(measurementOptions).length > 0) { requestBody.measurementOptions = measurementOptions; } } if (operation === 'measurementMtr') { requestBody.type = 'mtr'; let measurementOptions = {}; let moValues = this.getNodeParameter('measurementOptions', i); if (moValues.port && moValues.port !== 80) { measurementOptions.port = moValues.port; } if (moValues.protocol && moValues.protocol !== 'ICMP') { measurementOptions.protocol = moValues.protocol; } if (moValues.packets && moValues.packets !== 3) { measurementOptions.packets = moValues.packets; } if (Object.keys(measurementOptions).length > 0) { requestBody.measurementOptions = measurementOptions; } } if (operation === 'measurementHttp') { requestBody.type = 'http'; let measurementOptions = {}; let moValues = this.getNodeParameter('measurementOptions', i); measurementOptions.request = {}; measurementOptions.request.host = parsedTarget.host; measurementOptions.request.path = parsedTarget.path; measurementOptions.port = parsedTarget.port; if (parsedTarget.query !== '') { measurementOptions.request.query = parsedTarget.query; } if (parsedTarget.type === 'ipv4') { measurementOptions.ipVersion = 4; } else if (parsedTarget.type === 'ipv6') { measurementOptions.ipVersion = 6; } if (moValues.method && moValues.method !== 'HEAD') { measurementOptions.request.method = moValues.method; } if (moValues.headersUi) { if (moValues.headersUi.headers) { let headers = {}; for (let header of moValues.headersUi.headers) { if (header.key !== '') { headers[header.key] = header.value; } } if (Object.keys(headers).length > 0) { measurementOptions.request.headers = headers; } } } if (Object.keys(measurementOptions).length > 0) { requestBody.measurementOptions = measurementOptions; } } method = 'POST'; endpoint = '/v1/measurements'; responseData = await GenericFunctions_1.globalpingApiRequest.call(this, method, endpoint, requestBody, qs); if (responseData) { const measurementId = responseData.id; const attempts = 60; const timeout = 1; method = 'GET'; endpoint = `/v1/measurements/${measurementId}`; for (let i = attempts; i >= 1; i--) { responseData = await GenericFunctions_1.globalpingApiRequest.call(this, method, endpoint, body, qs); if (responseData.status !== 'in-progress') { break; } else { await (0, n8n_workflow_1.sleep)(timeout * 1000); } } } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message } }); continue; } throw error; } } return [returnData]; } } exports.GlobalpingV1 = GlobalpingV1; //# sourceMappingURL=GlobalpingV1.node.js.map