dsh-md-plus 0.1.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/lib/index.js ADDED
@@ -0,0 +1,44 @@
1
+ // dsh-md-plus 宿主半:只做一件事——把两个浏览器端运行时经同源路由发出去。
2
+ // 皮肤与行为全在浏览器半(lib/client.js),这里不做别的。
3
+ import { readFile } from "node:fs/promises";
4
+
5
+ export const name = "dsh-md-plus";
6
+ export const inject = ["webServer"];
7
+
8
+ /** 路由路径 → 构建产物文件名。客户端半里的同名常量必须与之一致。 */
9
+ export const RUNTIMES = {
10
+ "/dsh-md-plus/highlight-runtime.js": "highlight-runtime.js",
11
+ "/dsh-md-plus/mermaid-runtime.js": "mermaid-runtime.js",
12
+ };
13
+
14
+ export function apply(ctx) {
15
+ for (const [path, file] of Object.entries(RUNTIMES)) {
16
+ const source = new URL(`./${file}`, import.meta.url);
17
+ ctx.effect(
18
+ () =>
19
+ ctx.webServer.register({
20
+ kind: "exact",
21
+ path,
22
+ async handler(req, res) {
23
+ if (req.method !== "GET" && req.method !== "HEAD") {
24
+ res.writeHead(405);
25
+ res.end();
26
+ return;
27
+ }
28
+ try {
29
+ const body = await readFile(source);
30
+ res.writeHead(200, {
31
+ "content-type": "text/javascript; charset=utf-8",
32
+ "cache-control": "public, max-age=31536000, immutable",
33
+ });
34
+ res.end(req.method === "HEAD" ? undefined : body);
35
+ } catch {
36
+ res.writeHead(404);
37
+ res.end();
38
+ }
39
+ },
40
+ }),
41
+ `dsh-md-plus: ${path} route`,
42
+ );
43
+ }
44
+ }