fix(dashboard): show enriched network details
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { groupMapLocations, parseUserLocation, projectWorldPoint } from "./user-location-map";
|
||||
import { groupMapLocations, parseUserLocation, parseUserNetwork, projectWorldPoint } from "./user-location-map";
|
||||
|
||||
describe("user location map", () => {
|
||||
it("extracts a valid approximate location from cached IP intelligence", () => {
|
||||
@@ -19,6 +19,29 @@ describe("user location map", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("extracts enriched network fields from existing ProxyCheck cache entries", () => {
|
||||
expect(parseUserNetwork({
|
||||
network: { asn: "AS7922", provider: "Comcast Cable Communications, LLC" },
|
||||
rawResponse: {
|
||||
status: "ok",
|
||||
"203.0.113.10": { type: "Residential", proxy: "no" },
|
||||
},
|
||||
})).toEqual({
|
||||
asn: "AS7922",
|
||||
provider: "Comcast Cable Communications, LLC",
|
||||
connectionType: "Residential",
|
||||
proxy: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("prefers normalized network fields and preserves unavailable values", () => {
|
||||
expect(parseUserNetwork({
|
||||
network: { asn: "AS62371", provider: "Proton AG", connectionType: "VPN", proxy: true },
|
||||
rawResponse: { "198.51.100.5": { type: "Residential", proxy: "no" } },
|
||||
})).toEqual({ asn: "AS62371", provider: "Proton AG", connectionType: "VPN", proxy: true });
|
||||
expect(parseUserNetwork({ network: {} })).toEqual({ asn: null, provider: null, connectionType: null, proxy: null });
|
||||
});
|
||||
|
||||
it("rejects missing and out-of-range coordinates", () => {
|
||||
expect(parseUserLocation({ location: { latitude: 91, longitude: 0 } })).toBeNull();
|
||||
expect(parseUserLocation({ location: { city: "Unknown" } })).toBeNull();
|
||||
|
||||
@@ -6,6 +6,17 @@ function objectValue(value: unknown): UnknownMap | null {
|
||||
: null;
|
||||
}
|
||||
|
||||
function stringValue(value: unknown) {
|
||||
return typeof value === "string" && value.trim() ? value.trim() : null;
|
||||
}
|
||||
|
||||
function proxyValue(value: unknown) {
|
||||
if (typeof value === "boolean") return value;
|
||||
if (typeof value === "string" && value.toLowerCase() === "yes") return true;
|
||||
if (typeof value === "string" && value.toLowerCase() === "no") return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
function coordinate(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim() && Number.isFinite(Number(value))) return Number(value);
|
||||
@@ -18,6 +29,28 @@ export interface ParsedUserLocation {
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface ParsedUserNetwork {
|
||||
asn: string | null;
|
||||
provider: string | null;
|
||||
connectionType: string | null;
|
||||
proxy: boolean | null;
|
||||
}
|
||||
|
||||
export function parseUserNetwork(value: unknown): ParsedUserNetwork {
|
||||
const intelligence = objectValue(value);
|
||||
const network = objectValue(intelligence?.network);
|
||||
const providerResponse = objectValue(intelligence?.rawResponse);
|
||||
const legacyDetails = Object.values(providerResponse ?? {})
|
||||
.map(objectValue)
|
||||
.find((details) => details && ("type" in details || "proxy" in details));
|
||||
return {
|
||||
asn: stringValue(network?.asn),
|
||||
provider: stringValue(network?.provider),
|
||||
connectionType: stringValue(network?.connectionType) ?? stringValue(legacyDetails?.type),
|
||||
proxy: proxyValue(network?.proxy) ?? proxyValue(legacyDetails?.proxy),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseUserLocation(value: unknown): ParsedUserLocation | null {
|
||||
const intelligence = objectValue(value);
|
||||
const location = objectValue(intelligence?.location);
|
||||
|
||||
Reference in New Issue
Block a user