dsh-mesh-access 1.0.2 → 1.0.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # 更新日志
2
2
 
3
+ ## 1.0.3 — 静态资源长缓存(给慢链路减负)
4
+
5
+ 上游返回的 `/assets/*.js|css` **没有** `cache-control`,也没有 `etag` / `last-modified`,浏览器无法判断新鲜度,重载时只能整套重新下载(一套 1.5–3 MB)。在组网这种 250 KB/s、往返 500 ms 的链路上,一次重连就等于重传一遍,越重连越慢、越慢越容易再断。
6
+
7
+ - 反代对 `/assets/<带哈希>.(js|css|woff2|png|svg|webp)` 补上 `cache-control: public, max-age=31536000, immutable`;
8
+ - 文件名本身带内容哈希,所以长缓存是安全的(换版本 = 换文件名)。
9
+
3
10
  ## 1.0.2 — 把卖点改成「手机远程控制」
4
11
 
5
12
  功能没变,只是把话说对:这东西最主要的用法是**用手机远程控制电脑上的 DSH**(同一个实例、同一份会话),
package/lib/index.js CHANGED
@@ -88,6 +88,9 @@ function createProxy({ listenAddress, listenPort, upstream, upstreamPort }) {
88
88
  // 上游地址,就必须把 Origin 一起改写成上游的 origin;否则浏览器的 WebSocket 升级
89
89
  // 永远 403,界面上表现成「一直重新连接中」。
90
90
  const upstreamOrigin = `http://${target}`
91
+ // 带内容哈希的静态资源(/assets/index-BXV-oxvX.js 这种)
92
+ const CACHEABLE = /\/assets\/[^?]+\.(?:js|css|woff2?|png|svg|webp)$/
93
+
91
94
  const rewrite = (headers) => {
92
95
  const h = { ...headers }
93
96
  const incomingHost = headers.host ?? ''
@@ -102,7 +105,13 @@ function createProxy({ listenAddress, listenPort, upstream, upstreamPort }) {
102
105
  const up = http.request(
103
106
  { host: upstream, port: upstreamPort, method: req.method, path: req.url, headers: rewrite(req.headers) },
104
107
  (r) => {
105
- res.writeHead(r.statusCode ?? 502, r.headers)
108
+ const headers = { ...r.headers }
109
+ // 前端资源文件名带内容哈希,可以放心长缓存。上游没给 cache-control / etag,
110
+ // 浏览器就只能每次重新下载(一套 1.5–3 MB),在组网这种慢链路上等于每次重连都重传一遍。
111
+ if (r.statusCode === 200 && CACHEABLE.test(req.url || '')) {
112
+ headers['cache-control'] = 'public, max-age=31536000, immutable'
113
+ }
114
+ res.writeHead(r.statusCode ?? 502, headers)
106
115
  r.pipe(res)
107
116
  },
108
117
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mesh-access",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Control your DSH from your phone: opens a port on the mesh/VPN interface from inside the running server process, so a phone browser gets the same instance and the same session list — no SSH, no second dsh. Also reachable from another PC, a GUI NAS, or a TV box.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",