unprint 0.17.8 → 0.17.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.17.8",
3
+ "version": "0.17.9",
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
@@ -1164,6 +1164,14 @@ function curateResponse(res, options, { url, control, customOptions }) {
1164
1164
  };
1165
1165
  }
1166
1166
 
1167
+ async function closeBrowser(client, options) {
1168
+ if (options.client === null // this browser is single-use
1169
+ || (client.retired && client.active === 0)) { // this browser is retired to minimize garbage build-up
1170
+ // this browser won't be reused
1171
+ await client.browser.close();
1172
+ }
1173
+ }
1174
+
1167
1175
  async function browserRequest(url, customOptions = {}) {
1168
1176
  const options = merge.all([{
1169
1177
  timeout: 1000,
@@ -1197,7 +1205,15 @@ async function browserRequest(url, customOptions = {}) {
1197
1205
 
1198
1206
  const res = await page.goto(url, {
1199
1207
  ...options.page,
1200
- });
1208
+ }).catch((error) => error);
1209
+
1210
+ if (res instanceof Error) {
1211
+ return {
1212
+ ok: false,
1213
+ status: null,
1214
+ statusText: res.name,
1215
+ };
1216
+ }
1201
1217
 
1202
1218
  const status = res.status();
1203
1219
  const statusText = res.statusText();
@@ -1214,6 +1230,8 @@ async function browserRequest(url, customOptions = {}) {
1214
1230
 
1215
1231
  client.active -= 1;
1216
1232
 
1233
+ await closeBrowser(client, options);
1234
+
1217
1235
  return {
1218
1236
  ok: false,
1219
1237
  status,
@@ -1236,6 +1254,8 @@ async function browserRequest(url, customOptions = {}) {
1236
1254
  } catch (error) {
1237
1255
  client.active -= 1;
1238
1256
 
1257
+ await closeBrowser(client, options);
1258
+
1239
1259
  return {
1240
1260
  ok: false,
1241
1261
  controlError: error.message,
@@ -1262,11 +1282,7 @@ async function browserRequest(url, customOptions = {}) {
1262
1282
 
1263
1283
  client.active -= 1;
1264
1284
 
1265
- if (options.client === null // this browser is single-use
1266
- || (client.retired && client.active === 0)) { // this browser is retired to minimize garbage build-up
1267
- // this browser won't be reused
1268
- await client.browser.close();
1269
- }
1285
+ await closeBrowser(client, options);
1270
1286
 
1271
1287
  return curateResponse({
1272
1288
  data,
package/tests/browser.js CHANGED
@@ -16,6 +16,8 @@ unprint.options({ // or unprint.options();
16
16
 
17
17
  async function initTest() {
18
18
  // concurrency
19
+ /*
20
+ console.log('TEST CONCURRENCY');
19
21
  await Promise.all(Array.from({ length: 20 }).map(async () => {
20
22
  // await unprint.browser(`https://tools-httpstatus.pickup-services.com/${Math.random() < 0.2 ? '404' : '200'}?sleep=${Math.round(Math.random() * 500)}`, {
21
23
  await unprint.browser(`https://tools-httpstatus.pickup-services.com/200?sleep=${Math.round(Math.random() * 5000)}`, {
@@ -26,11 +28,24 @@ async function initTest() {
26
28
  },
27
29
  });
28
30
  }));
31
+ */
29
32
 
30
33
  // console.log('Requests done, waiting...');
31
34
 
32
35
  // await new Promise((resolve) => { setTimeout(() => resolve(), 60 * 60 * 1000); });
36
+ // timeout
37
+ console.log('TEST TIMEOUT');
38
+ await unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=30000', {
39
+ // client: null,
40
+ browser: {
41
+ headless: true,
42
+ },
43
+ page: {
44
+ timeout: 5000,
45
+ },
46
+ });
33
47
 
48
+ /*
34
49
  await Promise.all([
35
50
  unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=5000', {
36
51
  browser: {
@@ -56,6 +71,7 @@ async function initTest() {
56
71
  }),
57
72
  ]);
58
73
 
74
+ console.log('TEST SCRAPE');
59
75
  const res = await unprint.browser('https://www.scrapingcourse.com/', {
60
76
  browser: {
61
77
  headless: false,
@@ -69,6 +85,8 @@ async function initTest() {
69
85
 
70
86
  console.log('CARD TITLES', cards);
71
87
  console.log('CONTROL OUT', res.control);
88
+ */
89
+ console.log('CLOSING ALL BROWSERS');
72
90
 
73
91
  await unprint.closeAllBrowsers();
74
92
  }