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 websockets in proxy build script #373

Merged
merged 3 commits into from
May 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 24 additions & 20 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"find-cache-dir": "^3.3.1",
"glob": "^7.1.4",
"got": "^11.1.4",
"http-proxy": "^1.18.1",
"is-builtin-module": "^3.0.0",
"jsonschema": "^1.2.5",
"mime-types": "^2.1.26",
Expand Down
46 changes: 31 additions & 15 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import etag from 'etag';
import {EventEmitter} from 'events';
import execa from 'execa';
import {existsSync, promises as fs, readFileSync} from 'fs';
import got, {Method as RequestMethod} from 'got';
import http from 'http';
import mime from 'mime-types';
import npmRunPath from 'npm-run-path';
Expand Down Expand Up @@ -128,6 +127,19 @@ export async function command(commandOptions: CommandOptions) {
const messageBus = new EventEmitter();
const mountedDirectories: [string, string][] = [];

const httpProxyRequired = config.scripts.reduce((count, workerConfig) => count + (workerConfig.type === 'proxy'?1:0), 0) > 0;
const proxy = httpProxyRequired?await function(){
return import('http-proxy').then(httpProxy => {
const proxy = httpProxy.createProxyServer({});
proxy.on('error', function(err, req, res) {
const reqUrl = req.url!;
console.error(`✘ ${reqUrl}\n${err.message}`);
sendError(res, 502);
});
return proxy;
});
}():null;

// Start with a fresh install of your dependencies, for development, if needed
commandOptions.config.installOptions.dest = DEV_DEPENDENCIES_DIR;
commandOptions.config.installOptions.env.NODE_ENV = process.env.NODE_ENV || 'development';
Expand Down Expand Up @@ -365,20 +377,10 @@ export async function command(commandOptions: CommandOptions) {
}
if (reqPath.startsWith(workerConfig.args.toUrl)) {
const newPath = reqPath.substr(workerConfig.args.toUrl.length);
try {
const response = await got(`${workerConfig.args.fromUrl}${newPath}`, {
method: req.method as RequestMethod,
headers: req.headers,
throwHttpErrors: false,
});
res.writeHead(response.statusCode, response.headers);
res.write(response.body);
} catch (err) {
console.error(`✘ ${reqUrl}\n${err.message}`);
sendError(res, 500);
} finally {
res.end();
}
proxy.web(req, res, {
target: `${workerConfig.args.fromUrl}${newPath}`,
ignorePath: true,
});
return;
}
}
Expand Down Expand Up @@ -663,6 +665,20 @@ export async function command(commandOptions: CommandOptions) {

sendFile(req, res, wrappedResponse, responseFileExt);
})
.on('upgrade', function(req, socket, head) {
const reqUrl = req.url!;
let reqPath = decodeURI(url.parse(reqUrl).pathname!);

for (const workerConfig of config.scripts) {
if (workerConfig.type !== 'proxy') {
continue;
}
if (reqPath.startsWith(workerConfig.args.toUrl)) {
const newPath = reqPath.substr(workerConfig.args.toUrl.length);
proxy.ws(req, socket, head, { target: `${workerConfig.args.fromUrl}${newPath}`, ignorePath: true });
}
}
})
.listen(port);

// Live Reload + File System Watching
Expand Down