Skip to content

Commit a65078e

Browse files
committed
feat: added PROXY protocol support per <ladjs/lad#400>
1 parent fd373a1 commit a65078e

File tree

3 files changed

+339
-316
lines changed

3 files changed

+339
-316
lines changed

index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const etag = require('koa-etag');
1919
const json = require('koa-json');
2020
const koa404Handler = require('koa-404-handler');
2121
const koaConnect = require('koa-connect');
22+
const proxyWrap = require('findhit-proxywrap');
2223
const removeTrailingSlashes = require('koa-no-trailing-slash');
2324
const requestId = require('express-request-id');
2425
const requestReceived = require('request-received');
@@ -27,6 +28,9 @@ const sharedConfig = require('@ladjs/shared-config');
2728
const { boolean } = require('boolean');
2829
const { ratelimit } = require('koa-simple-ratelimit');
2930

31+
const proxiedHttp = proxyWrap.proxy(http);
32+
const proxiedHttps = proxyWrap.proxy(https);
33+
3034
class API {
3135
constructor(config) {
3236
this.config = {
@@ -167,11 +171,20 @@ class API {
167171
else app.use(this.config.routes);
168172
}
169173

174+
const createServer =
175+
this.config.protocol === 'https'
176+
? this.config.proxyProtocol
177+
? proxiedHttps.createServer
178+
: https.createServer
179+
: this.config.proxyProtocol
180+
? proxiedHttp.createServer
181+
: http.createServer;
182+
170183
// start server on either http or https
171184
if (this.config.protocol === 'https')
172-
server = https.createServer(this.config.ssl, app.callback());
185+
server = createServer(this.config.ssl, app.callback());
173186
// server = http2.createSecureServer(this.config.ssl, app.callback());
174-
else server = http.createServer(app.callback());
187+
else server = createServer(app.callback());
175188

176189
// expose app, server, client
177190
this.app = app;

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@
2020
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
2121
],
2222
"dependencies": {
23-
"@koa/router": "^9.1.0",
23+
"@koa/router": "^9.3.1",
2424
"@ladjs/i18n": "^3.0.12",
2525
"@ladjs/redis": "^1.0.4",
26-
"@ladjs/shared-config": "^3.0.8",
26+
"@ladjs/shared-config": "^3.0.9",
2727
"@ladjs/store-ip-address": "^0.0.7",
2828
"boolean": "3.0.1",
2929
"cabin": "^8.0.2",
3030
"express-request-id": "^1.4.1",
31+
"findhit-proxywrap": "^0.3.12",
3132
"kcors": "^2.2.2",
3233
"koa": "^2.13.0",
3334
"koa-404-handler": "^0.0.2",
3435
"koa-basic-auth": "^4.0.0",
3536
"koa-better-error-handler": "^4.0.3",
3637
"koa-better-timeout": "^0.0.4",
3738
"koa-bodyparser": "^4.3.0",
38-
"koa-compress": "^4.0.1",
39+
"koa-compress": "^5.0.0",
3940
"koa-conditional-get": "^2.0.0",
4041
"koa-connect": "^2.1.0",
4142
"koa-etag": "^3.0.0",
@@ -49,7 +50,7 @@
4950
"devDependencies": {
5051
"@commitlint/cli": "^9.0.1",
5152
"@commitlint/config-conventional": "^9.0.1",
52-
"ava": "^3.9.0",
53+
"ava": "^3.10.0",
5354
"codecov": "^3.7.0",
5455
"cross-env": "^7.0.2",
5556
"eslint": "6.x",

0 commit comments

Comments
 (0)