huaweicloud-devkit 2.0.0-next.3 → 2.0.0-next.4
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/README.md +3 -1
- package/package.json +3 -1
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.mcp.json +55 -1
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/dist/agents/codex-config.js +374 -0
- package/plugins/huaweicloud-core/dist/agents/codex-desktop.js +8 -1
- package/plugins/huaweicloud-core/dist/agents/codex.js +9 -0
- package/plugins/huaweicloud-core/dist/agents/opencode-config.js +141 -86
- package/plugins/huaweicloud-core/dist/agents/opencode.js +106 -39
- package/plugins/huaweicloud-core/dist/approval-gate.js +1185 -0
- package/plugins/huaweicloud-core/dist/approval-ledger.js +2331 -0
- package/plugins/huaweicloud-core/dist/approval-roster.js +89 -0
- package/plugins/huaweicloud-core/dist/approval-types.js +1 -0
- package/plugins/huaweicloud-core/dist/approvals-cli.js +4 -4
- package/plugins/huaweicloud-core/dist/data/icons-manifest.v1.json +23 -5
- package/plugins/huaweicloud-core/dist/dep/receipts.js +65 -5
- package/plugins/huaweicloud-core/dist/hcloud-cli.js +1 -1
- package/plugins/huaweicloud-core/dist/hcloud-plan.js +62 -61
- package/plugins/huaweicloud-core/dist/lib/approval-mode.js +7 -5
- package/plugins/huaweicloud-core/dist/lib/approval-tokens.js +18 -4
- package/plugins/huaweicloud-core/dist/lib/call-trace.js +7 -14
- package/plugins/huaweicloud-core/dist/lib/confirm-language.js +50 -0
- package/plugins/huaweicloud-core/dist/lib/credentials.js +9 -9
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +1398 -0
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +362 -32
- package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +6 -16
- package/plugins/huaweicloud-core/dist/lib/opencode-ask-rule.js +224 -78
- package/plugins/huaweicloud-core/dist/lib/opencode-saved-perms.js +282 -0
- package/plugins/huaweicloud-core/dist/lib/supplemental-redaction.js +116 -0
- package/plugins/huaweicloud-core/dist/lib/tool-schema-parts.js +31 -0
- package/plugins/huaweicloud-core/dist/lib/workspace-glob.js +442 -0
- package/plugins/huaweicloud-core/dist/mcp-config-io.js +28 -1
- package/plugins/huaweicloud-core/dist/mcp-protocol.js +68 -16
- package/plugins/huaweicloud-core/dist/mcp-server.js +6506 -912
- package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
- package/plugins/huaweicloud-core/dist/mcp-stdio-transport.js +14 -4
- package/plugins/huaweicloud-core/dist/packs/auth/approval.js +249 -0
- package/plugins/huaweicloud-core/dist/packs/auth/tools.js +44 -44
- package/plugins/huaweicloud-core/dist/packs/huaweicloud-cli/tools.js +85 -130
- package/plugins/huaweicloud-core/dist/packs/obs/approval.js +110 -0
- package/plugins/huaweicloud-core/dist/packs/obs/tools.js +7 -2
- package/plugins/huaweicloud-core/dist/packs/registry.js +19 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +245 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +156 -23
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +13 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-hwexec.js +136 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +11 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +1 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +79 -9
- package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +28 -9
- package/plugins/huaweicloud-core/dist/packs/secrets/approval.js +46 -0
- package/plugins/huaweicloud-core/dist/packs/secrets/tools.js +4 -1
- package/plugins/huaweicloud-core/dist/packs/update/approval.js +79 -0
- package/plugins/huaweicloud-core/dist/packs/update/tools.js +8 -1
- package/plugins/huaweicloud-core/dist/packs/voucher/tools.js +1 -1
- package/plugins/huaweicloud-core/dist/packs-configure.js +74 -0
- package/plugins/huaweicloud-core/dist/safety/classify-text.js +36 -0
- package/plugins/huaweicloud-core/dist/safety/policy.js +2 -0
- package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +1 -1
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-auth.js +65 -14
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-cleanup.js +422 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose-flow.js +66 -16
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose.js +13 -10
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-keepalive.js +42 -2
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-key.js +11 -3
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-tunnel.js +6 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-validation.js +10 -1
- package/plugins/huaweicloud-core/dist/sandbox/hwexec-task.js +244 -0
- package/plugins/huaweicloud-core/dist/setup-cli.js +36 -1
- package/plugins/huaweicloud-core/dist/tool-definitions.js +9 -1
- package/plugins/huaweicloud-core/dist/tool-schemas.js +32 -6
- package/plugins/huaweicloud-core/dist/tools.js +76 -9
- package/plugins/huaweicloud-core/dist/update-registry.js +358 -5
- package/plugins/huaweicloud-core/dist/update-upgrade.js +1083 -14
- package/plugins/huaweicloud-core/dist/vendor-gate.js +2 -2
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.mjs +150 -1
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +53 -3
- package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
- package/plugins/huaweicloud-core/safety/policy.json +18 -11
- package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +114 -10
- package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +190 -138
- package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +36 -12
- package/plugins/huaweicloud-core/skills/huawei-voucher/SKILL.md +2 -0
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +2 -2
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +11 -7
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +74 -29
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +21 -19
- package/plugins/huaweicloud-core/src/agents/codex-config.ts +432 -0
- package/plugins/huaweicloud-core/src/agents/codex-desktop.ts +8 -1
- package/plugins/huaweicloud-core/src/agents/codex.ts +10 -0
- package/plugins/huaweicloud-core/src/agents/opencode-config.ts +176 -88
- package/plugins/huaweicloud-core/src/agents/opencode.ts +125 -54
- package/plugins/huaweicloud-core/src/approval-gate.ts +1306 -0
- package/plugins/huaweicloud-core/src/approval-ledger.ts +2446 -0
- package/plugins/huaweicloud-core/src/approval-roster.ts +127 -0
- package/plugins/huaweicloud-core/src/approval-types.ts +237 -0
- package/plugins/huaweicloud-core/src/approvals-cli.ts +4 -4
- package/plugins/huaweicloud-core/src/data/icons-manifest.v1.json +23 -5
- package/plugins/huaweicloud-core/src/dep/receipts.ts +84 -5
- package/plugins/huaweicloud-core/src/hcloud-cli.ts +3 -0
- package/plugins/huaweicloud-core/src/hcloud-plan.ts +68 -70
- package/plugins/huaweicloud-core/src/lib/approval-mode.ts +7 -5
- package/plugins/huaweicloud-core/src/lib/approval-tokens.ts +19 -4
- package/plugins/huaweicloud-core/src/lib/call-trace.ts +7 -17
- package/plugins/huaweicloud-core/src/lib/confirm-language.ts +51 -0
- package/plugins/huaweicloud-core/src/lib/credentials.ts +9 -8
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +1428 -0
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +377 -30
- package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +8 -15
- package/plugins/huaweicloud-core/src/lib/opencode-ask-rule.ts +302 -83
- package/plugins/huaweicloud-core/src/lib/opencode-saved-perms.ts +354 -0
- package/plugins/huaweicloud-core/src/lib/supplemental-redaction.ts +129 -0
- package/plugins/huaweicloud-core/src/lib/tool-schema-parts.ts +40 -0
- package/plugins/huaweicloud-core/src/lib/workspace-glob.ts +414 -0
- package/plugins/huaweicloud-core/src/mcp-config-io.ts +27 -1
- package/plugins/huaweicloud-core/src/mcp-protocol.ts +81 -17
- package/plugins/huaweicloud-core/src/mcp-server.ts +7 -7
- package/plugins/huaweicloud-core/src/mcp-stdio-transport.ts +14 -4
- package/plugins/huaweicloud-core/src/packs/auth/approval.ts +302 -0
- package/plugins/huaweicloud-core/src/packs/auth/tools.ts +54 -45
- package/plugins/huaweicloud-core/src/packs/huaweicloud-cli/tools.ts +99 -137
- package/plugins/huaweicloud-core/src/packs/obs/approval.ts +122 -0
- package/plugins/huaweicloud-core/src/packs/obs/tools.ts +7 -2
- package/plugins/huaweicloud-core/src/packs/registry.ts +42 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +296 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +168 -23
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +11 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-hwexec.ts +228 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +11 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +1 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +100 -8
- package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +31 -8
- package/plugins/huaweicloud-core/src/packs/secrets/approval.ts +50 -0
- package/plugins/huaweicloud-core/src/packs/secrets/tools.ts +4 -1
- package/plugins/huaweicloud-core/src/packs/update/approval.ts +87 -0
- package/plugins/huaweicloud-core/src/packs/update/tools.ts +8 -1
- package/plugins/huaweicloud-core/src/packs/voucher/tools.ts +1 -1
- package/plugins/huaweicloud-core/src/packs-configure.ts +75 -0
- package/plugins/huaweicloud-core/src/safety/classify-text.ts +40 -0
- package/plugins/huaweicloud-core/src/safety/policy.ts +7 -0
- package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +1 -1
- package/plugins/huaweicloud-core/src/sandbox/devbridge-auth.ts +74 -14
- package/plugins/huaweicloud-core/src/sandbox/devbridge-cleanup.ts +509 -0
- package/plugins/huaweicloud-core/src/sandbox/devbridge-expose-flow.ts +70 -18
- package/plugins/huaweicloud-core/src/sandbox/devbridge-expose.ts +26 -10
- package/plugins/huaweicloud-core/src/sandbox/devbridge-keepalive.ts +41 -2
- package/plugins/huaweicloud-core/src/sandbox/devbridge-key.ts +9 -3
- package/plugins/huaweicloud-core/src/sandbox/devbridge-tunnel.ts +24 -0
- package/plugins/huaweicloud-core/src/sandbox/devbridge-validation.ts +11 -1
- package/plugins/huaweicloud-core/src/sandbox/hwexec-task.ts +304 -0
- package/plugins/huaweicloud-core/src/setup-cli.ts +39 -1
- package/plugins/huaweicloud-core/src/tool-definitions.ts +9 -1
- package/plugins/huaweicloud-core/src/tool-schemas.ts +39 -6
- package/plugins/huaweicloud-core/src/tools.ts +124 -10
- package/plugins/huaweicloud-core/src/update-registry.ts +340 -5
- package/plugins/huaweicloud-core/src/update-upgrade.ts +1130 -14
- package/plugins/huaweicloud-core/src/vendor-gate.ts +2 -2
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
// spawn) and the undici fetch path honoring HUAWEICLOUD_NPM_REGISTRY. Part of
|
|
3
3
|
// the src/update-* family behind the src/update-check.ts re-export face.
|
|
4
4
|
import { spawn, spawnSync } from 'node:child_process';
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { dirname, join, relative, sep } from 'node:path';
|
|
5
8
|
import { fetchWithProxy } from "./proxy/proxy-agent.js";
|
|
6
9
|
import { asText } from "./lib/as-text.js";
|
|
10
|
+
import { workspaceMemberMatch } from "./lib/workspace-glob.js";
|
|
7
11
|
const NPM_BIN = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
8
12
|
// One-property read with validation, for the JS boundary of thrown values:
|
|
9
13
|
// only a non-empty `message` string is surfaced. Exported for the upgrade act
|
|
@@ -43,15 +47,364 @@ function debugLog(message) {
|
|
|
43
47
|
console.error(`[debug] ${message}`);
|
|
44
48
|
}
|
|
45
49
|
}
|
|
50
|
+
// One spelling rule serves every env channel for the pre-flight and the child:
|
|
51
|
+
// trimmed non-empty exact-case first, else the first trimmed non-empty
|
|
52
|
+
// case/padding variant; whitespace counts as unset.
|
|
53
|
+
export function resolveEnvSpelling(env, canonical) {
|
|
54
|
+
const spellings = Object.entries(env)
|
|
55
|
+
.filter(([key, value]) => key.trim().toUpperCase() === canonical && Boolean(value?.trim()))
|
|
56
|
+
.map(([key, value]) => [key, value.trim()]);
|
|
57
|
+
const exact = spellings.find(([key]) => key === canonical);
|
|
58
|
+
return (exact ?? spellings[0])?.[1];
|
|
59
|
+
}
|
|
60
|
+
export function resolveRegistryOverride(env = process.env) {
|
|
61
|
+
return resolveEnvSpelling(env, 'HUAWEICLOUD_NPM_REGISTRY');
|
|
62
|
+
}
|
|
63
|
+
// The npm auth channels resolve through the same exact-case-first collapse as
|
|
64
|
+
// the child env, so duplicate/variant spellings cannot split the halves.
|
|
65
|
+
function resolveNpmAuthToken(env = process.env) {
|
|
66
|
+
return resolveEnvSpelling(env, 'NPM_TOKEN') ?? resolveEnvSpelling(env, 'NODE_AUTH_TOKEN');
|
|
67
|
+
}
|
|
68
|
+
// npm reads the userconfig by a case-insensitive `npm_config_userconfig` key,
|
|
69
|
+
// trims the value, and skips an empty value (falling back to ~/.npmrc). An
|
|
70
|
+
// empty spelling is skipped rather than terminal, so a set sibling still wins;
|
|
71
|
+
// a whitespace-only value trims to an empty path, which is not the fallback.
|
|
72
|
+
function resolveUserconfig(env = process.env) {
|
|
73
|
+
const raw = Object.entries(env)
|
|
74
|
+
.filter(([key, value]) => key.toUpperCase() === 'NPM_CONFIG_USERCONFIG' && value !== '')
|
|
75
|
+
.at(-1)?.[1];
|
|
76
|
+
if (raw === undefined)
|
|
77
|
+
return undefined;
|
|
78
|
+
// npm applies envReplace to every config value before path resolution, so a
|
|
79
|
+
// CI env block (`${CI_PROJECT_DIR}/.npmrc`) reaches the same file.
|
|
80
|
+
return expandTilde(envReplace(raw.trim(), env));
|
|
81
|
+
}
|
|
82
|
+
function expandTilde(value) {
|
|
83
|
+
if (value === '~')
|
|
84
|
+
return homedir();
|
|
85
|
+
return value.startsWith('~/') ? join(homedir(), value.slice(2)) : value;
|
|
86
|
+
}
|
|
87
|
+
function workspaceGlobs(value) {
|
|
88
|
+
const declared = Array.isArray(value)
|
|
89
|
+
? value
|
|
90
|
+
: value && typeof value === 'object' && Array.isArray(value.packages)
|
|
91
|
+
? value.packages
|
|
92
|
+
: [];
|
|
93
|
+
// npm rejects a non-string entry (EWORKSPACESCONFIG / a .match TypeError), so
|
|
94
|
+
// the declaration yields no members instead of promoting where npm crashes.
|
|
95
|
+
if (declared.some((entry) => typeof entry !== 'string'))
|
|
96
|
+
return [];
|
|
97
|
+
return declared;
|
|
98
|
+
}
|
|
99
|
+
function packageJsonWorkspaces(path) {
|
|
100
|
+
try {
|
|
101
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
102
|
+
if (!parsed || typeof parsed !== 'object')
|
|
103
|
+
return undefined;
|
|
104
|
+
const globs = workspaceGlobs(parsed.workspaces);
|
|
105
|
+
return globs.length ? globs : undefined;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function isWorkspaceMember(root, member, globs) {
|
|
112
|
+
return workspaceMemberMatch(relative(root, member).split(sep).join('/'), globs);
|
|
113
|
+
}
|
|
114
|
+
function localPrefix(start = process.cwd()) {
|
|
115
|
+
// npm's localPrefix starts at the nearest package dir and moves to an
|
|
116
|
+
// ancestor only when that ancestor's workspace map actually contains the
|
|
117
|
+
// package dir (mapWorkspaces membership, not the mere presence of the field).
|
|
118
|
+
let nearest;
|
|
119
|
+
let current = start;
|
|
120
|
+
for (;;) {
|
|
121
|
+
const manifest = join(current, 'package.json');
|
|
122
|
+
if (existsSync(manifest)) {
|
|
123
|
+
if (!nearest) {
|
|
124
|
+
nearest = current;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const globs = packageJsonWorkspaces(manifest);
|
|
128
|
+
if (globs && isWorkspaceMember(current, nearest, globs))
|
|
129
|
+
return current;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const parent = dirname(current);
|
|
133
|
+
if (parent === current)
|
|
134
|
+
return nearest ?? start;
|
|
135
|
+
current = parent;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Env spellings for npm's own config keys are case-insensitive; npm takes the
|
|
139
|
+
// last non-empty one when both are exported (an empty value is skipped, not
|
|
140
|
+
// terminal).
|
|
141
|
+
function envValue(env, name) {
|
|
142
|
+
const values = Object.entries(env)
|
|
143
|
+
.filter(([key]) => key.toUpperCase() === name)
|
|
144
|
+
.map(([, value]) => value?.trim())
|
|
145
|
+
.filter((value) => Boolean(value));
|
|
146
|
+
return values.at(-1);
|
|
147
|
+
}
|
|
148
|
+
function globalconfigPath(env) {
|
|
149
|
+
const globalconfig = envValue(env, 'NPM_CONFIG_GLOBALCONFIG');
|
|
150
|
+
if (globalconfig)
|
|
151
|
+
return expandTilde(globalconfig);
|
|
152
|
+
const prefix = envValue(env, 'NPM_CONFIG_PREFIX') || env.PREFIX || dirname(dirname(process.execPath));
|
|
153
|
+
return join(prefix, 'etc', 'npmrc');
|
|
154
|
+
}
|
|
155
|
+
function stripIniQuotes(value) {
|
|
156
|
+
const trimmed = value.trim();
|
|
157
|
+
const quoted = /^(['"])([\s\S]*)\1$/.exec(trimmed);
|
|
158
|
+
return quoted ? quoted[2] : trimmed;
|
|
159
|
+
}
|
|
160
|
+
// npm's ini value rule: a quoted value keeps its comment characters; an
|
|
161
|
+
// unquoted value ends at the first unescaped `;` or `#` (with or without
|
|
162
|
+
// whitespace) and backslash escapes those three characters.
|
|
163
|
+
function parseIniValue(raw) {
|
|
164
|
+
const trimmed = raw.trim();
|
|
165
|
+
if (/^(['"])[\s\S]*\1$/.test(trimmed))
|
|
166
|
+
return stripIniQuotes(trimmed);
|
|
167
|
+
let out = '';
|
|
168
|
+
let escaped = false;
|
|
169
|
+
for (const char of trimmed) {
|
|
170
|
+
if (escaped) {
|
|
171
|
+
out += ';#\\'.includes(char) ? char : `\\${char}`;
|
|
172
|
+
escaped = false;
|
|
173
|
+
}
|
|
174
|
+
else if (char === ';' || char === '#') {
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
else if (char === '\\') {
|
|
178
|
+
escaped = true;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
out += char;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (escaped)
|
|
185
|
+
out += '\\';
|
|
186
|
+
return out.trim();
|
|
187
|
+
}
|
|
188
|
+
// npm's env-replace: `\${...}` stays literal, `${VAR}` interpolates the exact
|
|
189
|
+
// env name (unset keeps the literal), and `${VAR?}` resolves empty when unset.
|
|
190
|
+
function envReplace(value, env) {
|
|
191
|
+
let out = '';
|
|
192
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
193
|
+
const char = value[index];
|
|
194
|
+
if (char === '\\' && value[index + 1] === '$') {
|
|
195
|
+
out += '$';
|
|
196
|
+
index += 1;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (char === '$' && value[index + 1] === '{') {
|
|
200
|
+
const end = value.indexOf('}', index + 2);
|
|
201
|
+
if (end < 0) {
|
|
202
|
+
out += char;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
let name = value.slice(index + 2, end);
|
|
206
|
+
const optional = name.endsWith('?');
|
|
207
|
+
if (optional)
|
|
208
|
+
name = name.slice(0, -1);
|
|
209
|
+
const resolved = env[name];
|
|
210
|
+
out += resolved !== undefined ? resolved : optional ? '' : value.slice(index, end + 1);
|
|
211
|
+
index = end;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
out += char;
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
function parseNpmrc(text, env) {
|
|
219
|
+
const entries = new Map();
|
|
220
|
+
for (const line of text.split(/\r?\n/)) {
|
|
221
|
+
const trimmed = line.trim();
|
|
222
|
+
if (!trimmed || trimmed.startsWith(';') || trimmed.startsWith('#'))
|
|
223
|
+
continue;
|
|
224
|
+
const match = /^([^=]+?)\s*=\s*(.*)$/.exec(trimmed);
|
|
225
|
+
if (!match)
|
|
226
|
+
continue;
|
|
227
|
+
const value = parseIniValue(match[2]);
|
|
228
|
+
entries.set(match[1].trim(), envReplace(value, env));
|
|
229
|
+
}
|
|
230
|
+
return entries;
|
|
231
|
+
}
|
|
232
|
+
// npm's auth cascade: env-spread nerf keys, the project/workspace root
|
|
233
|
+
// (nearest package.json), the userconfig, then the global config. Unreadable
|
|
234
|
+
// files contribute nothing.
|
|
235
|
+
function configLayers(env) {
|
|
236
|
+
const layers = [];
|
|
237
|
+
const spread = new Map();
|
|
238
|
+
for (const [key, value] of Object.entries(env)) {
|
|
239
|
+
const body = /^npm_config_(.*)$/i.exec(key)?.[1];
|
|
240
|
+
if (body?.startsWith('//') && value !== undefined)
|
|
241
|
+
spread.set(body, envReplace(value, env));
|
|
242
|
+
}
|
|
243
|
+
if (spread.size)
|
|
244
|
+
layers.push(spread);
|
|
245
|
+
const userconfig = resolveUserconfig(env);
|
|
246
|
+
const files = [join(localPrefix(), '.npmrc'), userconfig ?? join(homedir(), '.npmrc'), globalconfigPath(env)];
|
|
247
|
+
for (const file of files) {
|
|
248
|
+
try {
|
|
249
|
+
layers.push(parseNpmrc(readFileSync(file, 'utf8'), env));
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
// an unreadable config layer contributes no keys
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return layers;
|
|
256
|
+
}
|
|
257
|
+
function basicHeader(username, password) {
|
|
258
|
+
if (!username || password === undefined)
|
|
259
|
+
return undefined;
|
|
260
|
+
const decoded = (() => {
|
|
261
|
+
try {
|
|
262
|
+
return Buffer.from(password, 'base64').toString('utf8');
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return password;
|
|
266
|
+
}
|
|
267
|
+
})();
|
|
268
|
+
return `Basic ${Buffer.from(`${username}:${decoded}`).toString('base64')}`;
|
|
269
|
+
}
|
|
270
|
+
// The pre-flight auth header for a registry, resolved the way npm resolves it:
|
|
271
|
+
// walk every nerf-dart path level through to the host root (both the slash and
|
|
272
|
+
// slash-less keys), and take the first token/basic entry found across the
|
|
273
|
+
// config cascade. A falsy candidate is skipped, never terminal (npm's hasAuth
|
|
274
|
+
// tests truthiness and keeps walking), while an explicitly empty key's layer
|
|
275
|
+
// suppresses the lower layers; when the whole walk finds no auth and some level
|
|
276
|
+
// named an empty auth key, `null` tells the caller not to resurrect the env
|
|
277
|
+
// token.
|
|
278
|
+
function npmrcAuthHeader(registry, env = process.env) {
|
|
279
|
+
let url;
|
|
280
|
+
try {
|
|
281
|
+
url = new URL(registry);
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
const host = `//${url.host}`;
|
|
287
|
+
const path = url.pathname.replace(/\/+$/, '');
|
|
288
|
+
const levels = [];
|
|
289
|
+
let current = path;
|
|
290
|
+
for (;;) {
|
|
291
|
+
levels.push(current);
|
|
292
|
+
if (!current)
|
|
293
|
+
break;
|
|
294
|
+
const next = current.replace(/\/[^/]*$/, '');
|
|
295
|
+
if (next === current)
|
|
296
|
+
break;
|
|
297
|
+
current = next;
|
|
298
|
+
}
|
|
299
|
+
const layers = configLayers(env);
|
|
300
|
+
// `undefined` means no layer names the key; an empty string is that layer's
|
|
301
|
+
// explicit (falsy) value.
|
|
302
|
+
const lookup = (key) => {
|
|
303
|
+
for (const layer of layers) {
|
|
304
|
+
if (layer.has(key))
|
|
305
|
+
return layer.get(key);
|
|
306
|
+
}
|
|
307
|
+
return undefined;
|
|
308
|
+
};
|
|
309
|
+
let sawExplicitEmpty = false;
|
|
310
|
+
for (const level of levels) {
|
|
311
|
+
const prefix = `${host}${level}`;
|
|
312
|
+
const token = lookup(`${prefix}/:_authToken`) ?? lookup(`${prefix}:_authToken`);
|
|
313
|
+
if (token)
|
|
314
|
+
return token.startsWith('Bearer ') ? token : `Bearer ${token}`;
|
|
315
|
+
if (token === '')
|
|
316
|
+
sawExplicitEmpty = true;
|
|
317
|
+
const auth = lookup(`${prefix}/:_auth`) ?? lookup(`${prefix}:_auth`);
|
|
318
|
+
if (auth)
|
|
319
|
+
return `Basic ${auth}`;
|
|
320
|
+
if (auth === '')
|
|
321
|
+
sawExplicitEmpty = true;
|
|
322
|
+
const username = lookup(`${prefix}/:username`);
|
|
323
|
+
const password = lookup(`${prefix}/:_password`);
|
|
324
|
+
if (username && password) {
|
|
325
|
+
const basic = basicHeader(username, password);
|
|
326
|
+
if (basic)
|
|
327
|
+
return basic;
|
|
328
|
+
}
|
|
329
|
+
if (username !== undefined && password !== undefined && (!username || !password))
|
|
330
|
+
sawExplicitEmpty = true;
|
|
331
|
+
}
|
|
332
|
+
return sawExplicitEmpty ? null : undefined;
|
|
333
|
+
}
|
|
334
|
+
// The pre-flight follows redirects the way npm's registry client does: the same
|
|
335
|
+
// Authorization header travels to a same-host target leg (port or scheme
|
|
336
|
+
// changes re-authenticate), and a hostname change drops it.
|
|
337
|
+
async function fetchFollowingAuth(url, init) {
|
|
338
|
+
let current = url;
|
|
339
|
+
const originalHost = new URL(url).hostname;
|
|
340
|
+
for (let hop = 0; hop < 5; hop += 1) {
|
|
341
|
+
const hopInit = { ...init, redirect: 'manual' };
|
|
342
|
+
// npm re-authenticates a same-host redirect (port/scheme changes) but strips
|
|
343
|
+
// the Authorization when the hostname changes.
|
|
344
|
+
if (new URL(current).hostname !== originalHost)
|
|
345
|
+
delete hopInit.headers;
|
|
346
|
+
const response = await fetchWithProxy(current, hopInit);
|
|
347
|
+
const location = response.status >= 300 && response.status < 400 ? response.headers.get('location') : undefined;
|
|
348
|
+
if (!location)
|
|
349
|
+
return response;
|
|
350
|
+
try {
|
|
351
|
+
current = new URL(location, current).toString();
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
return response;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
46
359
|
export async function queryDistTagsFetch({ timeoutMs = 15000 } = {}) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
360
|
+
// One normalization with upgradeEnv: a padded or case-variant spelling is
|
|
361
|
+
// usable and a whitespace-only value is unset, so the pre-flight and the
|
|
362
|
+
// child pin cannot disagree.
|
|
363
|
+
let configured = resolveRegistryOverride();
|
|
364
|
+
// A registry override may carry URL credentials; npm only falls back to them
|
|
365
|
+
// when the config cascade yields no header, and it sends the raw
|
|
366
|
+
// (percent-encoded) userinfo as Basic.
|
|
367
|
+
let urlAuth;
|
|
368
|
+
if (configured) {
|
|
369
|
+
try {
|
|
370
|
+
const parsed = new URL(configured);
|
|
371
|
+
if (parsed.username || parsed.password) {
|
|
372
|
+
urlAuth = `Basic ${Buffer.from(`${parsed.username}:${parsed.password}`).toString('base64')}`;
|
|
373
|
+
parsed.username = '';
|
|
374
|
+
parsed.password = '';
|
|
375
|
+
configured = parsed.toString();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
// a malformed URL degrades like npm's own registry parse
|
|
380
|
+
}
|
|
50
381
|
}
|
|
382
|
+
let registry = 'https://registry.npmjs.org';
|
|
383
|
+
// The request URL is built by appending `/-/package/…`, so a trailing slash
|
|
384
|
+
// on the override would produce a doubled path.
|
|
385
|
+
if (configured)
|
|
386
|
+
registry = configured.replace(/\/+$/, '');
|
|
51
387
|
const controller = new AbortController();
|
|
52
388
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
389
|
+
// The pre-flight query needs the same mirror auth the child npm gets: a
|
|
390
|
+
// token-guarded private mirror 401s the packument without it, aborting the
|
|
391
|
+
// act before the spawn. The token is read for this request only; the env
|
|
392
|
+
// shaping of the child is a separate concern.
|
|
393
|
+
// npm's own order: the config cascade's named channel first (an .npmrc value
|
|
394
|
+
// may interpolate NODE_AUTH_TOKEN even when a stale NPM_TOKEN is exported),
|
|
395
|
+
// then the URL userinfo, then the env tokens, then no auth.
|
|
396
|
+
const token = resolveNpmAuthToken();
|
|
397
|
+
const configAuth = npmrcAuthHeader(registry);
|
|
398
|
+
// `null` is an explicitly empty config token: no env fallback (npm's merged
|
|
399
|
+
// value is empty and it sends nothing); URL userinfo still applies, as it
|
|
400
|
+
// does for npm when no Authorization header was set.
|
|
401
|
+
const authorization = configAuth ?? urlAuth ?? (configAuth === null || !token ? undefined : `Bearer ${token}`);
|
|
402
|
+
const init = {
|
|
403
|
+
signal: controller.signal,
|
|
404
|
+
...(authorization ? { headers: { authorization } } : {}),
|
|
405
|
+
};
|
|
53
406
|
try {
|
|
54
|
-
const resp = await
|
|
407
|
+
const resp = await fetchFollowingAuth(`${registry}/-/package/huaweicloud-devkit/dist-tags`, init);
|
|
55
408
|
clearTimeout(timer);
|
|
56
409
|
if (!resp || !resp.ok)
|
|
57
410
|
return null;
|
|
@@ -95,7 +448,7 @@ export function queryDistTags({ timeoutMs = 15000, cwd, } = {}) {
|
|
|
95
448
|
// delegation closes that inconsistency, so every default consumer
|
|
96
449
|
// (getCachedUpdateInfo, upgradePackage) honors the operator/E2E override
|
|
97
450
|
// without spawning npm.
|
|
98
|
-
if (
|
|
451
|
+
if (resolveRegistryOverride()) {
|
|
99
452
|
return queryDistTagsFetch({ timeoutMs });
|
|
100
453
|
}
|
|
101
454
|
return new Promise((resolve) => {
|