@d-zero/puppeteer-dealer 0.6.5 → 0.7.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/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,233 @@ 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?: (
66
+ result: R,
67
+ push: (...items: URLInfo[]) => Promise<void>,
68
+ ) => void | Promise<void>;
69
+ },
70
+ ): Promise<void>;
71
+
72
+ type URLInfo = {
73
+ readonly id: string | null;
74
+ readonly url: string | URL;
75
+ };
76
+
77
+ type DealHeader = (
78
+ progress: number,
79
+ done: number,
80
+ total: number,
81
+ limit: number,
82
+ ) => string;
83
+ ```
84
+
85
+ ### `createProcess`
86
+
87
+ 子プロセスを作成する関数です。`deal`関数の第3引数で使用します。
88
+
89
+ ```ts
90
+ import { createProcess } from '@d-zero/puppeteer-dealer';
91
+
92
+ const processFactory = createProcess(
93
+ './child-process.js', // 子プロセスのパス
94
+ {
95
+ // 子プロセスに渡すパラメータ
96
+ param1: 'value1',
97
+ param2: 'value2',
98
+ },
99
+ {
100
+ // Puppeteerオプション
101
+ locale: 'ja-JP',
102
+ headless: true,
103
+ // その他のLaunchOptions
104
+ },
105
+ );
106
+ ```
107
+
108
+ **型定義:**
109
+
110
+ ```ts
111
+ function createProcess<P, R = void>(
112
+ subModulePath: string,
113
+ params: P,
114
+ options?: PuppeteerDealerOptions & LaunchOptions,
115
+ ): (needAuth: boolean) => ChildProcessManager<P, R>;
116
+
117
+ type PuppeteerDealerOptions = {
118
+ readonly locale?: string;
119
+ } & DealOptions;
120
+ ```
121
+
122
+ ### `createChildProcess`
123
+
124
+ 子プロセス側で使用する関数です。Puppeteerページの処理を定義します。
125
+
126
+ ```ts
127
+ // child-process.ts
128
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
129
+
130
+ type ChildProcessParams = {
131
+ param1: string;
132
+ param2: string;
133
+ };
134
+
135
+ createChildProcess<ChildProcessParams>((params) => {
136
+ const { param1, param2, needAuth } = params;
137
+
138
+ return {
139
+ async eachPage({ page, id, url, index }, logger) {
140
+ // Puppeteerページの処理
141
+ logger('ページにアクセスしています...');
36
142
  await page.goto(url);
143
+
144
+ logger('処理を実行しています...');
37
145
  await page.evaluate(() => {
38
- // Do something
146
+ // ページ内での処理
39
147
  });
148
+
149
+ // 戻り値を返すことができます
150
+ return { success: true };
40
151
  },
41
- },
152
+ };
153
+ });
154
+ ```
155
+
156
+ **型定義:**
157
+
158
+ ```ts
159
+ function createChildProcess<P, R = void>(
160
+ handler: ChildProcessHandler<P & CommonParams, R>,
161
+ ): void;
162
+
163
+ type ChildProcessHandler<P extends CommonParams, R> = (
164
+ params: P,
165
+ ) => Promise<ChildProcessMethods<R>> | ChildProcessMethods<R>;
166
+
167
+ type ChildProcessMethods<R> = {
168
+ eachPage: (params: EachPageParams, logger: Logger) => Promise<R>;
169
+ };
170
+
171
+ type EachPageParams = {
172
+ readonly page: Page;
173
+ readonly id: string;
174
+ readonly url: string;
175
+ readonly index: number;
176
+ };
177
+
178
+ type CommonParams = {
179
+ readonly needAuth: boolean;
180
+ };
42
181
 
