n8n-nodes-netsuite-markival
Version:
NetSuite integration node for n8n with SuiteQL, RESTlet, and Raw REST support
56 lines • 2.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readLicenseCache = readLicenseCache;
exports.writeLicenseCache = writeLicenseCache;
exports.checkAndUpdateLicense = checkAndUpdateLicense;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const CACHE_FILE = path_1.default.join(__dirname, '..', '..', 'data', 'license_cache.json');
const CACHE_TTL = 8 * 60 * 60 * 1000;
function getCacheKey(accountId, licenseKey) {
return `${accountId}:${licenseKey}`;
}
async function readLicenseCache(logger) {
try {
const data = await fs_1.promises.readFile(CACHE_FILE, 'utf8');
return JSON.parse(data);
}
catch (error) {
logger === null || logger === void 0 ? void 0 : logger.debug('No cache file found, starting fresh');
return {};
}
}
async function writeLicenseCache(cache, logger) {
await fs_1.promises.mkdir(path_1.default.dirname(CACHE_FILE), { recursive: true });
await fs_1.promises.writeFile(CACHE_FILE, JSON.stringify(cache, null, 2));
}
async function checkAndUpdateLicense(accountId, licenseKey, validateWithSupabase, logger) {
const cache = await readLicenseCache(logger);
const cacheKey = getCacheKey(accountId, licenseKey);
const now = Date.now();
if (cache[cacheKey] && now < cache[cacheKey].expiresAt) {
logger === null || logger === void 0 ? void 0 : logger.debug('Using cached license status', {
accountId,
expiresIn: ((cache[cacheKey].expiresAt - now) / 1000 / 60).toFixed(0) + ' minutes',
});
return cache[cacheKey].valid;
}
logger === null || logger === void 0 ? void 0 : logger.debug('Checking license with Supabase', { accountId });
try {
const isValid = await validateWithSupabase();
cache[cacheKey] = {
valid: isValid,
expiresAt: now + CACHE_TTL,
};
await writeLicenseCache(cache, logger);
return isValid;
}
catch (error) {
logger === null || logger === void 0 ? void 0 : logger.error('Failed to validate license', { error });
throw error;
}
}
//# sourceMappingURL=LicenseCache.js.map