Skip to main content
Deno 2 is finally here 🎉️
Learn more

maxminddb Deno

A library that enables the usage of MaxmindDB geoIP databases by using the Rust library in a WebAssembly module

version Test Status Build Status Deno Page API doc

Built with 🦀🕸 by The Rust and WebAssembly Working Group

About

Uses the Rust MaxmindDB library and stripping of all filesystem dependency to create a WASM binary that gets embedded (there’s currently some issues around how to handle loading WASM in Deno libraries, so it’s inlined as a Uint8Array) to let you pass in the database yourself in js/ts and make it queryable.

Unfortunately, there’s not currently a good way to auto-generate the type information for the exported object, so you’ll have to refer to the example or now, though I will be working on getting that sorted out.

🚴 Usage

Instantiate a new MaxmindDB database by passing it as a raw Uint8Array

import { Maxmind } from "./mod.ts";
const dbRawFile = await Deno.readFile('./GeoLite2-City.mmdb')
const db = new Maxmind(dbRawFile)
const result = db.lookup_city('8.8.8.8')
result = {{
  city: {
    geoname_id: 4752186,
    names: Map {
      "en" => "Chesapeake",
      "ja" => "チェサピーク",
      "pt-BR" => "Chesapeake",
      "ru" => "Чесапик",
      "zh-CN" => "切萨皮克"
    }
  },
  continent: {
    code: "NA",
    geoname_id: 6255149,
    names: Map {
      "de" => "Nordamerika",
      "en" => "North America",
      "es" => "Norteamérica",
      "fr" => "Amérique du Nord",
      "ja" => "北アメリカ",
      "pt-BR" => "América do Norte",
      "ru" => "Северная Америка",
      "zh-CN" => "北美洲"
    }
  },
  country: {
    geoname_id: 6252001,
    is_in_european_union: undefined,
    iso_code: "US",
    names: Map {
      "de" => "USA",
      "en" => "United States",
      "es" => "Estados Unidos",
      "fr" => "États-Unis",
      "ja" => "アメリカ合衆国",
      "pt-BR" => "Estados Unidos",
      "ru" => "США",
      "zh-CN" => "美国"
    }
  },
  location: {
    latitude: 36.7348,
    longitude: -76.2343,
    metro_code: 544,
    time_zone: "America/New_York"
  },
  postal: { code: "23320" },
  registered_country: {
    geoname_id: 6252001,
    is_in_european_union: undefined,
    iso_code: "US",
    names: Map {
      "de" => "USA",
      "en" => "United States",
      "es" => "Estados Unidos",
      "fr" => "États-Unis",
      "ja" => "アメリカ合衆国",
      "pt-BR" => "Estados Unidos",
      "ru" => "США",
      "zh-CN" => "美国"
    }
  },
  represented_country: undefined,
  subdivisions: [
    {
      geoname_id: 6254928,
      iso_code: "VA",
      names: Map {
        "en" => "Virginia",
        "fr" => "Virginie",
        "ja" => "バージニア州",
        "pt-BR" => "Virgínia",
        "ru" => "Вирджиния",
        "zh-CN" => "弗吉尼亚州"
      }
    }
  ],
  traits: undefined
}}

Contributing

Build Setup

For running the automated build (which includes compiling the rust wasm) you’ll need the following tools installed

Once you have all the necessary tools installed, you can just run deno run --allow-run --allow-read --allow-write build.ts

It builds the wasm and interfacing javascript and typescript definitions, does some transformation on the javascript to support Deno, and writes it to the dist folder.

Testing

Under test/test.ts, the single non-comprehensive test downloads the available GeoLite2 test database from the Maxmind github repo, and uses that to test that basic functionality works. Since it fetches the test database over the network every run, it is a little slower (Though the test database is pretty small).