@d-zero/puppeteer-dealer 0.6.4 → 0.6.6

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/README.md +216 -22
  2. package/package.json +7 -7
package/README.md CHANGED
@@ -1,10 +1,18 @@
1
1
  # `@d-zero/puppeteer-dealer`
2
2
 
3
+ Puppeteerを使ってマルチプロセスで複数のURLを処理するためのパッケージです。
4
+
5
+ ## API
6
+
7
+ ### `deal`
8
+
9
+ URLリストを処理するメイン関数です。
10
+
3
11
  ```ts
4
- import { deal } from '@d-zero/puppeteer-dealer';
12
+ import { deal, createProcess } from '@d-zero/puppeteer-dealer';
5
13
 
6
14
  await deal(
7
- // Input list data
15
+ // URLリスト
8
16
  [
9
17
  {
10
18
  id: '1',
@@ -16,39 +24,225 @@ await deal(
16
24
  },
17
25
  ],
18
26
 
19
- // Header log
27
+ // ヘッダーログ
20
28
  (progress, done, total) => {
21
29
  return `Header ${Math.ceil(progress * 100)}% ${done}/${total}`;
22
30
  },
23
31
 
24
- // Handlers
32
+ // プロセス作成関数
33
+ () => {
34
+ return createProcess(
35
+ './child-process.js', // 子プロセスのパス
36
+ {
37
+ // 子プロセスに渡すパラメータ
38
+ // 任意のデータを渡すことができます
39
+ },
40
+ {
41
+ // オプション
42
+ locale: 'ja-JP',
43
+ headless: true,
44
+ },
45
+ );
46
+ },
47
+
48
+ // オプション
25
49
  {
26
- async beforeOpenPage(id, url, logger) {
27
- if (condition) {
28
- // continue (like Event preventDefault)
29
- return false;
30
- }
50
+ limit: 10,
51
+ debug: false,
52
+ verbose: false,
53
+ },
54
+ );
55
+ ```
31
56
 
