npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@d-zero/puppeteer-dealer

v0.7.10

Published

Puppeteer handles each page

Readme

@d-zero/puppeteer-dealer

Puppeteerを使ってマルチプロセスで複数のURLを処理するためのパッケージです。

API

deal

URLリストを処理するメイン関数です。

import { deal, createProcess } from '@d-zero/puppeteer-dealer';

await deal(
	// URLリスト
	[
		{
			id: '1',
			url: 'https://example.com',
		},
		{
			id: '2',
			url: 'https://example.com',
		},
	],

	// ヘッダーログ
	(progress, done, total) => {
		return `Header ${Math.ceil(progress * 100)}% ${done}/${total}`;
	},

	// プロセス作成関数
	() => {
		return createProcess(
			'./child-process.js', // 子プロセスのパス
			{
				// 子プロセスに渡すパラメータ
				// 任意のデータを渡すことができます
			},
			{
				// オプション
				locale: 'ja-JP',
				headless: true,
			},
		);
	},

	// オプション
	{
		limit: 10,
		debug: false,
		verbose: false,
	},
);

型定義:

function deal<T, R = void>(
	list: readonly URLInfo[],
	header: DealHeader,
	createProcess: () => (needAuth: boolean) => ChildProcessManager<T, R>,
	options?: Omit<DealOptions, 'header'> & {
		each?: (
			result: R,
			push: (...items: URLInfo[]) => Promise<void>,
		) => void | Promise<void>;
	},
): Promise<void>;

type URLInfo = {
	readonly id: string | null;
	readonly url: string | URL;
};

type DealHeader = (
	progress: number,
	done: number,
	total: number,
	limit: number,
) => string;

createProcess

子プロセスを作成する関数です。deal関数の第3引数で使用します。

import { createProcess } from '@d-zero/puppeteer-dealer';

const processFactory = createProcess(
	'./child-process.js', // 子プロセスのパス
	{
		// 子プロセスに渡すパラメータ
		param1: 'value1',
		param2: 'value2',
	},
	{
		// Puppeteerオプション
		locale: 'ja-JP',
		headless: true,
		// その他のLaunchOptions
	},
);

型定義:

function createProcess<P, R = void>(
	subModulePath: string,
	params: P,
	options?: PuppeteerDealerOptions & LaunchOptions,
): (needAuth: boolean) => ChildProcessManager<P, R>;

type PuppeteerDealerOptions = {
	readonly locale?: string;
} & DealOptions;

createChildProcess

子プロセス側で使用する関数です。Puppeteerページの処理を定義します。

// child-process.ts
import { createChildProcess } from '@d-zero/puppeteer-dealer';

type ChildProcessParams = {
	param1: string;
	param2: string;
};

createChildProcess<ChildProcessParams>((params) => {
	const { param1, param2, needAuth } = params;

	return {
		async eachPage({ page, id, url, index }, logger) {
			// Puppeteerページの処理
			logger('ページにアクセスしています...');
			await page.goto(url);

			logger('処理を実行しています...');
			await page.evaluate(() => {
				// ページ内での処理
			});

			// 戻り値を返すことができます
			return { success: true };
		},
	};
});

型定義:

function createChildProcess<P, R = void>(
	handler: ChildProcessHandler<P & CommonParams, R>,
): void;

type ChildProcessHandler<P extends CommonParams, R> = (
	params: P,
) => Promise<ChildProcessMethods<R>> | ChildProcessMethods<R>;

type ChildProcessMethods<R> = {
	eachPage: (params: EachPageParams, logger: Logger) => Promise<R>;
};

type EachPageParams = {
	readonly page: Page;
	readonly id: string;
	readonly url: string;
	readonly index: number;
};

type CommonParams = {
	readonly needAuth: boolean;
};

type Logger = (log: string) => void;

使用例

完全な使用例:

main-process.ts:

import path from 'node:path';
import { deal, createProcess } from '@d-zero/puppeteer-dealer';

type ChildProcessParams = {
	outputDir: string;
};

await deal(
	[
		{ id: '1', url: 'https://example.com/page1' },
		{ id: '2', url: 'https://example.com/page2' },
	],
	(_, done, total) => {
		return `処理中: ${done}/${total}`;
	},
	() => {
		return createProcess<ChildProcessParams>(
			path.resolve(import.meta.dirname, 'child-process.js'),
			{
				outputDir: './output',
			},
			{
				locale: 'ja-JP',
				headless: true,
			},
		);
	},
	{
		limit: 5,
		debug: false,
	},
);

child-process.ts:

import { createChildProcess } from '@d-zero/puppeteer-dealer';

type ChildProcessParams = {
	outputDir: string;
};

createChildProcess<ChildProcessParams>(async (params) => {
	const { outputDir, needAuth } = params;

	return {
		async eachPage({ page, id, url }, logger) {
			logger(`${url}を処理しています...`);

			await page.goto(url);

			const title = await page.title();
			logger(`タイトル: ${title}`);

			// スクリーンショットを撮る
			await page.screenshot({
				path: `${outputDir}/${id}.png`,
			});

			logger('完了');
		},
	};
});