puppeteer-browser-ready 1.2.0 → 1.2.2

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/README.md CHANGED
@@ -5,14 +5,13 @@ _Simple utility to go to a URL and wait for the HTTP response_
5
5
 
6
6
  [![License:MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/center-key/puppeteer-browser-ready/blob/main/LICENSE.txt)
7
7
  [![npm](https://img.shields.io/npm/v/puppeteer-browser-ready.svg)](https://www.npmjs.com/package/puppeteer-browser-ready)
8
- [![Vulnerabilities](https://snyk.io/test/github/center-key/puppeteer-browser-ready/badge.svg)](https://snyk.io/test/github/center-key/puppeteer-browser-ready)
9
8
  [![Build](https://github.com/center-key/puppeteer-browser-ready/workflows/build/badge.svg)](https://github.com/center-key/puppeteer-browser-ready/actions/workflows/run-spec-on-push.yaml)
10
9
 
11
10
  **puppeteer-browser-ready** is a helper utility to reduce the amount of boilerplate code needed
12
11
  to tell Puppeteer to visit a web page and and retrieve the HTML. 
13
12
  It's primarily intended for use within [Mocha](https://mochajs.org) test cases. 
14
- In addition to the raw HTML, you get a [cheerio](https://cheerio.js.org) reference so you can
15
- immediately run queries on the DOM.
13
+ In addition to the raw HTML, you get a [node-html-parsed](https://github.com/taoqf/node-html-parser)
14
+ root so you can immediately run queries on the DOM.
16
15
 
17
16
  ## A) Setup
18
17
  **Install packages:**
@@ -31,15 +30,15 @@ The **Promise** will resolve with a **Web** object containing a `title` field an
31
30
  Pass the **Web** object to the `browserReady.close(web)` function to disconnect the page.
32
31
  ```javascript
33
32
  const url = 'https://pretty-print-json.js.org/';
34
- let web; //fields: browser, page, response, status, location, title, html, $
33
+ let web; //fields: browser, page, response, status, location, title, html, root
35
34
  before(async () => web = await puppeteer.launch().then(browserReady.goto(url));
36
35
  after(async () => await browserReady.close(web));
37
36
  ```
38
37
  ### `goto()` Options
39
- | Name (key) | Type | Default | Description |
40
- | :----------- | :---------- | :------ | :----------------------------------------------- |
41
- | `addCheerio` | **boolean** | `false` | Return a cheerio reference for querying the DOM. |
42
- | `verbose` | **boolean** | `false` | Output HTTP connection debug messages. |
38
+ | Name (key) | Type | Default | Description |
39
+ | :----------- | :---------- | :------ | :-------------------------------------------------------- |
40
+ | `parseHtml` | **boolean** | `true` | Return the DOM root as an HTMLElement (node-html-parsed). |
41
+ | `verbose` | **boolean** | `false` | Output HTTP connection debug messages. |
43
42
 
44
43
  ### `startWebServer()` Options
45
44
  | Name (key) | Type | Default| Description |
@@ -63,7 +62,7 @@ type Web = {
63
62
  location: Location,
64
63
  title: string,
65
64
  html: string,
66
- $: cheerio.Root | null, //library for parsing and manipulating HTML
65
+ root: HTMLElement | null, //see node-html-parsed library
67
66
  };
68
67
  ```
69
68
 
@@ -120,7 +119,7 @@ import { browserReady } from 'puppeteer-browser-ready';
120
119
 
121
120
  // Setup
122
121
  const url = 'https://pretty-print-json.js.org/';
123
- let web; //fields: browser, page, response, status, location, title, html, $
122
+ let web; //fields: browser, page, response, status, location, title, html, root
124
123
  const loadWebPage = async () =>
125
124
  web = await puppeteer.launch().then(browserReady.goto(url));
126
125
  const closeWebPage = async () =>
@@ -212,7 +211,7 @@ Example configuration in **package.json** to allow 5,000 ms:
212
211
  ```json
213
212
  "scripts": {
214
213
  "pretest": "run-scripts clean build",
215
- "test": "mocha spec/*.spec.js --timeout 5000"
214
+ "test": "mocha spec/*.spec.js --timeout 7000"
216
215
  },
217
216
  ```
218
217
 
@@ -223,6 +222,7 @@ Example configuration in **package.json** to allow 5,000 ms:
223
222
  - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):  _Prepend a one-line banner comment (with license notice) to distribution files_
224
223
  - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):  _Copy or rename a file with optional package version number_
225
224
  - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):  _Recursively copy files from one folder to another folder_
225
+ - 🪺 [recursive-exec](https://github.com/center-key/recursive-exec):  _Run a command on each file in a folder and its subfolders_
226
226
  - 🔍 [replacer-util](https://github.com/center-key/replacer-util):  _Find and replace strings or template outputs in text files_
227
227
  - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):  _Revision web asset filenames with cache busting content hash fingerprints_
228
228
  - 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util):  _Organize npm scripts into named groups of easy to manage commands_
@@ -1,7 +1,8 @@
1
- //! puppeteer-browser-ready v1.2.0 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
1
+ //! puppeteer-browser-ready v1.2.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
2
2
 
3
3
  /// <reference types="cheerio" />
4
4
  import { Browser, HTTPResponse, Page } from 'puppeteer';
5
+ import { HTMLElement } from 'node-html-parser';
5
6
  import { Server } from 'http';
6
7
  import { SuiteFunction } from 'mocha';
7
8
  import httpTerminator from 'http-terminator';
@@ -29,9 +30,11 @@ export type Web = {
29
30
  title: string | null;
30
31
  html: string | null;
31
32
  $: cheerio.Root | null;
33
+ root: HTMLElement | null;
32
34
  };
33
35
  export type BrowserReadySettings = {
34
36
  addCheerio: boolean;
37
+ parseHtml: boolean;
35
38
  verbose: boolean;
36
39
  };
37
40
  export type BrowserReadyOptions = Partial<BrowserReadySettings>;
@@ -1,14 +1,6 @@
1
- //! puppeteer-browser-ready v1.2.0 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
1
+ //! puppeteer-browser-ready v1.2.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
2
2
 
3
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
- return new (P || (P = Promise))(function (resolve, reject) {
6
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
- step((generator = generator.apply(thisArg, _arguments || [])).next());
10
- });
11
- };
3
+ import { parse } from 'node-html-parser';
12
4
  import cheerio from 'cheerio';
13
5
  import express from 'express';
14
6
  import httpTerminator from 'http-terminator';
@@ -20,7 +12,7 @@ const browserReady = {
20
12
  },
21
13
  startWebServer(options) {
22
14
  const defaults = { folder: '.', port: 0, verbose: true, autoCleanup: true };
23
- const settings = Object.assign(Object.assign({}, defaults), options);
15
+ const settings = { ...defaults, ...options };
24
16
  const server = express().use(express.static(settings.folder)).listen(settings.port);
25
17
  const terminator = httpTerminator.createHttpTerminator({ server });
26
18
  const port = () => server.address().port;
@@ -51,28 +43,32 @@ const browserReady = {
51
43
  return http.terminator.terminate();
52
44
  },
53
45
  goto(url, options) {
54
- const defaults = { addCheerio: true, verbose: false };
55
- const settings = Object.assign(Object.assign({}, defaults), options);
46
+ const defaults = { addCheerio: true, parseHtml: true, verbose: false };
47
+ const settings = { ...defaults, ...options };
48
+ if (options?.addCheerio)
49
+ console.log('puppeteer-browser-ready: Option "addCheerio" is deprecated, use "parseHtml" instead.');
56
50
  const log = (label, msg) => settings.verbose &&
57
51
  console.log(' ', Date.now() % 100000, label + ':', msg);
58
- const web = (browser) => __awaiter(this, void 0, void 0, function* () {
52
+ const rootInfo = (root) => root.constructor.name + '/' + root.firstChild.toString();
53
+ const web = async (browser) => {
59
54
  log('Connected', browser.isConnected());
60
55
  try {
61
- const page = yield browser.newPage();
56
+ const page = await browser.newPage();
62
57
  log('Page....', url);
63
- const response = yield page.goto(url);
64
- log('Response', response === null || response === void 0 ? void 0 : response.url());
58
+ const response = await page.goto(url);
59
+ log('Response', response?.url());
65
60
  const status = response && response.status();
66
61
  log('Status', status);
67
- const location = yield page.evaluate(() => globalThis.location);
62
+ const location = await page.evaluate(() => globalThis.location);
68
63
  log('Host', location.host);
69
- const title = response && (yield page.title());
64
+ const title = response && await page.title();
70
65
  log('Title', title);
71
- const html = response && (yield response.text());
72
- log('Bytes', html === null || html === void 0 ? void 0 : html.length);
73
- const $ = html && settings.addCheerio ? cheerio.load(html) : null;
74
- log('$', $ && $['fn'].constructor.name);
75
- return { browser, page, response, status, location, title, html, $ };
66
+ const html = response && await response.text();
67
+ log('Bytes', html?.length);
68
+ const $ = html && settings.addCheerio ? cheerio.load(html) : null; //deprecated
69
+ const root = html && settings.parseHtml ? parse(html) : null;
70
+ log('DOM root', root ? rootInfo(root) : null);
71
+ return { browser, page, response, status, location, title, html, $, root };
76
72
  }
77
73
  catch (error) {
78
74
  const status = browser.isConnected() ? 'connected' : 'not connected';
@@ -80,15 +76,13 @@ const browserReady = {
80
76
  console.log(error);
81
77
  throw error;
82
78
  }
83
- });
79
+ };
84
80
  return web;
85
81
  },
86
- close(web) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- if (web && web.browser)
89
- yield web.browser.close();
90
- return web;
91
- });
82
+ async close(web) {
83
+ if (web && web.browser)
84
+ await web.browser.close();
85
+ return web;
92
86
  },
93
87
  };
94
88
  export { browserReady };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "puppeteer-browser-ready",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Simple utility to go to a URL and wait for the HTTP response",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -67,46 +67,49 @@
67
67
  "clean": [
68
68
  "rimraf build dist"
69
69
  ],
70
- "build": [
70
+ "lint": [
71
71
  "jshint . --exclude-path .gitignore",
72
- "eslint --max-warnings 0 . --ext .ts",
72
+ "eslint --max-warnings 0 . --ext .ts"
73
+ ],
74
+ "build": [
73
75
  "tsc",
74
76
  "add-dist-header build dist",
75
77
  "html-validator"
76
78
  ]
77
79
  },
78
80
  "scripts": {
79
- "pretest": "run-scripts clean build",
80
- "test": "mocha spec/*.spec.js --timeout 5000 --retries 1"
81
+ "pretest": "run-scripts clean lint build",
82
+ "test": "mocha spec/*.spec.js --timeout 7000 --retries 1"
81
83
  },
82
84
  "peerDependencies": {
83
- "puppeteer": "^10 || ^11 || ^12 || ^13 || ^14 || ^15 || ^16 || ^17 || ^18 || ^19.7 || ^20"
85
+ "puppeteer": "^15 || ^16 || ^17 || ^18 || ^19.7 || ^20 || ^21"
84
86
  },
85
87
  "dependencies": {
86
88
  "cheerio": "~1.0.0-rc.12",
87
89
  "express": "~4.18",
88
- "http-terminator": "~3.2"
90
+ "http-terminator": "~3.2",
91
+ "node-html-parser": "~6.1"
89
92
  },
90
93
  "devDependencies": {
91
94
  "@types/cheerio": "~0.22",
92
95
  "@types/express": "~4.17",
93
96
  "@types/mocha": "~10.0",
94
- "@types/node": "~20.3",
97
+ "@types/node": "~20.6",
95
98
  "@types/ws": "~8.5",
96
- "@typescript-eslint/eslint-plugin": "~5.60",
97
- "@typescript-eslint/parser": "~5.60",
98
- "add-dist-header": "~1.1",
99
+ "@typescript-eslint/eslint-plugin": "~6.7",
100
+ "@typescript-eslint/parser": "~6.7",
101
+ "add-dist-header": "~1.3",
99
102
  "assert-deep-strict-equal": "~1.1",
100
103
  "copy-file-util": "~1.1",
101
104
  "copy-folder-util": "~1.1",
102
- "eslint": "~8.43",
105
+ "eslint": "~8.49",
103
106
  "jshint": "~2.13",
104
107
  "mocha": "~10.2",
105
108
  "open": "~9.1",
106
- "puppeteer": "~20.7",
109
+ "puppeteer": "~21.2",
107
110
  "rimraf": "~5.0",
108
111
  "run-scripts-util": "~1.2",
109
- "typescript": "~5.1",
112
+ "typescript": "~5.2",
110
113
  "w3c-html-validator": "~1.4"
111
114
  }
112
115
  }