Skip to content

Commit ef72fd9

Browse files
committed
additional prettier results
1 parent 87102c7 commit ef72fd9

File tree

2 files changed

+154
-154
lines changed

2 files changed

+154
-154
lines changed

__tests__/ipinfoResProxyWrapper.test.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,57 @@ import IPinfoResProxyWrapper from "../src/ipinfoResProxyWrapper";
55
const testIfTokenIsSet = process.env.IPINFO_TOKEN ? test : test.skip;
66

77
beforeAll(() => {
8-
dotenv.config();
8+
dotenv.config();
99
});
1010

1111
describe("IPinfoResProxyWrapper", () => {
12-
testIfTokenIsSet("lookupIp", async () => {
13-
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
14-
15-
for (let i = 0; i < 5; i++) {
16-
const data = (await wrapper.lookupIp(
17-
"139.5.0.122"
18-
)) as IPinfoResProxy;
19-
20-
expect(data.ip).toEqual("139.5.0.122");
21-
expect(data.service).toBeDefined();
22-
expect(typeof data.service).toBe("string");
23-
expect(data.last_seen).toBeDefined();
24-
expect(typeof data.last_seen).toBe("string");
25-
expect(data.percent_days_seen).toBeDefined();
26-
expect(typeof data.percent_days_seen).toBe("number");
27-
}
28-
});
29-
30-
testIfTokenIsSet("isBogon", async () => {
31-
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
32-
33-
const data = (await wrapper.lookupIp("198.51.100.1")) as IPBogon;
34-
expect(data.ip).toEqual("198.51.100.1");
35-
expect(data.bogon).toEqual(true);
36-
});
37-
38-
test("Error is thrown for invalid token", async () => {
39-
const wrapper = new IPinfoResProxyWrapper("invalid-token");
40-
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
41-
});
42-
43-
test("Error is thrown when response cannot be parsed as JSON", async () => {
44-
const baseUrlWithUnparseableResponse = "https://ipinfo.io/developers#";
45-
const wrapper = new IPinfoResProxyWrapper(
46-
"token",
47-
undefined,
48-
undefined,
49-
baseUrlWithUnparseableResponse
50-
);
51-
52-
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
53-
54-
const result = await wrapper
55-
.lookupIp("1.2.3.4")
56-
.then((_) => "parseable")
57-
.catch((_) => "unparseable");
58-
59-
expect(result).toEqual("unparseable");
60-
});
12+
testIfTokenIsSet("lookupIp", async () => {
13+
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
14+
15+
for (let i = 0; i < 5; i++) {
16+
const data = (await wrapper.lookupIp(
17+
"139.5.0.122"
18+
)) as IPinfoResProxy;
19+
20+
expect(data.ip).toEqual("139.5.0.122");
21+
expect(data.service).toBeDefined();
22+
expect(typeof data.service).toBe("string");
23+
expect(data.last_seen).toBeDefined();
24+
expect(typeof data.last_seen).toBe("string");
25+
expect(data.percent_days_seen).toBeDefined();
26+
expect(typeof data.percent_days_seen).toBe("number");
27+
}
28+
});
29+
30+
testIfTokenIsSet("isBogon", async () => {
31+
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
32+
33+
const data = (await wrapper.lookupIp("198.51.100.1")) as IPBogon;
34+
expect(data.ip).toEqual("198.51.100.1");
35+
expect(data.bogon).toEqual(true);
36+
});
37+
38+
test("Error is thrown for invalid token", async () => {
39+
const wrapper = new IPinfoResProxyWrapper("invalid-token");
40+
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
41+
});
42+
43+
test("Error is thrown when response cannot be parsed as JSON", async () => {
44+
const baseUrlWithUnparseableResponse = "https://ipinfo.io/developers#";
45+
const wrapper = new IPinfoResProxyWrapper(
46+
"token",
47+
undefined,
48+
undefined,
49+
baseUrlWithUnparseableResponse
50+
);
51+
52+
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
53+
54+
const result = await wrapper
55+
.lookupIp("1.2.3.4")
56+
.then((_) => "parseable")
57+
.catch((_) => "unparseable");
58+
59+
expect(result).toEqual("unparseable");
60+
});
6161
});

src/ipinfoResProxyWrapper.ts

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,125 +4,125 @@ import LruCache from "./cache/lruCache";
44
import ApiLimitError from "./errors/apiLimitError";
55
import { isInSubnet } from "subnet-check";
66
import {
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";
1414
import VERSION from "./version";
1515

1616
const clientUserAgent = `IPinfoClient/nodejs/${VERSION}`;
1717

1818
export 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

Comments
 (0)