This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
59 lines (50 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
'use strict';
const http = require('spdy')
, fs = require('fs')
, request = require('request')
, app = require('connect')()
, proxy = require('./lib/proxy')
, push = require('./lib/push')
, argv = require('yargs').argv
, path = require('path')
, sprintf = require("sprintf-js").sprintf;
if (require.main === module) {
if (!argv.target || !argv.key || !argv.cert) return printHelp();
const baseUrl = argv.target;
const sslKey = argv.key;
const sslCert = argv.cert;
const port = argv.port || 8080;
const extensions = argv.extensions ? argv.extensions.split(',') : null;
const spdyOpts = {
key: fs.readFileSync(path.normalize(sslKey)),
cert: fs.readFileSync(path.normalize(sslCert)),
spdy: {
protocols: ['h2', 'spdy/3.1', 'http/1.1'],
plain: false,
'x-forwarded-for': true,
}
};
app.use(proxy(baseUrl));
app.use(push({baseUrl: baseUrl, extensions: extensions}));
http.createServer(spdyOpts, app).listen(port);
}
function printHelp() {
const help = `
Welcome to serverpush-proxy!
These parameters are required:
--target= The target URL to be proxied. E.g. http://localhost:8080.
--key= Path to your SSL key (HTTP/2 requires TLS (HTTPS) encryption). E.g. ./certs/key.pem.
--cert= Path to your SSL certificate. E.g. ./certs/cert.pem.
Additionally, these parameters are optional:
--extensions= File extensions to be push candidates. E.g. css,js,svg
--port= Port to make the proxy listen on. Defaults to 8080.
`;
console.log(sprintf(help));
}
module.exports = (config) => {
return {
proxy: proxy(config.baseUrl),
push: push(config)
}
}