Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for proxies. #18

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
Plugin to Adobe I/O CLI for executing commands related to Adobe Experience Manager.

<!-- toc -->
* [Overview](#overview)
* [Usage](#usage)
* [Adding New Commands](#adding-new-commands)
* [Commands](#commands)
* [Releasing](#releasing)
* [Contributing](#contributing)
* [Licensing](#licensing)
- [Overview](#overview)
- [Usage](#usage)
- [Adding New Commands](#adding-new-commands)
- [Testing Commands](#testing-commands)
- [Commands](#commands)
- [`aio-aem aem:upload FILES_FOLDERS`](#aio-aem-aemupload-files_folders)
- [Proxy Support](#proxy-support)
- [Releasing](#releasing)
- [Contributing](#contributing)
- [Licensing](#licensing)
<!-- tocstop -->


Expand Down Expand Up @@ -142,6 +145,10 @@ EXAMPLES
_See code: [src/commands/aem/upload.js](https://github.com/adobe/aio-cli-plugin-aem/blob/v1.1.1/src/commands/aem/upload.js)_
<!-- commandsstop -->

# Proxy Support

The AEM plugin supports proxies inline with the [AIO CLI](https://github.com/adobe/aio-cli#proxy-support). See the documentation there for details.

# Releasing

This module uses [semantic-release](https://github.com/semantic-release/semantic-release) when publishing new versions. The process is initiated upon merging commits to the `master` branch. Review semantic-release's documentation for commit message format.
Expand Down
92 changes: 53 additions & 39 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"Mark Frisbey"
],
"dependencies": {
"@adobe/aem-upload": "^2.0.1",
"@adobe/aem-upload": "^2.0.2",
"@oclif/command": "^1",
"@oclif/config": "^1.14.0",
"@oclif/errors": "^1.1.2",
"debug": "^4.1.0",
"hpagent": "^1.2.0",
"mustache": "^3.2.1",
"winston": "^3.2.1"
},
Expand Down
70 changes: 64 additions & 6 deletions src/commands/aem/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Path = require('path');
const fs = require('fs');
const winston = require('winston');
const mustache = require('mustache');
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');

const {flags} = require('@oclif/command');
const {
Expand Down Expand Up @@ -65,13 +66,10 @@ class UploadCommand extends BaseCommand {
deep,
} = newFlags;

const uploadUrl = `${trimRight(host, ['/'])}${target}`;
const uploadOptions = new FileSystemUploadOptions()
.withUrl(`${trimRight(host, ['/'])}${target}`)
.withHttpOptions({
headers: {
Authorization: `Basic ${Buffer.from(credential).toString('base64')}`
}
})
.withUrl(uploadUrl)
.withHttpOptions(this.buildHttpOptions(uploadUrl, credential))
.withDeepUpload(deep)
.withMaxConcurrent(parseInt(threads, 10));

Expand All @@ -94,6 +92,66 @@ class UploadCommand extends BaseCommand {

log.info(`Log file is saved to log file '${logFile}'`);
}

/**
* Creates a simple object containing the options that will be provided
* to Fetch for requests sent by the CLI.
* @param {string} uploadUrl URL to which items are being uploaded.
* @param {string} credential Basic auth credentials to include in
* each request.
* @returns {*} HTTP options for Fetch.
*/
buildHttpOptions(uploadUrl, credential) {
const httpOptions = {
headers: {
Authorization: `Basic ${Buffer.from(credential).toString('base64')}`
},
retryOptions: {
retryOnHttpResponseError: UploadCommand.shouldRetry,
},
};
const agent = this.getProxyAgent(uploadUrl);
if (agent) {
httpOptions.agent = agent;
}
return httpOptions;
}

/**
* Determines whether a given HTTP response error should be retried. Qualifying errors will include
* 404 response codes to the initiate/complete servlets. This is for handling eventual consistency
* issues.
* @param {*} [httpResponseError] An error received by the node-httptransfer module.
* @returns {boolean} True if a request should be retried, false otherwise.
*/
static shouldRetry(httpResponseError = {}) {
const { status, url = '' } = httpResponseError;
const lowerCaseUrl = url.toLowerCase();
if (status === 404 && (lowerCaseUrl.includes('.initiateupload.json') || lowerCaseUrl.includes('.completeupload.json'))) {
return true;
}
return false;
}

/**
* Retrieves the HTTP agent that should be used to provide proxy capabilities
* for the command.
* @param {string} uploadUrl URL to which the uploading is being performed.
* Will be used to determine which proxy to use.
* @returns {*} An object that can be used as an HTTP agent for fetch.
*/
getProxyAgent(uploadUrl) {
const url = new URL(uploadUrl);
const httpsProxy = process.env.HTTPS_PROXY;
const httpProxy = process.env.HTTP_PROXY;

if (url.protocol === 'https:' && httpsProxy) {
return new HttpsProxyAgent({ proxy: httpsProxy });
} else if (url.protocol === 'http:' && httpProxy) {
return new HttpProxyAgent({ proxy: httpProxy });
}
return undefined;
}
}

UploadCommand.flags = Object.assign({}, BaseCommand.flags, {
Expand Down
Loading