@@ -4,125 +4,125 @@ import LruCache from "./cache/lruCache";
44import ApiLimitError from "./errors/apiLimitError" ;
55import { isInSubnet } from "subnet-check" ;
66import {
7- REQUEST_TIMEOUT_DEFAULT ,
8- CACHE_VSN ,
9- HOST_RES_PROXY ,
10- BOGON_NETWORKS ,
11- IPinfoResProxy ,
12- IPBogon
7+ REQUEST_TIMEOUT_DEFAULT ,
8+ CACHE_VSN ,
9+ HOST_RES_PROXY ,
10+ BOGON_NETWORKS ,
11+ IPinfoResProxy ,
12+ IPBogon
1313} from "./common" ;
1414import VERSION from "./version" ;
1515
1616const clientUserAgent = `IPinfoClient/nodejs/${ VERSION } ` ;
1717
1818export default class IPinfoResProxyWrapper {
19- private token : string ;
20- private baseUrl : string ;
21- private cache : Cache ;
22- private timeout : number ;
23-
24- /**
25- * Creates IPinfoResProxyWrapper object to communicate with the IPinfo Res Proxy API.
26- *
27- * @param token Token string provided by IPinfo for the registered user.
28- * @param cache An implementation of IPCache interface, or LruCache if not specified.
29- * @param timeout Request timeout in milliseconds, or 5000ms if not specified. 0 disables the timeout.
30- * @param baseUrl The base url to use for api requests, or "ipinfo.io" if not specified.
31- */
32- constructor (
33- token : string ,
34- cache ?: Cache ,
35- timeout ?: number ,
36- baseUrl ?: string
37- ) {
38- this . token = token ;
39- this . cache = cache || new LruCache ( ) ;
40- this . timeout =
41- timeout === null || timeout === undefined
42- ? REQUEST_TIMEOUT_DEFAULT
43- : timeout ;
44- this . baseUrl = baseUrl || `https://${ HOST_RES_PROXY } ` ;
45- }
46-
47- public static cacheKey ( k : string ) : string {
48- return `${ k } :${ CACHE_VSN } ` ;
49- }
50-
51- public async fetchApi (
52- path : string ,
53- init : RequestInit = { }
54- ) : Promise < Response > {
55- const headers = {
56- Accept : "application/json" ,
57- Authorization : `Bearer ${ this . token } ` ,
58- "Content-Type" : "application/json" ,
59- "User-Agent" : clientUserAgent
60- } ;
61-
62- const request = Object . assign (
63- {
64- timeout : this . timeout ,
65- method : "GET" ,
66- compress : false
67- } ,
68- init ,
69- { headers : Object . assign ( headers , init . headers ) }
70- ) ;
71-
72- const url = [ this . baseUrl , path ] . join (
73- ! this . baseUrl . endsWith ( "/" ) && ! path . startsWith ( "/" ) ? "/" : ""
74- ) ;
75-
76- return fetch ( url , request ) . then ( ( response : Response ) => {
77- if ( response . status === 429 ) {
78- throw new ApiLimitError ( ) ;
79- }
80-
81- if ( response . status >= 400 ) {
82- throw new Error (
83- `Received an error from the IPinfo API ` +
84- `(using authorization ${ headers [ "Authorization" ] } ) ` +
85- `${ response . status } ${ response . statusText } ${ response . url } `
19+ private token : string ;
20+ private baseUrl : string ;
21+ private cache : Cache ;
22+ private timeout : number ;
23+
24+ /**
25+ * Creates IPinfoResProxyWrapper object to communicate with the IPinfo Res Proxy API.
26+ *
27+ * @param token Token string provided by IPinfo for the registered user.
28+ * @param cache An implementation of IPCache interface, or LruCache if not specified.
29+ * @param timeout Request timeout in milliseconds, or 5000ms if not specified. 0 disables the timeout.
30+ * @param baseUrl The base url to use for api requests, or "ipinfo.io" if not specified.
31+ */
32+ constructor (
33+ token : string ,
34+ cache ?: Cache ,
35+ timeout ?: number ,
36+ baseUrl ?: string
37+ ) {
38+ this . token = token ;
39+ this . cache = cache || new LruCache ( ) ;
40+ this . timeout =
41+ timeout === null || timeout === undefined
42+ ? REQUEST_TIMEOUT_DEFAULT
43+ : timeout ;
44+ this . baseUrl = baseUrl || `https://${ HOST_RES_PROXY } ` ;
45+ }
46+
47+ public static cacheKey ( k : string ) : string {
48+ return `${ k } :${ CACHE_VSN } ` ;
49+ }
50+
51+ public async fetchApi (
52+ path : string ,
53+ init : RequestInit = { }
54+ ) : Promise < Response > {
55+ const headers = {
56+ Accept : "application/json" ,
57+ Authorization : `Bearer ${ this . token } ` ,
58+ "Content-Type" : "application/json" ,
59+ "User-Agent" : clientUserAgent
60+ } ;
61+
62+ const request = Object . assign (
63+ {
64+ timeout : this . timeout ,
65+ method : "GET" ,
66+ compress : false
67+ } ,
68+ init ,
69+ { headers : Object . assign ( headers , init . headers ) }
8670 ) ;
87- }
8871
89- return response ;
90- } ) ;
91- }
72+ const url = [ this . baseUrl , path ] . join (
73+ ! this . baseUrl . endsWith ( "/" ) && ! path . startsWith ( "/" ) ? "/" : ""
74+ ) ;
9275
93- public async lookupIp (
94- ip : string | undefined = undefined
95- ) : Promise < IPinfoResProxy | IPBogon > {
96- if ( ip && this . isBogon ( ip ) ) {
97- return { ip, bogon : true } ;
76+ return fetch ( url , request ) . then ( ( response : Response ) => {
77+ if ( response . status === 429 ) {
78+ throw new ApiLimitError ( ) ;
79+ }
80+
81+ if ( response . status >= 400 ) {
82+ throw new Error (
83+ `Received an error from the IPinfo API ` +
84+ `(using authorization ${ headers [ "Authorization" ] } ) ` +
85+ `${ response . status } ${ response . statusText } ${ response . url } `
86+ ) ;
87+ }
88+
89+ return response ;
90+ } ) ;
9891 }
9992
100- if ( ! ip ) {
101- ip = "me" ;
102- }
93+ public async lookupIp (
94+ ip : string | undefined = undefined
95+ ) : Promise < IPinfoResProxy | IPBogon > {
96+ if ( ip && this . isBogon ( ip ) ) {
97+ return { ip, bogon : true } ;
98+ }
10399
104- const data = await this . cache . get ( IPinfoResProxyWrapper . cacheKey ( ip ) ) ;
100+ if ( ! ip ) {
101+ ip = "me" ;
102+ }
105103
106- if ( data ) {
107- return data ;
108- }
104+ const data = await this . cache . get ( IPinfoResProxyWrapper . cacheKey ( ip ) ) ;
109105
110- return this . fetchApi ( ip ) . then ( async ( response ) => {
111- const ipinfo = ( await response . json ( ) ) as IPinfoResProxy ;
112- this . cache . set ( IPinfoResProxyWrapper . cacheKey ( ip ) , ipinfo ) ;
106+ if ( data ) {
107+ return data ;
108+ }
109+
110+ return this . fetchApi ( ip ) . then ( async ( response ) => {
111+ const ipinfo = ( await response . json ( ) ) as IPinfoResProxy ;
112+ this . cache . set ( IPinfoResProxyWrapper . cacheKey ( ip ) , ipinfo ) ;
113113
114- return ipinfo ;
115- } ) ;
116- }
114+ return ipinfo ;
115+ } ) ;
116+ }
117117
118- private isBogon ( ip : string ) : boolean {
119- if ( ip != "" ) {
120- for ( let network of BOGON_NETWORKS ) {
121- if ( isInSubnet ( ip , network ) ) {
122- return true ;
118+ private isBogon ( ip : string ) : boolean {
119+ if ( ip != "" ) {
120+ for ( let network of BOGON_NETWORKS ) {
121+ if ( isInSubnet ( ip , network ) ) {
122+ return true ;
123+ }
124+ }
123125 }
124- }
126+ return false ;
125127 }
126- return false ;
127- }
128- }
128+ }
0 commit comments