Skip to content

Commit

Permalink
refactor(rule): use p-queue instead of p-all
Browse files Browse the repository at this point in the history
- add interval option as experimental
  • Loading branch information
azu committed Sep 7, 2019
1 parent 789a275 commit 9ff2f0f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
4 changes: 3 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ The default options are:
"ignore": [],
"preferGET": [],
"ignoreRedirects": false,
"concurrency": 8,
"retry": 3
}
}
Expand Down Expand Up @@ -134,11 +133,14 @@ Example:
This rule checks for redirects (3xx status codes) and consider's them an error by default.
To ignore redirects during checks, set this value to `false`.

<!-- Experimental
### concurrency
This rule checks links concurrently.
The default concurrency count is `8`.
-->
### retry

This rule checks the url with retry.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"get-url-origin": "^1.0.1",
"minimatch": "^3.0.4",
"node-fetch": "^2.6.0",
"p-all": "^2.1.0",
"p-memoize": "^3.1.0",
"p-queue": "^6.1.1",
"textlint-rule-helper": "^2.1.1"
},
"devDependencies": {
Expand Down
15 changes: 10 additions & 5 deletions src/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import minimatch from 'minimatch';
import { isAbsolute } from 'path';
import { getURLOrigin } from 'get-url-origin';
import pMemoize from 'p-memoize';
import pAll from 'p-all';
import PQueue from 'p-queue';
import * as http from 'http';
import * as https from 'https';

Expand All @@ -15,9 +15,11 @@ const DEFAULT_OPTIONS = {
baseURI: null, // {String|null} a base URI to resolve relative URIs.
ignore: [], // {Array<String>} URIs to be skipped from availability checks.
preferGET: [], // {Array<String>} origins to prefer GET over HEAD.
concurrency: 2, // {number} Concurrency count of linting link
retry: 3, // {number} Max retry count
keepAlive: true, // {boolean} if it is true, use keepAlive for checking request [experimental]
concurrency: 8, // {number} Concurrency count of linting link [Experimental]
interval: 500, // The length of time in milliseconds before the interval count resets. Must be finite. [Experimental]
intervalCap: 8, // The max number of runs in the given interval of time. [Experimental]
keepAlive: false, // {boolean} if it is true, use keepAlive for checking request [Experimental]
};

// Adopted from http://stackoverflow.com/a/3809435/951517
Expand Down Expand Up @@ -338,10 +340,13 @@ function reporter(context, options = {}) {
},

[`${context.Syntax.Document}:exit`]() {
const linkTasks = URIs.map((item) => () => lint(item, opts.retry));
return pAll(linkTasks, {
const queue = new PQueue({
concurrency: opts.concurrency,
intervalCap: opts.intervalCap,
interval: opts.interval
});
const linkTasks = URIs.map((item) => () => lint(item, opts.retry));
return queue.addAll(linkTasks);
},
};
}
Expand Down
1 change: 0 additions & 1 deletion test/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ tester.run('no-dead-link', rule, {
'should ignore non-http url [ftp](ftp://example.com) by default',
'should ignore non-http url [websockets](ws://example.com) by default',
'should be able to check a link in Markdown: [example](https://example.com/)',
'should be able to check a link in Markdown: [example](https://jcp.org/en/jsr/detail?id=330)',
'should be able to check a link in Markdown: [example](https://dev.mysql.com/downloads/mysql/)',
'should be able to check a URL in Markdown: https://example.com/',
'should success with retrying on error: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',
Expand Down
27 changes: 20 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,11 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

eventemitter3@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.0.tgz#d65176163887ee59f386d64c82610b696a4a74eb"
integrity sha512-qerSRB0p+UDEssxTtm6EDKcE7W4OaoisfIMl4CngyEhjpYglocpNg6UEqCvemdGhosAsg4sO2dXJOdyBifPGCg==

events@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
Expand Down Expand Up @@ -3701,13 +3706,6 @@ output-file-sync@^2.0.0:
is-plain-obj "^1.1.0"
mkdirp "^0.5.1"

p-all@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/p-all/-/p-all-2.1.0.tgz#91419be56b7dee8fe4c5db875d55e0da084244a0"
integrity sha512-HbZxz5FONzz/z2gJfk6bFca0BCiSRF8jU3yCsWOen/vR6lZjfPOu/e7L3uFzTW1i0H8TlC3vqQstEJPQL4/uLA==
dependencies:
p-map "^2.0.0"

p-defer@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c"
Expand Down Expand Up @@ -3783,6 +3781,21 @@ p-memoize@^3.1.0:
mem "^4.3.0"
mimic-fn "^2.1.0"

p-queue@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-6.1.1.tgz#eedbaf7335b4931ef857e2e58063fed461b97e80"
integrity sha512-R9gq36Th88xZ+rWAptN5IXLwqkwA1gagCQhT6ZXQ6RxEfmjb9ZW+UBzRVqv9sm5TQmbbI/TsKgGLbOaA61xR5w==
dependencies:
eventemitter3 "^4.0.0"
p-timeout "^3.1.0"

p-timeout@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-3.1.0.tgz#198c1f503bb973e9b9727177a276c80afd6851f3"
integrity sha512-C27DYI+tCroT8J8cTEyySGydl2B7FlxrGNF5/wmMbl1V+jeehUCzEE/BVgzRebdm2K3ZitKOKx8YbdFumDyYmw==
dependencies:
p-finally "^1.0.0"

p-try@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-1.0.0.tgz#cbc79cdbaf8fd4228e13f621f2b1a237c1b207b3"
Expand Down

0 comments on commit 9ff2f0f

Please sign in to comment.