robots-parser 2.1.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Robots.js +110 -64
- package/index.d.ts +12 -0
- package/package.json +42 -21
- package/readme.md +87 -48
- package/test/Robots.js +0 -610
package/Robots.js
CHANGED
|
@@ -25,7 +25,7 @@ function trimLine(line) {
|
|
|
25
25
|
/**
|
|
26
26
|
* Remove comments from lines
|
|
27
27
|
*
|
|
28
|
-
* @param {string} line
|
|
28
|
+
* @param {string} line
|
|
29
29
|
* @return {string}
|
|
30
30
|
* @private
|
|
31
31
|
*/
|
|
@@ -57,7 +57,7 @@ function splitLine(line) {
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* Normalises the user-agent string by converting it to
|
|
60
|
-
*
|
|
60
|
+
* lower case and removing any version numbers.
|
|
61
61
|
*
|
|
62
62
|
* @param {string} userAgent
|
|
63
63
|
* @return {string}
|
|
@@ -78,7 +78,7 @@ function formatUserAgent(userAgent) {
|
|
|
78
78
|
/**
|
|
79
79
|
* Normalises the URL encoding of a path by encoding
|
|
80
80
|
* unicode characters.
|
|
81
|
-
*
|
|
81
|
+
*
|
|
82
82
|
* @param {string} path
|
|
83
83
|
* @return {string}
|
|
84
84
|
* @private
|
|
@@ -86,16 +86,16 @@ function formatUserAgent(userAgent) {
|
|
|
86
86
|
function normaliseEncoding(path) {
|
|
87
87
|
try {
|
|
88
88
|
return urlEncodeToUpper(encodeURI(path).replace(/%25/g, '%'));
|
|
89
|
-
} catch(e) {
|
|
89
|
+
} catch (e) {
|
|
90
90
|
return path;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
* Convert URL encodings to
|
|
96
|
-
*
|
|
97
|
-
* e.g.: %2a%ef becomes %2A%EF
|
|
98
|
-
*
|
|
95
|
+
* Convert URL encodings to support case.
|
|
96
|
+
*
|
|
97
|
+
* e.g.: %2a%ef becomes %2A%EF
|
|
98
|
+
*
|
|
99
99
|
* @param {string} path
|
|
100
100
|
* @return {string}
|
|
101
101
|
* @private
|
|
@@ -107,32 +107,72 @@ function urlEncodeToUpper(path) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
111
|
-
* pattern.
|
|
110
|
+
* Matches a pattern with the specified path
|
|
112
111
|
*
|
|
113
|
-
*
|
|
112
|
+
* Uses same algorithm to match patterns as the Google implementation in
|
|
113
|
+
* google/robotstxt so it should be consistent with the spec.
|
|
114
114
|
*
|
|
115
|
-
* @
|
|
116
|
-
* @
|
|
115
|
+
* @see https://github.com/google/robotstxt/blob/f465f0ede81099dd8bc4aeb2966b3a892bd488b3/robots.cc#L74
|
|
116
|
+
* @param {string} pattern
|
|
117
|
+
* @param {string} path
|
|
118
|
+
* @return {boolean}
|
|
117
119
|
* @private
|
|
118
120
|
*/
|
|
119
|
-
function
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
function matches(pattern, path) {
|
|
122
|
+
// I've added extra comments to try make this easier to understand
|
|
123
|
+
|
|
124
|
+
// Stores the lengths of all the current matching substrings.
|
|
125
|
+
// Maximum number of possible matching lengths is every length in path plus
|
|
126
|
+
// 1 to handle 0 length too (if pattern starts with * which is zero or more)
|
|
127
|
+
var matchingLengths = new Array(path.length + 1);
|
|
128
|
+
var numMatchingLengths = 1;
|
|
129
|
+
|
|
130
|
+
// Initially longest match is 0
|
|
131
|
+
matchingLengths[0] = 0;
|
|
132
|
+
|
|
133
|
+
for (var p = 0; p < pattern.length; p++) {
|
|
134
|
+
// If $ is at the end of pattern then we must match the whole path.
|
|
135
|
+
// Which is true if the longest matching length matches path length
|
|
136
|
+
if (pattern[p] === '$' && p + 1 === pattern.length) {
|
|
137
|
+
return matchingLengths[numMatchingLengths - 1] === path.length;
|
|
138
|
+
}
|
|
123
139
|
|
|
124
|
-
|
|
140
|
+
// Handle wildcards
|
|
141
|
+
if (pattern[p] == '*') {
|
|
142
|
+
// Wildcard so all substrings minus the current smallest matching
|
|
143
|
+
// length are matches
|
|
144
|
+
numMatchingLengths = path.length - matchingLengths[0] + 1;
|
|
145
|
+
|
|
146
|
+
// Update matching lengths to include the smallest all the way up
|
|
147
|
+
// to numMatchingLengths
|
|
148
|
+
// Don't update smallest possible match as * matches zero or more
|
|
149
|
+
// so the smallest current match is also valid
|
|
150
|
+
for (var i = 1; i < numMatchingLengths; i++) {
|
|
151
|
+
matchingLengths[i] = matchingLengths[i - 1] + 1;
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
// Check the char at the matching length matches the pattern, if it
|
|
155
|
+
// does increment it and add it as a valid length, ignore if not.
|
|
156
|
+
var numMatches = 0;
|
|
157
|
+
for (var i = 0; i < numMatchingLengths; i++) {
|
|
158
|
+
if (
|
|
159
|
+
matchingLengths[i] < path.length &&
|
|
160
|
+
path[matchingLengths[i]] === pattern[p]
|
|
161
|
+
) {
|
|
162
|
+
matchingLengths[numMatches++] = matchingLengths[i] + 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
125
165
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
166
|
+
// No paths matched the current pattern char so not a match
|
|
167
|
+
if (numMatches == 0) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
129
170
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
.replace(endOfLinePattern, '$');
|
|
171
|
+
numMatchingLengths = numMatches;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
134
174
|
|
|
135
|
-
return
|
|
175
|
+
return true;
|
|
136
176
|
}
|
|
137
177
|
|
|
138
178
|
function parseRobots(contents, robots) {
|
|
@@ -145,7 +185,7 @@ function parseRobots(contents, robots) {
|
|
|
145
185
|
|
|
146
186
|
var currentUserAgents = [];
|
|
147
187
|
var isNoneUserAgentState = true;
|
|
148
|
-
for (var i=0; i < lines.length; i++) {
|
|
188
|
+
for (var i = 0; i < lines.length; i++) {
|
|
149
189
|
var line = lines[i];
|
|
150
190
|
|
|
151
191
|
if (!line || !line[0]) {
|
|
@@ -196,55 +236,59 @@ function parseRobots(contents, robots) {
|
|
|
196
236
|
* @private
|
|
197
237
|
*/
|
|
198
238
|
function findRule(path, rules) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
239
|
+
var matchedRule = null;
|
|
240
|
+
|
|
241
|
+
for (var i = 0; i < rules.length; i++) {
|
|
242
|
+
var rule = rules[i];
|
|
243
|
+
|
|
244
|
+
if (!matches(rule.pattern, path)) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// The longest matching rule takes precedence
|
|
249
|
+
// If rules are the same length then allow takes precedence
|
|
250
|
+
if (!matchedRule || rule.pattern.length > matchedRule.pattern.length) {
|
|
251
|
+
matchedRule = rule;
|
|
252
|
+
} else if (
|
|
253
|
+
rule.pattern.length == matchedRule.pattern.length &&
|
|
254
|
+
rule.allow &&
|
|
255
|
+
!matchedRule.allow
|
|
256
|
+
) {
|
|
257
|
+
matchedRule = rule;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return matchedRule;
|
|
221
262
|
}
|
|
222
263
|
|
|
223
264
|
/**
|
|
224
265
|
* Converts provided string into an URL object.
|
|
225
|
-
*
|
|
266
|
+
*
|
|
226
267
|
* Will return null if provided string is not a valid URL.
|
|
227
|
-
*
|
|
228
|
-
* @param {string} url
|
|
268
|
+
*
|
|
269
|
+
* @param {string} url
|
|
229
270
|
* @return {?URL}
|
|
230
271
|
* @private
|
|
231
272
|
*/
|
|
232
273
|
function parseUrl(url) {
|
|
233
274
|
try {
|
|
234
|
-
|
|
235
|
-
|
|
275
|
+
// Specify a URL to be used with relative paths
|
|
276
|
+
// Using non-existent subdomain so can never cause conflict unless
|
|
277
|
+
// trying to crawl it but doesn't exist and even if tried worst that can
|
|
278
|
+
// happen is it allows relative URLs on it.
|
|
279
|
+
return new URL(url, 'http://robots-relative.samclarke.com/');
|
|
280
|
+
} catch (e) {
|
|
236
281
|
return null;
|
|
237
282
|
}
|
|
238
283
|
}
|
|
239
284
|
|
|
240
|
-
|
|
241
285
|
function Robots(url, contents) {
|
|
242
286
|
this._url = parseUrl(url) || {};
|
|
243
287
|
this._url.port = this._url.port || 80;
|
|
244
288
|
|
|
245
|
-
this._rules =
|
|
289
|
+
this._rules = Object.create(null);
|
|
246
290
|
this._sitemaps = [];
|
|
247
|
-
this.
|
|
291
|
+
this._preferredHost = null;
|
|
248
292
|
|
|
249
293
|
parseRobots(contents || '', this);
|
|
250
294
|
}
|
|
@@ -269,7 +313,7 @@ Robots.prototype.addRule = function (userAgents, pattern, allow, lineNumber) {
|
|
|
269
313
|
}
|
|
270
314
|
|
|
271
315
|
rules[userAgent].push({
|
|
272
|
-
pattern:
|
|
316
|
+
pattern: normaliseEncoding(pattern),
|
|
273
317
|
allow: allow,
|
|
274
318
|
lineNumber: lineNumber
|
|
275
319
|
});
|
|
@@ -312,24 +356,26 @@ Robots.prototype.addSitemap = function (url) {
|
|
|
312
356
|
* @param {string} url
|
|
313
357
|
*/
|
|
314
358
|
Robots.prototype.setPreferredHost = function (url) {
|
|
315
|
-
this.
|
|
359
|
+
this._preferredHost = url;
|
|
316
360
|
};
|
|
317
361
|
|
|
318
362
|
Robots.prototype._getRule = function (url, ua) {
|
|
319
363
|
var parsedUrl = parseUrl(url) || {};
|
|
320
364
|
var userAgent = formatUserAgent(ua || '*');
|
|
321
365
|
|
|
322
|
-
parsedUrl.port = parsedUrl.port ||
|
|
366
|
+
parsedUrl.port = parsedUrl.port || 80;
|
|
323
367
|
|
|
324
368
|
// The base URL must match otherwise this robots.txt is not valid for it.
|
|
325
|
-
if (
|
|
369
|
+
if (
|
|
370
|
+
parsedUrl.protocol !== this._url.protocol ||
|
|
326
371
|
parsedUrl.hostname !== this._url.hostname ||
|
|
327
|
-
parsedUrl.port !== this._url.port
|
|
372
|
+
parsedUrl.port !== this._url.port
|
|
373
|
+
) {
|
|
328
374
|
return;
|
|
329
375
|
}
|
|
330
376
|
|
|
331
377
|
var rules = this._rules[userAgent] || this._rules['*'] || [];
|
|
332
|
-
var path = urlEncodeToUpper(parsedUrl.pathname + parsedUrl.search)
|
|
378
|
+
var path = urlEncodeToUpper(parsedUrl.pathname + parsedUrl.search);
|
|
333
379
|
var rule = findRule(path, rules);
|
|
334
380
|
|
|
335
381
|
return rule;
|
|
@@ -406,7 +452,7 @@ Robots.prototype.getCrawlDelay = function (ua) {
|
|
|
406
452
|
* @return {string?}
|
|
407
453
|
*/
|
|
408
454
|
Robots.prototype.getPreferredHost = function () {
|
|
409
|
-
return this.
|
|
455
|
+
return this._preferredHost;
|
|
410
456
|
};
|
|
411
457
|
|
|
412
458
|
/**
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare module 'robots-parser';
|
|
2
|
+
|
|
3
|
+
interface Robot {
|
|
4
|
+
isAllowed(url: string, ua?: string): boolean | undefined;
|
|
5
|
+
isDisallowed(url: string, ua?: string): boolean | undefined;
|
|
6
|
+
getMatchingLineNumber(url: string, ua?: string): number;
|
|
7
|
+
getCrawlDelay(ua?: string): number | undefined;
|
|
8
|
+
getSitemaps(): string[];
|
|
9
|
+
getPreferredHost(): string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function robotsParser(url: string, robotstxt: string): Robot;
|
package/package.json
CHANGED
|
@@ -1,23 +1,44 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
"name": "robots-parser",
|
|
3
|
+
"version": "2.4.0",
|
|
4
|
+
"description": "NodeJS robots.txt parser with support for wildcard (*) matching.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"robots.txt",
|
|
7
|
+
"parser",
|
|
8
|
+
"user-agent",
|
|
9
|
+
"scraper",
|
|
10
|
+
"spider",
|
|
11
|
+
"bot",
|
|
12
|
+
"robots-exclusion-standard"
|
|
13
|
+
],
|
|
14
|
+
"main": "index.js",
|
|
15
|
+
"directories": {
|
|
16
|
+
"test": "tests"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "nyc --reporter=text-summary --reporter=html --reporter=lcovonly mocha"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/samclarke/robots-parser.git"
|
|
24
|
+
},
|
|
25
|
+
"author": "Sam Clarke <sam@samclarke.com>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"files": [
|
|
28
|
+
"/Robots.js",
|
|
29
|
+
"/index.js",
|
|
30
|
+
"/index.d.ts"
|
|
31
|
+
],
|
|
32
|
+
"prettier": {
|
|
33
|
+
"tabWidth": 4,
|
|
34
|
+
"useTabs": true,
|
|
35
|
+
"singleQuote": true,
|
|
36
|
+
"trailingComma": "none"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"chai": "^4.3.4",
|
|
40
|
+
"mocha": "^9.1.3",
|
|
41
|
+
"nyc": "^15.1.0"
|
|
42
|
+
},
|
|
43
|
+
"types": "./index.d.ts"
|
|
23
44
|
}
|
package/readme.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
# Robots Parser [](https://www.npmjs.com/package/robots-parser) [](https://deepscan.io/dashboard#view=project&tid=457&pid=16277&bid=344939) [](https://github.com/samclarke/robots-parser/blob/master/license.md) [](https://coveralls.io/github/samclarke/robots-parser?branch=master)
|
|
2
2
|
|
|
3
3
|
NodeJS robots.txt parser.
|
|
4
4
|
|
|
5
5
|
It currently supports:
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
- User-agent:
|
|
8
|
+
- Allow:
|
|
9
|
+
- Disallow:
|
|
10
|
+
- Sitemap:
|
|
11
|
+
- Crawl-delay:
|
|
12
|
+
- Host:
|
|
13
|
+
- Paths with wildcards (\*) and EOL matching ($)
|
|
14
14
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
@@ -38,32 +38,32 @@ var robots = robotsParser('http://www.example.com/robots.txt', [
|
|
|
38
38
|
'Host: example.com'
|
|
39
39
|
].join('\n'));
|
|
40
40
|
|
|
41
|
-
robots.isAllowed('http://www.example.com/test.html', 'Sams-Bot/1.0'); //
|
|
41
|
+
robots.isAllowed('http://www.example.com/test.html', 'Sams-Bot/1.0'); // true
|
|
42
42
|
robots.isAllowed('http://www.example.com/dir/test.html', 'Sams-Bot/1.0'); // true
|
|
43
43
|
robots.isDisallowed('http://www.example.com/dir/test2.html', 'Sams-Bot/1.0'); // true
|
|
44
44
|
robots.getCrawlDelay('Sams-Bot/1.0'); // 1
|
|
45
45
|
robots.getSitemaps(); // ['http://example.com/sitemap.xml']
|
|
46
|
-
robots.
|
|
46
|
+
robots.getPreferredHost(); // example.com
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
|
|
50
49
|
### isAllowed(url, [ua])
|
|
50
|
+
|
|
51
51
|
**boolean or undefined**
|
|
52
52
|
|
|
53
53
|
Returns true if crawling the specified URL is allowed for the specified user-agent.
|
|
54
54
|
|
|
55
55
|
This will return `undefined` if the URL isn't valid for this robots.txt.
|
|
56
56
|
|
|
57
|
-
|
|
58
57
|
### isDisallowed(url, [ua])
|
|
58
|
+
|
|
59
59
|
**boolean or undefined**
|
|
60
60
|
|
|
61
61
|
Returns true if crawling the specified URL is not allowed for the specified user-agent.
|
|
62
62
|
|
|
63
63
|
This will return `undefined` if the URL isn't valid for this robots.txt.
|
|
64
64
|
|
|
65
|
-
|
|
66
65
|
### getMatchingLineNumber(url, [ua])
|
|
66
|
+
|
|
67
67
|
**number or undefined**
|
|
68
68
|
|
|
69
69
|
Returns the line number of the matching directive for the specified URL and user-agent if any.
|
|
@@ -72,80 +72,119 @@ Line numbers start at 1 and go up (1-based indexing).
|
|
|
72
72
|
|
|
73
73
|
Returns -1 if there is no matching directive. If a rule is manually added without a lineNumber then this will return undefined for that rule.
|
|
74
74
|
|
|
75
|
-
|
|
76
75
|
### getCrawlDelay([ua])
|
|
76
|
+
|
|
77
77
|
**number or undefined**
|
|
78
78
|
|
|
79
79
|
Returns the number of seconds the specified user-agent should wait between requests.
|
|
80
80
|
|
|
81
81
|
Returns undefined if no crawl delay has been specified for this user-agent.
|
|
82
82
|
|
|
83
|
-
|
|
84
83
|
### getSitemaps()
|
|
84
|
+
|
|
85
85
|
**array**
|
|
86
86
|
|
|
87
87
|
Returns an array of sitemap URLs specified by the `sitemap:` directive.
|
|
88
88
|
|
|
89
|
-
|
|
90
89
|
### getPreferredHost()
|
|
90
|
+
|
|
91
91
|
**string or null**
|
|
92
92
|
|
|
93
93
|
Returns the preferred host name specified by the `host:` directive or null if there isn't one.
|
|
94
94
|
|
|
95
|
-
|
|
96
95
|
# Changes
|
|
97
96
|
|
|
97
|
+
### Version 2.4.0:
|
|
98
|
+
|
|
99
|
+
- Added Typescript definitions
|
|
100
|
+
– Thanks to @danhab99 for creating
|
|
101
|
+
- Added SECURITY.md policy and CodeQL scanning
|
|
102
|
+
|
|
103
|
+
### Version 2.3.0:
|
|
104
|
+
|
|
105
|
+
- Fixed bug where if the user-agent passed to `isAllowed()` / `isDisallowed()` is called "constructor" it would throw an error.
|
|
106
|
+
- Added support for relative URLs. This does not affect the default behavior so can safely be upgraded.
|
|
107
|
+
|
|
108
|
+
Relative matching is only allowed if both the robots.txt URL and the URLs being checked are relative.
|
|
109
|
+
|
|
110
|
+
For example:
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
var robots = robotsParser('/robots.txt', [
|
|
114
|
+
'User-agent: *',
|
|
115
|
+
'Disallow: /dir/',
|
|
116
|
+
'Disallow: /test.html',
|
|
117
|
+
'Allow: /dir/test.html',
|
|
118
|
+
'Allow: /test.html'
|
|
119
|
+
].join('\n'));
|
|
120
|
+
|
|
121
|
+
robots.isAllowed('/test.html', 'Sams-Bot/1.0'); // false
|
|
122
|
+
robots.isAllowed('/dir/test.html', 'Sams-Bot/1.0'); // true
|
|
123
|
+
robots.isDisallowed('/dir/test2.html', 'Sams-Bot/1.0'); // true
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Version 2.2.0:
|
|
127
|
+
|
|
128
|
+
- Fixed bug that with matching wildcard patterns with some URLs
|
|
129
|
+
– Thanks to @ckylape for reporting and fixing
|
|
130
|
+
- Changed matching algorithm to match Google's implementation in google/robotstxt
|
|
131
|
+
- Changed order of precedence to match current spec
|
|
132
|
+
|
|
133
|
+
### Version 2.1.1:
|
|
134
|
+
|
|
135
|
+
- Fix bug that could be used to causing rule checking to take a long time
|
|
136
|
+
– Thanks to @andeanfog
|
|
137
|
+
|
|
98
138
|
### Version 2.1.0:
|
|
99
139
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
140
|
+
- Removed use of punycode module API's as new URL API handles it
|
|
141
|
+
- Improved test coverage
|
|
142
|
+
- Added tests for percent encoded paths and improved support
|
|
143
|
+
- Added `getMatchingLineNumber()` method
|
|
144
|
+
- Fixed bug with comments on same line as directive
|
|
105
145
|
|
|
106
146
|
### Version 2.0.0:
|
|
107
147
|
|
|
108
148
|
This release is not 100% backwards compatible as it now uses the new URL APIs which are not supported in Node < 7.
|
|
109
149
|
|
|
110
|
-
|
|
111
|
-
|
|
150
|
+
- Update code to not use deprecated URL module API's.
|
|
151
|
+
– Thanks to @kdzwinel
|
|
112
152
|
|
|
113
153
|
### Version 1.0.2:
|
|
114
154
|
|
|
115
|
-
|
|
155
|
+
- Fixed error caused by invalid URLs missing the protocol.
|
|
116
156
|
|
|
117
157
|
### Version 1.0.1:
|
|
118
158
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
159
|
+
- Fixed bug with the "user-agent" rule being treated as case sensitive.
|
|
160
|
+
– Thanks to @brendonboshell
|
|
161
|
+
- Improved test coverage.
|
|
162
|
+
– Thanks to @schornio
|
|
123
163
|
|
|
124
164
|
### Version 1.0.0:
|
|
125
165
|
|
|
126
|
-
|
|
127
|
-
|
|
166
|
+
- Initial release.
|
|
128
167
|
|
|
129
168
|
# License
|
|
130
169
|
|
|
131
|
-
|
|
170
|
+
The MIT License (MIT)
|
|
132
171
|
|
|
133
|
-
|
|
172
|
+
Copyright (c) 2014 Sam Clarke
|
|
134
173
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
174
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
175
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
176
|
+
in the Software without restriction, including without limitation the rights
|
|
177
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
178
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
179
|
+
furnished to do so, subject to the following conditions:
|
|
141
180
|
|
|
142
|
-
|
|
143
|
-
|
|
181
|
+
The above copyright notice and this permission notice shall be included in
|
|
182
|
+
all copies or substantial portions of the Software.
|
|
144
183
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
184
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
185
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
186
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
187
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
188
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
189
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
190
|
+
THE SOFTWARE.
|
package/test/Robots.js
DELETED
|
@@ -1,610 +0,0 @@
|
|
|
1
|
-
var robotsParser = require('../index');
|
|
2
|
-
var expect = require('chai').expect;
|
|
3
|
-
var punycode = require('punycode');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function testRobots(url, contents, allowed, disallowed) {
|
|
7
|
-
var robots = robotsParser(url, contents);
|
|
8
|
-
|
|
9
|
-
allowed.forEach(function (url) {
|
|
10
|
-
expect(robots.isAllowed(url)).to.equal(true);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
disallowed.forEach(function (url) {
|
|
14
|
-
expect(robots.isDisallowed(url)).to.equal(true);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
describe('Robots', function () {
|
|
19
|
-
it('should parse the disallow directive', function () {
|
|
20
|
-
var contents = [
|
|
21
|
-
'User-agent: *',
|
|
22
|
-
'Disallow: /fish/',
|
|
23
|
-
'Disallow: /test.html'
|
|
24
|
-
].join('\n');
|
|
25
|
-
|
|
26
|
-
var allowed = [
|
|
27
|
-
'http://www.example.com/fish',
|
|
28
|
-
'http://www.example.com/Test.html'
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
var disallowed = [
|
|
32
|
-
'http://www.example.com/fish/index.php',
|
|
33
|
-
'http://www.example.com/fish/',
|
|
34
|
-
'http://www.example.com/test.html'
|
|
35
|
-
];
|
|
36
|
-
|
|
37
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should parse the allow directive', function () {
|
|
41
|
-
var contents = [
|
|
42
|
-
'User-agent: *',
|
|
43
|
-
'Disallow: /fish/',
|
|
44
|
-
'Disallow: /test.html',
|
|
45
|
-
'Allow: /fish/test.html',
|
|
46
|
-
'Allow: /test.html'
|
|
47
|
-
].join('\n');
|
|
48
|
-
|
|
49
|
-
var allowed = [
|
|
50
|
-
'http://www.example.com/fish',
|
|
51
|
-
'http://www.example.com/fish/test.html',
|
|
52
|
-
'http://www.example.com/Test.html'
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
var disallowed = [
|
|
56
|
-
'http://www.example.com/fish/index.php',
|
|
57
|
-
'http://www.example.com/fish/',
|
|
58
|
-
'http://www.example.com/test.html'
|
|
59
|
-
];
|
|
60
|
-
|
|
61
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should parse patterns', function () {
|
|
65
|
-
var contents = [
|
|
66
|
-
'User-agent: *',
|
|
67
|
-
'Disallow: /fish*.php',
|
|
68
|
-
'Disallow: /*.dext$'
|
|
69
|
-
].join('\n');
|
|
70
|
-
|
|
71
|
-
var allowed = [
|
|
72
|
-
'http://www.example.com/Fish.PHP',
|
|
73
|
-
'http://www.example.com/Fish.dext1'
|
|
74
|
-
];
|
|
75
|
-
|
|
76
|
-
var disallowed = [
|
|
77
|
-
'http://www.example.com/fish.php',
|
|
78
|
-
'http://www.example.com/fishheads/catfish.php?parameters',
|
|
79
|
-
'http://www.example.com/AnYthInG.dext',
|
|
80
|
-
'http://www.example.com/Fish.dext.dext'
|
|
81
|
-
];
|
|
82
|
-
|
|
83
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should have the correct order presidence for allow and disallow', function () {
|
|
87
|
-
var contents = [
|
|
88
|
-
'User-agent: *',
|
|
89
|
-
'Disallow: /fish*.php',
|
|
90
|
-
'Allow: /fish/index.php',
|
|
91
|
-
'Disallow: /test',
|
|
92
|
-
'Allow: /test/',
|
|
93
|
-
].join('\n');
|
|
94
|
-
|
|
95
|
-
var allowed = [
|
|
96
|
-
'http://www.example.com/test/index.html',
|
|
97
|
-
'http://www.example.com/test/'
|
|
98
|
-
];
|
|
99
|
-
|
|
100
|
-
var disallowed = [
|
|
101
|
-
'http://www.example.com/fish.php',
|
|
102
|
-
'http://www.example.com/fishheads/catfish.php?parameters',
|
|
103
|
-
'http://www.example.com/fish/index.php',
|
|
104
|
-
'http://www.example.com/test'
|
|
105
|
-
];
|
|
106
|
-
|
|
107
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should ignore rules that are not in a group', function () {
|
|
111
|
-
var contents = [
|
|
112
|
-
'Disallow: /secret.html',
|
|
113
|
-
'Disallow: /test',
|
|
114
|
-
].join('\n');
|
|
115
|
-
|
|
116
|
-
var allowed = [
|
|
117
|
-
'http://www.example.com/secret.html',
|
|
118
|
-
'http://www.example.com/test/index.html',
|
|
119
|
-
'http://www.example.com/test/'
|
|
120
|
-
];
|
|
121
|
-
|
|
122
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, []);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
it('should ignore comments', function () {
|
|
127
|
-
var contents = [
|
|
128
|
-
'#',
|
|
129
|
-
'# This is a comment',
|
|
130
|
-
'#',
|
|
131
|
-
'User-agent: *',
|
|
132
|
-
'# This is a comment',
|
|
133
|
-
'Disallow: /fish/ # ignore',
|
|
134
|
-
'# Disallow: fish',
|
|
135
|
-
'Disallow: /test.html'
|
|
136
|
-
].join('\n');
|
|
137
|
-
|
|
138
|
-
var allowed = [
|
|
139
|
-
'http://www.example.com/fish',
|
|
140
|
-
'http://www.example.com/Test.html'
|
|
141
|
-
];
|
|
142
|
-
|
|
143
|
-
var disallowed = [
|
|
144
|
-
'http://www.example.com/fish/index.php',
|
|
145
|
-
'http://www.example.com/fish/',
|
|
146
|
-
'http://www.example.com/test.html'
|
|
147
|
-
];
|
|
148
|
-
|
|
149
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('should ignore invalid lines', function () {
|
|
153
|
-
var contents = [
|
|
154
|
-
'invalid line',
|
|
155
|
-
'User-agent: *',
|
|
156
|
-
'Disallow: /fish/',
|
|
157
|
-
':::::another invalid line:::::',
|
|
158
|
-
'Disallow: /test.html',
|
|
159
|
-
'Unknown: tule'
|
|
160
|
-
].join('\n');
|
|
161
|
-
|
|
162
|
-
var allowed = [
|
|
163
|
-
'http://www.example.com/fish',
|
|
164
|
-
'http://www.example.com/Test.html'
|
|
165
|
-
];
|
|
166
|
-
|
|
167
|
-
var disallowed = [
|
|
168
|
-
'http://www.example.com/fish/index.php',
|
|
169
|
-
'http://www.example.com/fish/',
|
|
170
|
-
'http://www.example.com/test.html'
|
|
171
|
-
];
|
|
172
|
-
|
|
173
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should ignore empty user-agent lines', function () {
|
|
177
|
-
var contents = [
|
|
178
|
-
'User-agent:',
|
|
179
|
-
'Disallow: /fish/',
|
|
180
|
-
'Disallow: /test.html'
|
|
181
|
-
].join('\n');
|
|
182
|
-
|
|
183
|
-
var allowed = [
|
|
184
|
-
'http://www.example.com/fish',
|
|
185
|
-
'http://www.example.com/Test.html',
|
|
186
|
-
'http://www.example.com/fish/index.php',
|
|
187
|
-
'http://www.example.com/fish/',
|
|
188
|
-
'http://www.example.com/test.html'
|
|
189
|
-
];
|
|
190
|
-
|
|
191
|
-
var disallowed = [];
|
|
192
|
-
|
|
193
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('should support groups with multiple user agents (case insensitive)', function () {
|
|
197
|
-
var contents = [
|
|
198
|
-
'User-agent: agenta',
|
|
199
|
-
'User-agent: agentb',
|
|
200
|
-
'Disallow: /fish',
|
|
201
|
-
].join('\n');
|
|
202
|
-
|
|
203
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
204
|
-
|
|
205
|
-
expect(robots.isAllowed("http://www.example.com/fish", "agenta")).to.equal(false);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('should return undefined for invalid urls', function () {
|
|
209
|
-
var contents = [
|
|
210
|
-
'User-agent: *',
|
|
211
|
-
'Disallow: /secret.html',
|
|
212
|
-
'Disallow: /test',
|
|
213
|
-
].join('\n');
|
|
214
|
-
|
|
215
|
-
var invalidUrls = [
|
|
216
|
-
'http://example.com/secret.html',
|
|
217
|
-
'http://www.example.net/test/index.html',
|
|
218
|
-
'http://www.examsple.com/test/',
|
|
219
|
-
'example.com/test/',
|
|
220
|
-
':::::;;`\\|/.example.com/test/'
|
|
221
|
-
];
|
|
222
|
-
|
|
223
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
224
|
-
|
|
225
|
-
invalidUrls.forEach(function (url) {
|
|
226
|
-
expect(robots.isAllowed(url)).to.equal(undefined);
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it('should handle Unicode, urlencoded and punycode URLs', function () {
|
|
231
|
-
var contents = [
|
|
232
|
-
'User-agent: *',
|
|
233
|
-
'Disallow: /secret.html',
|
|
234
|
-
'Disallow: /test',
|
|
235
|
-
].join('\n');
|
|
236
|
-
|
|
237
|
-
var allowed = [
|
|
238
|
-
'http://www.münich.com/index.html',
|
|
239
|
-
'http://www.xn--mnich-kva.com/index.html',
|
|
240
|
-
'http://www.m%C3%BCnich.com/index.html'
|
|
241
|
-
];
|
|
242
|
-
|
|
243
|
-
var disallowed = [
|
|
244
|
-
'http://www.münich.com/secret.html',
|
|
245
|
-
'http://www.xn--mnich-kva.com/secret.html',
|
|
246
|
-
'http://www.m%C3%BCnich.com/secret.html'
|
|
247
|
-
];
|
|
248
|
-
|
|
249
|
-
testRobots('http://www.münich.com/robots.txt', contents, allowed, disallowed);
|
|
250
|
-
testRobots('http://www.xn--mnich-kva.com/robots.txt', contents, allowed, disallowed);
|
|
251
|
-
testRobots('http://www.m%C3%BCnich.com/robots.txt', contents, allowed, disallowed);
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
it('should handle Unicode and urlencoded paths', function () {
|
|
255
|
-
var contents = [
|
|
256
|
-
'User-agent: *',
|
|
257
|
-
'Disallow: /%CF%80',
|
|
258
|
-
'Disallow: /%e2%9d%83',
|
|
259
|
-
'Disallow: /%a%a',
|
|
260
|
-
'Disallow: /💩',
|
|
261
|
-
'Disallow: /✼*t$',
|
|
262
|
-
'Disallow: /%E2%9C%A4*t$',
|
|
263
|
-
'Disallow: /✿%a',
|
|
264
|
-
'Disallow: /http%3A%2F%2Fexample.org'
|
|
265
|
-
].join('\n');
|
|
266
|
-
|
|
267
|
-
var allowed = [
|
|
268
|
-
'http://www.example.com/✼testing',
|
|
269
|
-
'http://www.example.com/%E2%9C%BCtesting',
|
|
270
|
-
'http://www.example.com/✤testing',
|
|
271
|
-
'http://www.example.com/%E2%9C%A4testing',
|
|
272
|
-
'http://www.example.com/http://example.org',
|
|
273
|
-
'http://www.example.com/http:%2F%2Fexample.org'
|
|
274
|
-
];
|
|
275
|
-
|
|
276
|
-
var disallowed = [
|
|
277
|
-
'http://www.example.com/%CF%80',
|
|
278
|
-
'http://www.example.com/%CF%80/index.html',
|
|
279
|
-
'http://www.example.com/π',
|
|
280
|
-
'http://www.example.com/π/index.html',
|
|
281
|
-
'http://www.example.com/%e2%9d%83',
|
|
282
|
-
'http://www.example.com/%E2%9D%83/index.html',
|
|
283
|
-
'http://www.example.com/❃',
|
|
284
|
-
'http://www.example.com/❃/index.html',
|
|
285
|
-
'http://www.example.com/%F0%9F%92%A9',
|
|
286
|
-
'http://www.example.com/%F0%9F%92%A9/index.html',
|
|
287
|
-
'http://www.example.com/💩',
|
|
288
|
-
'http://www.example.com/💩/index.html',
|
|
289
|
-
'http://www.example.com/%a%a',
|
|
290
|
-
'http://www.example.com/%a%a/index.html',
|
|
291
|
-
'http://www.example.com/✼test',
|
|
292
|
-
'http://www.example.com/%E2%9C%BCtest',
|
|
293
|
-
'http://www.example.com/✤test',
|
|
294
|
-
'http://www.example.com/%E2%9C%A4testt',
|
|
295
|
-
'http://www.example.com/✿%a',
|
|
296
|
-
'http://www.example.com/%E2%9C%BF%atest',
|
|
297
|
-
'http://www.example.com/http%3A%2F%2Fexample.org'
|
|
298
|
-
];
|
|
299
|
-
|
|
300
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
it('should handle lone high / low surrogates', function () {
|
|
304
|
-
var contents = [
|
|
305
|
-
'User-agent: *',
|
|
306
|
-
'Disallow: /\uD800',
|
|
307
|
-
'Disallow: /\uDC00'
|
|
308
|
-
].join('\n');
|
|
309
|
-
|
|
310
|
-
// These are invalid so can't be disallowed
|
|
311
|
-
var allowed = [
|
|
312
|
-
'http://www.example.com/\uDC00',
|
|
313
|
-
'http://www.example.com/\uD800'
|
|
314
|
-
];
|
|
315
|
-
|
|
316
|
-
var disallowed = [];
|
|
317
|
-
|
|
318
|
-
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
it('should ignore host case', function () {
|
|
322
|
-
var contents = [
|
|
323
|
-
'User-agent: *',
|
|
324
|
-
'Disallow: /secret.html',
|
|
325
|
-
'Disallow: /test',
|
|
326
|
-
].join('\n');
|
|
327
|
-
|
|
328
|
-
var allowed = [
|
|
329
|
-
'http://www.example.com/index.html',
|
|
330
|
-
'http://www.ExAmPlE.com/index.html',
|
|
331
|
-
'http://www.EXAMPLE.com/index.html'
|
|
332
|
-
];
|
|
333
|
-
|
|
334
|
-
var disallowed = [
|
|
335
|
-
'http://www.example.com/secret.html',
|
|
336
|
-
'http://www.ExAmPlE.com/secret.html',
|
|
337
|
-
'http://www.EXAMPLE.com/secret.html'
|
|
338
|
-
];
|
|
339
|
-
|
|
340
|
-
testRobots('http://www.eXample.com/robots.txt', contents, allowed, disallowed);
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it('should allow all if empty robots.txt', function () {
|
|
344
|
-
var allowed = [
|
|
345
|
-
'http://www.example.com/secret.html',
|
|
346
|
-
'http://www.example.com/test/index.html',
|
|
347
|
-
'http://www.example.com/test/'
|
|
348
|
-
];
|
|
349
|
-
|
|
350
|
-
var robots = robotsParser('http://www.example.com/robots.txt', '');
|
|
351
|
-
|
|
352
|
-
allowed.forEach(function (url) {
|
|
353
|
-
expect(robots.isAllowed(url)).to.equal(true);
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
it('should treat null as allowing all', function () {
|
|
358
|
-
var robots = robotsParser('http://www.example.com/robots.txt', null);
|
|
359
|
-
|
|
360
|
-
expect(robots.isAllowed("http://www.example.com/", "userAgent")).to.equal(true);
|
|
361
|
-
expect(robots.isAllowed("http://www.example.com/")).to.equal(true);
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
it('should handle invalid robots.txt urls', function () {
|
|
365
|
-
var contents = [
|
|
366
|
-
'user-agent: *',
|
|
367
|
-
'disallow: /',
|
|
368
|
-
|
|
369
|
-
'host: www.example.com',
|
|
370
|
-
'sitemap: /sitemap.xml'
|
|
371
|
-
].join('\n');
|
|
372
|
-
|
|
373
|
-
var sitemapUrls = [
|
|
374
|
-
undefined,
|
|
375
|
-
null,
|
|
376
|
-
'null',
|
|
377
|
-
':/wom/test/'
|
|
378
|
-
];
|
|
379
|
-
|
|
380
|
-
sitemapUrls.forEach(function (url) {
|
|
381
|
-
var robots = robotsParser(url, contents);
|
|
382
|
-
expect(robots.isAllowed('http://www.example.com/index.html')).to.equal(undefined);
|
|
383
|
-
expect(robots.getPreferredHost()).to.equal('www.example.com');
|
|
384
|
-
expect(robots.getSitemaps()).to.eql(['/sitemap.xml']);
|
|
385
|
-
});
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
it('should parse the crawl-delay directive', function () {
|
|
389
|
-
var contents = [
|
|
390
|
-
'user-agent: a',
|
|
391
|
-
'crawl-delay: 1',
|
|
392
|
-
|
|
393
|
-
'user-agent: b',
|
|
394
|
-
'disallow: /d',
|
|
395
|
-
|
|
396
|
-
'user-agent: c',
|
|
397
|
-
'user-agent: d',
|
|
398
|
-
'crawl-delay: 10'
|
|
399
|
-
].join('\n');
|
|
400
|
-
|
|
401
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
402
|
-
|
|
403
|
-
expect(robots.getCrawlDelay('a')).to.equal(1);
|
|
404
|
-
expect(robots.getCrawlDelay('b')).to.equal(undefined);
|
|
405
|
-
expect(robots.getCrawlDelay('c')).to.equal(10);
|
|
406
|
-
expect(robots.getCrawlDelay('d')).to.equal(10);
|
|
407
|
-
expect(robots.getCrawlDelay()).to.equal(undefined);
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
it('should ignore invalid crawl-delay directives', function () {
|
|
411
|
-
var contents = [
|
|
412
|
-
'user-agent: a',
|
|
413
|
-
'crawl-delay: 1.2.1',
|
|
414
|
-
|
|
415
|
-
'user-agent: b',
|
|
416
|
-
'crawl-delay: 1.a0',
|
|
417
|
-
|
|
418
|
-
'user-agent: c',
|
|
419
|
-
'user-agent: d',
|
|
420
|
-
'crawl-delay: 10a'
|
|
421
|
-
].join('\n');
|
|
422
|
-
|
|
423
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
424
|
-
|
|
425
|
-
expect(robots.getCrawlDelay('a')).to.equal(undefined);
|
|
426
|
-
expect(robots.getCrawlDelay('b')).to.equal(undefined);
|
|
427
|
-
expect(robots.getCrawlDelay('c')).to.equal(undefined);
|
|
428
|
-
expect(robots.getCrawlDelay('d')).to.equal(undefined);
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
it('should parse the sitemap directive', function () {
|
|
432
|
-
var contents = [
|
|
433
|
-
'user-agent: a',
|
|
434
|
-
'crawl-delay: 1',
|
|
435
|
-
'sitemap: http://example.com/test.xml',
|
|
436
|
-
|
|
437
|
-
'user-agent: b',
|
|
438
|
-
'disallow: /d',
|
|
439
|
-
|
|
440
|
-
'sitemap: /sitemap.xml',
|
|
441
|
-
'sitemap: http://example.com/test/sitemap.xml '
|
|
442
|
-
].join('\n');
|
|
443
|
-
|
|
444
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
445
|
-
|
|
446
|
-
expect(robots.getSitemaps()).to.eql([
|
|
447
|
-
'http://example.com/test.xml',
|
|
448
|
-
'/sitemap.xml',
|
|
449
|
-
'http://example.com/test/sitemap.xml'
|
|
450
|
-
]);
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
it('should parse the host directive', function () {
|
|
454
|
-
var contents = [
|
|
455
|
-
'user-agent: a',
|
|
456
|
-
'crawl-delay: 1',
|
|
457
|
-
'host: www.example.net',
|
|
458
|
-
|
|
459
|
-
'user-agent: b',
|
|
460
|
-
'disallow: /d',
|
|
461
|
-
|
|
462
|
-
'host: example.com'
|
|
463
|
-
].join('\n');
|
|
464
|
-
|
|
465
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
466
|
-
|
|
467
|
-
expect(robots.getPreferredHost()).to.equal('example.com');
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
it('should parse empty and invalid directives', function () {
|
|
471
|
-
var contents = [
|
|
472
|
-
'user-agent:',
|
|
473
|
-
'user-agent:::: a::',
|
|
474
|
-
'crawl-delay:',
|
|
475
|
-
'crawl-delay:::: 0:',
|
|
476
|
-
'host:',
|
|
477
|
-
'host:: example.com',
|
|
478
|
-
'sitemap:',
|
|
479
|
-
'sitemap:: site:map.xml',
|
|
480
|
-
'disallow:',
|
|
481
|
-
'disallow::: /:',
|
|
482
|
-
'allow:',
|
|
483
|
-
'allow::: /:',
|
|
484
|
-
].join('\n');
|
|
485
|
-
|
|
486
|
-
robotsParser('http://www.example.com/robots.txt', contents);
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
it('should treat only the last host directive as valid', function () {
|
|
490
|
-
var contents = [
|
|
491
|
-
'user-agent: a',
|
|
492
|
-
'crawl-delay: 1',
|
|
493
|
-
'host: www.example.net',
|
|
494
|
-
|
|
495
|
-
'user-agent: b',
|
|
496
|
-
'disallow: /d',
|
|
497
|
-
|
|
498
|
-
'host: example.net',
|
|
499
|
-
'host: example.com'
|
|
500
|
-
].join('\n');
|
|
501
|
-
|
|
502
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
503
|
-
|
|
504
|
-
expect(robots.getPreferredHost()).to.equal('example.com');
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
it('should return null when there is no host directive', function () {
|
|
508
|
-
var contents = [
|
|
509
|
-
'user-agent: a',
|
|
510
|
-
'crawl-delay: 1',
|
|
511
|
-
|
|
512
|
-
'user-agent: b',
|
|
513
|
-
'disallow: /d',
|
|
514
|
-
].join('\n');
|
|
515
|
-
|
|
516
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
517
|
-
|
|
518
|
-
expect(robots.getPreferredHost()).to.equal(null);
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
it('should fallback to * when a UA has no rules of its own', function () {
|
|
522
|
-
var contents = [
|
|
523
|
-
'user-agent: *',
|
|
524
|
-
'crawl-delay: 1',
|
|
525
|
-
|
|
526
|
-
'user-agent: b',
|
|
527
|
-
'crawl-delay: 12',
|
|
528
|
-
|
|
529
|
-
'user-agent: c',
|
|
530
|
-
'user-agent: d',
|
|
531
|
-
'crawl-delay: 10'
|
|
532
|
-
].join('\n');
|
|
533
|
-
|
|
534
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
535
|
-
|
|
536
|
-
expect(robots.getCrawlDelay('should-fall-back')).to.equal(1);
|
|
537
|
-
expect(robots.getCrawlDelay('d')).to.equal(10);
|
|
538
|
-
expect(robots.getCrawlDelay('dd')).to.equal(1);
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
it('should not fallback to * when a UA has rules', function () {
|
|
542
|
-
var contents = [
|
|
543
|
-
'user-agent: *',
|
|
544
|
-
'crawl-delay: 1',
|
|
545
|
-
|
|
546
|
-
'user-agent: b',
|
|
547
|
-
'disallow:'
|
|
548
|
-
].join('\n');
|
|
549
|
-
|
|
550
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
551
|
-
|
|
552
|
-
expect(robots.getCrawlDelay('b')).to.equal(undefined);
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
it('should ignore version numbers in the UA string', function () {
|
|
556
|
-
var contents = [
|
|
557
|
-
'user-agent: *',
|
|
558
|
-
'crawl-delay: 1',
|
|
559
|
-
|
|
560
|
-
'user-agent: b',
|
|
561
|
-
'crawl-delay: 12',
|
|
562
|
-
|
|
563
|
-
'user-agent: c',
|
|
564
|
-
'user-agent: d',
|
|
565
|
-
'crawl-delay: 10'
|
|
566
|
-
].join('\n');
|
|
567
|
-
|
|
568
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
569
|
-
|
|
570
|
-
expect(robots.getCrawlDelay('should-fall-back/1.0.0')).to.equal(1);
|
|
571
|
-
expect(robots.getCrawlDelay('d/12')).to.equal(10);
|
|
572
|
-
expect(robots.getCrawlDelay('dd / 0-32-3')).to.equal(1);
|
|
573
|
-
expect(robots.getCrawlDelay('b / 1.0')).to.equal(12);
|
|
574
|
-
});
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
it('should return the line number of the matching directive', function () {
|
|
578
|
-
var contents = [
|
|
579
|
-
'',
|
|
580
|
-
'User-agent: *',
|
|
581
|
-
'',
|
|
582
|
-
'Disallow: /fish/',
|
|
583
|
-
'Disallow: /test.html',
|
|
584
|
-
'Allow: /fish/test.html',
|
|
585
|
-
'Allow: /test.html',
|
|
586
|
-
'',
|
|
587
|
-
'User-agent: a',
|
|
588
|
-
'allow: /',
|
|
589
|
-
'',
|
|
590
|
-
'User-agent: b',
|
|
591
|
-
'disallow: /test',
|
|
592
|
-
'disallow: /t*t',
|
|
593
|
-
// check UA returns -1 if no matching UA and also handles patterns both allow and disaloow
|
|
594
|
-
].join('\n');
|
|
595
|
-
|
|
596
|
-
var robots = robotsParser('http://www.example.com/robots.txt', contents);
|
|
597
|
-
|
|
598
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/fish')).to.equal(-1);
|
|
599
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/fish/test.html')).to.equal(6);
|
|
600
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/Test.html')).to.equal(-1);
|
|
601
|
-
|
|
602
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/fish/index.php')).to.equal(4);
|
|
603
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/fish/')).to.equal(4);
|
|
604
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/test.html')).to.equal(5);
|
|
605
|
-
|
|
606
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/test.html', 'a')).to.equal(10);
|
|
607
|
-
expect(robots.getMatchingLineNumber('http://www.example.com/test.html', 'b')).to.equal(14);
|
|
608
|
-
});
|
|
609
|
-
});
|
|
610
|
-
|