puppeteer-browser-ready 0.4.5 → 0.5.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/README.md
CHANGED
|
@@ -26,22 +26,35 @@ import { browserReady } from 'puppeteer-browser-ready';
|
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
## B) Usage
|
|
29
|
-
Use the `browserReady.goto()` function to tell Puppeteer which page to open.
|
|
30
|
-
resolve with a **Web** object containing a `title` field and a `html` field.
|
|
31
|
-
object to the `browserReady.close()` function to disconnect the page.
|
|
29
|
+
Use the `browserReady.goto(url, options)` function to tell Puppeteer which page to open.
|
|
30
|
+
The **Promise** will resolve with a **Web** object containing a `title` field and a `html` field.
|
|
31
|
+
Pass the **Web** object to the `browserReady.close(web)` function to disconnect the page.
|
|
32
32
|
```javascript
|
|
33
33
|
const url = 'https://pretty-print-json.js.org/';
|
|
34
34
|
let web; //fields: browser, page, response, status, location, title, html, $
|
|
35
35
|
before(async () => web = await puppeteer.launch().then(browserReady.goto(url));
|
|
36
36
|
after(async () => await browserReady.close(web));
|
|
37
37
|
```
|
|
38
|
+
### `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. |
|
|
43
|
+
|
|
44
|
+
### `startWebServer()` Options
|
|
45
|
+
| Name (key) | Type | Default| Description |
|
|
46
|
+
| :------------ | :---------- | :----- | :----------------------------------------------- |
|
|
47
|
+
| `autoCleanup` | **boolean** | `true` | Terminate connection on interruption (`SIGINT`). |
|
|
48
|
+
| `folder` | **string** | `'.'` | Document root for the static web server. |
|
|
49
|
+
| `port` | **number** | `0` | Port number for server (`0` find open port). |
|
|
50
|
+
| `verbose` | **boolean** | `true` | Output informational messages. |
|
|
38
51
|
|
|
39
52
|
## C) TypeScript Declarations
|
|
40
53
|
The **TypeScript Declaration File** file is
|
|
41
54
|
[puppeteer-browser-ready.d.ts](dist/puppeteer-browser-ready.d.ts) in the **dist** folder.
|
|
42
55
|
|
|
43
|
-
The `browserReady.goto()` function returns a function that takes a Puppeteer **Browser**
|
|
44
|
-
returns a **Promise** that resolves with a **Web** object:
|
|
56
|
+
The `browserReady.goto(url, options)` function returns a function that takes a Puppeteer **Browser**
|
|
57
|
+
object and returns a **Promise** that resolves with a **Web** object:
|
|
45
58
|
```typescript
|
|
46
59
|
type Web = {
|
|
47
60
|
browser: Puppeteer.Browser,
|
|
@@ -54,8 +67,8 @@ type Web = {
|
|
|
54
67
|
};
|
|
55
68
|
```
|
|
56
69
|
|
|
57
|
-
The optional `browserReady.startWebServer()` function starts a static web server and returns
|
|
58
|
-
**Promise** for when the [server](spec/start-web-server.spec.js) is ready:
|
|
70
|
+
The optional `browserReady.startWebServer(options)` function starts a static web server and returns
|
|
71
|
+
a **Promise** for when the [server](spec/start-web-server.spec.js) is ready:
|
|
59
72
|
```typescript
|
|
60
73
|
export type Http = {
|
|
61
74
|
server: Server,
|
|
@@ -69,7 +82,7 @@ export type Http = {
|
|
|
69
82
|
|
|
70
83
|
## D) Examples
|
|
71
84
|
|
|
72
|
-
### Example: Node.js program
|
|
85
|
+
### Example 1: Node.js program
|
|
73
86
|
**Code:**
|
|
74
87
|
```javascript
|
|
75
88
|
import puppeteer from 'puppeteer';
|
|
@@ -95,7 +108,7 @@ The HTML from https://pretty-print-json.js.org/ is 8200 characters
|
|
|
95
108
|
long and contains 7 <p> tags.
|
|
96
109
|
```
|
|
97
110
|
|
|
98
|
-
### Example: Mocha specification suite
|
|
111
|
+
### Example 2: Mocha specification suite
|
|
99
112
|
**Code:**
|
|
100
113
|
```javascript
|
|
101
114
|
// Mocha Specification Suite
|
|
@@ -159,9 +172,9 @@ describe('The document content', () => {
|
|
|
159
172
|
✓ has a 🚀 traveling to 🪐!
|
|
160
173
|
```
|
|
161
174
|
|
|
162
|
-
### Example: Start and shutdown a static web server
|
|
163
|
-
The [startWebServer() and shutdownWebServer()](spec/start-web-server.spec.js) functions
|
|
164
|
-
in global fixtures to start and shutdown a static web server.
|
|
175
|
+
### Example 3: Start and shutdown a static web server
|
|
176
|
+
The [startWebServer(options) and shutdownWebServer(http)](spec/start-web-server.spec.js) functions
|
|
177
|
+
can be used in global fixtures to start and shutdown a static web server.
|
|
165
178
|
|
|
166
179
|
For example, the **spec/fixtures/setup-teardown.js** file below starts a web server on port `7123`
|
|
167
180
|
with the web root pointed to the project's **docs** folder.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v0.
|
|
1
|
+
//! puppeteer-browser-ready v0.5.0 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
/// <reference types="cheerio" />
|
|
4
4
|
import httpTerminator from 'http-terminator';
|
|
@@ -30,7 +30,7 @@ export declare type Web = {
|
|
|
30
30
|
};
|
|
31
31
|
export declare type BrowserReadyOptions = {
|
|
32
32
|
addCheerio?: boolean;
|
|
33
|
-
|
|
33
|
+
verbose?: boolean;
|
|
34
34
|
};
|
|
35
35
|
declare const browserReady: {
|
|
36
36
|
log(...args: unknown[]): void;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v0.
|
|
1
|
+
//! puppeteer-browser-ready v0.5.0 ~~ 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
12
|
// Imports
|
|
4
13
|
import cheerio from 'cheerio';
|
|
5
14
|
import express from 'express';
|
|
@@ -12,7 +21,7 @@ const browserReady = {
|
|
|
12
21
|
},
|
|
13
22
|
startWebServer(options) {
|
|
14
23
|
const defaults = { folder: '.', port: 0, verbose: true, autoCleanup: true };
|
|
15
|
-
const settings = {
|
|
24
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
16
25
|
const server = express().use(express.static(settings.folder)).listen(settings.port);
|
|
17
26
|
const terminator = httpTerminator.createHttpTerminator({ server });
|
|
18
27
|
const port = () => server.address().port;
|
|
@@ -43,24 +52,27 @@ const browserReady = {
|
|
|
43
52
|
return http.terminator.terminate();
|
|
44
53
|
},
|
|
45
54
|
goto(url, options) {
|
|
46
|
-
const defaults = { addCheerio: true,
|
|
47
|
-
const settings = {
|
|
48
|
-
const log = (
|
|
49
|
-
console.log(' ', Date.now() % 100000,
|
|
50
|
-
const web =
|
|
55
|
+
const defaults = { addCheerio: true, verbose: false };
|
|
56
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
57
|
+
const log = (label, msg) => settings.verbose &&
|
|
58
|
+
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
59
|
+
const web = (browser) => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
log('Connected', browser.isConnected());
|
|
51
61
|
try {
|
|
52
|
-
const page =
|
|
53
|
-
log(
|
|
54
|
-
const response =
|
|
55
|
-
log(
|
|
62
|
+
const page = yield browser.newPage();
|
|
63
|
+
log('Page....', url);
|
|
64
|
+
const response = yield page.goto(url);
|
|
65
|
+
log('Response', response.url());
|
|
56
66
|
const status = response && response.status();
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
log('Status', status);
|
|
68
|
+
const location = yield page.evaluate(() => globalThis.location);
|
|
69
|
+
log('Host', location.host);
|
|
70
|
+
const title = response && (yield page.title());
|
|
71
|
+
log('Title', title);
|
|
72
|
+
const html = response && (yield response.text());
|
|
73
|
+
log('Bytes', html.length);
|
|
62
74
|
const $ = html && settings.addCheerio ? cheerio.load(html) : null;
|
|
63
|
-
log($ && $['fn']);
|
|
75
|
+
log('$', $ && $['fn'].constructor.name);
|
|
64
76
|
return { browser, page, response, status, location, title, html, $ };
|
|
65
77
|
}
|
|
66
78
|
catch (error) {
|
|
@@ -69,13 +81,15 @@ const browserReady = {
|
|
|
69
81
|
console.log(error);
|
|
70
82
|
throw error;
|
|
71
83
|
}
|
|
72
|
-
};
|
|
84
|
+
});
|
|
73
85
|
return web;
|
|
74
86
|
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
close(web) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (web && web.browser)
|
|
90
|
+
yield web.browser.close();
|
|
91
|
+
return web;
|
|
92
|
+
});
|
|
79
93
|
},
|
|
80
94
|
};
|
|
81
95
|
export { browserReady };
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v0.
|
|
1
|
+
//! puppeteer-browser-ready v0.5.0 ~~ 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
12
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
13
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
14
|
};
|
|
@@ -27,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
36
|
},
|
|
28
37
|
startWebServer(options) {
|
|
29
38
|
const defaults = { folder: '.', port: 0, verbose: true, autoCleanup: true };
|
|
30
|
-
const settings = {
|
|
39
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
31
40
|
const server = (0, express_1.default)().use(express_1.default.static(settings.folder)).listen(settings.port);
|
|
32
41
|
const terminator = http_terminator_1.default.createHttpTerminator({ server });
|
|
33
42
|
const port = () => server.address().port;
|
|
@@ -58,24 +67,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
58
67
|
return http.terminator.terminate();
|
|
59
68
|
},
|
|
60
69
|
goto(url, options) {
|
|
61
|
-
const defaults = { addCheerio: true,
|
|
62
|
-
const settings = {
|
|
63
|
-
const log = (
|
|
64
|
-
console.log(' ', Date.now() % 100000,
|
|
65
|
-
const web =
|
|
70
|
+
const defaults = { addCheerio: true, verbose: false };
|
|
71
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
72
|
+
const log = (label, msg) => settings.verbose &&
|
|
73
|
+
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
74
|
+
const web = (browser) => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
log('Connected', browser.isConnected());
|
|
66
76
|
try {
|
|
67
|
-
const page =
|
|
68
|
-
log(
|
|
69
|
-
const response =
|
|
70
|
-
log(
|
|
77
|
+
const page = yield browser.newPage();
|
|
78
|
+
log('Page....', url);
|
|
79
|
+
const response = yield page.goto(url);
|
|
80
|
+
log('Response', response.url());
|
|
71
81
|
const status = response && response.status();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
log('Status', status);
|
|
83
|
+
const location = yield page.evaluate(() => globalThis.location);
|
|
84
|
+
log('Host', location.host);
|
|
85
|
+
const title = response && (yield page.title());
|
|
86
|
+
log('Title', title);
|
|
87
|
+
const html = response && (yield response.text());
|
|
88
|
+
log('Bytes', html.length);
|
|
77
89
|
const $ = html && settings.addCheerio ? cheerio_1.default.load(html) : null;
|
|
78
|
-
log($ && $['fn']);
|
|
90
|
+
log('$', $ && $['fn'].constructor.name);
|
|
79
91
|
return { browser, page, response, status, location, title, html, $ };
|
|
80
92
|
}
|
|
81
93
|
catch (error) {
|
|
@@ -84,13 +96,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
84
96
|
console.log(error);
|
|
85
97
|
throw error;
|
|
86
98
|
}
|
|
87
|
-
};
|
|
99
|
+
});
|
|
88
100
|
return web;
|
|
89
101
|
},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
close(web) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
if (web && web.browser)
|
|
105
|
+
yield web.browser.close();
|
|
106
|
+
return web;
|
|
107
|
+
});
|
|
94
108
|
},
|
|
95
109
|
};
|
|
96
110
|
exports.browserReady = browserReady;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "puppeteer-browser-ready",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Simple utility to go to a URL and wait for the HTTP response (written in TypeScript)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"step:03": "eslint --max-warnings 0 . --ext .ts",
|
|
71
71
|
"step:04": "tsc",
|
|
72
72
|
"step:05": "tsc --module UMD --outDir build/umd",
|
|
73
|
-
"step:06": "cpy build/umd/puppeteer-browser-ready.js build --rename=puppeteer-browser-ready.umd.cjs",
|
|
73
|
+
"step:06": "cpy build/umd/puppeteer-browser-ready.js build --rename=puppeteer-browser-ready.umd.cjs --flat=true",
|
|
74
74
|
"step:07": "add-dist-header build dist",
|
|
75
75
|
"step:08": "w3c-html-validator",
|
|
76
76
|
"pretest": "npm-run-all step:*",
|
|
@@ -82,25 +82,25 @@
|
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"cheerio": "~1.0.0-rc.10",
|
|
84
84
|
"express": "~4.17",
|
|
85
|
-
"http-terminator": "~3.
|
|
85
|
+
"http-terminator": "~3.2"
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
88
|
"@types/cheerio": "~0.22",
|
|
89
89
|
"@types/express": "~4.17",
|
|
90
90
|
"@types/node": "~17.0",
|
|
91
|
-
"@typescript-eslint/eslint-plugin": "~5.
|
|
92
|
-
"@typescript-eslint/parser": "~5.
|
|
91
|
+
"@typescript-eslint/eslint-plugin": "~5.16",
|
|
92
|
+
"@typescript-eslint/parser": "~5.16",
|
|
93
93
|
"add-dist-header": "~0.1",
|
|
94
94
|
"assert-deep-strict-equal": "~1.0",
|
|
95
|
-
"cpy-cli": "~
|
|
96
|
-
"eslint": "~8.
|
|
95
|
+
"cpy-cli": "~4.1",
|
|
96
|
+
"eslint": "~8.11",
|
|
97
97
|
"jshint": "~2.13",
|
|
98
98
|
"mocha": "~9.2",
|
|
99
99
|
"npm-run-all2": "~5.0",
|
|
100
100
|
"open": "~8.4",
|
|
101
|
-
"puppeteer": "~13.
|
|
101
|
+
"puppeteer": "~13.5",
|
|
102
102
|
"rimraf": "~3.0",
|
|
103
|
-
"typescript": "~4.
|
|
103
|
+
"typescript": "~4.6",
|
|
104
104
|
"w3c-html-validator": "~1.0"
|
|
105
105
|
}
|
|
106
106
|
}
|