sap-adt-cli 1.5.0 → 1.5.2

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 CHANGED
@@ -34,6 +34,10 @@ artifact over HTTPS, verifies its SHA-256 (from the manifest bundled with
34
34
  the package — never from the download endpoint), extracts safely and swaps
35
35
  it in atomically, then self-tests with the absolute install path.
36
36
 
37
+ On a proxied network, set `HTTPS_PROXY` (and `NO_PROXY` for hosts that go
38
+ direct) before running `install`; the wizard tunnels through an `http://`
39
+ proxy, reports download progress, and aborts on a timeout instead of hanging.
40
+
37
41
  ## Use
38
42
 
39
43
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sap-adt-cli",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Installer and launcher for the self-contained sap-adt CLI (SAP ADT REST API). No Python required; Node.js >=18 needed for this npm channel only.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -1,12 +1,12 @@
1
1
  {
2
- "cli_version": "1.5.0",
2
+ "cli_version": "1.5.2",
3
3
  "release_state": "published",
4
4
  "artifacts": {
5
5
  "linux-x86_64": {
6
- "url": "https://github.com/shrek-abaper/sap-engineering-skill/releases/download/sap-adt-v1.5.0/sap-adt-1.5.0-linux-x86_64.tar.gz",
7
- "sha256": "2ffbbc88708f90f205c69fead46860b6f6b16cae70bf5810676779b9d1f1c5f1",
6
+ "url": "https://github.com/shrek-abaper/sap-engineering-skill/releases/download/sap-adt-v1.5.2/sap-adt-1.5.2-linux-x86_64.tar.gz",
7
+ "sha256": "c1da5705f4e2b6efdacf15dec50b48e1b1c667acafc8432bc87f916cc84ce680",
8
8
  "format": "tar.gz",
9
- "size_bytes": 16557686,
9
+ "size_bytes": 16556438,
10
10
  "allowed_redirect_hosts": [
11
11
  "github.com",
12
12
  "release-assets.githubusercontent.com",
@@ -13,6 +13,8 @@
13
13
  * explicitly allow-listed redirect host in the manifest
14
14
  * - extraction rejects absolute paths, ".." traversal, and symlinks
15
15
  * - no curl|sh, no shell:true, no string-concatenated commands
16
+ * - the only network egress is the artifact download, through the proxy
17
+ * named by HTTPS_PROXY/HTTP_PROXY when the environment sets one
16
18
  *
17
19
  * Test seam: file:// artifact URLs are accepted ONLY when the environment
18
20
  * variable SAP_ADT_INSTALL_ALLOW_FILE=1 is set (local packaging tests).
@@ -22,13 +24,30 @@
22
24
  const { spawnSync } = require("node:child_process");
23
25
  const crypto = require("node:crypto");
24
26
  const fs = require("node:fs");
27
+ const http = require("node:http");
25
28
  const https = require("node:https");
29
+ const net = require("node:net");
26
30
  const os = require("node:os");
27
31
  const path = require("node:path");
32
+ const tls = require("node:tls");
33
+ const { Transform } = require("node:stream");
28
34
  const { pipeline } = require("node:stream/promises");
29
35
 
30
36
  const PKG_DIR = path.resolve(__dirname, "..");
31
37
  const MAX_REDIRECTS = 3;
38
+ // Node's core https client ignores HTTPS_PROXY before v24, so a blocked
39
+ // direct route hung this wizard forever. Both timers are inactivity-based:
40
+ // a slow but progressing download is never aborted.
41
+ const CONNECT_TIMEOUT_MS = intEnv("SAP_ADT_INSTALL_CONNECT_TIMEOUT_MS", 30_000);
42
+ const IDLE_TIMEOUT_MS = 60_000;
43
+ const PROGRESS_INTERVAL_MS = 5_000;
44
+
45
+ function intEnv(name, fallback) {
46
+ const raw = process.env[name];
47
+ if (!raw) return fallback;
48
+ const value = Number(raw);
49
+ return Number.isFinite(value) && value > 0 ? value : fallback;
50
+ }
32
51
 
33
52
  function fail(message, code = 1) {
34
53
  process.stderr.write(`[sap-adt install] ERROR: ${message}\n`);
@@ -77,6 +96,157 @@ function pickArtifact(manifest, key) {
77
96
  return entry;
78
97
  }
79
98
 
99
+ function firstEnv(env, names) {
100
+ for (const name of names) {
101
+ const value = env[name];
102
+ if (value && value.trim()) return value.trim();
103
+ }
104
+ return "";
105
+ }
106
+
107
+ /* Proxy for a target URL, or null when the request should go direct.
108
+ * Honors the usual spellings plus npm's own config variables; `env` is
109
+ * injectable so the tests never depend on the machine's network. */
110
+ function proxyForUrl(targetUrl, env = process.env) {
111
+ const raw = firstEnv(env, [
112
+ "https_proxy", "HTTPS_PROXY", "npm_config_https_proxy", "npm_config_proxy",
113
+ "http_proxy", "HTTP_PROXY",
114
+ ]);
115
+ if (!raw) return null;
116
+
117
+ let proxy;
118
+ try {
119
+ proxy = new URL(raw.includes("://") ? raw : `http://${raw}`);
120
+ } catch {
121
+ throw new Error(`HTTPS_PROXY is not a valid URL (got "${raw.replace(/:[^:@/]*@/, ":***@")}")`);
122
+ }
123
+ // Only plain HTTP CONNECT proxies: TLS-to-the-proxy would need TLS-in-TLS,
124
+ // and refusing loudly beats a half-working tunnel.
125
+ if (proxy.protocol !== "http:") {
126
+ throw new Error(`unsupported proxy scheme "${proxy.protocol}" in HTTPS_PROXY (only http:// proxies are supported)`);
127
+ }
128
+
129
+ const target = new URL(targetUrl);
130
+ const noProxy = firstEnv(env, ["no_proxy", "NO_PROXY", "npm_config_noproxy"]);
131
+ return bypassesProxy(target.hostname, target.port || "443", noProxy) ? null : proxy;
132
+ }
133
+
134
+ function bypassesProxy(hostname, port, noProxy) {
135
+ if (!noProxy) return false;
136
+ const host = hostname.replace(/^\[|\]$/g, "").toLowerCase();
137
+ for (let entry of noProxy.split(",")) {
138
+ entry = entry.trim().toLowerCase();
139
+ if (!entry) continue;
140
+ if (entry === "*") return true;
141
+ // "host:port" (never "::1" — the host part must be colon-free)
142
+ const withPort = /^([^:]+):(\d+)$/.exec(entry);
143
+ if (withPort) {
144
+ if (withPort[2] !== String(port)) continue;
145
+ entry = withPort[1];
146
+ }
147
+ entry = entry.replace(/^\[|\]$/g, "");
148
+ if (entry.startsWith("*.")) entry = entry.slice(1); // *.corp.com -> .corp.com
149
+ if (entry.startsWith(".")) {
150
+ if (host === entry.slice(1) || host.endsWith(entry)) return true;
151
+ } else if (entry.endsWith(".*")) {
152
+ if (host.startsWith(entry.slice(0, -1))) return true; // 10.* -> 10.
153
+ } else if (host === entry) {
154
+ return true;
155
+ }
156
+ }
157
+ return false;
158
+ }
159
+
160
+ function fmtBytes(n) {
161
+ const units = ["B", "KiB", "MiB", "GiB"];
162
+ let value = n;
163
+ let unit = 0;
164
+ while (value >= 1024 && unit < units.length - 1) {
165
+ value /= 1024;
166
+ unit += 1;
167
+ }
168
+ return `${unit === 0 ? value : value.toFixed(1)} ${units[unit]}`;
169
+ }
170
+
171
+ /* HTTPS through an HTTP(S) proxy: CONNECT tunnel, then TLS to the target so
172
+ * the certificate is still verified against the real host name. */
173
+ function openTunnel(proxy, host, port) {
174
+ return new Promise((resolve, reject) => {
175
+ const target = net.isIP(host) === 6 ? `[${host}]` : host;
176
+ const headers = { Host: `${target}:${port}` };
177
+ if (proxy.username) {
178
+ const user = decodeURIComponent(proxy.username);
179
+ const pass = decodeURIComponent(proxy.password || "");
180
+ headers["Proxy-Authorization"] =
181
+ `Basic ${Buffer.from(`${user}:${pass}`).toString("base64")}`;
182
+ }
183
+ const req = http.request({
184
+ host: proxy.hostname,
185
+ port: proxy.port || 80,
186
+ method: "CONNECT",
187
+ path: `${target}:${port}`,
188
+ headers,
189
+ agent: false,
190
+ timeout: CONNECT_TIMEOUT_MS,
191
+ });
192
+ req.on("connect", (res, socket) => {
193
+ if (res.statusCode === 407) {
194
+ socket.destroy();
195
+ return reject(new Error(
196
+ `proxy ${proxy.host} requires authentication (HTTP 407) — put credentials in ` +
197
+ "HTTPS_PROXY as http://user:pass@host:port"));
198
+ }
199
+ if (res.statusCode < 200 || res.statusCode >= 300) {
200
+ socket.destroy();
201
+ return reject(new Error(
202
+ `proxy CONNECT ${proxy.host} -> ${target}:${port} failed: HTTP ${res.statusCode}`));
203
+ }
204
+ const secure = tls.connect({
205
+ socket,
206
+ servername: net.isIP(host) ? undefined : host,
207
+ });
208
+ secure.setTimeout(CONNECT_TIMEOUT_MS, () => secure.destroy(new Error(
209
+ `TLS handshake with ${target} through the proxy timed out after ${CONNECT_TIMEOUT_MS} ms`)));
210
+ secure.once("secureConnect", () => {
211
+ secure.setTimeout(0);
212
+ resolve(secure);
213
+ });
214
+ secure.once("error", (e) => reject(new Error(
215
+ `TLS handshake with ${target} through the proxy failed: ${e.message} ` +
216
+ "(a TLS-intercepting proxy needs NODE_EXTRA_CA_CERTS pointing at its CA)")));
217
+ });
218
+ req.on("timeout", () => req.destroy(new Error(
219
+ `proxy CONNECT to ${proxy.host} timed out after ${CONNECT_TIMEOUT_MS} ms — ` +
220
+ "check HTTPS_PROXY/NO_PROXY and that the proxy allows CONNECT")));
221
+ req.on("error", reject);
222
+ req.end();
223
+ });
224
+ }
225
+
226
+ class TunnelAgent extends https.Agent {
227
+ constructor(proxy) {
228
+ super({ keepAlive: false, maxSockets: 1 });
229
+ this.proxy = proxy;
230
+ }
231
+
232
+ // Node's Agent contract: a 2-argument createConnection is the async form.
233
+ createConnection(options, cb) {
234
+ const host = options.servername || options.host;
235
+ openTunnel(this.proxy, host, options.port || 443).then(
236
+ (socket) => cb(null, socket),
237
+ cb,
238
+ );
239
+ }
240
+ }
241
+
242
+ function timeoutHint(target, proxy) {
243
+ const via = proxy ? ` via proxy ${proxy.host}` : "";
244
+ const advice = proxy
245
+ ? "check HTTPS_PROXY or add this host to NO_PROXY"
246
+ : "if this network requires a proxy, set HTTPS_PROXY=http://host:port";
247
+ return `timed out after ${IDLE_TIMEOUT_MS} ms connecting to ${target}${via} — ${advice}`;
248
+ }
249
+
80
250
  function download(url, allowedHosts, dest, redirectsLeft) {
81
251
  return new Promise((resolve, reject) => {
82
252
  const parsed = new URL(url);
@@ -94,7 +264,14 @@ function download(url, allowedHosts, dest, redirectsLeft) {
94
264
  if (!allowedHosts.includes(parsed.host)) {
95
265
  return reject(new Error(`artifact host not allow-listed: ${parsed.host}`));
96
266
  }
97
- https.get(url, (res) => {
267
+ const proxy = proxyForUrl(url);
268
+ const options = { timeout: IDLE_TIMEOUT_MS };
269
+ if (proxy) {
270
+ info(`using proxy ${proxy.host} for ${parsed.host}`);
271
+ options.agent = new TunnelAgent(proxy);
272
+ }
273
+
274
+ const req = https.get(url, options, (res) => {
98
275
  if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
99
276
  res.resume();
100
277
  if (redirectsLeft <= 0) return reject(new Error("too many redirects"));
@@ -116,8 +293,31 @@ function download(url, allowedHosts, dest, redirectsLeft) {
116
293
  res.resume();
117
294
  return reject(new Error(`download failed: HTTP ${res.statusCode}`));
118
295
  }
119
- pipeline(res, fs.createWriteStream(dest)).then(resolve, reject);
120
- }).on("error", reject);
296
+ const total = Number(res.headers["content-length"]) || 0;
297
+ info(`downloading ${total ? fmtBytes(total) : "artifact"}${proxy ? ` via ${proxy.host}` : ""}`);
298
+ const started = Date.now();
299
+ let received = 0;
300
+ let lastReport = started;
301
+ const counter = new Transform({
302
+ transform(chunk, _encoding, callback) {
303
+ received += chunk.length;
304
+ const now = Date.now();
305
+ if (now - lastReport >= PROGRESS_INTERVAL_MS) {
306
+ lastReport = now;
307
+ const pct = total ? ` (${Math.floor((received / total) * 100)}%)` : "";
308
+ info(`downloading ${fmtBytes(received)}${total ? ` of ${fmtBytes(total)}` : ""}${pct}`);
309
+ }
310
+ callback(null, chunk);
311
+ },
312
+ flush(callback) {
313
+ info(`downloaded ${fmtBytes(received)} in ${((Date.now() - started) / 1000).toFixed(1)} s`);
314
+ callback();
315
+ },
316
+ });
317
+ pipeline(res, counter, fs.createWriteStream(dest)).then(resolve, reject);
318
+ });
319
+ req.on("timeout", () => req.destroy(new Error(timeoutHint(parsed.host, proxy))));
320
+ req.on("error", reject);
121
321
  });
122
322
  }
123
323
 
@@ -155,6 +355,18 @@ function extract(archive, format, destDir) {
155
355
  }
156
356
  }
157
357
 
358
+ /* Rename, or copy when staging and target sit on different filesystems
359
+ * (os.tmpdir() is tmpfs on many Linux/WSL setups → EXDEV). */
360
+ function moveTree(staging, target) {
361
+ try {
362
+ fs.renameSync(staging, target);
363
+ } catch (e) {
364
+ if (e.code !== "EXDEV") throw e;
365
+ fs.cpSync(staging, target, { recursive: true });
366
+ fs.rmSync(staging, { recursive: true, force: true });
367
+ }
368
+ }
369
+
158
370
  /* Swap runtime atomically-ish: stage -> rename old aside -> rename new in ->
159
371
  * on any failure, restore the old one and leave it working. */
160
372
  function installTree(staging, runtimeDir) {
@@ -165,7 +377,7 @@ function installTree(staging, runtimeDir) {
165
377
  fs.renameSync(runtimeDir, backup);
166
378
  movedOld = true;
167
379
  }
168
- fs.renameSync(staging, runtimeDir);
380
+ moveTree(staging, runtimeDir);
169
381
  if (movedOld) fs.rmSync(backup, { recursive: true, force: true });
170
382
  } catch (e) {
171
383
  fs.rmSync(runtimeDir, { recursive: true, force: true });
@@ -223,4 +435,9 @@ async function main() {
223
435
  }
224
436
  }
225
437
 
226
- main().catch((e) => fail(e.message));
438
+ if (require.main === module) {
439
+ main().catch((e) => fail(e.message));
440
+ }
441
+
442
+ // Pure helpers only; the wizard itself stays a script.
443
+ module.exports = { moveTree, proxyForUrl };