/* eslint-disable no-console */ import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import { fetch } from "undici"; import { globalEnv } from "../../../common/globalEnv.js"; import LockFile from "../../storage/LockFile.js"; import AuthorizationService from "@entity-access/server-pages/dist/services/AuthorizationService.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { writeFile } from "fs/promises"; import TempFileService from "../../storage/TempFileService.js"; @RegisterScoped export default class PuppeteerService { // @Inject // siteCache: SiteDiskContentCache; @Inject authService: AuthorizationService; @Inject tfs: TempFileService; async generate({ url, mobile = false, width = mobile ? 650 : 1200, height = mobile ? 1000 : 1536, deviceScaleFactor = mobile ? 2 : 1, pageReadyScript = void 0, output = "webp", fileName = "w.webp", cacheKey = globalEnv.serverID, test = "true" as ("true" | "window.pageReady"), testDelay = 1000, fullPage = false, timeout = 15000, timezone = void 0, pageEvalScript = "() => void 0", page = void 0 as Page }) { const pageCookies = page?.request?.cookies; let cookies = "undefined"; if (pageCookies) { const domain = page.request.URL.hostname; cookies = JSON.stringify(Object.entries(pageCookies).map(([name, value]) => ({ name, value, domain, httpOnly: true, secure: true }))); } const qs = new URLSearchParams({ url, width: width as any, height: height as any, deviceScaleFactor: deviceScaleFactor as any, fullPage: fullPage ? "true" : "false", test, testDelay: testDelay as any, timeout: timeout as any, pageEvalScript, output, v: fileName }); if(mobile) { qs.append("mobile" , "true"); } if(pageReadyScript){ qs.append("pageReadyScript", pageReadyScript); } if (cookies) { qs.append("cookies", cookies); } if(timezone) { qs.append("timezone", timezone); } const query = qs.toString(); const fetchUrl = `${globalEnv.puppeteer.url}/screen-shot?${query}`; using _ = await LockFile.lock(`puppeteer-preview-generator`); // console.log(JSON.stringify({ // target: globalEnv.puppeteer.url, // url, // fetchUrl // })) const r = await fetch(fetchUrl, { // dispatcher }); if (r.status >= 399) { throw new Error(`Puppeteer Error ${await r.text()}`); } const tf = await this.tfs.createTempFile2(fileName) await writeFile(tf.path, r.body as any); return tf; } async generateVideo({ url, mobile = false, width = mobile ? 650 : 1200, height = mobile ? 1000 : 1536, deviceScaleFactor = mobile ? 2 : 1, pageReadyScript = void 0, output = "webp", fileName = "w.webp", cacheKey = globalEnv.serverID, fullPage = false, timeout = 30000, pageStopScript, pageEvalScript = "() => void 0", page = void 0 as Page }) { const pageCookies = page?.request?.cookies; let cookies = "undefined"; if (pageCookies) { const domain = page.request.URL.hostname; cookies = JSON.stringify(Object.entries(pageCookies).map(([name, value]) => ({ name, value, domain, httpOnly: true, secure: true }))); } const qs = new URLSearchParams({ url, width: width as any, height: height as any, deviceScaleFactor: deviceScaleFactor as any, fullPage: fullPage ? "true" : "false", timeout: timeout as any, pageEvalScript, output, v: fileName }); if(mobile) { qs.append("mobile" , "true"); } if(pageReadyScript){ qs.append("pageReadyScript", pageReadyScript); } if(pageStopScript) { qs.append("stopScript",pageStopScript); } if (cookies) { qs.append("cookies", cookies); } const query = qs.toString(); const fetchUrl = `${globalEnv.puppeteer.url}/screen-cast?${query}`; using _ = await LockFile.lock(`puppeteer-preview-generator`); // console.log(JSON.stringify({ // target: globalEnv.puppeteer.url, // url, // fetchUrl // })) const r = await fetch(fetchUrl, { // dispatcher }); if (r.status >= 399) { throw new Error(`Puppeteer Error ${await r.text()}`); } const tf = await this.tfs.createTempFile2(fileName); await writeFile(tf.path, r.body as any); return tf; } }