Skip to content

Commit

Permalink
fix: ensure proper support for corporate proxies
Browse files Browse the repository at this point in the history
Proxies can be passed as env variables or as npm config options.
The highest precedence have the GLOBAL_AGENT_* env variables before
the regular env variables followed by the npm configuration options.

1.) env: GLOBAL_AGENT_HTTP_PROXY
2.) env: HTTP_PROXY
3.) npm: http-proxy
4.) npm: proxy

Fixes #142
  • Loading branch information
petermuessig committed Mar 9, 2024
1 parent c68817b commit 035364b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
engine-strict=true
engine-strict=true
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,20 @@ yo easy-ui5 [project|library] <sub-generator-id>
If you are running Easy UI5 behind a coporate proxy, just use the default proxy environment variables for Node.js to configure your corporate proxy:

- `HTTP_PROXY`: Specify the value to use as the HTTP proxy for all connections, e.g., `HTTP_PROXY="http://proxy.mycompany.com:8080/"`.
- `HTTPS_PROXY`: Specify the value to use as the HTTPS proxy for all connections, e.g., `HTTPS_PROXY="https://proxy.mycompany.com:8080/"`.
- `HTTPS_PROXY`: Specify the value to use as the HTTPS proxy for all connections, e.g., `HTTPS_PROXY="http://proxy.mycompany.com:8080/"`.
- `NO_PROXY`: Define the hosts that should bypass the proxy, e.g., `NO_PROXY="localhost,.mycompany.com,192.168.6.254:80"`.

In addition, Easy UI5 also supports proxy configuration from the `.npmrc` configuration:

```text
http-proxy=http://proxy.mycompany.com:8080/
https-proxy=http://proxy.mycompany.com:8080/
proxy=http://proxy.mycompany.com:8080/
no-proxy=localhost,.mycompany.com,192.168.6.254:80
```

This configuration is shared with npm itself since this proxy configuration is used to download the packages from npm.

## How to obtain support

Please use the GitHub bug tracking system to post questions, bug reports or to create pull requests.
Expand Down
47 changes: 30 additions & 17 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@ import { Octokit } from "@octokit/rest";
import { throttling } from "@octokit/plugin-throttling";
const MyOctokit = Octokit.plugin(throttling);
import spawn from "cross-spawn";
import nodeFetch from "node-fetch";

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

// apply proxy settings to GLOBAL_AGENT to support the proxy configuration
// provided via the standard Node.js environment varibales (used for fetch API)
let HTTP_PROXY, HTTPS_PROXY, NO_PROXY;
if (global?.GLOBAL_AGENT) {
HTTP_PROXY = global.GLOBAL_AGENT.HTTP_PROXY = process.env.HTTP_PROXY || process.env.http_proxy;
HTTPS_PROXY = global.GLOBAL_AGENT.HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.https_proxy;
NO_PROXY = global.GLOBAL_AGENT.NO_PROXY = process.env.NO_PROXY || process.env.no_proxy;
}

