diff --git a/scripts/common.js b/scripts/common.js new file mode 100644 index 000000000..e41e20c79 --- /dev/null +++ b/scripts/common.js @@ -0,0 +1,28 @@ +const languageMap = { + web: ['ts', 'js'], + flutter: ['dart'], + ios: ['m', 'swift'], + android: ['kt', 'java'], +}; + +// Function to check if the template should be generated for a specific language +function filterLanguages(destination, langCode) { + if (!destination?.config?.supportedConnectionModes) { + console.warn(`Destination ${destination.name} is missing supportedConnectionModes`); + return false; + } + const { supportedConnectionModes } = destination.config; + + // Filtering logic + return Object.keys(supportedConnectionModes) + .filter((platform) => { + const modes = supportedConnectionModes[platform]; + // Check if "device" or "hybrid" mode is present + return modes.includes('device') || modes.includes('hybrid'); + }) + .some((platform) => languageMap[platform]?.includes(langCode)); +} + +module.exports = { + filterLanguages, +}; diff --git a/scripts/dummySourceConfigApiToTestSdKs.js b/scripts/dummySourceConfigApiToTestSdKs.js new file mode 100644 index 000000000..f115ac5c0 --- /dev/null +++ b/scripts/dummySourceConfigApiToTestSdKs.js @@ -0,0 +1,117 @@ +/* eslint-disable no-console */ +const fs = require('fs'); +const path = require('path'); +const http = require('http'); +const url = require('url'); +const { filterLanguages } = require('./common'); + +const destinationsDir = path.join(__dirname, '../src/configurations/destinations'); + +const getJSDeviceModeDestinations = () => + fs + .readdirSync(destinationsDir) + .map((destination) => { + const dbConfig = fs.readFileSync( + path.join(destinationsDir, destination, 'db-config.json'), + 'utf8', + ); + return JSON.parse(dbConfig); + }) + .filter((destination) => filterLanguages(destination, 'js')) + .map((destinationDefinition) => { + const cleanDestName = destinationDefinition.name.replace(/[^\dA-Za-z]/g, ''); + return { + id: cleanDestName, + name: destinationDefinition.name, + config: {}, + enabled: true, + destinationDefinitionId: cleanDestName, + destinationDefinition: { + name: destinationDefinition.name, + displayName: destinationDefinition.displayName, + }, + updatedAt: new Date().toISOString(), + }; + }); + +const deviceModeJSDestinations = getJSDeviceModeDestinations(); + +// Sample JSON response for /sourceConfig +const getSourceConfig = (writeKey) => ({ + source: { + id: 'someSourceId', + name: 'http dev', + writeKey, + config: { + statsCollection: { + errors: { + enabled: false, + }, + metrics: { + enabled: false, + }, + }, + }, + enabled: true, + workspaceId: 'someWorkspaceId', + destinations: deviceModeJSDestinations, + updatedAt: new Date().toISOString(), + dataplanes: {}, + }, + updatedAt: new Date().toISOString(), +}); + +// Function to decode and validate Basic Auth +function getAuthInfo(authHeader) { + if (!authHeader || !authHeader.startsWith('Basic ')) { + return {}; + } + + // Decode the Base64 string + const base64Credentials = authHeader.split(' ')[1]; + const credentials = Buffer.from(base64Credentials, 'base64').toString('utf-8'); + const [username, password] = credentials.split(':'); + + return { username, password }; +} + +// Function to handle CORS +function setCorsHeaders(res) { + res.setHeader('Access-Control-Allow-Origin', '*'); // Allow all origins + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); // Allowed methods + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Allowed headers +} + +// Create the HTTP server +const server = http.createServer((req, res) => { + // Set CORS headers + setCorsHeaders(res); + + // Handle OPTIONS preflight requests for CORS + if (req.method === 'OPTIONS') { + res.writeHead(204); // No Content + res.end(); + return; + } + + // Parse the request URL + const parsedUrl = url.parse(req.url, true); // `true` parses query parameters into an object + const { query } = parsedUrl; + + const authHeader = req.headers.authorization; + + const authInfo = getAuthInfo(authHeader); + const writeKey = authInfo?.username || query.writeKey; + + // Set response headers + res.writeHead(200, { 'Content-Type': 'application/json' }); + + // Send JSON response + res.end(JSON.stringify(getSourceConfig(writeKey))); +}); + +// Start the server +const PORT = process.env.PORT || 8000; +server.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +}); diff --git a/scripts/generateConstants.js b/scripts/generateConstants.js index e19f68da1..ec6a9d78c 100644 --- a/scripts/generateConstants.js +++ b/scripts/generateConstants.js @@ -1,6 +1,7 @@ /* eslint-disable no-console */ const fs = require('fs'); const path = require('path'); +const { filterLanguages } = require('./common'); const destinationNameRegex = /^\w+$/; // Path to the templates and generated files @@ -8,31 +9,6 @@ const templatesDir = path.join(__dirname, '../templates'); const generatedDir = path.join(__dirname, '../generated'); const destinationsDir = path.join(__dirname, '../src/configurations/destinations'); -const languageMap = { - web: ['ts', 'js'], - flutter: ['dart'], - ios: ['m', 'swift'], - android: ['kt', 'java'], -}; - -// Function to check if the template should be generated for a specific language -function filterLanguages(destination, langCode) { - if (!destination?.config?.supportedConnectionModes) { - console.warn(`Destination ${destination.name} is missing supportedConnectionModes`); - return false; - } - const { supportedConnectionModes } = destination.config; - - // Filtering logic - return Object.keys(supportedConnectionModes) - .filter((platform) => { - const modes = supportedConnectionModes[platform]; - // Check if "device" or "hybrid" mode is present - return modes.includes('device') || modes.includes('hybrid'); - }) - .some((platform) => languageMap[platform]?.includes(langCode)); -} - // Function to read the template file and process it function processTemplate(template, data) { // Create a function to evaluate the template with the data