phantomas 2.10.0 → 2.12.0

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/core/scope.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * phantomas browser "scope" with helper code
3
3
  *
4
- * Code below is executed in page's "scope" (injected by lib/browser.js)
4
+ * Code below is executed in page's "scope" (injected by the scope.injectJs() helper)
5
5
  */
6
6
  /* istanbul ignore next */
7
7
  (function coreScope(scope) {
@@ -5,8 +5,8 @@
5
5
 
6
6
  module.exports = function (phantomas) {
7
7
  const puppeteer = require("puppeteer"),
8
- devices = puppeteer.devices,
9
- // @see https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js
8
+ devices = puppeteer.KnownDevices,
9
+ // @see https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/common/Device.ts
10
10
  availableDevices = {
11
11
  phone: "Galaxy S5", // 360x640
12
12
  "phone-landscape": "Galaxy S5 landscape", // 640x360
@@ -14,7 +14,7 @@ module.exports = function (phantomas) {
14
14
  "tablet-landscape": "Kindle Fire HDX landscape", // 1280x800
15
15
  };
16
16
 
17
- var device;
17
+ let device;
18
18
 
19
19
  // check if --phone or --tablet option was passed
20
20
  Object.keys(availableDevices).forEach(function (item) {
@@ -3,6 +3,8 @@
3
3
  */
4
4
  "use strict";
5
5
 
6
+ const { setTimeout } = require("timers/promises");
7
+
6
8
  module.exports = function (phantomas) {
7
9
  // e.g. --post-load-delay 5
8
10
  var delay = parseInt(phantomas.getParam("post-load-delay"), 10);
@@ -14,9 +16,9 @@ module.exports = function (phantomas) {
14
16
  // https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#framewaitforselectororfunctionortimeout-options-args
15
17
  phantomas.log("Will wait %d second(s) after load", delay);
16
18
 
17
- phantomas.on("beforeClose", (page) => {
19
+ phantomas.on("beforeClose", async () => {
18
20
  phantomas.log("Sleeping for %d seconds", delay);
19
21
 
20
- return page.waitForTimeout(delay * 1000);
22
+ return setTimeout(delay * 1000);
21
23
  });
22
24
  };
package/lib/browser.js CHANGED
@@ -179,7 +179,7 @@ Browser.prototype.init = async (phantomasOptions) => {
179
179
  var meta = responses[data.requestId];
180
180
 
181
181
  /* istanbul ignore if */
182
- if (typeof meta === "undefined") {
182
+ if (typeof meta?.response === "undefined") {
183
183
  // the browser sometimes looses trace of a request, let's ignore.
184
184
  networkDebug(
185
185
  "Can't find request id %d in previous requests",
package/lib/index.js CHANGED
@@ -89,7 +89,10 @@ function phantomas(url, opts) {
89
89
  return page.$$(selector);
90
90
  },
91
91
 
92
- // @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageevaluateonnewdocumentpagefunction-args
92
+ // @see https://pptr.dev/api/puppeteer.page.evaluateonnewdocument
93
+ // Adds a function which would be invoked in when:
94
+ // - the child frame is attached or navigated
95
+ // - the page is navigated
93
96
  injectJs: async (script) => {
94
97
  const debug = require("debug")("phantomas:injectJs");
95
98
 
@@ -98,7 +101,9 @@ function phantomas(url, opts) {
98
101
  suffix = "}";
99
102
 
100
103
  const preloadFile =
101
- prefix + require("fs").readFileSync(script, "utf8") + suffix;
104
+ prefix +
105
+ (await require("fs/promises").readFile(script, "utf8")) +
106
+ suffix;
102
107
 
103
108
  await page.evaluateOnNewDocument(preloadFile);
104
109
 
@@ -117,8 +122,9 @@ function phantomas(url, opts) {
117
122
  // @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageexposefunctionname-puppeteerfunction
118
123
  await page.exposeFunction("__phantomas_emit", scope.emit);
119
124
 
120
- // Inject helper code into the browser's scope
121
- events.on("init", () => {
125
+ // Inject helper code into the browser's scope (but only once!)
126
+ events.once("init", () => {
127
+ debug("onInit: injecting the core/scope.js ...");
122
128
  scope.injectJs(__dirname + "/../core/scope.js");
123
129
  });
124
130
 
@@ -92,13 +92,17 @@
92
92
  })();
93
93
 
94
94
  // count <iframe> tags
95
- document.querySelectorAll("iframe").forEach(function (iframe) {
96
- phantomas.incrMetric("iframesCount"); // @desc number of iframe nodes
95
+ const iframes = document.querySelectorAll("iframe");
96
+ phantomas.setMetric("iframesCount", iframes.length); // @desc number of iframe nodes
97
+
98
+ for (const iframe of iframes) {
99
+ phantomas.log(`iframe: ${iframe.src}`);
100
+
97
101
  phantomas.addOffender("iframesCount", {
98
102
  element: phantomas.getDOMPath(iframe),
99
103
  url: iframe.src,
100
104
  });
101
- });
105
+ }
102
106
 
103
107
  phantomas.spyEnabled(true);
104
108
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phantomas",
3
- "version": "2.10.0",
3
+ "version": "2.12.0",
4
4
  "author": "macbre <maciej.brencz@gmail.com> (http://macbre.net)",
5
5
  "description": "Headless Chromium-based web performance metrics collector and monitoring tool",
6
6
  "main": "./lib/index.js",
@@ -27,14 +27,14 @@
27
27
  "node": ">=16.0"
28
28
  },
29
29
  "dependencies": {
30
- "analyze-css": "^2.0.0",
30
+ "analyze-css": "^2.1.89",
31
31
  "analyze-image": "^1.0.0",
32
32
  "commander": "^9.0.0",
33
33
  "debug": "^4.1.1",
34
34
  "decamelize": "^5.0.0",
35
35
  "fast-stats": "0.0.6",
36
36
  "js-yaml": "^4.0.0",
37
- "puppeteer": "^21.2.1"
37
+ "puppeteer": "^22.10.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@jest/globals": "^28.0.0",
@@ -62,7 +62,7 @@
62
62
  "coverageThreshold": {
63
63
  "global": {
64
64
  "statements": 89,
65
- "branches": 85,
65
+ "branches": 80,
66
66
  "functions": 89,
67
67
  "lines": 89
68
68
  }