Skip to content

uyamazak/fastify-hc-pages

Repository files navigation

npm version ci License: MIT

fastify-hc-pages

A plugin that allows you to use Headless Chrome Pages from Fastify by Puppeteer.

Native TypeScript is supportted.

This plugin was derived from the following repository.

https://github.com/uyamazak/hc-pdf-server

Getting started

npm i @uyamazak/fastify-hc-pages

# OR

yarn add @uyamazak/fastify-hc-pages

Usage

import fastify from 'fastify'
import { hcPages } from '@uyamazak/fastify-hc-pages'

const app = async () => {
  const server = fastify()
  // Register this plugin
  server.register(hcPages)

  // Work together with Puppeteer's Page in callback function.
  server.get('/gettitle', async (_, reply) => {
    // Make result you need in callback function with Page
    const result = await server.runOnPage<string>(async (page: Page) => {
      await page.goto('https://example.com')
      return await page.title()
    })
    reply.send(result)
  })
}

Plugin Options

The following settings can be changed in the options when registering this plugin.

Here are the details and default values

server.register(hcPages, {
  /**
   * Number of Pages to launch.
   * Change according to the number of requests and machine resources.
   */
  pagesNum: 3,
  pageOptions: {
    /**
     * @see https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-pagesetuseragentuseragent
     */
    userAgent: '',
    /**
     * @see https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-pagesetdefaulttimeouttimeout
     */
    pageTimeoutMilliseconds: 10000,
    /**
     * @see https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-pageemulatemediatypetype
     */
    emulateMediaTypeScreenEnabled: false,
    /**
     * Add Accept-Language HTTP header
     */
    acceptLanguage: '',
    /**
     * @see https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-pagesetviewportviewport
     */
    viewport: null,
  },
  /**
   * @see https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-puppeteerlaunchoptions
   */
  launchOptions: {
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-gpu',
      '--disable-dev-shm-usage',
    ]
  },
})

LICENCE

MIT

Contributing

Pull requests, Issues, GitHub Sponsors are welcome.

Contributors ✨

Thanks!

salos1982 (Pull Request)

Author

uyamazak