dsh-mo-image 0.7.0 → 1.0.1

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/lib/net.js CHANGED
@@ -33,7 +33,15 @@ function dispatcherFor(proxyUrl) {
33
33
  }
34
34
 
35
35
  function hostOf(url) {
36
- try { return new URL(String(url)).host || String(url); } catch (_error) { return String(url); }
36
+ try { return new URL(String(url)).host || '未知主机'; } catch (_error) { return '未知主机'; }
37
+ }
38
+
39
+ function proxyEndpointOf(proxyUrl) {
40
+ try {
41
+ const url = new URL(proxyUrl);
42
+ if (url.host) return url.protocol + '//' + url.host;
43
+ } catch (_error) { /* malformed input can contain credentials */ }
44
+ return '代理地址不可用';
37
45
  }
38
46
 
39
47
  export async function resolveRequestProxy(url, options = {}) {
@@ -54,6 +62,21 @@ function causeCode(error) {
54
62
  return '';
55
63
  }
56
64
 
65
+ const PROXY_ERROR_NAMES = new Set(['Error', 'TypeError', 'RangeError', 'URIError', 'AbortError', 'TimeoutError']);
66
+
67
+ // Preserve metadata used for retry and timeout decisions. A proxy/transport can
68
+ // echo credentials in arbitrary messages or extra properties on the original error.
69
+ function safeProxyCause(error, depth = 0) {
70
+ if (!error || depth >= 5) return undefined;
71
+ const name = PROXY_ERROR_NAMES.has(error.name) ? error.name : 'Error';
72
+ const code = error.code;
73
+ const validCode = (typeof code === 'string' || typeof code === 'number') && /^[A-Z0-9_]{1,80}$/.test(String(code));
74
+ const safe = new Error(validCode ? String(code) : name, { cause: safeProxyCause(error.cause, depth + 1) });
75
+ safe.name = name;
76
+ if (validCode) safe.code = code;
77
+ return safe;
78
+ }
79
+
57
80
  const HINTS = {
58
81
  UND_ERR_CONNECT_TIMEOUT: 'TCP 连接超时',
59
82
  UND_ERR_HEADERS_TIMEOUT: '等待响应头超时',
@@ -85,13 +108,23 @@ const HINT_KINDS = {
85
108
  DEPTH_ZERO_SELF_SIGNED_CERT: 'tls'
86
109
  };
87
110
 
88
- export function describeNetworkError(error, url, decision) {
111
+ function networkDiagnostic(error, url, decision) {
89
112
  const host = hostOf(url);
90
- const code = causeCode(error) || (error && error.code) || '';
113
+ const proxied = Boolean(decision?.proxyUrl);
114
+ const cause = proxied ? safeProxyCause(error) : error;
115
+ const code = causeCode(cause);
116
+ const detail = code || (proxied ? '代理请求失败' : cause?.message || '');
117
+ return { host, proxied, cause, code, detail };
118
+ }
119
+
120
+ function describeDiagnostic({ host, proxied, code, detail }) {
91
121
  const hint = HINTS[code] || '';
92
- const via = decision && decision.proxyUrl ? '经系统网络配置' : '直连';
93
- const detail = [code || (error && error.message) || '未知错误', via].join(',');
94
- return '网络请求失败:' + host + ' ' + detail + '。' + (hint ? hint + '。' : '');
122
+ const via = proxied ? '经系统网络配置' : '直连';
123
+ return '网络请求失败:' + host + ' ' + (detail || '未知错误') + ',' + via + '。' + (hint ? hint + '。' : '');
124
+ }
125
+
126
+ export function describeNetworkError(error, url, decision) {
127
+ return describeDiagnostic(networkDiagnostic(error, url, decision));
95
128
  }
96
129
 
97
130
  /**
@@ -99,14 +132,13 @@ export function describeNetworkError(error, url, decision) {
99
132
  * code/params 让设置页按当前语言重写整句。
100
133
  */
101
134
  export function networkFailure(error, url, decision) {
102
- const host = hostOf(url);
103
- const code = causeCode(error) || (error && error.code) || '';
104
- const wrapped = new Error(describeNetworkError(error, url, decision), { cause: error });
135
+ const diagnostic = networkDiagnostic(error, url, decision);
136
+ const wrapped = new Error(describeDiagnostic(diagnostic), { cause: diagnostic.cause });
105
137
  return localizedError(wrapped, 'network.failed', {
106
- host: host,
107
- cause: code || (error && error.message) || '',
108
- via: decision && decision.proxyUrl ? 'proxy' : 'direct',
109
- hint: HINT_KINDS[code] || ''
138
+ host: diagnostic.host,
139
+ cause: diagnostic.detail,
140
+ via: diagnostic.proxied ? 'proxy' : 'direct',
141
+ hint: HINT_KINDS[diagnostic.code] || ''
110
142
  });
111
143
  }
112
144
 
@@ -128,26 +160,29 @@ export async function request(url, init = {}, options = {}) {
128
160
  }
129
161
  }
130
162
 
131
- const dispatcher = (() => {
132
- try {
133
- return dispatcherFor(decision.proxyUrl);
134
- } catch (error) {
135
- throw localizedError(new Error('网络连接配置无效:' + (error && error.message ? error.message : error)), 'network.routeInvalid');
136
- }
137
- })();
138
163
  const host = hostOf(url);
139
- const key = decision.proxyUrl + '|' + host;
164
+ const endpoint = proxyEndpointOf(decision.proxyUrl);
165
+ const source = '(来源 ' + decision.source + ')';
166
+ let dispatcher;
167
+ try {
168
+ dispatcher = dispatcherFor(decision.proxyUrl);
169
+ } catch (error) {
170
+ logLine('network: ' + host + ' 的代理配置无效 ' + endpoint + source);
171
+ throw localizedError(new Error('网络连接配置无效:' + error.message), 'network.routeInvalid');
172
+ }
173
+ const key = endpoint + '|' + decision.source + '|' + host;
140
174
  if (!loggedHosts.has(key)) {
141
175
  loggedHosts.add(key);
142
- logLine('network: ' + host + ' 走代理 ' + decision.proxyUrl + '(来源 ' + decision.source + ')');
176
+ logLine('network: ' + host + ' 走代理 ' + endpoint + source);
143
177
  }
144
178
 
145
179
  try {
146
180
  return await fetch(url, Object.assign({}, fetchInit, { dispatcher: dispatcher }));
147
181
  } catch (error) {
148
- if (error && error.name === 'AbortError') throw localizedError(error, 'network.timeout', { host: hostOf(url) });
149
- logLine('network: ' + host + ' 经代理 ' + decision.proxyUrl + ' 失败:' + (causeCode(error) || error.message));
150
- throw networkFailure(error, url, decision);
182
+ if (error?.name === 'AbortError') throw localizedError(safeProxyCause(error), 'network.timeout', { host });
183
+ const failure = networkFailure(error, url, decision);
184
+ logLine('network: ' + host + ' 经代理 ' + endpoint + source + ' 失败:' + failure.localized.params.cause);
185
+ throw failure;
151
186
  }
152
187
  }
153
188