Skip to content

Commit 7aabfb4

Browse files
authored
Merge pull request daquinoaldo#4 from daquinoaldo/production
Production env
2 parents 57cf312 + 551f101 commit 7aabfb4

5 files changed

Lines changed: 167 additions & 17 deletions

File tree

.travis.yml

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,97 @@
11
language: node_js
22
node_js:
3-
- "node"
3+
- node
4+
- 11
5+
- 11.9
6+
- 11.8
7+
- 11.7
8+
- 11.6
9+
- 11.5
10+
- 11.4
11+
- 11.3
12+
- 11.2
13+
- 11.1
14+
- 11.0
15+
- 10
16+
- 10.15.1
17+
- 10.15.0
18+
- 10.14.2
19+
- 10.14.1
20+
- 10.14.0
21+
- 10.13
22+
- 10.12
23+
- 10.11
24+
- 10.10
25+
- 10.9
26+
- 10.8
27+
- 10.7
28+
- 10.6
29+
- 10.5
30+
- 10.4.1
31+
- 10.4.0
32+
- 10.3
33+
- 10.2.1
34+
- 10.2.0
35+
- 10.1
36+
- 10.0
37+
- 9.11.2
38+
- 9.11.1
39+
- 9.11.0
40+
- 9.10.1
41+
- 9.10.0
42+
- 9.9
43+
- 9.8
44+
- 9.7.1
45+
- 9.7.0
46+
- 9.6.1
47+
- 9.6.0
48+
- 9.5
49+
- 9.4
50+
- 9.3
51+
- 9.2.1
52+
- 9.2.0
53+
- 9.1
54+
- 9.0
55+
- 8
56+
- 8.15
57+
- 8.14.1
58+
- 8.14.0
59+
- 8.13
60+
- 8.12
61+
- 8.11.4
62+
- 8.11.3
63+
- 8.11.2
64+
- 8.11.1
65+
- 8.11.0
66+
- 8.10
67+
- 8.9.4
68+
- 8.9.3
69+
- 8.9.2
70+
- 8.9.1
71+
- 8.9.0
72+
- 8.8.1
73+
- 8.8.0
74+
- 8.7
75+
- 8.6
76+
- 8.5
77+
- 8.4
78+
- 8.3
79+
- 8.2.1
80+
- 8.2.0
81+
- 8.1.4
82+
- 8.1.3
83+
- 8.1.2
84+
- 8.1.1
85+
- 8.1.0
86+
- 8.0
87+
- 7.10.1
88+
- 7.10.0
89+
- 7.9
90+
- 7.8
91+
- 7.7.4
92+
- 7.7.3
93+
- 7.7.2
94+
- 7.7.1
95+
- 7.7.0
96+
- 7.6
497
after_success: npm run coveralls

