Skip to content

Commit e3da0f8

Browse files
authored
Merge pull request #102 from 0x6b/fix-96
feat: add `preferGET` option
2 parents 8aabe2d + 270c432 commit e3da0f8

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

ReadMe.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ The default options are:
6262
"no-dead-link": {
6363
"checkRelative": true,
6464
"baseURI": null,
65-
"ignore": []
65+
"ignore": [],
66+
"preferGET": []
6667
}
6768
}
6869
}
@@ -107,6 +108,22 @@ Example:
107108
}
108109
```
109110

111+
### preferGET
112+
113+
An array of [origins](https://url.spec.whatwg.org/#origin) to lets the rule connect to the origin's URL by `GET` instead of default `HEAD` request.
114+
115+
Although the rule will fall back to `GET` method when `HEAD` request is failed (status code is not between 200 and 300), in order to shorten time to run your test, you can use this option when you are sure that target origin always returns 5xx for `HEAD` request.
116+
117+
Example:
118+
119+
```json
120+
"no-dead-link": {
121+
"preferGET": [
122+
"http://example.com"
123+
]
124+
}
125+
```
126+
110127
## Tests
111128

112129
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"repository": "textlint-rule/textlint-rule-no-dead-link",
2525
"dependencies": {
2626
"fs-extra": "^5.0.0",
27+
"get-url-origin": "^1.0.1",
2728
"isomorphic-fetch": "^2.2.1",
2829
"textlint-rule-helper": "^2.0.0"
2930
},

src/no-dead-link.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import fetch from 'isomorphic-fetch';
33
import URL from 'url';
44
import fs from 'fs-extra';
55
import { isAbsolute } from 'path';
6+
import { getURLOrigin } from 'get-url-origin';
67

78
const DEFAULT_OPTIONS = {
89
checkRelative: true, // {boolean} `false` disables the checks for relative URIs
910
baseURI: null, // {String|null} a base URI to resolve relative URIs.
1011
ignore: [], // {Array<String>} URIs to be skipped from availability checks.
12+
preferGET: [], // {Array<String>} origins to prefer GET over HEAD.
1113
};
1214

1315
// Adopted from http://stackoverflow.com/a/3809435/951517
@@ -84,6 +86,10 @@ async function isAliveURI(uri, method = 'HEAD') {
8486
};
8587
}
8688

89+
if (!res.ok && method === 'HEAD') {
90+
return isAliveURI(uri, 'GET');
91+
}
92+
8793
return {
8894
ok: res.ok,
8995
message: `${res.status} ${res.statusText}`,
@@ -156,9 +162,16 @@ function reporter(context, options = {}) {
156162
uri = URL.resolve(base, uri);
157163
}
158164

165+
const method =
166+
opts.preferGET.filter(
167+
(origin) => getURLOrigin(uri) === getURLOrigin(origin),
168+
).length > 0
169+
? 'GET'
170+
: 'HEAD';
171+
159172
const result = isLocal(uri)
160173
? await isAliveLocalFile(uri)
161-
: await isAliveURI(uri);
174+
: await isAliveURI(uri, method);
162175
const { ok, redirected, redirectTo, message } = result;
163176

164177
if (!ok) {

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
--require babel-core/register
2-
--timeout 10000
2+
--timeout 20000

test/no-dead-link.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tester.run('no-dead-link', rule, {
1010
valid: [
1111
'should be able to check a link in Markdown: [example](https://example.com/)',
1212
'should be able to check a URL in Markdown: https://example.com/',
13+
'should success with retrying on error: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',
1314
'should treat 200 OK as alive: https://httpstat.us/200',
1415
{
1516
text:
@@ -57,6 +58,20 @@ tester.run('no-dead-link', rule, {
5758
{
5859
inputPath: path.join(__dirname, 'fixtures/a.md'),
5960
},
61+
{
62+
text:
63+
'should success with GET method: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',
64+
options: {
65+
preferGET: ['https://www.npmjs.com'],
66+
},
67+
},
68+
{
69+
text:
70+
'should success with GET method whether the option is specific URL: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',
71+
options: {
72+
preferGET: ['https://www.npmjs.com/search?q=textlint-rule'],
73+
},
74+
},
6075
],
6176
invalid: [
6277
{

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,10 @@ get-stream@^3.0.0:
15231523
version "3.0.0"
15241524
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
15251525

1526+
get-url-origin@^1.0.1:
1527+
version "1.0.1"
1528+
resolved "https://registry.yarnpkg.com/get-url-origin/-/get-url-origin-1.0.1.tgz#edebfcc085433e84c6b32a5738b0996edfd5a7b9"
1529+
15261530
getpass@^0.1.1:
15271531
version "0.1.6"
15281532
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.6.tgz#283ffd9fc1256840875311c1b60e8c40187110e6"

0 commit comments

Comments
 (0)