tt-help-cli-ycl 1.3.6 → 1.3.7

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.
Files changed (46) hide show
  1. package/README.md +17 -17
  2. package/cli.js +9 -9
  3. package/package.json +45 -45
  4. package/src/cli/auto.js +131 -121
  5. package/src/cli/explore.js +147 -138
  6. package/src/cli/progress.js +111 -111
  7. package/src/cli/scrape.js +47 -47
  8. package/src/cli/utils.js +18 -18
  9. package/src/cli/videos.js +41 -41
  10. package/src/cli/watch.js +31 -31
  11. package/src/lib/args.js +391 -391
  12. package/src/lib/browser/anti-detect.js +23 -23
  13. package/src/lib/browser/cdp.js +142 -142
  14. package/src/lib/browser/launch.js +43 -43
  15. package/src/lib/browser/page.js +87 -87
  16. package/src/lib/constants.js +109 -95
  17. package/src/lib/delay.js +54 -54
  18. package/src/lib/explore-fetch.js +118 -118
  19. package/src/lib/fetcher.js +45 -45
  20. package/src/lib/filter.js +66 -66
  21. package/src/lib/io.js +54 -54
  22. package/src/lib/mac-or-uuid.js +82 -0
  23. package/src/lib/output.js +80 -80
  24. package/src/lib/parser.js +47 -47
  25. package/src/lib/retry.js +44 -44
  26. package/src/lib/scrape.js +40 -40
  27. package/src/lib/url.js +52 -52
  28. package/src/main.mjs +221 -221
  29. package/src/scraper/auto-core.mjs +185 -185
  30. package/src/scraper/core.mjs +190 -190
  31. package/src/scraper/explore-core.mjs +162 -162
  32. package/src/scraper/modules/captcha-handler.mjs +114 -114
  33. package/src/scraper/modules/comment-extractor.mjs +69 -69
  34. package/src/scraper/modules/follow-extractor.mjs +121 -121
  35. package/src/scraper/modules/guess-extractor.mjs +51 -51
  36. package/src/scraper/modules/page-error-detector.mjs +70 -70
  37. package/src/scraper/modules/page-helpers.mjs +48 -48
  38. package/src/scraper/modules/scroll-collector.mjs +189 -189
  39. package/src/test-auto-follow.cjs +109 -0
  40. package/src/test-extractors.cjs +75 -0
  41. package/src/test-follow.cjs +41 -0
  42. package/src/videos/core.mjs +126 -126
  43. package/src/watch/data-store.mjs +258 -261
  44. package/src/watch/public/index.html +466 -465
  45. package/src/watch/server.mjs +291 -281
  46. package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
