-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: embed mmdb as pure js data (#782)
- Loading branch information
Showing
9 changed files
with
200 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as zlib from 'node:zlib'; | ||
import * as fs from 'node:fs'; | ||
|
||
const buf = fs.readFileSync('server/geoip/GeoLite2-Country.mmdb'); | ||
|
||
const compressed = zlib.brotliCompressSync(buf).toString('base64'); | ||
|
||
fs.writeFileSync( | ||
'server/geoip/data.ts', | ||
` | ||
import * as zlib from 'node:zlib'; | ||
// data is brotli compressed GeoLite2-Country.mmdb in base64 format | ||
export const data = zlib.brotliDecompressSync(Buffer.from('${compressed}', 'base64')); | ||
`, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GeoLite2-Country.mmdb |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
download `GeoLite2-Country.mmdb` from maxmind and put it here. | ||
|
||
Then run `node ./scripts/update-mmdb.mjs` to update `data.ts` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Reader} from '@maxmind/geoip2-node'; | ||
|
||
import * as data from '../geoip/data'; | ||
|
||
const r = Reader.openBuffer(data.data); | ||
|
||
export function lookup(s: string): string { | ||
try { | ||
return r.country(s)?.country?.isoCode ?? ''; | ||
} catch { | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters