unprint 0.17.2 → 0.17.3

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.2",
3
+ "version": "0.17.3",
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
@@ -1048,25 +1048,34 @@ const clients = new Map();
1048
1048
  /* eslint-enable no-param-reassign */
1049
1049
  async function getBrowserInstance(scope, options) {
1050
1050
  if (clients.has(scope)) {
1051
- return clients.get(scope);
1051
+ const client = clients.get(scope);
1052
+
1053
+ await client.launchers;
1054
+
1055
+ return client;
1052
1056
  }
1053
1057
 
1054
- const browser = await chromium.launch({
1058
+ // if we await here, we create a race condition, and a second call to getBrowserInstance would launch another browser that will overwrite the first one
1059
+ const browserLauncher = chromium.launch({
1055
1060
  headless: true,
1056
1061
  ...options.browser,
1057
1062
  });
1058
1063
 
1059
- const context = await browser.newContext({
1064
+ const contextLauncher = browserLauncher.then((browser) => browser.newContext({
1060
1065
  userAgent: 'unprint',
1061
1066
  ...options.context,
1062
- });
1067
+ }));
1063
1068
 
1064
- const client = { context, browser };
1069
+ const launchers = Promise.all([browserLauncher, contextLauncher]);
1070
+ const client = { launchers };
1065
1071
 
1066
1072
  if (scope) {
1067
1073
  clients.set(scope, client);
1068
1074
  }
1069
1075
 
1076
+ client.browser = await browserLauncher;
1077
+ client.context = await contextLauncher;
1078
+
1070
1079
  return client;
1071
1080
  }
1072
1081
 
package/tests/browser.js CHANGED
@@ -3,6 +3,25 @@
3
3
  const unprint = require('../src/app');
4
4
 
5
5
  async function initTest() {
6
+ // concurrency
7
+ await Promise.all([
8
+ unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=500', {
9
+ browser: {
10
+ headless: false,
11
+ },
12
+ }),
13
+ unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=500', {
14
+ browser: {
15
+ headless: false,
16
+ },
17
+ }),
18
+ unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=500', {
19
+ browser: {
20
+ headless: false,
21
+ },
22
+ }),
23
+ ]);
24
+
6
25
  await Promise.all([
7
26
  unprint.browser('https://tools-httpstatus.pickup-services.com/200?sleep=5000', {
8
27
  browser: {