next-l402
Version:
Lightning Network HTTP 402 Payment Required protocol implementation for Next.js applications
65 lines • 1.75 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.l402Cache = void 0;
const node_cache_1 = __importDefault(require("node-cache"));
/**
* Cache for storing L402 sessions by payment hash
* TTL: 1 hour (3600 seconds)
*/
class L402Cache {
constructor(ttlSeconds = 3600) {
this.cache = new node_cache_1.default({
stdTTL: ttlSeconds,
checkperiod: 600, // Check for expired keys every 10 minutes
useClones: false, // Don't clone objects for better performance
});
}
/**
* Store an L402 session by payment hash
*/
setSession(paymentHash, session) {
this.cache.set(paymentHash, session);
}
/**
* Retrieve an L402 session by payment hash
*/
getSession(paymentHash) {
return this.cache.get(paymentHash);
}
/**
* Remove an L402 session
*/
removeSession(paymentHash) {
this.cache.del(paymentHash);
}
/**
* Get cache statistics
*/
getStats() {
return this.cache.getStats();
}
/**
* Store a cached challenge for a route
*/
setCachedChallenge(routeKey, challenge) {
this.cache.set(`challenge:${routeKey}`, challenge);
}
/**
* Get a cached challenge for a route
*/
getCachedChallenge(routeKey) {
return this.cache.get(`challenge:${routeKey}`);
}
/**
* Clear all sessions (for testing)
*/
clear() {
this.cache.flushAll();
}
}
// Global cache instance
exports.l402Cache = new L402Cache();
//# sourceMappingURL=cache.js.map
;