From 965999be678b1b9fb89a91292ec9e77ddcf0177f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Tue, 12 May 2026 21:37:22 +0800 Subject: [PATCH 1/4] perf(kernel): Include .mts/.cts and update SSE send format dprint.json: expand file includes to cover .mts and .cts files and set importDeclaration.spaceSurroundingNamedImports to false. src/kernel.ts: change SSE port.send usage to the object form (including event, data, id, retry) for update events and send the connection message as a plain data payload. This adapts to the updated SSE send API/shape and ensures update events include an id and retry hint. --- dprint.json | 3 ++- src/kernel.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/dprint.json b/dprint.json index 173ab39..2b96aa7 100644 --- a/dprint.json +++ b/dprint.json @@ -5,7 +5,7 @@ "useTabs": false, "newLineKind": "lf", "includes": [ - "**/*.{ts,tsx,js,jsx,mjs,cjs}", + "**/*.{ts,tsx,js,jsx,mjs,cjs,mts,cts}", "**/*.{json,jsonc}", "**/*.{css,scss}", "**/*.{md,markdown}", @@ -28,6 +28,7 @@ "nextControlFlowPosition": "sameLine", "spaceSurroundingProperties": false, "importDeclaration.forceMultiLine": "whenMultiple", + "importDeclaration.spaceSurroundingNamedImports": false, "importDeclaration.sortNamedImports": "maintain" }, "json": { diff --git a/src/kernel.ts b/src/kernel.ts index 5971eb8..624eeb4 100644 --- a/src/kernel.ts +++ b/src/kernel.ts @@ -607,11 +607,16 @@ class KernelPlugin { request.port.onopen = async (event) => { await logger.debug("sse server: port open", event); // send 是同步的。 - // eventType 会成为 SSE 的 `event:` 字段。 // send is synchronous. - // eventType becomes the SSE `event:` field. - request.port.send("message", "Connected to plugin SSE!"); - request.port.send("update", JSON.stringify({ts: Date.now()})); + request.port.send({ + event: "update", + data: JSON.stringify({ts: Date.now()}), + id: Date.now().toString(), + retry: 5000, + }); + request.port.send({ + data: "Connected to plugin SSE!", + }); }; request.port.onclose = async (event) => { await logger.debug("sse server: port close", event); From c8e8b0cad1a0571c6482ed1e3c4f70f8a6e711ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Wed, 13 May 2026 01:55:43 +0800 Subject: [PATCH 2/4] feat(kernel): Watch/unwatch plugin storage events Register a filesystem watcher for the plugin storage directory on load and remove it on unload. Adds storage.watcher.add("./") in the onload path and storage.watcher.remove("./") in the onunload path, and includes storage in the siyuan destructuring. This ensures the plugin listens for storage events and cleans up the watcher to avoid resource leaks. --- src/kernel.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/kernel.ts b/src/kernel.ts index 624eeb4..447d643 100644 --- a/src/kernel.ts +++ b/src/kernel.ts @@ -170,6 +170,11 @@ class KernelPlugin { ); // ── siyuan.storage 示例 + + // 监听插件存储目录的文件系统事件。 + // Watch filesystem events in the plugin storage directory. + await storage.watcher.add("./"); + // put:将 UTF-8 字符串写入相对于插件数据目录的路径。 // put: write a UTF-8 string to a path relative to the plugin data dir. await storage.put("demo.txt", JSON.stringify(new Date().toISOString())); @@ -391,7 +396,11 @@ class KernelPlugin { * After broadcasting, close any open client-side connections to avoid resource leaks in the kernel process. */ private async onunload(): Promise { - const {rpc, logger} = this.siyuan; + const {rpc, logger, storage} = this.siyuan; + + // 解除对插件存储目录的文件系统事件的监听。 + // Unwatch filesystem events in the plugin storage directory. + await storage.watcher.remove("./"); // 解绑在 onload 中注册的 RPC 方法。 // Unbind the RPC method registered in onload. From 35755b3d7027cfd25e652d192c371a01cf3d1ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Wed, 13 May 2026 11:09:02 +0800 Subject: [PATCH 3/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/kernel.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/kernel.ts b/src/kernel.ts index 447d643..0d0fead 100644 --- a/src/kernel.ts +++ b/src/kernel.ts @@ -617,10 +617,11 @@ class KernelPlugin { await logger.debug("sse server: port open", event); // send 是同步的。 // send is synchronous. + const now = Date.now(); request.port.send({ event: "update", - data: JSON.stringify({ts: Date.now()}), - id: Date.now().toString(), + data: JSON.stringify({ts: now}), + id: now.toString(), retry: 5000, }); request.port.send({ From 24457662ca9ddac9213e61ebe04579cd6ec59c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Wed, 13 May 2026 11:15:34 +0800 Subject: [PATCH 4/4] docs(kernel): Update SSE port.send documentation Clarify the Server-Sent Events handler docs: replace the previous signature port.send(eventType, data) with the single-argument port.send(event) and remove the explicit mapping notes for `event:` and `data:`. This is a documentation-only change to reduce confusion about the SSE send API; no functional code changes. --- src/kernel.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/kernel.ts b/src/kernel.ts index 0d0fead..7c8717e 100644 --- a/src/kernel.ts +++ b/src/kernel.ts @@ -600,9 +600,8 @@ class KernelPlugin { * Demonstrates the SSE server handler at `GET /plugin/private//*path` (Server-Sent Events). * * `request.port.onopen` fires once the SSE stream is ready. - * Call `port.send(eventType, data)` inside `onopen` or later to push events. - * `eventType` maps to the SSE `event:` field. - * `data` maps to `data:`. + * Call `port.send(event)` inside `onopen` or later to push events. + * * `send` is synchronous. * * The connection stays open until `port.close()` is called or the client disconnects.