@rpcbase/server 0.194.0 → 0.196.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.194.0",
3
+ "version": "0.196.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -5,13 +5,22 @@ const glob = require("glob")
5
5
 
6
6
  const async_wrapper = require("../helpers/async_wrapper")
7
7
 
8
+
9
+ const COMPRESSED_EXTENSIONS_MAP = {
10
+ ".css": "text/css",
11
+ ".js": "text/javascript",
12
+ ".svg": "image/svg+xml",
13
+ }
14
+
15
+ const COMPRESSED_EXTENSIONS = Object.keys(COMPRESSED_EXTENSIONS_MAP)
16
+
17
+
18
+
8
19
  const src_path = path.join(process.cwd(), "./src/")
9
20
  const build_dir = path.join(process.cwd(), "build/")
10
21
  const client_build_dir = path.join(build_dir, "./client")
11
22
 
12
23
 
13
- // TODO: add build time static assets compression
14
-
15
24
  const get_client_routes = () => {
16
25
  const client_files = glob.sync(path.join(client_build_dir, "./**/*"))
17
26
  const routes = client_files
@@ -24,6 +33,44 @@ const get_client_routes = () => {
24
33
  }
25
34
 
26
35
 
36
+ const serve_file = (req, res, full_path) => {
37
+ const file_path = path.join(client_build_dir, full_path)
38
+
39
+ const extname = path.extname(full_path)
40
+ const has_compression = COMPRESSED_EXTENSIONS.includes(extname)
41
+
42
+ if (has_compression) {
43
+ const accept_encoding = req.headers["accept-encoding"]
44
+ // brotli
45
+ if (accept_encoding.indexOf("br") > -1) {
46
+ res.sendFile(`${file_path}.br`, {
47
+ headers: {
48
+ "Content-Encoding": "br"
49
+ "Content-Type": COMPRESSED_EXTENSIONS_MAP[extname],
50
+ }
51
+ })
52
+ }
53
+ // gzip
54
+ else if (accept_encoding.indexOf("gzip") > -1) {
55
+ res.sendFile(`${file_path}.gz`, {
56
+ headers: {
57
+ "Content-Encoding": "gzip",
58
+ "Content-Type": COMPRESSED_EXTENSIONS_MAP[extname],
59
+ }
60
+ })
61
+ }
62
+ // unknown accept-encoding
63
+ else {
64
+ res.sendFile(file_path)
65
+ }
66
+ }
67
+ // not compressed
68
+ else {
69
+ res.sendFile(file_path)
70
+ }
71
+ }
72
+
73
+
27
74
  const client_router = (app) => {
28
75
  const client_routes = get_client_routes()
29
76
 
@@ -45,8 +92,8 @@ const client_router = (app) => {
45
92
  }
46
93
 
47
94
  if (client_routes.indexOf(full_path) > -1) {
48
- const file_path = path.join(client_build_dir, full_path)
49
- return res.sendFile(file_path)
95
+ serve_file(req, res, full_path)
96
+ return
50
97
  } else {
51
98
  // TODO: should handle 404 here
52
99
  res.writeHead(200, {