@syengup/fridaynext 0.1.0-beta.2 → 0.1.0-beta.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syengup/fridaynext",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.3",
4
4
  "description": "Friday 连接器:一次安装、一次配对,发现并添加这台 Mac 上的全部 Agent",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
package/src/daemon.mjs CHANGED
@@ -12,7 +12,8 @@ const settings = readJSON(paths.home + "/settings.json", {});
12
12
  const lan = settings.lanHost ?? lanAddress();
13
13
  const port = Number.isSafeInteger(settings.port) ? settings.port : DEFAULT_PORT;
14
14
 
15
- const connector = await startConnector({ home, host: settings.listenHost ?? "0.0.0.0", port, lanHost: lan ?? "127.0.0.1" });
15
+ const connector = await startConnector({ home, host: settings.listenHost ?? "0.0.0.0", port, lanHost: lan ?? "127.0.0.1", inventoryMaxAgeMs: 15_000 });
16
+ connector.warmInventory().catch(() => {});
16
17
  console.log(`[fridaynext-connector] 已启动 ${connector.url}`);
17
18
  // 开机或崩溃恢复后,自动拉起经连接器启用过、插件已启用但未运行的宿主后端。
18
19
  const stopSupervisor = connector.startSupervisor({ log: report => console.log(`[fridaynext-connector] 监督 ${JSON.stringify(report)}`) });
package/src/server.mjs CHANGED
@@ -35,7 +35,7 @@ function readBody(req, limit = 16_384) {
35
35
  * 启动连接器 HTTPS 服务(连接器 HTTP API v1)。只监听给定地址;TLS 使用 Kit 身份设施;
36
36
  * 电脑令牌只授权本 API,连接器不接触后端永久凭据。
37
37
  */
38
- export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost, manifest, env = {}, computerName = systemComputerName, now = () => Date.now() }) {
38
+ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost, manifest, env = {}, computerName = systemComputerName, now = () => Date.now(), inventoryMaxAgeMs = 0 }) {
39
39
  const paths = layout(home);
40
40
  ensurePrivateDir(home);
41
41
  ensurePrivateDir(paths.data);
@@ -48,8 +48,19 @@ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost
48
48
  const registry = providerRegistry({ manifest: manifest ?? readJSON(paths.providers) ?? { contract: "fridaynext.provider/1", providers: [] }, env });
49
49
  const supervised = supervisedStore(paths.supervised);
50
50
  const advertisedHost = lanHost ?? (host === "0.0.0.0" ? "127.0.0.1" : host);
51
+ // 汇总需逐个探测宿主,真机上可能超过 20 秒:保留最近一次结果,过期则后台刷新;宿主操作或改名后失效。
51
52
  let inventoryChain = Promise.resolve();
52
- const inventory = () => (inventoryChain = inventoryChain.then(() => collectInventory({ registry, stateFile: paths.inventory, computer: computer() })));
53
+ let cached = null, cachedAt = 0, refreshing = null;
54
+ const collect = () => (inventoryChain = inventoryChain.then(() => collectInventory({ registry, stateFile: paths.inventory, computer: computer() })));
55
+ const refresh = () => refreshing ??= collect().then(value => { cached = value; cachedAt = Date.now(); return value; }).finally(() => { refreshing = null; });
56
+ const inventory = async () => {
57
+ if (inventoryMaxAgeMs > 0 && cached) {
58
+ if (Date.now() - cachedAt > inventoryMaxAgeMs) refresh().catch(() => {});
59
+ return cached;
60
+ }
61
+ return refresh();
62
+ };
63
+ const invalidate = () => { cached = null; cachedAt = 0; };
53
64
 
54
65
  async function hostAction(res, hostId, command, body) {
55
66
  if (!registry.has(hostId)) return fail(res, 404, "HOST_UNKNOWN", "未知宿主");
@@ -62,6 +73,7 @@ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost
62
73
  }
63
74
  const result = await registry.run(hostId, command, command === "enable" ? { confirmHostRestart: confirmed, lanHost: advertisedHost } : { confirmHostRestart: confirmed });
64
75
  supervised.add(hostId);
76
+ invalidate();
65
77
  return send(res, 200, command === "enable"
66
78
  ? { state: result.state, hostRestarted: result.hostRestarted === true, changed: result.changed === true }
67
79
  : { state: result.state, hostRestarted: result.hostRestarted === true });
@@ -79,7 +91,7 @@ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost
79
91
  pairingTicketTtlSec: Number.isSafeInteger(result.pairingTicketTtlSec) ? result.pairingTicketTtlSec : 600, ...https("publicUrl"), ...https("controlPlane") });
80
92
  }
81
93
  const result = await registry.run(hostId, command, command === "start" ? { lanHost: advertisedHost } : undefined);
82
- if (command === "start") supervised.add(hostId);
94
+ if (command === "start") { supervised.add(hostId); invalidate(); }
83
95
  return send(res, 200, { state: result.state });
84
96
  } catch (error) {
85
97
  const known = error instanceof ProviderError ? error : new ProviderError("PROVIDER_INTERNAL", "Provider 执行失败", true);
@@ -116,6 +128,7 @@ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost
116
128
  if (tail === "computer" && method === "PATCH") {
117
129
  const body = await readBody(req);
118
130
  const next = renameComputer(paths.computer, body?.computerName);
131
+ if (next) invalidate();
119
132
  return next ? send(res, 200, { computerName: next.computerName }) : fail(res, 400, "INVALID_NAME", "名称无效");
120
133
  }
121
134
  if (tail === "devices/self" && method === "DELETE") { devices.revoke(device.deviceId); return send(res, 204); }
@@ -142,6 +155,8 @@ export async function startConnector({ home, host = "0.0.0.0", port = 0, lanHost
142
155
  /** 守护进程调用:开机与周期监督受监督宿主的后端。 */
143
156
  startSupervisor: options => startSupervisor({ registry, store: supervised, ...options }),
144
157
  inventory,
158
+ /** 守护进程启动时预热,首次扫码不必等待完整探测。 */
159
+ warmInventory: () => refresh(),
145
160
  close: () => new Promise(resolve => { server.closeAllConnections?.(); server.close(() => resolve()); }),
146
161
  };
147
162
  }