32
- return true;
33
- },
34
- async deal(page, id, url, logger) {
35
- // Do something
57
+ **型定義:**
58
+
59
+ ```ts
60
+ function deal<T, R = void>(
61
+ list: readonly URLInfo[],
62
+ header: DealHeader,
63
+ createProcess: () => (needAuth: boolean) => ChildProcessManager<T, R>,
64
+ options?: Omit<DealOptions, 'header'> & {
65
+ each?: (result: R) => void | Promise<void>;
66
+ },
67
+ ): Promise<void>;
68
+
69
+ type URLInfo = {
70
+ readonly id: string | null;
71
+ readonly url: string | URL;
72
+ };
73
+
74
+ type DealHeader = (progress: number, done: number, total: number) => string;
75
+ ```
76
+
77
+ ### `createProcess`
78
+
79
+ 子プロセスを作成する関数です。`deal`関数の第3引数で使用します。
80
+
81
+ ```ts
82
+ import { createProcess } from '@d-zero/puppeteer-dealer';
83
+
84
+ const processFactory = createProcess(
85
+ './child-process.js', // 子プロセスのパス
86
+ {
87
+ // 子プロセスに渡すパラメータ
88
+ param1: 'value1',
89
+ param2: 'value2',
90
+ },
91
+ {
92
+ // Puppeteerオプション
93
+ locale: 'ja-JP',
94
+ headless: true,
95
+ // その他のLaunchOptions
96
+ },
97
+ );
98
+ ```
99
+
100
+ **型定義:**
101
+
102
+ ```ts
103
+ function createProcess<P, R = void>(
104
+ subModulePath: string,
105
+ params: P,
106
+ options?: PuppeteerDealerOptions & LaunchOptions,
107
+ ): (needAuth: boolean) => ChildProcessManager<P, R>;
108
+
109
+ type PuppeteerDealerOptions = {
110
+ readonly locale?: string;
111
+ } & DealOptions;
112
+ ```
113
+
114
+ ### `createChildProcess`
115
+
116
+ 子プロセス側で使用する関数です。Puppeteerページの処理を定義します。
117
+
118
+ ```ts
119
+ // child-process.ts
120
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
121
+
122
+ type ChildProcessParams = {
123
+ param1: string;
124
+ param2: string;
125
+ };
126
+
127
+ createChildProcess<ChildProcessParams>((params) => {
128
+ const { param1, param2, needAuth } = params;
129
+
130
+ return {
131
+ async eachPage({ page, id, url, index }, logger) {
132
+ // Puppeteerページの処理
133
+ logger('ページにアクセスしています...');
36
134
  await page.goto(url);
135
+
136
+ logger('処理を実行しています...');
37
137
  await page.evaluate(() => {
38
- // Do something
138
+ // ページ内での処理
39
139
  });
140
+
141
+ // 戻り値を返すことができます
142
+ return { success: true };
40
143
  },
41
- },
144
+ };
145
+ });
146
+ ```
147
+
148
+ **型定義:**
149
+
150
+ ```ts
151
+ function createChildProcess<P, R = void>(
152
+ handler: ChildProcessHandler<P & CommonParams, R>,
153
+ ): void;
154
+
155
+ type ChildProcessHandler<P extends CommonParams, R> = (
156
+ params: P,
157
+ ) => Promise<ChildProcessMethods<R>> | ChildProcessMethods<R>;
158
+
159
+ type ChildProcessMethods<R> = {
160
+ eachPage: (params: EachPageParams, logger: Logger) => Promise<R>;
161
+ };
162
+
163
+ type EachPageParams = {
164
+ readonly page: Page;
165
+ readonly id: string;
166
+ readonly url: string;
167
+ readonly index: number;
168
+ };
169
+
170
+ type CommonParams = {
171
+ readonly needAuth: boolean;
172
+ };
42
173
 
43
- // Options
174
+ type Logger = (log: string) => void;
175
+ ```
176
+
177
+ ## 使用例
178
+
179
+ 完全な使用例:
180
+
181
+ **main-process.ts:**
182
+
183
+ ```ts
184
+ import path from 'node:path';
185
+ import { deal, createProcess } from '@d-zero/puppeteer-dealer';
186
+
187
+ type ChildProcessParams = {
188
+ outputDir: string;
189
+ };
190
+
191
+ await deal(
192
+ [
193
+ { id: '1', url: 'https://example.com/page1' },
194
+ { id: '2', url: 'https://example.com/page2' },
195
+ ],
196
+ (_, done, total) => {
197
+ return `処理中: ${done}/${total}`;
198
+ },
199
+ () => {
200
+ return createProcess<ChildProcessParams>(
201
+ path.resolve(import.meta.dirname, 'child-process.js'),
202
+ {
203
+ outputDir: './output',
204
+ },
205
+ {
206
+ locale: 'ja-JP',
207
+ headless: true,
208
+ },
209
+ );
210
+ },
44
211
  {
45
- locale: 'ja-JP',
212
+ limit: 5,
46
213
  debug: false,
47
- limit: 10,
48
- verbose: false,
49
-
50
- // Puppeteer launch options
51
- headless: true,
52
214
  },
53
215
  );
54
216
  ```
217
+
218
+ **child-process.ts:**
219
+
220
+ ```ts
221
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
222
+
223
+ type ChildProcessParams = {
224
+ outputDir: string;
225
+ };
226
+
227
+ createChildProcess<ChildProcessParams>(async (params) => {
228
+ const { outputDir, needAuth } = params;
229
+
230
+ return {
231
+ async eachPage({ page, id, url }, logger) {
232
+ logger(`${url}を処理しています...`);
233
+
234
+ await page.goto(url);
235
+
236
+ const title = await page.title();
237
+ logger(`タイトル: ${title}`);
238
+
239
+ // スクリーンショットを撮る
240
+ await page.screenshot({
241
+ path: `${outputDir}/${id}.png`,
242
+ });
243
+
244
+ logger('完了');
245
+ },
246
+ };
247
+ });
248
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/puppeteer-dealer",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "Puppeteer handles each page",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -23,19 +23,19 @@
23
23
  "clean": "tsc --build --clean"
24
24
  },
25
25
  "dependencies": {
26
- "@d-zero/dealer": "1.5.3",
27
- "@d-zero/proc-talk": "0.4.14",
28
- "@d-zero/shared": "0.17.1",
26
+ "@d-zero/dealer": "1.5.4",
27
+ "@d-zero/proc-talk": "0.4.15",
28
+ "@d-zero/shared": "0.18.0",
29
29
  "ansi-colors": "4.1.3",
30
30
  "debug": "4.4.3",
31
31
  "puppeteer-extra": "3.3.6",
32
32
  "puppeteer-extra-plugin-stealth": "2.11.2"
33
33
  },
34
34
  "devDependencies": {
35
- "puppeteer": "24.34.0"
35
+ "puppeteer": "24.37.1"
36
36
  },
37
37
  "peerDependencies": {
38
- "puppeteer": "24.34.0"
38
+ "puppeteer": "24.37.1"
39
39
  },
40
- "gitHead": "c976f890ac4225e20df8fc83118f31231bd6323c"
40
+ "gitHead": "008ea25926579b061e36c1f5f4f4fc7e15cb782e"
41
41
  }