n8n-nodes-duckduckgo-search
Version:
A powerful and comprehensive n8n community node that seamlessly integrates DuckDuckGo search capabilities into your workflows. Search the web, find images, discover news, and explore videos - all with privacy-focused, reliable results.
138 lines (137 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProxyStatusMessage = exports.testProxyConnection = exports.applyProxyToDDG = exports.applyProxyToRequest = exports.getProxyConfig = exports.createProxyAgent = exports.parseProxyUrl = void 0;
const https_proxy_agent_1 = require("https-proxy-agent");
const socks_proxy_agent_1 = require("socks-proxy-agent");
const url_1 = require("url");
function parseProxyUrl(proxyUrl) {
let url;
try {
url = new url_1.URL(proxyUrl);
}
catch (error) {
throw new Error(`Invalid proxy URL: ${proxyUrl}. Expected format: protocol://[user:pass@]host:port`);
}
const protocol = url.protocol.replace(':', '');
if (!['http', 'https', 'socks4', 'socks5'].includes(protocol)) {
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
const config = {
protocol: protocol,
host: url.hostname,
port: url.port ? parseInt(url.port) : getDefaultPort(protocol),
};
if (url.username && url.password) {
config.auth = {
username: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
};
}
return config;
}
exports.parseProxyUrl = parseProxyUrl;
function getDefaultPort(protocol) {
switch (protocol) {
case 'http':
return 8080;
case 'https':
return 443;
case 'socks4':
case 'socks5':
return 1080;
default:
return 8080;
}
}
function createProxyAgent(config) {
const authString = config.auth
? `${config.auth.username}:${config.auth.password}@`
: '';
const proxyUrl = `${config.protocol}://${authString}${config.host}:${config.port}`;
if (config.protocol === 'socks4' || config.protocol === 'socks5') {
return new socks_proxy_agent_1.SocksProxyAgent(proxyUrl);
}
else {
return new https_proxy_agent_1.HttpsProxyAgent(proxyUrl);
}
}
exports.createProxyAgent = createProxyAgent;
function getProxyConfig(context, itemIndex) {
const proxySettings = context.getNodeParameter('proxySettings', itemIndex, {});
if (!proxySettings.useProxy) {
return undefined;
}
const proxyType = proxySettings.proxyType || 'http';
const proxyHost = proxySettings.proxyHost || '';
const proxyPort = proxySettings.proxyPort || 8080;
const proxyAuth = proxySettings.proxyAuth || false;
if (!proxyHost) {
throw new Error('Proxy host is required when proxy is enabled');
}
const config = {
protocol: proxyType,
host: proxyHost,
port: proxyPort,
};
if (proxyAuth) {
const proxyUsername = proxySettings.proxyUsername || '';
const proxyPassword = proxySettings.proxyPassword || '';
if (!proxyUsername || !proxyPassword) {
throw new Error('Proxy username and password are required when proxy authentication is enabled');
}
config.auth = {
username: proxyUsername,
password: proxyPassword,
};
}
return config;
}
exports.getProxyConfig = getProxyConfig;
function applyProxyToRequest(options, proxyConfig) {
if (!proxyConfig) {
return options;
}
const agent = createProxyAgent(proxyConfig);
return {
...options,
agent,
proxy: undefined,
};
}
exports.applyProxyToRequest = applyProxyToRequest;
function applyProxyToDDG(options, proxyConfig) {
if (!proxyConfig) {
return options;
}
const agent = createProxyAgent(proxyConfig);
return {
...options,
requestOptions: {
...options.requestOptions,
agent,
},
};
}
exports.applyProxyToDDG = applyProxyToDDG;
async function testProxyConnection(context, proxyConfig) {
try {
const testUrl = 'https://api.duckduckgo.com/';
const proxyOptions = applyProxyToRequest({
method: 'GET',
url: testUrl,
timeout: 10000,
}, proxyConfig);
const response = await context.helpers.request(proxyOptions);
return response !== undefined;
}
catch (error) {
context.logger.error(`Proxy test failed: ${error.message}`);
return false;
}
}
exports.testProxyConnection = testProxyConnection;
function getProxyStatusMessage(config) {
const authInfo = config.auth ? ' (authenticated)' : '';
return `Using ${config.protocol.toUpperCase()} proxy: ${config.host}:${config.port}${authInfo}`;
}
exports.getProxyStatusMessage = getProxyStatusMessage;