@capyclaw/scrapling 1.1.3 → 1.1.4

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/cordis.patch.yml CHANGED
@@ -3,7 +3,8 @@
3
3
  #
4
4
  # 配置项(cordis config → 环境变量 DSH_SCRAPLING_* → 默认值):
5
5
  # enabled 是否启用 DSH_SCRAPLING_ENABLED true
6
- # port 工作台端口 DSH_SCRAPLING_PORT auto(broker 分配;固定值需未被占用)
6
+ # port 工作台端口 DSH_SCRAPLING_PORT 39640(固定端口;boot 注入要求
7
+ # URL 在注入时机已知,auto 会错过时机)
7
8
  # host 监听地址 DSH_SCRAPLING_HOST 127.0.0.1
8
9
  # timeoutMs 请求超时(ms) DSH_SCRAPLING_TIMEOUT_MS 20000
9
10
  # workbench 是否拉起工作台 DSH_SCRAPLING_WORKBENCH true
@@ -15,4 +16,4 @@
15
16
  name: "@capyclaw/scrapling"
16
17
  config:
17
18
  enabled: true
18
- port: auto
19
+ port: 39640
package/lib/host.mjs CHANGED
@@ -10,9 +10,10 @@
10
10
  * scrapling,直接抛 NOT_IMPLEMENTED,不会伪造结果。
11
11
  *
12
12
  * 工作台服务(server/server.mjs):
13
- * - 由 dsh-service-broker 作为独立进程管理;Cordis 进程不监听业务端口;
13
+ * - 由 service-broker 作为独立进程管理;Cordis 进程不监听业务端口;
14
14
  * - 依赖注入可选:workbench: false 可只注册进程内 fetch/parse API;
15
- * - 通过 webserver/index-inject 注入 broker 返回的 URL,浏览器半挂面板。
15
+ * - 通过 webserver/index-inject 注入 URL(固定端口,注入时机先于 broker.start);
16
+ * 浏览器半据此挂右侧边栏面板。
16
17
  *
17
18
  * 配置(resolveConfig:ctx config → 环境变量 → 默认值):
18
19
  * enabled 是否注册服务/界面 DSH_SCRAPLING_ENABLED 默认 true
@@ -35,6 +36,11 @@ export const inject = ['serviceBroker']
35
36
 
36
37
  const SERVICE_ID = 'scrapling-workbench'
37
38
 
