jwt-token-pair-generator
Version:
A secure RSA token pair generator CLI and library for generating and storing RSA keys for JWT
137 lines • 5.31 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const pino_1 = __importDefault(require("pino"));
const pino_pretty_1 = __importDefault(require("pino-pretty"));
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
// Define custom levels
const customLevels = {
step: 50, // Between info (30) and warn (40)
};
class AsyncFileWriter {
constructor(logFilePath) {
this.queue = [];
this.isWriting = false;
this.logFilePath = logFilePath;
this.initializeFile();
}
initializeFile() {
return __awaiter(this, void 0, void 0, function* () {
try {
// Create directory if it doesn't exist
yield promises_1.default.mkdir(path_1.default.dirname(this.logFilePath), { recursive: true });
// Create file if it doesn't exist
try {
yield promises_1.default.access(this.logFilePath);
}
catch (_a) {
yield promises_1.default.writeFile(this.logFilePath, "[]", "utf-8");
}
}
catch (error) {
console.error("Error initializing log file:", error);
}
});
}
addToQueue(entry) {
return __awaiter(this, void 0, void 0, function* () {
this.queue.push(entry);
if (!this.isWriting) {
yield this.processQueue();
}
});
}
processQueue() {
return __awaiter(this, void 0, void 0, function* () {
if (this.queue.length === 0 || this.isWriting) {
return;
}
this.isWriting = true;
try {
// Read existing logs
let logs = [];
try {
const content = yield promises_1.default.readFile(this.logFilePath, "utf-8");
logs = JSON.parse(content);
}
catch (error) {
logs = [];
}
// Add new logs from queue
logs.push(...this.queue);
this.queue = [];
// Write all logs back to file with pretty formatting
yield promises_1.default.writeFile(this.logFilePath, JSON.stringify(logs, null, 2), "utf-8");
}
catch (error) {
console.error("Error writing to log file:", error);
}
finally {
this.isWriting = false;
// If more logs were added while writing, process them
if (this.queue.length > 0) {
yield this.processQueue();
}
}
});
}
}
// Create and configure the logger
const createLogger = (logLevel, logFilePath = path_1.default.join(process.cwd(), "logs", "log.json")) => {
const fileWriter = new AsyncFileWriter(logFilePath);
// Configure pino-pretty stream
const prettyStream = (0, pino_pretty_1.default)({
colorize: true,
levelFirst: true,
translateTime: "SYS:standard",
customColors: "step:magenta",
customLevels: "step:35",
customPrettifiers: {
time: (timestamp) => `🕒 ${timestamp}`,
step: (level) => "👉 STEP",
},
});
// Custom destination function for file writing
const fileDestination = (msg) => {
try {
const logObject = JSON.parse(msg);
const entry = {
time: new Date(logObject.time).toISOString(),
logLevel: logObject.level,
logMessage: logObject.msg,
};
// Use setImmediate to make it non-blocking
setImmediate(() => {
fileWriter.addToQueue(entry).catch(console.error);
});
}
catch (error) {
console.error("Error processing log:", error);
}
};
const logger = (0, pino_1.default)({
customLevels: customLevels,
useOnlyCustomLevels: false,
level: logLevel || "warn",
formatters: {
level: (label) => ({ level: label }),
},
}, pino_1.default.multistream([{ stream: prettyStream }, { stream: { write: fileDestination } }]));
return logger;
};
// Export singleton instance
exports.logger = createLogger;
//# sourceMappingURL=logger.js.map