s-bit-agent
Version:
s.BitAgent is a simple Bitwarden CLI wrapper which provides a SSH2 Key Agent solution for Bitwarden.
62 lines • 2.65 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheService = void 0;
const common_1 = require("@nestjs/common");
const bitwarden_service_1 = require("./bitwarden.service");
const session_service_1 = require("./session.service");
const log_service_1 = require("../shared/log.service");
let CacheService = class CacheService {
constructor(bitService, logService, sessionService) {
this.bitService = bitService;
this.logService = logService;
this.sessionService = sessionService;
this.timeout = 300000;
this.cache = [];
this.scheduler = null;
this.lastRefill = 0;
}
resetTimeout() {
if (this.scheduler)
clearTimeout(this.scheduler);
this.scheduler = setTimeout(() => {
this.scheduler = null;
this.cache = [];
}, this.timeout);
}
isCacheValid() {
return this.cache.length && Date.now() - this.lastRefill < this.timeout;
}
async getCacheInternal(reason, token) {
this.resetTimeout();
if (!this.isCacheValid()) {
this.logService.info('Refilling cache');
const sess = token ? token : await this.sessionService.getSession(reason);
this.cache = this.bitService.getKeyItems(sess);
this.lastRefill = Date.now();
}
return this.cache;
}
getCache(reason) {
return this.getCacheInternal(reason, null);
}
getCacheWithToken(token) {
return this.getCacheInternal(null, token);
}
};
exports.CacheService = CacheService;
exports.CacheService = CacheService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [bitwarden_service_1.BitwardenService,
log_service_1.LogService,
session_service_1.SessionService])
], CacheService);
//# sourceMappingURL=cache.service.js.map