Files

129 lines
5.0 KiB
JavaScript

"use strict";
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.FileCookieStore = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const tough_cookie_1 = require("tough-cookie");
class FileCookieStore {
filePath;
options;
static sharedJars = new Map();
constructor(filePath, options = {}) {
this.filePath = filePath;
this.options = options;
}
async load() {
const sharedJar = FileCookieStore.sharedJars.get(this.filePath);
if (sharedJar) {
return sharedJar;
}
try {
if (!fs.existsSync(this.filePath)) {
this.options.logger?.info?.('Neuer Cookie-Store wird erstellt');
return this.remember(new tough_cookie_1.CookieJar());
}
const raw = fs.readFileSync(this.filePath, 'utf8');
const parsed = JSON.parse(raw);
if (this.isEncryptedCookieFile(parsed)) {
if (!this.options.encryptor) {
throw new Error('Cookie-Datei ist verschluesselt, aber kein Encryptor wurde konfiguriert');
}
const decrypted = this.options.encryptor.decrypt(parsed.data);
const cookieJson = JSON.parse(decrypted);
return this.remember(this.cookieJarFromJson(cookieJson));
}
return this.remember(this.cookieJarFromJson(parsed));
}
catch (error) {
this.options.logger?.warn?.('Cookies konnten nicht geladen werden, neuer Cookie-Store wird erstellt', error);
return this.remember(new tough_cookie_1.CookieJar());
}
}
async save(cookieJar) {
FileCookieStore.sharedJars.set(this.filePath, cookieJar);
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
const cookieJson = cookieJar.toJSON();
let fileContent;
if (this.options.encryptor) {
fileContent = JSON.stringify({
encrypted: true,
algorithm: 'aes-256-gcm',
data: this.options.encryptor.encrypt(JSON.stringify(cookieJson)),
updatedAt: new Date().toISOString(),
}, null, 2);
}
else {
fileContent = JSON.stringify(cookieJson, null, 2);
}
fs.writeFileSync(this.filePath, fileContent, {
mode: 0o600,
});
}
async clear() {
FileCookieStore.sharedJars.set(this.filePath, new tough_cookie_1.CookieJar());
if (fs.existsSync(this.filePath)) {
fs.unlinkSync(this.filePath);
}
}
isEncryptedCookieFile(value) {
return (typeof value === 'object' &&
value !== null &&
value.encrypted === true &&
value.algorithm === 'aes-256-gcm' &&
typeof value.data === 'string');
}
cookieJarFromJson(value) {
if (Array.isArray(value)) {
const cookieJar = new tough_cookie_1.CookieJar();
for (const cookieData of value) {
const cookie = tough_cookie_1.Cookie.fromJSON(cookieData);
if (cookie) {
const domain = cookie.domain ?? 'admin7.agrarmonitor.de';
const url = domain.startsWith('http') ? domain : `https://${domain}`;
cookieJar.setCookieSync(cookie, url);
}
}
return cookieJar;
}
return tough_cookie_1.CookieJar.fromJSON(JSON.stringify(value));
}
remember(cookieJar) {
FileCookieStore.sharedJars.set(this.filePath, cookieJar);
return cookieJar;
}
}
exports.FileCookieStore = FileCookieStore;
//# sourceMappingURL=FileCookieStore.js.map