@lock-dev/geo-block
Version:
Geographic blocking module for lock.dev security framework
39 lines (38 loc) • 1.5 kB
JavaScript
// packages/geo-block/src/providers/index.ts
import * as fs from 'fs';
import { MaxMindProvider } from './maxmind';
import { IpApiProvider } from './ip-api';
/**
* Create a geo lookup provider based on configuration
* @param config Geo-blocking configuration
*/
export function createProvider(config) {
// If using MaxMind, check if database exists
if (config.provider === 'maxmind') {
// Verify database exists when MaxMind is specified
if (!config.maxmindDbPath || !fs.existsSync(config.maxmindDbPath)) {
console.warn(`MaxMind database not found at path: ${config.maxmindDbPath || 'undefined'}. ` +
`Falling back to ip-api.com service.`);
// Fall back to ip-api service
return new IpApiProvider(config);
}
return new MaxMindProvider(config);
}
// Handle other providers
switch (config.provider) {
case 'ipapi':
return new IpApiProvider(config);
case 'custom':
if (!config.customLookup) {
throw new Error('Custom lookup function must be provided when using custom provider');
}
return {
init: async () => { },
lookup: config.customLookup
};
default:
// Default to ip-api if provider is unknown
console.warn(`Unknown provider: ${config.provider}. Falling back to ip-api.com service.`);
return new IpApiProvider(config);
}
}