// helper to retrieve config entries from npm
// --> npm config set easy-ui5_addGhOrg XYZ
const NPM_CONFIG_PREFIX = "easy-ui5_";
let npmConfig;
const getNPMConfig = (configName) => {
const getNPMConfig = (configName, prefix = "easy-ui5_") => {
if (!npmConfig) {
npmConfig = libnpmconfig.read();
}
return npmConfig && npmConfig[`${NPM_CONFIG_PREFIX}${configName}`];
return npmConfig && npmConfig[`${prefix}${configName}`];
};

// apply proxy settings to GLOBAL_AGENT to support the proxy configuration for node-fetch using the GLOBAL_AGENT
// ==> the configuration is derived from the environment variables ([GLOBAL_AGENT_](HTTP|HTTPS|NO)_PROXY) and the npm config ((http|https|no)-proxy)
// ==> empty values will allow to override the more general proxy settings and make the proxy value undefined
let HTTP_PROXY, HTTPS_PROXY, NO_PROXY;
if (global?.GLOBAL_AGENT) {
HTTP_PROXY = process.env.GLOBAL_AGENT_HTTP_PROXY ?? process.env.HTTP_PROXY ?? process.env.http_proxy ?? getNPMConfig("http-proxy", "") ?? getNPMConfig("proxy", "");
global.GLOBAL_AGENT.HTTP_PROXY = HTTP_PROXY = HTTP_PROXY || global.GLOBAL_AGENT.HTTP_PROXY;
HTTPS_PROXY = process.env.GLOBAL_AGENT_HTTPS_PROXY ?? process.env.HTTPS_PROXY ?? process.env.https_proxy ?? getNPMConfig("https-proxy", "") ?? getNPMConfig("proxy", "");
global.GLOBAL_AGENT.HTTPS_PROXY = HTTPS_PROXY = HTTPS_PROXY || global.GLOBAL_AGENT.HTTPS_PROXY;
NO_PROXY = process.env.GLOBAL_AGENT_NO_PROXY ?? process.env.NO_PROXY ?? process.env.no_proxy ?? getNPMConfig("no-proxy", "");
global.GLOBAL_AGENT.NO_PROXY = NO_PROXY = NO_PROXY || global.GLOBAL_AGENT.NO_PROXY;
}

// the command line options of the generator
const generatorOptions = {
pluginsHome: {
Expand Down Expand Up @@ -265,7 +269,7 @@ export default class extends Generator {
});
generator.branch = repoInfo.data.default_branch;
} catch (e) {
console.error(`Generator "${owner}/${repo}!${dir}${branch ? "#" + branch : ""}" not found! Run with --verbose for details!`);
console.error(`Generator "${owner}/${repo}!${dir}${branch ? "#" + branch : ""}" not found! Run with --verbose for details!\n(Hint: ${e.message})`);
if (this.options.verbose) {
console.error(e);
}
Expand All @@ -283,7 +287,9 @@ export default class extends Generator {
});
commitSHA = reqBranch.data.commit.sha;
} catch (ex) {
console.error(chalk.red(`Failed to retrieve the branch "${generator.branch}" for repository "${generator.name}" for "${generator.org}" organization! Run with --verbose for details!`));
console.error(
chalk.red(`Failed to retrieve the branch "${generator.branch}" for repository "${generator.name}" for "${generator.org}" organization! Run with --verbose for details!\n(Hint: ${e.message})`)
);
if (this.options.verbose) {
console.error(chalk.red(ex.message));
}
Expand Down Expand Up @@ -405,6 +411,13 @@ export default class extends Generator {
// define the options for the Octokit API
const octokitOptions = {
userAgent: `${this.rootGeneratorName()}:${this.rootGeneratorVersion()}`,
request: {
fetch: (_url, _options) => {
return nodeFetch(_url, {
..._options,
});
},
},
auth: this.options.ghAuthToken,
baseUrl: this.options.ghBaseUrl,
throttle: {
Expand Down Expand Up @@ -555,7 +568,7 @@ export default class extends Generator {
};
});
} catch (e) {
console.error("Failed to connect to bestofui5.org to retrieve all available generators! Run with --verbose for details!");
console.error(`Failed to connect to bestofui5.org to retrieve all available generators! Run with --verbose for details!\n(Hint: ${e.message})`);
if (this.options.verbose) {
console.error(e);
}
Expand All @@ -566,7 +579,7 @@ export default class extends Generator {
try {
availGenerators = await listGeneratorsForOrg(this.options.ghOrg, this.options.subGeneratorPrefix, this.options.ghThreshold);
} catch (e) {
console.error(`Failed to connect to GitHub to retrieve all available generators for "${this.options.ghOrg}" organization! Run with --verbose for details!`);
console.error(`Failed to connect to GitHub to retrieve all available generators for "${this.options.ghOrg}" organization! Run with --verbose for details!\n(Hint: ${e.message})`);
if (this.options.verbose) {
console.error(e);
}
Expand All @@ -585,7 +598,7 @@ export default class extends Generator {
try {
availGenerators = availGenerators.concat(await listGeneratorsForUser(this.options.addGhOrg, this.options.addSubGeneratorPrefix, this.options.ghThreshold));
} catch (e1) {
console.error(`Failed to connect to GitHub to retrieve additional generators for organization or user "${this.options.addGhOrg}"! Run with --verbose for details!`);
console.error(`Failed to connect to GitHub to retrieve additional generators for organization or user "${this.options.addGhOrg}"! Run with --verbose for details!\n(Hint: ${e.message})`);
if (this.options.verbose) {
console.error(e1);
}
Expand Down
159 changes: 143 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"colors": "^1.4.0",
"glob": "^10.3.10",
"libnpmconfig": "^1.2.1",
"node-fetch": "^3.3.2",
"rimraf": "^5.0.5",
"yeoman-environment": "^3.19.3",
"yeoman-generator": "^5.10.0",
Expand Down

0 comments on commit 035364b

Please sign in to comment.