dsh-git-branch-manage 1.2.0 → 1.3.0
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/CHANGELOG.md +17 -0
- package/lib/client.js +189 -13
- package/lib/git.d.ts +82 -9
- package/lib/git.js +260 -24
- package/lib/index.d.ts +11 -0
- package/lib/index.js +34 -3
- package/lib/network-ip.d.ts +99 -0
- package/lib/network-ip.js +206 -0
- package/lib/network-probe.d.ts +28 -0
- package/lib/network-probe.js +47 -0
- package/lib/network-proxy.d.ts +35 -0
- package/lib/network-proxy.js +55 -0
- package/lib/network-tool.js +3 -2
- package/lib/network.d.ts +58 -0
- package/lib/network.js +128 -0
- package/lib/normalize.d.ts +14 -2
- package/lib/normalize.js +38 -2
- package/lib/routes.d.ts +24 -3
- package/lib/routes.js +97 -8
- package/lib/wire.d.ts +35 -0
- package/lib/wire.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/** Budget for ONE pinned probe; candidates run in parallel, so the race is bounded by this. */
|
|
2
|
+
export const IP_PROBE_TIMEOUT_MS = 5_000;
|
|
3
|
+
/** How long a winning channel is reused before the IPs are re-resolved. */
|
|
4
|
+
export const IP_CACHE_TTL_MS = 600_000;
|
|
5
|
+
/**
|
|
6
|
+
* Concurrent candidates. Probes run in parallel, so the wall clock is one
|
|
7
|
+
* timeout no matter how many run; this only bounds process count.
|
|
8
|
+
*/
|
|
9
|
+
export const MAX_IP_CANDIDATES = 6;
|
|
10
|
+
/** Hosts this channel applies to. A mirror or an enterprise host has its own DNS. */
|
|
11
|
+
const OPTIMIZABLE_HOSTS = ['github.com'];
|
|
12
|
+
/** DNS-over-HTTPS resolvers, tried in order until one answers. */
|
|
13
|
+
const DOH_URLS = [
|
|
14
|
+
host => `https://dns.alidns.com/resolve?name=${encodeURIComponent(host)}&type=A`,
|
|
15
|
+
host => `https://doh.pub/dns-query?name=${encodeURIComponent(host)}&type=A`,
|
|
16
|
+
host => `https://doh.360.cn/resolve?name=${encodeURIComponent(host)}&type=A`,
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Hostnames whose answers widen the candidate pool for `github.com`.
|
|
20
|
+
*
|
|
21
|
+
* Measured on 2026-11-03: `dns.alidns.com` answered a single address for
|
|
22
|
+
* `github.com` (20.205.243.166) and `doh.pub` answered a different one
|
|
23
|
+
* (140.82.112.3) — one resolver gives one edge, and that edge may be dead.
|
|
24
|
+
* GitHub's edge fleet serves `api.`/`codeload.` from the same ranges, so their
|
|
25
|
+
* answers are extra tickets in the same lottery. Every candidate still has to
|
|
26
|
+
* answer a real `ls-remote` before it is used, so a wrong guess costs one
|
|
27
|
+
* parallel probe and nothing else.
|
|
28
|
+
*/
|
|
29
|
+
const SIBLING_SOURCES = ['api.github.com', 'codeload.github.com'];
|
|
30
|
+
const cache = new Map();
|
|
31
|
+
/** Drop every cached channel (tests, and a user-triggered re-probe). */
|
|
32
|
+
export function resetIpChannelCache() {
|
|
33
|
+
cache.clear();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Pull the IPv4 answers out of one DoH JSON body. Anything malformed answers
|
|
37
|
+
* `[]` rather than throwing: a resolver hiccup must degrade to the direct
|
|
38
|
+
* channel, not fail the push before it starts.
|
|
39
|
+
*/
|
|
40
|
+
export function parseDohIps(payload) {
|
|
41
|
+
if (typeof payload !== 'object' || payload === null)
|
|
42
|
+
return [];
|
|
43
|
+
const answer = payload.Answer;
|
|
44
|
+
if (!Array.isArray(answer))
|
|
45
|
+
return [];
|
|
46
|
+
const ips = [];
|
|
47
|
+
for (const item of answer) {
|
|
48
|
+
if (typeof item !== 'object' || item === null)
|
|
49
|
+
continue;
|
|
50
|
+
const record = item;
|
|
51
|
+
if (record.type !== 1 || typeof record.data !== 'string')
|
|
52
|
+
continue;
|
|
53
|
+
if (!/^\d{1,3}(?:\.\d{1,3}){3}$/.test(record.data))
|
|
54
|
+
continue;
|
|
55
|
+
if (!record.data.split('.').every(part => Number(part) <= 255))
|
|
56
|
+
continue;
|
|
57
|
+
if (!ips.includes(record.data))
|
|
58
|
+
ips.push(record.data);
|
|
59
|
+
}
|
|
60
|
+
return ips;
|
|
61
|
+
}
|
|
62
|
+
/** Budget for one DoH query. Three resolvers are asked in parallel, so this bounds resolution. */
|
|
63
|
+
export const DOH_TIMEOUT_MS = 3_000;
|
|
64
|
+
/** One DoH question, or no answers (a blocked resolver is a normal state, not an error). */
|
|
65
|
+
async function askResolver(url, fetchImpl) {
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetchImpl(url, {
|
|
68
|
+
headers: { accept: 'application/dns-json' },
|
|
69
|
+
signal: AbortSignal.timeout(DOH_TIMEOUT_MS),
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok)
|
|
72
|
+
return [];
|
|
73
|
+
return parseDohIps(await response.json());
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Ask every resolver about one host, in parallel, and union the answers.
|
|
81
|
+
*
|
|
82
|
+
* Sequential fallback was measured to be the wrong shape: the resolvers that
|
|
83
|
+
* are blocked here are blocked for the full timeout each, so a "fallback
|
|
84
|
+
* chain" spends three timeouts to learn what one parallel round trip tells.
|
|
85
|
+
* @param host - hostname to resolve.
|
|
86
|
+
* @param fetchImpl - fetch seam (tests inject a stub).
|
|
87
|
+
*/
|
|
88
|
+
export async function resolveHostIps(host, fetchImpl = fetch) {
|
|
89
|
+
const answers = await Promise.all(DOH_URLS.map(url => askResolver(url(host), fetchImpl)));
|
|
90
|
+
const ips = [];
|
|
91
|
+
for (const answer of answers) {
|
|
92
|
+
for (const ip of answer) {
|
|
93
|
+
if (!ips.includes(ip))
|
|
94
|
+
ips.push(ip);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return ips;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Candidates to race for one endpoint: the endpoint's own answers first, then
|
|
101
|
+
* the sibling services' answers (only for `github.com`), deduplicated and
|
|
102
|
+
* capped at {@link MAX_IP_CANDIDATES}. Everything runs in parallel, so the
|
|
103
|
+
* whole lookup is bounded by {@link DOH_TIMEOUT_MS}.
|
|
104
|
+
* @param host - endpoint hostname.
|
|
105
|
+
* @param fetchImpl - fetch seam.
|
|
106
|
+
*/
|
|
107
|
+
export async function candidateIpsFor(host, fetchImpl = fetch) {
|
|
108
|
+
const sources = host === 'github.com' ? [host, ...SIBLING_SOURCES] : [host];
|
|
109
|
+
const answers = await Promise.all(sources.map(source => resolveHostIps(source, fetchImpl)));
|
|
110
|
+
const ips = [];
|
|
111
|
+
for (const answer of answers) {
|
|
112
|
+
for (const ip of answer) {
|
|
113
|
+
if (!ips.includes(ip))
|
|
114
|
+
ips.push(ip);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return ips.slice(0, MAX_IP_CANDIDATES);
|
|
118
|
+
}
|
|
119
|
+
/** Whether this endpoint is one the pinned-IP channel may rewrite. */
|
|
120
|
+
export function isOptimizableEndpoint(endpoint) {
|
|
121
|
+
try {
|
|
122
|
+
const url = new URL(endpoint);
|
|
123
|
+
return url.protocol === 'https:' && OPTIMIZABLE_HOSTS.includes(url.hostname);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The argv that pins `host:443` to one IP for a single command. `-c` goes
|
|
131
|
+
* before the subcommand, per git's global-option rule.
|
|
132
|
+
*/
|
|
133
|
+
export function pinArgs(host, ip) {
|
|
134
|
+
return ['-c', `http.curloptResolve=${host}:443:${ip}`];
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Race up to {@link MAX_IP_CANDIDATES} pinned channels and answer the argv of
|
|
138
|
+
* the first that actually answers `ls-remote`. "First" means first to
|
|
139
|
+
* SUCCEED: all candidates start together and a slow-but-working IP can beat
|
|
140
|
+
* one that fails later.
|
|
141
|
+
* @param ips - candidates in resolver order.
|
|
142
|
+
* @param host - hostname being pinned.
|
|
143
|
+
* @param probe - one pinned probe (the caller supplies the budget + argv).
|
|
144
|
+
* @returns the winning `-c` argv, or undefined when every candidate failed.
|
|
145
|
+
*/
|
|
146
|
+
export async function racePinnedChannels(ips, host, probe) {
|
|
147
|
+
const candidates = ips.slice(0, MAX_IP_CANDIDATES);
|
|
148
|
+
const results = await Promise.all(candidates.map(async (ip) => {
|
|
149
|
+
const args = pinArgs(host, ip);
|
|
150
|
+
try {
|
|
151
|
+
await probe(args);
|
|
152
|
+
return args;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
// Promise.all keeps resolver order, so a tie goes to the first answer.
|
|
159
|
+
return results.find(item => item !== undefined);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The channel a read/write may fall back to when the direct one fails in a
|
|
163
|
+
* retryable way. Wraps resolution + race + caching; answers undefined for
|
|
164
|
+
* endpoints outside {@link OPTIMIZABLE_HOSTS} or when nothing works.
|
|
165
|
+
* @param options.endpoint - the URL the command would use.
|
|
166
|
+
* @param options.probe - one pinned probe, built by {@link pinnedProbe}.
|
|
167
|
+
* @param options.now - clock seam for the cache.
|
|
168
|
+
* @param options.fetchImpl - DoH fetch seam.
|
|
169
|
+
*/
|
|
170
|
+
export async function pinnedChannelFor(options) {
|
|
171
|
+
const { endpoint, probe } = options;
|
|
172
|
+
if (!isOptimizableEndpoint(endpoint))
|
|
173
|
+
return undefined;
|
|
174
|
+
const host = new URL(endpoint).hostname;
|
|
175
|
+
const now = options.now ?? Date.now;
|
|
176
|
+
const cached = cache.get(endpoint);
|
|
177
|
+
if (cached !== undefined && now() - cached.at < IP_CACHE_TTL_MS)
|
|
178
|
+
return cached.args;
|
|
179
|
+
const ips = await candidateIpsFor(host, options.fetchImpl);
|
|
180
|
+
if (ips.length === 0)
|
|
181
|
+
return undefined;
|
|
182
|
+
const args = await racePinnedChannels(ips, host, probe);
|
|
183
|
+
if (args !== undefined)
|
|
184
|
+
cache.set(endpoint, { args, at: now() });
|
|
185
|
+
return args;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Build the probe the race runs: one pinned `ls-remote` at the probe budget,
|
|
189
|
+
* with terminal prompts disabled so a credentialed endpoint fails instead of
|
|
190
|
+
* hanging on an invisible dialog.
|
|
191
|
+
* @param exec - executor seam.
|
|
192
|
+
* @param cwd - any directory inside the repository.
|
|
193
|
+
* @param endpoint - URL being probed.
|
|
194
|
+
* @param authArgs - per-host credential argv.
|
|
195
|
+
*/
|
|
196
|
+
export function pinnedProbe(exec, cwd, endpoint, authArgs = []) {
|
|
197
|
+
return async (args) => {
|
|
198
|
+
const result = await exec('git', [...args, ...authArgs, 'ls-remote', '--heads', endpoint], {
|
|
199
|
+
cwd,
|
|
200
|
+
timeoutMs: IP_PROBE_TIMEOUT_MS,
|
|
201
|
+
env: { GIT_TERMINAL_PROMPT: '0' },
|
|
202
|
+
});
|
|
203
|
+
if (result.code !== 0)
|
|
204
|
+
throw new Error(result.errorMessage ?? result.stderr ?? 'pinned probe failed');
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel diagnostics for the settings UI: one read of the endpoint per
|
|
3
|
+
* channel, so a user whose network is failing can see WHICH path reaches
|
|
4
|
+
* GitHub instead of guessing.
|
|
5
|
+
*
|
|
6
|
+
* A probe is the same judgement the write path makes (0041/0042), run on
|
|
7
|
+
* demand: the channel is good only when `ls-remote` exits 0. A TCP-level
|
|
8
|
+
* "reachable" proves nothing — measured on 2026-11-03, 7 of 8 GitHub edges
|
|
9
|
+
* accepted a connection and still failed the read.
|
|
10
|
+
*
|
|
11
|
+
* @module git-worktree/network-probe
|
|
12
|
+
*/
|
|
13
|
+
import type { Exec } from './git.js';
|
|
14
|
+
import type { NetworkChannelId, NetworkChannelReport } from './wire.js';
|
|
15
|
+
/**
|
|
16
|
+
* Probe one channel by reading the endpoint through it.
|
|
17
|
+
*
|
|
18
|
+
* Failures are returned as data: a dead channel must not hide the next one,
|
|
19
|
+
* and the caller renders every report side by side.
|
|
20
|
+
* @param exec - executor seam.
|
|
21
|
+
* @param cwd - any directory inside the repository.
|
|
22
|
+
* @param channel - which channel this argv stands for.
|
|
23
|
+
* @param url - endpoint to read.
|
|
24
|
+
* @param channelArgs - the channel's argv prefix (empty for direct).
|
|
25
|
+
* @param authArgs - credential argv for this host.
|
|
26
|
+
* @returns the channel's verdict, failures included.
|
|
27
|
+
*/
|
|
28
|
+
export declare function channelReport(exec: Exec, cwd: string, channel: NetworkChannelId, url: string, channelArgs: readonly string[], authArgs?: readonly string[]): Promise<NetworkChannelReport>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel diagnostics for the settings UI: one read of the endpoint per
|
|
3
|
+
* channel, so a user whose network is failing can see WHICH path reaches
|
|
4
|
+
* GitHub instead of guessing.
|
|
5
|
+
*
|
|
6
|
+
* A probe is the same judgement the write path makes (0041/0042), run on
|
|
7
|
+
* demand: the channel is good only when `ls-remote` exits 0. A TCP-level
|
|
8
|
+
* "reachable" proves nothing — measured on 2026-11-03, 7 of 8 GitHub edges
|
|
9
|
+
* accepted a connection and still failed the read.
|
|
10
|
+
*
|
|
11
|
+
* @module git-worktree/network-probe
|
|
12
|
+
*/
|
|
13
|
+
import { PROBE_TIMEOUT_MS } from './network.js';
|
|
14
|
+
const DETAIL_LIMIT = 300;
|
|
15
|
+
/**
|
|
16
|
+
* Keep a failure readable in a toast: the first non-empty line, capped.
|
|
17
|
+
* @param text - raw stderr or error message.
|
|
18
|
+
*/
|
|
19
|
+
function firstLine(text) {
|
|
20
|
+
const line = text.split(/\r?\n/).find(item => item.trim() !== '') ?? '';
|
|
21
|
+
return line.length > DETAIL_LIMIT ? `${line.slice(0, DETAIL_LIMIT)}…` : line;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Probe one channel by reading the endpoint through it.
|
|
25
|
+
*
|
|
26
|
+
* Failures are returned as data: a dead channel must not hide the next one,
|
|
27
|
+
* and the caller renders every report side by side.
|
|
28
|
+
* @param exec - executor seam.
|
|
29
|
+
* @param cwd - any directory inside the repository.
|
|
30
|
+
* @param channel - which channel this argv stands for.
|
|
31
|
+
* @param url - endpoint to read.
|
|
32
|
+
* @param channelArgs - the channel's argv prefix (empty for direct).
|
|
33
|
+
* @param authArgs - credential argv for this host.
|
|
34
|
+
* @returns the channel's verdict, failures included.
|
|
35
|
+
*/
|
|
36
|
+
export async function channelReport(exec, cwd, channel, url, channelArgs, authArgs = []) {
|
|
37
|
+
const started = Date.now();
|
|
38
|
+
const result = await exec('git', [...channelArgs, ...authArgs, 'ls-remote', '--heads', url], {
|
|
39
|
+
cwd,
|
|
40
|
+
timeoutMs: PROBE_TIMEOUT_MS,
|
|
41
|
+
env: { GIT_TERMINAL_PROMPT: '0' },
|
|
42
|
+
});
|
|
43
|
+
const elapsedMs = Date.now() - started;
|
|
44
|
+
if (result.code === 0)
|
|
45
|
+
return { channel, ok: true, elapsedMs };
|
|
46
|
+
return { channel, ok: false, elapsedMs, detail: firstLine(result.errorMessage ?? result.stderr) };
|
|
47
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The third network channel: an explicit proxy, or the one Windows already
|
|
3
|
+
* has configured.
|
|
4
|
+
*
|
|
5
|
+
* Why the registry matters: git links libcurl, and libcurl does NOT read the
|
|
6
|
+
* WinINET/PAC settings that every browser on the machine honours. Measured on
|
|
7
|
+
* 2026-11-03 with a dead local proxy: `git ls-remote` failed with
|
|
8
|
+
* `Failed to connect to github.com:443 over proxy 127.0.0.1` the moment
|
|
9
|
+
* `-c http.proxy=…` was passed, i.e. git honours the argv and ignores the
|
|
10
|
+
* system setting entirely. A `gitport`-style tool bundles its own proxy; this
|
|
11
|
+
* plugin instead borrows the user's, and needs a way to find it.
|
|
12
|
+
*/
|
|
13
|
+
import type { Exec } from './git.js';
|
|
14
|
+
import { proxyUrlError } from './normalize.js';
|
|
15
|
+
/** A registry read is local and cheap; it must not become the slowest step. */
|
|
16
|
+
export declare const SYSTEM_PROXY_TIMEOUT_MS = 3000;
|
|
17
|
+
/** Re-exported so host callers keep one import site for the proxy rules. */
|
|
18
|
+
export { proxyUrlError };
|
|
19
|
+
/** The argv that routes one command through the proxy; `-c` precedes the subcommand. */
|
|
20
|
+
export declare function proxyArgs(url: string): readonly string[];
|
|
21
|
+
/**
|
|
22
|
+
* Parse the `ProxyServer` value WinINET stores, which is either `host:port` or
|
|
23
|
+
* a per-scheme list like `http=host:port;https=host:port`. The https entry
|
|
24
|
+
* wins; a plain value is used as is. Anything malformed answers undefined.
|
|
25
|
+
* @param raw - stdout of `reg query … /v ProxyServer`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseProxyServer(raw: string): string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The system proxy Windows already has configured, when it is enabled and
|
|
30
|
+
* points somewhere usable; undefined otherwise (off Windows, disabled, empty,
|
|
31
|
+
* or unparseable). Read-only: nothing here changes the machine's settings.
|
|
32
|
+
* @param exec - executor seam.
|
|
33
|
+
* @param cwd - directory for the `reg` calls.
|
|
34
|
+
*/
|
|
35
|
+
export declare function systemProxyUrl(exec: Exec, cwd: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { proxyUrlError } from './normalize.js';
|
|
2
|
+
/** A registry read is local and cheap; it must not become the slowest step. */
|
|
3
|
+
export const SYSTEM_PROXY_TIMEOUT_MS = 3_000;
|
|
4
|
+
const INTERNET_SETTINGS = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings';
|
|
5
|
+
/** Re-exported so host callers keep one import site for the proxy rules. */
|
|
6
|
+
export { proxyUrlError };
|
|
7
|
+
/** The argv that routes one command through the proxy; `-c` precedes the subcommand. */
|
|
8
|
+
export function proxyArgs(url) {
|
|
9
|
+
return ['-c', `http.proxy=${url}`];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parse the `ProxyServer` value WinINET stores, which is either `host:port` or
|
|
13
|
+
* a per-scheme list like `http=host:port;https=host:port`. The https entry
|
|
14
|
+
* wins; a plain value is used as is. Anything malformed answers undefined.
|
|
15
|
+
* @param raw - stdout of `reg query … /v ProxyServer`.
|
|
16
|
+
*/
|
|
17
|
+
export function parseProxyServer(raw) {
|
|
18
|
+
const line = raw.split(/\r?\n/).find(item => /ProxyServer\s+REG_SZ/i.test(item));
|
|
19
|
+
if (line === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
const value = line.replace(/^.*?ProxyServer\s+REG_SZ\s*/i, '').trim();
|
|
22
|
+
if (value === '')
|
|
23
|
+
return undefined;
|
|
24
|
+
if (!value.includes('='))
|
|
25
|
+
return value.includes(':') ? `http://${value}` : undefined;
|
|
26
|
+
const entries = value.split(';').map(item => item.trim()).filter(item => item !== '');
|
|
27
|
+
const https = entries.find(item => /^https=/i.test(item));
|
|
28
|
+
const chosen = https ?? entries.find(item => /^http=/i.test(item)) ?? entries[0];
|
|
29
|
+
const address = chosen?.replace(/^[a-z]+=/i, '').trim();
|
|
30
|
+
if (address === undefined || address === '' || !address.includes(':'))
|
|
31
|
+
return undefined;
|
|
32
|
+
const scheme = https === undefined ? 'http' : 'https';
|
|
33
|
+
return `${scheme}://${address}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The system proxy Windows already has configured, when it is enabled and
|
|
37
|
+
* points somewhere usable; undefined otherwise (off Windows, disabled, empty,
|
|
38
|
+
* or unparseable). Read-only: nothing here changes the machine's settings.
|
|
39
|
+
* @param exec - executor seam.
|
|
40
|
+
* @param cwd - directory for the `reg` calls.
|
|
41
|
+
*/
|
|
42
|
+
export async function systemProxyUrl(exec, cwd) {
|
|
43
|
+
if (process.platform !== 'win32')
|
|
44
|
+
return undefined;
|
|
45
|
+
const enabled = await exec('reg', ['query', INTERNET_SETTINGS, '/v', 'ProxyEnable'], { cwd, timeoutMs: SYSTEM_PROXY_TIMEOUT_MS });
|
|
46
|
+
if (enabled.code !== 0 || !/ProxyEnable\s+REG_DWORD\s+0x1\b/i.test(enabled.stdout))
|
|
47
|
+
return undefined;
|
|
48
|
+
const server = await exec('reg', ['query', INTERNET_SETTINGS, '/v', 'ProxyServer'], { cwd, timeoutMs: SYSTEM_PROXY_TIMEOUT_MS });
|
|
49
|
+
if (server.code !== 0)
|
|
50
|
+
return undefined;
|
|
51
|
+
const url = parseProxyServer(server.stdout);
|
|
52
|
+
if (url === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
return proxyUrlError(url) === undefined ? url : undefined;
|
|
55
|
+
}
|
package/lib/network-tool.js
CHANGED
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { isAbsolute } from 'node:path';
|
|
20
20
|
import { basicUsername, createLocalCredentialStore, } from './credentials.js';
|
|
21
|
-
import { childProcessExec
|
|
21
|
+
import { childProcessExec } from './git.js';
|
|
22
|
+
import { FETCH_TIMEOUT_MS } from './network.js';
|
|
22
23
|
/** Model-facing tool name. */
|
|
23
24
|
export const GIT_NETWORK_TOOL_NAME = 'git_network';
|
|
24
25
|
/** The only git subcommands this tool will run. */
|
|
@@ -224,7 +225,7 @@ export function gitNetworkTool(deps) {
|
|
|
224
225
|
// the same token serves this command and a plain shell `git push`.
|
|
225
226
|
const result = await exec('git', request.args, {
|
|
226
227
|
cwd: request.repoPath,
|
|
227
|
-
timeoutMs:
|
|
228
|
+
timeoutMs: FETCH_TIMEOUT_MS,
|
|
228
229
|
});
|
|
229
230
|
const stdout = cap(redactSecrets(result.stdout, entries));
|
|
230
231
|
const stderrSource = result.stderr.trim() === '' && result.errorMessage !== undefined
|
package/lib/network.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network policy for the host half: how long each remote operation may take,
|
|
3
|
+
* what a failed one actually means, and how a push that timed out is
|
|
4
|
+
* reconciled with the remote afterwards. Nothing here spawns a process — the
|
|
5
|
+
* budgets and the classifier are plain data so both the git wrapper and the
|
|
6
|
+
* routes can use them without a second seam.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Budget for one reachability probe (`ls-remote`): a probe exists to fail
|
|
10
|
+
* FAST, so a dead host costs seconds, not the minutes a real transfer gets.
|
|
11
|
+
*/
|
|
12
|
+
export declare const PROBE_TIMEOUT_MS = 10000;
|
|
13
|
+
/**
|
|
14
|
+
* Budget for the READ half of the network (fetch/update): a slow link with a
|
|
15
|
+
* real pack transfer outruns the local default, so this is deliberately wide.
|
|
16
|
+
*/
|
|
17
|
+
export declare const FETCH_TIMEOUT_MS = 300000;
|
|
18
|
+
/**
|
|
19
|
+
* Budget for the WRITE half (push/push-tag). Wider than fetch: a push that
|
|
20
|
+
* is killed mid-transfer may already have updated the remote refs, and the
|
|
21
|
+
* caller pays for that ambiguity with a verification round-trip.
|
|
22
|
+
*/
|
|
23
|
+
export declare const PUSH_TIMEOUT_MS = 600000;
|
|
24
|
+
/** Budget for the post-timeout reconciliation (`ls-remote` after a push). */
|
|
25
|
+
export declare const VERIFY_TIMEOUT_MS = 10000;
|
|
26
|
+
/** What a failed git network command most likely means. */
|
|
27
|
+
export type GitFailureKind = 'timeout' | 'unreachable' | 'reset' | 'proxy' | 'tls' | 'credentials' | 'diverged' | 'protected' | 'unknown';
|
|
28
|
+
/** Diagnosis of one failed remote command. */
|
|
29
|
+
export interface GitFailure {
|
|
30
|
+
/** The shape of the failure; the UI may key wording off it later. */
|
|
31
|
+
kind: GitFailureKind;
|
|
32
|
+
/**
|
|
33
|
+
* Whether another network channel (a pinned IP, a proxy) could plausibly
|
|
34
|
+
* make the SAME command succeed. Credential, TLS, divergence and hook
|
|
35
|
+
* failures are about state, not reachability, so they never retry.
|
|
36
|
+
*/
|
|
37
|
+
retryable: boolean;
|
|
38
|
+
/** One sentence the caller may append to the toast; undefined = say nothing. */
|
|
39
|
+
hint: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Classify one failed git network command from its diagnostic text.
|
|
43
|
+
* @param stderr - git's diagnostic (stderr, or the executor's fallback text).
|
|
44
|
+
* @param options - `timedOut` when the executor killed the process at its
|
|
45
|
+
* budget; that outranks the text, because a killed push carries no verdict.
|
|
46
|
+
* @returns the kind, whether another channel may help, and a one-line hint.
|
|
47
|
+
*/
|
|
48
|
+
export declare function classifyGitFailure(stderr: string, options?: {
|
|
49
|
+
timedOut?: boolean;
|
|
50
|
+
}): GitFailure;
|
|
51
|
+
/**
|
|
52
|
+
* Append a classified hint to git's own message, once: the raw diagnostic
|
|
53
|
+
* stays first so the user can still copy it into a bug report.
|
|
54
|
+
* @param message - the existing error text.
|
|
55
|
+
* @param failure - classifier output.
|
|
56
|
+
* @returns the message with the hint appended, unchanged when there is none.
|
|
57
|
+
*/
|
|
58
|
+
export declare function withHint(message: string, failure: GitFailure): string;
|
package/lib/network.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network policy for the host half: how long each remote operation may take,
|
|
3
|
+
* what a failed one actually means, and how a push that timed out is
|
|
4
|
+
* reconciled with the remote afterwards. Nothing here spawns a process — the
|
|
5
|
+
* budgets and the classifier are plain data so both the git wrapper and the
|
|
6
|
+
* routes can use them without a second seam.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Budget for one reachability probe (`ls-remote`): a probe exists to fail
|
|
10
|
+
* FAST, so a dead host costs seconds, not the minutes a real transfer gets.
|
|
11
|
+
*/
|
|
12
|
+
export const PROBE_TIMEOUT_MS = 10_000;
|
|
13
|
+
/**
|
|
14
|
+
* Budget for the READ half of the network (fetch/update): a slow link with a
|
|
15
|
+
* real pack transfer outruns the local default, so this is deliberately wide.
|
|
16
|
+
*/
|
|
17
|
+
export const FETCH_TIMEOUT_MS = 300_000;
|
|
18
|
+
/**
|
|
19
|
+
* Budget for the WRITE half (push/push-tag). Wider than fetch: a push that
|
|
20
|
+
* is killed mid-transfer may already have updated the remote refs, and the
|
|
21
|
+
* caller pays for that ambiguity with a verification round-trip.
|
|
22
|
+
*/
|
|
23
|
+
export const PUSH_TIMEOUT_MS = 600_000;
|
|
24
|
+
/** Budget for the post-timeout reconciliation (`ls-remote` after a push). */
|
|
25
|
+
export const VERIFY_TIMEOUT_MS = 10_000;
|
|
26
|
+
/** Patterns are matched in order: the first hit wins. */
|
|
27
|
+
const RULES = [
|
|
28
|
+
{
|
|
29
|
+
kind: 'proxy',
|
|
30
|
+
retryable: false,
|
|
31
|
+
hint: 'The configured proxy refused or failed the CONNECT tunnel; check the proxy address or turn it off.',
|
|
32
|
+
test: text => /over proxy/i.test(text)
|
|
33
|
+
|| /Proxy CONNECT aborted/i.test(text)
|
|
34
|
+
|| /CONNECT tunnel failed/i.test(text)
|
|
35
|
+
|| /Received HTTP code 407/i.test(text),
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
kind: 'credentials',
|
|
39
|
+
retryable: false,
|
|
40
|
+
hint: 'Git could not read usable credentials; sign in for this host in the credentials panel, then retry.',
|
|
41
|
+
test: text => /terminal prompts disabled/i.test(text)
|
|
42
|
+
|| /could not read Username/i.test(text)
|
|
43
|
+
|| /Authentication failed/i.test(text)
|
|
44
|
+
|| /Permission denied \(publickey/i.test(text)
|
|
45
|
+
|| /HTTP 40[13]\b/.test(text),
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
kind: 'unreachable',
|
|
49
|
+
retryable: true,
|
|
50
|
+
hint: 'The host could not be reached; retry, then pin an IP or use a proxy.',
|
|
51
|
+
test: text => /Could not resolve host/i.test(text)
|
|
52
|
+
|| /Failed to connect/i.test(text)
|
|
53
|
+
|| /Could not connect to server/i.test(text)
|
|
54
|
+
|| /Connection refused/i.test(text)
|
|
55
|
+
|| /Network is unreachable/i.test(text),
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
kind: 'reset',
|
|
59
|
+
retryable: true,
|
|
60
|
+
hint: 'The connection was reset mid-transfer; retry, then pin an IP or use a proxy.',
|
|
61
|
+
test: text => /Connection was reset/i.test(text)
|
|
62
|
+
|| /Recv failure/i.test(text)
|
|
63
|
+
|| /RPC failed/i.test(text)
|
|
64
|
+
|| /early EOF/i.test(text)
|
|
65
|
+
|| /remote end hung up unexpectedly/i.test(text)
|
|
66
|
+
|| /unexpected disconnect/i.test(text),
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
kind: 'tls',
|
|
70
|
+
retryable: false,
|
|
71
|
+
hint: 'TLS negotiation failed; the network is intercepting the connection or the certificate is untrusted.',
|
|
72
|
+
test: text => /SSL certificate problem/i.test(text)
|
|
73
|
+
|| /schannel/i.test(text)
|
|
74
|
+
|| /SSL_ERROR/i.test(text)
|
|
75
|
+
|| /tlsv1 alert/i.test(text),
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
kind: 'diverged',
|
|
79
|
+
retryable: false,
|
|
80
|
+
hint: 'The remote has commits you do not have; pull or rebase before pushing (the plugin never forces).',
|
|
81
|
+
test: text => /non-fast-forward/i.test(text)
|
|
82
|
+
|| /fetch first/i.test(text)
|
|
83
|
+
|| /Not possible to fast-forward/i.test(text)
|
|
84
|
+
|| /\[rejected\]/i.test(text),
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
kind: 'protected',
|
|
88
|
+
retryable: false,
|
|
89
|
+
hint: 'A server-side rule refused the update (protected branch or hook); resolve it on the host, not in the network.',
|
|
90
|
+
test: text => /protected branch/i.test(text)
|
|
91
|
+
|| /hook declined/i.test(text)
|
|
92
|
+
|| /pre-receive hook/i.test(text),
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
/**
|
|
96
|
+
* Classify one failed git network command from its diagnostic text.
|
|
97
|
+
* @param stderr - git's diagnostic (stderr, or the executor's fallback text).
|
|
98
|
+
* @param options - `timedOut` when the executor killed the process at its
|
|
99
|
+
* budget; that outranks the text, because a killed push carries no verdict.
|
|
100
|
+
* @returns the kind, whether another channel may help, and a one-line hint.
|
|
101
|
+
*/
|
|
102
|
+
export function classifyGitFailure(stderr, options = {}) {
|
|
103
|
+
if (options.timedOut === true) {
|
|
104
|
+
return {
|
|
105
|
+
kind: 'timeout',
|
|
106
|
+
retryable: true,
|
|
107
|
+
hint: 'The command did not finish within its time budget; the remote may still be applying it — verify before retrying a push.',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const text = stderr.trim();
|
|
111
|
+
for (const rule of RULES) {
|
|
112
|
+
if (rule.test(text))
|
|
113
|
+
return { kind: rule.kind, retryable: rule.retryable, hint: rule.hint };
|
|
114
|
+
}
|
|
115
|
+
return { kind: 'unknown', retryable: false, hint: undefined };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Append a classified hint to git's own message, once: the raw diagnostic
|
|
119
|
+
* stays first so the user can still copy it into a bug report.
|
|
120
|
+
* @param message - the existing error text.
|
|
121
|
+
* @param failure - classifier output.
|
|
122
|
+
* @returns the message with the hint appended, unchanged when there is none.
|
|
123
|
+
*/
|
|
124
|
+
export function withHint(message, failure) {
|
|
125
|
+
if (failure.hint === undefined || message.includes(failure.hint))
|
|
126
|
+
return message;
|
|
127
|
+
return `${message}\n\n${failure.hint}`;
|
|
128
|
+
}
|
package/lib/normalize.d.ts
CHANGED
|
@@ -75,8 +75,8 @@ export type AccelerateUrlIssue = 'empty' | 'invalid';
|
|
|
75
75
|
export declare function accelerateUrlError(value: string): AccelerateUrlIssue | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* Normalize an accepted accelerate URL: trim, and append the trailing `/`
|
|
78
|
-
* the prefix concatenation requires (`https://gh-proxy.com/` +
|
|
79
|
-
*
|
|
78
|
+
* the prefix concatenation requires (`https://gh-proxy.com/` + the complete
|
|
79
|
+
* `https://github.com/o/r.git` URL). accelArgs builds the Git rewrite base.
|
|
80
80
|
* @param value - an accepted (non-empty, http(s), whitespace-free) URL.
|
|
81
81
|
* @returns the prefix ending in `/`.
|
|
82
82
|
*/
|
|
@@ -105,3 +105,15 @@ export type RemoteUrlIssue = 'empty' | 'whitespace' | 'invalid';
|
|
|
105
105
|
* @returns the issue kind, or undefined when acceptable.
|
|
106
106
|
*/
|
|
107
107
|
export declare function remoteUrlIssue(url: string): RemoteUrlIssue | undefined;
|
|
108
|
+
/** Schemes git's libcurl accepts for an HTTP(S) remote. */
|
|
109
|
+
export declare const PROXY_SCHEMES: readonly string[];
|
|
110
|
+
/**
|
|
111
|
+
* Why this proxy value cannot be used, or undefined when it can.
|
|
112
|
+
*
|
|
113
|
+
* Credentials are rejected on purpose: the value is stored in the plugin
|
|
114
|
+
* config and passed as one `-c` argument, so `user:pass@` would be readable
|
|
115
|
+
* in the settings UI and in any diagnostics (0043).
|
|
116
|
+
* @param value - candidate proxy URL.
|
|
117
|
+
* @returns the user-facing reason, or undefined.
|
|
118
|
+
*/
|
|
119
|
+
export declare function proxyUrlError(value: string): string | undefined;
|
package/lib/normalize.js
CHANGED
|
@@ -117,8 +117,8 @@ export function accelerateUrlError(value) {
|
|
|
117
117
|
}
|
|
118
118
|
/**
|
|
119
119
|
* Normalize an accepted accelerate URL: trim, and append the trailing `/`
|
|
120
|
-
* the prefix concatenation requires (`https://gh-proxy.com/` +
|
|
121
|
-
*
|
|
120
|
+
* the prefix concatenation requires (`https://gh-proxy.com/` + the complete
|
|
121
|
+
* `https://github.com/o/r.git` URL). accelArgs builds the Git rewrite base.
|
|
122
122
|
* @param value - an accepted (non-empty, http(s), whitespace-free) URL.
|
|
123
123
|
* @returns the prefix ending in `/`.
|
|
124
124
|
*/
|
|
@@ -173,3 +173,39 @@ export function remoteUrlIssue(url) {
|
|
|
173
173
|
return undefined;
|
|
174
174
|
return 'invalid';
|
|
175
175
|
}
|
|
176
|
+
/** Schemes git's libcurl accepts for an HTTP(S) remote. */
|
|
177
|
+
export const PROXY_SCHEMES = ['http', 'https', 'socks5', 'socks5h'];
|
|
178
|
+
/**
|
|
179
|
+
* Why this proxy value cannot be used, or undefined when it can.
|
|
180
|
+
*
|
|
181
|
+
* Credentials are rejected on purpose: the value is stored in the plugin
|
|
182
|
+
* config and passed as one `-c` argument, so `user:pass@` would be readable
|
|
183
|
+
* in the settings UI and in any diagnostics (0043).
|
|
184
|
+
* @param value - candidate proxy URL.
|
|
185
|
+
* @returns the user-facing reason, or undefined.
|
|
186
|
+
*/
|
|
187
|
+
export function proxyUrlError(value) {
|
|
188
|
+
let url;
|
|
189
|
+
try {
|
|
190
|
+
url = new URL(value);
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
return 'must be an absolute proxy URL such as http://127.0.0.1:7890';
|
|
194
|
+
}
|
|
195
|
+
if (!PROXY_SCHEMES.includes(url.protocol.replace(':', ''))) {
|
|
196
|
+
return `scheme must be one of ${PROXY_SCHEMES.join(', ')}`;
|
|
197
|
+
}
|
|
198
|
+
if (url.hostname === '')
|
|
199
|
+
return 'must name a proxy host';
|
|
200
|
+
if (url.port === '')
|
|
201
|
+
return 'must name a proxy port';
|
|
202
|
+
const port = Number(url.port);
|
|
203
|
+
if (!Number.isInteger(port) || port < 1 || port > 65_535)
|
|
204
|
+
return 'proxy port must be 1-65535';
|
|
205
|
+
if (url.username !== '' || url.password !== '')
|
|
206
|
+
return 'credentials in the proxy URL are not supported';
|
|
207
|
+
if ((url.pathname !== '' && url.pathname !== '/') || url.search !== '' || url.hash !== '') {
|
|
208
|
+
return 'must not carry a path, query, or fragment';
|
|
209
|
+
}
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|