Skip to content

Commit

Permalink
Merge pull request #100 from uyamazak/add_type_runOnPageCallback
Browse files Browse the repository at this point in the history
Use Generics to return value of runOnPage callback
  • Loading branch information
uyamazak authored Feb 3, 2021
2 parents 08ffdb9 + 538bd05 commit 7e4f44c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ export const app = async (
const pdfOptionsQuery =
request.query.pdf_option ?? defaultPresetPdfOptionsName
try {
const buffer = await server.runOnPage(async (page: Page) => {
const buffer = await server.runOnPage<Buffer>(async (page: Page) => {
await page.goto(url)
const pdfOptions = server.getPDFOptions(pdfOptionsQuery)
const buffer = await page.pdf(pdfOptions)
return buffer
})

reply.headers(createPDFHttpHeader(buffer))
reply.send(buffer)
} catch (error) {
console.error(`error ${error}`)
reply.code(500).send({ error, url })
return
}
Expand All @@ -139,17 +139,15 @@ export const app = async (
const pdfOptions = server.getPDFOptions(pdfOptionsQuery)

try {
const buffer = await server.runOnPage(async (page: Page) => {
const buffer = await server.runOnPage<Buffer>(async (page: Page) => {
await page.setContent(html, { waitUntil: ['domcontentloaded'] })

const buffer = await page.pdf(pdfOptions)
return buffer
})

reply.headers(createPDFHttpHeader(buffer))
reply.send(buffer)
} catch (error) {
console.error(`werror ${error}`)
console.error(`error ${error}`)
reply.code(500).send({ error })
return
}
Expand Down
20 changes: 12 additions & 8 deletions src/plugins/hc-pages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FastifyInstance } from 'fastify'
import { launch, ChromeArgOptions, Page, Browser } from 'puppeteer'
import fp from 'fastify-plugin'
import { HcPageConfig } from '../types/hc-pages'
import { HcPageConfig, RunOnPageCallback } from '../types/hc-pages'

export class HCPages {
private pages: Page[]
private readyPages: Page[]
Expand All @@ -28,16 +29,16 @@ export class HCPages {
await this.closeBrowser()
}

private async runCallback(
private async runCallback<T>(
page: Page,
callback: (page: Page) => Buffer
): Promise<Buffer> {
callback: RunOnPageCallback<T>
): Promise<T> {
const result = await callback(page)
this.readyPages.push(page)
return result
}

async runOnPage(callback: (page: Page) => Buffer): Promise<Buffer> {
async runOnPage<T>(callback: RunOnPageCallback<T>): Promise<T> {
let page = this.readyPages.pop()
while (!page) {
await Promise.race(this.currentPromises)
Expand Down Expand Up @@ -123,9 +124,12 @@ async function plugin(
) {
const hcPages = new HCPages(options)
await hcPages.init()
fastify.decorate('runOnPage', async (callback) => {
return await hcPages.runOnPage(callback)
})
fastify.decorate(
'runOnPage',
async (callback: RunOnPageCallback<unknown>) => {
return await hcPages.runOnPage(callback)
}
)
fastify.decorate('destroyHcPages', async () => {
await hcPages.destroy()
})
Expand Down
5 changes: 3 additions & 2 deletions src/types/hc-pages.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Viewport } from 'puppeteer'
import { Viewport, Page } from 'puppeteer'

interface HcPageConfig {
pagesNum: number
Expand All @@ -9,9 +9,10 @@ interface HcPageConfig {
viewport?: Viewport
}

type RunOnPageCallback<T> = (page: Page) => Promise<T>
declare module 'fastify' {
interface FastifyInstance {
runOnPage(callback: unknown): Buffer
runOnPage<T>(callback: RunOnPageCallback<T>): Promise<T>
destroyHcPages(): Promise<void>
}
}
1 change: 1 addition & 0 deletions test/plugins/hc-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ test('enable options', async (t) => {
acceptLanguage: 'ja',
})
await hcPages.init()
t.equal((await hcPages.createPages()).length, pagesNum)
await hcPages.destroy()
})

0 comments on commit 7e4f44c

Please sign in to comment.