UNPKG

card-bin-db

Version:

A fast and lightweight library for querying bank card information (issuer, brand, type, country) based on BIN (Bank Identification Number).

131 lines (126 loc) 3.94 kB
// node_modules/.pnpm/tsup@8.5.0_typescript@5.9.2/node_modules/tsup/assets/esm_shims.js import path from "path"; import { fileURLToPath } from "url"; var getFilename = () => fileURLToPath(import.meta.url); var getDirname = () => path.dirname(getFilename()); var __dirname = /* @__PURE__ */ getDirname(); // src/config.ts import path2 from "path"; import fs from "fs/promises"; async function exists(path3) { try { await fs.access(path3); return true; } catch { return false; } } var dataDir = path2.resolve(__dirname, "../bindb"); async function loadBins(name3, keys3) { const filePath = path2.join(dataDir, `${name3}.csv`); if (!await exists(filePath)) { throw new Error(`File not found: ${filePath}`); } const result = {}; (await fs.readFile(filePath, "utf-8")).split("\n").forEach((line) => { const parts = line.trim().split(","); const obj = {}; keys3.forEach((key, index) => { obj[key] = decodeURIComponent(parts[index] || ""); }); const bin = obj.bin; if (bin) { bin.split("/").forEach((part) => { if (part.includes("-")) { const arr = part.split("-"); let start = Number(arr[0]); let end = Number(arr[1]); Array.from({ length: end - start + 1 }).map((_, i) => i + start); for (let i = start; i <= end; i++) { const _bin = i.toString(); result[_bin] = obj; } } else { result[part] = obj; } }); delete obj.bin; } result[obj.bin] = obj; }); return result; } var initializing = {}; async function initializeOnce(key, callback) { return new Promise(async (resolve, reject) => { if (!initializing[key]) { initializing[key] = []; initializing[key].push({ resolve, reject }); await callback().then((value) => { initializing[key] && initializing[key].forEach((item) => item.resolve(value)); }).catch((reason) => { initializing[key] && initializing[key].forEach((item) => item.reject(reason)); }); } else { initializing[key].push({ resolve, reject }); } }); } var stores = {}; async function getEngine(name3, keys3) { if (!stores[name3]) { await initializeOnce(name3, async () => { return loadBins(name3, keys3); }).then((value) => { if (value) { stores[name3] = value; } }); } return stores[name3]; } // src/bincheck.ts var name = "bincheck"; var keys = ["bin", "card_brand", "card_type", "card_level", "bank_name", "bank_website", "bank_phone", "country_name", "country_code", "country_iso3", "currency"]; async function getEngineBinCheck() { return await getEngine(name, keys); } // src/pst.ts var name2 = "pst"; var keys2 = ["bin", "country_iso3", "bank_name", "card_level", "card_brand", "card_type"]; async function getEnginePst() { return await getEngine(name2, keys2); } // src/index.ts import countries from "world-countries"; async function lookupBin(bin, engine = "bincheck") { bin = bin.substring(0, 6); let binCheck = {}; if (engine === "pst") { binCheck = await getEnginePst(); } else if (engine === "bincheck") { binCheck = await getEngineBinCheck(); } else { throw new Error("Unknown engine: " + engine); } if (binCheck[bin]) { const info = binCheck[bin]; const country = countries.find((c) => c.cca3 === info.country_iso3 || c.cca2 === info.country_code); return { bin, card_brand: info.card_brand, card_type: info.card_type, card_level: info.card_level, bank_name: info.bank_name, country_name: info.country_name ?? country?.name?.common ?? "", country_code: info.country_code ?? country?.cca2 ?? "", country_iso3: info.country_iso3 ?? country?.cca3 ?? "", currency: country ? Object.keys(country.currencies)[0] : info.currency || "" }; } } export { getEngineBinCheck, getEnginePst, lookupBin };