-
Notifications
You must be signed in to change notification settings - Fork 0
/
robots.mts
162 lines (133 loc) · 3.97 KB
/
robots.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { URL } from 'node:url';
import { globalHeaders } from './global.mjs';
interface robots {
agents : Map<string, {
allow : Array<string>,
disallow : Array<string>
}>,
sitemaps : Array<string>
};
interface robotsRules {
allow : Array<string>,
disallow : Array<string>,
sitemaps : Array<string>
}
function matchRobots(glob : string, str : string) : boolean {
let j : number = 0;
let matches : boolean = true;
let any : boolean = false;
for (let i : number = 0; i < str.length; i++) {
const strChr : string = str[i];
const strChrNext : string = str[i + 1];
const globChr : string = glob[j];
const globChrNext : string = glob[j + 1];
/*
console.log(`i: ${i}, strChr: ${strChr}, strChrNext: ${strChrNext}`);
console.log(`j: ${j}, globChr: ${globChr}, globChrNext: ${globChrNext}`);
*/
if (globChr === "*") {
any = true;
}
if (globChr === "$") {
matches = false;
break;
}
if (any && strChrNext === globChrNext) {
if (strChrNext == undefined && globChrNext == undefined) {
break;
}
any = false;
}
if (!any) {
if (strChr !== globChr) {
matches = false;
break;
}
j++;
}
}
// console.log(`'${str}' ${matches ? "matches" : "doesn't match"} '${glob}'.`);
return matches;
}
function getRobotsRules(productToken : string, robots : robots) : robotsRules {
let result : robotsRules = {
allow: [],
disallow: [],
sitemaps: []
};
for (const agent of robots.agents) {
if (matchRobots(agent[0], productToken)) {
for (const allowUrl of agent[1].allow ?? []) {
result.allow.push(allowUrl);
}
for (const disallowUrl of agent[1].disallow ?? []) {
result.disallow.push(disallowUrl);
}
result.sitemaps = robots.sitemaps;
}
}
return result;
}
function parseRobots(strRobots : string) : robots {
let robots = strRobots.split("\n");
let result : robots = {
agents: new Map<string, {
allow : Array<string>,
disallow : Array<string>
}>,
sitemaps: []
};
const record = (line : string, name : string) : boolean => line.toLowerCase().startsWith(`${name}:`);
let currentUserAgents : Array<string> = [];
for (let line of robots) {
if (line.indexOf("#") != -1) {
line = line.substring(0, line.indexOf("#"));
}
if (line.trim() === "") {
currentUserAgents = [];
continue;
} else if (record(line, "user-agent")) {
currentUserAgents.push(line.substring(12));
for (let currentUserAgent of currentUserAgents) {
let value : undefined | {
allow : Array<string>,
disallow : Array<string>
} = result.agents.get(currentUserAgent) ?? { allow: [], disallow: [] };
value.allow = value.allow ?? [];
value.disallow = value.disallow ?? [];
result.agents.set(currentUserAgent, value);
}
continue;
}
if (record(line, "allow")) {
for (const currentUserAgent of currentUserAgents) {
let value = result.agents.get(currentUserAgent) ?? { allow: [], disallow: [] };
value.allow.push(line.substring(6).trim());
result.agents.set(currentUserAgent, value);
}
} else if (record(line, "disallow")) {
for (const currentUserAgent of currentUserAgents) {
let value = result.agents.get(currentUserAgent) ?? { allow: [], disallow: [] };
value.disallow.push(line.substring(9).trim());
result.agents.set(currentUserAgent, value);
}
} else if (record(line, "sitemap")) {
result.sitemaps.push(line.substring(8).trim());
}
}
return result;
}
async function getRobots(baseUrl : string) {
const url = new URL("/robots.txt", baseUrl);
const headers = new Headers(globalHeaders);
const options = {
headers: headers
};
const res = await fetch(url.href, options);
if (!res.ok) {
throw new Error("HTTP Error " + res.status + ": " + res.statusText);
}
const robots = await res.text();
return parseRobots(robots);
}
export { getRobots, getRobotsRules, parseRobots, matchRobots, robots, robotsRules }