cortico-world-parlor 0.26.4 → 0.26.5

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,3 +1,12 @@
1
+ ## 0.26.5
2
+
3
+ 修 0.26.4 自己带来的一处缓存隐患,行为不变。
4
+
5
+ - npm 打包会把所有文件的 mtime 抹成 1985 年,0.26.4 的 ETag 用「大小 + mtime」算,
6
+ 等于只看大小:以后某个版本改了 `play.js` 却刚好长度一样,浏览器会一直拿着旧的那份
7
+ (先吃 5 分钟缓存,再一直 304)。现在 ETag 改成按内容算,缓存判据加上 inode,
8
+ 换文件一定重新读、重新压。
9
+
1
10
  ## 0.26.4
2
11
 
3
12
  装完就能玩:少一步配置、少一次「落在旧身份上」、首屏快一截。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-parlor",
3
- "version": "0.26.4",
3
+ "version": "0.26.5",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "筆離嘢"
@@ -2,6 +2,7 @@
2
2
  * 网页客户端的静态文件。房主的客厅在香港,从国内拉一次 play.js(129KB)要七八秒;
3
3
  * 压完只有 30KB 左右,所以文本资源按文件压一次放内存,配上 ETag —— 第二次打开只回 304。
4
4
  */
5
+ import { createHash } from 'node:crypto';
5
6
  import { readFileSync, statSync } from 'node:fs';
6
7
  import type { IncomingMessage, ServerResponse } from 'node:http';
7
8
  import { gzipSync } from 'node:zlib';
@@ -34,22 +35,26 @@ export const PLAY_CSP = [
34
35
  const COMPRESSIBLE = new Set(['.html', '.js', '.css', '.svg', '.json', '.map']);
35
36
 
36
37
  interface Asset {
38
+ /** 缓存判据:大小 / mtime / inode 任一变了就重读。npm 包里 mtime 是固定的 1985,inode 是唯一可靠的信号。 */
39
+ stamp: string;
37
40
  etag: string;
38
41
  body: Buffer;
39
42
  gzipped: Buffer;
40
43
  }
41
44
 
42
- /** 一个进程里每个文件只读一次盘、只压一次;文件换了(mtime/size 变了)就重来。 */
45
+ /** 一个进程里每个文件只读一次盘、只压一次;文件换了就重来。 */
43
46
  const assets = new Map<string, Asset>();
44
47
 
45
48
  function loadAsset(rel: string): Asset {
46
49
  const file = new URL(`../web/${rel}`, import.meta.url);
47
50
  const info = statSync(file);
48
- const etag = `"${info.size.toString(16)}-${Math.floor(info.mtimeMs).toString(16)}"`;
51
+ const stamp = `${info.size}:${Math.floor(info.mtimeMs)}:${info.ino}`;
49
52
  const cached = assets.get(rel);
50
- if (cached && cached.etag === etag) return cached;
53
+ if (cached && cached.stamp === stamp) return cached;
51
54
  const body = readFileSync(file);
52
- const asset: Asset = { etag, body, gzipped: gzipSync(body) };
55
+ // ETag 用内容算:npm 打包会把 mtime 抹平,同长度的改动靠 mtime 认不出来。
56
+ const etag = `"${createHash('sha1').update(body).digest('base64url').slice(0, 18)}"`;
57
+ const asset: Asset = { stamp, etag, body, gzipped: gzipSync(body) };
53
58
  assets.set(rel, asset);
54
59
  return asset;
55
60
  }