@reldens/server-utils 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,14 +20,16 @@ class ProtocolEnforcer
20
20
  this.useHttps = config.useHttps || false;
21
21
  this.enforceProtocol = config.enforceProtocol !== false;
22
22
  app.use((req, res, next) => {
23
- let protocol = (req.get('X-Forwarded-Proto') || req.protocol || '').toLowerCase();
23
+ let forwardedProto = req.get('X-Forwarded-Proto');
24
+ let protocol = (forwardedProto || req.protocol || '').toLowerCase();
24
25
  let host = (req.get('host') || '').toLowerCase().trim();
26
+ let isBehindProxy = !!forwardedProto;
25
27
  if(this.isDevelopmentMode){
26
28
  res.removeHeader('Origin-Agent-Cluster');
27
29
  res.removeHeader('Strict-Transport-Security');
28
30
  res.removeHeader('upgrade-insecure-requests');
29
31
  res.set('Origin-Agent-Cluster', '?0');
30
- if(this.enforceProtocol && host){
32
+ if(this.enforceProtocol && host && !isBehindProxy){
31
33
  if(!this.useHttps && 'https' === protocol){
32
34
  return res.redirect(301, 'http://'+host+req.url);
33
35
  }
@@ -97,30 +97,12 @@ class AppServerFactory
97
97
  };
98
98
  }
99
99
 
100
- parseUrl(url)
101
- {
102
- if(!url || 'string' !== typeof url){
103
- return false;
104
- }
105
- let cleanUrl = url.trim();
106
- if(!cleanUrl.startsWith('http://') && !cleanUrl.startsWith('https://')){
107
- return false;
108
- }
109
- let isHttps = cleanUrl.startsWith('https://');
110
- let domain = cleanUrl.replace(/^https?:\/\//, '').split(':')[0];
111
- return {
112
- isHttps,
113
- domain,
114
- protocol: isHttps ? 'https' : 'http'
115
- };
116
- }
117
-
118
100
  createAppServer(appServerConfig)
119
101
  {
120
102
  if(appServerConfig){
121
103
  Object.assign(this, appServerConfig);
122
104
  }
123
- this.processEnvironmentUrls();
105
+ this.addHttpDomainsAsDevelopment();
124
106
  this.detectDevelopmentMode();
125
107
  this.setupDevelopmentConfiguration();
126
108
  this.setupProtocolEnforcement();
@@ -142,23 +124,23 @@ class AppServerFactory
142
124
  return {app: this.app, appServer: this.appServer};
143
125
  }
144
126
 
145
- processEnvironmentUrls()
127
+ extractDomainFromHttpUrl(url)
146
128
  {
147
- let publicUrlInfo = this.parseUrl(process.env.RELDENS_PUBLIC_URL);
148
- let hostUrlInfo = this.parseUrl(process.env.RELDENS_APP_HOST);
149
- if(publicUrlInfo){
150
- this.useHttps = publicUrlInfo.isHttps;
151
- if(!this.developmentDomains.includes(publicUrlInfo.domain)){
152
- this.developmentDomains.push(publicUrlInfo.domain);
153
- }
129
+ if(!url || !url.startsWith('http://')){
130
+ return false;
154
131
  }
155
- if(hostUrlInfo){
156
- if(!publicUrlInfo){
157
- this.useHttps = hostUrlInfo.isHttps;
158
- }
159
- if(!this.developmentDomains.includes(hostUrlInfo.domain)){
160
- this.developmentDomains.push(hostUrlInfo.domain);
161
- }
132
+ return url.replace(/^http:\/\//, '').split(':')[0];
133
+ }
134
+
135
+ addHttpDomainsAsDevelopment()
136
+ {
137
+ let hostDomain = this.extractDomainFromHttpUrl(process.env.RELDENS_APP_HOST);
138
+ let publicDomain = this.extractDomainFromHttpUrl(process.env.RELDENS_PUBLIC_URL);
139
+ if(hostDomain && !this.developmentDomains.includes(hostDomain)){
140
+ this.developmentDomains.push(hostDomain);
141
+ }
142
+ if(publicDomain && !this.developmentDomains.includes(publicDomain)){
143
+ this.developmentDomains.push(publicDomain);
162
144
  }
163
145
  }
164
146
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/server-utils",
3
3
  "scope": "@reldens",
4
- "version": "0.30.0",
4
+ "version": "0.31.0",
5
5
  "description": "Reldens - Server Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",