package/src/lib/filter.js CHANGED
@@ -1,66 +1,66 @@
1
- export function parseFilter(filterStr) {
2
- if (!filterStr) return null;
3
-
4
- const filter = {};
5
- const pairs = filterStr.split('&');
6
-
7
- for (const pair of pairs) {
8
- const [key, value] = pair.split('=');
9
- if (!key || value === undefined) continue;
10
-
11
- const trimmedKey = key.trim();
12
- const trimmedValue = value.trim();
13
-
14
- // 处理布尔值
15
- if (trimmedValue === 'true') {
16
- filter[trimmedKey] = true;
17
- } else if (trimmedValue === 'false') {
18
- filter[trimmedKey] = false;
19
- } else {
20
- // 支持逗号分隔的多个值(如 locationCreated=DE,ES)
21
- filter[trimmedKey] = trimmedValue.split(',').map(v => v.trim());
22
- }
23
- }
24
-
25
- return Object.keys(filter).length > 0 ? filter : null;
26
- }
27
-
28
- export function applyFilter(results, filter) {
29
- if (!filter || results.length === 0) return results;
30
-
31
- return results.filter(item => {
32
- for (const [key, expectedValue] of Object.entries(filter)) {
33
- const actualValue = item[key];
34
-
35
- // 如果字段不存在,过滤掉
36
- if (actualValue === undefined || actualValue === null) {
37
- return false;
38
- }
39
-
40
- // 数组值匹配(如 locationCreated=DE,ES)
41
- if (Array.isArray(expectedValue)) {
42
- if (!expectedValue.includes(String(actualValue))) {
43
- return false;
44
- }
45
- }
46
- // 布尔值或精确匹配
47
- else if (actualValue !== expectedValue) {
48
- return false;
49
- }
50
- }
51
- return true;
52
- });
53
- }
54
-
55
- export function formatFilterDescription(filter) {
56
- if (!filter) return '';
57
-
58
- const parts = Object.entries(filter).map(([key, value]) => {
59
- if (Array.isArray(value)) {
60
- return `${key}=${value.join(',')}`;
61
- }
62
- return `${key}=${value}`;
63
- });
64
-
65
- return parts.join(' & ');
66
- }
1
+ export function parseFilter(filterStr) {
2
+ if (!filterStr) return null;
3
+
4
+ const filter = {};
5
+ const pairs = filterStr.split('&');
6
+
7
+ for (const pair of pairs) {
8
+ const [key, value] = pair.split('=');
9
+ if (!key || value === undefined) continue;
10
+
11
+ const trimmedKey = key.trim();
12
+ const trimmedValue = value.trim();
13
+
14
+ // 处理布尔值
15
+ if (trimmedValue === 'true') {
16
+ filter[trimmedKey] = true;
17
+ } else if (trimmedValue === 'false') {
18
+ filter[trimmedKey] = false;
19
+ } else {
20
+ // 支持逗号分隔的多个值(如 locationCreated=DE,ES)
21
+ filter[trimmedKey] = trimmedValue.split(',').map(v => v.trim());
22
+ }
23
+ }
24
+
25
+ return Object.keys(filter).length > 0 ? filter : null;
26
+ }
27
+
28
+ export function applyFilter(results, filter) {
29
+ if (!filter || results.length === 0) return results;
30
+
31
+ return results.filter(item => {
32
+ for (const [key, expectedValue] of Object.entries(filter)) {
33
+ const actualValue = item[key];
34
+
35
+ // 如果字段不存在,过滤掉
36
+ if (actualValue === undefined || actualValue === null) {
37
+ return false;
38
+ }
39
+
40
+ // 数组值匹配(如 locationCreated=DE,ES)
41
+ if (Array.isArray(expectedValue)) {
42
+ if (!expectedValue.includes(String(actualValue))) {
43
+ return false;
44
+ }
45
+ }
46
+ // 布尔值或精确匹配
47
+ else if (actualValue !== expectedValue) {
48
+ return false;
49
+ }
50
+ }
51
+ return true;
52
+ });
53
+ }
54
+
55
+ export function formatFilterDescription(filter) {
56
+ if (!filter) return '';
57
+
58
+ const parts = Object.entries(filter).map(([key, value]) => {
59
+ if (Array.isArray(value)) {
60
+ return `${key}=${value.join(',')}`;
61
+ }
62
+ return `${key}=${value}`;
63
+ });
64
+
65
+ return parts.join(' & ');
66
+ }
package/src/lib/io.js CHANGED
@@ -1,54 +1,54 @@
1
- import { extractDisplayPath } from './url.js';
2
-
3
- let lastBarCount = 0;
4
-
5
- export function createProgressBar(current, total, maxWidth = 30) {
6
- const filled = Math.round((current / total) * maxWidth);
7
- return '█'.repeat(filled).padEnd(maxWidth);
8
- }
9
-
10
- export function calculateConcurrency(total) {
11
- return Math.min(5, Math.max(1, Math.floor(total / 10)), total);
12
- }
13
-
14
- export function createMultiProgressBars(count) {
15
- return Array.from({ length: count }, () => ({
16
- current: 0,
17
- total: 0,
18
- status: 'pending',
19
- url: '',
20
- }));
21
- }
22
-
23
- export function renderMultiProgressBars(bars, maxWidth = 30) {
24
- const activeBars = bars.filter(bar => bar.total > 0);
25
-
26
- if (activeBars.length === 0) return;
27
-
28
- const lines = activeBars.map((bar) => {
29
- const prog = createProgressBar(bar.current, bar.total, maxWidth);
30
- const icon = bar.status === 'done' ? '✓' :
31
- bar.status === 'error' ? '' : '⟳';
32
- const urlDisplay = bar.url ? extractDisplayPath(bar.url) : '';
33
- return ` [${prog}] ${bar.current}/${bar.total} ${icon} ${urlDisplay}`;
34
- });
35
-
36
- const output = lines.join('\n');
37
-
38
- if (lastBarCount > 0) {
39
- process.stdout.write(`\x1b[${lastBarCount}A`);
40
- }
41
-
42
- process.stdout.write('\x1b[0J');
43
- process.stdout.write(output + '\n');
44
-
45
- lastBarCount = activeBars.length;
46
- }
47
-
48
- export function clearProgressBars() {
49
- if (lastBarCount > 0) {
50
- process.stdout.write(`\x1b[${lastBarCount}A`);
51
- process.stdout.write('\x1b[0J');
52
- lastBarCount = 0;
53
- }
54
- }
1
+ import { extractDisplayPath } from './url.js';
2
+
3
+ let lastBarCount = 0;
4
+
5
+ export function createProgressBar(current, total, maxWidth = 30) {
6
+ const filled = Math.round((current / total) * maxWidth);
7
+ return '█'.repeat(filled).padEnd(maxWidth);
8
+ }
9
+
10
+ export function calculateConcurrency(total) {
11
+ return Math.min(5, Math.max(1, Math.floor(total / 10)), total);
12
+ }
13
+
14
+ export function createMultiProgressBars(count) {
15
+ return Array.from({ length: count }, () => ({
16
+ current: 0,
17
+ total: 0,
18
+ status: 'pending',
19
+ url: '',
20
+ }));
21
+ }
22
+
23
+ export function renderMultiProgressBars(bars, maxWidth = 30) {
24
+ const activeBars = bars.filter(bar => bar.total > 0);
25
+
26
+ if (activeBars.length === 0) return;
27
+
28
+ const lines = activeBars.map((bar) => {
29
+ const prog = createProgressBar(bar.current, bar.total, maxWidth);
30
+ const icon = bar.status === 'done' ? '✓' :
31
+ bar.status === 'error' ? '' : '⟳';
32
+ const urlDisplay = bar.url ? extractDisplayPath(bar.url) : '';
33
+ return ` [${prog}] ${bar.current}/${bar.total} ${icon} ${urlDisplay}`;
34
+ });
35
+
36
+ const output = lines.join('\n');
37
+
38
+ if (lastBarCount > 0) {
39
+ process.stdout.write(`\x1b[${lastBarCount}A`);
40
+ }
41
+
42
+ process.stdout.write('\x1b[0J');
43
+ process.stdout.write(output + '\n');
44
+
45
+ lastBarCount = activeBars.length;
46
+ }
47
+
48
+ export function clearProgressBars() {
49
+ if (lastBarCount > 0) {
50
+ process.stdout.write(`\x1b[${lastBarCount}A`);
51
+ process.stdout.write('\x1b[0J');
52
+ lastBarCount = 0;
53
+ }
54
+ }
@@ -0,0 +1,82 @@
1
+ import { exec } from 'child_process';
2
+ import { randomBytes } from 'crypto';
3
+ import { promisify } from 'util';
4
+
5
+ const execAsync = promisify(exec);
6
+
7
+ /**
8
+ * Get MAC address of the first non-loopback network interface
9
+ * Falls back to UUID if MAC cannot be retrieved
10
+ * @returns {Promise<string>} MAC address or UUID
11
+ */
12
+ export async function getMacOrUuid() {
13
+ try {
14
+ const mac = await getMacAddress();
15
+ if (mac) {
16
+ return mac.toLowerCase().replace(/:/g, '-');
17
+ }
18
+ } catch {
19
+ // Fallback to UUID
20
+ }
21
+
22
+ return generateUuid();
23
+ }
24
+
25
+ /**
26
+ * Get MAC address based on OS
27
+ * @returns {Promise<string|null>} MAC address or null
28
+ */
29
+ async function getMacAddress() {
30
+ const isWindows = process.platform === 'win32';
31
+ const isMac = process.platform === 'darwin';
32
+ const isLinux = process.platform === 'linux';
33
+
34
+ try {
35
+ if (isWindows) {
36
+ // Windows: use ipconfig
37
+ const { stdout } = await execAsync('ipconfig /all');
38
+ const match = stdout.match(/Physical\s*Address[^:]*:\s*([0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2})/);
39
+ if (match && match[1]) {
40
+ return match[1];
41
+ }
42
+ } else if (isMac) {
43
+ // macOS: use ifconfig
44
+ const { stdout } = await execAsync('ifconfig');
45
+ const match = stdout.match(/ether\s+([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/);
46
+ if (match && match[1]) {
47
+ return match[1];
48
+ }
49
+ } else if (isLinux) {
50
+ // Linux: use ip command
51
+ const { stdout } = await execAsync('ip link show');
52
+ const match = stdout.match(/link\/ether\s+([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/);
53
+ if (match && match[1]) {
54
+ return match[1];
55
+ }
56
+ }
57
+ } catch {
58
+ return null;
59
+ }
60
+
61
+ return null;
62
+ }
63
+
64
+ /**
65
+ * Generate a UUID v4
66
+ * @returns {string} UUID string
67
+ */
68
+ function generateUuid() {
69
+ const bytes = randomBytes(16);
70
+ bytes[6] = (bytes[6] & 0x0f) | 0x40; // version 4
71
+ bytes[8] = (bytes[8] & 0x3f) | 0x80; // variant RFC4122
72
+
73
+ const hex = bytes.reduce((acc, byte, i) => {
74
+ const h = byte.toString(16).padStart(2, '0');
75
+ if ([7, 11, 15, 19].includes(i)) {
76
+ return acc + '-' + h;
77
+ }
78
+ return acc + h;
79
+ }, '');
80
+
81
+ return hex;
82
+ }
package/src/lib/output.js CHANGED
@@ -1,80 +1,80 @@
1
- export function deduplicate(results) {
2
- const seen = new Set();
3
- return results.filter(r => {
4
- if (r.id) {
5
- const key = r.id;
6
- if (seen.has(key)) return false;
7
- seen.add(key);
8
- return true;
9
- }
10
- const key = r.secUid || r.uniqueId;
11
- if (seen.has(key)) return false;
12
- seen.add(key);
13
- return true;
14
- });
15
- }
16
-
17
- export function formatTable(data) {
18
- if (data.length === 0) return '';
19
-
20
- if (data.length === 1) {
21
- const lines = [];
22
- for (const [key, val] of Object.entries(data[0])) {
23
- if (typeof val === 'string' && val.length > 80) {
24
- lines.push(` ${key}: ${val.substring(0, 80)}...`);
25
- } else {
26
- lines.push(` ${key}: ${val}`);
27
- }
28
- }
29
- return lines.join('\n');
30
- }
31
-
32
- const cols = [
33
- { key: 'uniqueId', label: '用户名', width: 20 },
34
- { key: 'locationCreated', label: '地区', width: 6 },
35
- { key: 'nickname', label: '昵称', width: 20 },
36
- { key: 'ttSeller', label: 'TT卖家', width: 8 },
37
- { key: 'verified', label: '已认证', width: 8 },
38
- { key: 'followerCount', label: '粉丝', width: 10 },
39
- { key: 'videoCount', label: '视频', width: 8 },
40
- ];
41
-
42
- for (const row of data) {
43
- for (const col of cols) {
44
- const val = String(row[col.key] ?? '-');
45
- col.width = Math.max(col.width, val.length, col.label.length);
46
- }
47
- }
48
-
49
- const sep = (w) => '-'.repeat(w);
50
- const pad = (s, w) => s.padEnd(w);
51
-
52
- const header = cols.map(c => pad(c.label, c.width)).join(' │ ');
53
- const divider = cols.map(c => sep(c.width)).join('-+-');
54
- const rows = data.map(r =>
55
- cols.map(c => pad(String(r[c.key] ?? '-'), c.width)).join(' │ ')
56
- );
57
-
58
- return [header, divider, ...rows].join('\n');
59
- }
60
-
61
- export function formatOutput(data, format) {
62
- if (format === 'table') return formatTable(data);
63
-
64
- if (format === 'raw') {
65
- if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
66
- return data.map(d => d.url).join('\n');
67
- }
68
- if (Array.isArray(data) && data.length > 0 && 'uniqueId' in data[0]) {
69
- return data.map(d => `https://www.tiktok.com/@${d.uniqueId}`).join('\n');
70
- }
71
- return JSON.stringify(data, null, 2);
72
- }
73
-
74
- // Default JSON output, but for explore results (url-only) output pure text
75
- if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
76
- return data.map(d => d.url).join('\n');
77
- }
78
-
79
- return JSON.stringify(data, null, 2);
80
- }
1
+ export function deduplicate(results) {
2
+ const seen = new Set();
3
+ return results.filter(r => {
4
+ if (r.id) {
5
+ const key = r.id;
6
+ if (seen.has(key)) return false;
7
+ seen.add(key);
8
+ return true;
9
+ }
10
+ const key = r.secUid || r.uniqueId;
11
+ if (seen.has(key)) return false;
12
+ seen.add(key);
13
+ return true;
14
+ });
15
+ }
16
+
17
+ export function formatTable(data) {
18
+ if (data.length === 0) return '';
19
+
20
+ if (data.length === 1) {
21
+ const lines = [];
22
+ for (const [key, val] of Object.entries(data[0])) {
23
+ if (typeof val === 'string' && val.length > 80) {
24
+ lines.push(` ${key}: ${val.substring(0, 80)}...`);
25
+ } else {
26
+ lines.push(` ${key}: ${val}`);
27
+ }
28
+ }
29
+ return lines.join('\n');
30
+ }
31
+
32
+ const cols = [
33
+ { key: 'uniqueId', label: '用户名', width: 20 },
34
+ { key: 'locationCreated', label: '地区', width: 6 },
35
+ { key: 'nickname', label: '昵称', width: 20 },
36
+ { key: 'ttSeller', label: 'TT卖家', width: 8 },
37
+ { key: 'verified', label: '已认证', width: 8 },
38
+ { key: 'followerCount', label: '粉丝', width: 10 },
39
+ { key: 'videoCount', label: '视频', width: 8 },
40
+ ];
41
+
42
+ for (const row of data) {
43
+ for (const col of cols) {
44
+ const val = String(row[col.key] ?? '-');
45
+ col.width = Math.max(col.width, val.length, col.label.length);
46
+ }
47
+ }
48
+
49
+ const sep = (w) => '-'.repeat(w);
50
+ const pad = (s, w) => s.padEnd(w);
51
+
52
+ const header = cols.map(c => pad(c.label, c.width)).join(' │ ');
53
+ const divider = cols.map(c => sep(c.width)).join('-+-');
54
+ const rows = data.map(r =>
55
+ cols.map(c => pad(String(r[c.key] ?? '-'), c.width)).join(' │ ')
56
+ );
57
+
58
+ return [header, divider, ...rows].join('\n');
59
+ }
60
+
61
+ export function formatOutput(data, format) {
62
+ if (format === 'table') return formatTable(data);
63
+
64
+ if (format === 'raw') {
65
+ if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
66
+ return data.map(d => d.url).join('\n');
67
+ }
68
+ if (Array.isArray(data) && data.length > 0 && 'uniqueId' in data[0]) {
69
+ return data.map(d => `https://www.tiktok.com/@${d.uniqueId}`).join('\n');
70
+ }
71
+ return JSON.stringify(data, null, 2);
72
+ }
73
+
74
+ // Default JSON output, but for explore results (url-only) output pure text
75
+ if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
76
+ return data.map(d => d.url).join('\n');
77
+ }
78
+
79
+ return JSON.stringify(data, null, 2);
80
+ }
package/src/lib/parser.js CHANGED
@@ -1,47 +1,47 @@
1
- export const USER_SECTION_SIZE = 12000;
2
-
3
- export function extractUserSection(html) {
4
- const idx = html.indexOf('"uniqueId"');
5
- if (idx < 0) return null;
6
- return html.substring(idx, idx + USER_SECTION_SIZE);
7
- }
8
-
9
- export function parseUserSection(section) {
10
- const data = {};
11
-
12
- for (const key of ['uniqueId', 'uid', 'secUid']) {
13
- const m = section.match(new RegExp(`"${key}":"([^"]*)`));
14
- if (m) data[key] = m[1];
15
- }
16
-
17
- for (const key of ['nickname', 'signature']) {
18
- const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
19
- if (m) {
20
- const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
21
- data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
22
- }
23
- }
24
-
25
- for (const key of ['ttSeller', 'verified']) {
26
- const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
27
- data[key] = m ? m[1] === 'true' : undefined;
28
- }
29
-
30
- for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
31
- const m = section.match(new RegExp(`"${key}":(\\d+)`));
32
- if (m) data[key] = parseInt(m[1], 10);
33
- }
34
-
35
- const mt = section.match(/"createTime":(\d+)/);
36
- if (mt) data.createTime = parseInt(mt[1], 10);
37
-
38
- const ma = section.match(/"avatarLarger":"([^"]*)/);
39
- if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
40
-
41
- return data;
42
- }
43
-
44
- export function extractLocationCreated(html) {
45
- const m = html.match(/"locationCreated":"([^"]*)/);
46
- return m ? m[1] : null;
47
- }
1
+ export const USER_SECTION_SIZE = 12000;
2
+
3
+ export function extractUserSection(html) {
4
+ const idx = html.indexOf('"uniqueId"');
5
+ if (idx < 0) return null;
6
+ return html.substring(idx, idx + USER_SECTION_SIZE);
7
+ }
8
+
9
+ export function parseUserSection(section) {
10
+ const data = {};
11
+
12
+ for (const key of ['uniqueId', 'uid', 'secUid']) {
13
+ const m = section.match(new RegExp(`"${key}":"([^"]*)`));
14
+ if (m) data[key] = m[1];
15
+ }
16
+
17
+ for (const key of ['nickname', 'signature']) {
18
+ const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
19
+ if (m) {
20
+ const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
21
+ data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
22
+ }
23
+ }
24
+
25
+ for (const key of ['ttSeller', 'verified']) {
26
+ const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
27
+ data[key] = m ? m[1] === 'true' : undefined;
28
+ }
29
+
30
+ for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
31
+ const m = section.match(new RegExp(`"${key}":(\\d+)`));
32
+ if (m) data[key] = parseInt(m[1], 10);
33
+ }
34
+
35
+ const mt = section.match(/"createTime":(\d+)/);
36
+ if (mt) data.createTime = parseInt(mt[1], 10);
37
+
38
+ const ma = section.match(/"avatarLarger":"([^"]*)/);
39
+ if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
40
+
41
+ return data;
42
+ }
43
+
44
+ export function extractLocationCreated(html) {
45
+ const m = html.match(/"locationCreated":"([^"]*)/);
46
+ return m ? m[1] : null;
47
+ }
package/src/lib/retry.js CHANGED
@@ -1,44 +1,44 @@
1
- import { delay } from './delay.js';
2
-
3
- const RETRYABLE_PATTERNS = [
4
- 'interrupted',
5
- 'Navigation.*interrupted',
6
- 'net::',
7
- 'ECONN',
8
- 'ETIMEDOUT',
9
- 'ENOTFOUND',
10
- 'EAI_AGAIN',
11
- 'ESOCKETRESET',
12
- 'connection.*refused',
13
- 'connection.*reset',
14
- 'failed.*navigate',
15
- 'target.*closed',
16
- 'crash',
17
- ];
18
-
19
- export function isRetryableError(error) {
20
- if (!error) return false;
21
- const msg = (error.message || error.toString() || '').toLowerCase();
22
- return RETRYABLE_PATTERNS.some(p => new RegExp(p, 'i').test(msg));
23
- }
24
-
25
- export async function retryWithBackoff(fn, { maxRetries = 3, baseDelay = 3000, log } = {}) {
26
- let lastError;
27
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
28
- try {
29
- return await fn();
30
- } catch (error) {
31
- lastError = error;
32
- if (attempt >= maxRetries || !isRetryableError(error)) {
33
- throw error;
34
- }
35
- const jitter = Math.random() * 2000;
36
- const waitTime = baseDelay * Math.pow(2, attempt) + jitter;
37
- if (log) {
38
- log(` [重试] ${attempt + 1}/${maxRetries},${Math.round(waitTime / 1000)}s 后重试...`);
39
- }
40
- await delay(Math.round(waitTime), Math.round(waitTime));
41
- }
42
- }
43
- throw lastError;
44
- }
1
+ import { delay } from './delay.js';
2
+
3
+ const RETRYABLE_PATTERNS = [
4
+ 'interrupted',
5
+ 'Navigation.*interrupted',
6
+ 'net::',
7
+ 'ECONN',
8
+ 'ETIMEDOUT',
9
+ 'ENOTFOUND',
10
+ 'EAI_AGAIN',
11
+ 'ESOCKETRESET',
12
+ 'connection.*refused',
13
+ 'connection.*reset',
14
+ 'failed.*navigate',
15
+ 'target.*closed',
16
+ 'crash',
17
+ ];
18
+
19
+ export function isRetryableError(error) {
20
+ if (!error) return false;
21
+ const msg = (error.message || error.toString() || '').toLowerCase();
22
+ return RETRYABLE_PATTERNS.some(p => new RegExp(p, 'i').test(msg));
23
+ }
24
+
25
+ export async function retryWithBackoff(fn, { maxRetries = 3, baseDelay = 3000, log } = {}) {
26
+ let lastError;
27
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
28
+ try {
29
+ return await fn();
30
+ } catch (error) {
31
+ lastError = error;
32
+ if (attempt >= maxRetries || !isRetryableError(error)) {
33
+ throw error;
34
+ }
35
+ const jitter = Math.random() * 2000;
36
+ const waitTime = baseDelay * Math.pow(2, attempt) + jitter;
37
+ if (log) {
38
+ log(` [重试] ${attempt + 1}/${maxRetries},${Math.round(waitTime / 1000)}s 后重试...`);
39
+ }
40
+ await delay(Math.round(waitTime), Math.round(waitTime));
41
+ }
42
+ }
43
+ throw lastError;
44
+ }