puppeteer-browser-ready 0.4.7 → 0.5.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
|
@@ -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.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
/// <reference types="cheerio" />
|
|
4
4
|
import httpTerminator from 'http-terminator';
|
|
@@ -24,19 +24,19 @@ export declare type Web = {
|
|
|
24
24
|
response: HTTPResponse | null;
|
|
25
25
|
status: number | null;
|
|
26
26
|
location: Location;
|
|
27
|
-
title: string;
|
|
28
|
-
html: string;
|
|
27
|
+
title: string | null;
|
|
28
|
+
html: string | null;
|
|
29
29
|
$: cheerio.Root | null;
|
|
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;
|
|
37
|
-
startWebServer(options?: StartWebServerOptions
|
|
37
|
+
startWebServer(options?: StartWebServerOptions): Promise<Http>;
|
|
38
38
|
shutdownWebServer(http: Http): Promise<void>;
|
|
39
|
-
goto(url: string, options?: BrowserReadyOptions
|
|
39
|
+
goto(url: string, options?: BrowserReadyOptions): (browser: Browser) => Promise<Web>;
|
|
40
40
|
close(web: Web): Promise<Web>;
|
|
41
41
|
};
|
|
42
42
|
export { browserReady };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v0.
|
|
1
|
+
//! puppeteer-browser-ready v0.5.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -52,9 +52,9 @@ const browserReady = {
|
|
|
52
52
|
return http.terminator.terminate();
|
|
53
53
|
},
|
|
54
54
|
goto(url, options) {
|
|
55
|
-
const defaults = { addCheerio: true,
|
|
55
|
+
const defaults = { addCheerio: true, verbose: false };
|
|
56
56
|
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
57
|
-
const log = (label, msg) => settings.
|
|
57
|
+
const log = (label, msg) => settings.verbose &&
|
|
58
58
|
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
59
59
|
const web = (browser) => __awaiter(this, void 0, void 0, function* () {
|
|
60
60
|
log('Connected', browser.isConnected());
|
|
@@ -62,7 +62,7 @@ const browserReady = {
|
|
|
62
62
|
const page = yield browser.newPage();
|
|
63
63
|
log('Page....', url);
|
|
64
64
|
const response = yield page.goto(url);
|
|
65
|
-
log('Response', response.url());
|
|
65
|
+
log('Response', response === null || response === void 0 ? void 0 : response.url());
|
|
66
66
|
const status = response && response.status();
|
|
67
67
|
log('Status', status);
|
|
68
68
|
const location = yield page.evaluate(() => globalThis.location);
|
|
@@ -70,7 +70,7 @@ const browserReady = {
|
|
|
70
70
|
const title = response && (yield page.title());
|
|
71
71
|
log('Title', title);
|
|
72
72
|
const html = response && (yield response.text());
|
|
73
|
-
log('Bytes', html.length);
|
|
73
|
+
log('Bytes', html === null || html === void 0 ? void 0 : html.length);
|
|
74
74
|
const $ = html && settings.addCheerio ? cheerio.load(html) : null;
|
|
75
75
|
log('$', $ && $['fn'].constructor.name);
|
|
76
76
|
return { browser, page, response, status, location, title, html, $ };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v0.
|
|
1
|
+
//! puppeteer-browser-ready v0.5.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -67,9 +67,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
67
67
|
return http.terminator.terminate();
|
|
68
68
|
},
|
|
69
69
|
goto(url, options) {
|
|
70
|
-
const defaults = { addCheerio: true,
|
|
70
|
+
const defaults = { addCheerio: true, verbose: false };
|
|
71
71
|
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
72
|
-
const log = (label, msg) => settings.
|
|
72
|
+
const log = (label, msg) => settings.verbose &&
|
|
73
73
|
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
74
74
|
const web = (browser) => __awaiter(this, void 0, void 0, function* () {
|
|
75
75
|
log('Connected', browser.isConnected());
|
|
@@ -77,7 +77,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
77
77
|
const page = yield browser.newPage();
|
|
78
78
|
log('Page....', url);
|
|
79
79
|
const response = yield page.goto(url);
|
|
80
|
-
log('Response', response.url());
|
|
80
|
+
log('Response', response === null || response === void 0 ? void 0 : response.url());
|
|
81
81
|
const status = response && response.status();
|
|
82
82
|
log('Status', status);
|
|
83
83
|
const location = yield page.evaluate(() => globalThis.location);
|
|
@@ -85,7 +85,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
85
85
|
const title = response && (yield page.title());
|
|
86
86
|
log('Title', title);
|
|
87
87
|
const html = response && (yield response.text());
|
|
88
|
-
log('Bytes', html.length);
|
|
88
|
+
log('Bytes', html === null || html === void 0 ? void 0 : html.length);
|
|
89
89
|
const $ = html && settings.addCheerio ? cheerio_1.default.load(html) : null;
|
|
90
90
|
log('$', $ && $['fn'].constructor.name);
|
|
91
91
|
return { browser, page, response, status, location, title, html, $ };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "puppeteer-browser-ready",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.2",
|
|
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",
|
|
@@ -77,30 +77,30 @@
|
|
|
77
77
|
"test": "mocha spec/*.spec.js --timeout 5000"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
|
-
"puppeteer": "^5 || ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || ^13"
|
|
80
|
+
"puppeteer": "^5 || ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || ^13 || ^14"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"cheerio": "~1.0.0-rc.10",
|
|
84
|
-
"express": "~4.
|
|
84
|
+
"express": "~4.18",
|
|
85
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.27",
|
|
92
|
+
"@typescript-eslint/parser": "~5.27",
|
|
93
93
|
"add-dist-header": "~0.1",
|
|
94
94
|
"assert-deep-strict-equal": "~1.0",
|
|
95
95
|
"cpy-cli": "~4.1",
|
|
96
|
-
"eslint": "~8.
|
|
96
|
+
"eslint": "~8.16",
|
|
97
97
|
"jshint": "~2.13",
|
|
98
|
-
"mocha": "~
|
|
98
|
+
"mocha": "~10.0",
|
|
99
99
|
"npm-run-all2": "~5.0",
|
|
100
100
|
"open": "~8.4",
|
|
101
|
-
"puppeteer": "~
|
|
101
|
+
"puppeteer": "~14.2",
|
|
102
102
|
"rimraf": "~3.0",
|
|
103
|
-
"typescript": "~4.
|
|
103
|
+
"typescript": "~4.7",
|
|
104
104
|
"w3c-html-validator": "~1.0"
|
|
105
105
|
}
|
|
106
106
|
}
|