@@ -4,6 +4,11 @@ import { numberToCountryCode } from './functions/numberToCountryCode.js'
44import { numberToDir } from './functions/numberToDir.js'
55import { parseIp } from './functions/parseIp.js'
66
7+ /**
8+ * Sets up an IP lookup function based on the specified data type.
9+ * @template T - The type of data to return ('country' or 'geocode')
10+ * @returns A function that takes an IP address and returns location data
11+ */
712export function setup < T extends 'country' | 'geocode' > ( ) : ( ipInput : string ) => Promise < T extends 'country' ? { country : string } | null : { latitude : number , longitude : number , country : string } | null > {
813 const CDN_URL = __CDN_URL__
914 const MAIN_RECORD_SIZE = __DATA_TYPE__ === 'country' ? 2 : 8
@@ -16,12 +21,20 @@ export function setup<T extends 'country' | 'geocode'>(): (ipInput: string) => P
1621 6 : CDN_URL ,
1722 }
1823
24+ /**
25+ * Loads the index for the specified IP version.
26+ * @param ipVersion - The IP version (4 or 6)
27+ * @returns A promise that resolves to the loaded index
28+ */
1929 async function loadIndex ( ipVersion : 4 | 6 ) {
20- //* Determine the base URL for downloading the index
2130 const baseUrl = getBaseUrl ( )
2231 return downloadIndex ( baseUrl , ipVersion )
2332 }
2433
34+ /**
35+ * Determines the base URL for downloading the index.
36+ * @returns The base URL as a string
37+ */
2538 function getBaseUrl ( ) : string {
2639 //* If we are not in the DOM we just use the CDN_URL to download the index
2740 if ( typeof document === 'undefined' || ! document . currentScript ) {
@@ -37,6 +50,12 @@ export function setup<T extends 'country' | 'geocode'>(): (ipInput: string) => P
3750 return document . currentScript . src . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' )
3851 }
3952
53+ /**
54+ * Downloads the index file for the specified IP version.
55+ * @param baseUrl - The base URL for downloading
56+ * @param version - The IP version (4 or 6)
57+ * @returns A promise that resolves to the downloaded index
58+ */
4059 async function downloadIndex ( baseUrl : string , version : 4 | 6 ) {
4160 const result = await fetchArrayBuffer (
4261 new URL ( `indexes/${ version } .idx` , baseUrl ) ,
@@ -50,6 +69,11 @@ export function setup<T extends 'country' | 'geocode'>(): (ipInput: string) => P
5069 return ( INDEXES [ version ] = new BigUint64Array ( buffer ) )
5170 }
5271
72+ /**
73+ * Performs an IP lookup and returns location data.
74+ * @param ipInput - The IP address to look up
75+ * @returns A promise that resolves to location data or null if not found
76+ */
5377 return async function IpLookup ( ipInput : string ) {
5478 const { version, ip } = parseIp ( ipInput )
5579
@@ -100,6 +124,15 @@ export function setup<T extends 'country' | 'geocode'>(): (ipInput: string) => P
100124 return null
101125 } as ( ipInput : string ) => Promise < T extends 'country' ? { country : string } | null : { latitude : number , longitude : number , country : string } | null >
102126
127+ /**
128+ * Retrieves the end IP for a given record.
129+ * @param dataBuffer - The buffer containing IP data
130+ * @param ipVersion - The IP version (4 or 6)
131+ * @param recordCount - The total number of records
132+ * @param recordIndex - The index of the current record
133+ * @param ipSize - The size of an IP address in bytes
134+ * @returns The end IP as a number
135+ */
103136 function getEndIp ( dataBuffer : ArrayBuffer , ipVersion : 4 | 6 , recordCount : number , recordIndex : number , ipSize : number ) {
104137 const endIpBuffer = dataBuffer . slice (
105138 ( recordCount + recordIndex ) * ipSize ,
@@ -110,6 +143,15 @@ export function setup<T extends 'country' | 'geocode'>(): (ipInput: string) => P
110143 : new BigUint64Array ( endIpBuffer ) [ 0 ] !
111144 }
112145
146+ /**
147+ * Parses a record and returns location data.
148+ * @param dataBuffer - The buffer containing record data
149+ * @param recordCount - The total number of records
150+ * @param recordIndex - The index of the current record
151+ * @param ipSize - The size of an IP address in bytes
152+ * @param MAIN_RECORD_SIZE - The size of the main record data
153+ * @returns Location data object
154+ */
113155 function parseRecord ( dataBuffer : ArrayBuffer , recordCount : number , recordIndex : number , ipSize : number , MAIN_RECORD_SIZE : number ) {
114156 const recordBuffer = dataBuffer . slice (
115157 recordCount * ipSize * 2 + recordIndex * MAIN_RECORD_SIZE ,
0 commit comments