Skip to content

Commit

Permalink
lib/maxmind
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-ilin committed Jun 9, 2024
1 parent 370a17d commit 6407d26
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/on_commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: CI

on:
workflow_dispatch:

pull_request:
push:
branches: [main]

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: 🛎️ Checkout
uses: actions/checkout@v4

- name: ⚙️ Setup Bun
uses: oven-sh/setup-bun@v1

- name: 📦 Build
run: |
bun install
make fmt test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
lib/**/*.mmdb
lib/**/*.d.*ts
lib/**/index.mjs
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY : all test
all :

.PHONY : lib/maxmind
lib/maxmind :
@ $(MAKE) --directory=lib/maxmind

.PHONY : fmt
fmt :
bun x prettier --write .
@ git diff-index --quiet HEAD

.PHONY : test
test :
@ $(MAKE) --directory=lib/maxmind test

.PHONY : distclean
distclean :
-rm -r node_modules
-@ $(MAKE) --directory=lib/maxmind clean
4 changes: 3 additions & 1 deletion assets/fonts/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
font-family: 'Pacifico';
font-style: normal;
font-weight: normal;
src: local('Pacifico Regular'), url('Pacifico-Regular.woff') format('woff');
src:
local('Pacifico Regular'),
url('Pacifico-Regular.woff') format('woff');
}
16 changes: 16 additions & 0 deletions lib/maxmind/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY : all test clean
all : test/GeoLite2-Country-Test.mmdb index.mjs

index.mjs : index.mts
../../node_modules/.bin/tsc --pretty
../../node_modules/.bin/webpack

test/GeoLite2-Country-Test.mmdb :
cd test && wget 'https://cdn.jsdelivr.net/gh/maxmind/MaxMind-DB/test-data/GeoLite2-Country-Test.mmdb'

test :
cd test && ../../../node_modules/.bin/tsc
cd test && bun test

