/**
* Script Fetcher Utility
* Loads automation scripts from Apify KV store
*/
const { ApifyClient } = require('apify-client');
const scriptCache = new Map();
/**
* Fetch script from Apify KV store
*
* @param {string} scriptUrl - Full URL to KV store record
* Format: https://api.apify.com/v2/key-value-stores/{storeId}/records/{key}
* @param {string} apifyToken - Apify API token
* @returns {Promise<string>} The automation script
*/
const fetchScript = async (scriptUrl, apifyToken) => {
if (!scriptUrl || !apifyToken) {
throw new Error('scriptUrl and apifyToken are required to fetch script');
}
// Check cache first
if (scriptCache.has(scriptUrl)) {
return scriptCache.get(scriptUrl);
}
try {
// Extract store ID and record key from URL
// URL format: https://api.apify.com/v2/key-value-stores/{storeId}/records/{key}
const match = scriptUrl.match(/\/key-value-stores\/([^/]+)\/records\/(.+)$/);
if (!match) {
throw new Error(`Invalid script URL format: ${scriptUrl}`);
}
const [, storeId, recordKey] = match;
// Create Apify client and fetch from KV store
const client = new ApifyClient({ token: apifyToken });
const kvStore = client.keyValueStore(storeId);
const record = await kvStore.getRecord(recordKey);
if (!record) {
throw new Error(`Script not found at ${scriptUrl}`);
}
// Cache the script for future calls
scriptCache.set(scriptUrl, record.value);
return record.value;
} catch (error) {
throw new Error(`Failed to fetch script from KV store: ${error.message}`);
}
};
const clearScriptCache = () => {
scriptCache.clear();
};
const getCacheStats = () => {
return {
cachedScripts: scriptCache.size,
urls: Array.from(scriptCache.keys())
};
};
module.exports = {
fetchScript,
clearScriptCache,
getCacheStats
};