@capyclaw/scrapling 1.0.0 → 1.0.2

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/lib/host.mjs CHANGED
@@ -1,83 +1,83 @@
1
- /**
2
- * dsh-scrapling 宿主半(Cordis 入口)
3
- *
4
- * 能力边界(诚实声明):
5
- * - fetch() 已实现:用 Node 内置 fetch 发起 HTTP 请求,返回状态/头/正文。
6
- * - fetchWithBrowser() 未实现:浏览器渲染 / Cloudflare 绕过需要外部 Python scrapling,
7
- * 当前直接抛 NOT_IMPLEMENTED,不会伪造结果。
8
- * - parse() 未实现:CSS/XPath 选择器需要 DOM 解析器,同上。
9
- *
10
- * 配置(resolveConfig:ctx config → 环境变量 → 默认值):
11
- * enabled 是否注册服务 DSH_SCRAPLING_ENABLED 默认 true
12
- * timeoutMs 单次请求超时(ms) DSH_SCRAPLING_TIMEOUT_MS 默认 20000
13
- * userAgent 默认 UA DSH_SCRAPLING_USER_AGENT 默认 undici
14
- */
15
- import { resolveConfig } from 'dsh-plugin-kit/config'
16
-
17
- export const name = '@capyclaw/scrapling'
18
-
19
- function notImplemented(feature) {
20
- const error = new Error(
21
- `[dsh-scrapling] ${feature} 未实现:浏览器渲染与选择器解析依赖外部 Python scrapling 运行时。` +
22
- '请使用 fetch() 做普通 HTTP 抓取,或另行部署 scrapling 服务后通过 HTTP 调用。',
23
- )
24
- error.code = 'NOT_IMPLEMENTED'
25
- return error
26
- }
27
-
28
- export async function apply(ctx, config = {}) {
29
- const { value: cfg, issues } = resolveConfig(config, {
30
- envPrefix: 'DSH_SCRAPLING',
31
- defaults: { enabled: true, timeoutMs: 20000, userAgent: 'undici' },
32
- fields: {
33
- enabled: { env: 'ENABLED', type: 'boolean' },
34
- timeoutMs: { env: 'TIMEOUT_MS', type: 'number' },
35
- userAgent: { env: 'USER_AGENT' },
36
- },
37
- })
38
- if (issues.length) {
39
- for (const issue of issues) ctx.logger?.warn?.(`[dsh-scrapling] 配置: ${issue}`)
40
- }
41
-
42
- if (!cfg.enabled) {
43
- ctx.logger?.info?.('[dsh-scrapling] 已按配置禁用,不注册 scrapling 服务')
44
- return
45
- }
46
-
47
- ctx.provide('scrapling', {
48
- /** 普通 HTTP 抓取(真实实现)。 */
49
- async fetch(url, options = {}) {
50
- const response = await fetch(url, {
51
- method: options.method || 'GET',
52
- headers: {
53
- 'user-agent': options.userAgent || cfg.userAgent,
54
- ...(options.headers || {}),
55
- },
56
- body: options.body,
57
- signal: AbortSignal.timeout(Number(options.timeoutMs) || cfg.timeoutMs),
58
- })
59
- const html = await response.text()
60
- return {
61
- url,
62
- status: response.status,
63
- ok: response.ok,
64
- headers: Object.fromEntries(response.headers.entries()),
65
- html,
66
- }
67
- },
68
-
69
- /** 浏览器渲染:未实现。 */
70
- async fetchWithBrowser() {
71
- throw notImplemented('fetchWithBrowser')
72
- },
73
-
74
- /** 选择器解析:未实现。 */
75
- parse() {
76
- throw notImplemented('parse')
77
- },
78
- })
79
-
80
- ctx.logger?.info?.(
81
- `[dsh-scrapling] 已注册 scrapling 服务(仅 HTTP fetch;浏览器/解析未实现,timeout=${cfg.timeoutMs}ms)`,
82
- )
83
- }
1
+ /**
2
+ * dsh-scrapling 宿主半(Cordis 入口)
3
+ *
4
+ * 能力边界(诚实声明):
5
+ * - fetch() 已实现:用 Node 内置 fetch 发起 HTTP 请求,返回状态/头/正文。
6
+ * - fetchWithBrowser() 未实现:浏览器渲染 / Cloudflare 绕过需要外部 Python scrapling,
7
+ * 当前直接抛 NOT_IMPLEMENTED,不会伪造结果。
8
+ * - parse() 未实现:CSS/XPath 选择器需要 DOM 解析器,同上。
9
+ *
10
+ * 配置(resolveConfig:ctx config → 环境变量 → 默认值):
11
+ * enabled 是否注册服务 DSH_SCRAPLING_ENABLED 默认 true
12
+ * timeoutMs 单次请求超时(ms) DSH_SCRAPLING_TIMEOUT_MS 默认 20000
13
+ * userAgent 默认 UA DSH_SCRAPLING_USER_AGENT 默认 undici
14
+ */
15
+ import { resolveConfig } from '@capyclaw/plugin-kit/config'
16
+
17
+ export const name = '@capyclaw/scrapling'
18
+
19
+ function notImplemented(feature) {
20
+ const error = new Error(
21
+ `[dsh-scrapling] ${feature} 未实现:浏览器渲染与选择器解析依赖外部 Python scrapling 运行时。` +
22
+ '请使用 fetch() 做普通 HTTP 抓取,或另行部署 scrapling 服务后通过 HTTP 调用。',
23
+ )
24
+ error.code = 'NOT_IMPLEMENTED'
25
+ return error
26
+ }
27
+
28
+ export async function apply(ctx, config = {}) {
29
+ const { value: cfg, issues } = resolveConfig(config, {
30
+ envPrefix: 'DSH_SCRAPLING',
31
+ defaults: { enabled: true, timeoutMs: 20000, userAgent: 'undici' },
32
+ fields: {
33
+ enabled: { env: 'ENABLED', type: 'boolean' },
34
+ timeoutMs: { env: 'TIMEOUT_MS', type: 'number' },
35
+ userAgent: { env: 'USER_AGENT' },
36
+ },
37
+ })
38
+ if (issues.length) {
39
+ for (const issue of issues) ctx.logger?.warn?.(`[dsh-scrapling] 配置: ${issue}`)
40
+ }
41
+
42
+ if (!cfg.enabled) {
43
+ ctx.logger?.info?.('[dsh-scrapling] 已按配置禁用,不注册 scrapling 服务')
44
+ return
45
+ }
46
+
47
+ ctx.provide('scrapling', {
48
+ /** 普通 HTTP 抓取(真实实现)。 */
49
+ async fetch(url, options = {}) {
50
+ const response = await fetch(url, {
51
+ method: options.method || 'GET',
52
+ headers: {
53
+ 'user-agent': options.userAgent || cfg.userAgent,
54
+ ...(options.headers || {}),
55
+ },
56
+ body: options.body,
57
+ signal: AbortSignal.timeout(Number(options.timeoutMs) || cfg.timeoutMs),
58
+ })
59
+ const html = await response.text()
60
+ return {
61
+ url,
62
+ status: response.status,
63
+ ok: response.ok,
64
+ headers: Object.fromEntries(response.headers.entries()),
65
+ html,
66
+ }
67
+ },
68
+
69
+ /** 浏览器渲染:未实现。 */
70
+ async fetchWithBrowser() {
71
+ throw notImplemented('fetchWithBrowser')
72
+ },
73
+
74
+ /** 选择器解析:未实现。 */
75
+ parse() {
76
+ throw notImplemented('parse')
77
+ },
78
+ })
79
+
80
+ ctx.logger?.info?.(
81
+ `[dsh-scrapling] 已注册 scrapling 服务(仅 HTTP fetch;浏览器/解析未实现,timeout=${cfg.timeoutMs}ms)`,
82
+ )
83
+ }
package/package.json CHANGED
@@ -1,63 +1,60 @@
1
- {
2
- "name": "@capyclaw/scrapling",
3
- "version": "1.0.0",
4
- "description": "Scrapling 网页抓取集成:支持 HTTP 请求、隐身浏览器、JavaScript 渲染、Cloudflare 绕过、并发爬虫和数据导出",
5
- "type": "module",
6
- "main": "./lib/host.mjs",
7
- "exports": {
8
- ".": "./lib/host.mjs",
9
- "./api": "./lib/index.mjs",
10
- "./client": "./client/client.js",
11
- "./package.json": "./package.json"
12
- },
13
- "dependencies": {
14
- "dsh-plugin-kit": "^0.2.0"
15
- },
16
- "dsh": {
17
- "bundle": {
18
- "patch": "./cordis.patch.yml"
19
- },
20
- "client": {
21
- "platform": "web",
22
- "immediately": false
23
- },
24
- "category": "tool",
25
- "platform": "both"
26
- },
27
- "meta": {
28
- "title": "Scrapling 网页抓取",
29
- "description": "强大的网页抓取工具:支持快速 HTTP 请求、隐身浏览器模式、JavaScript 渲染、Cloudflare 自动绕过、CSS/XPath 选择器、并发爬虫、代理轮换和多格式数据导出(JSON/CSV/XML)。"
30
- },
31
- "files": [
32
- "lib/",
33
- "client/",
34
- "test/",
35
- "cordis.patch.yml",
36
- "README.md"
37
- ],
38
- "scripts": {
39
- "test": "node test/plugin.test.mjs"
40
- },
41
- "engines": {
42
- "node": ">=18",
43
- "dsh": "^0.2.0-rc.2"
44
- },
45
- "license": "MIT",
46
- "author": {
47
- "name": "CapyClaw",
48
- "url": "https://github.com/capyclaw",
49
- "email": "support@capyclaw.dev"
50
- },
51
- "repository": {
52
- "type": "git",
53
- "url": "https://github.com/capyclaw/dsh-plugins",
54
- "directory": "extensions/official/capyclaw-scrapling"
55
- },
56
- "bugs": {
57
- "url": "https://github.com/capyclaw/dsh-plugins/issues"
58
- },
59
- "homepage": "https://github.com/capyclaw/dsh-plugins",
60
- "publishConfig": {
61
- "access": "public"
62
- }
63
- }
1
+ {
2
+ "name": "@capyclaw/scrapling",
3
+ "version": "1.0.2",
4
+ "description": "Scrapling 网页抓取集成:支持 HTTP 请求、隐身浏览器、JavaScript 渲染、Cloudflare 绕过、并发爬虫和数据导出",
5
+ "type": "module",
6
+ "main": "./lib/host.mjs",
7
+ "exports": {
8
+ ".": "./lib/host.mjs",
9
+ "./api": "./lib/index.mjs",
10
+ "./client": "./client/client.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "dependencies": {
14
+ "@capyclaw/plugin-kit": "^0.2.0"
15
+ },
16
+ "dsh": {
17
+ "bundle": {
18
+ "patch": "./cordis.patch.yml"
19
+ },
20
+ "client": {
21
+ "platform": "web",
22
+ "immediately": false
23
+ },
24
+ "category": "tool",
25
+ "platform": "both"
26
+ },
27
+ "meta": {
28
+ "title": "Scrapling 网页抓取",
29
+ "description": "强大的网页抓取工具:支持快速 HTTP 请求、隐身浏览器模式、JavaScript 渲染、Cloudflare 自动绕过、CSS/XPath 选择器、并发爬虫、代理轮换和多格式数据导出(JSON/CSV/XML)。"
30
+ },
31
+ "files": [
32
+ "lib/",
33
+ "client/",
34
+ "cordis.patch.yml",
35
+ "README.md"
36
+ ],
37
+ "scripts": {},
38
+ "engines": {
39
+ "node": ">=18",
40
+ "dsh": "^0.2.0-rc.2"
41
+ },
42
+ "license": "MIT",
43
+ "author": {
44
+ "name": "CapyClaw",
45
+ "url": "https://github.com/capyclaw",
46
+ "email": "support@capyclaw.dev"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/Maple496/capyclaw.git",
51
+ "directory": "extensions/official/capyclaw-scrapling"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/Maple496/capyclaw/issues"
55
+ },
56
+ "homepage": "https://github.com/capyclaw/dsh-plugins",
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }
@@ -1,32 +0,0 @@
1
- /**
2
- * dsh-scrapling 插件测试
3
- */
4
- import { describe, it } from 'node:test'
5
- import assert from 'node:assert'
6
-
7
- describe('dsh-scrapling', () => {
8
- it('应该导出 name 和 apply', async () => {
9
- const plugin = await import('../lib/host.mjs')
10
- assert.strictEqual(plugin.name, '@capyclaw/scrapling')
11
- assert.strictEqual(typeof plugin.apply, 'function')
12
- })
13
-
14
- it('应该提供 scrapling 服务', async () => {
15
- const plugin = await import('../lib/host.mjs')
16
-
17
- // 模拟 Cordis 上下文
18
- const services = {}
19
- const ctx = {
20
- provide(name, service) {
21
- services[name] = service
22
- }
23
- }
24
-
25
- await plugin.apply(ctx, {})
26
-
27
- assert.ok(services.scrapling)
28
- assert.strictEqual(typeof services.scrapling.fetch, 'function')
29
- assert.strictEqual(typeof services.scrapling.fetchWithBrowser, 'function')
30
- assert.strictEqual(typeof services.scrapling.parse, 'function')
31
- })
32
- })