1- import fetch from "node-fetch"
2- import type { RequestInit , Response } from "node-fetch"
3- import Cache from "./cache/cache"
4- import LruCache from "./cache/lruCache"
5- import ApiLimitError from "./errors/apiLimitError"
6- import { isInSubnet } from "subnet-check"
1+ import type { RequestInit , Response } from "node-fetch" ;
2+ import Cache from "./cache/cache" ;
3+ import LruCache from "./cache/lruCache" ;
4+ import ApiLimitError from "./errors/apiLimitError" ;
5+ import { isInSubnet } from "subnet-check" ;
76import {
87 REQUEST_TIMEOUT_DEFAULT ,
98 CACHE_VSN ,
109 HOST_RES_PROXY ,
1110 BOGON_NETWORKS ,
1211 IPinfoResProxy ,
1312 IPBogon
14- } from "./common"
15- import VERSION from "./version"
13+ } from "./common" ;
14+ import VERSION from "./version" ;
1615
17- const clientUserAgent = `IPinfoClient/nodejs/${ VERSION } `
16+ const clientUserAgent = `IPinfoClient/nodejs/${ VERSION } ` ;
1817
1918export default class IPinfoResProxyWrapper {
2019 private token : string ;
@@ -24,7 +23,7 @@ export default class IPinfoResProxyWrapper {
2423
2524 /**
2625 * Creates IPinfoResProxyWrapper object to communicate with the IPinfo Res Proxy API.
27- *
26+ *
2827 * @param token Token string provided by IPinfo for the registered user.
2928 * @param cache An implementation of IPCache interface, or LruCache if not specified.
3029 * @param timeout Request timeout in milliseconds, or 5000ms if not specified. 0 disables the timeout.
@@ -34,19 +33,19 @@ export default class IPinfoResProxyWrapper {
3433 token : string ,
3534 cache ?: Cache ,
3635 timeout ?: number ,
37- baseUrl ?: string ,
36+ baseUrl ?: string
3837 ) {
39- this . token = token
40- this . cache = cache || new LruCache ( )
38+ this . token = token ;
39+ this . cache = cache || new LruCache ( ) ;
4140 this . timeout =
4241 timeout === null || timeout === undefined
4342 ? REQUEST_TIMEOUT_DEFAULT
44- : timeout
45- this . baseUrl = baseUrl || `https://${ HOST_RES_PROXY } `
43+ : timeout ;
44+ this . baseUrl = baseUrl || `https://${ HOST_RES_PROXY } ` ;
4645 }
4746
4847 public static cacheKey ( k : string ) : string {
49- return `${ k } :${ CACHE_VSN } `
48+ return `${ k } :${ CACHE_VSN } ` ;
5049 }
5150
5251 public async fetchApi (
@@ -58,75 +57,72 @@ export default class IPinfoResProxyWrapper {
5857 Authorization : `Bearer ${ this . token } ` ,
5958 "Content-Type" : "application/json" ,
6059 "User-Agent" : clientUserAgent
61- }
60+ } ;
6261
6362 const request = Object . assign (
6463 {
6564 timeout : this . timeout ,
6665 method : "GET" ,
67- compress : false ,
66+ compress : false
6867 } ,
6968 init ,
70- { headers : Object . assign ( headers , init . headers ) } ,
71- )
69+ { headers : Object . assign ( headers , init . headers ) }
70+ ) ;
7271
7372 const url = [ this . baseUrl , path ] . join (
74- ! this . baseUrl . endsWith ( "/" ) && ! path . startsWith ( "/" )
75- ? "/"
76- : ""
77- )
73+ ! this . baseUrl . endsWith ( "/" ) && ! path . startsWith ( "/" ) ? "/" : ""
74+ ) ;
7875
7976 return fetch ( url , request ) . then ( ( response : Response ) => {
8077 if ( response . status === 429 ) {
81- throw new ApiLimitError ( )
78+ throw new ApiLimitError ( ) ;
8279 }
8380
8481 if ( response . status >= 400 ) {
85- throw new Error (
82+ throw new Error (
8683 `Received an error from the IPinfo API ` +
87- `(using authorization ${ headers [ "Authorization" ] } ) ` +
88- `${ response . status } ${ response . statusText } ${ response . url } `
89- )
84+ `(using authorization ${ headers [ "Authorization" ] } ) ` +
85+ `${ response . status } ${ response . statusText } ${ response . url } `
86+ ) ;
9087 }
9188
92- return response
93- } )
89+ return response ;
90+ } ) ;
9491 }
9592
9693 public async lookupIp (
9794 ip : string | undefined = undefined
9895 ) : Promise < IPinfoResProxy | IPBogon > {
9996 if ( ip && this . isBogon ( ip ) ) {
100- return { ip, bogon : true }
97+ return { ip, bogon : true } ;
10198 }
10299
103100 if ( ! ip ) {
104- ip = "me"
101+ ip = "me" ;
105102 }
106103
107- const data = await this . cache . get ( IPinfoResProxyWrapper . cacheKey ( ip ) )
104+ const data = await this . cache . get ( IPinfoResProxyWrapper . cacheKey ( ip ) ) ;
108105
109106 if ( data ) {
110- return data
107+ return data ;
111108 }
112109
113- return this . fetchApi ( ip )
114- . then ( async ( response ) => {
115- const ipinfo = ( await response . json ( ) ) as IPinfoResProxy
116- this . cache . set ( IPinfoResProxyWrapper . cacheKey ( ip ) , ipinfo )
110+ return this . fetchApi ( ip ) . then ( async ( response ) => {
111+ const ipinfo = ( await response . json ( ) ) as IPinfoResProxy ;
112+ this . cache . set ( IPinfoResProxyWrapper . cacheKey ( ip ) , ipinfo ) ;
117113
118- return ipinfo
119- } )
114+ return ipinfo ;
115+ } ) ;
120116 }
121117
122118 private isBogon ( ip : string ) : boolean {
123119 if ( ip != "" ) {
124120 for ( let network of BOGON_NETWORKS ) {
125121 if ( isInSubnet ( ip , network ) ) {
126- return true
122+ return true ;
127123 }
128124 }
129125 }
130- return false
126+ return false ;
131127 }
132- }
128+ }
0 commit comments