@jackwener/opencli 1.5.1 → 1.5.3
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/.github/workflows/ci.yml +6 -7
- package/README.md +21 -362
- package/dist/browser/cdp.js +20 -1
- package/dist/browser/daemon-client.js +3 -2
- package/dist/browser/discover.js +11 -7
- package/dist/browser/dom-helpers.d.ts +11 -0
- package/dist/browser/dom-helpers.js +42 -0
- package/dist/browser/dom-helpers.test.d.ts +1 -0
- package/dist/browser/dom-helpers.test.js +92 -0
- package/dist/browser/index.d.ts +0 -10
- package/dist/browser/index.js +0 -11
- package/dist/browser/mcp.js +4 -3
- package/dist/browser/page.d.ts +2 -0
- package/dist/browser/page.js +42 -3
- package/dist/browser.test.js +17 -8
- package/dist/cli-manifest.json +4 -5
- package/dist/clis/36kr/hot.js +1 -1
- package/dist/clis/36kr/search.js +1 -1
- package/dist/clis/_shared/common.d.ts +8 -0
- package/dist/clis/_shared/common.js +10 -0
- package/dist/clis/apple-podcasts/commands.test.js +26 -3
- package/dist/clis/apple-podcasts/top.js +4 -1
- package/dist/clis/bloomberg/news.js +1 -1
- package/dist/clis/douban/utils.js +3 -6
- package/dist/clis/medium/utils.js +1 -1
- package/dist/clis/producthunt/browse.js +1 -1
- package/dist/clis/producthunt/hot.js +1 -1
- package/dist/clis/sinablog/utils.js +6 -7
- package/dist/clis/substack/utils.js +2 -2
- package/dist/clis/twitter/block.js +1 -1
- package/dist/clis/twitter/bookmark.js +1 -1
- package/dist/clis/twitter/delete.js +1 -1
- package/dist/clis/twitter/follow.js +1 -1
- package/dist/clis/twitter/followers.js +2 -2
- package/dist/clis/twitter/following.js +2 -2
- package/dist/clis/twitter/hide-reply.js +1 -1
- package/dist/clis/twitter/like.js +1 -1
- package/dist/clis/twitter/notifications.js +1 -1
- package/dist/clis/twitter/profile.js +1 -1
- package/dist/clis/twitter/reply-dm.js +1 -1
- package/dist/clis/twitter/reply.js +1 -1
- package/dist/clis/twitter/search.js +1 -1
- package/dist/clis/twitter/unblock.js +1 -1
- package/dist/clis/twitter/unbookmark.js +1 -1
- package/dist/clis/twitter/unfollow.js +1 -1
- package/dist/clis/v2ex/hot.yaml +3 -17
- package/dist/clis/weread/shelf.js +132 -9
- package/dist/clis/weread/utils.js +5 -1
- package/dist/clis/xiaohongshu/comments.test.js +1 -0
- package/dist/clis/xiaohongshu/creator-note-detail.test.js +1 -0
- package/dist/clis/xiaohongshu/creator-notes.test.js +1 -0
- package/dist/clis/xiaohongshu/publish.test.js +1 -0
- package/dist/clis/xiaohongshu/search.test.js +1 -0
- package/dist/daemon.js +1 -0
- package/dist/doctor.js +7 -3
- package/dist/download/index.js +39 -33
- package/dist/download/index.test.js +15 -1
- package/dist/execution.js +3 -8
- package/dist/extension-manifest-regression.test.d.ts +1 -0
- package/dist/extension-manifest-regression.test.js +12 -0
- package/dist/main.js +2 -0
- package/dist/node-network.d.ts +10 -0
- package/dist/node-network.js +174 -0
- package/dist/node-network.test.d.ts +1 -0
- package/dist/node-network.test.js +55 -0
- package/dist/pipeline/executor.test.js +1 -0
- package/dist/pipeline/steps/download.test.js +1 -0
- package/dist/pipeline/steps/intercept.js +4 -5
- package/dist/types.d.ts +2 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +4 -0
- package/dist/weread-private-api-regression.test.js +185 -0
- package/docs/superpowers/plans/2026-03-28-perf-smart-wait.md +1143 -0
- package/docs/superpowers/specs/2026-03-28-perf-smart-wait-design.md +170 -0
- package/extension/dist/background.js +4 -2
- package/extension/manifest.json +4 -1
- package/extension/package-lock.json +2 -2
- package/extension/package.json +1 -1
- package/extension/src/background.ts +2 -1
- package/package.json +2 -1
- package/src/browser/cdp.ts +21 -0
- package/src/browser/daemon-client.ts +3 -2
- package/src/browser/discover.ts +10 -7
- package/src/browser/dom-helpers.test.ts +100 -0
- package/src/browser/dom-helpers.ts +44 -0
- package/src/browser/index.ts +0 -13
- package/src/browser/mcp.ts +4 -3
- package/src/browser/page.ts +41 -2
- package/src/browser.test.ts +19 -9
- package/src/clis/36kr/hot.ts +1 -1
- package/src/clis/36kr/search.ts +1 -1
- package/src/clis/_shared/common.ts +11 -0
- package/src/clis/apple-podcasts/commands.test.ts +30 -2
- package/src/clis/apple-podcasts/top.ts +4 -1
- package/src/clis/bloomberg/news.ts +1 -1
- package/src/clis/douban/utils.ts +3 -7
- package/src/clis/medium/utils.ts +1 -1
- package/src/clis/producthunt/browse.ts +1 -1
- package/src/clis/producthunt/hot.ts +1 -1
- package/src/clis/sinablog/utils.ts +6 -7
- package/src/clis/substack/utils.ts +2 -2
- package/src/clis/twitter/block.ts +1 -1
- package/src/clis/twitter/bookmark.ts +1 -1
- package/src/clis/twitter/delete.ts +1 -1
- package/src/clis/twitter/follow.ts +1 -1
- package/src/clis/twitter/followers.ts +2 -2
- package/src/clis/twitter/following.ts +2 -2
- package/src/clis/twitter/hide-reply.ts +1 -1
- package/src/clis/twitter/like.ts +1 -1
- package/src/clis/twitter/notifications.ts +1 -1
- package/src/clis/twitter/profile.ts +1 -1
- package/src/clis/twitter/reply-dm.ts +1 -1
- package/src/clis/twitter/reply.ts +1 -1
- package/src/clis/twitter/search.ts +1 -1
- package/src/clis/twitter/unblock.ts +1 -1
- package/src/clis/twitter/unbookmark.ts +1 -1
- package/src/clis/twitter/unfollow.ts +1 -1
- package/src/clis/v2ex/hot.yaml +3 -17
- package/src/clis/weread/shelf.ts +169 -9
- package/src/clis/weread/utils.ts +6 -1
- package/src/clis/xiaohongshu/comments.test.ts +1 -0
- package/src/clis/xiaohongshu/creator-note-detail.test.ts +1 -0
- package/src/clis/xiaohongshu/creator-notes.test.ts +1 -0
- package/src/clis/xiaohongshu/publish.test.ts +1 -0
- package/src/clis/xiaohongshu/search.test.ts +1 -0
- package/src/daemon.ts +1 -0
- package/src/doctor.ts +9 -5
- package/src/download/index.test.ts +19 -1
- package/src/download/index.ts +50 -41
- package/src/execution.ts +3 -11
- package/src/extension-manifest-regression.test.ts +17 -0
- package/src/main.ts +3 -0
- package/src/node-network.test.ts +93 -0
- package/src/node-network.ts +213 -0
- package/src/pipeline/executor.test.ts +1 -0
- package/src/pipeline/steps/download.test.ts +1 -0
- package/src/pipeline/steps/intercept.ts +4 -5
- package/src/types.ts +2 -0
- package/src/utils.ts +5 -0
- package/src/weread-private-api-regression.test.ts +207 -0
- package/tests/e2e/browser-public.test.ts +1 -1
- package/tests/e2e/output-formats.test.ts +10 -14
- package/tests/e2e/plugin-management.test.ts +4 -1
- package/tests/e2e/public-commands.test.ts +12 -1
- package/vitest.config.ts +1 -15
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Agent, EnvHttpProxyAgent, fetch as undiciFetch } from 'undici';
|
|
2
|
+
const LOOPBACK_NO_PROXY_ENTRIES = ['127.0.0.1', 'localhost', '::1'];
|
|
3
|
+
const PROXY_ENV_BY_PROTOCOL = {
|
|
4
|
+
'http:': ['http_proxy', 'HTTP_PROXY', 'all_proxy', 'ALL_PROXY'],
|
|
5
|
+
'https:': ['https_proxy', 'HTTPS_PROXY', 'all_proxy', 'ALL_PROXY'],
|
|
6
|
+
};
|
|
7
|
+
const DEFAULT_PORT_BY_PROTOCOL = {
|
|
8
|
+
'http:': '80',
|
|
9
|
+
'https:': '443',
|
|
10
|
+
};
|
|
11
|
+
let installed = false;
|
|
12
|
+
const directDispatcher = new Agent();
|
|
13
|
+
const proxyDispatcherCache = new Map();
|
|
14
|
+
const nativeFetch = globalThis.fetch.bind(globalThis);
|
|
15
|
+
function readEnv(env, lower, upper) {
|
|
16
|
+
const lowerValue = env[lower];
|
|
17
|
+
if (typeof lowerValue === 'string' && lowerValue.trim() !== '')
|
|
18
|
+
return lowerValue;
|
|
19
|
+
const upperValue = env[upper];
|
|
20
|
+
if (typeof upperValue === 'string' && upperValue.trim() !== '')
|
|
21
|
+
return upperValue;
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
function readProxyEnv(env, keys) {
|
|
25
|
+
for (const key of keys) {
|
|
26
|
+
const value = env[key];
|
|
27
|
+
if (typeof value === 'string' && value.trim() !== '')
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
function normalizeHostname(hostname) {
|
|
33
|
+
return hostname.replace(/^\[(.*)\]$/, '$1').toLowerCase();
|
|
34
|
+
}
|
|
35
|
+
function splitNoProxy(raw) {
|
|
36
|
+
return (raw ?? '')
|
|
37
|
+
.split(/[,\s]+/)
|
|
38
|
+
.map((token) => token.trim())
|
|
39
|
+
.filter(Boolean);
|
|
40
|
+
}
|
|
41
|
+
function parseNoProxyEntry(entry) {
|
|
42
|
+
if (entry === '*')
|
|
43
|
+
return { host: '*' };
|
|
44
|
+
const trimmed = entry.trim().replace(/^\*?\./, '');
|
|
45
|
+
if (trimmed.startsWith('[')) {
|
|
46
|
+
const end = trimmed.indexOf(']');
|
|
47
|
+
if (end !== -1) {
|
|
48
|
+
const host = trimmed.slice(1, end);
|
|
49
|
+
const rest = trimmed.slice(end + 1);
|
|
50
|
+
if (rest.startsWith(':'))
|
|
51
|
+
return { host: normalizeHostname(host), port: rest.slice(1) };
|
|
52
|
+
return { host: normalizeHostname(host) };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const colonCount = (trimmed.match(/:/g) ?? []).length;
|
|
56
|
+
if (colonCount === 1) {
|
|
57
|
+
const [host, port] = trimmed.split(':');
|
|
58
|
+
return { host: normalizeHostname(host), port };
|
|
59
|
+
}
|
|
60
|
+
return { host: normalizeHostname(trimmed) };
|
|
61
|
+
}
|
|
62
|
+
function effectiveNoProxyEntries(env) {
|
|
63
|
+
const raw = readEnv(env, 'no_proxy', 'NO_PROXY');
|
|
64
|
+
const entries = splitNoProxy(raw).map(parseNoProxyEntry);
|
|
65
|
+
const seen = new Set(entries.map((entry) => `${entry.host}:${entry.port ?? ''}`));
|
|
66
|
+
for (const rawEntry of LOOPBACK_NO_PROXY_ENTRIES) {
|
|
67
|
+
const entry = parseNoProxyEntry(rawEntry);
|
|
68
|
+
const key = `${entry.host}:${entry.port ?? ''}`;
|
|
69
|
+
if (seen.has(key))
|
|
70
|
+
continue;
|
|
71
|
+
entries.push(entry);
|
|
72
|
+
seen.add(key);
|
|
73
|
+
}
|
|
74
|
+
return entries;
|
|
75
|
+
}
|
|
76
|
+
function serializeNoProxyEntry(entry) {
|
|
77
|
+
if (entry.host === '*')
|
|
78
|
+
return '*';
|
|
79
|
+
const host = entry.host.includes(':') ? `[${entry.host}]` : entry.host;
|
|
80
|
+
return entry.port ? `${host}:${entry.port}` : host;
|
|
81
|
+
}
|
|
82
|
+
function effectiveNoProxyValue(entries) {
|
|
83
|
+
if (entries.length === 0)
|
|
84
|
+
return undefined;
|
|
85
|
+
return entries.map(serializeNoProxyEntry).join(',');
|
|
86
|
+
}
|
|
87
|
+
function matchesNoProxyEntry(url, entry) {
|
|
88
|
+
const { host, port } = entry;
|
|
89
|
+
if (host === '*')
|
|
90
|
+
return true;
|
|
91
|
+
const hostname = normalizeHostname(url.hostname);
|
|
92
|
+
const urlPort = url.port || DEFAULT_PORT_BY_PROTOCOL[url.protocol] || undefined;
|
|
93
|
+
if (port && port !== urlPort)
|
|
94
|
+
return false;
|
|
95
|
+
return hostname === host || hostname.endsWith(`.${host}`);
|
|
96
|
+
}
|
|
97
|
+
function resolveProxyConfig(env = process.env) {
|
|
98
|
+
const noProxyEntries = effectiveNoProxyEntries(env);
|
|
99
|
+
return {
|
|
100
|
+
httpProxy: readProxyEnv(env, PROXY_ENV_BY_PROTOCOL['http:']),
|
|
101
|
+
httpsProxy: readProxyEnv(env, [
|
|
102
|
+
'https_proxy',
|
|
103
|
+
'HTTPS_PROXY',
|
|
104
|
+
'http_proxy',
|
|
105
|
+
'HTTP_PROXY',
|
|
106
|
+
'all_proxy',
|
|
107
|
+
'ALL_PROXY',
|
|
108
|
+
]),
|
|
109
|
+
noProxy: effectiveNoProxyValue(noProxyEntries),
|
|
110
|
+
noProxyEntries,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function createProxyDispatcher(config) {
|
|
114
|
+
const cacheKey = JSON.stringify([
|
|
115
|
+
config.httpProxy ?? '',
|
|
116
|
+
config.httpsProxy ?? '',
|
|
117
|
+
config.noProxy ?? '',
|
|
118
|
+
]);
|
|
119
|
+
const cached = proxyDispatcherCache.get(cacheKey);
|
|
120
|
+
if (cached)
|
|
121
|
+
return cached;
|
|
122
|
+
const dispatcher = new EnvHttpProxyAgent({
|
|
123
|
+
httpProxy: config.httpProxy,
|
|
124
|
+
httpsProxy: config.httpsProxy,
|
|
125
|
+
noProxy: config.noProxy,
|
|
126
|
+
});
|
|
127
|
+
proxyDispatcherCache.set(cacheKey, dispatcher);
|
|
128
|
+
return dispatcher;
|
|
129
|
+
}
|
|
130
|
+
function resolveUrl(input) {
|
|
131
|
+
if (typeof input === 'string')
|
|
132
|
+
return new URL(input);
|
|
133
|
+
if (input instanceof URL)
|
|
134
|
+
return input;
|
|
135
|
+
if (typeof Request !== 'undefined' && input instanceof Request)
|
|
136
|
+
return new URL(input.url);
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
export function hasProxyEnv(env = process.env) {
|
|
140
|
+
const config = resolveProxyConfig(env);
|
|
141
|
+
return Boolean(config.httpProxy || config.httpsProxy);
|
|
142
|
+
}
|
|
143
|
+
export function decideProxy(url, env = process.env) {
|
|
144
|
+
const config = resolveProxyConfig(env);
|
|
145
|
+
if (config.noProxyEntries.some((entry) => matchesNoProxyEntry(url, entry))) {
|
|
146
|
+
return { mode: 'direct' };
|
|
147
|
+
}
|
|
148
|
+
const proxyUrl = url.protocol === 'https:' ? config.httpsProxy : config.httpProxy;
|
|
149
|
+
if (!proxyUrl)
|
|
150
|
+
return { mode: 'direct' };
|
|
151
|
+
return { mode: 'proxy', proxyUrl };
|
|
152
|
+
}
|
|
153
|
+
export function getDispatcherForUrl(url, env = process.env) {
|
|
154
|
+
const config = resolveProxyConfig(env);
|
|
155
|
+
if (!config.httpProxy && !config.httpsProxy)
|
|
156
|
+
return directDispatcher;
|
|
157
|
+
return createProxyDispatcher(config);
|
|
158
|
+
}
|
|
159
|
+
export async function fetchWithNodeNetwork(input, init = {}) {
|
|
160
|
+
const url = resolveUrl(input);
|
|
161
|
+
if (!url || !hasProxyEnv()) {
|
|
162
|
+
return nativeFetch(input, init);
|
|
163
|
+
}
|
|
164
|
+
return (await undiciFetch(input, {
|
|
165
|
+
...init,
|
|
166
|
+
dispatcher: getDispatcherForUrl(url),
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
export function installNodeNetwork() {
|
|
170
|
+
if (installed)
|
|
171
|
+
return;
|
|
172
|
+
globalThis.fetch = ((input, init) => (fetchWithNodeNetwork(input, init)));
|
|
173
|
+
installed = true;
|
|
174
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { decideProxy, hasProxyEnv } from './node-network.js';
|
|
3
|
+
describe('node network proxy decisions', () => {
|
|
4
|
+
it('detects common proxy env variables', () => {
|
|
5
|
+
expect(hasProxyEnv({ https_proxy: 'http://127.0.0.1:7897' })).toBe(true);
|
|
6
|
+
expect(hasProxyEnv({ HTTP_PROXY: 'http://proxy.example:8080' })).toBe(true);
|
|
7
|
+
expect(hasProxyEnv({})).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
it('routes external https traffic through https_proxy', () => {
|
|
10
|
+
const decision = decideProxy(new URL('https://www.v2ex.com/api/topics/latest.json'), { https_proxy: 'http://127.0.0.1:7897' });
|
|
11
|
+
expect(decision).toEqual({
|
|
12
|
+
mode: 'proxy',
|
|
13
|
+
proxyUrl: 'http://127.0.0.1:7897',
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
it('falls back to HTTP_PROXY for https traffic when HTTPS_PROXY is absent', () => {
|
|
17
|
+
const decision = decideProxy(new URL('https://www.v2ex.com/api/topics/latest.json'), { HTTP_PROXY: 'http://127.0.0.1:7897' });
|
|
18
|
+
expect(decision).toEqual({
|
|
19
|
+
mode: 'proxy',
|
|
20
|
+
proxyUrl: 'http://127.0.0.1:7897',
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
it('bypasses proxies for loopback addresses', () => {
|
|
24
|
+
const env = { https_proxy: 'http://127.0.0.1:7897', http_proxy: 'http://127.0.0.1:7897' };
|
|
25
|
+
expect(decideProxy(new URL('http://127.0.0.1:19825/status'), env)).toEqual({ mode: 'direct' });
|
|
26
|
+
expect(decideProxy(new URL('http://localhost:19825/status'), env)).toEqual({ mode: 'direct' });
|
|
27
|
+
expect(decideProxy(new URL('http://[::1]:19825/status'), env)).toEqual({ mode: 'direct' });
|
|
28
|
+
});
|
|
29
|
+
it('honors NO_PROXY domain matches', () => {
|
|
30
|
+
const decision = decideProxy(new URL('https://api.example.com/v1/items'), {
|
|
31
|
+
https_proxy: 'http://127.0.0.1:7897',
|
|
32
|
+
no_proxy: '.example.com',
|
|
33
|
+
});
|
|
34
|
+
expect(decision).toEqual({ mode: 'direct' });
|
|
35
|
+
});
|
|
36
|
+
it('supports wildcard-style NO_PROXY subdomain entries', () => {
|
|
37
|
+
const decision = decideProxy(new URL('https://api.example.com/v1/items'), {
|
|
38
|
+
https_proxy: 'http://127.0.0.1:7897',
|
|
39
|
+
no_proxy: '*.example.com',
|
|
40
|
+
});
|
|
41
|
+
expect(decision).toEqual({ mode: 'direct' });
|
|
42
|
+
});
|
|
43
|
+
it('matches NO_PROXY entries that rely on the default URL port', () => {
|
|
44
|
+
const env = { https_proxy: 'http://127.0.0.1:7897', http_proxy: 'http://127.0.0.1:7897' };
|
|
45
|
+
expect(decideProxy(new URL('https://example.com/'), { ...env, NO_PROXY: 'example.com:443' })).toEqual({ mode: 'direct' });
|
|
46
|
+
expect(decideProxy(new URL('http://example.com/health'), { ...env, NO_PROXY: 'example.com:80' })).toEqual({ mode: 'direct' });
|
|
47
|
+
});
|
|
48
|
+
it('falls back to ALL_PROXY when protocol-specific settings are absent', () => {
|
|
49
|
+
const decision = decideProxy(new URL('http://example.net/data'), { ALL_PROXY: 'socks5://127.0.0.1:1080' });
|
|
50
|
+
expect(decision).toEqual({
|
|
51
|
+
mode: 'proxy',
|
|
52
|
+
proxyUrl: 'socks5://127.0.0.1:1080',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -28,6 +28,7 @@ function createMockPage(overrides = {}) {
|
|
|
28
28
|
installInterceptor: vi.fn(),
|
|
29
29
|
getInterceptedRequests: vi.fn().mockResolvedValue([]),
|
|
30
30
|
screenshot: vi.fn().mockResolvedValue(''),
|
|
31
|
+
waitForCapture: vi.fn().mockResolvedValue(undefined),
|
|
31
32
|
...overrides,
|
|
32
33
|
};
|
|
33
34
|
}
|
|
@@ -39,6 +39,7 @@ function createMockPage(getCookies) {
|
|
|
39
39
|
installInterceptor: vi.fn(),
|
|
40
40
|
getInterceptedRequests: vi.fn().mockResolvedValue([]),
|
|
41
41
|
screenshot: vi.fn().mockResolvedValue(''),
|
|
42
|
+
waitForCapture: vi.fn().mockResolvedValue(undefined),
|
|
42
43
|
};
|
|
43
44
|
}
|
|
44
45
|
describe('stepDownload', () => {
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* Pipeline step: intercept — declarative XHR interception.
|
|
3
3
|
*/
|
|
4
4
|
import { render, normalizeEvaluateSource } from '../template.js';
|
|
5
|
-
import { generateInterceptorJs, generateReadInterceptedJs } from '../../interceptor.js';
|
|
6
5
|
export async function stepIntercept(page, params, data, args) {
|
|
7
6
|
const cfg = typeof params === 'object' ? params : {};
|
|
8
7
|
const trigger = cfg.trigger ?? '';
|
|
@@ -12,7 +11,7 @@ export async function stepIntercept(page, params, data, args) {
|
|
|
12
11
|
if (!capturePattern)
|
|
13
12
|
return data;
|
|
14
13
|
// Step 1: Inject fetch/XHR interceptor BEFORE trigger
|
|
15
|
-
await page.
|
|
14
|
+
await page.installInterceptor(capturePattern);
|
|
16
15
|
// Step 2: Execute the trigger action
|
|
17
16
|
if (trigger.startsWith('navigate:')) {
|
|
18
17
|
const url = render(trigger.slice('navigate:'.length), { args, data });
|
|
@@ -29,10 +28,10 @@ export async function stepIntercept(page, params, data, args) {
|
|
|
29
28
|
else if (trigger === 'scroll') {
|
|
30
29
|
await page.scroll('down');
|
|
31
30
|
}
|
|
32
|
-
// Step 3: Wait
|
|
33
|
-
await page.
|
|
31
|
+
// Step 3: Wait for network capture (event-driven, not fixed sleep)
|
|
32
|
+
await page.waitForCapture(timeout);
|
|
34
33
|
// Step 4: Retrieve captured data
|
|
35
|
-
const matchingResponses = await page.
|
|
34
|
+
const matchingResponses = await page.getInterceptedRequests();
|
|
36
35
|
// Step 5: Select from response if specified
|
|
37
36
|
let result = matchingResponses.length === 1 ? matchingResponses[0] :
|
|
38
37
|
matchingResponses.length > 1 ? matchingResponses : data;
|
package/dist/types.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export interface SnapshotOptions {
|
|
|
23
23
|
}
|
|
24
24
|
export interface WaitOptions {
|
|
25
25
|
text?: string;
|
|
26
|
+
selector?: string;
|
|
26
27
|
time?: number;
|
|
27
28
|
timeout?: number;
|
|
28
29
|
}
|
|
@@ -67,6 +68,7 @@ export interface IPage {
|
|
|
67
68
|
}): Promise<void>;
|
|
68
69
|
installInterceptor(pattern: string): Promise<void>;
|
|
69
70
|
getInterceptedRequests(): Promise<any[]>;
|
|
71
|
+
waitForCapture(timeout?: number): Promise<void>;
|
|
70
72
|
screenshot(options?: ScreenshotOptions): Promise<string>;
|
|
71
73
|
closeWindow?(): Promise<void>;
|
|
72
74
|
/** Returns the current page URL, or null if unavailable. */
|
package/dist/utils.d.ts
CHANGED
|
@@ -5,5 +5,7 @@
|
|
|
5
5
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
6
6
|
/** Simple async concurrency limiter. */
|
|
7
7
|
export declare function mapConcurrent<T, R>(items: T[], limit: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
|
|
8
|
+
/** Pause for the given number of milliseconds. */
|
|
9
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
8
10
|
/** Save a base64-encoded string to a file, creating parent directories as needed. */
|
|
9
11
|
export declare function saveBase64ToFile(base64: string, filePath: string): Promise<void>;
|
package/dist/utils.js
CHANGED
|
@@ -21,6 +21,10 @@ export async function mapConcurrent(items, limit, fn) {
|
|
|
21
21
|
await Promise.all(workers);
|
|
22
22
|
return results;
|
|
23
23
|
}
|
|
24
|
+
/** Pause for the given number of milliseconds. */
|
|
25
|
+
export function sleep(ms) {
|
|
26
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
27
|
+
}
|
|
24
28
|
/** Save a base64-encoded string to a file, creating parent directories as needed. */
|
|
25
29
|
export async function saveBase64ToFile(base64, filePath) {
|
|
26
30
|
const dir = path.dirname(filePath);
|
|
@@ -44,6 +44,21 @@ describe('weread private API regression', () => {
|
|
|
44
44
|
}));
|
|
45
45
|
await expect(fetchPrivateApi(mockPage, '/book/info')).rejects.toThrow('Not logged in');
|
|
46
46
|
});
|
|
47
|
+
it('maps auth-expired API error codes to AUTH_REQUIRED even on HTTP 200', async () => {
|
|
48
|
+
const mockPage = {
|
|
49
|
+
getCookies: vi.fn().mockResolvedValue([]),
|
|
50
|
+
evaluate: vi.fn(),
|
|
51
|
+
};
|
|
52
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
53
|
+
ok: true,
|
|
54
|
+
status: 200,
|
|
55
|
+
json: () => Promise.resolve({ errcode: -2012, errmsg: '登录超时' }),
|
|
56
|
+
}));
|
|
57
|
+
await expect(fetchPrivateApi(mockPage, '/book/info')).rejects.toMatchObject({
|
|
58
|
+
code: 'AUTH_REQUIRED',
|
|
59
|
+
message: 'Not logged in to WeRead',
|
|
60
|
+
});
|
|
61
|
+
});
|
|
47
62
|
it('maps non-auth API errors to API_ERROR', async () => {
|
|
48
63
|
const mockPage = {
|
|
49
64
|
getCookies: vi.fn().mockResolvedValue([]),
|
|
@@ -119,4 +134,174 @@ describe('weread private API regression', () => {
|
|
|
119
134
|
},
|
|
120
135
|
]);
|
|
121
136
|
});
|
|
137
|
+
it('falls back to structured shelf cache when the private API reports AUTH_REQUIRED', async () => {
|
|
138
|
+
const command = getRegistry().get('weread/shelf');
|
|
139
|
+
expect(command?.func).toBeTypeOf('function');
|
|
140
|
+
const mockPage = {
|
|
141
|
+
getCookies: vi.fn()
|
|
142
|
+
.mockResolvedValueOnce([
|
|
143
|
+
{ name: 'wr_skey', value: 'skey123', domain: '.weread.qq.com' },
|
|
144
|
+
])
|
|
145
|
+
.mockResolvedValueOnce([
|
|
146
|
+
{ name: 'wr_vid', value: 'vid-current', domain: '.weread.qq.com' },
|
|
147
|
+
]),
|
|
148
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
149
|
+
evaluate: vi.fn().mockImplementation(async (source) => {
|
|
150
|
+
expect(source).toContain('shelf:rawBooks:vid-current');
|
|
151
|
+
expect(source).toContain('shelf:shelfIndexes:vid-current');
|
|
152
|
+
return {
|
|
153
|
+
cacheFound: true,
|
|
154
|
+
rawBooks: [
|
|
155
|
+
{
|
|
156
|
+
bookId: '40055543',
|
|
157
|
+
title: '置身事内:中国政府与经济发展',
|
|
158
|
+
author: '兰小欢',
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
bookId: '29196155',
|
|
162
|
+
title: '文明、现代化、价值投资与中国',
|
|
163
|
+
author: '李录',
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
shelfIndexes: [
|
|
167
|
+
{ bookId: '29196155', idx: 0, role: 'book' },
|
|
168
|
+
{ bookId: '40055543', idx: 1, role: 'book' },
|
|
169
|
+
],
|
|
170
|
+
lastChapters: {
|
|
171
|
+
'29196155': 40,
|
|
172
|
+
'40055543': 60,
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}),
|
|
176
|
+
};
|
|
177
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
178
|
+
ok: false,
|
|
179
|
+
status: 401,
|
|
180
|
+
json: () => Promise.resolve({ errcode: -2012, errmsg: '登录超时' }),
|
|
181
|
+
}));
|
|
182
|
+
const result = await command.func(mockPage, { limit: 1 });
|
|
183
|
+
expect(mockPage.goto).toHaveBeenCalledWith('https://weread.qq.com/web/shelf');
|
|
184
|
+
expect(mockPage.getCookies).toHaveBeenCalledWith({ domain: 'weread.qq.com' });
|
|
185
|
+
expect(mockPage.evaluate).toHaveBeenCalledTimes(1);
|
|
186
|
+
expect(result).toEqual([
|
|
187
|
+
{
|
|
188
|
+
title: '文明、现代化、价值投资与中国',
|
|
189
|
+
author: '李录',
|
|
190
|
+
progress: '-',
|
|
191
|
+
bookId: '29196155',
|
|
192
|
+
},
|
|
193
|
+
]);
|
|
194
|
+
});
|
|
195
|
+
it('rethrows AUTH_REQUIRED when the current session has no structured shelf cache', async () => {
|
|
196
|
+
const command = getRegistry().get('weread/shelf');
|
|
197
|
+
expect(command?.func).toBeTypeOf('function');
|
|
198
|
+
const mockPage = {
|
|
199
|
+
getCookies: vi.fn()
|
|
200
|
+
.mockResolvedValueOnce([
|
|
201
|
+
{ name: 'wr_skey', value: 'skey123', domain: '.weread.qq.com' },
|
|
202
|
+
])
|
|
203
|
+
.mockResolvedValueOnce([
|
|
204
|
+
{ name: 'wr_vid', value: 'vid-current', domain: '.weread.qq.com' },
|
|
205
|
+
]),
|
|
206
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
207
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
208
|
+
cacheFound: false,
|
|
209
|
+
rawBooks: [],
|
|
210
|
+
shelfIndexes: [],
|
|
211
|
+
lastChapters: {},
|
|
212
|
+
}),
|
|
213
|
+
};
|
|
214
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
215
|
+
ok: false,
|
|
216
|
+
status: 401,
|
|
217
|
+
json: () => Promise.resolve({ errcode: -2012, errmsg: '登录超时' }),
|
|
218
|
+
}));
|
|
219
|
+
await expect(command.func(mockPage, { limit: 20 })).rejects.toMatchObject({
|
|
220
|
+
code: 'AUTH_REQUIRED',
|
|
221
|
+
message: 'Not logged in to WeRead',
|
|
222
|
+
});
|
|
223
|
+
expect(mockPage.goto).toHaveBeenCalledWith('https://weread.qq.com/web/shelf');
|
|
224
|
+
expect(mockPage.getCookies).toHaveBeenCalledWith({ domain: 'weread.qq.com' });
|
|
225
|
+
});
|
|
226
|
+
it('returns an empty list when the current session cache is confirmed but empty', async () => {
|
|
227
|
+
const command = getRegistry().get('weread/shelf');
|
|
228
|
+
expect(command?.func).toBeTypeOf('function');
|
|
229
|
+
const mockPage = {
|
|
230
|
+
getCookies: vi.fn()
|
|
231
|
+
.mockResolvedValueOnce([
|
|
232
|
+
{ name: 'wr_skey', value: 'skey123', domain: '.weread.qq.com' },
|
|
233
|
+
])
|
|
234
|
+
.mockResolvedValueOnce([
|
|
235
|
+
{ name: 'wr_vid', value: 'vid-current', domain: '.weread.qq.com' },
|
|
236
|
+
]),
|
|
237
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
238
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
239
|
+
cacheFound: true,
|
|
240
|
+
rawBooks: [],
|
|
241
|
+
shelfIndexes: [],
|
|
242
|
+
lastChapters: {},
|
|
243
|
+
}),
|
|
244
|
+
};
|
|
245
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
246
|
+
ok: false,
|
|
247
|
+
status: 401,
|
|
248
|
+
json: () => Promise.resolve({ errcode: -2012, errmsg: '登录超时' }),
|
|
249
|
+
}));
|
|
250
|
+
const result = await command.func(mockPage, { limit: 20 });
|
|
251
|
+
expect(mockPage.goto).toHaveBeenCalledWith('https://weread.qq.com/web/shelf');
|
|
252
|
+
expect(mockPage.getCookies).toHaveBeenCalledWith({ domain: 'weread.qq.com' });
|
|
253
|
+
expect(result).toEqual([]);
|
|
254
|
+
});
|
|
255
|
+
it('falls back to raw book cache order when shelf indexes are unavailable', async () => {
|
|
256
|
+
const command = getRegistry().get('weread/shelf');
|
|
257
|
+
expect(command?.func).toBeTypeOf('function');
|
|
258
|
+
const mockPage = {
|
|
259
|
+
getCookies: vi.fn()
|
|
260
|
+
.mockResolvedValueOnce([
|
|
261
|
+
{ name: 'wr_skey', value: 'skey123', domain: '.weread.qq.com' },
|
|
262
|
+
])
|
|
263
|
+
.mockResolvedValueOnce([
|
|
264
|
+
{ name: 'wr_vid', value: 'vid-current', domain: '.weread.qq.com' },
|
|
265
|
+
]),
|
|
266
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
267
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
268
|
+
cacheFound: true,
|
|
269
|
+
rawBooks: [
|
|
270
|
+
{
|
|
271
|
+
bookId: '40055543',
|
|
272
|
+
title: '置身事内:中国政府与经济发展',
|
|
273
|
+
author: '兰小欢',
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
bookId: '29196155',
|
|
277
|
+
title: '文明、现代化、价值投资与中国',
|
|
278
|
+
author: '李录',
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
shelfIndexes: [],
|
|
282
|
+
}),
|
|
283
|
+
};
|
|
284
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
285
|
+
ok: false,
|
|
286
|
+
status: 401,
|
|
287
|
+
json: () => Promise.resolve({ errcode: -2012, errmsg: '登录超时' }),
|
|
288
|
+
}));
|
|
289
|
+
const result = await command.func(mockPage, { limit: 2 });
|
|
290
|
+
expect(mockPage.getCookies).toHaveBeenCalledWith({ url: 'https://i.weread.qq.com/shelf/sync?synckey=0&lectureSynckey=0' });
|
|
291
|
+
expect(mockPage.getCookies).toHaveBeenCalledWith({ domain: 'weread.qq.com' });
|
|
292
|
+
expect(result).toEqual([
|
|
293
|
+
{
|
|
294
|
+
title: '置身事内:中国政府与经济发展',
|
|
295
|
+
author: '兰小欢',
|
|
296
|
+
progress: '-',
|
|
297
|
+
bookId: '40055543',
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
title: '文明、现代化、价值投资与中国',
|
|
301
|
+
author: '李录',
|
|
302
|
+
progress: '-',
|
|
303
|
+
bookId: '29196155',
|
|
304
|
+
},
|
|
305
|
+
]);
|
|
306
|
+
});
|
|
122
307
|
});
|