@aomex-contrib/browser 0.0.0-stage → 1.0.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ # @aomex-contrib/browser
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [`eb9e293`](https://github.com/aomex/aomex-contrib/commit/eb9e293afc5e553ed884cd278f0fabe09db1e297) Thanks [@geekact](https://github.com/geekact)! - - 包名由 `@aomex-contrib/playwright` 更名为 `@aomex-contrib/browser`
8
+ - 底层驱动改为 Patchright;peer 由 `playwright` 改为 `patchright`
9
+ - 移除 `playwright-extra`、stealth、recaptcha 依赖及 `stealth` / `recaptcha` 配置项
10
+ - 反检测默认:`channel` 固定 `'chrome'`、`viewport: null`、不注入 UA / 时区 / 语言;Docker 默认仍无头(可选有头 + Xvfb)
11
+
12
+ ## 0.3.2
13
+
14
+ ### Patch Changes
15
+
16
+ - [`763206a`](https://github.com/aomex/aomex-contrib/commit/763206aa2f085192e397a1dd3fa994fa1d5ba906) Thanks [@geekact](https://github.com/geekact)! - 超时竞速里的定时器可能拖住进程退出
17
+
18
+ ## 0.3.1
19
+
20
+ ### Patch Changes
21
+
22
+ - [`aec988b`](https://github.com/aomex/aomex-contrib/commit/aec988bbfce31bf8ea76707c6abdc0b9c111f50f) Thanks [@geekact](https://github.com/geekact)! - 修复未启用 recaptcha 时,`withPage` 回调里的 `page` 被收成 `never`,导致 `page.goto` 等普通方法无法使用
23
+
24
+ ## 0.3.0
25
+
26
+ ### Minor Changes
27
+
28
+ - [`e99f20f`](https://github.com/aomex/aomex-contrib/commit/e99f20fae0eed3544433a312993023258aebaed4) Thanks [@geekact](https://github.com/geekact)! - 包名由 `@aomex-contrib/playwright-service` 更名为 `@aomex-contrib/playwright`
29
+
30
+ ## 0.2.0
31
+
32
+ ### Minor Changes
33
+
34
+ - [`0d8018b`](https://github.com/aomex/aomex-contrib/commit/0d8018b156edece0ea04774955c7e0d381d2d235) Thanks [@geekact](https://github.com/geekact)! - 新增 `@aomex-contrib/playwright-service`:基于 Playwright 的 Browser Service
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2024 geekact
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,231 @@
1
- # Temporary Holding Version
1
+ # @aomex-contrib/browser
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 基于 [Patchright](https://github.com/Kaliiiiiiiiii-Vinyzu/patchright)(Playwright 驱动补丁版)的 aomex **Browser Service**:`onInit` 启动唯一浏览器进程,每次任务用独立 `BrowserContext` 注入代理。`channel` 固定为 Google Chrome(需镜像已装),并保留 `viewport: null`、不注入 UA 等反检测默认;Docker 下默认仍无头。
4
+
5
+ 适合:业务里多处要开浏览器、按账号/任务换代理、retry 时复用同一 Browser。
6
+
7
+ 仅支持 **Google Chrome**(`channel` 焊死为 `chrome`);Firefox / WebKit 不在 Patchright 补丁范围内。
8
+
9
+ ---
10
+
11
+ ## 安装浏览器
12
+
13
+ `pnpm add` 只装 npm 包,不会下载浏览器。默认走 **Google Chrome**,本地可在业务项目加:
14
+
15
+ ```json
16
+ {
17
+ "scripts": {
18
+ "postinstall": "patchright install chrome"
19
+ }
20
+ }
21
+ ```
22
+
23
+ Docker 构建时再带系统依赖内联安装(需 root):
24
+
25
+ ```dockerfile
26
+ RUN pnpm exec patchright install --with-deps chrome
27
+ ```
28
+
29
+ 本包运行时要求 **Node >= 26.13.0**。`patchright` 为 peer,需业务侧自行安装(不要再装 `playwright` 当驱动)。
30
+
31
+ ### 本地
32
+
33
+ ```bash
34
+ pnpm add @aomex-contrib/browser patchright
35
+ ```
36
+
37
+ ### Docker
38
+
39
+ ```dockerfile
40
+ FROM node:26
41
+
42
+ WORKDIR /app
43
+
44
+ RUN pnpm exec patchright install --with-deps chrome
45
+
46
+ CMD ["node", "dist/index.js"]
47
+ ```
48
+
49
+ ```bash
50
+ docker run --shm-size=1g your-image
51
+ ```
52
+
53
+ ```yaml
54
+ services:
55
+ app:
56
+ shm_size: 1g
57
+ ```
58
+
59
+ 要点:
60
+
61
+ - 用官方 `node:26`(Debian);避开 Alpine / musl
62
+ - 默认 `headless: true`;`channel` 固定 `chrome`
63
+ - `/dev/shm` 建议 ≥ 1g,否则易崩
64
+
65
+ ### Docker:有界面(可选,反检测更强)
66
+
67
+ ```dockerfile
68
+ FROM node:26
69
+
70
+ RUN apt-get update \
71
+ && apt-get install -y --no-install-recommends xvfb \
72
+ && rm -rf /var/lib/apt/lists/*
73
+
74
+ WORKDIR /app
75
+
76
+ RUN pnpm exec patchright install --with-deps chrome
77
+
78
+ CMD ["node", "dist/index.js"]
79
+ ```
80
+
81
+ ```ts
82
+ createPlaywrightService({
83
+ logger,
84
+ headless: false, // Linux 无 DISPLAY 时自动起 Xvfb
85
+ // timezoneId / locale:与代理出口地区对齐
86
+ });
87
+ ```
88
+
89
+ ---
90
+
91
+ ## 注册
92
+
93
+ ```ts
94
+ // src/services/browser.service.ts
95
+ import { createPlaywrightService } from '@aomex-contrib/browser';
96
+ import { Logger } from '@aomex/logger';
97
+
98
+ export default createPlaywrightService({
99
+ logger: new Logger(),
100
+ // idleTimeout: '5 minutes',
101
+ // maxConcurrent: 3,
102
+ // timezoneId / locale:有代理时建议与出口地区一致;不传则用系统默认
103
+ });
104
+
105
+ // src/services/index.ts
106
+ export const services = await combineServices({
107
+ browser: () => import('./browser.service.js'),
108
+ // ...
109
+ });
110
+ ```
111
+
112
+ 不要在 `services/index` 里直接 `import` 并内联 `createPlaywrightService(...)`:
113
+
114
+ ```ts
115
+ // ❌ 入口一加载就会拉起 Patchright,进程一开始就多占内存
116
+ import { createPlaywrightService } from '@aomex-contrib/browser';
117
+ browser: async () => createPlaywrightService({ logger }),
118
+ ```
119
+
120
+ 应像上文一样 `() => import('./browser.service.js')`,等第一次用到 browser 再加载。
121
+
122
+ ### 继承扩展方法
123
+
124
+ `createPlaywrightService` 返回的是 class,可 `extends` 后加业务方法(内部继续调 `withPage`):
125
+
126
+ ```ts
127
+ // src/services/browser.service.ts
128
+ import { createPlaywrightService, type WithPageOptions } from '@aomex-contrib/browser';
129
+ import { Logger } from '@aomex/logger';
130
+ import type { BrowserContext, Page } from 'patchright';
131
+
132
+ const PlaywrightService = createPlaywrightService({
133
+ logger: new Logger(),
134
+ });
135
+
136
+ export default class BrowserService extends PlaywrightService {
137
+ /** 失败时截图后原样抛出;各业务 Service 复用,不要把站点流程写进来 */
138
+ async withPageOrScreenshot<T>(
139
+ opts: WithPageOptions,
140
+ onHandle: (page: Page, context: BrowserContext) => Promise<T>,
141
+ ): Promise<T> {
142
+ return this.withPage(opts, async (page, context) => {
143
+ try {
144
+ return await onHandle(page, context);
145
+ } catch (error) {
146
+ await page.screenshot({ path: `failed-${Date.now()}.png`, fullPage: true });
147
+ throw error;
148
+ }
149
+ });
150
+ }
151
+
152
+ /** 打开 URL 并返回页面 HTML */
153
+ async getHtml(url: string, opts: WithPageOptions = {}) {
154
+ return this.withPage(opts, async (page) => {
155
+ await page.goto(url);
156
+ return page.content();
157
+ });
158
+ }
159
+ }
160
+ ```
161
+
162
+ 公开 API:`withPage`、`getStats`。继承类**只放通用、可跨业务复用的方法**;具体站点点击流、账号流程仍放在各自业务 Service。
163
+
164
+ ---
165
+
166
+ ## 用法
167
+
168
+ ```ts
169
+ const proxy = {
170
+ host: '1.2.3.4',
171
+ port: 8080,
172
+ username: 'u',
173
+ password: 'p',
174
+ };
175
+
176
+ const cookies = await services.browser.withPage(
177
+ {
178
+ proxy,
179
+ cookies: savedCookies,
180
+ // storageState / geolocation / permissions ...
181
+ // 尽量不要乱加 extraHTTPHeaders / userAgent
182
+ },
183
+ async (page, context) => {
184
+ await page.goto('https://example.com/login');
185
+ return context.cookies();
186
+ },
187
+ );
188
+ ```
189
+
190
+ `WithPageOptions`:`proxy`(本包简化结构)、`cookies`(创建后再 `addCookies`),以及 Playwright `BrowserContextOptions` 其余字段(会覆盖 Service 级默认)。
191
+
192
+ ### 行为说明
193
+
194
+ | API | 作用 |
195
+ | --------------- | ------------------------------------------------------------- |
196
+ | `onInit` | 首次用到 Service 时 `chromium.launch` |
197
+ | `withPage` | `newContext({ proxy })` → `newPage` → 回调 → **关闭 Context** |
198
+ | `getStats` | `{ active, waiting, connected }` 并发与连接状态 |
199
+ | `idleTimeout` | 空闲回收 Browser(省内存);下次 `withPage` 自动再启 |
200
+ | `maxConcurrent` | 同时 `withPage` 上限;超出排队 |
201
+ | `onDestroy` | 关 Browser;超时强杀进程 |
202
+
203
+ 代理:`proxy.server` 或 `proxy.host` + `proxy.port`;账密写在 `proxy.username` / `proxy.password`。
204
+
205
+ 人机验证(reCAPTCHA / hCaptcha)请在业务 Service 里直接调打码平台 API。
206
+
207
+ ---
208
+
209
+ ## 配置项
210
+
211
+ `createPlaywrightService({ ... })`:
212
+
213
+ | 字段 | 说明 | 默认 |
214
+ | ---------------- | -------------------------------------------------------------------------- | -------------- |
215
+ | `headless` | 是否无头;`false` 时 Linux 无 DISPLAY 会自动起 Xvfb(需已安装) | `true` |
216
+ | `viewport` | 视口;`null` 跟随窗口/显示,避免固定分辨率指纹 | `null` |
217
+ | `timezoneId` | Context 时区;不传则用系统默认 | — |
218
+ | `locale` | Context 语言;不传则用系统默认 | — |
219
+ | `defaultTimeout` | Context 默认超时(`DurationInput`) | `'2 minutes'` |
220
+ | `closeTimeout` | 关闭 Browser/Context 等待上限 | `'10 seconds'` |
221
+ | `idleTimeout` | 空闲回收 Browser;再有任务会自动 launch | — |
222
+ | `maxConcurrent` | 同时 `withPage` 上限;超出排队;不传则不限制 | — |
223
+ | `launch` | `chromium.launch` 选项(不含 `channel`,已焊死 `chrome`);`args` 追加其后 | — |
224
+ | `logger` | **必填**,`Logger` 实例 | — |
225
+ | `userAgent` | 自定义 UA;默认不设置(推荐) | — |
226
+
227
+ 站点专用点击流放在**业务 Service**。
228
+
229
+ ---
230
+
231
+ 通用能力见:https://aomex.js.org