Skip to content

Commit cc7316b

Browse files
robhoganfacebook-github-bot
authored andcommitted
Add proxy support to HttpStore for remote caches (attempt 3)
Summary: Add HTTPS proxying support to `HttpStore`, for use by remote cache configurations. ``` - **[Feature]**: Support `proxy` and `socketPath` in remote cache `HttpStore` ``` Reviewed By: GijsWeterings Differential Revision: D73036895 fbshipit-source-id: 5c4bf8f39bd0dd41683fce3c90afab36f0075f00
1 parent d341681 commit cc7316b

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
declare module 'https-proxy-agent' {
12+
type HttpHeaders = {[key: string]: string | Array<string>};
13+
14+
declare export type HttpsProxyAgentOptions = tls$connectOptions &
15+
http$agentOptions & {
16+
headers?: HttpHeaders | (() => HttpHeaders),
17+
...
18+
};
19+
20+
declare export class HttpsProxyAgent extends http$Agent<net$Socket> {
21+
static protocols: ['http', 'https'];
22+
+proxy: URL;
23+
proxyHeaders: HttpHeaders | (() => HttpHeaders);
24+
connectOpts: tls$connectOptions;
25+
constructor(proxy: string | URL, opts?: HttpsProxyAgentOptions): this;
26+
connect(
27+
req: http$requestOptions,
28+
opts: http$agentOptions,
29+
): Promise<net$Socket>;
30+
}
31+
}

packages/metro-cache/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"exponential-backoff": "^3.1.1",
2424
"flow-enums-runtime": "^0.0.6",
25+
"https-proxy-agent": "^7.0.5",
2526
"metro-core": "0.82.1"
2627
},
2728
"devDependencies": {

packages/metro-cache/src/stores/HttpStore.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010

1111
'use strict';
1212

13-
import type {Agent as HttpAgent} from 'http';
14-
import type {Agent as HttpsAgent} from 'https';
15-
1613
const HttpError = require('./HttpError');
1714
const NetworkError = require('./NetworkError');
1815
const {backOff} = require('exponential-backoff');
1916
const http = require('http');
2017
const https = require('https');
21-
const url = require('url');
18+
const {HttpsProxyAgent} = require('https-proxy-agent');
2219
const zlib = require('zlib');
2320

2421
export type Options =
@@ -46,14 +43,16 @@ type EndpointOptions = {
4643
maxAttempts?: number,
4744
retryNetworkErrors?: boolean,
4845
retryStatuses?: $ReadOnlySet<number>,
46+
socketPath?: string,
47+
proxy?: string,
4948
};
5049

5150
type Endpoint = {
5251
module: typeof http | typeof https,
5352
host: string,
5453
path: string,
5554
port: number,
56-
agent: HttpAgent | HttpsAgent,
55+
agent: http$Agent<tls$TLSSocket> | http$Agent<net$Socket>,
5756
params: URLSearchParams,
5857
headers?: {[string]: string},
5958
timeout: number,
@@ -92,7 +91,7 @@ class HttpStore<T> {
9291
}
9392

9493
createEndpointConfig(options: EndpointOptions): Endpoint {
95-
const agentConfig: http$agentOptions = {
94+
const agentConfig = {
9695
family: options.family,
9796
keepAlive: true,
9897
keepAliveMsecs: options.timeout || 5000,
@@ -115,19 +114,29 @@ class HttpStore<T> {
115114
agentConfig.ca = options.ca;
116115
}
117116

118-
const uri = url.parse(options.endpoint);
117+
if (options.socketPath != null) {
118+
// $FlowFixMe `socketPath` is missing in the Flow definition
119+
agentConfig.socketPath = options.socketPath;
120+
}
121+
122+
const uri = new URL(options.endpoint);
119123
const module = uri.protocol === 'http:' ? http : https;
120124

125+
const agent =
126+
options.proxy != null
127+
? new HttpsProxyAgent(options.proxy, agentConfig)
128+
: new module.Agent(agentConfig);
129+
121130
if (!uri.hostname || !uri.pathname) {
122131
throw new TypeError('Invalid endpoint: ' + options.endpoint);
123132
}
124133

125134
return {
135+
agent,
126136
headers: options.headers,
127137
host: uri.hostname,
128138
path: uri.pathname,
129139
port: +uri.port,
130-
agent: new module.Agent(agentConfig),
131140
params: new URLSearchParams(options.params),
132141
timeout: options.timeout || 5000,
133142
module: uri.protocol === 'http:' ? http : https,

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,13 @@ acorn@^8.9.0:
15301530
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.10.0.tgz#8be5b3907a67221a81ab23c7889c4c5526b62ec5"
15311531
integrity sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw==
15321532

1533+
agent-base@^7.0.2:
1534+
version "7.1.1"
1535+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.1.tgz#bdbded7dfb096b751a2a087eeeb9664725b2e317"
1536+
integrity sha512-H0TSyFNDMomMNJQBn8wFV5YC/2eJ+VXECwOadZJT554xP6cODZHPX3H9QMQECxvrgiSOP1pHjy1sMWQVYJOUOA==
1537+
dependencies:
1538+
debug "^4.3.4"
1539+
15331540
ajv@^6.12.4:
15341541
version "6.12.6"
15351542
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
@@ -2133,6 +2140,13 @@ [email protected], debug@^2.6.9:
21332140
dependencies:
21342141
ms "2.0.0"
21352142

2143+
debug@4:
2144+
version "4.3.7"
2145+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52"
2146+
integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==
2147+
dependencies:
2148+
ms "^2.1.3"
2149+
21362150
debug@^3.2.7:
21372151
version "3.2.7"
21382152
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
@@ -3072,6 +3086,14 @@ html-escaper@^2.0.0:
30723086
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.0.tgz#71e87f931de3fe09e56661ab9a29aadec707b491"
30733087
integrity sha512-a4u9BeERWGu/S8JiWEAQcdrg9v4QArtP9keViQjGMdff20fBdd8waotXaNmODqBe6uZ3Nafi7K/ho4gCQHV3Ig==
30743088

3089+
https-proxy-agent@^7.0.5:
3090+
version "7.0.5"
3091+
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.5.tgz#9e8b5013873299e11fab6fd548405da2d6c602b2"
3092+
integrity sha512-1e4Wqeblerz+tMKPIq2EMGiiWW1dIjZOksyHWSUm1rmuvw/how9hBHZ38lAGj5ID4Ik6EdkOw7NmWPy6LAwalw==
3093+
dependencies:
3094+
agent-base "^7.0.2"
3095+
debug "4"
3096+
30753097
human-signals@^2.1.0:
30763098
version "2.1.0"
30773099
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"

0 commit comments

Comments
 (0)