43
- // Options
182
+ type Logger = (log: string) => void;
183
+ ```
184
+
185
+ ## 使用例
186
+
187
+ 完全な使用例:
188
+
189
+ **main-process.ts:**
190
+
191
+ ```ts
192
+ import path from 'node:path';
193
+ import { deal, createProcess } from '@d-zero/puppeteer-dealer';
194
+
195
+ type ChildProcessParams = {
196
+ outputDir: string;
197
+ };
198
+
199
+ await deal(
200
+ [
201
+ { id: '1', url: 'https://example.com/page1' },
202
+ { id: '2', url: 'https://example.com/page2' },
203
+ ],
204
+ (_, done, total) => {
205
+ return `処理中: ${done}/${total}`;
206
+ },
207
+ () => {
208
+ return createProcess<ChildProcessParams>(
209
+ path.resolve(import.meta.dirname, 'child-process.js'),
210
+ {
211
+ outputDir: './output',
212
+ },
213
+ {
214
+ locale: 'ja-JP',
215
+ headless: true,
216
+ },
217
+ );
218
+ },
44
219
  {
45
- locale: 'ja-JP',
220
+ limit: 5,
46
221
  debug: false,
47
- limit: 10,
48
- verbose: false,
49
-
50
- // Puppeteer launch options
51
- headless: true,
52
222
  },
53
223
  );
54
224
  ```
225
+
226
+ **child-process.ts:**
227
+
228
+ ```ts
229
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
230
+
231
+ type ChildProcessParams = {
232
+ outputDir: string;
233
+ };
234
+
235
+ createChildProcess<ChildProcessParams>(async (params) => {
236
+ const { outputDir, needAuth } = params;
237
+
238
+ return {
239
+ async eachPage({ page, id, url }, logger) {
240
+ logger(`${url}を処理しています...`);
241
+
242
+ await page.goto(url);
243
+
244
+ const title = await page.title();
245
+ logger(`タイトル: ${title}`);
246
+
247
+ // スクリーンショットを撮る
248
+ await page.screenshot({
249
+ path: `${outputDir}/${id}.png`,
250
+ });
251
+
252
+ logger('完了');
253
+ },
254
+ };
255
+ });
256
+ ```
package/dist/deal.d.ts CHANGED
@@ -9,5 +9,5 @@ import type { DealHeader, DealOptions } from '@d-zero/dealer';
9
9
  * @param options
10
10
  */
11
11
  export declare function deal<T, R = void>(list: readonly URLInfo[], header: DealHeader, createProcess: () => (needAuth: boolean) => ChildProcessManager<T, R>, options?: Omit<DealOptions, 'header'> & {
12
- each?: (result: R) => void | Promise<void>;
12
+ each?: (result: R, push: (...items: URLInfo[]) => Promise<void>) => void | Promise<void>;
13
13
  }): Promise<void>;
package/dist/deal.js CHANGED
@@ -12,7 +12,7 @@ export function deal(list, header, createProcess, options) {
12
12
  const urlObj = new URL(url);
13
13
  return !!(urlObj.username && urlObj.password);
14
14
  });
15
- return coreDeal(list, ({ id, url }, update, index, setLineHeader) => {
15
+ return coreDeal(list, ({ id, url }, update, index, setLineHeader, push) => {
16
16
  const fileId = id || index.toString().padStart(3, '0');
17
17
  const lineHeader = `%braille% ${c.bgWhite(` ${fileId} `)} ${c.gray(url.toString())}: `;
18
18
  setLineHeader(lineHeader);
@@ -24,7 +24,7 @@ export function deal(list, header, createProcess, options) {
24
24
  processManager.log((log) => update(log));
25
25
  const result = await processManager.each(fileId, url.toString(), index);
26
26
  if (options?.each) {
27
- await options.each(result);
27
+ await options.each(result, push);
28
28
  }
29
29
  await processManager.close();
30
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/puppeteer-dealer",
3
- "version": "0.6.5",
3
+ "version": "0.7.0",
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.6.0",
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.36.0"
35
+ "puppeteer": "24.37.2"
36
36
  },
37
37
  "peerDependencies": {
38
- "puppeteer": "24.36.0"
38
+ "puppeteer": "24.37.2"
39
39
  },
40
- "gitHead": "e2189e6878674b8fef5fa3c121ad109c448040fe"
40
+ "gitHead": "a6d7b36c485bbc0782375c6e1ad0d0606f423e97"
41
41
  }