@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.
36 lines • 1.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.oracleCheckedId = exports.genAsciiId = exports.genHexId = exports.genSyncId = void 0;
const crypto_1 = __importDefault(require("crypto"));
const uid_safe_1 = require("uid-safe");
const oracle_execute_1 = require("../database/oracle-execute");
function genSyncId(bytes) {
// tslint:disable-next-line: no-magic-numbers
return uid_safe_1.sync(bytes || 40);
}
exports.genSyncId = genSyncId;
function genHexId(bytes) {
return crypto_1.default.randomBytes(bytes || 16).toString("hex");
}
exports.genHexId = genHexId;
function genAsciiId(length, mixedCase) {
// tslint:disable-next-line: no-magic-numbers
const format = () => (mixedCase && Math.random() < 0.5 ? "toString" : "toUpperCase");
// tslint:disable-next-line: no-magic-numbers
return [...Array(length || 32)].map(() => Math.random().toString(36)[2][format()]()).join("");
}
exports.genAsciiId = genAsciiId;
async function oracleCheckedId(genId, table, field) {
var _a;
const id = genId();
const result = await oracle_execute_1.oracleExecute(`SELECT * FROM ${table} WHERE ${field} = :id`, { id });
if ((_a = result.rows) === null || _a === void 0 ? void 0 : _a.length) {
return oracleCheckedId(genId, table, field);
}
return id;
}
exports.oracleCheckedId = oracleCheckedId;
//# sourceMappingURL=id-generation.js.map