@hhkaos/webmentions-widget 0.5.0 → 0.5.2
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/bin/snapshot.js +31 -9
- package/package.json +1 -1
package/bin/snapshot.js
CHANGED
|
@@ -17,16 +17,18 @@
|
|
|
17
17
|
|
|
18
18
|
import {readFile, writeFile, mkdir} from 'node:fs/promises';
|
|
19
19
|
import {dirname} from 'node:path';
|
|
20
|
+
import {pathToFileURL} from 'node:url';
|
|
20
21
|
|
|
21
22
|
import {getSnapshotMentions, mergeSnapshot, parseWebmentionJson} from '../src/core.js';
|
|
22
23
|
|
|
23
24
|
const DEFAULT_API = 'https://webmention.io/api/mentions.jf2';
|
|
24
25
|
const PER_PAGE = 100;
|
|
25
26
|
const PAGE_DELAY_MS = 500;
|
|
26
|
-
const RETRIES =
|
|
27
|
-
const RETRY_DELAY_MS =
|
|
27
|
+
const RETRIES = 8;
|
|
28
|
+
const RETRY_DELAY_MS = 3000;
|
|
29
|
+
const MAX_RETRY_DELAY_MS = 30000;
|
|
28
30
|
|
|
29
|
-
function parseArgs(argv) {
|
|
31
|
+
export function parseArgs(argv) {
|
|
30
32
|
const args = {};
|
|
31
33
|
|
|
32
34
|
argv.forEach((arg, index) => {
|
|
@@ -35,7 +37,18 @@ function parseArgs(argv) {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
const [flag, inline] = arg.slice(2).split('=');
|
|
38
|
-
|
|
40
|
+
|
|
41
|
+
if (inline !== undefined) {
|
|
42
|
+
args[flag] = inline;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const next = argv[index + 1];
|
|
47
|
+
|
|
48
|
+
// A flag is boolean when nothing follows it, or when what follows is
|
|
49
|
+
// another flag. `--full` as the last argument is the common case, and
|
|
50
|
+
// reading it as its (missing) value silently disabled it.
|
|
51
|
+
args[flag] = next === undefined || next.startsWith('--') ? true : next;
|
|
39
52
|
});
|
|
40
53
|
|
|
41
54
|
return args;
|
|
@@ -61,7 +74,13 @@ async function fetchPage(url) {
|
|
|
61
74
|
|
|
62
75
|
for (let attempt = 0; attempt <= RETRIES; attempt += 1) {
|
|
63
76
|
if (attempt > 0) {
|
|
64
|
-
|
|
77
|
+
// Capped exponential backoff with jitter. webmention.io has spent today
|
|
78
|
+
// failing roughly two requests in three, so a job that gives up after
|
|
79
|
+
// half a minute loses the coin flip often enough to skip whole days; a
|
|
80
|
+
// daily job can afford to be patient for a couple of minutes instead.
|
|
81
|
+
const backoff = Math.min(RETRY_DELAY_MS * 2 ** (attempt - 1), MAX_RETRY_DELAY_MS);
|
|
82
|
+
|
|
83
|
+
await wait(backoff + Math.random() * 1000);
|
|
65
84
|
}
|
|
66
85
|
|
|
67
86
|
try {
|
|
@@ -163,7 +182,10 @@ async function main() {
|
|
|
163
182
|
console.log(`Snapshot updated: ${before} → ${snapshot.count} mentions (lastId ${snapshot.lastId}).`);
|
|
164
183
|
}
|
|
165
184
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
185
|
+
// Only run when invoked as a command, so the helpers above stay testable.
|
|
186
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
187
|
+
main().catch((error) => {
|
|
188
|
+
console.error(error);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
});
|
|
191
|
+
}
|
package/package.json
CHANGED