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

mozilla / blurts-server / #12377

pending completion
#12377

push

circleci

mozilla-pontoon
Pontoon: Update Spanish (Argentina) (es-AR) localization of Firefox Monitor Website

Co-authored-by: Marcelo Poli <enzomatrix@gmail.com>

282 of 1374 branches covered (20.52%)

Branch coverage included in aggregate %.

959 of 3725 relevant lines covered (25.74%)

2.11 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