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

Proxy support for amp-optimizer #857

Merged
merged 5 commits into from
Jul 30, 2020
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: 3 additions & 18 deletions packages/optimizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,11 @@ Automatically import any missing AMP Extensions (e.g. amp-carousel).

#### `fetch`

Provide a custom fetch handler. You can use this option to configure a custom proxy server. Example:
Inside proxy, environment variable `https_proxy` is supported. Example:

```js
const nodeFetch = require('node-fetch');

const proxyHost = '...';
const proxyPort = '...';

const fetch = (url, opts={}) => {
opts.agent = new HttpsProxyAgent(`${proxyHost}:${proxyPort}');
return nodeFetch(url, opts)
}
const optimizer = AmpOptimizer.create({
fetch,
});
```

- name: `fetch`
- valid options: a [whatwg fetch](https://github.com/whatwg/fetch) compatible fetch implementation.
- default: [node-fetch](https://www.npmjs.com/package/node-fetch)
https_proxy=http://[proxy_host]:[port]
```

#### `format`

Expand Down
5 changes: 5 additions & 0 deletions packages/optimizer/lib/warmup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const AbortController = require('abort-controller');
const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');
const log = require('@ampproject/toolbox-core').log.tag('AMP OPTIMIZER');
const AmpOptimizer = require('../');

Expand All @@ -29,6 +30,10 @@ const fetchWithTimout = (url, opts = {}) => {
controller.abort();
}, DOWNLOAD_TIMEOUT);
opts.signal = controller.signal;
const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
if (httpsProxy) {
opts.agent = new HttpsProxyAgent(httpsProxy);
}
return fetch(url, opts).finally(() => {
clearTimeout(timeout);
});
Expand Down
270 changes: 0 additions & 270 deletions packages/optimizer/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 packages/optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"domhandler": "3.0.0",
"domutils": "2.1.0",
"htmlparser2": "4.1.0",
"https-proxy-agent": "5.0.0",
"lru-cache": "5.1.1",
"node-fetch": "2.6.0",
"normalize-html-whitespace": "1.0.0",
Expand Down