README.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ It works with MacOS, Linux and Windows, on Chrome and Firefox, and requires you
1212
[![Known Vulnerabilities](https://snyk.io/test/npm/https-localhost/badge.svg)](https://snyk.io/test/npm/https-localhost)
1313
[![GitHub issues](https://img.shields.io/github/issues/daquinoaldo/https-localhost.svg)](https://github.com/daquinoaldo/https-localhost/issues)
1414

15-
## Install
15+
## Install and use standalone
1616
```
1717
npm i -g https-localhost
1818
```
19-
20-
### Use standalone
2119
```
2220
serve ~/myproj
2321
```
@@ -26,13 +24,12 @@ serve ~/myproj
2624
- You can change the port setting the PORT environmental variable: `PORT=4433 serve ~/myproj`.
2725

2826

29-
### Use as module
30-
Install the dependency
27+
## Use as module
28+
Install as a dependency:
3129
```
3230
npm i -s https-localhost
3331
```
34-
35-
Put in your `index.js` file:
32+
Then put in your `index.js` file:
3633
```
3734
const app = require("https-localhost")
3835
// app is an express app, do what you usually do with express
@@ -42,6 +39,15 @@ app.listen(port)
4239
- To redirect the http traffic to https use `app.redirect()`.
4340
- You can serve static files with `app.serve(path)`.
4441

42+
## Production
43+
This tool has a production version that activates **HTTP/2**, **compression** and **minify**.
44+
```
45+
NODE_ENV=production serve ~/myproj
46+
```
47+
I decide to not activate it by default since it is usually an unwanted behaviour for localhost testing, but sometimes it could be userful, e.g. to test Progressive Web Application or more ingeneral the website performances.
48+
49+
**IMPORTANT**: the fact that there is a production enviornment doesn't mean that this tool is suitable for production. It's intended to be used only for local testing.
50+
4551
## Why and how it works
4652
Serving static content on localhost in a trusted SSL connection is not so simple.
4753
It requires to manually generate and trust certificates, with complicate commands and many manual steps.
@@ -67,6 +73,21 @@ Checkout the updated list [here](https://github.com/FiloSottile/mkcert/blob/mast
6773
- Java (when `JAVA_HOME` is set)
6874

6975

76+
## Troubleshooting
77+
### Node.js version
78+
https-localhost requires Node.js 7.6 or higher.
79+
<sub>If you need compatibility with previously Node.js versions let me know, I'll try to rearrange the code.</sub>
80+
81+
### RangeError
82+
```
83+
RangeError: Invalid typed array length: -4095
84+
```
85+
It is a known bug of `spdy` that is present sometimes with some old Node.js versions.
86+
87+
It should be present only with `NODE_ENV=production`, hence the easiest fix is to avoid using the production env. Anyway, if you need the production env, you can try to update Node.js to the latest release, or to the most stable LTS version.
88+
89+
I've tried to reproduce this error without any success (checkout the [Travis build logs](https://travis-ci.org/daquinoaldo/https-localhost)). If you can help please open an issue and describe as better as you can how to reproduce it, I'll be happy to help you.
90+
7091
## License
7192
Is released under [AGPL-3.0 - GNU Affero General Public License v3.0](LICENSE).
7293

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const path = require("path")
44
const fs = require("fs")
55
const http = require("http")
66
// spdy allows http2, while waiting express to support the http2 module
7-
const https = require("spdy")
7+
const https = process.env.NODE_ENV === "production"
8+
? require("spdy") : require("https")
89
const express = require("express")
910
const compression = require("compression")
1011
const minify = require("express-minify")
@@ -35,9 +36,12 @@ app.listen = function(port = process.env.PORT ||
3536
}
3637

3738
// use gzip compression minify
38-
app.use(compression({ threshold: 1 }))
39-
app.use(minify())
40-
app.set("json spaces", 0)
39+
if (process.env.NODE_ENV === "production") {
40+
console.log("HOLA")
41+
app.use(compression({ threshold: 1 }))
42+
app.use(minify())
43+
app.set("json spaces", 0)
44+
}
4145

4246
/* SETUP USEFUL FUNCTIONS */
4347

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "https-localhost",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "HTTPS server running on localhost",
55
"main": "index.js",
66
"scripts": {

test/test.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function makeRequest(path = "/", secure = true, port = HTTPS_PORT) {
1515
port: port,
1616
path: path,
1717
method: "GET",
18-
headers: { },
18+
headers: { "accept-encoding": "gzip" },
1919
rejectUnauthorized: false
2020
}
2121
const protocol = secure ? https : http
@@ -26,7 +26,8 @@ function makeRequest(path = "/", secure = true, port = HTTPS_PORT) {
2626
resp.on("data", chunk => data += chunk)
2727
resp.on("end", () => resolve({
2828
data: data,
29-
statusCode: resp.statusCode
29+
statusCode: resp.statusCode,
30+
headers: resp.headers
3031
}))
3132
}).on("error", err => reject(err))
3233
.end()
@@ -61,8 +62,6 @@ describe("Testing https-localhost", () => {
6162
})
6263

6364
it("serves static files from custom path", async function() {
64-
// set the environment port
65-
process.env.PORT = HTTPS_PORT
6665
// start the server (serving the test folder)
6766
app.serve("test", HTTPS_PORT)
6867
// make the request and check the output
@@ -72,6 +71,8 @@ describe("Testing https-localhost", () => {
7271
})
7372

7473
it("serves static files from default path and env port", async function() {
74+
// set the environment port
75+
process.env.PORT = HTTPS_PORT
7576
// start the server (serving the default folder)
7677
app.serve()
7778
// make the request and check the output
@@ -80,11 +81,42 @@ describe("Testing https-localhost", () => {
8081
res.data.toString() === fs.readFileSync("test/static.html").toString()))
8182
})
8283

84+
it("doesn't crash on 404", async function() {
85+
// start the server (serving the default folder)
86+
app.serve()
87+
// make the request and check the status code
88+
await makeRequest("/do-not-exist")
89+
.then(res => assert(res.statusCode === 404))
90+
})
91+
92+
it("doesn't crash if the static path doesn't exists", async function() {
93+
// start the server (serving a non existing folder)
94+
app.serve("do-not-exists")
95+
// make the request and check the status code
96+
await makeRequest("/")
97+
.then(res => assert(res.statusCode === 404))
98+
})
99+
83100
it("redirect http to https", async function() {
84101
// start the redirection
85102
await app.redirect(HTTP_PORT)
86103
// make the request and check the status
87104
await makeRequest("/", false, HTTP_PORT)
88105
.then(res => assert(res.statusCode === 301))
89106
})
107+
108+
// IMPORTANT: this test MUST be the latest one, always.
109+
// It delete the cache og the index module,
110+
// so the variable app is broken after this test.
111+
it("is ready for production", async function() {
112+
// set NODE_ENV to production
113+
delete require.cache[require.resolve("../index.js")]
114+
process.env.NODE_ENV = "production"
115+
const production = require("../index.js")
116+
// start the server (serving the test folder)
117+
production.serve("test", HTTPS_PORT)
118+
// make the request and check the output
119+
await makeRequest("/static.html")
120+
.then(res => assert(res.headers["content-encoding"] === "gzip"))
121+
})
90122
})

0 commit comments

Comments
 (0)