feat(geo): make geo a package instead of service (#161)

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-06-06 05:56:54 +02:00
committed by GitHub
parent f59bcfba3c
commit 34414e1d3e
24 changed files with 677 additions and 112 deletions

BIN
packages/GeoLite2-City.mmdb Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 MiB

1
packages/geo/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './src/geo';

20
packages/geo/package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "@openpanel/geo",
"version": "0.0.1",
"main": "index.ts",
"scripts": {
"typecheck": "tsc --noEmit",
"codegen": "jiti scripts/download.ts"
},
"dependencies": {
"@maxmind/geoip2-node": "^6.1.0"
},
"devDependencies": {
"@openpanel/tsconfig": "workspace:*",
"@types/node": "20.14.8",
"fast-extract": "^1.4.3",
"tar": "^7.4.3",
"typescript": "^5.2.2",
"jiti": "^2.4.1"
}
}

View File

@@ -0,0 +1,66 @@
import fs from 'node:fs';
import https from 'node:https';
import path from 'node:path';
import zlib from 'node:zlib';
import * as tar from 'tar';
import type { Parser } from 'tar';
const db = 'GeoLite2-City';
const download = async (url: string): Promise<Parser> => {
return new Promise((resolve) => {
https.get(url, (res) => {
const gunzip = zlib.createGunzip();
const parser = tar.t();
res.pipe(gunzip).pipe(parser as any);
resolve(parser);
});
});
};
async function main(): Promise<void> {
let url = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${db}.tar.gz`;
if (process.env.MAXMIND_LICENSE_KEY) {
url = [
'https://download.maxmind.com/app/geoip_download',
`?edition_id=${db}&license_key=${process.env.MAXMIND_LICENSE_KEY}&suffix=tar.gz`,
].join('');
}
const dest = path.resolve(__dirname, '../');
if (!fs.existsSync(dest)) {
console.log('Geo database not found');
process.exit(1);
}
try {
const res = await download(url);
await new Promise<void>((resolve, reject) => {
res.on('entry', (entry) => {
if (entry.path.endsWith('.mmdb')) {
const filename = path.join(dest, path.basename(entry.path));
entry.pipe(fs.createWriteStream(filename));
console.log('Saved geo database:', filename);
}
});
res.on('error', (e) => {
reject(e);
});
res.on('finish', () => {
resolve();
});
});
} catch (error) {
console.error('Error downloading geo database:', error);
process.exit(1);
}
}
main();

69
packages/geo/src/geo.ts Normal file
View File

@@ -0,0 +1,69 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import type { ReaderModel } from '@maxmind/geoip2-node';
import { Reader } from '@maxmind/geoip2-node';
const filename = 'GeoLite2-City.mmdb';
// From api or worker package
const dbPath = path.join(__dirname, `../../../packages/geo/${filename}`);
// From local package
const dbPathLocal = path.join(__dirname, `../${filename}`);
let reader: ReaderModel | null = null;
async function loadDatabase(dbPath: string) {
try {
const dbBuffer = await readFile(dbPath);
reader = Reader.openBuffer(dbBuffer);
console.log('GeoLite2-City.mmdb loaded (dist)');
} catch (error) {
try {
const dbBuffer = await readFile(dbPathLocal);
reader = Reader.openBuffer(dbBuffer);
console.log('GeoLite2-City.mmdb loaded (local)');
} catch (error) {
console.error('GeoLite2-City.mmdb not found');
}
}
}
export interface GeoLocation {
country: string | undefined;
city: string | undefined;
region: string | undefined;
longitude: number | undefined;
latitude: number | undefined;
}
const DEFAULT_GEO: GeoLocation = {
country: undefined,
city: undefined,
region: undefined,
longitude: undefined,
latitude: undefined,
};
const ignore = ['127.0.0.1', '::1'];
export async function getGeoLocation(ip?: string): Promise<GeoLocation> {
if (!ip || ignore.includes(ip)) {
return DEFAULT_GEO;
}
if (!reader) {
await loadDatabase(dbPath);
}
try {
const response = await reader?.city(ip);
return {
city: response?.city?.names.en,
country: response?.country?.isoCode,
region: response?.subdivisions?.[0]?.names.en,
longitude: response?.location?.longitude,
latitude: response?.location?.latitude,
};
} catch (error) {
return DEFAULT_GEO;
}
}

View File

@@ -0,0 +1,12 @@
{
"extends": "@openpanel/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
},
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": [".", "*.mmdb"],
"exclude": ["node_modules"]
}