Add a CLI for running a proxy server (#495)

This commit is contained in:
prathamesh0 2023-12-05 18:17:27 +05:30 committed by GitHub
parent ceb0a3de23
commit 304b116c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 2 deletions

View File

@ -94,3 +94,25 @@ A basic CLI to to fetch and compare watcher GQL response.
```
* The diff for latest and previous GQL responses is shown
## proxy
Runs a proxy server pointed to the given upstream endpoint
* Install dependencies and build:
```bash
# In repo root
yarn
# In packages/cli
yarn build
```
* Run the proxy server:
```bash
yarn proxy --upstream <UPSTREAM_ENDPOINT> --origin-header [ORIGN_HEADER] --port [PROXY_SERVER_PORT]
# For details, run: yarn proxy --help
```

View File

@ -9,7 +9,8 @@
"clean": "rm -rf ./dist",
"copy-assets": "copyfiles -u 1 src/**/*.gql dist/",
"chat": "DEBUG='vulcanize:*, laconic:*' node dist/chat.js",
"compare-gql": "DEBUG='vulcanize:*' node dist/compare-gql.js"
"compare-gql": "DEBUG='vulcanize:*' node dist/compare-gql.js",
"proxy": "DEBUG='laconic:*' node dist/proxy.js"
},
"dependencies": {
"@apollo/client": "^3.7.1",
@ -31,6 +32,7 @@
"graphql": "^15.5.0",
"graphql-request": "^6.1.0",
"graphql-subscriptions": "^2.0.0",
"http-proxy-middleware": "^2.0.6",
"js-yaml": "^4.0.0",
"json-diff": "^0.5.4",
"lodash": "^4.17.21",

View File

@ -17,3 +17,4 @@ export * from './fill';
export * from './create-state-gql';
export * from './peer';
export * from './utils';
export * from './proxy';

79
packages/cli/src/proxy.ts Normal file
View File

@ -0,0 +1,79 @@
//
// Copyright 2023 Vulcanize, Inc.
//
import debug from 'debug';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import cors from 'cors';
const log = debug('laconic:proxy');
interface Arguments {
port: number;
upstream: string;
originHeader?: string;
}
async function main (): Promise<void> {
const argv: Arguments = _getArgv();
const app = express();
// Enable CORS
app.use(cors());
const upstreamEndpoint: string = argv.upstream;
if (!upstreamEndpoint) {
throw new Error('Upstream endpoint not provided');
}
// Create a proxy
const proxyMiddleware = createProxyMiddleware({
target: upstreamEndpoint,
changeOrigin: true, // Enable CORS bypass
logLevel: 'debug', // Set log level as needed
onProxyReq: (proxyReq) => {
if (argv.originHeader) {
proxyReq.setHeader('Origin', argv.originHeader);
}
}
});
// Use the proxy middleware for incoming requests
app.use('/', proxyMiddleware);
// Start the server
app.listen(argv.port, () => {
log(`Proxy server listening on port ${argv.port}`);
});
}
function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).env(
'PROXY'
).options({
port: {
type: 'number',
describe: 'Port to listen on (env: PROXY_PORT)',
default: 4000
},
upstream: {
type: 'string',
describe: 'Upstream endpoint (env: PROXY_UPSTREAM)',
demandOption: true
},
originHeader: {
type: 'string',
describe: 'Origin header to be used (env: PROXY_ORIGIN_HEADER)'
}
}).argv;
}
main().catch(err => {
log(err);
});

View File

@ -3671,6 +3671,13 @@
"@types/minimatch" "*"
"@types/node" "*"
"@types/http-proxy@^1.17.8":
version "1.17.14"
resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.14.tgz#57f8ccaa1c1c3780644f8a94f9c6b5000b5e2eec"
integrity sha512-SSrD0c1OQzlFX7pGu1eXxSEjemej64aaNPRhhVYUGqXh0BtldAAx37MG8btcumvpgKyZp1F5Gn3JkktdxiFv6w==
dependencies:
"@types/node" "*"
"@types/js-yaml@^4.0.3", "@types/js-yaml@^4.0.4":
version "4.0.5"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.5.tgz#738dd390a6ecc5442f35e7f03fa1431353f7e138"
@ -8010,7 +8017,7 @@ eventemitter3@4.0.4:
resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.4.tgz"
integrity sha512-rlaVLnVxtxvoyLsQQFBx53YmXHDxRIzzTLbdfxqi4yocpSjAxXwkU0cScM5JgSKMqEhrZpnvQ2D9gjylR0AimQ==
eventemitter3@^4.0.4, eventemitter3@^4.0.7:
eventemitter3@^4.0.0, eventemitter3@^4.0.4, eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
@ -8401,6 +8408,11 @@ flow-stoplight@^1.0.0:
resolved "https://registry.npmjs.org/flow-stoplight/-/flow-stoplight-1.0.0.tgz"
integrity sha1-SiksW8/4s5+mzAyxqFPYbyfu/3s=
follow-redirects@^1.0.0:
version "1.15.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==
follow-redirects@^1.12.1:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
@ -9418,6 +9430,26 @@ http-proxy-agent@^4.0.1:
agent-base "6"
debug "4"
http-proxy-middleware@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz#e1a4dd6979572c7ab5a4e4b55095d1f32a74963f"
integrity sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==
dependencies:
"@types/http-proxy" "^1.17.8"
http-proxy "^1.18.1"
is-glob "^4.0.1"
is-plain-obj "^3.0.0"
micromatch "^4.0.2"
http-proxy@^1.18.1:
version "1.18.1"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549"
integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==
dependencies:
eventemitter3 "^4.0.0"
follow-redirects "^1.0.0"
requires-port "^1.0.0"
http-signature@~1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz"
@ -10107,6 +10139,11 @@ is-plain-obj@^2.0.0, is-plain-obj@^2.1.0:
resolved "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
is-plain-obj@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==
is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4:
version "2.0.4"
resolved "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz"
@ -14207,6 +14244,11 @@ require-package-name@^2.0.1:
resolved "https://registry.yarnpkg.com/require-package-name/-/require-package-name-2.0.1.tgz#c11e97276b65b8e2923f75dabf5fb2ef0c3841b9"
integrity sha512-uuoJ1hU/k6M0779t3VMVIYpb2VMJk05cehCaABFhXaibcbvfgR8wKiozLjVFSzJPmQMRqIcO0HMyTFqfV09V6Q==
requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==
resolve-cwd@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/resolve-cwd/-/resolve-cwd-3.0.0.tgz"