n8n-nodes-everest-tms
Version:
Everest TMS n8n integration
208 lines • 7.09 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.readTokenFromCache = readTokenFromCache;
exports.writeTokenToCache = writeTokenToCache;
exports.clearTokenFromCache = clearTokenFromCache;
exports.cleanupExpiredTokens = cleanupExpiredTokens;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const crypto = __importStar(require("crypto"));
// Cache directory - using relative path for better portability
const CACHE_DIR = './tokens';
/**
* Generate a unique cache key for credentials
* @param domain - API domain
* @param clientId - Client ID
* @returns Unique cache key
*/
function generateCacheKey(domain, clientId) {
const keyData = `${domain}:${clientId}`;
return crypto.createHash('sha256').update(keyData).digest('hex');
}
/**
* Get the cache file path for a given cache key
* @param cacheKey - Unique cache key
* @returns Full path to the cache file
*/
function getCacheFilePath(cacheKey) {
return path.join(CACHE_DIR, `${cacheKey}.json`);
}
/**
* Ensure the cache directory exists
*/
function ensureCacheDirectory() {
try {
if (!fs.existsSync(CACHE_DIR)) {
fs.mkdirSync(CACHE_DIR, { recursive: true });
}
}
catch (error) {
// Silently fail if we can't create the directory
// This ensures the node still works even if caching fails
}
}
/**
* Read token from cache
* @param domain - API domain
* @param clientId - Client ID
* @returns Token data if valid, null otherwise
*/
function readTokenFromCache(domain, clientId) {
try {
const cacheKey = generateCacheKey(domain, clientId);
const cacheFilePath = getCacheFilePath(cacheKey);
if (!fs.existsSync(cacheFilePath)) {
return null;
}
const cacheContent = fs.readFileSync(cacheFilePath, 'utf8');
const tokenData = JSON.parse(cacheContent);
// Check if token is expired (with 5 minute buffer)
const now = Math.floor(Date.now() / 1000);
const bufferTime = 5 * 60; // 5 minutes
if (tokenData.expires_at && tokenData.expires_at <= (now + bufferTime)) {
// Token is expired or will expire soon, remove it
try {
fs.unlinkSync(cacheFilePath);
}
catch (error) {
// Ignore deletion errors
}
return null;
}
// Verify the token data matches the current credentials
if (tokenData.domain !== domain || tokenData.client_id !== clientId) {
// Credentials don't match, remove the cache file
try {
fs.unlinkSync(cacheFilePath);
}
catch (error) {
// Ignore deletion errors
}
return null;
}
return tokenData;
}
catch (error) {
// If there's any error reading the cache, return null
return null;
}
}
/**
* Write token to cache
* @param domain - API domain
* @param clientId - Client ID
* @param bearerToken - Bearer token to cache
* @param expiresIn - Token expiration time in seconds (optional)
*/
function writeTokenToCache(domain, clientId, bearerToken, expiresIn) {
try {
ensureCacheDirectory();
const cacheKey = generateCacheKey(domain, clientId);
const cacheFilePath = getCacheFilePath(cacheKey);
// Calculate expiration time
// Default to 1 hour if not provided
const defaultExpirationSeconds = 60 * 60; // 1 hour
const expirationSeconds = expiresIn || defaultExpirationSeconds;
const expiresAt = Math.floor(Date.now() / 1000) + expirationSeconds;
const tokenData = {
bearer_token: bearerToken,
expires_at: expiresAt,
domain,
client_id: clientId,
};
fs.writeFileSync(cacheFilePath, JSON.stringify(tokenData, null, 2), 'utf8');
}
catch (error) {
// Silently fail if we can't write to cache
// This ensures the node still works even if caching fails
}
}
/**
* Clear token from cache
* @param domain - API domain
* @param clientId - Client ID
*/
function clearTokenFromCache(domain, clientId) {
try {
const cacheKey = generateCacheKey(domain, clientId);
const cacheFilePath = getCacheFilePath(cacheKey);
if (fs.existsSync(cacheFilePath)) {
fs.unlinkSync(cacheFilePath);
}
}
catch (error) {
// Ignore deletion errors
}
}
/**
* Clean up expired tokens from cache
* This function can be called periodically to clean up old cache files
*/
function cleanupExpiredTokens() {
try {
if (!fs.existsSync(CACHE_DIR)) {
return;
}
const files = fs.readdirSync(CACHE_DIR);
const now = Math.floor(Date.now() / 1000);
for (const file of files) {
if (!file.endsWith('.json')) {
continue;
}
const filePath = path.join(CACHE_DIR, file);
try {
const content = fs.readFileSync(filePath, 'utf8');
const tokenData = JSON.parse(content);
if (tokenData.expires_at && tokenData.expires_at <= now) {
fs.unlinkSync(filePath);
}
}
catch (error) {
// If we can't parse the file, delete it
try {
fs.unlinkSync(filePath);
}
catch (deleteError) {
// Ignore deletion errors
}
}
}
}
catch (error) {
// Ignore cleanup errors
}
}
//# sourceMappingURL=TokenCache.js.map