UNPKG

@szegedsw/lib-node

Version:

A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.

53 lines 2.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProcessLock = void 0; const crypto_1 = __importDefault(require("crypto")); const logger_1 = require("../logger/logger"); const lockhash = crypto_1.default.createHash("sha256").update(new Date().toISOString()).digest("hex"); let ProcessLock = /** @class */ (() => { class ProcessLock { static async lock(type) { // starting lock process logger_1.Logger.trace(`Locking process type(s) of '${type}'...`); if (typeof type === "string") { while (ProcessLock.locks[type]) { await (async () => null).sleep(1); } ProcessLock.locks[type] = true; } else { // lock this process, only one can have access await ProcessLock.lock(lockhash); // locking multiple resources await Promise.all(type.map(async (t) => { await this.lock(t); })); // unlocking this process ProcessLock.unlock(lockhash); } logger_1.Logger.trace(`Successfully locked process type(s) of '${type}'!`); } static unlock(type) { logger_1.Logger.trace(`Unlocking process type(s) of '${type}'...`); if (typeof type === "string") { ProcessLock.locks[type] = false; } else { type.forEach((t) => (ProcessLock.locks[t] = false)); } logger_1.Logger.trace(`Successfully unlocked process type(s) of '${type}'!`); } } Object.defineProperty(ProcessLock, "locks", { enumerable: true, configurable: true, writable: true, value: {} }); return ProcessLock; })(); exports.ProcessLock = ProcessLock; //# sourceMappingURL=process-lock.js.map