@rpcbase/server 0.194.0 → 0.195.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.
- package/package.json +1 -1
- package/src/client/client_router.js +41 -4
package/package.json
CHANGED
|
@@ -5,13 +5,14 @@ const glob = require("glob")
|
|
|
5
5
|
|
|
6
6
|
const async_wrapper = require("../helpers/async_wrapper")
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
const COMPRESSED_EXTENSIONS = [".css", ".js", ".svg"]
|
|
10
|
+
|
|
8
11
|
const src_path = path.join(process.cwd(), "./src/")
|
|
9
12
|
const build_dir = path.join(process.cwd(), "build/")
|
|
10
13
|
const client_build_dir = path.join(build_dir, "./client")
|
|
11
14
|
|
|
12
15
|
|
|
13
|
-
// TODO: add build time static assets compression
|
|
14
|
-
|
|
15
16
|
const get_client_routes = () => {
|
|
16
17
|
const client_files = glob.sync(path.join(client_build_dir, "./**/*"))
|
|
17
18
|
const routes = client_files
|
|
@@ -24,6 +25,42 @@ const get_client_routes = () => {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
|
|
28
|
+
const serve_file = (req, res, full_path) => {
|
|
29
|
+
const file_path = path.join(client_build_dir, full_path)
|
|
30
|
+
|
|
31
|
+
const extname = path.extname(full_path)
|
|
32
|
+
const has_compression = COMPRESSED_EXTENSIONS.includes(extname)
|
|
33
|
+
|
|
34
|
+
if (has_compression) {
|
|
35
|
+
const accept_encoding = req.headers["accept-encoding"]
|
|
36
|
+
// brotli
|
|
37
|
+
if (accept_encoding.indexOf("br") > -1) {
|
|
38
|
+
res.sendFile(`${file_path}.br`, {
|
|
39
|
+
headers: {
|
|
40
|
+
"Content-Encoding": "br"
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
// gzip
|
|
45
|
+
else if (accept_encoding.indexOf("gzip") > -1) {
|
|
46
|
+
res.sendFile(`${file_path}.gz`, {
|
|
47
|
+
headers: {
|
|
48
|
+
"Content-Encoding": "gzip"
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
// unknown accept-encoding
|
|
53
|
+
else {
|
|
54
|
+
res.sendFile(file_path)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// not compressed
|
|
58
|
+
else {
|
|
59
|
+
res.sendFile(file_path)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
27
64
|
const client_router = (app) => {
|
|
28
65
|
const client_routes = get_client_routes()
|
|
29
66
|
|
|
@@ -45,8 +82,8 @@ const client_router = (app) => {
|
|
|
45
82
|
}
|
|
46
83
|
|
|
47
84
|
if (client_routes.indexOf(full_path) > -1) {
|
|
48
|
-
|
|
49
|
-
return
|
|
85
|
+
serve_file(req, res, full_path)
|
|
86
|
+
return
|
|
50
87
|
} else {
|
|
51
88
|
// TODO: should handle 404 here
|
|
52
89
|
res.writeHead(200, {
|