graceful-playwright 0.0.0 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) [2024], [Beeno Tung (Tung Cheung Leong)]
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # graceful-playwright
2
+
3
+ Gracefully handle timeout and network error with auto retry.
4
+
5
+ [![npm Package Version](https://img.shields.io/npm/v/graceful-playwright)](https://www.npmjs.com/package/graceful-playwright)
6
+
7
+ ## Features
8
+
9
+ - auto retry when `page.goto()` timeout or encountered `/ERR_NETWORK_CHANGED/i`
10
+
11
+ - auto restart page when `page.goto()` crashed with `/page crashed/i` error
12
+
13
+ - support restarting page from `Browser` or `BrowserContext` instance
14
+
15
+ - support wrapping existing `Page` instance
16
+
17
+ - proxy frequently used methods
18
+
19
+ - create `Page` instance lazily (on-demand)
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install graceful-playwright
25
+ ```
26
+
27
+ You can install the package with yarn, pnpm or slnpm as well.
28
+
29
+ ## Usage Example
30
+
31
+ More usage examples see: [example.ts](./example.ts) and [core.spec.ts](./core.spec.ts)
32
+
33
+ ```typescript
34
+ import { GracefulPage } from 'graceful-playwright'
35
+
36
+ let browser = await chromium.launch()
37
+ let page = new GracefulPage({ from: browser })
38
+
39
+ await page.goto('http://example.net')
40
+ let lines: string[] = await page.evaluate(() =>
41
+ Array.from(document.querySelectorAll('a'), a => a.href),
42
+ )
43
+ console.log('lines:', lines)
44
+
45
+ await page.close()
46
+ await browser.close()
47
+ ```
48
+
49
+ ## License
50
+
51
+ This project is licensed with [BSD-2-Clause](./LICENSE)
52
+
53
+ This is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):
54
+
55
+ - The freedom to run the program as you wish, for any purpose
56
+ - The freedom to study how the program works, and change it so it does your computing as you wish
57
+ - The freedom to redistribute copies so you can help others
58
+ - The freedom to distribute copies of your modified versions to others
package/core.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { Browser, BrowserContext, Page } from 'playwright';
2
+ export declare class GracefulPage {
3
+ options: {
4
+ from: Browser | BrowserContext;
5
+ page?: Page | Promise<Page>;
6
+ /**
7
+ * @default 5000 ms
8
+ */
9
+ retryInterval?: number;
10
+ /**
11
+ * @default error => console.error(error)
12
+ */
13
+ onError?: (error: unknown) => void;
14
+ };
15
+ constructor(options: {
16
+ from: Browser | BrowserContext;
17
+ page?: Page | Promise<Page>;
18
+ /**
19
+ * @default 5000 ms
20
+ */
21
+ retryInterval?: number;
22
+ /**
23
+ * @default error => console.error(error)
24
+ */
25
+ onError?: (error: unknown) => void;
26
+ });
27
+ fork(): GracefulPage;
28
+ getRetryInterval(): number;
29
+ getOnError(): (error: unknown) => void;
30
+ getPage(): Page | Promise<Page>;
31
+ restart(options?: Parameters<Page['close']>[0]): Promise<Page>;
32
+ goto(url: string,
33
+ /**
34
+ * @default { waitUtil: "domcontentloaded" }
35
+ */
36
+ options?: Parameters<Page['goto']>[1]): Promise<import("playwright").Response | null>;
37
+ /** @description proxy method to (await this.getPage()).evaluate */
38
+ evaluate: Page['evaluate'];
39
+ /** @description proxy method to (await this.getPage()).fill */
40
+ fill: Page['fill'];
41
+ /** @description proxy method to (await this.getPage()).click */
42
+ click: Page['click'];
43
+ /** @description proxy method to (await this.getPage()).content */
44
+ content: Page['content'];
45
+ /** @description proxy method to (await this.getPage()).title */
46
+ title: Page['title'];
47
+ /** @description proxy method to (await this.getPage()).innerHTML */
48
+ innerHTML: Page['innerHTML'];
49
+ /** @description proxy method to (await this.getPage()).innerText */
50
+ innerText: Page['innerText'];
51
+ /** @description proxy method to (await this.getPage()).click */
52
+ close: Page['close'];
53
+ }
package/core.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GracefulPage = void 0;
4
+ class GracefulPage {
5
+ options;
6
+ constructor(options) {
7
+ this.options = options;
8
+ }
9
+ fork() {
10
+ let { page, ...options } = this.options;
11
+ return new GracefulPage(options);
12
+ }
13
+ getRetryInterval() {
14
+ return this.options.retryInterval || 5000;
15
+ }
16
+ getOnError() {
17
+ return this.options.onError || ((error) => console.error(error));
18
+ }
19
+ getPage() {
20
+ this.options.page ||= this.options.from.newPage();
21
+ return this.options.page;
22
+ }
23
+ async restart(options) {
24
+ await this.close(options);
25
+ return this.getPage();
26
+ }
27
+ async goto(url,
28
+ /**
29
+ * @default { waitUtil: "domcontentloaded" }
30
+ */
31
+ options) {
32
+ for (;;) {
33
+ let page = await this.getPage();
34
+ try {
35
+ let response = await page.goto(url, {
36
+ waitUntil: 'domcontentloaded',
37
+ ...options,
38
+ });
39
+ return response;
40
+ }
41
+ catch (error) {
42
+ let message = String(error);
43
+ let flags = {
44
+ retry: message.match(/timeout/i) || message.match(/ERR_NETWORK_CHANGED/i),
45
+ restart: message.match(/page crashed/i),
46
+ };
47
+ if (flags.retry || flags.restart) {
48
+ if (flags.restart) {
49
+ await this.restart();
50
+ }
51
+ await sleep(this.getRetryInterval());
52
+ continue;
53
+ }
54
+ throw error;
55
+ }
56
+ }
57
+ }
58
+ /** @description proxy method to (await this.getPage()).evaluate */
59
+ evaluate = async (pageFunction, arg) => {
60
+ let page = await this.getPage();
61
+ return await page.evaluate(pageFunction, arg);
62
+ };
63
+ /** @description proxy method to (await this.getPage()).fill */
64
+ fill = async (selector, value, options) => {
65
+ let page = await this.getPage();
66
+ return await page.fill(selector, value, options);
67
+ };
68
+ /** @description proxy method to (await this.getPage()).click */
69
+ click = async (selector, options) => {
70
+ let page = await this.getPage();
71
+ return await page.click(selector, options);
72
+ };
73
+ /** @description proxy method to (await this.getPage()).content */
74
+ content = async () => {
75
+ let page = await this.getPage();
76
+ return await page.content();
77
+ };
78
+ /** @description proxy method to (await this.getPage()).title */
79
+ title = async () => {
80
+ let page = await this.getPage();
81
+ return await page.title();
82
+ };
83
+ /** @description proxy method to (await this.getPage()).innerHTML */
84
+ innerHTML = async (selector, options) => {
85
+ let page = await this.getPage();
86
+ return await page.innerHTML(selector, options);
87
+ };
88
+ /** @description proxy method to (await this.getPage()).innerText */
89
+ innerText = async (selector, options) => {
90
+ let page = await this.getPage();
91
+ return await page.innerText(selector, options);
92
+ };
93
+ /** @description proxy method to (await this.getPage()).click */
94
+ close = async (options) => {
95
+ let promise = Promise.resolve(this.options.page)
96
+ .then(page => page?.close(options))
97
+ .catch(this.getOnError());
98
+ this.options.page = undefined;
99
+ return promise;
100
+ };
101
+ }
102
+ exports.GracefulPage = GracefulPage;
103
+ function sleep(ms) {
104
+ return new Promise(resolve => setTimeout(resolve, ms));
105
+ }
package/package.json CHANGED
@@ -1,17 +1,65 @@
1
1
  {
2
2
  "name": "graceful-playwright",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "1.0.0",
4
+ "description": "Gracefully handle timeout and network error with auto retry.",
5
+ "keywords": [
6
+ "graceful",
7
+ "retry",
8
+ "retries",
9
+ "error",
10
+ "errors",
11
+ "handling",
12
+ "timeout",
13
+ "ERR_NETWORK_CHANGED",
14
+ "page",
15
+ "crashed",
16
+ "goto",
17
+ "playwright",
18
+ "browser",
19
+ "automation",
20
+ "scrapping",
21
+ "crawling",
22
+ "crawler",
23
+ "typescript"
24
+ ],
25
+ "author": "Beeno Tung <aabbcc1241@yahoo.com.hk> (https://beeno-tung.surge.sh)",
26
+ "license": "BSD-2-Clause",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/beenotung/graceful-playwright.git"
30
+ },
31
+ "homepage": "https://github.com/beenotung/graceful-playwright#readme",
32
+ "bugs": {
33
+ "url": "https://github.com/beenotung/graceful-playwright/issues"
34
+ },
35
+ "main": "core.js",
36
+ "types": "core.d.ts",
37
+ "files": [
38
+ "core.js",
39
+ "core.d.ts"
40
+ ],
6
41
  "scripts": {
7
- "test": "tsc --noEmit",
8
- "build": "tsc -p ."
42
+ "clean": "rimraf *.js *.d.ts",
43
+ "test": "run-p build mocha",
44
+ "build": "run-s clean tsc",
45
+ "tsc": "tsc -p .",
46
+ "mocha": "ts-mocha core.spec.ts"
47
+ },
48
+ "peerDependencies": {
49
+ "playwright": "^1.41.2"
9
50
  },
10
- "keywords": [],
11
- "author": "",
12
- "license": "ISC",
13
51
  "devDependencies": {
52
+ "@types/chai": "4",
53
+ "@types/express": "^4.17.21",
54
+ "@types/mocha": "^10.0.6",
14
55
  "@types/node": "^20.11.17",
56
+ "chai": "4",
57
+ "express": "^4.18.2",
58
+ "mocha": "^10.3.0",
59
+ "npm-run-all": "^4.1.5",
60
+ "playwright": "^1.41.2",
61
+ "rimraf": "^5.0.5",
62
+ "ts-mocha": "^10.0.0",
15
63
  "ts-node": "^10.9.2",
16
64
  "ts-node-dev": "^2.0.0",
17
65
  "typescript": "^5.3.3"
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2022",
4
- "module": "commonjs",
5
- "esModuleInterop": true,
6
- "forceConsistentCasingInFileNames": true,
7
- "strict": true,
8
- "skipLibCheck": true,
9
- "incremental": true,
10
- "outDir": "dist"
11
- },
12
- "exclude": [
13
- "dist"
14
- ]
15
- }