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).

124 lines (116 loc) 5.05 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/config.ts var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises); async function exists(path2) { try { await _promises2.default.access(path2); return true; } catch (e) { return false; } } var dataDir = _path2.default.resolve(__dirname, "../bindb"); async function loadBins(name3, keys3) { const filePath = _path2.default.join(dataDir, `${name3}.csv`); if (!await exists(filePath)) { throw new Error(`File not found: ${filePath}`); } const result = {}; (await _promises2.default.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 var _worldcountries = require('world-countries'); var _worldcountries2 = _interopRequireDefault(_worldcountries); 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 = _worldcountries2.default.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: _nullishCoalesce(_nullishCoalesce(info.country_name, () => ( _optionalChain([country, 'optionalAccess', _2 => _2.name, 'optionalAccess', _3 => _3.common]))), () => ( "")), country_code: _nullishCoalesce(_nullishCoalesce(info.country_code, () => ( _optionalChain([country, 'optionalAccess', _4 => _4.cca2]))), () => ( "")), country_iso3: _nullishCoalesce(_nullishCoalesce(info.country_iso3, () => ( _optionalChain([country, 'optionalAccess', _5 => _5.cca3]))), () => ( "")), currency: country ? Object.keys(country.currencies)[0] : info.currency || "" }; } } exports.getEngineBinCheck = getEngineBinCheck; exports.getEnginePst = getEnginePst; exports.lookupBin = lookupBin;