Skip to content

Commit 3f58756

Browse files
committed
Rename methods (we return a list of matches now)
1 parent 2fa9007 commit 3f58756

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

library/agent/Agent.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,14 +1074,14 @@ t.test("it fetches blocked lists", async () => {
10741074

10751075
await setTimeout(0);
10761076

1077-
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), [
1077+
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), [
10781078
{
10791079
key: "some/key",
10801080
monitor: false,
10811081
reason: "Description",
10821082
},
10831083
]);
1084-
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), [
1084+
t.same(agent.getConfig().getBlockedIPAddresses("fe80::1234:5678:abcd:ef12"), [
10851085
{
10861086
key: "some/key",
10871087
monitor: false,
@@ -1092,7 +1092,7 @@ t.test("it fetches blocked lists", async () => {
10921092
t.same(
10931093
agent
10941094
.getConfig()
1095-
.isUserAgentBlocked(
1095+
.getBlockedUserAgents(
10961096
"Mozilla/5.0 (compatible) AI2Bot (+https://www.allenai.org/crawler)"
10971097
),
10981098
[
@@ -1104,7 +1104,9 @@ t.test("it fetches blocked lists", async () => {
11041104
);
11051105

11061106
t.same(
1107-
agent.getConfig().isUserAgentBlocked("Mozilla/5.0 (compatible) Bytespider"),
1107+
agent
1108+
.getConfig()
1109+
.getBlockedUserAgents("Mozilla/5.0 (compatible) Bytespider"),
11081110
[
11091111
{
11101112
key: "ai_bots",
@@ -1113,7 +1115,10 @@ t.test("it fetches blocked lists", async () => {
11131115
]
11141116
);
11151117

1116-
t.same(agent.getConfig().isUserAgentBlocked("Mozilla/5.0 (compatible)"), []);
1118+
t.same(
1119+
agent.getConfig().getBlockedUserAgents("Mozilla/5.0 (compatible)"),
1120+
[]
1121+
);
11171122
});
11181123

11191124
t.test("it does not fetch blocked IPs if serverless", async () => {
@@ -1127,15 +1132,15 @@ t.test("it does not fetch blocked IPs if serverless", async () => {
11271132

11281133
await setTimeout(0);
11291134

1130-
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), []);
1135+
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), []);
11311136
t.same(agent.getConfig().isAllowedIPAddress("1.3.2.4"), {
11321137
allowed: true,
11331138
});
11341139

11351140
t.same(
11361141
agent
11371142
.getConfig()
1138-
.isUserAgentBlocked(
1143+
.getBlockedUserAgents(
11391144
"Mozilla/5.0 (compatible) AI2Bot (+https://www.allenai.org/crawler)"
11401145
),
11411146
[]
@@ -1153,14 +1158,14 @@ t.test("it only allows some IP addresses", async () => {
11531158

11541159
await setTimeout(0);
11551160

1156-
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), [
1161+
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), [
11571162
{
11581163
key: "some/key",
11591164
monitor: false,
11601165
reason: "Description",
11611166
},
11621167
]);
1163-
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), [
1168+
t.same(agent.getConfig().getBlockedIPAddresses("fe80::1234:5678:abcd:ef12"), [
11641169
{
11651170
key: "some/key",
11661171
monitor: false,

library/agent/ServiceConfig.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,54 +112,54 @@ t.test("ip blocking works", async () => {
112112
],
113113
[]
114114
);
115-
t.same(config.isIPAddressBlocked("1.2.3.4"), [
115+
t.same(config.getBlockedIPAddresses("1.2.3.4"), [
116116
{
117117
key: "geoip/Belgium;BE",
118118
monitor: false,
119119
reason: "description",
120120
},
121121
]);
122-
t.same(config.isIPAddressBlocked("2.3.4.5"), []);
123-
t.same(config.isIPAddressBlocked("192.168.2.2"), [
122+
t.same(config.getBlockedIPAddresses("2.3.4.5"), []);
123+
t.same(config.getBlockedIPAddresses("192.168.2.2"), [
124124
{
125125
key: "geoip/Belgium;BE",
126126
monitor: false,
127127
reason: "description",
128128
},
129129
]);
130-
t.same(config.isIPAddressBlocked("fd00:1234:5678:9abc::1"), [
130+
t.same(config.getBlockedIPAddresses("fd00:1234:5678:9abc::1"), [
131131
{
132132
key: "geoip/Belgium;BE",
133133
monitor: false,
134134
reason: "description",
135135
},
136136
]);
137-
t.same(config.isIPAddressBlocked("fd00:1234:5678:9abc::2"), []);
138-
t.same(config.isIPAddressBlocked("fd00:3234:5678:9abc::1"), [
137+
t.same(config.getBlockedIPAddresses("fd00:1234:5678:9abc::2"), []);
138+
t.same(config.getBlockedIPAddresses("fd00:3234:5678:9abc::1"), [
139139
{
140140
key: "geoip/Belgium;BE",
141141
monitor: false,
142142
reason: "description",
143143
},
144144
]);
145-
t.same(config.isIPAddressBlocked("fd00:3234:5678:9abc::2"), [
145+
t.same(config.getBlockedIPAddresses("fd00:3234:5678:9abc::2"), [
146146
{
147147
key: "geoip/Belgium;BE",
148148
monitor: false,
149149
reason: "description",
150150
},
151151
]);
152-
t.same(config.isIPAddressBlocked("5.6.7.8"), [
152+
t.same(config.getBlockedIPAddresses("5.6.7.8"), [
153153
{
154154
key: "geoip/Belgium;BE",
155155
monitor: false,
156156
reason: "description",
157157
},
158158
]);
159-
t.same(config.isIPAddressBlocked("1.2"), []);
159+
t.same(config.getBlockedIPAddresses("1.2"), []);
160160

161161
config.updateBlockedIPAddresses([]);
162-
t.same(config.isIPAddressBlocked("1.2.3.4"), []);
162+
t.same(config.getBlockedIPAddresses("1.2.3.4"), []);
163163
});
164164

165165
t.test("update blocked IPs contains empty IPs", async (t) => {
@@ -173,7 +173,7 @@ t.test("update blocked IPs contains empty IPs", async (t) => {
173173
monitor: false,
174174
},
175175
]);
176-
t.same(config.isIPAddressBlocked("1.2.3.4"), []);
176+
t.same(config.getBlockedIPAddresses("1.2.3.4"), []);
177177
});
178178

179179
t.test("it blocks bots", async () => {
@@ -186,23 +186,23 @@ t.test("it blocks bots", async () => {
186186
},
187187
]);
188188

189-
t.same(config.isUserAgentBlocked("googlebot"), [
189+
t.same(config.getBlockedUserAgents("googlebot"), [
190190
{
191191
key: "test",
192192
monitor: false,
193193
},
194194
]);
195-
t.same(config.isUserAgentBlocked("123 bingbot abc"), [
195+
t.same(config.getBlockedUserAgents("123 bingbot abc"), [
196196
{
197197
key: "test",
198198
monitor: false,
199199
},
200200
]);
201-
t.same(config.isUserAgentBlocked("bing"), []);
201+
t.same(config.getBlockedUserAgents("bing"), []);
202202

203203
config.updateBlockedUserAgents([]);
204204

205-
t.same(config.isUserAgentBlocked("googlebot"), []);
205+
t.same(config.getBlockedUserAgents("googlebot"), []);
206206
});
207207

208208
t.test("restricting access to some ips", async () => {
@@ -320,13 +320,13 @@ t.test("should return all matching user agent patterns", async (t) => {
320320
monitor: false,
321321
},
322322
]);
323-
t.same(config.isUserAgentBlocked("googlebot"), [
323+
t.same(config.getBlockedUserAgents("googlebot"), [
324324
{
325325
key: "bots",
326326
monitor: false,
327327
},
328328
]);
329-
t.same(config.isUserAgentBlocked("firefox"), []);
329+
t.same(config.getBlockedUserAgents("firefox"), []);
330330
});
331331

332332
t.test("it returns and updates blocked user agents", async (t) => {
@@ -343,7 +343,7 @@ t.test("it returns and updates blocked user agents", async (t) => {
343343
monitor: true,
344344
},
345345
]);
346-
t.same(config.isUserAgentBlocked("googlebot"), [
346+
t.same(config.getBlockedUserAgents("googlebot"), [
347347
{
348348
key: "bots",
349349
monitor: false,
@@ -353,17 +353,17 @@ t.test("it returns and updates blocked user agents", async (t) => {
353353
monitor: true,
354354
},
355355
]);
356-
t.same(config.isUserAgentBlocked("bingbot"), [
356+
t.same(config.getBlockedUserAgents("bingbot"), [
357357
{
358358
key: "bots",
359359
monitor: false,
360360
},
361361
]);
362362

363363
config.updateBlockedUserAgents([]);
364-
t.same(config.isUserAgentBlocked("googlebot"), []);
365-
t.same(config.isUserAgentBlocked("bingbot"), []);
366-
t.same(config.isUserAgentBlocked("firefox"), []);
364+
t.same(config.getBlockedUserAgents("googlebot"), []);
365+
t.same(config.getBlockedUserAgents("bingbot"), []);
366+
t.same(config.getBlockedUserAgents("firefox"), []);
367367
});
368368

369369
t.test("it ignores user agent lists with empty patterns", async (t) => {
@@ -375,7 +375,7 @@ t.test("it ignores user agent lists with empty patterns", async (t) => {
375375
monitor: false,
376376
},
377377
]);
378-
t.same(config.isUserAgentBlocked("googlebot"), []);
378+
t.same(config.getBlockedUserAgents("googlebot"), []);
379379
});
380380

381381
t.test(
@@ -389,6 +389,6 @@ t.test(
389389
monitor: false,
390390
},
391391
]);
392-
t.same(config.isUserAgentBlocked("googlebot"), []);
392+
t.same(config.getBlockedUserAgents("googlebot"), []);
393393
}
394394
);

library/agent/ServiceConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class ServiceConfig {
115115
return this.blockedUserIds.has(userId);
116116
}
117117

118-
isIPAddressBlocked(
118+
getBlockedIPAddresses(
119119
ip: string
120120
): { key: string; monitor: boolean; reason: string }[] {
121121
return this.blockedIPAddresses
@@ -166,7 +166,7 @@ export class ServiceConfig {
166166
this.setBlockedUserAgents(blockedUserAgents);
167167
}
168168

169-
isUserAgentBlocked(ua: string): { key: string; monitor: boolean }[] {
169+
getBlockedUserAgents(ua: string): { key: string; monitor: boolean }[] {
170170
return this.blockedUserAgents
171171
.filter((list) => list.pattern.test(ua))
172172
.map((list) => ({

library/sources/http-server/checkIfRequestIsBlocked.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function checkIfRequestIsBlocked(
6666
}
6767

6868
const blockedIPs = context.remoteAddress
69-
? agent.getConfig().isIPAddressBlocked(context.remoteAddress)
69+
? agent.getConfig().getBlockedIPAddresses(context.remoteAddress)
7070
: [];
7171

7272
if (blockedIPs.length > 0) {
@@ -90,7 +90,7 @@ export function checkIfRequestIsBlocked(
9090

9191
const blockedUserAgents =
9292
context.headers && typeof context.headers["user-agent"] === "string"
93-
? agent.getConfig().isUserAgentBlocked(context.headers["user-agent"])
93+
? agent.getConfig().getBlockedUserAgents(context.headers["user-agent"])
9494
: [];
9595

9696
if (blockedUserAgents.length > 0) {

0 commit comments

Comments
 (0)