Skip to content

Commit d71d0b2

Browse files
fix: get rid of undefined in probe location object
1 parent c560a7d commit d71d0b2

File tree

10 files changed

+20
-19
lines changed

10 files changed

+20
-19
lines changed

Diff for: src/lib/adopted-probes.ts

-4
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ export class AdoptedProbes {
200200
const adoptedValue = _.get(adoptedProbe, adoptedField) as string | number;
201201
const connectedValue = _.get(connectedProbe, connectedField) as string | number;
202202

203-
if (!adoptedValue && !connectedValue) { // undefined and null values are treated equal and don't require sync
204-
return;
205-
}
206-
207203
if (adoptedValue !== connectedValue) {
208204
updateObject[adoptedField] = connectedValue;
209205
}

Diff for: src/lib/geoip/client.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import type { ProbeLocation } from '../../probe/types.js';
77
import RedisCache from '../cache/redis-cache.js';
88
import { getRedisClient } from '../redis/client.js';
99
import { scopedLogger } from '../logger.js';
10-
import { getRegionByCountry } from '../location/location.js';
1110
import { isAddrWhitelisted } from './whitelist.js';
1211
import { ipinfoLookup } from './providers/ipinfo.js';
1312
import { fastlyLookup } from './providers/fastly.js';
1413
import { maxmindLookup } from './providers/maxmind.js';
1514
import { ipmapLookup } from './providers/ipmap.js';
1615
import { type Ip2LocationBundledResponse, ip2LocationLookup } from './providers/ip2location.js';
1716

18-
export type LocationInfo = Omit<ProbeLocation, 'region'>;
1917
type Provider = 'ipmap' | 'ip2location' | 'ipinfo' | 'maxmind' | 'fastly';
18+
export type LocationInfo = ProbeLocation & {isHosting: boolean | null};
2019
export type LocationInfoWithProvider = LocationInfo & {provider: Provider};
2120
export type NetworkInfo = {
2221
network: string;
@@ -31,7 +30,7 @@ export const createGeoipClient = (): GeoipClient => new GeoipClient(new RedisCac
3130
export default class GeoipClient {
3231
constructor (private readonly cache: CacheInterface) {}
3332

34-
async lookup (addr: string): Promise<ProbeLocation> {
33+
async lookup (addr: string): Promise<LocationInfo> {
3534
let isHosting = null;
3635
const results = await Promise
3736
.allSettled([
@@ -74,14 +73,12 @@ export default class GeoipClient {
7473
throw new ProbeError(`unresolvable geoip: ${addr}`);
7574
}
7675

77-
const region = getRegionByCountry(match.country);
78-
7976
return {
8077
continent: match.continent,
8178
country: match.country,
8279
state: match.state,
8380
city: match.city,
84-
region,
81+
region: match.region,
8582
normalizedCity: match.normalizedCity,
8683
asn: Number(networkMatch.asn),
8784
latitude: Number(match.latitude),

Diff for: src/lib/geoip/fake-client.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { ProbeLocation } from '../../probe/types.js';
1+
import type { LocationInfo } from './client.js';
22

3-
export const fakeLookup = (): ProbeLocation => {
3+
export const fakeLookup = (): LocationInfo => {
44
return {
55
continent: 'SA',
66
country: 'AR',
7-
state: undefined,
7+
state: null,
88
city: 'Buenos Aires',
99
region: 'South America',
1010
normalizedCity: 'buenos aires',
@@ -13,5 +13,6 @@ export const fakeLookup = (): ProbeLocation => {
1313
longitude: -58.3772,
1414
network: 'InterBS S.R.L. (BAEHOST)',
1515
normalizedNetwork: 'interbs s.r.l. (baehost)',
16+
isHosting: null,
1617
};
1718
};

Diff for: src/lib/geoip/providers/fastly.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import got from 'got';
2+
import { getRegionByCountry } from '../../location/location.js';
23
import { getCity } from '../city-approximation.js';
34
import type { LocationInfo } from '../client.js';
45
import {
@@ -38,6 +39,7 @@ export const fastlyLookup = async (addr: string): Promise<LocationInfo> => {
3839

3940
return {
4041
continent: data.continent_code,
42+
region: getRegionByCountry(data.country_code),
4143
country: data.country_code,
4244
state: data.country_code === 'US' ? data.region : null,
4345
city: normalizeCityNamePublic(city),

Diff for: src/lib/geoip/providers/ip2location.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import got from 'got';
22
import config from 'config';
33
import {
44
getContinentByCountry,
5+
getRegionByCountry,
56
getStateIsoByName,
67
} from '../../location/location.js';
78
import type { LocationInfo } from '../client.js';
@@ -45,6 +46,7 @@ export const ip2LocationLookup = async (addr: string): Promise<Ip2LocationBundle
4546

4647
const location = {
4748
continent: result.country_code ? getContinentByCountry(result.country_code) : '',
49+
region: result.country_code ? getRegionByCountry(result.country_code) : '',
4850
state: result.country_code === 'US' && result.region_name ? getStateIsoByName(result.region_name) : null,
4951
country: result.country_code ?? '',
5052
city: normalizeCityNamePublic(city),

Diff for: src/lib/geoip/providers/ipinfo.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import got from 'got';
22
import config from 'config';
3-
import { getContinentByCountry, getStateIsoByName } from '../../location/location.js';
3+
import { getContinentByCountry, getRegionByCountry, getStateIsoByName } from '../../location/location.js';
44
import type { LocationInfo } from '../client.js';
55
import {
66
normalizeCityName,
@@ -34,6 +34,7 @@ export const ipinfoLookup = async (addr: string): Promise<LocationInfo> => {
3434

3535
return {
3636
continent: result.country ? getContinentByCountry(result.country) : '',
37+
region: result.country ? getRegionByCountry(result.country) : '',
3738
state: result.country === 'US' && result.region ? getStateIsoByName(result.region) : null,
3839
country: result.country ?? '',
3940
city: normalizeCityNamePublic(city),

Diff for: src/lib/geoip/providers/ipmap.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import got from 'got';
2-
import { getContinentByCountry } from '../../location/location.js';
2+
import { getContinentByCountry, getRegionByCountry } from '../../location/location.js';
33
import type { LocationInfo } from '../client.js';
44
import {
55
normalizeCityName,
@@ -24,6 +24,7 @@ export const ipmapLookup = async (addr: string): Promise<LocationInfo> => {
2424

2525
return {
2626
continent: location.countryCodeAlpha2 ? getContinentByCountry(location.countryCodeAlpha2) : '',
27+
region: location.countryCodeAlpha2 ? getRegionByCountry(location.countryCodeAlpha2) : '',
2728
state: location.countryCodeAlpha2 === 'US' && location.stateAnsiCode ? location.stateAnsiCode : null,
2829
country: location.countryCodeAlpha2 ?? '',
2930
city: normalizeCityNamePublic(city),

Diff for: src/lib/geoip/providers/maxmind.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
normalizeNetworkName,
1010
} from '../utils.js';
1111
import { getCity } from '../city-approximation.js';
12+
import { getRegionByCountry } from '../../location/location.js';
1213

1314
const client = new WebServiceClient(config.get('maxmind.accountId'), config.get('maxmind.licenseKey'));
1415

@@ -39,6 +40,7 @@ export const maxmindLookup = async (addr: string): Promise<LocationInfo> => {
3940

4041
return {
4142
continent: data.continent?.code ?? '',
43+
region: data.country?.isoCode ? getRegionByCountry(data.country?.isoCode) : '',
4244
country: data.country?.isoCode ?? '',
4345
state: data.country?.isoCode === 'US' ? data.subdivisions?.map(s => s.isoCode)[0] ?? '' : null,
4446
city: normalizeCityNamePublic(city),

Diff for: src/probe/builder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getRegionAliases,
1313
} from '../lib/location/location.js';
1414
import { ProbeError } from '../lib/probe-error.js';
15-
import { createGeoipClient } from '../lib/geoip/client.js';
15+
import { createGeoipClient, LocationInfo } from '../lib/geoip/client.js';
1616
import type GeoipClient from '../lib/geoip/client.js';
1717
import getProbeIp from '../lib/get-probe-ip.js';
1818
import { getRegion } from '../lib/ip-ranges.js';
@@ -118,7 +118,7 @@ export const getIndex = (location: ProbeLocation, tags: Tag[]) => {
118118
return index;
119119
};
120120

121-
const getLocation = (ipInfo: ProbeLocation): ProbeLocation => ({
121+
const getLocation = (ipInfo: LocationInfo): ProbeLocation => ({
122122
continent: ipInfo.continent,
123123
region: ipInfo.region,
124124
country: ipInfo.country,
@@ -132,7 +132,7 @@ const getLocation = (ipInfo: ProbeLocation): ProbeLocation => ({
132132
normalizedNetwork: ipInfo.normalizedNetwork,
133133
});
134134

135-
const getTags = (clientIp: string, ipInfo: ProbeLocation) => {
135+
const getTags = (clientIp: string, ipInfo: LocationInfo) => {
136136
const tags: Tag[] = [];
137137
const cloudRegion = getRegion(clientIp);
138138

Diff for: src/probe/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export type ProbeLocation = {
1010
state: string | null;
1111
network: string;
1212
normalizedNetwork: string;
13-
isHosting: boolean | null;
1413
};
1514

1615
export type ProbeStats = {

0 commit comments

Comments
 (0)