dsh-share-html 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +138 -2
- package/cordis.patch.yml +5 -0
- package/lib/client.js +614 -0
- package/lib/index.js +90 -0
- package/lib/kernel.js +2632 -0
- package/locale/en.json +33 -0
- package/locale/zh.json +33 -0
- package/package.json +70 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Host 半:把阶段一的纯函数内核接到 DSH 的 sessionQuery 上(M7 + M8)。
|
|
2
|
+
//
|
|
3
|
+
// 这是发布产物:只做"接线",业务逻辑全在 src/host 的纯函数里。
|
|
4
|
+
// 写成 .js 是因为宿主 Node 不会对 node_modules 里的 .ts 做类型剥离。
|
|
5
|
+
//
|
|
6
|
+
// 传输层选型(逐条实测,不是猜的):
|
|
7
|
+
// - `ctx.provide` 挂的服务是 Host 进程内作用域,浏览器半取不到;
|
|
8
|
+
// - `harness.handle` / `host.call` 只属于运行时动态插件(cordis runner),
|
|
9
|
+
// 包形式插件没有这套 seat;
|
|
10
|
+
// - `connection.rpc.handle(channel, fn)` 会往 webServer 注册独立路由,
|
|
11
|
+
// 而 webServer 是**调用方 fiber** 的依赖 —— 插件自己声明了也不够用;
|
|
12
|
+
// - `connection.rpc.intercept('/api', matches, fn)` 只在内存表里挂一个
|
|
13
|
+
// endpoint 拦截器,不碰 webServer,**这才是包形式插件能用的那条路**。
|
|
14
|
+
// 因此:Host 在共享 `/api` 通道上拦截 `share-html/` 前缀,
|
|
15
|
+
// Client 用 `ctx.connection.rpc.call('/api', 'share-html/<method>', payload)`。
|
|
16
|
+
|
|
17
|
+
// 内核由 scripts/build.mjs 从 src/host 打包而来。
|
|
18
|
+
// 这里**不能**直接 import '../src/host/*.ts':那会迫使宿主依赖 Node 的类型剥离
|
|
19
|
+
// 实验特性(Node 24 默认开、20/22 直接失败),npm 包不能赌这个前提。
|
|
20
|
+
import { createHandlers } from './kernel.js'
|
|
21
|
+
|
|
22
|
+
export const name = 'dsh-share-html'
|
|
23
|
+
|
|
24
|
+
// sessionQuery 读会话;connection 是对浏览器的 RPC 通道。
|
|
25
|
+
export const inject = ['sessionQuery', 'connection']
|
|
26
|
+
|
|
27
|
+
/** Client 侧调用的通道(共享 API 通道)。 */
|
|
28
|
+
export const CHANNEL = '/api'
|
|
29
|
+
/** 本插件在 /api 上占用的端点前缀。 */
|
|
30
|
+
export const ENDPOINT_PREFIX = 'share-html/'
|
|
31
|
+
|
|
32
|
+
export function apply(ctx) {
|
|
33
|
+
const handlers = createHandlers(ctx)
|
|
34
|
+
|
|
35
|
+
const logger = typeof ctx.get === "function" ? ctx.get("logger") : undefined
|
|
36
|
+
const log = logger && typeof logger.info === "function" ? logger.info.bind(logger) : console.log
|
|
37
|
+
|
|
38
|
+
const jsonReply = (body) => new Response(JSON.stringify(body), {
|
|
39
|
+
status: 200,
|
|
40
|
+
headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" },
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const connection = ctx.connection
|
|
44
|
+
|
|
45
|
+
// 首选:exact Fetch route。每个方法一个 /api/share-html/<method>,
|
|
46
|
+
// 由 connection 自己完成 Host/Origin 校验与 cookie 认证,不碰共享 /api 通道,
|
|
47
|
+
// 因此不会与 dsh-api-gateway 抢 interceptor。
|
|
48
|
+
if (connection && connection.fetch && typeof connection.fetch.register === "function") {
|
|
49
|
+
const handlers_ = handlers
|
|
50
|
+
const methods = Object.keys(handlers_)
|
|
51
|
+
const disposers = methods.map((method) => connection.fetch.register({
|
|
52
|
+
path: "/api/" + method,
|
|
53
|
+
methods: ["POST"],
|
|
54
|
+
requestBody: "buffered",
|
|
55
|
+
fetch: async (request) => {
|
|
56
|
+
let payload = null
|
|
57
|
+
try {
|
|
58
|
+
const text = await request.text()
|
|
59
|
+
payload = text.length === 0 ? null : JSON.parse(text)
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return jsonReply({ ok: false, error: { code: "bad-request", message: "无法解析请求体: " + String(error && error.message ? error.message : error) } })
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const value = await handlers_[method](payload)
|
|
65
|
+
return jsonReply({ ok: true, value })
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return jsonReply({ ok: false, error: { code: "internal", message: String(error && error.message ? error.message : error) } })
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
}))
|
|
71
|
+
ctx.effect(() => () => { for (const dispose of disposers) { try { dispose() } catch {} } })
|
|
72
|
+
log("[dsh-share-html] host half ready on %s (methods: %s)", "/api/share-html/*", methods.join(", "))
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 回退:老 core 没有 connection.fetch 时才用共享通道拦截器(会占满 /api)。
|
|
77
|
+
const owned = (endpoint) => typeof endpoint === "string" && endpoint.startsWith(ENDPOINT_PREFIX)
|
|
78
|
+
const disposer = ctx.connection.rpc.intercept(CHANNEL, owned, async (endpoint, payload) => {
|
|
79
|
+
const handler = handlers[endpoint]
|
|
80
|
+
if (!handler) return { ok: false, error: { code: "unknown-method", message: "未知方法 " + endpoint } }
|
|
81
|
+
try {
|
|
82
|
+
const value = await handler(payload)
|
|
83
|
+
return { ok: true, value }
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return { ok: false, error: { code: "internal", message: String(error && error.message ? error.message : error) } }
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
ctx.effect(() => disposer)
|
|
89
|
+
log("[dsh-share-html] host half ready on %s %s (methods: %s)", CHANNEL, ENDPOINT_PREFIX, Object.keys(handlers).join(", "))
|
|
90
|
+
}
|