unprint 0.19.16 → 0.19.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/app.js +25 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.19.16",
3
+ "version": "0.19.18",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {
package/src/app.js CHANGED
@@ -1225,6 +1225,10 @@ async function getBrowserContext(browser, options, useProxy) {
1225
1225
  server: `${options.proxy.host}:${options.proxy.port}`,
1226
1226
  },
1227
1227
  }),
1228
+ extraHTTPHeaders: {
1229
+ ...options.headers,
1230
+ cookie: getCookie(options),
1231
+ },
1228
1232
  });
1229
1233
  }
1230
1234
 
@@ -1259,10 +1263,11 @@ async function getBrowserInstance(scope, options, useProxy = false, useRemote =
1259
1263
  : chromium.launch({
1260
1264
  headless: true,
1261
1265
  ...options.browser,
1262
- })).then(async (browser) => {
1263
- const context = await getBrowserContext(browser, options, useProxy);
1266
+ })).then(async (browser) => { // eslint-disable-line arrow-body-style
1267
+ // legacy
1268
+ // const context = await getBrowserContext(browser, options, useProxy);
1264
1269
 
1265
- return { browser, context };
1270
+ return { browser };
1266
1271
  });
1267
1272
 
1268
1273
  const client = {
@@ -1281,12 +1286,13 @@ async function getBrowserInstance(scope, options, useProxy = false, useRemote =
1281
1286
  }
1282
1287
 
1283
1288
  try {
1284
- const { browser, context } = await launchers;
1289
+ // const { browser, context } = await launchers;
1290
+ const { browser } = await launchers;
1285
1291
 
1286
- context.setDefaultNavigationTimeout(options.timeout);
1292
+ // context.setDefaultNavigationTimeout(options.timeout);
1287
1293
 
1288
1294
  client.browser = browser;
1289
- client.context = context;
1295
+ // client.context = context;
1290
1296
  } catch (error) {
1291
1297
  clients.delete(scopeKey);
1292
1298
 
@@ -1304,7 +1310,7 @@ async function closeAllBrowsers() {
1304
1310
  const closingClients = Array.from(clients.values());
1305
1311
 
1306
1312
  await Promise.all(closingClients.map(async (client) => {
1307
- await client.context.close();
1313
+ await client.context?.close();
1308
1314
  await client.browser.close();
1309
1315
 
1310
1316
  clients.delete(client.key);
@@ -1322,7 +1328,7 @@ async function closeBrowser(client, options = {}) {
1322
1328
  if (options.client === null // this browser is single-use
1323
1329
  || (client.retired && client.active === 0)) { // this browser is retired to minimize garbage build-up
1324
1330
  // this browser won't be reused, browser close DOES NOT automatically close context https://github.com/microsoft/playwright/issues/15163
1325
- await client.context.close();
1331
+ await client.context?.close();
1326
1332
  await client.browser.close();
1327
1333
 
1328
1334
  clients.delete(client.key);
@@ -1432,19 +1438,18 @@ async function browserRequest(url, customOptions = {}) {
1432
1438
 
1433
1439
  client.active += 1;
1434
1440
 
1435
- const page = await client.context.newPage();
1436
-
1437
- await page.setExtraHTTPHeaders(curateHeaders({
1438
- 'user-agent': options.browserUserAgent || options.userAgent,
1439
- ...options.headers,
1440
- cookie: getCookie(options),
1441
- }, options));
1441
+ const context = await getBrowserContext(client.browser, options, useProxy);
1442
+ const page = await context.newPage();
1442
1443
 
1443
1444
  const res = await page.goto(url, {
1444
1445
  ...options.page,
1445
1446
  }).catch((error) => error);
1446
1447
 
1447
1448
  if (res instanceof Error) {
1449
+ await context.close();
1450
+ await page.close();
1451
+ await closeBrowser(client, options);
1452
+
1448
1453
  return {
1449
1454
  ok: false,
1450
1455
  status: null,
@@ -1469,6 +1474,8 @@ async function browserRequest(url, customOptions = {}) {
1469
1474
 
1470
1475
  client.active -= 1;
1471
1476
 
1477
+ await page.close();
1478
+ await context.close();
1472
1479
  await closeBrowser(client, options);
1473
1480
 
1474
1481
  return curateResponse({
@@ -1494,6 +1501,8 @@ async function browserRequest(url, customOptions = {}) {
1494
1501
  } catch (error) {
1495
1502
  client.active -= 1;
1496
1503
 
1504
+ await page.close();
1505
+ await context.close();
1497
1506
  await closeBrowser(client, options);
1498
1507
 
1499
1508
  events.emit('controlError', {
@@ -1519,6 +1528,7 @@ async function browserRequest(url, customOptions = {}) {
1519
1528
  const data = await page.content();
1520
1529
 
1521
1530
  await page.close();
1531
+ await context.close();
1522
1532
 
1523
1533
  events.emit('requestSuccess', {
1524
1534
  ...feedbackBase,