dsh-md-plus 0.4.2 → 0.4.4

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/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // dsh-md-plus 宿主半:只做一件事——把两个浏览器端运行时经同源路由发出去。
2
2
  // 皮肤与行为全在浏览器半(lib/client.js),这里不做别的。
3
+ import { createHash } from "node:crypto";
3
4
  import { readFile, stat } from "node:fs/promises";
4
5
 
5
6
  export const name = "dsh-md-plus";
@@ -11,6 +12,22 @@ export const RUNTIMES = {
11
12
  "/dsh-md-plus/mermaid-runtime.js": "mermaid-runtime.js",
12
13
  };
13
14
 
15
+ const etagCache = new Map();
16
+
17
+ /** 读运行时并生成基于内容的 ETag;ctime 变化时重新读取。 */
18
+ async function readRuntime(source) {
19
+ const info = await stat(source);
20
+ const cached = etagCache.get(source.href);
21
+ if (cached !== undefined && cached.size === info.size && cached.ctimeMs === info.ctimeMs) {
22
+ return cached;
23
+ }
24
+ const body = await readFile(source);
25
+ const etag = `W/"${createHash("sha1").update(body).digest("base64url")}"`;
26
+ const entry = { size: info.size, ctimeMs: info.ctimeMs, etag, body };
27
+ etagCache.set(source.href, entry);
28
+ return entry;
29
+ }
30
+
14
31
  export function apply(ctx) {
15
32
  for (const [path, file] of Object.entries(RUNTIMES)) {
16
33
  const source = new URL(`./${file}`, import.meta.url);
@@ -27,21 +44,19 @@ export function apply(ctx) {
27
44
  }
28
45
  try {
29
46
  // 文件名不带内容指纹,原来的一年 immutable 会让升级后的运行时发不出去:
30
- // 改成每次回源校验,没变就 304。
31
- const info = await stat(source);
32
- const etag = `W/"${info.size.toString(16)}-${Math.floor(info.mtimeMs).toString(16)}"`;
33
- if (req.headers?.["if-none-match"] === etag) {
34
- res.writeHead(304, { etag, "cache-control": "no-cache" });
47
+ // 改成每次回源校验,内容没变就 304。
48
+ const entry = await readRuntime(source);
49
+ if (req.headers?.["if-none-match"] === entry.etag) {
50
+ res.writeHead(304, { etag: entry.etag, "cache-control": "no-cache" });
35
51
  res.end();
36
52
  return;
37
53
  }
38
- const body = await readFile(source);
39
54
  res.writeHead(200, {
40
55
  "content-type": "text/javascript; charset=utf-8",
41
56
  "cache-control": "no-cache",
42
- etag,
57
+ etag: entry.etag,
43
58
  });
44
- res.end(req.method === "HEAD" ? undefined : body);
59
+ res.end(req.method === "HEAD" ? undefined : entry.body);
45
60
  } catch {
46
61
  res.writeHead(404);
47
62
  res.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-md-plus",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Markdown display enhancements for DeepSeek Harness Web: code block restyle, syntax highlighting for languages DSH does not ship, and built-in mermaid diagrams.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",