• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / #13066

pending completion
#13066

push

circleci

Vinnl
<a href="https://github.com/mozilla/blurts-server/commit/<a class=hub.com/mozilla/blurts-server/commit/<a class="double-link" href="https://git"><a class=hub.com/mozilla/blurts-server/commit/<a class="double-link" href="https://git"><a class=hub.com/mozilla/blurts-server/commit/<a class="double-link" href="https://git"><a class=hub.com/mozilla/blurts-server/commit/<a class="double-link" href="https://git"><a class=hub.com/mozilla/blurts-server/commit/<a class="double-link" href="https://git"><a class=hub.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d">7a9d21547">&lt;a href=&quot;https://github.com/mozilla/blurts-server/commit/</a><a class="double-link" href="https://github.com/mozilla/blurts-server/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/mozilla/blurts-server/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/mozilla/blurts-server/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/mozilla/blurts-server/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d">7a9d21547</a><a href="https://github.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d">&lt;a href=&quot;https://github.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d&quot;&gt;&amp;quot;&amp;gt;&amp;amp;lt;a href=&amp;amp;quot;https://github.com/mozilla/blurts-server/commit/&amp;lt;/a&amp;gt;&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://github.com/mozilla/blurts-server/commit/&amp;amp;lt;a class=&amp;amp;quot;double-link&amp;amp;quot; href=&amp;amp;quot;https://git&amp;quot;&amp;gt;&amp;amp;lt;a class=&amp;lt;/a&amp;gt;hub.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d&amp;quot;&amp;gt;7a9d21547&amp;lt;/a&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d&amp;quot;&amp;gt;&amp;amp;lt;a href=&amp;amp;quot;https://github.com/mozilla/blurts-server/commit/7a9d215474a0334e43c421e3b8b509052c80054d&amp;amp;quot;&amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt;Revert &amp;amp;amp;amp;quot;fixup! Add a bunch of initial type annotatio&amp;lt;/a&amp;</a>gt;ns&amp;amp;quot;

This reverts commit &amp;lt;/a&amp;gt;&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;qu... (continued)

282 of 1621 branches covered (17.4%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 2 files covered. (100.0%)

959 of 4334 relevant lines covered (22.13%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/ip-location-service.js
1
'use strict'
2

3
const path = require('path')
×
4
const reader = require('@maxmind/geoip2-node').Reader
×
5
const AppConstants = require('./app-constants')
×
6

7
let locationDb, timestamp
8

9
async function openLocationDb () {
10
  if (locationDb && isFresh()) return console.warn('Location database already open.')
×
11

12
  try {
×
13
    const dbPath = path.join(AppConstants.GEOIP_GEOLITE2_PATH, AppConstants.GEOIP_GEOLITE2_CITY_FILENAME)
×
14
    locationDb = await reader.open(dbPath)
×
15
  } catch (e) {
16
    return console.warn('Could not open location database:', e.message)
×
17
  }
18

19
  timestamp = Date.now()
×
20
  return true
×
21
}
22

23
async function readLocationData (ip, locales) {
24
  let locationArr
25

26
  if (!isFresh()) await openLocationDb()
×
27

28
  try {
×
29
    const data = locationDb.city(ip)
×
30
    const countryName = data.country?.names[locales.find(locale => data.country?.names[locale])] // find valid locale key and return its value
×
31
    const cityName = data.city?.names[locales.find(locale => data.city?.names[locale])]
×
32
    const subdivisionName = data.subdivisions?.[0].isoCode
×
33
    const subdivisionFiltered = /[A-z]{2,}/.test(subdivisionName) ? subdivisionName : null // return strings that are 2 or more letters, or null (avoid unfamiliar subdivisions like `E` or `09`)
×
34

35
    locationArr = [cityName, subdivisionFiltered, countryName].filter(str => str) // [city name, state code, country code] with non-null items.
×
36
  } catch (e) {
37
    return console.warn('Could not read location from database:', e.message)
×
38
  }
39

40
  return {
×
41
    shortLocation: locationArr.slice(0, 2).join(', '), // shows the first two location values from the ones available
42
    fullLocation: locationArr.join(', ') // shows up to three location values from the ones available
43
  }
44
}
45

46
function isFresh () {
47
  if (Date.now() - timestamp < 259200000) return true // 1000ms * 60s * 60m * 24h * 3 elapsed time is less than 24hrs
×
48
  return false
×
49
}
50

51
module.exports = {
×
52
  openLocationDb,
53
  readLocationData
54
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc