import Result from './result' import fetch from 'node-fetch' type Status = 'succeeded'|'failed' export interface ResultData { // Size of the PDF in bytes. size: number // Width of the PDF in pixel. width: number // Height of the PDF in pixel. height: number // Expiration time of the CDN URL. expiresAt: Date|null // Runtime in milliseconds. runtime: number // URL to the PDF. url: string } export default class Conversion extends Result { private cachedBuffer: Buffer|null = null constructor(public initializedAt: Date, public finishedAt: Date, public status: Status, public error: string|null, public result: ResultData) { super() } async buffer() { if (this.cachedBuffer) { return this.cachedBuffer } if (!this.result) { throw new Error('PDF does not exist.') } const response = await fetch(this.result.url) if (response.status !== 200) { throw new Error('PDF could not be downloaded from the CDN.') } return this.cachedBuffer = await response.buffer() } }