clean :
- rm test/*.mmdb *.d.mts index.*js
8 changes: 8 additions & 0 deletions lib/maxmind/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { CountryResponse } from 'mmdb-lib';
import { Reader } from 'mmdb-lib';

export async function init(response: Pick<Response, 'arrayBuffer'>) {
const db = Buffer.from(await response.arrayBuffer());

return new Reader<CountryResponse>(db);
}
210 changes: 210 additions & 0 deletions lib/maxmind/test/maxmind.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import { describe, expect, it } from 'bun:test';
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

import * as maxmind from '../index.mjs';

describe('lib / maxmind', () => {
const arrayBuffer = fs
.readFile(
fileURLToPath(import.meta.resolve('./GeoLite2-Country-Test.mmdb')),
)
.then(({ buffer }) => buffer as ArrayBuffer);
const mockDatabase = { arrayBuffer: () => arrayBuffer };

it('should resolve a country for IPv4', async () => {
const reader = await maxmind.init(mockDatabase);

expect(reader.get('89.160.20.122')).toStrictEqual({
continent: {
code: 'EU',
geoname_id: 6255148,
names: {
de: 'Europa',
en: 'Europe',
es: 'Europa',
fr: 'Europe',
ja: 'ヨーロッパ',
'pt-BR': 'Europa',
ru: 'Европа',
'zh-CN': '欧洲',
},
},
country: {
geoname_id: 2661886,
is_in_european_union: true,
iso_code: 'SE',
names: {
de: 'Schweden',
en: 'Sweden',
es: 'Suecia',
fr: 'Suède',
ja: 'スウェーデン王国',
'pt-BR': 'Suécia',
ru: 'Швеция',
'zh-CN': '瑞典',
},
},
registered_country: {
geoname_id: 2921044,
is_in_european_union: true,
iso_code: 'DE',
names: {
de: 'Deutschland',
en: 'Germany',
es: 'Alemania',
fr: 'Allemagne',
ja: 'ドイツ連邦共和国',
'pt-BR': 'Alemanha',
ru: 'Германия',
'zh-CN': '德国',
},
},
});

expect(reader.get('67.43.156.156')).toStrictEqual({
continent: {
code: 'AS',
geoname_id: 6255147,
names: {
de: 'Asien',
en: 'Asia',
es: 'Asia',
fr: 'Asie',
ja: 'アジア',
'pt-BR': 'Ásia',
ru: 'Азия',
'zh-CN': '亚洲',
},
},
country: {
geoname_id: 1252634,
iso_code: 'BT',
names: {
de: 'Bhutan',
en: 'Bhutan',
es: 'Bután',
fr: 'Bhutan',
ja: 'ブータン王国',
'pt-BR': 'Butão',
ru: 'Бутан',
'zh-CN': '不丹',
},
},
registered_country: {
geoname_id: 798549,
is_in_european_union: true,
iso_code: 'RO',
names: {
de: 'Rumänien',
en: 'Romania',
es: 'Rumanía',
fr: 'Roumanie',
ja: 'ルーマニア',
'pt-BR': 'Romênia',
ru: 'Румыния',
'zh-CN': '罗马尼亚',
},
},
traits: {
is_anonymous_proxy: true,
},
});
});

it('should resolve a country for IPv6', async () => {
const reader = await maxmind.init(mockDatabase);

expect(reader.get('2a02:fc40::1')).toStrictEqual({
continent: {
code: 'EU',
geoname_id: 6255148,
names: {
de: 'Europa',
en: 'Europe',
es: 'Europa',
fr: 'Europe',
ja: 'ヨーロッパ',
'pt-BR': 'Europa',
ru: 'Европа',
'zh-CN': '欧洲',
},
},
country: {
geoname_id: 2623032,
is_in_european_union: true,
iso_code: 'DK',
names: {
de: 'Dänemark',
en: 'Denmark',
es: 'Dinamarca',
fr: 'Danemark',
ja: 'デンマーク王国',
'pt-BR': 'Dinamarca',
ru: 'Дания',
'zh-CN': '丹麦',
},
},
registered_country: {
geoname_id: 2623032,
is_in_european_union: true,
iso_code: 'DK',
names: {
de: 'Dänemark',
en: 'Denmark',
es: 'Dinamarca',
fr: 'Danemark',
ja: 'デンマーク王国',
'pt-BR': 'Dinamarca',
ru: 'Дания',
'zh-CN': '丹麦',
},
},
});

expect(reader.get('2a02:d300::1')).toStrictEqual({
continent: {
code: 'EU',
geoname_id: 6255148,
names: {
de: 'Europa',
en: 'Europe',
es: 'Europa',
fr: 'Europe',
ja: 'ヨーロッパ',
'pt-BR': 'Europa',
ru: 'Европа',
'zh-CN': '欧洲',
},
},
country: {
geoname_id: 690791,
iso_code: 'UA',
names: {
de: 'Ukraine',
en: 'Ukraine',
es: 'Ucrania',
fr: 'Ukraine',
ja: 'ウクライナ共和国',
'pt-BR': 'Ucrânia',
ru: 'Украина',
'zh-CN': '乌克兰',
},
},
registered_country: {
geoname_id: 690791,
iso_code: 'UA',
names: {
de: 'Ukraine',
en: 'Ukraine',
es: 'Ucrania',
fr: 'Ukraine',
ja: 'ウクライナ共和国',
'pt-BR': 'Ucrânia',
ru: 'Украина',
'zh-CN': '乌克兰',
},
},
});
});
});
15 changes: 15 additions & 0 deletions lib/maxmind/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",

"include": ["*.ts"],

"compilerOptions": {
"target": "ESNext",
"module": "Preserve",
"skipLibCheck": true,
"noEmit": true,

"strict": true,
"forceConsistentCasingInFileNames": true
}
}
15 changes: 15 additions & 0 deletions lib/maxmind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",

"include": ["*.mts"],

"compilerOptions": {
"target": "ESNext",
"module": "Preserve",
"types": [],
"declaration": true,

"strict": true,
"forceConsistentCasingInFileNames": true
}
}
28 changes: 28 additions & 0 deletions lib/maxmind/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { fileURLToPath } from 'node:url';

import webpack from 'webpack';

/** @type webpack.Configuration */
const config = {
mode: 'none',
experiments: { outputModule: true },

entry: { index: fileURLToPath(import.meta.resolve('./index.mjs')) },
output: {
path: fileURLToPath(import.meta.resolve('.')),
libraryTarget: 'module',
},

resolve: {
fallback: {
net: false,
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
],
};

export default config;
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"private": true,
"name": "ctf",
"version": "0.1.0",
"author": "nilfalse.com",
"license": "MPL-2.0",
"type": "module",
"scripts": {
"postinstall": "make lib/maxmind"
},
"dependencies": {
"buffer": "^6.0.3",
"mmdb-lib": "^2.1.1"
},
"devDependencies": {
"@types/bun": "latest",
"prettier": "^3.3.1",
"webpack-cli": "^5.1.4"
},
"peerDependencies": {
"typescript": "*"
},
"prettier": {
"singleQuote": true
}
}
1 change: 1 addition & 0 deletions src/service_worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import * as maxmind from '../lib/maxmind/index.mjs';
Loading

0 comments on commit 6407d26

Please sign in to comment.