From 29a09c259643124094cedffb038b2ee6e91cae67 Mon Sep 17 00:00:00 2001 From: Xmader Date: Wed, 27 Jan 2021 12:35:52 -0500 Subject: [PATCH] refactor: pdf worker fetch blobs --- src/worker.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/worker.ts b/src/worker.ts index 179a5b8..1bc0dd9 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -24,13 +24,12 @@ const readData = (blob: Blob, type: DataResultType): Promise => { }) } -const fetchData = async (imgUrl: string, type: DataResultType): Promise => { +const fetchBlob = async (imgUrl: string): Promise => { const r = await fetch(imgUrl) - const blob = await r.blob() - return readData(blob, type) + return r.blob() } -const generatePDF = async (imgURLs: string[], imgType: ImgType, width: number, height: number): Promise => { +const generatePDF = async (imgBlobs: Blob[], imgType: ImgType, width: number, height: number): Promise => { // @ts-ignore const pdf = new (PDFDocument as typeof import('pdfkit'))({ // compress: true, @@ -41,7 +40,7 @@ const generatePDF = async (imgURLs: string[], imgType: ImgType, width: number, h }) if (imgType === 'png') { - const imgDataUrlList: string[] = await Promise.all(imgURLs.map(url => fetchData(url, 'dataUrl'))) + const imgDataUrlList: string[] = await Promise.all(imgBlobs.map(b => readData(b, 'dataUrl'))) imgDataUrlList.forEach((data) => { pdf.addPage() @@ -51,7 +50,7 @@ const generatePDF = async (imgURLs: string[], imgType: ImgType, width: number, h }) }) } else { // imgType == "svg" - const svgList = await Promise.all(imgURLs.map(url => fetchData(url, 'text'))) + const svgList = await Promise.all(imgBlobs.map(b => readData(b, 'text'))) svgList.forEach((svg) => { pdf.addPage() @@ -71,14 +70,16 @@ export type PDFWorkerMessage = [string[], ImgType, number, number]; onmessage = async (e): Promise => { const [ - imgURLs, + imgUrls, imgType, width, height, ] = e.data as PDFWorkerMessage + const imgBlobs = await Promise.all(imgUrls.map(url => fetchBlob(url))) + const pdfBuf = await generatePDF( - imgURLs, + imgBlobs, imgType, width, height,