UNPKG

otp-gen-agent

Version:

A small and secure one time password (otp) generator for Javascript based on nanoid

104 lines (101 loc) 3.29 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { WEBHOOK_EVENTS: () => WEBHOOK_EVENTS, bulkOtpGen: () => bulkOtpGen, customOtpGen: () => customOtpGen, otpGen: () => otpGen, setWebhookHandler: () => setWebhookHandler }); module.exports = __toCommonJS(index_exports); var import_nanoid = require("nanoid"); // src/constants.ts var ITEM_ALPHABET = "0123456789"; var OTP_LENGTH = 6; var WEBHOOK_EVENTS = { OTP_GENERATED: "otp-generated", OTP_BULK_GENERATED: "bulk-otp-generated" }; // src/index.ts var nid = (0, import_nanoid.customAlphabet)(ITEM_ALPHABET, OTP_LENGTH); var webhookHandler = null; var setWebhookHandler = (handler) => { if (typeof handler !== "function") { throw new Error("Webhook handler must be a function"); } webhookHandler = handler; }; var triggerWebhook = async (event, data) => { if (webhookHandler) { try { await webhookHandler(event, data); } catch (error) { throw new Error("Error in running Webhook handler"); } } }; var otpGen = async () => { const otp = nid(); await triggerWebhook(WEBHOOK_EVENTS.OTP_GENERATED, { otp }); return otp; }; var customOtpGen = async ({ length = OTP_LENGTH, chars = ITEM_ALPHABET } = {}) => { if (!Number.isInteger(length) || length <= 0) { throw new Error("otp length must be greater than 0"); } const idGen = (0, import_nanoid.customAlphabet)(chars, length); const otp = idGen(); await triggerWebhook(WEBHOOK_EVENTS.OTP_GENERATED, { otp }); return otp; }; var YIELD_EVERY = 1e3; var yieldToEventLoop = () => new Promise((resolve) => setTimeout(resolve, 0)); var bulkOtpGen = async (count = 0, opts = {}) => { if (!Number.isInteger(count) || count <= 0) { throw new Error("count must be greater than 0"); } const { length = OTP_LENGTH, chars = ITEM_ALPHABET } = opts; if (!Number.isInteger(length) || length <= 0) { throw new Error("otp length must be greater than 0"); } const idGen = (0, import_nanoid.customAlphabet)(chars, length); const idArr = new Array(count); for (let i = 0; i < count; i++) { idArr[i] = idGen(); if ((i + 1) % YIELD_EVERY === 0) { await yieldToEventLoop(); } } await triggerWebhook(WEBHOOK_EVENTS.OTP_BULK_GENERATED, { count, otps: idArr }); return idArr; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WEBHOOK_EVENTS, bulkOtpGen, customOtpGen, otpGen, setWebhookHandler }); //# sourceMappingURL=index.cjs.map