UNPKG

@dataql/node

Version:

DataQL core SDK for unified data management with MongoDB and GraphQL - Production Multi-Cloud Ready

112 lines (111 loc) 5.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GLOBAL_ENDPOINTS = exports.DEFAULT_GLOBAL_CONFIG = exports.DEFAULT_WORKER_ENDPOINTS = exports.ALL_ENDPOINTS = exports.WORKER_ENDPOINTS = exports.SERVERLESS_ENDPOINTS = void 0; exports.testEndpointPerformance = testEndpointPerformance; exports.selectBestEndpoint = selectBestEndpoint; exports.getWorkerUrl = getWorkerUrl; exports.detectEnvironment = detectEnvironment; // IMPORTANT: These are the serverless function URLs used by the WORKER for backend routing // The SDK should NOT call these directly - it should call the worker URL // The worker handles session creation and routing to these serverless functions exports.SERVERLESS_ENDPOINTS = { aws: [ "https://sglqyw134l.execute-api.us-east-1.amazonaws.com/data", "https://pho6fk2l3h.execute-api.us-east-2.amazonaws.com/data", "https://9b6s23cku9.execute-api.us-west-2.amazonaws.com/data", "https://i9pxdyzrt7.execute-api.eu-west-1.amazonaws.com/data", "https://3vj7hmpms8.execute-api.ap-southeast-1.amazonaws.com/data", ], gcp: [ "https://us-east1-data-ql.cloudfunctions.net/dataql-mongodb-gcp", "https://us-east4-data-ql.cloudfunctions.net/dataql-mongodb-gcp", "https://us-west1-data-ql.cloudfunctions.net/dataql-mongodb-gcp", "https://europe-west1-data-ql.cloudfunctions.net/dataql-mongodb-gcp", "https://asia-southeast1-data-ql.cloudfunctions.net/dataql-mongodb-gcp", ], azure: [ "https://dataql-mongodb-azure-eastus.azurewebsites.net/api/dataql-mongodb", "https://dataql-mongodb-azure-eastus2.azurewebsites.net/api/dataql-mongodb", "https://dataql-mongodb-azure-westus2.azurewebsites.net/api/dataql-mongodb", "https://dataql-mongodb-azure-westeurope.azurewebsites.net/api/dataql-mongodb", "https://dataql-mongodb-azure-southeastasia.azurewebsites.net/api/dataql-mongodb", ], }; // DEPRECATED: This was incorrectly exposing serverless functions to the SDK // export const GLOBAL_ENDPOINTS: MultiCloudEndpoints = SERVERLESS_ENDPOINTS; // SDK should only use worker endpoints, not serverless functions directly exports.WORKER_ENDPOINTS = [ "https://edge.dataql.com", // Future: Could add additional worker regions here ]; // For backwards compatibility, this should now return worker endpoints exports.ALL_ENDPOINTS = exports.WORKER_ENDPOINTS; // Default endpoints for different environments exports.DEFAULT_WORKER_ENDPOINTS = { development: "https://edge.dataql.com", staging: "https://edge.dataql.com", production: "https://edge.dataql.com", }; // Performance testing for endpoint selection async function testEndpointPerformance(endpoint, timeout = 5000) { const startTime = Date.now(); try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); const response = await fetch(`${endpoint}/health`, { method: "GET", signal: controller.signal, headers: { "Content-Type": "application/json", }, }); clearTimeout(timeoutId); const responseTime = Date.now() - startTime; return { url: endpoint, responseTime, available: response.ok, }; } catch (error) { return { url: endpoint, responseTime: Date.now() - startTime, available: false, }; } } // Intelligent endpoint selection - now uses worker endpoints only async function selectBestEndpoint(endpoints = exports.ALL_ENDPOINTS, maxConcurrent = 5) { // Test endpoints in parallel (limited concurrency) const results = []; for (let i = 0; i < endpoints.length; i += maxConcurrent) { const batch = endpoints.slice(i, i + maxConcurrent); const batchResults = await Promise.all(batch.map((endpoint) => testEndpointPerformance(endpoint))); results.push(...batchResults); } // Filter available endpoints and sort by response time const availableEndpoints = results .filter((result) => result.available) .sort((a, b) => a.responseTime - b.responseTime); // Return the fastest available endpoint, or fallback to default return availableEndpoints.length > 0 ? availableEndpoints[0].url : exports.DEFAULT_WORKER_ENDPOINTS.production; } function getWorkerUrl(_environment) { // Return the hardcoded production URL for production environment // Applications can use selectBestEndpoint() for intelligent routing return "https://edge.dataql.com"; } function detectEnvironment() { // Always return production for backward compatibility return "production"; } exports.DEFAULT_GLOBAL_CONFIG = { enableGlobalRouting: true, maxEndpointTestTime: 3000, fallbackEndpoint: "https://edge.dataql.com", }; // Export serverless endpoints for worker use only exports.GLOBAL_ENDPOINTS = exports.SERVERLESS_ENDPOINTS;