39
+ /* 版本号单一真源:读包内 package.json(createRequire 全 Node 版本稳态)。 */
40
+ import { createRequire } from 'node:module'
41
+ const requireFromHere = createRequire(import.meta.url)
42
+ const { version: PKG_VERSION } = requireFromHere('../package.json')
43
+
38
44
  function notImplemented(feature) {
39
45
  const error = new Error(
40
46
  `[dsh-scrapling] ${feature} 未实现:浏览器渲染与 XPath 依赖外部 Python scrapling 运行时。` +
@@ -49,7 +55,7 @@ export async function apply(ctx, config = {}) {
49
55
  envPrefix: 'DSH_SCRAPLING',
50
56
  defaults: {
51
57
  enabled: true,
52
- port: 'auto',
58
+ port: DEFAULT_PORT,
53
59
  host: '127.0.0.1',
54
60
  timeoutMs: 20000,
55
61
  workbench: true,
@@ -66,9 +72,9 @@ export async function apply(ctx, config = {}) {
66
72
  for (const issue of issues) ctx.logger?.warn?.(`[dsh-scrapling] 配置: ${issue}`)
67
73
  const port = cfg.port === 'auto' ? 'auto' : Number(cfg.port)
68
74
  if (port !== 'auto' && (!Number.isInteger(port) || port < 1 || port > 65535)) {
69
- ctx.logger?.warn?.(`[dsh-scrapling] 非法 port=${cfg.port},回退为 broker 自动分配`)
75
+ ctx.logger?.warn?.(`[dsh-scrapling] 非法 port=${cfg.port},回退为默认 ${DEFAULT_PORT}`)
70
76
  }
71
- const servicePort = port === 'auto' || !Number.isInteger(port) || port < 1 || port > 65535 ? 'auto' : port
77
+ const servicePort = port === 'auto' || !Number.isInteger(port) || port < 1 || port > 65535 ? DEFAULT_PORT : port
72
78
 
73
79
  if (!cfg.enabled) {
74
80
  ctx.logger?.info?.('[dsh-scrapling] 已按配置禁用,不注册 scrapling 服务')
@@ -105,11 +111,17 @@ export async function apply(ctx, config = {}) {
105
111
  crawler,
106
112
  })
107
113
 
108
- /* 工作台由 broker 独立拉起;Cordis 进程只注册 API 与 sidebar URL。 */
114
+ /* 工作台由 broker 独立拉起;Cordis 进程只注册 API 与 sidebar URL。
115
+ * 时序契约:webserver/index-inject 是 webserver 启动时机的一次性事件,
116
+ * 注册必须发生在 broker.start 之前,且 URL 必须此刻可知——所以默认固定端口
117
+ *(port=auto 时 URL 要等 broker 分配,注入会错过时机,只做尽力补注册并告警)。 */
109
118
  let serviceBroker = null
110
119
  let stopServiceOnDispose = false
111
- let serviceUrl = ''
112
120
  if (cfg.workbench) {
121
+ const expectedUrl = `http://${cfg.host === '0.0.0.0' ? '127.0.0.1' : cfg.host}:${servicePort}`
122
+ ctx.on?.('webserver/index-inject', (table) => {
123
+ table.push({ kind: 'global', name: '__DSH_SCRAPLING__', value: { url: `${expectedUrl}/`, version: PKG_VERSION } })
124
+ })
113
125
  const broker = ctx.serviceBroker
114
126
  serviceBroker = broker || null
115
127
  if (!broker || typeof broker.start !== 'function') {
@@ -131,12 +143,12 @@ export async function apply(ctx, config = {}) {
131
143
  try {
132
144
  const started = await broker.start(spec)
133
145
  if (!started?.url) throw new Error('serviceBroker.start 未返回服务 url')
134
- serviceUrl = started.url.replace(/\/$/, '')
146
+ const serviceUrl = started.url.replace(/\/$/, '')
135
147
  stopServiceOnDispose = true
136
148
  ctx.logger?.info?.(`[dsh-scrapling] broker 工作台已就绪: ${serviceUrl}/`)
137
- ctx.on?.('webserver/index-inject', (table) => {
138
- table.push({ kind: 'global', name: '__DSH_SCRAPLING__', value: { url: `${serviceUrl}/`, version: '1.1.1' } })
139
- })
149
+ if (serviceUrl !== expectedUrl) {
150
+ ctx.logger?.warn?.(`[dsh-scrapling] 实际服务地址 ${serviceUrl} 与注入地址 ${expectedUrl} 不一致(port=auto?),面板可能指向旧地址;建议使用固定端口`)
151
+ }
140
152
  } catch (error) {
141
153
  ctx.logger?.warn?.(`[dsh-scrapling] broker 工作台启动失败(fetch/parse API 仍可用): ${error.message}`)
142
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/scrapling",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Scrapling 网页抓取集成:HTTP 抓取 + 纯 Node CSS 选择器提取 + 可视化爬虫工作台(浏览器打开即用)",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
package/server/server.mjs CHANGED
@@ -11,11 +11,16 @@
11
11
  import { readFile } from 'node:fs/promises'
12
12
  import path from 'node:path'
13
13
  import { fileURLToPath } from 'node:url'
14
+ import { createRequire } from 'node:module'
14
15
  import { isMain, parseArgs } from '@capyclaw/plugin-kit'
15
16
  import { installServiceSignalHandlers, startStandaloneService } from '@capyclaw/plugin-kit/service/server'
16
17
 
17
18
  import { createCrawler } from '../lib/crawler.mjs'
18
19
 
20
+ /* 版本号单一真源:读包内 package.json。 */
21
+ const requireFromHere = createRequire(import.meta.url)
22
+ const { version: PKG_VERSION } = requireFromHere('../package.json')
23
+
19
24
  export const DEFAULT_PORT = 39640
20
25
  export const CONTRACT_VERSION = '1'
21
26
 
@@ -105,7 +110,7 @@ export async function startWorkbenchServer(options = {}) {
105
110
  userAgent: options.userAgent,
106
111
  maxHtmlBytes: options.maxHtmlBytes,
107
112
  })
108
- const version = options.version || '1.1.1'
113
+ const version = options.version || PKG_VERSION
109
114
  const uiDir = path.join(packageDir, 'ui')
110
115
  const demoDir = path.join(packageDir, 'demo')
111
116
  const files = staticFiles(uiDir, demoDir)