feat: use vercel route to proxy sourcegraph API and proxy support for development

This commit is contained in:
netcon
2021-03-01 22:50:17 +08:00
parent 48cd8ddcf1
commit 0b0254b0d7
5 changed files with 64 additions and 9 deletions

View File

@@ -12,13 +12,11 @@ import {
import { TextSearchQuery, TextSearchOptions } from 'vscode';
import { trimStart, trimEnd } from './util';
// the Sourcegraph refused the CORS check now, use `cors-anywhere` for temporary
const CORS_PROXY_PREFIX = 'https://cors-anywhere.herokuapp.com/';
const sourcegraphLink = createHttpLink({
uri: CORS_PROXY_PREFIX + 'https://sourcegraph.com/.api/graphql',
headers: {
'x-requested-with': 'https://github1s.com',
},
// Since the Sourcegraph refused the CORS check now,
// use Vercel Serverless Function to proxy it temporarily
// See `/api/sourcegraph.js`
uri: '/api/sourcegraph',
});
const sourcegraphClient = new ApolloClient({

View File

@@ -15,6 +15,7 @@
"eslint-plugin-jsdoc": "^31.6.1",
"eslint-plugin-prettier": "^3.3.1",
"fs-extra": "^9.1.0",
"http-proxy": "^1.18.1",
"husky": "^5.0.9",
"lint-staged": "^10.5.4",
"npm-run-all": "^4.1.5",

View File

@@ -5,12 +5,37 @@ const fs = require('fs');
const path = require('path');
const http = require('http');
const handler = require('serve-handler');
const httpProxy = require('http-proxy');
const APP_ROOT = path.join(__dirname, '..');
const options = { public: path.join(APP_ROOT, 'dist'), cleanUrls: false };
// now sourcegraph graphql api is refused the CORS check
// just proxy the request to sourcegraph directly for a taste
const proxy = httpProxy.createProxyServer({
target: 'https://sourcegraph.com/.api/graphql',
ignorePath: true,
changeOrigin: false,
headers: {
host: 'sourcegraph.com',
},
});
const sourcegraphProxyHandler = (req, res) => {
proxy.web(req, res);
proxy.on('error', (e) => {
res.writeHead(500, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ message: e.message }));
});
};
const server = http.createServer((request, response) => {
const urlObj = url.parse(request.url);
if (urlObj.pathname.startsWith('/api/sourcegraph')) {
return sourcegraphProxyHandler(request, response);
}
return fs.access(
path.join(APP_ROOT, 'dist', urlObj.pathname),
fs.constants.F_OK,

View File

@@ -1,8 +1,15 @@
{
"routes": [{
"routes": [
{
"src": "/api/sourcegraph",
"dest": "https://sourcegraph.com/.api/graphql"
},
{
"src": "/static/(.*)",
"dest": "/static/$1",
"headers": { "Cache-Control": "max-age=604800" }
"headers": {
"Cache-Control": "max-age=604800"
}
},
{
"src": "/favicon.ico",
@@ -17,4 +24,4 @@
"dest": "/index.html"
}
]
}
}

View File

@@ -707,6 +707,11 @@ event-stream@=3.3.4:
stream-combiner "~0.0.4"
through "~2.3.1"
eventemitter3@^4.0.0:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
execa@3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-3.4.0.tgz#c08ed4550ef65d858fac269ffc8572446f37eb89"
@@ -818,6 +823,11 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.1.1.tgz#c4b489e80096d9df1dfc97c79871aea7c617c469"
integrity sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==
follow-redirects@^1.0.0:
version "1.13.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==
follow-redirects@^1.10.0:
version "1.13.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.2.tgz#dd73c8effc12728ba5cf4259d760ea5fb83e3147"
@@ -954,6 +964,15 @@ hosted-git-info@^2.1.4:
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"
integrity sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==
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"
human-signals@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
@@ -1598,6 +1617,11 @@ require-from-string@^2.0.2:
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==
requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
resolve-from@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6"