aseli commited on
Commit
f3a829e
1 Parent(s): dbb8a67

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +38 -20
index.js CHANGED
@@ -1,4 +1,4 @@
1
- const express = require('express');
2
  const { chromium } = require('playwright');
3
  const QRCode = require('qrcode');
4
  const Jimp = require('jimp');
@@ -8,6 +8,10 @@ const QrCodeReader = require('qrcode-reader');
8
  const app = express();
9
  const port = 7860;
10
 
 
 
 
 
11
  app.use(express.json());
12
 
13
  app.get('/', (req, res) => {
@@ -75,16 +79,25 @@ app.get('/qrcode', async (req, res) => {
75
  async function processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }) {
76
  try {
77
  console.log('process screenshot');
78
- const browser = await chromium.launch({
79
- headless: true,
80
- args: ["--no-sandbox", "--disable-setuid-sandbox"]
81
- });
82
- const context = await browser.newContext({
83
- userAgent: ua || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
84
- viewport: { width: width || 1280, height: height || 800 },
85
- deviceScaleFactor: 2
86
- });
87
- const page = await context.newPage();
 
 
 
 
 
 
 
 
 
88
 
89
  if (language) {
90
  await page.emulateMedia({ colorScheme: language });
@@ -94,12 +107,6 @@ async function processScreenshot(req, res, { ua, url, type, width, height, langu
94
 
95
  // Handle JavaScript challenges
96
  await page.waitForLoadState('networkidle');
97
-
98
- // Example to handle JavaScript challenge
99
- // This is a placeholder, replace with actual challenge handling if needed
100
- // await page.evaluate(() => {
101
- // // Solve any inline JavaScript challenges
102
- // });
103
 
104
  let screenshotOptions = {
105
  fullPage: fullpage == 1 ? true : false,
@@ -129,18 +136,18 @@ async function processScreenshot(req, res, { ua, url, type, width, height, langu
129
  });
130
  } else if (type === 'custom') {
131
  if (width && height > 4096) {
 
132
  return res.json({
133
  "status": 400,
134
  "message": "Width and height values should not exceed 4096 pixels."
135
  });
136
- await browser.close();
137
  }
138
  if (width && height < 400) {
 
139
  return res.json({
140
  "status": 400,
141
  "message": "Width and height values should not be less than 400 pixels."
142
  });
143
- await browser.close();
144
  }
145
  await page.setViewportSize({
146
  width: Number(width) || 375,
@@ -158,7 +165,18 @@ async function processScreenshot(req, res, { ua, url, type, width, height, langu
158
  });
159
  res.end(screenshot);
160
  console.log('process done');
161
- await browser.close();
 
 
 
 
 
 
 
 
 
 
 
162
  } catch (e) {
163
  console.error(e);
164
  return res.json({
 
1
+ const express = require('express');
2
  const { chromium } = require('playwright');
3
  const QRCode = require('qrcode');
4
  const Jimp = require('jimp');
 
8
  const app = express();
9
  const port = 7860;
10
 
11
+ // Global variable to hold the browser instance and context
12
+ let browser;
13
+ let browserContext;
14
+
15
  app.use(express.json());
16
 
17
  app.get('/', (req, res) => {
 
79
  async function processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }) {
80
  try {
81
  console.log('process screenshot');
82
+
83
+ // Launch browser and create a context if it doesn't exist
84
+ if (!browser) {
85
+ browser = await chromium.launch({
86
+ headless: true,
87
+ args: ["--no-sandbox", "--disable-setuid-sandbox"]
88
+ });
89
+ }
90
+
91
+ // Reuse or create a new context
92
+ if (!browserContext) {
93
+ browserContext = await browser.newContext({
94
+ userAgent: ua || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
95
+ viewport: { width: width || 1280, height: height || 800 },
96
+ deviceScaleFactor: 2
97
+ });
98
+ }
99
+
100
+ const page = await browserContext.newPage();
101
 
102
  if (language) {
103
  await page.emulateMedia({ colorScheme: language });
 
107
 
108
  // Handle JavaScript challenges
109
  await page.waitForLoadState('networkidle');
 
 
 
 
 
 
110
 
111
  let screenshotOptions = {
112
  fullPage: fullpage == 1 ? true : false,
 
136
  });
137
  } else if (type === 'custom') {
138
  if (width && height > 4096) {
139
+ await page.close();
140
  return res.json({
141
  "status": 400,
142
  "message": "Width and height values should not exceed 4096 pixels."
143
  });
 
144
  }
145
  if (width && height < 400) {
146
+ await page.close();
147
  return res.json({
148
  "status": 400,
149
  "message": "Width and height values should not be less than 400 pixels."
150
  });
 
151
  }
152
  await page.setViewportSize({
153
  width: Number(width) || 375,
 
165
  });
166
  res.end(screenshot);
167
  console.log('process done');
168
+ await page.close(); // Close the tab instead of the browser
169
+
170
+ // Periodically close and reopen the browser to avoid memory bloat
171
+ if (browserContext.pages().length > 10) { // Arbitrary threshold
172
+ await browserContext.close();
173
+ browserContext = await browser.newContext();
174
+ }
175
+
176
+ // Trigger garbage collection
177
+ if (global.gc) {
178
+ global.gc();
179
+ }
180
  } catch (e) {
181
  console.error(e);
182
  return res.json({