Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add mock source config api to test device mode sdk loading #1856

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions scripts/common.js
Original file line number Diff line number Diff line change
@@ -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,
};
117 changes: 117 additions & 0 deletions scripts/dummySourceConfigApiToTestSdKs.js
Original file line number Diff line number Diff line change
@@ -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();

koladilip marked this conversation as resolved.
Show resolved Hide resolved
// 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 };
}
koladilip marked this conversation as resolved.
Show resolved Hide resolved

// 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}`);
});
26 changes: 1 addition & 25 deletions scripts/generateConstants.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
/* 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
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
Expand Down
Loading