dsh-aupdate 0.3.0 → 0.3.1

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.
Files changed (2) hide show
  1. package/index.js +31 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1586,12 +1586,9 @@ function settingsSnapshot(config, ctxRef) {
1586
1586
  }
1587
1587
  }
1588
1588
 
1589
- function registerRoutes(ctx, ctxRef) {
1590
- const webServer = serviceOf(ctx, 'webServer')
1591
- if (webServer === null || typeof webServer.register !== 'function') {
1592
- writeLog('webServer unavailable: settings page will report it')
1593
- return false
1594
- }
1589
+ function registerRoutes(scope, ctxRef) {
1590
+ const webServer = serviceOf(scope, 'webServer')
1591
+ if (webServer === null || typeof webServer.register !== 'function') return false
1595
1592
  const handler = async (req, res) => {
1596
1593
  try {
1597
1594
  const url = new URL(req.url ?? '/', 'http://localhost')
@@ -1660,10 +1657,10 @@ function registerRoutes(ctx, ctxRef) {
1660
1657
  }
1661
1658
  }
1662
1659
  try {
1663
- const effect = propOf(ctx, 'effect')
1660
+ const effect = propOf(scope, 'effect')
1664
1661
  const register = () => webServer.register({ kind: 'prefix', path: ROUTE_PREFIX, handler })
1665
1662
  if (typeof effect === 'function') {
1666
- effect.call(ctx, register, 'dsh-aupdate: settings routes')
1663
+ effect.call(scope, register, 'dsh-aupdate: settings routes')
1667
1664
  writeLog('settings route ready: ' + ROUTE_PREFIX)
1668
1665
  return true
1669
1666
  }
@@ -1701,19 +1698,44 @@ export function apply(ctx, config) {
1701
1698
  writeLog('tool registration threw: ' + oneLine(error?.message ?? error, 300))
1702
1699
  }
1703
1700
  }
1701
+ // 路由和工具是同一类问题:apply 执行时 webServer 可能还没就绪,
1702
+ // 一次性取服务会静默失败且永不重试。同样放进动态 inject 作用域里重放。
1703
+ let routeService = null
1704
+ const offerRoutes = (scope) => {
1705
+ try {
1706
+ const webServer = serviceOf(scope, 'webServer')
1707
+ if (webServer === null || typeof webServer.register !== 'function') {
1708
+ writeLog('webServer unavailable: settings page will report it')
1709
+ return
1710
+ }
1711
+ if (routeService === webServer) return
1712
+ const ok = registerRoutes(scope, ctxRef)
1713
+ if (ok) routeService = webServer
1714
+ } catch (error) {
1715
+ writeLog('route registration threw: ' + oneLine(error?.message ?? error, 300))
1716
+ }
1717
+ }
1704
1718
  const inject = propOf(ctx, 'inject')
1705
1719
  if (typeof inject === 'function') {
1720
+ // 兜底:inject 的作用域回调只在依赖齐备时重放,若是宿主不认 webServer
1721
+ // 这个依赖名,回调可能永远不来。所以先就地抢一次,拿不到再等重放。
1722
+ offerRoutes(ctx)
1706
1723
  try {
1707
- const fiber = inject.call(ctx, ['tools'], offerTool)
1724
+ const fiber = inject.call(ctx, ['tools', 'webServer'], (scope) => {
1725
+ offerTool(scope)
1726
+ offerRoutes(scope)
1727
+ })
1708
1728
  Promise.resolve(fiber).catch((error) => {
1709
1729
  writeLog('tool scope ended: ' + oneLine(error?.message ?? error, 300))
1710
1730
  })
1711
1731
  } catch (error) {
1712
1732
  writeLog('cannot offer the tool: ' + oneLine(error?.message ?? error, 300))
1713
1733
  offerTool(ctx)
1734
+ offerRoutes(ctx)
1714
1735
  }
1715
1736
  } else {
1716
1737
  offerTool(ctx)
1738
+ offerRoutes(ctx)
1717
1739
  }
1718
1740
 
1719
1741
  const runSafely = (mode) => {
@@ -1786,7 +1808,6 @@ export function apply(ctx, config) {
1786
1808
 
1787
1809
  // 设置页要能读到运行状态并改设置,所以路由最后注册、且与 enabled 无关:
1788
1810
  // 关掉定时检查之后,用户仍然要能看到这一页并把它重新打开。
1789
- registerRoutes(ctx, ctxRef)
1790
1811
  if (!settings.enabled) writeLog('disabled by config; scheduler parked, settings page still live')
1791
1812
 
1792
1813
  const stop = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-aupdate",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "定时检查 DSH 插件更新,串行安装后就地生效,不重启;支持多种调度节奏、失败抑制、按包回滚,并在动手前留快照、忙时自动让开。",
6
6
  "license": "MIT",